vcl svm: provide apps access to fifo chunks
[vpp.git] / src / plugins / hs_apps / vcl / vcl_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 <sys/stat.h>
25 #include <fcntl.h>
26 #include <hs_apps/vcl/vcl_test.h>
27 #include <sys/epoll.h>
28 #include <vppinfra/mem.h>
29 #include <pthread.h>
30
31 typedef struct
32 {
33   uint8_t is_alloc;
34   int fd;
35   uint8_t *buf;
36   uint32_t buf_size;
37   vcl_test_cfg_t cfg;
38   vcl_test_stats_t stats;
39   vppcom_endpt_t endpt;
40   uint8_t ip[16];
41   vppcom_data_segment_t ds[2];
42 } vcl_test_server_conn_t;
43
44 typedef struct
45 {
46   uint16_t port;
47   uint32_t address_ip6;
48   u8 proto;
49   u8 workers;
50   vppcom_endpt_t endpt;
51 } vcl_test_server_cfg_t;
52
53 typedef struct
54 {
55   uint32_t wrk_index;
56   int listen_fd;
57   int epfd;
58   struct epoll_event wait_events[VCL_TEST_CFG_MAX_EPOLL_EVENTS];
59   size_t conn_pool_size;
60   vcl_test_server_conn_t *conn_pool;
61   int nfds;
62   pthread_t thread_handle;
63 } vcl_test_server_worker_t;
64
65 typedef struct
66 {
67   vcl_test_server_cfg_t cfg;
68   vcl_test_server_worker_t *workers;
69
70   struct sockaddr_storage servaddr;
71   volatile int worker_fails;
72   volatile int active_workers;
73   u8 use_ds;
74 } vcl_test_server_main_t;
75
76 static __thread int __wrk_index = 0;
77
78 static vcl_test_server_main_t vcl_server_main;
79
80 static inline void
81 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
82 {
83   vcl_test_server_conn_t *conn_pool;
84   size_t new_size = wrk->conn_pool_size + expand_size;
85   int i;
86
87   conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
88   if (conn_pool)
89     {
90       for (i = wrk->conn_pool_size; i < new_size; i++)
91         {
92           vcl_test_server_conn_t *conn = &conn_pool[i];
93           memset (conn, 0, sizeof (*conn));
94           vcl_test_cfg_init (&conn->cfg);
95           vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
96                               &conn->buf, &conn->buf_size);
97           conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
98         }
99
100       wrk->conn_pool = conn_pool;
101       wrk->conn_pool_size = new_size;
102     }
103   else
104     {
105       vterr ("conn_pool_expand()", -errno);
106     }
107 }
108
109 static inline vcl_test_server_conn_t *
110 conn_pool_alloc (vcl_test_server_worker_t * wrk)
111 {
112   int i, expand = 0;
113
114 again:
115   for (i = 0; i < wrk->conn_pool_size; i++)
116     {
117       if (!wrk->conn_pool[i].is_alloc)
118         {
119           wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
120           wrk->conn_pool[i].is_alloc = 1;
121           return (&wrk->conn_pool[i]);
122         }
123     }
124
125   if (expand == 0)
126     {
127       conn_pool_expand (wrk, 2 * wrk->conn_pool_size);
128       expand = 1;
129       goto again;
130     }
131   vtwrn ("Failed to allocate connection even after expand");
132   return 0;
133 }
134
135 static inline void
136 conn_pool_free (vcl_test_server_conn_t * conn)
137 {
138   conn->fd = 0;
139   conn->is_alloc = 0;
140 }
141
142 static inline void
143 sync_config_and_reply (vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg)
144 {
145   conn->cfg = *rx_cfg;
146   vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
147                       &conn->buf, &conn->buf_size);
148   conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
149
150   if (conn->cfg.verbose)
151     {
152       vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
153       vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
154     }
155   (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
156                          sizeof (conn->cfg), NULL, conn->cfg.verbose);
157 }
158
159 static void
160 vts_server_start_stop (vcl_test_server_worker_t * wrk,
161                        vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg)
162 {
163   u8 is_bi = rx_cfg->test == VCL_TEST_TYPE_BI;
164   vcl_test_server_conn_t *tc;
165   char buf[64];
166   int i;
167
168   if (rx_cfg->ctrl_handle == conn->fd)
169     {
170       for (i = 0; i < wrk->conn_pool_size; i++)
171         {
172           tc = &wrk->conn_pool[i];
173           if (tc->cfg.ctrl_handle != conn->fd)
174             continue;
175
176           vcl_test_stats_accumulate (&conn->stats, &tc->stats);
177           if (vcl_comp_tspec (&conn->stats.stop, &tc->stats.stop) < 0)
178             conn->stats.stop = tc->stats.stop;
179           /* Client delays sending of disconnect */
180           conn->stats.stop.tv_sec -= VCL_TEST_DELAY_DISCONNECT;
181           if (conn->cfg.verbose)
182             {
183               snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS", tc->fd);
184               vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
185                                    is_bi /* show tx */ , conn->cfg.verbose);
186             }
187         }
188
189       vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
190                            is_bi /* show_tx */ , conn->cfg.verbose);
191       vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
192       if (conn->cfg.verbose)
193         {
194           vtinf ("  vcl server main\n"
195                  VCL_TEST_SEPARATOR_STRING
196                  "       buf:  %p\n"
197                  "  buf size:  %u (0x%08x)\n"
198                  VCL_TEST_SEPARATOR_STRING,
199                  conn->buf, conn->buf_size, conn->buf_size);
200         }
201
202       sync_config_and_reply (conn, rx_cfg);
203       memset (&conn->stats, 0, sizeof (conn->stats));
204     }
205   else
206     {
207       if (rx_cfg->ctrl_handle == ~0)
208         {
209           rx_cfg->ctrl_handle = conn->fd;
210           vtinf ("Set control fd %d for test!", conn->fd);
211         }
212       else
213         {
214           vtinf ("Starting %s-directional Stream Test (fd %d)!",
215                  is_bi ? "Bi" : "Uni", conn->fd);
216         }
217
218       sync_config_and_reply (conn, rx_cfg);
219
220       /* read the 1st chunk, record start time */
221       memset (&conn->stats, 0, sizeof (conn->stats));
222       clock_gettime (CLOCK_REALTIME, &conn->stats.start);
223     }
224 }
225
226 static inline void
227 vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes)
228 {
229   vcl_test_server_main_t *vsm = &vcl_server_main;
230   int client_fd = conn->fd;
231
232   if (conn->cfg.test == VCL_TEST_TYPE_BI)
233     {
234       if (vsm->use_ds)
235         {
236           (void) vcl_test_write (client_fd, conn->ds[0].data, conn->ds[0].len,
237                                  &conn->stats, conn->cfg.verbose);
238           if (conn->ds[1].len)
239             (void) vcl_test_write (client_fd, conn->ds[1].data,
240                                    conn->ds[1].len, &conn->stats,
241                                    conn->cfg.verbose);
242         }
243       else
244         (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
245                                conn->cfg.verbose);
246     }
247
248   if (vsm->use_ds)
249     vppcom_session_free_segments (conn->fd, rx_bytes);
250
251   if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
252     clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
253 }
254
255 static void
256 vts_copy_ds (void *buf, vppcom_data_segment_t * ds, u32 max_bytes)
257 {
258   uint32_t n_bytes = 0, ds_idx = 0;
259
260   while (n_bytes < max_bytes)
261     {
262       clib_memcpy_fast (buf + n_bytes, ds[ds_idx].data,
263                         clib_min (ds[ds_idx].len, max_bytes - n_bytes));
264       ds_idx += 1;
265     }
266 }
267
268 static void
269 vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes)
270 {
271   vcl_test_server_main_t *vsm = &vcl_server_main;
272   int tx_bytes, nbytes, pos;
273
274   if (vsm->use_ds)
275     vts_copy_ds (conn->buf, conn->ds, rx_bytes);
276
277   /* If it looks vaguely like a string, make sure it's terminated */
278   pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
279   ((char *) conn->buf)[pos] = 0;
280   vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf);
281
282   if (conn->cfg.verbose)
283     vtinf ("(fd %d): Echoing back", conn->fd);
284
285   nbytes = strlen ((const char *) conn->buf) + 1;
286   tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
287                              conn->cfg.verbose);
288   if (tx_bytes >= 0)
289     vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf);
290 }
291
292 static void
293 vts_new_client (vcl_test_server_worker_t * wrk, int listen_fd)
294 {
295   vcl_test_server_conn_t *conn;
296   struct epoll_event ev;
297   int rv, client_fd;
298
299   conn = conn_pool_alloc (wrk);
300   if (!conn)
301     {
302       vtwrn ("No free connections!");
303       return;
304     }
305
306   client_fd = vppcom_session_accept (listen_fd, &conn->endpt, 0);
307   if (client_fd < 0)
308     {
309       vterr ("vppcom_session_accept()", client_fd);
310       return;
311     }
312   conn->fd = client_fd;
313
314   vtinf ("Got a connection -- fd = %d (0x%08x) on listener fd = %d (0x%08x)",
315          client_fd, client_fd, listen_fd, listen_fd);
316
317   ev.events = EPOLLIN;
318   ev.data.u64 = conn - wrk->conn_pool;
319   rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev);
320   if (rv < 0)
321     {
322       vterr ("vppcom_epoll_ctl()", rv);
323       return;
324     }
325   wrk->nfds++;
326 }
327
328 static void
329 print_usage_and_exit (void)
330 {
331   fprintf (stderr,
332            "vcl_test_server [OPTIONS] <port>\n"
333            "  OPTIONS\n"
334            "  -h               Print this message and exit.\n"
335            "  -6               Use IPv6\n"
336            "  -w <num>         Number of workers\n"
337            "  -p <PROTO>       Use <PROTO> transport layer\n"
338            "  -D               Use UDP transport layer\n"
339            "  -L               Use TLS transport layer\n");
340   exit (1);
341 }
342
343 static void
344 vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm)
345 {
346   struct sockaddr_storage *servaddr = &vsm->servaddr;
347   memset (servaddr, 0, sizeof (*servaddr));
348
349   if (vsm->cfg.address_ip6)
350     {
351       struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
352       server_addr->sin6_family = AF_INET6;
353       server_addr->sin6_addr = in6addr_any;
354       server_addr->sin6_port = htons (vsm->cfg.port);
355     }
356   else
357     {
358       struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
359       server_addr->sin_family = AF_INET;
360       server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
361       server_addr->sin_port = htons (vsm->cfg.port);
362     }
363
364   if (vsm->cfg.address_ip6)
365     {
366       struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
367       vsm->cfg.endpt.is_ip4 = 0;
368       vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
369       vsm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
370     }
371   else
372     {
373       struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
374       vsm->cfg.endpt.is_ip4 = 1;
375       vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
376       vsm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
377     }
378 }
379
380 static void
381 vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc,
382                               char **argv)
383 {
384   int v, c;
385
386   vsm->cfg.proto = VPPCOM_PROTO_TCP;
387
388   opterr = 0;
389   while ((c = getopt (argc, argv, "6DLsw:p:")) != -1)
390     switch (c)
391       {
392       case '6':
393         vsm->cfg.address_ip6 = 1;
394         break;
395
396       case 'p':
397         if (vppcom_unformat_proto (&vsm->cfg.proto, optarg))
398           vtwrn ("Invalid vppcom protocol %s, defaulting to TCP", optarg);
399         break;
400
401       case 'D':
402         vsm->cfg.proto = VPPCOM_PROTO_UDP;
403         break;
404
405       case 'L':
406         vsm->cfg.proto = VPPCOM_PROTO_TLS;
407         break;
408
409       case 'w':
410         v = atoi (optarg);
411         if (v > 1)
412           vsm->cfg.workers = v;
413         else
414           vtwrn ("Invalid number of workers %d", v);
415         break;
416       case 's':
417         vsm->use_ds = 1;
418         break;
419       case '?':
420         switch (optopt)
421           {
422           case 'w':
423           case 'p':
424             vtwrn ("Option `-%c' requires an argument.", optopt);
425             break;
426           default:
427             if (isprint (optopt))
428               vtwrn ("Unknown option `-%c'.", optopt);
429             else
430               vtwrn ("Unknown option character `\\x%x'.", optopt);
431           }
432         /* fall thru */
433       case 'h':
434       default:
435         print_usage_and_exit ();
436       }
437
438   if (argc < (optind + 1))
439     {
440       fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
441       print_usage_and_exit ();
442     }
443
444   if (sscanf (argv[optind], "%d", &v) == 1)
445     vsm->cfg.port = (uint16_t) v;
446   else
447     {
448       fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
449       print_usage_and_exit ();
450     }
451
452   vcl_test_init_endpoint_addr (vsm);
453 }
454
455 static void
456 vts_clean_connected_listeners (vcl_test_server_worker_t * wrk,
457                                int listener_fd)
458 {
459   if ((vppcom_session_n_accepted (listener_fd) == 0) &
460       vppcom_session_is_connectable_listener (listener_fd))
461     {
462       vtinf ("Connected Listener fd %x has no more sessions", listener_fd);
463       vppcom_session_close (listener_fd);
464       wrk->nfds--;
465     }
466 }
467
468 int
469 vts_handle_cfg (vcl_test_server_worker_t * wrk, vcl_test_cfg_t * rx_cfg,
470                 vcl_test_server_conn_t * conn, int rx_bytes)
471 {
472   int listener_fd;
473   if (rx_cfg->verbose)
474     {
475       vtinf ("(fd %d): Received a cfg msg!", conn->fd);
476       vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
477     }
478
479   if (rx_bytes != sizeof (*rx_cfg))
480     {
481       vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
482              rx_bytes, sizeof (*rx_cfg));
483       conn->cfg.rxbuf_size = 0;
484       conn->cfg.num_writes = 0;
485       if (conn->cfg.verbose)
486         {
487           vtinf ("(fd %d): Replying to cfg msg", conn->fd);
488           vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
489         }
490       vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
491                       sizeof (conn->cfg), NULL, conn->cfg.verbose);
492       return -1;
493     }
494
495   switch (rx_cfg->test)
496     {
497     case VCL_TEST_TYPE_NONE:
498     case VCL_TEST_TYPE_ECHO:
499       sync_config_and_reply (conn, rx_cfg);
500       break;
501
502     case VCL_TEST_TYPE_BI:
503     case VCL_TEST_TYPE_UNI:
504       vts_server_start_stop (wrk, conn, rx_cfg);
505       break;
506
507     case VCL_TEST_TYPE_EXIT:
508       vtinf ("Session fd %d closing!", conn->fd);
509       clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
510       listener_fd = vppcom_session_listener (conn->fd);
511       vppcom_session_close (conn->fd);
512       conn_pool_free (conn);
513       wrk->nfds--;
514       vts_clean_connected_listeners (wrk, listener_fd);
515       break;
516
517     default:
518       vtwrn ("Unknown test type %d", rx_cfg->test);
519       vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
520       break;
521     }
522
523   return 0;
524 }
525
526 static void
527 vts_worker_init (vcl_test_server_worker_t * wrk)
528 {
529   vcl_test_server_main_t *vsm = &vcl_server_main;
530   struct epoll_event listen_ev;
531   int rv;
532
533   __wrk_index = wrk->wrk_index;
534
535   vtinf ("Initializing worker ...");
536
537   conn_pool_expand (wrk, VCL_TEST_CFG_MAX_TEST_SESS + 1);
538   if (wrk->wrk_index)
539     if (vppcom_worker_register ())
540       vtfail ("vppcom_worker_register()", 1);
541
542   wrk->listen_fd = vppcom_session_create (vsm->cfg.proto,
543                                           0 /* is_nonblocking */ );
544   if (wrk->listen_fd < 0)
545     vtfail ("vppcom_session_create()", wrk->listen_fd);
546
547   if (vsm->cfg.proto == VPPCOM_PROTO_UDP)
548     {
549       vppcom_session_attr (wrk->listen_fd, VPPCOM_ATTR_SET_CONNECTED, 0, 0);
550     }
551
552   if (vsm->cfg.proto == VPPCOM_PROTO_TLS
553       || vsm->cfg.proto == VPPCOM_PROTO_QUIC)
554     {
555       vppcom_session_tls_add_cert (wrk->listen_fd, vcl_test_crt_rsa,
556                                    vcl_test_crt_rsa_len);
557       vppcom_session_tls_add_key (wrk->listen_fd, vcl_test_key_rsa,
558                                   vcl_test_key_rsa_len);
559     }
560
561   rv = vppcom_session_bind (wrk->listen_fd, &vsm->cfg.endpt);
562   if (rv < 0)
563     vtfail ("vppcom_session_bind()", rv);
564
565   if (!(vsm->cfg.proto == VPPCOM_PROTO_UDP))
566     {
567       rv = vppcom_session_listen (wrk->listen_fd, 10);
568       if (rv < 0)
569         vtfail ("vppcom_session_listen()", rv);
570     }
571
572   wrk->epfd = vppcom_epoll_create ();
573   if (wrk->epfd < 0)
574     vtfail ("vppcom_epoll_create()", wrk->epfd);
575
576   listen_ev.events = EPOLLIN;
577   listen_ev.data.u32 = ~0;
578   rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd,
579                          &listen_ev);
580   if (rv < 0)
581     vtfail ("vppcom_epoll_ctl", rv);
582
583   vsm->active_workers += 1;
584   vtinf ("Waiting for a client to connect on port %d ...", vsm->cfg.port);
585 }
586
587 static int
588 vts_conn_expect_config (vcl_test_server_conn_t * conn)
589 {
590   if (conn->cfg.test == VCL_TEST_TYPE_ECHO)
591     return 1;
592
593   return (conn->stats.rx_bytes < 128
594           || conn->stats.rx_bytes > conn->cfg.total_bytes);
595 }
596
597 static vcl_test_cfg_t *
598 vts_conn_read_config (vcl_test_server_conn_t * conn)
599 {
600   vcl_test_server_main_t *vsm = &vcl_server_main;
601
602   if (vsm->use_ds)
603     {
604       /* We could avoid the copy if the first segment is big enough but this
605        * just simplifies things */
606       vts_copy_ds (conn->buf, conn->ds, sizeof (vcl_test_cfg_t));
607     }
608   return (vcl_test_cfg_t *) conn->buf;
609 }
610
611 static inline int
612 vts_conn_read (vcl_test_server_conn_t * conn)
613 {
614   vcl_test_server_main_t *vsm = &vcl_server_main;
615   if (vsm->use_ds)
616     return vcl_test_read_ds (conn->fd, conn->ds, &conn->stats);
617   else
618     return vcl_test_read (conn->fd, conn->buf, conn->buf_size, &conn->stats);
619 }
620
621 static inline int
622 vts_conn_has_ascii (vcl_test_server_conn_t * conn)
623 {
624   vcl_test_server_main_t *vsm = &vcl_server_main;
625
626   if (vsm->use_ds)
627     return isascii (conn->ds[0].data[0]);
628   else
629     return isascii (conn->buf[0]);
630 }
631
632 static void *
633 vts_worker_loop (void *arg)
634 {
635   vcl_test_server_main_t *vsm = &vcl_server_main;
636   vcl_test_server_worker_t *wrk = arg;
637   vcl_test_server_conn_t *conn;
638   int i, rx_bytes, num_ev, listener_fd;
639   vcl_test_cfg_t *rx_cfg;
640
641   if (wrk->wrk_index)
642     vts_worker_init (wrk);
643
644   while (1)
645     {
646       num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
647                                   VCL_TEST_CFG_MAX_EPOLL_EVENTS, 60000.0);
648       if (num_ev < 0)
649         {
650           vterr ("vppcom_epoll_wait()", num_ev);
651           goto fail;
652         }
653       else if (num_ev == 0)
654         {
655           vtinf ("vppcom_epoll_wait() timeout!");
656           continue;
657         }
658       for (i = 0; i < num_ev; i++)
659         {
660           conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
661           if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
662             {
663               vtinf ("Closing session %d on HUP", conn->fd);
664               listener_fd = vppcom_session_listener (conn->fd);
665               vppcom_session_close (conn->fd);
666               wrk->nfds--;
667               vts_clean_connected_listeners (wrk, listener_fd);
668               if (!wrk->nfds)
669                 {
670                   vtinf ("All client connections closed\n");
671                   goto done;
672                 }
673               continue;
674             }
675           if (wrk->wait_events[i].data.u32 == ~0)
676             {
677               vts_new_client (wrk, wrk->listen_fd);
678               continue;
679             }
680           else if (vppcom_session_is_connectable_listener (conn->fd))
681             {
682               vts_new_client (wrk, conn->fd);
683               continue;
684             }
685
686           if (EPOLLIN & wrk->wait_events[i].events)
687             {
688             read_again:
689               rx_bytes = vts_conn_read (conn);
690
691               if (rx_bytes <= 0)
692                 {
693                   if (errno == ECONNRESET)
694                     {
695                       vtinf ("Connection reset by remote peer.\n");
696                       goto fail;
697                     }
698                   else
699                     continue;
700                 }
701
702               if (vts_conn_expect_config (conn))
703                 {
704                   rx_cfg = vts_conn_read_config (conn);
705                   if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
706                     {
707                       if (vsm->use_ds)
708                         vppcom_session_free_segments (conn->fd, rx_bytes);
709                       vts_handle_cfg (wrk, rx_cfg, conn, rx_bytes);
710                       if (!wrk->nfds)
711                         {
712                           vtinf ("All client connections closed\n");
713                           goto done;
714                         }
715                       continue;
716                     }
717                 }
718               if ((conn->cfg.test == VCL_TEST_TYPE_UNI)
719                   || (conn->cfg.test == VCL_TEST_TYPE_BI))
720                 {
721                   vts_server_rx (conn, rx_bytes);
722                   if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0,
723                                            0) > 0)
724                     goto read_again;
725                   continue;
726                 }
727               if (vts_conn_has_ascii (conn))
728                 {
729                   vts_server_echo (conn, rx_bytes);
730                 }
731               else
732                 {
733                   vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
734                 }
735             }
736           else
737             {
738               vtwrn ("Unhandled event");
739               goto fail;
740             }
741         }
742     }
743
744 fail:
745   vsm->worker_fails -= 1;
746
747 done:
748   vppcom_session_close (wrk->listen_fd);
749   if (wrk->conn_pool)
750     free (wrk->conn_pool);
751   vsm->active_workers -= 1;
752   return 0;
753 }
754
755 int
756 main (int argc, char **argv)
757 {
758   vcl_test_server_main_t *vsm = &vcl_server_main;
759   int rv, i;
760
761   clib_mem_init_thread_safe (0, 64 << 20);
762   vsm->cfg.port = VCL_TEST_SERVER_PORT;
763   vsm->cfg.workers = 1;
764   vsm->active_workers = 0;
765   vcl_test_server_process_opts (vsm, argc, argv);
766
767   rv = vppcom_app_create ("vcl_test_server");
768   if (rv)
769     vtfail ("vppcom_app_create()", rv);
770
771   vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers));
772   vts_worker_init (&vsm->workers[0]);
773   for (i = 1; i < vsm->cfg.workers; i++)
774     {
775       vsm->workers[i].wrk_index = i;
776       rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
777                            vts_worker_loop, (void *) &vsm->workers[i]);
778     }
779   vts_worker_loop (&vsm->workers[0]);
780
781   while (vsm->active_workers > 0)
782     ;
783
784   vppcom_app_destroy ();
785   free (vsm->workers);
786
787   return vsm->worker_fails;
788 }
789
790 /*
791  * fd.io coding-style-patch-verification: ON
792  *
793  * Local Variables:
794  * eval: (c-set-style "gnu")
795  * End:
796  */