xref: /xnu-8796.141.3/tests/aqm_qdelay_utun.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2021 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  */
4*1b191cb5SApple OSS Distributions 
5*1b191cb5SApple OSS Distributions #include <sys/socket.h>
6*1b191cb5SApple OSS Distributions #include <sys/sockio.h>
7*1b191cb5SApple OSS Distributions #include <sys/ioctl.h>
8*1b191cb5SApple OSS Distributions #include <sys/kern_control.h>
9*1b191cb5SApple OSS Distributions #include <sys/sys_domain.h>
10*1b191cb5SApple OSS Distributions 
11*1b191cb5SApple OSS Distributions #include <net/if.h>
12*1b191cb5SApple OSS Distributions #include <net/if_utun.h>
13*1b191cb5SApple OSS Distributions #include <netinet/in.h>
14*1b191cb5SApple OSS Distributions #include <netinet/in_var.h>
15*1b191cb5SApple OSS Distributions #include <netinet/ip6.h>
16*1b191cb5SApple OSS Distributions #include <arpa/inet.h>
17*1b191cb5SApple OSS Distributions 
18*1b191cb5SApple OSS Distributions #include <err.h>
19*1b191cb5SApple OSS Distributions #include <errno.h>
20*1b191cb5SApple OSS Distributions #include <stdbool.h>
21*1b191cb5SApple OSS Distributions #include <stdint.h>
22*1b191cb5SApple OSS Distributions #include <stdio.h>
23*1b191cb5SApple OSS Distributions #include <stdlib.h>
24*1b191cb5SApple OSS Distributions #include <string.h>
25*1b191cb5SApple OSS Distributions #include <unistd.h>
26*1b191cb5SApple OSS Distributions 
27*1b191cb5SApple OSS Distributions #include <net/pktsched/pktsched.h>
28*1b191cb5SApple OSS Distributions #include <net/classq/if_classq.h>
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions #include <darwintest.h>
31*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
32*1b191cb5SApple OSS Distributions 
33*1b191cb5SApple OSS Distributions #define MSEC_PER_SEC    1000            /* milliseconds per second */
34*1b191cb5SApple OSS Distributions static void
nsec_to_str(unsigned long long nsec,char * buf)35*1b191cb5SApple OSS Distributions nsec_to_str(unsigned long long nsec, char *buf)
36*1b191cb5SApple OSS Distributions {
37*1b191cb5SApple OSS Distributions 	const char *u;
38*1b191cb5SApple OSS Distributions 	long double n = nsec, t;
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions 	if (nsec >= NSEC_PER_SEC) {
41*1b191cb5SApple OSS Distributions 		t = n / NSEC_PER_SEC;
42*1b191cb5SApple OSS Distributions 		u = "sec ";
43*1b191cb5SApple OSS Distributions 	} else if (n >= USEC_PER_SEC) {
44*1b191cb5SApple OSS Distributions 		t = n / USEC_PER_SEC;
45*1b191cb5SApple OSS Distributions 		u = "msec";
46*1b191cb5SApple OSS Distributions 	} else if (n >= MSEC_PER_SEC) {
47*1b191cb5SApple OSS Distributions 		t = n / MSEC_PER_SEC;
48*1b191cb5SApple OSS Distributions 		u = "usec";
49*1b191cb5SApple OSS Distributions 	} else {
50*1b191cb5SApple OSS Distributions 		t = n;
51*1b191cb5SApple OSS Distributions 		u = "nsec";
52*1b191cb5SApple OSS Distributions 	}
53*1b191cb5SApple OSS Distributions 
54*1b191cb5SApple OSS Distributions 	snprintf(buf, 32, "%-5.2Lf %4s", t, u);
55*1b191cb5SApple OSS Distributions }
56*1b191cb5SApple OSS Distributions 
57*1b191cb5SApple OSS Distributions static int
create_tun()58*1b191cb5SApple OSS Distributions create_tun()
59*1b191cb5SApple OSS Distributions {
60*1b191cb5SApple OSS Distributions 	int tun_fd;
61*1b191cb5SApple OSS Distributions 	struct ctl_info kernctl_info;
62*1b191cb5SApple OSS Distributions 	struct sockaddr_ctl kernctl_addr;
63*1b191cb5SApple OSS Distributions 
64*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(tun_fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), NULL);
65*1b191cb5SApple OSS Distributions 
66*1b191cb5SApple OSS Distributions 	memset(&kernctl_info, 0, sizeof(kernctl_info));
67*1b191cb5SApple OSS Distributions 	strlcpy(kernctl_info.ctl_name, UTUN_CONTROL_NAME, sizeof(kernctl_info.ctl_name));
68*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ioctl(tun_fd, CTLIOCGINFO, &kernctl_info), NULL);
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions 	memset(&kernctl_addr, 0, sizeof(kernctl_addr));
71*1b191cb5SApple OSS Distributions 	kernctl_addr.sc_len = sizeof(kernctl_addr);
72*1b191cb5SApple OSS Distributions 	kernctl_addr.sc_family = AF_SYSTEM;
73*1b191cb5SApple OSS Distributions 	kernctl_addr.ss_sysaddr = AF_SYS_CONTROL;
74*1b191cb5SApple OSS Distributions 	kernctl_addr.sc_id = kernctl_info.ctl_id;
75*1b191cb5SApple OSS Distributions 	kernctl_addr.sc_unit = 0;
76*1b191cb5SApple OSS Distributions 
77*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(bind(tun_fd, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr)), NULL);
78*1b191cb5SApple OSS Distributions 
79*1b191cb5SApple OSS Distributions 	const int enable = 1;
80*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(setsockopt(tun_fd, SYSPROTO_CONTROL, UTUN_OPT_ENABLE_NETIF,
81*1b191cb5SApple OSS Distributions 	    &enable, sizeof(enable)), NULL);
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_FAILURE(setsockopt(tun_fd, SYSPROTO_CONTROL, UTUN_OPT_ENABLE_FLOWSWITCH,
84*1b191cb5SApple OSS Distributions 	    &enable, sizeof(enable)), EINVAL, NULL);
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(connect(tun_fd, (struct sockaddr *)&kernctl_addr, sizeof(kernctl_addr)), NULL);;
87*1b191cb5SApple OSS Distributions 
88*1b191cb5SApple OSS Distributions 	return tun_fd;
89*1b191cb5SApple OSS Distributions }
90*1b191cb5SApple OSS Distributions 
91*1b191cb5SApple OSS Distributions static short
ifnet_get_flags(int s,const char ifname[IFNAMSIZ])92*1b191cb5SApple OSS Distributions ifnet_get_flags(int s, const char ifname[IFNAMSIZ])
93*1b191cb5SApple OSS Distributions {
94*1b191cb5SApple OSS Distributions 	struct ifreq    ifr;
95*1b191cb5SApple OSS Distributions 	memset(&ifr, 0, sizeof(ifr));
96*1b191cb5SApple OSS Distributions 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
97*1b191cb5SApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr), NULL);
98*1b191cb5SApple OSS Distributions 	return ifr.ifr_flags;
99*1b191cb5SApple OSS Distributions }
100*1b191cb5SApple OSS Distributions 
101*1b191cb5SApple OSS Distributions static void
ifnet_add_addr4(const char ifname[IFNAMSIZ],struct in_addr * addr,struct in_addr * mask,struct in_addr * broadaddr)102*1b191cb5SApple OSS Distributions ifnet_add_addr4(const char ifname[IFNAMSIZ], struct in_addr *addr, struct in_addr *mask, struct in_addr *broadaddr)
103*1b191cb5SApple OSS Distributions {
104*1b191cb5SApple OSS Distributions 	struct sockaddr_in *sin;
105*1b191cb5SApple OSS Distributions 	struct in_aliasreq ifra;
106*1b191cb5SApple OSS Distributions 	int s;
107*1b191cb5SApple OSS Distributions 
108*1b191cb5SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, 0), NULL);
109*1b191cb5SApple OSS Distributions 
110*1b191cb5SApple OSS Distributions 	memset(&ifra, 0, sizeof(ifra));
111*1b191cb5SApple OSS Distributions 	strlcpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
112*1b191cb5SApple OSS Distributions 
113*1b191cb5SApple OSS Distributions 	if (addr != NULL) {
114*1b191cb5SApple OSS Distributions 		sin = &ifra.ifra_addr;
115*1b191cb5SApple OSS Distributions 		sin->sin_len = sizeof(*sin);
116*1b191cb5SApple OSS Distributions 		sin->sin_family = AF_INET;
117*1b191cb5SApple OSS Distributions 		sin->sin_addr = *addr;
118*1b191cb5SApple OSS Distributions 	}
119*1b191cb5SApple OSS Distributions 
120*1b191cb5SApple OSS Distributions 	if (mask != NULL) {
121*1b191cb5SApple OSS Distributions 		sin = &ifra.ifra_mask;
122*1b191cb5SApple OSS Distributions 		sin->sin_len = sizeof(*sin);
123*1b191cb5SApple OSS Distributions 		sin->sin_family = AF_INET;
124*1b191cb5SApple OSS Distributions 		sin->sin_addr = *mask;
125*1b191cb5SApple OSS Distributions 	}
126*1b191cb5SApple OSS Distributions 
127*1b191cb5SApple OSS Distributions 	if (broadaddr != NULL || (addr != NULL &&
128*1b191cb5SApple OSS Distributions 	    (ifnet_get_flags(s, ifname) & IFF_POINTOPOINT) != 0)) {
129*1b191cb5SApple OSS Distributions 		sin = &ifra.ifra_broadaddr;
130*1b191cb5SApple OSS Distributions 		sin->sin_len = sizeof(*sin);
131*1b191cb5SApple OSS Distributions 		sin->sin_family = AF_INET;
132*1b191cb5SApple OSS Distributions 		sin->sin_addr = (broadaddr != NULL) ? *broadaddr : *addr;
133*1b191cb5SApple OSS Distributions 	}
134*1b191cb5SApple OSS Distributions 
135*1b191cb5SApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(ioctl(s, SIOCAIFADDR, &ifra), NULL);
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_EXPECT_POSIX_ZERO(close(s), NULL);
138*1b191cb5SApple OSS Distributions }
139*1b191cb5SApple OSS Distributions 
140*1b191cb5SApple OSS Distributions static struct if_qstatsreq ifqr;
141*1b191cb5SApple OSS Distributions static struct if_ifclassq_stats *ifcqs;
142*1b191cb5SApple OSS Distributions static uint32_t scheduler;
143*1b191cb5SApple OSS Distributions 
144*1b191cb5SApple OSS Distributions #define FQ_IF_BE_INDEX  7
145*1b191cb5SApple OSS Distributions static int
aqmstats_setup(char * iface)146*1b191cb5SApple OSS Distributions aqmstats_setup(char *iface)
147*1b191cb5SApple OSS Distributions {
148*1b191cb5SApple OSS Distributions 	unsigned int ifindex;
149*1b191cb5SApple OSS Distributions 	int s;
150*1b191cb5SApple OSS Distributions 
151*1b191cb5SApple OSS Distributions 	ifindex = if_nametoindex(iface);
152*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(ifindex != 0, "interface index for utun");
153*1b191cb5SApple OSS Distributions 
154*1b191cb5SApple OSS Distributions 	ifcqs = malloc(sizeof(*ifcqs));
155*1b191cb5SApple OSS Distributions 	T_ASSERT_TRUE(ifcqs != 0, "Allocated ifcqs");
156*1b191cb5SApple OSS Distributions 
157*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, 0), NULL);
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions 	bzero(&ifqr, sizeof(ifqr));
160*1b191cb5SApple OSS Distributions 	strlcpy(ifqr.ifqr_name, iface, sizeof(ifqr.ifqr_name));
161*1b191cb5SApple OSS Distributions 	ifqr.ifqr_buf = ifcqs;
162*1b191cb5SApple OSS Distributions 	ifqr.ifqr_len = sizeof(*ifcqs);
163*1b191cb5SApple OSS Distributions 
164*1b191cb5SApple OSS Distributions 	// Get the scheduler
165*1b191cb5SApple OSS Distributions 	ifqr.ifqr_slot = 0;
166*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ioctl(s, SIOCGIFQUEUESTATS, (char *)&ifqr), NULL);
167*1b191cb5SApple OSS Distributions 	scheduler = ifcqs->ifqs_scheduler;
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions 	// Update the slot to BE
170*1b191cb5SApple OSS Distributions 	ifqr.ifqr_slot = FQ_IF_BE_INDEX;
171*1b191cb5SApple OSS Distributions 
172*1b191cb5SApple OSS Distributions 	return s;
173*1b191cb5SApple OSS Distributions }
174*1b191cb5SApple OSS Distributions 
175*1b191cb5SApple OSS Distributions static void
aqmstats_cleanup()176*1b191cb5SApple OSS Distributions aqmstats_cleanup()
177*1b191cb5SApple OSS Distributions {
178*1b191cb5SApple OSS Distributions 	free(ifcqs);
179*1b191cb5SApple OSS Distributions }
180*1b191cb5SApple OSS Distributions 
181*1b191cb5SApple OSS Distributions T_DECL(aqm_qdelay, "This test checks the min/max/avg AQM queuing delay")
182*1b191cb5SApple OSS Distributions {
183*1b191cb5SApple OSS Distributions 	T_SETUPBEGIN;
184*1b191cb5SApple OSS Distributions 
185*1b191cb5SApple OSS Distributions 	// Create tun device with IPv4 address
186*1b191cb5SApple OSS Distributions 	int tun_fd = create_tun();
187*1b191cb5SApple OSS Distributions 
188*1b191cb5SApple OSS Distributions 	char ifname[IFXNAMSIZ];
189*1b191cb5SApple OSS Distributions 	socklen_t optlen = IFNAMSIZ;
190*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(getsockopt(tun_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, ifname, &optlen), NULL);
191*1b191cb5SApple OSS Distributions 	T_ASSERT_TRUE(ifname[optlen - 1] == '\0', NULL);
192*1b191cb5SApple OSS Distributions 	T_LOG("Created interface %s", ifname);
193*1b191cb5SApple OSS Distributions 
194*1b191cb5SApple OSS Distributions 	uint32_t ifaddr = (10 << 24) | ((unsigned)getpid() & 0xffff) << 8 | 160;
195*1b191cb5SApple OSS Distributions 	struct in_addr tun_addr1, tun_addr2, mask;
196*1b191cb5SApple OSS Distributions 	tun_addr1.s_addr = htonl(ifaddr);
197*1b191cb5SApple OSS Distributions 	tun_addr2.s_addr = htonl(ifaddr + 1);
198*1b191cb5SApple OSS Distributions 	mask.s_addr = htonl(0xffffffff);
199*1b191cb5SApple OSS Distributions 
200*1b191cb5SApple OSS Distributions 	ifnet_add_addr4(ifname, &tun_addr1, &mask, &tun_addr2);
201*1b191cb5SApple OSS Distributions 
202*1b191cb5SApple OSS Distributions 	// Create UDP socket to send
203*1b191cb5SApple OSS Distributions 	int sock_fd;
204*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sock_fd = socket(AF_INET, SOCK_DGRAM, 0), NULL);
205*1b191cb5SApple OSS Distributions 	struct sockaddr_in sin;
206*1b191cb5SApple OSS Distributions 	memset(&sin, 0, sizeof(sin));
207*1b191cb5SApple OSS Distributions 	sin.sin_len = sizeof(sin);
208*1b191cb5SApple OSS Distributions 	sin.sin_family = AF_INET;
209*1b191cb5SApple OSS Distributions 	sin.sin_addr = tun_addr1;
210*1b191cb5SApple OSS Distributions 
211*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(bind(sock_fd, (struct sockaddr *)&sin, sizeof(sin)), NULL);
212*1b191cb5SApple OSS Distributions 
213*1b191cb5SApple OSS Distributions 	struct sockaddr_in dest;
214*1b191cb5SApple OSS Distributions 	memset(&sin, 0, sizeof(dest));
215*1b191cb5SApple OSS Distributions 	dest.sin_len = sizeof(dest);
216*1b191cb5SApple OSS Distributions 	dest.sin_family = AF_INET;
217*1b191cb5SApple OSS Distributions 	dest.sin_addr = tun_addr2;
218*1b191cb5SApple OSS Distributions 	dest.sin_port = ntohs(12345);
219*1b191cb5SApple OSS Distributions 
220*1b191cb5SApple OSS Distributions 	// Setup the state for AQM stats
221*1b191cb5SApple OSS Distributions 	int stats_fd = aqmstats_setup(ifname);
222*1b191cb5SApple OSS Distributions 
223*1b191cb5SApple OSS Distributions 	T_SETUPEND;
224*1b191cb5SApple OSS Distributions 
225*1b191cb5SApple OSS Distributions 	char min[32], max[32], avg[32];
226*1b191cb5SApple OSS Distributions 	// Get the current value of min/max/avg qdelay
227*1b191cb5SApple OSS Distributions 	if (scheduler == PKTSCHEDT_FQ_CODEL) {
228*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ioctl(stats_fd, SIOCGIFQUEUESTATS, (char *)&ifqr), NULL);
229*1b191cb5SApple OSS Distributions 		nsec_to_str(ifcqs->ifqs_fq_codel_stats.fcls_min_qdelay, min);
230*1b191cb5SApple OSS Distributions 		nsec_to_str(ifcqs->ifqs_fq_codel_stats.fcls_max_qdelay, max);
231*1b191cb5SApple OSS Distributions 		nsec_to_str(ifcqs->ifqs_fq_codel_stats.fcls_avg_qdelay, avg);
232*1b191cb5SApple OSS Distributions 
233*1b191cb5SApple OSS Distributions 		T_LOG("min/max/avg qdelay %10s    %10s    %10s", min, max, avg);
234*1b191cb5SApple OSS Distributions 	}
235*1b191cb5SApple OSS Distributions 
236*1b191cb5SApple OSS Distributions 	// Send data
237*1b191cb5SApple OSS Distributions 	T_LOG("Sending 10 UDP packets...");
238*1b191cb5SApple OSS Distributions 	uint8_t content[0x578] = {0};
239*1b191cb5SApple OSS Distributions 	for (int i = 0; i < 5; i++) {
240*1b191cb5SApple OSS Distributions 		sendto(sock_fd, content, sizeof(content), 0, (struct sockaddr *)&dest,
241*1b191cb5SApple OSS Distributions 		    (socklen_t) sizeof(dest));
242*1b191cb5SApple OSS Distributions 		usleep(1000);
243*1b191cb5SApple OSS Distributions 	}
244*1b191cb5SApple OSS Distributions 
245*1b191cb5SApple OSS Distributions 	// Get the current value of min/max/avg qdelay
246*1b191cb5SApple OSS Distributions 	if (scheduler == PKTSCHEDT_FQ_CODEL) {
247*1b191cb5SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ioctl(stats_fd, SIOCGIFQUEUESTATS, (char *)&ifqr), NULL);
248*1b191cb5SApple OSS Distributions 		nsec_to_str(ifcqs->ifqs_fq_codel_stats.fcls_min_qdelay, min);
249*1b191cb5SApple OSS Distributions 		nsec_to_str(ifcqs->ifqs_fq_codel_stats.fcls_max_qdelay, max);
250*1b191cb5SApple OSS Distributions 		nsec_to_str(ifcqs->ifqs_fq_codel_stats.fcls_avg_qdelay, avg);
251*1b191cb5SApple OSS Distributions 
252*1b191cb5SApple OSS Distributions 		T_LOG("min/max/avg qdelay %10s    %10s    %10s", min, max, avg);
253*1b191cb5SApple OSS Distributions 		T_ASSERT_TRUE(ifcqs->ifqs_fq_codel_stats.fcls_min_qdelay <= ifcqs->ifqs_fq_codel_stats.fcls_avg_qdelay &&
254*1b191cb5SApple OSS Distributions 		    ifcqs->ifqs_fq_codel_stats.fcls_min_qdelay <= ifcqs->ifqs_fq_codel_stats.fcls_max_qdelay, "min qdelay check");
255*1b191cb5SApple OSS Distributions 		T_ASSERT_TRUE(ifcqs->ifqs_fq_codel_stats.fcls_avg_qdelay <= ifcqs->ifqs_fq_codel_stats.fcls_max_qdelay, "avg qdelay check");
256*1b191cb5SApple OSS Distributions 	}
257*1b191cb5SApple OSS Distributions 
258*1b191cb5SApple OSS Distributions 	aqmstats_cleanup();
259*1b191cb5SApple OSS Distributions 	// Close socket and utun device
260*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(close(sock_fd), NULL);
261*1b191cb5SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(close(tun_fd), NULL);
262*1b191cb5SApple OSS Distributions }
263