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 <vcl/sock_test.h>
28 #define SOCK_SERVER_USE_EPOLL 1
29 #define VPPCOM_SESSION_ATTR_UNIT_TEST 0
31 #if SOCK_SERVER_USE_EPOLL
32 #include <sys/epoll.h>
36 #if VPPCOM_SESSION_ATTR_UNIT_TEST
37 #define BUFLEN sizeof (uint64_t) * 16
39 uint32_t buflen = BUFLEN;
40 uint32_t *flags = (uint32_t *) buffer;
51 sock_test_stats_t stats;
58 #define SOCK_SERVER_MAX_TEST_CONN 10
59 #define SOCK_SERVER_MAX_EPOLL_EVENTS 10
63 #if SOCK_SERVER_USE_EPOLL
65 struct epoll_event listen_ev;
66 struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS];
69 size_t conn_pool_size;
70 sock_server_conn_t *conn_pool;
74 struct timeval timeout;
77 sock_server_main_t sock_server_main;
79 #if ! SOCK_SERVER_USE_EPOLL
83 sock_server_main_t *ssm = &sock_server_main;
86 for (nfds = i = 0; i < FD_SETSIZE; i++)
88 if (FD_ISSET (i, &ssm->rd_fdset) || FD_ISSET (i, &ssm->wr_fdset))
95 conn_fdset_set (sock_server_conn_t * conn, fd_set * fdset)
97 sock_server_main_t *ssm = &sock_server_main;
99 FD_SET (conn->fd, fdset);
100 ssm->nfds = get_nfds ();
104 conn_fdset_clr (sock_server_conn_t * conn, fd_set * fdset)
106 sock_server_main_t *ssm = &sock_server_main;
108 FD_CLR (conn->fd, fdset);
109 ssm->nfds = get_nfds ();
114 conn_pool_expand (size_t expand_size)
116 sock_server_main_t *ssm = &sock_server_main;
117 sock_server_conn_t *conn_pool;
118 size_t new_size = ssm->conn_pool_size + expand_size;
121 conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool));
124 for (i = ssm->conn_pool_size; i < new_size; i++)
126 sock_server_conn_t *conn = &conn_pool[i];
127 memset (conn, 0, sizeof (*conn));
128 sock_test_cfg_init (&conn->cfg);
129 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
130 &conn->buf, &conn->buf_size);
131 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
134 ssm->conn_pool = conn_pool;
135 ssm->conn_pool_size = new_size;
139 int errno_val = errno;
140 perror ("ERROR in conn_pool_expand()");
141 fprintf (stderr, "ERROR: Memory allocation failed (errno = %d)!\n",
146 static inline sock_server_conn_t *
147 conn_pool_alloc (void)
149 sock_server_main_t *ssm = &sock_server_main;
152 for (i = 0; i < ssm->conn_pool_size; i++)
154 if (!ssm->conn_pool[i].is_alloc)
157 ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip;
159 ssm->conn_pool[i].is_alloc = 1;
160 return (&ssm->conn_pool[i]);
168 conn_pool_free (sock_server_conn_t * conn)
170 #if ! SOCK_SERVER_USE_EPOLL
171 sock_server_main_t *ssm = &sock_server_main;
173 conn_fdset_clr (conn, &ssm->rd_fdset);
174 conn_fdset_clr (conn, &ssm->wr_fdset);
181 sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg)
184 sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
185 &conn->buf, &conn->buf_size);
186 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
188 if (conn->cfg.verbose)
190 printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd);
191 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
193 (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg,
194 sizeof (conn->cfg), NULL, conn->cfg.verbose);
198 stream_test_server_start_stop (sock_server_conn_t * conn,
199 sock_test_cfg_t * rx_cfg)
201 sock_server_main_t *ssm = &sock_server_main;
202 int client_fd = conn->fd;
203 sock_test_t test = rx_cfg->test;
205 if (rx_cfg->ctrl_handle == conn->fd)
208 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
210 for (i = 0; i < ssm->conn_pool_size; i++)
212 sock_server_conn_t *tc = &ssm->conn_pool[i];
214 if (tc->cfg.ctrl_handle == conn->fd)
216 sock_test_stats_accumulate (&conn->stats, &tc->stats);
218 if (conn->cfg.verbose)
222 sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd);
223 sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
224 test == SOCK_TEST_TYPE_BI
231 sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
232 (test == SOCK_TEST_TYPE_BI) /* show_tx */ ,
234 sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
235 if (conn->cfg.verbose)
237 printf (" sock server main\n"
238 SOCK_TEST_SEPARATOR_STRING
240 " buf size: %u (0x%08x)\n"
241 SOCK_TEST_SEPARATOR_STRING,
242 conn->buf, conn->buf_size, conn->buf_size);
245 sync_config_and_reply (conn, rx_cfg);
246 printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n"
247 SOCK_TEST_BANNER_STRING "\n", conn->fd,
248 test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
252 printf ("\n" SOCK_TEST_BANNER_STRING
253 "SERVER (fd %d): %s-directional Stream Test!\n"
254 " Sending client the test cfg to start streaming data...\n",
255 client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni");
257 rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd :
260 sync_config_and_reply (conn, rx_cfg);
262 /* read the 1st chunk, record start time */
263 memset (&conn->stats, 0, sizeof (conn->stats));
264 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
270 stream_test_server (sock_server_conn_t * conn, int rx_bytes)
272 int client_fd = conn->fd;
273 sock_test_t test = conn->cfg.test;
275 if (test == SOCK_TEST_TYPE_BI)
276 (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
279 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
281 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
288 sock_server_main_t *ssm = &sock_server_main;
290 sock_server_conn_t *conn;
292 if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1))
293 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
295 conn = conn_pool_alloc ();
298 fprintf (stderr, "\nERROR: No free connections!\n");
303 client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, 0,
304 -1.0 /* wait forever */ );
308 client_fd = accept4 (ssm->listen_fd, (struct sockaddr *) NULL, NULL, NULL);
310 client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL);
316 perror ("ERROR in new_client()");
317 fprintf (stderr, "ERROR: accept failed (errno = %d)!\n", errno_val);
320 printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n",
321 client_fd, client_fd);
323 conn->fd = client_fd;
325 #if ! SOCK_SERVER_USE_EPOLL
326 conn_fdset_set (conn, &ssm->rd_fdset);
330 struct epoll_event ev;
334 ev.data.u64 = conn - ssm->conn_pool;
336 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
340 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev);
346 perror ("ERROR in new_client()");
347 fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n",
357 main (int argc, char **argv)
359 sock_server_main_t *ssm = &sock_server_main;
360 int client_fd, rv, main_rv = 0;
361 int tx_bytes, rx_bytes, nbytes;
362 sock_server_conn_t *conn;
363 sock_test_cfg_t *rx_cfg;
365 uint64_t xtra_bytes = 0;
366 struct sockaddr_in servaddr;
369 uint16_t port = SOCK_TEST_SERVER_PORT;
370 #if ! SOCK_SERVER_USE_EPOLL
371 fd_set _rfdset, *rfdset = &_rfdset;
374 vppcom_endpt_t endpt;
376 #if ! SOCK_SERVER_USE_EPOLL
377 fd_set _wfdset, *wfdset = &_wfdset;
381 if ((argc == 2) && (sscanf (argv[1], "%d", &v) == 1))
384 conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1);
387 rv = vppcom_app_create ("vcl_test_server");
396 vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP,
397 0 /* is_nonblocking */ );
400 ssm->listen_fd = socket (AF_INET, SOCK_STREAM, 0);
402 if (ssm->listen_fd < 0)
405 perror ("ERROR in main()");
406 fprintf (stderr, "ERROR: socket() failed (errno = %d)!\n", errno_val);
407 return ssm->listen_fd;
410 memset (&servaddr, 0, sizeof (servaddr));
412 servaddr.sin_family = AF_INET;
413 servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
414 servaddr.sin_port = htons (port);
417 endpt.vrf = VPPCOM_VRF_DEFAULT;
418 endpt.is_ip4 = (servaddr.sin_family == AF_INET);
419 endpt.ip = (uint8_t *) & servaddr.sin_addr;
420 endpt.port = (uint16_t) servaddr.sin_port;
422 rv = vppcom_session_bind (ssm->listen_fd, &endpt);
429 #if VPPCOM_SESSION_ATTR_UNIT_TEST
431 if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS,
432 buffer, &buflen) != VPPCOM_OK)
433 printf ("\nGET_FLAGS0: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
435 *flags = O_RDWR | O_NONBLOCK;
436 if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_SET_FLAGS,
437 buffer, &buflen) != VPPCOM_OK)
438 printf ("\nSET_FLAGS1: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
440 if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS,
441 buffer, &buflen) != VPPCOM_OK)
442 printf ("\nGET_FLAGS1:Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
445 if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_SET_FLAGS,
446 buffer, &buflen) != VPPCOM_OK)
447 printf ("\nSET_FLAGS2: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
449 if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS,
450 buffer, &buflen) != VPPCOM_OK)
451 printf ("\nGET_FLAGS2:Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
454 if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_PEER_ADDR,
455 buffer, &buflen) != VPPCOM_OK)
456 printf ("\nGET_PEER_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
458 if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_LCL_ADDR,
459 buffer, &buflen) != VPPCOM_OK)
460 printf ("\nGET_LCL_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
464 bind (ssm->listen_fd, (struct sockaddr *) &servaddr, sizeof (servaddr));
469 perror ("ERROR in main()");
470 fprintf (stderr, "ERROR: bind failed (errno = %d)!\n", errno_val);
475 rv = vppcom_session_listen (ssm->listen_fd, 10);
482 rv = listen (ssm->listen_fd, 10);
487 perror ("ERROR in main()");
488 fprintf (stderr, "ERROR: listen failed (errno = %d)!\n", errno_val);
492 printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port);
494 #if ! SOCK_SERVER_USE_EPOLL
496 FD_ZERO (&ssm->wr_fdset);
497 FD_ZERO (&ssm->rd_fdset);
499 FD_SET (ssm->listen_fd, &ssm->rd_fdset);
500 ssm->nfds = ssm->listen_fd + 1;
504 ssm->epfd = vppcom_epoll_create ();
508 ssm->epfd = epoll_create (1);
513 perror ("ERROR in main()");
514 fprintf (stderr, "ERROR: epoll_create failed (errno = %d)!\n",
519 ssm->listen_ev.events = EPOLLIN;
520 ssm->listen_ev.data.u32 = ~0;
522 rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd,
527 rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev);
532 perror ("ERROR in main()");
533 fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n", errno_val);
540 #if ! SOCK_SERVER_USE_EPOLL
541 _rfdset = ssm->rd_fdset;
544 rv = vppcom_select (ssm->nfds, (uint64_t *) rfdset, NULL, NULL, 0);
547 struct timeval timeout;
548 timeout = ssm->timeout;
549 _wfdset = ssm->wr_fdset;
550 rv = select (ssm->nfds, rfdset, wfdset, NULL, &timeout);
556 fprintf (stderr, "\nERROR: select() failed -- aborting!\n");
563 if (FD_ISSET (ssm->listen_fd, rfdset))
566 for (i = 0; i < ssm->conn_pool_size; i++)
568 if (!ssm->conn_pool[i].is_alloc)
571 conn = &ssm->conn_pool[i];
575 num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events,
576 SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0);
580 num_ev = epoll_wait (ssm->epfd, ssm->wait_events,
581 SOCK_SERVER_MAX_EPOLL_EVENTS, 60000);
585 perror ("epoll_wait()");
586 fprintf (stderr, "\nERROR: epoll_wait() failed -- aborting!\n");
592 fprintf (stderr, "\nepoll_wait() timeout!\n");
595 for (i = 0; i < num_ev; i++)
597 if (ssm->wait_events[i].data.u32 == ~0)
602 conn = &ssm->conn_pool[ssm->wait_events[i].data.u32];
604 client_fd = conn->fd;
606 #if ! SOCK_SERVER_USE_EPOLL
607 if (FD_ISSET (client_fd, rfdset))
609 if (EPOLLIN & ssm->wait_events[i].events)
613 #if VPPCOM_SESSION_ATTR_UNIT_TEST
615 if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_NREAD,
616 buffer, &buflen) < VPPCOM_OK)
617 printf ("\nNREAD: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n");
618 if (vppcom_session_attr (client_fd,
619 VPPCOM_ATTR_GET_PEER_ADDR,
620 buffer, &buflen) != VPPCOM_OK)
621 printf ("\nGET_PEER_ADDR: Oh no, Mr. "
622 "Biiiiiiiiiiiilllllll ! ! ! !\n");
624 if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_LCL_ADDR,
625 buffer, &buflen) != VPPCOM_OK)
626 printf ("\nGET_LCL_ADDR: Oh no, Mr. "
627 "Biiiiiiiiiiiilllllll ! ! ! !\n");
630 rx_bytes = sock_test_read (client_fd, conn->buf,
631 conn->buf_size, &conn->stats);
634 rx_cfg = (sock_test_cfg_t *) conn->buf;
635 if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC)
639 printf ("SERVER (fd %d): Received a cfg message!\n",
641 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
644 if (rx_bytes != sizeof (*rx_cfg))
646 printf ("SERVER (fd %d): Invalid cfg message "
647 "size (%d)!\n Should be %lu bytes.\n",
648 client_fd, rx_bytes, sizeof (*rx_cfg));
649 conn->cfg.rxbuf_size = 0;
650 conn->cfg.num_writes = 0;
651 if (conn->cfg.verbose)
653 printf ("SERVER (fd %d): Replying to "
654 "cfg message!\n", client_fd);
655 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
657 sock_test_write (client_fd, (uint8_t *) & conn->cfg,
658 sizeof (conn->cfg), NULL,
663 switch (rx_cfg->test)
665 case SOCK_TEST_TYPE_NONE:
666 case SOCK_TEST_TYPE_ECHO:
667 sync_config_and_reply (conn, rx_cfg);
670 case SOCK_TEST_TYPE_BI:
671 case SOCK_TEST_TYPE_UNI:
672 stream_test_server_start_stop (conn, rx_cfg);
675 case SOCK_TEST_TYPE_EXIT:
676 printf ("SERVER: Have a great day, "
677 "connection %d!\n", client_fd);
679 vppcom_session_close (client_fd);
683 conn_pool_free (conn);
684 #if ! SOCK_SERVER_USE_EPOLL
685 if (ssm->nfds == (ssm->listen_fd + 1))
691 printf ("SERVER: All client connections "
692 "closed.\n\nSERVER: "
693 "May the force be with you!\n\n");
699 fprintf (stderr, "ERROR: Unknown test type!\n");
700 sock_test_cfg_dump (rx_cfg, 0 /* is_client */ );
706 else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) ||
707 (conn->cfg.test == SOCK_TEST_TYPE_BI))
709 stream_test_server (conn, rx_bytes);
713 else if (isascii (conn->buf[0]))
715 // If it looks vaguely like a string, make sure it's terminated
716 ((char *) conn->buf)[rx_bytes <
717 conn->buf_size ? rx_bytes :
718 conn->buf_size - 1] = 0;
719 printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n",
720 conn->fd, rx_bytes, conn->buf);
725 if (errno == ECONNRESET)
727 printf ("\nSERVER: Connection reset by remote peer.\n"
728 " Y'all have a great day now!\n\n");
735 if (isascii (conn->buf[0]))
737 // If it looks vaguely like a string, make sure it's terminated
738 ((char *) conn->buf)[rx_bytes <
739 conn->buf_size ? rx_bytes :
740 conn->buf_size - 1] = 0;
743 "ERROR: FIFO not drained in previous test!\n"
744 " extra chunks %u (0x%x)\n"
745 " extra bytes %lu (0x%lx)\n",
746 xtra, xtra, xtra_bytes, xtra_bytes);
751 if (conn->cfg.verbose)
752 printf ("SERVER (fd %d): Echoing back\n", client_fd);
754 nbytes = strlen ((const char *) conn->buf) + 1;
756 tx_bytes = sock_test_write (client_fd, conn->buf,
757 nbytes, &conn->stats,
760 printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n",
761 conn->fd, tx_bytes, conn->buf);
764 else // Extraneous read data from non-echo tests???
767 xtra_bytes += rx_bytes;
775 vppcom_session_close (ssm->listen_fd);
776 vppcom_app_destroy ();
778 close (ssm->listen_fd);
781 free (ssm->conn_pool);
787 * fd.io coding-style-patch-verification: ON
790 * eval: (c-set-style "gnu")