1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3
4 #include <test_progs.h>
5 #include "test_module_attach.skel.h"
6
7 static int duration;
8
trigger_module_test_read(int read_sz)9 static int trigger_module_test_read(int read_sz)
10 {
11 int fd, err;
12
13 fd = open("/sys/kernel/bpf_testmod", O_RDONLY);
14 err = -errno;
15 if (CHECK(fd < 0, "testmod_file_open", "failed: %d\n", err))
16 return err;
17
18 read(fd, NULL, read_sz);
19 close(fd);
20
21 return 0;
22 }
23
trigger_module_test_write(int write_sz)24 static int trigger_module_test_write(int write_sz)
25 {
26 int fd, err;
27 char *buf = malloc(write_sz);
28
29 if (!buf)
30 return -ENOMEM;
31
32 memset(buf, 'a', write_sz);
33 buf[write_sz-1] = '\0';
34
35 fd = open("/sys/kernel/bpf_testmod", O_WRONLY);
36 err = -errno;
37 if (CHECK(fd < 0, "testmod_file_open", "failed: %d\n", err)) {
38 free(buf);
39 return err;
40 }
41
42 write(fd, buf, write_sz);
43 close(fd);
44 free(buf);
45 return 0;
46 }
47
delete_module(const char * name,int flags)48 static int delete_module(const char *name, int flags)
49 {
50 return syscall(__NR_delete_module, name, flags);
51 }
52
test_module_attach(void)53 void test_module_attach(void)
54 {
55 const int READ_SZ = 456;
56 const int WRITE_SZ = 457;
57 struct test_module_attach* skel;
58 struct test_module_attach__bss *bss;
59 struct bpf_link *link;
60 int err;
61
62 skel = test_module_attach__open();
63 if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
64 return;
65
66 err = bpf_program__set_attach_target(skel->progs.handle_fentry_manual,
67 0, "bpf_testmod_test_read");
68 ASSERT_OK(err, "set_attach_target");
69
70 err = test_module_attach__load(skel);
71 if (CHECK(err, "skel_load", "failed to load skeleton\n"))
72 return;
73
74 bss = skel->bss;
75
76 err = test_module_attach__attach(skel);
77 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
78 goto cleanup;
79
80 /* trigger tracepoint */
81 ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read");
82 ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write");
83
84 ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
85 ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare");
86 ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
87 ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
88 ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");
89 ASSERT_EQ(bss->fexit_read_sz, READ_SZ, "fexit");
90 ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet");
91 ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret");
92
93 test_module_attach__detach(skel);
94
95 /* attach fentry/fexit and make sure it get's module reference */
96 link = bpf_program__attach(skel->progs.handle_fentry);
97 if (!ASSERT_OK_PTR(link, "attach_fentry"))
98 goto cleanup;
99
100 ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module");
101 bpf_link__destroy(link);
102
103 link = bpf_program__attach(skel->progs.handle_fexit);
104 if (!ASSERT_OK_PTR(link, "attach_fexit"))
105 goto cleanup;
106
107 ASSERT_ERR(delete_module("bpf_testmod", 0), "delete_module");
108 bpf_link__destroy(link);
109
110 cleanup:
111 test_module_attach__destroy(skel);
112 }
113