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;
61 } vcl_test_server_main_t;
63 vcl_test_main_t vcl_test_main;
65 static vcl_test_server_main_t vcl_server_main;
68 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
70 vcl_test_session_t *conn_pool;
71 size_t new_size = wrk->conn_pool_size + expand_size;
74 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
77 for (i = wrk->conn_pool_size; i < new_size; i++)
79 vcl_test_session_t *conn = &conn_pool[i];
80 memset (conn, 0, sizeof (*conn));
83 wrk->conn_pool = conn_pool;
84 wrk->conn_pool_size = new_size;
88 vterr ("conn_pool_expand()", -errno);
92 static inline vcl_test_session_t *
93 conn_pool_alloc (vcl_test_server_worker_t *wrk)
95 vcl_test_session_t *conn;
99 for (i = 0; i < wrk->conn_pool_size; i++)
101 if (!wrk->conn_pool[i].is_alloc)
103 conn = &wrk->conn_pool[i];
104 conn->endpt.ip = wrk->conn_pool[i].ip;
106 conn->session_index = i;
107 memset (&conn->stats, 0, sizeof (vcl_test_stats_t));
108 vcl_test_cfg_init (&conn->cfg);
109 return (&wrk->conn_pool[i]);
115 conn_pool_expand (wrk, 2 * wrk->conn_pool_size);
119 vtwrn ("Failed to allocate connection even after expand");
124 conn_pool_free (vcl_test_session_t *ts)
128 vcl_test_session_buf_free (ts);
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->server_cfg.proto]->close)
157 vt->protos[vsm->server_cfg.proto]->close (conn);
159 vppcom_session_close (conn->fd);
164 vts_session_cleanup (vcl_test_session_t *ts)
166 vts_session_close (ts);
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_cleanup (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!");
325 conn->cfg = vsm->ctrl->cfg;
326 vcl_test_session_buf_alloc (conn);
328 tp = vcl_test_main.protos[vsm->server_cfg.proto];
329 if (tp->accept (listen_fd, conn))
332 vtinf ("Got a connection -- fd = %d (0x%08x) on listener fd = %d (0x%08x)",
333 conn->fd, conn->fd, listen_fd, listen_fd);
336 ev.data.u64 = conn - wrk->conn_pool;
337 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, conn->fd, &ev);
340 vterr ("vppcom_epoll_ctl()", rv);
349 print_usage_and_exit (void)
352 "vcl_test_server [OPTIONS] <port>\n"
354 " -h Print this message and exit.\n"
356 " -w <num> Number of workers\n"
357 " -p <PROTO> Use <PROTO> transport layer\n"
358 " -D Use UDP transport layer\n"
359 " -L Use TLS transport layer\n");
364 vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm)
366 struct sockaddr_storage *servaddr = &vsm->servaddr;
367 memset (servaddr, 0, sizeof (*servaddr));
369 if (vsm->server_cfg.address_ip6)
371 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
372 server_addr->sin6_family = AF_INET6;
373 server_addr->sin6_addr = in6addr_any;
374 server_addr->sin6_port = htons (vsm->server_cfg.port);
378 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
379 server_addr->sin_family = AF_INET;
380 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
381 server_addr->sin_port = htons (vsm->server_cfg.port);
384 if (vsm->server_cfg.address_ip6)
386 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
387 vsm->server_cfg.endpt.is_ip4 = 0;
388 vsm->server_cfg.endpt.ip = (uint8_t *) &server_addr->sin6_addr;
389 vsm->server_cfg.endpt.port = (uint16_t) server_addr->sin6_port;
393 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
394 vsm->server_cfg.endpt.is_ip4 = 1;
395 vsm->server_cfg.endpt.ip = (uint8_t *) &server_addr->sin_addr;
396 vsm->server_cfg.endpt.port = (uint16_t) server_addr->sin_port;
401 vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc,
406 vsm->server_cfg.proto = VPPCOM_PROTO_TCP;
409 while ((c = getopt (argc, argv, "6DLsw:hp:")) != -1)
413 vsm->server_cfg.address_ip6 = 1;
417 if (vppcom_unformat_proto (&vsm->server_cfg.proto, optarg))
418 vtwrn ("Invalid vppcom protocol %s, defaulting to TCP", optarg);
422 vsm->server_cfg.proto = VPPCOM_PROTO_UDP;
426 vsm->server_cfg.proto = VPPCOM_PROTO_TLS;
432 vsm->server_cfg.workers = v;
434 vtwrn ("Invalid number of workers %d", v);
444 vtwrn ("Option `-%c' requires an argument.", optopt);
447 if (isprint (optopt))
448 vtwrn ("Unknown option `-%c'.", optopt);
450 vtwrn ("Unknown option character `\\x%x'.", optopt);
455 print_usage_and_exit ();
458 if (argc < (optind + 1))
460 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
461 print_usage_and_exit ();
464 if (sscanf (argv[optind], "%d", &v) == 1)
465 vsm->server_cfg.port = (uint16_t) v;
468 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
469 print_usage_and_exit ();
472 vcl_test_init_endpoint_addr (vsm);
476 vts_handle_ctrl_cfg (vcl_test_server_worker_t *wrk, vcl_test_cfg_t *rx_cfg,
477 vcl_test_session_t *conn, int rx_bytes)
481 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
482 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
485 if (rx_bytes != sizeof (*rx_cfg))
487 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
488 rx_bytes, sizeof (*rx_cfg));
489 conn->cfg.rxbuf_size = 0;
490 conn->cfg.num_writes = 0;
491 if (conn->cfg.verbose)
493 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
494 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
496 conn->write (conn, &conn->cfg, sizeof (conn->cfg));
500 switch (rx_cfg->test)
502 case VCL_TEST_TYPE_NONE:
503 case VCL_TEST_TYPE_ECHO:
504 sync_config_and_reply (conn, rx_cfg);
507 case VCL_TEST_TYPE_BI:
508 case VCL_TEST_TYPE_UNI:
509 vts_test_cmd (wrk, conn, rx_cfg);
512 case VCL_TEST_TYPE_EXIT:
513 vtinf ("Ctrl session fd %d closing!", conn->fd);
514 vts_session_cleanup (conn);
517 vts_wrk_cleanup_all (wrk);
521 vtwrn ("Unknown test type %d", rx_cfg->test);
522 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
530 vts_worker_init (vcl_test_server_worker_t * wrk)
532 vcl_test_server_main_t *vsm = &vcl_server_main;
533 vcl_test_main_t *vt = &vcl_test_main;
534 const vcl_test_proto_vft_t *tp;
535 struct epoll_event listen_ev;
538 __wrk_index = wrk->wrk_index;
540 vtinf ("Initializing worker ...");
542 conn_pool_expand (wrk, VCL_TEST_CFG_MAX_TEST_SESS + 1);
544 if (vppcom_worker_register ())
545 vtfail ("vppcom_worker_register()", 1);
547 tp = vt->protos[vsm->server_cfg.proto];
548 if ((rv = tp->listen (&wrk->listener, &vsm->server_cfg.endpt)))
549 vtfail ("proto listen", rv);
551 /* First worker already has epoll fd */
554 wrk->epfd = vppcom_epoll_create ();
556 vtfail ("vppcom_epoll_create()", wrk->epfd);
559 listen_ev.events = EPOLLIN;
560 listen_ev.data.u32 = VCL_TEST_DATA_LISTENER;
562 vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listener.fd, &listen_ev);
564 vtfail ("vppcom_epoll_ctl", rv);
566 vsm->active_workers += 1;
567 vtinf ("Waiting for client data connections on port %d ...",
568 ntohs (vsm->server_cfg.endpt.port));
572 vts_conn_read (vcl_test_session_t *conn)
574 vcl_test_server_main_t *vsm = &vcl_server_main;
576 return vcl_test_read_ds (conn);
578 return conn->read (conn, conn->rxbuf, conn->rxbuf_size);
582 vts_worker_loop (void *arg)
584 struct epoll_event ep_evts[VCL_TEST_CFG_MAX_EPOLL_EVENTS];
585 vcl_test_server_main_t *vsm = &vcl_server_main;
586 vcl_test_server_worker_t *wrk = arg;
587 vcl_test_session_t *conn;
588 int i, rx_bytes, num_ev;
589 vcl_test_cfg_t *rx_cfg;
592 vts_worker_init (wrk);
597 vppcom_epoll_wait (wrk->epfd, ep_evts, VCL_TEST_CFG_MAX_EPOLL_EVENTS,
598 0 /* poll session events */);
601 vterr ("vppcom_epoll_wait()", num_ev);
604 else if (num_ev == 0)
608 for (i = 0; i < num_ev; i++)
610 conn = &wrk->conn_pool[ep_evts[i].data.u32];
612 * Check for close events
614 if (ep_evts[i].events & (EPOLLHUP | EPOLLRDHUP))
616 vts_session_cleanup (conn);
620 vtinf ("All client connections closed\n");
627 * Check if new session needs to be accepted
630 if (!wrk->wrk_index && ep_evts[i].data.u32 == VCL_TEST_CTRL_LISTENER)
634 vtwrn ("ctrl already exists");
637 vsm->ctrl = vts_accept_client (wrk, vsm->ctrl_listen_fd);
640 if (ep_evts[i].data.u32 == VCL_TEST_DATA_LISTENER)
642 conn = vts_accept_client (wrk, wrk->listener.fd);
643 conn->cfg = vsm->ctrl->cfg;
646 else if (vppcom_session_is_connectable_listener (conn->fd))
648 vts_accept_client (wrk, conn->fd);
653 * Message on control session
656 if (!wrk->wrk_index && conn->fd == vsm->ctrl->fd)
658 rx_bytes = conn->read (conn, conn->rxbuf, conn->rxbuf_size);
659 rx_cfg = (vcl_test_cfg_t *) conn->rxbuf;
660 if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
662 vts_handle_ctrl_cfg (wrk, rx_cfg, conn, rx_bytes);
665 vtinf ("All client connections closed\n");
669 else if (isascii (conn->rxbuf[0]))
671 vts_server_echo (conn, rx_bytes);
675 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
681 * Read perf test data
684 if (EPOLLIN & ep_evts[i].events)
687 rx_bytes = vts_conn_read (conn);
691 if (errno == ECONNRESET)
693 vtinf ("Connection reset by remote peer.\n");
699 vts_server_process_rx (conn, rx_bytes);
700 if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0, 0) >
707 vtwrn ("Unhandled event");
714 vsm->worker_fails -= 1;
717 vppcom_session_close (wrk->listener.fd);
719 free (wrk->conn_pool);
720 vsm->active_workers -= 1;
725 vts_ctrl_session_init (vcl_test_server_worker_t *wrk)
727 vcl_test_server_main_t *vsm = &vcl_server_main;
728 struct epoll_event listen_ev;
731 vtinf ("Initializing main ctrl session ...");
733 vsm->ctrl_listen_fd =
734 vppcom_session_create (VPPCOM_PROTO_TCP, 0 /* is_nonblocking */);
735 if (vsm->ctrl_listen_fd < 0)
736 vtfail ("vppcom_session_create()", vsm->ctrl_listen_fd);
738 rv = vppcom_session_bind (vsm->ctrl_listen_fd, &vsm->server_cfg.endpt);
740 vtfail ("vppcom_session_bind()", rv);
742 rv = vppcom_session_listen (vsm->ctrl_listen_fd, 10);
744 vtfail ("vppcom_session_listen()", rv);
746 wrk->epfd = vppcom_epoll_create ();
748 vtfail ("vppcom_epoll_create()", wrk->epfd);
750 listen_ev.events = EPOLLIN;
751 listen_ev.data.u32 = VCL_TEST_CTRL_LISTENER;
752 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, vsm->ctrl_listen_fd,
755 vtfail ("vppcom_epoll_ctl", rv);
757 vtinf ("Waiting for client ctrl connection on port %d ...",
758 vsm->server_cfg.port);
762 main (int argc, char **argv)
764 vcl_test_server_main_t *vsm = &vcl_server_main;
765 vcl_test_main_t *vt = &vcl_test_main;
768 clib_mem_init_thread_safe (0, 64 << 20);
769 vsm->server_cfg.port = VCL_TEST_SERVER_PORT;
770 vsm->server_cfg.workers = 1;
771 vsm->active_workers = 0;
772 vcl_test_server_process_opts (vsm, argc, argv);
774 rv = vppcom_app_create ("vcl_test_server");
776 vtfail ("vppcom_app_create()", rv);
778 /* Protos like tls/dtls/quic need init */
779 if (vt->protos[vsm->server_cfg.proto]->init)
780 vt->protos[vsm->server_cfg.proto]->init (0);
782 vsm->workers = calloc (vsm->server_cfg.workers, sizeof (*vsm->workers));
783 vts_ctrl_session_init (&vsm->workers[0]);
785 /* Update ctrl port to data port */
786 vsm->server_cfg.endpt.port += 1;
787 vts_worker_init (&vsm->workers[0]);
788 for (i = 1; i < vsm->server_cfg.workers; i++)
790 vsm->workers[i].wrk_index = i;
791 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
792 vts_worker_loop, (void *) &vsm->workers[i]);
795 vts_worker_loop (&vsm->workers[0]);
797 while (vsm->active_workers > 0)
800 vppcom_app_destroy ();
803 return vsm->worker_fails;
807 * fd.io coding-style-patch-verification: ON
810 * eval: (c-set-style "gnu")