2 * Copyright (c) 2017-2018 Cisco and/or its affiliates.
3 * 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
21 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <vcl/vcl_test.h>
29 vppcom_endpt_t server_endpt;
30 struct sockaddr_storage server_addr;
31 uint32_t server_addr_size;
33 sock_test_socket_t ctrl_socket;
34 sock_test_socket_t *test_socket;
35 uint32_t num_test_sockets;
39 sock_client_main_t sock_client_main;
43 sock_test_cfg_sync (sock_test_socket_t * socket)
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;
50 if (socket->cfg.verbose)
51 sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ );
53 ctrl->cfg.seq_num = ++scm->cfg_seq_num;
54 if (socket->cfg.verbose)
56 printf ("CLIENT (fd %d): Sending config sent to server.\n", socket->fd);
57 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
59 tx_bytes = vcl_test_write (socket->fd, (uint8_t *) & ctrl->cfg,
60 sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose);
63 fprintf (stderr, "CLIENT (fd %d): ERROR: write test cfg failed (%d)!\n",
64 socket->fd, tx_bytes);
68 rx_bytes = vcl_test_read (socket->fd, (uint8_t *) socket->rxbuf,
69 sizeof (sock_test_cfg_t), NULL);
73 if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC)
75 fprintf (stderr, "CLIENT (fd %d): ERROR: Bad server reply cfg "
76 "-- aborting!\n", socket->fd);
79 if ((rx_bytes != sizeof (sock_test_cfg_t))
80 || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg))
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))
86 fprintf (stderr, "\tRx bytes %d != cfg size %lu\n",
87 rx_bytes, sizeof (sock_test_cfg_t));
91 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
92 fprintf (stderr, "CLIENT (fd %d): Valid config sent to server.\n",
94 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
98 else if (socket->cfg.verbose)
100 printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd);
101 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
103 ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ?
104 rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle);
112 sock_client_main_t *scm = &sock_client_main;
113 sock_test_socket_t *ctrl = &scm->ctrl_socket;
114 sock_test_socket_t *tsock;
115 int rx_bytes, tx_bytes, nbytes;
119 fd_set wr_fdset, rd_fdset;
120 fd_set _wfdset, *wfdset = &_wfdset;
121 fd_set _rfdset, *rfdset = &_rfdset;
125 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
126 ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1;
127 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
129 tsock = &scm->test_socket[n];
130 tsock->cfg = ctrl->cfg;
131 sock_test_socket_buf_alloc (tsock);
132 if (sock_test_cfg_sync (tsock))
135 memcpy (tsock->txbuf, ctrl->txbuf, nbytes);
136 memset (&tsock->stats, 0, sizeof (tsock->stats));
138 FD_SET (tsock->fd, &wr_fdset);
139 FD_SET (tsock->fd, &rd_fdset);
140 nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
144 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
150 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
155 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
162 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
164 tsock = &scm->test_socket[i];
165 if (!((tsock->stats.stop.tv_sec == 0) &&
166 (tsock->stats.stop.tv_nsec == 0)))
169 if (FD_ISSET (tsock->fd, wfdset) &&
170 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
174 vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes,
175 &tsock->stats, ctrl->cfg.verbose);
178 fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) "
179 "failed -- aborting test!\n", tsock->fd);
183 printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n",
184 tsock->fd, tx_bytes, tsock->txbuf);
187 if ((FD_ISSET (tsock->fd, rfdset)) &&
188 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
191 vcl_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
192 nbytes, &tsock->stats);
195 printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n",
196 tsock->fd, rx_bytes, tsock->rxbuf);
198 if (tsock->stats.rx_bytes != tsock->stats.tx_bytes)
199 printf ("CLIENT: WARNING: bytes read (%lu) "
200 "!= bytes written (%lu)!\n",
201 tsock->stats.rx_bytes, tsock->stats.tx_bytes);
205 if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)
207 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
212 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
214 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
216 tsock = &scm->test_socket[i];
217 tsock->stats.start = ctrl->stats.start;
219 if (ctrl->cfg.verbose)
223 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
224 sock_test_stats_dump (buf, &tsock->stats,
225 1 /* show_rx */ , 1 /* show tx */ ,
229 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
232 if (ctrl->cfg.verbose)
234 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
235 1 /* show_rx */ , 1 /* show tx */ ,
237 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
239 if (ctrl->cfg.verbose > 1)
241 printf (" ctrl socket info\n"
242 SOCK_TEST_SEPARATOR_STRING
245 " rxbuf size: %u (0x%08x)\n"
247 " txbuf size: %u (0x%08x)\n"
248 SOCK_TEST_SEPARATOR_STRING,
249 ctrl->fd, (uint32_t) ctrl->fd,
250 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
251 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
257 stream_test_client (sock_test_t test)
259 sock_client_main_t *scm = &sock_client_main;
260 sock_test_socket_t *ctrl = &scm->ctrl_socket;
261 sock_test_socket_t *tsock;
266 fd_set wr_fdset, rd_fdset;
267 fd_set _wfdset, *wfdset = &_wfdset;
268 fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0;
270 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
271 ctrl->cfg.ctrl_handle = ~0;
273 printf ("\n" SOCK_TEST_BANNER_STRING
274 "CLIENT (fd %d): %s-directional Stream Test!\n\n"
275 "CLIENT (fd %d): Sending config to server on ctrl socket...\n",
276 ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd);
278 if (sock_test_cfg_sync (ctrl))
280 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
286 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
287 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
289 tsock = &scm->test_socket[n];
290 tsock->cfg = ctrl->cfg;
291 sock_test_socket_buf_alloc (tsock);
292 printf ("CLIENT (fd %d): Sending config to server on "
293 "test socket %d...\n", tsock->fd, n);
294 sock_test_cfg_sync (tsock);
296 /* Fill payload with incrementing uint32's */
297 for (i = 0; i < tsock->txbuf_size; i++)
298 tsock->txbuf[i] = i & 0xff;
300 memset (&tsock->stats, 0, sizeof (tsock->stats));
301 FD_SET (tsock->fd, &wr_fdset);
302 FD_SET (tsock->fd, &rd_fdset);
303 nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
307 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
313 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
318 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
325 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
327 tsock = &scm->test_socket[i];
328 if (!((tsock->stats.stop.tv_sec == 0) &&
329 (tsock->stats.stop.tv_nsec == 0)))
332 if ((test == SOCK_TEST_TYPE_BI) &&
333 FD_ISSET (tsock->fd, rfdset) &&
334 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
336 (void) vcl_test_read (tsock->fd,
337 (uint8_t *) tsock->rxbuf,
338 tsock->rxbuf_size, &tsock->stats);
341 if (FD_ISSET (tsock->fd, wfdset) &&
342 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
345 vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
346 ctrl->cfg.txbuf_size, &tsock->stats,
350 fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) "
351 "failed -- aborting test!\n", tsock->fd);
356 if (((test == SOCK_TEST_TYPE_UNI) &&
357 (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) ||
358 ((test == SOCK_TEST_TYPE_BI) &&
359 (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)))
361 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
366 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
368 printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
371 if (sock_test_cfg_sync (ctrl))
373 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
377 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
379 tsock = &scm->test_socket[i];
381 if (ctrl->cfg.verbose)
385 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
386 sock_test_stats_dump (buf, &tsock->stats,
387 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
388 1 /* show tx */ , ctrl->cfg.verbose);
391 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
394 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
395 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
396 1 /* show tx */ , ctrl->cfg.verbose);
397 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
399 if (ctrl->cfg.verbose)
401 printf (" ctrl socket info\n"
402 SOCK_TEST_SEPARATOR_STRING
405 " rxbuf size: %u (0x%08x)\n"
407 " txbuf size: %u (0x%08x)\n"
408 SOCK_TEST_SEPARATOR_STRING,
409 ctrl->fd, (uint32_t) ctrl->fd,
410 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
411 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
414 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
415 if (sock_test_cfg_sync (ctrl))
416 fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!");
418 printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n"
419 SOCK_TEST_BANNER_STRING "\n", ctrl->fd,
420 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
426 sock_client_main_t *scm = &sock_client_main;
427 sock_test_socket_t *ctrl = &scm->ctrl_socket;
428 sock_test_socket_t *tsock;
431 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
433 tsock = &scm->test_socket[i];
434 tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
436 /* coverity[COPY_PASTE_ERROR] */
437 if (ctrl->cfg.verbose)
439 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
441 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
443 (void) vcl_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
444 sizeof (tsock->cfg), &tsock->stats,
448 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
449 if (ctrl->cfg.verbose)
451 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
452 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
454 (void) vcl_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
455 sizeof (ctrl->cfg), &ctrl->stats, ctrl->cfg.verbose);
456 printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
461 sock_test_connect_test_sockets (uint32_t num_test_sockets)
463 sock_client_main_t *scm = &sock_client_main;
464 sock_test_socket_t *ctrl = &scm->ctrl_socket;
465 sock_test_socket_t *tsock;
466 int i, rv, errno_val;
468 if (num_test_sockets < 1)
474 if (num_test_sockets < scm->num_test_sockets)
476 for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
478 tsock = &scm->test_socket[i];
479 vppcom_session_close (tsock->fd);
485 else if (num_test_sockets > scm->num_test_sockets)
487 tsock = realloc (scm->test_socket,
488 sizeof (sock_test_socket_t) * num_test_sockets);
492 perror ("ERROR in sock_test_connect_test_sockets()");
493 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
498 if (!scm->test_socket)
499 memset (tsock, 0, sizeof (*tsock));
501 scm->test_socket = tsock;
502 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
504 tsock = &scm->test_socket[i];
505 tsock->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
508 1 /* is_nonblocking */ );
517 perror ("ERROR in sock_test_connect_test_sockets()");
518 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
523 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
532 perror ("ERROR in sock_test_connect_test_sockets()");
533 fprintf (stderr, "CLIENT: ERROR: connect failed "
534 "(errno = %d)!\n", errno_val);
537 tsock->cfg = ctrl->cfg;
538 sock_test_socket_buf_alloc (tsock);
539 sock_test_cfg_sync (tsock);
541 printf ("CLIENT (fd %d): Test socket %d connected.\n",
546 scm->num_test_sockets = num_test_sockets;
547 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
556 printf ("CLIENT: Test configuration commands:"
557 INDENT SOCK_TEST_TOKEN_HELP
558 "\t\t\tDisplay help."
559 INDENT SOCK_TEST_TOKEN_EXIT
560 "\t\t\tExit test client & server."
561 INDENT SOCK_TEST_TOKEN_SHOW_CFG
562 "\t\t\tShow the current test cfg."
563 INDENT SOCK_TEST_TOKEN_RUN_UNI
564 "\t\t\tRun the Uni-directional test."
565 INDENT SOCK_TEST_TOKEN_RUN_BI
566 "\t\t\tRun the Bi-directional test."
567 INDENT SOCK_TEST_TOKEN_VERBOSE
568 "\t\t\tToggle verbose setting."
569 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
570 "<rxbuf size>\tRx buffer size (bytes)."
571 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
572 "<txbuf size>\tTx buffer size (bytes)."
573 INDENT SOCK_TEST_TOKEN_NUM_WRITES
574 "<# of writes>\tNumber of txbuf writes to server." "\n");
578 cfg_txbuf_size_set (void)
580 sock_client_main_t *scm = &sock_client_main;
581 sock_test_socket_t *ctrl = &scm->ctrl_socket;
582 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
583 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
585 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
587 ctrl->cfg.txbuf_size = txbuf_size;
588 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
589 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
590 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
591 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
594 fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
595 "minimum buf size (%u)!\n",
596 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
600 cfg_num_writes_set (void)
602 sock_client_main_t *scm = &sock_client_main;
603 sock_test_socket_t *ctrl = &scm->ctrl_socket;
604 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
605 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
609 ctrl->cfg.num_writes = num_writes;
610 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
611 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
615 fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
620 cfg_num_test_sockets_set (void)
622 sock_client_main_t *scm = &sock_client_main;
623 sock_test_socket_t *ctrl = &scm->ctrl_socket;
624 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
625 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
627 if ((num_test_sockets > 0) &&
628 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
630 ctrl->cfg.num_test_sockets = num_test_sockets;
631 sock_test_connect_test_sockets (num_test_sockets);
633 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
637 fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: "
639 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
644 cfg_rxbuf_size_set (void)
646 sock_client_main_t *scm = &sock_client_main;
647 sock_test_socket_t *ctrl = &scm->ctrl_socket;
648 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
649 uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
651 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
653 ctrl->cfg.rxbuf_size = rxbuf_size;
654 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
655 (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
656 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
659 fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
660 "minimum buf size (%u)!\n",
661 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
665 cfg_verbose_toggle (void)
667 sock_client_main_t *scm = &sock_client_main;
668 sock_test_socket_t *ctrl = &scm->ctrl_socket;
670 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
671 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
678 sock_client_main_t *scm = &sock_client_main;
679 sock_test_socket_t *ctrl = &scm->ctrl_socket;
680 sock_test_t rv = SOCK_TEST_TYPE_NONE;
682 if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf,
683 strlen (SOCK_TEST_TOKEN_EXIT)))
684 rv = SOCK_TEST_TYPE_EXIT;
686 else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf,
687 strlen (SOCK_TEST_TOKEN_HELP)))
690 else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf,
691 strlen (SOCK_TEST_TOKEN_SHOW_CFG)))
694 else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf,
695 strlen (SOCK_TEST_TOKEN_VERBOSE)))
696 cfg_verbose_toggle ();
698 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
699 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
700 cfg_txbuf_size_set ();
702 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
703 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
704 cfg_num_test_sockets_set ();
706 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
707 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
708 cfg_num_writes_set ();
710 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
711 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
712 cfg_rxbuf_size_set ();
714 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
715 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
716 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
718 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
719 strlen (SOCK_TEST_TOKEN_RUN_BI)))
720 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
723 rv = SOCK_TEST_TYPE_ECHO;
729 print_usage_and_exit (void)
732 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
734 " -h Print this message and exit.\n"
736 " -u Use UDP transport layer\n"
737 " -c Print test config before test.\n"
738 " -w <dir> Write test results to <dir>.\n"
739 " -X Exit after running test.\n"
740 " -E Run Echo test.\n"
741 " -N <num-writes> Test Cfg: number of writes.\n"
742 " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
743 " -T <txbuf-size> Test Cfg: tx buffer size.\n"
744 " -U Run Uni-directional test.\n"
745 " -B Run Bi-directional test.\n"
746 " -V Verbose mode.\n");
751 main (int argc, char **argv)
753 sock_client_main_t *scm = &sock_client_main;
754 sock_test_socket_t *ctrl = &scm->ctrl_socket;
755 int c, rv, errno_val;
756 sock_test_t post_test = SOCK_TEST_TYPE_NONE;
758 sock_test_cfg_init (&ctrl->cfg);
759 sock_test_socket_buf_alloc (ctrl);
762 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1)
770 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
771 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
773 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
775 print_usage_and_exit ();
777 if (!ctrl->cfg.num_test_sockets ||
778 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
780 fprintf (stderr, "CLIENT: ERROR: Invalid number of "
781 "sockets (%d) specified for option -%c!\n"
782 " Valid range is 1 - %d\n",
783 ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
784 print_usage_and_exit ();
789 fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n");
793 post_test = SOCK_TEST_TYPE_EXIT;
797 if (strlen (optarg) > ctrl->txbuf_size)
799 fprintf (stderr, "CLIENT: ERROR: Option -%c value "
800 "larger than txbuf size (%d)!\n",
801 optopt, ctrl->txbuf_size);
802 print_usage_and_exit ();
804 strcpy (ctrl->txbuf, optarg);
805 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
809 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
810 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
812 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
814 print_usage_and_exit ();
816 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
818 fprintf (stderr, "CLIENT: ERROR: value greater than max "
819 "number test sockets (%d)!",
820 SOCK_TEST_CFG_MAX_TEST_SCKTS);
821 print_usage_and_exit ();
826 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
827 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
829 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
831 print_usage_and_exit ();
833 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
837 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
838 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
840 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
842 print_usage_and_exit ();
844 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
846 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
847 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
848 (uint8_t **) & ctrl->rxbuf,
853 fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) "
854 "less than minumum (%u)\n",
855 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
856 print_usage_and_exit ();
862 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
863 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
865 fprintf (stderr, "CLIENT: ERROR: Invalid value "
866 "for option -%c!\n", c);
867 print_usage_and_exit ();
869 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
871 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
872 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
873 (uint8_t **) & ctrl->txbuf,
875 ctrl->cfg.total_bytes =
876 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
880 fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) "
881 "less than minumum (%u)!\n",
882 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
883 print_usage_and_exit ();
888 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
892 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
896 ctrl->cfg.verbose = 1;
900 ctrl->cfg.address_ip6 = 1;
904 ctrl->cfg.transport_udp = 1;
916 fprintf (stderr, "CLIENT: ERROR: Option -%c "
917 "requires an argument.\n", optopt);
921 if (isprint (optopt))
922 fprintf (stderr, "CLIENT: ERROR: Unknown "
923 "option `-%c'.\n", optopt);
925 fprintf (stderr, "CLIENT: ERROR: Unknown "
926 "option character `\\x%x'.\n", optopt);
931 print_usage_and_exit ();
934 if (argc < (optind + 2))
936 fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n");
937 print_usage_and_exit ();
940 ctrl->fd = vppcom_app_create ("vcl_test_client");
948 ctrl->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
951 0 /* is_nonblocking */ );
962 perror ("ERROR in main()");
963 fprintf (stderr, "CLIENT: ERROR: socket "
964 "failed (errno = %d)!\n", errno_val);
968 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
969 if (ctrl->cfg.address_ip6)
971 struct sockaddr_in6 *server_addr =
972 (struct sockaddr_in6 *) &scm->server_addr;
973 scm->server_addr_size = sizeof (*server_addr);
974 server_addr->sin6_family = AF_INET6;
975 inet_pton (AF_INET6, argv[optind++], &(server_addr->sin6_addr));
976 server_addr->sin6_port = htons (atoi (argv[optind]));
980 struct sockaddr_in *server_addr =
981 (struct sockaddr_in *) &scm->server_addr;
982 scm->server_addr_size = sizeof (*server_addr);
983 server_addr->sin_family = AF_INET;
984 inet_pton (AF_INET, argv[optind++], &(server_addr->sin_addr));
985 server_addr->sin_port = htons (atoi (argv[optind]));
988 if (ctrl->cfg.address_ip6)
990 struct sockaddr_in6 *server_addr =
991 (struct sockaddr_in6 *) &scm->server_addr;
992 scm->server_endpt.is_ip4 = 0;
993 scm->server_endpt.ip = (uint8_t *) & server_addr->sin6_addr;
994 scm->server_endpt.port = (uint16_t) server_addr->sin6_port;
998 struct sockaddr_in *server_addr =
999 (struct sockaddr_in *) &scm->server_addr;
1000 scm->server_endpt.is_ip4 = 1;
1001 scm->server_endpt.ip = (uint8_t *) & server_addr->sin_addr;
1002 scm->server_endpt.port = (uint16_t) server_addr->sin_port;
1007 printf ("\nCLIENT: Connecting to server...\n");
1009 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
1018 perror ("ERROR in main()");
1019 fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n",
1024 sock_test_cfg_sync (ctrl);
1025 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
1029 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
1031 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
1035 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
1039 switch (ctrl->cfg.test)
1041 case SOCK_TEST_TYPE_ECHO:
1042 echo_test_client ();
1045 case SOCK_TEST_TYPE_UNI:
1046 case SOCK_TEST_TYPE_BI:
1047 stream_test_client (ctrl->cfg.test);
1050 case SOCK_TEST_TYPE_EXIT:
1053 case SOCK_TEST_TYPE_NONE:
1059 case SOCK_TEST_TYPE_EXIT:
1060 switch (ctrl->cfg.test)
1062 case SOCK_TEST_TYPE_EXIT:
1063 case SOCK_TEST_TYPE_UNI:
1064 case SOCK_TEST_TYPE_BI:
1065 case SOCK_TEST_TYPE_ECHO:
1066 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1069 case SOCK_TEST_TYPE_NONE:
1075 case SOCK_TEST_TYPE_NONE:
1076 case SOCK_TEST_TYPE_ECHO:
1077 case SOCK_TEST_TYPE_UNI:
1078 case SOCK_TEST_TYPE_BI:
1083 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1084 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1086 printf ("\nCLIENT: Type some characters and hit <return>\n"
1087 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1089 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1091 if (strlen (ctrl->txbuf) == 1)
1093 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1096 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1098 /* Parse input for keywords */
1099 ctrl->cfg.test = parse_input ();
1104 vppcom_session_close (ctrl->fd);
1105 vppcom_app_destroy ();
1110 * fd.io coding-style-patch-verification: ON
1113 * eval: (c-set-style "gnu")