rxtimestamp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #include <errno.h>
  2. #include <error.h>
  3. #include <getopt.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/time.h>
  10. #include <sys/socket.h>
  11. #include <sys/select.h>
  12. #include <sys/ioctl.h>
  13. #include <arpa/inet.h>
  14. #include <net/if.h>
  15. #include <asm/types.h>
  16. #include <linux/net_tstamp.h>
  17. #include <linux/errqueue.h>
  18. #include "kselftest.h"
  19. struct options {
  20. int so_timestamp;
  21. int so_timestampns;
  22. int so_timestamping;
  23. };
  24. struct tstamps {
  25. bool tstamp;
  26. bool tstampns;
  27. bool swtstamp;
  28. bool hwtstamp;
  29. };
  30. struct socket_type {
  31. char *friendly_name;
  32. int type;
  33. int protocol;
  34. bool enabled;
  35. };
  36. struct test_case {
  37. struct options sockopt;
  38. struct tstamps expected;
  39. bool enabled;
  40. bool warn_on_fail;
  41. };
  42. struct sof_flag {
  43. int mask;
  44. char *name;
  45. };
  46. static struct sof_flag sof_flags[] = {
  47. #define SOF_FLAG(f) { f, #f }
  48. SOF_FLAG(SOF_TIMESTAMPING_SOFTWARE),
  49. SOF_FLAG(SOF_TIMESTAMPING_RX_SOFTWARE),
  50. SOF_FLAG(SOF_TIMESTAMPING_RX_HARDWARE),
  51. SOF_FLAG(SOF_TIMESTAMPING_OPT_RX_FILTER),
  52. SOF_FLAG(SOF_TIMESTAMPING_RAW_HARDWARE),
  53. };
  54. static struct socket_type socket_types[] = {
  55. { "ip", SOCK_RAW, IPPROTO_EGP },
  56. { "udp", SOCK_DGRAM, IPPROTO_UDP },
  57. { "tcp", SOCK_STREAM, IPPROTO_TCP },
  58. };
  59. static struct test_case test_cases[] = {
  60. { {}, {} },
  61. {
  62. { .so_timestamp = 1 },
  63. { .tstamp = true }
  64. },
  65. {
  66. { .so_timestampns = 1 },
  67. { .tstampns = true }
  68. },
  69. {
  70. { .so_timestamp = 1, .so_timestampns = 1 },
  71. { .tstampns = true }
  72. },
  73. {
  74. { .so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE },
  75. {}
  76. },
  77. {
  78. /* Loopback device does not support hw timestamps. */
  79. { .so_timestamping = SOF_TIMESTAMPING_RX_HARDWARE },
  80. {}
  81. },
  82. {
  83. { .so_timestamping = SOF_TIMESTAMPING_SOFTWARE },
  84. .warn_on_fail = true
  85. },
  86. {
  87. { .so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE
  88. | SOF_TIMESTAMPING_RX_HARDWARE },
  89. {}
  90. },
  91. {
  92. { .so_timestamping = SOF_TIMESTAMPING_RAW_HARDWARE
  93. | SOF_TIMESTAMPING_OPT_RX_FILTER },
  94. {}
  95. },
  96. {
  97. { .so_timestamping = SOF_TIMESTAMPING_SOFTWARE
  98. | SOF_TIMESTAMPING_OPT_RX_FILTER },
  99. {}
  100. },
  101. {
  102. { .so_timestamping = SOF_TIMESTAMPING_SOFTWARE
  103. | SOF_TIMESTAMPING_RX_SOFTWARE
  104. | SOF_TIMESTAMPING_OPT_RX_FILTER },
  105. { .swtstamp = true }
  106. },
  107. {
  108. { .so_timestamping = SOF_TIMESTAMPING_SOFTWARE
  109. | SOF_TIMESTAMPING_RX_SOFTWARE },
  110. { .swtstamp = true }
  111. },
  112. {
  113. { .so_timestamp = 1, .so_timestamping = SOF_TIMESTAMPING_SOFTWARE
  114. | SOF_TIMESTAMPING_RX_SOFTWARE },
  115. { .tstamp = true, .swtstamp = true }
  116. },
  117. };
  118. static struct option long_options[] = {
  119. { "list_tests", no_argument, 0, 'l' },
  120. { "test_num", required_argument, 0, 'n' },
  121. { "op_size", required_argument, 0, 's' },
  122. { "tcp", no_argument, 0, 't' },
  123. { "udp", no_argument, 0, 'u' },
  124. { "ip", no_argument, 0, 'i' },
  125. { "strict", no_argument, 0, 'S' },
  126. { "ipv4", no_argument, 0, '4' },
  127. { "ipv6", no_argument, 0, '6' },
  128. { NULL, 0, NULL, 0 },
  129. };
  130. static int next_port = 19999;
  131. static int op_size = 10 * 1024;
  132. void print_test_case(struct test_case *t)
  133. {
  134. int f = 0;
  135. printf("sockopts {");
  136. if (t->sockopt.so_timestamp)
  137. printf(" SO_TIMESTAMP ");
  138. if (t->sockopt.so_timestampns)
  139. printf(" SO_TIMESTAMPNS ");
  140. if (t->sockopt.so_timestamping) {
  141. printf(" SO_TIMESTAMPING: {");
  142. for (f = 0; f < ARRAY_SIZE(sof_flags); f++)
  143. if (t->sockopt.so_timestamping & sof_flags[f].mask)
  144. printf(" %s |", sof_flags[f].name);
  145. printf("}");
  146. }
  147. printf("} expected cmsgs: {");
  148. if (t->expected.tstamp)
  149. printf(" SCM_TIMESTAMP ");
  150. if (t->expected.tstampns)
  151. printf(" SCM_TIMESTAMPNS ");
  152. if (t->expected.swtstamp || t->expected.hwtstamp) {
  153. printf(" SCM_TIMESTAMPING {");
  154. if (t->expected.swtstamp)
  155. printf("0");
  156. if (t->expected.swtstamp && t->expected.hwtstamp)
  157. printf(",");
  158. if (t->expected.hwtstamp)
  159. printf("2");
  160. printf("}");
  161. }
  162. printf("}\n");
  163. }
  164. void do_send(int src)
  165. {
  166. int r;
  167. char *buf = malloc(op_size);
  168. memset(buf, 'z', op_size);
  169. r = write(src, buf, op_size);
  170. if (r < 0)
  171. error(1, errno, "Failed to sendmsg");
  172. free(buf);
  173. }
  174. bool do_recv(int rcv, int read_size, struct tstamps expected)
  175. {
  176. const int CMSG_SIZE = 1024;
  177. struct scm_timestamping *ts;
  178. struct tstamps actual = {};
  179. char cmsg_buf[CMSG_SIZE];
  180. struct iovec recv_iov;
  181. struct cmsghdr *cmsg;
  182. bool failed = false;
  183. struct msghdr hdr;
  184. int flags = 0;
  185. int r;
  186. memset(&hdr, 0, sizeof(hdr));
  187. hdr.msg_iov = &recv_iov;
  188. hdr.msg_iovlen = 1;
  189. recv_iov.iov_base = malloc(read_size);
  190. recv_iov.iov_len = read_size;
  191. hdr.msg_control = cmsg_buf;
  192. hdr.msg_controllen = sizeof(cmsg_buf);
  193. r = recvmsg(rcv, &hdr, flags);
  194. if (r < 0)
  195. error(1, errno, "Failed to recvmsg");
  196. if (r != read_size)
  197. error(1, 0, "Only received %d bytes of payload.", r);
  198. if (hdr.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
  199. error(1, 0, "Message was truncated.");
  200. for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
  201. cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
  202. if (cmsg->cmsg_level != SOL_SOCKET)
  203. error(1, 0, "Unexpected cmsg_level %d",
  204. cmsg->cmsg_level);
  205. switch (cmsg->cmsg_type) {
  206. case SCM_TIMESTAMP:
  207. actual.tstamp = true;
  208. break;
  209. case SCM_TIMESTAMPNS:
  210. actual.tstampns = true;
  211. break;
  212. case SCM_TIMESTAMPING:
  213. ts = (struct scm_timestamping *)CMSG_DATA(cmsg);
  214. actual.swtstamp = !!ts->ts[0].tv_sec;
  215. if (ts->ts[1].tv_sec != 0)
  216. error(0, 0, "ts[1] should not be set.");
  217. actual.hwtstamp = !!ts->ts[2].tv_sec;
  218. break;
  219. default:
  220. error(1, 0, "Unexpected cmsg_type %d", cmsg->cmsg_type);
  221. }
  222. }
  223. #define VALIDATE(field) \
  224. do { \
  225. if (expected.field != actual.field) { \
  226. if (expected.field) \
  227. error(0, 0, "Expected " #field " to be set."); \
  228. else \
  229. error(0, 0, \
  230. "Expected " #field " to not be set."); \
  231. failed = true; \
  232. } \
  233. } while (0)
  234. VALIDATE(tstamp);
  235. VALIDATE(tstampns);
  236. VALIDATE(swtstamp);
  237. VALIDATE(hwtstamp);
  238. #undef VALIDATE
  239. free(recv_iov.iov_base);
  240. return failed;
  241. }
  242. void config_so_flags(int rcv, struct options o)
  243. {
  244. int on = 1;
  245. if (setsockopt(rcv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
  246. error(1, errno, "Failed to enable SO_REUSEADDR");
  247. if (o.so_timestamp &&
  248. setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMP,
  249. &o.so_timestamp, sizeof(o.so_timestamp)) < 0)
  250. error(1, errno, "Failed to enable SO_TIMESTAMP");
  251. if (o.so_timestampns &&
  252. setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPNS,
  253. &o.so_timestampns, sizeof(o.so_timestampns)) < 0)
  254. error(1, errno, "Failed to enable SO_TIMESTAMPNS");
  255. if (o.so_timestamping &&
  256. setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPING,
  257. &o.so_timestamping, sizeof(o.so_timestamping)) < 0)
  258. error(1, errno, "Failed to set SO_TIMESTAMPING");
  259. }
  260. bool run_test_case(struct socket_type *s, int test_num, char ip_version,
  261. bool strict)
  262. {
  263. union {
  264. struct sockaddr_in6 addr6;
  265. struct sockaddr_in addr4;
  266. struct sockaddr addr_un;
  267. } addr;
  268. int read_size = op_size;
  269. int src, dst, rcv, port;
  270. socklen_t addr_size;
  271. bool failed = false;
  272. port = (s->type == SOCK_RAW) ? 0 : next_port++;
  273. memset(&addr, 0, sizeof(addr));
  274. if (ip_version == '4') {
  275. addr.addr4.sin_family = AF_INET;
  276. addr.addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  277. addr.addr4.sin_port = htons(port);
  278. addr_size = sizeof(addr.addr4);
  279. if (s->type == SOCK_RAW)
  280. read_size += 20; /* for IPv4 header */
  281. } else {
  282. addr.addr6.sin6_family = AF_INET6;
  283. addr.addr6.sin6_addr = in6addr_loopback;
  284. addr.addr6.sin6_port = htons(port);
  285. addr_size = sizeof(addr.addr6);
  286. }
  287. printf("Starting testcase %d over ipv%c...\n", test_num, ip_version);
  288. src = socket(addr.addr_un.sa_family, s->type,
  289. s->protocol);
  290. if (src < 0)
  291. error(1, errno, "Failed to open src socket");
  292. dst = socket(addr.addr_un.sa_family, s->type,
  293. s->protocol);
  294. if (dst < 0)
  295. error(1, errno, "Failed to open dst socket");
  296. if (bind(dst, &addr.addr_un, addr_size) < 0)
  297. error(1, errno, "Failed to bind to port %d", port);
  298. if (s->type == SOCK_STREAM && (listen(dst, 1) < 0))
  299. error(1, errno, "Failed to listen");
  300. if (connect(src, &addr.addr_un, addr_size) < 0)
  301. error(1, errno, "Failed to connect");
  302. if (s->type == SOCK_STREAM) {
  303. rcv = accept(dst, NULL, NULL);
  304. if (rcv < 0)
  305. error(1, errno, "Failed to accept");
  306. close(dst);
  307. } else {
  308. rcv = dst;
  309. }
  310. config_so_flags(rcv, test_cases[test_num].sockopt);
  311. usleep(20000); /* setsockopt for SO_TIMESTAMPING is asynchronous */
  312. do_send(src);
  313. failed = do_recv(rcv, read_size, test_cases[test_num].expected);
  314. close(rcv);
  315. close(src);
  316. if (failed) {
  317. printf("FAILURE in testcase %d over ipv%c ", test_num,
  318. ip_version);
  319. print_test_case(&test_cases[test_num]);
  320. if (!strict && test_cases[test_num].warn_on_fail)
  321. failed = false;
  322. }
  323. return failed;
  324. }
  325. int main(int argc, char **argv)
  326. {
  327. bool all_protocols = true;
  328. bool all_tests = true;
  329. bool cfg_ipv4 = false;
  330. bool cfg_ipv6 = false;
  331. bool strict = false;
  332. int arg_index = 0;
  333. int failures = 0;
  334. int s, t, opt;
  335. while ((opt = getopt_long(argc, argv, "", long_options,
  336. &arg_index)) != -1) {
  337. switch (opt) {
  338. case 'l':
  339. for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
  340. printf("%d\t", t);
  341. print_test_case(&test_cases[t]);
  342. }
  343. return 0;
  344. case 'n':
  345. t = atoi(optarg);
  346. if (t >= ARRAY_SIZE(test_cases))
  347. error(1, 0, "Invalid test case: %d", t);
  348. all_tests = false;
  349. test_cases[t].enabled = true;
  350. break;
  351. case 's':
  352. op_size = atoi(optarg);
  353. break;
  354. case 't':
  355. all_protocols = false;
  356. socket_types[2].enabled = true;
  357. break;
  358. case 'u':
  359. all_protocols = false;
  360. socket_types[1].enabled = true;
  361. break;
  362. case 'i':
  363. all_protocols = false;
  364. socket_types[0].enabled = true;
  365. break;
  366. case 'S':
  367. strict = true;
  368. break;
  369. case '4':
  370. cfg_ipv4 = true;
  371. break;
  372. case '6':
  373. cfg_ipv6 = true;
  374. break;
  375. default:
  376. error(1, 0, "Failed to parse parameters.");
  377. }
  378. }
  379. for (s = 0; s < ARRAY_SIZE(socket_types); s++) {
  380. if (!all_protocols && !socket_types[s].enabled)
  381. continue;
  382. printf("Testing %s...\n", socket_types[s].friendly_name);
  383. for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
  384. if (!all_tests && !test_cases[t].enabled)
  385. continue;
  386. if (cfg_ipv4 || !cfg_ipv6)
  387. if (run_test_case(&socket_types[s], t, '4',
  388. strict))
  389. failures++;
  390. if (cfg_ipv6 || !cfg_ipv4)
  391. if (run_test_case(&socket_types[s], t, '6',
  392. strict))
  393. failures++;
  394. }
  395. }
  396. if (!failures)
  397. printf("PASSED.\n");
  398. return failures;
  399. }