1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Test the SO_TXTIME API
4  *
5  * Takes two streams of { payload, delivery time }[], one input and one output.
6  * Sends the input stream and verifies arrival matches the output stream.
7  * The two streams can differ due to out-of-order delivery and drops.
8  */
9 
10 #define _GNU_SOURCE
11 
12 #include <arpa/inet.h>
13 #include <error.h>
14 #include <errno.h>
15 #include <inttypes.h>
16 #include <linux/net_tstamp.h>
17 #include <linux/errqueue.h>
18 #include <linux/if_ether.h>
19 #include <linux/ipv6.h>
20 #include <linux/udp.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include <unistd.h>
31 
32 static int	cfg_clockid	= CLOCK_TAI;
33 static bool	cfg_do_ipv4;
34 static bool	cfg_do_ipv6;
35 static uint16_t	cfg_port	= 8000;
36 static int	cfg_variance_us	= 4000;
37 
38 static uint64_t glob_tstart;
39 
40 /* encode one timed transmission (of a 1B payload) */
41 struct timed_send {
42 	char	data;
43 	int64_t	delay_us;
44 };
45 
46 #define MAX_NUM_PKT	8
47 static struct timed_send cfg_in[MAX_NUM_PKT];
48 static struct timed_send cfg_out[MAX_NUM_PKT];
49 static int cfg_num_pkt;
50 
51 static int cfg_errq_level;
52 static int cfg_errq_type;
53 
54 static uint64_t gettime_ns(void)
55 {
56 	struct timespec ts;
57 
58 	if (clock_gettime(cfg_clockid, &ts))
59 		error(1, errno, "gettime");
60 
61 	return ts.tv_sec * (1000ULL * 1000 * 1000) + ts.tv_nsec;
62 }
63 
64 static void do_send_one(int fdt, struct timed_send *ts)
65 {
66 	char control[CMSG_SPACE(sizeof(uint64_t))];
67 	struct msghdr msg = {0};
68 	struct iovec iov = {0};
69 	struct cmsghdr *cm;
70 	uint64_t tdeliver;
71 	int ret;
72 
73 	iov.iov_base = &ts->data;
74 	iov.iov_len = 1;
75 
76 	msg.msg_iov = &iov;
77 	msg.msg_iovlen = 1;
78 
79 	if (ts->delay_us >= 0) {
80 		memset(control, 0, sizeof(control));
81 		msg.msg_control = &control;
82 		msg.msg_controllen = sizeof(control);
83 
84 		tdeliver = glob_tstart + ts->delay_us * 1000;
85 
86 		cm = CMSG_FIRSTHDR(&msg);
87 		cm->cmsg_level = SOL_SOCKET;
88 		cm->cmsg_type = SCM_TXTIME;
89 		cm->cmsg_len = CMSG_LEN(sizeof(tdeliver));
90 		memcpy(CMSG_DATA(cm), &tdeliver, sizeof(tdeliver));
91 	}
92 
93 	ret = sendmsg(fdt, &msg, 0);
94 	if (ret == -1)
95 		error(1, errno, "write");
96 	if (ret == 0)
97 		error(1, 0, "write: 0B");
98 
99 }
100 
101 static bool do_recv_one(int fdr, struct timed_send *ts)
102 {
103 	int64_t tstop, texpect;
104 	char rbuf[2];
105 	int ret;
106 
107 	ret = recv(fdr, rbuf, sizeof(rbuf), 0);
108 	if (ret == -1 && errno == EAGAIN)
109 		return true;
110 	if (ret == -1)
111 		error(1, errno, "read");
112 	if (ret != 1)
113 		error(1, 0, "read: %dB", ret);
114 
115 	tstop = (gettime_ns() - glob_tstart) / 1000;
116 	texpect = ts->delay_us >= 0 ? ts->delay_us : 0;
117 
118 	fprintf(stderr, "payload:%c delay:%lld expected:%lld (us)\n",
119 			rbuf[0], (long long)tstop, (long long)texpect);
120 
121 	if (rbuf[0] != ts->data)
122 		error(1, 0, "payload mismatch. expected %c", ts->data);
123 
124 	if (labs(tstop - texpect) > cfg_variance_us)
125 		error(1, 0, "exceeds variance (%d us)", cfg_variance_us);
126 
127 	return false;
128 }
129 
130 static void do_recv_verify_empty(int fdr)
131 {
132 	char rbuf[1];
133 	int ret;
134 
135 	ret = recv(fdr, rbuf, sizeof(rbuf), 0);
136 	if (ret != -1 || errno != EAGAIN)
137 		error(1, 0, "recv: not empty as expected (%d, %d)", ret, errno);
138 }
139 
140 static void do_recv_errqueue_timeout(int fdt)
141 {
142 	char control[CMSG_SPACE(sizeof(struct sock_extended_err)) +
143 		     CMSG_SPACE(sizeof(struct sockaddr_in6))] = {0};
144 	char data[sizeof(struct ethhdr) + sizeof(struct ipv6hdr) +
145 		  sizeof(struct udphdr) + 1];
146 	struct sock_extended_err *err;
147 	struct msghdr msg = {0};
148 	struct iovec iov = {0};
149 	struct cmsghdr *cm;
150 	int64_t tstamp = 0;
151 	int ret;
152 
153 	iov.iov_base = data;
154 	iov.iov_len = sizeof(data);
155 
156 	msg.msg_iov = &iov;
157 	msg.msg_iovlen = 1;
158 
159 	msg.msg_control = control;
160 	msg.msg_controllen = sizeof(control);
161 
162 	while (1) {
163 		const char *reason;
164 
165 		ret = recvmsg(fdt, &msg, MSG_ERRQUEUE);
166 		if (ret == -1 && errno == EAGAIN)
167 			break;
168 		if (ret == -1)
169 			error(1, errno, "errqueue");
170 		if (msg.msg_flags != MSG_ERRQUEUE)
171 			error(1, 0, "errqueue: flags 0x%x\n", msg.msg_flags);
172 
173 		cm = CMSG_FIRSTHDR(&msg);
174 		if (cm->cmsg_level != cfg_errq_level ||
175 		    cm->cmsg_type != cfg_errq_type)
176 			error(1, 0, "errqueue: type 0x%x.0x%x\n",
177 				    cm->cmsg_level, cm->cmsg_type);
178 
179 		err = (struct sock_extended_err *)CMSG_DATA(cm);
180 		if (err->ee_origin != SO_EE_ORIGIN_TXTIME)
181 			error(1, 0, "errqueue: origin 0x%x\n", err->ee_origin);
182 
183 		switch (err->ee_errno) {
184 		case ECANCELED:
185 			if (err->ee_code != SO_EE_CODE_TXTIME_MISSED)
186 				error(1, 0, "errqueue: unknown ECANCELED %u\n",
187 				      err->ee_code);
188 			reason = "missed txtime";
189 		break;
190 		case EINVAL:
191 			if (err->ee_code != SO_EE_CODE_TXTIME_INVALID_PARAM)
192 				error(1, 0, "errqueue: unknown EINVAL %u\n",
193 				      err->ee_code);
194 			reason = "invalid txtime";
195 		break;
196 		default:
197 			error(1, 0, "errqueue: errno %u code %u\n",
198 			      err->ee_errno, err->ee_code);
199 		};
200 
201 		tstamp = ((int64_t) err->ee_data) << 32 | err->ee_info;
202 		tstamp -= (int64_t) glob_tstart;
203 		tstamp /= 1000 * 1000;
204 		fprintf(stderr, "send: pkt %c at %" PRId64 "ms dropped: %s\n",
205 			data[ret - 1], tstamp, reason);
206 
207 		msg.msg_flags = 0;
208 		msg.msg_controllen = sizeof(control);
209 	}
210 
211 	error(1, 0, "recv: timeout");
212 }
213 
214 static void setsockopt_txtime(int fd)
215 {
216 	struct sock_txtime so_txtime_val = { .clockid = cfg_clockid };
217 	struct sock_txtime so_txtime_val_read = { 0 };
218 	socklen_t vallen = sizeof(so_txtime_val);
219 
220 	so_txtime_val.flags = SOF_TXTIME_REPORT_ERRORS;
221 
222 	if (setsockopt(fd, SOL_SOCKET, SO_TXTIME,
223 		       &so_txtime_val, sizeof(so_txtime_val)))
224 		error(1, errno, "setsockopt txtime");
225 
226 	if (getsockopt(fd, SOL_SOCKET, SO_TXTIME,
227 		       &so_txtime_val_read, &vallen))
228 		error(1, errno, "getsockopt txtime");
229 
230 	if (vallen != sizeof(so_txtime_val) ||
231 	    memcmp(&so_txtime_val, &so_txtime_val_read, vallen))
232 		error(1, 0, "getsockopt txtime: mismatch");
233 }
234 
235 static int setup_tx(struct sockaddr *addr, socklen_t alen)
236 {
237 	int fd;
238 
239 	fd = socket(addr->sa_family, SOCK_DGRAM, 0);
240 	if (fd == -1)
241 		error(1, errno, "socket t");
242 
243 	if (connect(fd, addr, alen))
244 		error(1, errno, "connect");
245 
246 	setsockopt_txtime(fd);
247 
248 	return fd;
249 }
250 
251 static int setup_rx(struct sockaddr *addr, socklen_t alen)
252 {
253 	struct timeval tv = { .tv_usec = 100 * 1000 };
254 	int fd;
255 
256 	fd = socket(addr->sa_family, SOCK_DGRAM, 0);
257 	if (fd == -1)
258 		error(1, errno, "socket r");
259 
260 	if (bind(fd, addr, alen))
261 		error(1, errno, "bind");
262 
263 	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
264 		error(1, errno, "setsockopt rcv timeout");
265 
266 	return fd;
267 }
268 
269 static void do_test(struct sockaddr *addr, socklen_t alen)
270 {
271 	int fdt, fdr, i;
272 
273 	fprintf(stderr, "\nSO_TXTIME ipv%c clock %s\n",
274 			addr->sa_family == PF_INET ? '4' : '6',
275 			cfg_clockid == CLOCK_TAI ? "tai" : "monotonic");
276 
277 	fdt = setup_tx(addr, alen);
278 	fdr = setup_rx(addr, alen);
279 
280 	glob_tstart = gettime_ns();
281 
282 	for (i = 0; i < cfg_num_pkt; i++)
283 		do_send_one(fdt, &cfg_in[i]);
284 	for (i = 0; i < cfg_num_pkt; i++)
285 		if (do_recv_one(fdr, &cfg_out[i]))
286 			do_recv_errqueue_timeout(fdt);
287 
288 	do_recv_verify_empty(fdr);
289 
290 	if (close(fdr))
291 		error(1, errno, "close r");
292 	if (close(fdt))
293 		error(1, errno, "close t");
294 }
295 
296 static int parse_io(const char *optarg, struct timed_send *array)
297 {
298 	char *arg, *tok;
299 	int aoff = 0;
300 
301 	arg = strdup(optarg);
302 	if (!arg)
303 		error(1, errno, "strdup");
304 
305 	while ((tok = strtok(arg, ","))) {
306 		arg = NULL;	/* only pass non-zero on first call */
307 
308 		if (aoff / 2 == MAX_NUM_PKT)
309 			error(1, 0, "exceeds max pkt count (%d)", MAX_NUM_PKT);
310 
311 		if (aoff & 1) {	/* parse delay */
312 			array->delay_us = strtol(tok, NULL, 0) * 1000;
313 			array++;
314 		} else {	/* parse character */
315 			array->data = tok[0];
316 		}
317 
318 		aoff++;
319 	}
320 
321 	free(arg);
322 
323 	return aoff / 2;
324 }
325 
326 static void parse_opts(int argc, char **argv)
327 {
328 	int c, ilen, olen;
329 
330 	while ((c = getopt(argc, argv, "46c:")) != -1) {
331 		switch (c) {
332 		case '4':
333 			cfg_do_ipv4 = true;
334 			break;
335 		case '6':
336 			cfg_do_ipv6 = true;
337 			break;
338 		case 'c':
339 			if (!strcmp(optarg, "tai"))
340 				cfg_clockid = CLOCK_TAI;
341 			else if (!strcmp(optarg, "monotonic") ||
342 				 !strcmp(optarg, "mono"))
343 				cfg_clockid = CLOCK_MONOTONIC;
344 			else
345 				error(1, 0, "unknown clock id %s", optarg);
346 			break;
347 		default:
348 			error(1, 0, "parse error at %d", optind);
349 		}
350 	}
351 
352 	if (argc - optind != 2)
353 		error(1, 0, "Usage: %s [-46] -c <clock> <in> <out>", argv[0]);
354 
355 	ilen = parse_io(argv[optind], cfg_in);
356 	olen = parse_io(argv[optind + 1], cfg_out);
357 	if (ilen != olen)
358 		error(1, 0, "i/o streams len mismatch (%d, %d)\n", ilen, olen);
359 	cfg_num_pkt = ilen;
360 }
361 
362 int main(int argc, char **argv)
363 {
364 	parse_opts(argc, argv);
365 
366 	if (cfg_do_ipv6) {
367 		struct sockaddr_in6 addr6 = {0};
368 
369 		addr6.sin6_family = AF_INET6;
370 		addr6.sin6_port = htons(cfg_port);
371 		addr6.sin6_addr = in6addr_loopback;
372 
373 		cfg_errq_level = SOL_IPV6;
374 		cfg_errq_type = IPV6_RECVERR;
375 
376 		do_test((void *)&addr6, sizeof(addr6));
377 	}
378 
379 	if (cfg_do_ipv4) {
380 		struct sockaddr_in addr4 = {0};
381 
382 		addr4.sin_family = AF_INET;
383 		addr4.sin_port = htons(cfg_port);
384 		addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
385 
386 		cfg_errq_level = SOL_IP;
387 		cfg_errq_type = IP_RECVERR;
388 
389 		do_test((void *)&addr4, sizeof(addr4));
390 	}
391 
392 	return 0;
393 }
394