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>
39 #include <linux/errqueue.h>
40 #include <linux/if_ether.h>
41 #include <linux/net_tstamp.h>
44 #include <netinet/in.h>
45 #include <netinet/ip.h>
46 #include <netinet/udp.h>
47 #include <netinet/tcp.h>
48 #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 bool cfg_show_payload;
72 static bool cfg_do_pktinfo;
73 static uint16_t dest_port = 9000;
75 static struct sockaddr_in daddr;
76 static struct sockaddr_in6 daddr6;
77 static struct timespec ts_prev;
79 static void __print_timestamp(const char *name, struct timespec *cur,
80 uint32_t key, int payload_len)
82 if (!(cur->tv_sec | cur->tv_nsec))
85 fprintf(stderr, " %s: %lu s %lu us (seq=%u, len=%u)",
86 name, cur->tv_sec, cur->tv_nsec / 1000,
89 if ((ts_prev.tv_sec | ts_prev.tv_nsec)) {
90 int64_t cur_ms, prev_ms;
92 cur_ms = (long) cur->tv_sec * 1000 * 1000;
93 cur_ms += cur->tv_nsec / 1000;
95 prev_ms = (long) ts_prev.tv_sec * 1000 * 1000;
96 prev_ms += ts_prev.tv_nsec / 1000;
98 fprintf(stderr, " (%+ld us)", cur_ms - prev_ms);
102 fprintf(stderr, "\n");
105 static void print_timestamp_usr(void)
108 struct timeval tv; /* avoid dependency on -lrt */
110 gettimeofday(&tv, NULL);
111 ts.tv_sec = tv.tv_sec;
112 ts.tv_nsec = tv.tv_usec * 1000;
114 __print_timestamp(" USR", &ts, 0, 0);
117 static void print_timestamp(struct scm_timestamping *tss, int tstype,
118 int tskey, int payload_len)
123 case SCM_TSTAMP_SCHED:
133 error(1, 0, "unknown timestamp type: %u",
136 __print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
139 /* TODO: convert to check_and_print payload once API is stable */
140 static void print_payload(char *data, int len)
147 fprintf(stderr, "payload: ");
148 for (i = 0; i < len; i++)
149 fprintf(stderr, "%02hhx ", data[i]);
150 fprintf(stderr, "\n");
153 static void print_pktinfo(int family, int ifindex, void *saddr, void *daddr)
155 char sa[INET6_ADDRSTRLEN], da[INET6_ADDRSTRLEN];
157 fprintf(stderr, " pktinfo: ifindex=%u src=%s dst=%s\n",
159 saddr ? inet_ntop(family, saddr, sa, sizeof(sa)) : "unknown",
160 daddr ? inet_ntop(family, daddr, da, sizeof(da)) : "unknown");
163 static void __poll(int fd)
165 struct pollfd pollfd;
168 memset(&pollfd, 0, sizeof(pollfd));
170 ret = poll(&pollfd, 1, 100);
172 error(1, errno, "poll");
175 static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
177 struct sock_extended_err *serr = NULL;
178 struct scm_timestamping *tss = NULL;
181 for (cm = CMSG_FIRSTHDR(msg);
183 cm = CMSG_NXTHDR(msg, cm)) {
184 if (cm->cmsg_level == SOL_SOCKET &&
185 cm->cmsg_type == SCM_TIMESTAMPING) {
186 tss = (void *) CMSG_DATA(cm);
187 } else if ((cm->cmsg_level == SOL_IP &&
188 cm->cmsg_type == IP_RECVERR) ||
189 (cm->cmsg_level == SOL_IPV6 &&
190 cm->cmsg_type == IPV6_RECVERR)) {
191 serr = (void *) CMSG_DATA(cm);
192 if (serr->ee_errno != ENOMSG ||
193 serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
194 fprintf(stderr, "unknown ip error %d %d\n",
199 } else if (cm->cmsg_level == SOL_IP &&
200 cm->cmsg_type == IP_PKTINFO) {
201 struct in_pktinfo *info = (void *) CMSG_DATA(cm);
202 print_pktinfo(AF_INET, info->ipi_ifindex,
203 &info->ipi_spec_dst, &info->ipi_addr);
204 } else if (cm->cmsg_level == SOL_IPV6 &&
205 cm->cmsg_type == IPV6_PKTINFO) {
206 struct in6_pktinfo *info6 = (void *) CMSG_DATA(cm);
207 print_pktinfo(AF_INET6, info6->ipi6_ifindex,
208 NULL, &info6->ipi6_addr);
210 fprintf(stderr, "unknown cmsg %d,%d\n",
211 cm->cmsg_level, cm->cmsg_type);
215 print_timestamp(tss, serr->ee_info, serr->ee_data, payload_len);
218 static int recv_errmsg(int fd)
220 static char ctrl[1024 /* overprovision*/];
221 static struct msghdr msg;
226 data = malloc(cfg_payload_len);
228 error(1, 0, "malloc");
230 memset(&msg, 0, sizeof(msg));
231 memset(&entry, 0, sizeof(entry));
232 memset(ctrl, 0, sizeof(ctrl));
234 entry.iov_base = data;
235 entry.iov_len = cfg_payload_len;
236 msg.msg_iov = &entry;
240 msg.msg_control = ctrl;
241 msg.msg_controllen = sizeof(ctrl);
243 ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
244 if (ret == -1 && errno != EAGAIN)
245 error(1, errno, "recvmsg");
248 __recv_errmsg_cmsg(&msg, ret);
249 if (cfg_show_payload)
250 print_payload(data, cfg_payload_len);
257 static void do_test(int family, unsigned int opt)
260 int fd, i, val = 1, total_len;
262 if (family == AF_INET6 && cfg_proto != SOCK_STREAM) {
263 /* due to lack of checksum generation code */
264 fprintf(stderr, "test: skipping datagram over IPv6\n");
268 total_len = cfg_payload_len;
269 if (cfg_proto == SOCK_RAW) {
270 total_len += sizeof(struct udphdr);
271 if (cfg_ipproto == IPPROTO_RAW)
272 total_len += sizeof(struct iphdr);
275 buf = malloc(total_len);
277 error(1, 0, "malloc");
279 fd = socket(family, cfg_proto, cfg_ipproto);
281 error(1, errno, "socket");
283 if (cfg_proto == SOCK_STREAM) {
284 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
285 (char*) &val, sizeof(val)))
286 error(1, 0, "setsockopt no nagle");
288 if (family == PF_INET) {
289 if (connect(fd, (void *) &daddr, sizeof(daddr)))
290 error(1, errno, "connect ipv4");
292 if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
293 error(1, errno, "connect ipv6");
297 if (cfg_do_pktinfo) {
298 if (family == AF_INET6) {
299 if (setsockopt(fd, SOL_IPV6, IPV6_RECVPKTINFO,
301 error(1, errno, "setsockopt pktinfo ipv6");
303 if (setsockopt(fd, SOL_IP, IP_PKTINFO,
305 error(1, errno, "setsockopt pktinfo ipv4");
309 opt |= SOF_TIMESTAMPING_SOFTWARE |
310 SOF_TIMESTAMPING_OPT_CMSG |
311 SOF_TIMESTAMPING_OPT_ID;
312 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
313 (char *) &opt, sizeof(opt)))
314 error(1, 0, "setsockopt timestamping");
316 for (i = 0; i < cfg_num_pkts; i++) {
317 memset(&ts_prev, 0, sizeof(ts_prev));
318 memset(buf, 'a' + i, total_len);
320 if (cfg_proto == SOCK_RAW) {
324 if (cfg_ipproto == IPPROTO_RAW) {
325 struct iphdr *iph = (void *) buf;
327 memset(iph, 0, sizeof(*iph));
331 iph->daddr = daddr.sin_addr.s_addr;
332 iph->protocol = IPPROTO_UDP;
333 /* kernel writes saddr, csum, len */
338 udph = (void *) buf + off;
339 udph->source = ntohs(9000); /* random spoof */
340 udph->dest = ntohs(dest_port);
341 udph->len = ntohs(sizeof(*udph) + cfg_payload_len);
342 udph->check = 0; /* not allowed for IPv6 */
345 print_timestamp_usr();
346 if (cfg_proto != SOCK_STREAM) {
347 if (family == PF_INET)
348 val = sendto(fd, buf, total_len, 0, (void *) &daddr, sizeof(daddr));
350 val = sendto(fd, buf, total_len, 0, (void *) &daddr6, sizeof(daddr6));
352 val = send(fd, buf, cfg_payload_len, 0);
354 if (val != total_len)
355 error(1, errno, "send");
357 /* wait for all errors to be queued, else ACKs arrive OOO */
362 while (!recv_errmsg(fd)) {}
366 error(1, errno, "close");
372 static void __attribute__((noreturn)) usage(const char *filepath)
374 fprintf(stderr, "\nUsage: %s [options] hostname\n"
375 "\nwhere options are:\n"
378 " -h: show this message\n"
379 " -I: request PKTINFO\n"
380 " -l N: send N bytes at a time\n"
382 " -R: use raw (IP_HDRINCL)\n"
383 " -p N: connect to port N\n"
385 " -x: show payload (up to 70 bytes)\n",
390 static void parse_opt(int argc, char **argv)
395 while ((c = getopt(argc, argv, "46hIl:p:rRux")) != -1) {
404 cfg_do_pktinfo = true;
408 cfg_proto = SOCK_RAW;
409 cfg_ipproto = IPPROTO_UDP;
413 cfg_proto = SOCK_RAW;
414 cfg_ipproto = IPPROTO_RAW;
418 cfg_proto = SOCK_DGRAM;
419 cfg_ipproto = IPPROTO_UDP;
422 cfg_payload_len = strtoul(optarg, NULL, 10);
425 dest_port = strtoul(optarg, NULL, 10);
428 cfg_show_payload = true;
436 if (!cfg_payload_len)
437 error(1, 0, "payload may not be nonzero");
438 if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
439 error(1, 0, "udp packet might exceed expected MTU");
440 if (!do_ipv4 && !do_ipv6)
441 error(1, 0, "pass -4 or -6, not both");
443 error(1, 0, "pass -r, -R or -u, not multiple");
445 if (optind != argc - 1)
446 error(1, 0, "missing required hostname argument");
449 static void resolve_hostname(const char *hostname)
451 struct addrinfo *addrs, *cur;
452 int have_ipv4 = 0, have_ipv6 = 0;
454 if (getaddrinfo(hostname, NULL, NULL, &addrs))
455 error(1, errno, "getaddrinfo");
458 while (cur && !have_ipv4 && !have_ipv6) {
459 if (!have_ipv4 && cur->ai_family == AF_INET) {
460 memcpy(&daddr, cur->ai_addr, sizeof(daddr));
461 daddr.sin_port = htons(dest_port);
464 else if (!have_ipv6 && cur->ai_family == AF_INET6) {
465 memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
466 daddr6.sin6_port = htons(dest_port);
474 do_ipv4 &= have_ipv4;
475 do_ipv6 &= have_ipv6;
478 static void do_main(int family)
480 fprintf(stderr, "family: %s\n",
481 family == PF_INET ? "INET" : "INET6");
483 fprintf(stderr, "test SND\n");
484 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);
486 fprintf(stderr, "test ENQ\n");
487 do_test(family, SOF_TIMESTAMPING_TX_SCHED);
489 fprintf(stderr, "test ENQ + SND\n");
490 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
491 SOF_TIMESTAMPING_TX_SOFTWARE);
493 if (cfg_proto == SOCK_STREAM) {
494 fprintf(stderr, "\ntest ACK\n");
495 do_test(family, SOF_TIMESTAMPING_TX_ACK);
497 fprintf(stderr, "\ntest SND + ACK\n");
498 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
499 SOF_TIMESTAMPING_TX_ACK);
501 fprintf(stderr, "\ntest ENQ + SND + ACK\n");
502 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
503 SOF_TIMESTAMPING_TX_SOFTWARE |
504 SOF_TIMESTAMPING_TX_ACK);
508 const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };
510 int main(int argc, char **argv)
515 parse_opt(argc, argv);
516 resolve_hostname(argv[argc - 1]);
518 fprintf(stderr, "protocol: %s\n", sock_names[cfg_proto]);
519 fprintf(stderr, "payload: %u\n", cfg_payload_len);
520 fprintf(stderr, "server port: %u\n", dest_port);
521 fprintf(stderr, "\n");