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