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 socket_server_echo_af_unix_init (sock_server_main_t * ssm)
365 if (ssm->af_unix_listen_fd > 0)
368 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
369 ssm->af_unix_listen_fd = socket (AF_UNIX, SOCK_STREAM, 0);
370 if (ssm->af_unix_listen_fd < 0)
373 perror ("ERROR in main(): socket(AF_UNIX) failed");
375 "SERVER: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed "
376 "(errno = %d)!\n", errno_val);
377 return ssm->af_unix_listen_fd;
380 memset (&ssm->serveraddr, 0, sizeof (ssm->serveraddr));
381 ssm->serveraddr.sun_family = AF_UNIX;
382 strncpy (ssm->serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME,
383 sizeof (ssm->serveraddr.sun_path));
385 rv = bind (ssm->af_unix_listen_fd, (struct sockaddr *) &ssm->serveraddr,
386 SUN_LEN (&ssm->serveraddr));
390 perror ("ERROR in main(): bind(SOCK_TEST_AF_UNIX_FILENAME) failed");
391 fprintf (stderr, "SERVER: ERROR: bind() fd %d, \"%s\": "
392 "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
393 SOCK_TEST_AF_UNIX_FILENAME, errno_val);
394 close (ssm->af_unix_listen_fd);
395 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
399 rv = listen (ssm->af_unix_listen_fd, 10);
403 perror ("ERROR in main(): listen(AF_UNIX) failed");
404 fprintf (stderr, "SERVER: ERROR: listen() fd %d, \"%s\": "
405 "failed (errno = %d)!\n", ssm->af_unix_listen_fd,
406 SOCK_TEST_AF_UNIX_FILENAME, errno_val);
407 close (ssm->af_unix_listen_fd);
408 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
412 ssm->af_unix_listen_ev.events = EPOLLIN;
413 ssm->af_unix_listen_ev.data.u32 = SOCK_TEST_AF_UNIX_ACCEPT_DATA;
414 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->af_unix_listen_fd,
415 &ssm->af_unix_listen_ev);
419 perror ("ERROR in main(): mixed epoll_ctl(EPOLL_CTL_ADD)");
420 fprintf (stderr, "SERVER: ERROR: mixed epoll_ctl(epfd %d (0x%x), "
421 "EPOLL_CTL_ADD, af_unix_listen_fd %d (0x%x), EPOLLIN) failed "
422 "(errno = %d)!\n", ssm->epfd, ssm->epfd,
423 ssm->af_unix_listen_fd, ssm->af_unix_listen_fd, errno_val);
424 close (ssm->af_unix_listen_fd);
425 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
433 print_usage_and_exit (void)
436 "sock_test_server [OPTIONS] <port>\n"
438 " -h Print this message and exit.\n"
440 " -u Use UDP transport layer\n");
445 main (int argc, char **argv)
447 sock_server_main_t *ssm = &sock_server_main;
448 int client_fd, rv, main_rv = 0;
449 int tx_bytes, rx_bytes, nbytes;
450 sock_server_conn_t *conn;
451 vcl_test_cfg_t *rx_cfg;
453 uint64_t xtra_bytes = 0;
454 struct sockaddr_storage servaddr;
457 uint16_t port = VCL_TEST_SERVER_PORT;
458 uint32_t servaddr_size;
461 while ((c = getopt (argc, argv, "6D")) != -1)
465 ssm->cfg.address_ip6 = 1;
469 ssm->cfg.transport_udp = 1;
476 if (isprint (optopt))
477 fprintf (stderr, "SERVER: ERROR: Unknown "
478 "option `-%c'.\n", optopt);
480 fprintf (stderr, "SERVER: ERROR: Unknown "
481 "option character `\\x%x'.\n", optopt);
486 print_usage_and_exit ();
489 if (argc < (optind + 1))
491 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
492 print_usage_and_exit ();
495 if (sscanf (argv[optind], "%d", &v) == 1)
499 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
500 print_usage_and_exit ();
503 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
505 ssm->listen_fd = socket (ssm->cfg.address_ip6 ? AF_INET6 : AF_INET,
506 ssm->cfg.transport_udp ? SOCK_DGRAM : SOCK_STREAM,
509 if (ssm->listen_fd < 0)
512 perror ("ERROR in main()");
513 fprintf (stderr, "SERVER: ERROR: socket() failed "
514 "(errno = %d)!\n", errno_val);
515 return ssm->listen_fd;
518 memset (&servaddr, 0, sizeof (servaddr));
520 if (ssm->cfg.address_ip6)
522 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr;
523 servaddr_size = sizeof (*server_addr);
524 server_addr->sin6_family = AF_INET6;
525 server_addr->sin6_addr = in6addr_any;
526 server_addr->sin6_port = htons (port);
530 struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr;
531 servaddr_size = sizeof (*server_addr);
532 server_addr->sin_family = AF_INET;
533 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
534 server_addr->sin_port = htons (port);
537 rv = bind (ssm->listen_fd, (struct sockaddr *) &servaddr, servaddr_size);
541 perror ("ERROR bind returned");
542 fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n",
546 if (fcntl (ssm->listen_fd, F_SETFL, O_NONBLOCK) < 0)
549 perror ("ERROR fcntl returned");
550 fprintf (stderr, "SERVER: ERROR: fcntl failed (errno = %d)!\n",
554 rv = listen (ssm->listen_fd, 10);
558 perror ("ERROR in main()");
559 fprintf (stderr, "SERVER: ERROR: listen failed "
560 "(errno = %d)!\n", errno_val);
564 ssm->epfd = epoll_create (1);
568 perror ("ERROR in main()");
569 fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n",
574 ssm->listen_ev.events = EPOLLIN;
575 ssm->listen_ev.data.u32 = ~0;
577 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
581 perror ("ERROR in main()");
582 fprintf (stderr, "SERVER: ERROR: epoll_ctl failed "
583 "(errno = %d)!\n", errno_val);
587 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
592 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
593 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
596 perror ("epoll_wait()");
597 fprintf (stderr, "\nSERVER: ERROR: epoll_wait() "
598 "failed -- aborting!\n");
604 fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n");
607 for (i = 0; i < num_ev; i++)
609 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
610 if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
615 if (ssm->wait_events[i].data.u32 == ~0)
620 else if (ssm->wait_events[i].data.u32 ==
621 SOCK_TEST_AF_UNIX_ACCEPT_DATA)
626 client_fd = conn->fd;
628 if (EPOLLIN & ssm->wait_events[i].events)
631 rx_bytes = sock_test_read (client_fd, conn->buf,
632 conn->buf_size, &conn->stats);
635 rx_cfg = (vcl_test_cfg_t *) conn->buf;
636 if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
640 printf ("SERVER (fd %d): Received a cfg message!\n",
642 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
645 if (rx_bytes != sizeof (*rx_cfg))
647 printf ("SERVER (fd %d): Invalid cfg message "
648 "size (%d)!\n Should be %lu bytes.\n",
649 client_fd, rx_bytes, sizeof (*rx_cfg));
650 conn->cfg.rxbuf_size = 0;
651 conn->cfg.num_writes = 0;
652 if (conn->cfg.verbose)
654 printf ("SERVER (fd %d): Replying to "
655 "cfg message!\n", client_fd);
656 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
658 sock_test_write (client_fd, (uint8_t *) & conn->cfg,
659 sizeof (conn->cfg), NULL,
664 switch (rx_cfg->test)
666 case VCL_TEST_TYPE_NONE:
667 sync_config_and_reply (conn, rx_cfg);
670 case VCL_TEST_TYPE_ECHO:
671 if (socket_server_echo_af_unix_init (ssm))
673 sync_config_and_reply (conn, rx_cfg);
676 case VCL_TEST_TYPE_BI:
677 case VCL_TEST_TYPE_UNI:
678 stream_test_server_start_stop (conn, rx_cfg);
681 case VCL_TEST_TYPE_EXIT:
682 printf ("SERVER: Have a great day, "
683 "connection %d!\n", client_fd);
685 conn_pool_free (conn);
686 printf ("SERVER: Closed client fd %d\n", client_fd);
690 printf ("SERVER: All client connections "
691 "closed.\n\nSERVER: "
692 "May the force be with you!\n\n");
699 "SERVER: ERROR: Unknown test type!\n");
700 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
706 else if ((conn->cfg.test == VCL_TEST_TYPE_UNI) ||
707 (conn->cfg.test == VCL_TEST_TYPE_BI))
709 stream_test_server (conn, rx_bytes);
710 if (ioctl (conn->fd, FIONREAD))
715 else if (isascii (conn->buf[0]))
717 // If it looks vaguely like a string, make sure it's terminated
718 ((char *) conn->buf)[rx_bytes <
719 conn->buf_size ? rx_bytes :
720 conn->buf_size - 1] = 0;
721 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
722 conn->fd, rx_bytes, conn->buf);
727 if (errno == ECONNRESET)
729 printf ("\nSERVER: Connection reset by remote peer.\n"
730 " Y'all have a great day now!\n\n");
737 if (isascii (conn->buf[0]))
739 /* If it looks vaguely like a string,
740 * make sure it's terminated
742 ((char *) conn->buf)[rx_bytes <
743 conn->buf_size ? rx_bytes :
744 conn->buf_size - 1] = 0;
746 fprintf (stderr, "SERVER: ERROR: "
747 "FIFO not drained in previous test!\n"
748 " extra chunks %u (0x%x)\n"
749 " extra bytes %lu (0x%lx)\n",
750 xtra, xtra, xtra_bytes, xtra_bytes);
755 if (conn->cfg.verbose)
756 printf ("SERVER (fd %d): Echoing back\n", client_fd);
758 nbytes = strlen ((const char *) conn->buf) + 1;
760 tx_bytes = sock_test_write (client_fd, conn->buf,
761 nbytes, &conn->stats,
764 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
765 conn->fd, tx_bytes, conn->buf);
768 else // Extraneous read data from non-echo tests???
771 xtra_bytes += rx_bytes;
778 close (ssm->listen_fd);
779 close (ssm->af_unix_listen_fd);
780 unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME);
783 free (ssm->conn_pool);
789 * fd.io coding-style-patch-verification: ON
792 * eval: (c-set-style "gnu")