blob: 7fc01ff490db716e861df0ceefae3b9e3b45db68 [file] [log] [blame]
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -08001// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2020 Facebook */
3
4#include <test_progs.h>
Hou Taofa7f17d2021-10-04 17:48:57 +08005#include <stdbool.h>
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -08006#include "test_module_attach.skel.h"
7
8static int duration;
9
Hou Taofa7f17d2021-10-04 17:48:57 +080010static 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 Olsaa1c05c32021-04-14 21:51:46 +020035static int delete_module(const char *name, int flags)
36{
37 return syscall(__NR_delete_module, name, flags);
38}
39
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080040void test_module_attach(void)
41{
42 const int READ_SZ = 456;
Qais Yousef407be922021-01-19 12:22:37 +000043 const int WRITE_SZ = 457;
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080044 struct test_module_attach* skel;
45 struct test_module_attach__bss *bss;
Jiri Olsaa1c05c32021-04-14 21:51:46 +020046 struct bpf_link *link;
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080047 int err;
Hou Taofa7f17d2021-10-04 17:48:57 +080048 int writable_val = 0;
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080049
Andrii Nakryiko2e33f832020-12-11 13:58:25 -080050 skel = test_module_attach__open();
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080051 if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
52 return;
53
Andrii Nakryiko2e33f832020-12-11 13:58:25 -080054 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 Nakryiko1e38abe2020-12-03 12:46:34 -080062 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 Yousef407be922021-01-19 12:22:37 +000070 ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write");
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080071
72 ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
Qais Yousef407be922021-01-19 12:22:37 +000073 ASSERT_EQ(bss->raw_tp_bare_write_sz, WRITE_SZ, "raw_tp_bare");
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080074 ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
75 ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
Andrii Nakryiko2e33f832020-12-11 13:58:25 -080076 ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");
Andrii Nakryiko1e38abe2020-12-03 12:46:34 -080077 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 Taofa7f17d2021-10-04 17:48:57 +080081 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 Olsaa1c05c32021-04-14 21:51:46 +020089 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 Olsae697d8d2022-10-25 15:41:47 +0200106 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 Nakryiko1e38abe2020-12-03 12:46:34 -0800113cleanup:
114 test_module_attach__destroy(skel);
115}