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