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>
38 vcl_test_stats_t stats;
41 vppcom_data_segments_t ds;
42 } vcl_test_server_conn_t;
51 } vcl_test_server_cfg_t;
58 struct epoll_event wait_events[VCL_TEST_CFG_MAX_EPOLL_EVENTS];
59 size_t conn_pool_size;
60 vcl_test_server_conn_t *conn_pool;
62 pthread_t thread_handle;
63 } vcl_test_server_worker_t;
67 vcl_test_server_cfg_t cfg;
68 vcl_test_server_worker_t *workers;
70 struct sockaddr_storage servaddr;
71 volatile int worker_fails;
72 volatile int active_workers;
74 } vcl_test_server_main_t;
76 static __thread int __wrk_index = 0;
78 static vcl_test_server_main_t vcl_server_main;
81 conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size)
83 vcl_test_server_conn_t *conn_pool;
84 size_t new_size = wrk->conn_pool_size + expand_size;
87 conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool));
90 for (i = wrk->conn_pool_size; i < new_size; i++)
92 vcl_test_server_conn_t *conn = &conn_pool[i];
93 memset (conn, 0, sizeof (*conn));
94 vcl_test_cfg_init (&conn->cfg);
95 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
96 &conn->buf, &conn->buf_size);
97 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
100 wrk->conn_pool = conn_pool;
101 wrk->conn_pool_size = new_size;
105 vterr ("conn_pool_expand()", -errno);
109 static inline vcl_test_server_conn_t *
110 conn_pool_alloc (vcl_test_server_worker_t * wrk)
115 for (i = 0; i < wrk->conn_pool_size; i++)
117 if (!wrk->conn_pool[i].is_alloc)
119 wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip;
120 wrk->conn_pool[i].is_alloc = 1;
121 return (&wrk->conn_pool[i]);
127 conn_pool_expand (wrk, 2 * wrk->conn_pool_size);
131 vtwrn ("Failed to allocate connection even after expand");
136 conn_pool_free (vcl_test_server_conn_t * conn)
143 sync_config_and_reply (vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg)
146 vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ ,
147 &conn->buf, &conn->buf_size);
148 conn->cfg.txbuf_size = conn->cfg.rxbuf_size;
150 if (conn->cfg.verbose)
152 vtinf ("(fd %d): Replying to cfg message!\n", conn->fd);
153 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
155 (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
156 sizeof (conn->cfg), NULL, conn->cfg.verbose);
160 vts_server_start_stop (vcl_test_server_worker_t * wrk,
161 vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg)
163 u8 is_bi = rx_cfg->test == VCL_TEST_TYPE_BI;
164 vcl_test_server_conn_t *tc;
168 if (rx_cfg->ctrl_handle == conn->fd)
170 for (i = 0; i < wrk->conn_pool_size; i++)
172 tc = &wrk->conn_pool[i];
173 if (tc->cfg.ctrl_handle != conn->fd)
176 vcl_test_stats_accumulate (&conn->stats, &tc->stats);
177 if (vcl_comp_tspec (&conn->stats.stop, &tc->stats.stop) < 0)
178 conn->stats.stop = tc->stats.stop;
179 /* Client delays sending of disconnect */
180 conn->stats.stop.tv_sec -= VCL_TEST_DELAY_DISCONNECT;
181 if (conn->cfg.verbose)
183 snprintf (buf, sizeof (buf), "SERVER (fd %d) RESULTS", tc->fd);
184 vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ ,
185 is_bi /* show tx */ , conn->cfg.verbose);
189 vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ ,
190 is_bi /* show_tx */ , conn->cfg.verbose);
191 vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ );
192 if (conn->cfg.verbose)
194 vtinf (" vcl server main\n"
195 VCL_TEST_SEPARATOR_STRING
197 " buf size: %u (0x%08x)\n"
198 VCL_TEST_SEPARATOR_STRING,
199 conn->buf, conn->buf_size, conn->buf_size);
202 sync_config_and_reply (conn, rx_cfg);
203 memset (&conn->stats, 0, sizeof (conn->stats));
207 if (rx_cfg->ctrl_handle == ~0)
209 rx_cfg->ctrl_handle = conn->fd;
210 vtinf ("Set control fd %d for test!", conn->fd);
214 vtinf ("Starting %s-directional Stream Test (fd %d)!",
215 is_bi ? "Bi" : "Uni", conn->fd);
218 sync_config_and_reply (conn, rx_cfg);
220 /* read the 1st chunk, record start time */
221 memset (&conn->stats, 0, sizeof (conn->stats));
222 clock_gettime (CLOCK_REALTIME, &conn->stats.start);
227 vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes)
229 vcl_test_server_main_t *vsm = &vcl_server_main;
230 int client_fd = conn->fd;
232 if (conn->cfg.test == VCL_TEST_TYPE_BI)
236 (void) vcl_test_write (client_fd, conn->ds[0].data, conn->ds[0].len,
237 &conn->stats, conn->cfg.verbose);
239 (void) vcl_test_write (client_fd, conn->ds[1].data,
240 conn->ds[1].len, &conn->stats,
244 (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats,
249 vppcom_session_free_segments (conn->fd, conn->ds);
251 if (conn->stats.rx_bytes >= conn->cfg.total_bytes)
252 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
256 vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes)
258 vcl_test_server_main_t *vsm = &vcl_server_main;
259 int tx_bytes, nbytes, pos;
262 vppcom_data_segment_copy (conn->buf, conn->ds, rx_bytes);
264 /* If it looks vaguely like a string, make sure it's terminated */
265 pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1;
266 ((char *) conn->buf)[pos] = 0;
267 vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf);
269 if (conn->cfg.verbose)
270 vtinf ("(fd %d): Echoing back", conn->fd);
272 nbytes = strlen ((const char *) conn->buf) + 1;
273 tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats,
276 vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf);
280 vts_new_client (vcl_test_server_worker_t * wrk, int listen_fd)
282 vcl_test_server_conn_t *conn;
283 struct epoll_event ev;
286 conn = conn_pool_alloc (wrk);
289 vtwrn ("No free connections!");
293 client_fd = vppcom_session_accept (listen_fd, &conn->endpt, 0);
296 vterr ("vppcom_session_accept()", client_fd);
299 conn->fd = client_fd;
301 vtinf ("Got a connection -- fd = %d (0x%08x) on listener fd = %d (0x%08x)",
302 client_fd, client_fd, listen_fd, listen_fd);
305 ev.data.u64 = conn - wrk->conn_pool;
306 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev);
309 vterr ("vppcom_epoll_ctl()", rv);
316 print_usage_and_exit (void)
319 "vcl_test_server [OPTIONS] <port>\n"
321 " -h Print this message and exit.\n"
323 " -w <num> Number of workers\n"
324 " -p <PROTO> Use <PROTO> transport layer\n"
325 " -D Use UDP transport layer\n"
326 " -L Use TLS transport layer\n");
331 vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm)
333 struct sockaddr_storage *servaddr = &vsm->servaddr;
334 memset (servaddr, 0, sizeof (*servaddr));
336 if (vsm->cfg.address_ip6)
338 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
339 server_addr->sin6_family = AF_INET6;
340 server_addr->sin6_addr = in6addr_any;
341 server_addr->sin6_port = htons (vsm->cfg.port);
345 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
346 server_addr->sin_family = AF_INET;
347 server_addr->sin_addr.s_addr = htonl (INADDR_ANY);
348 server_addr->sin_port = htons (vsm->cfg.port);
351 if (vsm->cfg.address_ip6)
353 struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr;
354 vsm->cfg.endpt.is_ip4 = 0;
355 vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr;
356 vsm->cfg.endpt.port = (uint16_t) server_addr->sin6_port;
360 struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr;
361 vsm->cfg.endpt.is_ip4 = 1;
362 vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr;
363 vsm->cfg.endpt.port = (uint16_t) server_addr->sin_port;
368 vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc,
373 vsm->cfg.proto = VPPCOM_PROTO_TCP;
376 while ((c = getopt (argc, argv, "6DLsw:p:")) != -1)
380 vsm->cfg.address_ip6 = 1;
384 if (vppcom_unformat_proto (&vsm->cfg.proto, optarg))
385 vtwrn ("Invalid vppcom protocol %s, defaulting to TCP", optarg);
389 vsm->cfg.proto = VPPCOM_PROTO_UDP;
393 vsm->cfg.proto = VPPCOM_PROTO_TLS;
399 vsm->cfg.workers = v;
401 vtwrn ("Invalid number of workers %d", v);
411 vtwrn ("Option `-%c' requires an argument.", optopt);
414 if (isprint (optopt))
415 vtwrn ("Unknown option `-%c'.", optopt);
417 vtwrn ("Unknown option character `\\x%x'.", optopt);
422 print_usage_and_exit ();
425 if (argc < (optind + 1))
427 fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n");
428 print_usage_and_exit ();
431 if (sscanf (argv[optind], "%d", &v) == 1)
432 vsm->cfg.port = (uint16_t) v;
435 fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]);
436 print_usage_and_exit ();
439 vcl_test_init_endpoint_addr (vsm);
443 vts_clean_connected_listeners (vcl_test_server_worker_t * wrk,
446 if ((vppcom_session_n_accepted (listener_fd) == 0) &
447 vppcom_session_is_connectable_listener (listener_fd))
449 vtinf ("Connected Listener fd %x has no more sessions", listener_fd);
450 vppcom_session_close (listener_fd);
456 vts_handle_cfg (vcl_test_server_worker_t * wrk, vcl_test_cfg_t * rx_cfg,
457 vcl_test_server_conn_t * conn, int rx_bytes)
462 vtinf ("(fd %d): Received a cfg msg!", conn->fd);
463 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
466 if (rx_bytes != sizeof (*rx_cfg))
468 vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd,
469 rx_bytes, sizeof (*rx_cfg));
470 conn->cfg.rxbuf_size = 0;
471 conn->cfg.num_writes = 0;
472 if (conn->cfg.verbose)
474 vtinf ("(fd %d): Replying to cfg msg", conn->fd);
475 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
477 vcl_test_write (conn->fd, (uint8_t *) & conn->cfg,
478 sizeof (conn->cfg), NULL, conn->cfg.verbose);
482 switch (rx_cfg->test)
484 case VCL_TEST_TYPE_NONE:
485 case VCL_TEST_TYPE_ECHO:
486 sync_config_and_reply (conn, rx_cfg);
489 case VCL_TEST_TYPE_BI:
490 case VCL_TEST_TYPE_UNI:
491 vts_server_start_stop (wrk, conn, rx_cfg);
494 case VCL_TEST_TYPE_EXIT:
495 vtinf ("Session fd %d closing!", conn->fd);
496 clock_gettime (CLOCK_REALTIME, &conn->stats.stop);
497 listener_fd = vppcom_session_listener (conn->fd);
498 vppcom_session_close (conn->fd);
499 conn_pool_free (conn);
501 vts_clean_connected_listeners (wrk, listener_fd);
505 vtwrn ("Unknown test type %d", rx_cfg->test);
506 vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ );
514 vts_worker_init (vcl_test_server_worker_t * wrk)
516 vcl_test_server_main_t *vsm = &vcl_server_main;
517 struct epoll_event listen_ev;
520 __wrk_index = wrk->wrk_index;
522 vtinf ("Initializing worker ...");
524 conn_pool_expand (wrk, VCL_TEST_CFG_MAX_TEST_SESS + 1);
526 if (vppcom_worker_register ())
527 vtfail ("vppcom_worker_register()", 1);
529 wrk->listen_fd = vppcom_session_create (vsm->cfg.proto,
530 0 /* is_nonblocking */ );
531 if (wrk->listen_fd < 0)
532 vtfail ("vppcom_session_create()", wrk->listen_fd);
535 if (vsm->cfg.proto == VPPCOM_PROTO_TLS
536 || vsm->cfg.proto == VPPCOM_PROTO_QUIC)
538 vppcom_session_tls_add_cert (wrk->listen_fd, vcl_test_crt_rsa,
539 vcl_test_crt_rsa_len);
540 vppcom_session_tls_add_key (wrk->listen_fd, vcl_test_key_rsa,
541 vcl_test_key_rsa_len);
544 rv = vppcom_session_bind (wrk->listen_fd, &vsm->cfg.endpt);
546 vtfail ("vppcom_session_bind()", rv);
548 if (!(vsm->cfg.proto == VPPCOM_PROTO_UDP))
550 rv = vppcom_session_listen (wrk->listen_fd, 10);
552 vtfail ("vppcom_session_listen()", rv);
555 wrk->epfd = vppcom_epoll_create ();
557 vtfail ("vppcom_epoll_create()", wrk->epfd);
559 listen_ev.events = EPOLLIN;
560 listen_ev.data.u32 = ~0;
561 rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd,
564 vtfail ("vppcom_epoll_ctl", rv);
566 vsm->active_workers += 1;
567 vtinf ("Waiting for a client to connect on port %d ...", vsm->cfg.port);
571 vts_conn_expect_config (vcl_test_server_conn_t * conn)
573 if (conn->cfg.test == VCL_TEST_TYPE_ECHO)
576 return (conn->stats.rx_bytes < 128
577 || conn->stats.rx_bytes > conn->cfg.total_bytes);
580 static vcl_test_cfg_t *
581 vts_conn_read_config (vcl_test_server_conn_t * conn)
583 vcl_test_server_main_t *vsm = &vcl_server_main;
587 /* We could avoid the copy if the first segment is big enough but this
588 * just simplifies things */
589 vppcom_data_segment_copy (conn->buf, conn->ds, sizeof (vcl_test_cfg_t));
591 return (vcl_test_cfg_t *) conn->buf;
595 vts_conn_read (vcl_test_server_conn_t * conn)
597 vcl_test_server_main_t *vsm = &vcl_server_main;
599 return vcl_test_read_ds (conn->fd, conn->ds, &conn->stats);
601 return vcl_test_read (conn->fd, conn->buf, conn->buf_size, &conn->stats);
605 vts_conn_has_ascii (vcl_test_server_conn_t * conn)
607 vcl_test_server_main_t *vsm = &vcl_server_main;
610 return isascii (conn->ds[0].data[0]);
612 return isascii (conn->buf[0]);
616 vts_worker_loop (void *arg)
618 vcl_test_server_main_t *vsm = &vcl_server_main;
619 vcl_test_server_worker_t *wrk = arg;
620 vcl_test_server_conn_t *conn;
621 int i, rx_bytes, num_ev, listener_fd;
622 vcl_test_cfg_t *rx_cfg;
625 vts_worker_init (wrk);
629 num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events,
630 VCL_TEST_CFG_MAX_EPOLL_EVENTS, 60000.0);
633 vterr ("vppcom_epoll_wait()", num_ev);
636 else if (num_ev == 0)
638 vtinf ("vppcom_epoll_wait() timeout!");
641 for (i = 0; i < num_ev; i++)
643 conn = &wrk->conn_pool[wrk->wait_events[i].data.u32];
644 if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP))
646 vtinf ("Closing session %d on HUP", conn->fd);
647 listener_fd = vppcom_session_listener (conn->fd);
648 vppcom_session_close (conn->fd);
650 vts_clean_connected_listeners (wrk, listener_fd);
653 vtinf ("All client connections closed\n");
658 if (wrk->wait_events[i].data.u32 == ~0)
660 vts_new_client (wrk, wrk->listen_fd);
663 else if (vppcom_session_is_connectable_listener (conn->fd))
665 vts_new_client (wrk, conn->fd);
669 if (EPOLLIN & wrk->wait_events[i].events)
672 rx_bytes = vts_conn_read (conn);
676 if (errno == ECONNRESET)
678 vtinf ("Connection reset by remote peer.\n");
685 if (vts_conn_expect_config (conn))
687 rx_cfg = vts_conn_read_config (conn);
688 if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC)
691 vppcom_session_free_segments (conn->fd, conn->ds);
692 vts_handle_cfg (wrk, rx_cfg, conn, rx_bytes);
695 vtinf ("All client connections closed\n");
701 if ((conn->cfg.test == VCL_TEST_TYPE_UNI)
702 || (conn->cfg.test == VCL_TEST_TYPE_BI))
704 vts_server_rx (conn, rx_bytes);
705 if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0,
710 if (vts_conn_has_ascii (conn))
712 vts_server_echo (conn, rx_bytes);
716 vtwrn ("FIFO not drained! extra bytes %d", rx_bytes);
721 vtwrn ("Unhandled event");
728 vsm->worker_fails -= 1;
731 vppcom_session_close (wrk->listen_fd);
733 free (wrk->conn_pool);
734 vsm->active_workers -= 1;
739 main (int argc, char **argv)
741 vcl_test_server_main_t *vsm = &vcl_server_main;
744 clib_mem_init_thread_safe (0, 64 << 20);
745 vsm->cfg.port = VCL_TEST_SERVER_PORT;
746 vsm->cfg.workers = 1;
747 vsm->active_workers = 0;
748 vcl_test_server_process_opts (vsm, argc, argv);
750 rv = vppcom_app_create ("vcl_test_server");
752 vtfail ("vppcom_app_create()", rv);
754 vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers));
755 vts_worker_init (&vsm->workers[0]);
756 for (i = 1; i < vsm->cfg.workers; i++)
758 vsm->workers[i].wrk_index = i;
759 rv = pthread_create (&vsm->workers[i].thread_handle, NULL,
760 vts_worker_loop, (void *) &vsm->workers[i]);
762 vts_worker_loop (&vsm->workers[0]);
764 while (vsm->active_workers > 0)
767 vppcom_app_destroy ();
770 return vsm->worker_fails;
774 * fd.io coding-style-patch-verification: ON
777 * eval: (c-set-style "gnu")