bd777ccb91733ee20fee70bc2c5b436ac50e0fb1
[vpp.git] / src / plugins / hs_apps / vcl / sock_test_server.c
1 /*
2  * Copyright (c) 2017-2019 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 <sys/types.h>
19 #include <sys/socket.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #include <ctype.h>
24 #include <hs_apps/vcl/sock_test.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28
29 #define SOCK_SERVER_USE_EPOLL 1
30 #define VPPCOM_SESSION_ATTR_UNIT_TEST 0
31
32 #if SOCK_SERVER_USE_EPOLL
33 #include <sys/epoll.h>
34 #if !defined(VCL_TEST)
35 #include <sys/un.h>
36 #endif
37 #endif
38
39 #ifdef VCL_TEST
40 #if VPPCOM_SESSION_ATTR_UNIT_TEST
41 #define BUFLEN  sizeof (uint64_t) * 16
42 uint64_t buffer[16];
43 uint32_t buflen = BUFLEN;
44 uint32_t *flags = (uint32_t *) buffer;
45 #endif
46 #endif
47
48 typedef struct
49 {
50   uint8_t is_alloc;
51   int fd;
52   uint8_t *buf;
53   uint32_t buf_size;
54   vcl_test_cfg_t cfg;
55   vcl_test_stats_t stats;
56 #ifdef VCL_TEST
57   vppcom_endpt_t endpt;
58   uint8_t ip[16];
59 #endif
60 } sock_server_conn_t;
61
62 typedef struct
63 {
64   uint32_t port;
65   uint32_t address_ip6;
66   uint32_t transport_udp;
67 } sock_server_cfg_t;
68
69 #define SOCK_SERVER_MAX_TEST_CONN  10
70 #define SOCK_SERVER_MAX_EPOLL_EVENTS 10
71 typedef struct
72 {
73   int listen_fd;
74   sock_server_cfg_t cfg;
75 #if SOCK_SERVER_USE_EPOLL
76   int epfd;
77   struct epoll_event listen_ev;
78   struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
79 #if !defined (VCL_TEST)
80   int af_unix_listen_fd;
81   int af_unix_fd;
82   struct epoll_event af_unix_listen_ev;
83   struct sockaddr_un serveraddr;
84   uint32_t af_unix_xacts;
85 #endif
86 #endif
87   size_t num_conn;
88   size_t conn_pool_size;
89   sock_server_conn_t *conn_pool;
90   int nfds;
91   fd_set rd_fdset;
92   fd_set wr_fdset;
93   struct timeval timeout;
94 } sock_server_main_t;
95
96 sock_server_main_t sock_server_main;
97
98 #if ! SOCK_SERVER_USE_EPOLL
99 static inline int
100 get_nfds (void)
101 {
102   sock_server_main_t *ssm = &sock_server_main;
103   int i, nfds;
104
105   for (nfds = i = 0; i < FD_SETSIZE; i++)
106     {
107       if (FD_ISSET (i, &ssm->rd_fdset) || FD_ISSET (i, &ssm->wr_fdset))
108         nfds = i + 1;
109     }
110   return nfds;
111 }
112
113 static inline void
114 conn_fdset_set (sock_server_conn_t * conn, fd_set * fdset)
115 {
116   sock_server_main_t *ssm = &sock_server_main;
117
118   FD_SET (conn->fd, fdset);
119   ssm->nfds = get_nfds ();
120 }
121
122 static inline void
123 conn_fdset_clr (sock_server_conn_t * conn, fd_set * fdset)
124 {
125   sock_server_main_t *ssm = &sock_server_main;
126
127   FD_CLR (conn->fd, fdset);
128   ssm->nfds = get_nfds ();
129 }
130 #endif
131
132 static inline void
133 conn_pool_expand (size_t expand_size)
134 {
135   sock_server_main_t *ssm = &sock_server_main;
136   sock_server_conn_t *conn_pool;
137   size_t new_size = ssm->conn_pool_size + expand_size;
138   int i;
139
140   conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
141   if (conn_pool)
142     {
143       for (i = ssm->conn_pool_size; i < new_size; i++)
144         {
145           sock_server_conn_t *conn = &conn_pool[i];
146           memset (conn, 0, sizeof (*conn));
147           vcl_test_cfg_init (&conn->cfg);
148           vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
149                               &conn->buf, &conn->buf_size);
150           conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
151         }
152
153       ssm->conn_pool = conn_pool;
154       ssm->conn_pool_size = new_size;
155     }
156   else
157     {
158       int errno_val = errno;
159       perror ("ERROR in conn_pool_expand()");
160       fprintf (stderr, "SERVER: ERROR: Memory allocation "
161                "failed (errno = %d)!\n", errno_val);
162     }
163 }
164
165 static inline sock_server_conn_t *
166 conn_pool_alloc (void)
167 {
168   sock_server_main_t *ssm = &sock_server_main;
169   int i;
170
171   for (i = 0; i < ssm->conn_pool_size; i++)
172     {
173       if (!ssm->conn_pool[i].is_alloc)
174         {
175 #ifdef VCL_TEST
176           ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip;
177 #endif
178           ssm->conn_pool[i].is_alloc = 1;
179           return (&ssm->conn_pool[i]);
180         }
181     }
182
183   return 0;
184 }
185
186 static inline void
187 conn_pool_free (sock_server_conn_t * conn)
188 {
189 #if ! SOCK_SERVER_USE_EPOLL
190   sock_server_main_t *ssm = &sock_server_main;
191
192   conn_fdset_clr (conn, &ssm->rd_fdset);
193   conn_fdset_clr (conn, &ssm->wr_fdset);
194 #endif
195   conn->fd = 0;
196   conn->is_alloc = 0;
197 }
198
199 static inline void
200 sync_config_and_reply (sock_server_conn_t * conn, vcl_test_cfg_t * rx_cfg)
201 {
202   conn->cfg = *rx_cfg;
203   vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
204                       &conn->buf, &conn->buf_size);
205   conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
206
207   if (conn->cfg.verbose)
208     {
209       printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
210       vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
211     }
212   (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
213                           sizeof (conn->cfg), NULL, conn->cfg.verbose);
214 }
215
216 static void
217 stream_test_server_start_stop (sock_server_conn_t * conn,
218                                vcl_test_cfg_t * rx_cfg)
219 {
220   sock_server_main_t *ssm = &sock_server_main;
221   int client_fd = conn->fd;
222   vcl_test_t test = rx_cfg->test;
223
224   if (rx_cfg->ctrl_handle == conn->fd)
225     {
226       int i;
227       clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
228
229       for (i = 0; i < ssm->conn_pool_size; i++)
230         {
231           sock_server_conn_t *tc = &ssm->conn_pool[i];
232
233           if (tc->cfg.ctrl_handle == conn->fd)
234             {
235               vcl_test_stats_accumulate (&conn->stats, &tc->stats);
236
237               if (conn->cfg.verbose)
238                 {
239                   static char buf[64];
240
241                   snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS",
242                             tc->fd);
243                   vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
244                                        test == VCL_TEST_TYPE_BI
245                                        /* show tx */ ,
246                                        conn->cfg.verbose);
247                 }
248             }
249         }
250
251       vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
252                            (test == VCL_TEST_TYPE_BI) /* show_tx */ ,
253                            conn->cfg.verbose);
254       vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
255       if (conn->cfg.verbose)
256         {
257           printf ("  sock server main\n"
258                   VCL_TEST_SEPARATOR_STRING
259                   "       buf:  %p\n"
260                   "  buf size:  %u (0x%08x)\n"
261                   VCL_TEST_SEPARATOR_STRING,
262                   conn->buf, conn->buf_size, conn->buf_size);
263         }
264
265       sync_config_and_reply (conn, rx_cfg);
266       printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
267               SOCK_TEST_BANNER_STRING "\n", conn->fd,
268               test == VCL_TEST_TYPE_BI ? "Bi" : "Uni");
269     }
270   else
271     {
272       printf ("\n" SOCK_TEST_BANNER_STRING
273               "SERVER (fd %d): %s-directional Stream Test!\n"
274               "  Sending client the test cfg to start streaming data...\n",
275               client_fd, test == VCL_TEST_TYPE_BI ? "Bi" : "Uni");
276
277       rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
278         rx_cfg->ctrl_handle;
279
280       sync_config_and_reply (conn, rx_cfg);
281
282       /* read the 1st chunk, record start time */
283       memset (&conn->stats, 0, sizeof (conn->stats));
284       clock_gettime (CLOCK_REALTIME, &conn->stats.start);
285     }
286 }
287
288
289 static inline void
290 stream_test_server (sock_server_conn_t * conn, int rx_bytes)
291 {
292   int client_fd = conn->fd;
293   vcl_test_t test = conn->cfg.test;
294
295   if (test == VCL_TEST_TYPE_BI)
296     (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
297                             conn->cfg.verbose);
298
299   if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
300     {
301       clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
302     }
303 }
304
305 #if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
306 static inline void
307 af_unix_echo (void)
308 {
309   sock_server_main_t *ssm = &sock_server_main;
310   int af_unix_client_fd;
311   int rv;
312   int errno_val;
313   uint8_t buffer[256];
314   size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
315
316 #if HAVE_ACCEPT4
317   af_unix_client_fd = accept4 (ssm->af_unix_listen_fd,
318                                (struct sockaddr *) NULL, NULL, NULL);
319 #else
320   af_unix_client_fd = accept (ssm->af_unix_listen_fd,
321                               (struct sockaddr *) NULL, NULL);
322 #endif
323   if (af_unix_client_fd < 0)
324     {
325       errno_val = errno;
326       perror ("ERROR in af_unix_accept()");
327       fprintf (stderr, "SERVER: ERROR: accept failed "
328                "(errno = %d)!\n", errno_val);
329       return;
330     }
331
332   printf ("SERVER: Got an AF_UNIX connection -- fd = %d (0x%08x)!\n",
333           af_unix_client_fd, af_unix_client_fd);
334
335   memset (buffer, 0, sizeof (buffer));
336
337   rv = read (af_unix_client_fd, buffer, nbytes);
338   if (rv < 0)
339     {
340       errno_val = errno;
341       perror ("ERROR in af_unix_echo(): read() failed");
342       fprintf (stderr, "SERVER: ERROR: read(af_unix_client_fd %d (0x%x), "
343                "nbytes %lu) failed (errno = %d)!\n", af_unix_client_fd,
344                af_unix_client_fd, nbytes, errno_val);
345       goto done;
346     }
347   /* Make the buffer is NULL-terminated. */
348   buffer[sizeof (buffer) - 1] = 0;
349   printf ("SERVER (AF_UNIX): RX (%d bytes) - '%s'\n", rv, buffer);
350
351   if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
352     {
353       rv = write (af_unix_client_fd, buffer, nbytes);
354       if (rv < 0)
355         {
356           errno_val = errno;
357           perror ("ERROR in af_unix_echo(): write() failed");
358           fprintf (stderr,
359                    "SERVER: ERROR: write(af_unix_client_fd %d (0x%x), "
360                    "\"%s\", nbytes %ld) failed (errno = %d)!\n",
361                    af_unix_client_fd, af_unix_client_fd, buffer, nbytes,
362                    errno_val);
363           goto done;
364         }
365       printf ("SERVER (AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
366       ssm->af_unix_xacts++;
367     }
368 done:
369   close (af_unix_client_fd);
370 }
371
372 #endif
373
374 static inline void
375 new_client (void)
376 {
377   sock_server_main_t *ssm = &sock_server_main;
378   int client_fd;
379   sock_server_conn_t *conn;
380
381   if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
382     conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
383
384   conn = conn_pool_alloc ();
385   if (!conn)
386     {
387       fprintf (stderr, "\nSERVER: ERROR: No free connections!\n");
388       return;
389     }
390
391 #ifdef VCL_TEST
392   client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, 0);
393   if (client_fd < 0)
394     errno = -client_fd;
395 #elif HAVE_ACCEPT4
396   client_fd = accept4 (ssm->listen_fd, (struct sockaddr *) NULL, NULL, NULL);
397 #else
398   client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
399 #endif
400   if (client_fd < 0)
401     {
402       int errno_val;
403       errno_val = errno;
404       perror ("ERROR in new_client()");
405       fprintf (stderr, "SERVER: ERROR: accept failed "
406                "(errno = %d)!\n", errno_val);
407       return;
408     }
409
410   printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
411           client_fd, client_fd);
412
413   conn->fd = client_fd;
414
415 #if ! SOCK_SERVER_USE_EPOLL
416   conn_fdset_set (conn, &ssm->rd_fdset);
417   ssm->nfds++;
418 #else
419   {
420     struct epoll_event ev;
421     int rv;
422
423     ev.events = EPOLLIN;
424     ev.data.u64 = conn - ssm->conn_pool;
425 #ifdef VCL_TEST
426     rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
427     if (rv)
428       errno = -rv;
429 #else
430     rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
431 #endif
432     if (rv < 0)
433       {
434         int errno_val;
435         errno_val = errno;
436         perror ("ERROR in new_client()");
437         fprintf (stderr, "SERVER: ERROR: epoll_ctl failed (errno = %d)!\n",
438                  errno_val);
439       }
440     else
441       ssm->nfds++;
442   }
443 #endif
444 }
445
446 void
447 print_usage_and_exit (void)
448 {
449   fprintf (stderr,
450            "sock_test_server [OPTIONS] <port>\n"
451            "  OPTIONS\n"
452            "  -h               Print this message and exit.\n"
453            "  -6               Use IPv6\n"
454            "  -u               Use UDP transport layer\n");
455   exit (1);
456 }
457
458 int
459 main (int argc, char **argv)
460 {
461   sock_server_main_t *ssm = &sock_server_main;
462   int client_fd, rv, main_rv = 0;
463   int tx_bytes, rx_bytes, nbytes;
464   sock_server_conn_t *conn;
465   vcl_test_cfg_t *rx_cfg;
466   uint32_t xtra = 0;
467   uint64_t xtra_bytes = 0;
468   struct sockaddr_storage servaddr;
469   int errno_val;
470   int c, v, i;
471   uint16_t port = VCL_TEST_SERVER_PORT;
472 #if ! SOCK_SERVER_USE_EPOLL
473   fd_set _rfdset, *rfdset = &_rfdset;
474 #endif
475 #ifdef VCL_TEST
476   vppcom_endpt_t endpt;
477 #else
478   uint32_t servaddr_size;
479 #if ! SOCK_SERVER_USE_EPOLL
480   fd_set _wfdset, *wfdset = &_wfdset;
481 #endif
482 #endif
483
484   opterr = 0;
485   while ((c = getopt (argc, argv, "6D")) != -1)
486     switch (c)
487       {
488       case '6':
489         ssm->cfg.address_ip6 = 1;
490         break;
491
492       case 'D':
493         ssm->cfg.transport_udp = 1;
494         break;
495
496       case '?':
497         switch (optopt)
498           {
499           default:
500             if (isprint (optopt))
501               fprintf (stderr, "SERVER: ERROR: Unknown "
502                        "option `-%c'.\n", optopt);
503             else
504               fprintf (stderr, "SERVER: ERROR: Unknown "
505                        "option character `\\x%x'.\n", optopt);
506           }
507         /* fall thru */
508       case 'h':
509       default:
510         print_usage_and_exit ();
511       }
512
513   if (argc < (optind + 1))
514     {
515       fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
516       print_usage_and_exit ();
517     }
518
519   if (sscanf (argv[optind], "%d", &v) == 1)
520     port = (uint16_t) v;
521   else
522     {
523       fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
524       print_usage_and_exit ();
525     }
526
527   conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
528
529 #ifdef VCL_TEST
530   rv = vppcom_app_create ("vcl_test_server");
531   if (rv)
532     {
533       errno = -rv;
534       ssm->listen_fd = -1;
535     }
536   else
537     {
538       ssm->listen_fd = vppcom_session_create (ssm->cfg.transport_udp ?
539                                               VPPCOM_PROTO_UDP :
540                                               VPPCOM_PROTO_TCP,
541                                               0 /* is_nonblocking */ );
542     }
543 #else
544   ssm->listen_fd = socket (ssm->cfg.address_ip6 ? AF_INET6 : AF_INET,
545                            ssm->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM,
546                            0);
547 #if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
548   unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
549   ssm->af_unix_listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
550   if (ssm->af_unix_listen_fd < 0)
551     {
552       errno_val = errno;
553       perror ("ERROR in main(): socket(AF_UNIX) failed");
554       fprintf (stderr,
555                "SERVER: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed "
556                "(errno = %d)!\n", errno_val);
557       return ssm->af_unix_listen_fd;
558     }
559
560   memset (&ssm->serveraddr, 0, sizeof (ssm->serveraddr));
561   ssm->serveraddr.sun_family = AF_UNIX;
562   strcpy (ssm->serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME);
563
564   rv = bind (ssm->af_unix_listen_fd, (struct sockaddr *) &ssm->serveraddr,
565              SUN_LEN (&ssm->serveraddr));
566   if (rv < 0)
567     {
568       errno_val = errno;
569       perror ("ERROR in main(): bind(SOCK_TEST_AF_UNIX_FILENAME) failed");
570       fprintf (stderr, "SERVER: ERROR: bind() fd %d, \"%s\": "
571                "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
572                SOCK_TEST_AF_UNIX_FILENAME, errno_val);
573       close (ssm->af_unix_listen_fd);
574       unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
575       return rv;
576     }
577
578   rv = listen (ssm->af_unix_listen_fd, 10);
579   if (rv < 0)
580     {
581       errno_val = errno;
582       perror ("ERROR in main(): listen(AF_UNIX) failed");
583       fprintf (stderr, "SERVER: ERROR: listen() fd %d, \"%s\": "
584                "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
585                SOCK_TEST_AF_UNIX_FILENAME, errno_val);
586       close (ssm->af_unix_listen_fd);
587       unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
588       return rv;
589     }
590 #endif /* SOCK_SERVER_USE_EPOLL */
591 #endif
592   if (ssm->listen_fd < 0)
593     {
594       errno_val = errno;
595       perror ("ERROR in main()");
596       fprintf (stderr, "SERVER: ERROR: socket() failed "
597                "(errno = %d)!\n", errno_val);
598       return ssm->listen_fd;
599     }
600
601   memset (&servaddr, 0, sizeof (servaddr));
602
603   if (ssm->cfg.address_ip6)
604     {
605       struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
606 #ifndef VCL_TEST
607       servaddr_size = sizeof (*server_addr);
608 #endif
609       server_addr->sin6_family = AF_INET6;
610       server_addr->sin6_addr = in6addr_any;
611       server_addr->sin6_port = htons (port);
612     }
613   else
614     {
615       struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
616 #ifndef VCL_TEST
617       servaddr_size = sizeof (*server_addr);
618 #endif
619       server_addr->sin_family = AF_INET;
620       server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
621       server_addr->sin_port = htons (port);
622     }
623
624 #ifdef VCL_TEST
625   if (ssm->cfg.address_ip6)
626     {
627       struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
628       endpt.is_ip4 = 0;
629       endpt.ip = (uint8_t *) & server_addr->sin6_addr;
630       endpt.port = (uint16_t) server_addr->sin6_port;
631     }
632   else
633     {
634       struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
635       endpt.is_ip4 = 1;
636       endpt.ip = (uint8_t *) & server_addr->sin_addr;
637       endpt.port = (uint16_t) server_addr->sin_port;
638     }
639
640   rv = vppcom_session_bind (ssm->listen_fd, &endpt);
641   if (rv)
642     {
643       errno = -rv;
644       rv = -1;
645     }
646 #else
647   rv = bind (ssm->listen_fd, (struct sockaddr *) &servaddr, servaddr_size);
648 #endif
649   if (rv < 0)
650     {
651       errno_val = errno;
652       perror ("ERROR in main()");
653       fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n",
654                errno_val);
655       return rv;
656     }
657   if (fcntl (ssm->listen_fd, F_SETFL, O_NONBLOCK) < 0)
658     {
659       errno_val = errno;
660       perror ("ERROR in main()");
661       fprintf (stderr, "SERVER: ERROR: fcntl failed (errno = %d)!\n",
662                errno_val);
663       return rv;
664     }
665
666 #ifdef VCL_TEST
667   rv = vppcom_session_listen (ssm->listen_fd, 10);
668   if (rv)
669     {
670       errno = -rv;
671       rv = -1;
672     }
673 #else
674   rv = listen (ssm->listen_fd, 10);
675 #endif
676   if (rv < 0)
677     {
678       errno_val = errno;
679       perror ("ERROR in main()");
680       fprintf (stderr, "SERVER: ERROR: listen failed "
681                "(errno = %d)!\n", errno_val);
682       return rv;
683     }
684
685 #if ! SOCK_SERVER_USE_EPOLL
686
687   FD_ZERO (&ssm->wr_fdset);
688   FD_ZERO (&ssm->rd_fdset);
689
690   FD_SET (ssm->listen_fd, &ssm->rd_fdset);
691   ssm->nfds = ssm->listen_fd + 1;
692
693 #else
694 #ifdef VCL_TEST
695   ssm->epfd = vppcom_epoll_create ();
696   if (ssm->epfd < 0)
697     errno = -ssm->epfd;
698 #else
699   ssm->epfd = epoll_create (1);
700 #endif
701   if (ssm->epfd < 0)
702     {
703       errno_val = errno;
704       perror ("ERROR in main()");
705       fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n",
706                errno_val);
707       return ssm->epfd;
708     }
709
710   ssm->listen_ev.events = EPOLLIN;
711   ssm->listen_ev.data.u32 = ~0;
712 #ifdef VCL_TEST
713   rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd,
714                          &ssm->listen_ev);
715   if (rv < 0)
716     errno = -rv;
717 #else
718   ssm->af_unix_listen_ev.events = EPOLLIN;
719   ssm->af_unix_listen_ev.data.u32 = SOCK_TEST_AF_UNIX_ACCEPT_DATA;
720   rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->af_unix_listen_fd,
721                   &ssm->af_unix_listen_ev);
722   if (rv < 0)
723     {
724       errno_val = errno;
725       perror ("ERROR in main(): mixed epoll_ctl(EPOLL_CTL_ADD)");
726       fprintf (stderr, "SERVER: ERROR: mixed epoll_ctl(epfd %d (0x%x), "
727                "EPOLL_CTL_ADD, af_unix_listen_fd %d (0x%x), EPOLLIN) failed "
728                "(errno = %d)!\n", ssm->epfd, ssm->epfd,
729                ssm->af_unix_listen_fd, ssm->af_unix_listen_fd, errno_val);
730       close (ssm->af_unix_listen_fd);
731       unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
732       return rv;
733     }
734
735   rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
736 #endif
737   if (rv < 0)
738     {
739       errno_val = errno;
740       perror ("ERROR in main()");
741       fprintf (stderr, "SERVER: ERROR: epoll_ctl failed "
742                "(errno = %d)!\n", errno_val);
743       return rv;
744     }
745 #endif
746
747   printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
748
749   while (1)
750     {
751 #if ! SOCK_SERVER_USE_EPOLL
752       _rfdset = ssm->rd_fdset;
753
754 #ifdef VCL_TEST
755       rv = vppcom_select (ssm->nfds, (unsigned long *) rfdset, NULL, NULL, 0);
756 #else
757       {
758         struct timeval timeout;
759         timeout = ssm->timeout;
760         _wfdset = ssm->wr_fdset;
761         rv = select (ssm->nfds, rfdset, wfdset, NULL, &timeout);
762       }
763 #endif
764       if (rv < 0)
765         {
766           perror ("select()");
767           fprintf (stderr, "\nSERVER: ERROR: select() failed -- aborting!\n");
768           main_rv = -1;
769           goto done;
770         }
771       else if (rv == 0)
772         continue;
773
774       if (FD_ISSET (ssm->listen_fd, rfdset))
775         new_client ();
776
777       for (i = 0; i < ssm->conn_pool_size; i++)
778         {
779           if (!ssm->conn_pool[i].is_alloc)
780             continue;
781
782           conn = &ssm->conn_pool[i];
783 #else
784       int num_ev;
785 #ifdef VCL_TEST
786       num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events,
787                                   SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0);
788       if (num_ev < 0)
789         errno = -num_ev;
790 #else
791       num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
792                            SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
793 #endif
794       if (num_ev < 0)
795         {
796           perror ("epoll_wait()");
797           fprintf (stderr, "\nSERVER: ERROR: epoll_wait() "
798                    "failed -- aborting!\n");
799           main_rv = -1;
800           goto done;
801         }
802       if (num_ev == 0)
803         {
804           fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n");
805           continue;
806         }
807       for (i = 0; i < num_ev; i++)
808         {
809           conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
810           if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
811             {
812 #ifdef VCL_TEST
813               vppcom_session_close (conn->fd);
814 #else
815               close (conn->fd);
816 #endif
817               continue;
818             }
819           if (ssm->wait_events[i].data.u32 == ~0)
820             {
821               new_client ();
822               continue;
823             }
824 #if !defined (VCL_TEST)
825           else if (ssm->wait_events[i].data.u32 ==
826                    SOCK_TEST_AF_UNIX_ACCEPT_DATA)
827             {
828               af_unix_echo ();
829               continue;
830             }
831 #endif
832 #endif
833           client_fd = conn->fd;
834
835 #if ! SOCK_SERVER_USE_EPOLL
836           if (FD_ISSET (client_fd, rfdset))
837 #else
838           if (EPOLLIN & ssm->wait_events[i].events)
839 #endif
840             {
841             read_again:
842               rx_bytes = sock_test_read (client_fd, conn->buf,
843                                          conn->buf_size, &conn->stats);
844               if (rx_bytes > 0)
845                 {
846                   rx_cfg = (vcl_test_cfg_t *) conn->buf;
847                   if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
848                     {
849                       if (rx_cfg->verbose)
850                         {
851                           printf ("SERVER (fd %d): Received a cfg message!\n",
852                                   client_fd);
853                           vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
854                         }
855
856                       if (rx_bytes != sizeof (*rx_cfg))
857                         {
858                           printf ("SERVER (fd %d): Invalid cfg message "
859                                   "size (%d)!\n  Should be %lu bytes.\n",
860                                   client_fd, rx_bytes, sizeof (*rx_cfg));
861                           conn->cfg.rxbuf_size = 0;
862                           conn->cfg.num_writes = 0;
863                           if (conn->cfg.verbose)
864                             {
865                               printf ("SERVER (fd %d): Replying to "
866                                       "cfg message!\n", client_fd);
867                               vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
868                             }
869                           sock_test_write (client_fd, (uint8_t *) & conn->cfg,
870                                            sizeof (conn->cfg), NULL,
871                                            conn->cfg.verbose);
872                           continue;
873                         }
874
875                       switch (rx_cfg->test)
876                         {
877                         case VCL_TEST_TYPE_NONE:
878                         case VCL_TEST_TYPE_ECHO:
879                           sync_config_and_reply (conn, rx_cfg);
880                           break;
881
882                         case VCL_TEST_TYPE_BI:
883                         case VCL_TEST_TYPE_UNI:
884                           stream_test_server_start_stop (conn, rx_cfg);
885                           break;
886
887                         case VCL_TEST_TYPE_EXIT:
888                           printf ("SERVER: Have a great day, "
889                                   "connection %d!\n", client_fd);
890 #ifdef VCL_TEST
891                           vppcom_session_close (client_fd);
892 #else
893                           close (client_fd);
894 #endif
895                           conn_pool_free (conn);
896                           printf ("SERVER: Closed client fd %d\n", client_fd);
897 #if ! SOCK_SERVER_USE_EPOLL
898                           if (ssm->nfds == (ssm->listen_fd + 1))
899 #else
900                           ssm->nfds--;
901                           if (!ssm->nfds)
902 #endif
903                             {
904                               printf ("SERVER: All client connections "
905                                       "closed.\n\nSERVER: "
906                                       "May the force be with you!\n\n");
907                               goto done;
908                             }
909                           break;
910
911                         default:
912                           fprintf (stderr,
913                                    "SERVER: ERROR: Unknown test type!\n");
914                           vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
915                           break;
916                         }
917                       continue;
918                     }
919
920                   else if ((conn->cfg.test == VCL_TEST_TYPE_UNI) ||
921                            (conn->cfg.test == VCL_TEST_TYPE_BI))
922                     {
923                       stream_test_server (conn, rx_bytes);
924                       if (ioctl (conn->fd, FIONREAD))
925                         goto read_again;
926                       continue;
927                     }
928
929                   else if (isascii (conn->buf[0]))
930                     {
931                       // If it looks vaguely like a string, make sure it's terminated
932                       ((char *) conn->buf)[rx_bytes <
933                                            conn->buf_size ? rx_bytes :
934                                            conn->buf_size - 1] = 0;
935                       printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
936                               conn->fd, rx_bytes, conn->buf);
937                     }
938                 }
939               else              // rx_bytes < 0
940                 {
941                   if (errno == ECONNRESET)
942                     {
943                       printf ("\nSERVER: Connection reset by remote peer.\n"
944                               "  Y'all have a great day now!\n\n");
945                       break;
946                     }
947                   else
948                     continue;
949                 }
950
951               if (isascii (conn->buf[0]))
952                 {
953                   /* If it looks vaguely like a string,
954                    * make sure it's terminated
955                    */
956                   ((char *) conn->buf)[rx_bytes <
957                                        conn->buf_size ? rx_bytes :
958                                        conn->buf_size - 1] = 0;
959                   if (xtra)
960                     fprintf (stderr, "SERVER: ERROR: "
961                              "FIFO not drained in previous test!\n"
962                              "       extra chunks %u (0x%x)\n"
963                              "        extra bytes %lu (0x%lx)\n",
964                              xtra, xtra, xtra_bytes, xtra_bytes);
965
966                   xtra = 0;
967                   xtra_bytes = 0;
968
969                   if (conn->cfg.verbose)
970                     printf ("SERVER (fd %d): Echoing back\n", client_fd);
971
972                   nbytes = strlen ((const char *) conn->buf) + 1;
973
974                   tx_bytes = sock_test_write (client_fd, conn->buf,
975                                               nbytes, &conn->stats,
976                                               conn->cfg.verbose);
977                   if (tx_bytes >= 0)
978                     printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
979                             conn->fd, tx_bytes, conn->buf);
980                 }
981
982               else              // Extraneous read data from non-echo tests???
983                 {
984                   xtra++;
985                   xtra_bytes += rx_bytes;
986                 }
987             }
988         }
989     }
990
991 done:
992 #ifdef VCL_TEST
993   vppcom_session_close (ssm->listen_fd);
994   vppcom_app_destroy ();
995 #else
996   close (ssm->listen_fd);
997
998 #if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
999   close (ssm->af_unix_listen_fd);
1000   unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
1001 #endif /* SOCK_SERVER_USE_EPOLL */
1002
1003 #endif
1004   if (ssm->conn_pool)
1005     free (ssm->conn_pool);
1006
1007   return main_rv;
1008 }
1009
1010 /*
1011  * fd.io coding-style-patch-verification: ON
1012  *
1013  * Local Variables:
1014  * eval: (c-set-style "gnu")
1015  * End:
1016  */