1
2#include <test_progs.h>
3#include <network_helpers.h>
4
5#include "test_pkt_access.skel.h"
6
7static const __u32 duration;
8
9static void check_run_cnt(int prog_fd, __u64 run_cnt)
10{
11 struct bpf_prog_info info = {};
12 __u32 info_len = sizeof(info);
13 int err;
14
15 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
16 if (CHECK(err, "get_prog_info", "failed to get bpf_prog_info for fd %d\n", prog_fd))
17 return;
18
19 CHECK(run_cnt != info.run_cnt, "run_cnt",
20 "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt);
21}
22
23void test_prog_run_xattr(void)
24{
25 struct test_pkt_access *skel;
26 int err, stats_fd = -1;
27 char buf[10] = {};
28 __u64 run_cnt = 0;
29
30 struct bpf_prog_test_run_attr tattr = {
31 .repeat = 1,
32 .data_in = &pkt_v4,
33 .data_size_in = sizeof(pkt_v4),
34 .data_out = buf,
35 .data_size_out = 5,
36 };
37
38 stats_fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
39 if (CHECK_ATTR(stats_fd < 0, "enable_stats", "failed %d\n", errno))
40 return;
41
42 skel = test_pkt_access__open_and_load();
43 if (CHECK_ATTR(!skel, "open_and_load", "failed\n"))
44 goto cleanup;
45
46 tattr.prog_fd = bpf_program__fd(skel->progs.test_pkt_access);
47
48 err = bpf_prog_test_run_xattr(&tattr);
49 CHECK_ATTR(err >= 0 || errno != ENOSPC || tattr.retval, "run",
50 "err %d errno %d retval %d\n", err, errno, tattr.retval);
51
52 CHECK_ATTR(tattr.data_size_out != sizeof(pkt_v4), "data_size_out",
53 "incorrect output size, want %zu have %u\n",
54 sizeof(pkt_v4), tattr.data_size_out);
55
56 CHECK_ATTR(buf[5] != 0, "overflow",
57 "BPF_PROG_TEST_RUN ignored size hint\n");
58
59 run_cnt += tattr.repeat;
60 check_run_cnt(tattr.prog_fd, run_cnt);
61
62 tattr.data_out = NULL;
63 tattr.data_size_out = 0;
64 tattr.repeat = 2;
65 errno = 0;
66
67 err = bpf_prog_test_run_xattr(&tattr);
68 CHECK_ATTR(err || errno || tattr.retval, "run_no_output",
69 "err %d errno %d retval %d\n", err, errno, tattr.retval);
70
71 tattr.data_size_out = 1;
72 err = bpf_prog_test_run_xattr(&tattr);
73 CHECK_ATTR(err != -EINVAL, "run_wrong_size_out", "err %d\n", err);
74
75 run_cnt += tattr.repeat;
76 check_run_cnt(tattr.prog_fd, run_cnt);
77
78cleanup:
79 if (skel)
80 test_pkt_access__destroy(skel);
81 if (stats_fd >= 0)
82 close(stats_fd);
83}
84