7 #include <util/bpf-loader.h>
8 #include <util/evlist.h>
10 #include <linux/filter.h>
11 #include <linux/kernel.h>
12 #include <api/fs/fs.h>
18 #define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
20 #ifdef HAVE_LIBBPF_SUPPORT
22 static int epoll_wait_loop(void)
26 /* Should fail NR_ITERS times */
27 for (i
= 0; i
< NR_ITERS
; i
++)
28 epoll_wait(-(i
+ 1), NULL
, 0, 0);
32 #ifdef HAVE_BPF_PROLOGUE
34 static int llseek_loop(void)
38 fds
[0] = open("/dev/null", O_RDONLY
);
39 fds
[1] = open("/dev/null", O_RDWR
);
41 if (fds
[0] < 0 || fds
[1] < 0)
44 for (i
= 0; i
< NR_ITERS
; i
++) {
45 lseek(fds
[i
% 2], i
, (i
/ 2) % 2 ? SEEK_CUR
: SEEK_SET
);
46 lseek(fds
[(i
+ 1) % 2], i
, (i
/ 2) % 2 ? SEEK_CUR
: SEEK_SET
);
56 enum test_llvm__testcase prog_id
;
59 const char *msg_compile_fail
;
60 const char *msg_load_fail
;
61 int (*target_func
)(void);
64 } bpf_testcase_table
[] = {
67 "Basic BPF filtering",
69 "fix 'perf test LLVM' first",
70 "load bpf object failed",
80 "check your vmlinux setting?",
85 #ifdef HAVE_BPF_PROLOGUE
87 LLVM_TESTCASE_BPF_PROLOGUE
,
88 "BPF prologue generation",
89 "[bpf_prologue_test]",
91 "check your vmlinux setting?",
98 LLVM_TESTCASE_BPF_RELOCATION
,
99 "BPF relocation checker",
100 "[bpf_relocation_test]",
101 "fix 'perf test LLVM' first",
102 "libbpf error when dealing with relocation",
109 static int do_test(struct bpf_object
*obj
, int (*func
)(void),
112 struct record_opts opts
= {
119 .default_interval
= 1,
123 char sbuf
[STRERR_BUFSIZE
];
124 struct perf_evlist
*evlist
;
125 int i
, ret
= TEST_FAIL
, err
= 0, count
= 0;
127 struct parse_events_state parse_state
;
128 struct parse_events_error parse_error
;
130 bzero(&parse_error
, sizeof(parse_error
));
131 bzero(&parse_state
, sizeof(parse_state
));
132 parse_state
.error
= &parse_error
;
133 INIT_LIST_HEAD(&parse_state
.list
);
135 err
= parse_events_load_bpf_obj(&parse_state
, &parse_state
.list
, obj
, NULL
);
136 if (err
|| list_empty(&parse_state
.list
)) {
137 pr_debug("Failed to add events selected by BPF\n");
141 snprintf(pid
, sizeof(pid
), "%d", getpid());
142 pid
[sizeof(pid
) - 1] = '\0';
143 opts
.target
.tid
= opts
.target
.pid
= pid
;
145 /* Instead of perf_evlist__new_default, don't add default events */
146 evlist
= perf_evlist__new();
148 pr_debug("Not enough memory to create evlist\n");
152 err
= perf_evlist__create_maps(evlist
, &opts
.target
);
154 pr_debug("Not enough memory to create thread/cpu maps\n");
155 goto out_delete_evlist
;
158 perf_evlist__splice_list_tail(evlist
, &parse_state
.list
);
159 evlist
->nr_groups
= parse_state
.nr_groups
;
161 perf_evlist__config(evlist
, &opts
, NULL
);
163 err
= perf_evlist__open(evlist
);
165 pr_debug("perf_evlist__open: %s\n",
166 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
167 goto out_delete_evlist
;
170 err
= perf_evlist__mmap(evlist
, opts
.mmap_pages
, false);
172 pr_debug("perf_evlist__mmap: %s\n",
173 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
174 goto out_delete_evlist
;
177 perf_evlist__enable(evlist
);
179 perf_evlist__disable(evlist
);
181 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
182 union perf_event
*event
;
184 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
185 const u32 type
= event
->header
.type
;
187 if (type
== PERF_RECORD_SAMPLE
)
192 if (count
!= expect
) {
193 pr_debug("BPF filter result incorrect\n");
194 goto out_delete_evlist
;
200 perf_evlist__delete(evlist
);
204 static struct bpf_object
*
205 prepare_bpf(void *obj_buf
, size_t obj_buf_sz
, const char *name
)
207 struct bpf_object
*obj
;
209 obj
= bpf__prepare_load_buffer(obj_buf
, obj_buf_sz
, name
);
211 pr_debug("Compile BPF program failed.\n");
217 static int __test__bpf(int idx
)
222 struct bpf_object
*obj
;
224 ret
= test_llvm__fetch_bpf_obj(&obj_buf
, &obj_buf_sz
,
225 bpf_testcase_table
[idx
].prog_id
,
227 if (ret
!= TEST_OK
|| !obj_buf
|| !obj_buf_sz
) {
228 pr_debug("Unable to get BPF object, %s\n",
229 bpf_testcase_table
[idx
].msg_compile_fail
);
236 obj
= prepare_bpf(obj_buf
, obj_buf_sz
,
237 bpf_testcase_table
[idx
].name
);
238 if ((!!bpf_testcase_table
[idx
].target_func
) != (!!obj
)) {
240 pr_debug("Fail to load BPF object: %s\n",
241 bpf_testcase_table
[idx
].msg_load_fail
);
243 pr_debug("Success unexpectedly: %s\n",
244 bpf_testcase_table
[idx
].msg_load_fail
);
251 bpf_testcase_table
[idx
].target_func
,
252 bpf_testcase_table
[idx
].expect_result
);
255 if (bpf_testcase_table
[idx
].pin
) {
258 if (!bpf_fs__mount()) {
259 pr_debug("BPF filesystem not mounted\n");
263 err
= mkdir(PERF_TEST_BPF_PATH
, 0777);
264 if (err
&& errno
!= EEXIST
) {
265 pr_debug("Failed to make perf_test dir: %s\n",
270 if (bpf_object__pin(obj
, PERF_TEST_BPF_PATH
))
272 if (rm_rf(PERF_TEST_BPF_PATH
))
282 int test__bpf_subtest_get_nr(void)
284 return (int)ARRAY_SIZE(bpf_testcase_table
);
287 const char *test__bpf_subtest_get_desc(int i
)
289 if (i
< 0 || i
>= (int)ARRAY_SIZE(bpf_testcase_table
))
291 return bpf_testcase_table
[i
].desc
;
294 static int check_env(void)
297 unsigned int kver_int
;
298 char license
[] = "GPL";
300 struct bpf_insn insns
[] = {
301 BPF_MOV64_IMM(BPF_REG_0
, 1),
305 err
= fetch_kernel_version(&kver_int
, NULL
, 0);
307 pr_debug("Unable to get kernel version\n");
311 err
= bpf_load_program(BPF_PROG_TYPE_KPROBE
, insns
,
312 sizeof(insns
) / sizeof(insns
[0]),
313 license
, kver_int
, NULL
, 0);
315 pr_err("Missing basic BPF support, skip this test: %s\n",
324 int test__bpf(struct test
*test __maybe_unused
, int i
)
328 if (i
< 0 || i
>= (int)ARRAY_SIZE(bpf_testcase_table
))
331 if (geteuid() != 0) {
332 pr_debug("Only root can run BPF test\n");
339 err
= __test__bpf(i
);
344 int test__bpf_subtest_get_nr(void)
349 const char *test__bpf_subtest_get_desc(int i __maybe_unused
)
354 int test__bpf(struct test
*test __maybe_unused
, int i __maybe_unused
)
356 pr_debug("Skip BPF test because BPF support is not compiled\n");