txtimestamp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Copyright 2014 Google Inc.
  3. * Author: willemb@google.com (Willem de Bruijn)
  4. *
  5. * Test software tx timestamping, including
  6. *
  7. * - SCHED, SND and ACK timestamps
  8. * - RAW, UDP and TCP
  9. * - IPv4 and IPv6
  10. * - various packet sizes (to test GSO and TSO)
  11. *
  12. * Consult the command line arguments for help on running
  13. * the various testcases.
  14. *
  15. * This test requires a dummy TCP server.
  16. * A simple `nc6 [-u] -l -p $DESTPORT` will do
  17. *
  18. *
  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.
  22. *
  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
  26. * more details.
  27. *
  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.
  31. */
  32. #define _GNU_SOURCE
  33. #include <arpa/inet.h>
  34. #include <asm/types.h>
  35. #include <error.h>
  36. #include <errno.h>
  37. #include <inttypes.h>
  38. #include <linux/errqueue.h>
  39. #include <linux/if_ether.h>
  40. #include <linux/net_tstamp.h>
  41. #include <netdb.h>
  42. #include <net/if.h>
  43. #include <netinet/in.h>
  44. #include <netinet/ip.h>
  45. #include <netinet/udp.h>
  46. #include <netinet/tcp.h>
  47. #include <netpacket/packet.h>
  48. #include <poll.h>
  49. #include <stdarg.h>
  50. #include <stdbool.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <sys/ioctl.h>
  55. #include <sys/select.h>
  56. #include <sys/socket.h>
  57. #include <sys/time.h>
  58. #include <sys/types.h>
  59. #include <time.h>
  60. #include <unistd.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 int cfg_poll_timeout = 100;
  69. static bool cfg_show_payload;
  70. static bool cfg_do_pktinfo;
  71. static bool cfg_loop_nodata;
  72. static bool cfg_no_delay;
  73. static uint16_t dest_port = 9000;
  74. static struct sockaddr_in daddr;
  75. static struct sockaddr_in6 daddr6;
  76. static struct timespec ts_prev;
  77. static void __print_timestamp(const char *name, struct timespec *cur,
  78. uint32_t key, int payload_len)
  79. {
  80. if (!(cur->tv_sec | cur->tv_nsec))
  81. return;
  82. fprintf(stderr, " %s: %lu s %lu us (seq=%u, len=%u)",
  83. name, cur->tv_sec, cur->tv_nsec / 1000,
  84. key, payload_len);
  85. if ((ts_prev.tv_sec | ts_prev.tv_nsec)) {
  86. int64_t cur_ms, prev_ms;
  87. cur_ms = (long) cur->tv_sec * 1000 * 1000;
  88. cur_ms += cur->tv_nsec / 1000;
  89. prev_ms = (long) ts_prev.tv_sec * 1000 * 1000;
  90. prev_ms += ts_prev.tv_nsec / 1000;
  91. fprintf(stderr, " (%+" PRId64 " us)", cur_ms - prev_ms);
  92. }
  93. ts_prev = *cur;
  94. fprintf(stderr, "\n");
  95. }
  96. static void print_timestamp_usr(void)
  97. {
  98. struct timespec ts;
  99. struct timeval tv; /* avoid dependency on -lrt */
  100. gettimeofday(&tv, NULL);
  101. ts.tv_sec = tv.tv_sec;
  102. ts.tv_nsec = tv.tv_usec * 1000;
  103. __print_timestamp(" USR", &ts, 0, 0);
  104. }
  105. static void print_timestamp(struct scm_timestamping *tss, int tstype,
  106. int tskey, int payload_len)
  107. {
  108. const char *tsname;
  109. switch (tstype) {
  110. case SCM_TSTAMP_SCHED:
  111. tsname = " ENQ";
  112. break;
  113. case SCM_TSTAMP_SND:
  114. tsname = " SND";
  115. break;
  116. case SCM_TSTAMP_ACK:
  117. tsname = " ACK";
  118. break;
  119. default:
  120. error(1, 0, "unknown timestamp type: %u",
  121. tstype);
  122. }
  123. __print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
  124. }
  125. /* TODO: convert to check_and_print payload once API is stable */
  126. static void print_payload(char *data, int len)
  127. {
  128. int i;
  129. if (!len)
  130. return;
  131. if (len > 70)
  132. len = 70;
  133. fprintf(stderr, "payload: ");
  134. for (i = 0; i < len; i++)
  135. fprintf(stderr, "%02hhx ", data[i]);
  136. fprintf(stderr, "\n");
  137. }
  138. static void print_pktinfo(int family, int ifindex, void *saddr, void *daddr)
  139. {
  140. char sa[INET6_ADDRSTRLEN], da[INET6_ADDRSTRLEN];
  141. fprintf(stderr, " pktinfo: ifindex=%u src=%s dst=%s\n",
  142. ifindex,
  143. saddr ? inet_ntop(family, saddr, sa, sizeof(sa)) : "unknown",
  144. daddr ? inet_ntop(family, daddr, da, sizeof(da)) : "unknown");
  145. }
  146. static void __poll(int fd)
  147. {
  148. struct pollfd pollfd;
  149. int ret;
  150. memset(&pollfd, 0, sizeof(pollfd));
  151. pollfd.fd = fd;
  152. ret = poll(&pollfd, 1, cfg_poll_timeout);
  153. if (ret != 1)
  154. error(1, errno, "poll");
  155. }
  156. static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
  157. {
  158. struct sock_extended_err *serr = NULL;
  159. struct scm_timestamping *tss = NULL;
  160. struct cmsghdr *cm;
  161. int batch = 0;
  162. for (cm = CMSG_FIRSTHDR(msg);
  163. cm && cm->cmsg_len;
  164. cm = CMSG_NXTHDR(msg, cm)) {
  165. if (cm->cmsg_level == SOL_SOCKET &&
  166. cm->cmsg_type == SCM_TIMESTAMPING) {
  167. tss = (void *) CMSG_DATA(cm);
  168. } else if ((cm->cmsg_level == SOL_IP &&
  169. cm->cmsg_type == IP_RECVERR) ||
  170. (cm->cmsg_level == SOL_IPV6 &&
  171. cm->cmsg_type == IPV6_RECVERR)) {
  172. serr = (void *) CMSG_DATA(cm);
  173. if (serr->ee_errno != ENOMSG ||
  174. serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
  175. fprintf(stderr, "unknown ip error %d %d\n",
  176. serr->ee_errno,
  177. serr->ee_origin);
  178. serr = NULL;
  179. }
  180. } else if (cm->cmsg_level == SOL_IP &&
  181. cm->cmsg_type == IP_PKTINFO) {
  182. struct in_pktinfo *info = (void *) CMSG_DATA(cm);
  183. print_pktinfo(AF_INET, info->ipi_ifindex,
  184. &info->ipi_spec_dst, &info->ipi_addr);
  185. } else if (cm->cmsg_level == SOL_IPV6 &&
  186. cm->cmsg_type == IPV6_PKTINFO) {
  187. struct in6_pktinfo *info6 = (void *) CMSG_DATA(cm);
  188. print_pktinfo(AF_INET6, info6->ipi6_ifindex,
  189. NULL, &info6->ipi6_addr);
  190. } else
  191. fprintf(stderr, "unknown cmsg %d,%d\n",
  192. cm->cmsg_level, cm->cmsg_type);
  193. if (serr && tss) {
  194. print_timestamp(tss, serr->ee_info, serr->ee_data,
  195. payload_len);
  196. serr = NULL;
  197. tss = NULL;
  198. batch++;
  199. }
  200. }
  201. if (batch > 1)
  202. fprintf(stderr, "batched %d timestamps\n", batch);
  203. }
  204. static int recv_errmsg(int fd)
  205. {
  206. static char ctrl[1024 /* overprovision*/];
  207. static struct msghdr msg;
  208. struct iovec entry;
  209. static char *data;
  210. int ret = 0;
  211. data = malloc(cfg_payload_len);
  212. if (!data)
  213. error(1, 0, "malloc");
  214. memset(&msg, 0, sizeof(msg));
  215. memset(&entry, 0, sizeof(entry));
  216. memset(ctrl, 0, sizeof(ctrl));
  217. entry.iov_base = data;
  218. entry.iov_len = cfg_payload_len;
  219. msg.msg_iov = &entry;
  220. msg.msg_iovlen = 1;
  221. msg.msg_name = NULL;
  222. msg.msg_namelen = 0;
  223. msg.msg_control = ctrl;
  224. msg.msg_controllen = sizeof(ctrl);
  225. ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
  226. if (ret == -1 && errno != EAGAIN)
  227. error(1, errno, "recvmsg");
  228. if (ret >= 0) {
  229. __recv_errmsg_cmsg(&msg, ret);
  230. if (cfg_show_payload)
  231. print_payload(data, cfg_payload_len);
  232. }
  233. free(data);
  234. return ret == -1;
  235. }
  236. static void do_test(int family, unsigned int opt)
  237. {
  238. char *buf;
  239. int fd, i, val = 1, total_len;
  240. if (family == AF_INET6 && cfg_proto != SOCK_STREAM) {
  241. /* due to lack of checksum generation code */
  242. fprintf(stderr, "test: skipping datagram over IPv6\n");
  243. return;
  244. }
  245. total_len = cfg_payload_len;
  246. if (cfg_proto == SOCK_RAW) {
  247. total_len += sizeof(struct udphdr);
  248. if (cfg_ipproto == IPPROTO_RAW)
  249. total_len += sizeof(struct iphdr);
  250. }
  251. buf = malloc(total_len);
  252. if (!buf)
  253. error(1, 0, "malloc");
  254. fd = socket(family, cfg_proto, cfg_ipproto);
  255. if (fd < 0)
  256. error(1, errno, "socket");
  257. if (cfg_proto == SOCK_STREAM) {
  258. if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
  259. (char*) &val, sizeof(val)))
  260. error(1, 0, "setsockopt no nagle");
  261. if (family == PF_INET) {
  262. if (connect(fd, (void *) &daddr, sizeof(daddr)))
  263. error(1, errno, "connect ipv4");
  264. } else {
  265. if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
  266. error(1, errno, "connect ipv6");
  267. }
  268. }
  269. if (cfg_do_pktinfo) {
  270. if (family == AF_INET6) {
  271. if (setsockopt(fd, SOL_IPV6, IPV6_RECVPKTINFO,
  272. &val, sizeof(val)))
  273. error(1, errno, "setsockopt pktinfo ipv6");
  274. } else {
  275. if (setsockopt(fd, SOL_IP, IP_PKTINFO,
  276. &val, sizeof(val)))
  277. error(1, errno, "setsockopt pktinfo ipv4");
  278. }
  279. }
  280. opt |= SOF_TIMESTAMPING_SOFTWARE |
  281. SOF_TIMESTAMPING_OPT_CMSG |
  282. SOF_TIMESTAMPING_OPT_ID;
  283. if (cfg_loop_nodata)
  284. opt |= SOF_TIMESTAMPING_OPT_TSONLY;
  285. if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
  286. (char *) &opt, sizeof(opt)))
  287. error(1, 0, "setsockopt timestamping");
  288. for (i = 0; i < cfg_num_pkts; i++) {
  289. memset(&ts_prev, 0, sizeof(ts_prev));
  290. memset(buf, 'a' + i, total_len);
  291. if (cfg_proto == SOCK_RAW) {
  292. struct udphdr *udph;
  293. int off = 0;
  294. if (cfg_ipproto == IPPROTO_RAW) {
  295. struct iphdr *iph = (void *) buf;
  296. memset(iph, 0, sizeof(*iph));
  297. iph->ihl = 5;
  298. iph->version = 4;
  299. iph->ttl = 2;
  300. iph->daddr = daddr.sin_addr.s_addr;
  301. iph->protocol = IPPROTO_UDP;
  302. /* kernel writes saddr, csum, len */
  303. off = sizeof(*iph);
  304. }
  305. udph = (void *) buf + off;
  306. udph->source = ntohs(9000); /* random spoof */
  307. udph->dest = ntohs(dest_port);
  308. udph->len = ntohs(sizeof(*udph) + cfg_payload_len);
  309. udph->check = 0; /* not allowed for IPv6 */
  310. }
  311. print_timestamp_usr();
  312. if (cfg_proto != SOCK_STREAM) {
  313. if (family == PF_INET)
  314. val = sendto(fd, buf, total_len, 0, (void *) &daddr, sizeof(daddr));
  315. else
  316. val = sendto(fd, buf, total_len, 0, (void *) &daddr6, sizeof(daddr6));
  317. } else {
  318. val = send(fd, buf, cfg_payload_len, 0);
  319. }
  320. if (val != total_len)
  321. error(1, errno, "send");
  322. /* wait for all errors to be queued, else ACKs arrive OOO */
  323. if (!cfg_no_delay)
  324. usleep(50 * 1000);
  325. __poll(fd);
  326. while (!recv_errmsg(fd)) {}
  327. }
  328. if (close(fd))
  329. error(1, errno, "close");
  330. free(buf);
  331. usleep(400 * 1000);
  332. }
  333. static void __attribute__((noreturn)) usage(const char *filepath)
  334. {
  335. fprintf(stderr, "\nUsage: %s [options] hostname\n"
  336. "\nwhere options are:\n"
  337. " -4: only IPv4\n"
  338. " -6: only IPv6\n"
  339. " -h: show this message\n"
  340. " -c N: number of packets for each test\n"
  341. " -D: no delay between packets\n"
  342. " -F: poll() waits forever for an event\n"
  343. " -I: request PKTINFO\n"
  344. " -l N: send N bytes at a time\n"
  345. " -n: set no-payload option\n"
  346. " -r: use raw\n"
  347. " -R: use raw (IP_HDRINCL)\n"
  348. " -p N: connect to port N\n"
  349. " -u: use udp\n"
  350. " -x: show payload (up to 70 bytes)\n",
  351. filepath);
  352. exit(1);
  353. }
  354. static void parse_opt(int argc, char **argv)
  355. {
  356. int proto_count = 0;
  357. char c;
  358. while ((c = getopt(argc, argv, "46c:DFhIl:np:rRux")) != -1) {
  359. switch (c) {
  360. case '4':
  361. do_ipv6 = 0;
  362. break;
  363. case '6':
  364. do_ipv4 = 0;
  365. break;
  366. case 'c':
  367. cfg_num_pkts = strtoul(optarg, NULL, 10);
  368. break;
  369. case 'D':
  370. cfg_no_delay = true;
  371. break;
  372. case 'F':
  373. cfg_poll_timeout = -1;
  374. break;
  375. case 'I':
  376. cfg_do_pktinfo = true;
  377. break;
  378. case 'n':
  379. cfg_loop_nodata = true;
  380. break;
  381. case 'r':
  382. proto_count++;
  383. cfg_proto = SOCK_RAW;
  384. cfg_ipproto = IPPROTO_UDP;
  385. break;
  386. case 'R':
  387. proto_count++;
  388. cfg_proto = SOCK_RAW;
  389. cfg_ipproto = IPPROTO_RAW;
  390. break;
  391. case 'u':
  392. proto_count++;
  393. cfg_proto = SOCK_DGRAM;
  394. cfg_ipproto = IPPROTO_UDP;
  395. break;
  396. case 'l':
  397. cfg_payload_len = strtoul(optarg, NULL, 10);
  398. break;
  399. case 'p':
  400. dest_port = strtoul(optarg, NULL, 10);
  401. break;
  402. case 'x':
  403. cfg_show_payload = true;
  404. break;
  405. case 'h':
  406. default:
  407. usage(argv[0]);
  408. }
  409. }
  410. if (!cfg_payload_len)
  411. error(1, 0, "payload may not be nonzero");
  412. if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
  413. error(1, 0, "udp packet might exceed expected MTU");
  414. if (!do_ipv4 && !do_ipv6)
  415. error(1, 0, "pass -4 or -6, not both");
  416. if (proto_count > 1)
  417. error(1, 0, "pass -r, -R or -u, not multiple");
  418. if (optind != argc - 1)
  419. error(1, 0, "missing required hostname argument");
  420. }
  421. static void resolve_hostname(const char *hostname)
  422. {
  423. struct addrinfo *addrs, *cur;
  424. int have_ipv4 = 0, have_ipv6 = 0;
  425. if (getaddrinfo(hostname, NULL, NULL, &addrs))
  426. error(1, errno, "getaddrinfo");
  427. cur = addrs;
  428. while (cur && !have_ipv4 && !have_ipv6) {
  429. if (!have_ipv4 && cur->ai_family == AF_INET) {
  430. memcpy(&daddr, cur->ai_addr, sizeof(daddr));
  431. daddr.sin_port = htons(dest_port);
  432. have_ipv4 = 1;
  433. }
  434. else if (!have_ipv6 && cur->ai_family == AF_INET6) {
  435. memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
  436. daddr6.sin6_port = htons(dest_port);
  437. have_ipv6 = 1;
  438. }
  439. cur = cur->ai_next;
  440. }
  441. if (addrs)
  442. freeaddrinfo(addrs);
  443. do_ipv4 &= have_ipv4;
  444. do_ipv6 &= have_ipv6;
  445. }
  446. static void do_main(int family)
  447. {
  448. fprintf(stderr, "family: %s\n",
  449. family == PF_INET ? "INET" : "INET6");
  450. fprintf(stderr, "test SND\n");
  451. do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);
  452. fprintf(stderr, "test ENQ\n");
  453. do_test(family, SOF_TIMESTAMPING_TX_SCHED);
  454. fprintf(stderr, "test ENQ + SND\n");
  455. do_test(family, SOF_TIMESTAMPING_TX_SCHED |
  456. SOF_TIMESTAMPING_TX_SOFTWARE);
  457. if (cfg_proto == SOCK_STREAM) {
  458. fprintf(stderr, "\ntest ACK\n");
  459. do_test(family, SOF_TIMESTAMPING_TX_ACK);
  460. fprintf(stderr, "\ntest SND + ACK\n");
  461. do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
  462. SOF_TIMESTAMPING_TX_ACK);
  463. fprintf(stderr, "\ntest ENQ + SND + ACK\n");
  464. do_test(family, SOF_TIMESTAMPING_TX_SCHED |
  465. SOF_TIMESTAMPING_TX_SOFTWARE |
  466. SOF_TIMESTAMPING_TX_ACK);
  467. }
  468. }
  469. const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };
  470. int main(int argc, char **argv)
  471. {
  472. if (argc == 1)
  473. usage(argv[0]);
  474. parse_opt(argc, argv);
  475. resolve_hostname(argv[argc - 1]);
  476. fprintf(stderr, "protocol: %s\n", sock_names[cfg_proto]);
  477. fprintf(stderr, "payload: %u\n", cfg_payload_len);
  478. fprintf(stderr, "server port: %u\n", dest_port);
  479. fprintf(stderr, "\n");
  480. if (do_ipv4)
  481. do_main(PF_INET);
  482. if (do_ipv6)
  483. do_main(PF_INET6);
  484. return 0;
  485. }