blob: 057c6b8fdb53e7d24fffa871450e2582d1dd4fee [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Wang Nanba1fae42015-11-06 13:49:43 +00002#include <stdio.h>
3#include <sys/epoll.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03004#include <sys/types.h>
5#include <sys/stat.h>
Wang Nan6a7d5502016-01-25 09:55:53 +00006#include <util/util.h>
Wang Nanba1fae42015-11-06 13:49:43 +00007#include <util/bpf-loader.h>
8#include <util/evlist.h>
Wang Nan6a7d5502016-01-25 09:55:53 +00009#include <linux/bpf.h>
10#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030011#include <linux/kernel.h>
Joe Stringera2630532017-01-26 13:20:01 -080012#include <api/fs/fs.h>
Wang Nan6a7d5502016-01-25 09:55:53 +000013#include <bpf/bpf.h>
Wang Nanba1fae42015-11-06 13:49:43 +000014#include "tests.h"
15#include "llvm.h"
16#include "debug.h"
17#define NR_ITERS 111
Joe Stringera2630532017-01-26 13:20:01 -080018#define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
Wang Nanba1fae42015-11-06 13:49:43 +000019
20#ifdef HAVE_LIBBPF_SUPPORT
21
Arnaldo Carvalho de Melo4ffde492016-07-11 23:22:30 -030022static int epoll_wait_loop(void)
Wang Nanba1fae42015-11-06 13:49:43 +000023{
24 int i;
25
26 /* Should fail NR_ITERS times */
27 for (i = 0; i < NR_ITERS; i++)
Arnaldo Carvalho de Melo4ffde492016-07-11 23:22:30 -030028 epoll_wait(-(i + 1), NULL, 0, 0);
Wang Nanba1fae42015-11-06 13:49:43 +000029 return 0;
30}
31
Wang Nanbbb7d492015-11-16 12:10:14 +000032#ifdef HAVE_BPF_PROLOGUE
33
34static int llseek_loop(void)
35{
36 int fds[2], i;
37
38 fds[0] = open("/dev/null", O_RDONLY);
39 fds[1] = open("/dev/null", O_RDWR);
40
41 if (fds[0] < 0 || fds[1] < 0)
42 return -1;
43
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);
47 }
48 close(fds[0]);
49 close(fds[1]);
50 return 0;
51}
52
53#endif
54
Wang Nanba1fae42015-11-06 13:49:43 +000055static struct {
56 enum test_llvm__testcase prog_id;
57 const char *desc;
58 const char *name;
59 const char *msg_compile_fail;
60 const char *msg_load_fail;
61 int (*target_func)(void);
62 int expect_result;
Joe Stringera2630532017-01-26 13:20:01 -080063 bool pin;
Wang Nanba1fae42015-11-06 13:49:43 +000064} bpf_testcase_table[] = {
65 {
66 LLVM_TESTCASE_BASE,
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030067 "Basic BPF filtering",
Wang Nanba1fae42015-11-06 13:49:43 +000068 "[basic_bpf_test]",
69 "fix 'perf test LLVM' first",
70 "load bpf object failed",
Arnaldo Carvalho de Melo4ffde492016-07-11 23:22:30 -030071 &epoll_wait_loop,
Wang Nanba1fae42015-11-06 13:49:43 +000072 (NR_ITERS + 1) / 2,
Joe Stringera2630532017-01-26 13:20:01 -080073 false,
74 },
75 {
76 LLVM_TESTCASE_BASE,
77 "BPF pinning",
78 "[bpf_pinning]",
79 "fix kbuild first",
80 "check your vmlinux setting?",
81 &epoll_wait_loop,
82 (NR_ITERS + 1) / 2,
83 true,
Wang Nanba1fae42015-11-06 13:49:43 +000084 },
Wang Nanbbb7d492015-11-16 12:10:14 +000085#ifdef HAVE_BPF_PROLOGUE
86 {
87 LLVM_TESTCASE_BPF_PROLOGUE,
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030088 "BPF prologue generation",
Wang Nanbbb7d492015-11-16 12:10:14 +000089 "[bpf_prologue_test]",
90 "fix kbuild first",
91 "check your vmlinux setting?",
92 &llseek_loop,
93 (NR_ITERS + 1) / 4,
Joe Stringera2630532017-01-26 13:20:01 -080094 false,
Wang Nanbbb7d492015-11-16 12:10:14 +000095 },
96#endif
Wang Nan7b6982c2016-01-25 09:55:48 +000097 {
98 LLVM_TESTCASE_BPF_RELOCATION,
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030099 "BPF relocation checker",
Wang Nan7b6982c2016-01-25 09:55:48 +0000100 "[bpf_relocation_test]",
101 "fix 'perf test LLVM' first",
102 "libbpf error when dealing with relocation",
103 NULL,
104 0,
Joe Stringera2630532017-01-26 13:20:01 -0800105 false,
Wang Nan7b6982c2016-01-25 09:55:48 +0000106 },
Wang Nanba1fae42015-11-06 13:49:43 +0000107};
108
109static int do_test(struct bpf_object *obj, int (*func)(void),
110 int expect)
111{
112 struct record_opts opts = {
113 .target = {
114 .uid = UINT_MAX,
115 .uses_mmap = true,
116 },
117 .freq = 0,
118 .mmap_pages = 256,
119 .default_interval = 1,
120 };
121
122 char pid[16];
123 char sbuf[STRERR_BUFSIZE];
124 struct perf_evlist *evlist;
125 int i, ret = TEST_FAIL, err = 0, count = 0;
126
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300127 struct parse_events_state parse_state;
Wang Nanba1fae42015-11-06 13:49:43 +0000128 struct parse_events_error parse_error;
129
130 bzero(&parse_error, sizeof(parse_error));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300131 bzero(&parse_state, sizeof(parse_state));
132 parse_state.error = &parse_error;
133 INIT_LIST_HEAD(&parse_state.list);
Wang Nanba1fae42015-11-06 13:49:43 +0000134
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300135 err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
136 if (err || list_empty(&parse_state.list)) {
Wang Nanba1fae42015-11-06 13:49:43 +0000137 pr_debug("Failed to add events selected by BPF\n");
Wang Nanad0dd7a2015-11-17 08:32:46 +0000138 return TEST_FAIL;
Wang Nanba1fae42015-11-06 13:49:43 +0000139 }
140
141 snprintf(pid, sizeof(pid), "%d", getpid());
142 pid[sizeof(pid) - 1] = '\0';
143 opts.target.tid = opts.target.pid = pid;
144
145 /* Instead of perf_evlist__new_default, don't add default events */
146 evlist = perf_evlist__new();
147 if (!evlist) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200148 pr_debug("Not enough memory to create evlist\n");
Wang Nanba1fae42015-11-06 13:49:43 +0000149 return TEST_FAIL;
150 }
151
152 err = perf_evlist__create_maps(evlist, &opts.target);
153 if (err < 0) {
154 pr_debug("Not enough memory to create thread/cpu maps\n");
155 goto out_delete_evlist;
156 }
157
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300158 perf_evlist__splice_list_tail(evlist, &parse_state.list);
159 evlist->nr_groups = parse_state.nr_groups;
Wang Nanba1fae42015-11-06 13:49:43 +0000160
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300161 perf_evlist__config(evlist, &opts, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000162
163 err = perf_evlist__open(evlist);
164 if (err < 0) {
165 pr_debug("perf_evlist__open: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300166 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000167 goto out_delete_evlist;
168 }
169
Wang Nanf74b9d3a2017-12-03 02:00:37 +0000170 err = perf_evlist__mmap(evlist, opts.mmap_pages);
Wang Nanba1fae42015-11-06 13:49:43 +0000171 if (err < 0) {
172 pr_debug("perf_evlist__mmap: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300173 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000174 goto out_delete_evlist;
175 }
176
177 perf_evlist__enable(evlist);
178 (*func)();
179 perf_evlist__disable(evlist);
180
181 for (i = 0; i < evlist->nr_mmaps; i++) {
182 union perf_event *event;
183
184 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
185 const u32 type = event->header.type;
186
187 if (type == PERF_RECORD_SAMPLE)
188 count ++;
189 }
190 }
191
Wang Nanad0dd7a2015-11-17 08:32:46 +0000192 if (count != expect) {
Arnaldo Carvalho de Melo6703c972018-01-02 14:59:21 -0300193 pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect, count);
Wang Nanad0dd7a2015-11-17 08:32:46 +0000194 goto out_delete_evlist;
195 }
Wang Nanba1fae42015-11-06 13:49:43 +0000196
197 ret = TEST_OK;
198
199out_delete_evlist:
200 perf_evlist__delete(evlist);
201 return ret;
202}
203
204static struct bpf_object *
205prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
206{
207 struct bpf_object *obj;
208
209 obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
210 if (IS_ERR(obj)) {
211 pr_debug("Compile BPF program failed.\n");
212 return NULL;
213 }
214 return obj;
215}
216
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300217static int __test__bpf(int idx)
Wang Nanba1fae42015-11-06 13:49:43 +0000218{
219 int ret;
220 void *obj_buf;
221 size_t obj_buf_sz;
222 struct bpf_object *obj;
223
224 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300225 bpf_testcase_table[idx].prog_id,
Wang Nan7b6982c2016-01-25 09:55:48 +0000226 true, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000227 if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
228 pr_debug("Unable to get BPF object, %s\n",
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300229 bpf_testcase_table[idx].msg_compile_fail);
230 if (idx == 0)
Wang Nanba1fae42015-11-06 13:49:43 +0000231 return TEST_SKIP;
232 else
233 return TEST_FAIL;
234 }
235
236 obj = prepare_bpf(obj_buf, obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300237 bpf_testcase_table[idx].name);
Wang Nan7b6982c2016-01-25 09:55:48 +0000238 if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
239 if (!obj)
240 pr_debug("Fail to load BPF object: %s\n",
241 bpf_testcase_table[idx].msg_load_fail);
242 else
243 pr_debug("Success unexpectedly: %s\n",
244 bpf_testcase_table[idx].msg_load_fail);
Wang Nanba1fae42015-11-06 13:49:43 +0000245 ret = TEST_FAIL;
246 goto out;
247 }
248
Joe Stringera2630532017-01-26 13:20:01 -0800249 if (obj) {
Wang Nan7b6982c2016-01-25 09:55:48 +0000250 ret = do_test(obj,
251 bpf_testcase_table[idx].target_func,
252 bpf_testcase_table[idx].expect_result);
Joe Stringera2630532017-01-26 13:20:01 -0800253 if (ret != TEST_OK)
254 goto out;
255 if (bpf_testcase_table[idx].pin) {
256 int err;
257
258 if (!bpf_fs__mount()) {
259 pr_debug("BPF filesystem not mounted\n");
260 ret = TEST_FAIL;
261 goto out;
262 }
263 err = mkdir(PERF_TEST_BPF_PATH, 0777);
264 if (err && errno != EEXIST) {
265 pr_debug("Failed to make perf_test dir: %s\n",
266 strerror(errno));
267 ret = TEST_FAIL;
268 goto out;
269 }
270 if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
271 ret = TEST_FAIL;
272 if (rm_rf(PERF_TEST_BPF_PATH))
273 ret = TEST_FAIL;
274 }
275 }
276
Wang Nanba1fae42015-11-06 13:49:43 +0000277out:
278 bpf__clear();
279 return ret;
280}
281
Wang Nan77a0cf62015-11-17 08:32:49 +0000282int test__bpf_subtest_get_nr(void)
Wang Nanba1fae42015-11-06 13:49:43 +0000283{
Wang Nan77a0cf62015-11-17 08:32:49 +0000284 return (int)ARRAY_SIZE(bpf_testcase_table);
285}
286
287const char *test__bpf_subtest_get_desc(int i)
288{
289 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
290 return NULL;
291 return bpf_testcase_table[i].desc;
292}
293
Wang Nan6a7d5502016-01-25 09:55:53 +0000294static int check_env(void)
295{
296 int err;
297 unsigned int kver_int;
298 char license[] = "GPL";
299
300 struct bpf_insn insns[] = {
301 BPF_MOV64_IMM(BPF_REG_0, 1),
302 BPF_EXIT_INSN(),
303 };
304
305 err = fetch_kernel_version(&kver_int, NULL, 0);
306 if (err) {
307 pr_debug("Unable to get kernel version\n");
308 return err;
309 }
310
311 err = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
312 sizeof(insns) / sizeof(insns[0]),
313 license, kver_int, NULL, 0);
314 if (err < 0) {
315 pr_err("Missing basic BPF support, skip this test: %s\n",
316 strerror(errno));
317 return err;
318 }
319 close(err);
320
321 return 0;
322}
323
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300324int test__bpf(struct test *test __maybe_unused, int i)
Wang Nan77a0cf62015-11-17 08:32:49 +0000325{
Wang Nanba1fae42015-11-06 13:49:43 +0000326 int err;
327
Wang Nan77a0cf62015-11-17 08:32:49 +0000328 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
329 return TEST_FAIL;
330
Wang Nanba1fae42015-11-06 13:49:43 +0000331 if (geteuid() != 0) {
332 pr_debug("Only root can run BPF test\n");
333 return TEST_SKIP;
334 }
335
Wang Nan6a7d5502016-01-25 09:55:53 +0000336 if (check_env())
337 return TEST_SKIP;
338
Wang Nan77a0cf62015-11-17 08:32:49 +0000339 err = __test__bpf(i);
340 return err;
Wang Nanba1fae42015-11-06 13:49:43 +0000341}
342
343#else
Wang Nan77a0cf62015-11-17 08:32:49 +0000344int test__bpf_subtest_get_nr(void)
345{
346 return 0;
347}
348
349const char *test__bpf_subtest_get_desc(int i __maybe_unused)
350{
351 return NULL;
352}
353
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300354int test__bpf(struct test *test __maybe_unused, int i __maybe_unused)
Wang Nanba1fae42015-11-06 13:49:43 +0000355{
356 pr_debug("Skip BPF test because BPF support is not compiled\n");
357 return TEST_SKIP;
358}
359#endif