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