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> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
Wang Nan | 6a7d550 | 2016-01-25 09:55:53 +0000 | [diff] [blame] | 6 | #include <util/util.h> |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 7 | #include <util/bpf-loader.h> |
| 8 | #include <util/evlist.h> |
Wang Nan | 6a7d550 | 2016-01-25 09:55:53 +0000 | [diff] [blame] | 9 | #include <linux/bpf.h> |
| 10 | #include <linux/filter.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Joe Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 12 | #include <api/fs/fs.h> |
Wang Nan | 6a7d550 | 2016-01-25 09:55:53 +0000 | [diff] [blame] | 13 | #include <bpf/bpf.h> |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 14 | #include "tests.h" |
| 15 | #include "llvm.h" |
| 16 | #include "debug.h" |
| 17 | #define NR_ITERS 111 |
Joe Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 18 | #define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test" |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 19 | |
| 20 | #ifdef HAVE_LIBBPF_SUPPORT |
| 21 | |
Arnaldo Carvalho de Melo | 4ffde49 | 2016-07-11 23:22:30 -0300 | [diff] [blame] | 22 | static int epoll_wait_loop(void) |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 23 | { |
| 24 | int i; |
| 25 | |
| 26 | /* Should fail NR_ITERS times */ |
| 27 | for (i = 0; i < NR_ITERS; i++) |
Arnaldo Carvalho de Melo | 4ffde49 | 2016-07-11 23:22:30 -0300 | [diff] [blame] | 28 | epoll_wait(-(i + 1), NULL, 0, 0); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 29 | return 0; |
| 30 | } |
| 31 | |
Wang Nan | bbb7d49 | 2015-11-16 12:10:14 +0000 | [diff] [blame] | 32 | #ifdef HAVE_BPF_PROLOGUE |
| 33 | |
| 34 | static 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 Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 55 | static 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 Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 63 | bool pin; |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 64 | } bpf_testcase_table[] = { |
| 65 | { |
| 66 | LLVM_TESTCASE_BASE, |
Arnaldo Carvalho de Melo | 030910c | 2016-11-29 12:38:14 -0300 | [diff] [blame] | 67 | "Basic BPF filtering", |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 68 | "[basic_bpf_test]", |
| 69 | "fix 'perf test LLVM' first", |
| 70 | "load bpf object failed", |
Arnaldo Carvalho de Melo | 4ffde49 | 2016-07-11 23:22:30 -0300 | [diff] [blame] | 71 | &epoll_wait_loop, |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 72 | (NR_ITERS + 1) / 2, |
Joe Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 73 | 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 Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 84 | }, |
Wang Nan | bbb7d49 | 2015-11-16 12:10:14 +0000 | [diff] [blame] | 85 | #ifdef HAVE_BPF_PROLOGUE |
| 86 | { |
| 87 | LLVM_TESTCASE_BPF_PROLOGUE, |
Arnaldo Carvalho de Melo | 030910c | 2016-11-29 12:38:14 -0300 | [diff] [blame] | 88 | "BPF prologue generation", |
Wang Nan | bbb7d49 | 2015-11-16 12:10:14 +0000 | [diff] [blame] | 89 | "[bpf_prologue_test]", |
| 90 | "fix kbuild first", |
| 91 | "check your vmlinux setting?", |
| 92 | &llseek_loop, |
| 93 | (NR_ITERS + 1) / 4, |
Joe Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 94 | false, |
Wang Nan | bbb7d49 | 2015-11-16 12:10:14 +0000 | [diff] [blame] | 95 | }, |
| 96 | #endif |
Wang Nan | 7b6982c | 2016-01-25 09:55:48 +0000 | [diff] [blame] | 97 | { |
| 98 | LLVM_TESTCASE_BPF_RELOCATION, |
Arnaldo Carvalho de Melo | 030910c | 2016-11-29 12:38:14 -0300 | [diff] [blame] | 99 | "BPF relocation checker", |
Wang Nan | 7b6982c | 2016-01-25 09:55:48 +0000 | [diff] [blame] | 100 | "[bpf_relocation_test]", |
| 101 | "fix 'perf test LLVM' first", |
| 102 | "libbpf error when dealing with relocation", |
| 103 | NULL, |
| 104 | 0, |
Joe Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 105 | false, |
Wang Nan | 7b6982c | 2016-01-25 09:55:48 +0000 | [diff] [blame] | 106 | }, |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static 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 Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 127 | struct parse_events_state parse_state; |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 128 | struct parse_events_error parse_error; |
| 129 | |
| 130 | bzero(&parse_error, sizeof(parse_error)); |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 131 | bzero(&parse_state, sizeof(parse_state)); |
| 132 | parse_state.error = &parse_error; |
| 133 | INIT_LIST_HEAD(&parse_state.list); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 134 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 135 | err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL); |
| 136 | if (err || list_empty(&parse_state.list)) { |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 137 | pr_debug("Failed to add events selected by BPF\n"); |
Wang Nan | ad0dd7a | 2015-11-17 08:32:46 +0000 | [diff] [blame] | 138 | return TEST_FAIL; |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 139 | } |
| 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 Alemayhu | 042cfb5 | 2016-10-13 18:18:11 +0200 | [diff] [blame] | 148 | pr_debug("Not enough memory to create evlist\n"); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 149 | 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 Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 158 | perf_evlist__splice_list_tail(evlist, &parse_state.list); |
| 159 | evlist->nr_groups = parse_state.nr_groups; |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 160 | |
Arnaldo Carvalho de Melo | e68ae9c | 2016-04-11 18:15:29 -0300 | [diff] [blame] | 161 | perf_evlist__config(evlist, &opts, NULL); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 162 | |
| 163 | err = perf_evlist__open(evlist); |
| 164 | if (err < 0) { |
| 165 | pr_debug("perf_evlist__open: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 166 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 167 | goto out_delete_evlist; |
| 168 | } |
| 169 | |
Wang Nan | f74b9d3a | 2017-12-03 02:00:37 +0000 | [diff] [blame] | 170 | err = perf_evlist__mmap(evlist, opts.mmap_pages); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 171 | if (err < 0) { |
| 172 | pr_debug("perf_evlist__mmap: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 173 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 174 | 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 Nan | ad0dd7a | 2015-11-17 08:32:46 +0000 | [diff] [blame] | 192 | if (count != expect) { |
Arnaldo Carvalho de Melo | 6703c97 | 2018-01-02 14:59:21 -0300 | [diff] [blame^] | 193 | pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect, count); |
Wang Nan | ad0dd7a | 2015-11-17 08:32:46 +0000 | [diff] [blame] | 194 | goto out_delete_evlist; |
| 195 | } |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 196 | |
| 197 | ret = TEST_OK; |
| 198 | |
| 199 | out_delete_evlist: |
| 200 | perf_evlist__delete(evlist); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static struct bpf_object * |
| 205 | prepare_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 Melo | 916d409 | 2015-11-18 17:38:49 -0300 | [diff] [blame] | 217 | static int __test__bpf(int idx) |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 218 | { |
| 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 Melo | 916d409 | 2015-11-18 17:38:49 -0300 | [diff] [blame] | 225 | bpf_testcase_table[idx].prog_id, |
Wang Nan | 7b6982c | 2016-01-25 09:55:48 +0000 | [diff] [blame] | 226 | true, NULL); |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 227 | if (ret != TEST_OK || !obj_buf || !obj_buf_sz) { |
| 228 | pr_debug("Unable to get BPF object, %s\n", |
Arnaldo Carvalho de Melo | 916d409 | 2015-11-18 17:38:49 -0300 | [diff] [blame] | 229 | bpf_testcase_table[idx].msg_compile_fail); |
| 230 | if (idx == 0) |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 231 | return TEST_SKIP; |
| 232 | else |
| 233 | return TEST_FAIL; |
| 234 | } |
| 235 | |
| 236 | obj = prepare_bpf(obj_buf, obj_buf_sz, |
Arnaldo Carvalho de Melo | 916d409 | 2015-11-18 17:38:49 -0300 | [diff] [blame] | 237 | bpf_testcase_table[idx].name); |
Wang Nan | 7b6982c | 2016-01-25 09:55:48 +0000 | [diff] [blame] | 238 | 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 Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 245 | ret = TEST_FAIL; |
| 246 | goto out; |
| 247 | } |
| 248 | |
Joe Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 249 | if (obj) { |
Wang Nan | 7b6982c | 2016-01-25 09:55:48 +0000 | [diff] [blame] | 250 | ret = do_test(obj, |
| 251 | bpf_testcase_table[idx].target_func, |
| 252 | bpf_testcase_table[idx].expect_result); |
Joe Stringer | a263053 | 2017-01-26 13:20:01 -0800 | [diff] [blame] | 253 | 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 Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 277 | out: |
| 278 | bpf__clear(); |
| 279 | return ret; |
| 280 | } |
| 281 | |
Wang Nan | 77a0cf6 | 2015-11-17 08:32:49 +0000 | [diff] [blame] | 282 | int test__bpf_subtest_get_nr(void) |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 283 | { |
Wang Nan | 77a0cf6 | 2015-11-17 08:32:49 +0000 | [diff] [blame] | 284 | return (int)ARRAY_SIZE(bpf_testcase_table); |
| 285 | } |
| 286 | |
| 287 | const 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 Nan | 6a7d550 | 2016-01-25 09:55:53 +0000 | [diff] [blame] | 294 | static 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 Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 324 | int test__bpf(struct test *test __maybe_unused, int i) |
Wang Nan | 77a0cf6 | 2015-11-17 08:32:49 +0000 | [diff] [blame] | 325 | { |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 326 | int err; |
| 327 | |
Wang Nan | 77a0cf6 | 2015-11-17 08:32:49 +0000 | [diff] [blame] | 328 | if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table)) |
| 329 | return TEST_FAIL; |
| 330 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 331 | if (geteuid() != 0) { |
| 332 | pr_debug("Only root can run BPF test\n"); |
| 333 | return TEST_SKIP; |
| 334 | } |
| 335 | |
Wang Nan | 6a7d550 | 2016-01-25 09:55:53 +0000 | [diff] [blame] | 336 | if (check_env()) |
| 337 | return TEST_SKIP; |
| 338 | |
Wang Nan | 77a0cf6 | 2015-11-17 08:32:49 +0000 | [diff] [blame] | 339 | err = __test__bpf(i); |
| 340 | return err; |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | #else |
Wang Nan | 77a0cf6 | 2015-11-17 08:32:49 +0000 | [diff] [blame] | 344 | int test__bpf_subtest_get_nr(void) |
| 345 | { |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | const char *test__bpf_subtest_get_desc(int i __maybe_unused) |
| 350 | { |
| 351 | return NULL; |
| 352 | } |
| 353 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 354 | int test__bpf(struct test *test __maybe_unused, int i __maybe_unused) |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 355 | { |
| 356 | pr_debug("Skip BPF test because BPF support is not compiled\n"); |
| 357 | return TEST_SKIP; |
| 358 | } |
| 359 | #endif |