txtimestamp.c 13 KB

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