xref: /xnu-11215/tests/bpf_write_batch.c (revision 8d741a5d)
1 /*
2  * Copyright (c) 2023-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <darwintest.h>
30 
31 #include <sys/ioctl.h>
32 #include <sys/sysctl.h>
33 #include <sys/uio.h>
34 
35 #include <net/if.h>
36 #include <net/if_arp.h>
37 #include <net/if_fake_var.h>
38 #include <net/bpf.h>
39 #include <net/ethernet.h>
40 
41 #include <netinet/ip.h>
42 
43 #include <stdlib.h>
44 #include <string.h>
45 #include <strings.h>
46 
47 #include "net_test_lib.h"
48 #include "bpflib.h"
49 #include "in_cksum.h"
50 
51 T_GLOBAL_META(
52 	T_META_NAMESPACE("xnu.net"),
53 	T_META_ASROOT(true),
54 	T_META_RADAR_COMPONENT_NAME("xnu"),
55 	T_META_RADAR_COMPONENT_VERSION("networking"),
56 	T_META_CHECK_LEAKS(false));
57 
58 #define MAXBUF 32
59 static void
HexDump(void * data,size_t len)60 HexDump(void *data, size_t len)
61 {
62 	size_t i, j, k;
63 	unsigned char *ptr = (unsigned char *)data;
64 	unsigned char buf[3 * MAXBUF + 1];
65 
66 	for (i = 0; i < len; i += MAXBUF) {
67 		for (j = i, k = 0; j < i + MAXBUF && j < len; j++) {
68 			unsigned char msnbl = ptr[j] >> 4;
69 			unsigned char lsnbl = ptr[j] & 0x0f;
70 
71 			buf[k++] = msnbl < 10 ? msnbl + '0' : msnbl + 'a' - 10;
72 			buf[k++] = lsnbl < 10 ? lsnbl + '0' : lsnbl + 'a' - 10;
73 			if ((j % 2) == 1) {
74 				buf[k++] = ' ';
75 			}
76 			if ((j % MAXBUF) == MAXBUF - 1) {
77 				buf[k++] = ' ';
78 			}
79 		}
80 		buf[k] = 0;
81 		T_LOG("%5zd: %s\n", i, buf);
82 	}
83 }
84 
85 static char ifname1[IF_NAMESIZE];
86 static char ifname2[IF_NAMESIZE];
87 static int default_fake_max_mtu = 0;
88 
89 static void
cleanup(void)90 cleanup(void)
91 {
92 	if (ifname1[0] != '\0') {
93 		(void)ifnet_destroy(ifname1, false);
94 		T_LOG("ifnet_destroy %s", ifname1);
95 	}
96 	if (ifname2[0] != '\0') {
97 		(void)ifnet_destroy(ifname2, false);
98 		T_LOG("ifnet_destroy %s", ifname2);
99 	}
100 
101 	if (default_fake_max_mtu != 0) {
102 		T_LOG("sysctl net.link.fake.max_mtu=%d", default_fake_max_mtu);
103 		(void) sysctlbyname("net.link.fake.max_mtu", NULL, NULL, &default_fake_max_mtu, sizeof(int));
104 	}
105 }
106 
107 static void
init(int mtu)108 init(int mtu)
109 {
110 	T_ATEND(cleanup);
111 
112 	if (mtu > 0) {
113 		size_t oldlen = sizeof(int);
114 		T_ASSERT_POSIX_SUCCESS(sysctlbyname("net.link.fake.max_mtu", &default_fake_max_mtu, &oldlen, &mtu, sizeof(int)),
115 		    "sysctl net.link.fake.max_mtu %d -> %d", default_fake_max_mtu, mtu);
116 	}
117 }
118 
119 static int
setup_feth_pair(int mtu)120 setup_feth_pair(int mtu)
121 {
122 	int error = 0;
123 
124 	strlcpy(ifname1, FETH_NAME, sizeof(ifname1));
125 	error = ifnet_create_2(ifname1, sizeof(ifname1));
126 	if (error != 0) {
127 		ifname1[0] = '\0';
128 		goto done;
129 	}
130 	T_LOG("created %s", ifname1);
131 
132 	strlcpy(ifname2, FETH_NAME, sizeof(ifname2));
133 	error = ifnet_create_2(ifname2, sizeof(ifname2));
134 	if (error != 0) {
135 		ifname2[0] = '\0';
136 		goto done;
137 	}
138 	T_LOG("created %s", ifname2);
139 
140 	ifnet_attach_ip(ifname1);
141 
142 	fake_set_peer(ifname1, ifname2);
143 	if (mtu != 0) {
144 		ifnet_set_mtu(ifname1, mtu);
145 		ifnet_set_mtu(ifname2, mtu);
146 	}
147 done:
148 	return error;
149 }
150 
151 static int
create_bpf_on_interface(const char * ifname,int * out_fd,int * out_bdlen,u_int write_size_max)152 create_bpf_on_interface(const char *ifname, int *out_fd, int *out_bdlen, u_int write_size_max)
153 {
154 	int bpf_fd = -1;
155 	int error = 0;
156 	int bdlen = 0;
157 	u_int value;
158 
159 	bpf_fd = bpf_new();
160 	if (bpf_fd < 0) {
161 		error = errno;
162 		T_LOG("bpf_new");
163 		goto done;
164 	}
165 	T_ASSERT_POSIX_SUCCESS(bpf_set_blen(bpf_fd, 128 * 1024), NULL);
166 
167 	T_ASSERT_POSIX_SUCCESS(bpf_get_blen(bpf_fd, &bdlen), NULL);
168 
169 	T_ASSERT_POSIX_SUCCESS(bpf_set_immediate(bpf_fd, 1), NULL);
170 
171 	T_ASSERT_POSIX_SUCCESS(bpf_setif(bpf_fd, ifname), "bpf set if %s",
172 	    ifname1);
173 
174 	T_ASSERT_POSIX_SUCCESS(bpf_set_see_sent(bpf_fd, 1), NULL);
175 
176 	T_ASSERT_POSIX_SUCCESS(bpf_set_header_complete(bpf_fd, 1), NULL);
177 
178 #ifdef BIOCSWRITEMAX
179 	T_ASSERT_POSIX_SUCCESS(bpf_set_write_size_max(bpf_fd, write_size_max), NULL);
180 
181 	T_ASSERT_POSIX_SUCCESS(bpf_get_write_size_max(bpf_fd, &value), NULL);
182 
183 	T_LOG("write_size_max %u %s value %u", write_size_max, write_size_max != value ? "!=" : "==", value);
184 #else
185 	if (write_size_max > 0) {
186 		T_SKIP("BIOCSWRITEMAX not supported");
187 	}
188 #endif
189 
190 #ifdef BIOCSBATCHWRITE
191 	T_ASSERT_POSIX_SUCCESS(bpf_set_batch_write(bpf_fd, 1), NULL);
192 
193 	T_ASSERT_POSIX_SUCCESS(bpf_get_batch_write(bpf_fd, &value), NULL);
194 
195 	T_LOG("batch_write %u %s 1", value, value != 1 ? "!=" : "==");
196 #else
197 	T_SKIP("BIOCSBATCHWRITE not supported");
198 #endif
199 
200 	struct timeval five_seconds = { .tv_sec = 5, .tv_usec = 0 };
201 	T_ASSERT_POSIX_SUCCESS(bpf_set_timeout(bpf_fd, &five_seconds), NULL);
202 
203 done:
204 	*out_bdlen = bdlen;
205 	*out_fd = bpf_fd;
206 	return error;
207 }
208 
209 static void
make_bootp_packet(struct iovec * iov,u_int ip_len,int id)210 make_bootp_packet(struct iovec *iov, u_int ip_len, int id)
211 {
212 	u_int payload_len;
213 
214 	if (ip_len == 0) {
215 		payload_len = (u_int)sizeof(dhcp_min_payload);
216 	} else {
217 		T_ASSERT_GE((size_t)ip_len, sizeof(struct ip) + sizeof(struct udphdr) + sizeof(dhcp_min_payload),
218 		    "ip_len");
219 		payload_len = ip_len - (sizeof(struct ip) + sizeof(struct udphdr));
220 	}
221 
222 	struct ether_addr src_eaddr = { 0 };
223 	ifnet_get_lladdr(ifname1, &src_eaddr);
224 
225 	struct in_addr src_ip = { .s_addr = INADDR_ANY };
226 	uint16_t src_port = 68;
227 
228 	struct ether_addr dst_eaddr = { 0 };
229 	memset(dst_eaddr.octet, 255, ETHER_ADDR_LEN);
230 
231 	struct in_addr dst_ip = { .s_addr = INADDR_BROADCAST };
232 
233 	uint16_t dst_port = 67;
234 
235 	void *payload = calloc(1, payload_len);
236 
237 	make_dhcp_payload((dhcp_min_payload_t)payload, &src_eaddr);
238 
239 	struct bootp *dhcp = (struct bootp *)payload;
240 	dhcp->bp_xid = (uint32_t)id;
241 
242 	u_int pkt_size = ETHER_HDR_LEN + IP_MAXPACKET;
243 	unsigned char *pkt = calloc(1, pkt_size);
244 
245 	u_int frame_length = ethernet_udp4_frame_populate((void *)pkt,
246 	    pkt_size,
247 	    &src_eaddr,
248 	    src_ip,
249 	    src_port,
250 	    &dst_eaddr,
251 	    dst_ip,
252 	    dst_port,
253 	    payload,
254 	    payload_len);
255 
256 	T_ASSERT_EQ(sizeof(struct bpf_hdr), BPF_WORDALIGN(sizeof(struct bpf_hdr)), "bpfhdr.bh_hdrlen == BPF_WORDALIGN(sizeof(struct bpf_hdr))");
257 
258 	struct bpf_hdr bpfhdr = { 0 };
259 	bpfhdr.bh_caplen = frame_length;
260 	bpfhdr.bh_datalen = frame_length;
261 	bpfhdr.bh_hdrlen = sizeof(struct bpf_hdr);
262 
263 	iov->iov_len = BPF_WORDALIGN(bpfhdr.bh_hdrlen + frame_length);
264 	iov->iov_base = calloc(1, iov->iov_len);
265 
266 	T_LOG("iov_len %lu bh_hdrlen %u frame_length %u ip_len %u payload_len %u",
267 	    iov->iov_len, bpfhdr.bh_hdrlen, frame_length, ip_len, payload_len);
268 
269 	bcopy(&bpfhdr, iov->iov_base, bpfhdr.bh_hdrlen);
270 	bcopy(pkt, (char *)iov->iov_base + bpfhdr.bh_hdrlen, frame_length);
271 
272 	free(pkt);
273 	free(payload);
274 }
275 
276 static void
do_bpf_write_batch(int count,const char * ifname,u_int ip_len,bool expect_success,u_int write_size_max)277 do_bpf_write_batch(int count, const char *ifname, u_int ip_len, bool expect_success, u_int write_size_max)
278 {
279 	int bpf_fd = -1;
280 	int bdlen = 0;
281 	struct iovec *iovs = NULL;
282 	int i;
283 
284 	T_ASSERT_POSIX_ZERO(create_bpf_on_interface(ifname, &bpf_fd, &bdlen, write_size_max), NULL);
285 	T_LOG("bpf bdlen %d", bdlen);
286 
287 	/*
288 	 * Allocate an iovec for each packet that contains the BPF header + the data
289 	 */
290 	iovs = calloc((size_t)count, sizeof(struct iovec));
291 
292 	ssize_t total_len = 0;
293 	for (i = 0; i < count; i++) {
294 		make_bootp_packet(&iovs[i], ip_len, i + 1);
295 		total_len += iovs[i].iov_len;
296 
297 		struct bpf_hdr *h0 = (struct bpf_hdr *)iovs[i].iov_base;;
298 
299 		T_LOG("tv_sec %u tv_usec %u caplen %u datalen %u hdrlen %u",
300 		    h0->bh_tstamp.tv_sec, h0->bh_tstamp.tv_usec,
301 		    h0->bh_caplen, h0->bh_datalen, h0->bh_hdrlen);
302 
303 		HexDump(iovs[i].iov_base, MIN((size_t)iovs[i].iov_len, 512));
304 
305 		T_ASSERT_EQ((int)(iovs[i].iov_len % BPF_ALIGNMENT), 0, "iovs[i].iov_len %% BPF_ALIGNMENT == 0");
306 	}
307 	T_LOG("total_len %ld", total_len);
308 
309 	ssize_t nwritten;
310 	nwritten = writev(bpf_fd, iovs, count);
311 
312 	T_LOG("bpf write returned %ld", nwritten);
313 
314 	if (expect_success) {
315 		T_ASSERT_POSIX_SUCCESS(nwritten, "write bpf");
316 	} else {
317 		T_ASSERT_POSIX_FAILURE(nwritten, EMSGSIZE, "write bpf");
318 		goto done;
319 	}
320 
321 	T_LOG("bpf written %ld bytes over %lu", nwritten, total_len);
322 
323 	T_ASSERT_GE((size_t)nwritten, iovs[0].iov_len, "nwritten %lu >= iovs[0].iov_len %lu", nwritten, iovs[0].iov_len);
324 
325 	/*
326 	 * Give 100 ms for the packets to be captured
327 	 */
328 	usleep(100000);
329 
330 	/*
331 	 * Read the batch and verify the content matches what we just sent.
332 	 */
333 	unsigned char *buffer = calloc(1, (size_t)bdlen);
334 	T_ASSERT_NOTNULL(buffer, "malloc()");
335 
336 	ssize_t nread = read(bpf_fd, buffer, (size_t)bdlen);
337 
338 	T_ASSERT_POSIX_SUCCESS(nread, "read bpf");
339 
340 	T_LOG("bpf read %ld bytes", nread);
341 
342 	/*
343 	 * We need at least the BPF header
344 	 */
345 	T_ASSERT_GT((size_t)nread, sizeof(sizeof(struct bpf_hdr)), NULL);
346 
347 	unsigned char *bp = buffer;
348 	unsigned char *ep = buffer + nread;
349 
350 	for (i = 0; i < count && bp < ep; i++) {
351 		struct bpf_hdr *hp = (struct bpf_hdr *)(void *)bp;
352 
353 		T_LOG("tv_sec %u tv_usec %u caplen %u datalen %u hdrlen %u",
354 		    hp->bh_tstamp.tv_sec, hp->bh_tstamp.tv_usec,
355 		    hp->bh_caplen, hp->bh_datalen, hp->bh_hdrlen);
356 
357 		HexDump(bp, MIN((size_t)BPF_WORDALIGN(hp->bh_hdrlen + hp->bh_caplen), 512));
358 
359 		/*
360 		 * Note: The following will fail if there is parasitic traffic and that should not happen over feth
361 		 */
362 		unsigned char *p0 = iovs[i].iov_base;
363 		struct bpf_hdr *h0 = (struct bpf_hdr *)iovs[i].iov_base;;
364 
365 		T_ASSERT_EQ(h0->bh_caplen, hp->bh_caplen, "h0->bh_caplen %d == hp->bh_caplen %d", h0->bh_caplen, hp->bh_caplen);
366 		T_ASSERT_EQ(h0->bh_datalen, hp->bh_datalen, "h0->bh_datalen %d == hp->bh_datalen %d", h0->bh_datalen, hp->bh_datalen);
367 
368 		T_ASSERT_EQ_INT(bcmp(h0->bh_hdrlen + p0, hp->bh_hdrlen + bp, hp->bh_caplen), 0, "bpf read same bytes as written iov %d", i);
369 
370 		bp += BPF_WORDALIGN(hp->bh_hdrlen + hp->bh_caplen);
371 	}
372 
373 	if (buffer != NULL) {
374 		free(buffer);
375 	}
376 done:
377 	if (bpf_fd != -1) {
378 		close(bpf_fd);
379 	}
380 }
381 
382 static void
test_bpf_write_batch(int count,u_int data_len,int mtu,bool expect_success,u_int write_size_max)383 test_bpf_write_batch(int count, u_int data_len, int mtu, bool expect_success, u_int write_size_max)
384 {
385 	init(mtu);
386 
387 	T_ASSERT_POSIX_ZERO(setup_feth_pair(mtu), NULL);
388 
389 	do_bpf_write_batch(count, ifname1, data_len, expect_success, write_size_max);
390 }
391 
392 T_DECL(bpf_write_batch_dhcp, "BPF write DHCP feth MTU 1500")
393 {
394 	test_bpf_write_batch(1, 0, 1500, true, 0);
395 }
396 
397 T_DECL(bpf_write_batch_dhcp_x2, "BPF write DHCP feth MTU 1500 x 2")
398 {
399 	test_bpf_write_batch(2, 0, 1500, true, 0);
400 }
401 
402 T_DECL(bpf_write_batch_dhcp_x3, "BPF write DHCP feth MTU 1500 x 3")
403 {
404 	test_bpf_write_batch(3, 0, 1500, true, 0);
405 }
406 
407 T_DECL(bpf_write_batch_1020, "BPF write 1020 feth MTU 1500 x 2")
408 {
409 	test_bpf_write_batch(2, 1020, 1500, true, 0);
410 }
411 
412 T_DECL(bpf_write_batch_1021, "BPF write 1021 feth MTU 1500 x 2")
413 {
414 	test_bpf_write_batch(2, 1021, 1500, true, 0);
415 }
416 
417 T_DECL(bpf_write_batch_1022, "BPF write 1022 feth MTU 1500 x 2")
418 {
419 	test_bpf_write_batch(2, 1022, 1500, true, 0);
420 }
421 
422 T_DECL(bpf_write_batch_1023, "BPF write 1023 feth MTU 1500 x2 ")
423 {
424 	test_bpf_write_batch(2, 1023, 1500, true, 0);
425 }
426