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 
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 
24 void test_module_attach(void)
25 {
26 	const int READ_SZ = 456;
27 	struct test_module_attach* skel;
28 	struct test_module_attach__bss *bss;
29 	int err;
30 
31 	skel = test_module_attach__open();
32 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
33 		return;
34 
35 	err = bpf_program__set_attach_target(skel->progs.handle_fentry_manual,
36 					     0, "bpf_testmod_test_read");
37 	ASSERT_OK(err, "set_attach_target");
38 
39 	err = test_module_attach__load(skel);
40 	if (CHECK(err, "skel_load", "failed to load skeleton\n"))
41 		return;
42 
43 	bss = skel->bss;
44 
45 	err = test_module_attach__attach(skel);
46 	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
47 		goto cleanup;
48 
49 	/* trigger tracepoint */
50 	ASSERT_OK(trigger_module_test_read(READ_SZ), "trigger_read");
51 
52 	ASSERT_EQ(bss->raw_tp_read_sz, READ_SZ, "raw_tp");
53 	ASSERT_EQ(bss->tp_btf_read_sz, READ_SZ, "tp_btf");
54 	ASSERT_EQ(bss->fentry_read_sz, READ_SZ, "fentry");
55 	ASSERT_EQ(bss->fentry_manual_read_sz, READ_SZ, "fentry_manual");
56 	ASSERT_EQ(bss->fexit_read_sz, READ_SZ, "fexit");
57 	ASSERT_EQ(bss->fexit_ret, -EIO, "fexit_tet");
58 	ASSERT_EQ(bss->fmod_ret_read_sz, READ_SZ, "fmod_ret");
59 
60 cleanup:
61 	test_module_attach__destroy(skel);
62 }
63