FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
sock_test_client.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <unistd.h>
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <ctype.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <stdio.h>
23 #include <time.h>
24 #include <arpa/inet.h>
25 #include <uri/sock_test.h>
26 
27 typedef struct
28 {
29 #ifdef VCL_TEST
30  vppcom_endpt_t server_endpt;
31 #endif
32  struct sockaddr_in server_addr;
35  uint32_t num_test_sockets;
36  uint8_t dump_cfg;
38 
40 
41 
42 static int
44 {
46  sock_test_socket_t *ctrl = &scm->ctrl_socket;
47  sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf;
48  int rx_bytes, tx_bytes;
49 
50  if (socket->cfg.verbose)
51  sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ );
52 
53  tx_bytes = sock_test_write (socket->fd, (uint8_t *) & ctrl->cfg,
54  sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose);
55  if (tx_bytes < 0)
56  {
57  fprintf (stderr, "ERROR: write test cfg failed (%d)!\n", tx_bytes);
58  return tx_bytes;
59  }
60 
61  rx_bytes = sock_test_read (socket->fd, (uint8_t *) socket->rxbuf,
62  sizeof (sock_test_cfg_t), NULL);
63  if (rx_bytes < 0)
64  return rx_bytes;
65 
66  if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC)
67  {
68  fprintf (stderr, "ERROR: Bad server reply cfg -- aborting!\n");
69  return -1;
70  }
71  if (socket->cfg.verbose)
72  {
73  printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd);
74  sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
75  }
76  if ((rx_bytes != sizeof (sock_test_cfg_t))
77  || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg))
78  {
79  fprintf (stderr,
80  "ERROR: Invalid config received from server -- aborting!\n");
81  sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
82  return -1;
83  }
84  ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ?
85  rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle);
86 
87  return 0;
88 }
89 
90 static void
92 {
94  sock_test_socket_t *ctrl = &scm->ctrl_socket;
95  sock_test_socket_t *tsock;
96  int rx_bytes, tx_bytes, nbytes;
97  uint32_t i, n;
98  int rv;
99  int nfds = 0;
100  fd_set wr_fdset, rd_fdset;
101  fd_set _wfdset, *wfdset = &_wfdset;
102  fd_set _rfdset, *rfdset = &_rfdset;
103 
104  FD_ZERO (&wr_fdset);
105  FD_ZERO (&rd_fdset);
106  memset (&ctrl->stats, 0, sizeof (ctrl->stats));
107  ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1;
108  for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
109  {
110  tsock = &scm->test_socket[n];
111  tsock->cfg = ctrl->cfg;
113  sock_test_cfg_sync (tsock);
114 
115  memcpy (tsock->txbuf, ctrl->txbuf, nbytes);
116  memset (&tsock->stats, 0, sizeof (tsock->stats));
117 
118  FD_SET (tsock->fd, &wr_fdset);
119  FD_SET (tsock->fd, &rd_fdset);
120  nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
121  }
122 
123  nfds++;
124  clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
125  while (n)
126  {
127  _wfdset = wr_fdset;
128  _rfdset = rd_fdset;
129 
130 #ifdef VCL_TEST
131  rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
132  NULL, 0);
133 #else
134  {
135  struct timeval timeout;
136  timeout.tv_sec = 0;
137  timeout.tv_usec = 0;
138  rv = select (nfds, rfdset, wfdset, NULL, &timeout);
139  }
140 #endif
141  if (rv < 0)
142  {
143  perror ("select()");
144  fprintf (stderr, "\nERROR: select() failed -- aborting test!\n");
145  return;
146  }
147  else if (rv == 0)
148  continue;
149 
150  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
151  {
152  tsock = &scm->test_socket[i];
153  if (!((tsock->stats.stop.tv_sec == 0) &&
154  (tsock->stats.stop.tv_nsec == 0)))
155  continue;
156 
157  if (FD_ISSET (tsock->fd, wfdset) &&
158  (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
159 
160  {
161  tx_bytes =
162  sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes,
163  &tsock->stats, ctrl->cfg.verbose);
164  if (tx_bytes < 0)
165  {
166  fprintf (stderr, "\nERROR: sock_test_write(%d) failed "
167  "-- aborting test!\n", tsock->fd);
168  return;
169  }
170 
171  printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n",
172  tsock->fd, tx_bytes, tsock->txbuf);
173  }
174 
175  if ((FD_ISSET (tsock->fd, rfdset)) &&
176  (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
177  {
178  rx_bytes =
179  sock_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
180  nbytes, &tsock->stats);
181  if (rx_bytes > 0)
182  {
183  printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n",
184  tsock->fd, rx_bytes, tsock->rxbuf);
185 
186  if (tsock->stats.rx_bytes != tsock->stats.tx_bytes)
187  printf
188  ("WARNING: bytes read (%lu) != bytes written (%lu)!\n",
189  tsock->stats.rx_bytes, tsock->stats.tx_bytes);
190  }
191  }
192 
193  if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)
194  {
195  clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
196  n--;
197  }
198  }
199  }
200  clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
201 
202  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
203  {
204  tsock = &scm->test_socket[i];
205  tsock->stats.start = ctrl->stats.start;
206 
207  if (ctrl->cfg.verbose)
208  {
209  static char buf[64];
210 
211  sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
212  sock_test_stats_dump (buf, &tsock->stats,
213  1 /* show_rx */ , 1 /* show tx */ ,
214  ctrl->cfg.verbose);
215  }
216 
217  sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
218  }
219 
220  if (ctrl->cfg.verbose)
221  {
222  sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
223  1 /* show_rx */ , 1 /* show tx */ ,
224  ctrl->cfg.verbose);
225  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
226 
227  if (ctrl->cfg.verbose > 1)
228  {
229  printf (" ctrl socket info\n"
231  " fd: %d (0x%08x)\n"
232  " rxbuf: %p\n"
233  " rxbuf size: %u (0x%08x)\n"
234  " txbuf: %p\n"
235  " txbuf size: %u (0x%08x)\n"
237  ctrl->fd, (uint32_t) ctrl->fd,
238  ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
239  ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
240  }
241  }
242 }
243 
244 static void
246 {
248  sock_test_socket_t *ctrl = &scm->ctrl_socket;
249  sock_test_socket_t *tsock;
250  int tx_bytes;
251  uint32_t i, n;
252  int rv;
253  int nfds = 0;
254  fd_set wr_fdset, rd_fdset;
255  fd_set _wfdset, *wfdset = &_wfdset;
256  fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0;
257 
258  ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
259  ctrl->cfg.ctrl_handle = ~0;
260 
261  printf ("\n" SOCK_TEST_BANNER_STRING
262  "CLIENT (fd %d): %s-directional Stream Test!\n\n"
263  "CLIENT (fd %d): Sending config to server on ctrl socket...\n",
264  ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd);
265 
266  if (sock_test_cfg_sync (ctrl))
267  {
268  fprintf (stderr, "ERROR: test cfg sync failed -- aborting!");
269  return;
270  }
271 
272  FD_ZERO (&wr_fdset);
273  FD_ZERO (&rd_fdset);
274  memset (&ctrl->stats, 0, sizeof (ctrl->stats));
275  for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
276  {
277  tsock = &scm->test_socket[n];
278  tsock->cfg = ctrl->cfg;
280  printf ("CLIENT (fd %d): Sending config to server on "
281  "test socket %d...\n", tsock->fd, n);
282  sock_test_cfg_sync (tsock);
283 
284  /* Fill payload with incrementing uint32's */
285  for (i = 0; i < tsock->txbuf_size; i++)
286  tsock->txbuf[i] = i & 0xff;
287 
288  memset (&tsock->stats, 0, sizeof (tsock->stats));
289  FD_SET (tsock->fd, &wr_fdset);
290  FD_SET (tsock->fd, &rd_fdset);
291  nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
292  }
293 
294  nfds++;
295  clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
296  while (n)
297  {
298  _wfdset = wr_fdset;
299  _rfdset = rd_fdset;
300 
301 #ifdef VCL_TEST
302  rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
303  NULL, 0);
304 #else
305  {
306  struct timeval timeout;
307  timeout.tv_sec = 0;
308  timeout.tv_usec = 0;
309  rv = select (nfds, rfdset, wfdset, NULL, &timeout);
310  }
311 #endif
312  if (rv < 0)
313  {
314  perror ("select()");
315  fprintf (stderr, "\nERROR: select() failed -- aborting test!\n");
316  return;
317  }
318  else if (rv == 0)
319  continue;
320 
321  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
322  {
323  tsock = &scm->test_socket[i];
324  if (!((tsock->stats.stop.tv_sec == 0) &&
325  (tsock->stats.stop.tv_nsec == 0)))
326  continue;
327 
328  if (FD_ISSET (tsock->fd, wfdset) &&
329  (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
330  {
331  tx_bytes =
332  sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
333  ctrl->cfg.txbuf_size, &tsock->stats,
334  ctrl->cfg.verbose);
335  if (tx_bytes < 0)
336  {
337  fprintf (stderr, "\nERROR: sock_test_write(%d) failed "
338  "-- aborting test!\n", tsock->fd);
339  return;
340  }
341  }
342 
343  if ((test == SOCK_TEST_TYPE_BI) &&
344  FD_ISSET (tsock->fd, rfdset) &&
345  (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
346  {
347  (void) sock_test_read (tsock->fd,
348  (uint8_t *) tsock->rxbuf,
349  tsock->rxbuf_size, &tsock->stats);
350  }
351 
352  if (((test == SOCK_TEST_TYPE_UNI) &&
353  (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) ||
354  ((test == SOCK_TEST_TYPE_BI) &&
355  (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)))
356  {
357  clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
358  n--;
359  }
360  }
361  }
362  clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);
363 
364  printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
365  ctrl->fd);
366 
367  if (sock_test_cfg_sync (ctrl))
368  {
369  fprintf (stderr, "ERROR: test cfg sync failed -- aborting!");
370  return;
371  }
372 
373  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
374  {
375  tsock = &scm->test_socket[i];
376 
377  if (ctrl->cfg.verbose)
378  {
379  static char buf[64];
380 
381  sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
382  sock_test_stats_dump (buf, &tsock->stats,
383  test == SOCK_TEST_TYPE_BI /* show_rx */ ,
384  1 /* show tx */ , ctrl->cfg.verbose);
385  }
386 
387  sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
388  }
389 
390  sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
391  test == SOCK_TEST_TYPE_BI /* show_rx */ ,
392  1 /* show tx */ , ctrl->cfg.verbose);
393  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
394 
395  if (ctrl->cfg.verbose)
396  {
397  printf (" ctrl socket info\n"
399  " fd: %d (0x%08x)\n"
400  " rxbuf: %p\n"
401  " rxbuf size: %u (0x%08x)\n"
402  " txbuf: %p\n"
403  " txbuf size: %u (0x%08x)\n"
405  ctrl->fd, (uint32_t) ctrl->fd,
406  ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
407  ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
408  }
409 
410  ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
411  if (sock_test_cfg_sync (ctrl))
412  fprintf (stderr, "ERROR: post-test cfg sync failed!");
413 
414  printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n"
415  SOCK_TEST_BANNER_STRING "\n", ctrl->fd,
416  test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
417 }
418 
419 static void
421 {
423  sock_test_socket_t *ctrl = &scm->ctrl_socket;
424  sock_test_socket_t *tsock;
425  int i;
426 
427  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
428  {
429  tsock = &scm->test_socket[i];
430  tsock->cfg.test = SOCK_TEST_TYPE_EXIT;
431 
432  /* coverity[COPY_PASTE_ERROR] */
433  if (ctrl->cfg.verbose)
434  {
435  printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
436  tsock->fd);
437  sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
438  }
439  (void) sock_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
440  sizeof (tsock->cfg), &tsock->stats,
441  ctrl->cfg.verbose);
442  }
443 
444  ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
445  if (ctrl->cfg.verbose)
446  {
447  printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
448  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
449  }
450  (void) sock_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
451  sizeof (ctrl->cfg), &ctrl->stats,
452  ctrl->cfg.verbose);
453  printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
454  sleep (1);
455 }
456 
457 static int
458 sock_test_connect_test_sockets (uint32_t num_test_sockets)
459 {
461  sock_test_socket_t *ctrl = &scm->ctrl_socket;
462  sock_test_socket_t *tsock;
463  int i, rv, errno_val;
464 
465  if (num_test_sockets < 1)
466  {
467  errno = EINVAL;
468  return -1;
469  }
470 
471  if (num_test_sockets < scm->num_test_sockets)
472  {
473  for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
474  {
475  tsock = &scm->test_socket[i];
476 #ifdef VCL_TEST
477  vppcom_session_close (tsock->fd);
478 #else
479  close (tsock->fd);
480 #endif
481  free (tsock->txbuf);
482  free (tsock->rxbuf);
483  }
484  }
485 
486  else if (num_test_sockets > scm->num_test_sockets)
487  {
488  tsock = realloc (scm->test_socket,
489  sizeof (sock_test_socket_t) * num_test_sockets);
490  if (!tsock)
491  {
492  errno_val = errno;
493  perror ("ERROR in sock_test_connect_test_sockets()");
494  fprintf (stderr, "ERROR: socket failed (errno = %d)!\n", errno_val);
495  return -1;
496  }
497 
498  if (!scm->test_socket)
499  memset (tsock, 0, sizeof (*tsock));
500 
501  scm->test_socket = tsock;
502  for (i = scm->num_test_sockets; i < num_test_sockets; i++)
503  {
504  tsock = &scm->test_socket[i];
505 #ifdef VCL_TEST
506  tsock->fd =
508  0 /* is_nonblocking */ );
509  if (tsock->fd < 0)
510  {
511  errno = -tsock->fd;
512  tsock->fd = -1;
513  }
514 #else
515  tsock->fd = socket (AF_INET, SOCK_STREAM, 0);
516 #endif
517  if (tsock->fd < 0)
518  {
519  errno_val = errno;
520  perror ("ERROR in sock_test_connect_test_sockets()");
521  fprintf (stderr, "ERROR: socket failed (errno = %d)!\n",
522  errno_val);
523  return tsock->fd;
524  }
525 
526 #ifdef VCL_TEST
527  rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
528 #else
529  rv =
530  connect (tsock->fd, (struct sockaddr *) &scm->server_addr,
531  sizeof (scm->server_addr));
532 #endif
533  if (rv < 0)
534  {
535  errno_val = errno;
536  perror ("ERROR in main()");
537  fprintf (stderr, "ERROR: connect failed (errno = %d)!\n",
538  errno_val);
539  }
540  tsock->cfg = ctrl->cfg;
542  sock_test_cfg_sync (tsock);
543 
544  printf ("CLIENT (fd %d): Test socket %d connected.\n",
545  tsock->fd, i);
546  }
547  }
548 
549  scm->num_test_sockets = num_test_sockets;
550  printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
551  return 0;
552 }
553 
554 static void
555 dump_help (void)
556 {
557 #define INDENT "\n "
558 
559  printf ("Test configuration commands:"
561  "\t\t\tDisplay help."
563  "\t\t\tExit test client & server."
565  "\t\t\tShow the current test cfg."
567  "\t\t\tRun the Uni-directional test."
569  "\t\t\tRun the Bi-directional test."
571  "\t\t\tToggle verbose setting."
573  "<rxbuf size>\tRx buffer size (bytes)."
575  "<txbuf size>\tTx buffer size (bytes)."
577  "<# of writes>\tNumber of txbuf writes to server." "\n");
578 }
579 
580 static void
582 {
584  sock_test_socket_t *ctrl = &scm->ctrl_socket;
585  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
586  uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);
587 
588  if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
589  {
590  ctrl->cfg.txbuf_size = txbuf_size;
591  ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
592  sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
593  (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
594  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
595  }
596  else
597  fprintf (stderr,
598  "ERROR: Invalid txbuf size (%lu) < minimum buf size (%u)!\n",
599  txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
600 }
601 
602 static void
604 {
606  sock_test_socket_t *ctrl = &scm->ctrl_socket;
607  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
608  uint32_t num_writes = strtoul ((const char *) p, NULL, 10);
609 
610  if (num_writes > 0)
611  {
612  ctrl->cfg.num_writes = num_writes;
613  ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
614  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
615  }
616  else
617  {
618  fprintf (stderr, "ERROR: invalid num writes: %u\n", num_writes);
619  }
620 }
621 
622 static void
624 {
626  sock_test_socket_t *ctrl = &scm->ctrl_socket;
627  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
628  uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);
629 
630  if ((num_test_sockets > 0) &&
631  (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
632  {
633  ctrl->cfg.num_test_sockets = num_test_sockets;
634  sock_test_connect_test_sockets (num_test_sockets);
635  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
636  }
637  else
638  {
639  fprintf (stderr, "ERROR: invalid num test sockets: %u, (%d max)\n",
640  num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
641  }
642 }
643 
644 static void
646 {
648  sock_test_socket_t *ctrl = &scm->ctrl_socket;
649  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
650  uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);
651 
652  if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
653  {
654  ctrl->cfg.rxbuf_size = rxbuf_size;
655  sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
656  (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
657  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
658  }
659  else
660  fprintf (stderr,
661  "ERROR: Invalid rxbuf size (%lu) < minimum buf size (%u)!\n",
662  rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
663 }
664 
665 static void
667 {
669  sock_test_socket_t *ctrl = &scm->ctrl_socket;
670 
671  ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
672  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
673 
674 }
675 
676 static sock_test_t
678 {
680  sock_test_socket_t *ctrl = &scm->ctrl_socket;
682 
683  if (!strcmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf))
684  rv = SOCK_TEST_TYPE_EXIT;
685 
686  else if (!strcmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf))
687  dump_help ();
688 
689  else if (!strcmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf))
690  scm->dump_cfg = 1;
691 
692  else if (!strcmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf))
694 
695  else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
696  strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
698 
699  else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
702 
703  else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
704  strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
706 
707  else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
708  strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
710 
711  else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
712  strlen (SOCK_TEST_TOKEN_RUN_UNI)))
713  rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
714 
715  else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
716  strlen (SOCK_TEST_TOKEN_RUN_BI)))
717  rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;
718 
719  else
720  rv = SOCK_TEST_TYPE_ECHO;
721 
722  return rv;
723 }
724 
725 void
727 {
728  fprintf (stderr,
729  "sock_test_client [OPTIONS] <ipaddr> <port>\n"
730  " OPTIONS\n"
731  " -h Print this message and exit.\n"
732  " -c Print test config before test.\n"
733  " -w <dir> Write test results to <dir>.\n"
734  " -X Exit after running test.\n"
735  " -E Run Echo test.\n"
736  " -N <num-writes> Test Cfg: number of writes.\n"
737  " -R <rxbuf-size> Test Cfg: rx buffer size.\n"
738  " -T <txbuf-size> Test Cfg: tx buffer size.\n"
739  " -U Run Uni-directional test.\n"
740  " -B Run Bi-directional test.\n"
741  " -V Verbose mode.\n");
742  exit (1);
743 }
744 
745 int
746 main (int argc, char **argv)
747 {
749  sock_test_socket_t *ctrl = &scm->ctrl_socket;
750  int c, rv, errno_val;
751  sock_test_t post_test = SOCK_TEST_TYPE_NONE;
752 
753  sock_test_cfg_init (&ctrl->cfg);
755 
756  opterr = 0;
757  while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV")) != -1)
758  switch (c)
759  {
760  case 'c':
761  scm->dump_cfg = 1;
762  break;
763 
764  case 's':
765  if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
766  if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
767  {
768  fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
770  }
771  if (!ctrl->cfg.num_test_sockets ||
772  (ctrl->cfg.num_test_sockets > FD_SETSIZE))
773  {
774  fprintf (stderr, "ERROR: Invalid number of sockets (%d)"
775  "specified for option -%c!\n"
776  " Valid range is 1 - %d\n",
777  ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
779  }
780  break;
781 
782  case 'w':
783  fprintf (stderr, "Writing test results to files is TBD.\n");
784  break;
785 
786  case 'X':
787  post_test = SOCK_TEST_TYPE_EXIT;
788  break;
789 
790  case 'E':
791  if (strlen (optarg) > ctrl->txbuf_size)
792  {
793  fprintf (stderr,
794  "ERROR: Option -%c value larger than txbuf size (%d)!\n",
795  optopt, ctrl->txbuf_size);
797  }
798  strcpy (ctrl->txbuf, optarg);
799  ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
800  break;
801 
802  case 'I':
803  if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
804  if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
805  {
806  fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
808  }
810  {
811  fprintf (stderr, "ERROR: value greater than max number test"
812  " sockets (%d)!", SOCK_TEST_CFG_MAX_TEST_SCKTS);
814  }
815  break;
816 
817  case 'N':
818  if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
819  if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
820  {
821  fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
823  }
824  ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
825  break;
826 
827  case 'R':
828  if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
829  if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
830  {
831  fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
833  }
835  {
836  ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
837  sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
838  (uint8_t **) & ctrl->rxbuf,
839  &ctrl->rxbuf_size);
840  }
841  else
842  {
843  fprintf (stderr,
844  "ERROR: rxbuf size (%lu) less than minumum (%u)\n",
847  }
848 
849  break;
850 
851  case 'T':
852  if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
853  if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
854  {
855  fprintf (stderr, "ERROR: Invalid value for option -%c!\n", c);
857  }
859  {
860  ctrl->txbuf_size = ctrl->cfg.txbuf_size;
861  sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
862  (uint8_t **) & ctrl->txbuf,
863  &ctrl->txbuf_size);
864  ctrl->cfg.total_bytes =
865  ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
866  }
867  else
868  {
869  fprintf (stderr,
870  "ERROR: txbuf size (%lu) less than minumum (%u)!\n",
873  }
874  break;
875 
876  case 'U':
877  ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
878  break;
879 
880  case 'B':
881  ctrl->cfg.test = SOCK_TEST_TYPE_BI;
882  break;
883 
884  case 'V':
885  ctrl->cfg.verbose = 1;
886  break;
887 
888  case '?':
889  switch (optopt)
890  {
891  case 'E':
892  case 'I':
893  case 'N':
894  case 'R':
895  case 'T':
896  case 'w':
897  fprintf (stderr, "ERROR: Option -%c requires an argument.\n",
898  optopt);
899  break;
900 
901  default:
902  if (isprint (optopt))
903  fprintf (stderr, "ERROR: Unknown option `-%c'.\n", optopt);
904  else
905  fprintf (stderr, "ERROR: Unknown option character `\\x%x'.\n",
906  optopt);
907  }
908  /* fall thru */
909  case 'h':
910  default:
912  }
913 
914  if (argc < (optind + 2))
915  {
916  fprintf (stderr, "ERROR: Insufficient number of arguments!\n");
918  }
919 
920 #ifdef VCL_TEST
921  ctrl->fd = vppcom_app_create ("vcl_test_client");
922  if (ctrl->fd < 0)
923  {
924  errno = -ctrl->fd;
925  ctrl->fd = -1;
926  }
927  else
928  {
930  0 /* is_nonblocking */ );
931  if (ctrl->fd < 0)
932  {
933  errno = -ctrl->fd;
934  ctrl->fd = -1;
935  }
936  }
937 #else
938  ctrl->fd = socket (AF_INET, SOCK_STREAM, 0);
939 #endif
940 
941  if (ctrl->fd < 0)
942  {
943  errno_val = errno;
944  perror ("ERROR in main()");
945  fprintf (stderr, "ERROR: socket failed (errno = %d)!\n", errno_val);
946  return ctrl->fd;
947  }
948 
949  memset (&scm->server_addr, 0, sizeof (scm->server_addr));
950 
951  scm->server_addr.sin_family = AF_INET;
952  inet_pton (AF_INET, argv[optind++], &(scm->server_addr.sin_addr));
953  scm->server_addr.sin_port = htons (atoi (argv[optind]));
954 
955 #ifdef VCL_TEST
956  scm->server_endpt.vrf = VPPCOM_VRF_DEFAULT;
957  scm->server_endpt.is_ip4 = (scm->server_addr.sin_family == AF_INET);
958  scm->server_endpt.ip = (uint8_t *) & scm->server_addr.sin_addr;
959  scm->server_endpt.port = (uint16_t) scm->server_addr.sin_port;
960 #endif
961 
962  do
963  {
964  printf ("\nCLIENT: Connecting to server...\n");
965 
966 #ifdef VCL_TEST
967  rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
968 #else
969  rv =
970  connect (ctrl->fd, (struct sockaddr *) &scm->server_addr,
971  sizeof (scm->server_addr));
972 #endif
973  if (rv < 0)
974  {
975  errno_val = errno;
976  perror ("ERROR in main()");
977  fprintf (stderr, "ERROR: connect failed (errno = %d)!\n",
978  errno_val);
979  }
980 
981  sock_test_cfg_sync (ctrl);
982  printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
983  }
984  while (rv < 0);
985 
987 
988  while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
989  {
990  if (scm->dump_cfg)
991  {
992  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
993  scm->dump_cfg = 0;
994  }
995 
996  switch (ctrl->cfg.test)
997  {
998  case SOCK_TEST_TYPE_ECHO:
999  echo_test_client ();
1000  break;
1001 
1002  case SOCK_TEST_TYPE_UNI:
1003  case SOCK_TEST_TYPE_BI:
1004  stream_test_client (ctrl->cfg.test);
1005  break;
1006 
1007  case SOCK_TEST_TYPE_EXIT:
1008  continue;
1009 
1010  case SOCK_TEST_TYPE_NONE:
1011  default:
1012  break;
1013  }
1014  switch (post_test)
1015  {
1016  case SOCK_TEST_TYPE_EXIT:
1017  switch (ctrl->cfg.test)
1018  {
1019  case SOCK_TEST_TYPE_EXIT:
1020  case SOCK_TEST_TYPE_UNI:
1021  case SOCK_TEST_TYPE_BI:
1022  case SOCK_TEST_TYPE_ECHO:
1023  ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
1024  continue;
1025 
1026  case SOCK_TEST_TYPE_NONE:
1027  default:
1028  break;
1029  }
1030  break;
1031 
1032  case SOCK_TEST_TYPE_NONE:
1033  case SOCK_TEST_TYPE_ECHO:
1034  case SOCK_TEST_TYPE_UNI:
1035  case SOCK_TEST_TYPE_BI:
1036  default:
1037  break;
1038  }
1039 
1040  memset (ctrl->txbuf, 0, ctrl->txbuf_size);
1041  memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);
1042 
1043  printf ("\nType some characters and hit <return>\n"
1044  "('" SOCK_TEST_TOKEN_HELP "' for help): ");
1045 
1046  if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
1047  {
1048  if (strlen (ctrl->txbuf) == 1)
1049  {
1050  printf ("\nCLIENT: Nothing to send! Please try again...\n");
1051  continue;
1052  }
1053  ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline.
1054 
1055  /* Parse input for keywords */
1056  ctrl->cfg.test = parse_input ();
1057  }
1058  }
1059 
1060  exit_client ();
1061 #ifdef VCL_TEST
1062  vppcom_session_close (ctrl->fd);
1063  vppcom_app_destroy ();
1064 #else
1065  close (ctrl->fd);
1066 #endif
1067  return 0;
1068 }
1069 
1070 /*
1071  * fd.io coding-style-patch-verification: ON
1072  *
1073  * Local Variables:
1074  * eval: (c-set-style "gnu")
1075  * End:
1076  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
int vppcom_app_create(char *app_name)
Definition: vppcom.c:1690
#define SOCK_TEST_TOKEN_EXIT
Definition: sock_test.h:25
#define SOCK_TEST_TOKEN_RXBUF_SIZE
Definition: sock_test.h:30
int vppcom_session_create(u32 vrf, u8 proto, u8 is_nonblocking)
Definition: vppcom.c:1802
#define NULL
Definition: clib.h:55
#define SOCK_TEST_TOKEN_RUN_UNI
Definition: sock_test.h:32
static void stream_test_client(sock_test_t test)
uint32_t ctrl_handle
Definition: sock_test.h:64
uint64_t rx_bytes
Definition: sock_test.h:76
uint32_t magic
Definition: sock_test.h:62
int main(int argc, char **argv)
static int sock_test_write(int fd, uint8_t *buf, uint32_t nbytes, sock_test_stats_t *stats, uint32_t verbose)
Definition: sock_test.h:354
#define SOCK_TEST_CFG_CTRL_MAGIC
Definition: sock_test.h:44
uint32_t test
Definition: sock_test.h:63
int vppcom_session_connect(uint32_t session_index, vppcom_endpt_t *server_ep)
Definition: vppcom.c:2052
static void sock_test_buf_alloc(sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf, uint32_t *bufsize)
Definition: sock_test.h:141
#define INDENT
static sock_test_t parse_input()
static void sock_test_cfg_init(sock_test_cfg_t *cfg)
Definition: sock_test.h:113
#define SOCK_TEST_BANNER_STRING
Definition: sock_test.h:35
static int sock_test_cfg_sync(sock_test_socket_t *socket)
uint32_t verbose
Definition: sock_test.h:66
void vppcom_app_destroy(void)
Definition: vppcom.c:1776
static void exit_client(void)
#define SOCK_TEST_TOKEN_NUM_TEST_SCKTS
Definition: sock_test.h:28
static void sock_test_cfg_dump(sock_test_cfg_t *cfg, uint8_t is_client)
Definition: sock_test.h:203
#define SOCK_TEST_TOKEN_SHOW_CFG
Definition: sock_test.h:31
static int sock_test_connect_test_sockets(uint32_t num_test_sockets)
uint64_t tx_bytes
Definition: sock_test.h:80
uint32_t num_test_sockets
Definition: sock_test.h:65
#define SOCK_TEST_CFG_MAX_TEST_SCKTS
Definition: sock_test.h:49
#define SOCK_TEST_TOKEN_NUM_WRITES
Definition: sock_test.h:29
static void echo_test_client()
static void cfg_verbose_toggle(void)
#define SOCK_TEST_TOKEN_VERBOSE
Definition: sock_test.h:26
uint64_t total_bytes
Definition: sock_test.h:70
struct timespec stop
Definition: sock_test.h:84
sock_test_t
Definition: sock_test.h:51
svmdb_client_t * c
struct timespec start
Definition: sock_test.h:83
int vppcom_select(unsigned long n_bits, unsigned long *read_map, unsigned long *write_map, unsigned long *except_map, double time_to_wait)
Definition: vppcom.c:2306
static void sock_test_stats_dump(char *header, sock_test_stats_t *stats, uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
Definition: sock_test.h:240
#define SOCK_TEST_TOKEN_TXBUF_SIZE
Definition: sock_test.h:27
uint64_t txbuf_size
Definition: sock_test.h:68
#define SOCK_TEST_CFG_BUF_SIZE_MIN
Definition: sock_test.h:48
static void cfg_rxbuf_size_set(void)
static void sock_test_socket_buf_alloc(sock_test_socket_t *socket)
Definition: sock_test.h:167
static void cfg_num_writes_set(void)
sock_test_socket_t * test_socket
#define SOCK_TEST_TOKEN_RUN_BI
Definition: sock_test.h:33
static int sock_test_cfg_verify(sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg)
Definition: sock_test.h:127
static void cfg_txbuf_size_set(void)
uint64_t num_writes
Definition: sock_test.h:69
static int sock_test_read(int fd, uint8_t *buf, uint32_t nbytes, sock_test_stats_t *stats)
Definition: sock_test.h:307
sock_client_main_t sock_client_main
sock_test_stats_t stats
Definition: sock_test.h:95
struct sockaddr_in server_addr
uint32_t rxbuf_size
Definition: sock_test.h:92
#define SOCK_TEST_SEPARATOR_STRING
Definition: sock_test.h:37
static void dump_help(void)
sock_test_cfg_t cfg
Definition: sock_test.h:94
static void sock_test_stats_accumulate(sock_test_stats_t *accum, sock_test_stats_t *incr)
Definition: sock_test.h:99
#define SOCK_TEST_TOKEN_HELP
Definition: sock_test.h:24
void print_usage_and_exit(void)
static void cfg_num_test_sockets_set(void)
int vppcom_session_close(uint32_t session_index)
Definition: vppcom.c:1825
#define VPPCOM_VRF_DEFAULT
Definition: vppcom.h:26
sock_test_socket_t ctrl_socket
uint64_t rxbuf_size
Definition: sock_test.h:67
uint32_t txbuf_size
Definition: sock_test.h:90