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