Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2020 Facebook */
3
4#include <test_progs.h>
5#include <stdbool.h>
6#include "test_module_attach.skel.h"
7
8static int duration;
9
10static 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
35static int delete_module(const char *name, int flags)
36{
37 return syscall(__NR_delete_module, name, flags);
38}
39
40void test_module_attach(void)
41{
42 const int READ_SZ = 456;
43 const int WRITE_SZ = 457;
44 struct test_module_attach* skel;
45 struct test_module_attach__bss *bss;
46 struct bpf_link *link;
47 int err;
48 int writable_val = 0;
49
50 skel = test_module_attach__open();
51 if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
52 return;
53
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
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");
70 ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write");
71
72 ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
73 ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare");
74 ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
75 ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
76 ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");
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
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
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
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
113cleanup:
114 test_module_attach__destroy(skel);
115}
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
7static int duration;
8
9static 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
24static 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
48static int delete_module(const char *name, int flags)
49{
50 return syscall(__NR_delete_module, name, flags);
51}
52
53void 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
110cleanup:
111 test_module_attach__destroy(skel);
112}