2 * Copyright (c) 2017-2021 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>
26 #include <hs_apps/vcl/vcl_test.h>
27 #include <sys/epoll.h>
28 #include <vppinfra/mem.h>
38 } vcl_test_server_cfg_t;
42 vcl_test_session_t *conn_pool;
47 vcl_test_session_t listener;
48 pthread_t thread_handle;
49 } vcl_test_server_worker_t;
53 vcl_test_server_worker_t *workers;
54 vcl_test_session_t *ctrl;
55 vcl_test_server_cfg_t server_cfg;
57 struct sockaddr_storage servaddr;
58 volatile int worker_fails;
59 volatile int active_workers;
62 } vcl_test_server_main_t;
64 vcl_test_main_t vcl_test_main;
66 static vcl_test_server_main_t vcl_server_main;
69 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
71 vcl_test_session_t *conn_pool;
72 size_t new_size = wrk->conn_pool_size + expand_size;
75 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
78 for (i = wrk->conn_pool_size; i < new_size; i++)
80 vcl_test_session_t *conn = &conn_pool[i];
81 memset (conn, 0, sizeof (*conn));
84 wrk->conn_pool = conn_pool;
85 wrk->conn_pool_size = new_size;
89 vterr ("conn_pool_expand()", -errno);
93 static inline vcl_test_session_t *
94 conn_pool_alloc (vcl_test_server_worker_t *wrk)
96 vcl_test_session_t *conn;
100 for (i = 0; i < wrk->conn_pool_size; i++)
102 if (!wrk->conn_pool[i].is_alloc)
104 conn = &wrk->conn_pool[i];
105 memset (conn, 0, sizeof (*conn));
106 conn->endpt.ip = wrk->conn_pool[i].ip;
108 conn->session_index = i;
109 hs_test_cfg_init (&conn->cfg);
110 return (&wrk->conn_pool[i]);
116 conn_pool_expand (wrk, 2 * wrk->conn_pool_size);
120 vtwrn ("Failed to allocate connection even after expand");
125 conn_pool_free (vcl_test_session_t *ts)
129 vcl_test_session_buf_free (ts);
133 sync_config_and_reply (vcl_test_session_t *conn, hs_test_cfg_t *rx_cfg)
136 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */, (uint8_t **) &conn->rxbuf,
138 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
140 if (conn->cfg.verbose)
142 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
143 hs_test_cfg_dump (&conn->cfg, 0 /* is_client */);
145 (void) vcl_test_write (conn, &conn->cfg, sizeof (conn->cfg));
149 vts_session_close (vcl_test_session_t *conn)
151 vcl_test_server_main_t *vsm = &vcl_server_main;
152 vcl_test_main_t *vt = &vcl_test_main;
157 if (vt->protos[vsm->server_cfg.proto]->close)
158 vt->protos[vsm->server_cfg.proto]->close (conn);
160 vppcom_session_close (conn->fd);
165 vts_session_cleanup (vcl_test_session_t *ts)
167 vts_session_close (ts);
172 vts_wrk_cleanup_all (vcl_test_server_worker_t *wrk)
174 vcl_test_session_t *conn;
177 for (i = 0; i < wrk->conn_pool_size; i++)
179 conn = &wrk->conn_pool[i];
180 vts_session_cleanup (conn);
187 vts_test_cmd (vcl_test_server_worker_t *wrk, vcl_test_session_t *conn,
188 hs_test_cfg_t *rx_cfg)
190 u8 is_bi = rx_cfg->test == HS_TEST_TYPE_BI;
191 vcl_test_session_t *tc;
195 if (rx_cfg->cmd == HS_TEST_CMD_STOP)
197 struct timespec stop;
198 clock_gettime (CLOCK_REALTIME, &stop);
200 /* Test session are not closed, e.g., connection-less or errors */
203 vtinf ("%u sessions are still open", wrk->nfds - 1);
204 stop.tv_sec -= VCL_TEST_DELAY_DISCONNECT;
205 conn->stats.stop = stop;
208 /* Accumulate stats over all of the worker's sessions */
209 for (i = 0; i < wrk->conn_pool_size; i++)
211 tc = &wrk->conn_pool[i];
215 vcl_test_stats_accumulate (&conn->stats, &tc->stats);
218 vts_session_cleanup (tc);
221 /* Only relevant if all connections previously closed */
222 if (vcl_comp_tspec (&conn->stats.stop, &tc->stats.stop) < 0)
223 conn->stats.stop = tc->stats.stop;
226 if (conn->cfg.verbose)
228 snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS", conn->fd);
229 vcl_test_stats_dump (buf, &conn->stats, 1 /* show_rx */,
230 is_bi /* show tx */, conn->cfg.verbose);
233 vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
234 is_bi /* show_tx */ , conn->cfg.verbose);
235 hs_test_cfg_dump (&conn->cfg, 0 /* is_client */);
236 if (conn->cfg.verbose)
238 vtinf (" vcl server main\n" HS_TEST_SEPARATOR_STRING
240 " buf size: %u (0x%08x)\n" HS_TEST_SEPARATOR_STRING,
241 conn->rxbuf, conn->rxbuf_size, conn->rxbuf_size);
244 sync_config_and_reply (conn, rx_cfg);
245 memset (&conn->stats, 0, sizeof (conn->stats));
247 else if (rx_cfg->cmd == HS_TEST_CMD_SYNC)
249 rx_cfg->ctrl_handle = conn->fd;
250 vtinf ("Set control fd %d for test!", conn->fd);
251 sync_config_and_reply (conn, rx_cfg);
253 else if (rx_cfg->cmd == HS_TEST_CMD_START)
255 vtinf ("Starting %s-directional Stream Test (fd %d)!",
256 is_bi ? "Bi" : "Uni", conn->fd);
257 rx_cfg->ctrl_handle = conn->fd;
258 sync_config_and_reply (conn, rx_cfg);
260 /* read the 1st chunk, record start time */
261 memset (&conn->stats, 0, sizeof (conn->stats));
262 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
267 vts_server_process_rx (vcl_test_session_t *conn, int rx_bytes)
269 vcl_test_server_main_t *vsm = &vcl_server_main;
271 if (conn->cfg.test == HS_TEST_TYPE_BI)
275 (void) vcl_test_write (conn, conn->ds[0].data, conn->ds[0].len);
277 (void) vcl_test_write (conn, conn->ds[1].data, conn->ds[1].len);
280 (void) vcl_test_write (conn, conn->rxbuf, rx_bytes);
284 vppcom_session_free_segments (conn->fd, rx_bytes);
286 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
287 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
291 vts_server_echo (vcl_test_session_t *conn, int rx_bytes)
293 int tx_bytes, nbytes, pos;
295 /* If it looks vaguely like a string, make sure it's terminated */
296 pos = rx_bytes < conn->rxbuf_size ? rx_bytes : conn->rxbuf_size - 1;
297 ((char *) conn->rxbuf)[pos] = 0;
298 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->rxbuf);
300 if (conn->cfg.verbose)
301 vtinf ("(fd %d): Echoing back", conn->fd);
303 nbytes = strlen ((const char *) conn->rxbuf) + 1;
304 tx_bytes = conn->write (conn, conn->rxbuf, nbytes);
306 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->rxbuf);
309 static vcl_test_session_t *
310 vts_accept_ctrl (vcl_test_server_worker_t *wrk, int listen_fd)
312 vcl_test_server_main_t *vsm = &vcl_server_main;
313 const vcl_test_proto_vft_t *tp;
314 vcl_test_session_t *conn;
315 struct epoll_event ev;
318 conn = conn_pool_alloc (wrk);
321 vtwrn ("No free connections!");
326 conn->cfg = vsm->ctrl->cfg;
327 vcl_test_session_buf_alloc (conn);
328 clock_gettime (CLOCK_REALTIME, &conn->old_stats.stop);
330 tp = vcl_test_main.protos[VPPCOM_PROTO_TCP];
331 if (tp->accept (listen_fd, conn))
334 vtinf ("CTRL accepted fd = %d (0x%08x) on listener fd = %d (0x%08x)",
335 conn->fd, conn->fd, listen_fd, listen_fd);
337 ev.events = EPOLLET | EPOLLIN;
338 ev.data.u64 = conn - wrk->conn_pool;
339 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, conn->fd, &ev);
342 vterr ("vppcom_epoll_ctl()", rv);
351 static vcl_test_session_t *
352 vts_accept_client (vcl_test_server_worker_t *wrk, int listen_fd)
354 vcl_test_server_main_t *vsm = &vcl_server_main;
355 const vcl_test_proto_vft_t *tp;
356 vcl_test_session_t *conn;
357 struct epoll_event ev;
360 conn = conn_pool_alloc (wrk);
363 vtwrn ("No free connections!");
368 conn->cfg = vsm->ctrl->cfg;
369 vcl_test_session_buf_alloc (conn);
370 clock_gettime (CLOCK_REALTIME, &conn->old_stats.stop);
372 tp = vcl_test_main.protos[vsm->server_cfg.proto];
373 if (tp->accept (listen_fd, conn))
376 if (conn->cfg.num_test_sessions < VCL_TEST_CFG_MAX_SELECT_SESS)
377 vtinf ("Got a connection -- fd = %d (0x%08x) on listener fd = %d (0x%08x)",
378 conn->fd, conn->fd, listen_fd, listen_fd);
380 ev.events = EPOLLET | EPOLLIN;
381 ev.data.u64 = conn - wrk->conn_pool;
382 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, conn->fd, &ev);
385 vterr ("vppcom_epoll_ctl()", rv);
394 print_usage_and_exit (void)
396 fprintf (stderr, "vcl_test_server [OPTIONS] <port>\n"
398 " -h Print this message and exit.\n"
400 " -w <num> Number of workers\n"
401 " -p <PROTO> Use <PROTO> transport layer\n"
402 " -D Use UDP transport layer\n"
403 " -L Use TLS transport layer\n"
404 " -S Incremental stats\n");
409 vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm)
411 struct sockaddr_storage *servaddr = &vsm->servaddr;
412 memset (servaddr, 0, sizeof (*servaddr));
414 if (vsm->server_cfg.address_ip6)
416 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
417 server_addr->sin6_family = AF_INET6;
418 server_addr->sin6_addr = in6addr_any;
419 server_addr->sin6_port = htons (vsm->server_cfg.port);
423 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
424 server_addr->sin_family = AF_INET;
425 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
426 server_addr->sin_port = htons (vsm->server_cfg.port);
429 if (vsm->server_cfg.address_ip6)
431 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
432 vsm->server_cfg.endpt.is_ip4 = 0;
433 vsm->server_cfg.endpt.ip = (uint8_t *) &server_addr->sin6_addr;
434 vsm->server_cfg.endpt.port = (uint16_t) server_addr->sin6_port;
438 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
439 vsm->server_cfg.endpt.is_ip4 = 1;
440 vsm->server_cfg.endpt.ip = (uint8_t *) &server_addr->sin_addr;
441 vsm->server_cfg.endpt.port = (uint16_t) server_addr->sin_port;
446 vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc,
451 vsm->server_cfg.proto = VPPCOM_PROTO_TCP;
454 while ((c = getopt (argc, argv, "6DLsw:hp:S")) != -1)
458 vsm->server_cfg.address_ip6 = 1;
462 if (vppcom_unformat_proto (&vsm->server_cfg.proto, optarg))
463 vtwrn ("Invalid vppcom protocol %s, defaulting to TCP", optarg);
467 vsm->server_cfg.proto = VPPCOM_PROTO_UDP;
471 vsm->server_cfg.proto = VPPCOM_PROTO_TLS;
477 vsm->server_cfg.workers = v;
479 vtwrn ("Invalid number of workers %d", v);
485 vsm->incremental_stats = 1;
492 vtwrn ("Option `-%c' requires an argument.", optopt);
495 if (isprint (optopt))
496 vtwrn ("Unknown option `-%c'.", optopt);
498 vtwrn ("Unknown option character `\\x%x'.", optopt);
503 print_usage_and_exit ();
506 if (argc > (optind + 1))
508 fprintf (stderr, "Incorrect number of arguments!\n");
509 print_usage_and_exit ();
511 else if (argc > 1 && argc == (optind + 1))
513 if (sscanf (argv[optind], "%d", &v) == 1)
514 vsm->server_cfg.port = (uint16_t) v;
517 fprintf (stderr, "Invalid port (%s)!\n", argv[optind]);
518 print_usage_and_exit ();
522 vcl_test_init_endpoint_addr (vsm);
526 vts_handle_ctrl_cfg (vcl_test_server_worker_t *wrk, hs_test_cfg_t *rx_cfg,
527 vcl_test_session_t *conn, int rx_bytes)
531 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
532 hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
535 if (rx_bytes != sizeof (*rx_cfg))
537 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
538 rx_bytes, sizeof (*rx_cfg));
539 conn->cfg.rxbuf_size = 0;
540 conn->cfg.num_writes = 0;
541 if (conn->cfg.verbose)
543 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
544 hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
546 conn->write (conn, &conn->cfg, sizeof (conn->cfg));
550 switch (rx_cfg->test)
552 case HS_TEST_TYPE_NONE:
553 case HS_TEST_TYPE_ECHO:
554 sync_config_and_reply (conn, rx_cfg);
557 case HS_TEST_TYPE_BI:
558 case HS_TEST_TYPE_UNI:
559 vts_test_cmd (wrk, conn, rx_cfg);
562 case HS_TEST_TYPE_EXIT:
563 vtinf ("Ctrl session fd %d closing!", conn->fd);
564 vts_session_cleanup (conn);
567 vts_wrk_cleanup_all (wrk);
568 vcl_server_main.ctrl = 0;
572 vtwrn ("Unknown test type %d", rx_cfg->test);
573 hs_test_cfg_dump (rx_cfg, 0 /* is_client */);
581 vts_worker_init (vcl_test_server_worker_t * wrk)
583 vcl_test_server_main_t *vsm = &vcl_server_main;
584 vcl_test_main_t *vt = &vcl_test_main;
585 const vcl_test_proto_vft_t *tp;
586 struct epoll_event listen_ev;
589 __wrk_index = wrk->wrk_index;
591 vtinf ("Initializing worker ...");
593 conn_pool_expand (wrk, VCL_TEST_CFG_INIT_TEST_SESS + 1);
595 if (vppcom_worker_register ())
596 vtfail ("vppcom_worker_register()", 1);
598 tp = vt->protos[vsm->server_cfg.proto];
599 if ((rv = tp->listen (&wrk->listener, &vsm->server_cfg.endpt)))
600 vtfail ("proto listen", rv);
602 /* First worker already has epoll fd */
605 wrk->epfd = vppcom_epoll_create ();
607 vtfail ("vppcom_epoll_create()", wrk->epfd);
610 listen_ev.events = EPOLLET | EPOLLIN;
611 listen_ev.data.u32 = VCL_TEST_DATA_LISTENER;
613 vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listener.fd, &listen_ev);
615 vtfail ("vppcom_epoll_ctl", rv);
617 vsm->active_workers += 1;
618 vtinf ("Waiting for client data connections on port %d ...",
619 ntohs (vsm->server_cfg.endpt.port));
623 vts_conn_read (vcl_test_session_t *conn)
625 vcl_test_server_main_t *vsm = &vcl_server_main;
627 return vcl_test_read_ds (conn);
629 return conn->read (conn, conn->rxbuf, conn->rxbuf_size);
633 vts_inc_stats_check (vcl_test_session_t *ts)
635 /* Avoid checking time too often because of syscall cost */
636 if (ts->stats.rx_bytes - ts->old_stats.rx_bytes < 1 << 20)
639 clock_gettime (CLOCK_REALTIME, &ts->stats.stop);
640 if (vcl_test_time_diff (&ts->old_stats.stop, &ts->stats.stop) > 1)
642 vcl_test_stats_dump_inc (ts, 1 /* is_rx */);
643 ts->old_stats = ts->stats;
648 vts_worker_loop (void *arg)
650 struct epoll_event ep_evts[VCL_TEST_CFG_MAX_EPOLL_EVENTS];
651 vcl_test_server_main_t *vsm = &vcl_server_main;
652 vcl_test_server_worker_t *wrk = arg;
653 vcl_test_session_t *conn;
654 int i, rx_bytes, num_ev;
655 hs_test_cfg_t *rx_cfg;
658 vts_worker_init (wrk);
663 vppcom_epoll_wait (wrk->epfd, ep_evts, VCL_TEST_CFG_MAX_EPOLL_EVENTS,
664 0 /* poll session events */);
667 vterr ("vppcom_epoll_wait()", num_ev);
670 else if (num_ev == 0)
674 for (i = 0; i < num_ev; i++)
676 conn = &wrk->conn_pool[ep_evts[i].data.u32];
678 * Check for close events
680 if (ep_evts[i].events & (EPOLLHUP | EPOLLRDHUP))
682 if (conn == vsm->ctrl)
684 vtinf ("ctrl session went away");
687 vts_session_cleanup (conn);
693 * Check if new session needs to be accepted
696 if (!wrk->wrk_index && ep_evts[i].data.u32 == VCL_TEST_CTRL_LISTENER)
700 vtwrn ("ctrl already exists");
703 vsm->ctrl = vts_accept_ctrl (wrk, vsm->ctrl_listen_fd);
707 /* at this point ctrl session must be valid */
710 if (ep_evts[i].data.u32 == VCL_TEST_DATA_LISTENER)
712 conn = vts_accept_client (wrk, wrk->listener.fd);
713 conn->cfg = vsm->ctrl->cfg;
716 else if (vppcom_session_is_connectable_listener (conn->fd))
718 vts_accept_client (wrk, conn->fd);
723 * Message on control session
726 if (!wrk->wrk_index && conn->fd == vsm->ctrl->fd)
728 rx_bytes = conn->read (conn, conn->rxbuf, conn->rxbuf_size);
729 rx_cfg = (hs_test_cfg_t *) conn->rxbuf;
730 if (rx_cfg->magic == HS_TEST_CFG_CTRL_MAGIC)
732 vts_handle_ctrl_cfg (wrk, rx_cfg, conn, rx_bytes);
735 vtinf ("All client connections closed\n");
739 else if (isascii (conn->rxbuf[0]))
741 vts_server_echo (conn, rx_bytes);
745 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
751 * Read perf test data
754 if (EPOLLIN & ep_evts[i].events)
757 rx_bytes = vts_conn_read (conn);
761 if (errno == ECONNRESET)
763 vtinf ("Connection reset by remote peer.\n");
769 vts_server_process_rx (conn, rx_bytes);
770 if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0, 0) >
773 if (vsm->incremental_stats)
774 vts_inc_stats_check (conn);
779 vtwrn ("Unhandled event");
786 vsm->worker_fails -= 1;
789 vppcom_session_close (wrk->listener.fd);
791 free (wrk->conn_pool);
792 vsm->active_workers -= 1;
797 vts_ctrl_session_init (vcl_test_server_worker_t *wrk)
799 vcl_test_server_main_t *vsm = &vcl_server_main;
800 struct epoll_event listen_ev;
803 vtinf ("Initializing main ctrl session ...");
805 vsm->ctrl_listen_fd =
806 vppcom_session_create (VPPCOM_PROTO_TCP, 0 /* is_nonblocking */);
807 if (vsm->ctrl_listen_fd < 0)
808 vtfail ("vppcom_session_create()", vsm->ctrl_listen_fd);
810 rv = vppcom_session_bind (vsm->ctrl_listen_fd, &vsm->server_cfg.endpt);
812 vtfail ("vppcom_session_bind()", rv);
814 rv = vppcom_session_listen (vsm->ctrl_listen_fd, 10);
816 vtfail ("vppcom_session_listen()", rv);
818 wrk->epfd = vppcom_epoll_create ();
820 vtfail ("vppcom_epoll_create()", wrk->epfd);
822 listen_ev.events = EPOLLET | EPOLLIN;
823 listen_ev.data.u32 = VCL_TEST_CTRL_LISTENER;
824 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, vsm->ctrl_listen_fd,
827 vtfail ("vppcom_epoll_ctl", rv);
829 vtinf ("Waiting for client ctrl connection on port %d ...",
830 vsm->server_cfg.port);
834 main (int argc, char **argv)
836 vcl_test_server_main_t *vsm = &vcl_server_main;
837 vcl_test_main_t *vt = &vcl_test_main;
840 clib_mem_init_thread_safe (0, 64 << 20);
841 vsm->server_cfg.port = VCL_TEST_SERVER_PORT;
842 vsm->server_cfg.workers = 1;
843 vsm->active_workers = 0;
844 vcl_test_server_process_opts (vsm, argc, argv);
846 rv = vppcom_app_create ("vcl_test_server");
848 vtfail ("vppcom_app_create()", rv);
850 /* Protos like tls/dtls/quic need init */
851 if (vt->protos[vsm->server_cfg.proto]->init)
852 vt->protos[vsm->server_cfg.proto]->init (0);
854 vsm->workers = calloc (vsm->server_cfg.workers, sizeof (*vsm->workers));
855 vts_ctrl_session_init (&vsm->workers[0]);
857 /* Update ctrl port to data port */
858 vsm->server_cfg.endpt.port = hs_make_data_port (vsm->server_cfg.endpt.port);
859 vts_worker_init (&vsm->workers[0]);
860 for (i = 1; i < vsm->server_cfg.workers; i++)
862 vsm->workers[i].wrk_index = i;
863 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
864 vts_worker_loop, (void *) &vsm->workers[i]);
866 vtfail ("pthread_create()", rv);
869 vts_worker_loop (&vsm->workers[0]);
871 while (vsm->active_workers > 0)
874 vppcom_app_destroy ();
877 return vsm->worker_fails;
881 * fd.io coding-style-patch-verification: ON
884 * eval: (c-set-style "gnu")