txtimestamp.c 21.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2014 Google Inc.
 * Author: willemb@google.com (Willem de Bruijn)
 *
 * Test software tx timestamping, including
 *
 * - SCHED, SND and ACK timestamps
 * - RAW, UDP and TCP
 * - IPv4 and IPv6
 * - various packet sizes (to test GSO and TSO)
 *
 * Consult the command line arguments for help on running
 * the various testcases.
 *
 * This test requires a dummy TCP server.
 * A simple `nc6 [-u] -l -p $DESTPORT` will do
 */

20 21
#define _GNU_SOURCE

22 23 24 25
#include <arpa/inet.h>
#include <asm/types.h>
#include <error.h>
#include <errno.h>
26
#include <inttypes.h>
27 28
#include <linux/errqueue.h>
#include <linux/if_ether.h>
29
#include <linux/ipv6.h>
30 31 32 33 34 35 36 37 38 39
#include <linux/net_tstamp.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netpacket/packet.h>
#include <poll.h>
#include <stdarg.h>
40
#include <stdbool.h>
41 42 43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
44
#include <sys/epoll.h>
45 46 47 48 49 50 51 52
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

53 54 55 56
#define NSEC_PER_USEC	1000L
#define USEC_PER_SEC	1000000L
#define NSEC_PER_SEC	1000000000LL

57 58 59 60 61 62 63
/* command line parameters */
static int cfg_proto = SOCK_STREAM;
static int cfg_ipproto = IPPROTO_TCP;
static int cfg_num_pkts = 4;
static int do_ipv4 = 1;
static int do_ipv6 = 1;
static int cfg_payload_len = 10;
64
static int cfg_poll_timeout = 100;
65 66
static int cfg_delay_snd;
static int cfg_delay_ack;
67
static int cfg_delay_tolerance_usec = 500;
68 69
static bool cfg_show_payload;
static bool cfg_do_pktinfo;
70 71
static bool cfg_busy_poll;
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 76
static bool cfg_use_epoll;
static bool cfg_epollet;
77
static bool cfg_do_listen;
78
static uint16_t dest_port = 9000;
79
static bool cfg_print_nsec;
80 81 82

static struct sockaddr_in daddr;
static struct sockaddr_in6 daddr6;
83 84 85 86 87
static struct timespec ts_usr;

static int saved_tskey = -1;
static int saved_tskey_type = -1;

88 89 90 91 92 93 94 95 96 97 98
struct timing_event {
	int64_t min;
	int64_t max;
	int64_t total;
	int count;
};

static struct timing_event usr_enq;
static struct timing_event usr_snd;
static struct timing_event usr_ack;

99 100
static bool test_failed;

101 102 103 104 105
static int64_t timespec_to_ns64(struct timespec *ts)
{
	return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
}

106 107
static int64_t timespec_to_us64(struct timespec *ts)
{
108
	return ts->tv_sec * USEC_PER_SEC + ts->tv_nsec / NSEC_PER_USEC;
109 110
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static void init_timing_event(struct timing_event *te)
{
	te->min = INT64_MAX;
	te->max = 0;
	te->total = 0;
	te->count = 0;
}

static void add_timing_event(struct timing_event *te,
		struct timespec *t_start, struct timespec *t_end)
{
	int64_t ts_delta = timespec_to_ns64(t_end) - timespec_to_ns64(t_start);

	te->count++;
	if (ts_delta < te->min)
		te->min = ts_delta;
	if (ts_delta > te->max)
		te->max = ts_delta;
	te->total += ts_delta;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static void validate_key(int tskey, int tstype)
{
	int stepsize;

	/* compare key for each subsequent request
	 * must only test for one type, the first one requested
	 */
	if (saved_tskey == -1)
		saved_tskey_type = tstype;
	else if (saved_tskey_type != tstype)
		return;

	stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;
	if (tskey != saved_tskey + stepsize) {
		fprintf(stderr, "ERROR: key %d, expected %d\n",
				tskey, saved_tskey + stepsize);
		test_failed = true;
	}

	saved_tskey = tskey;
}

static void validate_timestamp(struct timespec *cur, int min_delay)
{
	int64_t cur64, start64;
157
	int max_delay;
158 159 160

	cur64 = timespec_to_us64(cur);
	start64 = timespec_to_us64(&ts_usr);
161
	max_delay = min_delay + cfg_delay_tolerance_usec;
162 163

	if (cur64 < start64 + min_delay || cur64 > start64 + max_delay) {
164
		fprintf(stderr, "ERROR: %lu us expected between %d and %d\n",
165 166 167 168
				cur64 - start64, min_delay, max_delay);
		test_failed = true;
	}
}
169

170 171 172 173 174 175 176 177
static void __print_ts_delta_formatted(int64_t ts_delta)
{
	if (cfg_print_nsec)
		fprintf(stderr, "%lu ns", ts_delta);
	else
		fprintf(stderr, "%lu us", ts_delta / NSEC_PER_USEC);
}

178 179 180
static void __print_timestamp(const char *name, struct timespec *cur,
			      uint32_t key, int payload_len)
{
181 182
	int64_t ts_delta;

183 184 185
	if (!(cur->tv_sec | cur->tv_nsec))
		return;

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	if (cfg_print_nsec)
		fprintf(stderr, "  %s: %lu s %lu ns (seq=%u, len=%u)",
				name, cur->tv_sec, cur->tv_nsec,
				key, payload_len);
	else
		fprintf(stderr, "  %s: %lu s %lu us (seq=%u, len=%u)",
				name, cur->tv_sec, cur->tv_nsec / NSEC_PER_USEC,
				key, payload_len);

	if (cur != &ts_usr) {
		ts_delta = timespec_to_ns64(cur) - timespec_to_ns64(&ts_usr);
		fprintf(stderr, "  (USR +");
		__print_ts_delta_formatted(ts_delta);
		fprintf(stderr, ")");
	}
201 202 203 204 205 206

	fprintf(stderr, "\n");
}

static void print_timestamp_usr(void)
{
207 208
	if (clock_gettime(CLOCK_REALTIME, &ts_usr))
		error(1, errno, "clock_gettime");
209

210
	__print_timestamp("  USR", &ts_usr, 0, 0);
211 212 213 214 215 216 217
}

static void print_timestamp(struct scm_timestamping *tss, int tstype,
			    int tskey, int payload_len)
{
	const char *tsname;

218 219
	validate_key(tskey, tstype);

220 221 222
	switch (tstype) {
	case SCM_TSTAMP_SCHED:
		tsname = "  ENQ";
223
		validate_timestamp(&tss->ts[0], 0);
224
		add_timing_event(&usr_enq, &ts_usr, &tss->ts[0]);
225 226 227
		break;
	case SCM_TSTAMP_SND:
		tsname = "  SND";
228
		validate_timestamp(&tss->ts[0], cfg_delay_snd);
229
		add_timing_event(&usr_snd, &ts_usr, &tss->ts[0]);
230 231 232
		break;
	case SCM_TSTAMP_ACK:
		tsname = "  ACK";
233
		validate_timestamp(&tss->ts[0], cfg_delay_ack);
234
		add_timing_event(&usr_ack, &ts_usr, &tss->ts[0]);
235 236 237 238 239 240 241 242
		break;
	default:
		error(1, 0, "unknown timestamp type: %u",
		tstype);
	}
	__print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static void print_timing_event(char *name, struct timing_event *te)
{
	if (!te->count)
		return;

	fprintf(stderr, "    %s: count=%d", name, te->count);
	fprintf(stderr, ", avg=");
	__print_ts_delta_formatted((int64_t)(te->total / te->count));
	fprintf(stderr, ", min=");
	__print_ts_delta_formatted(te->min);
	fprintf(stderr, ", max=");
	__print_ts_delta_formatted(te->max);
	fprintf(stderr, "\n");
}

258 259 260 261 262
/* TODO: convert to check_and_print payload once API is stable */
static void print_payload(char *data, int len)
{
	int i;

263 264 265
	if (!len)
		return;

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	if (len > 70)
		len = 70;

	fprintf(stderr, "payload: ");
	for (i = 0; i < len; i++)
		fprintf(stderr, "%02hhx ", data[i]);
	fprintf(stderr, "\n");
}

static void print_pktinfo(int family, int ifindex, void *saddr, void *daddr)
{
	char sa[INET6_ADDRSTRLEN], da[INET6_ADDRSTRLEN];

	fprintf(stderr, "         pktinfo: ifindex=%u src=%s dst=%s\n",
		ifindex,
		saddr ? inet_ntop(family, saddr, sa, sizeof(sa)) : "unknown",
		daddr ? inet_ntop(family, daddr, da, sizeof(da)) : "unknown");
}

285 286 287 288 289 290 291 292 293 294 295
static void __epoll(int epfd)
{
	struct epoll_event events;
	int ret;

	memset(&events, 0, sizeof(events));
	ret = epoll_wait(epfd, &events, 1, cfg_poll_timeout);
	if (ret != 1)
		error(1, errno, "epoll_wait");
}

296 297 298 299 300 301 302
static void __poll(int fd)
{
	struct pollfd pollfd;
	int ret;

	memset(&pollfd, 0, sizeof(pollfd));
	pollfd.fd = fd;
303
	ret = poll(&pollfd, 1, cfg_poll_timeout);
304 305 306 307 308 309 310 311 312
	if (ret != 1)
		error(1, errno, "poll");
}

static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
{
	struct sock_extended_err *serr = NULL;
	struct scm_timestamping *tss = NULL;
	struct cmsghdr *cm;
313
	int batch = 0;
314 315 316 317 318 319 320 321

	for (cm = CMSG_FIRSTHDR(msg);
	     cm && cm->cmsg_len;
	     cm = CMSG_NXTHDR(msg, cm)) {
		if (cm->cmsg_level == SOL_SOCKET &&
		    cm->cmsg_type == SCM_TIMESTAMPING) {
			tss = (void *) CMSG_DATA(cm);
		} else if ((cm->cmsg_level == SOL_IP &&
322 323
			    cm->cmsg_type == IP_RECVERR) ||
			   (cm->cmsg_level == SOL_IPV6 &&
324
			    cm->cmsg_type == IPV6_RECVERR) ||
325
			   (cm->cmsg_level == SOL_PACKET &&
326
			    cm->cmsg_type == PACKET_TX_TIMESTAMP)) {
327 328 329 330 331 332 333 334
			serr = (void *) CMSG_DATA(cm);
			if (serr->ee_errno != ENOMSG ||
			    serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
				fprintf(stderr, "unknown ip error %d %d\n",
						serr->ee_errno,
						serr->ee_origin);
				serr = NULL;
			}
335 336 337 338 339 340 341 342 343 344
		} else if (cm->cmsg_level == SOL_IP &&
			   cm->cmsg_type == IP_PKTINFO) {
			struct in_pktinfo *info = (void *) CMSG_DATA(cm);
			print_pktinfo(AF_INET, info->ipi_ifindex,
				      &info->ipi_spec_dst, &info->ipi_addr);
		} else if (cm->cmsg_level == SOL_IPV6 &&
			   cm->cmsg_type == IPV6_PKTINFO) {
			struct in6_pktinfo *info6 = (void *) CMSG_DATA(cm);
			print_pktinfo(AF_INET6, info6->ipi6_ifindex,
				      NULL, &info6->ipi6_addr);
345 346 347
		} else
			fprintf(stderr, "unknown cmsg %d,%d\n",
					cm->cmsg_level, cm->cmsg_type);
348 349 350 351 352 353 354 355

		if (serr && tss) {
			print_timestamp(tss, serr->ee_info, serr->ee_data,
					payload_len);
			serr = NULL;
			tss = NULL;
			batch++;
		}
356 357
	}

358 359
	if (batch > 1)
		fprintf(stderr, "batched %d timestamps\n", batch);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
}

static int recv_errmsg(int fd)
{
	static char ctrl[1024 /* overprovision*/];
	static struct msghdr msg;
	struct iovec entry;
	static char *data;
	int ret = 0;

	data = malloc(cfg_payload_len);
	if (!data)
		error(1, 0, "malloc");

	memset(&msg, 0, sizeof(msg));
	memset(&entry, 0, sizeof(entry));
	memset(ctrl, 0, sizeof(ctrl));

	entry.iov_base = data;
	entry.iov_len = cfg_payload_len;
	msg.msg_iov = &entry;
	msg.msg_iovlen = 1;
	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = ctrl;
	msg.msg_controllen = sizeof(ctrl);

	ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
	if (ret == -1 && errno != EAGAIN)
		error(1, errno, "recvmsg");

391
	if (ret >= 0) {
392 393 394 395
		__recv_errmsg_cmsg(&msg, ret);
		if (cfg_show_payload)
			print_payload(data, cfg_payload_len);
	}
396 397 398 399 400

	free(data);
	return ret == -1;
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
static uint16_t get_ip_csum(const uint16_t *start, int num_words,
			    unsigned long sum)
{
	int i;

	for (i = 0; i < num_words; i++)
		sum += start[i];

	while (sum >> 16)
		sum = (sum & 0xFFFF) + (sum >> 16);

	return ~sum;
}

static uint16_t get_udp_csum(const struct udphdr *udph, int alen)
{
	unsigned long pseudo_sum, csum_len;
	const void *csum_start = udph;

	pseudo_sum = htons(IPPROTO_UDP);
	pseudo_sum += udph->len;

	/* checksum ip(v6) addresses + udp header + payload */
	csum_start -= alen * 2;
	csum_len = ntohs(udph->len) + alen * 2;

	return get_ip_csum(csum_start, csum_len >> 1, pseudo_sum);
}

static int fill_header_ipv4(void *p)
{
	struct iphdr *iph = p;

	memset(iph, 0, sizeof(*iph));

	iph->ihl	= 5;
	iph->version	= 4;
	iph->ttl	= 2;
	iph->saddr	= daddr.sin_addr.s_addr;	/* set for udp csum calc */
	iph->daddr	= daddr.sin_addr.s_addr;
	iph->protocol	= IPPROTO_UDP;

	/* kernel writes saddr, csum, len */

	return sizeof(*iph);
}

static int fill_header_ipv6(void *p)
{
	struct ipv6hdr *ip6h = p;

	memset(ip6h, 0, sizeof(*ip6h));

	ip6h->version		= 6;
	ip6h->payload_len	= htons(sizeof(struct udphdr) + cfg_payload_len);
	ip6h->nexthdr		= IPPROTO_UDP;
	ip6h->hop_limit		= 64;

	ip6h->saddr             = daddr6.sin6_addr;
	ip6h->daddr		= daddr6.sin6_addr;

	/* kernel does not write saddr in case of ipv6 */

	return sizeof(*ip6h);
}

static void fill_header_udp(void *p, bool is_ipv4)
{
	struct udphdr *udph = p;

	udph->source = ntohs(dest_port + 1);	/* spoof */
	udph->dest   = ntohs(dest_port);
	udph->len    = ntohs(sizeof(*udph) + cfg_payload_len);
	udph->check  = 0;

	udph->check  = get_udp_csum(udph, is_ipv4 ? sizeof(struct in_addr) :
						    sizeof(struct in6_addr));
}

480
static void do_test(int family, unsigned int report_opt)
481
{
482
	char control[CMSG_SPACE(sizeof(uint32_t))];
483
	struct sockaddr_ll laddr;
484 485 486 487
	unsigned int sock_opt;
	struct cmsghdr *cmsg;
	struct msghdr msg;
	struct iovec iov;
488
	char *buf;
489
	int fd, i, val = 1, total_len, epfd = 0;
490

491 492 493 494
	init_timing_event(&usr_enq);
	init_timing_event(&usr_snd);
	init_timing_event(&usr_ack);

495
	total_len = cfg_payload_len;
496
	if (cfg_use_pf_packet || cfg_proto == SOCK_RAW) {
497
		total_len += sizeof(struct udphdr);
498 499 500 501 502 503 504 505 506 507 508
		if (cfg_use_pf_packet || cfg_ipproto == IPPROTO_RAW)
			if (family == PF_INET)
				total_len += sizeof(struct iphdr);
			else
				total_len += sizeof(struct ipv6hdr);

		/* special case, only rawv6_sendmsg:
		 * pass proto in sin6_port if not connected
		 * also see ANK comment in net/ipv4/raw.c
		 */
		daddr6.sin6_port = htons(cfg_ipproto);
509 510 511 512 513 514
	}

	buf = malloc(total_len);
	if (!buf)
		error(1, 0, "malloc");

515 516
	fd = socket(cfg_use_pf_packet ? PF_PACKET : family,
		    cfg_proto, cfg_ipproto);
517 518 519
	if (fd < 0)
		error(1, errno, "socket");

520 521 522 523 524 525 526 527 528 529 530 531 532 533
	if (cfg_use_epoll) {
		struct epoll_event ev;

		memset(&ev, 0, sizeof(ev));
		ev.data.fd = fd;
		if (cfg_epollet)
			ev.events |= EPOLLET;
		epfd = epoll_create(1);
		if (epfd <= 0)
			error(1, errno, "epoll_create");
		if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev))
			error(1, errno, "epoll_ctl");
	}

534 535 536
	/* reset expected key on each new socket */
	saved_tskey = -1;

537 538 539 540 541 542 543 544 545 546 547 548 549 550
	if (cfg_proto == SOCK_STREAM) {
		if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
			       (char*) &val, sizeof(val)))
			error(1, 0, "setsockopt no nagle");

		if (family == PF_INET) {
			if (connect(fd, (void *) &daddr, sizeof(daddr)))
				error(1, errno, "connect ipv4");
		} else {
			if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
				error(1, errno, "connect ipv6");
		}
	}

551 552 553 554 555 556 557 558 559 560 561 562
	if (cfg_do_pktinfo) {
		if (family == AF_INET6) {
			if (setsockopt(fd, SOL_IPV6, IPV6_RECVPKTINFO,
				       &val, sizeof(val)))
				error(1, errno, "setsockopt pktinfo ipv6");
		} else {
			if (setsockopt(fd, SOL_IP, IP_PKTINFO,
				       &val, sizeof(val)))
				error(1, errno, "setsockopt pktinfo ipv4");
		}
	}

563 564 565 566 567 568 569
	sock_opt = SOF_TIMESTAMPING_SOFTWARE |
		   SOF_TIMESTAMPING_OPT_CMSG |
		   SOF_TIMESTAMPING_OPT_ID;

	if (!cfg_use_cmsg)
		sock_opt |= report_opt;

570
	if (cfg_loop_nodata)
571
		sock_opt |= SOF_TIMESTAMPING_OPT_TSONLY;
572

573
	if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
574
		       (char *) &sock_opt, sizeof(sock_opt)))
575 576 577
		error(1, 0, "setsockopt timestamping");

	for (i = 0; i < cfg_num_pkts; i++) {
578
		memset(&msg, 0, sizeof(msg));
579 580
		memset(buf, 'a' + i, total_len);

581
		if (cfg_use_pf_packet || cfg_proto == SOCK_RAW) {
582 583
			int off = 0;

584 585 586 587 588
			if (cfg_use_pf_packet || cfg_ipproto == IPPROTO_RAW) {
				if (family == PF_INET)
					off = fill_header_ipv4(buf);
				else
					off = fill_header_ipv6(buf);
589 590
			}

591
			fill_header_udp(buf + off, family == PF_INET);
592 593 594
		}

		print_timestamp_usr();
595 596 597 598

		iov.iov_base = buf;
		iov.iov_len = total_len;

599
		if (cfg_proto != SOCK_STREAM) {
600 601 602 603 604 605 606 607 608 609 610
			if (cfg_use_pf_packet) {
				memset(&laddr, 0, sizeof(laddr));

				laddr.sll_family	= AF_PACKET;
				laddr.sll_ifindex	= 1;
				laddr.sll_protocol	= htons(family == AF_INET ? ETH_P_IP : ETH_P_IPV6);
				laddr.sll_halen		= ETH_ALEN;

				msg.msg_name = (void *)&laddr;
				msg.msg_namelen = sizeof(laddr);
			} else if (family == PF_INET) {
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
				msg.msg_name = (void *)&daddr;
				msg.msg_namelen = sizeof(daddr);
			} else {
				msg.msg_name = (void *)&daddr6;
				msg.msg_namelen = sizeof(daddr6);
			}
		}

		msg.msg_iov = &iov;
		msg.msg_iovlen = 1;

		if (cfg_use_cmsg) {
			memset(control, 0, sizeof(control));

			msg.msg_control = control;
			msg.msg_controllen = sizeof(control);

			cmsg = CMSG_FIRSTHDR(&msg);
			cmsg->cmsg_level = SOL_SOCKET;
			cmsg->cmsg_type = SO_TIMESTAMPING;
			cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));

			*((uint32_t *) CMSG_DATA(cmsg)) = report_opt;
634
		}
635 636

		val = sendmsg(fd, &msg, 0);
637 638 639 640
		if (val != total_len)
			error(1, errno, "send");

		/* wait for all errors to be queued, else ACKs arrive OOO */
641 642
		if (cfg_sleep_usec)
			usleep(cfg_sleep_usec);
643

644 645 646 647 648 649
		if (!cfg_busy_poll) {
			if (cfg_use_epoll)
				__epoll(epfd);
			else
				__poll(fd);
		}
650 651 652 653

		while (!recv_errmsg(fd)) {}
	}

654 655 656 657
	print_timing_event("USR-ENQ", &usr_enq);
	print_timing_event("USR-SND", &usr_snd);
	print_timing_event("USR-ACK", &usr_ack);

658 659 660 661
	if (close(fd))
		error(1, errno, "close");

	free(buf);
662
	usleep(100 * NSEC_PER_USEC);
663 664 665 666 667 668 669 670 671
}

static void __attribute__((noreturn)) usage(const char *filepath)
{
	fprintf(stderr, "\nUsage: %s [options] hostname\n"
			"\nwhere options are:\n"
			"  -4:   only IPv4\n"
			"  -6:   only IPv6\n"
			"  -h:   show this message\n"
672
			"  -b:   busy poll to read from error queue\n"
673
			"  -c N: number of packets for each test\n"
674
			"  -C:   use cmsg to set tstamp recording options\n"
675 676 677
			"  -e:   use level-triggered epoll() instead of poll()\n"
			"  -E:   use event-triggered epoll() instead of poll()\n"
			"  -F:   poll()/epoll() waits forever for an event\n"
678
			"  -I:   request PKTINFO\n"
679
			"  -l N: send N bytes at a time\n"
680
			"  -L    listen on hostname and port\n"
681
			"  -n:   set no-payload option\n"
682
			"  -N:   print timestamps and durations in nsec (instead of usec)\n"
683 684
			"  -p N: connect to port N\n"
			"  -P:   use PF_PACKET\n"
685 686
			"  -r:   use raw\n"
			"  -R:   use raw (IP_HDRINCL)\n"
687
			"  -S N: usec to sleep before reading error queue\n"
688
			"  -t N: tolerance (usec) for timestamp validation\n"
689
			"  -u:   use udp\n"
690 691
			"  -v:   validate SND delay (usec)\n"
			"  -V:   validate ACK delay (usec)\n"
692
			"  -x:   show payload (up to 70 bytes)\n",
693 694 695 696 697 698 699
			filepath);
	exit(1);
}

static void parse_opt(int argc, char **argv)
{
	int proto_count = 0;
700
	int c;
701

702
	while ((c = getopt(argc, argv,
703
				"46bc:CeEFhIl:LnNp:PrRS:t:uv:V:x")) != -1) {
704 705 706 707 708 709 710
		switch (c) {
		case '4':
			do_ipv6 = 0;
			break;
		case '6':
			do_ipv4 = 0;
			break;
711 712 713
		case 'b':
			cfg_busy_poll = true;
			break;
714 715 716
		case 'c':
			cfg_num_pkts = strtoul(optarg, NULL, 10);
			break;
717 718 719
		case 'C':
			cfg_use_cmsg = true;
			break;
720 721 722 723 724 725
		case 'e':
			cfg_use_epoll = true;
			break;
		case 'E':
			cfg_use_epoll = true;
			cfg_epollet = true;
726 727 728
		case 'F':
			cfg_poll_timeout = -1;
			break;
729 730 731
		case 'I':
			cfg_do_pktinfo = true;
			break;
732 733 734
		case 'l':
			cfg_payload_len = strtoul(optarg, NULL, 10);
			break;
735 736 737
		case 'L':
			cfg_do_listen = true;
			break;
738 739 740
		case 'n':
			cfg_loop_nodata = true;
			break;
741 742 743
		case 'N':
			cfg_print_nsec = true;
			break;
744 745 746 747 748 749 750 751 752
		case 'p':
			dest_port = strtoul(optarg, NULL, 10);
			break;
		case 'P':
			proto_count++;
			cfg_use_pf_packet = true;
			cfg_proto = SOCK_DGRAM;
			cfg_ipproto = 0;
			break;
753 754 755 756 757 758 759 760 761 762
		case 'r':
			proto_count++;
			cfg_proto = SOCK_RAW;
			cfg_ipproto = IPPROTO_UDP;
			break;
		case 'R':
			proto_count++;
			cfg_proto = SOCK_RAW;
			cfg_ipproto = IPPROTO_RAW;
			break;
763 764 765
		case 'S':
			cfg_sleep_usec = strtoul(optarg, NULL, 10);
			break;
766 767 768
		case 't':
			cfg_delay_tolerance_usec = strtoul(optarg, NULL, 10);
			break;
769 770 771 772 773
		case 'u':
			proto_count++;
			cfg_proto = SOCK_DGRAM;
			cfg_ipproto = IPPROTO_UDP;
			break;
774 775 776 777 778 779
		case 'v':
			cfg_delay_snd = strtoul(optarg, NULL, 10);
			break;
		case 'V':
			cfg_delay_ack = strtoul(optarg, NULL, 10);
			break;
780 781 782
		case 'x':
			cfg_show_payload = true;
			break;
783 784 785 786 787 788 789 790 791 792 793 794 795
		case 'h':
		default:
			usage(argv[0]);
		}
	}

	if (!cfg_payload_len)
		error(1, 0, "payload may not be nonzero");
	if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
		error(1, 0, "udp packet might exceed expected MTU");
	if (!do_ipv4 && !do_ipv6)
		error(1, 0, "pass -4 or -6, not both");
	if (proto_count > 1)
796 797 798
		error(1, 0, "pass -P, -r, -R or -u, not multiple");
	if (cfg_do_pktinfo && cfg_use_pf_packet)
		error(1, 0, "cannot ask for pktinfo over pf_packet");
799 800
	if (cfg_busy_poll && cfg_use_epoll)
		error(1, 0, "pass epoll or busy_poll, not both");
801 802 803 804 805 806 807

	if (optind != argc - 1)
		error(1, 0, "missing required hostname argument");
}

static void resolve_hostname(const char *hostname)
{
808
	struct addrinfo hints = { .ai_family = do_ipv4 ? AF_INET : AF_INET6 };
809 810 811
	struct addrinfo *addrs, *cur;
	int have_ipv4 = 0, have_ipv6 = 0;

812 813
retry:
	if (getaddrinfo(hostname, NULL, &hints, &addrs))
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
		error(1, errno, "getaddrinfo");

	cur = addrs;
	while (cur && !have_ipv4 && !have_ipv6) {
		if (!have_ipv4 && cur->ai_family == AF_INET) {
			memcpy(&daddr, cur->ai_addr, sizeof(daddr));
			daddr.sin_port = htons(dest_port);
			have_ipv4 = 1;
		}
		else if (!have_ipv6 && cur->ai_family == AF_INET6) {
			memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
			daddr6.sin6_port = htons(dest_port);
			have_ipv6 = 1;
		}
		cur = cur->ai_next;
	}
	if (addrs)
		freeaddrinfo(addrs);

833 834 835 836 837
	if (do_ipv6 && hints.ai_family != AF_INET6) {
		hints.ai_family = AF_INET6;
		goto retry;
	}

838 839 840 841
	do_ipv4 &= have_ipv4;
	do_ipv6 &= have_ipv6;
}

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
static void do_listen(int family, void *addr, int alen)
{
	int fd, type;

	type = cfg_proto == SOCK_RAW ? SOCK_DGRAM : cfg_proto;

	fd = socket(family, type, 0);
	if (fd == -1)
		error(1, errno, "socket rx");

	if (bind(fd, addr, alen))
		error(1, errno, "bind rx");

	if (type == SOCK_STREAM && listen(fd, 10))
		error(1, errno, "listen rx");

	/* leave fd open, will be closed on process exit.
	 * this enables connect() to succeed and avoids icmp replies
	 */
}

863 864
static void do_main(int family)
{
865 866 867
	fprintf(stderr, "family:       %s %s\n",
			family == PF_INET ? "INET" : "INET6",
			cfg_use_pf_packet ? "(PF_PACKET)" : "");
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

	fprintf(stderr, "test SND\n");
	do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);

	fprintf(stderr, "test ENQ\n");
	do_test(family, SOF_TIMESTAMPING_TX_SCHED);

	fprintf(stderr, "test ENQ + SND\n");
	do_test(family, SOF_TIMESTAMPING_TX_SCHED |
			SOF_TIMESTAMPING_TX_SOFTWARE);

	if (cfg_proto == SOCK_STREAM) {
		fprintf(stderr, "\ntest ACK\n");
		do_test(family, SOF_TIMESTAMPING_TX_ACK);

		fprintf(stderr, "\ntest SND + ACK\n");
		do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
				SOF_TIMESTAMPING_TX_ACK);

		fprintf(stderr, "\ntest ENQ + SND + ACK\n");
		do_test(family, SOF_TIMESTAMPING_TX_SCHED |
				SOF_TIMESTAMPING_TX_SOFTWARE |
				SOF_TIMESTAMPING_TX_ACK);
	}
}

const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };

int main(int argc, char **argv)
{
	if (argc == 1)
		usage(argv[0]);

	parse_opt(argc, argv);
	resolve_hostname(argv[argc - 1]);

	fprintf(stderr, "protocol:     %s\n", sock_names[cfg_proto]);
	fprintf(stderr, "payload:      %u\n", cfg_payload_len);
	fprintf(stderr, "server port:  %u\n", dest_port);
	fprintf(stderr, "\n");

909 910 911
	if (do_ipv4) {
		if (cfg_do_listen)
			do_listen(PF_INET, &daddr, sizeof(daddr));
912
		do_main(PF_INET);
913 914 915 916 917
	}

	if (do_ipv6) {
		if (cfg_do_listen)
			do_listen(PF_INET6, &daddr6, sizeof(daddr6));
918
		do_main(PF_INET6);
919
	}
920

921
	return test_failed;
922
}