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.
33 #include <arpa/inet.h>
34 #include <asm/types.h>
37 #include <linux/errqueue.h>
38 #include <linux/if_ether.h>
39 #include <linux/net_tstamp.h>
42 #include <netinet/in.h>
43 #include <netinet/ip.h>
44 #include <netinet/udp.h>
45 #include <netinet/tcp.h>
46 #include <netpacket/packet.h>
53 #include <sys/ioctl.h>
54 #include <sys/select.h>
55 #include <sys/socket.h>
57 #include <sys/types.h>
61 /* command line parameters */
62 static int cfg_proto = SOCK_STREAM;
63 static int cfg_ipproto = IPPROTO_TCP;
64 static int cfg_num_pkts = 4;
65 static int do_ipv4 = 1;
66 static int do_ipv6 = 1;
67 static int cfg_payload_len = 10;
68 static uint16_t dest_port = 9000;
70 static struct sockaddr_in daddr;
71 static struct sockaddr_in6 daddr6;
72 static struct timespec ts_prev;
74 static void __print_timestamp(const char *name, struct timespec *cur,
75 uint32_t key, int payload_len)
77 if (!(cur->tv_sec | cur->tv_nsec))
80 fprintf(stderr, " %s: %lu s %lu us (seq=%u, len=%u)",
81 name, cur->tv_sec, cur->tv_nsec / 1000,
84 if ((ts_prev.tv_sec | ts_prev.tv_nsec)) {
85 int64_t cur_ms, prev_ms;
87 cur_ms = (long) cur->tv_sec * 1000 * 1000;
88 cur_ms += cur->tv_nsec / 1000;
90 prev_ms = (long) ts_prev.tv_sec * 1000 * 1000;
91 prev_ms += ts_prev.tv_nsec / 1000;
93 fprintf(stderr, " (%+ld us)", cur_ms - prev_ms);
97 fprintf(stderr, "\n");
100 static void print_timestamp_usr(void)
103 struct timeval tv; /* avoid dependency on -lrt */
105 gettimeofday(&tv, NULL);
106 ts.tv_sec = tv.tv_sec;
107 ts.tv_nsec = tv.tv_usec * 1000;
109 __print_timestamp(" USR", &ts, 0, 0);
112 static void print_timestamp(struct scm_timestamping *tss, int tstype,
113 int tskey, int payload_len)
118 case SCM_TSTAMP_SCHED:
128 error(1, 0, "unknown timestamp type: %u",
131 __print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
134 static void __poll(int fd)
136 struct pollfd pollfd;
139 memset(&pollfd, 0, sizeof(pollfd));
141 ret = poll(&pollfd, 1, 100);
143 error(1, errno, "poll");
146 static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
148 struct sock_extended_err *serr = NULL;
149 struct scm_timestamping *tss = NULL;
152 for (cm = CMSG_FIRSTHDR(msg);
154 cm = CMSG_NXTHDR(msg, cm)) {
155 if (cm->cmsg_level == SOL_SOCKET &&
156 cm->cmsg_type == SCM_TIMESTAMPING) {
157 tss = (void *) CMSG_DATA(cm);
158 } else if ((cm->cmsg_level == SOL_IP &&
159 cm->cmsg_type == IP_RECVERR) ||
160 (cm->cmsg_level == SOL_IPV6 &&
161 cm->cmsg_type == IPV6_RECVERR)) {
163 serr = (void *) CMSG_DATA(cm);
164 if (serr->ee_errno != ENOMSG ||
165 serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
166 fprintf(stderr, "unknown ip error %d %d\n",
172 fprintf(stderr, "unknown cmsg %d,%d\n",
173 cm->cmsg_level, cm->cmsg_type);
177 print_timestamp(tss, serr->ee_info, serr->ee_data, payload_len);
180 static int recv_errmsg(int fd)
182 static char ctrl[1024 /* overprovision*/];
183 static struct msghdr msg;
188 data = malloc(cfg_payload_len);
190 error(1, 0, "malloc");
192 memset(&msg, 0, sizeof(msg));
193 memset(&entry, 0, sizeof(entry));
194 memset(ctrl, 0, sizeof(ctrl));
196 entry.iov_base = data;
197 entry.iov_len = cfg_payload_len;
198 msg.msg_iov = &entry;
202 msg.msg_control = ctrl;
203 msg.msg_controllen = sizeof(ctrl);
205 ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
206 if (ret == -1 && errno != EAGAIN)
207 error(1, errno, "recvmsg");
209 __recv_errmsg_cmsg(&msg, ret);
215 static void do_test(int family, unsigned int opt)
218 int fd, i, val, total_len;
220 if (family == IPPROTO_IPV6 && cfg_proto != SOCK_STREAM) {
221 /* due to lack of checksum generation code */
222 fprintf(stderr, "test: skipping datagram over IPv6\n");
226 total_len = cfg_payload_len;
227 if (cfg_proto == SOCK_RAW) {
228 total_len += sizeof(struct udphdr);
229 if (cfg_ipproto == IPPROTO_RAW)
230 total_len += sizeof(struct iphdr);
233 buf = malloc(total_len);
235 error(1, 0, "malloc");
237 fd = socket(family, cfg_proto, cfg_ipproto);
239 error(1, errno, "socket");
241 if (cfg_proto == SOCK_STREAM) {
243 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
244 (char*) &val, sizeof(val)))
245 error(1, 0, "setsockopt no nagle");
247 if (family == PF_INET) {
248 if (connect(fd, (void *) &daddr, sizeof(daddr)))
249 error(1, errno, "connect ipv4");
251 if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
252 error(1, errno, "connect ipv6");
256 opt |= SOF_TIMESTAMPING_SOFTWARE |
257 SOF_TIMESTAMPING_OPT_ID;
258 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
259 (char *) &opt, sizeof(opt)))
260 error(1, 0, "setsockopt timestamping");
262 for (i = 0; i < cfg_num_pkts; i++) {
263 memset(&ts_prev, 0, sizeof(ts_prev));
264 memset(buf, 'a' + i, total_len);
265 buf[total_len - 2] = '\n';
266 buf[total_len - 1] = '\0';
268 if (cfg_proto == SOCK_RAW) {
272 if (cfg_ipproto == IPPROTO_RAW) {
273 struct iphdr *iph = (void *) buf;
275 memset(iph, 0, sizeof(*iph));
279 iph->daddr = daddr.sin_addr.s_addr;
280 iph->protocol = IPPROTO_UDP;
281 /* kernel writes saddr, csum, len */
286 udph = (void *) buf + off;
287 udph->source = ntohs(9000); /* random spoof */
288 udph->dest = ntohs(dest_port);
289 udph->len = ntohs(sizeof(*udph) + cfg_payload_len);
290 udph->check = 0; /* not allowed for IPv6 */
293 print_timestamp_usr();
294 if (cfg_proto != SOCK_STREAM) {
295 if (family == PF_INET)
296 val = sendto(fd, buf, total_len, 0, (void *) &daddr, sizeof(daddr));
298 val = sendto(fd, buf, total_len, 0, (void *) &daddr6, sizeof(daddr6));
300 val = send(fd, buf, cfg_payload_len, 0);
302 if (val != total_len)
303 error(1, errno, "send");
305 /* wait for all errors to be queued, else ACKs arrive OOO */
310 while (!recv_errmsg(fd)) {}
314 error(1, errno, "close");
320 static void __attribute__((noreturn)) usage(const char *filepath)
322 fprintf(stderr, "\nUsage: %s [options] hostname\n"
323 "\nwhere options are:\n"
326 " -h: show this message\n"
327 " -l N: send N bytes at a time\n"
329 " -R: use raw (IP_HDRINCL)\n"
330 " -p N: connect to port N\n"
336 static void parse_opt(int argc, char **argv)
341 while ((c = getopt(argc, argv, "46hl:p:rRu")) != -1) {
351 cfg_proto = SOCK_RAW;
352 cfg_ipproto = IPPROTO_UDP;
356 cfg_proto = SOCK_RAW;
357 cfg_ipproto = IPPROTO_RAW;
361 cfg_proto = SOCK_DGRAM;
362 cfg_ipproto = IPPROTO_UDP;
365 cfg_payload_len = strtoul(optarg, NULL, 10);
368 dest_port = strtoul(optarg, NULL, 10);
376 if (!cfg_payload_len)
377 error(1, 0, "payload may not be nonzero");
378 if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
379 error(1, 0, "udp packet might exceed expected MTU");
380 if (!do_ipv4 && !do_ipv6)
381 error(1, 0, "pass -4 or -6, not both");
383 error(1, 0, "pass -r, -R or -u, not multiple");
385 if (optind != argc - 1)
386 error(1, 0, "missing required hostname argument");
389 static void resolve_hostname(const char *hostname)
391 struct addrinfo *addrs, *cur;
392 int have_ipv4 = 0, have_ipv6 = 0;
394 if (getaddrinfo(hostname, NULL, NULL, &addrs))
395 error(1, errno, "getaddrinfo");
398 while (cur && !have_ipv4 && !have_ipv6) {
399 if (!have_ipv4 && cur->ai_family == AF_INET) {
400 memcpy(&daddr, cur->ai_addr, sizeof(daddr));
401 daddr.sin_port = htons(dest_port);
404 else if (!have_ipv6 && cur->ai_family == AF_INET6) {
405 memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
406 daddr6.sin6_port = htons(dest_port);
414 do_ipv4 &= have_ipv4;
415 do_ipv6 &= have_ipv6;
418 static void do_main(int family)
420 fprintf(stderr, "family: %s\n",
421 family == PF_INET ? "INET" : "INET6");
423 fprintf(stderr, "test SND\n");
424 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);
426 fprintf(stderr, "test ENQ\n");
427 do_test(family, SOF_TIMESTAMPING_TX_SCHED);
429 fprintf(stderr, "test ENQ + SND\n");
430 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
431 SOF_TIMESTAMPING_TX_SOFTWARE);
433 if (cfg_proto == SOCK_STREAM) {
434 fprintf(stderr, "\ntest ACK\n");
435 do_test(family, SOF_TIMESTAMPING_TX_ACK);
437 fprintf(stderr, "\ntest SND + ACK\n");
438 do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
439 SOF_TIMESTAMPING_TX_ACK);
441 fprintf(stderr, "\ntest ENQ + SND + ACK\n");
442 do_test(family, SOF_TIMESTAMPING_TX_SCHED |
443 SOF_TIMESTAMPING_TX_SOFTWARE |
444 SOF_TIMESTAMPING_TX_ACK);
448 const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };
450 int main(int argc, char **argv)
455 parse_opt(argc, argv);
456 resolve_hostname(argv[argc - 1]);
458 fprintf(stderr, "protocol: %s\n", sock_names[cfg_proto]);
459 fprintf(stderr, "payload: %u\n", cfg_payload_len);
460 fprintf(stderr, "server port: %u\n", dest_port);
461 fprintf(stderr, "\n");