xref: /linux/tools/testing/selftests/net/txtimestamp.c (revision c65182ef9df6bb96fd85b56a2bcdd18d64c4d3b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2014 Google Inc.
4  * Author: willemb@google.com (Willem de Bruijn)
5  *
6  * Test software tx timestamping, including
7  *
8  * - SCHED, SND and ACK timestamps
9  * - RAW, UDP and TCP
10  * - IPv4 and IPv6
11  * - various packet sizes (to test GSO and TSO)
12  *
13  * Consult the command line arguments for help on running
14  * the various testcases.
15  *
16  * This test requires a dummy TCP server.
17  * A simple `nc6 [-u] -l -p $DESTPORT` will do
18  */
19 
20 #define _GNU_SOURCE
21 
22 #include <arpa/inet.h>
23 #include <asm/types.h>
24 #include <error.h>
25 #include <errno.h>
26 #include <inttypes.h>
27 #include <linux/errqueue.h>
28 #include <linux/if_ether.h>
29 #include <linux/if_packet.h>
30 #include <linux/ipv6.h>
31 #include <linux/net_tstamp.h>
32 #include <netdb.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 #include <netinet/udp.h>
37 #include <netinet/tcp.h>
38 #include <poll.h>
39 #include <stdarg.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/epoll.h>
45 #include <sys/ioctl.h>
46 #include <sys/select.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <time.h>
51 #include <unistd.h>
52 
53 #define NSEC_PER_USEC	1000L
54 #define USEC_PER_SEC	1000000L
55 #define NSEC_PER_SEC	1000000000LL
56 
57 /* command line parameters */
58 static int cfg_proto = SOCK_STREAM;
59 static int cfg_ipproto = IPPROTO_TCP;
60 static int cfg_num_pkts = 4;
61 static int do_ipv4 = 1;
62 static int do_ipv6 = 1;
63 static int cfg_payload_len = 10;
64 static int cfg_poll_timeout = 100;
65 static int cfg_delay_snd;
66 static int cfg_delay_ack;
67 static int cfg_delay_tolerance_usec = 500;
68 static bool cfg_show_payload;
69 static bool cfg_do_pktinfo;
70 static bool cfg_busy_poll;
71 static int cfg_sleep_usec = 50 * 1000;
72 static bool cfg_loop_nodata;
73 static bool cfg_use_cmsg;
74 static bool cfg_use_pf_packet;
75 static bool cfg_use_epoll;
76 static bool cfg_epollet;
77 static bool cfg_do_listen;
78 static uint16_t dest_port = 9000;
79 static bool cfg_print_nsec;
80 static uint32_t ts_opt_id;
81 static bool cfg_use_cmsg_opt_id;
82 
83 static struct sockaddr_in daddr;
84 static struct sockaddr_in6 daddr6;
85 static struct timespec ts_usr;
86 
87 static int saved_tskey = -1;
88 static int saved_tskey_type = -1;
89 
90 struct timing_event {
91 	int64_t min;
92 	int64_t max;
93 	int64_t total;
94 	int count;
95 };
96 
97 static struct timing_event usr_enq;
98 static struct timing_event usr_snd;
99 static struct timing_event usr_ack;
100 
101 static bool test_failed;
102 
103 static int64_t timespec_to_ns64(struct timespec *ts)
104 {
105 	return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
106 }
107 
108 static int64_t timespec_to_us64(struct timespec *ts)
109 {
110 	return ts->tv_sec * USEC_PER_SEC + ts->tv_nsec / NSEC_PER_USEC;
111 }
112 
113 static void init_timing_event(struct timing_event *te)
114 {
115 	te->min = INT64_MAX;
116 	te->max = 0;
117 	te->total = 0;
118 	te->count = 0;
119 }
120 
121 static void add_timing_event(struct timing_event *te,
122 		struct timespec *t_start, struct timespec *t_end)
123 {
124 	int64_t ts_delta = timespec_to_ns64(t_end) - timespec_to_ns64(t_start);
125 
126 	te->count++;
127 	if (ts_delta < te->min)
128 		te->min = ts_delta;
129 	if (ts_delta > te->max)
130 		te->max = ts_delta;
131 	te->total += ts_delta;
132 }
133 
134 static void validate_key(int tskey, int tstype)
135 {
136 	int stepsize;
137 
138 	/* compare key for each subsequent request
139 	 * must only test for one type, the first one requested
140 	 */
141 	if (saved_tskey == -1 || cfg_use_cmsg_opt_id)
142 		saved_tskey_type = tstype;
143 	else if (saved_tskey_type != tstype)
144 		return;
145 
146 	stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;
147 	stepsize = cfg_use_cmsg_opt_id ? 0 : stepsize;
148 	if (tskey != saved_tskey + stepsize) {
149 		fprintf(stderr, "ERROR: key %d, expected %d\n",
150 				tskey, saved_tskey + stepsize);
151 		test_failed = true;
152 	}
153 
154 	saved_tskey = tskey;
155 }
156 
157 static void validate_timestamp(struct timespec *cur, int min_delay)
158 {
159 	int64_t cur64, start64;
160 	int max_delay;
161 
162 	cur64 = timespec_to_us64(cur);
163 	start64 = timespec_to_us64(&ts_usr);
164 	max_delay = min_delay + cfg_delay_tolerance_usec;
165 
166 	if (cur64 < start64 + min_delay || cur64 > start64 + max_delay) {
167 		fprintf(stderr, "ERROR: %" PRId64 " us expected between %d and %d\n",
168 				cur64 - start64, min_delay, max_delay);
169 		if (!getenv("KSFT_MACHINE_SLOW"))
170 			test_failed = true;
171 	}
172 }
173 
174 static void __print_ts_delta_formatted(int64_t ts_delta)
175 {
176 	if (cfg_print_nsec)
177 		fprintf(stderr, "%" PRId64 " ns", ts_delta);
178 	else
179 		fprintf(stderr, "%" PRId64 " us", ts_delta / NSEC_PER_USEC);
180 }
181 
182 static void __print_timestamp(const char *name, struct timespec *cur,
183 			      uint32_t key, int payload_len)
184 {
185 	int64_t ts_delta;
186 
187 	if (!(cur->tv_sec | cur->tv_nsec))
188 		return;
189 
190 	if (cfg_print_nsec)
191 		fprintf(stderr, "  %s: %lu s %lu ns (seq=%u, len=%u)",
192 				name, cur->tv_sec, cur->tv_nsec,
193 				key, payload_len);
194 	else
195 		fprintf(stderr, "  %s: %lu s %lu us (seq=%u, len=%u)",
196 				name, cur->tv_sec, cur->tv_nsec / NSEC_PER_USEC,
197 				key, payload_len);
198 
199 	if (cur != &ts_usr) {
200 		ts_delta = timespec_to_ns64(cur) - timespec_to_ns64(&ts_usr);
201 		fprintf(stderr, "  (USR +");
202 		__print_ts_delta_formatted(ts_delta);
203 		fprintf(stderr, ")");
204 	}
205 
206 	fprintf(stderr, "\n");
207 }
208 
209 static void record_timestamp_usr(void)
210 {
211 	if (clock_gettime(CLOCK_REALTIME, &ts_usr))
212 		error(1, errno, "clock_gettime");
213 }
214 
215 static void print_timestamp(struct scm_timestamping *tss, int tstype,
216 			    int tskey, int payload_len)
217 {
218 	const char *tsname = NULL;
219 
220 	validate_key(tskey, tstype);
221 
222 	switch (tstype) {
223 	case SCM_TSTAMP_SCHED:
224 		tsname = "  ENQ";
225 		validate_timestamp(&tss->ts[0], 0);
226 		add_timing_event(&usr_enq, &ts_usr, &tss->ts[0]);
227 		break;
228 	case SCM_TSTAMP_SND:
229 		tsname = "  SND";
230 		validate_timestamp(&tss->ts[0], cfg_delay_snd);
231 		add_timing_event(&usr_snd, &ts_usr, &tss->ts[0]);
232 		break;
233 	case SCM_TSTAMP_ACK:
234 		tsname = "  ACK";
235 		validate_timestamp(&tss->ts[0], cfg_delay_ack);
236 		add_timing_event(&usr_ack, &ts_usr, &tss->ts[0]);
237 		break;
238 	default:
239 		error(1, 0, "unknown timestamp type: %u",
240 		tstype);
241 	}
242 	__print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
243 }
244 
245 static void print_timing_event(char *name, struct timing_event *te)
246 {
247 	if (!te->count)
248 		return;
249 
250 	fprintf(stderr, "    %s: count=%d", name, te->count);
251 	fprintf(stderr, ", avg=");
252 	__print_ts_delta_formatted((int64_t)(te->total / te->count));
253 	fprintf(stderr, ", min=");
254 	__print_ts_delta_formatted(te->min);
255 	fprintf(stderr, ", max=");
256 	__print_ts_delta_formatted(te->max);
257 	fprintf(stderr, "\n");
258 }
259 
260 /* TODO: convert to check_and_print payload once API is stable */
261 static void print_payload(char *data, int len)
262 {
263 	int i;
264 
265 	if (!len)
266 		return;
267 
268 	if (len > 70)
269 		len = 70;
270 
271 	fprintf(stderr, "payload: ");
272 	for (i = 0; i < len; i++)
273 		fprintf(stderr, "%02hhx ", data[i]);
274 	fprintf(stderr, "\n");
275 }
276 
277 static void print_pktinfo(int family, int ifindex, void *saddr, void *daddr)
278 {
279 	char sa[INET6_ADDRSTRLEN], da[INET6_ADDRSTRLEN];
280 
281 	fprintf(stderr, "         pktinfo: ifindex=%u src=%s dst=%s\n",
282 		ifindex,
283 		saddr ? inet_ntop(family, saddr, sa, sizeof(sa)) : "unknown",
284 		daddr ? inet_ntop(family, daddr, da, sizeof(da)) : "unknown");
285 }
286 
287 static void __epoll(int epfd)
288 {
289 	struct epoll_event events;
290 	int ret;
291 
292 	memset(&events, 0, sizeof(events));
293 	ret = epoll_wait(epfd, &events, 1, cfg_poll_timeout);
294 	if (ret != 1)
295 		error(1, errno, "epoll_wait");
296 }
297 
298 static void __poll(int fd)
299 {
300 	struct pollfd pollfd;
301 	int ret;
302 
303 	memset(&pollfd, 0, sizeof(pollfd));
304 	pollfd.fd = fd;
305 	ret = poll(&pollfd, 1, cfg_poll_timeout);
306 	if (ret != 1)
307 		error(1, errno, "poll");
308 }
309 
310 static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
311 {
312 	struct sock_extended_err *serr = NULL;
313 	struct scm_timestamping *tss = NULL;
314 	struct cmsghdr *cm;
315 	int batch = 0;
316 
317 	for (cm = CMSG_FIRSTHDR(msg);
318 	     cm && cm->cmsg_len;
319 	     cm = CMSG_NXTHDR(msg, cm)) {
320 		if (cm->cmsg_level == SOL_SOCKET &&
321 		    cm->cmsg_type == SCM_TIMESTAMPING) {
322 			tss = (void *) CMSG_DATA(cm);
323 		} else if ((cm->cmsg_level == SOL_IP &&
324 			    cm->cmsg_type == IP_RECVERR) ||
325 			   (cm->cmsg_level == SOL_IPV6 &&
326 			    cm->cmsg_type == IPV6_RECVERR) ||
327 			   (cm->cmsg_level == SOL_PACKET &&
328 			    cm->cmsg_type == PACKET_TX_TIMESTAMP)) {
329 			serr = (void *) CMSG_DATA(cm);
330 			if (serr->ee_errno != ENOMSG ||
331 			    serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
332 				fprintf(stderr, "unknown ip error %d %d\n",
333 						serr->ee_errno,
334 						serr->ee_origin);
335 				serr = NULL;
336 			}
337 		} else if (cm->cmsg_level == SOL_IP &&
338 			   cm->cmsg_type == IP_PKTINFO) {
339 			struct in_pktinfo *info = (void *) CMSG_DATA(cm);
340 			print_pktinfo(AF_INET, info->ipi_ifindex,
341 				      &info->ipi_spec_dst, &info->ipi_addr);
342 		} else if (cm->cmsg_level == SOL_IPV6 &&
343 			   cm->cmsg_type == IPV6_PKTINFO) {
344 			struct in6_pktinfo *info6 = (void *) CMSG_DATA(cm);
345 			print_pktinfo(AF_INET6, info6->ipi6_ifindex,
346 				      NULL, &info6->ipi6_addr);
347 		} else
348 			fprintf(stderr, "unknown cmsg %d,%d\n",
349 					cm->cmsg_level, cm->cmsg_type);
350 
351 		if (serr && tss) {
352 			print_timestamp(tss, serr->ee_info, serr->ee_data,
353 					payload_len);
354 			serr = NULL;
355 			tss = NULL;
356 			batch++;
357 		}
358 	}
359 
360 	if (batch > 1) {
361 		fprintf(stderr, "batched %d timestamps\n", batch);
362 	} else if (!batch) {
363 		fprintf(stderr, "Failed to report timestamps\n");
364 		test_failed = true;
365 	}
366 }
367 
368 static int recv_errmsg(int fd)
369 {
370 	static char ctrl[1024 /* overprovision*/];
371 	static struct msghdr msg;
372 	struct iovec entry;
373 	static char *data;
374 	int ret = 0;
375 
376 	data = malloc(cfg_payload_len);
377 	if (!data)
378 		error(1, 0, "malloc");
379 
380 	memset(&msg, 0, sizeof(msg));
381 	memset(&entry, 0, sizeof(entry));
382 	memset(ctrl, 0, sizeof(ctrl));
383 
384 	entry.iov_base = data;
385 	entry.iov_len = cfg_payload_len;
386 	msg.msg_iov = &entry;
387 	msg.msg_iovlen = 1;
388 	msg.msg_name = NULL;
389 	msg.msg_namelen = 0;
390 	msg.msg_control = ctrl;
391 	msg.msg_controllen = sizeof(ctrl);
392 
393 	ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
394 	if (ret == -1 && errno != EAGAIN)
395 		error(1, errno, "recvmsg");
396 
397 	if (ret >= 0) {
398 		__recv_errmsg_cmsg(&msg, ret);
399 		if (cfg_show_payload)
400 			print_payload(data, cfg_payload_len);
401 	}
402 
403 	free(data);
404 	return ret == -1;
405 }
406 
407 static uint16_t get_ip_csum(const uint16_t *start, int num_words,
408 			    unsigned long sum)
409 {
410 	int i;
411 
412 	for (i = 0; i < num_words; i++)
413 		sum += start[i];
414 
415 	while (sum >> 16)
416 		sum = (sum & 0xFFFF) + (sum >> 16);
417 
418 	return ~sum;
419 }
420 
421 static uint16_t get_udp_csum(const struct udphdr *udph, int alen)
422 {
423 	unsigned long pseudo_sum, csum_len;
424 	const void *csum_start = udph;
425 
426 	pseudo_sum = htons(IPPROTO_UDP);
427 	pseudo_sum += udph->len;
428 
429 	/* checksum ip(v6) addresses + udp header + payload */
430 	csum_start -= alen * 2;
431 	csum_len = ntohs(udph->len) + alen * 2;
432 
433 	return get_ip_csum(csum_start, csum_len >> 1, pseudo_sum);
434 }
435 
436 static int fill_header_ipv4(void *p)
437 {
438 	struct iphdr *iph = p;
439 
440 	memset(iph, 0, sizeof(*iph));
441 
442 	iph->ihl	= 5;
443 	iph->version	= 4;
444 	iph->ttl	= 2;
445 	iph->saddr	= daddr.sin_addr.s_addr;	/* set for udp csum calc */
446 	iph->daddr	= daddr.sin_addr.s_addr;
447 	iph->protocol	= IPPROTO_UDP;
448 
449 	/* kernel writes saddr, csum, len */
450 
451 	return sizeof(*iph);
452 }
453 
454 static int fill_header_ipv6(void *p)
455 {
456 	struct ipv6hdr *ip6h = p;
457 
458 	memset(ip6h, 0, sizeof(*ip6h));
459 
460 	ip6h->version		= 6;
461 	ip6h->payload_len	= htons(sizeof(struct udphdr) + cfg_payload_len);
462 	ip6h->nexthdr		= IPPROTO_UDP;
463 	ip6h->hop_limit		= 64;
464 
465 	ip6h->saddr             = daddr6.sin6_addr;
466 	ip6h->daddr		= daddr6.sin6_addr;
467 
468 	/* kernel does not write saddr in case of ipv6 */
469 
470 	return sizeof(*ip6h);
471 }
472 
473 static void fill_header_udp(void *p, bool is_ipv4)
474 {
475 	struct udphdr *udph = p;
476 
477 	udph->source = ntohs(dest_port + 1);	/* spoof */
478 	udph->dest   = ntohs(dest_port);
479 	udph->len    = ntohs(sizeof(*udph) + cfg_payload_len);
480 	udph->check  = 0;
481 
482 	udph->check  = get_udp_csum(udph, is_ipv4 ? sizeof(struct in_addr) :
483 						    sizeof(struct in6_addr));
484 }
485 
486 static void do_test(int family, unsigned int report_opt)
487 {
488 	char control[2 * CMSG_SPACE(sizeof(uint32_t))];
489 	struct sockaddr_ll laddr;
490 	unsigned int sock_opt;
491 	struct cmsghdr *cmsg;
492 	struct msghdr msg;
493 	struct iovec iov;
494 	char *buf;
495 	int fd, i, val = 1, total_len, epfd = 0;
496 
497 	init_timing_event(&usr_enq);
498 	init_timing_event(&usr_snd);
499 	init_timing_event(&usr_ack);
500 
501 	total_len = cfg_payload_len;
502 	if (cfg_use_pf_packet || cfg_proto == SOCK_RAW) {
503 		total_len += sizeof(struct udphdr);
504 		if (cfg_use_pf_packet || cfg_ipproto == IPPROTO_RAW) {
505 			if (family == PF_INET)
506 				total_len += sizeof(struct iphdr);
507 			else
508 				total_len += sizeof(struct ipv6hdr);
509 		}
510 		/* special case, only rawv6_sendmsg:
511 		 * pass proto in sin6_port if not connected
512 		 * also see ANK comment in net/ipv4/raw.c
513 		 */
514 		daddr6.sin6_port = htons(cfg_ipproto);
515 	}
516 
517 	buf = malloc(total_len);
518 	if (!buf)
519 		error(1, 0, "malloc");
520 
521 	fd = socket(cfg_use_pf_packet ? PF_PACKET : family,
522 		    cfg_proto, cfg_ipproto);
523 	if (fd < 0)
524 		error(1, errno, "socket");
525 
526 	if (cfg_use_epoll) {
527 		struct epoll_event ev;
528 
529 		memset(&ev, 0, sizeof(ev));
530 		ev.data.fd = fd;
531 		if (cfg_epollet)
532 			ev.events |= EPOLLET;
533 		epfd = epoll_create(1);
534 		if (epfd <= 0)
535 			error(1, errno, "epoll_create");
536 		if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev))
537 			error(1, errno, "epoll_ctl");
538 	}
539 
540 	/* reset expected key on each new socket */
541 	saved_tskey = -1;
542 
543 	if (cfg_proto == SOCK_STREAM) {
544 		if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
545 			       (char*) &val, sizeof(val)))
546 			error(1, 0, "setsockopt no nagle");
547 
548 		if (family == PF_INET) {
549 			if (connect(fd, (void *) &daddr, sizeof(daddr)))
550 				error(1, errno, "connect ipv4");
551 		} else {
552 			if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
553 				error(1, errno, "connect ipv6");
554 		}
555 	}
556 
557 	if (cfg_do_pktinfo) {
558 		if (family == AF_INET6) {
559 			if (setsockopt(fd, SOL_IPV6, IPV6_RECVPKTINFO,
560 				       &val, sizeof(val)))
561 				error(1, errno, "setsockopt pktinfo ipv6");
562 		} else {
563 			if (setsockopt(fd, SOL_IP, IP_PKTINFO,
564 				       &val, sizeof(val)))
565 				error(1, errno, "setsockopt pktinfo ipv4");
566 		}
567 	}
568 
569 	sock_opt = SOF_TIMESTAMPING_SOFTWARE |
570 		   SOF_TIMESTAMPING_OPT_CMSG |
571 		   SOF_TIMESTAMPING_OPT_ID;
572 
573 	if (!cfg_use_cmsg)
574 		sock_opt |= report_opt;
575 
576 	if (cfg_loop_nodata)
577 		sock_opt |= SOF_TIMESTAMPING_OPT_TSONLY;
578 
579 	if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
580 		       (char *) &sock_opt, sizeof(sock_opt)))
581 		error(1, 0, "setsockopt timestamping");
582 
583 	for (i = 0; i < cfg_num_pkts; i++) {
584 		memset(&msg, 0, sizeof(msg));
585 		memset(buf, 'a' + i, total_len);
586 
587 		if (cfg_use_pf_packet || cfg_proto == SOCK_RAW) {
588 			int off = 0;
589 
590 			if (cfg_use_pf_packet || cfg_ipproto == IPPROTO_RAW) {
591 				if (family == PF_INET)
592 					off = fill_header_ipv4(buf);
593 				else
594 					off = fill_header_ipv6(buf);
595 			}
596 
597 			fill_header_udp(buf + off, family == PF_INET);
598 		}
599 
600 		iov.iov_base = buf;
601 		iov.iov_len = total_len;
602 
603 		if (cfg_proto != SOCK_STREAM) {
604 			if (cfg_use_pf_packet) {
605 				memset(&laddr, 0, sizeof(laddr));
606 
607 				laddr.sll_family	= AF_PACKET;
608 				laddr.sll_ifindex	= 1;
609 				laddr.sll_protocol	= htons(family == AF_INET ? ETH_P_IP : ETH_P_IPV6);
610 				laddr.sll_halen		= ETH_ALEN;
611 
612 				msg.msg_name = (void *)&laddr;
613 				msg.msg_namelen = sizeof(laddr);
614 			} else if (family == PF_INET) {
615 				msg.msg_name = (void *)&daddr;
616 				msg.msg_namelen = sizeof(daddr);
617 			} else {
618 				msg.msg_name = (void *)&daddr6;
619 				msg.msg_namelen = sizeof(daddr6);
620 			}
621 		}
622 
623 		msg.msg_iov = &iov;
624 		msg.msg_iovlen = 1;
625 
626 		if (cfg_use_cmsg || cfg_use_cmsg_opt_id) {
627 			memset(control, 0, sizeof(control));
628 
629 			msg.msg_control = control;
630 			msg.msg_controllen = cfg_use_cmsg * CMSG_SPACE(sizeof(uint32_t));
631 			msg.msg_controllen += cfg_use_cmsg_opt_id * CMSG_SPACE(sizeof(uint32_t));
632 
633 			cmsg = NULL;
634 			if (cfg_use_cmsg) {
635 				cmsg = CMSG_FIRSTHDR(&msg);
636 				cmsg->cmsg_level = SOL_SOCKET;
637 				cmsg->cmsg_type = SO_TIMESTAMPING;
638 				cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));
639 
640 				*((uint32_t *)CMSG_DATA(cmsg)) = report_opt;
641 			}
642 			if (cfg_use_cmsg_opt_id) {
643 				cmsg = cmsg ? CMSG_NXTHDR(&msg, cmsg) : CMSG_FIRSTHDR(&msg);
644 				cmsg->cmsg_level = SOL_SOCKET;
645 				cmsg->cmsg_type = SCM_TS_OPT_ID;
646 				cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));
647 
648 				*((uint32_t *)CMSG_DATA(cmsg)) = ts_opt_id;
649 				saved_tskey = ts_opt_id;
650 			}
651 
652 		}
653 
654 		record_timestamp_usr();
655 
656 		val = sendmsg(fd, &msg, 0);
657 		if (val != total_len)
658 			error(1, errno, "send");
659 
660 		__print_timestamp("  USR", &ts_usr, 0, 0);
661 
662 		/* wait for all errors to be queued, else ACKs arrive OOO */
663 		if (cfg_sleep_usec)
664 			usleep(cfg_sleep_usec);
665 
666 		if (!cfg_busy_poll) {
667 			if (cfg_use_epoll)
668 				__epoll(epfd);
669 			else
670 				__poll(fd);
671 		}
672 
673 		while (!recv_errmsg(fd)) {}
674 	}
675 
676 	print_timing_event("USR-ENQ", &usr_enq);
677 	print_timing_event("USR-SND", &usr_snd);
678 	print_timing_event("USR-ACK", &usr_ack);
679 
680 	if (close(fd))
681 		error(1, errno, "close");
682 
683 	free(buf);
684 	usleep(100 * NSEC_PER_USEC);
685 }
686 
687 static void __attribute__((noreturn)) usage(const char *filepath)
688 {
689 	fprintf(stderr, "\nUsage: %s [options] hostname\n"
690 			"\nwhere options are:\n"
691 			"  -4:   only IPv4\n"
692 			"  -6:   only IPv6\n"
693 			"  -h:   show this message\n"
694 			"  -b:   busy poll to read from error queue\n"
695 			"  -c N: number of packets for each test\n"
696 			"  -C:   use cmsg to set tstamp recording options\n"
697 			"  -e:   use level-triggered epoll() instead of poll()\n"
698 			"  -E:   use event-triggered epoll() instead of poll()\n"
699 			"  -F:   poll()/epoll() waits forever for an event\n"
700 			"  -I:   request PKTINFO\n"
701 			"  -l N: send N bytes at a time\n"
702 			"  -L    listen on hostname and port\n"
703 			"  -n:   set no-payload option\n"
704 			"  -N:   print timestamps and durations in nsec (instead of usec)\n"
705 			"  -o N: use SCM_TS_OPT_ID control message to provide N as tskey\n"
706 			"  -p N: connect to port N\n"
707 			"  -P:   use PF_PACKET\n"
708 			"  -r:   use raw\n"
709 			"  -R:   use raw (IP_HDRINCL)\n"
710 			"  -S N: usec to sleep before reading error queue\n"
711 			"  -t N: tolerance (usec) for timestamp validation\n"
712 			"  -u:   use udp\n"
713 			"  -v:   validate SND delay (usec)\n"
714 			"  -V:   validate ACK delay (usec)\n"
715 			"  -x:   show payload (up to 70 bytes)\n",
716 			filepath);
717 	exit(1);
718 }
719 
720 static void parse_opt(int argc, char **argv)
721 {
722 	int proto_count = 0;
723 	int c;
724 
725 	while ((c = getopt(argc, argv,
726 				"46bc:CeEFhIl:LnNo:p:PrRS:t:uv:V:x")) != -1) {
727 		switch (c) {
728 		case '4':
729 			do_ipv6 = 0;
730 			break;
731 		case '6':
732 			do_ipv4 = 0;
733 			break;
734 		case 'b':
735 			cfg_busy_poll = true;
736 			break;
737 		case 'c':
738 			cfg_num_pkts = strtoul(optarg, NULL, 10);
739 			break;
740 		case 'C':
741 			cfg_use_cmsg = true;
742 			break;
743 		case 'e':
744 			cfg_use_epoll = true;
745 			break;
746 		case 'E':
747 			cfg_use_epoll = true;
748 			cfg_epollet = true;
749 		case 'F':
750 			cfg_poll_timeout = -1;
751 			break;
752 		case 'I':
753 			cfg_do_pktinfo = true;
754 			break;
755 		case 'l':
756 			cfg_payload_len = strtoul(optarg, NULL, 10);
757 			break;
758 		case 'L':
759 			cfg_do_listen = true;
760 			break;
761 		case 'n':
762 			cfg_loop_nodata = true;
763 			break;
764 		case 'N':
765 			cfg_print_nsec = true;
766 			break;
767 		case 'o':
768 			ts_opt_id = strtoul(optarg, NULL, 10);
769 			cfg_use_cmsg_opt_id = true;
770 			break;
771 		case 'p':
772 			dest_port = strtoul(optarg, NULL, 10);
773 			break;
774 		case 'P':
775 			proto_count++;
776 			cfg_use_pf_packet = true;
777 			cfg_proto = SOCK_DGRAM;
778 			cfg_ipproto = 0;
779 			break;
780 		case 'r':
781 			proto_count++;
782 			cfg_proto = SOCK_RAW;
783 			cfg_ipproto = IPPROTO_UDP;
784 			break;
785 		case 'R':
786 			proto_count++;
787 			cfg_proto = SOCK_RAW;
788 			cfg_ipproto = IPPROTO_RAW;
789 			break;
790 		case 'S':
791 			cfg_sleep_usec = strtoul(optarg, NULL, 10);
792 			break;
793 		case 't':
794 			cfg_delay_tolerance_usec = strtoul(optarg, NULL, 10);
795 			break;
796 		case 'u':
797 			proto_count++;
798 			cfg_proto = SOCK_DGRAM;
799 			cfg_ipproto = IPPROTO_UDP;
800 			break;
801 		case 'v':
802 			cfg_delay_snd = strtoul(optarg, NULL, 10);
803 			break;
804 		case 'V':
805 			cfg_delay_ack = strtoul(optarg, NULL, 10);
806 			break;
807 		case 'x':
808 			cfg_show_payload = true;
809 			break;
810 		case 'h':
811 		default:
812 			usage(argv[0]);
813 		}
814 	}
815 
816 	if (!cfg_payload_len)
817 		error(1, 0, "payload may not be nonzero");
818 	if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
819 		error(1, 0, "udp packet might exceed expected MTU");
820 	if (!do_ipv4 && !do_ipv6)
821 		error(1, 0, "pass -4 or -6, not both");
822 	if (proto_count > 1)
823 		error(1, 0, "pass -P, -r, -R or -u, not multiple");
824 	if (cfg_do_pktinfo && cfg_use_pf_packet)
825 		error(1, 0, "cannot ask for pktinfo over pf_packet");
826 	if (cfg_busy_poll && cfg_use_epoll)
827 		error(1, 0, "pass epoll or busy_poll, not both");
828 	if (cfg_proto == SOCK_STREAM && cfg_use_cmsg_opt_id)
829 		error(1, 0, "TCP sockets don't support SCM_TS_OPT_ID");
830 
831 	if (optind != argc - 1)
832 		error(1, 0, "missing required hostname argument");
833 }
834 
835 static void resolve_hostname(const char *hostname)
836 {
837 	struct addrinfo hints = { .ai_family = do_ipv4 ? AF_INET : AF_INET6 };
838 	struct addrinfo *addrs, *cur;
839 	int have_ipv4 = 0, have_ipv6 = 0;
840 
841 retry:
842 	if (getaddrinfo(hostname, NULL, &hints, &addrs))
843 		error(1, errno, "getaddrinfo");
844 
845 	cur = addrs;
846 	while (cur && !have_ipv4 && !have_ipv6) {
847 		if (!have_ipv4 && cur->ai_family == AF_INET) {
848 			memcpy(&daddr, cur->ai_addr, sizeof(daddr));
849 			daddr.sin_port = htons(dest_port);
850 			have_ipv4 = 1;
851 		}
852 		else if (!have_ipv6 && cur->ai_family == AF_INET6) {
853 			memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
854 			daddr6.sin6_port = htons(dest_port);
855 			have_ipv6 = 1;
856 		}
857 		cur = cur->ai_next;
858 	}
859 	if (addrs)
860 		freeaddrinfo(addrs);
861 
862 	if (do_ipv6 && hints.ai_family != AF_INET6) {
863 		hints.ai_family = AF_INET6;
864 		goto retry;
865 	}
866 
867 	do_ipv4 &= have_ipv4;
868 	do_ipv6 &= have_ipv6;
869 }
870 
871 static void do_listen(int family, void *addr, int alen)
872 {
873 	int fd, type;
874 
875 	type = cfg_proto == SOCK_RAW ? SOCK_DGRAM : cfg_proto;
876 
877 	fd = socket(family, type, 0);
878 	if (fd == -1)
879 		error(1, errno, "socket rx");
880 
881 	if (bind(fd, addr, alen))
882 		error(1, errno, "bind rx");
883 
884 	if (type == SOCK_STREAM && listen(fd, 10))
885 		error(1, errno, "listen rx");
886 
887 	/* leave fd open, will be closed on process exit.
888 	 * this enables connect() to succeed and avoids icmp replies
889 	 */
890 }
891 
892 static void do_main(int family)
893 {
894 	fprintf(stderr, "family:       %s %s\n",
895 			family == PF_INET ? "INET" : "INET6",
896 			cfg_use_pf_packet ? "(PF_PACKET)" : "");
897 
898 	fprintf(stderr, "test SND\n");
899 	do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);
900 
901 	fprintf(stderr, "test ENQ\n");
902 	do_test(family, SOF_TIMESTAMPING_TX_SCHED);
903 
904 	fprintf(stderr, "test ENQ + SND\n");
905 	do_test(family, SOF_TIMESTAMPING_TX_SCHED |
906 			SOF_TIMESTAMPING_TX_SOFTWARE);
907 
908 	if (cfg_proto == SOCK_STREAM) {
909 		fprintf(stderr, "\ntest ACK\n");
910 		do_test(family, SOF_TIMESTAMPING_TX_ACK);
911 
912 		fprintf(stderr, "\ntest SND + ACK\n");
913 		do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
914 				SOF_TIMESTAMPING_TX_ACK);
915 
916 		fprintf(stderr, "\ntest ENQ + SND + ACK\n");
917 		do_test(family, SOF_TIMESTAMPING_TX_SCHED |
918 				SOF_TIMESTAMPING_TX_SOFTWARE |
919 				SOF_TIMESTAMPING_TX_ACK);
920 	}
921 }
922 
923 const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };
924 
925 int main(int argc, char **argv)
926 {
927 	if (argc == 1)
928 		usage(argv[0]);
929 
930 	parse_opt(argc, argv);
931 	resolve_hostname(argv[argc - 1]);
932 
933 	fprintf(stderr, "protocol:     %s\n", sock_names[cfg_proto]);
934 	fprintf(stderr, "payload:      %u\n", cfg_payload_len);
935 	fprintf(stderr, "server port:  %u\n", dest_port);
936 	fprintf(stderr, "\n");
937 
938 	if (do_ipv4) {
939 		if (cfg_do_listen)
940 			do_listen(PF_INET, &daddr, sizeof(daddr));
941 		do_main(PF_INET);
942 	}
943 
944 	if (do_ipv6) {
945 		if (cfg_do_listen)
946 			do_listen(PF_INET6, &daddr6, sizeof(daddr6));
947 		do_main(PF_INET6);
948 	}
949 
950 	return test_failed;
951 }
952