1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include <net/if.h>
5 #include <linux/if_ether.h>
6 #include <linux/if_packet.h>
7 #include <linux/if_link.h>
8 #include <linux/ipv6.h>
9 #include <linux/in6.h>
10 #include <netinet/udp.h>
11 #include <bpf/bpf_endian.h>
12 #include <uapi/linux/netdev.h>
13 #include "test_xdp_do_redirect.skel.h"
14 #include "xdp_dummy.skel.h"
15 
16 struct udp_packet {
17 	struct ethhdr eth;
18 	struct ipv6hdr iph;
19 	struct udphdr udp;
20 	__u8 payload[64 - sizeof(struct udphdr)
21 		     - sizeof(struct ethhdr) - sizeof(struct ipv6hdr)];
22 } __packed;
23 
24 static struct udp_packet pkt_udp = {
25 	.eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
26 	.eth.h_dest = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55},
27 	.eth.h_source = {0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb},
28 	.iph.version = 6,
29 	.iph.nexthdr = IPPROTO_UDP,
30 	.iph.payload_len = bpf_htons(sizeof(struct udp_packet)
31 				     - offsetof(struct udp_packet, udp)),
32 	.iph.hop_limit = 2,
33 	.iph.saddr.s6_addr16 = {bpf_htons(0xfc00), 0, 0, 0, 0, 0, 0, bpf_htons(1)},
34 	.iph.daddr.s6_addr16 = {bpf_htons(0xfc00), 0, 0, 0, 0, 0, 0, bpf_htons(2)},
35 	.udp.source = bpf_htons(1),
36 	.udp.dest = bpf_htons(1),
37 	.udp.len = bpf_htons(sizeof(struct udp_packet)
38 			     - offsetof(struct udp_packet, udp)),
39 	.payload = {0x42}, /* receiver XDP program matches on this */
40 };
41 
attach_tc_prog(struct bpf_tc_hook * hook,int fd)42 static int attach_tc_prog(struct bpf_tc_hook *hook, int fd)
43 {
44 	DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 1, .prog_fd = fd);
45 	int ret;
46 
47 	ret = bpf_tc_hook_create(hook);
48 	if (!ASSERT_OK(ret, "create tc hook"))
49 		return ret;
50 
51 	ret = bpf_tc_attach(hook, &opts);
52 	if (!ASSERT_OK(ret, "bpf_tc_attach")) {
53 		bpf_tc_hook_destroy(hook);
54 		return ret;
55 	}
56 
57 	return 0;
58 }
59 
60 /* The maximum permissible size is: PAGE_SIZE - sizeof(struct xdp_page_head) -
61  * SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) - XDP_PACKET_HEADROOM =
62  * 3408 bytes for 64-byte cacheline and 3216 for 256-byte one.
63  */
64 #if defined(__s390x__)
65 #define MAX_PKT_SIZE 3216
66 #else
67 #define MAX_PKT_SIZE 3408
68 #endif
test_max_pkt_size(int fd)69 static void test_max_pkt_size(int fd)
70 {
71 	char data[MAX_PKT_SIZE + 1] = {};
72 	int err;
73 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
74 			    .data_in = &data,
75 			    .data_size_in = MAX_PKT_SIZE,
76 			    .flags = BPF_F_TEST_XDP_LIVE_FRAMES,
77 			    .repeat = 1,
78 		);
79 	err = bpf_prog_test_run_opts(fd, &opts);
80 	ASSERT_OK(err, "prog_run_max_size");
81 
82 	opts.data_size_in += 1;
83 	err = bpf_prog_test_run_opts(fd, &opts);
84 	ASSERT_EQ(err, -EINVAL, "prog_run_too_big");
85 }
86 
87 #define NUM_PKTS 10000
test_xdp_do_redirect(void)88 void test_xdp_do_redirect(void)
89 {
90 	int err, xdp_prog_fd, tc_prog_fd, ifindex_src, ifindex_dst;
91 	char data[sizeof(pkt_udp) + sizeof(__u64)];
92 	struct test_xdp_do_redirect *skel = NULL;
93 	struct nstoken *nstoken = NULL;
94 	struct bpf_link *link;
95 	LIBBPF_OPTS(bpf_xdp_query_opts, query_opts);
96 	struct xdp_md ctx_in = { .data = sizeof(__u64),
97 				 .data_end = sizeof(data) };
98 	DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
99 			    .data_in = &data,
100 			    .data_size_in = sizeof(data),
101 			    .ctx_in = &ctx_in,
102 			    .ctx_size_in = sizeof(ctx_in),
103 			    .flags = BPF_F_TEST_XDP_LIVE_FRAMES,
104 			    .repeat = NUM_PKTS,
105 			    .batch_size = 64,
106 		);
107 	DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
108 			    .attach_point = BPF_TC_INGRESS);
109 
110 	memcpy(&data[sizeof(__u64)], &pkt_udp, sizeof(pkt_udp));
111 	((__u32 *)data)[0] = 0x42; /* metadata test value */
112 	((__u32 *)data)[1] = 0;
113 
114 	skel = test_xdp_do_redirect__open();
115 	if (!ASSERT_OK_PTR(skel, "skel"))
116 		return;
117 
118 	/* The XDP program we run with bpf_prog_run() will cycle through all
119 	 * three xmit (PASS/TX/REDIRECT) return codes starting from above, and
120 	 * ending up with PASS, so we should end up with two packets on the dst
121 	 * iface and NUM_PKTS-2 in the TC hook. We match the packets on the UDP
122 	 * payload.
123 	 */
124 	SYS(out, "ip netns add testns");
125 	nstoken = open_netns("testns");
126 	if (!ASSERT_OK_PTR(nstoken, "setns"))
127 		goto out;
128 
129 	SYS(out, "ip link add veth_src type veth peer name veth_dst");
130 	SYS(out, "ip link set dev veth_src address 00:11:22:33:44:55");
131 	SYS(out, "ip link set dev veth_dst address 66:77:88:99:aa:bb");
132 	SYS(out, "ip link set dev veth_src up");
133 	SYS(out, "ip link set dev veth_dst up");
134 	SYS(out, "ip addr add dev veth_src fc00::1/64");
135 	SYS(out, "ip addr add dev veth_dst fc00::2/64");
136 	SYS(out, "ip neigh add fc00::2 dev veth_src lladdr 66:77:88:99:aa:bb");
137 
138 	/* We enable forwarding in the test namespace because that will cause
139 	 * the packets that go through the kernel stack (with XDP_PASS) to be
140 	 * forwarded back out the same interface (because of the packet dst
141 	 * combined with the interface addresses). When this happens, the
142 	 * regular forwarding path will end up going through the same
143 	 * veth_xdp_xmit() call as the XDP_REDIRECT code, which can cause a
144 	 * deadlock if it happens on the same CPU. There's a local_bh_disable()
145 	 * in the test_run code to prevent this, but an earlier version of the
146 	 * code didn't have this, so we keep the test behaviour to make sure the
147 	 * bug doesn't resurface.
148 	 */
149 	SYS(out, "sysctl -qw net.ipv6.conf.all.forwarding=1");
150 
151 	ifindex_src = if_nametoindex("veth_src");
152 	ifindex_dst = if_nametoindex("veth_dst");
153 	if (!ASSERT_NEQ(ifindex_src, 0, "ifindex_src") ||
154 	    !ASSERT_NEQ(ifindex_dst, 0, "ifindex_dst"))
155 		goto out;
156 
157 	/* Check xdp features supported by veth driver */
158 	err = bpf_xdp_query(ifindex_src, XDP_FLAGS_DRV_MODE, &query_opts);
159 	if (!ASSERT_OK(err, "veth_src bpf_xdp_query"))
160 		goto out;
161 
162 	if (!ASSERT_EQ(query_opts.feature_flags,
163 		       NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
164 		       NETDEV_XDP_ACT_RX_SG,
165 		       "veth_src query_opts.feature_flags"))
166 		goto out;
167 
168 	err = bpf_xdp_query(ifindex_dst, XDP_FLAGS_DRV_MODE, &query_opts);
169 	if (!ASSERT_OK(err, "veth_dst bpf_xdp_query"))
170 		goto out;
171 
172 	if (!ASSERT_EQ(query_opts.feature_flags,
173 		       NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
174 		       NETDEV_XDP_ACT_RX_SG,
175 		       "veth_dst query_opts.feature_flags"))
176 		goto out;
177 
178 	/* Enable GRO */
179 	SYS(out, "ethtool -K veth_src gro on");
180 	SYS(out, "ethtool -K veth_dst gro on");
181 
182 	err = bpf_xdp_query(ifindex_src, XDP_FLAGS_DRV_MODE, &query_opts);
183 	if (!ASSERT_OK(err, "veth_src bpf_xdp_query gro on"))
184 		goto out;
185 
186 	if (!ASSERT_EQ(query_opts.feature_flags,
187 		       NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
188 		       NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_RX_SG |
189 		       NETDEV_XDP_ACT_NDO_XMIT_SG,
190 		       "veth_src query_opts.feature_flags gro on"))
191 		goto out;
192 
193 	err = bpf_xdp_query(ifindex_dst, XDP_FLAGS_DRV_MODE, &query_opts);
194 	if (!ASSERT_OK(err, "veth_dst bpf_xdp_query gro on"))
195 		goto out;
196 
197 	if (!ASSERT_EQ(query_opts.feature_flags,
198 		       NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
199 		       NETDEV_XDP_ACT_NDO_XMIT | NETDEV_XDP_ACT_RX_SG |
200 		       NETDEV_XDP_ACT_NDO_XMIT_SG,
201 		       "veth_dst query_opts.feature_flags gro on"))
202 		goto out;
203 
204 	memcpy(skel->rodata->expect_dst, &pkt_udp.eth.h_dest, ETH_ALEN);
205 	skel->rodata->ifindex_out = ifindex_src; /* redirect back to the same iface */
206 	skel->rodata->ifindex_in = ifindex_src;
207 	ctx_in.ingress_ifindex = ifindex_src;
208 	tc_hook.ifindex = ifindex_src;
209 
210 	if (!ASSERT_OK(test_xdp_do_redirect__load(skel), "load"))
211 		goto out;
212 
213 	link = bpf_program__attach_xdp(skel->progs.xdp_count_pkts, ifindex_dst);
214 	if (!ASSERT_OK_PTR(link, "prog_attach"))
215 		goto out;
216 	skel->links.xdp_count_pkts = link;
217 
218 	tc_prog_fd = bpf_program__fd(skel->progs.tc_count_pkts);
219 	if (attach_tc_prog(&tc_hook, tc_prog_fd))
220 		goto out;
221 
222 	xdp_prog_fd = bpf_program__fd(skel->progs.xdp_redirect);
223 	err = bpf_prog_test_run_opts(xdp_prog_fd, &opts);
224 	if (!ASSERT_OK(err, "prog_run"))
225 		goto out_tc;
226 
227 	/* wait for the packets to be flushed */
228 	kern_sync_rcu();
229 
230 	/* There will be one packet sent through XDP_REDIRECT and one through
231 	 * XDP_TX; these will show up on the XDP counting program, while the
232 	 * rest will be counted at the TC ingress hook (and the counting program
233 	 * resets the packet payload so they don't get counted twice even though
234 	 * they are re-xmited out the veth device
235 	 */
236 	ASSERT_EQ(skel->bss->pkts_seen_xdp, 2, "pkt_count_xdp");
237 	ASSERT_EQ(skel->bss->pkts_seen_zero, 2, "pkt_count_zero");
238 	ASSERT_EQ(skel->bss->pkts_seen_tc, NUM_PKTS - 2, "pkt_count_tc");
239 
240 	test_max_pkt_size(bpf_program__fd(skel->progs.xdp_count_pkts));
241 
242 out_tc:
243 	bpf_tc_hook_destroy(&tc_hook);
244 out:
245 	if (nstoken)
246 		close_netns(nstoken);
247 	SYS_NOFAIL("ip netns del testns");
248 	test_xdp_do_redirect__destroy(skel);
249 }
250 
251 #define NS_NB		3
252 #define NS0		"NS0"
253 #define NS1		"NS1"
254 #define NS2		"NS2"
255 #define IPV4_NETWORK	"10.1.1"
256 #define VETH1_INDEX	111
257 #define VETH2_INDEX	222
258 
259 struct test_data {
260 	struct netns_obj *ns[NS_NB];
261 	u32 xdp_flags;
262 };
263 
cleanup(struct test_data * data)264 static void cleanup(struct test_data *data)
265 {
266 	int i;
267 
268 	for (i = 0; i < NS_NB; i++)
269 		netns_free(data->ns[i]);
270 }
271 
272 /**
273  * ping_setup -
274  * Create two veth peers and forward packets in-between using XDP
275  *
276  *    ------------           ------------
277  *    |    NS1   |           |    NS2   |
278  *    |   veth0  |           |   veth0  |
279  *    | 10.1.1.1 |           | 10.1.1.2 |
280  *    -----|------           ------|-----
281  *         |                       |
282  *         |                       |
283  *    -----|-----------------------|-------
284  *    |  veth1                   veth2    |
285  *    | (id:111)                (id:222)  |
286  *    |    |                        |     |
287  *    |    ----- xdp forwarding -----     |
288  *    |                                   |
289  *    |               NS0                 |
290  *    -------------------------------------
291  */
ping_setup(struct test_data * data)292 static int ping_setup(struct test_data *data)
293 {
294 	int i;
295 
296 	data->ns[0] = netns_new(NS0, false);
297 	if (!ASSERT_OK_PTR(data->ns[0], "create ns"))
298 		return -1;
299 
300 	for (i = 1; i < NS_NB; i++) {
301 		char ns_name[4] = {};
302 
303 		snprintf(ns_name, 4, "NS%d", i);
304 		data->ns[i] = netns_new(ns_name, false);
305 		if (!ASSERT_OK_PTR(data->ns[i], "create ns"))
306 			goto fail;
307 
308 		SYS(fail,
309 		    "ip -n %s link add veth%d index %d%d%d type veth peer name veth0 netns %s",
310 		    NS0, i, i, i, i, ns_name);
311 		SYS(fail, "ip -n %s link set veth%d up", NS0, i);
312 
313 		SYS(fail, "ip -n %s addr add %s.%d/24 dev veth0", ns_name, IPV4_NETWORK, i);
314 		SYS(fail, "ip -n %s link set veth0 up", ns_name);
315 	}
316 
317 	return 0;
318 
319 fail:
320 	cleanup(data);
321 	return -1;
322 }
323 
ping_test(struct test_data * data)324 static void ping_test(struct test_data *data)
325 {
326 	struct test_xdp_do_redirect *skel = NULL;
327 	struct xdp_dummy *skel_dummy = NULL;
328 	struct nstoken *nstoken = NULL;
329 	int i, ret;
330 
331 	skel_dummy = xdp_dummy__open_and_load();
332 	if (!ASSERT_OK_PTR(skel_dummy, "open and load xdp_dummy skeleton"))
333 		goto close;
334 
335 	for (i = 1; i < NS_NB; i++) {
336 		char ns_name[4] = {};
337 
338 		snprintf(ns_name, 4, "NS%d", i);
339 		nstoken = open_netns(ns_name);
340 		if (!ASSERT_OK_PTR(nstoken, "open ns"))
341 			goto close;
342 
343 		ret = bpf_xdp_attach(if_nametoindex("veth0"),
344 				     bpf_program__fd(skel_dummy->progs.xdp_dummy_prog),
345 				     data->xdp_flags, NULL);
346 		if (!ASSERT_GE(ret, 0, "bpf_xdp_attach dummy_prog"))
347 			goto close;
348 
349 		close_netns(nstoken);
350 		nstoken = NULL;
351 	}
352 
353 	skel = test_xdp_do_redirect__open_and_load();
354 	if (!ASSERT_OK_PTR(skel, "open and load skeleton"))
355 		goto close;
356 
357 	nstoken = open_netns(NS0);
358 	if (!ASSERT_OK_PTR(nstoken, "open NS0"))
359 		goto close;
360 
361 	ret = bpf_xdp_attach(VETH2_INDEX,
362 			     bpf_program__fd(skel->progs.xdp_redirect_to_111),
363 			     data->xdp_flags, NULL);
364 	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
365 		goto close;
366 
367 	ret = bpf_xdp_attach(VETH1_INDEX,
368 			     bpf_program__fd(skel->progs.xdp_redirect_to_222),
369 			     data->xdp_flags, NULL);
370 	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
371 		goto close;
372 
373 	close_netns(nstoken);
374 	nstoken = NULL;
375 
376 	nstoken = open_netns(NS1);
377 	if (!ASSERT_OK_PTR(nstoken, "open NS1"))
378 		goto close;
379 
380 	SYS(close, "ping -c 1 %s.2 > /dev/null", IPV4_NETWORK);
381 
382 close:
383 	close_netns(nstoken);
384 	xdp_dummy__destroy(skel_dummy);
385 	test_xdp_do_redirect__destroy(skel);
386 }
387 
388 
xdp_redirect_ping(u32 xdp_flags)389 static void xdp_redirect_ping(u32 xdp_flags)
390 {
391 	struct test_data data = {};
392 
393 	if (ping_setup(&data) < 0)
394 		return;
395 
396 	data.xdp_flags = xdp_flags;
397 	ping_test(&data);
398 	cleanup(&data);
399 }
400 
test_xdp_index_redirect(void)401 void test_xdp_index_redirect(void)
402 {
403 	if (test__start_subtest("noflag"))
404 		xdp_redirect_ping(0);
405 
406 	if (test__start_subtest("drvflag"))
407 		xdp_redirect_ping(XDP_FLAGS_DRV_MODE);
408 
409 	if (test__start_subtest("skbflag"))
410 		xdp_redirect_ping(XDP_FLAGS_SKB_MODE);
411 }
412 
413