1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4
5 struct meta {
6 int ifindex;
7 __u32 cb32_0;
8 __u8 cb8_0;
9 };
10
11 static union {
12 __u32 cb32[5];
13 __u8 cb8[20];
14 } cb = {
15 .cb32[0] = 0x81828384,
16 };
17
on_sample(void * ctx,int cpu,void * data,__u32 size)18 static void on_sample(void *ctx, int cpu, void *data, __u32 size)
19 {
20 struct meta *meta = (struct meta *)data;
21 struct ipv6_packet *pkt_v6 = data + sizeof(*meta);
22 int duration = 0;
23
24 if (CHECK(size != 72 + sizeof(*meta), "check_size", "size %u != %zu\n",
25 size, 72 + sizeof(*meta)))
26 return;
27 if (CHECK(meta->ifindex != 1, "check_meta_ifindex",
28 "meta->ifindex = %d\n", meta->ifindex))
29 /* spurious kfree_skb not on loopback device */
30 return;
31 if (CHECK(meta->cb8_0 != cb.cb8[0], "check_cb8_0", "cb8_0 %x != %x\n",
32 meta->cb8_0, cb.cb8[0]))
33 return;
34 if (CHECK(meta->cb32_0 != cb.cb32[0], "check_cb32_0",
35 "cb32_0 %x != %x\n",
36 meta->cb32_0, cb.cb32[0]))
37 return;
38 if (CHECK(pkt_v6->eth.h_proto != 0xdd86, "check_eth",
39 "h_proto %x\n", pkt_v6->eth.h_proto))
40 return;
41 if (CHECK(pkt_v6->iph.nexthdr != 6, "check_ip",
42 "iph.nexthdr %x\n", pkt_v6->iph.nexthdr))
43 return;
44 if (CHECK(pkt_v6->tcp.doff != 5, "check_tcp",
45 "tcp.doff %x\n", pkt_v6->tcp.doff))
46 return;
47
48 *(bool *)ctx = true;
49 }
50
51 /* TODO: fix kernel panic caused by this test in parallel mode */
serial_test_kfree_skb(void)52 void serial_test_kfree_skb(void)
53 {
54 struct __sk_buff skb = {};
55 struct bpf_prog_test_run_attr tattr = {
56 .data_in = &pkt_v6,
57 .data_size_in = sizeof(pkt_v6),
58 .ctx_in = &skb,
59 .ctx_size_in = sizeof(skb),
60 };
61 struct bpf_prog_load_attr attr = {
62 .file = "./kfree_skb.o",
63 };
64
65 struct bpf_link *link = NULL, *link_fentry = NULL, *link_fexit = NULL;
66 struct bpf_map *perf_buf_map, *global_data;
67 struct bpf_program *prog, *fentry, *fexit;
68 struct bpf_object *obj, *obj2 = NULL;
69 struct perf_buffer_opts pb_opts = {};
70 struct perf_buffer *pb = NULL;
71 int err, kfree_skb_fd;
72 bool passed = false;
73 __u32 duration = 0;
74 const int zero = 0;
75 bool test_ok[2];
76
77 err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
78 &obj, &tattr.prog_fd);
79 if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
80 return;
81
82 err = bpf_prog_load_xattr(&attr, &obj2, &kfree_skb_fd);
83 if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
84 goto close_prog;
85
86 prog = bpf_object__find_program_by_title(obj2, "tp_btf/kfree_skb");
87 if (CHECK(!prog, "find_prog", "prog kfree_skb not found\n"))
88 goto close_prog;
89 fentry = bpf_object__find_program_by_title(obj2, "fentry/eth_type_trans");
90 if (CHECK(!fentry, "find_prog", "prog eth_type_trans not found\n"))
91 goto close_prog;
92 fexit = bpf_object__find_program_by_title(obj2, "fexit/eth_type_trans");
93 if (CHECK(!fexit, "find_prog", "prog eth_type_trans not found\n"))
94 goto close_prog;
95
96 global_data = bpf_object__find_map_by_name(obj2, ".bss");
97 if (CHECK(!global_data, "find global data", "not found\n"))
98 goto close_prog;
99
100 link = bpf_program__attach_raw_tracepoint(prog, NULL);
101 if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
102 goto close_prog;
103 link_fentry = bpf_program__attach_trace(fentry);
104 if (!ASSERT_OK_PTR(link_fentry, "attach fentry"))
105 goto close_prog;
106 link_fexit = bpf_program__attach_trace(fexit);
107 if (!ASSERT_OK_PTR(link_fexit, "attach fexit"))
108 goto close_prog;
109
110 perf_buf_map = bpf_object__find_map_by_name(obj2, "perf_buf_map");
111 if (CHECK(!perf_buf_map, "find_perf_buf_map", "not found\n"))
112 goto close_prog;
113
114 /* set up perf buffer */
115 pb_opts.sample_cb = on_sample;
116 pb_opts.ctx = &passed;
117 pb = perf_buffer__new(bpf_map__fd(perf_buf_map), 1, &pb_opts);
118 if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
119 goto close_prog;
120
121 memcpy(skb.cb, &cb, sizeof(cb));
122 err = bpf_prog_test_run_xattr(&tattr);
123 duration = tattr.duration;
124 CHECK(err || tattr.retval, "ipv6",
125 "err %d errno %d retval %d duration %d\n",
126 err, errno, tattr.retval, duration);
127
128 /* read perf buffer */
129 err = perf_buffer__poll(pb, 100);
130 if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
131 goto close_prog;
132
133 /* make sure kfree_skb program was triggered
134 * and it sent expected skb into ring buffer
135 */
136 ASSERT_TRUE(passed, "passed");
137
138 err = bpf_map_lookup_elem(bpf_map__fd(global_data), &zero, test_ok);
139 if (CHECK(err, "get_result",
140 "failed to get output data: %d\n", err))
141 goto close_prog;
142
143 CHECK_FAIL(!test_ok[0] || !test_ok[1]);
144 close_prog:
145 perf_buffer__free(pb);
146 bpf_link__destroy(link);
147 bpf_link__destroy(link_fentry);
148 bpf_link__destroy(link_fexit);
149 bpf_object__close(obj);
150 bpf_object__close(obj2);
151 }
152