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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
24 #include <hs_apps/vcl/sock_test.h>
27 #include <sys/ioctl.h>
28 #include <sys/epoll.h>
31 #define SOCK_SERVER_MAX_TEST_CONN 10
32 #define SOCK_SERVER_MAX_EPOLL_EVENTS 10
41 vcl_test_stats_t stats;
48 uint32_t transport_udp;
54 sock_server_cfg_t cfg;
56 struct epoll_event listen_ev;
57 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
58 int af_unix_listen_fd;
60 struct epoll_event af_unix_listen_ev;
61 struct sockaddr_un serveraddr;
62 uint32_t af_unix_xacts;
64 size_t conn_pool_size;
65 sock_server_conn_t *conn_pool;
69 struct timeval timeout;
72 sock_server_main_t sock_server_main;
75 conn_pool_expand (size_t expand_size)
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;
82 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
85 for (i = ssm->conn_pool_size; i < new_size; i++)
87 sock_server_conn_t *conn = &conn_pool[i];
88 memset (conn, 0, sizeof (*conn));
89 vcl_test_cfg_init (&conn->cfg);
90 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
91 &conn->buf, &conn->buf_size);
92 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
95 ssm->conn_pool = conn_pool;
96 ssm->conn_pool_size = new_size;
100 int errno_val = errno;
101 perror ("ERROR in conn_pool_expand()");
102 fprintf (stderr, "SERVER: ERROR: Memory allocation "
103 "failed (errno = %d)!\n", errno_val);
107 static inline sock_server_conn_t *
108 conn_pool_alloc (void)
110 sock_server_main_t *ssm = &sock_server_main;
113 for (i = 0; i < ssm->conn_pool_size; i++)
115 if (!ssm->conn_pool[i].is_alloc)
117 ssm->conn_pool[i].is_alloc = 1;
118 return (&ssm->conn_pool[i]);
126 conn_pool_free (sock_server_conn_t * conn)
133 sync_config_and_reply (sock_server_conn_t * conn, vcl_test_cfg_t * rx_cfg)
136 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
137 &conn->buf, &conn->buf_size);
138 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
140 if (conn->cfg.verbose)
142 printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
143 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
145 (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
146 sizeof (conn->cfg), NULL, conn->cfg.verbose);
150 stream_test_server_start_stop (sock_server_conn_t * conn,
151 vcl_test_cfg_t * rx_cfg)
153 sock_server_main_t *ssm = &sock_server_main;
154 int client_fd = conn->fd;
155 vcl_test_t test = rx_cfg->test;
157 if (rx_cfg->ctrl_handle == conn->fd)
160 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
162 for (i = 0; i < ssm->conn_pool_size; i++)
164 sock_server_conn_t *tc = &ssm->conn_pool[i];
166 if (tc->cfg.ctrl_handle == conn->fd)
168 vcl_test_stats_accumulate (&conn->stats, &tc->stats);
170 if (conn->cfg.verbose)
174 snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS",
176 vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
177 test == VCL_TEST_TYPE_BI
184 vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
185 (test == VCL_TEST_TYPE_BI) /* show_tx */ ,
187 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
188 if (conn->cfg.verbose)
190 printf (" sock server main\n"
191 VCL_TEST_SEPARATOR_STRING
193 " buf size: %u (0x%08x)\n"
194 VCL_TEST_SEPARATOR_STRING,
195 conn->buf, conn->buf_size, conn->buf_size);
198 sync_config_and_reply (conn, rx_cfg);
199 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
200 SOCK_TEST_BANNER_STRING "\n", conn->fd,
201 test == VCL_TEST_TYPE_BI ? "Bi" : "Uni");
205 printf ("\n" SOCK_TEST_BANNER_STRING
206 "SERVER (fd %d): %s-directional Stream Test!\n"
207 " Sending client the test cfg to start streaming data...\n",
208 client_fd, test == VCL_TEST_TYPE_BI ? "Bi" : "Uni");
210 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
213 sync_config_and_reply (conn, rx_cfg);
215 /* read the 1st chunk, record start time */
216 memset (&conn->stats, 0, sizeof (conn->stats));
217 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
223 stream_test_server (sock_server_conn_t * conn, int rx_bytes)
225 int client_fd = conn->fd;
226 vcl_test_t test = conn->cfg.test;
228 if (test == VCL_TEST_TYPE_BI)
229 (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
232 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
234 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
241 sock_server_main_t *ssm = &sock_server_main;
242 int af_unix_client_fd;
246 size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1;
249 af_unix_client_fd = accept4 (ssm->af_unix_listen_fd,
250 (struct sockaddr *) NULL, NULL, NULL);
252 af_unix_client_fd = accept (ssm->af_unix_listen_fd,
253 (struct sockaddr *) NULL, NULL);
255 if (af_unix_client_fd < 0)
258 perror ("ERROR in af_unix_accept()");
259 fprintf (stderr, "SERVER: ERROR: accept failed "
260 "(errno = %d)!\n", errno_val);
264 printf ("SERVER: Got an AF_UNIX connection -- fd = %d (0x%08x)!\n",
265 af_unix_client_fd, af_unix_client_fd);
267 memset (buffer, 0, sizeof (buffer));
269 rv = read (af_unix_client_fd, buffer, nbytes);
273 perror ("ERROR in af_unix_echo(): read() failed");
274 fprintf (stderr, "SERVER: ERROR: read(af_unix_client_fd %d (0x%x), "
275 "nbytes %lu) failed (errno = %d)!\n", af_unix_client_fd,
276 af_unix_client_fd, nbytes, errno_val);
279 /* Make the buffer is NULL-terminated. */
280 buffer[sizeof (buffer) - 1] = 0;
281 printf ("SERVER (AF_UNIX): RX (%d bytes) - '%s'\n", rv, buffer);
283 if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes))
285 rv = write (af_unix_client_fd, buffer, nbytes);
289 perror ("ERROR in af_unix_echo(): write() failed");
291 "SERVER: ERROR: write(af_unix_client_fd %d (0x%x), "
292 "\"%s\", nbytes %ld) failed (errno = %d)!\n",
293 af_unix_client_fd, af_unix_client_fd, buffer, nbytes,
297 printf ("SERVER (AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer);
298 ssm->af_unix_xacts++;
301 close (af_unix_client_fd);
307 sock_server_main_t *ssm = &sock_server_main;
309 sock_server_conn_t *conn;
311 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
312 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
314 conn = conn_pool_alloc ();
317 fprintf (stderr, "\nSERVER: ERROR: No free connections!\n");
322 client_fd = accept4 (ssm->listen_fd, (struct sockaddr *) NULL, NULL, NULL);
324 client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
330 perror ("ERROR in new_client()");
331 fprintf (stderr, "SERVER: ERROR: accept failed "
332 "(errno = %d)!\n", errno_val);
336 printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
337 client_fd, client_fd);
339 conn->fd = client_fd;
341 struct epoll_event ev;
345 ev.data.u64 = conn - ssm->conn_pool;
346 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
352 perror ("ERROR in new_client()");
353 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed (errno = %d)!\n",
361 print_usage_and_exit (void)
364 "sock_test_server [OPTIONS] <port>\n"
366 " -h Print this message and exit.\n"
368 " -u Use UDP transport layer\n");
373 main (int argc, char **argv)
375 sock_server_main_t *ssm = &sock_server_main;
376 int client_fd, rv, main_rv = 0;
377 int tx_bytes, rx_bytes, nbytes;
378 sock_server_conn_t *conn;
379 vcl_test_cfg_t *rx_cfg;
381 uint64_t xtra_bytes = 0;
382 struct sockaddr_storage servaddr;
385 uint16_t port = VCL_TEST_SERVER_PORT;
386 uint32_t servaddr_size;
389 while ((c = getopt (argc, argv, "6D")) != -1)
393 ssm->cfg.address_ip6 = 1;
397 ssm->cfg.transport_udp = 1;
404 if (isprint (optopt))
405 fprintf (stderr, "SERVER: ERROR: Unknown "
406 "option `-%c'.\n", optopt);
408 fprintf (stderr, "SERVER: ERROR: Unknown "
409 "option character `\\x%x'.\n", optopt);
414 print_usage_and_exit ();
417 if (argc < (optind + 1))
419 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
420 print_usage_and_exit ();
423 if (sscanf (argv[optind], "%d", &v) == 1)
427 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
428 print_usage_and_exit ();
431 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
433 ssm->listen_fd = socket (ssm->cfg.address_ip6 ? AF_INET6 : AF_INET,
434 ssm->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM,
436 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
437 ssm->af_unix_listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
438 if (ssm->af_unix_listen_fd < 0)
441 perror ("ERROR in main(): socket(AF_UNIX) failed");
443 "SERVER: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed "
444 "(errno = %d)!\n", errno_val);
445 return ssm->af_unix_listen_fd;
448 memset (&ssm->serveraddr, 0, sizeof (ssm->serveraddr));
449 ssm->serveraddr.sun_family = AF_UNIX;
450 strncpy (ssm->serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME,
451 sizeof (ssm->serveraddr.sun_path));
453 rv = bind (ssm->af_unix_listen_fd, (struct sockaddr *) &ssm->serveraddr,
454 SUN_LEN (&ssm->serveraddr));
458 perror ("ERROR in main(): bind(SOCK_TEST_AF_UNIX_FILENAME) failed");
459 fprintf (stderr, "SERVER: ERROR: bind() fd %d, \"%s\": "
460 "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
461 SOCK_TEST_AF_UNIX_FILENAME, errno_val);
462 close (ssm->af_unix_listen_fd);
463 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
467 rv = listen (ssm->af_unix_listen_fd, 10);
471 perror ("ERROR in main(): listen(AF_UNIX) failed");
472 fprintf (stderr, "SERVER: ERROR: listen() fd %d, \"%s\": "
473 "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
474 SOCK_TEST_AF_UNIX_FILENAME, errno_val);
475 close (ssm->af_unix_listen_fd);
476 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
480 if (ssm->listen_fd < 0)
483 perror ("ERROR in main()");
484 fprintf (stderr, "SERVER: ERROR: socket() failed "
485 "(errno = %d)!\n", errno_val);
486 return ssm->listen_fd;
489 memset (&servaddr, 0, sizeof (servaddr));
491 if (ssm->cfg.address_ip6)
493 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
494 servaddr_size = sizeof (*server_addr);
495 server_addr->sin6_family = AF_INET6;
496 server_addr->sin6_addr = in6addr_any;
497 server_addr->sin6_port = htons (port);
501 struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
502 servaddr_size = sizeof (*server_addr);
503 server_addr->sin_family = AF_INET;
504 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
505 server_addr->sin_port = htons (port);
508 rv = bind (ssm->listen_fd, (struct sockaddr *) &servaddr, servaddr_size);
512 perror ("ERROR bind returned");
513 fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n",
517 if (fcntl (ssm->listen_fd, F_SETFL, O_NONBLOCK) < 0)
520 perror ("ERROR fcntl returned");
521 fprintf (stderr, "SERVER: ERROR: fcntl failed (errno = %d)!\n",
526 rv = listen (ssm->listen_fd, 10);
530 perror ("ERROR in main()");
531 fprintf (stderr, "SERVER: ERROR: listen failed "
532 "(errno = %d)!\n", errno_val);
536 ssm->epfd = epoll_create (1);
540 perror ("ERROR in main()");
541 fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n",
546 ssm->listen_ev.events = EPOLLIN;
547 ssm->listen_ev.data.u32 = ~0;
548 ssm->af_unix_listen_ev.events = EPOLLIN;
549 ssm->af_unix_listen_ev.data.u32 = SOCK_TEST_AF_UNIX_ACCEPT_DATA;
550 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->af_unix_listen_fd,
551 &ssm->af_unix_listen_ev);
555 perror ("ERROR in main(): mixed epoll_ctl(EPOLL_CTL_ADD)");
556 fprintf (stderr, "SERVER: ERROR: mixed epoll_ctl(epfd %d (0x%x), "
557 "EPOLL_CTL_ADD, af_unix_listen_fd %d (0x%x), EPOLLIN) failed "
558 "(errno = %d)!\n", ssm->epfd, ssm->epfd,
559 ssm->af_unix_listen_fd, ssm->af_unix_listen_fd, errno_val);
560 close (ssm->af_unix_listen_fd);
561 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
565 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
569 perror ("ERROR in main()");
570 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed "
571 "(errno = %d)!\n", errno_val);
575 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
580 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
581 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
584 perror ("epoll_wait()");
585 fprintf (stderr, "\nSERVER: ERROR: epoll_wait() "
586 "failed -- aborting!\n");
592 fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n");
595 for (i = 0; i < num_ev; i++)
597 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
598 if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
603 if (ssm->wait_events[i].data.u32 == ~0)
608 else if (ssm->wait_events[i].data.u32 ==
609 SOCK_TEST_AF_UNIX_ACCEPT_DATA)
614 client_fd = conn->fd;
616 if (EPOLLIN & ssm->wait_events[i].events)
619 rx_bytes = sock_test_read (client_fd, conn->buf,
620 conn->buf_size, &conn->stats);
623 rx_cfg = (vcl_test_cfg_t *) conn->buf;
624 if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
628 printf ("SERVER (fd %d): Received a cfg message!\n",
630 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
633 if (rx_bytes != sizeof (*rx_cfg))
635 printf ("SERVER (fd %d): Invalid cfg message "
636 "size (%d)!\n Should be %lu bytes.\n",
637 client_fd, rx_bytes, sizeof (*rx_cfg));
638 conn->cfg.rxbuf_size = 0;
639 conn->cfg.num_writes = 0;
640 if (conn->cfg.verbose)
642 printf ("SERVER (fd %d): Replying to "
643 "cfg message!\n", client_fd);
644 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
646 sock_test_write (client_fd, (uint8_t *) & conn->cfg,
647 sizeof (conn->cfg), NULL,
652 switch (rx_cfg->test)
654 case VCL_TEST_TYPE_NONE:
655 case VCL_TEST_TYPE_ECHO:
656 sync_config_and_reply (conn, rx_cfg);
659 case VCL_TEST_TYPE_BI:
660 case VCL_TEST_TYPE_UNI:
661 stream_test_server_start_stop (conn, rx_cfg);
664 case VCL_TEST_TYPE_EXIT:
665 printf ("SERVER: Have a great day, "
666 "connection %d!\n", client_fd);
668 conn_pool_free (conn);
669 printf ("SERVER: Closed client fd %d\n", client_fd);
673 printf ("SERVER: All client connections "
674 "closed.\n\nSERVER: "
675 "May the force be with you!\n\n");
682 "SERVER: ERROR: Unknown test type!\n");
683 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
689 else if ((conn->cfg.test == VCL_TEST_TYPE_UNI) ||
690 (conn->cfg.test == VCL_TEST_TYPE_BI))
692 stream_test_server (conn, rx_bytes);
693 if (ioctl (conn->fd, FIONREAD))
698 else if (isascii (conn->buf[0]))
700 // If it looks vaguely like a string, make sure it's terminated
701 ((char *) conn->buf)[rx_bytes <
702 conn->buf_size ? rx_bytes :
703 conn->buf_size - 1] = 0;
704 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
705 conn->fd, rx_bytes, conn->buf);
710 if (errno == ECONNRESET)
712 printf ("\nSERVER: Connection reset by remote peer.\n"
713 " Y'all have a great day now!\n\n");
720 if (isascii (conn->buf[0]))
722 /* If it looks vaguely like a string,
723 * make sure it's terminated
725 ((char *) conn->buf)[rx_bytes <
726 conn->buf_size ? rx_bytes :
727 conn->buf_size - 1] = 0;
729 fprintf (stderr, "SERVER: ERROR: "
730 "FIFO not drained in previous test!\n"
731 " extra chunks %u (0x%x)\n"
732 " extra bytes %lu (0x%lx)\n",
733 xtra, xtra, xtra_bytes, xtra_bytes);
738 if (conn->cfg.verbose)
739 printf ("SERVER (fd %d): Echoing back\n", client_fd);
741 nbytes = strlen ((const char *) conn->buf) + 1;
743 tx_bytes = sock_test_write (client_fd, conn->buf,
744 nbytes, &conn->stats,
747 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
748 conn->fd, tx_bytes, conn->buf);
751 else // Extraneous read data from non-echo tests???
754 xtra_bytes += rx_bytes;
761 close (ssm->listen_fd);
762 close (ssm->af_unix_listen_fd);
763 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
766 free (ssm->conn_pool);
772 * fd.io coding-style-patch-verification: ON
775 * eval: (c-set-style "gnu")