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