hsa: unify echo test setup
[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 #include <sys/epoll.h>
29 #include <sys/un.h>
30
31 #define SOCK_SERVER_MAX_TEST_CONN  10
32 #define SOCK_SERVER_MAX_EPOLL_EVENTS 10
33
34 typedef struct
35 {
36   uint8_t is_alloc;
37   int fd;
38   uint8_t *buf;
39   uint32_t buf_size;
40   hs_test_cfg_t cfg;
41   vcl_test_stats_t stats;
42 } sock_server_conn_t;
43
44 typedef struct
45 {
46   uint32_t port;
47   uint32_t address_ip6;
48   uint32_t transport_udp;
49 } sock_server_cfg_t;
50
51 typedef struct
52 {
53   int listen_fd;
54   sock_server_cfg_t cfg;
55   int epfd;
56   struct epoll_event listen_ev;
57   struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
58   int af_unix_listen_fd;
59   int af_unix_fd;
60   struct epoll_event af_unix_listen_ev;
61   struct sockaddr_un serveraddr;
62   uint32_t af_unix_xacts;
63   size_t num_conn;
64   size_t conn_pool_size;
65   sock_server_conn_t *conn_pool;
66   int nfds;
67   fd_set rd_fdset;
68   fd_set wr_fdset;
69   struct timeval timeout;
70 } sock_server_main_t;
71
72 sock_server_main_t sock_server_main;
73
74 static inline void
75 conn_pool_expand (size_t expand_size)
76 {
77   sock_server_main_t *ssm = &sock_server_main;
78   sock_server_conn_t *conn_pool;
79   size_t new_size = ssm->conn_pool_size + expand_size;
80   int i;
81
82   conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
83   if (!conn_pool)
84     stfail ("conn_pool_expand()");
85
86   for (i = ssm->conn_pool_size; i < new_size; i++)
87     {
88       sock_server_conn_t *conn = &conn_pool[i];
89       memset (conn, 0, sizeof (*conn));
90       hs_test_cfg_init (&conn->cfg);
91       vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , &conn->buf,
92                           &conn->buf_size);
93       conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
94     }
95
96   ssm->conn_pool = conn_pool;
97   ssm->conn_pool_size = new_size;
98 }
99
100 static inline sock_server_conn_t *
101 conn_pool_alloc (void)
102 {
103   sock_server_main_t *ssm = &sock_server_main;
104   int i;
105
106   for (i = 0; i < ssm->conn_pool_size; i++)
107     {
108       if (!ssm->conn_pool[i].is_alloc)
109         {
110           ssm->conn_pool[i].is_alloc = 1;
111           return (&ssm->conn_pool[i]);
112         }
113     }
114
115   return 0;
116 }
117
118 static inline void
119 conn_pool_free (sock_server_conn_t * conn)
120 {
121   conn->fd = 0;
122   conn->is_alloc = 0;
123 }
124
125 static inline void
126 sync_config_and_reply (sock_server_conn_t *conn, hs_test_cfg_t *rx_cfg)
127 {
128   conn->cfg = *rx_cfg;
129   vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
130                       &conn->buf, &conn->buf_size);
131   conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
132
133   if (conn->cfg.verbose)
134     {
135       stinf ("(fd %d): Replying to cfg message!\n", conn->fd);
136       hs_test_cfg_dump (&conn->cfg, 0 /* is_client */);
137     }
138   (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
139                           sizeof (conn->cfg), NULL, conn->cfg.verbose);
140 }
141
142 static void
143 stream_test_server_start_stop (sock_server_conn_t *conn, hs_test_cfg_t *rx_cfg)
144 {
145   sock_server_main_t *ssm = &sock_server_main;
146   int client_fd = conn->fd;
147   hs_test_t test = rx_cfg->test;
148
149   if (rx_cfg->ctrl_handle == conn->fd)
150     {
151       int i;
152       clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
153
154       for (i = 0; i < ssm->conn_pool_size; i++)
155         {
156           sock_server_conn_t *tc = &ssm->conn_pool[i];
157
158           if (tc->cfg.ctrl_handle == conn->fd)
159             {
160               vcl_test_stats_accumulate (&conn->stats, &tc->stats);
161
162               if (conn->cfg.verbose)
163                 {
164                   static char buf[64];
165
166                   snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS",
167                             tc->fd);
168                   vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */,
169                                        test == HS_TEST_TYPE_BI
170                                        /* show tx */,
171                                        conn->cfg.verbose);
172                 }
173             }
174         }
175
176       vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */,
177                            (test == HS_TEST_TYPE_BI) /* show_tx */,
178                            conn->cfg.verbose);
179       hs_test_cfg_dump (&conn->cfg, 0 /* is_client */);
180       if (conn->cfg.verbose)
181         {
182           stinf ("  sock server main\n" HS_TEST_SEPARATOR_STRING
183                  "       buf:  %p\n"
184                  "  buf size:  %u (0x%08x)\n" HS_TEST_SEPARATOR_STRING,
185                  conn->buf, conn->buf_size, conn->buf_size);
186         }
187
188       sync_config_and_reply (conn, rx_cfg);
189       stinf ("SERVER (fd %d): %s-directional Stream Test "
190              "Complete!\n" SOCK_TEST_BANNER_STRING "\n",
191              conn->fd, test == HS_TEST_TYPE_BI ? "Bi" : "Uni");
192     }
193   else
194     {
195       stinf (SOCK_TEST_BANNER_STRING
196              "SERVER (fd %d): %s-directional Stream Test!\n"
197              "  Sending client the test cfg to start streaming data...\n",
198              client_fd, test == HS_TEST_TYPE_BI ? "Bi" : "Uni");
199
200       rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
201         rx_cfg->ctrl_handle;
202
203       sync_config_and_reply (conn, rx_cfg);
204
205       /* read the 1st chunk, record start time */
206       memset (&conn->stats, 0, sizeof (conn->stats));
207       clock_gettime (CLOCK_REALTIME, &conn->stats.start);
208     }
209 }
210
211
212 static inline void
213 stream_test_server (sock_server_conn_t * conn, int rx_bytes)
214 {
215   int client_fd = conn->fd;
216   hs_test_t test = conn->cfg.test;
217
218   if (test == HS_TEST_TYPE_BI)
219     (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
220                             conn->cfg.verbose);
221
222   if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
223     {
224       clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
225     }
226 }
227
228 static inline void
229 af_unix_echo (void)
230 {
231   sock_server_main_t *ssm = &sock_server_main;
232   int af_unix_client_fd, rv;
233   uint8_t buffer[256];
234   size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
235
236   af_unix_client_fd = accept (ssm->af_unix_listen_fd,
237                               (struct sockaddr *) NULL, NULL);
238   if (af_unix_client_fd < 0)
239     stfail ("af_unix_echo accept()");
240
241   stinf ("Got an AF_UNIX connection -- fd = %d (0x%08x)!",
242          af_unix_client_fd, af_unix_client_fd);
243
244   memset (buffer, 0, sizeof (buffer));
245
246   rv = read (af_unix_client_fd, buffer, nbytes);
247   if (rv < 0)
248     stfail ("af_unix_echo read()");
249
250   /* Make the buffer is NULL-terminated. */
251   buffer[sizeof (buffer) - 1] = 0;
252   stinf ("(AF_UNIX): RX (%d bytes) - '%s'", rv, buffer);
253
254   if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
255     {
256       rv = write (af_unix_client_fd, buffer, nbytes);
257       if (rv < 0)
258         stfail ("af_unix_echo write()");
259       stinf ("(AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
260       ssm->af_unix_xacts++;
261     }
262   close (af_unix_client_fd);
263 }
264
265 static inline void
266 new_client (void)
267 {
268   sock_server_main_t *ssm = &sock_server_main;
269   int client_fd;
270   sock_server_conn_t *conn;
271
272   if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
273     conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
274
275   conn = conn_pool_alloc ();
276   if (!conn)
277     stfail ("No free connections!");
278
279   client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
280   if (client_fd < 0)
281     stfail ("new_client accept()");
282
283   stinf ("Got a connection -- fd = %d (0x%08x)!\n", client_fd, client_fd);
284   if (fcntl (client_fd, F_SETFL, O_NONBLOCK) < 0)
285     stfail ("fcntl()");
286
287   conn->fd = client_fd;
288
289   struct epoll_event ev;
290   int rv;
291
292   ev.events = EPOLLET | EPOLLIN;
293   ev.data.u64 = conn - ssm->conn_pool;
294   rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
295
296   if (rv < 0)
297     stfail ("new_client epoll_ctl()");
298
299   ssm->nfds++;
300 }
301
302 static int
303 socket_server_echo_af_unix_init (sock_server_main_t * ssm)
304 {
305   int rv;
306
307   if (ssm->af_unix_listen_fd > 0)
308     return 0;
309
310   unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
311   ssm->af_unix_listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
312   if (ssm->af_unix_listen_fd < 0)
313     stfail ("echo_af_unix_init socket()");
314
315   memset (&ssm->serveraddr, 0, sizeof (ssm->serveraddr));
316   ssm->serveraddr.sun_family = AF_UNIX;
317   strncpy (ssm->serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME,
318            sizeof (ssm->serveraddr.sun_path));
319
320   rv = bind (ssm->af_unix_listen_fd, (struct sockaddr *) &ssm->serveraddr,
321              SUN_LEN (&ssm->serveraddr));
322   if (rv < 0)
323     stfail ("echo_af_unix_init bind()");
324
325   rv = listen (ssm->af_unix_listen_fd, 10);
326   if (rv < 0)
327     stfail ("echo_af_unix_init listen()");
328
329   ssm->af_unix_listen_ev.events = EPOLLET | EPOLLIN;
330   ssm->af_unix_listen_ev.data.u32 = SOCK_TEST_AF_UNIX_ACCEPT_DATA;
331   rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->af_unix_listen_fd,
332                   &ssm->af_unix_listen_ev);
333   if (rv < 0)
334     stfail ("echo_af_unix_init epoll_ctl()");
335
336   return 0;
337 }
338
339 void
340 print_usage_and_exit (void)
341 {
342   fprintf (stderr,
343            "sock_test_server [OPTIONS] <port>\n"
344            "  OPTIONS\n"
345            "  -h               Print this message and exit.\n"
346            "  -6               Use IPv6\n"
347            "  -u               Use UDP transport layer\n");
348   exit (1);
349 }
350
351
352 static void
353 sts_server_echo (sock_server_conn_t * conn, int rx_bytes)
354 {
355   int tx_bytes, nbytes, pos;
356
357   /* If it looks vaguely like a string make sure it's terminated */
358   pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
359   ((char *) conn->buf)[pos] = 0;
360
361   if (conn->cfg.verbose)
362     stinf ("(fd %d): Echoing back\n", conn->fd);
363
364   nbytes = strlen ((const char *) conn->buf) + 1;
365
366   tx_bytes = sock_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
367                               conn->cfg.verbose);
368   if (tx_bytes >= 0)
369     stinf ("(fd %d): TX (%d bytes) - '%s'\n", conn->fd, tx_bytes, conn->buf);
370 }
371
372 static int
373 sts_handle_cfg (hs_test_cfg_t *rx_cfg, sock_server_conn_t *conn, int rx_bytes)
374 {
375   sock_server_main_t *ssm = &sock_server_main;
376
377   if (rx_cfg->verbose)
378     {
379       stinf ("(fd %d): Received a cfg message!\n", conn->fd);
380       hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
381     }
382
383   if (rx_bytes != sizeof (*rx_cfg))
384     {
385       stinf ("(fd %d): Invalid cfg message size (%d) expected %lu!", conn->fd,
386              rx_bytes, sizeof (*rx_cfg));
387       conn->cfg.rxbuf_size = 0;
388       conn->cfg.num_writes = 0;
389       if (conn->cfg.verbose)
390         {
391           stinf ("(fd %d): Replying to cfg message!\n", conn->fd);
392           hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
393         }
394       sock_test_write (conn->fd, (uint8_t *) & conn->cfg, sizeof (conn->cfg),
395                        NULL, conn->cfg.verbose);
396       return -1;
397     }
398
399   switch (rx_cfg->test)
400     {
401     case HS_TEST_TYPE_NONE:
402       sync_config_and_reply (conn, rx_cfg);
403       break;
404
405     case HS_TEST_TYPE_ECHO:
406       if (socket_server_echo_af_unix_init (ssm))
407         goto done;
408
409       sync_config_and_reply (conn, rx_cfg);
410       break;
411
412     case HS_TEST_TYPE_BI:
413     case HS_TEST_TYPE_UNI:
414       stream_test_server_start_stop (conn, rx_cfg);
415       break;
416
417     case HS_TEST_TYPE_EXIT:
418       stinf ("Have a great day connection %d!", conn->fd);
419       close (conn->fd);
420       conn_pool_free (conn);
421       stinf ("Closed client fd %d", conn->fd);
422       ssm->nfds--;
423       break;
424
425     default:
426       stinf ("ERROR: Unknown test type!\n");
427       hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
428       break;
429     }
430
431 done:
432   return 0;
433 }
434
435 static int
436 sts_conn_expect_config (sock_server_conn_t * conn)
437 {
438   if (conn->cfg.test == HS_TEST_TYPE_ECHO)
439     return 1;
440
441   return (conn->stats.rx_bytes < 128
442           || conn->stats.rx_bytes > conn->cfg.total_bytes);
443 }
444
445 int
446 main (int argc, char **argv)
447 {
448   int client_fd, rv, main_rv = 0, rx_bytes, c, v, i;
449   sock_server_main_t *ssm = &sock_server_main;
450   sock_server_conn_t *conn;
451   hs_test_cfg_t *rx_cfg;
452   struct sockaddr_storage servaddr;
453   uint16_t port = VCL_TEST_SERVER_PORT;
454   uint32_t servaddr_size;
455
456   opterr = 0;
457   while ((c = getopt (argc, argv, "6D")) != -1)
458     switch (c)
459       {
460       case '6':
461         ssm->cfg.address_ip6 = 1;
462         break;
463
464       case 'D':
465         ssm->cfg.transport_udp = 1;
466         break;
467
468       case '?':
469         switch (optopt)
470           {
471           default:
472             if (isprint (optopt))
473               stinf ("ERROR: Unknown option `-%c'", optopt);
474             else
475               stinf ("ERROR: Unknown option character `\\x%x'.\n", optopt);
476           }
477         /* fall thru */
478       case 'h':
479       default:
480         print_usage_and_exit ();
481       }
482
483   if (argc < (optind + 1))
484     {
485       stinf ("ERROR: Insufficient number of arguments!\n");
486       print_usage_and_exit ();
487     }
488
489   if (sscanf (argv[optind], "%d", &v) == 1)
490     port = (uint16_t) v;
491   else
492     {
493       stinf ("ERROR: Invalid port (%s)!\n", argv[optind]);
494       print_usage_and_exit ();
495     }
496
497   conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
498
499   ssm->listen_fd = socket (ssm->cfg.address_ip6 ? AF_INET6 : AF_INET,
500                            ssm->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM,
501                            0);
502
503   if (ssm->listen_fd < 0)
504     stfail ("main listen()");
505
506   memset (&servaddr, 0, sizeof (servaddr));
507
508   if (ssm->cfg.address_ip6)
509     {
510       struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
511       servaddr_size = sizeof (*server_addr);
512       server_addr->sin6_family = AF_INET6;
513       server_addr->sin6_addr = in6addr_any;
514       server_addr->sin6_port = htons (port);
515     }
516   else
517     {
518       struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
519       servaddr_size = sizeof (*server_addr);
520       server_addr->sin_family = AF_INET;
521       server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
522       server_addr->sin_port = htons (port);
523     }
524
525   rv = bind (ssm->listen_fd, (struct sockaddr *) &servaddr, servaddr_size);
526   if (rv < 0)
527     stfail ("main bind()");
528
529   rv = fcntl (ssm->listen_fd, F_SETFL, O_NONBLOCK);
530   if (rv < 0)
531     stfail ("main fcntl()");
532
533   rv = listen (ssm->listen_fd, 10);
534   if (rv < 0)
535     stfail ("main listen()");
536
537   ssm->epfd = epoll_create (1);
538   if (ssm->epfd < 0)
539     stfail ("main epoll_create()");
540
541   ssm->listen_ev.events = EPOLLET | EPOLLIN;
542   ssm->listen_ev.data.u32 = ~0;
543
544   rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
545   if (rv < 0)
546     stfail ("main epoll_ctl()");
547
548   stinf ("Waiting for a client to connect on port %d...\n", port);
549
550   while (1)
551     {
552       int num_ev;
553       num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
554                            SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
555       if (num_ev < 0)
556         stfail ("main epoll_wait()");
557
558       if (num_ev == 0)
559         {
560           stinf ("epoll_wait() timeout!\n");
561           continue;
562         }
563       for (i = 0; i < num_ev; i++)
564         {
565           conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
566           if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
567             {
568               close (conn->fd);
569               continue;
570             }
571           if (ssm->wait_events[i].data.u32 == ~0)
572             {
573               new_client ();
574               continue;
575             }
576           else if (ssm->wait_events[i].data.u32 ==
577                    SOCK_TEST_AF_UNIX_ACCEPT_DATA)
578             {
579               af_unix_echo ();
580               continue;
581             }
582           client_fd = conn->fd;
583
584           if (EPOLLIN & ssm->wait_events[i].events)
585             {
586             read_again:
587               rx_bytes = sock_test_read (client_fd, conn->buf,
588                                          conn->buf_size, &conn->stats);
589
590               if (rx_bytes <= 0)
591                 {
592                   if (errno == ECONNRESET)
593                     {
594                       stinf ("Connection reset by peer\n");
595                       main_rv = -1;
596                       goto done;
597                     }
598                   else
599                     continue;
600                 }
601
602               if (sts_conn_expect_config (conn))
603                 {
604                   rx_cfg = (hs_test_cfg_t *) conn->buf;
605                   if (rx_cfg->magic == HS_TEST_CFG_CTRL_MAGIC)
606                     {
607                       sts_handle_cfg (rx_cfg, conn, rx_bytes);
608                       if (!ssm->nfds)
609                         {
610                           stinf ("All client connections closed.\n\nSERVER: "
611                                  "May the force be with you!\n\n");
612                           goto done;
613                         }
614                       continue;
615                     }
616                 }
617
618               if ((conn->cfg.test == HS_TEST_TYPE_UNI) ||
619                   (conn->cfg.test == HS_TEST_TYPE_BI))
620                 {
621                   stream_test_server (conn, rx_bytes);
622                   if (ioctl (conn->fd, FIONREAD))
623                     goto read_again;
624                   continue;
625                 }
626               else if (isascii (conn->buf[0]))
627                 {
628                   sts_server_echo (conn, rx_bytes);
629                 }
630               else
631                 {
632                   stwrn ("FIFO not drained! extra bytes %d", rx_bytes);
633                 }
634             }
635         }
636     }
637
638 done:
639   close (ssm->listen_fd);
640   close (ssm->af_unix_listen_fd);
641   unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
642
643   if (ssm->conn_pool)
644     free (ssm->conn_pool);
645
646   return main_rv;
647 }
648
649 /*
650  * fd.io coding-style-patch-verification: ON
651  *
652  * Local Variables:
653  * eval: (c-set-style "gnu")
654  * End:
655  */