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/ipv6.h>
43 #include <linux/net_tstamp.h>
46 #include <netinet/in.h>
47 #include <netinet/ip.h>
48 #include <netinet/udp.h>
49 #include <netinet/tcp.h>
50 #include <netpacket/packet.h>
57 #include <sys/ioctl.h>
58 #include <sys/select.h>
59 #include <sys/socket.h>
61 #include <sys/types.h>
65 /* command line parameters */
66 static int cfg_proto = SOCK_STREAM;
67 static int cfg_ipproto = IPPROTO_TCP;
68 static int cfg_num_pkts = 4;
69 static int do_ipv4 = 1;
70 static int do_ipv6 = 1;
71 static int cfg_payload_len = 10;
72 static int cfg_poll_timeout = 100;
73 static int cfg_delay_snd;
74 static int cfg_delay_ack;
75 static bool cfg_show_payload;
76 static bool cfg_do_pktinfo;
77 static bool cfg_loop_nodata;
78 static bool cfg_no_delay;
79 static bool cfg_use_cmsg;
80 static bool cfg_use_pf_packet;
81 static bool cfg_do_listen;
82 static uint16_t dest_port = 9000;
84 static struct sockaddr_in daddr;
85 static struct sockaddr_in6 daddr6;
86 static struct timespec ts_usr;
88 static int saved_tskey = -1;
89 static int saved_tskey_type = -1;
91 static bool test_failed;
93 static int64_t timespec_to_us64(struct timespec *ts)
95 return ts->tv_sec * 1000 * 1000 + ts->tv_nsec / 1000;
98 static void validate_key(int tskey, int tstype)
102 /* compare key for each subsequent request
103 * must only test for one type, the first one requested
105 if (saved_tskey == -1)
106 saved_tskey_type = tstype;
107 else if (saved_tskey_type != tstype)
110 stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;
111 if (tskey != saved_tskey + stepsize) {
112 fprintf(stderr, "ERROR: key %d, expected %d\n",
113 tskey, saved_tskey + stepsize);
120 static void validate_timestamp(struct timespec *cur, int min_delay)
122 int max_delay = min_delay + 500 /* processing time upper bound */;
123 int64_t cur64, start64;
125 cur64 = timespec_to_us64(cur);
126 start64 = timespec_to_us64(&ts_usr);
128 if (cur64 < start64 + min_delay || cur64 > start64 + max_delay) {
129 fprintf(stderr, "ERROR: delay %lu expected between %d and %d\n",
130 cur64 - start64, min_delay, max_delay);
135 static void __print_timestamp(const char *name, struct timespec *cur,
136 uint32_t key, int payload_len)
138 if (!(cur->tv_sec | cur->tv_nsec))
141 fprintf(stderr, " %s: %lu s %lu us (seq=%u, len=%u)",
142 name, cur->tv_sec, cur->tv_nsec / 1000,
146 fprintf(stderr, " (USR %+" PRId64 " us)",
147 timespec_to_us64(cur) - timespec_to_us64(&ts_usr));
149 fprintf(stderr, "\n");
152 static void print_timestamp_usr(void)
154 if (clock_gettime(CLOCK_REALTIME, &ts_usr))
155 error(1, errno, "clock_gettime");
157 __print_timestamp(" USR", &ts_usr, 0, 0);
160 static void print_timestamp(struct scm_timestamping *tss, int tstype,
161 int tskey, int payload_len)
165 validate_key(tskey, tstype);
168 case SCM_TSTAMP_SCHED:
170 validate_timestamp(&tss->ts[0], 0);
174 validate_timestamp(&tss->ts[0], cfg_delay_snd);
178 validate_timestamp(&tss->ts[0], cfg_delay_ack);
181 error(1, 0, "unknown timestamp type: %u",
184 __print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
187 /* TODO: convert to check_and_print payload once API is stable */
188 static void print_payload(char *data, int len)
198 fprintf(stderr, "payload: ");
199 for (i = 0; i < len; i++)
200 fprintf(stderr, "%02hhx ", data[i]);
201 fprintf(stderr, "\n");
204 static void print_pktinfo(int family, int ifindex, void *saddr, void *daddr)
206 char sa[INET6_ADDRSTRLEN], da[INET6_ADDRSTRLEN];
208 fprintf(stderr, " pktinfo: ifindex=%u src=%s dst=%s\n",
210 saddr ? inet_ntop(family, saddr, sa, sizeof(sa)) : "unknown",
211 daddr ? inet_ntop(family, daddr, da, sizeof(da)) : "unknown");
214 static void __poll(int fd)
216 struct pollfd pollfd;
219 memset(&pollfd, 0, sizeof(pollfd));
221 ret = poll(&pollfd, 1, cfg_poll_timeout);
223 error(1, errno, "poll");
226 static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
228 struct sock_extended_err *serr = NULL;
229 struct scm_timestamping *tss = NULL;
233 for (cm = CMSG_FIRSTHDR(msg);
235 cm = CMSG_NXTHDR(msg, cm)) {
236 if (cm->cmsg_level == SOL_SOCKET &&
237 cm->cmsg_type == SCM_TIMESTAMPING) {
238 tss = (void *) CMSG_DATA(cm);
239 } else if ((cm->cmsg_level == SOL_IP &&
240 cm->cmsg_type == IP_RECVERR) ||
241 (cm->cmsg_level == SOL_IPV6 &&
242 cm->cmsg_type == IPV6_RECVERR) ||
243 (cm->cmsg_level == SOL_PACKET &&
244 cm->cmsg_type == PACKET_TX_TIMESTAMP)) {
245 serr = (void *) CMSG_DATA(cm);
246 if (serr->ee_errno != ENOMSG ||
247 serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
248 fprintf(stderr, "unknown ip error %d %d\n",
253 } else if (cm->cmsg_level == SOL_IP &&
254 cm->cmsg_type == IP_PKTINFO) {
255 struct in_pktinfo *info = (void *) CMSG_DATA(cm);
256 print_pktinfo(AF_INET, info->ipi_ifindex,
257 &info->ipi_spec_dst, &info->ipi_addr);
258 } else if (cm->cmsg_level == SOL_IPV6 &&
259 cm->cmsg_type == IPV6_PKTINFO) {
260 struct in6_pktinfo *info6 = (void *) CMSG_DATA(cm);
261 print_pktinfo(AF_INET6, info6->ipi6_ifindex,
262 NULL, &info6->ipi6_addr);
264 fprintf(stderr, "unknown cmsg %d,%d\n",
265 cm->cmsg_level, cm->cmsg_type);
268 print_timestamp(tss, serr->ee_info, serr->ee_data,
277 fprintf(stderr, "batched %d timestamps\n", batch);
280 static int recv_errmsg(int fd)
282 static char ctrl[1024 /* overprovision*/];
283 static struct msghdr msg;
288 data = malloc(cfg_payload_len);
290 error(1, 0, "malloc");
292 memset(&msg, 0, sizeof(msg));
293 memset(&entry, 0, sizeof(entry));
294 memset(ctrl, 0, sizeof(ctrl));
296 entry.iov_base = data;
297 entry.iov_len = cfg_payload_len;
298 msg.msg_iov = &entry;
302 msg.msg_control = ctrl;
303 msg.msg_controllen = sizeof(ctrl);
305 ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
306 if (ret == -1 && errno != EAGAIN)
307 error(1, errno, "recvmsg");
310 __recv_errmsg_cmsg(&msg, ret);
311 if (cfg_show_payload)
312 print_payload(data, cfg_payload_len);
319 static uint16_t get_ip_csum(const uint16_t *start, int num_words,
324 for (i = 0; i < num_words; i++)
328 sum = (sum & 0xFFFF) + (sum >> 16);
333 static uint16_t get_udp_csum(const struct udphdr *udph, int alen)
335 unsigned long pseudo_sum, csum_len;
336 const void *csum_start = udph;
338 pseudo_sum = htons(IPPROTO_UDP);
339 pseudo_sum += udph->len;
341 /* checksum ip(v6) addresses + udp header + payload */
342 csum_start -= alen * 2;
343 csum_len = ntohs(udph->len) + alen * 2;
345 return get_ip_csum(csum_start, csum_len >> 1, pseudo_sum);
348 static int fill_header_ipv4(void *p)
350 struct iphdr *iph = p;
352 memset(iph, 0, sizeof(*iph));
357 iph->saddr = daddr.sin_addr.s_addr; /* set for udp csum calc */
358 iph->daddr = daddr.sin_addr.s_addr;
359 iph->protocol = IPPROTO_UDP;
361 /* kernel writes saddr, csum, len */
366 static int fill_header_ipv6(void *p)
368 struct ipv6hdr *ip6h = p;
370 memset(ip6h, 0, sizeof(*ip6h));
373 ip6h->payload_len = htons(sizeof(struct udphdr) + cfg_payload_len);
374 ip6h->nexthdr = IPPROTO_UDP;
375 ip6h->hop_limit = 64;
377 ip6h->saddr = daddr6.sin6_addr;
378 ip6h->daddr = daddr6.sin6_addr;
380 /* kernel does not write saddr in case of ipv6 */
382 return sizeof(*ip6h);
385 static void fill_header_udp(void *p, bool is_ipv4)
387 struct udphdr *udph = p;
389 udph->source = ntohs(dest_port + 1); /* spoof */
390 udph->dest = ntohs(dest_port);
391 udph->len = ntohs(sizeof(*udph) + cfg_payload_len);
394 udph->check = get_udp_csum(udph, is_ipv4 ? sizeof(struct in_addr) :
395 sizeof(struct in6_addr));
398 static void do_test(int family, unsigned int report_opt)
400 char control[CMSG_SPACE(sizeof(uint32_t))];
401 struct sockaddr_ll laddr;
402 unsigned int sock_opt;
403 struct cmsghdr *cmsg;
407 int fd, i, val = 1, total_len;
409 total_len = cfg_payload_len;
410 if (cfg_use_pf_packet || cfg_proto == SOCK_RAW) {
411 total_len += sizeof(struct udphdr);
412 if (cfg_use_pf_packet || cfg_ipproto == IPPROTO_RAW)
413 if (family == PF_INET)
414 total_len += sizeof(struct iphdr);
416 total_len += sizeof(struct ipv6hdr);
418 /* special case, only rawv6_sendmsg:
419 * pass proto in sin6_port if not connected
420 * also see ANK comment in net/ipv4/raw.c
422 daddr6.sin6_port = htons(cfg_ipproto);
425 buf = malloc(total_len);
427 error(1, 0, "malloc");
429 fd = socket(cfg_use_pf_packet ? PF_PACKET : family,
430 cfg_proto, cfg_ipproto);
432 error(1, errno, "socket");
434 /* reset expected key on each new socket */
437 if (cfg_proto == SOCK_STREAM) {
438 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
439 (char*) &val, sizeof(val)))
440 error(1, 0, "setsockopt no nagle");
442 if (family == PF_INET) {
443 if (connect(fd, (void *) &daddr, sizeof(daddr)))
444 error(1, errno, "connect ipv4");
446 if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
447 error(1, errno, "connect ipv6");
451 if (cfg_do_pktinfo) {
452 if (family == AF_INET6) {
453 if (setsockopt(fd, SOL_IPV6, IPV6_RECVPKTINFO,
455 error(1, errno, "setsockopt pktinfo ipv6");
457 if (setsockopt(fd, SOL_IP, IP_PKTINFO,
459 error(1, errno, "setsockopt pktinfo ipv4");
463 sock_opt = SOF_TIMESTAMPING_SOFTWARE |
464 SOF_TIMESTAMPING_OPT_CMSG |
465 SOF_TIMESTAMPING_OPT_ID;
468 sock_opt |= report_opt;
471 sock_opt |= SOF_TIMESTAMPING_OPT_TSONLY;
473 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
474 (char *) &sock_opt, sizeof(sock_opt)))
475 error(1, 0, "setsockopt timestamping");
477 for (i = 0; i < cfg_num_pkts; i++) {
478 memset(&msg, 0, sizeof(msg));
479 memset(buf, 'a' + i, total_len);
481 if (cfg_use_pf_packet || cfg_proto == SOCK_RAW) {
484 if (cfg_use_pf_packet || cfg_ipproto == IPPROTO_RAW) {
485 if (family == PF_INET)
486 off = fill_header_ipv4(buf);
488 off = fill_header_ipv6(buf);
491 fill_header_udp(buf + off, family == PF_INET);
494 print_timestamp_usr();
497 iov.iov_len = total_len;
499 if (cfg_proto != SOCK_STREAM) {
500 if (cfg_use_pf_packet) {
501 memset(&laddr, 0, sizeof(laddr));
503 laddr.sll_family = AF_PACKET;
504 laddr.sll_ifindex = 1;
505 laddr.sll_protocol = htons(family == AF_INET ? ETH_P_IP : ETH_P_IPV6);
506 laddr.sll_halen = ETH_ALEN;
508 msg.msg_name = (void *)&laddr;
509 msg.msg_namelen = sizeof(laddr);
510 } else if (family == PF_INET) {
511 msg.msg_name = (void *)&daddr;
512 msg.msg_namelen = sizeof(daddr);
514 msg.msg_name = (void *)&daddr6;
515 msg.msg_namelen = sizeof(daddr6);
523 memset(control, 0, sizeof(control));
525 msg.msg_control = control;
526 msg.msg_controllen = sizeof(control);
528 cmsg = CMSG_FIRSTHDR(&msg);
529 cmsg->cmsg_level = SOL_SOCKET;
530 cmsg->cmsg_type = SO_TIMESTAMPING;
531 cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));
533 *((uint32_t *) CMSG_DATA(cmsg)) = report_opt;
536 val = sendmsg(fd, &msg, 0);
537 if (val != total_len)
538 error(1, errno, "send");
540 /* wait for all errors to be queued, else ACKs arrive OOO */
546 while (!recv_errmsg(fd)) {}
550 error(1, errno, "close");
556 static void __attribute__((noreturn)) usage(const char *filepath)
558 fprintf(stderr, "\nUsage: %s [options] hostname\n"
559 "\nwhere options are:\n"
562 " -h: show this message\n"
563 " -c N: number of packets for each test\n"
564 " -C: use cmsg to set tstamp recording options\n"
565 " -D: no delay between packets\n"
566 " -F: poll() waits forever for an event\n"
567 " -I: request PKTINFO\n"
568 " -l N: send N bytes at a time\n"
569 " -L listen on hostname and port\n"
570 " -n: set no-payload option\n"
571 " -p N: connect to port N\n"
572 " -P: use PF_PACKET\n"
574 " -R: use raw (IP_HDRINCL)\n"
576 " -v: validate SND delay (usec)\n"
577 " -V: validate ACK delay (usec)\n"
578 " -x: show payload (up to 70 bytes)\n",
583 static void parse_opt(int argc, char **argv)
588 while ((c = getopt(argc, argv, "46c:CDFhIl:Lnp:PrRuv:V:x")) != -1) {
597 cfg_num_pkts = strtoul(optarg, NULL, 10);
606 cfg_poll_timeout = -1;
609 cfg_do_pktinfo = true;
612 cfg_payload_len = strtoul(optarg, NULL, 10);
615 cfg_do_listen = true;
618 cfg_loop_nodata = true;
621 dest_port = strtoul(optarg, NULL, 10);
625 cfg_use_pf_packet = true;
626 cfg_proto = SOCK_DGRAM;
631 cfg_proto = SOCK_RAW;
632 cfg_ipproto = IPPROTO_UDP;
636 cfg_proto = SOCK_RAW;
637 cfg_ipproto = IPPROTO_RAW;
641 cfg_proto = SOCK_DGRAM;
642 cfg_ipproto = IPPROTO_UDP;
645 cfg_delay_snd = strtoul(optarg, NULL, 10);
648 cfg_delay_ack = strtoul(optarg, NULL, 10);
651 cfg_show_payload = true;
659 if (!cfg_payload_len)
660 error(1, 0, "payload may not be nonzero");
661 if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
662 error(1, 0, "udp packet might exceed expected MTU");
663 if (!do_ipv4 && !do_ipv6)
664 error(1, 0, "pass -4 or -6, not both");
666 error(1, 0, "pass -P, -r, -R or -u, not multiple");
667 if (cfg_do_pktinfo && cfg_use_pf_packet)
668 error(1, 0, "cannot ask for pktinfo over pf_packet");
670 if (optind != argc - 1)
671 error(1, 0, "missing required hostname argument");
674 static void resolve_hostname(const char *hostname)
676 struct addrinfo hints = { .ai_family = do_ipv4 ? AF_INET : AF_INET6 };
677 struct addrinfo *addrs, *cur;
678 int have_ipv4 = 0, have_ipv6 = 0;
681 if (getaddrinfo(hostname, NULL, &hints, &addrs))
682 error(1, errno, "getaddrinfo");
685 while (cur && !have_ipv4 && !have_ipv6) {
686 if (!have_ipv4 && cur->ai_family == AF_INET) {
687 memcpy(&daddr, cur->ai_addr, sizeof(daddr));
688 daddr.sin_port = htons(dest_port);
691 else if (!have_ipv6 && cur->ai_family == AF_INET6) {
692 memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
693 daddr6.sin6_port = htons(dest_port);
701 if (do_ipv6 && hints.ai_family != AF_INET6) {
702 hints.ai_family = AF_INET6;
706 do_ipv4 &= have_ipv4;
707 do_ipv6 &= have_ipv6;
710 static void do_listen(int family, void *addr, int alen)
714 type = cfg_proto == SOCK_RAW ? SOCK_DGRAM : cfg_proto;
716 fd = socket(family, type, 0);
718 error(1, errno, "socket rx");
720 if (bind(fd, addr, alen))
721 error(1, errno, "bind rx");
723 if (type == SOCK_STREAM && listen(fd, 10))
724 error(1, errno, "listen rx");
726 /* leave fd open, will be closed on process exit.
727 * this enables connect() to succeed and avoids icmp replies
731 static void do_main(int family)
733 fprintf(stderr, "family: %s %s\n",
734 family == PF_INET ? "INET" : "INET6",
735 cfg_use_pf_packet ? "(PF_PACKET)" : "");
737 fprintf(stderr, "test SND\n");
738 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);
740 fprintf(stderr, "test ENQ\n");
741 do_test(family, SOF_TIMESTAMPING_TX_SCHED);
743 fprintf(stderr, "test ENQ + SND\n");
744 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
745 SOF_TIMESTAMPING_TX_SOFTWARE);
747 if (cfg_proto == SOCK_STREAM) {
748 fprintf(stderr, "\ntest ACK\n");
749 do_test(family, SOF_TIMESTAMPING_TX_ACK);
751 fprintf(stderr, "\ntest SND + ACK\n");
752 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
753 SOF_TIMESTAMPING_TX_ACK);
755 fprintf(stderr, "\ntest ENQ + SND + ACK\n");
756 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
757 SOF_TIMESTAMPING_TX_SOFTWARE |
758 SOF_TIMESTAMPING_TX_ACK);
762 const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };
764 int main(int argc, char **argv)
769 parse_opt(argc, argv);
770 resolve_hostname(argv[argc - 1]);
772 fprintf(stderr, "protocol: %s\n", sock_names[cfg_proto]);
773 fprintf(stderr, "payload: %u\n", cfg_payload_len);
774 fprintf(stderr, "server port: %u\n", dest_port);
775 fprintf(stderr, "\n");
779 do_listen(PF_INET, &daddr, sizeof(daddr));
785 do_listen(PF_INET6, &daddr6, sizeof(daddr6));