Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2020 Facebook */ |
| 3 | |
| 4 | #include <test_progs.h> |
Hou Tao | fa7f17d | 2021-10-04 17:48:57 +0800 | [diff] [blame] | 5 | #include <stdbool.h> |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 6 | #include "test_module_attach.skel.h" |
| 7 | |
| 8 | static int duration; |
| 9 | |
Hou Tao | fa7f17d | 2021-10-04 17:48:57 +0800 | [diff] [blame] | 10 | static int trigger_module_test_writable(int *val) |
| 11 | { |
| 12 | int fd, err; |
| 13 | char buf[65]; |
| 14 | ssize_t rd; |
| 15 | |
| 16 | fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY); |
| 17 | err = -errno; |
| 18 | if (!ASSERT_GE(fd, 0, "testmode_file_open")) |
| 19 | return err; |
| 20 | |
| 21 | rd = read(fd, buf, sizeof(buf) - 1); |
| 22 | err = -errno; |
| 23 | if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) { |
| 24 | close(fd); |
| 25 | return err; |
| 26 | } |
| 27 | |
| 28 | buf[rd] = '\0'; |
| 29 | *val = strtol(buf, NULL, 0); |
| 30 | close(fd); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Jiri Olsa | a1c05c3 | 2021-04-14 21:51:46 +0200 | [diff] [blame] | 35 | static int delete_module(const char *name, int flags) |
| 36 | { |
| 37 | return syscall(__NR_delete_module, name, flags); |
| 38 | } |
| 39 | |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 40 | void test_module_attach(void) |
| 41 | { |
| 42 | const int READ_SZ = 456; |
Qais Yousef | 407be92 | 2021-01-19 12:22:37 +0000 | [diff] [blame] | 43 | const int WRITE_SZ = 457; |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 44 | struct test_module_attach* skel; |
| 45 | struct test_module_attach__bss *bss; |
Jiri Olsa | a1c05c3 | 2021-04-14 21:51:46 +0200 | [diff] [blame] | 46 | struct bpf_link *link; |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 47 | int err; |
Hou Tao | fa7f17d | 2021-10-04 17:48:57 +0800 | [diff] [blame] | 48 | int writable_val = 0; |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 49 | |
Andrii Nakryiko | 2e33f83 | 2020-12-11 13:58:25 -0800 | [diff] [blame] | 50 | skel = test_module_attach__open(); |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 51 | if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) |
| 52 | return; |
| 53 | |
Andrii Nakryiko | 2e33f83 | 2020-12-11 13:58:25 -0800 | [diff] [blame] | 54 | err = bpf_program__set_attach_target(skel->progs.handle_fentry_manual, |
| 55 | 0, "bpf_testmod_test_read"); |
| 56 | ASSERT_OK(err, "set_attach_target"); |
| 57 | |
| 58 | err = test_module_attach__load(skel); |
| 59 | if (CHECK(err, "skel_load", "failed to load skeleton\n")) |
| 60 | return; |
| 61 | |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 62 | bss = skel->bss; |
| 63 | |
| 64 | err = test_module_attach__attach(skel); |
| 65 | if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) |
| 66 | goto cleanup; |
| 67 | |
| 68 | /* trigger tracepoint */ |
| 69 | ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read"); |
Qais Yousef | 407be92 | 2021-01-19 12:22:37 +0000 | [diff] [blame] | 70 | ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write"); |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 71 | |
| 72 | ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp"); |
Qais Yousef | 407be92 | 2021-01-19 12:22:37 +0000 | [diff] [blame] | 73 | ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare"); |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 74 | ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf"); |
| 75 | ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry"); |
Andrii Nakryiko | 2e33f83 | 2020-12-11 13:58:25 -0800 | [diff] [blame] | 76 | ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual"); |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 77 | ASSERT_EQ(bss->fexit_read_sz, READ_SZ, "fexit"); |
| 78 | ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet"); |
| 79 | ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret"); |
| 80 | |
Hou Tao | fa7f17d | 2021-10-04 17:48:57 +0800 | [diff] [blame] | 81 | bss->raw_tp_writable_bare_early_ret = true; |
| 82 | bss->raw_tp_writable_bare_out_val = 0xf1f2f3f4; |
| 83 | ASSERT_OK(trigger_module_test_writable(&writable_val), |
| 84 | "trigger_writable"); |
| 85 | ASSERT_EQ(bss->raw_tp_writable_bare_in_val, 1024, "writable_test_in"); |
| 86 | ASSERT_EQ(bss->raw_tp_writable_bare_out_val, writable_val, |
| 87 | "writable_test_out"); |
| 88 | |
Jiri Olsa | a1c05c3 | 2021-04-14 21:51:46 +0200 | [diff] [blame] | 89 | test_module_attach__detach(skel); |
| 90 | |
| 91 | /* attach fentry/fexit and make sure it get's module reference */ |
| 92 | link = bpf_program__attach(skel->progs.handle_fentry); |
| 93 | if (!ASSERT_OK_PTR(link, "attach_fentry")) |
| 94 | goto cleanup; |
| 95 | |
| 96 | ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module"); |
| 97 | bpf_link__destroy(link); |
| 98 | |
| 99 | link = bpf_program__attach(skel->progs.handle_fexit); |
| 100 | if (!ASSERT_OK_PTR(link, "attach_fexit")) |
| 101 | goto cleanup; |
| 102 | |
| 103 | ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module"); |
| 104 | bpf_link__destroy(link); |
| 105 | |
Jiri Olsa | e697d8d | 2022-10-25 15:41:47 +0200 | [diff] [blame] | 106 | link = bpf_program__attach(skel->progs.kprobe_multi); |
| 107 | if (!ASSERT_OK_PTR(link, "attach_kprobe_multi")) |
| 108 | goto cleanup; |
| 109 | |
| 110 | ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module"); |
| 111 | bpf_link__destroy(link); |
| 112 | |
Andrii Nakryiko | 1e38abe | 2020-12-03 12:46:34 -0800 | [diff] [blame] | 113 | cleanup: |
| 114 | test_module_attach__destroy(skel); |
| 115 | } |