Loading...
1#include <stdio.h>
2#include <sys/epoll.h>
3#include <util/util.h>
4#include <util/bpf-loader.h>
5#include <util/evlist.h>
6#include <linux/bpf.h>
7#include <linux/filter.h>
8#include <bpf/bpf.h>
9#include "tests.h"
10#include "llvm.h"
11#include "debug.h"
12#define NR_ITERS 111
13
14#ifdef HAVE_LIBBPF_SUPPORT
15
16static int epoll_pwait_loop(void)
17{
18 int i;
19
20 /* Should fail NR_ITERS times */
21 for (i = 0; i < NR_ITERS; i++)
22 epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
23 return 0;
24}
25
26#ifdef HAVE_BPF_PROLOGUE
27
28static int llseek_loop(void)
29{
30 int fds[2], i;
31
32 fds[0] = open("/dev/null", O_RDONLY);
33 fds[1] = open("/dev/null", O_RDWR);
34
35 if (fds[0] < 0 || fds[1] < 0)
36 return -1;
37
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);
41 }
42 close(fds[0]);
43 close(fds[1]);
44 return 0;
45}
46
47#endif
48
49static struct {
50 enum test_llvm__testcase prog_id;
51 const char *desc;
52 const char *name;
53 const char *msg_compile_fail;
54 const char *msg_load_fail;
55 int (*target_func)(void);
56 int expect_result;
57} bpf_testcase_table[] = {
58 {
59 LLVM_TESTCASE_BASE,
60 "Test basic BPF filtering",
61 "[basic_bpf_test]",
62 "fix 'perf test LLVM' first",
63 "load bpf object failed",
64 &epoll_pwait_loop,
65 (NR_ITERS + 1) / 2,
66 },
67#ifdef HAVE_BPF_PROLOGUE
68 {
69 LLVM_TESTCASE_BPF_PROLOGUE,
70 "Test BPF prologue generation",
71 "[bpf_prologue_test]",
72 "fix kbuild first",
73 "check your vmlinux setting?",
74 &llseek_loop,
75 (NR_ITERS + 1) / 4,
76 },
77#endif
78 {
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",
84 NULL,
85 0,
86 },
87};
88
89static int do_test(struct bpf_object *obj, int (*func)(void),
90 int expect)
91{
92 struct record_opts opts = {
93 .target = {
94 .uid = UINT_MAX,
95 .uses_mmap = true,
96 },
97 .freq = 0,
98 .mmap_pages = 256,
99 .default_interval = 1,
100 };
101
102 char pid[16];
103 char sbuf[STRERR_BUFSIZE];
104 struct perf_evlist *evlist;
105 int i, ret = TEST_FAIL, err = 0, count = 0;
106
107 struct parse_events_evlist parse_evlist;
108 struct parse_events_error parse_error;
109
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);
114
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");
118 return TEST_FAIL;
119 }
120
121 snprintf(pid, sizeof(pid), "%d", getpid());
122 pid[sizeof(pid) - 1] = '\0';
123 opts.target.tid = opts.target.pid = pid;
124
125 /* Instead of perf_evlist__new_default, don't add default events */
126 evlist = perf_evlist__new();
127 if (!evlist) {
128 pr_debug("No ehough memory to create evlist\n");
129 return TEST_FAIL;
130 }
131
132 err = perf_evlist__create_maps(evlist, &opts.target);
133 if (err < 0) {
134 pr_debug("Not enough memory to create thread/cpu maps\n");
135 goto out_delete_evlist;
136 }
137
138 perf_evlist__splice_list_tail(evlist, &parse_evlist.list);
139 evlist->nr_groups = parse_evlist.nr_groups;
140
141 perf_evlist__config(evlist, &opts);
142
143 err = perf_evlist__open(evlist);
144 if (err < 0) {
145 pr_debug("perf_evlist__open: %s\n",
146 strerror_r(errno, sbuf, sizeof(sbuf)));
147 goto out_delete_evlist;
148 }
149
150 err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
151 if (err < 0) {
152 pr_debug("perf_evlist__mmap: %s\n",
153 strerror_r(errno, sbuf, sizeof(sbuf)));
154 goto out_delete_evlist;
155 }
156
157 perf_evlist__enable(evlist);
158 (*func)();
159 perf_evlist__disable(evlist);
160
161 for (i = 0; i < evlist->nr_mmaps; i++) {
162 union perf_event *event;
163
164 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
165 const u32 type = event->header.type;
166
167 if (type == PERF_RECORD_SAMPLE)
168 count ++;
169 }
170 }
171
172 if (count != expect) {
173 pr_debug("BPF filter result incorrect\n");
174 goto out_delete_evlist;
175 }
176
177 ret = TEST_OK;
178
179out_delete_evlist:
180 perf_evlist__delete(evlist);
181 return ret;
182}
183
184static struct bpf_object *
185prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
186{
187 struct bpf_object *obj;
188
189 obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
190 if (IS_ERR(obj)) {
191 pr_debug("Compile BPF program failed.\n");
192 return NULL;
193 }
194 return obj;
195}
196
197static int __test__bpf(int idx)
198{
199 int ret;
200 void *obj_buf;
201 size_t obj_buf_sz;
202 struct bpf_object *obj;
203
204 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
205 bpf_testcase_table[idx].prog_id,
206 true, NULL);
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);
210 if (idx == 0)
211 return TEST_SKIP;
212 else
213 return TEST_FAIL;
214 }
215
216 obj = prepare_bpf(obj_buf, obj_buf_sz,
217 bpf_testcase_table[idx].name);
218 if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
219 if (!obj)
220 pr_debug("Fail to load BPF object: %s\n",
221 bpf_testcase_table[idx].msg_load_fail);
222 else
223 pr_debug("Success unexpectedly: %s\n",
224 bpf_testcase_table[idx].msg_load_fail);
225 ret = TEST_FAIL;
226 goto out;
227 }
228
229 if (obj)
230 ret = do_test(obj,
231 bpf_testcase_table[idx].target_func,
232 bpf_testcase_table[idx].expect_result);
233out:
234 bpf__clear();
235 return ret;
236}
237
238int test__bpf_subtest_get_nr(void)
239{
240 return (int)ARRAY_SIZE(bpf_testcase_table);
241}
242
243const char *test__bpf_subtest_get_desc(int i)
244{
245 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
246 return NULL;
247 return bpf_testcase_table[i].desc;
248}
249
250static int check_env(void)
251{
252 int err;
253 unsigned int kver_int;
254 char license[] = "GPL";
255
256 struct bpf_insn insns[] = {
257 BPF_MOV64_IMM(BPF_REG_0, 1),
258 BPF_EXIT_INSN(),
259 };
260
261 err = fetch_kernel_version(&kver_int, NULL, 0);
262 if (err) {
263 pr_debug("Unable to get kernel version\n");
264 return err;
265 }
266
267 err = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
268 sizeof(insns) / sizeof(insns[0]),
269 license, kver_int, NULL, 0);
270 if (err < 0) {
271 pr_err("Missing basic BPF support, skip this test: %s\n",
272 strerror(errno));
273 return err;
274 }
275 close(err);
276
277 return 0;
278}
279
280int test__bpf(int i)
281{
282 int err;
283
284 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
285 return TEST_FAIL;
286
287 if (geteuid() != 0) {
288 pr_debug("Only root can run BPF test\n");
289 return TEST_SKIP;
290 }
291
292 if (check_env())
293 return TEST_SKIP;
294
295 err = __test__bpf(i);
296 return err;
297}
298
299#else
300int test__bpf_subtest_get_nr(void)
301{
302 return 0;
303}
304
305const char *test__bpf_subtest_get_desc(int i __maybe_unused)
306{
307 return NULL;
308}
309
310int test__bpf(int i __maybe_unused)
311{
312 pr_debug("Skip BPF test because BPF support is not compiled\n");
313 return TEST_SKIP;
314}
315#endif
1// SPDX-License-Identifier: GPL-2.0
2#include <errno.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <sys/epoll.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <util/record.h>
10#include <util/util.h>
11#include <util/bpf-loader.h>
12#include <util/evlist.h>
13#include <linux/filter.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <api/fs/fs.h>
17#include <perf/mmap.h>
18#include "tests.h"
19#include "llvm.h"
20#include "debug.h"
21#include "parse-events.h"
22#include "util/mmap.h"
23#define NR_ITERS 111
24#define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
25
26#ifdef HAVE_LIBBPF_SUPPORT
27#include <linux/bpf.h>
28#include <bpf/bpf.h>
29
30static int epoll_pwait_loop(void)
31{
32 int i;
33
34 /* Should fail NR_ITERS times */
35 for (i = 0; i < NR_ITERS; i++)
36 epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
37 return 0;
38}
39
40#ifdef HAVE_BPF_PROLOGUE
41
42static int llseek_loop(void)
43{
44 int fds[2], i;
45
46 fds[0] = open("/dev/null", O_RDONLY);
47 fds[1] = open("/dev/null", O_RDWR);
48
49 if (fds[0] < 0 || fds[1] < 0)
50 return -1;
51
52 for (i = 0; i < NR_ITERS; i++) {
53 lseek(fds[i % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
54 lseek(fds[(i + 1) % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
55 }
56 close(fds[0]);
57 close(fds[1]);
58 return 0;
59}
60
61#endif
62
63static struct {
64 enum test_llvm__testcase prog_id;
65 const char *desc;
66 const char *name;
67 const char *msg_compile_fail;
68 const char *msg_load_fail;
69 int (*target_func)(void);
70 int expect_result;
71 bool pin;
72} bpf_testcase_table[] = {
73 {
74 .prog_id = LLVM_TESTCASE_BASE,
75 .desc = "Basic BPF filtering",
76 .name = "[basic_bpf_test]",
77 .msg_compile_fail = "fix 'perf test LLVM' first",
78 .msg_load_fail = "load bpf object failed",
79 .target_func = &epoll_pwait_loop,
80 .expect_result = (NR_ITERS + 1) / 2,
81 },
82 {
83 .prog_id = LLVM_TESTCASE_BASE,
84 .desc = "BPF pinning",
85 .name = "[bpf_pinning]",
86 .msg_compile_fail = "fix kbuild first",
87 .msg_load_fail = "check your vmlinux setting?",
88 .target_func = &epoll_pwait_loop,
89 .expect_result = (NR_ITERS + 1) / 2,
90 .pin = true,
91 },
92#ifdef HAVE_BPF_PROLOGUE
93 {
94 .prog_id = LLVM_TESTCASE_BPF_PROLOGUE,
95 .desc = "BPF prologue generation",
96 .name = "[bpf_prologue_test]",
97 .msg_compile_fail = "fix kbuild first",
98 .msg_load_fail = "check your vmlinux setting?",
99 .target_func = &llseek_loop,
100 .expect_result = (NR_ITERS + 1) / 4,
101 },
102#endif
103};
104
105static int do_test(struct bpf_object *obj, int (*func)(void),
106 int expect)
107{
108 struct record_opts opts = {
109 .target = {
110 .uid = UINT_MAX,
111 .uses_mmap = true,
112 },
113 .freq = 0,
114 .mmap_pages = 256,
115 .default_interval = 1,
116 };
117
118 char pid[16];
119 char sbuf[STRERR_BUFSIZE];
120 struct evlist *evlist;
121 int i, ret = TEST_FAIL, err = 0, count = 0;
122
123 struct parse_events_state parse_state;
124 struct parse_events_error parse_error;
125
126 bzero(&parse_error, sizeof(parse_error));
127 bzero(&parse_state, sizeof(parse_state));
128 parse_state.error = &parse_error;
129 INIT_LIST_HEAD(&parse_state.list);
130
131 err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
132 if (err || list_empty(&parse_state.list)) {
133 pr_debug("Failed to add events selected by BPF\n");
134 return TEST_FAIL;
135 }
136
137 snprintf(pid, sizeof(pid), "%d", getpid());
138 pid[sizeof(pid) - 1] = '\0';
139 opts.target.tid = opts.target.pid = pid;
140
141 /* Instead of evlist__new_default, don't add default events */
142 evlist = evlist__new();
143 if (!evlist) {
144 pr_debug("Not enough memory to create evlist\n");
145 return TEST_FAIL;
146 }
147
148 err = evlist__create_maps(evlist, &opts.target);
149 if (err < 0) {
150 pr_debug("Not enough memory to create thread/cpu maps\n");
151 goto out_delete_evlist;
152 }
153
154 evlist__splice_list_tail(evlist, &parse_state.list);
155 evlist->core.nr_groups = parse_state.nr_groups;
156
157 evlist__config(evlist, &opts, NULL);
158
159 err = evlist__open(evlist);
160 if (err < 0) {
161 pr_debug("perf_evlist__open: %s\n",
162 str_error_r(errno, sbuf, sizeof(sbuf)));
163 goto out_delete_evlist;
164 }
165
166 err = evlist__mmap(evlist, opts.mmap_pages);
167 if (err < 0) {
168 pr_debug("evlist__mmap: %s\n",
169 str_error_r(errno, sbuf, sizeof(sbuf)));
170 goto out_delete_evlist;
171 }
172
173 evlist__enable(evlist);
174 (*func)();
175 evlist__disable(evlist);
176
177 for (i = 0; i < evlist->core.nr_mmaps; i++) {
178 union perf_event *event;
179 struct mmap *md;
180
181 md = &evlist->mmap[i];
182 if (perf_mmap__read_init(&md->core) < 0)
183 continue;
184
185 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
186 const u32 type = event->header.type;
187
188 if (type == PERF_RECORD_SAMPLE)
189 count ++;
190 }
191 perf_mmap__read_done(&md->core);
192 }
193
194 if (count != expect * evlist->core.nr_entries) {
195 pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect * evlist->core.nr_entries, count);
196 goto out_delete_evlist;
197 }
198
199 ret = TEST_OK;
200
201out_delete_evlist:
202 evlist__delete(evlist);
203 return ret;
204}
205
206static struct bpf_object *
207prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
208{
209 struct bpf_object *obj;
210
211 obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
212 if (IS_ERR(obj)) {
213 pr_debug("Compile BPF program failed.\n");
214 return NULL;
215 }
216 return obj;
217}
218
219static int __test__bpf(int idx)
220{
221 int ret;
222 void *obj_buf;
223 size_t obj_buf_sz;
224 struct bpf_object *obj;
225
226 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
227 bpf_testcase_table[idx].prog_id,
228 true, NULL);
229 if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
230 pr_debug("Unable to get BPF object, %s\n",
231 bpf_testcase_table[idx].msg_compile_fail);
232 if (idx == 0)
233 return TEST_SKIP;
234 else
235 return TEST_FAIL;
236 }
237
238 obj = prepare_bpf(obj_buf, obj_buf_sz,
239 bpf_testcase_table[idx].name);
240 if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
241 if (!obj)
242 pr_debug("Fail to load BPF object: %s\n",
243 bpf_testcase_table[idx].msg_load_fail);
244 else
245 pr_debug("Success unexpectedly: %s\n",
246 bpf_testcase_table[idx].msg_load_fail);
247 ret = TEST_FAIL;
248 goto out;
249 }
250
251 if (obj) {
252 ret = do_test(obj,
253 bpf_testcase_table[idx].target_func,
254 bpf_testcase_table[idx].expect_result);
255 if (ret != TEST_OK)
256 goto out;
257 if (bpf_testcase_table[idx].pin) {
258 int err;
259
260 if (!bpf_fs__mount()) {
261 pr_debug("BPF filesystem not mounted\n");
262 ret = TEST_FAIL;
263 goto out;
264 }
265 err = mkdir(PERF_TEST_BPF_PATH, 0777);
266 if (err && errno != EEXIST) {
267 pr_debug("Failed to make perf_test dir: %s\n",
268 strerror(errno));
269 ret = TEST_FAIL;
270 goto out;
271 }
272 if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
273 ret = TEST_FAIL;
274 if (rm_rf(PERF_TEST_BPF_PATH))
275 ret = TEST_FAIL;
276 }
277 }
278
279out:
280 free(obj_buf);
281 bpf__clear();
282 return ret;
283}
284
285int test__bpf_subtest_get_nr(void)
286{
287 return (int)ARRAY_SIZE(bpf_testcase_table);
288}
289
290const char *test__bpf_subtest_get_desc(int i)
291{
292 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
293 return NULL;
294 return bpf_testcase_table[i].desc;
295}
296
297static int check_env(void)
298{
299 int err;
300 unsigned int kver_int;
301 char license[] = "GPL";
302
303 struct bpf_insn insns[] = {
304 BPF_MOV64_IMM(BPF_REG_0, 1),
305 BPF_EXIT_INSN(),
306 };
307
308 err = fetch_kernel_version(&kver_int, NULL, 0);
309 if (err) {
310 pr_debug("Unable to get kernel version\n");
311 return err;
312 }
313
314 err = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
315 sizeof(insns) / sizeof(insns[0]),
316 license, kver_int, NULL, 0);
317 if (err < 0) {
318 pr_err("Missing basic BPF support, skip this test: %s\n",
319 strerror(errno));
320 return err;
321 }
322 close(err);
323
324 return 0;
325}
326
327int test__bpf(struct test *test __maybe_unused, int i)
328{
329 int err;
330
331 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
332 return TEST_FAIL;
333
334 if (geteuid() != 0) {
335 pr_debug("Only root can run BPF test\n");
336 return TEST_SKIP;
337 }
338
339 if (check_env())
340 return TEST_SKIP;
341
342 err = __test__bpf(i);
343 return err;
344}
345
346#else
347int test__bpf_subtest_get_nr(void)
348{
349 return 0;
350}
351
352const char *test__bpf_subtest_get_desc(int i __maybe_unused)
353{
354 return NULL;
355}
356
357int test__bpf(struct test *test __maybe_unused, int i __maybe_unused)
358{
359 pr_debug("Skip BPF test because BPF support is not compiled\n");
360 return TEST_SKIP;
361}
362#endif