1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Test the SO_TXTIME API
4  *
5  * Takes a stream of { payload, delivery time }[], to be sent across two
6  * processes. Start this program on two separate network namespaces or
7  * connected hosts, one instance in transmit mode and the other in receive
8  * mode using the '-r' option. Receiver will compare arrival timestamps to
9  * the expected stream. Sender will read transmit timestamps from the error
10  * queue. The streams can differ due to out-of-order delivery and drops.
11  */
12 
13 #define _GNU_SOURCE
14 
15 #include <arpa/inet.h>
16 #include <error.h>
17 #include <errno.h>
18 #include <inttypes.h>
19 #include <linux/net_tstamp.h>
20 #include <linux/errqueue.h>
21 #include <linux/if_ether.h>
22 #include <linux/ipv6.h>
23 #include <linux/udp.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <poll.h>
35 
36 static int	cfg_clockid	= CLOCK_TAI;
37 static uint16_t	cfg_port	= 8000;
38 static int	cfg_variance_us	= 4000;
39 static uint64_t	cfg_start_time_ns;
40 static int	cfg_mark;
41 static bool	cfg_rx;
42 
43 static uint64_t glob_tstart;
44 static uint64_t tdeliver_max;
45 
46 /* encode one timed transmission (of a 1B payload) */
47 struct timed_send {
48 	char	data;
49 	int64_t	delay_us;
50 };
51 
52 #define MAX_NUM_PKT	8
53 static struct timed_send cfg_buf[MAX_NUM_PKT];
54 static int cfg_num_pkt;
55 
56 static int cfg_errq_level;
57 static int cfg_errq_type;
58 
59 static struct sockaddr_storage cfg_dst_addr;
60 static struct sockaddr_storage cfg_src_addr;
61 static socklen_t cfg_alen;
62 
gettime_ns(clockid_t clock)63 static uint64_t gettime_ns(clockid_t clock)
64 {
65 	struct timespec ts;
66 
67 	if (clock_gettime(clock, &ts))
68 		error(1, errno, "gettime");
69 
70 	return ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
71 }
72 
do_send_one(int fdt,struct timed_send * ts)73 static void do_send_one(int fdt, struct timed_send *ts)
74 {
75 	char control[CMSG_SPACE(sizeof(uint64_t))];
76 	struct msghdr msg = {0};
77 	struct iovec iov = {0};
78 	struct cmsghdr *cm;
79 	uint64_t tdeliver;
80 	int ret;
81 
82 	iov.iov_base = &ts->data;
83 	iov.iov_len = 1;
84 
85 	msg.msg_iov = &iov;
86 	msg.msg_iovlen = 1;
87 	msg.msg_name = (struct sockaddr *)&cfg_dst_addr;
88 	msg.msg_namelen = cfg_alen;
89 
90 	if (ts->delay_us >= 0) {
91 		memset(control, 0, sizeof(control));
92 		msg.msg_control = &control;
93 		msg.msg_controllen = sizeof(control);
94 
95 		tdeliver = glob_tstart + ts->delay_us * 1000;
96 		tdeliver_max = tdeliver_max > tdeliver ?
97 			       tdeliver_max : tdeliver;
98 
99 		cm = CMSG_FIRSTHDR(&msg);
100 		cm->cmsg_level = SOL_SOCKET;
101 		cm->cmsg_type = SCM_TXTIME;
102 		cm->cmsg_len = CMSG_LEN(sizeof(tdeliver));
103 		memcpy(CMSG_DATA(cm), &tdeliver, sizeof(tdeliver));
104 	}
105 
106 	ret = sendmsg(fdt, &msg, 0);
107 	if (ret == -1)
108 		error(1, errno, "write");
109 	if (ret == 0)
110 		error(1, 0, "write: 0B");
111 
112 }
113 
do_recv_one(int fdr,struct timed_send * ts)114 static void do_recv_one(int fdr, struct timed_send *ts)
115 {
116 	int64_t tstop, texpect;
117 	char rbuf[2];
118 	int ret;
119 
120 	ret = recv(fdr, rbuf, sizeof(rbuf), 0);
121 	if (ret == -1 && errno == EAGAIN)
122 		error(1, EAGAIN, "recv: timeout");
123 	if (ret == -1)
124 		error(1, errno, "read");
125 	if (ret != 1)
126 		error(1, 0, "read: %dB", ret);
127 
128 	tstop = (gettime_ns(cfg_clockid) - glob_tstart) / 1000;
129 	texpect = ts->delay_us >= 0 ? ts->delay_us : 0;
130 
131 	fprintf(stderr, "payload:%c delay:%lld expected:%lld (us)\n",
132 			rbuf[0], (long long)tstop, (long long)texpect);
133 
134 	if (rbuf[0] != ts->data)
135 		error(1, 0, "payload mismatch. expected %c", ts->data);
136 
137 	if (llabs(tstop - texpect) > cfg_variance_us)
138 		error(1, 0, "exceeds variance (%d us)", cfg_variance_us);
139 }
140 
do_recv_verify_empty(int fdr)141 static void do_recv_verify_empty(int fdr)
142 {
143 	char rbuf[1];
144 	int ret;
145 
146 	ret = recv(fdr, rbuf, sizeof(rbuf), 0);
147 	if (ret != -1 || errno != EAGAIN)
148 		error(1, 0, "recv: not empty as expected (%d, %d)", ret, errno);
149 }
150 
do_recv_errqueue_timeout(int fdt)151 static int do_recv_errqueue_timeout(int fdt)
152 {
153 	char control[CMSG_SPACE(sizeof(struct sock_extended_err)) +
154 		     CMSG_SPACE(sizeof(struct sockaddr_in6))] = {0};
155 	char data[sizeof(struct ethhdr) + sizeof(struct ipv6hdr) +
156 		  sizeof(struct udphdr) + 1];
157 	struct sock_extended_err *err;
158 	int ret, num_tstamp = 0;
159 	struct msghdr msg = {0};
160 	struct iovec iov = {0};
161 	struct cmsghdr *cm;
162 	int64_t tstamp = 0;
163 
164 	iov.iov_base = data;
165 	iov.iov_len = sizeof(data);
166 
167 	msg.msg_iov = &iov;
168 	msg.msg_iovlen = 1;
169 
170 	msg.msg_control = control;
171 	msg.msg_controllen = sizeof(control);
172 
173 	while (1) {
174 		const char *reason;
175 
176 		ret = recvmsg(fdt, &msg, MSG_ERRQUEUE);
177 		if (ret == -1 && errno == EAGAIN)
178 			break;
179 		if (ret == -1)
180 			error(1, errno, "errqueue");
181 		if (msg.msg_flags != MSG_ERRQUEUE)
182 			error(1, 0, "errqueue: flags 0x%x\n", msg.msg_flags);
183 
184 		cm = CMSG_FIRSTHDR(&msg);
185 		if (cm->cmsg_level != cfg_errq_level ||
186 		    cm->cmsg_type != cfg_errq_type)
187 			error(1, 0, "errqueue: type 0x%x.0x%x\n",
188 				    cm->cmsg_level, cm->cmsg_type);
189 
190 		err = (struct sock_extended_err *)CMSG_DATA(cm);
191 		if (err->ee_origin != SO_EE_ORIGIN_TXTIME)
192 			error(1, 0, "errqueue: origin 0x%x\n", err->ee_origin);
193 
194 		switch (err->ee_errno) {
195 		case ECANCELED:
196 			if (err->ee_code != SO_EE_CODE_TXTIME_MISSED)
197 				error(1, 0, "errqueue: unknown ECANCELED %u\n",
198 				      err->ee_code);
199 			reason = "missed txtime";
200 		break;
201 		case EINVAL:
202 			if (err->ee_code != SO_EE_CODE_TXTIME_INVALID_PARAM)
203 				error(1, 0, "errqueue: unknown EINVAL %u\n",
204 				      err->ee_code);
205 			reason = "invalid txtime";
206 		break;
207 		default:
208 			error(1, 0, "errqueue: errno %u code %u\n",
209 			      err->ee_errno, err->ee_code);
210 		}
211 
212 		tstamp = ((int64_t) err->ee_data) << 32 | err->ee_info;
213 		tstamp -= (int64_t) glob_tstart;
214 		tstamp /= 1000 * 1000;
215 		fprintf(stderr, "send: pkt %c at %" PRId64 "ms dropped: %s\n",
216 			data[ret - 1], tstamp, reason);
217 
218 		msg.msg_flags = 0;
219 		msg.msg_controllen = sizeof(control);
220 		num_tstamp++;
221 	}
222 
223 	return num_tstamp;
224 }
225 
recv_errqueue_msgs(int fdt)226 static void recv_errqueue_msgs(int fdt)
227 {
228 	struct pollfd pfd = { .fd = fdt, .events = POLLERR };
229 	const int timeout_ms = 10;
230 	int ret, num_tstamp = 0;
231 
232 	do {
233 		ret = poll(&pfd, 1, timeout_ms);
234 		if (ret == -1)
235 			error(1, errno, "poll");
236 
237 		if (ret && (pfd.revents & POLLERR))
238 			num_tstamp += do_recv_errqueue_timeout(fdt);
239 
240 		if (num_tstamp == cfg_num_pkt)
241 			break;
242 
243 	} while (gettime_ns(cfg_clockid) < tdeliver_max);
244 }
245 
start_time_wait(void)246 static void start_time_wait(void)
247 {
248 	uint64_t now;
249 	int err;
250 
251 	if (!cfg_start_time_ns)
252 		return;
253 
254 	now = gettime_ns(CLOCK_REALTIME);
255 	if (cfg_start_time_ns < now)
256 		return;
257 
258 	err = usleep((cfg_start_time_ns - now) / 1000);
259 	if (err)
260 		error(1, errno, "usleep");
261 }
262 
setsockopt_txtime(int fd)263 static void setsockopt_txtime(int fd)
264 {
265 	struct sock_txtime so_txtime_val = { .clockid = cfg_clockid };
266 	struct sock_txtime so_txtime_val_read = { 0 };
267 	socklen_t vallen = sizeof(so_txtime_val);
268 
269 	so_txtime_val.flags = SOF_TXTIME_REPORT_ERRORS;
270 
271 	if (setsockopt(fd, SOL_SOCKET, SO_TXTIME,
272 		       &so_txtime_val, sizeof(so_txtime_val)))
273 		error(1, errno, "setsockopt txtime");
274 
275 	if (getsockopt(fd, SOL_SOCKET, SO_TXTIME,
276 		       &so_txtime_val_read, &vallen))
277 		error(1, errno, "getsockopt txtime");
278 
279 	if (vallen != sizeof(so_txtime_val) ||
280 	    memcmp(&so_txtime_val, &so_txtime_val_read, vallen))
281 		error(1, 0, "getsockopt txtime: mismatch");
282 }
283 
setup_tx(struct sockaddr * addr,socklen_t alen)284 static int setup_tx(struct sockaddr *addr, socklen_t alen)
285 {
286 	int fd;
287 
288 	fd = socket(addr->sa_family, SOCK_DGRAM, 0);
289 	if (fd == -1)
290 		error(1, errno, "socket t");
291 
292 	if (connect(fd, addr, alen))
293 		error(1, errno, "connect");
294 
295 	setsockopt_txtime(fd);
296 
297 	if (cfg_mark &&
298 	    setsockopt(fd, SOL_SOCKET, SO_MARK, &cfg_mark, sizeof(cfg_mark)))
299 		error(1, errno, "setsockopt mark");
300 
301 	return fd;
302 }
303 
setup_rx(struct sockaddr * addr,socklen_t alen)304 static int setup_rx(struct sockaddr *addr, socklen_t alen)
305 {
306 	struct timeval tv = { .tv_usec = 100 * 1000 };
307 	int fd;
308 
309 	fd = socket(addr->sa_family, SOCK_DGRAM, 0);
310 	if (fd == -1)
311 		error(1, errno, "socket r");
312 
313 	if (bind(fd, addr, alen))
314 		error(1, errno, "bind");
315 
316 	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
317 		error(1, errno, "setsockopt rcv timeout");
318 
319 	return fd;
320 }
321 
do_test_tx(struct sockaddr * addr,socklen_t alen)322 static void do_test_tx(struct sockaddr *addr, socklen_t alen)
323 {
324 	int fdt, i;
325 
326 	fprintf(stderr, "\nSO_TXTIME ipv%c clock %s\n",
327 			addr->sa_family == PF_INET ? '4' : '6',
328 			cfg_clockid == CLOCK_TAI ? "tai" : "monotonic");
329 
330 	fdt = setup_tx(addr, alen);
331 
332 	start_time_wait();
333 	glob_tstart = gettime_ns(cfg_clockid);
334 
335 	for (i = 0; i < cfg_num_pkt; i++)
336 		do_send_one(fdt, &cfg_buf[i]);
337 
338 	recv_errqueue_msgs(fdt);
339 
340 	if (close(fdt))
341 		error(1, errno, "close t");
342 }
343 
do_test_rx(struct sockaddr * addr,socklen_t alen)344 static void do_test_rx(struct sockaddr *addr, socklen_t alen)
345 {
346 	int fdr, i;
347 
348 	fdr = setup_rx(addr, alen);
349 
350 	start_time_wait();
351 	glob_tstart = gettime_ns(cfg_clockid);
352 
353 	for (i = 0; i < cfg_num_pkt; i++)
354 		do_recv_one(fdr, &cfg_buf[i]);
355 
356 	do_recv_verify_empty(fdr);
357 
358 	if (close(fdr))
359 		error(1, errno, "close r");
360 }
361 
setup_sockaddr(int domain,const char * str_addr,struct sockaddr_storage * sockaddr)362 static void setup_sockaddr(int domain, const char *str_addr,
363 			   struct sockaddr_storage *sockaddr)
364 {
365 	struct sockaddr_in6 *addr6 = (void *) sockaddr;
366 	struct sockaddr_in *addr4 = (void *) sockaddr;
367 
368 	switch (domain) {
369 	case PF_INET:
370 		memset(addr4, 0, sizeof(*addr4));
371 		addr4->sin_family = AF_INET;
372 		addr4->sin_port = htons(cfg_port);
373 		if (str_addr &&
374 		    inet_pton(AF_INET, str_addr, &(addr4->sin_addr)) != 1)
375 			error(1, 0, "ipv4 parse error: %s", str_addr);
376 		break;
377 	case PF_INET6:
378 		memset(addr6, 0, sizeof(*addr6));
379 		addr6->sin6_family = AF_INET6;
380 		addr6->sin6_port = htons(cfg_port);
381 		if (str_addr &&
382 		    inet_pton(AF_INET6, str_addr, &(addr6->sin6_addr)) != 1)
383 			error(1, 0, "ipv6 parse error: %s", str_addr);
384 		break;
385 	}
386 }
387 
parse_io(const char * optarg,struct timed_send * array)388 static int parse_io(const char *optarg, struct timed_send *array)
389 {
390 	char *arg, *tok;
391 	int aoff = 0;
392 
393 	arg = strdup(optarg);
394 	if (!arg)
395 		error(1, errno, "strdup");
396 
397 	while ((tok = strtok(arg, ","))) {
398 		arg = NULL;	/* only pass non-zero on first call */
399 
400 		if (aoff / 2 == MAX_NUM_PKT)
401 			error(1, 0, "exceeds max pkt count (%d)", MAX_NUM_PKT);
402 
403 		if (aoff & 1) {	/* parse delay */
404 			array->delay_us = strtol(tok, NULL, 0) * 1000;
405 			array++;
406 		} else {	/* parse character */
407 			array->data = tok[0];
408 		}
409 
410 		aoff++;
411 	}
412 
413 	free(arg);
414 
415 	return aoff / 2;
416 }
417 
usage(const char * progname)418 static void usage(const char *progname)
419 {
420 	fprintf(stderr, "\nUsage: %s [options] <payload>\n"
421 			"Options:\n"
422 			"  -4            only IPv4\n"
423 			"  -6            only IPv6\n"
424 			"  -c <clock>    monotonic or tai (default)\n"
425 			"  -D <addr>     destination IP address (server)\n"
426 			"  -S <addr>     source IP address (client)\n"
427 			"  -r            run rx mode\n"
428 			"  -t <nsec>     start time (UTC nanoseconds)\n"
429 			"  -m <mark>     socket mark\n"
430 			"\n",
431 			progname);
432 	exit(1);
433 }
434 
parse_opts(int argc,char ** argv)435 static void parse_opts(int argc, char **argv)
436 {
437 	char *daddr = NULL, *saddr = NULL;
438 	int domain = PF_UNSPEC;
439 	int c;
440 
441 	while ((c = getopt(argc, argv, "46c:S:D:rt:m:")) != -1) {
442 		switch (c) {
443 		case '4':
444 			if (domain != PF_UNSPEC)
445 				error(1, 0, "Pass one of -4 or -6");
446 			domain = PF_INET;
447 			cfg_alen = sizeof(struct sockaddr_in);
448 			cfg_errq_level = SOL_IP;
449 			cfg_errq_type = IP_RECVERR;
450 			break;
451 		case '6':
452 			if (domain != PF_UNSPEC)
453 				error(1, 0, "Pass one of -4 or -6");
454 			domain = PF_INET6;
455 			cfg_alen = sizeof(struct sockaddr_in6);
456 			cfg_errq_level = SOL_IPV6;
457 			cfg_errq_type = IPV6_RECVERR;
458 			break;
459 		case 'c':
460 			if (!strcmp(optarg, "tai"))
461 				cfg_clockid = CLOCK_TAI;
462 			else if (!strcmp(optarg, "monotonic") ||
463 				 !strcmp(optarg, "mono"))
464 				cfg_clockid = CLOCK_MONOTONIC;
465 			else
466 				error(1, 0, "unknown clock id %s", optarg);
467 			break;
468 		case 'S':
469 			saddr = optarg;
470 			break;
471 		case 'D':
472 			daddr = optarg;
473 			break;
474 		case 'r':
475 			cfg_rx = true;
476 			break;
477 		case 't':
478 			cfg_start_time_ns = strtoll(optarg, NULL, 0);
479 			break;
480 		case 'm':
481 			cfg_mark = strtol(optarg, NULL, 0);
482 			break;
483 		default:
484 			usage(argv[0]);
485 		}
486 	}
487 
488 	if (argc - optind != 1)
489 		usage(argv[0]);
490 
491 	if (domain == PF_UNSPEC)
492 		error(1, 0, "Pass one of -4 or -6");
493 	if (!daddr)
494 		error(1, 0, "-D <server addr> required\n");
495 	if (!cfg_rx && !saddr)
496 		error(1, 0, "-S <client addr> required\n");
497 
498 	setup_sockaddr(domain, daddr, &cfg_dst_addr);
499 	setup_sockaddr(domain, saddr, &cfg_src_addr);
500 
501 	cfg_num_pkt = parse_io(argv[optind], cfg_buf);
502 }
503 
main(int argc,char ** argv)504 int main(int argc, char **argv)
505 {
506 	parse_opts(argc, argv);
507 
508 	if (cfg_rx)
509 		do_test_rx((void *)&cfg_dst_addr, cfg_alen);
510 	else
511 		do_test_tx((void *)&cfg_src_addr, cfg_alen);
512 
513 	return 0;
514 }
515