blob: af3e01dbb20465974507f5af7bf29550cf976c68 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallacee4d5a652018-06-24 21:21:21 -04002 * Copyright (c) 2017-2018 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Dave Wallacee4d5a652018-06-24 21:21:21 -040016#include <unistd.h>
17#include <errno.h>
18#include <stdlib.h>
19#include <ctype.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <stdio.h>
23#include <time.h>
24#include <arpa/inet.h>
25#include <vcl/vcl_test.h>
Dave Wallace543852a2017-08-03 02:11:34 -040026
Dave Wallacee4d5a652018-06-24 21:21:21 -040027typedef struct
28{
29 vppcom_endpt_t server_endpt;
30 struct sockaddr_storage server_addr;
31 uint32_t server_addr_size;
32 uint32_t cfg_seq_num;
33 sock_test_socket_t ctrl_socket;
34 sock_test_socket_t *test_socket;
35 uint32_t num_test_sockets;
36 uint8_t dump_cfg;
37} sock_client_main_t;
38
39sock_client_main_t sock_client_main;
40
41
42static int
43sock_test_cfg_sync (sock_test_socket_t * socket)
44{
45 sock_client_main_t *scm = &sock_client_main;
46 sock_test_socket_t *ctrl = &scm->ctrl_socket;
47 sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf;
48 int rx_bytes, tx_bytes;
49
50 if (socket->cfg.verbose)
51 sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ );
52
53 ctrl->cfg.seq_num = ++scm->cfg_seq_num;
54 if (socket->cfg.verbose)
55 {
56 printf ("CLIENT (fd %d): Sending config sent to server.\n", socket->fd);
57 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
58 }
59 tx_bytes = vcl_test_write (socket->fd, (uint8_t *) & ctrl->cfg,
60 sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose);
61 if (tx_bytes < 0)
62 {
63 fprintf (stderr, "CLIENT (fd %d): ERROR: write test cfg failed (%d)!\n",
64 socket->fd, tx_bytes);
65 return tx_bytes;
66 }
67
68 rx_bytes = vcl_test_read (socket->fd, (uint8_t *) socket->rxbuf,
69 sizeof (sock_test_cfg_t), NULL);
70 if (rx_bytes < 0)
71 return rx_bytes;
72
73 if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC)
74 {
75 fprintf (stderr, "CLIENT (fd %d): ERROR: Bad server reply cfg "
76 "-- aborting!\n", socket->fd);
77 return -1;
78 }
79 if ((rx_bytes != sizeof (sock_test_cfg_t))
80 || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg))
81 {
82 fprintf (stderr, "CLIENT (fd %d): ERROR: Invalid config received "
83 "from server!\n", socket->fd);
84 if (rx_bytes != sizeof (sock_test_cfg_t))
85 {
86 fprintf (stderr, "\tRx bytes %d != cfg size %lu\n",
87 rx_bytes, sizeof (sock_test_cfg_t));
88 }
89 else
90 {
91 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
92 fprintf (stderr, "CLIENT (fd %d): Valid config sent to server.\n",
93 socket->fd);
94 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
95 }
96 return -1;
97 }
98 else if (socket->cfg.verbose)
99 {
100 printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd);
101 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
102 }
103 ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ?
104 rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle);
105
106 return 0;
107}
108
109static void
110echo_test_client ()
111{
112 sock_client_main_t *scm = &sock_client_main;
113 sock_test_socket_t *ctrl = &scm->ctrl_socket;
114 sock_test_socket_t *tsock;
115 int rx_bytes, tx_bytes, nbytes;
116 uint32_t i, n;
117 int rv;
118 int nfds = 0;
119 fd_set wr_fdset, rd_fdset;
120 fd_set _wfdset, *wfdset = &_wfdset;
121 fd_set _rfdset, *rfdset = &_rfdset;
122
123 FD_ZERO (&wr_fdset);
124 FD_ZERO (&rd_fdset);
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++)
128 {
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))
133 return;
134
135 memcpy (tsock->txbuf, ctrl->txbuf, nbytes);
136 memset (&tsock->stats, 0, sizeof (tsock->stats));
137
138 FD_SET (tsock->fd, &wr_fdset);
139 FD_SET (tsock->fd, &rd_fdset);
140 nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
141 }
142
143 nfds++;
144 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
145 while (n)
146 {
147 _wfdset = wr_fdset;
148 _rfdset = rd_fdset;
149
150 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
151 NULL, 0);
152 if (rv < 0)
153 {
154 perror ("select()");
155 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
156 "aborting test!\n");
157 return;
158 }
159 else if (rv == 0)
160 continue;
161
162 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
163 {
164 tsock = &scm->test_socket[i];
165 if (!((tsock->stats.stop.tv_sec == 0) &&
166 (tsock->stats.stop.tv_nsec == 0)))
167 continue;
168
169 if (FD_ISSET (tsock->fd, wfdset) &&
170 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
171
172 {
173 tx_bytes =
174 vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes,
175 &tsock->stats, ctrl->cfg.verbose);
176 if (tx_bytes < 0)
177 {
178 fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) "
179 "failed -- aborting test!\n", tsock->fd);
180 return;
181 }
182
183 printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n",
184 tsock->fd, tx_bytes, tsock->txbuf);
185 }
186
187 if ((FD_ISSET (tsock->fd, rfdset)) &&
188 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
189 {
190 rx_bytes =
191 vcl_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
192 nbytes, &tsock->stats);
193 if (rx_bytes > 0)
194 {
195 printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n",
196 tsock->fd, rx_bytes, tsock->rxbuf);
197
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);
202 }
203 }
204
205 if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)
206 {
207 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
208 n--;
209 }
210 }
211 }
212 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
213
214 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
215 {
216 tsock = &scm->test_socket[i];
217 tsock->stats.start = ctrl->stats.start;
218
219 if (ctrl->cfg.verbose)
220 {
221 static char buf[64];
222
223 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
224 sock_test_stats_dump (buf, &tsock->stats,
225 1 /* show_rx */ , 1 /* show tx */ ,
226 ctrl->cfg.verbose);
227 }
228
229 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
230 }
231
232 if (ctrl->cfg.verbose)
233 {
234 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
235 1 /* show_rx */ , 1 /* show tx */ ,
236 ctrl->cfg.verbose);
237 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
238
239 if (ctrl->cfg.verbose > 1)
240 {
241 printf (" ctrl socket info\n"
242 SOCK_TEST_SEPARATOR_STRING
243 " fd: %d (0x%08x)\n"
244 " rxbuf: %p\n"
245 " rxbuf size: %u (0x%08x)\n"
246 " txbuf: %p\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);
252 }
253 }
254}
255
256static void
257stream_test_client (sock_test_t test)
258{
259 sock_client_main_t *scm = &sock_client_main;
260 sock_test_socket_t *ctrl = &scm->ctrl_socket;
261 sock_test_socket_t *tsock;
262 int tx_bytes;
263 uint32_t i, n;
264 int rv;
265 int nfds = 0;
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;
269
270 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
271 ctrl->cfg.ctrl_handle = ~0;
272
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);
277
278 if (sock_test_cfg_sync (ctrl))
279 {
280 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
281 return;
282 }
283
284 FD_ZERO (&wr_fdset);
285 FD_ZERO (&rd_fdset);
286 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
287 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
288 {
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);
295
296 /* Fill payload with incrementing uint32's */
297 for (i = 0; i < tsock->txbuf_size; i++)
298 tsock->txbuf[i] = i & 0xff;
299
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;
304 }
305
306 nfds++;
307 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
308 while (n)
309 {
310 _wfdset = wr_fdset;
311 _rfdset = rd_fdset;
312
313 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
314 NULL, 0);
315 if (rv < 0)
316 {
317 perror ("select()");
318 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
319 "aborting test!\n");
320 return;
321 }
322 else if (rv == 0)
323 continue;
324
325 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
326 {
327 tsock = &scm->test_socket[i];
328 if (!((tsock->stats.stop.tv_sec == 0) &&
329 (tsock->stats.stop.tv_nsec == 0)))
330 continue;
331
332 if ((test == SOCK_TEST_TYPE_BI) &&
333 FD_ISSET (tsock->fd, rfdset) &&
334 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
335 {
336 (void) vcl_test_read (tsock->fd,
337 (uint8_t *) tsock->rxbuf,
338 tsock->rxbuf_size, &tsock->stats);
339 }
340
341 if (FD_ISSET (tsock->fd, wfdset) &&
342 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
343 {
344 tx_bytes =
345 vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
346 ctrl->cfg.txbuf_size, &tsock->stats,
347 ctrl->cfg.verbose);
348 if (tx_bytes < 0)
349 {
350 fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) "
351 "failed -- aborting test!\n", tsock->fd);
352 return;
353 }
354 }
355
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)))
360 {
361 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
362 n--;
363 }
364 }
365 }
366 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
367
368 printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
369 ctrl->fd);
370
371 if (sock_test_cfg_sync (ctrl))
372 {
373 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
374 return;
375 }
376
377 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
378 {
379 tsock = &scm->test_socket[i];
380
381 if (ctrl->cfg.verbose)
382 {
383 static char buf[64];
384
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);
389 }
390
391 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
392 }
393
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 */ );
398
399 if (ctrl->cfg.verbose)
400 {
401 printf (" ctrl socket info\n"
402 SOCK_TEST_SEPARATOR_STRING
403 " fd: %d (0x%08x)\n"
404 " rxbuf: %p\n"
405 " rxbuf size: %u (0x%08x)\n"
406 " txbuf: %p\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);
412 }
413
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!");
417
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");
421}
422
423static void
424exit_client (void)
425{
426 sock_client_main_t *scm = &sock_client_main;
427 sock_test_socket_t *ctrl = &scm->ctrl_socket;
428 sock_test_socket_t *tsock;
429 int i;
430
431 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
432 {
433 tsock = &scm->test_socket[i];
434 tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
435
436 /* coverity[COPY_PASTE_ERROR] */
437 if (ctrl->cfg.verbose)
438 {
439 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
440 tsock->fd);
441 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
442 }
443 (void) vcl_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
444 sizeof (tsock->cfg), &tsock->stats,
445 ctrl->cfg.verbose);
446 }
447
448 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
449 if (ctrl->cfg.verbose)
450 {
451 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
452 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
453 }
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");
457 sleep (1);
458}
459
460static int
461sock_test_connect_test_sockets (uint32_t num_test_sockets)
462{
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;
467
468 if (num_test_sockets < 1)
469 {
470 errno = EINVAL;
471 return -1;
472 }
473
474 if (num_test_sockets < scm->num_test_sockets)
475 {
476 for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
477 {
478 tsock = &scm->test_socket[i];
479 vppcom_session_close (tsock->fd);
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, "CLIENT: ERROR: socket failed (errno = %d)!\n",
494 errno_val);
495 return -1;
496 }
497
498 if (!scm->test_socket)
499 memset (tsock, 0, sizeof (*tsock));
500
501 scm->test_socket = tsock;
502 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
503 {
504 tsock = &scm->test_socket[i];
505 tsock->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
506 VPPCOM_PROTO_UDP :
507 VPPCOM_PROTO_TCP,
508 1 /* is_nonblocking */ );
509 if (tsock->fd < 0)
510 {
511 errno = -tsock->fd;
512 tsock->fd = -1;
513 }
514 if (tsock->fd < 0)
515 {
516 errno_val = errno;
517 perror ("ERROR in sock_test_connect_test_sockets()");
518 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
519 errno_val);
520 return tsock->fd;
521 }
522
523 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
524 if (rv)
525 {
526 errno = -rv;
527 rv = -1;
528 }
529 if (rv < 0)
530 {
531 errno_val = errno;
532 perror ("ERROR in sock_test_connect_test_sockets()");
533 fprintf (stderr, "CLIENT: ERROR: connect failed "
534 "(errno = %d)!\n", errno_val);
535 return -1;
536 }
537 tsock->cfg = ctrl->cfg;
538 sock_test_socket_buf_alloc (tsock);
539 sock_test_cfg_sync (tsock);
540
541 printf ("CLIENT (fd %d): Test socket %d connected.\n",
542 tsock->fd, i);
543 }
544 }
545
546 scm->num_test_sockets = num_test_sockets;
547 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
548 return 0;
549}
550
551static void
552dump_help (void)
553{
554#define INDENT "\n "
555
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");
575}
576
577static void
578cfg_txbuf_size_set (void)
579{
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);
584
585 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
586 {
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 */ );
592 }
593 else
594 fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
595 "minimum buf size (%u)!\n",
596 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
597}
598
599static void
600cfg_num_writes_set (void)
601{
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);
606
607 if (num_writes > 0)
608 {
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 */ );
612 }
613 else
614 {
615 fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
616 }
617}
618
619static void
620cfg_num_test_sockets_set (void)
621{
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);
626
627 if ((num_test_sockets > 0) &&
628 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
629 {
630 ctrl->cfg.num_test_sockets = num_test_sockets;
631 sock_test_connect_test_sockets (num_test_sockets);
632
633 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
634 }
635 else
636 {
637 fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: "
638 "%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, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
660 "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 (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf,
683 strlen (SOCK_TEST_TOKEN_EXIT)))
684 rv = SOCK_TEST_TYPE_EXIT;
685
686 else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf,
687 strlen (SOCK_TEST_TOKEN_HELP)))
688 dump_help ();
689
690 else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf,
691 strlen (SOCK_TEST_TOKEN_SHOW_CFG)))
692 scm->dump_cfg = 1;
693
694 else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf,
695 strlen (SOCK_TEST_TOKEN_VERBOSE)))
696 cfg_verbose_toggle ();
697
698 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
699 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
700 cfg_txbuf_size_set ();
701
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 ();
705
706 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
707 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
708 cfg_num_writes_set ();
709
710 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
711 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
712 cfg_rxbuf_size_set ();
713
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;
717
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;
721
722 else
723 rv = SOCK_TEST_TYPE_ECHO;
724
725 return rv;
726}
727
728void
729print_usage_and_exit (void)
730{
731 fprintf (stderr,
732 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
733 " OPTIONS\n"
734 " -h Print this message and exit.\n"
735 " -6 Use IPv6\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");
747 exit (1);
748}
749
750int
751main (int argc, char **argv)
752{
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;
757
758 sock_test_cfg_init (&ctrl->cfg);
759 sock_test_socket_buf_alloc (ctrl);
760
761 opterr = 0;
762 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1)
763 switch (c)
764 {
765 case 'c':
766 scm->dump_cfg = 1;
767 break;
768
769 case 's':
770 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
771 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
772 {
773 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
774 "option -%c!\n", c);
775 print_usage_and_exit ();
776 }
777 if (!ctrl->cfg.num_test_sockets ||
778 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
779 {
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 ();
785 }
786 break;
787
788 case 'w':
789 fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n");
790 break;
791
792 case 'X':
793 post_test = SOCK_TEST_TYPE_EXIT;
794 break;
795
796 case 'E':
797 if (strlen (optarg) > ctrl->txbuf_size)
798 {
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 ();
803 }
804 strcpy (ctrl->txbuf, optarg);
805 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
806 break;
807
808 case 'I':
809 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
810 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
811 {
812 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
813 "option -%c!\n", c);
814 print_usage_and_exit ();
815 }
816 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
817 {
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 ();
822 }
823 break;
824
825 case 'N':
826 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
827 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
828 {
829 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
830 "option -%c!\n", c);
831 print_usage_and_exit ();
832 }
833 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
834 break;
835
836 case 'R':
837 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
838 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
839 {
840 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
841 "option -%c!\n", c);
842 print_usage_and_exit ();
843 }
844 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
845 {
846 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
847 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
848 (uint8_t **) & ctrl->rxbuf,
849 &ctrl->rxbuf_size);
850 }
851 else
852 {
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 ();
857 }
858
859 break;
860
861 case 'T':
862 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
863 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
864 {
865 fprintf (stderr, "CLIENT: ERROR: Invalid value "
866 "for option -%c!\n", c);
867 print_usage_and_exit ();
868 }
869 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
870 {
871 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
872 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
873 (uint8_t **) & ctrl->txbuf,
874 &ctrl->txbuf_size);
875 ctrl->cfg.total_bytes =
876 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
877 }
878 else
879 {
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 ();
884 }
885 break;
886
887 case 'U':
888 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
889 break;
890
891 case 'B':
892 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
893 break;
894
895 case 'V':
896 ctrl->cfg.verbose = 1;
897 break;
898
899 case '6':
900 ctrl->cfg.address_ip6 = 1;
901 break;
902
903 case 'D':
904 ctrl->cfg.transport_udp = 1;
905 break;
906
907 case '?':
908 switch (optopt)
909 {
910 case 'E':
911 case 'I':
912 case 'N':
913 case 'R':
914 case 'T':
915 case 'w':
916 fprintf (stderr, "CLIENT: ERROR: Option -%c "
917 "requires an argument.\n", optopt);
918 break;
919
920 default:
921 if (isprint (optopt))
922 fprintf (stderr, "CLIENT: ERROR: Unknown "
923 "option `-%c'.\n", optopt);
924 else
925 fprintf (stderr, "CLIENT: ERROR: Unknown "
926 "option character `\\x%x'.\n", optopt);
927 }
928 /* fall thru */
929 case 'h':
930 default:
931 print_usage_and_exit ();
932 }
933
934 if (argc < (optind + 2))
935 {
936 fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n");
937 print_usage_and_exit ();
938 }
939
940 ctrl->fd = vppcom_app_create ("vcl_test_client");
941 if (ctrl->fd < 0)
942 {
943 errno = -ctrl->fd;
944 ctrl->fd = -1;
945 }
946 else
947 {
948 ctrl->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
949 VPPCOM_PROTO_UDP :
950 VPPCOM_PROTO_TCP,
951 0 /* is_nonblocking */ );
952 if (ctrl->fd < 0)
953 {
954 errno = -ctrl->fd;
955 ctrl->fd = -1;
956 }
957 }
958
959 if (ctrl->fd < 0)
960 {
961 errno_val = errno;
962 perror ("ERROR in main()");
963 fprintf (stderr, "CLIENT: ERROR: socket "
964 "failed (errno = %d)!\n", errno_val);
965 return ctrl->fd;
966 }
967
968 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
969 if (ctrl->cfg.address_ip6)
970 {
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]));
977 }
978 else
979 {
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]));
986 }
987
988 if (ctrl->cfg.address_ip6)
989 {
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;
995 }
996 else
997 {
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;
1003 }
1004
1005 do
1006 {
1007 printf ("\nCLIENT: Connecting to server...\n");
1008
1009 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
1010 if (rv)
1011 {
1012 errno = -rv;
1013 rv = -1;
1014 }
1015 if (rv < 0)
1016 {
1017 errno_val = errno;
1018 perror ("ERROR in main()");
1019 fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n",
1020 errno_val);
1021 return -1;
1022 }
1023
1024 sock_test_cfg_sync (ctrl);
1025 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
1026 }
1027 while (rv < 0);
1028
1029 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
1030
1031 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
1032 {
1033 if (scm->dump_cfg)
1034 {
1035 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
1036 scm->dump_cfg = 0;
1037 }
1038
1039 switch (ctrl->cfg.test)
1040 {
1041 case SOCK_TEST_TYPE_ECHO:
1042 echo_test_client ();
1043 break;
1044
1045 case SOCK_TEST_TYPE_UNI:
1046 case SOCK_TEST_TYPE_BI:
1047 stream_test_client (ctrl->cfg.test);
1048 break;
1049
1050 case SOCK_TEST_TYPE_EXIT:
1051 continue;
1052
1053 case SOCK_TEST_TYPE_NONE:
1054 default:
1055 break;
1056 }
1057 switch (post_test)
1058 {
1059 case SOCK_TEST_TYPE_EXIT:
1060 switch (ctrl->cfg.test)
1061 {
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;
1067 continue;
1068
1069 case SOCK_TEST_TYPE_NONE:
1070 default:
1071 break;
1072 }
1073 break;
1074
1075 case SOCK_TEST_TYPE_NONE:
1076 case SOCK_TEST_TYPE_ECHO:
1077 case SOCK_TEST_TYPE_UNI:
1078 case SOCK_TEST_TYPE_BI:
1079 default:
1080 break;
1081 }
1082
1083 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1084 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1085
1086 printf ("\nCLIENT: Type some characters and hit <return>\n"
1087 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1088
1089 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1090 {
1091 if (strlen (ctrl->txbuf) == 1)
1092 {
1093 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1094 continue;
1095 }
1096 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1097
1098 /* Parse input for keywords */
1099 ctrl->cfg.test = parse_input ();
1100 }
1101 }
1102
1103 exit_client ();
1104 vppcom_session_close (ctrl->fd);
1105 vppcom_app_destroy ();
1106 return 0;
1107}
Dave Wallace543852a2017-08-03 02:11:34 -04001108
1109/*
1110 * fd.io coding-style-patch-verification: ON
1111 *
1112 * Local Variables:
1113 * eval: (c-set-style "gnu")
1114 * End:
1115 */