blob: 4e61045b0559caaf7c5b5db711f3267229996859 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallacee4d5a652018-06-24 21:21:21 -04002 * Copyright (c) 2017-2018 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Dave Wallacee4d5a652018-06-24 21:21:21 -040016#include <unistd.h>
17#include <errno.h>
18#include <stdlib.h>
19#include <ctype.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <stdio.h>
23#include <time.h>
24#include <arpa/inet.h>
25#include <vcl/vcl_test.h>
Dave Wallace543852a2017-08-03 02:11:34 -040026
Dave Wallacee4d5a652018-06-24 21:21:21 -040027typedef struct
28{
29 vppcom_endpt_t server_endpt;
30 struct sockaddr_storage server_addr;
31 uint32_t server_addr_size;
32 uint32_t cfg_seq_num;
33 sock_test_socket_t ctrl_socket;
34 sock_test_socket_t *test_socket;
35 uint32_t num_test_sockets;
36 uint8_t dump_cfg;
37} sock_client_main_t;
38
39sock_client_main_t sock_client_main;
40
41
42static int
43sock_test_cfg_sync (sock_test_socket_t * socket)
44{
45 sock_client_main_t *scm = &sock_client_main;
46 sock_test_socket_t *ctrl = &scm->ctrl_socket;
47 sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf;
48 int rx_bytes, tx_bytes;
49
50 if (socket->cfg.verbose)
51 sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ );
52
53 ctrl->cfg.seq_num = ++scm->cfg_seq_num;
54 if (socket->cfg.verbose)
55 {
56 printf ("CLIENT (fd %d): Sending config sent to server.\n", socket->fd);
57 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
58 }
59 tx_bytes = vcl_test_write (socket->fd, (uint8_t *) & ctrl->cfg,
60 sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose);
61 if (tx_bytes < 0)
62 {
63 fprintf (stderr, "CLIENT (fd %d): ERROR: write test cfg failed (%d)!\n",
64 socket->fd, tx_bytes);
65 return tx_bytes;
66 }
67
68 rx_bytes = vcl_test_read (socket->fd, (uint8_t *) socket->rxbuf,
69 sizeof (sock_test_cfg_t), NULL);
70 if (rx_bytes < 0)
71 return rx_bytes;
72
73 if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC)
74 {
75 fprintf (stderr, "CLIENT (fd %d): ERROR: Bad server reply cfg "
76 "-- aborting!\n", socket->fd);
77 return -1;
78 }
79 if ((rx_bytes != sizeof (sock_test_cfg_t))
80 || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg))
81 {
82 fprintf (stderr, "CLIENT (fd %d): ERROR: Invalid config received "
83 "from server!\n", socket->fd);
84 if (rx_bytes != sizeof (sock_test_cfg_t))
85 {
86 fprintf (stderr, "\tRx bytes %d != cfg size %lu\n",
87 rx_bytes, sizeof (sock_test_cfg_t));
88 }
89 else
90 {
91 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
92 fprintf (stderr, "CLIENT (fd %d): Valid config sent to server.\n",
93 socket->fd);
94 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
95 }
96 return -1;
97 }
98 else if (socket->cfg.verbose)
99 {
100 printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd);
101 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
102 }
103 ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ?
104 rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle);
105
106 return 0;
107}
108
109static void
110echo_test_client ()
111{
112 sock_client_main_t *scm = &sock_client_main;
113 sock_test_socket_t *ctrl = &scm->ctrl_socket;
114 sock_test_socket_t *tsock;
Florin Coras134a9962018-08-28 11:32:04 -0700115 int rv, nfds = 0, rx_bytes, tx_bytes, nbytes;
116 uint32_t i, n, sidx;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400117 fd_set wr_fdset, rd_fdset;
118 fd_set _wfdset, *wfdset = &_wfdset;
119 fd_set _rfdset, *rfdset = &_rfdset;
120
121 FD_ZERO (&wr_fdset);
122 FD_ZERO (&rd_fdset);
123 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
124 ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1;
125 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
126 {
127 tsock = &scm->test_socket[n];
128 tsock->cfg = ctrl->cfg;
129 sock_test_socket_buf_alloc (tsock);
130 if (sock_test_cfg_sync (tsock))
131 return;
132
133 memcpy (tsock->txbuf, ctrl->txbuf, nbytes);
134 memset (&tsock->stats, 0, sizeof (tsock->stats));
135
136 FD_SET (tsock->fd, &wr_fdset);
137 FD_SET (tsock->fd, &rd_fdset);
Florin Coras134a9962018-08-28 11:32:04 -0700138 sidx = vppcom_session_index (tsock->fd);
139 nfds = sidx > nfds ? sidx : nfds;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400140 }
141
142 nfds++;
143 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
144 while (n)
145 {
146 _wfdset = wr_fdset;
147 _rfdset = rd_fdset;
148
149 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
150 NULL, 0);
151 if (rv < 0)
152 {
153 perror ("select()");
154 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
155 "aborting test!\n");
156 return;
157 }
158 else if (rv == 0)
159 continue;
160
161 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
162 {
163 tsock = &scm->test_socket[i];
164 if (!((tsock->stats.stop.tv_sec == 0) &&
165 (tsock->stats.stop.tv_nsec == 0)))
166 continue;
167
Florin Coras134a9962018-08-28 11:32:04 -0700168 if (FD_ISSET (vppcom_session_index (tsock->fd), wfdset)
Florin Coras54693d22018-07-17 10:46:29 -0700169 && (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
Dave Wallacee4d5a652018-06-24 21:21:21 -0400170
171 {
Florin Coras54693d22018-07-17 10:46:29 -0700172 tx_bytes = vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
173 nbytes, &tsock->stats,
174 ctrl->cfg.verbose);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400175 if (tx_bytes < 0)
176 {
177 fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) "
178 "failed -- aborting test!\n", tsock->fd);
179 return;
180 }
181
Dave Wallacee4d5a652018-06-24 21:21:21 -0400182 }
183
Florin Coras134a9962018-08-28 11:32:04 -0700184 if ((FD_ISSET (vppcom_session_index (tsock->fd), rfdset)) &&
Dave Wallacee4d5a652018-06-24 21:21:21 -0400185 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
186 {
Florin Coras54693d22018-07-17 10:46:29 -0700187 rx_bytes = vcl_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
188 nbytes, &tsock->stats);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400189 if (rx_bytes > 0)
190 {
191 printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n",
192 tsock->fd, rx_bytes, tsock->rxbuf);
193
194 if (tsock->stats.rx_bytes != tsock->stats.tx_bytes)
195 printf ("CLIENT: WARNING: bytes read (%lu) "
196 "!= bytes written (%lu)!\n",
197 tsock->stats.rx_bytes, tsock->stats.tx_bytes);
198 }
199 }
200
201 if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)
202 {
203 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
204 n--;
205 }
206 }
207 }
208 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
209
210 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
211 {
212 tsock = &scm->test_socket[i];
213 tsock->stats.start = ctrl->stats.start;
214
215 if (ctrl->cfg.verbose)
216 {
217 static char buf[64];
218
219 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
220 sock_test_stats_dump (buf, &tsock->stats,
221 1 /* show_rx */ , 1 /* show tx */ ,
222 ctrl->cfg.verbose);
223 }
224
225 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
226 }
227
228 if (ctrl->cfg.verbose)
229 {
230 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
231 1 /* show_rx */ , 1 /* show tx */ ,
232 ctrl->cfg.verbose);
233 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
234
235 if (ctrl->cfg.verbose > 1)
236 {
237 printf (" ctrl socket info\n"
238 SOCK_TEST_SEPARATOR_STRING
239 " fd: %d (0x%08x)\n"
240 " rxbuf: %p\n"
241 " rxbuf size: %u (0x%08x)\n"
242 " txbuf: %p\n"
243 " txbuf size: %u (0x%08x)\n"
244 SOCK_TEST_SEPARATOR_STRING,
245 ctrl->fd, (uint32_t) ctrl->fd,
246 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
247 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
248 }
249 }
250}
251
252static void
253stream_test_client (sock_test_t test)
254{
255 sock_client_main_t *scm = &sock_client_main;
256 sock_test_socket_t *ctrl = &scm->ctrl_socket;
257 sock_test_socket_t *tsock;
Florin Coras293aa052018-08-30 18:49:13 -0700258 int tx_bytes, rv, nfds = 0;
259 uint32_t i, n, sidx;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400260 fd_set wr_fdset, rd_fdset;
261 fd_set _wfdset, *wfdset = &_wfdset;
262 fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0;
263
264 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
265 ctrl->cfg.ctrl_handle = ~0;
266
267 printf ("\n" SOCK_TEST_BANNER_STRING
268 "CLIENT (fd %d): %s-directional Stream Test!\n\n"
269 "CLIENT (fd %d): Sending config to server on ctrl socket...\n",
270 ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd);
271
272 if (sock_test_cfg_sync (ctrl))
273 {
274 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
275 return;
276 }
277
278 FD_ZERO (&wr_fdset);
279 FD_ZERO (&rd_fdset);
280 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
281 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
282 {
283 tsock = &scm->test_socket[n];
284 tsock->cfg = ctrl->cfg;
285 sock_test_socket_buf_alloc (tsock);
286 printf ("CLIENT (fd %d): Sending config to server on "
287 "test socket %d...\n", tsock->fd, n);
288 sock_test_cfg_sync (tsock);
289
290 /* Fill payload with incrementing uint32's */
291 for (i = 0; i < tsock->txbuf_size; i++)
292 tsock->txbuf[i] = i & 0xff;
293
294 memset (&tsock->stats, 0, sizeof (tsock->stats));
295 FD_SET (tsock->fd, &wr_fdset);
296 FD_SET (tsock->fd, &rd_fdset);
Florin Coras293aa052018-08-30 18:49:13 -0700297 sidx = vppcom_session_index (tsock->fd);
298 nfds = sidx > nfds ? sidx : nfds;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400299 }
300
301 nfds++;
302 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
303 while (n)
304 {
305 _wfdset = wr_fdset;
306 _rfdset = rd_fdset;
307
308 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
309 NULL, 0);
310 if (rv < 0)
311 {
312 perror ("select()");
313 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
314 "aborting test!\n");
315 return;
316 }
317 else if (rv == 0)
318 continue;
319
320 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
321 {
322 tsock = &scm->test_socket[i];
323 if (!((tsock->stats.stop.tv_sec == 0) &&
324 (tsock->stats.stop.tv_nsec == 0)))
325 continue;
326
327 if ((test == SOCK_TEST_TYPE_BI) &&
Florin Coras293aa052018-08-30 18:49:13 -0700328 FD_ISSET (vppcom_session_index (tsock->fd), rfdset) &&
Dave Wallacee4d5a652018-06-24 21:21:21 -0400329 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
330 {
331 (void) vcl_test_read (tsock->fd,
332 (uint8_t *) tsock->rxbuf,
333 tsock->rxbuf_size, &tsock->stats);
334 }
335
Florin Coras293aa052018-08-30 18:49:13 -0700336 if (FD_ISSET (vppcom_session_index (tsock->fd), wfdset)
Florin Coras54693d22018-07-17 10:46:29 -0700337 && (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
Dave Wallacee4d5a652018-06-24 21:21:21 -0400338 {
Florin Coras54693d22018-07-17 10:46:29 -0700339 tx_bytes = vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
340 ctrl->cfg.txbuf_size, &tsock->stats,
341 ctrl->cfg.verbose);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400342 if (tx_bytes < 0)
343 {
344 fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) "
345 "failed -- aborting test!\n", tsock->fd);
346 return;
347 }
348 }
349
350 if (((test == SOCK_TEST_TYPE_UNI) &&
351 (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) ||
352 ((test == SOCK_TEST_TYPE_BI) &&
353 (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)))
354 {
355 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
356 n--;
357 }
358 }
359 }
360 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
361
362 printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
363 ctrl->fd);
364
365 if (sock_test_cfg_sync (ctrl))
366 {
367 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
368 return;
369 }
370
371 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
372 {
373 tsock = &scm->test_socket[i];
374
375 if (ctrl->cfg.verbose)
376 {
377 static char buf[64];
378
379 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
380 sock_test_stats_dump (buf, &tsock->stats,
381 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
382 1 /* show tx */ , ctrl->cfg.verbose);
383 }
384
385 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
386 }
387
388 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
389 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
390 1 /* show tx */ , ctrl->cfg.verbose);
391 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
392
393 if (ctrl->cfg.verbose)
394 {
395 printf (" ctrl socket info\n"
396 SOCK_TEST_SEPARATOR_STRING
397 " fd: %d (0x%08x)\n"
398 " rxbuf: %p\n"
399 " rxbuf size: %u (0x%08x)\n"
400 " txbuf: %p\n"
401 " txbuf size: %u (0x%08x)\n"
402 SOCK_TEST_SEPARATOR_STRING,
403 ctrl->fd, (uint32_t) ctrl->fd,
404 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
405 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
406 }
407
408 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
409 if (sock_test_cfg_sync (ctrl))
410 fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!");
411
412 printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n"
413 SOCK_TEST_BANNER_STRING "\n", ctrl->fd,
414 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
415}
416
417static void
418exit_client (void)
419{
420 sock_client_main_t *scm = &sock_client_main;
421 sock_test_socket_t *ctrl = &scm->ctrl_socket;
422 sock_test_socket_t *tsock;
423 int i;
424
425 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
426 {
427 tsock = &scm->test_socket[i];
428 tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
429
430 /* coverity[COPY_PASTE_ERROR] */
431 if (ctrl->cfg.verbose)
432 {
433 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
434 tsock->fd);
435 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
436 }
437 (void) vcl_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
438 sizeof (tsock->cfg), &tsock->stats,
439 ctrl->cfg.verbose);
440 }
441
442 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
443 if (ctrl->cfg.verbose)
444 {
445 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
446 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
447 }
448 (void) vcl_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
449 sizeof (ctrl->cfg), &ctrl->stats, ctrl->cfg.verbose);
450 printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
451 sleep (1);
452}
453
454static int
455sock_test_connect_test_sockets (uint32_t num_test_sockets)
456{
457 sock_client_main_t *scm = &sock_client_main;
458 sock_test_socket_t *ctrl = &scm->ctrl_socket;
459 sock_test_socket_t *tsock;
460 int i, rv, errno_val;
461
462 if (num_test_sockets < 1)
463 {
464 errno = EINVAL;
465 return -1;
466 }
467
468 if (num_test_sockets < scm->num_test_sockets)
469 {
470 for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
471 {
472 tsock = &scm->test_socket[i];
473 vppcom_session_close (tsock->fd);
474 free (tsock->txbuf);
475 free (tsock->rxbuf);
476 }
477 }
478
479 else if (num_test_sockets > scm->num_test_sockets)
480 {
481 tsock = realloc (scm->test_socket,
482 sizeof (sock_test_socket_t) * num_test_sockets);
483 if (!tsock)
484 {
485 errno_val = errno;
486 perror ("ERROR in sock_test_connect_test_sockets()");
487 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
488 errno_val);
489 return -1;
490 }
491
492 if (!scm->test_socket)
493 memset (tsock, 0, sizeof (*tsock));
494
495 scm->test_socket = tsock;
496 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
497 {
498 tsock = &scm->test_socket[i];
499 tsock->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
500 VPPCOM_PROTO_UDP :
501 VPPCOM_PROTO_TCP,
502 1 /* is_nonblocking */ );
503 if (tsock->fd < 0)
504 {
505 errno = -tsock->fd;
506 tsock->fd = -1;
507 }
508 if (tsock->fd < 0)
509 {
510 errno_val = errno;
511 perror ("ERROR in sock_test_connect_test_sockets()");
512 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
513 errno_val);
514 return tsock->fd;
515 }
516
517 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
518 if (rv)
519 {
520 errno = -rv;
521 rv = -1;
522 }
523 if (rv < 0)
524 {
525 errno_val = errno;
526 perror ("ERROR in sock_test_connect_test_sockets()");
527 fprintf (stderr, "CLIENT: ERROR: connect failed "
528 "(errno = %d)!\n", errno_val);
529 return -1;
530 }
531 tsock->cfg = ctrl->cfg;
532 sock_test_socket_buf_alloc (tsock);
533 sock_test_cfg_sync (tsock);
534
535 printf ("CLIENT (fd %d): Test socket %d connected.\n",
536 tsock->fd, i);
537 }
538 }
539
540 scm->num_test_sockets = num_test_sockets;
541 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
542 return 0;
543}
544
545static void
546dump_help (void)
547{
548#define INDENT "\n "
549
550 printf ("CLIENT: Test configuration commands:"
551 INDENT SOCK_TEST_TOKEN_HELP
552 "\t\t\tDisplay help."
553 INDENT SOCK_TEST_TOKEN_EXIT
554 "\t\t\tExit test client & server."
555 INDENT SOCK_TEST_TOKEN_SHOW_CFG
556 "\t\t\tShow the current test cfg."
557 INDENT SOCK_TEST_TOKEN_RUN_UNI
558 "\t\t\tRun the Uni-directional test."
559 INDENT SOCK_TEST_TOKEN_RUN_BI
560 "\t\t\tRun the Bi-directional test."
561 INDENT SOCK_TEST_TOKEN_VERBOSE
562 "\t\t\tToggle verbose setting."
563 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
564 "<rxbuf size>\tRx buffer size (bytes)."
565 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
566 "<txbuf size>\tTx buffer size (bytes)."
567 INDENT SOCK_TEST_TOKEN_NUM_WRITES
568 "<# of writes>\tNumber of txbuf writes to server." "\n");
569}
570
571static void
572cfg_txbuf_size_set (void)
573{
574 sock_client_main_t *scm = &sock_client_main;
575 sock_test_socket_t *ctrl = &scm->ctrl_socket;
576 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
577 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
578
579 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
580 {
581 ctrl->cfg.txbuf_size = txbuf_size;
582 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
583 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
584 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
585 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
586 }
587 else
588 fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
589 "minimum buf size (%u)!\n",
590 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
591}
592
593static void
594cfg_num_writes_set (void)
595{
596 sock_client_main_t *scm = &sock_client_main;
597 sock_test_socket_t *ctrl = &scm->ctrl_socket;
598 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
599 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
600
601 if (num_writes > 0)
602 {
603 ctrl->cfg.num_writes = num_writes;
604 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
605 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
606 }
607 else
608 {
609 fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
610 }
611}
612
613static void
614cfg_num_test_sockets_set (void)
615{
616 sock_client_main_t *scm = &sock_client_main;
617 sock_test_socket_t *ctrl = &scm->ctrl_socket;
618 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
619 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
620
621 if ((num_test_sockets > 0) &&
622 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
623 {
624 ctrl->cfg.num_test_sockets = num_test_sockets;
625 sock_test_connect_test_sockets (num_test_sockets);
626
627 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
628 }
629 else
630 {
631 fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: "
632 "%u, (%d max)\n",
633 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
634 }
635}
636
637static void
638cfg_rxbuf_size_set (void)
639{
640 sock_client_main_t *scm = &sock_client_main;
641 sock_test_socket_t *ctrl = &scm->ctrl_socket;
642 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
643 uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
644
645 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
646 {
647 ctrl->cfg.rxbuf_size = rxbuf_size;
648 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
649 (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
650 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
651 }
652 else
653 fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
654 "minimum buf size (%u)!\n",
655 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
656}
657
658static void
659cfg_verbose_toggle (void)
660{
661 sock_client_main_t *scm = &sock_client_main;
662 sock_test_socket_t *ctrl = &scm->ctrl_socket;
663
664 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
665 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
666
667}
668
669static sock_test_t
670parse_input ()
671{
672 sock_client_main_t *scm = &sock_client_main;
673 sock_test_socket_t *ctrl = &scm->ctrl_socket;
674 sock_test_t rv = SOCK_TEST_TYPE_NONE;
675
676 if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf,
677 strlen (SOCK_TEST_TOKEN_EXIT)))
678 rv = SOCK_TEST_TYPE_EXIT;
679
680 else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf,
681 strlen (SOCK_TEST_TOKEN_HELP)))
682 dump_help ();
683
684 else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf,
685 strlen (SOCK_TEST_TOKEN_SHOW_CFG)))
686 scm->dump_cfg = 1;
687
688 else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf,
689 strlen (SOCK_TEST_TOKEN_VERBOSE)))
690 cfg_verbose_toggle ();
691
692 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
693 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
694 cfg_txbuf_size_set ();
695
696 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
697 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
698 cfg_num_test_sockets_set ();
699
700 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
701 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
702 cfg_num_writes_set ();
703
704 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
705 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
706 cfg_rxbuf_size_set ();
707
708 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
709 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
710 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
711
712 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
713 strlen (SOCK_TEST_TOKEN_RUN_BI)))
714 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
715
716 else
717 rv = SOCK_TEST_TYPE_ECHO;
718
719 return rv;
720}
721
722void
723print_usage_and_exit (void)
724{
725 fprintf (stderr,
726 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
727 " OPTIONS\n"
728 " -h Print this message and exit.\n"
729 " -6 Use IPv6\n"
730 " -u Use UDP transport layer\n"
731 " -c Print test config before test.\n"
732 " -w <dir> Write test results to <dir>.\n"
733 " -X Exit after running test.\n"
734 " -E Run Echo test.\n"
735 " -N <num-writes> Test Cfg: number of writes.\n"
736 " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
737 " -T <txbuf-size> Test Cfg: tx buffer size.\n"
738 " -U Run Uni-directional test.\n"
739 " -B Run Bi-directional test.\n"
740 " -V Verbose mode.\n");
741 exit (1);
742}
743
744int
745main (int argc, char **argv)
746{
747 sock_client_main_t *scm = &sock_client_main;
748 sock_test_socket_t *ctrl = &scm->ctrl_socket;
749 int c, rv, errno_val;
750 sock_test_t post_test = SOCK_TEST_TYPE_NONE;
751
752 sock_test_cfg_init (&ctrl->cfg);
753 sock_test_socket_buf_alloc (ctrl);
754
755 opterr = 0;
756 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1)
757 switch (c)
758 {
759 case 'c':
760 scm->dump_cfg = 1;
761 break;
762
763 case 's':
764 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
765 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
766 {
767 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
768 "option -%c!\n", c);
769 print_usage_and_exit ();
770 }
771 if (!ctrl->cfg.num_test_sockets ||
772 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
773 {
774 fprintf (stderr, "CLIENT: ERROR: Invalid number of "
775 "sockets (%d) specified for option -%c!\n"
776 " Valid range is 1 - %d\n",
777 ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
778 print_usage_and_exit ();
779 }
780 break;
781
782 case 'w':
783 fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n");
784 break;
785
786 case 'X':
787 post_test = SOCK_TEST_TYPE_EXIT;
788 break;
789
790 case 'E':
791 if (strlen (optarg) > ctrl->txbuf_size)
792 {
793 fprintf (stderr, "CLIENT: ERROR: Option -%c value "
794 "larger than txbuf size (%d)!\n",
795 optopt, ctrl->txbuf_size);
796 print_usage_and_exit ();
797 }
798 strcpy (ctrl->txbuf, optarg);
799 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
800 break;
801
802 case 'I':
803 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
804 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
805 {
806 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
807 "option -%c!\n", c);
808 print_usage_and_exit ();
809 }
810 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
811 {
812 fprintf (stderr, "CLIENT: ERROR: value greater than max "
813 "number test sockets (%d)!",
814 SOCK_TEST_CFG_MAX_TEST_SCKTS);
815 print_usage_and_exit ();
816 }
817 break;
818
819 case 'N':
820 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
821 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
822 {
823 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
824 "option -%c!\n", c);
825 print_usage_and_exit ();
826 }
827 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
828 break;
829
830 case 'R':
831 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
832 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
833 {
834 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
835 "option -%c!\n", c);
836 print_usage_and_exit ();
837 }
838 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
839 {
840 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
841 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
842 (uint8_t **) & ctrl->rxbuf,
843 &ctrl->rxbuf_size);
844 }
845 else
846 {
847 fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) "
848 "less than minumum (%u)\n",
849 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
850 print_usage_and_exit ();
851 }
852
853 break;
854
855 case 'T':
856 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
857 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
858 {
859 fprintf (stderr, "CLIENT: ERROR: Invalid value "
860 "for option -%c!\n", c);
861 print_usage_and_exit ();
862 }
863 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
864 {
865 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
866 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
867 (uint8_t **) & ctrl->txbuf,
868 &ctrl->txbuf_size);
869 ctrl->cfg.total_bytes =
870 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
871 }
872 else
873 {
874 fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) "
875 "less than minumum (%u)!\n",
876 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
877 print_usage_and_exit ();
878 }
879 break;
880
881 case 'U':
882 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
883 break;
884
885 case 'B':
886 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
887 break;
888
889 case 'V':
890 ctrl->cfg.verbose = 1;
891 break;
892
893 case '6':
894 ctrl->cfg.address_ip6 = 1;
895 break;
896
897 case 'D':
898 ctrl->cfg.transport_udp = 1;
899 break;
900
901 case '?':
902 switch (optopt)
903 {
904 case 'E':
905 case 'I':
906 case 'N':
907 case 'R':
908 case 'T':
909 case 'w':
910 fprintf (stderr, "CLIENT: ERROR: Option -%c "
911 "requires an argument.\n", optopt);
912 break;
913
914 default:
915 if (isprint (optopt))
916 fprintf (stderr, "CLIENT: ERROR: Unknown "
917 "option `-%c'.\n", optopt);
918 else
919 fprintf (stderr, "CLIENT: ERROR: Unknown "
920 "option character `\\x%x'.\n", optopt);
921 }
922 /* fall thru */
923 case 'h':
924 default:
925 print_usage_and_exit ();
926 }
927
928 if (argc < (optind + 2))
929 {
930 fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n");
931 print_usage_and_exit ();
932 }
933
934 ctrl->fd = vppcom_app_create ("vcl_test_client");
935 if (ctrl->fd < 0)
936 {
937 errno = -ctrl->fd;
938 ctrl->fd = -1;
939 }
940 else
941 {
942 ctrl->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
943 VPPCOM_PROTO_UDP :
944 VPPCOM_PROTO_TCP,
945 0 /* is_nonblocking */ );
946 if (ctrl->fd < 0)
947 {
948 errno = -ctrl->fd;
949 ctrl->fd = -1;
950 }
951 }
952
953 if (ctrl->fd < 0)
954 {
955 errno_val = errno;
956 perror ("ERROR in main()");
957 fprintf (stderr, "CLIENT: ERROR: socket "
958 "failed (errno = %d)!\n", errno_val);
959 return ctrl->fd;
960 }
961
962 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
963 if (ctrl->cfg.address_ip6)
964 {
965 struct sockaddr_in6 *server_addr =
966 (struct sockaddr_in6 *) &scm->server_addr;
967 scm->server_addr_size = sizeof (*server_addr);
968 server_addr->sin6_family = AF_INET6;
969 inet_pton (AF_INET6, argv[optind++], &(server_addr->sin6_addr));
970 server_addr->sin6_port = htons (atoi (argv[optind]));
971 }
972 else
973 {
974 struct sockaddr_in *server_addr =
975 (struct sockaddr_in *) &scm->server_addr;
976 scm->server_addr_size = sizeof (*server_addr);
977 server_addr->sin_family = AF_INET;
978 inet_pton (AF_INET, argv[optind++], &(server_addr->sin_addr));
979 server_addr->sin_port = htons (atoi (argv[optind]));
980 }
981
982 if (ctrl->cfg.address_ip6)
983 {
984 struct sockaddr_in6 *server_addr =
985 (struct sockaddr_in6 *) &scm->server_addr;
986 scm->server_endpt.is_ip4 = 0;
987 scm->server_endpt.ip = (uint8_t *) & server_addr->sin6_addr;
988 scm->server_endpt.port = (uint16_t) server_addr->sin6_port;
989 }
990 else
991 {
992 struct sockaddr_in *server_addr =
993 (struct sockaddr_in *) &scm->server_addr;
994 scm->server_endpt.is_ip4 = 1;
995 scm->server_endpt.ip = (uint8_t *) & server_addr->sin_addr;
996 scm->server_endpt.port = (uint16_t) server_addr->sin_port;
997 }
998
999 do
1000 {
1001 printf ("\nCLIENT: Connecting to server...\n");
1002
1003 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
1004 if (rv)
1005 {
1006 errno = -rv;
1007 rv = -1;
1008 }
1009 if (rv < 0)
1010 {
1011 errno_val = errno;
1012 perror ("ERROR in main()");
1013 fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n",
1014 errno_val);
1015 return -1;
1016 }
1017
1018 sock_test_cfg_sync (ctrl);
1019 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
1020 }
1021 while (rv < 0);
1022
1023 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
1024
1025 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
1026 {
1027 if (scm->dump_cfg)
1028 {
1029 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
1030 scm->dump_cfg = 0;
1031 }
1032
1033 switch (ctrl->cfg.test)
1034 {
1035 case SOCK_TEST_TYPE_ECHO:
1036 echo_test_client ();
1037 break;
1038
1039 case SOCK_TEST_TYPE_UNI:
1040 case SOCK_TEST_TYPE_BI:
1041 stream_test_client (ctrl->cfg.test);
1042 break;
1043
1044 case SOCK_TEST_TYPE_EXIT:
1045 continue;
1046
1047 case SOCK_TEST_TYPE_NONE:
1048 default:
1049 break;
1050 }
1051 switch (post_test)
1052 {
1053 case SOCK_TEST_TYPE_EXIT:
1054 switch (ctrl->cfg.test)
1055 {
1056 case SOCK_TEST_TYPE_EXIT:
1057 case SOCK_TEST_TYPE_UNI:
1058 case SOCK_TEST_TYPE_BI:
1059 case SOCK_TEST_TYPE_ECHO:
1060 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1061 continue;
1062
1063 case SOCK_TEST_TYPE_NONE:
1064 default:
1065 break;
1066 }
1067 break;
1068
1069 case SOCK_TEST_TYPE_NONE:
1070 case SOCK_TEST_TYPE_ECHO:
1071 case SOCK_TEST_TYPE_UNI:
1072 case SOCK_TEST_TYPE_BI:
1073 default:
1074 break;
1075 }
1076
1077 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1078 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1079
1080 printf ("\nCLIENT: Type some characters and hit <return>\n"
1081 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1082
1083 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1084 {
1085 if (strlen (ctrl->txbuf) == 1)
1086 {
1087 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1088 continue;
1089 }
1090 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1091
1092 /* Parse input for keywords */
1093 ctrl->cfg.test = parse_input ();
1094 }
1095 }
1096
1097 exit_client ();
1098 vppcom_session_close (ctrl->fd);
1099 vppcom_app_destroy ();
1100 return 0;
1101}
Dave Wallace543852a2017-08-03 02:11:34 -04001102
1103/*
1104 * fd.io coding-style-patch-verification: ON
1105 *
1106 * Local Variables:
1107 * eval: (c-set-style "gnu")
1108 * End:
1109 */