blob: e8399beca62b32ff963f5e6828df85e2cb7d6100 [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>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03006#include <fcntl.h>
Wang Nan6a7d5502016-01-25 09:55:53 +00007#include <util/util.h>
Wang Nanba1fae42015-11-06 13:49:43 +00008#include <util/bpf-loader.h>
9#include <util/evlist.h>
Wang Nan6a7d5502016-01-25 09:55:53 +000010#include <linux/bpf.h>
11#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030012#include <linux/kernel.h>
Joe Stringera2630532017-01-26 13:20:01 -080013#include <api/fs/fs.h>
Wang Nan6a7d5502016-01-25 09:55:53 +000014#include <bpf/bpf.h>
Wang Nanba1fae42015-11-06 13:49:43 +000015#include "tests.h"
16#include "llvm.h"
17#include "debug.h"
18#define NR_ITERS 111
Joe Stringera2630532017-01-26 13:20:01 -080019#define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
Wang Nanba1fae42015-11-06 13:49:43 +000020
21#ifdef HAVE_LIBBPF_SUPPORT
22
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030023static int epoll_pwait_loop(void)
Wang Nanba1fae42015-11-06 13:49:43 +000024{
25 int i;
26
27 /* Should fail NR_ITERS times */
28 for (i = 0; i < NR_ITERS; i++)
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030029 epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +000030 return 0;
31}
32
Wang Nanbbb7d492015-11-16 12:10:14 +000033#ifdef HAVE_BPF_PROLOGUE
34
35static int llseek_loop(void)
36{
37 int fds[2], i;
38
39 fds[0] = open("/dev/null", O_RDONLY);
40 fds[1] = open("/dev/null", O_RDWR);
41
42 if (fds[0] < 0 || fds[1] < 0)
43 return -1;
44
45 for (i = 0; i < NR_ITERS; i++) {
46 lseek(fds[i % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
47 lseek(fds[(i + 1) % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
48 }
49 close(fds[0]);
50 close(fds[1]);
51 return 0;
52}
53
54#endif
55
Wang Nanba1fae42015-11-06 13:49:43 +000056static struct {
57 enum test_llvm__testcase prog_id;
58 const char *desc;
59 const char *name;
60 const char *msg_compile_fail;
61 const char *msg_load_fail;
62 int (*target_func)(void);
63 int expect_result;
Joe Stringera2630532017-01-26 13:20:01 -080064 bool pin;
Wang Nanba1fae42015-11-06 13:49:43 +000065} bpf_testcase_table[] = {
66 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030067 .prog_id = LLVM_TESTCASE_BASE,
68 .desc = "Basic BPF filtering",
69 .name = "[basic_bpf_test]",
70 .msg_compile_fail = "fix 'perf test LLVM' first",
71 .msg_load_fail = "load bpf object failed",
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030072 .target_func = &epoll_pwait_loop,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030073 .expect_result = (NR_ITERS + 1) / 2,
Joe Stringera2630532017-01-26 13:20:01 -080074 },
75 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030076 .prog_id = LLVM_TESTCASE_BASE,
77 .desc = "BPF pinning",
78 .name = "[bpf_pinning]",
79 .msg_compile_fail = "fix kbuild first",
80 .msg_load_fail = "check your vmlinux setting?",
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030081 .target_func = &epoll_pwait_loop,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030082 .expect_result = (NR_ITERS + 1) / 2,
83 .pin = true,
Wang Nanba1fae42015-11-06 13:49:43 +000084 },
Wang Nanbbb7d492015-11-16 12:10:14 +000085#ifdef HAVE_BPF_PROLOGUE
86 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030087 .prog_id = LLVM_TESTCASE_BPF_PROLOGUE,
88 .desc = "BPF prologue generation",
89 .name = "[bpf_prologue_test]",
90 .msg_compile_fail = "fix kbuild first",
91 .msg_load_fail = "check your vmlinux setting?",
92 .target_func = &llseek_loop,
93 .expect_result = (NR_ITERS + 1) / 4,
Wang Nanbbb7d492015-11-16 12:10:14 +000094 },
95#endif
Wang Nan7b6982c2016-01-25 09:55:48 +000096 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030097 .prog_id = LLVM_TESTCASE_BPF_RELOCATION,
98 .desc = "BPF relocation checker",
99 .name = "[bpf_relocation_test]",
100 .msg_compile_fail = "fix 'perf test LLVM' first",
101 .msg_load_fail = "libbpf error when dealing with relocation",
Wang Nan7b6982c2016-01-25 09:55:48 +0000102 },
Wang Nanba1fae42015-11-06 13:49:43 +0000103};
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 perf_evlist *evlist;
121 int i, ret = TEST_FAIL, err = 0, count = 0;
122
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300123 struct parse_events_state parse_state;
Wang Nanba1fae42015-11-06 13:49:43 +0000124 struct parse_events_error parse_error;
125
126 bzero(&parse_error, sizeof(parse_error));
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300127 bzero(&parse_state, sizeof(parse_state));
128 parse_state.error = &parse_error;
129 INIT_LIST_HEAD(&parse_state.list);
Wang Nanba1fae42015-11-06 13:49:43 +0000130
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300131 err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
132 if (err || list_empty(&parse_state.list)) {
Wang Nanba1fae42015-11-06 13:49:43 +0000133 pr_debug("Failed to add events selected by BPF\n");
Wang Nanad0dd7a2015-11-17 08:32:46 +0000134 return TEST_FAIL;
Wang Nanba1fae42015-11-06 13:49:43 +0000135 }
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 perf_evlist__new_default, don't add default events */
142 evlist = perf_evlist__new();
143 if (!evlist) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200144 pr_debug("Not enough memory to create evlist\n");
Wang Nanba1fae42015-11-06 13:49:43 +0000145 return TEST_FAIL;
146 }
147
148 err = perf_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
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300154 perf_evlist__splice_list_tail(evlist, &parse_state.list);
155 evlist->nr_groups = parse_state.nr_groups;
Wang Nanba1fae42015-11-06 13:49:43 +0000156
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300157 perf_evlist__config(evlist, &opts, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000158
159 err = perf_evlist__open(evlist);
160 if (err < 0) {
161 pr_debug("perf_evlist__open: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300162 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000163 goto out_delete_evlist;
164 }
165
Wang Nanf74b9d3a2017-12-03 02:00:37 +0000166 err = perf_evlist__mmap(evlist, opts.mmap_pages);
Wang Nanba1fae42015-11-06 13:49:43 +0000167 if (err < 0) {
168 pr_debug("perf_evlist__mmap: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300169 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000170 goto out_delete_evlist;
171 }
172
173 perf_evlist__enable(evlist);
174 (*func)();
175 perf_evlist__disable(evlist);
176
177 for (i = 0; i < evlist->nr_mmaps; i++) {
178 union perf_event *event;
179
180 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
181 const u32 type = event->header.type;
182
183 if (type == PERF_RECORD_SAMPLE)
184 count ++;
185 }
186 }
187
Wang Nanad0dd7a2015-11-17 08:32:46 +0000188 if (count != expect) {
Arnaldo Carvalho de Melo6703c972018-01-02 14:59:21 -0300189 pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect, count);
Wang Nanad0dd7a2015-11-17 08:32:46 +0000190 goto out_delete_evlist;
191 }
Wang Nanba1fae42015-11-06 13:49:43 +0000192
193 ret = TEST_OK;
194
195out_delete_evlist:
196 perf_evlist__delete(evlist);
197 return ret;
198}
199
200static struct bpf_object *
201prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
202{
203 struct bpf_object *obj;
204
205 obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
206 if (IS_ERR(obj)) {
207 pr_debug("Compile BPF program failed.\n");
208 return NULL;
209 }
210 return obj;
211}
212
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300213static int __test__bpf(int idx)
Wang Nanba1fae42015-11-06 13:49:43 +0000214{
215 int ret;
216 void *obj_buf;
217 size_t obj_buf_sz;
218 struct bpf_object *obj;
219
220 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300221 bpf_testcase_table[idx].prog_id,
Wang Nan7b6982c2016-01-25 09:55:48 +0000222 true, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000223 if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
224 pr_debug("Unable to get BPF object, %s\n",
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300225 bpf_testcase_table[idx].msg_compile_fail);
226 if (idx == 0)
Wang Nanba1fae42015-11-06 13:49:43 +0000227 return TEST_SKIP;
228 else
229 return TEST_FAIL;
230 }
231
232 obj = prepare_bpf(obj_buf, obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300233 bpf_testcase_table[idx].name);
Wang Nan7b6982c2016-01-25 09:55:48 +0000234 if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
235 if (!obj)
236 pr_debug("Fail to load BPF object: %s\n",
237 bpf_testcase_table[idx].msg_load_fail);
238 else
239 pr_debug("Success unexpectedly: %s\n",
240 bpf_testcase_table[idx].msg_load_fail);
Wang Nanba1fae42015-11-06 13:49:43 +0000241 ret = TEST_FAIL;
242 goto out;
243 }
244
Joe Stringera2630532017-01-26 13:20:01 -0800245 if (obj) {
Wang Nan7b6982c2016-01-25 09:55:48 +0000246 ret = do_test(obj,
247 bpf_testcase_table[idx].target_func,
248 bpf_testcase_table[idx].expect_result);
Joe Stringera2630532017-01-26 13:20:01 -0800249 if (ret != TEST_OK)
250 goto out;
251 if (bpf_testcase_table[idx].pin) {
252 int err;
253
254 if (!bpf_fs__mount()) {
255 pr_debug("BPF filesystem not mounted\n");
256 ret = TEST_FAIL;
257 goto out;
258 }
259 err = mkdir(PERF_TEST_BPF_PATH, 0777);
260 if (err && errno != EEXIST) {
261 pr_debug("Failed to make perf_test dir: %s\n",
262 strerror(errno));
263 ret = TEST_FAIL;
264 goto out;
265 }
266 if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
267 ret = TEST_FAIL;
268 if (rm_rf(PERF_TEST_BPF_PATH))
269 ret = TEST_FAIL;
270 }
271 }
272
Wang Nanba1fae42015-11-06 13:49:43 +0000273out:
274 bpf__clear();
275 return ret;
276}
277
Wang Nan77a0cf62015-11-17 08:32:49 +0000278int test__bpf_subtest_get_nr(void)
Wang Nanba1fae42015-11-06 13:49:43 +0000279{
Wang Nan77a0cf62015-11-17 08:32:49 +0000280 return (int)ARRAY_SIZE(bpf_testcase_table);
281}
282
283const char *test__bpf_subtest_get_desc(int i)
284{
285 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
286 return NULL;
287 return bpf_testcase_table[i].desc;
288}
289
Wang Nan6a7d5502016-01-25 09:55:53 +0000290static int check_env(void)
291{
292 int err;
293 unsigned int kver_int;
294 char license[] = "GPL";
295
296 struct bpf_insn insns[] = {
297 BPF_MOV64_IMM(BPF_REG_0, 1),
298 BPF_EXIT_INSN(),
299 };
300
301 err = fetch_kernel_version(&kver_int, NULL, 0);
302 if (err) {
303 pr_debug("Unable to get kernel version\n");
304 return err;
305 }
306
307 err = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
308 sizeof(insns) / sizeof(insns[0]),
309 license, kver_int, NULL, 0);
310 if (err < 0) {
311 pr_err("Missing basic BPF support, skip this test: %s\n",
312 strerror(errno));
313 return err;
314 }
315 close(err);
316
317 return 0;
318}
319
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300320int test__bpf(struct test *test __maybe_unused, int i)
Wang Nan77a0cf62015-11-17 08:32:49 +0000321{
Wang Nanba1fae42015-11-06 13:49:43 +0000322 int err;
323
Wang Nan77a0cf62015-11-17 08:32:49 +0000324 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
325 return TEST_FAIL;
326
Wang Nanba1fae42015-11-06 13:49:43 +0000327 if (geteuid() != 0) {
328 pr_debug("Only root can run BPF test\n");
329 return TEST_SKIP;
330 }
331
Wang Nan6a7d5502016-01-25 09:55:53 +0000332 if (check_env())
333 return TEST_SKIP;
334
Wang Nan77a0cf62015-11-17 08:32:49 +0000335 err = __test__bpf(i);
336 return err;
Wang Nanba1fae42015-11-06 13:49:43 +0000337}
338
339#else
Wang Nan77a0cf62015-11-17 08:32:49 +0000340int test__bpf_subtest_get_nr(void)
341{
342 return 0;
343}
344
345const char *test__bpf_subtest_get_desc(int i __maybe_unused)
346{
347 return NULL;
348}
349
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300350int test__bpf(struct test *test __maybe_unused, int i __maybe_unused)
Wang Nanba1fae42015-11-06 13:49:43 +0000351{
352 pr_debug("Skip BPF test because BPF support is not compiled\n");
353 return TEST_SKIP;
354}
355#endif