4 #include <util/bpf-loader.h>
5 #include <util/evlist.h>
7 #include <linux/filter.h>
14 #ifdef HAVE_LIBBPF_SUPPORT
16 static int epoll_pwait_loop(void)
20 /* Should fail NR_ITERS times */
21 for (i
= 0; i
< NR_ITERS
; i
++)
22 epoll_pwait(-(i
+ 1), NULL
, 0, 0, NULL
);
26 #ifdef HAVE_BPF_PROLOGUE
28 static int llseek_loop(void)
32 fds
[0] = open("/dev/null", O_RDONLY
);
33 fds
[1] = open("/dev/null", O_RDWR
);
35 if (fds
[0] < 0 || fds
[1] < 0)
38 for (i
= 0; i
< NR_ITERS
; i
++) {
39 lseek(fds
[i
% 2], i
, (i
/ 2) % 2 ? SEEK_CUR
: SEEK_SET
);
40 lseek(fds
[(i
+ 1) % 2], i
, (i
/ 2) % 2 ? SEEK_CUR
: SEEK_SET
);
50 enum test_llvm__testcase prog_id
;
53 const char *msg_compile_fail
;
54 const char *msg_load_fail
;
55 int (*target_func
)(void);
57 } bpf_testcase_table
[] = {
60 "Test basic BPF filtering",
62 "fix 'perf test LLVM' first",
63 "load bpf object failed",
67 #ifdef HAVE_BPF_PROLOGUE
69 LLVM_TESTCASE_BPF_PROLOGUE
,
70 "Test BPF prologue generation",
71 "[bpf_prologue_test]",
73 "check your vmlinux setting?",
79 LLVM_TESTCASE_BPF_RELOCATION
,
80 "Test BPF relocation checker",
81 "[bpf_relocation_test]",
82 "fix 'perf test LLVM' first",
83 "libbpf error when dealing with relocation",
89 static int do_test(struct bpf_object
*obj
, int (*func
)(void),
92 struct record_opts opts
= {
99 .default_interval
= 1,
103 char sbuf
[STRERR_BUFSIZE
];
104 struct perf_evlist
*evlist
;
105 int i
, ret
= TEST_FAIL
, err
= 0, count
= 0;
107 struct parse_events_evlist parse_evlist
;
108 struct parse_events_error parse_error
;
110 bzero(&parse_error
, sizeof(parse_error
));
111 bzero(&parse_evlist
, sizeof(parse_evlist
));
112 parse_evlist
.error
= &parse_error
;
113 INIT_LIST_HEAD(&parse_evlist
.list
);
115 err
= parse_events_load_bpf_obj(&parse_evlist
, &parse_evlist
.list
, obj
, NULL
);
116 if (err
|| list_empty(&parse_evlist
.list
)) {
117 pr_debug("Failed to add events selected by BPF\n");
121 snprintf(pid
, sizeof(pid
), "%d", getpid());
122 pid
[sizeof(pid
) - 1] = '\0';
123 opts
.target
.tid
= opts
.target
.pid
= pid
;
125 /* Instead of perf_evlist__new_default, don't add default events */
126 evlist
= perf_evlist__new();
128 pr_debug("No ehough memory to create evlist\n");
132 err
= perf_evlist__create_maps(evlist
, &opts
.target
);
134 pr_debug("Not enough memory to create thread/cpu maps\n");
135 goto out_delete_evlist
;
138 perf_evlist__splice_list_tail(evlist
, &parse_evlist
.list
);
139 evlist
->nr_groups
= parse_evlist
.nr_groups
;
141 perf_evlist__config(evlist
, &opts
);
143 err
= perf_evlist__open(evlist
);
145 pr_debug("perf_evlist__open: %s\n",
146 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
147 goto out_delete_evlist
;
150 err
= perf_evlist__mmap(evlist
, opts
.mmap_pages
, false);
152 pr_debug("perf_evlist__mmap: %s\n",
153 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
154 goto out_delete_evlist
;
157 perf_evlist__enable(evlist
);
159 perf_evlist__disable(evlist
);
161 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
162 union perf_event
*event
;
164 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
165 const u32 type
= event
->header
.type
;
167 if (type
== PERF_RECORD_SAMPLE
)
172 if (count
!= expect
) {
173 pr_debug("BPF filter result incorrect\n");
174 goto out_delete_evlist
;
180 perf_evlist__delete(evlist
);
184 static struct bpf_object
*
185 prepare_bpf(void *obj_buf
, size_t obj_buf_sz
, const char *name
)
187 struct bpf_object
*obj
;
189 obj
= bpf__prepare_load_buffer(obj_buf
, obj_buf_sz
, name
);
191 pr_debug("Compile BPF program failed.\n");
197 static int __test__bpf(int idx
)
202 struct bpf_object
*obj
;
204 ret
= test_llvm__fetch_bpf_obj(&obj_buf
, &obj_buf_sz
,
205 bpf_testcase_table
[idx
].prog_id
,
207 if (ret
!= TEST_OK
|| !obj_buf
|| !obj_buf_sz
) {
208 pr_debug("Unable to get BPF object, %s\n",
209 bpf_testcase_table
[idx
].msg_compile_fail
);
216 obj
= prepare_bpf(obj_buf
, obj_buf_sz
,
217 bpf_testcase_table
[idx
].name
);
218 if ((!!bpf_testcase_table
[idx
].target_func
) != (!!obj
)) {
220 pr_debug("Fail to load BPF object: %s\n",
221 bpf_testcase_table
[idx
].msg_load_fail
);
223 pr_debug("Success unexpectedly: %s\n",
224 bpf_testcase_table
[idx
].msg_load_fail
);
231 bpf_testcase_table
[idx
].target_func
,
232 bpf_testcase_table
[idx
].expect_result
);
238 int test__bpf_subtest_get_nr(void)
240 return (int)ARRAY_SIZE(bpf_testcase_table
);
243 const char *test__bpf_subtest_get_desc(int i
)
245 if (i
< 0 || i
>= (int)ARRAY_SIZE(bpf_testcase_table
))
247 return bpf_testcase_table
[i
].desc
;
250 static int check_env(void)
253 unsigned int kver_int
;
254 char license
[] = "GPL";
256 struct bpf_insn insns
[] = {
257 BPF_MOV64_IMM(BPF_REG_0
, 1),
261 err
= fetch_kernel_version(&kver_int
, NULL
, 0);
263 pr_debug("Unable to get kernel version\n");
267 err
= bpf_load_program(BPF_PROG_TYPE_KPROBE
, insns
,
268 sizeof(insns
) / sizeof(insns
[0]),
269 license
, kver_int
, NULL
, 0);
271 pr_err("Missing basic BPF support, skip this test: %s\n",
284 if (i
< 0 || i
>= (int)ARRAY_SIZE(bpf_testcase_table
))
287 if (geteuid() != 0) {
288 pr_debug("Only root can run BPF test\n");
295 err
= __test__bpf(i
);
300 int test__bpf_subtest_get_nr(void)
305 const char *test__bpf_subtest_get_desc(int i __maybe_unused
)
310 int test__bpf(int i __maybe_unused
)
312 pr_debug("Skip BPF test because BPF support is not compiled\n");