1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4
test_fentry_fexit(void)5 void test_fentry_fexit(void)
6 {
7 struct bpf_prog_load_attr attr_fentry = {
8 .file = "./fentry_test.o",
9 };
10 struct bpf_prog_load_attr attr_fexit = {
11 .file = "./fexit_test.o",
12 };
13
14 struct bpf_object *obj_fentry = NULL, *obj_fexit = NULL, *pkt_obj;
15 struct bpf_map *data_map_fentry, *data_map_fexit;
16 char fentry_name[] = "fentry/bpf_fentry_testX";
17 char fexit_name[] = "fexit/bpf_fentry_testX";
18 int err, pkt_fd, kfree_skb_fd, i;
19 struct bpf_link *link[12] = {};
20 struct bpf_program *prog[12];
21 __u32 duration, retval;
22 const int zero = 0;
23 u64 result[12];
24
25 err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
26 &pkt_obj, &pkt_fd);
27 if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
28 return;
29 err = bpf_prog_load_xattr(&attr_fentry, &obj_fentry, &kfree_skb_fd);
30 if (CHECK(err, "prog_load fail", "err %d errno %d\n", err, errno))
31 goto close_prog;
32 err = bpf_prog_load_xattr(&attr_fexit, &obj_fexit, &kfree_skb_fd);
33 if (CHECK(err, "prog_load fail", "err %d errno %d\n", err, errno))
34 goto close_prog;
35
36 for (i = 0; i < 6; i++) {
37 fentry_name[sizeof(fentry_name) - 2] = '1' + i;
38 prog[i] = bpf_object__find_program_by_title(obj_fentry, fentry_name);
39 if (CHECK(!prog[i], "find_prog", "prog %s not found\n", fentry_name))
40 goto close_prog;
41 link[i] = bpf_program__attach_trace(prog[i]);
42 if (CHECK(IS_ERR(link[i]), "attach_trace", "failed to link\n"))
43 goto close_prog;
44 }
45 data_map_fentry = bpf_object__find_map_by_name(obj_fentry, "fentry_t.bss");
46 if (CHECK(!data_map_fentry, "find_data_map", "data map not found\n"))
47 goto close_prog;
48
49 for (i = 6; i < 12; i++) {
50 fexit_name[sizeof(fexit_name) - 2] = '1' + i - 6;
51 prog[i] = bpf_object__find_program_by_title(obj_fexit, fexit_name);
52 if (CHECK(!prog[i], "find_prog", "prog %s not found\n", fexit_name))
53 goto close_prog;
54 link[i] = bpf_program__attach_trace(prog[i]);
55 if (CHECK(IS_ERR(link[i]), "attach_trace", "failed to link\n"))
56 goto close_prog;
57 }
58 data_map_fexit = bpf_object__find_map_by_name(obj_fexit, "fexit_te.bss");
59 if (CHECK(!data_map_fexit, "find_data_map", "data map not found\n"))
60 goto close_prog;
61
62 err = bpf_prog_test_run(pkt_fd, 1, &pkt_v6, sizeof(pkt_v6),
63 NULL, NULL, &retval, &duration);
64 CHECK(err || retval, "ipv6",
65 "err %d errno %d retval %d duration %d\n",
66 err, errno, retval, duration);
67
68 err = bpf_map_lookup_elem(bpf_map__fd(data_map_fentry), &zero, &result);
69 if (CHECK(err, "get_result",
70 "failed to get output data: %d\n", err))
71 goto close_prog;
72
73 err = bpf_map_lookup_elem(bpf_map__fd(data_map_fexit), &zero, result + 6);
74 if (CHECK(err, "get_result",
75 "failed to get output data: %d\n", err))
76 goto close_prog;
77
78 for (i = 0; i < 12; i++)
79 if (CHECK(result[i] != 1, "result", "bpf_fentry_test%d failed err %ld\n",
80 i % 6 + 1, result[i]))
81 goto close_prog;
82
83 close_prog:
84 for (i = 0; i < 12; i++)
85 if (!IS_ERR_OR_NULL(link[i]))
86 bpf_link__destroy(link[i]);
87 bpf_object__close(obj_fentry);
88 bpf_object__close(obj_fexit);
89 bpf_object__close(pkt_obj);
90 }
91