2 * Copyright (c) 2017-2019 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>
39 vcl_test_stats_t stats;
42 vppcom_data_segment_t ds[2];
43 } vcl_test_server_conn_t;
52 } vcl_test_server_cfg_t;
59 struct epoll_event wait_events[VCL_TEST_CFG_MAX_EPOLL_EVENTS];
60 size_t conn_pool_size;
61 vcl_test_server_conn_t *conn_pool;
63 pthread_t thread_handle;
64 } vcl_test_server_worker_t;
68 vcl_test_server_cfg_t cfg;
69 vcl_test_server_worker_t *workers;
71 vcl_test_server_conn_t *ctrl;
73 struct sockaddr_storage servaddr;
74 volatile int worker_fails;
75 volatile int active_workers;
77 } vcl_test_server_main_t;
79 static __thread int __wrk_index = 0;
81 static vcl_test_server_main_t vcl_server_main;
84 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
86 vcl_test_server_conn_t *conn_pool;
87 size_t new_size = wrk->conn_pool_size + expand_size;
90 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
93 for (i = wrk->conn_pool_size; i < new_size; i++)
95 vcl_test_server_conn_t *conn = &conn_pool[i];
96 memset (conn, 0, sizeof (*conn));
97 vcl_test_cfg_init (&conn->cfg);
98 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
99 &conn->buf, &conn->buf_size);
100 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
103 wrk->conn_pool = conn_pool;
104 wrk->conn_pool_size = new_size;
108 vterr ("conn_pool_expand()", -errno);
112 static inline vcl_test_server_conn_t *
113 conn_pool_alloc (vcl_test_server_worker_t * wrk)
118 for (i = 0; i < wrk->conn_pool_size; i++)
120 if (!wrk->conn_pool[i].is_alloc)
122 wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
123 wrk->conn_pool[i].is_alloc = 1;
124 return (&wrk->conn_pool[i]);
130 conn_pool_expand (wrk, 2 * wrk->conn_pool_size);
134 vtwrn ("Failed to allocate connection even after expand");
139 conn_pool_free (vcl_test_server_conn_t * conn)
146 sync_config_and_reply (vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg)
149 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
150 &conn->buf, &conn->buf_size);
151 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
153 if (conn->cfg.verbose)
155 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
156 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
158 (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
159 sizeof (conn->cfg), NULL, conn->cfg.verbose);
163 vts_session_close (vcl_test_server_conn_t *conn)
167 vppcom_session_close (conn->fd);
172 vts_session_cleanup (vcl_test_server_conn_t *conn)
174 vts_session_close (conn);
175 conn_pool_free (conn);
179 vts_wrk_cleanup_all (vcl_test_server_worker_t *wrk)
181 vcl_test_server_conn_t *conn;
184 for (i = 0; i < wrk->conn_pool_size; i++)
186 conn = &wrk->conn_pool[i];
187 vts_session_cleanup (conn);
194 vts_test_cmd (vcl_test_server_worker_t *wrk, vcl_test_server_conn_t *conn,
195 vcl_test_cfg_t *rx_cfg)
197 u8 is_bi = rx_cfg->test == VCL_TEST_TYPE_BI;
198 vcl_test_server_conn_t *tc;
202 if (rx_cfg->cmd == VCL_TEST_CMD_STOP)
204 struct timespec stop;
205 clock_gettime (CLOCK_REALTIME, &stop);
207 /* Test session are not closed, e.g., connection-less or errors */
210 vtinf ("%u sessions are still open", wrk->nfds - 1);
211 stop.tv_sec -= VCL_TEST_DELAY_DISCONNECT;
212 conn->stats.stop = stop;
215 /* Accumulate stats over all of the worker's sessions */
216 for (i = 0; i < wrk->conn_pool_size; i++)
218 tc = &wrk->conn_pool[i];
222 vcl_test_stats_accumulate (&conn->stats, &tc->stats);
225 vts_session_close (tc);
228 /* Only relevant if all connections previously closed */
229 if (vcl_comp_tspec (&conn->stats.stop, &tc->stats.stop) < 0)
230 conn->stats.stop = tc->stats.stop;
233 if (conn->cfg.verbose)
235 snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS", conn->fd);
236 vcl_test_stats_dump (buf, &conn->stats, 1 /* show_rx */,
237 is_bi /* show tx */, conn->cfg.verbose);
240 vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
241 is_bi /* show_tx */ , conn->cfg.verbose);
242 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
243 if (conn->cfg.verbose)
245 vtinf (" vcl server main\n"
246 VCL_TEST_SEPARATOR_STRING
248 " buf size: %u (0x%08x)\n"
249 VCL_TEST_SEPARATOR_STRING,
250 conn->buf, conn->buf_size, conn->buf_size);
253 sync_config_and_reply (conn, rx_cfg);
254 memset (&conn->stats, 0, sizeof (conn->stats));
256 else if (rx_cfg->cmd == VCL_TEST_CMD_SYNC)
258 rx_cfg->ctrl_handle = conn->fd;
259 vtinf ("Set control fd %d for test!", conn->fd);
260 sync_config_and_reply (conn, rx_cfg);
262 else if (rx_cfg->cmd == VCL_TEST_CMD_START)
264 vtinf ("Starting %s-directional Stream Test (fd %d)!",
265 is_bi ? "Bi" : "Uni", conn->fd);
266 rx_cfg->ctrl_handle = conn->fd;
267 sync_config_and_reply (conn, rx_cfg);
269 /* read the 1st chunk, record start time */
270 memset (&conn->stats, 0, sizeof (conn->stats));
271 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
276 vts_server_process_rx (vcl_test_server_conn_t *conn, int rx_bytes)
278 vcl_test_server_main_t *vsm = &vcl_server_main;
279 int client_fd = conn->fd;
281 if (conn->cfg.test == VCL_TEST_TYPE_BI)
285 (void) vcl_test_write (client_fd, conn->ds[0].data, conn->ds[0].len,
286 &conn->stats, conn->cfg.verbose);
288 (void) vcl_test_write (client_fd, conn->ds[1].data,
289 conn->ds[1].len, &conn->stats,
293 (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
298 vppcom_session_free_segments (conn->fd, rx_bytes);
300 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
301 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
305 vts_copy_ds (void *buf, vppcom_data_segment_t * ds, u32 max_bytes)
307 uint32_t n_bytes = 0, ds_idx = 0, to_copy;
309 while (n_bytes < max_bytes)
311 to_copy = clib_min (ds[ds_idx].len, max_bytes - n_bytes);
312 clib_memcpy_fast (buf + n_bytes, ds[ds_idx].data, to_copy);
319 vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes)
321 vcl_test_server_main_t *vsm = &vcl_server_main;
322 int tx_bytes, nbytes, pos;
325 vts_copy_ds (conn->buf, conn->ds, rx_bytes);
327 /* If it looks vaguely like a string, make sure it's terminated */
328 pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
329 ((char *) conn->buf)[pos] = 0;
330 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf);
332 if (conn->cfg.verbose)
333 vtinf ("(fd %d): Echoing back", conn->fd);
335 nbytes = strlen ((const char *) conn->buf) + 1;
336 tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
339 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf);
342 static vcl_test_server_conn_t *
343 vts_accept_client (vcl_test_server_worker_t *wrk, int listen_fd)
345 vcl_test_server_conn_t *conn;
346 struct epoll_event ev;
349 conn = conn_pool_alloc (wrk);
352 vtwrn ("No free connections!");
356 client_fd = vppcom_session_accept (listen_fd, &conn->endpt, 0);
359 vterr ("vppcom_session_accept()", client_fd);
362 conn->fd = client_fd;
365 vtinf ("Got a connection -- fd = %d (0x%08x) on listener fd = %d (0x%08x)",
366 client_fd, client_fd, listen_fd, listen_fd);
369 ev.data.u64 = conn - wrk->conn_pool;
370 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev);
373 vterr ("vppcom_epoll_ctl()", rv);
382 print_usage_and_exit (void)
385 "vcl_test_server [OPTIONS] <port>\n"
387 " -h Print this message and exit.\n"
389 " -w <num> Number of workers\n"
390 " -p <PROTO> Use <PROTO> transport layer\n"
391 " -D Use UDP transport layer\n"
392 " -L Use TLS transport layer\n");
397 vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm)
399 struct sockaddr_storage *servaddr = &vsm->servaddr;
400 memset (servaddr, 0, sizeof (*servaddr));
402 if (vsm->cfg.address_ip6)
404 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
405 server_addr->sin6_family = AF_INET6;
406 server_addr->sin6_addr = in6addr_any;
407 server_addr->sin6_port = htons (vsm->cfg.port);
411 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
412 server_addr->sin_family = AF_INET;
413 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
414 server_addr->sin_port = htons (vsm->cfg.port);
417 if (vsm->cfg.address_ip6)
419 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
420 vsm->cfg.endpt.is_ip4 = 0;
421 vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
422 vsm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
426 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
427 vsm->cfg.endpt.is_ip4 = 1;
428 vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
429 vsm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
434 vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc,
439 vsm->cfg.proto = VPPCOM_PROTO_TCP;
442 while ((c = getopt (argc, argv, "6DLsw:hp:")) != -1)
446 vsm->cfg.address_ip6 = 1;
450 if (vppcom_unformat_proto (&vsm->cfg.proto, optarg))
451 vtwrn ("Invalid vppcom protocol %s, defaulting to TCP", optarg);
455 vsm->cfg.proto = VPPCOM_PROTO_UDP;
459 vsm->cfg.proto = VPPCOM_PROTO_TLS;
465 vsm->cfg.workers = v;
467 vtwrn ("Invalid number of workers %d", v);
477 vtwrn ("Option `-%c' requires an argument.", optopt);
480 if (isprint (optopt))
481 vtwrn ("Unknown option `-%c'.", optopt);
483 vtwrn ("Unknown option character `\\x%x'.", optopt);
488 print_usage_and_exit ();
491 if (argc < (optind + 1))
493 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
494 print_usage_and_exit ();
497 if (sscanf (argv[optind], "%d", &v) == 1)
498 vsm->cfg.port = (uint16_t) v;
501 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
502 print_usage_and_exit ();
505 vcl_test_init_endpoint_addr (vsm);
509 vts_clean_connected_listeners (vcl_test_server_worker_t * wrk,
512 if ((vppcom_session_n_accepted (listener_fd) == 0) &
513 vppcom_session_is_connectable_listener (listener_fd))
515 vtinf ("Connected Listener fd %x has no more sessions", listener_fd);
516 vppcom_session_close (listener_fd);
522 vts_handle_ctrl_cfg (vcl_test_server_worker_t *wrk, vcl_test_cfg_t *rx_cfg,
523 vcl_test_server_conn_t *conn, int rx_bytes)
528 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
529 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
532 if (rx_bytes != sizeof (*rx_cfg))
534 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
535 rx_bytes, sizeof (*rx_cfg));
536 conn->cfg.rxbuf_size = 0;
537 conn->cfg.num_writes = 0;
538 if (conn->cfg.verbose)
540 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
541 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
543 vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
544 sizeof (conn->cfg), NULL, conn->cfg.verbose);
548 switch (rx_cfg->test)
550 case VCL_TEST_TYPE_NONE:
551 case VCL_TEST_TYPE_ECHO:
552 sync_config_and_reply (conn, rx_cfg);
555 case VCL_TEST_TYPE_BI:
556 case VCL_TEST_TYPE_UNI:
557 vts_test_cmd (wrk, conn, rx_cfg);
560 case VCL_TEST_TYPE_EXIT:
561 vtinf ("Ctrl session fd %d closing!", conn->fd);
562 listener_fd = vppcom_session_listener (conn->fd);
563 vts_clean_connected_listeners (wrk, listener_fd);
564 vts_session_cleanup (conn);
567 vts_wrk_cleanup_all (wrk);
571 vtwrn ("Unknown test type %d", rx_cfg->test);
572 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
580 vts_worker_init (vcl_test_server_worker_t * wrk)
582 vcl_test_server_main_t *vsm = &vcl_server_main;
583 struct epoll_event listen_ev;
586 __wrk_index = wrk->wrk_index;
588 vtinf ("Initializing worker ...");
590 conn_pool_expand (wrk, VCL_TEST_CFG_MAX_TEST_SESS + 1);
592 if (vppcom_worker_register ())
593 vtfail ("vppcom_worker_register()", 1);
595 wrk->listen_fd = vppcom_session_create (vsm->cfg.proto,
596 0 /* is_nonblocking */ );
597 if (wrk->listen_fd < 0)
598 vtfail ("vppcom_session_create()", wrk->listen_fd);
600 if (vsm->cfg.proto == VPPCOM_PROTO_UDP)
602 vppcom_session_attr (wrk->listen_fd, VPPCOM_ATTR_SET_CONNECTED, 0, 0);
605 if (vsm->cfg.proto == VPPCOM_PROTO_TLS ||
606 vsm->cfg.proto == VPPCOM_PROTO_QUIC ||
607 vsm->cfg.proto == VPPCOM_PROTO_DTLS)
609 vppcom_cert_key_pair_t ckpair;
613 vtinf ("Adding tls certs ...");
614 ckpair.cert = vcl_test_crt_rsa;
615 ckpair.key = vcl_test_key_rsa;
616 ckpair.cert_len = vcl_test_crt_rsa_len;
617 ckpair.key_len = vcl_test_key_rsa_len;
618 ckp_index = vppcom_add_cert_key_pair (&ckpair);
620 vtfail ("vppcom_add_cert_key_pair()", ckp_index);
622 ckp_len = sizeof (ckp_index);
623 vppcom_session_attr (wrk->listen_fd, VPPCOM_ATTR_SET_CKPAIR, &ckp_index,
627 rv = vppcom_session_bind (wrk->listen_fd, &vsm->cfg.endpt);
629 vtfail ("vppcom_session_bind()", rv);
631 if (!(vsm->cfg.proto == VPPCOM_PROTO_UDP))
633 rv = vppcom_session_listen (wrk->listen_fd, 10);
635 vtfail ("vppcom_session_listen()", rv);
638 /* First worker already has epoll fd */
641 wrk->epfd = vppcom_epoll_create ();
643 vtfail ("vppcom_epoll_create()", wrk->epfd);
646 listen_ev.events = EPOLLIN;
647 listen_ev.data.u32 = VCL_TEST_DATA_LISTENER;
648 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd,
651 vtfail ("vppcom_epoll_ctl", rv);
653 vsm->active_workers += 1;
654 vtinf ("Waiting for a client to connect on port %d ...", vsm->cfg.port);
658 vts_conn_read (vcl_test_server_conn_t * conn)
660 vcl_test_server_main_t *vsm = &vcl_server_main;
662 return vcl_test_read_ds (conn->fd, conn->ds, &conn->stats);
664 return vcl_test_read (conn->fd, conn->buf, conn->buf_size, &conn->stats);
668 vts_worker_loop (void *arg)
670 vcl_test_server_main_t *vsm = &vcl_server_main;
671 vcl_test_server_worker_t *wrk = arg;
672 vcl_test_server_conn_t *conn;
673 int i, rx_bytes, num_ev, listener_fd;
674 vcl_test_cfg_t *rx_cfg;
677 vts_worker_init (wrk);
681 num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
682 VCL_TEST_CFG_MAX_EPOLL_EVENTS, 60000.0);
685 vterr ("vppcom_epoll_wait()", num_ev);
688 else if (num_ev == 0)
690 vtinf ("vppcom_epoll_wait() timeout!");
693 for (i = 0; i < num_ev; i++)
695 conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
697 * Check for close events
699 if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
701 listener_fd = vppcom_session_listener (conn->fd);
702 vts_clean_connected_listeners (wrk, listener_fd);
703 vts_session_close (conn);
707 vtinf ("All client connections closed\n");
714 * Check if new session needs to be accepted
717 if (wrk->wait_events[i].data.u32 == VCL_TEST_CTRL_LISTENER)
721 vtwrn ("ctrl already exists");
724 vsm->ctrl = vts_accept_client (wrk, vsm->ctrl_listen_fd);
727 if (wrk->wait_events[i].data.u32 == VCL_TEST_DATA_LISTENER)
729 conn = vts_accept_client (wrk, wrk->listen_fd);
730 conn->cfg = vsm->ctrl->cfg;
733 else if (vppcom_session_is_connectable_listener (conn->fd))
735 vts_accept_client (wrk, conn->fd);
740 * Message on control session
743 if (!wrk->wrk_index && conn->fd == vsm->ctrl->fd)
745 rx_bytes = vcl_test_read (conn->fd, conn->buf, conn->buf_size,
747 rx_cfg = (vcl_test_cfg_t *) conn->buf;
748 if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
750 vts_handle_ctrl_cfg (wrk, rx_cfg, conn, rx_bytes);
753 vtinf ("All client connections closed\n");
757 else if (isascii (conn->buf[0]))
759 vts_server_echo (conn, rx_bytes);
763 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
769 * Read perf test data
772 if (EPOLLIN & wrk->wait_events[i].events)
775 rx_bytes = vts_conn_read (conn);
779 if (errno == ECONNRESET)
781 vtinf ("Connection reset by remote peer.\n");
787 vts_server_process_rx (conn, rx_bytes);
788 if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0, 0) >
795 vtwrn ("Unhandled event");
802 vsm->worker_fails -= 1;
805 vppcom_session_close (wrk->listen_fd);
807 free (wrk->conn_pool);
808 vsm->active_workers -= 1;
813 vts_ctrl_session_init (vcl_test_server_worker_t *wrk)
815 vcl_test_server_main_t *vsm = &vcl_server_main;
816 struct epoll_event listen_ev;
819 vtinf ("Initializing main ctrl session ...");
821 vsm->ctrl_listen_fd =
822 vppcom_session_create (VPPCOM_PROTO_TCP, 0 /* is_nonblocking */);
823 if (vsm->ctrl_listen_fd < 0)
824 vtfail ("vppcom_session_create()", vsm->ctrl_listen_fd);
826 rv = vppcom_session_bind (vsm->ctrl_listen_fd, &vsm->cfg.endpt);
828 vtfail ("vppcom_session_bind()", rv);
830 rv = vppcom_session_listen (vsm->ctrl_listen_fd, 10);
832 vtfail ("vppcom_session_listen()", rv);
834 wrk->epfd = vppcom_epoll_create ();
836 vtfail ("vppcom_epoll_create()", wrk->epfd);
838 listen_ev.events = EPOLLIN;
839 listen_ev.data.u32 = VCL_TEST_CTRL_LISTENER;
840 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, vsm->ctrl_listen_fd,
843 vtfail ("vppcom_epoll_ctl", rv);
845 vtinf ("Waiting for a client to connect on port %d ...", vsm->cfg.port);
849 main (int argc, char **argv)
851 vcl_test_server_main_t *vsm = &vcl_server_main;
854 clib_mem_init_thread_safe (0, 64 << 20);
855 vsm->cfg.port = VCL_TEST_SERVER_PORT;
856 vsm->cfg.workers = 1;
857 vsm->active_workers = 0;
858 vcl_test_server_process_opts (vsm, argc, argv);
860 rv = vppcom_app_create ("vcl_test_server");
862 vtfail ("vppcom_app_create()", rv);
864 vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers));
865 vts_ctrl_session_init (&vsm->workers[0]);
867 /* Update ctrl port to data port */
868 vsm->cfg.endpt.port += 1;
869 vts_worker_init (&vsm->workers[0]);
870 for (i = 1; i < vsm->cfg.workers; i++)
872 vsm->workers[i].wrk_index = i;
873 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
874 vts_worker_loop, (void *) &vsm->workers[i]);
877 vts_worker_loop (&vsm->workers[0]);
879 while (vsm->active_workers > 0)
882 vppcom_app_destroy ();
885 return vsm->worker_fails;
889 * fd.io coding-style-patch-verification: ON
892 * eval: (c-set-style "gnu")