blob: 4319f01b5abeadcd52f8305108ecae156460e2a7 [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
Dave Wallaced48e9762017-08-22 23:29:33 -0400497 if (!scm->test_socket)
498 memset (tsock, 0, sizeof (*tsock));
499
Dave Wallace543852a2017-08-03 02:11:34 -0400500 scm->test_socket = tsock;
501 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
502 {
503 tsock = &scm->test_socket[i];
504#ifdef VCL_TEST
505 tsock->fd =
506 vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP,
507 0 /* is_nonblocking */ );
508 if (tsock->fd < 0)
509 {
510 errno = -tsock->fd;
511 tsock->fd = -1;
512 }
513#else
514 tsock->fd = socket (AF_INET, SOCK_STREAM, 0);
515#endif
516 if (tsock->fd < 0)
517 {
518 errno_val = errno;
519 perror ("ERROR in sock_test_connect_test_sockets()");
520 fprintf (stderr, "ERROR: socket failed (errno = %d)!\n",
521 errno_val);
522 return tsock->fd;
523 }
524
525#ifdef VCL_TEST
526 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
527#else
528 rv =
529 connect (tsock->fd, (struct sockaddr *) &scm->server_addr,
530 sizeof (scm->server_addr));
531#endif
532 if (rv < 0)
533 {
534 errno_val = errno;
535 perror ("ERROR in main()");
536 fprintf (stderr, "ERROR: connect failed (errno = %d)!\n",
537 errno_val);
538 }
539 tsock->cfg = ctrl->cfg;
540 sock_test_socket_buf_alloc (tsock);
541 sock_test_cfg_sync (tsock);
542
543 printf ("CLIENT (fd %d): Test socket %d connected.\n",
544 tsock->fd, i);
545 }
546 }
547
548 scm->num_test_sockets = num_test_sockets;
549 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
550 return 0;
551}
552
553static void
554dump_help (void)
555{
556#define INDENT "\n "
557
558 printf ("Test configuration commands:"
559 INDENT SOCK_TEST_TOKEN_HELP
560 "\t\t\tDisplay help."
561 INDENT SOCK_TEST_TOKEN_EXIT
562 "\t\t\tExit test client & server."
563 INDENT SOCK_TEST_TOKEN_SHOW_CFG
564 "\t\t\tShow the current test cfg."
565 INDENT SOCK_TEST_TOKEN_RUN_UNI
566 "\t\t\tRun the Uni-directional test."
567 INDENT SOCK_TEST_TOKEN_RUN_BI
568 "\t\t\tRun the Bi-directional test."
569 INDENT SOCK_TEST_TOKEN_VERBOSE
570 "\t\t\tToggle verbose setting."
571 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
572 "<rxbuf size>\tRx buffer size (bytes)."
573 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
574 "<txbuf size>\tTx buffer size (bytes)."
575 INDENT SOCK_TEST_TOKEN_NUM_WRITES
576 "<# of writes>\tNumber of txbuf writes to server." "\n");
577}
578
579static void
580cfg_txbuf_size_set (void)
581{
582 sock_client_main_t *scm = &sock_client_main;
583 sock_test_socket_t *ctrl = &scm->ctrl_socket;
584 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
585 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
586
587 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
588 {
589 ctrl->cfg.txbuf_size = txbuf_size;
590 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
591 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
592 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
593 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
594 }
595 else
596 fprintf (stderr,
597 "ERROR: Invalid txbuf size (%lu) < minimum buf size (%u)!\n",
598 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
599}
600
601static void
602cfg_num_writes_set (void)
603{
604 sock_client_main_t *scm = &sock_client_main;
605 sock_test_socket_t *ctrl = &scm->ctrl_socket;
606 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
607 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
608
609 if (num_writes > 0)
610 {
611 ctrl->cfg.num_writes = num_writes;
612 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
613 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
614 }
615 else
616 {
617 fprintf (stderr, "ERROR: invalid num writes: %u\n", num_writes);
618 }
619}
620
621static void
622cfg_num_test_sockets_set (void)
623{
624 sock_client_main_t *scm = &sock_client_main;
625 sock_test_socket_t *ctrl = &scm->ctrl_socket;
626 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
627 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
628
629 if ((num_test_sockets > 0) &&
630 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
631 {
632 ctrl->cfg.num_test_sockets = num_test_sockets;
633 sock_test_connect_test_sockets (num_test_sockets);
634 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
635 }
636 else
637 {
638 fprintf (stderr, "ERROR: invalid num test sockets: %u, (%d max)\n",
639 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
640 }
641}
642
643static void
644cfg_rxbuf_size_set (void)
645{
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);
650
651 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
652 {
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 */ );
657 }
658 else
659 fprintf (stderr,
660 "ERROR: Invalid rxbuf size (%lu) < minimum buf size (%u)!\n",
661 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
662}
663
664static void
665cfg_verbose_toggle (void)
666{
667 sock_client_main_t *scm = &sock_client_main;
668 sock_test_socket_t *ctrl = &scm->ctrl_socket;
669
670 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
671 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
672
673}
674
675static sock_test_t
676parse_input ()
677{
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;
681
682 if (!strcmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf))
683 rv = SOCK_TEST_TYPE_EXIT;
684
685 else if (!strcmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf))
686 dump_help ();
687
688 else if (!strcmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf))
689 scm->dump_cfg = 1;
690
691 else if (!strcmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf))
692 cfg_verbose_toggle ();
693
694 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
695 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
696 cfg_txbuf_size_set ();
697
698 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
699 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
700 cfg_num_test_sockets_set ();
701
702 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
703 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
704 cfg_num_writes_set ();
705
706 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
707 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
708 cfg_rxbuf_size_set ();
709
710 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
711 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
712 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
713
714 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
715 strlen (SOCK_TEST_TOKEN_RUN_BI)))
716 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
717
718 else
719 rv = SOCK_TEST_TYPE_ECHO;
720
721 return rv;
722}
723
724void
725print_usage_and_exit (void)
726{
727 fprintf (stderr,
728 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
729 " OPTIONS\n"
730 " -h Print this message and exit.\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:UBV")) != -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, "ERROR: Invalid value for option -%c!\n", c);
768 print_usage_and_exit ();
769 }
770 if (!ctrl->cfg.num_test_sockets ||
771 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
772 {
773 fprintf (stderr, "ERROR: Invalid number of sockets (%d)"
774 "specified for option -%c!\n"
775 " Valid range is 1 - %d\n",
776 ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
777 print_usage_and_exit ();
778 }
779 break;
780
781 case 'w':
782 fprintf (stderr, "Writing test results to files is TBD.\n");
783 break;
784
785 case 'X':
786 post_test = SOCK_TEST_TYPE_EXIT;
787 break;
788
789 case 'E':
790 if (strlen (optarg) > ctrl->txbuf_size)
791 {
792 fprintf (stderr,
793 "ERROR: Option -%c value larger than txbuf size (%d)!\n",
794 optopt, ctrl->txbuf_size);
795 print_usage_and_exit ();
796 }
797 strcpy (ctrl->txbuf, optarg);
798 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
799 break;
800
801 case 'I':
802 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
803 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
804 {
805 fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
806 print_usage_and_exit ();
807 }
808 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
809 {
810 fprintf (stderr, "ERROR: value greater than max number test"
811 " sockets (%d)!", SOCK_TEST_CFG_MAX_TEST_SCKTS);
812 print_usage_and_exit ();
813 }
814 break;
815
816 case 'N':
817 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
818 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
819 {
820 fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
821 print_usage_and_exit ();
822 }
823 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
824 break;
825
826 case 'R':
827 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
828 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
829 {
830 fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
831 print_usage_and_exit ();
832 }
833 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
834 {
835 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
836 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
837 (uint8_t **) & ctrl->rxbuf,
838 &ctrl->rxbuf_size);
839 }
840 else
841 {
842 fprintf (stderr,
843 "ERROR: rxbuf size (%lu) less than minumum (%u)\n",
844 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
845 print_usage_and_exit ();
846 }
847
848 break;
849
850 case 'T':
851 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
852 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
853 {
854 fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
855 print_usage_and_exit ();
856 }
857 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
858 {
859 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
860 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
861 (uint8_t **) & ctrl->txbuf,
862 &ctrl->txbuf_size);
863 ctrl->cfg.total_bytes =
864 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
865 }
866 else
867 {
868 fprintf (stderr,
869 "ERROR: txbuf size (%lu) less than minumum (%u)!\n",
870 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
871 print_usage_and_exit ();
872 }
873 break;
874
875 case 'U':
876 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
877 break;
878
879 case 'B':
880 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
881 break;
882
883 case 'V':
884 ctrl->cfg.verbose = 1;
885 break;
886
887 case '?':
888 switch (optopt)
889 {
890 case 'E':
891 case 'I':
892 case 'N':
893 case 'R':
894 case 'T':
895 case 'w':
896 fprintf (stderr, "ERROR: Option -%c requires an argument.\n",
897 optopt);
898 default:
899 if (isprint (optopt))
900 fprintf (stderr, "ERROR: Unknown option `-%c'.\n", optopt);
901 else
902 fprintf (stderr, "ERROR: Unknown option character `\\x%x'.\n",
903 optopt);
904 }
905 /* fall thru */
906 case 'h':
907 default:
908 print_usage_and_exit ();
909 }
910
911 if (argc < (optind + 2))
912 {
913 fprintf (stderr, "ERROR: Insufficient number of arguments!\n");
914 print_usage_and_exit ();
915 }
916
917#ifdef VCL_TEST
918 ctrl->fd = vppcom_app_create ("vcl_test_client");
919 if (ctrl->fd < 0)
920 {
921 errno = -ctrl->fd;
922 ctrl->fd = -1;
923 }
924 else
925 {
926 ctrl->fd = vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP,
927 0 /* is_nonblocking */ );
928 if (ctrl->fd < 0)
929 {
930 errno = -ctrl->fd;
931 ctrl->fd = -1;
932 }
933 }
934#else
935 ctrl->fd = socket (AF_INET, SOCK_STREAM, 0);
936#endif
937
938 if (ctrl->fd < 0)
939 {
940 errno_val = errno;
941 perror ("ERROR in main()");
942 fprintf (stderr, "ERROR: socket failed (errno = %d)!\n", errno_val);
943 return ctrl->fd;
944 }
945
946 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
947
948 scm->server_addr.sin_family = AF_INET;
949 inet_pton (AF_INET, argv[optind++], &(scm->server_addr.sin_addr));
950 scm->server_addr.sin_port = htons (atoi (argv[optind]));
951
952#ifdef VCL_TEST
953 scm->server_endpt.vrf = VPPCOM_VRF_DEFAULT;
954 scm->server_endpt.is_ip4 = (scm->server_addr.sin_family == AF_INET);
955 scm->server_endpt.ip = (uint8_t *) & scm->server_addr.sin_addr;
956 scm->server_endpt.port = (uint16_t) scm->server_addr.sin_port;
957#endif
958
959 do
960 {
961 printf ("\nCLIENT: Connecting to server...\n");
962
963#ifdef VCL_TEST
964 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
965#else
966 rv =
967 connect (ctrl->fd, (struct sockaddr *) &scm->server_addr,
968 sizeof (scm->server_addr));
969#endif
970 if (rv < 0)
971 {
972 errno_val = errno;
973 perror ("ERROR in main()");
974 fprintf (stderr, "ERROR: connect failed (errno = %d)!\n",
975 errno_val);
976 }
977
978 sock_test_cfg_sync (ctrl);
979 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
980 }
981 while (rv < 0);
982
983 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
984
985 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
986 {
987 if (scm->dump_cfg)
988 {
989 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
990 scm->dump_cfg = 0;
991 }
992
993 switch (ctrl->cfg.test)
994 {
995 case SOCK_TEST_TYPE_ECHO:
996 echo_test_client ();
997 break;
998
999 case SOCK_TEST_TYPE_UNI:
1000 case SOCK_TEST_TYPE_BI:
1001 stream_test_client (ctrl->cfg.test);
1002 break;
1003
1004 case SOCK_TEST_TYPE_EXIT:
1005 continue;
1006
1007 case SOCK_TEST_TYPE_NONE:
1008 default:
1009 break;
1010 }
1011 switch (post_test)
1012 {
1013 case SOCK_TEST_TYPE_EXIT:
1014 switch (ctrl->cfg.test)
1015 {
1016 case SOCK_TEST_TYPE_EXIT:
1017 case SOCK_TEST_TYPE_UNI:
1018 case SOCK_TEST_TYPE_BI:
1019 case SOCK_TEST_TYPE_ECHO:
1020 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1021 continue;
1022
1023 case SOCK_TEST_TYPE_NONE:
1024 default:
1025 break;
1026 }
1027 break;
1028
1029 case SOCK_TEST_TYPE_NONE:
1030 case SOCK_TEST_TYPE_ECHO:
1031 case SOCK_TEST_TYPE_UNI:
1032 case SOCK_TEST_TYPE_BI:
1033 default:
1034 break;
1035 }
1036
1037 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1038 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1039
1040 printf ("\nType some characters and hit <return>\n"
1041 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1042
1043 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1044 {
1045 if (strlen (ctrl->txbuf) == 1)
1046 {
1047 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1048 continue;
1049 }
1050 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1051
1052 /* Parse input for keywords */
1053 ctrl->cfg.test = parse_input ();
1054 }
1055 }
1056
1057 exit_client ();
1058#ifdef VCL_TEST
1059 vppcom_session_close (ctrl->fd);
1060 vppcom_app_destroy ();
1061#else
1062 close (ctrl->fd);
1063#endif
1064 return 0;
1065}
1066
1067/*
1068 * fd.io coding-style-patch-verification: ON
1069 *
1070 * Local Variables:
1071 * eval: (c-set-style "gnu")
1072 * End:
1073 */