blob: de77fc9afe2a5f2135be1359ccd2d69f52cd402b [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
Florin Coras6d4bb422018-09-04 22:07:27 -070047sock_client_main_t vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -040048
49
50static int
51sock_test_cfg_sync (sock_test_socket_t * socket)
52{
Florin Coras6d4bb422018-09-04 22:07:27 -070053 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -040054 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{
Florin Coras6d4bb422018-09-04 22:07:27 -0700120 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400121 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{
Florin Coras6d4bb422018-09-04 22:07:27 -0700364 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400365 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 {
Florin Coras54693d22018-07-17 10:46:29 -0700458 tx_bytes = sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
459 ctrl->cfg.txbuf_size, &tsock->stats,
460 ctrl->cfg.verbose);
Dave Wallace543852a2017-08-03 02:11:34 -0400461 if (tx_bytes < 0)
462 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500463 fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) "
464 "failed -- aborting test!\n", tsock->fd);
Dave Wallace543852a2017-08-03 02:11:34 -0400465 return;
466 }
467 }
468
Dave Wallace543852a2017-08-03 02:11:34 -0400469 if (((test == SOCK_TEST_TYPE_UNI) &&
470 (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) ||
471 ((test == SOCK_TEST_TYPE_BI) &&
472 (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)))
473 {
474 clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
475 n--;
476 }
477 }
478 }
479 clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
480
481 printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
482 ctrl->fd);
483
484 if (sock_test_cfg_sync (ctrl))
485 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500486 fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
Dave Wallace543852a2017-08-03 02:11:34 -0400487 return;
488 }
489
490 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
491 {
492 tsock = &scm->test_socket[i];
493
494 if (ctrl->cfg.verbose)
495 {
496 static char buf[64];
497
498 sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
499 sock_test_stats_dump (buf, &tsock->stats,
500 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
501 1 /* show tx */ , ctrl->cfg.verbose);
502 }
503
504 sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
505 }
506
507 sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
508 test == SOCK_TEST_TYPE_BI /* show_rx */ ,
509 1 /* show tx */ , ctrl->cfg.verbose);
510 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
511
512 if (ctrl->cfg.verbose)
513 {
514 printf (" ctrl socket info\n"
515 SOCK_TEST_SEPARATOR_STRING
516 " fd: %d (0x%08x)\n"
517 " rxbuf: %p\n"
518 " rxbuf size: %u (0x%08x)\n"
519 " txbuf: %p\n"
520 " txbuf size: %u (0x%08x)\n"
521 SOCK_TEST_SEPARATOR_STRING,
522 ctrl->fd, (uint32_t) ctrl->fd,
523 ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
524 ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
525 }
526
527 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
528 if (sock_test_cfg_sync (ctrl))
Dave Wallace048b1d62018-01-03 22:24:41 -0500529 fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!");
Dave Wallace543852a2017-08-03 02:11:34 -0400530
531 printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n"
532 SOCK_TEST_BANNER_STRING "\n", ctrl->fd,
533 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
534}
535
536static void
537exit_client (void)
538{
Florin Coras6d4bb422018-09-04 22:07:27 -0700539 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400540 sock_test_socket_t *ctrl = &scm->ctrl_socket;
541 sock_test_socket_t *tsock;
542 int i;
543
Dave Wallace3ee1fe12018-02-23 01:09:11 -0500544#ifndef VCL_TEST
545 printf ("CLIENT: af_unix_echo_tx %d, af_unix_echo_rx %d\n",
546 scm->af_unix_echo_tx, scm->af_unix_echo_rx);
547#endif
Dave Wallace543852a2017-08-03 02:11:34 -0400548 for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
549 {
550 tsock = &scm->test_socket[i];
551 tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
552
Chris Lukeb2bcad62017-09-18 08:51:22 -0400553 /* coverity[COPY_PASTE_ERROR] */
Dave Wallace543852a2017-08-03 02:11:34 -0400554 if (ctrl->cfg.verbose)
555 {
556 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
557 tsock->fd);
558 sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
559 }
560 (void) sock_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
561 sizeof (tsock->cfg), &tsock->stats,
562 ctrl->cfg.verbose);
563 }
564
565 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
566 if (ctrl->cfg.verbose)
567 {
568 printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
569 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
570 }
571 (void) sock_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
572 sizeof (ctrl->cfg), &ctrl->stats,
573 ctrl->cfg.verbose);
574 printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
575 sleep (1);
576}
577
578static int
579sock_test_connect_test_sockets (uint32_t num_test_sockets)
580{
Florin Coras6d4bb422018-09-04 22:07:27 -0700581 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400582 sock_test_socket_t *ctrl = &scm->ctrl_socket;
583 sock_test_socket_t *tsock;
584 int i, rv, errno_val;
585
586 if (num_test_sockets < 1)
587 {
588 errno = EINVAL;
589 return -1;
590 }
591
592 if (num_test_sockets < scm->num_test_sockets)
593 {
594 for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
595 {
596 tsock = &scm->test_socket[i];
597#ifdef VCL_TEST
598 vppcom_session_close (tsock->fd);
599#else
600 close (tsock->fd);
601#endif
602 free (tsock->txbuf);
603 free (tsock->rxbuf);
604 }
605 }
606
607 else if (num_test_sockets > scm->num_test_sockets)
608 {
609 tsock = realloc (scm->test_socket,
610 sizeof (sock_test_socket_t) * num_test_sockets);
611 if (!tsock)
612 {
613 errno_val = errno;
614 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500615 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
616 errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -0400617 return -1;
618 }
619
Dave Wallaced48e9762017-08-22 23:29:33 -0400620 if (!scm->test_socket)
621 memset (tsock, 0, sizeof (*tsock));
622
Dave Wallace543852a2017-08-03 02:11:34 -0400623 scm->test_socket = tsock;
624 for (i = scm->num_test_sockets; i < num_test_sockets; i++)
625 {
626 tsock = &scm->test_socket[i];
627#ifdef VCL_TEST
Dave Wallacede910062018-03-20 09:22:13 -0400628 tsock->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
629 VPPCOM_PROTO_UDP :
630 VPPCOM_PROTO_TCP,
631 1 /* is_nonblocking */ );
Dave Wallace543852a2017-08-03 02:11:34 -0400632 if (tsock->fd < 0)
633 {
634 errno = -tsock->fd;
635 tsock->fd = -1;
636 }
637#else
Dave Wallacede910062018-03-20 09:22:13 -0400638 tsock->fd = socket (ctrl->cfg.address_ip6 ? AF_INET6 : AF_INET,
639 ctrl->cfg.transport_udp ?
640 SOCK_DGRAM : SOCK_STREAM, 0);
Dave Wallace543852a2017-08-03 02:11:34 -0400641#endif
642 if (tsock->fd < 0)
643 {
644 errno_val = errno;
645 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500646 fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400647 errno_val);
648 return tsock->fd;
649 }
650
651#ifdef VCL_TEST
652 rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
Dave Wallaceee45d412017-11-24 21:44:06 -0500653 if (rv)
654 {
655 errno = -rv;
656 rv = -1;
657 }
Dave Wallace543852a2017-08-03 02:11:34 -0400658#else
659 rv =
660 connect (tsock->fd, (struct sockaddr *) &scm->server_addr,
Dave Wallacede910062018-03-20 09:22:13 -0400661 scm->server_addr_size);
Dave Wallace543852a2017-08-03 02:11:34 -0400662#endif
663 if (rv < 0)
664 {
665 errno_val = errno;
Dave Wallaceee45d412017-11-24 21:44:06 -0500666 perror ("ERROR in sock_test_connect_test_sockets()");
Dave Wallace048b1d62018-01-03 22:24:41 -0500667 fprintf (stderr, "CLIENT: ERROR: connect failed "
668 "(errno = %d)!\n", errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -0500669 return -1;
Dave Wallace543852a2017-08-03 02:11:34 -0400670 }
671 tsock->cfg = ctrl->cfg;
672 sock_test_socket_buf_alloc (tsock);
673 sock_test_cfg_sync (tsock);
674
675 printf ("CLIENT (fd %d): Test socket %d connected.\n",
676 tsock->fd, i);
677 }
678 }
679
680 scm->num_test_sockets = num_test_sockets;
681 printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
682 return 0;
683}
684
685static void
686dump_help (void)
687{
688#define INDENT "\n "
689
Dave Wallace048b1d62018-01-03 22:24:41 -0500690 printf ("CLIENT: Test configuration commands:"
Dave Wallace543852a2017-08-03 02:11:34 -0400691 INDENT SOCK_TEST_TOKEN_HELP
692 "\t\t\tDisplay help."
693 INDENT SOCK_TEST_TOKEN_EXIT
694 "\t\t\tExit test client & server."
695 INDENT SOCK_TEST_TOKEN_SHOW_CFG
696 "\t\t\tShow the current test cfg."
697 INDENT SOCK_TEST_TOKEN_RUN_UNI
698 "\t\t\tRun the Uni-directional test."
699 INDENT SOCK_TEST_TOKEN_RUN_BI
700 "\t\t\tRun the Bi-directional test."
701 INDENT SOCK_TEST_TOKEN_VERBOSE
702 "\t\t\tToggle verbose setting."
703 INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
704 "<rxbuf size>\tRx buffer size (bytes)."
705 INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
706 "<txbuf size>\tTx buffer size (bytes)."
707 INDENT SOCK_TEST_TOKEN_NUM_WRITES
708 "<# of writes>\tNumber of txbuf writes to server." "\n");
709}
710
711static void
712cfg_txbuf_size_set (void)
713{
Florin Coras6d4bb422018-09-04 22:07:27 -0700714 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400715 sock_test_socket_t *ctrl = &scm->ctrl_socket;
716 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
717 uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
718
719 if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
720 {
721 ctrl->cfg.txbuf_size = txbuf_size;
722 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
723 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
724 (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
725 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
726 }
727 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500728 fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
729 "minimum buf size (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400730 txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
731}
732
733static void
734cfg_num_writes_set (void)
735{
Florin Coras6d4bb422018-09-04 22:07:27 -0700736 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400737 sock_test_socket_t *ctrl = &scm->ctrl_socket;
738 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
739 uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
740
741 if (num_writes > 0)
742 {
743 ctrl->cfg.num_writes = num_writes;
744 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
745 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
746 }
747 else
748 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500749 fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
Dave Wallace543852a2017-08-03 02:11:34 -0400750 }
751}
752
753static void
754cfg_num_test_sockets_set (void)
755{
Florin Coras6d4bb422018-09-04 22:07:27 -0700756 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400757 sock_test_socket_t *ctrl = &scm->ctrl_socket;
758 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
759 uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
760
761 if ((num_test_sockets > 0) &&
762 (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
763 {
764 ctrl->cfg.num_test_sockets = num_test_sockets;
765 sock_test_connect_test_sockets (num_test_sockets);
Dave Wallaceee45d412017-11-24 21:44:06 -0500766
Dave Wallace543852a2017-08-03 02:11:34 -0400767 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
768 }
769 else
770 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500771 fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: "
772 "%u, (%d max)\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400773 num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
774 }
775}
776
777static void
778cfg_rxbuf_size_set (void)
779{
Florin Coras6d4bb422018-09-04 22:07:27 -0700780 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400781 sock_test_socket_t *ctrl = &scm->ctrl_socket;
782 char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
783 uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
784
785 if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
786 {
787 ctrl->cfg.rxbuf_size = rxbuf_size;
788 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
789 (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
790 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
791 }
792 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500793 fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
794 "minimum buf size (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400795 rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
796}
797
798static void
799cfg_verbose_toggle (void)
800{
Florin Coras6d4bb422018-09-04 22:07:27 -0700801 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400802 sock_test_socket_t *ctrl = &scm->ctrl_socket;
803
804 ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
805 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
806
807}
808
809static sock_test_t
810parse_input ()
811{
Florin Coras6d4bb422018-09-04 22:07:27 -0700812 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400813 sock_test_socket_t *ctrl = &scm->ctrl_socket;
814 sock_test_t rv = SOCK_TEST_TYPE_NONE;
815
Neale Ranns756cd942018-04-06 09:18:11 -0700816 if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf,
817 strlen (SOCK_TEST_TOKEN_EXIT)))
Dave Wallace543852a2017-08-03 02:11:34 -0400818 rv = SOCK_TEST_TYPE_EXIT;
819
Neale Ranns756cd942018-04-06 09:18:11 -0700820 else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf,
821 strlen (SOCK_TEST_TOKEN_HELP)))
Dave Wallace543852a2017-08-03 02:11:34 -0400822 dump_help ();
823
Neale Ranns756cd942018-04-06 09:18:11 -0700824 else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf,
825 strlen (SOCK_TEST_TOKEN_SHOW_CFG)))
Dave Wallace543852a2017-08-03 02:11:34 -0400826 scm->dump_cfg = 1;
827
Neale Ranns756cd942018-04-06 09:18:11 -0700828 else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf,
829 strlen (SOCK_TEST_TOKEN_VERBOSE)))
Dave Wallace543852a2017-08-03 02:11:34 -0400830 cfg_verbose_toggle ();
831
832 else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
833 strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
834 cfg_txbuf_size_set ();
835
836 else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
837 strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
838 cfg_num_test_sockets_set ();
839
840 else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
841 strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
842 cfg_num_writes_set ();
843
844 else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
845 strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
846 cfg_rxbuf_size_set ();
847
848 else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
849 strlen (SOCK_TEST_TOKEN_RUN_UNI)))
850 rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
851
852 else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
853 strlen (SOCK_TEST_TOKEN_RUN_BI)))
854 rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
855
856 else
857 rv = SOCK_TEST_TYPE_ECHO;
858
859 return rv;
860}
861
862void
863print_usage_and_exit (void)
864{
865 fprintf (stderr,
866 "sock_test_client [OPTIONS] <ipaddr> <port>\n"
867 " OPTIONS\n"
868 " -h Print this message and exit.\n"
Dave Wallacede910062018-03-20 09:22:13 -0400869 " -6 Use IPv6\n"
870 " -u Use UDP transport layer\n"
Dave Wallace543852a2017-08-03 02:11:34 -0400871 " -c Print test config before test.\n"
872 " -w <dir> Write test results to <dir>.\n"
873 " -X Exit after running test.\n"
874 " -E Run Echo test.\n"
875 " -N <num-writes> Test Cfg: number of writes.\n"
876 " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
877 " -T <txbuf-size> Test Cfg: tx buffer size.\n"
878 " -U Run Uni-directional test.\n"
879 " -B Run Bi-directional test.\n"
880 " -V Verbose mode.\n");
881 exit (1);
882}
883
884int
885main (int argc, char **argv)
886{
Florin Coras6d4bb422018-09-04 22:07:27 -0700887 sock_client_main_t *scm = &vcl_client_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400888 sock_test_socket_t *ctrl = &scm->ctrl_socket;
889 int c, rv, errno_val;
890 sock_test_t post_test = SOCK_TEST_TYPE_NONE;
891
892 sock_test_cfg_init (&ctrl->cfg);
893 sock_test_socket_buf_alloc (ctrl);
894
895 opterr = 0;
Dave Wallacede910062018-03-20 09:22:13 -0400896 while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1)
Dave Wallace543852a2017-08-03 02:11:34 -0400897 switch (c)
898 {
899 case 'c':
900 scm->dump_cfg = 1;
901 break;
902
903 case 's':
904 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
905 if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
906 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500907 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
908 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400909 print_usage_and_exit ();
910 }
911 if (!ctrl->cfg.num_test_sockets ||
912 (ctrl->cfg.num_test_sockets > FD_SETSIZE))
913 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500914 fprintf (stderr, "CLIENT: ERROR: Invalid number of "
915 "sockets (%d) specified for option -%c!\n"
Dave Wallace543852a2017-08-03 02:11:34 -0400916 " Valid range is 1 - %d\n",
917 ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
918 print_usage_and_exit ();
919 }
920 break;
921
922 case 'w':
Dave Wallace048b1d62018-01-03 22:24:41 -0500923 fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n");
Dave Wallace543852a2017-08-03 02:11:34 -0400924 break;
925
926 case 'X':
927 post_test = SOCK_TEST_TYPE_EXIT;
928 break;
929
930 case 'E':
931 if (strlen (optarg) > ctrl->txbuf_size)
932 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500933 fprintf (stderr, "CLIENT: ERROR: Option -%c value "
934 "larger than txbuf size (%d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400935 optopt, ctrl->txbuf_size);
936 print_usage_and_exit ();
937 }
938 strcpy (ctrl->txbuf, optarg);
939 ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
940 break;
941
942 case 'I':
943 if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
944 if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
945 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500946 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
947 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400948 print_usage_and_exit ();
949 }
950 if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
951 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500952 fprintf (stderr, "CLIENT: ERROR: value greater than max "
953 "number test sockets (%d)!",
954 SOCK_TEST_CFG_MAX_TEST_SCKTS);
Dave Wallace543852a2017-08-03 02:11:34 -0400955 print_usage_and_exit ();
956 }
957 break;
958
959 case 'N':
960 if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
961 if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
962 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500963 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
964 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400965 print_usage_and_exit ();
966 }
967 ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
968 break;
969
970 case 'R':
971 if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
972 if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
973 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500974 fprintf (stderr, "CLIENT: ERROR: Invalid value for "
975 "option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -0400976 print_usage_and_exit ();
977 }
978 if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
979 {
980 ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
981 sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
982 (uint8_t **) & ctrl->rxbuf,
983 &ctrl->rxbuf_size);
984 }
985 else
986 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500987 fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) "
988 "less than minumum (%u)\n",
Dave Wallace543852a2017-08-03 02:11:34 -0400989 ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
990 print_usage_and_exit ();
991 }
992
993 break;
994
995 case 'T':
996 if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
997 if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
998 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500999 fprintf (stderr, "CLIENT: ERROR: Invalid value "
1000 "for option -%c!\n", c);
Dave Wallace543852a2017-08-03 02:11:34 -04001001 print_usage_and_exit ();
1002 }
1003 if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
1004 {
1005 ctrl->txbuf_size = ctrl->cfg.txbuf_size;
1006 sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
1007 (uint8_t **) & ctrl->txbuf,
1008 &ctrl->txbuf_size);
1009 ctrl->cfg.total_bytes =
1010 ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
1011 }
1012 else
1013 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001014 fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) "
1015 "less than minumum (%u)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -04001016 ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
1017 print_usage_and_exit ();
1018 }
1019 break;
1020
1021 case 'U':
1022 ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
1023 break;
1024
1025 case 'B':
1026 ctrl->cfg.test = SOCK_TEST_TYPE_BI;
1027 break;
1028
1029 case 'V':
1030 ctrl->cfg.verbose = 1;
1031 break;
1032
Dave Wallacede910062018-03-20 09:22:13 -04001033 case '6':
1034 ctrl->cfg.address_ip6 = 1;
1035 break;
1036
1037 case 'D':
1038 ctrl->cfg.transport_udp = 1;
1039 break;
1040
Dave Wallace543852a2017-08-03 02:11:34 -04001041 case '?':
1042 switch (optopt)
1043 {
1044 case 'E':
1045 case 'I':
1046 case 'N':
1047 case 'R':
1048 case 'T':
1049 case 'w':
Dave Wallace048b1d62018-01-03 22:24:41 -05001050 fprintf (stderr, "CLIENT: ERROR: Option -%c "
1051 "requires an argument.\n", optopt);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001052 break;
1053
Dave Wallace543852a2017-08-03 02:11:34 -04001054 default:
1055 if (isprint (optopt))
Dave Wallace048b1d62018-01-03 22:24:41 -05001056 fprintf (stderr, "CLIENT: ERROR: Unknown "
1057 "option `-%c'.\n", optopt);
Dave Wallace543852a2017-08-03 02:11:34 -04001058 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001059 fprintf (stderr, "CLIENT: ERROR: Unknown "
1060 "option character `\\x%x'.\n", optopt);
Dave Wallace543852a2017-08-03 02:11:34 -04001061 }
1062 /* fall thru */
1063 case 'h':
1064 default:
1065 print_usage_and_exit ();
1066 }
1067
1068 if (argc < (optind + 2))
1069 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001070 fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n");
Dave Wallace543852a2017-08-03 02:11:34 -04001071 print_usage_and_exit ();
1072 }
1073
1074#ifdef VCL_TEST
1075 ctrl->fd = vppcom_app_create ("vcl_test_client");
1076 if (ctrl->fd < 0)
1077 {
1078 errno = -ctrl->fd;
1079 ctrl->fd = -1;
1080 }
1081 else
1082 {
Dave Wallacede910062018-03-20 09:22:13 -04001083 ctrl->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
1084 VPPCOM_PROTO_UDP :
1085 VPPCOM_PROTO_TCP,
Dave Wallace543852a2017-08-03 02:11:34 -04001086 0 /* is_nonblocking */ );
1087 if (ctrl->fd < 0)
1088 {
1089 errno = -ctrl->fd;
1090 ctrl->fd = -1;
1091 }
1092 }
1093#else
Dave Wallacede910062018-03-20 09:22:13 -04001094 ctrl->fd = socket (ctrl->cfg.address_ip6 ? AF_INET6 : AF_INET,
1095 ctrl->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM, 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001096#endif
1097
1098 if (ctrl->fd < 0)
1099 {
1100 errno_val = errno;
1101 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -05001102 fprintf (stderr, "CLIENT: ERROR: socket "
1103 "failed (errno = %d)!\n", errno_val);
Dave Wallace543852a2017-08-03 02:11:34 -04001104 return ctrl->fd;
1105 }
1106
1107 memset (&scm->server_addr, 0, sizeof (scm->server_addr));
Dave Wallacede910062018-03-20 09:22:13 -04001108 if (ctrl->cfg.address_ip6)
1109 {
1110 struct sockaddr_in6 *server_addr =
1111 (struct sockaddr_in6 *) &scm->server_addr;
1112 scm->server_addr_size = sizeof (*server_addr);
1113 server_addr->sin6_family = AF_INET6;
1114 inet_pton (AF_INET6, argv[optind++], &(server_addr->sin6_addr));
1115 server_addr->sin6_port = htons (atoi (argv[optind]));
1116 }
1117 else
1118 {
1119 struct sockaddr_in *server_addr =
1120 (struct sockaddr_in *) &scm->server_addr;
1121 scm->server_addr_size = sizeof (*server_addr);
1122 server_addr->sin_family = AF_INET;
1123 inet_pton (AF_INET, argv[optind++], &(server_addr->sin_addr));
1124 server_addr->sin_port = htons (atoi (argv[optind]));
1125 }
Dave Wallace543852a2017-08-03 02:11:34 -04001126
1127#ifdef VCL_TEST
Dave Wallacede910062018-03-20 09:22:13 -04001128 if (ctrl->cfg.address_ip6)
1129 {
1130 struct sockaddr_in6 *server_addr =
1131 (struct sockaddr_in6 *) &scm->server_addr;
1132 scm->server_endpt.is_ip4 = 0;
1133 scm->server_endpt.ip = (uint8_t *) & server_addr->sin6_addr;
1134 scm->server_endpt.port = (uint16_t) server_addr->sin6_port;
1135 }
1136 else
1137 {
1138 struct sockaddr_in *server_addr =
1139 (struct sockaddr_in *) &scm->server_addr;
1140 scm->server_endpt.is_ip4 = 1;
1141 scm->server_endpt.ip = (uint8_t *) & server_addr->sin_addr;
1142 scm->server_endpt.port = (uint16_t) server_addr->sin_port;
1143 }
Dave Wallace543852a2017-08-03 02:11:34 -04001144#endif
1145
1146 do
1147 {
1148 printf ("\nCLIENT: Connecting to server...\n");
1149
1150#ifdef VCL_TEST
1151 rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
Dave Wallaceee45d412017-11-24 21:44:06 -05001152 if (rv)
1153 {
1154 errno = -rv;
1155 rv = -1;
1156 }
Dave Wallace543852a2017-08-03 02:11:34 -04001157#else
1158 rv =
1159 connect (ctrl->fd, (struct sockaddr *) &scm->server_addr,
Dave Wallacede910062018-03-20 09:22:13 -04001160 scm->server_addr_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001161#endif
1162 if (rv < 0)
1163 {
1164 errno_val = errno;
1165 perror ("ERROR in main()");
Dave Wallace048b1d62018-01-03 22:24:41 -05001166 fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n",
Dave Wallace543852a2017-08-03 02:11:34 -04001167 errno_val);
Dave Wallaceee45d412017-11-24 21:44:06 -05001168 return -1;
Dave Wallace543852a2017-08-03 02:11:34 -04001169 }
1170
1171 sock_test_cfg_sync (ctrl);
1172 printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
1173 }
1174 while (rv < 0);
1175
1176 sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);
1177
1178 while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
1179 {
1180 if (scm->dump_cfg)
1181 {
1182 sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
1183 scm->dump_cfg = 0;
1184 }
1185
1186 switch (ctrl->cfg.test)
1187 {
1188 case SOCK_TEST_TYPE_ECHO:
1189 echo_test_client ();
1190 break;
1191
1192 case SOCK_TEST_TYPE_UNI:
1193 case SOCK_TEST_TYPE_BI:
1194 stream_test_client (ctrl->cfg.test);
1195 break;
1196
1197 case SOCK_TEST_TYPE_EXIT:
1198 continue;
1199
1200 case SOCK_TEST_TYPE_NONE:
1201 default:
1202 break;
1203 }
1204 switch (post_test)
1205 {
1206 case SOCK_TEST_TYPE_EXIT:
1207 switch (ctrl->cfg.test)
1208 {
1209 case SOCK_TEST_TYPE_EXIT:
1210 case SOCK_TEST_TYPE_UNI:
1211 case SOCK_TEST_TYPE_BI:
1212 case SOCK_TEST_TYPE_ECHO:
1213 ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1214 continue;
1215
1216 case SOCK_TEST_TYPE_NONE:
1217 default:
1218 break;
1219 }
1220 break;
1221
1222 case SOCK_TEST_TYPE_NONE:
1223 case SOCK_TEST_TYPE_ECHO:
1224 case SOCK_TEST_TYPE_UNI:
1225 case SOCK_TEST_TYPE_BI:
1226 default:
1227 break;
1228 }
1229
1230 memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1231 memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1232
Dave Wallace048b1d62018-01-03 22:24:41 -05001233 printf ("\nCLIENT: Type some characters and hit <return>\n"
Dave Wallace543852a2017-08-03 02:11:34 -04001234 "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1235
1236 if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1237 {
1238 if (strlen (ctrl->txbuf) == 1)
1239 {
1240 printf ("\nCLIENT: Nothing to send! Please try again...\n");
1241 continue;
1242 }
1243 ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1244
1245 /* Parse input for keywords */
1246 ctrl->cfg.test = parse_input ();
1247 }
1248 }
1249
1250 exit_client ();
1251#ifdef VCL_TEST
1252 vppcom_session_close (ctrl->fd);
1253 vppcom_app_destroy ();
Dave Wallace3ee1fe12018-02-23 01:09:11 -05001254 return 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001255#else
1256 close (ctrl->fd);
Dave Wallace3ee1fe12018-02-23 01:09:11 -05001257 return (scm->af_unix_echo_tx == scm->af_unix_echo_rx) ? 0 : -1;
Dave Wallace543852a2017-08-03 02:11:34 -04001258#endif
Dave Wallace543852a2017-08-03 02:11:34 -04001259}
1260
1261/*
1262 * fd.io coding-style-patch-verification: ON
1263 *
1264 * Local Variables:
1265 * eval: (c-set-style "gnu")
1266 * End:
1267 */