blob: e88b2b91b9dfd75f7901785e2177d88e37bda80f [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
Dave Wallacede910062018-03-20 09:22:13 -040038 struct sockaddr_storage server_addr;
39 uint32_t server_addr_size;
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -070040 uint32_t cfg_seq_num;
Dave Wallace543852a2017-08-03 02:11:34 -040041 sock_test_socket_t ctrl_socket;
42 sock_test_socket_t *test_socket;
43 uint32_t num_test_sockets;
44 uint8_t dump_cfg;
45} sock_client_main_t;
46
47sock_client_main_t sock_client_main;
48
49
50static int
51sock_test_cfg_sync (sock_test_socket_t * socket)
52{
53 sock_client_main_t *scm = &sock_client_main;
54 sock_test_socket_t *ctrl = &scm->ctrl_socket;
55 sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf;
56 int rx_bytes, tx_bytes;
57
58 if (socket->cfg.verbose)
59 sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ );
60
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -070061 ctrl->cfg.seq_num = ++scm->cfg_seq_num;
62 if (socket->cfg.verbose)
63 {
64 printf ("CLIENT (fd %d): Sending config sent to server.\n", socket->fd);
65 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
66 }
Dave Wallace543852a2017-08-03 02:11:34 -040067 tx_bytes = sock_test_write (socket->fd, (uint8_t *) & ctrl->cfg,
68 sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose);
69 if (tx_bytes < 0)
70 {
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -070071 fprintf (stderr, "CLIENT (fd %d): ERROR: write test cfg failed (%d)!\n",
72 socket->fd, tx_bytes);
Dave Wallace543852a2017-08-03 02:11:34 -040073 return tx_bytes;
74 }
75
76 rx_bytes = sock_test_read (socket->fd, (uint8_t *) socket->rxbuf,
77 sizeof (sock_test_cfg_t), NULL);
78 if (rx_bytes < 0)
79 return rx_bytes;
80
81 if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC)
82 {
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -070083 fprintf (stderr, "CLIENT (fd %d): ERROR: Bad server reply cfg "
84 "-- aborting!\n", socket->fd);
Dave Wallace543852a2017-08-03 02:11:34 -040085 return -1;
86 }
Dave Wallace543852a2017-08-03 02:11:34 -040087 if ((rx_bytes != sizeof (sock_test_cfg_t))
88 || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg))
89 {
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -070090 fprintf (stderr, "CLIENT (fd %d): ERROR: Invalid config received "
91 "from server!\n", socket->fd);
92 if (rx_bytes != sizeof (sock_test_cfg_t))
93 {
94 fprintf (stderr, "\tRx bytes %d != cfg size %lu\n",
95 rx_bytes, sizeof (sock_test_cfg_t));
96 }
97 else
98 {
99 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
100 fprintf (stderr, "CLIENT (fd %d): Valid config sent to server.\n",
101 socket->fd);
102 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
103 }
Dave Wallace543852a2017-08-03 02:11:34 -0400104 return -1;
105 }
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700106 else if (socket->cfg.verbose)
107 {
108 printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd);
109 sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
110 }
Dave Wallace543852a2017-08-03 02:11:34 -0400111 ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ?
112 rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle);
113
114 return 0;
115}
116
117static void
118echo_test_client ()
119{
120 sock_client_main_t *scm = &sock_client_main;
121 sock_test_socket_t *ctrl = &scm->ctrl_socket;
122 sock_test_socket_t *tsock;
123 int rx_bytes, tx_bytes, nbytes;
124 uint32_t i, n;
125 int rv;
126 int nfds = 0;
127 fd_set wr_fdset, rd_fdset;
128 fd_set _wfdset, *wfdset = &_wfdset;
129 fd_set _rfdset, *rfdset = &_rfdset;
130
131 FD_ZERO (&wr_fdset);
132 FD_ZERO (&rd_fdset);
133 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
134 ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1;
135 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
136 {
137 tsock = &scm->test_socket[n];
138 tsock->cfg = ctrl->cfg;
139 sock_test_socket_buf_alloc (tsock);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700140 if (sock_test_cfg_sync (tsock))
141 return;
Dave Wallace543852a2017-08-03 02:11:34 -0400142
143 memcpy (tsock->txbuf, ctrl->txbuf, nbytes);
144 memset (&tsock->stats, 0, sizeof (tsock->stats));
145
146 FD_SET (tsock->fd, &wr_fdset);
147 FD_SET (tsock->fd, &rd_fdset);
148 nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
149 }
150
151 nfds++;
152 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
153 while (n)
154 {
155 _wfdset = wr_fdset;
156 _rfdset = rd_fdset;
157
158#ifdef VCL_TEST
159 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
160 NULL, 0);
161#else
162 {
163 struct timeval timeout;
164 timeout.tv_sec = 0;
165 timeout.tv_usec = 0;
166 rv = select (nfds, rfdset, wfdset, NULL, &timeout);
167 }
168#endif
169 if (rv < 0)
170 {
171 perror ("select()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500172 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
173 "aborting test!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400174 return;
175 }
176 else if (rv == 0)
177 continue;
178
179 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
180 {
181 tsock = &scm->test_socket[i];
182 if (!((tsock->stats.stop.tv_sec == 0) &&
183 (tsock->stats.stop.tv_nsec == 0)))
184 continue;
185
186 if (FD_ISSET (tsock->fd, wfdset) &&
187 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
188
189 {
190 tx_bytes =
191 sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes,
192 &tsock->stats, ctrl->cfg.verbose);
193 if (tx_bytes < 0)
194 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500195 fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) "
196 "failed -- aborting test!\n", tsock->fd);
Dave Wallace543852a2017-08-03 02:11:34 -0400197 return;
198 }
199
200 printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n",
201 tsock->fd, tx_bytes, tsock->txbuf);
202 }
203
204 if ((FD_ISSET (tsock->fd, rfdset)) &&
205 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
206 {
207 rx_bytes =
208 sock_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
209 nbytes, &tsock->stats);
210 if (rx_bytes > 0)
211 {
212 printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n",
213 tsock->fd, rx_bytes, tsock->rxbuf);
214
215 if (tsock->stats.rx_bytes != tsock->stats.tx_bytes)
Dave Wallace048b1d62018-01-03 22:24:41 -0500216 printf ("CLIENT: WARNING: bytes read (%lu) "
217 "!= bytes written (%lu)!\n",
218 tsock->stats.rx_bytes, tsock->stats.tx_bytes);
Dave Wallace543852a2017-08-03 02:11:34 -0400219 }
220 }
221
222 if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)
223 {
224 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
225 n--;
226 }
227 }
228 }
229 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
230
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500231#ifndef VCL_TEST
232 {
233 int fd, errno_val;
234 struct sockaddr_un serveraddr;
235 uint8_t buffer[256];
236 size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
237 struct timeval timeout;
238
239 /* Open AF_UNIX socket and send an echo to test mixed epoll on server.
240 */
241 fd = socket (AF_UNIX, SOCK_STREAM, 0);
242 if (fd < 0)
243 {
244 errno_val = errno;
245 perror ("ERROR in echo_test_client(): socket(AF_UNIX) failed");
246 fprintf (stderr,
247 "CLIENT: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed "
248 "(errno = %d)!\n", errno_val);
249 goto out;
250 }
251 memset (&serveraddr, 0, sizeof (serveraddr));
252 serveraddr.sun_family = AF_UNIX;
253 strcpy (serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME);
254 rv = connect (fd, (struct sockaddr *) &serveraddr, SUN_LEN (&serveraddr));
255 if (rv < 0)
256 {
257 errno_val = errno;
258 perror ("ERROR in echo_test_client(): connect() failed");
259 fprintf (stderr, "CLIENT: ERROR: connect(fd %d, \"%s\", %lu) "
260 "failed (errno = %d)!\n", fd, SOCK_TEST_AF_UNIX_FILENAME,
261 SUN_LEN (&serveraddr), errno_val);
262 goto done;
263 }
264
265 scm->af_unix_echo_tx++;
266 strcpy ((char *) buffer, SOCK_TEST_MIXED_EPOLL_DATA);
267 timeout.tv_sec = 0;
268 timeout.tv_usec = 250000;
269 select (0, NULL, NULL, NULL, &timeout); /* delay .25 secs */
270 rv = write (fd, buffer, nbytes);
271 if (rv < 0)
272 {
273 errno_val = errno;
274 perror ("ERROR in echo_test_client(): write() failed");
275 fprintf (stderr, "CLIENT: ERROR: write(fd %d, \"%s\", %lu) "
276 "failed (errno = %d)!\n", fd, buffer, nbytes, errno_val);
277 goto done;
278 }
279 else if (rv < nbytes)
280 {
281 fprintf (stderr, "CLIENT: ERROR: write(fd %d, \"%s\", %lu) "
282 "returned %d!\n", fd, buffer, nbytes, rv);
283 goto done;
284 }
285
286 printf ("CLIENT (AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
287 memset (buffer, 0, sizeof (buffer));
288 rv = read (fd, buffer, nbytes);
289 if (rv < 0)
290 {
291 errno_val = errno;
292 perror ("ERROR in echo_test_client(): read() failed");
293 fprintf (stderr, "CLIENT: ERROR: read(fd %d, %p, %lu) "
294 "failed (errno = %d)!\n", fd, buffer, nbytes, errno_val);
295 goto done;
296 }
297 else if (rv < nbytes)
298 {
299 fprintf (stderr, "CLIENT: ERROR: read(fd %d, %p, %lu) "
300 "returned %d!\n", fd, buffer, nbytes, rv);
301 goto done;
302 }
303
304 if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
305 {
306 printf ("CLIENT (AF_UNIX): RX (%d bytes) - '%s'\n", rv, buffer);
307 scm->af_unix_echo_rx++;
308 }
309 else
310 printf ("CLIENT (AF_UNIX): ERROR: RX (%d bytes) - '%s'\n", rv, buffer);
311
312 done:
313 close (fd);
314 out:
315 ;
316 }
317#endif
318
Dave Wallace543852a2017-08-03 02:11:34 -0400319 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
320 {
321 tsock = &scm->test_socket[i];
322 tsock->stats.start = ctrl->stats.start;
323
324 if (ctrl->cfg.verbose)
325 {
326 static char buf[64];
327
328 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
329 sock_test_stats_dump (buf, &tsock->stats,
330 1 /* show_rx */ , 1 /* show tx */ ,
331 ctrl->cfg.verbose);
332 }
333
334 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
335 }
336
337 if (ctrl->cfg.verbose)
338 {
339 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
340 1 /* show_rx */ , 1 /* show tx */ ,
341 ctrl->cfg.verbose);
342 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
343
344 if (ctrl->cfg.verbose > 1)
345 {
346 printf (" ctrl socket info\n"
347 SOCK_TEST_SEPARATOR_STRING
348 " fd: %d (0x%08x)\n"
349 " rxbuf: %p\n"
350 " rxbuf size: %u (0x%08x)\n"
351 " txbuf: %p\n"
352 " txbuf size: %u (0x%08x)\n"
353 SOCK_TEST_SEPARATOR_STRING,
354 ctrl->fd, (uint32_t) ctrl->fd,
355 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
356 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
357 }
358 }
359}
360
361static void
362stream_test_client (sock_test_t test)
363{
364 sock_client_main_t *scm = &sock_client_main;
365 sock_test_socket_t *ctrl = &scm->ctrl_socket;
366 sock_test_socket_t *tsock;
367 int tx_bytes;
368 uint32_t i, n;
369 int rv;
370 int nfds = 0;
371 fd_set wr_fdset, rd_fdset;
372 fd_set _wfdset, *wfdset = &_wfdset;
373 fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0;
374
375 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
376 ctrl->cfg.ctrl_handle = ~0;
377
378 printf ("\n" SOCK_TEST_BANNER_STRING
379 "CLIENT (fd %d): %s-directional Stream Test!\n\n"
380 "CLIENT (fd %d): Sending config to server on ctrl socket...\n",
381 ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd);
382
383 if (sock_test_cfg_sync (ctrl))
384 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500385 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
Dave Wallace543852a2017-08-03 02:11:34 -0400386 return;
387 }
388
389 FD_ZERO (&wr_fdset);
390 FD_ZERO (&rd_fdset);
391 memset (&ctrl->stats, 0, sizeof (ctrl->stats));
392 for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
393 {
394 tsock = &scm->test_socket[n];
395 tsock->cfg = ctrl->cfg;
396 sock_test_socket_buf_alloc (tsock);
397 printf ("CLIENT (fd %d): Sending config to server on "
398 "test socket %d...\n", tsock->fd, n);
399 sock_test_cfg_sync (tsock);
400
401 /* Fill payload with incrementing uint32's */
402 for (i = 0; i < tsock->txbuf_size; i++)
403 tsock->txbuf[i] = i & 0xff;
404
405 memset (&tsock->stats, 0, sizeof (tsock->stats));
406 FD_SET (tsock->fd, &wr_fdset);
407 FD_SET (tsock->fd, &rd_fdset);
408 nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
409 }
410
411 nfds++;
412 clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
413 while (n)
414 {
415 _wfdset = wr_fdset;
416 _rfdset = rd_fdset;
417
418#ifdef VCL_TEST
419 rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
420 NULL, 0);
421#else
422 {
423 struct timeval timeout;
424 timeout.tv_sec = 0;
425 timeout.tv_usec = 0;
426 rv = select (nfds, rfdset, wfdset, NULL, &timeout);
427 }
428#endif
429 if (rv < 0)
430 {
431 perror ("select()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500432 fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
433 "aborting test!\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400434 return;
435 }
436 else if (rv == 0)
437 continue;
438
439 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
440 {
441 tsock = &scm->test_socket[i];
442 if (!((tsock->stats.stop.tv_sec == 0) &&
443 (tsock->stats.stop.tv_nsec == 0)))
444 continue;
445
Dave Wallace048b1d62018-01-03 22:24:41 -0500446 if ((test == SOCK_TEST_TYPE_BI) &&
447 FD_ISSET (tsock->fd, rfdset) &&
448 (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
449 {
450 (void) sock_test_read (tsock->fd,
451 (uint8_t *) tsock->rxbuf,
452 tsock->rxbuf_size, &tsock->stats);
453 }
454
Dave Wallace543852a2017-08-03 02:11:34 -0400455 if (FD_ISSET (tsock->fd, wfdset) &&
456 (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
457 {
458 tx_bytes =
459 sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
460 ctrl->cfg.txbuf_size, &tsock->stats,
461 ctrl->cfg.verbose);
462 if (tx_bytes < 0)
463 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500464 fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) "
465 "failed -- aborting test!\n", tsock->fd);
Dave Wallace543852a2017-08-03 02:11:34 -0400466 return;
467 }
468 }
469
Dave Wallace543852a2017-08-03 02:11:34 -0400470 if (((test == SOCK_TEST_TYPE_UNI) &&
471 (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) ||
472 ((test == SOCK_TEST_TYPE_BI) &&
473 (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)))
474 {
475 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
476 n--;
477 }
478 }
479 }
480 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
481
482 printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
483 ctrl->fd);
484
485 if (sock_test_cfg_sync (ctrl))
486 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500487 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
Dave Wallace543852a2017-08-03 02:11:34 -0400488 return;
489 }
490
491 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
492 {
493 tsock = &scm->test_socket[i];
494
495 if (ctrl->cfg.verbose)
496 {
497 static char buf[64];
498
499 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
500 sock_test_stats_dump (buf, &tsock->stats,
501 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
502 1 /* show tx */ , ctrl->cfg.verbose);
503 }
504
505 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
506 }
507
508 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
509 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
510 1 /* show tx */ , ctrl->cfg.verbose);
511 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
512
513 if (ctrl->cfg.verbose)
514 {
515 printf (" ctrl socket info\n"
516 SOCK_TEST_SEPARATOR_STRING
517 " fd: %d (0x%08x)\n"
518 " rxbuf: %p\n"
519 " rxbuf size: %u (0x%08x)\n"
520 " txbuf: %p\n"
521 " txbuf size: %u (0x%08x)\n"
522 SOCK_TEST_SEPARATOR_STRING,
523 ctrl->fd, (uint32_t) ctrl->fd,
524 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
525 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
526 }
527
528 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
529 if (sock_test_cfg_sync (ctrl))
Dave Wallace048b1d62018-01-03 22:24:41 -0500530 fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!");
Dave Wallace543852a2017-08-03 02:11:34 -0400531
532 printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n"
533 SOCK_TEST_BANNER_STRING "\n", ctrl->fd,
534 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
535}
536
537static void
538exit_client (void)
539{
540 sock_client_main_t *scm = &sock_client_main;
541 sock_test_socket_t *ctrl = &scm->ctrl_socket;
542 sock_test_socket_t *tsock;
543 int i;
544
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500545#ifndef VCL_TEST
546 printf ("CLIENT: af_unix_echo_tx %d, af_unix_echo_rx %d\n",
547 scm->af_unix_echo_tx, scm->af_unix_echo_rx);
548#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400549 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
550 {
551 tsock = &scm->test_socket[i];
552 tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
553
Chris Lukeb2bcad62017-09-18 08:51:22 -0400554 /* coverity[COPY_PASTE_ERROR] */
Dave Wallace543852a2017-08-03 02:11:34 -0400555 if (ctrl->cfg.verbose)
556 {
557 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
558 tsock->fd);
559 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
560 }
561 (void) sock_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
562 sizeof (tsock->cfg), &tsock->stats,
563 ctrl->cfg.verbose);
564 }
565
566 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
567 if (ctrl->cfg.verbose)
568 {
569 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
570 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
571 }
572 (void) sock_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
573 sizeof (ctrl->cfg), &ctrl->stats,
574 ctrl->cfg.verbose);
575 printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
576 sleep (1);
577}
578
579static int
580sock_test_connect_test_sockets (uint32_t num_test_sockets)
581{
582 sock_client_main_t *scm = &sock_client_main;
583 sock_test_socket_t *ctrl = &scm->ctrl_socket;
584 sock_test_socket_t *tsock;
585 int i, rv, errno_val;
586
587 if (num_test_sockets < 1)
588 {
589 errno = EINVAL;
590 return -1;
591 }
592
593 if (num_test_sockets < scm->num_test_sockets)
594 {
595 for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
596 {
597 tsock = &scm->test_socket[i];
598#ifdef VCL_TEST
599 vppcom_session_close (tsock->fd);
600#else
601 close (tsock->fd);
602#endif
603 free (tsock->txbuf);
604 free (tsock->rxbuf);
605 }
606 }
607
608 else if (num_test_sockets > scm->num_test_sockets)
609 {
610 tsock = realloc (scm->test_socket,
611 sizeof (sock_test_socket_t) * num_test_sockets);
612 if (!tsock)
613 {
614 errno_val = errno;
615 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500616 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
617 errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400618 return -1;
619 }
620
Dave Wallaced48e9762017-08-22 23:29:33 -0400621 if (!scm->test_socket)
622 memset (tsock, 0, sizeof (*tsock));
623
Dave Wallace543852a2017-08-03 02:11:34 -0400624 scm->test_socket = tsock;
625 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
626 {
627 tsock = &scm->test_socket[i];
628#ifdef VCL_TEST
Dave Wallacede910062018-03-20 09:22:13 -0400629 tsock->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
630 VPPCOM_PROTO_UDP :
631 VPPCOM_PROTO_TCP,
632 1 /* is_nonblocking */ );
Dave Wallace543852a2017-08-03 02:11:34 -0400633 if (tsock->fd < 0)
634 {
635 errno = -tsock->fd;
636 tsock->fd = -1;
637 }
638#else
Dave Wallacede910062018-03-20 09:22:13 -0400639 tsock->fd = socket (ctrl->cfg.address_ip6 ? AF_INET6 : AF_INET,
640 ctrl->cfg.transport_udp ?
641 SOCK_DGRAM : SOCK_STREAM, 0);
Dave Wallace543852a2017-08-03 02:11:34 -0400642#endif
643 if (tsock->fd < 0)
644 {
645 errno_val = errno;
646 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500647 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400648 errno_val);
649 return tsock->fd;
650 }
651
652#ifdef VCL_TEST
653 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
Dave Wallaceee45d412017-11-24 21:44:06 -0500654 if (rv)
655 {
656 errno = -rv;
657 rv = -1;
658 }
Dave Wallace543852a2017-08-03 02:11:34 -0400659#else
660 rv =
661 connect (tsock->fd, (struct sockaddr *) &scm->server_addr,
Dave Wallacede910062018-03-20 09:22:13 -0400662 scm->server_addr_size);
Dave Wallace543852a2017-08-03 02:11:34 -0400663#endif
664 if (rv < 0)
665 {
666 errno_val = errno;
Dave Wallaceee45d412017-11-24 21:44:06 -0500667 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500668 fprintf (stderr, "CLIENT: ERROR: connect failed "
669 "(errno = %d)!\n", errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -0500670 return -1;
Dave Wallace543852a2017-08-03 02:11:34 -0400671 }
672 tsock->cfg = ctrl->cfg;
673 sock_test_socket_buf_alloc (tsock);
674 sock_test_cfg_sync (tsock);
675
676 printf ("CLIENT (fd %d): Test socket %d connected.\n",
677 tsock->fd, i);
678 }
679 }
680
681 scm->num_test_sockets = num_test_sockets;
682 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
683 return 0;
684}
685
686static void
687dump_help (void)
688{
689#define INDENT "\n "
690
Dave Wallace048b1d62018-01-03 22:24:41 -0500691 printf ("CLIENT: Test configuration commands:"
Dave Wallace543852a2017-08-03 02:11:34 -0400692 INDENT SOCK_TEST_TOKEN_HELP
693 "\t\t\tDisplay help."
694 INDENT SOCK_TEST_TOKEN_EXIT
695 "\t\t\tExit test client & server."
696 INDENT SOCK_TEST_TOKEN_SHOW_CFG
697 "\t\t\tShow the current test cfg."
698 INDENT SOCK_TEST_TOKEN_RUN_UNI
699 "\t\t\tRun the Uni-directional test."
700 INDENT SOCK_TEST_TOKEN_RUN_BI
701 "\t\t\tRun the Bi-directional test."
702 INDENT SOCK_TEST_TOKEN_VERBOSE
703 "\t\t\tToggle verbose setting."
704 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
705 "<rxbuf size>\tRx buffer size (bytes)."
706 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
707 "<txbuf size>\tTx buffer size (bytes)."
708 INDENT SOCK_TEST_TOKEN_NUM_WRITES
709 "<# of writes>\tNumber of txbuf writes to server." "\n");
710}
711
712static void
713cfg_txbuf_size_set (void)
714{
715 sock_client_main_t *scm = &sock_client_main;
716 sock_test_socket_t *ctrl = &scm->ctrl_socket;
717 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
718 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
719
720 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
721 {
722 ctrl->cfg.txbuf_size = txbuf_size;
723 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
724 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
725 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
726 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
727 }
728 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500729 fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
730 "minimum buf size (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400731 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
732}
733
734static void
735cfg_num_writes_set (void)
736{
737 sock_client_main_t *scm = &sock_client_main;
738 sock_test_socket_t *ctrl = &scm->ctrl_socket;
739 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
740 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
741
742 if (num_writes > 0)
743 {
744 ctrl->cfg.num_writes = num_writes;
745 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
746 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
747 }
748 else
749 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500750 fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
Dave Wallace543852a2017-08-03 02:11:34 -0400751 }
752}
753
754static void
755cfg_num_test_sockets_set (void)
756{
757 sock_client_main_t *scm = &sock_client_main;
758 sock_test_socket_t *ctrl = &scm->ctrl_socket;
759 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
760 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
761
762 if ((num_test_sockets > 0) &&
763 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
764 {
765 ctrl->cfg.num_test_sockets = num_test_sockets;
766 sock_test_connect_test_sockets (num_test_sockets);
Dave Wallaceee45d412017-11-24 21:44:06 -0500767
Dave Wallace543852a2017-08-03 02:11:34 -0400768 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
769 }
770 else
771 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500772 fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: "
773 "%u, (%d max)\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400774 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
775 }
776}
777
778static void
779cfg_rxbuf_size_set (void)
780{
781 sock_client_main_t *scm = &sock_client_main;
782 sock_test_socket_t *ctrl = &scm->ctrl_socket;
783 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
784 uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
785
786 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
787 {
788 ctrl->cfg.rxbuf_size = rxbuf_size;
789 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
790 (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
791 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
792 }
793 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500794 fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
795 "minimum buf size (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400796 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
797}
798
799static void
800cfg_verbose_toggle (void)
801{
802 sock_client_main_t *scm = &sock_client_main;
803 sock_test_socket_t *ctrl = &scm->ctrl_socket;
804
805 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
806 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
807
808}
809
810static sock_test_t
811parse_input ()
812{
813 sock_client_main_t *scm = &sock_client_main;
814 sock_test_socket_t *ctrl = &scm->ctrl_socket;
815 sock_test_t rv = SOCK_TEST_TYPE_NONE;
816
Neale Ranns756cd942018-04-06 09:18:11 -0700817 if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf,
818 strlen (SOCK_TEST_TOKEN_EXIT)))
Dave Wallace543852a2017-08-03 02:11:34 -0400819 rv = SOCK_TEST_TYPE_EXIT;
820
Neale Ranns756cd942018-04-06 09:18:11 -0700821 else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf,
822 strlen (SOCK_TEST_TOKEN_HELP)))
Dave Wallace543852a2017-08-03 02:11:34 -0400823 dump_help ();
824
Neale Ranns756cd942018-04-06 09:18:11 -0700825 else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf,
826 strlen (SOCK_TEST_TOKEN_SHOW_CFG)))
Dave Wallace543852a2017-08-03 02:11:34 -0400827 scm->dump_cfg = 1;
828
Neale Ranns756cd942018-04-06 09:18:11 -0700829 else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf,
830 strlen (SOCK_TEST_TOKEN_VERBOSE)))
Dave Wallace543852a2017-08-03 02:11:34 -0400831 cfg_verbose_toggle ();
832
833 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
834 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
835 cfg_txbuf_size_set ();
836
837 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
838 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
839 cfg_num_test_sockets_set ();
840
841 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
842 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
843 cfg_num_writes_set ();
844
845 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
846 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
847 cfg_rxbuf_size_set ();
848
849 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
850 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
851 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
852
853 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
854 strlen (SOCK_TEST_TOKEN_RUN_BI)))
855 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
856
857 else
858 rv = SOCK_TEST_TYPE_ECHO;
859
860 return rv;
861}
862
863void
864print_usage_and_exit (void)
865{
866 fprintf (stderr,
867 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
868 " OPTIONS\n"
869 " -h Print this message and exit.\n"
Dave Wallacede910062018-03-20 09:22:13 -0400870 " -6 Use IPv6\n"
871 " -u Use UDP transport layer\n"
Dave Wallace543852a2017-08-03 02:11:34 -0400872 " -c Print test config before test.\n"
873 " -w <dir> Write test results to <dir>.\n"
874 " -X Exit after running test.\n"
875 " -E Run Echo test.\n"
876 " -N <num-writes> Test Cfg: number of writes.\n"
877 " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
878 " -T <txbuf-size> Test Cfg: tx buffer size.\n"
879 " -U Run Uni-directional test.\n"
880 " -B Run Bi-directional test.\n"
881 " -V Verbose mode.\n");
882 exit (1);
883}
884
885int
886main (int argc, char **argv)
887{
888 sock_client_main_t *scm = &sock_client_main;
889 sock_test_socket_t *ctrl = &scm->ctrl_socket;
890 int c, rv, errno_val;
891 sock_test_t post_test = SOCK_TEST_TYPE_NONE;
892
893 sock_test_cfg_init (&ctrl->cfg);
894 sock_test_socket_buf_alloc (ctrl);
895
896 opterr = 0;
Dave Wallacede910062018-03-20 09:22:13 -0400897 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1)
Dave Wallace543852a2017-08-03 02:11:34 -0400898 switch (c)
899 {
900 case 'c':
901 scm->dump_cfg = 1;
902 break;
903
904 case 's':
905 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
906 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
907 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500908 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
909 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400910 print_usage_and_exit ();
911 }
912 if (!ctrl->cfg.num_test_sockets ||
913 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
914 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500915 fprintf (stderr, "CLIENT: ERROR: Invalid number of "
916 "sockets (%d) specified for option -%c!\n"
Dave Wallace543852a2017-08-03 02:11:34 -0400917 " Valid range is 1 - %d\n",
918 ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
919 print_usage_and_exit ();
920 }
921 break;
922
923 case 'w':
Dave Wallace048b1d62018-01-03 22:24:41 -0500924 fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400925 break;
926
927 case 'X':
928 post_test = SOCK_TEST_TYPE_EXIT;
929 break;
930
931 case 'E':
932 if (strlen (optarg) > ctrl->txbuf_size)
933 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500934 fprintf (stderr, "CLIENT: ERROR: Option -%c value "
935 "larger than txbuf size (%d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400936 optopt, ctrl->txbuf_size);
937 print_usage_and_exit ();
938 }
939 strcpy (ctrl->txbuf, optarg);
940 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
941 break;
942
943 case 'I':
944 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
945 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
946 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500947 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
948 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400949 print_usage_and_exit ();
950 }
951 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
952 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500953 fprintf (stderr, "CLIENT: ERROR: value greater than max "
954 "number test sockets (%d)!",
955 SOCK_TEST_CFG_MAX_TEST_SCKTS);
Dave Wallace543852a2017-08-03 02:11:34 -0400956 print_usage_and_exit ();
957 }
958 break;
959
960 case 'N':
961 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
962 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
963 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500964 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
965 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400966 print_usage_and_exit ();
967 }
968 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
969 break;
970
971 case 'R':
972 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
973 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
974 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500975 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
976 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400977 print_usage_and_exit ();
978 }
979 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
980 {
981 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
982 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
983 (uint8_t **) & ctrl->rxbuf,
984 &ctrl->rxbuf_size);
985 }
986 else
987 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500988 fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) "
989 "less than minumum (%u)\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400990 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
991 print_usage_and_exit ();
992 }
993
994 break;
995
996 case 'T':
997 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
998 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
999 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001000 fprintf (stderr, "CLIENT: ERROR: Invalid value "
1001 "for option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -04001002 print_usage_and_exit ();
1003 }
1004 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
1005 {
1006 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
1007 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
1008 (uint8_t **) & ctrl->txbuf,
1009 &ctrl->txbuf_size);
1010 ctrl->cfg.total_bytes =
1011 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
1012 }
1013 else
1014 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001015 fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) "
1016 "less than minumum (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -04001017 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
1018 print_usage_and_exit ();
1019 }
1020 break;
1021
1022 case 'U':
1023 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
1024 break;
1025
1026 case 'B':
1027 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
1028 break;
1029
1030 case 'V':
1031 ctrl->cfg.verbose = 1;
1032 break;
1033
Dave Wallacede910062018-03-20 09:22:13 -04001034 case '6':
1035 ctrl->cfg.address_ip6 = 1;
1036 break;
1037
1038 case 'D':
1039 ctrl->cfg.transport_udp = 1;
1040 break;
1041
Dave Wallace543852a2017-08-03 02:11:34 -04001042 case '?':
1043 switch (optopt)
1044 {
1045 case 'E':
1046 case 'I':
1047 case 'N':
1048 case 'R':
1049 case 'T':
1050 case 'w':
Dave Wallace048b1d62018-01-03 22:24:41 -05001051 fprintf (stderr, "CLIENT: ERROR: Option -%c "
1052 "requires an argument.\n", optopt);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001053 break;
1054
Dave Wallace543852a2017-08-03 02:11:34 -04001055 default:
1056 if (isprint (optopt))
Dave Wallace048b1d62018-01-03 22:24:41 -05001057 fprintf (stderr, "CLIENT: ERROR: Unknown "
1058 "option `-%c'.\n", optopt);
Dave Wallace543852a2017-08-03 02:11:34 -04001059 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001060 fprintf (stderr, "CLIENT: ERROR: Unknown "
1061 "option character `\\x%x'.\n", optopt);
Dave Wallace543852a2017-08-03 02:11:34 -04001062 }
1063 /* fall thru */
1064 case 'h':
1065 default:
1066 print_usage_and_exit ();
1067 }
1068
1069 if (argc < (optind + 2))
1070 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001071 fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n");
Dave Wallace543852a2017-08-03 02:11:34 -04001072 print_usage_and_exit ();
1073 }
1074
1075#ifdef VCL_TEST
1076 ctrl->fd = vppcom_app_create ("vcl_test_client");
1077 if (ctrl->fd < 0)
1078 {
1079 errno = -ctrl->fd;
1080 ctrl->fd = -1;
1081 }
1082 else
1083 {
Dave Wallacede910062018-03-20 09:22:13 -04001084 ctrl->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
1085 VPPCOM_PROTO_UDP :
1086 VPPCOM_PROTO_TCP,
Dave Wallace543852a2017-08-03 02:11:34 -04001087 0 /* is_nonblocking */ );
1088 if (ctrl->fd < 0)
1089 {
1090 errno = -ctrl->fd;
1091 ctrl->fd = -1;
1092 }
1093 }
1094#else
Dave Wallacede910062018-03-20 09:22:13 -04001095 ctrl->fd = socket (ctrl->cfg.address_ip6 ? AF_INET6 : AF_INET,
1096 ctrl->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM, 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001097#endif
1098
1099 if (ctrl->fd < 0)
1100 {
1101 errno_val = errno;
1102 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -05001103 fprintf (stderr, "CLIENT: ERROR: socket "
1104 "failed (errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -04001105 return ctrl->fd;
1106 }
1107
1108 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
Dave Wallacede910062018-03-20 09:22:13 -04001109 if (ctrl->cfg.address_ip6)
1110 {
1111 struct sockaddr_in6 *server_addr =
1112 (struct sockaddr_in6 *) &scm->server_addr;
1113 scm->server_addr_size = sizeof (*server_addr);
1114 server_addr->sin6_family = AF_INET6;
1115 inet_pton (AF_INET6, argv[optind++], &(server_addr->sin6_addr));
1116 server_addr->sin6_port = htons (atoi (argv[optind]));
1117 }
1118 else
1119 {
1120 struct sockaddr_in *server_addr =
1121 (struct sockaddr_in *) &scm->server_addr;
1122 scm->server_addr_size = sizeof (*server_addr);
1123 server_addr->sin_family = AF_INET;
1124 inet_pton (AF_INET, argv[optind++], &(server_addr->sin_addr));
1125 server_addr->sin_port = htons (atoi (argv[optind]));
1126 }
Dave Wallace543852a2017-08-03 02:11:34 -04001127
1128#ifdef VCL_TEST
Dave Wallacede910062018-03-20 09:22:13 -04001129 if (ctrl->cfg.address_ip6)
1130 {
1131 struct sockaddr_in6 *server_addr =
1132 (struct sockaddr_in6 *) &scm->server_addr;
1133 scm->server_endpt.is_ip4 = 0;
1134 scm->server_endpt.ip = (uint8_t *) & server_addr->sin6_addr;
1135 scm->server_endpt.port = (uint16_t) server_addr->sin6_port;
1136 }
1137 else
1138 {
1139 struct sockaddr_in *server_addr =
1140 (struct sockaddr_in *) &scm->server_addr;
1141 scm->server_endpt.is_ip4 = 1;
1142 scm->server_endpt.ip = (uint8_t *) & server_addr->sin_addr;
1143 scm->server_endpt.port = (uint16_t) server_addr->sin_port;
1144 }
Dave Wallace543852a2017-08-03 02:11:34 -04001145#endif
1146
1147 do
1148 {
1149 printf ("\nCLIENT: Connecting to server...\n");
1150
1151#ifdef VCL_TEST
1152 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
Dave Wallaceee45d412017-11-24 21:44:06 -05001153 if (rv)
1154 {
1155 errno = -rv;
1156 rv = -1;
1157 }
Dave Wallace543852a2017-08-03 02:11:34 -04001158#else
1159 rv =
1160 connect (ctrl->fd, (struct sockaddr *) &scm->server_addr,
Dave Wallacede910062018-03-20 09:22:13 -04001161 scm->server_addr_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001162#endif
1163 if (rv < 0)
1164 {
1165 errno_val = errno;
1166 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -05001167 fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -04001168 errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -05001169 return -1;
Dave Wallace543852a2017-08-03 02:11:34 -04001170 }
1171
1172 sock_test_cfg_sync (ctrl);
1173 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
1174 }
1175 while (rv < 0);
1176
1177 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
1178
1179 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
1180 {
1181 if (scm->dump_cfg)
1182 {
1183 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
1184 scm->dump_cfg = 0;
1185 }
1186
1187 switch (ctrl->cfg.test)
1188 {
1189 case SOCK_TEST_TYPE_ECHO:
1190 echo_test_client ();
1191 break;
1192
1193 case SOCK_TEST_TYPE_UNI:
1194 case SOCK_TEST_TYPE_BI:
1195 stream_test_client (ctrl->cfg.test);
1196 break;
1197
1198 case SOCK_TEST_TYPE_EXIT:
1199 continue;
1200
1201 case SOCK_TEST_TYPE_NONE:
1202 default:
1203 break;
1204 }
1205 switch (post_test)
1206 {
1207 case SOCK_TEST_TYPE_EXIT:
1208 switch (ctrl->cfg.test)
1209 {
1210 case SOCK_TEST_TYPE_EXIT:
1211 case SOCK_TEST_TYPE_UNI:
1212 case SOCK_TEST_TYPE_BI:
1213 case SOCK_TEST_TYPE_ECHO:
1214 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1215 continue;
1216
1217 case SOCK_TEST_TYPE_NONE:
1218 default:
1219 break;
1220 }
1221 break;
1222
1223 case SOCK_TEST_TYPE_NONE:
1224 case SOCK_TEST_TYPE_ECHO:
1225 case SOCK_TEST_TYPE_UNI:
1226 case SOCK_TEST_TYPE_BI:
1227 default:
1228 break;
1229 }
1230
1231 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1232 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1233
Dave Wallace048b1d62018-01-03 22:24:41 -05001234 printf ("\nCLIENT: Type some characters and hit <return>\n"
Dave Wallace543852a2017-08-03 02:11:34 -04001235 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1236
1237 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1238 {
1239 if (strlen (ctrl->txbuf) == 1)
1240 {
1241 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1242 continue;
1243 }
1244 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1245
1246 /* Parse input for keywords */
1247 ctrl->cfg.test = parse_input ();
1248 }
1249 }
1250
1251 exit_client ();
1252#ifdef VCL_TEST
1253 vppcom_session_close (ctrl->fd);
1254 vppcom_app_destroy ();
Dave Wallace3ee1fe12018-02-23 01:09:11 -05001255 return 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001256#else
1257 close (ctrl->fd);
Dave Wallace3ee1fe12018-02-23 01:09:11 -05001258 return (scm->af_unix_echo_tx == scm->af_unix_echo_rx) ? 0 : -1;
Dave Wallace543852a2017-08-03 02:11:34 -04001259#endif
Dave Wallace543852a2017-08-03 02:11:34 -04001260}
1261
1262/*
1263 * fd.io coding-style-patch-verification: ON
1264 *
1265 * Local Variables:
1266 * eval: (c-set-style "gnu")
1267 * End:
1268 */