blob: 7a735f5f3c07031ed5ba9fe5a8733023558f306e [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 Wallace3ee1fe12018-02-23 01:09:11 -050026#ifndef VCL_TEST
27#include <sys/un.h>
28#endif
Dave Wallace543852a2017-08-03 02:11:34 -040029
30typedef struct
31{
32#ifdef VCL_TEST
33 vppcom_endpt_t server_endpt;
Dave Wallace3ee1fe12018-02-23 01:09:11 -050034#else
35 int af_unix_echo_tx;
36 int af_unix_echo_rx;
Dave Wallace543852a2017-08-03 02:11:34 -040037#endif
38 struct sockaddr_in server_addr;
39 sock_test_socket_t ctrl_socket;
40 sock_test_socket_t *test_socket;
41 uint32_t num_test_sockets;
42 uint8_t dump_cfg;
43} sock_client_main_t;
44
45sock_client_main_t sock_client_main;
46
47
48static int
49sock_test_cfg_sync (sock_test_socket_t * socket)
50{
51 sock_client_main_t *scm = &sock_client_main;
52 sock_test_socket_t *ctrl = &scm->ctrl_socket;
53 sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf;
54 int rx_bytes, tx_bytes;
55
56 if (socket->cfg.verbose)
57 sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ );
58
59 tx_bytes = sock_test_write (socket->fd, (uint8_t *) & ctrl->cfg,
60 sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose);
61 if (tx_bytes < 0)
62 {
Dave Wallace048b1d62018-01-03 22:24:41 -050063 fprintf (stderr, "CLIENT: ERROR: write test cfg failed (%d)!\n",
64 tx_bytes);
Dave Wallace543852a2017-08-03 02:11:34 -040065 return tx_bytes;
66 }
67
68 rx_bytes = sock_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 {
Dave Wallace048b1d62018-01-03 22:24:41 -050075 fprintf (stderr, "CLIENT: ERROR: Bad server reply cfg -- aborting!\n");
Dave Wallace543852a2017-08-03 02:11:34 -040076 return -1;
77 }
78 if (socket->cfg.verbose)
79 {
80 printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd);
81 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
82 }
83 if ((rx_bytes != sizeof (sock_test_cfg_t))
84 || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg))
85 {
Dave Wallace048b1d62018-01-03 22:24:41 -050086 fprintf (stderr, "CLIENT: ERROR: Invalid config received "
87 "from server -- aborting!\n");
Dave Wallace543852a2017-08-03 02:11:34 -040088 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
89 return -1;
90 }
91 ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ?
92 rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle);
93
94 return 0;
95}
96
97static void
98echo_test_client ()
99{
100 sock_client_main_t *scm = &sock_client_main;
101 sock_test_socket_t *ctrl = &scm->ctrl_socket;
102 sock_test_socket_t *tsock;
103 int rx_bytes, tx_bytes, nbytes;
104 uint32_t i, n;
105 int rv;
106 int nfds = 0;
107 fd_set wr_fdset, rd_fdset;
108 fd_set _wfdset, *wfdset = &_wfdset;
109 fd_set _rfdset, *rfdset = &_rfdset;
110
111 FD_ZERO (&wr_fdset);
112 FD_ZERO (&rd_fdset);
113 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
114 ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1;
115 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
116 {
117 tsock = &scm->test_socket[n];
118 tsock->cfg = ctrl->cfg;
119 sock_test_socket_buf_alloc (tsock);
120 sock_test_cfg_sync (tsock);
121
122 memcpy (tsock->txbuf, ctrl->txbuf, nbytes);
123 memset (&tsock->stats, 0, sizeof (tsock->stats));
124
125 FD_SET (tsock->fd, &wr_fdset);
126 FD_SET (tsock->fd, &rd_fdset);
127 nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
128 }
129
130 nfds++;
131 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
132 while (n)
133 {
134 _wfdset = wr_fdset;
135 _rfdset = rd_fdset;
136
137#ifdef VCL_TEST
138 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
139 NULL, 0);
140#else
141 {
142 struct timeval timeout;
143 timeout.tv_sec = 0;
144 timeout.tv_usec = 0;
145 rv = select (nfds, rfdset, wfdset, NULL, &timeout);
146 }
147#endif
148 if (rv < 0)
149 {
150 perror ("select()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500151 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
152 "aborting test!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400153 return;
154 }
155 else if (rv == 0)
156 continue;
157
158 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
159 {
160 tsock = &scm->test_socket[i];
161 if (!((tsock->stats.stop.tv_sec == 0) &&
162 (tsock->stats.stop.tv_nsec == 0)))
163 continue;
164
165 if (FD_ISSET (tsock->fd, wfdset) &&
166 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
167
168 {
169 tx_bytes =
170 sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes,
171 &tsock->stats, ctrl->cfg.verbose);
172 if (tx_bytes < 0)
173 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500174 fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) "
175 "failed -- aborting test!\n", tsock->fd);
Dave Wallace543852a2017-08-03 02:11:34 -0400176 return;
177 }
178
179 printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n",
180 tsock->fd, tx_bytes, tsock->txbuf);
181 }
182
183 if ((FD_ISSET (tsock->fd, rfdset)) &&
184 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
185 {
186 rx_bytes =
187 sock_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
188 nbytes, &tsock->stats);
189 if (rx_bytes > 0)
190 {
191 printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n",
192 tsock->fd, rx_bytes, tsock->rxbuf);
193
194 if (tsock->stats.rx_bytes != tsock->stats.tx_bytes)
Dave Wallace048b1d62018-01-03 22:24:41 -0500195 printf ("CLIENT: WARNING: bytes read (%lu) "
196 "!= bytes written (%lu)!\n",
197 tsock->stats.rx_bytes, tsock->stats.tx_bytes);
Dave Wallace543852a2017-08-03 02:11:34 -0400198 }
199 }
200
201 if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)
202 {
203 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
204 n--;
205 }
206 }
207 }
208 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
209
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500210#ifndef VCL_TEST
211 {
212 int fd, errno_val;
213 struct sockaddr_un serveraddr;
214 uint8_t buffer[256];
215 size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
216 struct timeval timeout;
217
218 /* Open AF_UNIX socket and send an echo to test mixed epoll on server.
219 */
220 fd = socket (AF_UNIX, SOCK_STREAM, 0);
221 if (fd < 0)
222 {
223 errno_val = errno;
224 perror ("ERROR in echo_test_client(): socket(AF_UNIX) failed");
225 fprintf (stderr,
226 "CLIENT: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed "
227 "(errno = %d)!\n", errno_val);
228 goto out;
229 }
230 memset (&serveraddr, 0, sizeof (serveraddr));
231 serveraddr.sun_family = AF_UNIX;
232 strcpy (serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME);
233 rv = connect (fd, (struct sockaddr *) &serveraddr, SUN_LEN (&serveraddr));
234 if (rv < 0)
235 {
236 errno_val = errno;
237 perror ("ERROR in echo_test_client(): connect() failed");
238 fprintf (stderr, "CLIENT: ERROR: connect(fd %d, \"%s\", %lu) "
239 "failed (errno = %d)!\n", fd, SOCK_TEST_AF_UNIX_FILENAME,
240 SUN_LEN (&serveraddr), errno_val);
241 goto done;
242 }
243
244 scm->af_unix_echo_tx++;
245 strcpy ((char *) buffer, SOCK_TEST_MIXED_EPOLL_DATA);
246 timeout.tv_sec = 0;
247 timeout.tv_usec = 250000;
248 select (0, NULL, NULL, NULL, &timeout); /* delay .25 secs */
249 rv = write (fd, buffer, nbytes);
250 if (rv < 0)
251 {
252 errno_val = errno;
253 perror ("ERROR in echo_test_client(): write() failed");
254 fprintf (stderr, "CLIENT: ERROR: write(fd %d, \"%s\", %lu) "
255 "failed (errno = %d)!\n", fd, buffer, nbytes, errno_val);
256 goto done;
257 }
258 else if (rv < nbytes)
259 {
260 fprintf (stderr, "CLIENT: ERROR: write(fd %d, \"%s\", %lu) "
261 "returned %d!\n", fd, buffer, nbytes, rv);
262 goto done;
263 }
264
265 printf ("CLIENT (AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
266 memset (buffer, 0, sizeof (buffer));
267 rv = read (fd, buffer, nbytes);
268 if (rv < 0)
269 {
270 errno_val = errno;
271 perror ("ERROR in echo_test_client(): read() failed");
272 fprintf (stderr, "CLIENT: ERROR: read(fd %d, %p, %lu) "
273 "failed (errno = %d)!\n", fd, buffer, nbytes, errno_val);
274 goto done;
275 }
276 else if (rv < nbytes)
277 {
278 fprintf (stderr, "CLIENT: ERROR: read(fd %d, %p, %lu) "
279 "returned %d!\n", fd, buffer, nbytes, rv);
280 goto done;
281 }
282
283 if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
284 {
285 printf ("CLIENT (AF_UNIX): RX (%d bytes) - '%s'\n", rv, buffer);
286 scm->af_unix_echo_rx++;
287 }
288 else
289 printf ("CLIENT (AF_UNIX): ERROR: RX (%d bytes) - '%s'\n", rv, buffer);
290
291 done:
292 close (fd);
293 out:
294 ;
295 }
296#endif
297
Dave Wallace543852a2017-08-03 02:11:34 -0400298 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
299 {
300 tsock = &scm->test_socket[i];
301 tsock->stats.start = ctrl->stats.start;
302
303 if (ctrl->cfg.verbose)
304 {
305 static char buf[64];
306
307 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
308 sock_test_stats_dump (buf, &tsock->stats,
309 1 /* show_rx */ , 1 /* show tx */ ,
310 ctrl->cfg.verbose);
311 }
312
313 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
314 }
315
316 if (ctrl->cfg.verbose)
317 {
318 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
319 1 /* show_rx */ , 1 /* show tx */ ,
320 ctrl->cfg.verbose);
321 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
322
323 if (ctrl->cfg.verbose > 1)
324 {
325 printf (" ctrl socket info\n"
326 SOCK_TEST_SEPARATOR_STRING
327 " fd: %d (0x%08x)\n"
328 " rxbuf: %p\n"
329 " rxbuf size: %u (0x%08x)\n"
330 " txbuf: %p\n"
331 " txbuf size: %u (0x%08x)\n"
332 SOCK_TEST_SEPARATOR_STRING,
333 ctrl->fd, (uint32_t) ctrl->fd,
334 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
335 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
336 }
337 }
338}
339
340static void
341stream_test_client (sock_test_t test)
342{
343 sock_client_main_t *scm = &sock_client_main;
344 sock_test_socket_t *ctrl = &scm->ctrl_socket;
345 sock_test_socket_t *tsock;
346 int tx_bytes;
347 uint32_t i, n;
348 int rv;
349 int nfds = 0;
350 fd_set wr_fdset, rd_fdset;
351 fd_set _wfdset, *wfdset = &_wfdset;
352 fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0;
353
354 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
355 ctrl->cfg.ctrl_handle = ~0;
356
357 printf ("\n" SOCK_TEST_BANNER_STRING
358 "CLIENT (fd %d): %s-directional Stream Test!\n\n"
359 "CLIENT (fd %d): Sending config to server on ctrl socket...\n",
360 ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd);
361
362 if (sock_test_cfg_sync (ctrl))
363 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500364 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
Dave Wallace543852a2017-08-03 02:11:34 -0400365 return;
366 }
367
368 FD_ZERO (&wr_fdset);
369 FD_ZERO (&rd_fdset);
370 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
371 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
372 {
373 tsock = &scm->test_socket[n];
374 tsock->cfg = ctrl->cfg;
375 sock_test_socket_buf_alloc (tsock);
376 printf ("CLIENT (fd %d): Sending config to server on "
377 "test socket %d...\n", tsock->fd, n);
378 sock_test_cfg_sync (tsock);
379
380 /* Fill payload with incrementing uint32's */
381 for (i = 0; i < tsock->txbuf_size; i++)
382 tsock->txbuf[i] = i & 0xff;
383
384 memset (&tsock->stats, 0, sizeof (tsock->stats));
385 FD_SET (tsock->fd, &wr_fdset);
386 FD_SET (tsock->fd, &rd_fdset);
387 nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
388 }
389
390 nfds++;
391 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
392 while (n)
393 {
394 _wfdset = wr_fdset;
395 _rfdset = rd_fdset;
396
397#ifdef VCL_TEST
398 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
399 NULL, 0);
400#else
401 {
402 struct timeval timeout;
403 timeout.tv_sec = 0;
404 timeout.tv_usec = 0;
405 rv = select (nfds, rfdset, wfdset, NULL, &timeout);
406 }
407#endif
408 if (rv < 0)
409 {
410 perror ("select()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500411 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
412 "aborting test!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400413 return;
414 }
415 else if (rv == 0)
416 continue;
417
418 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
419 {
420 tsock = &scm->test_socket[i];
421 if (!((tsock->stats.stop.tv_sec == 0) &&
422 (tsock->stats.stop.tv_nsec == 0)))
423 continue;
424
Dave Wallace048b1d62018-01-03 22:24:41 -0500425 if ((test == SOCK_TEST_TYPE_BI) &&
426 FD_ISSET (tsock->fd, rfdset) &&
427 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
428 {
429 (void) sock_test_read (tsock->fd,
430 (uint8_t *) tsock->rxbuf,
431 tsock->rxbuf_size, &tsock->stats);
432 }
433
Dave Wallace543852a2017-08-03 02:11:34 -0400434 if (FD_ISSET (tsock->fd, wfdset) &&
435 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
436 {
437 tx_bytes =
438 sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
439 ctrl->cfg.txbuf_size, &tsock->stats,
440 ctrl->cfg.verbose);
441 if (tx_bytes < 0)
442 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500443 fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) "
444 "failed -- aborting test!\n", tsock->fd);
Dave Wallace543852a2017-08-03 02:11:34 -0400445 return;
446 }
447 }
448
Dave Wallace543852a2017-08-03 02:11:34 -0400449 if (((test == SOCK_TEST_TYPE_UNI) &&
450 (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) ||
451 ((test == SOCK_TEST_TYPE_BI) &&
452 (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)))
453 {
454 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
455 n--;
456 }
457 }
458 }
459 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
460
461 printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
462 ctrl->fd);
463
464 if (sock_test_cfg_sync (ctrl))
465 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500466 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
Dave Wallace543852a2017-08-03 02:11:34 -0400467 return;
468 }
469
470 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
471 {
472 tsock = &scm->test_socket[i];
473
474 if (ctrl->cfg.verbose)
475 {
476 static char buf[64];
477
478 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
479 sock_test_stats_dump (buf, &tsock->stats,
480 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
481 1 /* show tx */ , ctrl->cfg.verbose);
482 }
483
484 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
485 }
486
487 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
488 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
489 1 /* show tx */ , ctrl->cfg.verbose);
490 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
491
492 if (ctrl->cfg.verbose)
493 {
494 printf (" ctrl socket info\n"
495 SOCK_TEST_SEPARATOR_STRING
496 " fd: %d (0x%08x)\n"
497 " rxbuf: %p\n"
498 " rxbuf size: %u (0x%08x)\n"
499 " txbuf: %p\n"
500 " txbuf size: %u (0x%08x)\n"
501 SOCK_TEST_SEPARATOR_STRING,
502 ctrl->fd, (uint32_t) ctrl->fd,
503 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
504 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
505 }
506
507 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
508 if (sock_test_cfg_sync (ctrl))
Dave Wallace048b1d62018-01-03 22:24:41 -0500509 fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!");
Dave Wallace543852a2017-08-03 02:11:34 -0400510
511 printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n"
512 SOCK_TEST_BANNER_STRING "\n", ctrl->fd,
513 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
514}
515
516static void
517exit_client (void)
518{
519 sock_client_main_t *scm = &sock_client_main;
520 sock_test_socket_t *ctrl = &scm->ctrl_socket;
521 sock_test_socket_t *tsock;
522 int i;
523
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500524#ifndef VCL_TEST
525 printf ("CLIENT: af_unix_echo_tx %d, af_unix_echo_rx %d\n",
526 scm->af_unix_echo_tx, scm->af_unix_echo_rx);
527#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400528 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
529 {
530 tsock = &scm->test_socket[i];
531 tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
532
Chris Lukeb2bcad62017-09-18 08:51:22 -0400533 /* coverity[COPY_PASTE_ERROR] */
Dave Wallace543852a2017-08-03 02:11:34 -0400534 if (ctrl->cfg.verbose)
535 {
536 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
537 tsock->fd);
538 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
539 }
540 (void) sock_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
541 sizeof (tsock->cfg), &tsock->stats,
542 ctrl->cfg.verbose);
543 }
544
545 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
546 if (ctrl->cfg.verbose)
547 {
548 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
549 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
550 }
551 (void) sock_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
552 sizeof (ctrl->cfg), &ctrl->stats,
553 ctrl->cfg.verbose);
554 printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
555 sleep (1);
556}
557
558static int
559sock_test_connect_test_sockets (uint32_t num_test_sockets)
560{
561 sock_client_main_t *scm = &sock_client_main;
562 sock_test_socket_t *ctrl = &scm->ctrl_socket;
563 sock_test_socket_t *tsock;
564 int i, rv, errno_val;
565
566 if (num_test_sockets < 1)
567 {
568 errno = EINVAL;
569 return -1;
570 }
571
572 if (num_test_sockets < scm->num_test_sockets)
573 {
574 for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
575 {
576 tsock = &scm->test_socket[i];
577#ifdef VCL_TEST
578 vppcom_session_close (tsock->fd);
579#else
580 close (tsock->fd);
581#endif
582 free (tsock->txbuf);
583 free (tsock->rxbuf);
584 }
585 }
586
587 else if (num_test_sockets > scm->num_test_sockets)
588 {
589 tsock = realloc (scm->test_socket,
590 sizeof (sock_test_socket_t) * num_test_sockets);
591 if (!tsock)
592 {
593 errno_val = errno;
594 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500595 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
596 errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400597 return -1;
598 }
599
Dave Wallaced48e9762017-08-22 23:29:33 -0400600 if (!scm->test_socket)
601 memset (tsock, 0, sizeof (*tsock));
602
Dave Wallace543852a2017-08-03 02:11:34 -0400603 scm->test_socket = tsock;
604 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
605 {
606 tsock = &scm->test_socket[i];
607#ifdef VCL_TEST
608 tsock->fd =
Dave Wallacec04cbf12018-02-07 18:14:02 -0500609 vppcom_session_create (VPPCOM_PROTO_TCP, 1 /* is_nonblocking */ );
Dave Wallace543852a2017-08-03 02:11:34 -0400610 if (tsock->fd < 0)
611 {
612 errno = -tsock->fd;
613 tsock->fd = -1;
614 }
615#else
616 tsock->fd = socket (AF_INET, SOCK_STREAM, 0);
617#endif
618 if (tsock->fd < 0)
619 {
620 errno_val = errno;
621 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500622 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400623 errno_val);
624 return tsock->fd;
625 }
626
627#ifdef VCL_TEST
628 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
Dave Wallaceee45d412017-11-24 21:44:06 -0500629 if (rv)
630 {
631 errno = -rv;
632 rv = -1;
633 }
Dave Wallace543852a2017-08-03 02:11:34 -0400634#else
635 rv =
636 connect (tsock->fd, (struct sockaddr *) &scm->server_addr,
637 sizeof (scm->server_addr));
638#endif
639 if (rv < 0)
640 {
641 errno_val = errno;
Dave Wallaceee45d412017-11-24 21:44:06 -0500642 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500643 fprintf (stderr, "CLIENT: ERROR: connect failed "
644 "(errno = %d)!\n", errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -0500645 return -1;
Dave Wallace543852a2017-08-03 02:11:34 -0400646 }
647 tsock->cfg = ctrl->cfg;
648 sock_test_socket_buf_alloc (tsock);
649 sock_test_cfg_sync (tsock);
650
651 printf ("CLIENT (fd %d): Test socket %d connected.\n",
652 tsock->fd, i);
653 }
654 }
655
656 scm->num_test_sockets = num_test_sockets;
657 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
658 return 0;
659}
660
661static void
662dump_help (void)
663{
664#define INDENT "\n "
665
Dave Wallace048b1d62018-01-03 22:24:41 -0500666 printf ("CLIENT: Test configuration commands:"
Dave Wallace543852a2017-08-03 02:11:34 -0400667 INDENT SOCK_TEST_TOKEN_HELP
668 "\t\t\tDisplay help."
669 INDENT SOCK_TEST_TOKEN_EXIT
670 "\t\t\tExit test client & server."
671 INDENT SOCK_TEST_TOKEN_SHOW_CFG
672 "\t\t\tShow the current test cfg."
673 INDENT SOCK_TEST_TOKEN_RUN_UNI
674 "\t\t\tRun the Uni-directional test."
675 INDENT SOCK_TEST_TOKEN_RUN_BI
676 "\t\t\tRun the Bi-directional test."
677 INDENT SOCK_TEST_TOKEN_VERBOSE
678 "\t\t\tToggle verbose setting."
679 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
680 "<rxbuf size>\tRx buffer size (bytes)."
681 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
682 "<txbuf size>\tTx buffer size (bytes)."
683 INDENT SOCK_TEST_TOKEN_NUM_WRITES
684 "<# of writes>\tNumber of txbuf writes to server." "\n");
685}
686
687static void
688cfg_txbuf_size_set (void)
689{
690 sock_client_main_t *scm = &sock_client_main;
691 sock_test_socket_t *ctrl = &scm->ctrl_socket;
692 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
693 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
694
695 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
696 {
697 ctrl->cfg.txbuf_size = txbuf_size;
698 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
699 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
700 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
701 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
702 }
703 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500704 fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
705 "minimum buf size (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400706 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
707}
708
709static void
710cfg_num_writes_set (void)
711{
712 sock_client_main_t *scm = &sock_client_main;
713 sock_test_socket_t *ctrl = &scm->ctrl_socket;
714 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
715 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
716
717 if (num_writes > 0)
718 {
719 ctrl->cfg.num_writes = num_writes;
720 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
721 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
722 }
723 else
724 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500725 fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
Dave Wallace543852a2017-08-03 02:11:34 -0400726 }
727}
728
729static void
730cfg_num_test_sockets_set (void)
731{
732 sock_client_main_t *scm = &sock_client_main;
733 sock_test_socket_t *ctrl = &scm->ctrl_socket;
734 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
735 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
736
737 if ((num_test_sockets > 0) &&
738 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
739 {
740 ctrl->cfg.num_test_sockets = num_test_sockets;
741 sock_test_connect_test_sockets (num_test_sockets);
Dave Wallaceee45d412017-11-24 21:44:06 -0500742
Dave Wallace543852a2017-08-03 02:11:34 -0400743 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
744 }
745 else
746 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500747 fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: "
748 "%u, (%d max)\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400749 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
750 }
751}
752
753static void
754cfg_rxbuf_size_set (void)
755{
756 sock_client_main_t *scm = &sock_client_main;
757 sock_test_socket_t *ctrl = &scm->ctrl_socket;
758 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
759 uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
760
761 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
762 {
763 ctrl->cfg.rxbuf_size = rxbuf_size;
764 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
765 (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
766 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
767 }
768 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500769 fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
770 "minimum buf size (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400771 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
772}
773
774static void
775cfg_verbose_toggle (void)
776{
777 sock_client_main_t *scm = &sock_client_main;
778 sock_test_socket_t *ctrl = &scm->ctrl_socket;
779
780 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
781 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
782
783}
784
785static sock_test_t
786parse_input ()
787{
788 sock_client_main_t *scm = &sock_client_main;
789 sock_test_socket_t *ctrl = &scm->ctrl_socket;
790 sock_test_t rv = SOCK_TEST_TYPE_NONE;
791
792 if (!strcmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf))
793 rv = SOCK_TEST_TYPE_EXIT;
794
795 else if (!strcmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf))
796 dump_help ();
797
798 else if (!strcmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf))
799 scm->dump_cfg = 1;
800
801 else if (!strcmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf))
802 cfg_verbose_toggle ();
803
804 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
805 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
806 cfg_txbuf_size_set ();
807
808 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
809 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
810 cfg_num_test_sockets_set ();
811
812 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
813 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
814 cfg_num_writes_set ();
815
816 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
817 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
818 cfg_rxbuf_size_set ();
819
820 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
821 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
822 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
823
824 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
825 strlen (SOCK_TEST_TOKEN_RUN_BI)))
826 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
827
828 else
829 rv = SOCK_TEST_TYPE_ECHO;
830
831 return rv;
832}
833
834void
835print_usage_and_exit (void)
836{
837 fprintf (stderr,
838 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
839 " OPTIONS\n"
840 " -h Print this message and exit.\n"
841 " -c Print test config before test.\n"
842 " -w <dir> Write test results to <dir>.\n"
843 " -X Exit after running test.\n"
844 " -E Run Echo test.\n"
845 " -N <num-writes> Test Cfg: number of writes.\n"
846 " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
847 " -T <txbuf-size> Test Cfg: tx buffer size.\n"
848 " -U Run Uni-directional test.\n"
849 " -B Run Bi-directional test.\n"
850 " -V Verbose mode.\n");
851 exit (1);
852}
853
854int
855main (int argc, char **argv)
856{
857 sock_client_main_t *scm = &sock_client_main;
858 sock_test_socket_t *ctrl = &scm->ctrl_socket;
859 int c, rv, errno_val;
860 sock_test_t post_test = SOCK_TEST_TYPE_NONE;
861
862 sock_test_cfg_init (&ctrl->cfg);
863 sock_test_socket_buf_alloc (ctrl);
864
865 opterr = 0;
866 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV")) != -1)
867 switch (c)
868 {
869 case 'c':
870 scm->dump_cfg = 1;
871 break;
872
873 case 's':
874 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
875 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
876 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500877 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
878 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400879 print_usage_and_exit ();
880 }
881 if (!ctrl->cfg.num_test_sockets ||
882 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
883 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500884 fprintf (stderr, "CLIENT: ERROR: Invalid number of "
885 "sockets (%d) specified for option -%c!\n"
Dave Wallace543852a2017-08-03 02:11:34 -0400886 " Valid range is 1 - %d\n",
887 ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
888 print_usage_and_exit ();
889 }
890 break;
891
892 case 'w':
Dave Wallace048b1d62018-01-03 22:24:41 -0500893 fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400894 break;
895
896 case 'X':
897 post_test = SOCK_TEST_TYPE_EXIT;
898 break;
899
900 case 'E':
901 if (strlen (optarg) > ctrl->txbuf_size)
902 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500903 fprintf (stderr, "CLIENT: ERROR: Option -%c value "
904 "larger than txbuf size (%d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400905 optopt, ctrl->txbuf_size);
906 print_usage_and_exit ();
907 }
908 strcpy (ctrl->txbuf, optarg);
909 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
910 break;
911
912 case 'I':
913 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
914 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
915 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500916 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
917 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400918 print_usage_and_exit ();
919 }
920 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
921 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500922 fprintf (stderr, "CLIENT: ERROR: value greater than max "
923 "number test sockets (%d)!",
924 SOCK_TEST_CFG_MAX_TEST_SCKTS);
Dave Wallace543852a2017-08-03 02:11:34 -0400925 print_usage_and_exit ();
926 }
927 break;
928
929 case 'N':
930 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
931 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
932 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500933 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
934 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400935 print_usage_and_exit ();
936 }
937 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
938 break;
939
940 case 'R':
941 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
942 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
943 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500944 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
945 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400946 print_usage_and_exit ();
947 }
948 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
949 {
950 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
951 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
952 (uint8_t **) & ctrl->rxbuf,
953 &ctrl->rxbuf_size);
954 }
955 else
956 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500957 fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) "
958 "less than minumum (%u)\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400959 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
960 print_usage_and_exit ();
961 }
962
963 break;
964
965 case 'T':
966 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
967 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
968 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500969 fprintf (stderr, "CLIENT: ERROR: Invalid value "
970 "for option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400971 print_usage_and_exit ();
972 }
973 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
974 {
975 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
976 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
977 (uint8_t **) & ctrl->txbuf,
978 &ctrl->txbuf_size);
979 ctrl->cfg.total_bytes =
980 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
981 }
982 else
983 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500984 fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) "
985 "less than minumum (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400986 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
987 print_usage_and_exit ();
988 }
989 break;
990
991 case 'U':
992 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
993 break;
994
995 case 'B':
996 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
997 break;
998
999 case 'V':
1000 ctrl->cfg.verbose = 1;
1001 break;
1002
1003 case '?':
1004 switch (optopt)
1005 {
1006 case 'E':
1007 case 'I':
1008 case 'N':
1009 case 'R':
1010 case 'T':
1011 case 'w':
Dave Wallace048b1d62018-01-03 22:24:41 -05001012 fprintf (stderr, "CLIENT: ERROR: Option -%c "
1013 "requires an argument.\n", optopt);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001014 break;
1015
Dave Wallace543852a2017-08-03 02:11:34 -04001016 default:
1017 if (isprint (optopt))
Dave Wallace048b1d62018-01-03 22:24:41 -05001018 fprintf (stderr, "CLIENT: ERROR: Unknown "
1019 "option `-%c'.\n", optopt);
Dave Wallace543852a2017-08-03 02:11:34 -04001020 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001021 fprintf (stderr, "CLIENT: ERROR: Unknown "
1022 "option character `\\x%x'.\n", optopt);
Dave Wallace543852a2017-08-03 02:11:34 -04001023 }
1024 /* fall thru */
1025 case 'h':
1026 default:
1027 print_usage_and_exit ();
1028 }
1029
1030 if (argc < (optind + 2))
1031 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001032 fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n");
Dave Wallace543852a2017-08-03 02:11:34 -04001033 print_usage_and_exit ();
1034 }
1035
1036#ifdef VCL_TEST
1037 ctrl->fd = vppcom_app_create ("vcl_test_client");
1038 if (ctrl->fd < 0)
1039 {
1040 errno = -ctrl->fd;
1041 ctrl->fd = -1;
1042 }
1043 else
1044 {
Dave Wallacec04cbf12018-02-07 18:14:02 -05001045 ctrl->fd = vppcom_session_create (VPPCOM_PROTO_TCP,
Dave Wallace543852a2017-08-03 02:11:34 -04001046 0 /* is_nonblocking */ );
1047 if (ctrl->fd < 0)
1048 {
1049 errno = -ctrl->fd;
1050 ctrl->fd = -1;
1051 }
1052 }
1053#else
1054 ctrl->fd = socket (AF_INET, SOCK_STREAM, 0);
1055#endif
1056
1057 if (ctrl->fd < 0)
1058 {
1059 errno_val = errno;
1060 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -05001061 fprintf (stderr, "CLIENT: ERROR: socket "
1062 "failed (errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -04001063 return ctrl->fd;
1064 }
1065
1066 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
1067
1068 scm->server_addr.sin_family = AF_INET;
1069 inet_pton (AF_INET, argv[optind++], &(scm->server_addr.sin_addr));
1070 scm->server_addr.sin_port = htons (atoi (argv[optind]));
1071
1072#ifdef VCL_TEST
Dave Wallace543852a2017-08-03 02:11:34 -04001073 scm->server_endpt.is_ip4 = (scm->server_addr.sin_family == AF_INET);
1074 scm->server_endpt.ip = (uint8_t *) & scm->server_addr.sin_addr;
1075 scm->server_endpt.port = (uint16_t) scm->server_addr.sin_port;
1076#endif
1077
1078 do
1079 {
1080 printf ("\nCLIENT: Connecting to server...\n");
1081
1082#ifdef VCL_TEST
1083 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
Dave Wallaceee45d412017-11-24 21:44:06 -05001084 if (rv)
1085 {
1086 errno = -rv;
1087 rv = -1;
1088 }
Dave Wallace543852a2017-08-03 02:11:34 -04001089#else
1090 rv =
1091 connect (ctrl->fd, (struct sockaddr *) &scm->server_addr,
1092 sizeof (scm->server_addr));
1093#endif
1094 if (rv < 0)
1095 {
1096 errno_val = errno;
1097 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -05001098 fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -04001099 errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -05001100 return -1;
Dave Wallace543852a2017-08-03 02:11:34 -04001101 }
1102
1103 sock_test_cfg_sync (ctrl);
1104 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
1105 }
1106 while (rv < 0);
1107
1108 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
1109
1110 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
1111 {
1112 if (scm->dump_cfg)
1113 {
1114 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
1115 scm->dump_cfg = 0;
1116 }
1117
1118 switch (ctrl->cfg.test)
1119 {
1120 case SOCK_TEST_TYPE_ECHO:
1121 echo_test_client ();
1122 break;
1123
1124 case SOCK_TEST_TYPE_UNI:
1125 case SOCK_TEST_TYPE_BI:
1126 stream_test_client (ctrl->cfg.test);
1127 break;
1128
1129 case SOCK_TEST_TYPE_EXIT:
1130 continue;
1131
1132 case SOCK_TEST_TYPE_NONE:
1133 default:
1134 break;
1135 }
1136 switch (post_test)
1137 {
1138 case SOCK_TEST_TYPE_EXIT:
1139 switch (ctrl->cfg.test)
1140 {
1141 case SOCK_TEST_TYPE_EXIT:
1142 case SOCK_TEST_TYPE_UNI:
1143 case SOCK_TEST_TYPE_BI:
1144 case SOCK_TEST_TYPE_ECHO:
1145 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1146 continue;
1147
1148 case SOCK_TEST_TYPE_NONE:
1149 default:
1150 break;
1151 }
1152 break;
1153
1154 case SOCK_TEST_TYPE_NONE:
1155 case SOCK_TEST_TYPE_ECHO:
1156 case SOCK_TEST_TYPE_UNI:
1157 case SOCK_TEST_TYPE_BI:
1158 default:
1159 break;
1160 }
1161
1162 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1163 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1164
Dave Wallace048b1d62018-01-03 22:24:41 -05001165 printf ("\nCLIENT: Type some characters and hit <return>\n"
Dave Wallace543852a2017-08-03 02:11:34 -04001166 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1167
1168 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1169 {
1170 if (strlen (ctrl->txbuf) == 1)
1171 {
1172 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1173 continue;
1174 }
1175 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1176
1177 /* Parse input for keywords */
1178 ctrl->cfg.test = parse_input ();
1179 }
1180 }
1181
1182 exit_client ();
1183#ifdef VCL_TEST
1184 vppcom_session_close (ctrl->fd);
1185 vppcom_app_destroy ();
Dave Wallace3ee1fe12018-02-23 01:09:11 -05001186 return 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001187#else
1188 close (ctrl->fd);
Dave Wallace3ee1fe12018-02-23 01:09:11 -05001189 return (scm->af_unix_echo_tx == scm->af_unix_echo_rx) ? 0 : -1;
Dave Wallace543852a2017-08-03 02:11:34 -04001190#endif
Dave Wallace543852a2017-08-03 02:11:34 -04001191}
1192
1193/*
1194 * fd.io coding-style-patch-verification: ON
1195 *
1196 * Local Variables:
1197 * eval: (c-set-style "gnu")
1198 * End:
1199 */