1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include "xdp_metadata.skel.h"
5 #include "xdp_metadata2.skel.h"
6 #include "xdp_metadata.h"
7 #include "xsk.h"
8 
9 #include <bpf/btf.h>
10 #include <linux/errqueue.h>
11 #include <linux/if_link.h>
12 #include <linux/net_tstamp.h>
13 #include <linux/udp.h>
14 #include <sys/mman.h>
15 #include <net/if.h>
16 #include <poll.h>
17 
18 #define TX_NAME "veTX"
19 #define RX_NAME "veRX"
20 
21 #define UDP_PAYLOAD_BYTES 4
22 
23 #define AF_XDP_SOURCE_PORT 1234
24 #define AF_XDP_CONSUMER_PORT 8080
25 
26 #define UMEM_NUM 16
27 #define UMEM_FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE
28 #define UMEM_SIZE (UMEM_FRAME_SIZE * UMEM_NUM)
29 #define XDP_FLAGS XDP_FLAGS_DRV_MODE
30 #define QUEUE_ID 0
31 
32 #define TX_ADDR "10.0.0.1"
33 #define RX_ADDR "10.0.0.2"
34 #define PREFIX_LEN "8"
35 #define FAMILY AF_INET
36 
37 #define SYS(cmd) ({ \
38 	if (!ASSERT_OK(system(cmd), (cmd))) \
39 		goto out; \
40 })
41 
42 struct xsk {
43 	void *umem_area;
44 	struct xsk_umem *umem;
45 	struct xsk_ring_prod fill;
46 	struct xsk_ring_cons comp;
47 	struct xsk_ring_prod tx;
48 	struct xsk_ring_cons rx;
49 	struct xsk_socket *socket;
50 };
51 
52 static int open_xsk(int ifindex, struct xsk *xsk)
53 {
54 	int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
55 	const struct xsk_socket_config socket_config = {
56 		.rx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
57 		.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
58 		.bind_flags = XDP_COPY,
59 	};
60 	const struct xsk_umem_config umem_config = {
61 		.fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
62 		.comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
63 		.frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE,
64 		.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG,
65 	};
66 	__u32 idx;
67 	u64 addr;
68 	int ret;
69 	int i;
70 
71 	xsk->umem_area = mmap(NULL, UMEM_SIZE, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
72 	if (!ASSERT_NEQ(xsk->umem_area, MAP_FAILED, "mmap"))
73 		return -1;
74 
75 	ret = xsk_umem__create(&xsk->umem,
76 			       xsk->umem_area, UMEM_SIZE,
77 			       &xsk->fill,
78 			       &xsk->comp,
79 			       &umem_config);
80 	if (!ASSERT_OK(ret, "xsk_umem__create"))
81 		return ret;
82 
83 	ret = xsk_socket__create(&xsk->socket, ifindex, QUEUE_ID,
84 				 xsk->umem,
85 				 &xsk->rx,
86 				 &xsk->tx,
87 				 &socket_config);
88 	if (!ASSERT_OK(ret, "xsk_socket__create"))
89 		return ret;
90 
91 	/* First half of umem is for TX. This way address matches 1-to-1
92 	 * to the completion queue index.
93 	 */
94 
95 	for (i = 0; i < UMEM_NUM / 2; i++) {
96 		addr = i * UMEM_FRAME_SIZE;
97 		printf("%p: tx_desc[%d] -> %lx\n", xsk, i, addr);
98 	}
99 
100 	/* Second half of umem is for RX. */
101 
102 	ret = xsk_ring_prod__reserve(&xsk->fill, UMEM_NUM / 2, &idx);
103 	if (!ASSERT_EQ(UMEM_NUM / 2, ret, "xsk_ring_prod__reserve"))
104 		return ret;
105 	if (!ASSERT_EQ(idx, 0, "fill idx != 0"))
106 		return -1;
107 
108 	for (i = 0; i < UMEM_NUM / 2; i++) {
109 		addr = (UMEM_NUM / 2 + i) * UMEM_FRAME_SIZE;
110 		printf("%p: rx_desc[%d] -> %lx\n", xsk, i, addr);
111 		*xsk_ring_prod__fill_addr(&xsk->fill, i) = addr;
112 	}
113 	xsk_ring_prod__submit(&xsk->fill, ret);
114 
115 	return 0;
116 }
117 
118 static void close_xsk(struct xsk *xsk)
119 {
120 	if (xsk->umem)
121 		xsk_umem__delete(xsk->umem);
122 	if (xsk->socket)
123 		xsk_socket__delete(xsk->socket);
124 	munmap(xsk->umem_area, UMEM_SIZE);
125 }
126 
127 static void ip_csum(struct iphdr *iph)
128 {
129 	__u32 sum = 0;
130 	__u16 *p;
131 	int i;
132 
133 	iph->check = 0;
134 	p = (void *)iph;
135 	for (i = 0; i < sizeof(*iph) / sizeof(*p); i++)
136 		sum += p[i];
137 
138 	while (sum >> 16)
139 		sum = (sum & 0xffff) + (sum >> 16);
140 
141 	iph->check = ~sum;
142 }
143 
144 static int generate_packet(struct xsk *xsk, __u16 dst_port)
145 {
146 	struct xdp_desc *tx_desc;
147 	struct udphdr *udph;
148 	struct ethhdr *eth;
149 	struct iphdr *iph;
150 	void *data;
151 	__u32 idx;
152 	int ret;
153 
154 	ret = xsk_ring_prod__reserve(&xsk->tx, 1, &idx);
155 	if (!ASSERT_EQ(ret, 1, "xsk_ring_prod__reserve"))
156 		return -1;
157 
158 	tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx);
159 	tx_desc->addr = idx % (UMEM_NUM / 2) * UMEM_FRAME_SIZE;
160 	printf("%p: tx_desc[%u]->addr=%llx\n", xsk, idx, tx_desc->addr);
161 	data = xsk_umem__get_data(xsk->umem_area, tx_desc->addr);
162 
163 	eth = data;
164 	iph = (void *)(eth + 1);
165 	udph = (void *)(iph + 1);
166 
167 	memcpy(eth->h_dest, "\x00\x00\x00\x00\x00\x02", ETH_ALEN);
168 	memcpy(eth->h_source, "\x00\x00\x00\x00\x00\x01", ETH_ALEN);
169 	eth->h_proto = htons(ETH_P_IP);
170 
171 	iph->version = 0x4;
172 	iph->ihl = 0x5;
173 	iph->tos = 0x9;
174 	iph->tot_len = htons(sizeof(*iph) + sizeof(*udph) + UDP_PAYLOAD_BYTES);
175 	iph->id = 0;
176 	iph->frag_off = 0;
177 	iph->ttl = 0;
178 	iph->protocol = IPPROTO_UDP;
179 	ASSERT_EQ(inet_pton(FAMILY, TX_ADDR, &iph->saddr), 1, "inet_pton(TX_ADDR)");
180 	ASSERT_EQ(inet_pton(FAMILY, RX_ADDR, &iph->daddr), 1, "inet_pton(RX_ADDR)");
181 	ip_csum(iph);
182 
183 	udph->source = htons(AF_XDP_SOURCE_PORT);
184 	udph->dest = htons(dst_port);
185 	udph->len = htons(sizeof(*udph) + UDP_PAYLOAD_BYTES);
186 	udph->check = 0;
187 
188 	memset(udph + 1, 0xAA, UDP_PAYLOAD_BYTES);
189 
190 	tx_desc->len = sizeof(*eth) + sizeof(*iph) + sizeof(*udph) + UDP_PAYLOAD_BYTES;
191 	xsk_ring_prod__submit(&xsk->tx, 1);
192 
193 	ret = sendto(xsk_socket__fd(xsk->socket), NULL, 0, MSG_DONTWAIT, NULL, 0);
194 	if (!ASSERT_GE(ret, 0, "sendto"))
195 		return ret;
196 
197 	return 0;
198 }
199 
200 static void complete_tx(struct xsk *xsk)
201 {
202 	__u32 idx;
203 	__u64 addr;
204 
205 	if (ASSERT_EQ(xsk_ring_cons__peek(&xsk->comp, 1, &idx), 1, "xsk_ring_cons__peek")) {
206 		addr = *xsk_ring_cons__comp_addr(&xsk->comp, idx);
207 
208 		printf("%p: complete tx idx=%u addr=%llx\n", xsk, idx, addr);
209 		xsk_ring_cons__release(&xsk->comp, 1);
210 	}
211 }
212 
213 static void refill_rx(struct xsk *xsk, __u64 addr)
214 {
215 	__u32 idx;
216 
217 	if (ASSERT_EQ(xsk_ring_prod__reserve(&xsk->fill, 1, &idx), 1, "xsk_ring_prod__reserve")) {
218 		printf("%p: complete idx=%u addr=%llx\n", xsk, idx, addr);
219 		*xsk_ring_prod__fill_addr(&xsk->fill, idx) = addr;
220 		xsk_ring_prod__submit(&xsk->fill, 1);
221 	}
222 }
223 
224 static int verify_xsk_metadata(struct xsk *xsk)
225 {
226 	const struct xdp_desc *rx_desc;
227 	struct pollfd fds = {};
228 	struct xdp_meta *meta;
229 	struct ethhdr *eth;
230 	struct iphdr *iph;
231 	__u64 comp_addr;
232 	void *data;
233 	__u64 addr;
234 	__u32 idx;
235 	int ret;
236 
237 	ret = recvfrom(xsk_socket__fd(xsk->socket), NULL, 0, MSG_DONTWAIT, NULL, NULL);
238 	if (!ASSERT_EQ(ret, 0, "recvfrom"))
239 		return -1;
240 
241 	fds.fd = xsk_socket__fd(xsk->socket);
242 	fds.events = POLLIN;
243 
244 	ret = poll(&fds, 1, 1000);
245 	if (!ASSERT_GT(ret, 0, "poll"))
246 		return -1;
247 
248 	ret = xsk_ring_cons__peek(&xsk->rx, 1, &idx);
249 	if (!ASSERT_EQ(ret, 1, "xsk_ring_cons__peek"))
250 		return -2;
251 
252 	rx_desc = xsk_ring_cons__rx_desc(&xsk->rx, idx);
253 	comp_addr = xsk_umem__extract_addr(rx_desc->addr);
254 	addr = xsk_umem__add_offset_to_addr(rx_desc->addr);
255 	printf("%p: rx_desc[%u]->addr=%llx addr=%llx comp_addr=%llx\n",
256 	       xsk, idx, rx_desc->addr, addr, comp_addr);
257 	data = xsk_umem__get_data(xsk->umem_area, addr);
258 
259 	/* Make sure we got the packet offset correctly. */
260 
261 	eth = data;
262 	ASSERT_EQ(eth->h_proto, htons(ETH_P_IP), "eth->h_proto");
263 	iph = (void *)(eth + 1);
264 	ASSERT_EQ((int)iph->version, 4, "iph->version");
265 
266 	/* custom metadata */
267 
268 	meta = data - sizeof(struct xdp_meta);
269 
270 	if (!ASSERT_NEQ(meta->rx_timestamp, 0, "rx_timestamp"))
271 		return -1;
272 
273 	if (!ASSERT_NEQ(meta->rx_hash, 0, "rx_hash"))
274 		return -1;
275 
276 	xsk_ring_cons__release(&xsk->rx, 1);
277 	refill_rx(xsk, comp_addr);
278 
279 	return 0;
280 }
281 
282 void test_xdp_metadata(void)
283 {
284 	struct xdp_metadata2 *bpf_obj2 = NULL;
285 	struct xdp_metadata *bpf_obj = NULL;
286 	struct bpf_program *new_prog, *prog;
287 	struct nstoken *tok = NULL;
288 	__u32 queue_id = QUEUE_ID;
289 	struct bpf_map *prog_arr;
290 	struct xsk tx_xsk = {};
291 	struct xsk rx_xsk = {};
292 	__u32 val, key = 0;
293 	int retries = 10;
294 	int rx_ifindex;
295 	int tx_ifindex;
296 	int sock_fd;
297 	int ret;
298 
299 	/* Setup new networking namespace, with a veth pair. */
300 
301 	SYS("ip netns add xdp_metadata");
302 	tok = open_netns("xdp_metadata");
303 	SYS("ip link add numtxqueues 1 numrxqueues 1 " TX_NAME
304 	    " type veth peer " RX_NAME " numtxqueues 1 numrxqueues 1");
305 	SYS("ip link set dev " TX_NAME " address 00:00:00:00:00:01");
306 	SYS("ip link set dev " RX_NAME " address 00:00:00:00:00:02");
307 	SYS("ip link set dev " TX_NAME " up");
308 	SYS("ip link set dev " RX_NAME " up");
309 	SYS("ip addr add " TX_ADDR "/" PREFIX_LEN " dev " TX_NAME);
310 	SYS("ip addr add " RX_ADDR "/" PREFIX_LEN " dev " RX_NAME);
311 
312 	rx_ifindex = if_nametoindex(RX_NAME);
313 	tx_ifindex = if_nametoindex(TX_NAME);
314 
315 	/* Setup separate AF_XDP for TX and RX interfaces. */
316 
317 	ret = open_xsk(tx_ifindex, &tx_xsk);
318 	if (!ASSERT_OK(ret, "open_xsk(TX_NAME)"))
319 		goto out;
320 
321 	ret = open_xsk(rx_ifindex, &rx_xsk);
322 	if (!ASSERT_OK(ret, "open_xsk(RX_NAME)"))
323 		goto out;
324 
325 	bpf_obj = xdp_metadata__open();
326 	if (!ASSERT_OK_PTR(bpf_obj, "open skeleton"))
327 		goto out;
328 
329 	prog = bpf_object__find_program_by_name(bpf_obj->obj, "rx");
330 	bpf_program__set_ifindex(prog, rx_ifindex);
331 	bpf_program__set_flags(prog, BPF_F_XDP_DEV_BOUND_ONLY);
332 
333 	if (!ASSERT_OK(xdp_metadata__load(bpf_obj), "load skeleton"))
334 		goto out;
335 
336 	/* Make sure we can't add dev-bound programs to prog maps. */
337 	prog_arr = bpf_object__find_map_by_name(bpf_obj->obj, "prog_arr");
338 	if (!ASSERT_OK_PTR(prog_arr, "no prog_arr map"))
339 		goto out;
340 
341 	val = bpf_program__fd(prog);
342 	if (!ASSERT_ERR(bpf_map__update_elem(prog_arr, &key, sizeof(key),
343 					     &val, sizeof(val), BPF_ANY),
344 			"update prog_arr"))
345 		goto out;
346 
347 	/* Attach BPF program to RX interface. */
348 
349 	ret = bpf_xdp_attach(rx_ifindex,
350 			     bpf_program__fd(bpf_obj->progs.rx),
351 			     XDP_FLAGS, NULL);
352 	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
353 		goto out;
354 
355 	sock_fd = xsk_socket__fd(rx_xsk.socket);
356 	ret = bpf_map_update_elem(bpf_map__fd(bpf_obj->maps.xsk), &queue_id, &sock_fd, 0);
357 	if (!ASSERT_GE(ret, 0, "bpf_map_update_elem"))
358 		goto out;
359 
360 	/* Send packet destined to RX AF_XDP socket. */
361 	if (!ASSERT_GE(generate_packet(&tx_xsk, AF_XDP_CONSUMER_PORT), 0,
362 		       "generate AF_XDP_CONSUMER_PORT"))
363 		goto out;
364 
365 	/* Verify AF_XDP RX packet has proper metadata. */
366 	if (!ASSERT_GE(verify_xsk_metadata(&rx_xsk), 0,
367 		       "verify_xsk_metadata"))
368 		goto out;
369 
370 	complete_tx(&tx_xsk);
371 
372 	/* Make sure freplace correctly picks up original bound device
373 	 * and doesn't crash.
374 	 */
375 
376 	bpf_obj2 = xdp_metadata2__open();
377 	if (!ASSERT_OK_PTR(bpf_obj2, "open skeleton"))
378 		goto out;
379 
380 	new_prog = bpf_object__find_program_by_name(bpf_obj2->obj, "freplace_rx");
381 	bpf_program__set_attach_target(new_prog, bpf_program__fd(prog), "rx");
382 
383 	if (!ASSERT_OK(xdp_metadata2__load(bpf_obj2), "load freplace skeleton"))
384 		goto out;
385 
386 	if (!ASSERT_OK(xdp_metadata2__attach(bpf_obj2), "attach freplace"))
387 		goto out;
388 
389 	/* Send packet to trigger . */
390 	if (!ASSERT_GE(generate_packet(&tx_xsk, AF_XDP_CONSUMER_PORT), 0,
391 		       "generate freplace packet"))
392 		goto out;
393 
394 	while (!retries--) {
395 		if (bpf_obj2->bss->called)
396 			break;
397 		usleep(10);
398 	}
399 	ASSERT_GT(bpf_obj2->bss->called, 0, "not called");
400 
401 out:
402 	close_xsk(&rx_xsk);
403 	close_xsk(&tx_xsk);
404 	xdp_metadata2__destroy(bpf_obj2);
405 	xdp_metadata__destroy(bpf_obj);
406 	if (tok)
407 		close_netns(tok);
408 	system("ip netns del xdp_metadata");
409 }
410