tst-udp-timeout.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* Test timeout handling in the UDP client.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <netinet/in.h>
  16. #include <rpc/clnt.h>
  17. #include <rpc/svc.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <support/check.h>
  21. #include <support/namespace.h>
  22. #include <support/test-driver.h>
  23. #include <support/xsocket.h>
  24. #include <support/xunistd.h>
  25. #include <sys/socket.h>
  26. #include <time.h>
  27. #include <unistd.h>
  28. /* Test data serialization and deserialization. */
  29. struct test_query
  30. {
  31. uint32_t a;
  32. uint32_t b;
  33. uint32_t timeout_ms;
  34. uint32_t wait_for_seq;
  35. uint32_t garbage_packets;
  36. };
  37. static bool_t
  38. xdr_test_query (XDR *xdrs, void *data, ...)
  39. {
  40. struct test_query *p = data;
  41. return xdr_uint32_t (xdrs, &p->a)
  42. && xdr_uint32_t (xdrs, &p->b)
  43. && xdr_uint32_t (xdrs, &p->timeout_ms)
  44. && xdr_uint32_t (xdrs, &p->wait_for_seq)
  45. && xdr_uint32_t (xdrs, &p->garbage_packets);
  46. }
  47. struct test_response
  48. {
  49. uint32_t seq;
  50. uint32_t sum;
  51. };
  52. static bool_t
  53. xdr_test_response (XDR *xdrs, void *data, ...)
  54. {
  55. struct test_response *p = data;
  56. return xdr_uint32_t (xdrs, &p->seq)
  57. && xdr_uint32_t (xdrs, &p->sum);
  58. }
  59. /* Implementation of the test server. */
  60. enum
  61. {
  62. /* RPC parameters, chosen at random. */
  63. PROGNUM = 15717,
  64. VERSNUM = 13689,
  65. /* Main RPC operation. */
  66. PROC_ADD = 1,
  67. /* Reset the sequence number. */
  68. PROC_RESET_SEQ,
  69. /* Request process termination. */
  70. PROC_EXIT,
  71. /* Special exit status to mark successful processing. */
  72. EXIT_MARKER = 55,
  73. };
  74. static void
  75. server_dispatch (struct svc_req *request, SVCXPRT *transport)
  76. {
  77. /* Query sequence number. */
  78. static uint32_t seq = 0;
  79. ++seq;
  80. if (test_verbose)
  81. printf ("info: server_dispatch seq=%u rq_proc=%lu\n",
  82. seq, request->rq_proc);
  83. switch (request->rq_proc)
  84. {
  85. case PROC_ADD:
  86. {
  87. struct test_query query;
  88. memset (&query, 0xc0, sizeof (query));
  89. TEST_VERIFY_EXIT
  90. (svc_getargs (transport, xdr_test_query,
  91. (void *) &query));
  92. if (test_verbose)
  93. printf (" a=%u b=%u timeout_ms=%u wait_for_seq=%u"
  94. " garbage_packets=%u\n",
  95. query.a, query.b, query.timeout_ms, query.wait_for_seq,
  96. query.garbage_packets);
  97. if (seq < query.wait_for_seq)
  98. {
  99. /* No response at this point. */
  100. if (test_verbose)
  101. printf (" skipped response\n");
  102. break;
  103. }
  104. if (query.garbage_packets > 0)
  105. {
  106. int per_packet_timeout;
  107. if (query.timeout_ms > 0)
  108. per_packet_timeout
  109. = query.timeout_ms * 1000 / query.garbage_packets;
  110. else
  111. per_packet_timeout = 0;
  112. char buf[20];
  113. memset (&buf, 0xc0, sizeof (buf));
  114. for (int i = 0; i < query.garbage_packets; ++i)
  115. {
  116. /* 13 is relatively prime to 20 = sizeof (buf) + 1, so
  117. the len variable will cover the entire interval
  118. [0, 20] if query.garbage_packets is sufficiently
  119. large. */
  120. size_t len = (i * 13 + 1) % (sizeof (buf) + 1);
  121. TEST_VERIFY (sendto (transport->xp_sock,
  122. buf, len, MSG_NOSIGNAL,
  123. (struct sockaddr *) &transport->xp_raddr,
  124. transport->xp_addrlen) == len);
  125. if (per_packet_timeout > 0)
  126. usleep (per_packet_timeout);
  127. }
  128. }
  129. else if (query.timeout_ms > 0)
  130. usleep (query.timeout_ms * 1000);
  131. struct test_response response =
  132. {
  133. .seq = seq,
  134. .sum = query.a + query.b,
  135. };
  136. TEST_VERIFY (svc_sendreply (transport, xdr_test_response,
  137. (void *) &response));
  138. }
  139. break;
  140. case PROC_RESET_SEQ:
  141. seq = 0;
  142. TEST_VERIFY (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL));
  143. break;
  144. case PROC_EXIT:
  145. TEST_VERIFY (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL));
  146. _exit (EXIT_MARKER);
  147. break;
  148. default:
  149. FAIL_EXIT1 ("invalid rq_proc value: %lu", request->rq_proc);
  150. break;
  151. }
  152. }
  153. /* Implementation of the test client. */
  154. static struct test_response
  155. test_call (CLIENT *clnt, int proc, struct test_query query,
  156. struct timeval timeout)
  157. {
  158. if (test_verbose)
  159. printf ("info: test_call proc=%d timeout=%lu.%06lu\n",
  160. proc, (unsigned long) timeout.tv_sec,
  161. (unsigned long) timeout.tv_usec);
  162. struct test_response response;
  163. TEST_VERIFY_EXIT (clnt_call (clnt, proc,
  164. xdr_test_query, (void *) &query,
  165. xdr_test_response, (void *) &response,
  166. timeout)
  167. == RPC_SUCCESS);
  168. return response;
  169. }
  170. static void
  171. test_call_timeout (CLIENT *clnt, int proc, struct test_query query,
  172. struct timeval timeout)
  173. {
  174. struct test_response response;
  175. TEST_VERIFY (clnt_call (clnt, proc,
  176. xdr_test_query, (void *) &query,
  177. xdr_test_response, (void *) &response,
  178. timeout)
  179. == RPC_TIMEDOUT);
  180. }
  181. /* Complete one regular RPC call to drain the server socket
  182. buffer. Resets the sequence number. */
  183. static void
  184. test_call_flush (CLIENT *clnt)
  185. {
  186. /* This needs a longer timeout to flush out all pending requests.
  187. The choice of 5 seconds is larger than the per-response timeouts
  188. requested via the timeout_ms field. */
  189. if (test_verbose)
  190. printf ("info: flushing pending queries\n");
  191. TEST_VERIFY_EXIT (clnt_call (clnt, PROC_RESET_SEQ,
  192. (xdrproc_t) xdr_void, NULL,
  193. (xdrproc_t) xdr_void, NULL,
  194. ((struct timeval) { 5, 0 }))
  195. == RPC_SUCCESS);
  196. }
  197. /* Return the number seconds since an arbitrary point in time. */
  198. static double
  199. get_ticks (void)
  200. {
  201. {
  202. struct timespec ts;
  203. if (clock_gettime (CLOCK_MONOTONIC, &ts) == 0)
  204. return ts.tv_sec + ts.tv_nsec * 1e-9;
  205. }
  206. {
  207. struct timeval tv;
  208. TEST_VERIFY_EXIT (gettimeofday (&tv, NULL) == 0);
  209. return tv.tv_sec + tv.tv_usec * 1e-6;
  210. }
  211. }
  212. static void
  213. test_udp_server (int port)
  214. {
  215. struct sockaddr_in sin =
  216. {
  217. .sin_family = AF_INET,
  218. .sin_addr.s_addr = htonl (INADDR_LOOPBACK),
  219. .sin_port = htons (port)
  220. };
  221. int sock = RPC_ANYSOCK;
  222. /* The client uses a 1.5 second timeout for retries. The timeouts
  223. are arbitrary, but chosen so that there is a substantial gap
  224. between them, but the total time spent waiting is not too
  225. large. */
  226. CLIENT *clnt = clntudp_create (&sin, PROGNUM, VERSNUM,
  227. (struct timeval) { 1, 500 * 1000 },
  228. &sock);
  229. TEST_VERIFY_EXIT (clnt != NULL);
  230. /* Basic call/response test. */
  231. struct test_response response = test_call
  232. (clnt, PROC_ADD,
  233. (struct test_query) { .a = 17, .b = 4 },
  234. (struct timeval) { 3, 0 });
  235. TEST_VERIFY (response.sum == 21);
  236. TEST_VERIFY (response.seq == 1);
  237. /* Check that garbage packets do not interfere with timeout
  238. processing. */
  239. double before = get_ticks ();
  240. response = test_call
  241. (clnt, PROC_ADD,
  242. (struct test_query) {
  243. .a = 19, .b = 4, .timeout_ms = 500, .garbage_packets = 21,
  244. },
  245. (struct timeval) { 3, 0 });
  246. TEST_VERIFY (response.sum == 23);
  247. TEST_VERIFY (response.seq == 2);
  248. double after = get_ticks ();
  249. if (test_verbose)
  250. printf ("info: 21 garbage packets took %f seconds\n", after - before);
  251. /* Expected timeout is 0.5 seconds. Add some slack in case process
  252. scheduling delays processing the query or response, but do not
  253. accept a retry (which would happen at 1.5 seconds). */
  254. TEST_VERIFY (0.5 <= after - before);
  255. TEST_VERIFY (after - before < 1.2);
  256. test_call_flush (clnt);
  257. /* Check that missing a response introduces a 1.5 second timeout, as
  258. requested when calling clntudp_create. */
  259. before = get_ticks ();
  260. response = test_call
  261. (clnt, PROC_ADD,
  262. (struct test_query) { .a = 170, .b = 40, .wait_for_seq = 2 },
  263. (struct timeval) { 3, 0 });
  264. TEST_VERIFY (response.sum == 210);
  265. TEST_VERIFY (response.seq == 2);
  266. after = get_ticks ();
  267. if (test_verbose)
  268. printf ("info: skipping one response took %f seconds\n",
  269. after - before);
  270. /* Expected timeout is 1.5 seconds. Do not accept a second retry
  271. (which would happen at 3 seconds). */
  272. TEST_VERIFY (1.5 <= after - before);
  273. TEST_VERIFY (after - before < 2.9);
  274. test_call_flush (clnt);
  275. /* Check that the overall timeout wins against the per-query
  276. timeout. */
  277. before = get_ticks ();
  278. test_call_timeout
  279. (clnt, PROC_ADD,
  280. (struct test_query) { .a = 170, .b = 41, .wait_for_seq = 2 },
  281. (struct timeval) { 0, 750 * 1000 });
  282. after = get_ticks ();
  283. if (test_verbose)
  284. printf ("info: 0.75 second timeout took %f seconds\n",
  285. after - before);
  286. TEST_VERIFY (0.75 <= after - before);
  287. TEST_VERIFY (after - before < 1.4);
  288. test_call_flush (clnt);
  289. for (int with_garbage = 0; with_garbage < 2; ++with_garbage)
  290. {
  291. /* Check that no response at all causes the client to bail out. */
  292. before = get_ticks ();
  293. test_call_timeout
  294. (clnt, PROC_ADD,
  295. (struct test_query) {
  296. .a = 170, .b = 40, .timeout_ms = 1200,
  297. .garbage_packets = with_garbage * 21
  298. },
  299. (struct timeval) { 0, 750 * 1000 });
  300. after = get_ticks ();
  301. if (test_verbose)
  302. printf ("info: test_udp_server: 0.75 second timeout took %f seconds"
  303. " (garbage %d)\n",
  304. after - before, with_garbage);
  305. TEST_VERIFY (0.75 <= after - before);
  306. TEST_VERIFY (after - before < 1.4);
  307. test_call_flush (clnt);
  308. /* As above, but check the total timeout. */
  309. before = get_ticks ();
  310. test_call_timeout
  311. (clnt, PROC_ADD,
  312. (struct test_query) {
  313. .a = 170, .b = 40, .timeout_ms = 3000,
  314. .garbage_packets = with_garbage * 30
  315. },
  316. (struct timeval) { 2, 500 * 1000 });
  317. after = get_ticks ();
  318. if (test_verbose)
  319. printf ("info: test_udp_server: 2.5 second timeout took %f seconds"
  320. " (garbage %d)\n",
  321. after - before, with_garbage);
  322. TEST_VERIFY (2.5 <= after - before);
  323. TEST_VERIFY (after - before < 3.0);
  324. test_call_flush (clnt);
  325. }
  326. TEST_VERIFY_EXIT (clnt_call (clnt, PROC_EXIT,
  327. (xdrproc_t) xdr_void, NULL,
  328. (xdrproc_t) xdr_void, NULL,
  329. ((struct timeval) { 5, 0 }))
  330. == RPC_SUCCESS);
  331. clnt_destroy (clnt);
  332. }
  333. static int
  334. do_test (void)
  335. {
  336. support_become_root ();
  337. support_enter_network_namespace ();
  338. SVCXPRT *transport = svcudp_create (RPC_ANYSOCK);
  339. TEST_VERIFY_EXIT (transport != NULL);
  340. TEST_VERIFY (svc_register (transport, PROGNUM, VERSNUM, server_dispatch, 0));
  341. pid_t pid = xfork ();
  342. if (pid == 0)
  343. {
  344. svc_run ();
  345. FAIL_EXIT1 ("supposed to be unreachable");
  346. }
  347. test_udp_server (transport->xp_port);
  348. int status;
  349. xwaitpid (pid, &status, 0);
  350. TEST_VERIFY (WIFEXITED (status) && WEXITSTATUS (status) == EXIT_MARKER);
  351. SVC_DESTROY (transport);
  352. return 0;
  353. }
  354. /* The minimum run time is around 17 seconds. */
  355. #define TIMEOUT 25
  356. #include <support/test-driver.c>