0001
0002
0003
0004 #include <test_progs.h>
0005 #include <stdbool.h>
0006 #include "test_module_attach.skel.h"
0007
0008 static int duration;
0009
0010 static int trigger_module_test_writable(int *val)
0011 {
0012 int fd, err;
0013 char buf[65];
0014 ssize_t rd;
0015
0016 fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
0017 err = -errno;
0018 if (!ASSERT_GE(fd, 0, "testmode_file_open"))
0019 return err;
0020
0021 rd = read(fd, buf, sizeof(buf) - 1);
0022 err = -errno;
0023 if (!ASSERT_GT(rd, 0, "testmod_file_rd_val")) {
0024 close(fd);
0025 return err;
0026 }
0027
0028 buf[rd] = '\0';
0029 *val = strtol(buf, NULL, 0);
0030 close(fd);
0031
0032 return 0;
0033 }
0034
0035 static int delete_module(const char *name, int flags)
0036 {
0037 return syscall(__NR_delete_module, name, flags);
0038 }
0039
0040 void test_module_attach(void)
0041 {
0042 const int READ_SZ = 456;
0043 const int WRITE_SZ = 457;
0044 struct test_module_attach* skel;
0045 struct test_module_attach__bss *bss;
0046 struct bpf_link *link;
0047 int err;
0048 int writable_val = 0;
0049
0050 skel = test_module_attach__open();
0051 if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
0052 return;
0053
0054 err = bpf_program__set_attach_target(skel->progs.handle_fentry_manual,
0055 0, "bpf_testmod_test_read");
0056 ASSERT_OK(err, "set_attach_target");
0057
0058 err = test_module_attach__load(skel);
0059 if (CHECK(err, "skel_load", "failed to load skeleton\n"))
0060 return;
0061
0062 bss = skel->bss;
0063
0064 err = test_module_attach__attach(skel);
0065 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
0066 goto cleanup;
0067
0068
0069 ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read");
0070 ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write");
0071
0072 ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
0073 ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare");
0074 ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
0075 ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
0076 ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");
0077 ASSERT_EQ(bss->fexit_read_sz, READ_SZ, "fexit");
0078 ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet");
0079 ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret");
0080
0081 bss->raw_tp_writable_bare_early_ret = true;
0082 bss->raw_tp_writable_bare_out_val = 0xf1f2f3f4;
0083 ASSERT_OK(trigger_module_test_writable(&writable_val),
0084 "trigger_writable");
0085 ASSERT_EQ(bss->raw_tp_writable_bare_in_val, 1024, "writable_test_in");
0086 ASSERT_EQ(bss->raw_tp_writable_bare_out_val, writable_val,
0087 "writable_test_out");
0088
0089 test_module_attach__detach(skel);
0090
0091
0092 link = bpf_program__attach(skel->progs.handle_fentry);
0093 if (!ASSERT_OK_PTR(link, "attach_fentry"))
0094 goto cleanup;
0095
0096 ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module");
0097 bpf_link__destroy(link);
0098
0099 link = bpf_program__attach(skel->progs.handle_fexit);
0100 if (!ASSERT_OK_PTR(link, "attach_fexit"))
0101 goto cleanup;
0102
0103 ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module");
0104 bpf_link__destroy(link);
0105
0106 cleanup:
0107 test_module_attach__destroy(skel);
0108 }