Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <errno.h>
0003 #include <stdio.h>
0004 #include <stdlib.h>
0005 #include <sys/epoll.h>
0006 #include <sys/types.h>
0007 #include <sys/stat.h>
0008 #include <fcntl.h>
0009 #include <util/record.h>
0010 #include <util/util.h>
0011 #include <util/bpf-loader.h>
0012 #include <util/evlist.h>
0013 #include <linux/filter.h>
0014 #include <linux/kernel.h>
0015 #include <linux/string.h>
0016 #include <api/fs/fs.h>
0017 #include <perf/mmap.h>
0018 #include "tests.h"
0019 #include "llvm.h"
0020 #include "debug.h"
0021 #include "parse-events.h"
0022 #include "util/mmap.h"
0023 #define NR_ITERS       111
0024 #define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
0025 
0026 #ifdef HAVE_LIBBPF_SUPPORT
0027 #include <linux/bpf.h>
0028 #include <bpf/bpf.h>
0029 
0030 static int epoll_pwait_loop(void)
0031 {
0032     int i;
0033 
0034     /* Should fail NR_ITERS times */
0035     for (i = 0; i < NR_ITERS; i++)
0036         epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
0037     return 0;
0038 }
0039 
0040 #ifdef HAVE_BPF_PROLOGUE
0041 
0042 static int llseek_loop(void)
0043 {
0044     int fds[2], i;
0045 
0046     fds[0] = open("/dev/null", O_RDONLY);
0047     fds[1] = open("/dev/null", O_RDWR);
0048 
0049     if (fds[0] < 0 || fds[1] < 0)
0050         return -1;
0051 
0052     for (i = 0; i < NR_ITERS; i++) {
0053         lseek(fds[i % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
0054         lseek(fds[(i + 1) % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
0055     }
0056     close(fds[0]);
0057     close(fds[1]);
0058     return 0;
0059 }
0060 
0061 #endif
0062 
0063 static struct {
0064     enum test_llvm__testcase prog_id;
0065     const char *name;
0066     const char *msg_compile_fail;
0067     const char *msg_load_fail;
0068     int (*target_func)(void);
0069     int expect_result;
0070     bool    pin;
0071 } bpf_testcase_table[] = {
0072     {
0073         .prog_id      = LLVM_TESTCASE_BASE,
0074         .name         = "[basic_bpf_test]",
0075         .msg_compile_fail = "fix 'perf test LLVM' first",
0076         .msg_load_fail    = "load bpf object failed",
0077         .target_func      = &epoll_pwait_loop,
0078         .expect_result    = (NR_ITERS + 1) / 2,
0079     },
0080     {
0081         .prog_id      = LLVM_TESTCASE_BASE,
0082         .name         = "[bpf_pinning]",
0083         .msg_compile_fail = "fix kbuild first",
0084         .msg_load_fail    = "check your vmlinux setting?",
0085         .target_func      = &epoll_pwait_loop,
0086         .expect_result    = (NR_ITERS + 1) / 2,
0087         .pin          = true,
0088     },
0089 #ifdef HAVE_BPF_PROLOGUE
0090     {
0091         .prog_id      = LLVM_TESTCASE_BPF_PROLOGUE,
0092         .name         = "[bpf_prologue_test]",
0093         .msg_compile_fail = "fix kbuild first",
0094         .msg_load_fail    = "check your vmlinux setting?",
0095         .target_func      = &llseek_loop,
0096         .expect_result    = (NR_ITERS + 1) / 4,
0097     },
0098 #endif
0099 };
0100 
0101 static int do_test(struct bpf_object *obj, int (*func)(void),
0102            int expect)
0103 {
0104     struct record_opts opts = {
0105         .target = {
0106             .uid = UINT_MAX,
0107             .uses_mmap = true,
0108         },
0109         .freq         = 0,
0110         .mmap_pages   = 256,
0111         .default_interval = 1,
0112     };
0113 
0114     char pid[16];
0115     char sbuf[STRERR_BUFSIZE];
0116     struct evlist *evlist;
0117     int i, ret = TEST_FAIL, err = 0, count = 0;
0118 
0119     struct parse_events_state parse_state;
0120     struct parse_events_error parse_error;
0121 
0122     parse_events_error__init(&parse_error);
0123     bzero(&parse_state, sizeof(parse_state));
0124     parse_state.error = &parse_error;
0125     INIT_LIST_HEAD(&parse_state.list);
0126 
0127     err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
0128     parse_events_error__exit(&parse_error);
0129     if (err || list_empty(&parse_state.list)) {
0130         pr_debug("Failed to add events selected by BPF\n");
0131         return TEST_FAIL;
0132     }
0133 
0134     snprintf(pid, sizeof(pid), "%d", getpid());
0135     pid[sizeof(pid) - 1] = '\0';
0136     opts.target.tid = opts.target.pid = pid;
0137 
0138     /* Instead of evlist__new_default, don't add default events */
0139     evlist = evlist__new();
0140     if (!evlist) {
0141         pr_debug("Not enough memory to create evlist\n");
0142         return TEST_FAIL;
0143     }
0144 
0145     err = evlist__create_maps(evlist, &opts.target);
0146     if (err < 0) {
0147         pr_debug("Not enough memory to create thread/cpu maps\n");
0148         goto out_delete_evlist;
0149     }
0150 
0151     evlist__splice_list_tail(evlist, &parse_state.list);
0152     evlist->core.nr_groups = parse_state.nr_groups;
0153 
0154     evlist__config(evlist, &opts, NULL);
0155 
0156     err = evlist__open(evlist);
0157     if (err < 0) {
0158         pr_debug("perf_evlist__open: %s\n",
0159              str_error_r(errno, sbuf, sizeof(sbuf)));
0160         goto out_delete_evlist;
0161     }
0162 
0163     err = evlist__mmap(evlist, opts.mmap_pages);
0164     if (err < 0) {
0165         pr_debug("evlist__mmap: %s\n",
0166              str_error_r(errno, sbuf, sizeof(sbuf)));
0167         goto out_delete_evlist;
0168     }
0169 
0170     evlist__enable(evlist);
0171     (*func)();
0172     evlist__disable(evlist);
0173 
0174     for (i = 0; i < evlist->core.nr_mmaps; i++) {
0175         union perf_event *event;
0176         struct mmap *md;
0177 
0178         md = &evlist->mmap[i];
0179         if (perf_mmap__read_init(&md->core) < 0)
0180             continue;
0181 
0182         while ((event = perf_mmap__read_event(&md->core)) != NULL) {
0183             const u32 type = event->header.type;
0184 
0185             if (type == PERF_RECORD_SAMPLE)
0186                 count ++;
0187         }
0188         perf_mmap__read_done(&md->core);
0189     }
0190 
0191     if (count != expect * evlist->core.nr_entries) {
0192         pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect * evlist->core.nr_entries, count);
0193         goto out_delete_evlist;
0194     }
0195 
0196     ret = TEST_OK;
0197 
0198 out_delete_evlist:
0199     evlist__delete(evlist);
0200     return ret;
0201 }
0202 
0203 static struct bpf_object *
0204 prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
0205 {
0206     struct bpf_object *obj;
0207 
0208     obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
0209     if (IS_ERR(obj)) {
0210         pr_debug("Compile BPF program failed.\n");
0211         return NULL;
0212     }
0213     return obj;
0214 }
0215 
0216 static int __test__bpf(int idx)
0217 {
0218     int ret;
0219     void *obj_buf;
0220     size_t obj_buf_sz;
0221     struct bpf_object *obj;
0222 
0223     ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
0224                        bpf_testcase_table[idx].prog_id,
0225                        false, NULL);
0226     if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
0227         pr_debug("Unable to get BPF object, %s\n",
0228              bpf_testcase_table[idx].msg_compile_fail);
0229         if ((idx == 0) || (ret == TEST_SKIP))
0230             return TEST_SKIP;
0231         else
0232             return TEST_FAIL;
0233     }
0234 
0235     obj = prepare_bpf(obj_buf, obj_buf_sz,
0236               bpf_testcase_table[idx].name);
0237     if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
0238         if (!obj)
0239             pr_debug("Fail to load BPF object: %s\n",
0240                  bpf_testcase_table[idx].msg_load_fail);
0241         else
0242             pr_debug("Success unexpectedly: %s\n",
0243                  bpf_testcase_table[idx].msg_load_fail);
0244         ret = TEST_FAIL;
0245         goto out;
0246     }
0247 
0248     if (obj) {
0249         ret = do_test(obj,
0250                   bpf_testcase_table[idx].target_func,
0251                   bpf_testcase_table[idx].expect_result);
0252         if (ret != TEST_OK)
0253             goto out;
0254         if (bpf_testcase_table[idx].pin) {
0255             int err;
0256 
0257             if (!bpf_fs__mount()) {
0258                 pr_debug("BPF filesystem not mounted\n");
0259                 ret = TEST_FAIL;
0260                 goto out;
0261             }
0262             err = mkdir(PERF_TEST_BPF_PATH, 0777);
0263             if (err && errno != EEXIST) {
0264                 pr_debug("Failed to make perf_test dir: %s\n",
0265                      strerror(errno));
0266                 ret = TEST_FAIL;
0267                 goto out;
0268             }
0269             if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
0270                 ret = TEST_FAIL;
0271             if (rm_rf(PERF_TEST_BPF_PATH))
0272                 ret = TEST_FAIL;
0273         }
0274     }
0275 
0276 out:
0277     free(obj_buf);
0278     bpf__clear();
0279     return ret;
0280 }
0281 
0282 static int check_env(void)
0283 {
0284     LIBBPF_OPTS(bpf_prog_load_opts, opts);
0285     int err;
0286     char license[] = "GPL";
0287 
0288     struct bpf_insn insns[] = {
0289         BPF_MOV64_IMM(BPF_REG_0, 1),
0290         BPF_EXIT_INSN(),
0291     };
0292 
0293     err = fetch_kernel_version(&opts.kern_version, NULL, 0);
0294     if (err) {
0295         pr_debug("Unable to get kernel version\n");
0296         return err;
0297     }
0298     err = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, license, insns,
0299                 ARRAY_SIZE(insns), &opts);
0300     if (err < 0) {
0301         pr_err("Missing basic BPF support, skip this test: %s\n",
0302                strerror(errno));
0303         return err;
0304     }
0305     close(err);
0306 
0307     return 0;
0308 }
0309 
0310 static int test__bpf(int i)
0311 {
0312     int err;
0313 
0314     if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
0315         return TEST_FAIL;
0316 
0317     if (geteuid() != 0) {
0318         pr_debug("Only root can run BPF test\n");
0319         return TEST_SKIP;
0320     }
0321 
0322     if (check_env())
0323         return TEST_SKIP;
0324 
0325     err = __test__bpf(i);
0326     return err;
0327 }
0328 #endif
0329 
0330 static int test__basic_bpf_test(struct test_suite *test __maybe_unused,
0331                 int subtest __maybe_unused)
0332 {
0333 #ifdef HAVE_LIBBPF_SUPPORT
0334     return test__bpf(0);
0335 #else
0336     pr_debug("Skip BPF test because BPF support is not compiled\n");
0337     return TEST_SKIP;
0338 #endif
0339 }
0340 
0341 static int test__bpf_pinning(struct test_suite *test __maybe_unused,
0342                  int subtest __maybe_unused)
0343 {
0344 #ifdef HAVE_LIBBPF_SUPPORT
0345     return test__bpf(1);
0346 #else
0347     pr_debug("Skip BPF test because BPF support is not compiled\n");
0348     return TEST_SKIP;
0349 #endif
0350 }
0351 
0352 static int test__bpf_prologue_test(struct test_suite *test __maybe_unused,
0353                    int subtest __maybe_unused)
0354 {
0355 #if defined(HAVE_LIBBPF_SUPPORT) && defined(HAVE_BPF_PROLOGUE)
0356     return test__bpf(2);
0357 #else
0358     pr_debug("Skip BPF test because BPF support is not compiled\n");
0359     return TEST_SKIP;
0360 #endif
0361 }
0362 
0363 
0364 static struct test_case bpf_tests[] = {
0365 #ifdef HAVE_LIBBPF_SUPPORT
0366     TEST_CASE("Basic BPF filtering", basic_bpf_test),
0367     TEST_CASE_REASON("BPF pinning", bpf_pinning,
0368             "clang isn't installed or environment missing BPF support"),
0369 #ifdef HAVE_BPF_PROLOGUE
0370     TEST_CASE_REASON("BPF prologue generation", bpf_prologue_test,
0371             "clang isn't installed or environment missing BPF support"),
0372 #else
0373     TEST_CASE_REASON("BPF prologue generation", bpf_prologue_test, "not compiled in"),
0374 #endif
0375 #else
0376     TEST_CASE_REASON("Basic BPF filtering", basic_bpf_test, "not compiled in"),
0377     TEST_CASE_REASON("BPF pinning", bpf_pinning, "not compiled in"),
0378     TEST_CASE_REASON("BPF prologue generation", bpf_prologue_test, "not compiled in"),
0379 #endif
0380     { .name = NULL, }
0381 };
0382 
0383 struct test_suite suite__bpf = {
0384     .desc = "BPF filter",
0385     .test_cases = bpf_tests,
0386 };