summary refs log tree commit diff
path: root/sunrpc/tst-udp-timeout.c
blob: 3c5b55d16cc5b8cad59a89f29e81d03b16bb4b2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* Test timeout handling in the UDP client.
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <netinet/in.h>
#include <rpc/clnt.h>
#include <rpc/svc.h>
#include <stdbool.h>
#include <string.h>
#include <support/check.h>
#include <support/namespace.h>
#include <support/test-driver.h>
#include <support/xsocket.h>
#include <support/xunistd.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>

/* Test data serialization and deserialization.   */

struct test_query
{
  uint32_t a;
  uint32_t b;
  uint32_t timeout_ms;
  uint32_t wait_for_seq;
  uint32_t garbage_packets;
};

static bool_t
xdr_test_query (XDR *xdrs, void *data, ...)
{
  struct test_query *p = data;
  return xdr_uint32_t (xdrs, &p->a)
    && xdr_uint32_t (xdrs, &p->b)
    && xdr_uint32_t (xdrs, &p->timeout_ms)
    && xdr_uint32_t (xdrs, &p->wait_for_seq)
    && xdr_uint32_t (xdrs, &p->garbage_packets);
}

struct test_response
{
  uint32_t seq;
  uint32_t sum;
};

static bool_t
xdr_test_response (XDR *xdrs, void *data, ...)
{
  struct test_response *p = data;
  return xdr_uint32_t (xdrs, &p->seq)
    && xdr_uint32_t (xdrs, &p->sum);
}

/* Implementation of the test server.  */

enum
  {
    /* RPC parameters, chosen at random.  */
    PROGNUM = 15717,
    VERSNUM = 13689,

    /* Main RPC operation.  */
    PROC_ADD = 1,

    /* Reset the sequence number.  */
    PROC_RESET_SEQ,

    /* Request process termination.  */
    PROC_EXIT,

    /* Special exit status to mark successful processing.  */
    EXIT_MARKER = 55,
  };

static void
server_dispatch (struct svc_req *request, SVCXPRT *transport)
{
  /* Query sequence number.  */
  static uint32_t seq = 0;
  ++seq;

  if (test_verbose)
    printf ("info: server_dispatch seq=%u rq_proc=%lu\n",
            seq, request->rq_proc);

  switch (request->rq_proc)
    {
    case PROC_ADD:
      {
        struct test_query query;
        memset (&query, 0xc0, sizeof (query));
        TEST_VERIFY_EXIT
          (svc_getargs (transport, xdr_test_query,
                        (void *) &query));

        if (test_verbose)
          printf ("  a=%u b=%u timeout_ms=%u wait_for_seq=%u"
                  " garbage_packets=%u\n",
                  query.a, query.b, query.timeout_ms, query.wait_for_seq,
                  query.garbage_packets);

        if (seq < query.wait_for_seq)
          {
            /* No response at this point.  */
            if (test_verbose)
              printf ("  skipped response\n");
            break;
          }

        if (query.garbage_packets > 0)
          {
            int per_packet_timeout;
            if (query.timeout_ms > 0)
              per_packet_timeout
                = query.timeout_ms * 1000 / query.garbage_packets;
            else
              per_packet_timeout = 0;

            char buf[20];
            memset (&buf, 0xc0, sizeof (buf));
            for (int i = 0; i < query.garbage_packets; ++i)
              {
                /* 13 is relatively prime to 20 = sizeof (buf) + 1, so
                   the len variable will cover the entire interval
                   [0, 20] if query.garbage_packets is sufficiently
                   large.  */
                size_t len = (i * 13 + 1) % (sizeof (buf) + 1);
                TEST_VERIFY (sendto (transport->xp_sock,
                                     buf, len, MSG_NOSIGNAL,
                                     (struct sockaddr *) &transport->xp_raddr,
                                     transport->xp_addrlen) == len);
                if (per_packet_timeout > 0)
                  usleep (per_packet_timeout);
              }
          }
        else if (query.timeout_ms > 0)
          usleep (query.timeout_ms * 1000);

        struct test_response response =
          {
            .seq = seq,
            .sum = query.a + query.b,
          };
        TEST_VERIFY (svc_sendreply (transport, xdr_test_response,
                                    (void *) &response));
      }
      break;

    case PROC_RESET_SEQ:
      seq = 0;
      TEST_VERIFY (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL));
      break;

    case PROC_EXIT:
      TEST_VERIFY (svc_sendreply (transport, (xdrproc_t) xdr_void, NULL));
      _exit (EXIT_MARKER);
      break;

    default:
      FAIL_EXIT1 ("invalid rq_proc value: %lu", request->rq_proc);
      break;
    }
}

/* Implementation of the test client.  */

static struct test_response
test_call (CLIENT *clnt, int proc, struct test_query query,
           struct timeval timeout)
{
  if (test_verbose)
    printf ("info: test_call proc=%d timeout=%lu.%06lu\n",
            proc, (unsigned long) timeout.tv_sec,
            (unsigned long) timeout.tv_usec);
  struct test_response response;
  TEST_VERIFY_EXIT (clnt_call (clnt, proc,
                               xdr_test_query, (void *) &query,
                               xdr_test_response, (void *) &response,
                               timeout)
                    == RPC_SUCCESS);
  return response;
}

static void
test_call_timeout (CLIENT *clnt, int proc, struct test_query query,
                   struct timeval timeout)
{
  struct test_response response;
  TEST_VERIFY (clnt_call (clnt, proc,
                          xdr_test_query, (void *) &query,
                          xdr_test_response, (void *) &response,
                          timeout)
               == RPC_TIMEDOUT);
}

/* Complete one regular RPC call to drain the server socket
   buffer.  Resets the sequence number.  */
static void
test_call_flush (CLIENT *clnt)
{
  /* This needs a longer timeout to flush out all pending requests.
     The choice of 5 seconds is larger than the per-response timeouts
     requested via the timeout_ms field.  */
  if (test_verbose)
    printf ("info: flushing pending queries\n");
  TEST_VERIFY_EXIT (clnt_call (clnt, PROC_RESET_SEQ,
                               (xdrproc_t) xdr_void, NULL,
                               (xdrproc_t) xdr_void, NULL,
                               ((struct timeval) { 5, 0 }))
                    == RPC_SUCCESS);
}

/* Return the number seconds since an arbitrary point in time.  */
static double
get_ticks (void)
{
  {
    struct timespec ts;
    if (clock_gettime (CLOCK_MONOTONIC, &ts) == 0)
      return ts.tv_sec + ts.tv_nsec * 1e-9;
  }
  {
    struct timeval tv;
    TEST_VERIFY_EXIT (gettimeofday (&tv, NULL) == 0);
    return tv.tv_sec + tv.tv_usec * 1e-6;
  }
}

static void
test_udp_server (int port)
{
  struct sockaddr_in sin =
    {
      .sin_family = AF_INET,
      .sin_addr.s_addr = htonl (INADDR_LOOPBACK),
      .sin_port = htons (port)
    };
  int sock = RPC_ANYSOCK;

  /* The client uses a 1.5 second timeout for retries.  The timeouts
     are arbitrary, but chosen so that there is a substantial gap
     between them, but the total time spent waiting is not too
     large.  */
  CLIENT *clnt = clntudp_create (&sin, PROGNUM, VERSNUM,
                                 (struct timeval) { 1, 500 * 1000 },
                                 &sock);
  TEST_VERIFY_EXIT (clnt != NULL);

  /* Basic call/response test.  */
  struct test_response response = test_call
    (clnt, PROC_ADD,
     (struct test_query) { .a = 17, .b = 4 },
     (struct timeval) { 3, 0 });
  TEST_VERIFY (response.sum == 21);
  TEST_VERIFY (response.seq == 1);

  /* Check that garbage packets do not interfere with timeout
     processing.  */
  double before = get_ticks ();
  response = test_call
    (clnt, PROC_ADD,
     (struct test_query) {
       .a = 19, .b = 4, .timeout_ms = 500, .garbage_packets = 21,
     },
     (struct timeval) { 3, 0 });
  TEST_VERIFY (response.sum == 23);
  TEST_VERIFY (response.seq == 2);
  double after = get_ticks ();
  if (test_verbose)
    printf ("info: 21 garbage packets took %f seconds\n", after - before);
  /* Expected timeout is 0.5 seconds.  Add some slack in case process
     scheduling delays processing the query or response, but do not
     accept a retry (which would happen at 1.5 seconds).  */
  TEST_VERIFY (0.5 <= after - before);
  TEST_VERIFY (after - before < 1.2);
  test_call_flush (clnt);

  /* Check that missing a response introduces a 1.5 second timeout, as
     requested when calling clntudp_create.  */
  before = get_ticks ();
  response = test_call
    (clnt, PROC_ADD,
     (struct test_query) { .a = 170, .b = 40, .wait_for_seq = 2 },
     (struct timeval) { 3, 0 });
  TEST_VERIFY (response.sum == 210);
  TEST_VERIFY (response.seq == 2);
  after = get_ticks ();
  if (test_verbose)
    printf ("info: skipping one response took %f seconds\n",
            after - before);
  /* Expected timeout is 1.5 seconds.  Do not accept a second retry
     (which would happen at 3 seconds).  */
  TEST_VERIFY (1.5 <= after - before);
  TEST_VERIFY (after - before < 2.9);
  test_call_flush (clnt);

  /* Check that the overall timeout wins against the per-query
     timeout.  */
  before = get_ticks ();
  test_call_timeout
    (clnt, PROC_ADD,
     (struct test_query) { .a = 170, .b = 41, .wait_for_seq = 2 },
     (struct timeval) { 0, 750 * 1000 });
  after = get_ticks ();
  if (test_verbose)
    printf ("info: 0.75 second timeout took %f seconds\n",
            after - before);
  TEST_VERIFY (0.75 <= after - before);
  TEST_VERIFY (after - before < 1.4);
  test_call_flush (clnt);

  for (int with_garbage = 0; with_garbage < 2; ++with_garbage)
    {
      /* Check that no response at all causes the client to bail out.  */
      before = get_ticks ();
      test_call_timeout
        (clnt, PROC_ADD,
         (struct test_query) {
           .a = 170, .b = 40, .timeout_ms = 1200,
           .garbage_packets = with_garbage * 21
         },
         (struct timeval) { 0, 750 * 1000 });
      after = get_ticks ();
      if (test_verbose)
        printf ("info: test_udp_server: 0.75 second timeout took %f seconds"
                " (garbage %d)\n",
                after - before, with_garbage);
      TEST_VERIFY (0.75 <= after - before);
      TEST_VERIFY (after - before < 1.4);
      test_call_flush (clnt);

      /* As above, but check the total timeout.  */
      before = get_ticks ();
      test_call_timeout
        (clnt, PROC_ADD,
         (struct test_query) {
           .a = 170, .b = 40, .timeout_ms = 3000,
           .garbage_packets = with_garbage * 30
         },
         (struct timeval) { 2, 500 * 1000 });
      after = get_ticks ();
      if (test_verbose)
        printf ("info: test_udp_server: 2.5 second timeout took %f seconds"
                " (garbage %d)\n",
                after - before, with_garbage);
      TEST_VERIFY (2.5 <= after - before);
      TEST_VERIFY (after - before < 3.0);
      test_call_flush (clnt);
    }

  TEST_VERIFY_EXIT (clnt_call (clnt, PROC_EXIT,
                               (xdrproc_t) xdr_void, NULL,
                               (xdrproc_t) xdr_void, NULL,
                               ((struct timeval) { 5, 0 }))
                    == RPC_SUCCESS);
  clnt_destroy (clnt);
}

static int
do_test (void)
{
  support_become_root ();
  support_enter_network_namespace ();

  SVCXPRT *transport = svcudp_create (RPC_ANYSOCK);
  TEST_VERIFY_EXIT (transport != NULL);
  TEST_VERIFY (svc_register (transport, PROGNUM, VERSNUM, server_dispatch, 0));

  pid_t pid = xfork ();
  if (pid == 0)
    {
      svc_run ();
      FAIL_EXIT1 ("supposed to be unreachable");
    }
  test_udp_server (transport->xp_port);

  int status;
  xwaitpid (pid, &status, 0);
  TEST_VERIFY (WIFEXITED (status) && WEXITSTATUS (status) == EXIT_MARKER);

  SVC_DESTROY (transport);
  return 0;
}

/* The minimum run time is around 17 seconds.  */
#define TIMEOUT 25
#include <support/test-driver.c>