2 * Copyright 2014 Google Inc.
3 * Author: willemb@google.com (Willem de Bruijn)
5 * Test software tx timestamping, including
7 * - SCHED, SND and ACK timestamps
10 * - various packet sizes (to test GSO and TSO)
12 * Consult the command line arguments for help on running
13 * the various testcases.
15 * This test requires a dummy TCP server.
16 * A simple `nc6 [-u] -l -p $DESTPORT` will do
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms and conditions of the GNU General Public License,
21 * version 2, as published by the Free Software Foundation.
23 * This program is distributed in the hope it will be useful, but WITHOUT
24 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
28 * You should have received a copy of the GNU General Public License along with
29 * this program; if not, write to the Free Software Foundation, Inc.,
30 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
35 #include <arpa/inet.h>
36 #include <asm/types.h>
40 #include <linux/errqueue.h>
41 #include <linux/if_ether.h>
42 #include <linux/net_tstamp.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/udp.h>
48 #include <netinet/tcp.h>
49 #include <netpacket/packet.h>
56 #include <sys/ioctl.h>
57 #include <sys/select.h>
58 #include <sys/socket.h>
60 #include <sys/types.h>
64 /* command line parameters */
65 static int cfg_proto = SOCK_STREAM;
66 static int cfg_ipproto = IPPROTO_TCP;
67 static int cfg_num_pkts = 4;
68 static int do_ipv4 = 1;
69 static int do_ipv6 = 1;
70 static int cfg_payload_len = 10;
71 static int cfg_poll_timeout = 100;
72 static bool cfg_show_payload;
73 static bool cfg_do_pktinfo;
74 static bool cfg_loop_nodata;
75 static bool cfg_no_delay;
76 static uint16_t dest_port = 9000;
78 static struct sockaddr_in daddr;
79 static struct sockaddr_in6 daddr6;
80 static struct timespec ts_prev;
82 static void __print_timestamp(const char *name, struct timespec *cur,
83 uint32_t key, int payload_len)
85 if (!(cur->tv_sec | cur->tv_nsec))
88 fprintf(stderr, " %s: %lu s %lu us (seq=%u, len=%u)",
89 name, cur->tv_sec, cur->tv_nsec / 1000,
92 if ((ts_prev.tv_sec | ts_prev.tv_nsec)) {
93 int64_t cur_ms, prev_ms;
95 cur_ms = (long) cur->tv_sec * 1000 * 1000;
96 cur_ms += cur->tv_nsec / 1000;
98 prev_ms = (long) ts_prev.tv_sec * 1000 * 1000;
99 prev_ms += ts_prev.tv_nsec / 1000;
101 fprintf(stderr, " (%+" PRId64 " us)", cur_ms - prev_ms);
105 fprintf(stderr, "\n");
108 static void print_timestamp_usr(void)
111 struct timeval tv; /* avoid dependency on -lrt */
113 gettimeofday(&tv, NULL);
114 ts.tv_sec = tv.tv_sec;
115 ts.tv_nsec = tv.tv_usec * 1000;
117 __print_timestamp(" USR", &ts, 0, 0);
120 static void print_timestamp(struct scm_timestamping *tss, int tstype,
121 int tskey, int payload_len)
126 case SCM_TSTAMP_SCHED:
136 error(1, 0, "unknown timestamp type: %u",
139 __print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
142 /* TODO: convert to check_and_print payload once API is stable */
143 static void print_payload(char *data, int len)
153 fprintf(stderr, "payload: ");
154 for (i = 0; i < len; i++)
155 fprintf(stderr, "%02hhx ", data[i]);
156 fprintf(stderr, "\n");
159 static void print_pktinfo(int family, int ifindex, void *saddr, void *daddr)
161 char sa[INET6_ADDRSTRLEN], da[INET6_ADDRSTRLEN];
163 fprintf(stderr, " pktinfo: ifindex=%u src=%s dst=%s\n",
165 saddr ? inet_ntop(family, saddr, sa, sizeof(sa)) : "unknown",
166 daddr ? inet_ntop(family, daddr, da, sizeof(da)) : "unknown");
169 static void __poll(int fd)
171 struct pollfd pollfd;
174 memset(&pollfd, 0, sizeof(pollfd));
176 ret = poll(&pollfd, 1, cfg_poll_timeout);
178 error(1, errno, "poll");
181 static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
183 struct sock_extended_err *serr = NULL;
184 struct scm_timestamping *tss = NULL;
188 for (cm = CMSG_FIRSTHDR(msg);
190 cm = CMSG_NXTHDR(msg, cm)) {
191 if (cm->cmsg_level == SOL_SOCKET &&
192 cm->cmsg_type == SCM_TIMESTAMPING) {
193 tss = (void *) CMSG_DATA(cm);
194 } else if ((cm->cmsg_level == SOL_IP &&
195 cm->cmsg_type == IP_RECVERR) ||
196 (cm->cmsg_level == SOL_IPV6 &&
197 cm->cmsg_type == IPV6_RECVERR)) {
198 serr = (void *) CMSG_DATA(cm);
199 if (serr->ee_errno != ENOMSG ||
200 serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
201 fprintf(stderr, "unknown ip error %d %d\n",
206 } else if (cm->cmsg_level == SOL_IP &&
207 cm->cmsg_type == IP_PKTINFO) {
208 struct in_pktinfo *info = (void *) CMSG_DATA(cm);
209 print_pktinfo(AF_INET, info->ipi_ifindex,
210 &info->ipi_spec_dst, &info->ipi_addr);
211 } else if (cm->cmsg_level == SOL_IPV6 &&
212 cm->cmsg_type == IPV6_PKTINFO) {
213 struct in6_pktinfo *info6 = (void *) CMSG_DATA(cm);
214 print_pktinfo(AF_INET6, info6->ipi6_ifindex,
215 NULL, &info6->ipi6_addr);
217 fprintf(stderr, "unknown cmsg %d,%d\n",
218 cm->cmsg_level, cm->cmsg_type);
221 print_timestamp(tss, serr->ee_info, serr->ee_data,
230 fprintf(stderr, "batched %d timestamps\n", batch);
233 static int recv_errmsg(int fd)
235 static char ctrl[1024 /* overprovision*/];
236 static struct msghdr msg;
241 data = malloc(cfg_payload_len);
243 error(1, 0, "malloc");
245 memset(&msg, 0, sizeof(msg));
246 memset(&entry, 0, sizeof(entry));
247 memset(ctrl, 0, sizeof(ctrl));
249 entry.iov_base = data;
250 entry.iov_len = cfg_payload_len;
251 msg.msg_iov = &entry;
255 msg.msg_control = ctrl;
256 msg.msg_controllen = sizeof(ctrl);
258 ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
259 if (ret == -1 && errno != EAGAIN)
260 error(1, errno, "recvmsg");
263 __recv_errmsg_cmsg(&msg, ret);
264 if (cfg_show_payload)
265 print_payload(data, cfg_payload_len);
272 static void do_test(int family, unsigned int opt)
275 int fd, i, val = 1, total_len;
277 if (family == AF_INET6 && cfg_proto != SOCK_STREAM) {
278 /* due to lack of checksum generation code */
279 fprintf(stderr, "test: skipping datagram over IPv6\n");
283 total_len = cfg_payload_len;
284 if (cfg_proto == SOCK_RAW) {
285 total_len += sizeof(struct udphdr);
286 if (cfg_ipproto == IPPROTO_RAW)
287 total_len += sizeof(struct iphdr);
290 buf = malloc(total_len);
292 error(1, 0, "malloc");
294 fd = socket(family, cfg_proto, cfg_ipproto);
296 error(1, errno, "socket");
298 if (cfg_proto == SOCK_STREAM) {
299 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
300 (char*) &val, sizeof(val)))
301 error(1, 0, "setsockopt no nagle");
303 if (family == PF_INET) {
304 if (connect(fd, (void *) &daddr, sizeof(daddr)))
305 error(1, errno, "connect ipv4");
307 if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
308 error(1, errno, "connect ipv6");
312 if (cfg_do_pktinfo) {
313 if (family == AF_INET6) {
314 if (setsockopt(fd, SOL_IPV6, IPV6_RECVPKTINFO,
316 error(1, errno, "setsockopt pktinfo ipv6");
318 if (setsockopt(fd, SOL_IP, IP_PKTINFO,
320 error(1, errno, "setsockopt pktinfo ipv4");
324 opt |= SOF_TIMESTAMPING_SOFTWARE |
325 SOF_TIMESTAMPING_OPT_CMSG |
326 SOF_TIMESTAMPING_OPT_ID;
328 opt |= SOF_TIMESTAMPING_OPT_TSONLY;
330 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
331 (char *) &opt, sizeof(opt)))
332 error(1, 0, "setsockopt timestamping");
334 for (i = 0; i < cfg_num_pkts; i++) {
335 memset(&ts_prev, 0, sizeof(ts_prev));
336 memset(buf, 'a' + i, total_len);
338 if (cfg_proto == SOCK_RAW) {
342 if (cfg_ipproto == IPPROTO_RAW) {
343 struct iphdr *iph = (void *) buf;
345 memset(iph, 0, sizeof(*iph));
349 iph->daddr = daddr.sin_addr.s_addr;
350 iph->protocol = IPPROTO_UDP;
351 /* kernel writes saddr, csum, len */
356 udph = (void *) buf + off;
357 udph->source = ntohs(9000); /* random spoof */
358 udph->dest = ntohs(dest_port);
359 udph->len = ntohs(sizeof(*udph) + cfg_payload_len);
360 udph->check = 0; /* not allowed for IPv6 */
363 print_timestamp_usr();
364 if (cfg_proto != SOCK_STREAM) {
365 if (family == PF_INET)
366 val = sendto(fd, buf, total_len, 0, (void *) &daddr, sizeof(daddr));
368 val = sendto(fd, buf, total_len, 0, (void *) &daddr6, sizeof(daddr6));
370 val = send(fd, buf, cfg_payload_len, 0);
372 if (val != total_len)
373 error(1, errno, "send");
375 /* wait for all errors to be queued, else ACKs arrive OOO */
381 while (!recv_errmsg(fd)) {}
385 error(1, errno, "close");
391 static void __attribute__((noreturn)) usage(const char *filepath)
393 fprintf(stderr, "\nUsage: %s [options] hostname\n"
394 "\nwhere options are:\n"
397 " -h: show this message\n"
398 " -c N: number of packets for each test\n"
399 " -D: no delay between packets\n"
400 " -F: poll() waits forever for an event\n"
401 " -I: request PKTINFO\n"
402 " -l N: send N bytes at a time\n"
403 " -n: set no-payload option\n"
405 " -R: use raw (IP_HDRINCL)\n"
406 " -p N: connect to port N\n"
408 " -x: show payload (up to 70 bytes)\n",
413 static void parse_opt(int argc, char **argv)
418 while ((c = getopt(argc, argv, "46c:DFhIl:np:rRux")) != -1) {
427 cfg_num_pkts = strtoul(optarg, NULL, 10);
433 cfg_poll_timeout = -1;
436 cfg_do_pktinfo = true;
439 cfg_loop_nodata = true;
443 cfg_proto = SOCK_RAW;
444 cfg_ipproto = IPPROTO_UDP;
448 cfg_proto = SOCK_RAW;
449 cfg_ipproto = IPPROTO_RAW;
453 cfg_proto = SOCK_DGRAM;
454 cfg_ipproto = IPPROTO_UDP;
457 cfg_payload_len = strtoul(optarg, NULL, 10);
460 dest_port = strtoul(optarg, NULL, 10);
463 cfg_show_payload = true;
471 if (!cfg_payload_len)
472 error(1, 0, "payload may not be nonzero");
473 if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
474 error(1, 0, "udp packet might exceed expected MTU");
475 if (!do_ipv4 && !do_ipv6)
476 error(1, 0, "pass -4 or -6, not both");
478 error(1, 0, "pass -r, -R or -u, not multiple");
480 if (optind != argc - 1)
481 error(1, 0, "missing required hostname argument");
484 static void resolve_hostname(const char *hostname)
486 struct addrinfo *addrs, *cur;
487 int have_ipv4 = 0, have_ipv6 = 0;
489 if (getaddrinfo(hostname, NULL, NULL, &addrs))
490 error(1, errno, "getaddrinfo");
493 while (cur && !have_ipv4 && !have_ipv6) {
494 if (!have_ipv4 && cur->ai_family == AF_INET) {
495 memcpy(&daddr, cur->ai_addr, sizeof(daddr));
496 daddr.sin_port = htons(dest_port);
499 else if (!have_ipv6 && cur->ai_family == AF_INET6) {
500 memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
501 daddr6.sin6_port = htons(dest_port);
509 do_ipv4 &= have_ipv4;
510 do_ipv6 &= have_ipv6;
513 static void do_main(int family)
515 fprintf(stderr, "family: %s\n",
516 family == PF_INET ? "INET" : "INET6");
518 fprintf(stderr, "test SND\n");
519 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);
521 fprintf(stderr, "test ENQ\n");
522 do_test(family, SOF_TIMESTAMPING_TX_SCHED);
524 fprintf(stderr, "test ENQ + SND\n");
525 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
526 SOF_TIMESTAMPING_TX_SOFTWARE);
528 if (cfg_proto == SOCK_STREAM) {
529 fprintf(stderr, "\ntest ACK\n");
530 do_test(family, SOF_TIMESTAMPING_TX_ACK);
532 fprintf(stderr, "\ntest SND + ACK\n");
533 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
534 SOF_TIMESTAMPING_TX_ACK);
536 fprintf(stderr, "\ntest ENQ + SND + ACK\n");
537 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
538 SOF_TIMESTAMPING_TX_SOFTWARE |
539 SOF_TIMESTAMPING_TX_ACK);
543 const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };
545 int main(int argc, char **argv)
550 parse_opt(argc, argv);
551 resolve_hostname(argv[argc - 1]);
553 fprintf(stderr, "protocol: %s\n", sock_names[cfg_proto]);
554 fprintf(stderr, "payload: %u\n", cfg_payload_len);
555 fprintf(stderr, "server port: %u\n", dest_port);
556 fprintf(stderr, "\n");