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