aboutsummaryrefslogtreecommitdiffstats
path: root/src/vcl/sock_test_client.c
blob: 1ed4b89a3b1da20986f0e2bc76fb8cac5ec95d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <time.h>
#include <arpa/inet.h>
#include <vcl/sock_test.h>
#ifndef VCL_TEST
#include <sys/un.h>
#endif

typedef struct
{
#ifdef VCL_TEST
  vppcom_endpt_t server_endpt;
#else
  int af_unix_echo_tx;
  int af_unix_echo_rx;
#endif
  struct sockaddr_storage server_addr;
  uint32_t server_addr_size;
  sock_test_socket_t ctrl_socket;
  sock_test_socket_t *test_socket;
  uint32_t num_test_sockets;
  uint8_t dump_cfg;
} sock_client_main_t;

sock_client_main_t sock_client_main;


static int
sock_test_cfg_sync (sock_test_socket_t * socket)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf;
  int rx_bytes, tx_bytes;

  if (socket->cfg.verbose)
    sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ );

  tx_bytes = sock_test_write (socket->fd, (uint8_t *) & ctrl->cfg,
			      sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose);
  if (tx_bytes < 0)
    {
      fprintf (stderr, "CLIENT: ERROR: write test cfg failed (%d)!\n",
	       tx_bytes);
      return tx_bytes;
    }

  rx_bytes = sock_test_read (socket->fd, (uint8_t *) socket->rxbuf,
			     sizeof (sock_test_cfg_t), NULL);
  if (rx_bytes < 0)
    return rx_bytes;

  if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC)
    {
      fprintf (stderr, "CLIENT: ERROR: Bad server reply cfg -- aborting!\n");
      return -1;
    }
  if (socket->cfg.verbose)
    {
      printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd);
      sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
    }
  if ((rx_bytes != sizeof (sock_test_cfg_t))
      || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg))
    {
      fprintf (stderr, "CLIENT: ERROR: Invalid config received "
	       "from server -- aborting!\n");
      sock_test_cfg_dump (rl_cfg, 1 /* is_client */ );
      return -1;
    }
  ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ?
			   rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle);

  return 0;
}

static void
echo_test_client ()
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  sock_test_socket_t *tsock;
  int rx_bytes, tx_bytes, nbytes;
  uint32_t i, n;
  int rv;
  int nfds = 0;
  fd_set wr_fdset, rd_fdset;
  fd_set _wfdset, *wfdset = &_wfdset;
  fd_set _rfdset, *rfdset = &_rfdset;

  FD_ZERO (&wr_fdset);
  FD_ZERO (&rd_fdset);
  memset (&ctrl->stats, 0, sizeof (ctrl->stats));
  ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1;
  for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
    {
      tsock = &scm->test_socket[n];
      tsock->cfg = ctrl->cfg;
      sock_test_socket_buf_alloc (tsock);
      sock_test_cfg_sync (tsock);

      memcpy (tsock->txbuf, ctrl->txbuf, nbytes);
      memset (&tsock->stats, 0, sizeof (tsock->stats));

      FD_SET (tsock->fd, &wr_fdset);
      FD_SET (tsock->fd, &rd_fdset);
      nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
    }

  nfds++;
  clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
  while (n)
    {
      _wfdset = wr_fdset;
      _rfdset = rd_fdset;

#ifdef VCL_TEST
      rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
			  NULL, 0);
#else
      {
	struct timeval timeout;
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	rv = select (nfds, rfdset, wfdset, NULL, &timeout);
      }
#endif
      if (rv < 0)
	{
	  perror ("select()");
	  fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
		   "aborting test!\n");
	  return;
	}
      else if (rv == 0)
	continue;

      for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
	{
	  tsock = &scm->test_socket[i];
	  if (!((tsock->stats.stop.tv_sec == 0) &&
		(tsock->stats.stop.tv_nsec == 0)))
	    continue;

	  if (FD_ISSET (tsock->fd, wfdset) &&
	      (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))

	    {
	      tx_bytes =
		sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes,
				 &tsock->stats, ctrl->cfg.verbose);
	      if (tx_bytes < 0)
		{
		  fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) "
			   "failed -- aborting test!\n", tsock->fd);
		  return;
		}

	      printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n",
		      tsock->fd, tx_bytes, tsock->txbuf);
	    }

	  if ((FD_ISSET (tsock->fd, rfdset)) &&
	      (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
	    {
	      rx_bytes =
		sock_test_read (tsock->fd, (uint8_t *) tsock->rxbuf,
				nbytes, &tsock->stats);
	      if (rx_bytes > 0)
		{
		  printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n",
			  tsock->fd, rx_bytes, tsock->rxbuf);

		  if (tsock->stats.rx_bytes != tsock->stats.tx_bytes)
		    printf ("CLIENT: WARNING: bytes read (%lu) "
			    "!= bytes written (%lu)!\n",
			    tsock->stats.rx_bytes, tsock->stats.tx_bytes);
		}
	    }

	  if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)
	    {
	      clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
	      n--;
	    }
	}
    }
  clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);

#ifndef VCL_TEST
  {
    int fd, errno_val;
    struct sockaddr_un serveraddr;
    uint8_t buffer[256];
    size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
    struct timeval timeout;

    /* Open AF_UNIX socket and send an echo to test mixed epoll on server.
     */
    fd = socket (AF_UNIX, SOCK_STREAM, 0);
    if (fd < 0)
      {
	errno_val = errno;
	perror ("ERROR in echo_test_client(): socket(AF_UNIX) failed");
	fprintf (stderr,
		 "CLIENT: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed "
		 "(errno = %d)!\n", errno_val);
	goto out;
      }
    memset (&serveraddr, 0, sizeof (serveraddr));
    serveraddr.sun_family = AF_UNIX;
    strcpy (serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME);
    rv = connect (fd, (struct sockaddr *) &serveraddr, SUN_LEN (&serveraddr));
    if (rv < 0)
      {
	errno_val = errno;
	perror ("ERROR in echo_test_client(): connect() failed");
	fprintf (stderr, "CLIENT: ERROR: connect(fd %d, \"%s\", %lu) "
		 "failed (errno = %d)!\n", fd, SOCK_TEST_AF_UNIX_FILENAME,
		 SUN_LEN (&serveraddr), errno_val);
	goto done;
      }

    scm->af_unix_echo_tx++;
    strcpy ((char *) buffer, SOCK_TEST_MIXED_EPOLL_DATA);
    timeout.tv_sec = 0;
    timeout.tv_usec = 250000;
    select (0, NULL, NULL, NULL, &timeout);	/* delay .25 secs */
    rv = write (fd, buffer, nbytes);
    if (rv < 0)
      {
	errno_val = errno;
	perror ("ERROR in echo_test_client(): write() failed");
	fprintf (stderr, "CLIENT: ERROR: write(fd %d, \"%s\", %lu) "
		 "failed (errno = %d)!\n", fd, buffer, nbytes, errno_val);
	goto done;
      }
    else if (rv < nbytes)
      {
	fprintf (stderr, "CLIENT: ERROR: write(fd %d, \"%s\", %lu) "
		 "returned %d!\n", fd, buffer, nbytes, rv);
	goto done;
      }

    printf ("CLIENT (AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
    memset (buffer, 0, sizeof (buffer));
    rv = read (fd, buffer, nbytes);
    if (rv < 0)
      {
	errno_val = errno;
	perror ("ERROR in echo_test_client(): read() failed");
	fprintf (stderr, "CLIENT: ERROR: read(fd %d, %p, %lu) "
		 "failed (errno = %d)!\n", fd, buffer, nbytes, errno_val);
	goto done;
      }
    else if (rv < nbytes)
      {
	fprintf (stderr, "CLIENT: ERROR: read(fd %d, %p, %lu) "
		 "returned %d!\n", fd, buffer, nbytes, rv);
	goto done;
      }

    if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
      {
	printf ("CLIENT (AF_UNIX): RX (%d bytes) - '%s'\n", rv, buffer);
	scm->af_unix_echo_rx++;
      }
    else
      printf ("CLIENT (AF_UNIX): ERROR: RX (%d bytes) - '%s'\n", rv, buffer);

  done:
    close (fd);
  out:
    ;
  }
#endif

  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
    {
      tsock = &scm->test_socket[i];
      tsock->stats.start = ctrl->stats.start;

      if (ctrl->cfg.verbose)
	{
	  static char buf[64];

	  sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
	  sock_test_stats_dump (buf, &tsock->stats,
				1 /* show_rx */ , 1 /* show tx */ ,
				ctrl->cfg.verbose);
	}

      sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
    }

  if (ctrl->cfg.verbose)
    {
      sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
			    1 /* show_rx */ , 1 /* show tx */ ,
			    ctrl->cfg.verbose);
      sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );

      if (ctrl->cfg.verbose > 1)
	{
	  printf ("  ctrl socket info\n"
		  SOCK_TEST_SEPARATOR_STRING
		  "          fd:  %d (0x%08x)\n"
		  "       rxbuf:  %p\n"
		  "  rxbuf size:  %u (0x%08x)\n"
		  "       txbuf:  %p\n"
		  "  txbuf size:  %u (0x%08x)\n"
		  SOCK_TEST_SEPARATOR_STRING,
		  ctrl->fd, (uint32_t) ctrl->fd,
		  ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
		  ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
	}
    }
}

static void
stream_test_client (sock_test_t test)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  sock_test_socket_t *tsock;
  int tx_bytes;
  uint32_t i, n;
  int rv;
  int nfds = 0;
  fd_set wr_fdset, rd_fdset;
  fd_set _wfdset, *wfdset = &_wfdset;
  fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0;

  ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
  ctrl->cfg.ctrl_handle = ~0;

  printf ("\n" SOCK_TEST_BANNER_STRING
	  "CLIENT (fd %d): %s-directional Stream Test!\n\n"
	  "CLIENT (fd %d): Sending config to server on ctrl socket...\n",
	  ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd);

  if (sock_test_cfg_sync (ctrl))
    {
      fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
      return;
    }

  FD_ZERO (&wr_fdset);
  FD_ZERO (&rd_fdset);
  memset (&ctrl->stats, 0, sizeof (ctrl->stats));
  for (n = 0; n != ctrl->cfg.num_test_sockets; n++)
    {
      tsock = &scm->test_socket[n];
      tsock->cfg = ctrl->cfg;
      sock_test_socket_buf_alloc (tsock);
      printf ("CLIENT (fd %d): Sending config to server on "
	      "test socket %d...\n", tsock->fd, n);
      sock_test_cfg_sync (tsock);

      /* Fill payload with incrementing uint32's */
      for (i = 0; i < tsock->txbuf_size; i++)
	tsock->txbuf[i] = i & 0xff;

      memset (&tsock->stats, 0, sizeof (tsock->stats));
      FD_SET (tsock->fd, &wr_fdset);
      FD_SET (tsock->fd, &rd_fdset);
      nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds;
    }

  nfds++;
  clock_gettime (CLOCK_REALTIME, &ctrl->stats.start);
  while (n)
    {
      _wfdset = wr_fdset;
      _rfdset = rd_fdset;

#ifdef VCL_TEST
      rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset,
			  NULL, 0);
#else
      {
	struct timeval timeout;
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	rv = select (nfds, rfdset, wfdset, NULL, &timeout);
      }
#endif
      if (rv < 0)
	{
	  perror ("select()");
	  fprintf (stderr, "\nCLIENT: ERROR: select() failed -- "
		   "aborting test!\n");
	  return;
	}
      else if (rv == 0)
	continue;

      for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
	{
	  tsock = &scm->test_socket[i];
	  if (!((tsock->stats.stop.tv_sec == 0) &&
		(tsock->stats.stop.tv_nsec == 0)))
	    continue;

	  if ((test == SOCK_TEST_TYPE_BI) &&
	      FD_ISSET (tsock->fd, rfdset) &&
	      (tsock->stats.rx_bytes < ctrl->cfg.total_bytes))
	    {
	      (void) sock_test_read (tsock->fd,
				     (uint8_t *) tsock->rxbuf,
				     tsock->rxbuf_size, &tsock->stats);
	    }

	  if (FD_ISSET (tsock->fd, wfdset) &&
	      (tsock->stats.tx_bytes < ctrl->cfg.total_bytes))
	    {
	      tx_bytes =
		sock_test_write (tsock->fd, (uint8_t *) tsock->txbuf,
				 ctrl->cfg.txbuf_size, &tsock->stats,
				 ctrl->cfg.verbose);
	      if (tx_bytes < 0)
		{
		  fprintf (stderr, "\nCLIENT: ERROR: sock_test_write(%d) "
			   "failed -- aborting test!\n", tsock->fd);
		  return;
		}
	    }

	  if (((test == SOCK_TEST_TYPE_UNI) &&
	       (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) ||
	      ((test == SOCK_TEST_TYPE_BI) &&
	       (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes)))
	    {
	      clock_gettime (CLOCK_REALTIME, &tsock->stats.stop);
	      n--;
	    }
	}
    }
  clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop);

  printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n",
	  ctrl->fd);

  if (sock_test_cfg_sync (ctrl))
    {
      fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!");
      return;
    }

  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
    {
      tsock = &scm->test_socket[i];

      if (ctrl->cfg.verbose)
	{
	  static char buf[64];

	  sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd);
	  sock_test_stats_dump (buf, &tsock->stats,
				test == SOCK_TEST_TYPE_BI /* show_rx */ ,
				1 /* show tx */ , ctrl->cfg.verbose);
	}

      sock_test_stats_accumulate (&ctrl->stats, &tsock->stats);
    }

  sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats,
			test == SOCK_TEST_TYPE_BI /* show_rx */ ,
			1 /* show tx */ , ctrl->cfg.verbose);
  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );

  if (ctrl->cfg.verbose)
    {
      printf ("  ctrl socket info\n"
	      SOCK_TEST_SEPARATOR_STRING
	      "          fd:  %d (0x%08x)\n"
	      "       rxbuf:  %p\n"
	      "  rxbuf size:  %u (0x%08x)\n"
	      "       txbuf:  %p\n"
	      "  txbuf size:  %u (0x%08x)\n"
	      SOCK_TEST_SEPARATOR_STRING,
	      ctrl->fd, (uint32_t) ctrl->fd,
	      ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size,
	      ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size);
    }

  ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
  if (sock_test_cfg_sync (ctrl))
    fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!");

  printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n"
	  SOCK_TEST_BANNER_STRING "\n", ctrl->fd,
	  test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
}

static void
exit_client (void)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  sock_test_socket_t *tsock;
  int i;

#ifndef VCL_TEST
  printf ("CLIENT: af_unix_echo_tx %d, af_unix_echo_rx %d\n",
	  scm->af_unix_echo_tx, scm->af_unix_echo_rx);
#endif
  for (i = 0; i < ctrl->cfg.num_test_sockets; i++)
    {
      tsock = &scm->test_socket[i];
      tsock->cfg.test = SOCK_TEST_TYPE_EXIT;

      /* coverity[COPY_PASTE_ERROR] */
      if (ctrl->cfg.verbose)
	{
	  printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n",
		  tsock->fd);
	  sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ );
	}
      (void) sock_test_write (tsock->fd, (uint8_t *) & tsock->cfg,
			      sizeof (tsock->cfg), &tsock->stats,
			      ctrl->cfg.verbose);
    }

  ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
  if (ctrl->cfg.verbose)
    {
      printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd);
      sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
    }
  (void) sock_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg,
			  sizeof (ctrl->cfg), &ctrl->stats,
			  ctrl->cfg.verbose);
  printf ("\nCLIENT: So long and thanks for all the fish!\n\n");
  sleep (1);
}

static int
sock_test_connect_test_sockets (uint32_t num_test_sockets)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  sock_test_socket_t *tsock;
  int i, rv, errno_val;

  if (num_test_sockets < 1)
    {
      errno = EINVAL;
      return -1;
    }

  if (num_test_sockets < scm->num_test_sockets)
    {
      for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--)
	{
	  tsock = &scm->test_socket[i];
#ifdef VCL_TEST
	  vppcom_session_close (tsock->fd);
#else
	  close (tsock->fd);
#endif
	  free (tsock->txbuf);
	  free (tsock->rxbuf);
	}
    }

  else if (num_test_sockets > scm->num_test_sockets)
    {
      tsock = realloc (scm->test_socket,
		       sizeof (sock_test_socket_t) * num_test_sockets);
      if (!tsock)
	{
	  errno_val = errno;
	  perror ("ERROR in sock_test_connect_test_sockets()");
	  fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
		   errno_val);
	  return -1;
	}

      if (!scm->test_socket)
	memset (tsock, 0, sizeof (*tsock));

      scm->test_socket = tsock;
      for (i = scm->num_test_sockets; i < num_test_sockets; i++)
	{
	  tsock = &scm->test_socket[i];
#ifdef VCL_TEST
	  tsock->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
					     VPPCOM_PROTO_UDP :
					     VPPCOM_PROTO_TCP,
					     1 /* is_nonblocking */ );
	  if (tsock->fd < 0)
	    {
	      errno = -tsock->fd;
	      tsock->fd = -1;
	    }
#else
	  tsock->fd = socket (ctrl->cfg.address_ip6 ? AF_INET6 : AF_INET,
			      ctrl->cfg.transport_udp ?
			      SOCK_DGRAM : SOCK_STREAM, 0);
#endif
	  if (tsock->fd < 0)
	    {
	      errno_val = errno;
	      perror ("ERROR in sock_test_connect_test_sockets()");
	      fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n",
		       errno_val);
	      return tsock->fd;
	    }

#ifdef VCL_TEST
	  rv = vppcom_session_connect (tsock->fd, &scm->server_endpt);
	  if (rv)
	    {
	      errno = -rv;
	      rv = -1;
	    }
#else
	  rv =
	    connect (tsock->fd, (struct sockaddr *) &scm->server_addr,
		     scm->server_addr_size);
#endif
	  if (rv < 0)
	    {
	      errno_val = errno;
	      perror ("ERROR in sock_test_connect_test_sockets()");
	      fprintf (stderr, "CLIENT: ERROR: connect failed "
		       "(errno = %d)!\n", errno_val);
	      return -1;
	    }
	  tsock->cfg = ctrl->cfg;
	  sock_test_socket_buf_alloc (tsock);
	  sock_test_cfg_sync (tsock);

	  printf ("CLIENT (fd %d): Test socket %d connected.\n",
		  tsock->fd, i);
	}
    }

  scm->num_test_sockets = num_test_sockets;
  printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1);
  return 0;
}

static void
dump_help (void)
{
#define INDENT "\n  "

  printf ("CLIENT: Test configuration commands:"
	  INDENT SOCK_TEST_TOKEN_HELP
	  "\t\t\tDisplay help."
	  INDENT SOCK_TEST_TOKEN_EXIT
	  "\t\t\tExit test client & server."
	  INDENT SOCK_TEST_TOKEN_SHOW_CFG
	  "\t\t\tShow the current test cfg."
	  INDENT SOCK_TEST_TOKEN_RUN_UNI
	  "\t\t\tRun the Uni-directional test."
	  INDENT SOCK_TEST_TOKEN_RUN_BI
	  "\t\t\tRun the Bi-directional test."
	  INDENT SOCK_TEST_TOKEN_VERBOSE
	  "\t\t\tToggle verbose setting."
	  INDENT SOCK_TEST_TOKEN_RXBUF_SIZE
	  "<rxbuf size>\tRx buffer size (bytes)."
	  INDENT SOCK_TEST_TOKEN_TXBUF_SIZE
	  "<txbuf size>\tTx buffer size (bytes)."
	  INDENT SOCK_TEST_TOKEN_NUM_WRITES
	  "<# of writes>\tNumber of txbuf writes to server." "\n");
}

static void
cfg_txbuf_size_set (void)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE);
  uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10);

  if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
    {
      ctrl->cfg.txbuf_size = txbuf_size;
      ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
      sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
			   (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size);
      sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
    }
  else
    fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < "
	     "minimum buf size (%u)!\n",
	     txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
}

static void
cfg_num_writes_set (void)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES);
  uint32_t num_writes = strtoul ((const char *) p, NULL, 10);

  if (num_writes > 0)
    {
      ctrl->cfg.num_writes = num_writes;
      ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
      sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
    }
  else
    {
      fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes);
    }
}

static void
cfg_num_test_sockets_set (void)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS);
  uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10);

  if ((num_test_sockets > 0) &&
      (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS))
    {
      ctrl->cfg.num_test_sockets = num_test_sockets;
      sock_test_connect_test_sockets (num_test_sockets);

      sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
    }
  else
    {
      fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: "
	       "%u, (%d max)\n",
	       num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS);
    }
}

static void
cfg_rxbuf_size_set (void)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE);
  uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10);

  if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
    {
      ctrl->cfg.rxbuf_size = rxbuf_size;
      sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
			   (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size);
      sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
    }
  else
    fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < "
	     "minimum buf size (%u)!\n",
	     rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
}

static void
cfg_verbose_toggle (void)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;

  ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1;
  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );

}

static sock_test_t
parse_input ()
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  sock_test_t rv = SOCK_TEST_TYPE_NONE;

  if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf,
		strlen (SOCK_TEST_TOKEN_EXIT)))
    rv = SOCK_TEST_TYPE_EXIT;

  else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_HELP)))
    dump_help ();

  else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_SHOW_CFG)))
    scm->dump_cfg = 1;

  else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_VERBOSE)))
    cfg_verbose_toggle ();

  else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_TXBUF_SIZE)))
    cfg_txbuf_size_set ();

  else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS)))
    cfg_num_test_sockets_set ();

  else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_NUM_WRITES)))
    cfg_num_writes_set ();

  else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_RXBUF_SIZE)))
    cfg_rxbuf_size_set ();

  else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_RUN_UNI)))
    rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI;

  else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf,
		     strlen (SOCK_TEST_TOKEN_RUN_BI)))
    rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI;

  else
    rv = SOCK_TEST_TYPE_ECHO;

  return rv;
}

void
print_usage_and_exit (void)
{
  fprintf (stderr,
	   "sock_test_client [OPTIONS] <ipaddr> <port>\n"
	   "  OPTIONS\n"
	   "  -h               Print this message and exit.\n"
	   "  -6               Use IPv6\n"
	   "  -u               Use UDP transport layer\n"
	   "  -c               Print test config before test.\n"
	   "  -w <dir>         Write test results to <dir>.\n"
	   "  -X               Exit after running test.\n"
	   "  -E               Run Echo test.\n"
	   "  -N <num-writes>  Test Cfg: number of writes.\n"
	   "  -R <rxbuf-size>  Test Cfg: rx buffer size.\n"
	   "  -T <txbuf-size>  Test Cfg: tx buffer size.\n"
	   "  -U               Run Uni-directional test.\n"
	   "  -B               Run Bi-directional test.\n"
	   "  -V               Verbose mode.\n");
  exit (1);
}

int
main (int argc, char **argv)
{
  sock_client_main_t *scm = &sock_client_main;
  sock_test_socket_t *ctrl = &scm->ctrl_socket;
  int c, rv, errno_val;
  sock_test_t post_test = SOCK_TEST_TYPE_NONE;

  sock_test_cfg_init (&ctrl->cfg);
  sock_test_socket_buf_alloc (ctrl);

  opterr = 0;
  while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1)
    switch (c)
      {
      case 'c':
	scm->dump_cfg = 1;
	break;

      case 's':
	if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
	  if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1)
	    {
	      fprintf (stderr, "CLIENT: ERROR: Invalid value for "
		       "option -%c!\n", c);
	      print_usage_and_exit ();
	    }
	if (!ctrl->cfg.num_test_sockets ||
	    (ctrl->cfg.num_test_sockets > FD_SETSIZE))
	  {
	    fprintf (stderr, "CLIENT: ERROR: Invalid number of "
		     "sockets (%d) specified for option -%c!\n"
		     "       Valid range is 1 - %d\n",
		     ctrl->cfg.num_test_sockets, c, FD_SETSIZE);
	    print_usage_and_exit ();
	  }
	break;

      case 'w':
	fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n");
	break;

      case 'X':
	post_test = SOCK_TEST_TYPE_EXIT;
	break;

      case 'E':
	if (strlen (optarg) > ctrl->txbuf_size)
	  {
	    fprintf (stderr, "CLIENT: ERROR: Option -%c value "
		     "larger than txbuf size (%d)!\n",
		     optopt, ctrl->txbuf_size);
	    print_usage_and_exit ();
	  }
	strcpy (ctrl->txbuf, optarg);
	ctrl->cfg.test = SOCK_TEST_TYPE_ECHO;
	break;

      case 'I':
	if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1)
	  if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1)
	    {
	      fprintf (stderr, "CLIENT: ERROR: Invalid value for "
		       "option -%c!\n", c);
	      print_usage_and_exit ();
	    }
	if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS)
	  {
	    fprintf (stderr, "CLIENT: ERROR: value greater than max "
		     "number test sockets (%d)!",
		     SOCK_TEST_CFG_MAX_TEST_SCKTS);
	    print_usage_and_exit ();
	  }
	break;

      case 'N':
	if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1)
	  if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1)
	    {
	      fprintf (stderr, "CLIENT: ERROR: Invalid value for "
		       "option -%c!\n", c);
	      print_usage_and_exit ();
	    }
	ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
	break;

      case 'R':
	if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1)
	  if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1)
	    {
	      fprintf (stderr, "CLIENT: ERROR: Invalid value for "
		       "option -%c!\n", c);
	      print_usage_and_exit ();
	    }
	if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
	  {
	    ctrl->rxbuf_size = ctrl->cfg.rxbuf_size;
	    sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ ,
				 (uint8_t **) & ctrl->rxbuf,
				 &ctrl->rxbuf_size);
	  }
	else
	  {
	    fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) "
		     "less than minumum (%u)\n",
		     ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
	    print_usage_and_exit ();
	  }

	break;

      case 'T':
	if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1)
	  if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1)
	    {
	      fprintf (stderr, "CLIENT: ERROR: Invalid value "
		       "for option -%c!\n", c);
	      print_usage_and_exit ();
	    }
	if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN)
	  {
	    ctrl->txbuf_size = ctrl->cfg.txbuf_size;
	    sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ ,
				 (uint8_t **) & ctrl->txbuf,
				 &ctrl->txbuf_size);
	    ctrl->cfg.total_bytes =
	      ctrl->cfg.num_writes * ctrl->cfg.txbuf_size;
	  }
	else
	  {
	    fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) "
		     "less than minumum (%u)!\n",
		     ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN);
	    print_usage_and_exit ();
	  }
	break;

      case 'U':
	ctrl->cfg.test = SOCK_TEST_TYPE_UNI;
	break;

      case 'B':
	ctrl->cfg.test = SOCK_TEST_TYPE_BI;
	break;

      case 'V':
	ctrl->cfg.verbose = 1;
	break;

      case '6':
	ctrl->cfg.address_ip6 = 1;
	break;

      case 'D':
	ctrl->cfg.transport_udp = 1;
	break;

      case '?':
	switch (optopt)
	  {
	  case 'E':
	  case 'I':
	  case 'N':
	  case 'R':
	  case 'T':
	  case 'w':
	    fprintf (stderr, "CLIENT: ERROR: Option -%c "
		     "requires an argument.\n", optopt);
	    break;

	  default:
	    if (isprint (optopt))
	      fprintf (stderr, "CLIENT: ERROR: Unknown "
		       "option `-%c'.\n", optopt);
	    else
	      fprintf (stderr, "CLIENT: ERROR: Unknown "
		       "option character `\\x%x'.\n", optopt);
	  }
	/* fall thru */
      case 'h':
      default:
	print_usage_and_exit ();
      }

  if (argc < (optind + 2))
    {
      fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n");
      print_usage_and_exit ();
    }

#ifdef VCL_TEST
  ctrl->fd = vppcom_app_create ("vcl_test_client");
  if (ctrl->fd < 0)
    {
      errno = -ctrl->fd;
      ctrl->fd = -1;
    }
  else
    {
      ctrl->fd = vppcom_session_create (ctrl->cfg.transport_udp ?
					VPPCOM_PROTO_UDP :
					VPPCOM_PROTO_TCP,
					0 /* is_nonblocking */ );
      if (ctrl->fd < 0)
	{
	  errno = -ctrl->fd;
	  ctrl->fd = -1;
	}
    }
#else
  ctrl->fd = socket (ctrl->cfg.address_ip6 ? AF_INET6 : AF_INET,
		     ctrl->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM, 0);
#endif

  if (ctrl->fd < 0)
    {
      errno_val = errno;
      perror ("ERROR in main()");
      fprintf (stderr, "CLIENT: ERROR: socket "
	       "failed (errno = %d)!\n", errno_val);
      return ctrl->fd;
    }

  memset (&scm->server_addr, 0, sizeof (scm->server_addr));
  if (ctrl->cfg.address_ip6)
    {
      struct sockaddr_in6 *server_addr =
	(struct sockaddr_in6 *) &scm->server_addr;
      scm->server_addr_size = sizeof (*server_addr);
      server_addr->sin6_family = AF_INET6;
      inet_pton (AF_INET6, argv[optind++], &(server_addr->sin6_addr));
      server_addr->sin6_port = htons (atoi (argv[optind]));
    }
  else
    {
      struct sockaddr_in *server_addr =
	(struct sockaddr_in *) &scm->server_addr;
      scm->server_addr_size = sizeof (*server_addr);
      server_addr->sin_family = AF_INET;
      inet_pton (AF_INET, argv[optind++], &(server_addr->sin_addr));
      server_addr->sin_port = htons (atoi (argv[optind]));
    }

#ifdef VCL_TEST
  if (ctrl->cfg.address_ip6)
    {
      struct sockaddr_in6 *server_addr =
	(struct sockaddr_in6 *) &scm->server_addr;
      scm->server_endpt.is_ip4 = 0;
      scm->server_endpt.ip = (uint8_t *) & server_addr->sin6_addr;
      scm->server_endpt.port = (uint16_t) server_addr->sin6_port;
    }
  else
    {
      struct sockaddr_in *server_addr =
	(struct sockaddr_in *) &scm->server_addr;
      scm->server_endpt.is_ip4 = 1;
      scm->server_endpt.ip = (uint8_t *) & server_addr->sin_addr;
      scm->server_endpt.port = (uint16_t) server_addr->sin_port;
    }
#endif

  do
    {
      printf ("\nCLIENT: Connecting to server...\n");

#ifdef VCL_TEST
      rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt);
      if (rv)
	{
	  errno = -rv;
	  rv = -1;
	}
#else
      rv =
	connect (ctrl->fd, (struct sockaddr *) &scm->server_addr,
		 scm->server_addr_size);
#endif
      if (rv < 0)
	{
	  errno_val = errno;
	  perror ("ERROR in main()");
	  fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n",
		   errno_val);
	  return -1;
	}

      sock_test_cfg_sync (ctrl);
      printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd);
    }
  while (rv < 0);

  sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets);

  while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT)
    {
      if (scm->dump_cfg)
	{
	  sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ );
	  scm->dump_cfg = 0;
	}

      switch (ctrl->cfg.test)
	{
	case SOCK_TEST_TYPE_ECHO:
	  echo_test_client ();
	  break;

	case SOCK_TEST_TYPE_UNI:
	case SOCK_TEST_TYPE_BI:
	  stream_test_client (ctrl->cfg.test);
	  break;

	case SOCK_TEST_TYPE_EXIT:
	  continue;

	case SOCK_TEST_TYPE_NONE:
	default:
	  break;
	}
      switch (post_test)
	{
	case SOCK_TEST_TYPE_EXIT:
	  switch (ctrl->cfg.test)
	    {
	    case SOCK_TEST_TYPE_EXIT:
	    case SOCK_TEST_TYPE_UNI:
	    case SOCK_TEST_TYPE_BI:
	    case SOCK_TEST_TYPE_ECHO:
	      ctrl->cfg.test = SOCK_TEST_TYPE_EXIT;
	      continue;

	    case SOCK_TEST_TYPE_NONE:
	    default:
	      break;
	    }
	  break;

	case SOCK_TEST_TYPE_NONE:
	case SOCK_TEST_TYPE_ECHO:
	case SOCK_TEST_TYPE_UNI:
	case SOCK_TEST_TYPE_BI:
	default:
	  break;
	}

      memset (ctrl->txbuf, 0, ctrl->txbuf_size);
      memset (ctrl->rxbuf, 0, ctrl->rxbuf_size);

      printf ("\nCLIENT: Type some characters and hit <return>\n"
	      "('" SOCK_TEST_TOKEN_HELP "' for help): ");

      if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL)
	{
	  if (strlen (ctrl->txbuf) == 1)
	    {
	      printf ("\nCLIENT: Nothing to send!  Please try again...\n");
	      continue;
	    }
	  ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0;	// chomp the newline.

	  /* Parse input for keywords */
	  ctrl->cfg.test = parse_input ();
	}
    }

  exit_client ();
#ifdef VCL_TEST
  vppcom_session_close (ctrl->fd);
  vppcom_app_destroy ();
  return 0;
#else
  close (ctrl->fd);
  return (scm->af_unix_echo_tx == scm->af_unix_echo_rx) ? 0 : -1;
#endif
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */