txtimestamp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #include <arpa/inet.h>
  33. #include <asm/types.h>
  34. #include <error.h>
  35. #include <errno.h>
  36. #include <linux/errqueue.h>
  37. #include <linux/if_ether.h>
  38. #include <linux/net_tstamp.h>
  39. #include <netdb.h>
  40. #include <net/if.h>
  41. #include <netinet/in.h>
  42. #include <netinet/ip.h>
  43. #include <netinet/udp.h>
  44. #include <netinet/tcp.h>
  45. #include <netpacket/packet.h>
  46. #include <poll.h>
  47. #include <stdarg.h>
  48. #include <stdint.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <sys/ioctl.h>
  53. #include <sys/select.h>
  54. #include <sys/socket.h>
  55. #include <sys/time.h>
  56. #include <sys/types.h>
  57. #include <time.h>
  58. #include <unistd.h>
  59. /* command line parameters */
  60. static int cfg_proto = SOCK_STREAM;
  61. static int cfg_ipproto = IPPROTO_TCP;
  62. static int cfg_num_pkts = 4;
  63. static int do_ipv4 = 1;
  64. static int do_ipv6 = 1;
  65. static int cfg_payload_len = 10;
  66. static uint16_t dest_port = 9000;
  67. static struct sockaddr_in daddr;
  68. static struct sockaddr_in6 daddr6;
  69. static struct timespec ts_prev;
  70. static void __print_timestamp(const char *name, struct timespec *cur,
  71. uint32_t key, int payload_len)
  72. {
  73. if (!(cur->tv_sec | cur->tv_nsec))
  74. return;
  75. fprintf(stderr, " %s: %lu s %lu us (seq=%u, len=%u)",
  76. name, cur->tv_sec, cur->tv_nsec / 1000,
  77. key, payload_len);
  78. if ((ts_prev.tv_sec | ts_prev.tv_nsec)) {
  79. int64_t cur_ms, prev_ms;
  80. cur_ms = (long) cur->tv_sec * 1000 * 1000;
  81. cur_ms += cur->tv_nsec / 1000;
  82. prev_ms = (long) ts_prev.tv_sec * 1000 * 1000;
  83. prev_ms += ts_prev.tv_nsec / 1000;
  84. fprintf(stderr, " (%+ld us)", cur_ms - prev_ms);
  85. }
  86. ts_prev = *cur;
  87. fprintf(stderr, "\n");
  88. }
  89. static void print_timestamp_usr(void)
  90. {
  91. struct timespec ts;
  92. struct timeval tv; /* avoid dependency on -lrt */
  93. gettimeofday(&tv, NULL);
  94. ts.tv_sec = tv.tv_sec;
  95. ts.tv_nsec = tv.tv_usec * 1000;
  96. __print_timestamp(" USR", &ts, 0, 0);
  97. }
  98. static void print_timestamp(struct scm_timestamping *tss, int tstype,
  99. int tskey, int payload_len)
  100. {
  101. const char *tsname;
  102. switch (tstype) {
  103. case SCM_TSTAMP_SCHED:
  104. tsname = " ENQ";
  105. break;
  106. case SCM_TSTAMP_SND:
  107. tsname = " SND";
  108. break;
  109. case SCM_TSTAMP_ACK:
  110. tsname = " ACK";
  111. break;
  112. default:
  113. error(1, 0, "unknown timestamp type: %u",
  114. tstype);
  115. }
  116. __print_timestamp(tsname, &tss->ts[0], tskey, payload_len);
  117. }
  118. static void __poll(int fd)
  119. {
  120. struct pollfd pollfd;
  121. int ret;
  122. memset(&pollfd, 0, sizeof(pollfd));
  123. pollfd.fd = fd;
  124. ret = poll(&pollfd, 1, 100);
  125. if (ret != 1)
  126. error(1, errno, "poll");
  127. }
  128. static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
  129. {
  130. struct sock_extended_err *serr = NULL;
  131. struct scm_timestamping *tss = NULL;
  132. struct cmsghdr *cm;
  133. for (cm = CMSG_FIRSTHDR(msg);
  134. cm && cm->cmsg_len;
  135. cm = CMSG_NXTHDR(msg, cm)) {
  136. if (cm->cmsg_level == SOL_SOCKET &&
  137. cm->cmsg_type == SCM_TIMESTAMPING) {
  138. tss = (void *) CMSG_DATA(cm);
  139. } else if ((cm->cmsg_level == SOL_IP &&
  140. cm->cmsg_type == IP_RECVERR) ||
  141. (cm->cmsg_level == SOL_IPV6 &&
  142. cm->cmsg_type == IPV6_RECVERR)) {
  143. serr = (void *) CMSG_DATA(cm);
  144. if (serr->ee_errno != ENOMSG ||
  145. serr->ee_origin != SO_EE_ORIGIN_TIMESTAMPING) {
  146. fprintf(stderr, "unknown ip error %d %d\n",
  147. serr->ee_errno,
  148. serr->ee_origin);
  149. serr = NULL;
  150. }
  151. } else
  152. fprintf(stderr, "unknown cmsg %d,%d\n",
  153. cm->cmsg_level, cm->cmsg_type);
  154. }
  155. if (serr && tss)
  156. print_timestamp(tss, serr->ee_info, serr->ee_data, payload_len);
  157. }
  158. static int recv_errmsg(int fd)
  159. {
  160. static char ctrl[1024 /* overprovision*/];
  161. static struct msghdr msg;
  162. struct iovec entry;
  163. static char *data;
  164. int ret = 0;
  165. data = malloc(cfg_payload_len);
  166. if (!data)
  167. error(1, 0, "malloc");
  168. memset(&msg, 0, sizeof(msg));
  169. memset(&entry, 0, sizeof(entry));
  170. memset(ctrl, 0, sizeof(ctrl));
  171. entry.iov_base = data;
  172. entry.iov_len = cfg_payload_len;
  173. msg.msg_iov = &entry;
  174. msg.msg_iovlen = 1;
  175. msg.msg_name = NULL;
  176. msg.msg_namelen = 0;
  177. msg.msg_control = ctrl;
  178. msg.msg_controllen = sizeof(ctrl);
  179. ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
  180. if (ret == -1 && errno != EAGAIN)
  181. error(1, errno, "recvmsg");
  182. __recv_errmsg_cmsg(&msg, ret);
  183. free(data);
  184. return ret == -1;
  185. }
  186. static void do_test(int family, unsigned int opt)
  187. {
  188. char *buf;
  189. int fd, i, val, total_len;
  190. if (family == IPPROTO_IPV6 && cfg_proto != SOCK_STREAM) {
  191. /* due to lack of checksum generation code */
  192. fprintf(stderr, "test: skipping datagram over IPv6\n");
  193. return;
  194. }
  195. total_len = cfg_payload_len;
  196. if (cfg_proto == SOCK_RAW) {
  197. total_len += sizeof(struct udphdr);
  198. if (cfg_ipproto == IPPROTO_RAW)
  199. total_len += sizeof(struct iphdr);
  200. }
  201. buf = malloc(total_len);
  202. if (!buf)
  203. error(1, 0, "malloc");
  204. fd = socket(family, cfg_proto, cfg_ipproto);
  205. if (fd < 0)
  206. error(1, errno, "socket");
  207. if (cfg_proto == SOCK_STREAM) {
  208. val = 1;
  209. if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
  210. (char*) &val, sizeof(val)))
  211. error(1, 0, "setsockopt no nagle");
  212. if (family == PF_INET) {
  213. if (connect(fd, (void *) &daddr, sizeof(daddr)))
  214. error(1, errno, "connect ipv4");
  215. } else {
  216. if (connect(fd, (void *) &daddr6, sizeof(daddr6)))
  217. error(1, errno, "connect ipv6");
  218. }
  219. }
  220. opt |= SOF_TIMESTAMPING_SOFTWARE |
  221. SOF_TIMESTAMPING_OPT_ID;
  222. if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
  223. (char *) &opt, sizeof(opt)))
  224. error(1, 0, "setsockopt timestamping");
  225. for (i = 0; i < cfg_num_pkts; i++) {
  226. memset(&ts_prev, 0, sizeof(ts_prev));
  227. memset(buf, 'a' + i, total_len);
  228. buf[total_len - 2] = '\n';
  229. buf[total_len - 1] = '\0';
  230. if (cfg_proto == SOCK_RAW) {
  231. struct udphdr *udph;
  232. int off = 0;
  233. if (cfg_ipproto == IPPROTO_RAW) {
  234. struct iphdr *iph = (void *) buf;
  235. memset(iph, 0, sizeof(*iph));
  236. iph->ihl = 5;
  237. iph->version = 4;
  238. iph->ttl = 2;
  239. iph->daddr = daddr.sin_addr.s_addr;
  240. iph->protocol = IPPROTO_UDP;
  241. /* kernel writes saddr, csum, len */
  242. off = sizeof(*iph);
  243. }
  244. udph = (void *) buf + off;
  245. udph->source = ntohs(9000); /* random spoof */
  246. udph->dest = ntohs(dest_port);
  247. udph->len = ntohs(sizeof(*udph) + cfg_payload_len);
  248. udph->check = 0; /* not allowed for IPv6 */
  249. }
  250. print_timestamp_usr();
  251. if (cfg_proto != SOCK_STREAM) {
  252. if (family == PF_INET)
  253. val = sendto(fd, buf, total_len, 0, (void *) &daddr, sizeof(daddr));
  254. else
  255. val = sendto(fd, buf, total_len, 0, (void *) &daddr6, sizeof(daddr6));
  256. } else {
  257. val = send(fd, buf, cfg_payload_len, 0);
  258. }
  259. if (val != total_len)
  260. error(1, errno, "send");
  261. /* wait for all errors to be queued, else ACKs arrive OOO */
  262. usleep(50 * 1000);
  263. __poll(fd);
  264. while (!recv_errmsg(fd)) {}
  265. }
  266. if (close(fd))
  267. error(1, errno, "close");
  268. free(buf);
  269. usleep(400 * 1000);
  270. }
  271. static void __attribute__((noreturn)) usage(const char *filepath)
  272. {
  273. fprintf(stderr, "\nUsage: %s [options] hostname\n"
  274. "\nwhere options are:\n"
  275. " -4: only IPv4\n"
  276. " -6: only IPv6\n"
  277. " -h: show this message\n"
  278. " -l N: send N bytes at a time\n"
  279. " -r: use raw\n"
  280. " -R: use raw (IP_HDRINCL)\n"
  281. " -p N: connect to port N\n"
  282. " -u: use udp\n",
  283. filepath);
  284. exit(1);
  285. }
  286. static void parse_opt(int argc, char **argv)
  287. {
  288. int proto_count = 0;
  289. char c;
  290. while ((c = getopt(argc, argv, "46hl:p:rRu")) != -1) {
  291. switch (c) {
  292. case '4':
  293. do_ipv6 = 0;
  294. break;
  295. case '6':
  296. do_ipv4 = 0;
  297. break;
  298. case 'r':
  299. proto_count++;
  300. cfg_proto = SOCK_RAW;
  301. cfg_ipproto = IPPROTO_UDP;
  302. break;
  303. case 'R':
  304. proto_count++;
  305. cfg_proto = SOCK_RAW;
  306. cfg_ipproto = IPPROTO_RAW;
  307. break;
  308. case 'u':
  309. proto_count++;
  310. cfg_proto = SOCK_DGRAM;
  311. cfg_ipproto = IPPROTO_UDP;
  312. break;
  313. case 'l':
  314. cfg_payload_len = strtoul(optarg, NULL, 10);
  315. break;
  316. case 'p':
  317. dest_port = strtoul(optarg, NULL, 10);
  318. break;
  319. case 'h':
  320. default:
  321. usage(argv[0]);
  322. }
  323. }
  324. if (!cfg_payload_len)
  325. error(1, 0, "payload may not be nonzero");
  326. if (cfg_proto != SOCK_STREAM && cfg_payload_len > 1472)
  327. error(1, 0, "udp packet might exceed expected MTU");
  328. if (!do_ipv4 && !do_ipv6)
  329. error(1, 0, "pass -4 or -6, not both");
  330. if (proto_count > 1)
  331. error(1, 0, "pass -r, -R or -u, not multiple");
  332. if (optind != argc - 1)
  333. error(1, 0, "missing required hostname argument");
  334. }
  335. static void resolve_hostname(const char *hostname)
  336. {
  337. struct addrinfo *addrs, *cur;
  338. int have_ipv4 = 0, have_ipv6 = 0;
  339. if (getaddrinfo(hostname, NULL, NULL, &addrs))
  340. error(1, errno, "getaddrinfo");
  341. cur = addrs;
  342. while (cur && !have_ipv4 && !have_ipv6) {
  343. if (!have_ipv4 && cur->ai_family == AF_INET) {
  344. memcpy(&daddr, cur->ai_addr, sizeof(daddr));
  345. daddr.sin_port = htons(dest_port);
  346. have_ipv4 = 1;
  347. }
  348. else if (!have_ipv6 && cur->ai_family == AF_INET6) {
  349. memcpy(&daddr6, cur->ai_addr, sizeof(daddr6));
  350. daddr6.sin6_port = htons(dest_port);
  351. have_ipv6 = 1;
  352. }
  353. cur = cur->ai_next;
  354. }
  355. if (addrs)
  356. freeaddrinfo(addrs);
  357. do_ipv4 &= have_ipv4;
  358. do_ipv6 &= have_ipv6;
  359. }
  360. static void do_main(int family)
  361. {
  362. fprintf(stderr, "family: %s\n",
  363. family == PF_INET ? "INET" : "INET6");
  364. fprintf(stderr, "test SND\n");
  365. do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE);
  366. fprintf(stderr, "test ENQ\n");
  367. do_test(family, SOF_TIMESTAMPING_TX_SCHED);
  368. fprintf(stderr, "test ENQ + SND\n");
  369. do_test(family, SOF_TIMESTAMPING_TX_SCHED |
  370. SOF_TIMESTAMPING_TX_SOFTWARE);
  371. if (cfg_proto == SOCK_STREAM) {
  372. fprintf(stderr, "\ntest ACK\n");
  373. do_test(family, SOF_TIMESTAMPING_TX_ACK);
  374. fprintf(stderr, "\ntest SND + ACK\n");
  375. do_test(family, SOF_TIMESTAMPING_TX_SOFTWARE |
  376. SOF_TIMESTAMPING_TX_ACK);
  377. fprintf(stderr, "\ntest ENQ + SND + ACK\n");
  378. do_test(family, SOF_TIMESTAMPING_TX_SCHED |
  379. SOF_TIMESTAMPING_TX_SOFTWARE |
  380. SOF_TIMESTAMPING_TX_ACK);
  381. }
  382. }
  383. const char *sock_names[] = { NULL, "TCP", "UDP", "RAW" };
  384. int main(int argc, char **argv)
  385. {
  386. if (argc == 1)
  387. usage(argv[0]);
  388. parse_opt(argc, argv);
  389. resolve_hostname(argv[argc - 1]);
  390. fprintf(stderr, "protocol: %s\n", sock_names[cfg_proto]);
  391. fprintf(stderr, "payload: %u\n", cfg_payload_len);
  392. fprintf(stderr, "server port: %u\n", dest_port);
  393. fprintf(stderr, "\n");
  394. if (do_ipv4)
  395. do_main(PF_INET);
  396. if (do_ipv6)
  397. do_main(PF_INET6);
  398. return 0;
  399. }