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