blob: 40ba043adaeaacdc16fbf83a1d9a15fccc703025 [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>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040025#include <vcl/sock_test.h>
Dave Wallace543852a2017-08-03 02:11:34 -040026
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
Chris Lukeb2bcad62017-09-18 08:51:22 -0400432 /* coverity[COPY_PASTE_ERROR] */
Dave Wallace543852a2017-08-03 02:11:34 -0400433 if (ctrl->cfg.verbose)
434 {
435 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
436 tsock->fd);
437 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
438 }
439 (void) sock_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
440 sizeof (tsock->cfg), &tsock->stats,
441 ctrl->cfg.verbose);
442 }
443
444 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
445 if (ctrl->cfg.verbose)
446 {
447 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
448 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
449 }
450 (void) sock_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
451 sizeof (ctrl->cfg), &ctrl->stats,
452 ctrl->cfg.verbose);
453 printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
454 sleep (1);
455}
456
457static int
458sock_test_connect_test_sockets (uint32_t num_test_sockets)
459{
460 sock_client_main_t *scm = &sock_client_main;
461 sock_test_socket_t *ctrl = &scm->ctrl_socket;
462 sock_test_socket_t *tsock;
463 int i, rv, errno_val;
464
465 if (num_test_sockets < 1)
466 {
467 errno = EINVAL;
468 return -1;
469 }
470
471 if (num_test_sockets < scm->num_test_sockets)
472 {
473 for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
474 {
475 tsock = &scm->test_socket[i];
476#ifdef VCL_TEST
477 vppcom_session_close (tsock->fd);
478#else
479 close (tsock->fd);
480#endif
481 free (tsock->txbuf);
482 free (tsock->rxbuf);
483 }
484 }
485
486 else if (num_test_sockets > scm->num_test_sockets)
487 {
488 tsock = realloc (scm->test_socket,
489 sizeof (sock_test_socket_t) * num_test_sockets);
490 if (!tsock)
491 {
492 errno_val = errno;
493 perror ("ERROR in sock_test_connect_test_sockets()");
494 fprintf (stderr, "ERROR: socket failed (errno = %d)!\n", errno_val);
495 return -1;
496 }
497
Dave Wallaced48e9762017-08-22 23:29:33 -0400498 if (!scm->test_socket)
499 memset (tsock, 0, sizeof (*tsock));
500
Dave Wallace543852a2017-08-03 02:11:34 -0400501 scm->test_socket = tsock;
502 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
503 {
504 tsock = &scm->test_socket[i];
505#ifdef VCL_TEST
506 tsock->fd =
507 vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP,
Dave Wallacef7f809c2017-10-03 01:48:42 -0400508 1 /* is_nonblocking */ );
Dave Wallace543852a2017-08-03 02:11:34 -0400509 if (tsock->fd < 0)
510 {
511 errno = -tsock->fd;
512 tsock->fd = -1;
513 }
514#else
515 tsock->fd = socket (AF_INET, SOCK_STREAM, 0);
516#endif
517 if (tsock->fd < 0)
518 {
519 errno_val = errno;
520 perror ("ERROR in sock_test_connect_test_sockets()");
521 fprintf (stderr, "ERROR: socket failed (errno = %d)!\n",
522 errno_val);
523 return tsock->fd;
524 }
525
526#ifdef VCL_TEST
527 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
528#else
529 rv =
530 connect (tsock->fd, (struct sockaddr *) &scm->server_addr,
531 sizeof (scm->server_addr));
532#endif
533 if (rv < 0)
534 {
535 errno_val = errno;
536 perror ("ERROR in main()");
537 fprintf (stderr, "ERROR: connect failed (errno = %d)!\n",
538 errno_val);
539 }
540 tsock->cfg = ctrl->cfg;
541 sock_test_socket_buf_alloc (tsock);
542 sock_test_cfg_sync (tsock);
543
544 printf ("CLIENT (fd %d): Test socket %d connected.\n",
545 tsock->fd, i);
546 }
547 }
548
549 scm->num_test_sockets = num_test_sockets;
550 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
551 return 0;
552}
553
554static void
555dump_help (void)
556{
557#define INDENT "\n "
558
559 printf ("Test configuration commands:"
560 INDENT SOCK_TEST_TOKEN_HELP
561 "\t\t\tDisplay help."
562 INDENT SOCK_TEST_TOKEN_EXIT
563 "\t\t\tExit test client & server."
564 INDENT SOCK_TEST_TOKEN_SHOW_CFG
565 "\t\t\tShow the current test cfg."
566 INDENT SOCK_TEST_TOKEN_RUN_UNI
567 "\t\t\tRun the Uni-directional test."
568 INDENT SOCK_TEST_TOKEN_RUN_BI
569 "\t\t\tRun the Bi-directional test."
570 INDENT SOCK_TEST_TOKEN_VERBOSE
571 "\t\t\tToggle verbose setting."
572 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
573 "<rxbuf size>\tRx buffer size (bytes)."
574 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
575 "<txbuf size>\tTx buffer size (bytes)."
576 INDENT SOCK_TEST_TOKEN_NUM_WRITES
577 "<# of writes>\tNumber of txbuf writes to server." "\n");
578}
579
580static void
581cfg_txbuf_size_set (void)
582{
583 sock_client_main_t *scm = &sock_client_main;
584 sock_test_socket_t *ctrl = &scm->ctrl_socket;
585 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
586 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
587
588 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
589 {
590 ctrl->cfg.txbuf_size = txbuf_size;
591 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
592 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
593 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
594 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
595 }
596 else
597 fprintf (stderr,
598 "ERROR: Invalid txbuf size (%lu) < minimum buf size (%u)!\n",
599 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
600}
601
602static void
603cfg_num_writes_set (void)
604{
605 sock_client_main_t *scm = &sock_client_main;
606 sock_test_socket_t *ctrl = &scm->ctrl_socket;
607 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
608 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
609
610 if (num_writes > 0)
611 {
612 ctrl->cfg.num_writes = num_writes;
613 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
614 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
615 }
616 else
617 {
618 fprintf (stderr, "ERROR: invalid num writes: %u\n", num_writes);
619 }
620}
621
622static void
623cfg_num_test_sockets_set (void)
624{
625 sock_client_main_t *scm = &sock_client_main;
626 sock_test_socket_t *ctrl = &scm->ctrl_socket;
627 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
628 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
629
630 if ((num_test_sockets > 0) &&
631 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
632 {
633 ctrl->cfg.num_test_sockets = num_test_sockets;
634 sock_test_connect_test_sockets (num_test_sockets);
635 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
636 }
637 else
638 {
639 fprintf (stderr, "ERROR: invalid num test sockets: %u, (%d max)\n",
640 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
641 }
642}
643
644static void
645cfg_rxbuf_size_set (void)
646{
647 sock_client_main_t *scm = &sock_client_main;
648 sock_test_socket_t *ctrl = &scm->ctrl_socket;
649 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
650 uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
651
652 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
653 {
654 ctrl->cfg.rxbuf_size = rxbuf_size;
655 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
656 (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
657 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
658 }
659 else
660 fprintf (stderr,
661 "ERROR: Invalid rxbuf size (%lu) < minimum buf size (%u)!\n",
662 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
663}
664
665static void
666cfg_verbose_toggle (void)
667{
668 sock_client_main_t *scm = &sock_client_main;
669 sock_test_socket_t *ctrl = &scm->ctrl_socket;
670
671 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
672 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
673
674}
675
676static sock_test_t
677parse_input ()
678{
679 sock_client_main_t *scm = &sock_client_main;
680 sock_test_socket_t *ctrl = &scm->ctrl_socket;
681 sock_test_t rv = SOCK_TEST_TYPE_NONE;
682
683 if (!strcmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf))
684 rv = SOCK_TEST_TYPE_EXIT;
685
686 else if (!strcmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf))
687 dump_help ();
688
689 else if (!strcmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf))
690 scm->dump_cfg = 1;
691
692 else if (!strcmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf))
693 cfg_verbose_toggle ();
694
695 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
696 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
697 cfg_txbuf_size_set ();
698
699 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
700 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
701 cfg_num_test_sockets_set ();
702
703 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
704 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
705 cfg_num_writes_set ();
706
707 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
708 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
709 cfg_rxbuf_size_set ();
710
711 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
712 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
713 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
714
715 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
716 strlen (SOCK_TEST_TOKEN_RUN_BI)))
717 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
718
719 else
720 rv = SOCK_TEST_TYPE_ECHO;
721
722 return rv;
723}
724
725void
726print_usage_and_exit (void)
727{
728 fprintf (stderr,
729 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
730 " OPTIONS\n"
731 " -h Print this message and exit.\n"
732 " -c Print test config before test.\n"
733 " -w <dir> Write test results to <dir>.\n"
734 " -X Exit after running test.\n"
735 " -E Run Echo test.\n"
736 " -N <num-writes> Test Cfg: number of writes.\n"
737 " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
738 " -T <txbuf-size> Test Cfg: tx buffer size.\n"
739 " -U Run Uni-directional test.\n"
740 " -B Run Bi-directional test.\n"
741 " -V Verbose mode.\n");
742 exit (1);
743}
744
745int
746main (int argc, char **argv)
747{
748 sock_client_main_t *scm = &sock_client_main;
749 sock_test_socket_t *ctrl = &scm->ctrl_socket;
750 int c, rv, errno_val;
751 sock_test_t post_test = SOCK_TEST_TYPE_NONE;
752
753 sock_test_cfg_init (&ctrl->cfg);
754 sock_test_socket_buf_alloc (ctrl);
755
756 opterr = 0;
757 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV")) != -1)
758 switch (c)
759 {
760 case 'c':
761 scm->dump_cfg = 1;
762 break;
763
764 case 's':
765 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
766 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
767 {
768 fprintf (stderr, "ERROR: Invalid value for 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, "ERROR: Invalid number of sockets (%d)"
775 "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, "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,
794 "ERROR: Option -%c value 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, "ERROR: Invalid value for option -%c!\n", c);
807 print_usage_and_exit ();
808 }
809 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
810 {
811 fprintf (stderr, "ERROR: value greater than max number test"
812 " sockets (%d)!", SOCK_TEST_CFG_MAX_TEST_SCKTS);
813 print_usage_and_exit ();
814 }
815 break;
816
817 case 'N':
818 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
819 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
820 {
821 fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
822 print_usage_and_exit ();
823 }
824 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
825 break;
826
827 case 'R':
828 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
829 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
830 {
831 fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
832 print_usage_and_exit ();
833 }
834 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
835 {
836 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
837 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
838 (uint8_t **) & ctrl->rxbuf,
839 &ctrl->rxbuf_size);
840 }
841 else
842 {
843 fprintf (stderr,
844 "ERROR: rxbuf size (%lu) less than minumum (%u)\n",
845 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
846 print_usage_and_exit ();
847 }
848
849 break;
850
851 case 'T':
852 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
853 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
854 {
855 fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
856 print_usage_and_exit ();
857 }
858 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
859 {
860 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
861 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
862 (uint8_t **) & ctrl->txbuf,
863 &ctrl->txbuf_size);
864 ctrl->cfg.total_bytes =
865 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
866 }
867 else
868 {
869 fprintf (stderr,
870 "ERROR: txbuf size (%lu) less than minumum (%u)!\n",
871 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
872 print_usage_and_exit ();
873 }
874 break;
875
876 case 'U':
877 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
878 break;
879
880 case 'B':
881 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
882 break;
883
884 case 'V':
885 ctrl->cfg.verbose = 1;
886 break;
887
888 case '?':
889 switch (optopt)
890 {
891 case 'E':
892 case 'I':
893 case 'N':
894 case 'R':
895 case 'T':
896 case 'w':
897 fprintf (stderr, "ERROR: Option -%c requires an argument.\n",
898 optopt);
Chris Lukeab7b8d92017-09-07 07:40:13 -0400899 break;
900
Dave Wallace543852a2017-08-03 02:11:34 -0400901 default:
902 if (isprint (optopt))
903 fprintf (stderr, "ERROR: Unknown option `-%c'.\n", optopt);
904 else
905 fprintf (stderr, "ERROR: Unknown option character `\\x%x'.\n",
906 optopt);
907 }
908 /* fall thru */
909 case 'h':
910 default:
911 print_usage_and_exit ();
912 }
913
914 if (argc < (optind + 2))
915 {
916 fprintf (stderr, "ERROR: Insufficient number of arguments!\n");
917 print_usage_and_exit ();
918 }
919
920#ifdef VCL_TEST
921 ctrl->fd = vppcom_app_create ("vcl_test_client");
922 if (ctrl->fd < 0)
923 {
924 errno = -ctrl->fd;
925 ctrl->fd = -1;
926 }
927 else
928 {
929 ctrl->fd = vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP,
930 0 /* is_nonblocking */ );
931 if (ctrl->fd < 0)
932 {
933 errno = -ctrl->fd;
934 ctrl->fd = -1;
935 }
936 }
937#else
938 ctrl->fd = socket (AF_INET, SOCK_STREAM, 0);
939#endif
940
941 if (ctrl->fd < 0)
942 {
943 errno_val = errno;
944 perror ("ERROR in main()");
945 fprintf (stderr, "ERROR: socket failed (errno = %d)!\n", errno_val);
946 return ctrl->fd;
947 }
948
949 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
950
951 scm->server_addr.sin_family = AF_INET;
952 inet_pton (AF_INET, argv[optind++], &(scm->server_addr.sin_addr));
953 scm->server_addr.sin_port = htons (atoi (argv[optind]));
954
955#ifdef VCL_TEST
956 scm->server_endpt.vrf = VPPCOM_VRF_DEFAULT;
957 scm->server_endpt.is_ip4 = (scm->server_addr.sin_family == AF_INET);
958 scm->server_endpt.ip = (uint8_t *) & scm->server_addr.sin_addr;
959 scm->server_endpt.port = (uint16_t) scm->server_addr.sin_port;
960#endif
961
962 do
963 {
964 printf ("\nCLIENT: Connecting to server...\n");
965
966#ifdef VCL_TEST
967 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
968#else
969 rv =
970 connect (ctrl->fd, (struct sockaddr *) &scm->server_addr,
971 sizeof (scm->server_addr));
972#endif
973 if (rv < 0)
974 {
975 errno_val = errno;
976 perror ("ERROR in main()");
977 fprintf (stderr, "ERROR: connect failed (errno = %d)!\n",
978 errno_val);
979 }
980
981 sock_test_cfg_sync (ctrl);
982 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
983 }
984 while (rv < 0);
985
986 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
987
988 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
989 {
990 if (scm->dump_cfg)
991 {
992 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
993 scm->dump_cfg = 0;
994 }
995
996 switch (ctrl->cfg.test)
997 {
998 case SOCK_TEST_TYPE_ECHO:
999 echo_test_client ();
1000 break;
1001
1002 case SOCK_TEST_TYPE_UNI:
1003 case SOCK_TEST_TYPE_BI:
1004 stream_test_client (ctrl->cfg.test);
1005 break;
1006
1007 case SOCK_TEST_TYPE_EXIT:
1008 continue;
1009
1010 case SOCK_TEST_TYPE_NONE:
1011 default:
1012 break;
1013 }
1014 switch (post_test)
1015 {
1016 case SOCK_TEST_TYPE_EXIT:
1017 switch (ctrl->cfg.test)
1018 {
1019 case SOCK_TEST_TYPE_EXIT:
1020 case SOCK_TEST_TYPE_UNI:
1021 case SOCK_TEST_TYPE_BI:
1022 case SOCK_TEST_TYPE_ECHO:
1023 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1024 continue;
1025
1026 case SOCK_TEST_TYPE_NONE:
1027 default:
1028 break;
1029 }
1030 break;
1031
1032 case SOCK_TEST_TYPE_NONE:
1033 case SOCK_TEST_TYPE_ECHO:
1034 case SOCK_TEST_TYPE_UNI:
1035 case SOCK_TEST_TYPE_BI:
1036 default:
1037 break;
1038 }
1039
1040 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1041 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1042
1043 printf ("\nType some characters and hit <return>\n"
1044 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1045
1046 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1047 {
1048 if (strlen (ctrl->txbuf) == 1)
1049 {
1050 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1051 continue;
1052 }
1053 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1054
1055 /* Parse input for keywords */
1056 ctrl->cfg.test = parse_input ();
1057 }
1058 }
1059
1060 exit_client ();
1061#ifdef VCL_TEST
1062 vppcom_session_close (ctrl->fd);
1063 vppcom_app_destroy ();
1064#else
1065 close (ctrl->fd);
1066#endif
1067 return 0;
1068}
1069
1070/*
1071 * fd.io coding-style-patch-verification: ON
1072 *
1073 * Local Variables:
1074 * eval: (c-set-style "gnu")
1075 * End:
1076 */