blob: d15f62dc426114f1070efb404c9ff88d8d300349 [file] [log] [blame]
Arnaldo Carvalho de Melo5875cf42019-06-13 18:29:05 -03001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Wang Nanba1fae42015-11-06 13:49:43 +00003#include <stdio.h>
4#include <sys/epoll.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03005#include <sys/types.h>
6#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03007#include <fcntl.h>
Wang Nan6a7d5502016-01-25 09:55:53 +00008#include <util/util.h>
Wang Nanba1fae42015-11-06 13:49:43 +00009#include <util/bpf-loader.h>
10#include <util/evlist.h>
Wang Nan6a7d5502016-01-25 09:55:53 +000011#include <linux/bpf.h>
12#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030013#include <linux/kernel.h>
Joe Stringera2630532017-01-26 13:20:01 -080014#include <api/fs/fs.h>
Wang Nan6a7d5502016-01-25 09:55:53 +000015#include <bpf/bpf.h>
Wang Nanba1fae42015-11-06 13:49:43 +000016#include "tests.h"
17#include "llvm.h"
18#include "debug.h"
19#define NR_ITERS 111
Joe Stringera2630532017-01-26 13:20:01 -080020#define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
Wang Nanba1fae42015-11-06 13:49:43 +000021
22#ifdef HAVE_LIBBPF_SUPPORT
23
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030024static int epoll_pwait_loop(void)
Wang Nanba1fae42015-11-06 13:49:43 +000025{
26 int i;
27
28 /* Should fail NR_ITERS times */
29 for (i = 0; i < NR_ITERS; i++)
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030030 epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +000031 return 0;
32}
33
Wang Nanbbb7d492015-11-16 12:10:14 +000034#ifdef HAVE_BPF_PROLOGUE
35
36static int llseek_loop(void)
37{
38 int fds[2], i;
39
40 fds[0] = open("/dev/null", O_RDONLY);
41 fds[1] = open("/dev/null", O_RDWR);
42
43 if (fds[0] < 0 || fds[1] < 0)
44 return -1;
45
46 for (i = 0; i < NR_ITERS; i++) {
47 lseek(fds[i % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
48 lseek(fds[(i + 1) % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
49 }
50 close(fds[0]);
51 close(fds[1]);
52 return 0;
53}
54
55#endif
56
Wang Nanba1fae42015-11-06 13:49:43 +000057static struct {
58 enum test_llvm__testcase prog_id;
59 const char *desc;
60 const char *name;
61 const char *msg_compile_fail;
62 const char *msg_load_fail;
63 int (*target_func)(void);
64 int expect_result;
Joe Stringera2630532017-01-26 13:20:01 -080065 bool pin;
Wang Nanba1fae42015-11-06 13:49:43 +000066} bpf_testcase_table[] = {
67 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030068 .prog_id = LLVM_TESTCASE_BASE,
69 .desc = "Basic BPF filtering",
70 .name = "[basic_bpf_test]",
71 .msg_compile_fail = "fix 'perf test LLVM' first",
72 .msg_load_fail = "load bpf object failed",
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030073 .target_func = &epoll_pwait_loop,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030074 .expect_result = (NR_ITERS + 1) / 2,
Joe Stringera2630532017-01-26 13:20:01 -080075 },
76 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030077 .prog_id = LLVM_TESTCASE_BASE,
78 .desc = "BPF pinning",
79 .name = "[bpf_pinning]",
80 .msg_compile_fail = "fix kbuild first",
81 .msg_load_fail = "check your vmlinux setting?",
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030082 .target_func = &epoll_pwait_loop,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030083 .expect_result = (NR_ITERS + 1) / 2,
84 .pin = true,
Wang Nanba1fae42015-11-06 13:49:43 +000085 },
Wang Nanbbb7d492015-11-16 12:10:14 +000086#ifdef HAVE_BPF_PROLOGUE
87 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030088 .prog_id = LLVM_TESTCASE_BPF_PROLOGUE,
89 .desc = "BPF prologue generation",
90 .name = "[bpf_prologue_test]",
91 .msg_compile_fail = "fix kbuild first",
92 .msg_load_fail = "check your vmlinux setting?",
93 .target_func = &llseek_loop,
94 .expect_result = (NR_ITERS + 1) / 4,
Wang Nanbbb7d492015-11-16 12:10:14 +000095 },
96#endif
Wang Nan7b6982c2016-01-25 09:55:48 +000097 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030098 .prog_id = LLVM_TESTCASE_BPF_RELOCATION,
99 .desc = "BPF relocation checker",
100 .name = "[bpf_relocation_test]",
101 .msg_compile_fail = "fix 'perf test LLVM' first",
102 .msg_load_fail = "libbpf error when dealing with relocation",
Wang Nan7b6982c2016-01-25 09:55:48 +0000103 },
Wang Nanba1fae42015-11-06 13:49:43 +0000104};
105
106static int do_test(struct bpf_object *obj, int (*func)(void),
107 int expect)
108{
109 struct record_opts opts = {
110 .target = {
111 .uid = UINT_MAX,
112 .uses_mmap = true,
113 },
114 .freq = 0,
115 .mmap_pages = 256,
116 .default_interval = 1,
117 };
118
119 char pid[16];
120 char sbuf[STRERR_BUFSIZE];
Jiri Olsa63503db2019-07-21 13:23:52 +0200121 struct evlist *evlist;
Wang Nanba1fae42015-11-06 13:49:43 +0000122 int i, ret = TEST_FAIL, err = 0, count = 0;
123
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300124 struct parse_events_state parse_state;
Wang Nanba1fae42015-11-06 13:49:43 +0000125 struct parse_events_error parse_error;
126
127 bzero(&parse_error, sizeof(parse_error));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300128 bzero(&parse_state, sizeof(parse_state));
129 parse_state.error = &parse_error;
130 INIT_LIST_HEAD(&parse_state.list);
Wang Nanba1fae42015-11-06 13:49:43 +0000131
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300132 err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
133 if (err || list_empty(&parse_state.list)) {
Wang Nanba1fae42015-11-06 13:49:43 +0000134 pr_debug("Failed to add events selected by BPF\n");
Wang Nanad0dd7a2015-11-17 08:32:46 +0000135 return TEST_FAIL;
Wang Nanba1fae42015-11-06 13:49:43 +0000136 }
137
138 snprintf(pid, sizeof(pid), "%d", getpid());
139 pid[sizeof(pid) - 1] = '\0';
140 opts.target.tid = opts.target.pid = pid;
141
142 /* Instead of perf_evlist__new_default, don't add default events */
Jiri Olsa0f98b112019-07-21 13:23:55 +0200143 evlist = evlist__new();
Wang Nanba1fae42015-11-06 13:49:43 +0000144 if (!evlist) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200145 pr_debug("Not enough memory to create evlist\n");
Wang Nanba1fae42015-11-06 13:49:43 +0000146 return TEST_FAIL;
147 }
148
149 err = perf_evlist__create_maps(evlist, &opts.target);
150 if (err < 0) {
151 pr_debug("Not enough memory to create thread/cpu maps\n");
152 goto out_delete_evlist;
153 }
154
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300155 perf_evlist__splice_list_tail(evlist, &parse_state.list);
156 evlist->nr_groups = parse_state.nr_groups;
Wang Nanba1fae42015-11-06 13:49:43 +0000157
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300158 perf_evlist__config(evlist, &opts, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000159
160 err = perf_evlist__open(evlist);
161 if (err < 0) {
162 pr_debug("perf_evlist__open: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300163 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000164 goto out_delete_evlist;
165 }
166
Wang Nanf74b9d3a2017-12-03 02:00:37 +0000167 err = perf_evlist__mmap(evlist, opts.mmap_pages);
Wang Nanba1fae42015-11-06 13:49:43 +0000168 if (err < 0) {
169 pr_debug("perf_evlist__mmap: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300170 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000171 goto out_delete_evlist;
172 }
173
174 perf_evlist__enable(evlist);
175 (*func)();
176 perf_evlist__disable(evlist);
177
178 for (i = 0; i < evlist->nr_mmaps; i++) {
179 union perf_event *event;
Kan Liang2f54f3a2018-03-01 18:09:01 -0500180 struct perf_mmap *md;
Wang Nanba1fae42015-11-06 13:49:43 +0000181
Kan Liang2f54f3a2018-03-01 18:09:01 -0500182 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -0500183 if (perf_mmap__read_init(md) < 0)
Kan Liang2f54f3a2018-03-01 18:09:01 -0500184 continue;
185
Kan Liang0019dc872018-03-06 10:36:06 -0500186 while ((event = perf_mmap__read_event(md)) != NULL) {
Wang Nanba1fae42015-11-06 13:49:43 +0000187 const u32 type = event->header.type;
188
189 if (type == PERF_RECORD_SAMPLE)
190 count ++;
191 }
Kan Liang2f54f3a2018-03-01 18:09:01 -0500192 perf_mmap__read_done(md);
Wang Nanba1fae42015-11-06 13:49:43 +0000193 }
194
Wang Nanad0dd7a2015-11-17 08:32:46 +0000195 if (count != expect) {
Arnaldo Carvalho de Melo6703c972018-01-02 14:59:21 -0300196 pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect, count);
Wang Nanad0dd7a2015-11-17 08:32:46 +0000197 goto out_delete_evlist;
198 }
Wang Nanba1fae42015-11-06 13:49:43 +0000199
200 ret = TEST_OK;
201
202out_delete_evlist:
203 perf_evlist__delete(evlist);
204 return ret;
205}
206
207static struct bpf_object *
208prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
209{
210 struct bpf_object *obj;
211
212 obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
213 if (IS_ERR(obj)) {
214 pr_debug("Compile BPF program failed.\n");
215 return NULL;
216 }
217 return obj;
218}
219
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300220static int __test__bpf(int idx)
Wang Nanba1fae42015-11-06 13:49:43 +0000221{
222 int ret;
223 void *obj_buf;
224 size_t obj_buf_sz;
225 struct bpf_object *obj;
226
227 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300228 bpf_testcase_table[idx].prog_id,
Wang Nan7b6982c2016-01-25 09:55:48 +0000229 true, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000230 if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
231 pr_debug("Unable to get BPF object, %s\n",
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300232 bpf_testcase_table[idx].msg_compile_fail);
233 if (idx == 0)
Wang Nanba1fae42015-11-06 13:49:43 +0000234 return TEST_SKIP;
235 else
236 return TEST_FAIL;
237 }
238
239 obj = prepare_bpf(obj_buf, obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300240 bpf_testcase_table[idx].name);
Wang Nan7b6982c2016-01-25 09:55:48 +0000241 if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
242 if (!obj)
243 pr_debug("Fail to load BPF object: %s\n",
244 bpf_testcase_table[idx].msg_load_fail);
245 else
246 pr_debug("Success unexpectedly: %s\n",
247 bpf_testcase_table[idx].msg_load_fail);
Wang Nanba1fae42015-11-06 13:49:43 +0000248 ret = TEST_FAIL;
249 goto out;
250 }
251
Joe Stringera2630532017-01-26 13:20:01 -0800252 if (obj) {
Wang Nan7b6982c2016-01-25 09:55:48 +0000253 ret = do_test(obj,
254 bpf_testcase_table[idx].target_func,
255 bpf_testcase_table[idx].expect_result);
Joe Stringera2630532017-01-26 13:20:01 -0800256 if (ret != TEST_OK)
257 goto out;
258 if (bpf_testcase_table[idx].pin) {
259 int err;
260
261 if (!bpf_fs__mount()) {
262 pr_debug("BPF filesystem not mounted\n");
263 ret = TEST_FAIL;
264 goto out;
265 }
266 err = mkdir(PERF_TEST_BPF_PATH, 0777);
267 if (err && errno != EEXIST) {
268 pr_debug("Failed to make perf_test dir: %s\n",
269 strerror(errno));
270 ret = TEST_FAIL;
271 goto out;
272 }
273 if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
274 ret = TEST_FAIL;
275 if (rm_rf(PERF_TEST_BPF_PATH))
276 ret = TEST_FAIL;
277 }
278 }
279
Wang Nanba1fae42015-11-06 13:49:43 +0000280out:
281 bpf__clear();
282 return ret;
283}
284
Wang Nan77a0cf62015-11-17 08:32:49 +0000285int test__bpf_subtest_get_nr(void)
Wang Nanba1fae42015-11-06 13:49:43 +0000286{
Wang Nan77a0cf62015-11-17 08:32:49 +0000287 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
Wang Nan6a7d5502016-01-25 09:55:53 +0000297static 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
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300327int test__bpf(struct test *test __maybe_unused, int i)
Wang Nan77a0cf62015-11-17 08:32:49 +0000328{
Wang Nanba1fae42015-11-06 13:49:43 +0000329 int err;
330
Wang Nan77a0cf62015-11-17 08:32:49 +0000331 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
332 return TEST_FAIL;
333
Wang Nanba1fae42015-11-06 13:49:43 +0000334 if (geteuid() != 0) {
335 pr_debug("Only root can run BPF test\n");
336 return TEST_SKIP;
337 }
338
Wang Nan6a7d5502016-01-25 09:55:53 +0000339 if (check_env())
340 return TEST_SKIP;
341
Wang Nan77a0cf62015-11-17 08:32:49 +0000342 err = __test__bpf(i);
343 return err;
Wang Nanba1fae42015-11-06 13:49:43 +0000344}
345
346#else
Wang Nan77a0cf62015-11-17 08:32:49 +0000347int 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
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300357int test__bpf(struct test *test __maybe_unused, int i __maybe_unused)
Wang Nanba1fae42015-11-06 13:49:43 +0000358{
359 pr_debug("Skip BPF test because BPF support is not compiled\n");
360 return TEST_SKIP;
361}
362#endif