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;
43 vcl_test_session_t listener;
45 struct epoll_event wait_events[VCL_TEST_CFG_MAX_EPOLL_EVENTS];
46 size_t conn_pool_size;
47 vcl_test_session_t *conn_pool;
49 pthread_t thread_handle;
50 } vcl_test_server_worker_t;
54 vcl_test_server_cfg_t cfg;
55 vcl_test_server_worker_t *workers;
56 vcl_test_session_t *ctrl;
58 struct sockaddr_storage servaddr;
59 volatile int worker_fails;
60 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));
82 vcl_test_cfg_init (&conn->cfg);
83 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */,
84 (uint8_t **) &conn->rxbuf, &conn->rxbuf_size);
85 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
88 wrk->conn_pool = conn_pool;
89 wrk->conn_pool_size = new_size;
93 vterr ("conn_pool_expand()", -errno);
97 static inline vcl_test_session_t *
98 conn_pool_alloc (vcl_test_server_worker_t *wrk)
103 for (i = 0; i < wrk->conn_pool_size; i++)
105 if (!wrk->conn_pool[i].is_alloc)
107 wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
108 wrk->conn_pool[i].is_alloc = 1;
109 wrk->conn_pool[i].session_index = i;
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 *conn)
132 sync_config_and_reply (vcl_test_session_t *conn, vcl_test_cfg_t *rx_cfg)
135 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */, (uint8_t **) &conn->rxbuf,
137 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
139 if (conn->cfg.verbose)
141 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
142 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
144 (void) vcl_test_write (conn, &conn->cfg, sizeof (conn->cfg));
148 vts_session_close (vcl_test_session_t *conn)
150 vcl_test_server_main_t *vsm = &vcl_server_main;
151 vcl_test_main_t *vt = &vcl_test_main;
156 if (vt->protos[vsm->cfg.proto]->close)
157 vt->protos[vsm->cfg.proto]->close (conn);
159 vppcom_session_close (conn->fd);
164 vts_session_cleanup (vcl_test_session_t *conn)
166 vts_session_close (conn);
167 conn_pool_free (conn);
171 vts_wrk_cleanup_all (vcl_test_server_worker_t *wrk)
173 vcl_test_session_t *conn;
176 for (i = 0; i < wrk->conn_pool_size; i++)
178 conn = &wrk->conn_pool[i];
179 vts_session_cleanup (conn);
186 vts_test_cmd (vcl_test_server_worker_t *wrk, vcl_test_session_t *conn,
187 vcl_test_cfg_t *rx_cfg)
189 u8 is_bi = rx_cfg->test == VCL_TEST_TYPE_BI;
190 vcl_test_session_t *tc;
194 if (rx_cfg->cmd == VCL_TEST_CMD_STOP)
196 struct timespec stop;
197 clock_gettime (CLOCK_REALTIME, &stop);
199 /* Test session are not closed, e.g., connection-less or errors */
202 vtinf ("%u sessions are still open", wrk->nfds - 1);
203 stop.tv_sec -= VCL_TEST_DELAY_DISCONNECT;
204 conn->stats.stop = stop;
207 /* Accumulate stats over all of the worker's sessions */
208 for (i = 0; i < wrk->conn_pool_size; i++)
210 tc = &wrk->conn_pool[i];
214 vcl_test_stats_accumulate (&conn->stats, &tc->stats);
217 vts_session_close (tc);
220 /* Only relevant if all connections previously closed */
221 if (vcl_comp_tspec (&conn->stats.stop, &tc->stats.stop) < 0)
222 conn->stats.stop = tc->stats.stop;
225 if (conn->cfg.verbose)
227 snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS", conn->fd);
228 vcl_test_stats_dump (buf, &conn->stats, 1 /* show_rx */,
229 is_bi /* show tx */, conn->cfg.verbose);
232 vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
233 is_bi /* show_tx */ , conn->cfg.verbose);
234 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
235 if (conn->cfg.verbose)
237 vtinf (" vcl server main\n" VCL_TEST_SEPARATOR_STRING
239 " buf size: %u (0x%08x)\n" VCL_TEST_SEPARATOR_STRING,
240 conn->rxbuf, conn->rxbuf_size, conn->rxbuf_size);
243 sync_config_and_reply (conn, rx_cfg);
244 memset (&conn->stats, 0, sizeof (conn->stats));
246 else if (rx_cfg->cmd == VCL_TEST_CMD_SYNC)
248 rx_cfg->ctrl_handle = conn->fd;
249 vtinf ("Set control fd %d for test!", conn->fd);
250 sync_config_and_reply (conn, rx_cfg);
252 else if (rx_cfg->cmd == VCL_TEST_CMD_START)
254 vtinf ("Starting %s-directional Stream Test (fd %d)!",
255 is_bi ? "Bi" : "Uni", conn->fd);
256 rx_cfg->ctrl_handle = conn->fd;
257 sync_config_and_reply (conn, rx_cfg);
259 /* read the 1st chunk, record start time */
260 memset (&conn->stats, 0, sizeof (conn->stats));
261 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
266 vts_server_process_rx (vcl_test_session_t *conn, int rx_bytes)
268 vcl_test_server_main_t *vsm = &vcl_server_main;
270 if (conn->cfg.test == VCL_TEST_TYPE_BI)
274 (void) vcl_test_write (conn, conn->ds[0].data, conn->ds[0].len);
276 (void) vcl_test_write (conn, conn->ds[1].data, conn->ds[1].len);
279 (void) vcl_test_write (conn, conn->rxbuf, rx_bytes);
283 vppcom_session_free_segments (conn->fd, rx_bytes);
285 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
286 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
290 vts_server_echo (vcl_test_session_t *conn, int rx_bytes)
292 int tx_bytes, nbytes, pos;
294 /* If it looks vaguely like a string, make sure it's terminated */
295 pos = rx_bytes < conn->rxbuf_size ? rx_bytes : conn->rxbuf_size - 1;
296 ((char *) conn->rxbuf)[pos] = 0;
297 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->rxbuf);
299 if (conn->cfg.verbose)
300 vtinf ("(fd %d): Echoing back", conn->fd);
302 nbytes = strlen ((const char *) conn->rxbuf) + 1;
303 tx_bytes = conn->write (conn, conn->rxbuf, nbytes);
305 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->rxbuf);
308 static vcl_test_session_t *
309 vts_accept_client (vcl_test_server_worker_t *wrk, int listen_fd)
311 vcl_test_server_main_t *vsm = &vcl_server_main;
312 const vcl_test_proto_vft_t *tp;
313 vcl_test_session_t *conn;
314 struct epoll_event ev;
317 conn = conn_pool_alloc (wrk);
320 vtwrn ("No free connections!");
324 tp = vcl_test_main.protos[vsm->cfg.proto];
325 if (tp->accept (listen_fd, conn))
328 vtinf ("Got a connection -- fd = %d (0x%08x) on listener fd = %d (0x%08x)",
329 conn->fd, conn->fd, listen_fd, listen_fd);
332 ev.data.u64 = conn - wrk->conn_pool;
333 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, conn->fd, &ev);
336 vterr ("vppcom_epoll_ctl()", rv);
345 print_usage_and_exit (void)
348 "vcl_test_server [OPTIONS] <port>\n"
350 " -h Print this message and exit.\n"
352 " -w <num> Number of workers\n"
353 " -p <PROTO> Use <PROTO> transport layer\n"
354 " -D Use UDP transport layer\n"
355 " -L Use TLS transport layer\n");
360 vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm)
362 struct sockaddr_storage *servaddr = &vsm->servaddr;
363 memset (servaddr, 0, sizeof (*servaddr));
365 if (vsm->cfg.address_ip6)
367 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
368 server_addr->sin6_family = AF_INET6;
369 server_addr->sin6_addr = in6addr_any;
370 server_addr->sin6_port = htons (vsm->cfg.port);
374 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
375 server_addr->sin_family = AF_INET;
376 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
377 server_addr->sin_port = htons (vsm->cfg.port);
380 if (vsm->cfg.address_ip6)
382 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
383 vsm->cfg.endpt.is_ip4 = 0;
384 vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
385 vsm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
389 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
390 vsm->cfg.endpt.is_ip4 = 1;
391 vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
392 vsm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
397 vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc,
402 vsm->cfg.proto = VPPCOM_PROTO_TCP;
405 while ((c = getopt (argc, argv, "6DLsw:hp:")) != -1)
409 vsm->cfg.address_ip6 = 1;
413 if (vppcom_unformat_proto (&vsm->cfg.proto, optarg))
414 vtwrn ("Invalid vppcom protocol %s, defaulting to TCP", optarg);
418 vsm->cfg.proto = VPPCOM_PROTO_UDP;
422 vsm->cfg.proto = VPPCOM_PROTO_TLS;
428 vsm->cfg.workers = v;
430 vtwrn ("Invalid number of workers %d", v);
440 vtwrn ("Option `-%c' requires an argument.", optopt);
443 if (isprint (optopt))
444 vtwrn ("Unknown option `-%c'.", optopt);
446 vtwrn ("Unknown option character `\\x%x'.", optopt);
451 print_usage_and_exit ();
454 if (argc < (optind + 1))
456 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
457 print_usage_and_exit ();
460 if (sscanf (argv[optind], "%d", &v) == 1)
461 vsm->cfg.port = (uint16_t) v;
464 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
465 print_usage_and_exit ();
468 vcl_test_init_endpoint_addr (vsm);
472 vts_handle_ctrl_cfg (vcl_test_server_worker_t *wrk, vcl_test_cfg_t *rx_cfg,
473 vcl_test_session_t *conn, int rx_bytes)
477 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
478 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
481 if (rx_bytes != sizeof (*rx_cfg))
483 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
484 rx_bytes, sizeof (*rx_cfg));
485 conn->cfg.rxbuf_size = 0;
486 conn->cfg.num_writes = 0;
487 if (conn->cfg.verbose)
489 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
490 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
492 conn->write (conn, &conn->cfg, sizeof (conn->cfg));
496 switch (rx_cfg->test)
498 case VCL_TEST_TYPE_NONE:
499 case VCL_TEST_TYPE_ECHO:
500 sync_config_and_reply (conn, rx_cfg);
503 case VCL_TEST_TYPE_BI:
504 case VCL_TEST_TYPE_UNI:
505 vts_test_cmd (wrk, conn, rx_cfg);
508 case VCL_TEST_TYPE_EXIT:
509 vtinf ("Ctrl session fd %d closing!", conn->fd);
510 vts_session_cleanup (conn);
513 vts_wrk_cleanup_all (wrk);
517 vtwrn ("Unknown test type %d", rx_cfg->test);
518 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
526 vts_worker_init (vcl_test_server_worker_t * wrk)
528 vcl_test_server_main_t *vsm = &vcl_server_main;
529 vcl_test_main_t *vt = &vcl_test_main;
530 const vcl_test_proto_vft_t *tp;
531 struct epoll_event listen_ev;
534 __wrk_index = wrk->wrk_index;
536 vtinf ("Initializing worker ...");
538 conn_pool_expand (wrk, VCL_TEST_CFG_MAX_TEST_SESS + 1);
540 if (vppcom_worker_register ())
541 vtfail ("vppcom_worker_register()", 1);
543 tp = vt->protos[vsm->cfg.proto];
544 if ((rv = tp->listen (&wrk->listener, &vsm->cfg.endpt)))
545 vtfail ("proto listen", rv);
547 /* First worker already has epoll fd */
550 wrk->epfd = vppcom_epoll_create ();
552 vtfail ("vppcom_epoll_create()", wrk->epfd);
555 listen_ev.events = EPOLLIN;
556 listen_ev.data.u32 = VCL_TEST_DATA_LISTENER;
558 vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listener.fd, &listen_ev);
560 vtfail ("vppcom_epoll_ctl", rv);
562 vsm->active_workers += 1;
563 vtinf ("Waiting for client data connections on port %d ...",
564 ntohs (vsm->cfg.endpt.port));
568 vts_conn_read (vcl_test_session_t *conn)
570 vcl_test_server_main_t *vsm = &vcl_server_main;
572 return vcl_test_read_ds (conn);
574 return conn->read (conn, conn->rxbuf, conn->rxbuf_size);
578 vts_worker_loop (void *arg)
580 vcl_test_server_main_t *vsm = &vcl_server_main;
581 vcl_test_server_worker_t *wrk = arg;
582 vcl_test_session_t *conn;
583 int i, rx_bytes, num_ev;
584 vcl_test_cfg_t *rx_cfg;
587 vts_worker_init (wrk);
591 num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
592 VCL_TEST_CFG_MAX_EPOLL_EVENTS, 60000.0);
595 vterr ("vppcom_epoll_wait()", num_ev);
598 else if (num_ev == 0)
600 vtinf ("vppcom_epoll_wait() timeout!");
603 for (i = 0; i < num_ev; i++)
605 conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
607 * Check for close events
609 if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
611 vts_session_close (conn);
615 vtinf ("All client connections closed\n");
622 * Check if new session needs to be accepted
625 if (wrk->wait_events[i].data.u32 == VCL_TEST_CTRL_LISTENER)
629 vtwrn ("ctrl already exists");
632 vsm->ctrl = vts_accept_client (wrk, vsm->ctrl_listen_fd);
635 if (wrk->wait_events[i].data.u32 == VCL_TEST_DATA_LISTENER)
637 conn = vts_accept_client (wrk, wrk->listener.fd);
638 conn->cfg = vsm->ctrl->cfg;
641 else if (vppcom_session_is_connectable_listener (conn->fd))
643 vts_accept_client (wrk, conn->fd);
648 * Message on control session
651 if (!wrk->wrk_index && conn->fd == vsm->ctrl->fd)
653 rx_bytes = conn->read (conn, conn->rxbuf, conn->rxbuf_size);
654 rx_cfg = (vcl_test_cfg_t *) conn->rxbuf;
655 if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
657 vts_handle_ctrl_cfg (wrk, rx_cfg, conn, rx_bytes);
660 vtinf ("All client connections closed\n");
664 else if (isascii (conn->rxbuf[0]))
666 vts_server_echo (conn, rx_bytes);
670 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
676 * Read perf test data
679 if (EPOLLIN & wrk->wait_events[i].events)
682 rx_bytes = vts_conn_read (conn);
686 if (errno == ECONNRESET)
688 vtinf ("Connection reset by remote peer.\n");
694 vts_server_process_rx (conn, rx_bytes);
695 if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0, 0) >
702 vtwrn ("Unhandled event");
709 vsm->worker_fails -= 1;
712 vppcom_session_close (wrk->listener.fd);
714 free (wrk->conn_pool);
715 vsm->active_workers -= 1;
720 vts_ctrl_session_init (vcl_test_server_worker_t *wrk)
722 vcl_test_server_main_t *vsm = &vcl_server_main;
723 struct epoll_event listen_ev;
726 vtinf ("Initializing main ctrl session ...");
728 vsm->ctrl_listen_fd =
729 vppcom_session_create (VPPCOM_PROTO_TCP, 0 /* is_nonblocking */);
730 if (vsm->ctrl_listen_fd < 0)
731 vtfail ("vppcom_session_create()", vsm->ctrl_listen_fd);
733 rv = vppcom_session_bind (vsm->ctrl_listen_fd, &vsm->cfg.endpt);
735 vtfail ("vppcom_session_bind()", rv);
737 rv = vppcom_session_listen (vsm->ctrl_listen_fd, 10);
739 vtfail ("vppcom_session_listen()", rv);
741 wrk->epfd = vppcom_epoll_create ();
743 vtfail ("vppcom_epoll_create()", wrk->epfd);
745 listen_ev.events = EPOLLIN;
746 listen_ev.data.u32 = VCL_TEST_CTRL_LISTENER;
747 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, vsm->ctrl_listen_fd,
750 vtfail ("vppcom_epoll_ctl", rv);
752 vtinf ("Waiting for client ctrl connection on port %d ...", vsm->cfg.port);
756 main (int argc, char **argv)
758 vcl_test_server_main_t *vsm = &vcl_server_main;
759 vcl_test_main_t *vt = &vcl_test_main;
762 clib_mem_init_thread_safe (0, 64 << 20);
763 vsm->cfg.port = VCL_TEST_SERVER_PORT;
764 vsm->cfg.workers = 1;
765 vsm->active_workers = 0;
766 vcl_test_server_process_opts (vsm, argc, argv);
768 rv = vppcom_app_create ("vcl_test_server");
770 vtfail ("vppcom_app_create()", rv);
772 /* Protos like tls/dtls/quic need init */
773 if (vt->protos[vsm->cfg.proto]->init)
774 vt->protos[vsm->cfg.proto]->init (0);
776 vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers));
777 vts_ctrl_session_init (&vsm->workers[0]);
779 /* Update ctrl port to data port */
780 vsm->cfg.endpt.port += 1;
781 vts_worker_init (&vsm->workers[0]);
782 for (i = 1; i < vsm->cfg.workers; i++)
784 vsm->workers[i].wrk_index = i;
785 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
786 vts_worker_loop, (void *) &vsm->workers[i]);
789 vts_worker_loop (&vsm->workers[0]);
791 while (vsm->active_workers > 0)
794 vppcom_app_destroy ();
797 return vsm->worker_fails;
801 * fd.io coding-style-patch-verification: ON
804 * eval: (c-set-style "gnu")