2 * Copyright (c) 2017 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 <uri/sock_test.h>
26 #define SOCK_SERVER_USE_EPOLL 1
28 #if SOCK_SERVER_USE_EPOLL
29 #include <sys/epoll.h>
39 sock_test_stats_t stats;
46 #define SOCK_SERVER_MAX_TEST_CONN 10
47 #define SOCK_SERVER_MAX_EPOLL_EVENTS 10
51 #if SOCK_SERVER_USE_EPOLL
53 struct epoll_event listen_ev;
54 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
57 size_t conn_pool_size;
58 sock_server_conn_t *conn_pool;
62 struct timeval timeout;
65 sock_server_main_t sock_server_main;
67 #if ! SOCK_SERVER_USE_EPOLL
71 sock_server_main_t *ssm = &sock_server_main;
74 for (nfds = i = 0; i < FD_SETSIZE; i++)
76 if (FD_ISSET (i, &ssm->rd_fdset) || FD_ISSET (i, &ssm->wr_fdset))
83 conn_fdset_set (sock_server_conn_t * conn, fd_set * fdset)
85 sock_server_main_t *ssm = &sock_server_main;
87 FD_SET (conn->fd, fdset);
88 ssm->nfds = get_nfds ();
92 conn_fdset_clr (sock_server_conn_t * conn, fd_set * fdset)
94 sock_server_main_t *ssm = &sock_server_main;
96 FD_CLR (conn->fd, fdset);
97 ssm->nfds = get_nfds ();
102 conn_pool_expand (size_t expand_size)
104 sock_server_main_t *ssm = &sock_server_main;
105 sock_server_conn_t *conn_pool;
106 size_t new_size = ssm->conn_pool_size + expand_size;
109 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
112 for (i = ssm->conn_pool_size; i < new_size; i++)
114 sock_server_conn_t *conn = &conn_pool[i];
115 memset (conn, 0, sizeof (*conn));
116 sock_test_cfg_init (&conn->cfg);
117 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
118 &conn->buf, &conn->buf_size);
119 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
122 ssm->conn_pool = conn_pool;
123 ssm->conn_pool_size = new_size;
127 int errno_val = errno;
128 perror ("ERROR in conn_pool_expand()");
129 fprintf (stderr, "ERROR: Memory allocation failed (errno = %d)!\n",
134 static inline sock_server_conn_t *
135 conn_pool_alloc (void)
137 sock_server_main_t *ssm = &sock_server_main;
140 for (i = 0; i < ssm->conn_pool_size; i++)
142 if (!ssm->conn_pool[i].is_alloc)
145 ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip;
147 ssm->conn_pool[i].is_alloc = 1;
148 return (&ssm->conn_pool[i]);
156 conn_pool_free (sock_server_conn_t * conn)
158 #if ! SOCK_SERVER_USE_EPOLL
159 sock_server_main_t *ssm = &sock_server_main;
161 conn_fdset_clr (conn, &ssm->rd_fdset);
162 conn_fdset_clr (conn, &ssm->wr_fdset);
169 sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg)
172 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
173 &conn->buf, &conn->buf_size);
174 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
176 if (conn->cfg.verbose)
178 printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
179 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
181 (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
182 sizeof (conn->cfg), NULL, conn->cfg.verbose);
186 stream_test_server_start_stop (sock_server_conn_t * conn,
187 sock_test_cfg_t * rx_cfg)
189 sock_server_main_t *ssm = &sock_server_main;
190 int client_fd = conn->fd;
191 sock_test_t test = rx_cfg->test;
193 if (rx_cfg->ctrl_handle == conn->fd)
196 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
198 for (i = 0; i < ssm->conn_pool_size; i++)
200 sock_server_conn_t *tc = &ssm->conn_pool[i];
202 if (tc->cfg.ctrl_handle == conn->fd)
204 sock_test_stats_accumulate (&conn->stats, &tc->stats);
206 if (conn->cfg.verbose)
210 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
211 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
212 test == SOCK_TEST_TYPE_BI
219 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
220 (test == SOCK_TEST_TYPE_BI) /* show_tx */ ,
222 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
223 if (conn->cfg.verbose)
225 printf (" sock server main\n"
226 SOCK_TEST_SEPARATOR_STRING
228 " buf size: %u (0x%08x)\n"
229 SOCK_TEST_SEPARATOR_STRING,
230 conn->buf, conn->buf_size, conn->buf_size);
233 sync_config_and_reply (conn, rx_cfg);
234 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
235 SOCK_TEST_BANNER_STRING "\n", conn->fd,
236 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
240 printf ("\n" SOCK_TEST_BANNER_STRING
241 "SERVER (fd %d): %s-directional Stream Test!\n"
242 " Sending client the test cfg to start streaming data...\n",
243 client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
245 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
248 sync_config_and_reply (conn, rx_cfg);
250 /* read the 1st chunk, record start time */
251 memset (&conn->stats, 0, sizeof (conn->stats));
252 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
258 stream_test_server (sock_server_conn_t * conn, int rx_bytes)
260 int client_fd = conn->fd;
261 sock_test_t test = conn->cfg.test;
263 if (test == SOCK_TEST_TYPE_BI)
264 (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
267 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
269 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
276 sock_server_main_t *ssm = &sock_server_main;
278 sock_server_conn_t *conn;
280 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
281 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
283 conn = conn_pool_alloc ();
286 fprintf (stderr, "\nERROR: No free connections!\n");
291 client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt,
292 -1.0 /* wait forever */ );
296 client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
302 perror ("ERROR in new_client()");
303 fprintf (stderr, "ERROR: accept failed (errno = %d)!\n", errno_val);
306 printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
307 client_fd, client_fd);
309 conn->fd = client_fd;
311 #if ! SOCK_SERVER_USE_EPOLL
312 conn_fdset_set (conn, &ssm->rd_fdset);
316 struct epoll_event ev;
320 ev.data.u64 = conn - ssm->conn_pool;
322 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
326 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
332 perror ("ERROR in new_client()");
333 fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n",
343 main (int argc, char **argv)
345 sock_server_main_t *ssm = &sock_server_main;
346 int client_fd, rv, main_rv = 0;
347 int tx_bytes, rx_bytes, nbytes;
348 sock_server_conn_t *conn;
349 sock_test_cfg_t *rx_cfg;
351 uint64_t xtra_bytes = 0;
352 struct sockaddr_in servaddr;
355 uint16_t port = SOCK_TEST_SERVER_PORT;
356 #if ! SOCK_SERVER_USE_EPOLL
357 fd_set _rfdset, *rfdset = &_rfdset;
360 vppcom_endpt_t endpt;
362 #if ! SOCK_SERVER_USE_EPOLL
363 fd_set _wfdset, *wfdset = &_wfdset;
367 if ((argc == 2) && (sscanf (argv[1], "%d", &v) == 1))
370 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
373 rv = vppcom_app_create ("vcl_test_server");
382 vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP,
383 0 /* is_nonblocking */ );
386 ssm->listen_fd = socket (AF_INET, SOCK_STREAM, 0);
388 if (ssm->listen_fd < 0)
391 perror ("ERROR in main()");
392 fprintf (stderr, "ERROR: socket() failed (errno = %d)!\n", errno_val);
393 return ssm->listen_fd;
396 memset (&servaddr, 0, sizeof (servaddr));
398 servaddr.sin_family = AF_INET;
399 servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
400 servaddr.sin_port = htons (port);
403 endpt.vrf = VPPCOM_VRF_DEFAULT;
404 endpt.is_ip4 = (servaddr.sin_family == AF_INET);
405 endpt.ip = (uint8_t *) & servaddr.sin_addr;
406 endpt.port = (uint16_t) servaddr.sin_port;
408 rv = vppcom_session_bind (ssm->listen_fd, &endpt);
416 bind (ssm->listen_fd, (struct sockaddr *) &servaddr, sizeof (servaddr));
421 perror ("ERROR in main()");
422 fprintf (stderr, "ERROR: bind failed (errno = %d)!\n", errno_val);
427 rv = vppcom_session_listen (ssm->listen_fd, 10);
434 rv = listen (ssm->listen_fd, 10);
439 perror ("ERROR in main()");
440 fprintf (stderr, "ERROR: listen failed (errno = %d)!\n", errno_val);
444 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
446 #if ! SOCK_SERVER_USE_EPOLL
448 FD_ZERO (&ssm->wr_fdset);
449 FD_ZERO (&ssm->rd_fdset);
451 FD_SET (ssm->listen_fd, &ssm->rd_fdset);
452 ssm->nfds = ssm->listen_fd + 1;
456 ssm->epfd = vppcom_epoll_create ();
460 ssm->epfd = epoll_create (1);
465 perror ("ERROR in main()");
466 fprintf (stderr, "ERROR: epoll_create failed (errno = %d)!\n",
471 ssm->listen_ev.events = EPOLLIN;
472 ssm->listen_ev.data.u32 = ~0;
474 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd,
479 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
484 perror ("ERROR in main()");
485 fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n", errno_val);
492 #if ! SOCK_SERVER_USE_EPOLL
493 _rfdset = ssm->rd_fdset;
496 rv = vppcom_select (ssm->nfds, (uint64_t *) rfdset, NULL, NULL, 0);
499 struct timeval timeout;
500 timeout = ssm->timeout;
501 _wfdset = ssm->wr_fdset;
502 rv = select (ssm->nfds, rfdset, wfdset, NULL, &timeout);
508 fprintf (stderr, "\nERROR: select() failed -- aborting!\n");
515 if (FD_ISSET (ssm->listen_fd, rfdset))
518 for (i = 0; i < ssm->conn_pool_size; i++)
520 if (!ssm->conn_pool[i].is_alloc)
523 conn = &ssm->conn_pool[i];
527 num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events,
528 SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0);
532 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
533 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
537 perror ("epoll_wait()");
538 fprintf (stderr, "\nERROR: epoll_wait() failed -- aborting!\n");
544 fprintf (stderr, "\nepoll_wait() timeout!\n");
547 for (i = 0; i < num_ev; i++)
549 if (ssm->wait_events[i].data.u32 == ~0)
554 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
556 client_fd = conn->fd;
558 #if ! SOCK_SERVER_USE_EPOLL
559 if (FD_ISSET (client_fd, rfdset))
561 if (EPOLLIN & ssm->wait_events[i].events)
564 rx_bytes = sock_test_read (client_fd, conn->buf,
565 conn->buf_size, &conn->stats);
568 rx_cfg = (sock_test_cfg_t *) conn->buf;
569 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
573 printf ("SERVER (fd %d): Received a cfg message!\n",
575 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
578 if (rx_bytes != sizeof (*rx_cfg))
580 printf ("SERVER (fd %d): Invalid cfg message "
581 "size (%d)!\n Should be %lu bytes.\n",
582 client_fd, rx_bytes, sizeof (*rx_cfg));
583 conn->cfg.rxbuf_size = 0;
584 conn->cfg.num_writes = 0;
585 if (conn->cfg.verbose)
587 printf ("SERVER (fd %d): Replying to "
588 "cfg message!\n", client_fd);
589 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
591 sock_test_write (client_fd, (uint8_t *) & conn->cfg,
592 sizeof (conn->cfg), NULL,
597 switch (rx_cfg->test)
599 case SOCK_TEST_TYPE_NONE:
600 case SOCK_TEST_TYPE_ECHO:
601 sync_config_and_reply (conn, rx_cfg);
604 case SOCK_TEST_TYPE_BI:
605 case SOCK_TEST_TYPE_UNI:
606 stream_test_server_start_stop (conn, rx_cfg);
609 case SOCK_TEST_TYPE_EXIT:
610 printf ("SERVER: Have a great day, "
611 "connection %d!\n", client_fd);
613 vppcom_session_close (client_fd);
617 conn_pool_free (conn);
618 #if ! SOCK_SERVER_USE_EPOLL
619 if (ssm->nfds == (ssm->listen_fd + 1))
625 printf ("SERVER: All client connections "
626 "closed.\n\nSERVER: "
627 "May the force be with you!\n\n");
633 fprintf (stderr, "ERROR: Unknown test type!\n");
634 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
640 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) ||
641 (conn->cfg.test == SOCK_TEST_TYPE_BI))
643 stream_test_server (conn, rx_bytes);
647 else if (isascii (conn->buf[0]))
649 // If it looks vaguely like a string, make sure it's terminated
650 ((char *) conn->buf)[rx_bytes <
651 conn->buf_size ? rx_bytes :
652 conn->buf_size - 1] = 0;
653 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
654 conn->fd, rx_bytes, conn->buf);
659 if (errno == ECONNRESET)
661 printf ("\nSERVER: Connection reset by remote peer.\n"
662 " Y'all have a great day now!\n\n");
669 if (isascii (conn->buf[0]))
671 // If it looks vaguely like a string, make sure it's terminated
672 ((char *) conn->buf)[rx_bytes <
673 conn->buf_size ? rx_bytes :
674 conn->buf_size - 1] = 0;
677 "ERROR: FIFO not drained in previous test!\n"
678 " extra chunks %u (0x%x)\n"
679 " extra bytes %lu (0x%lx)\n",
680 xtra, xtra, xtra_bytes, xtra_bytes);
685 if (conn->cfg.verbose)
686 printf ("SERVER (fd %d): Echoing back\n", client_fd);
688 nbytes = strlen ((const char *) conn->buf) + 1;
690 tx_bytes = sock_test_write (client_fd, conn->buf,
691 nbytes, &conn->stats,
694 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
695 conn->fd, tx_bytes, conn->buf);
698 else // Extraneous read data from non-echo tests???
701 xtra_bytes += rx_bytes;
709 vppcom_session_close (ssm->listen_fd);
710 vppcom_app_destroy ();
712 close (ssm->listen_fd);
715 free (ssm->conn_pool);
721 * fd.io coding-style-patch-verification: ON
724 * eval: (c-set-style "gnu")