diff options
Diffstat (limited to 'src/vcl/vcl_test_server.c')
-rw-r--r-- | src/vcl/vcl_test_server.c | 242 |
1 files changed, 118 insertions, 124 deletions
diff --git a/src/vcl/vcl_test_server.c b/src/vcl/vcl_test_server.c index 2913452ab10..54f164e43c1 100644 --- a/src/vcl/vcl_test_server.c +++ b/src/vcl/vcl_test_server.c @@ -34,8 +34,8 @@ typedef struct int fd; uint8_t *buf; uint32_t buf_size; - sock_test_cfg_t cfg; - sock_test_stats_t stats; + vcl_test_cfg_t cfg; + vcl_test_stats_t stats; vppcom_endpt_t endpt; uint8_t ip[16]; vppcom_data_segments_t ds; @@ -50,15 +50,12 @@ typedef struct vppcom_endpt_t endpt; } vcl_test_server_cfg_t; -#define SOCK_SERVER_MAX_TEST_CONN 16 -#define SOCK_SERVER_MAX_EPOLL_EVENTS 16 - typedef struct { uint32_t wrk_index; int listen_fd; int epfd; - struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS]; + struct epoll_event wait_events[VCL_TEST_CFG_MAX_EPOLL_EVENTS]; size_t conn_pool_size; vcl_test_server_conn_t *conn_pool; int nfds; @@ -78,7 +75,7 @@ typedef struct static __thread int __wrk_index = 0; -static vcl_test_server_main_t sock_server_main; +static vcl_test_server_main_t vcl_server_main; static inline void conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size) @@ -94,9 +91,9 @@ conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size) { vcl_test_server_conn_t *conn = &conn_pool[i]; memset (conn, 0, sizeof (*conn)); - sock_test_cfg_init (&conn->cfg); - sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , - &conn->buf, &conn->buf_size); + vcl_test_cfg_init (&conn->cfg); + vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , + &conn->buf, &conn->buf_size); conn->cfg.txbuf_size = conn->cfg.rxbuf_size; } @@ -143,18 +140,17 @@ conn_pool_free (vcl_test_server_conn_t * conn) } static inline void -sync_config_and_reply (vcl_test_server_conn_t * conn, - sock_test_cfg_t * rx_cfg) +sync_config_and_reply (vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg) { conn->cfg = *rx_cfg; - sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , - &conn->buf, &conn->buf_size); + vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , + &conn->buf, &conn->buf_size); conn->cfg.txbuf_size = conn->cfg.rxbuf_size; if (conn->cfg.verbose) { vtinf ("(fd %d): Replying to cfg message!\n", conn->fd); - sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); + vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); } (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg, sizeof (conn->cfg), NULL, conn->cfg.verbose); @@ -162,60 +158,62 @@ sync_config_and_reply (vcl_test_server_conn_t * conn, static void vts_server_start_stop (vcl_test_server_worker_t * wrk, - vcl_test_server_conn_t * conn, - sock_test_cfg_t * rx_cfg) + vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg) { - u8 is_bi = rx_cfg->test == SOCK_TEST_TYPE_BI; - int client_fd = conn->fd, i; + u8 is_bi = rx_cfg->test == VCL_TEST_TYPE_BI; vcl_test_server_conn_t *tc; char buf[64]; + int i; if (rx_cfg->ctrl_handle == conn->fd) { - clock_gettime (CLOCK_REALTIME, &conn->stats.stop); - for (i = 0; i < wrk->conn_pool_size; i++) { tc = &wrk->conn_pool[i]; if (tc->cfg.ctrl_handle != conn->fd) continue; - sock_test_stats_accumulate (&conn->stats, &tc->stats); - + vcl_test_stats_accumulate (&conn->stats, &tc->stats); + if (vcl_comp_tspec (&conn->stats.stop, &tc->stats.stop) < 0) + conn->stats.stop = tc->stats.stop; + /* Client delays sending of disconnect */ + conn->stats.stop.tv_sec -= VCL_TEST_DELAY_DISCONNECT; if (conn->cfg.verbose) { sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd); - sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ , - is_bi /* show tx */ , conn->cfg.verbose); + vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ , + is_bi /* show tx */ , conn->cfg.verbose); } } - sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ , - is_bi /* show_tx */ , conn->cfg.verbose); - sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); + vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ , + is_bi /* show_tx */ , conn->cfg.verbose); + vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); if (conn->cfg.verbose) { - vtinf (" sock server main\n" - SOCK_TEST_SEPARATOR_STRING + vtinf (" vcl server main\n" + VCL_TEST_SEPARATOR_STRING " buf: %p\n" " buf size: %u (0x%08x)\n" - SOCK_TEST_SEPARATOR_STRING, + VCL_TEST_SEPARATOR_STRING, conn->buf, conn->buf_size, conn->buf_size); } sync_config_and_reply (conn, rx_cfg); - vtinf ("(fd %d): %s-directional Stream Test Complete!\n" - SOCK_TEST_BANNER_STRING "\n", conn->fd, is_bi ? "Bi" : "Uni"); memset (&conn->stats, 0, sizeof (conn->stats)); } else { - vtinf (SOCK_TEST_BANNER_STRING "(fd %d): %s-directional Stream Test!\n" - " Sending client the test cfg to start streaming data...\n", - client_fd, is_bi ? "Bi" : "Uni"); - if (rx_cfg->ctrl_handle == ~0) - rx_cfg->ctrl_handle = conn->fd; + { + rx_cfg->ctrl_handle = conn->fd; + vtinf ("Set control fd %d for test!", conn->fd); + } + else + { + vtinf ("Starting %s-directional Stream Test (fd %d)!", + is_bi ? "Bi" : "Uni", conn->fd); + } sync_config_and_reply (conn, rx_cfg); @@ -228,12 +226,12 @@ vts_server_start_stop (vcl_test_server_worker_t * wrk, static inline void vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes) { - vcl_test_server_main_t *vts = &sock_server_main; + vcl_test_server_main_t *vsm = &vcl_server_main; int client_fd = conn->fd; - if (conn->cfg.test == SOCK_TEST_TYPE_BI) + if (conn->cfg.test == VCL_TEST_TYPE_BI) { - if (vts->use_ds) + if (vsm->use_ds) { (void) vcl_test_write (client_fd, conn->ds[0].data, conn->ds[0].len, &conn->stats, conn->cfg.verbose); @@ -247,7 +245,7 @@ vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes) conn->cfg.verbose); } - if (vts->use_ds) + if (vsm->use_ds) vppcom_session_free_segments (conn->fd, conn->ds); if (conn->stats.rx_bytes >= conn->cfg.total_bytes) @@ -257,10 +255,10 @@ vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes) static void vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes) { - vcl_test_server_main_t *vts = &sock_server_main; + vcl_test_server_main_t *vsm = &vcl_server_main; int tx_bytes, nbytes, pos; - if (vts->use_ds) + if (vsm->use_ds) vppcom_data_segment_copy (conn->buf, conn->ds, rx_bytes); /* If it looks vaguely like a string, make sure it's terminated */ @@ -278,11 +276,12 @@ vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes) vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf); } -static inline void +static void vts_new_client (vcl_test_server_worker_t * wrk) { - int client_fd; vcl_test_server_conn_t *conn; + struct epoll_event ev; + int rv, client_fd; conn = conn_pool_alloc (wrk); if (!conn) @@ -297,32 +296,26 @@ vts_new_client (vcl_test_server_worker_t * wrk) vterr ("vppcom_session_accept()", client_fd); return; } - - vtinf ("Got a connection -- fd = %d (0x%08x)!", client_fd, client_fd); - conn->fd = client_fd; - { - struct epoll_event ev; - int rv; + vtinf ("Got a connection -- fd = %d (0x%08x)!", client_fd, client_fd); - ev.events = EPOLLIN; - ev.data.u64 = conn - wrk->conn_pool; - rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev); - if (rv < 0) - { - vterr ("vppcom_epoll_ctl()", rv); - return; - } - wrk->nfds++; - } + ev.events = EPOLLIN; + ev.data.u64 = conn - wrk->conn_pool; + rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev); + if (rv < 0) + { + vterr ("vppcom_epoll_ctl()", rv); + return; + } + wrk->nfds++; } -void +static void print_usage_and_exit (void) { fprintf (stderr, - "sock_test_server [OPTIONS] <port>\n" + "vcl_test_server [OPTIONS] <port>\n" " OPTIONS\n" " -h Print this message and exit.\n" " -6 Use IPv6\n" @@ -332,71 +325,71 @@ print_usage_and_exit (void) } static void -vcl_test_init_endpoint_addr (vcl_test_server_main_t * ssm) +vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm) { - struct sockaddr_storage *servaddr = &ssm->servaddr; + struct sockaddr_storage *servaddr = &vsm->servaddr; memset (servaddr, 0, sizeof (*servaddr)); - if (ssm->cfg.address_ip6) + if (vsm->cfg.address_ip6) { struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr; server_addr->sin6_family = AF_INET6; server_addr->sin6_addr = in6addr_any; - server_addr->sin6_port = htons (ssm->cfg.port); + server_addr->sin6_port = htons (vsm->cfg.port); } else { struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr; server_addr->sin_family = AF_INET; server_addr->sin_addr.s_addr = htonl (INADDR_ANY); - server_addr->sin_port = htons (ssm->cfg.port); + server_addr->sin_port = htons (vsm->cfg.port); } - if (ssm->cfg.address_ip6) + if (vsm->cfg.address_ip6) { struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr; - ssm->cfg.endpt.is_ip4 = 0; - ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr; - ssm->cfg.endpt.port = (uint16_t) server_addr->sin6_port; + vsm->cfg.endpt.is_ip4 = 0; + vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr; + vsm->cfg.endpt.port = (uint16_t) server_addr->sin6_port; } else { struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr; - ssm->cfg.endpt.is_ip4 = 1; - ssm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr; - ssm->cfg.endpt.port = (uint16_t) server_addr->sin_port; + vsm->cfg.endpt.is_ip4 = 1; + vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr; + vsm->cfg.endpt.port = (uint16_t) server_addr->sin_port; } } static void -vcl_test_server_process_opts (vcl_test_server_main_t * ssm, int argc, +vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc, char **argv) { int v, c; - ssm->cfg.proto = VPPCOM_PROTO_TCP; + vsm->cfg.proto = VPPCOM_PROTO_TCP; opterr = 0; while ((c = getopt (argc, argv, "6Dsw:")) != -1) switch (c) { case '6': - ssm->cfg.address_ip6 = 1; + vsm->cfg.address_ip6 = 1; break; case 'D': - ssm->cfg.proto = VPPCOM_PROTO_UDP; + vsm->cfg.proto = VPPCOM_PROTO_UDP; break; case 'w': v = atoi (optarg); if (v > 1) - ssm->cfg.workers = v; + vsm->cfg.workers = v; else vtwrn ("Invalid number of workers %d", v); break; case 's': - ssm->use_ds = 1; + vsm->use_ds = 1; break; case '?': switch (optopt) @@ -420,24 +413,24 @@ vcl_test_server_process_opts (vcl_test_server_main_t * ssm, int argc, } if (sscanf (argv[optind], "%d", &v) == 1) - ssm->cfg.port = (uint16_t) v; + vsm->cfg.port = (uint16_t) v; else { fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]); print_usage_and_exit (); } - vcl_test_init_endpoint_addr (ssm); + vcl_test_init_endpoint_addr (vsm); } int -vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg, +vts_handle_cfg (vcl_test_server_worker_t * wrk, vcl_test_cfg_t * rx_cfg, vcl_test_server_conn_t * conn, int rx_bytes) { if (rx_cfg->verbose) { vtinf ("(fd %d): Received a cfg msg!", conn->fd); - sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); + vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ ); } if (rx_bytes != sizeof (*rx_cfg)) @@ -449,7 +442,7 @@ vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg, if (conn->cfg.verbose) { vtinf ("(fd %d): Replying to cfg msg", conn->fd); - sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); + vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ ); } vcl_test_write (conn->fd, (uint8_t *) & conn->cfg, sizeof (conn->cfg), NULL, conn->cfg.verbose); @@ -458,18 +451,19 @@ vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg, switch (rx_cfg->test) { - case SOCK_TEST_TYPE_NONE: - case SOCK_TEST_TYPE_ECHO: + case VCL_TEST_TYPE_NONE: + case VCL_TEST_TYPE_ECHO: sync_config_and_reply (conn, rx_cfg); break; - case SOCK_TEST_TYPE_BI: - case SOCK_TEST_TYPE_UNI: + case VCL_TEST_TYPE_BI: + case VCL_TEST_TYPE_UNI: vts_server_start_stop (wrk, conn, rx_cfg); break; - case SOCK_TEST_TYPE_EXIT: - vtinf ("Have a great day conn %d (closing)!", conn->fd); + case VCL_TEST_TYPE_EXIT: + vtinf ("Session fd %d closing!", conn->fd); + clock_gettime (CLOCK_REALTIME, &conn->stats.stop); vppcom_session_close (conn->fd); conn_pool_free (conn); wrk->nfds--; @@ -477,7 +471,7 @@ vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg, default: vtwrn ("Unknown test type %d", rx_cfg->test); - sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); + vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ ); break; } @@ -487,7 +481,7 @@ vts_handle_cfg (vcl_test_server_worker_t * wrk, sock_test_cfg_t * rx_cfg, static void vts_worker_init (vcl_test_server_worker_t * wrk) { - vcl_test_server_main_t *ssm = &sock_server_main; + vcl_test_server_main_t *vsm = &vcl_server_main; struct epoll_event listen_ev; int rv; @@ -495,21 +489,21 @@ vts_worker_init (vcl_test_server_worker_t * wrk) vtinf ("Initializing worker ..."); - conn_pool_expand (wrk, SOCK_SERVER_MAX_TEST_CONN + 1); + conn_pool_expand (wrk, VCL_TEST_CFG_MAX_TEST_SESS + 1); if (wrk->wrk_index) if (vppcom_worker_register ()) vtfail ("vppcom_worker_register()", 1); - wrk->listen_fd = vppcom_session_create (ssm->cfg.proto, + wrk->listen_fd = vppcom_session_create (vsm->cfg.proto, 0 /* is_nonblocking */ ); if (wrk->listen_fd < 0) vtfail ("vppcom_session_create()", wrk->listen_fd); - rv = vppcom_session_bind (wrk->listen_fd, &ssm->cfg.endpt); + rv = vppcom_session_bind (wrk->listen_fd, &vsm->cfg.endpt); if (rv < 0) vtfail ("vppcom_session_bind()", rv); - if (!(ssm->cfg.proto == VPPCOM_PROTO_UDP)) + if (!(vsm->cfg.proto == VPPCOM_PROTO_UDP)) { rv = vppcom_session_listen (wrk->listen_fd, 10); if (rv < 0) @@ -527,41 +521,39 @@ vts_worker_init (vcl_test_server_worker_t * wrk) if (rv < 0) vtfail ("vppcom_epoll_ctl", rv); - ssm->active_workers += 1; - vtinf ("Waiting for a client to connect on port %d ...", ssm->cfg.port); + vsm->active_workers += 1; + vtinf ("Waiting for a client to connect on port %d ...", vsm->cfg.port); } static int vts_conn_expect_config (vcl_test_server_conn_t * conn) { - if (conn->cfg.test == SOCK_TEST_TYPE_ECHO) + if (conn->cfg.test == VCL_TEST_TYPE_ECHO) return 1; return (conn->stats.rx_bytes < 128 - || conn->stats.rx_bytes >= conn->cfg.total_bytes); + || conn->stats.rx_bytes > conn->cfg.total_bytes); } -static sock_test_cfg_t * +static vcl_test_cfg_t * vts_conn_read_config (vcl_test_server_conn_t * conn) { - vcl_test_server_main_t *vts = &sock_server_main; + vcl_test_server_main_t *vsm = &vcl_server_main; - if (vts->use_ds) + if (vsm->use_ds) { /* We could avoid the copy if the first segment is big enough but this * just simplifies things */ - vppcom_data_segment_copy (conn->buf, conn->ds, - sizeof (sock_test_cfg_t)); - vppcom_session_free_segments (conn->fd, conn->ds); + vppcom_data_segment_copy (conn->buf, conn->ds, sizeof (vcl_test_cfg_t)); } - return (sock_test_cfg_t *) conn->buf; + return (vcl_test_cfg_t *) conn->buf; } static inline int vts_conn_read (vcl_test_server_conn_t * conn) { - vcl_test_server_main_t *vts = &sock_server_main; - if (vts->use_ds) + vcl_test_server_main_t *vsm = &vcl_server_main; + if (vsm->use_ds) return vcl_test_read_ds (conn->fd, conn->ds, &conn->stats); else return vcl_test_read (conn->fd, conn->buf, conn->buf_size, &conn->stats); @@ -570,9 +562,9 @@ vts_conn_read (vcl_test_server_conn_t * conn) static inline int vts_conn_has_ascii (vcl_test_server_conn_t * conn) { - vcl_test_server_main_t *vts = &sock_server_main; + vcl_test_server_main_t *vsm = &vcl_server_main; - if (vts->use_ds) + if (vsm->use_ds) return isascii (conn->ds[0].data[0]); else return isascii (conn->buf[0]); @@ -581,11 +573,11 @@ vts_conn_has_ascii (vcl_test_server_conn_t * conn) static void * vts_worker_loop (void *arg) { - vcl_test_server_main_t *ssm = &sock_server_main; + vcl_test_server_main_t *vsm = &vcl_server_main; vcl_test_server_worker_t *wrk = arg; vcl_test_server_conn_t *conn; int i, rx_bytes, num_ev; - sock_test_cfg_t *rx_cfg; + vcl_test_cfg_t *rx_cfg; if (wrk->wrk_index) vts_worker_init (wrk); @@ -593,7 +585,7 @@ vts_worker_loop (void *arg) while (1) { num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events, - SOCK_SERVER_MAX_EPOLL_EVENTS, 60000.0); + VCL_TEST_CFG_MAX_EPOLL_EVENTS, 60000.0); if (num_ev < 0) { vterr ("vppcom_epoll_wait()", num_ev); @@ -643,8 +635,10 @@ vts_worker_loop (void *arg) if (vts_conn_expect_config (conn)) { rx_cfg = vts_conn_read_config (conn); - if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC) + if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC) { + if (vsm->use_ds) + vppcom_session_free_segments (conn->fd, conn->ds); vts_handle_cfg (wrk, rx_cfg, conn, rx_bytes); if (!wrk->nfds) { @@ -654,8 +648,8 @@ vts_worker_loop (void *arg) continue; } } - if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) - || (conn->cfg.test == SOCK_TEST_TYPE_BI)) + if ((conn->cfg.test == VCL_TEST_TYPE_UNI) + || (conn->cfg.test == VCL_TEST_TYPE_BI)) { vts_server_rx (conn, rx_bytes); if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0, @@ -681,24 +675,24 @@ vts_worker_loop (void *arg) } fail: - ssm->worker_fails -= 1; + vsm->worker_fails -= 1; done: vppcom_session_close (wrk->listen_fd); if (wrk->conn_pool) free (wrk->conn_pool); - ssm->active_workers -= 1; + vsm->active_workers -= 1; return 0; } int main (int argc, char **argv) { - vcl_test_server_main_t *vsm = &sock_server_main; + vcl_test_server_main_t *vsm = &vcl_server_main; int rv, i; clib_mem_init_thread_safe (0, 64 << 20); - vsm->cfg.port = SOCK_TEST_SERVER_PORT; + vsm->cfg.port = VCL_TEST_SERVER_PORT; vsm->cfg.workers = 1; vsm->active_workers = 0; vcl_test_server_process_opts (vsm, argc, argv); |