rxtimestamp.c 9.6 KB

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