Commit abab306f authored by Lorenz Bauer's avatar Lorenz Bauer Committed by Alexei Starovoitov

selftests: bpf: Check that PROG_TEST_RUN repeats as requested

Extend a simple prog_run test to check that PROG_TEST_RUN adheres
to the requested repetitions. Convert it to use BPF skeleton.
Signed-off-by: default avatarLorenz Bauer <lmb@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210303101816.36774-5-lmb@cloudflare.com
parent 509b2937
...@@ -2,12 +2,31 @@ ...@@ -2,12 +2,31 @@
#include <test_progs.h> #include <test_progs.h>
#include <network_helpers.h> #include <network_helpers.h>
void test_prog_run_xattr(void) #include "test_pkt_access.skel.h"
static const __u32 duration;
static void check_run_cnt(int prog_fd, __u64 run_cnt)
{ {
const char *file = "./test_pkt_access.o"; struct bpf_prog_info info = {};
struct bpf_object *obj; __u32 info_len = sizeof(info);
char buf[10];
int err; int err;
err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
if (CHECK(err, "get_prog_info", "failed to get bpf_prog_info for fd %d\n", prog_fd))
return;
CHECK(run_cnt != info.run_cnt, "run_cnt",
"incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
}
void test_prog_run_xattr(void)
{
struct test_pkt_access *skel;
int err, stats_fd = -1;
char buf[10] = {};
__u64 run_cnt = 0;
struct bpf_prog_test_run_attr tattr = { struct bpf_prog_test_run_attr tattr = {
.repeat = 1, .repeat = 1,
.data_in = &pkt_v4, .data_in = &pkt_v4,
...@@ -16,12 +35,15 @@ void test_prog_run_xattr(void) ...@@ -16,12 +35,15 @@ void test_prog_run_xattr(void)
.data_size_out = 5, .data_size_out = 5,
}; };
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, stats_fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
&tattr.prog_fd); if (CHECK_ATTR(stats_fd < 0, "enable_stats", "failed %d\n", errno))
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
return; return;
memset(buf, 0, sizeof(buf)); skel = test_pkt_access__open_and_load();
if (CHECK_ATTR(!skel, "open_and_load", "failed\n"))
goto cleanup;
tattr.prog_fd = bpf_program__fd(skel->progs.test_pkt_access);
err = bpf_prog_test_run_xattr(&tattr); err = bpf_prog_test_run_xattr(&tattr);
CHECK_ATTR(err != -1 || errno != ENOSPC || tattr.retval, "run", CHECK_ATTR(err != -1 || errno != ENOSPC || tattr.retval, "run",
...@@ -34,8 +56,12 @@ void test_prog_run_xattr(void) ...@@ -34,8 +56,12 @@ void test_prog_run_xattr(void)
CHECK_ATTR(buf[5] != 0, "overflow", CHECK_ATTR(buf[5] != 0, "overflow",
"BPF_PROG_TEST_RUN ignored size hint\n"); "BPF_PROG_TEST_RUN ignored size hint\n");
run_cnt += tattr.repeat;
check_run_cnt(tattr.prog_fd, run_cnt);
tattr.data_out = NULL; tattr.data_out = NULL;
tattr.data_size_out = 0; tattr.data_size_out = 0;
tattr.repeat = 2;
errno = 0; errno = 0;
err = bpf_prog_test_run_xattr(&tattr); err = bpf_prog_test_run_xattr(&tattr);
...@@ -46,5 +72,12 @@ void test_prog_run_xattr(void) ...@@ -46,5 +72,12 @@ void test_prog_run_xattr(void)
err = bpf_prog_test_run_xattr(&tattr); err = bpf_prog_test_run_xattr(&tattr);
CHECK_ATTR(err != -EINVAL, "run_wrong_size_out", "err %d\n", err); CHECK_ATTR(err != -EINVAL, "run_wrong_size_out", "err %d\n", err);
bpf_object__close(obj); run_cnt += tattr.repeat;
check_run_cnt(tattr.prog_fd, run_cnt);
cleanup:
if (skel)
test_pkt_access__destroy(skel);
if (stats_fd != -1)
close(stats_fd);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment