diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/vcl.am | 26 | ||||
-rw-r--r-- | src/vcl/sock_test.h | 316 | ||||
-rw-r--r-- | src/vcl/sock_test_common.h | 311 | ||||
-rw-r--r-- | src/vcl/vcl_test.h | 127 | ||||
-rw-r--r-- | src/vcl/vcl_test_client.c | 1096 | ||||
-rw-r--r-- | src/vcl/vcl_test_server.c | 633 |
6 files changed, 2171 insertions, 338 deletions
diff --git a/src/vcl.am b/src/vcl.am index ccb323a033b..554ebfc893a 100644 --- a/src/vcl.am +++ b/src/vcl.am @@ -15,21 +15,21 @@ lib_LTLIBRARIES += libvppcom.la libvcl_ldpreload.la libvppcom_la_SOURCES = libvcl_ldpreload_la_SOURCES = -libvppcom_la_DEPENDENCIES = \ - libsvm.la \ +libvppcom_la_DEPENDENCIES = \ + libsvm.la \ libvlibmemoryclient.la libvppcom_la_LIBADD = $(libvppcom_la_DEPENDENCIES) -lpthread -lrt -ldl libvppcom_la_SOURCES += \ - vcl/vcl_event.c \ + vcl/vcl_event.c \ vcl/vppcom.c \ $(libvppinfra_la_SOURCES) \ $(libsvm_la_SOURCES) \ $(libvlibmemoryclient_la_SOURCES) nobase_include_HEADERS += \ - vcl/vcl_event.h \ + vcl/vcl_event.h \ vcl/vppcom.h libvcl_ldpreload_la_LIBADD = $(libvppcom_la_DEPENDENCIES) -lpthread -lrt -ldl @@ -48,8 +48,8 @@ noinst_PROGRAMS += \ vcl_test_server \ vcl_test_client \ sock_test_server \ - sock_test_client \ - test_vcl_listener_server \ + sock_test_client \ + test_vcl_listener_server \ test_vcl_listener_client @@ -64,20 +64,14 @@ vcl_test_server_LDADD = libvppcom.la vcl_test_client_SOURCES = vcl/vcl_test_client.c - -# Link libvcl_ldpreload.la instead of vppcom.la -# to validate that all symbols are included in -# libvcl_ldpreload.la at build time. -# -# Not recommended for production VCL apps as -# it includes extraneous code which will never -# be executed. -vcl_test_client_LDADD = libvcl_ldpreload.la +vcl_test_client_LDADD = libvppcom.la sock_test_server_SOURCES = vcl/sock_test_server.c sock_test_client_SOURCES = vcl/sock_test_client.c nobase_include_HEADERS += \ - vcl/sock_test.h + vcl/sock_test_common.h \ + vcl/sock_test.h \ + vcl/vcl_test.h # vi:syntax=automake diff --git a/src/vcl/sock_test.h b/src/vcl/sock_test.h index 26dc3f1e653..6e0b22ae81f 100644 --- a/src/vcl/sock_test.h +++ b/src/vcl/sock_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017-2018 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -20,306 +20,13 @@ #include <errno.h> #include <stdlib.h> #include <string.h> - -#define SOCK_TEST_TOKEN_HELP "#H" -#define SOCK_TEST_TOKEN_EXIT "#X" -#define SOCK_TEST_TOKEN_VERBOSE "#V" -#define SOCK_TEST_TOKEN_TXBUF_SIZE "#T:" -#define SOCK_TEST_TOKEN_NUM_TEST_SCKTS "#I:" -#define SOCK_TEST_TOKEN_NUM_WRITES "#N:" -#define SOCK_TEST_TOKEN_RXBUF_SIZE "#R:" -#define SOCK_TEST_TOKEN_SHOW_CFG "#C" -#define SOCK_TEST_TOKEN_RUN_UNI "#U" -#define SOCK_TEST_TOKEN_RUN_BI "#B" - -#define SOCK_TEST_BANNER_STRING \ - "============================================\n" -#define SOCK_TEST_SEPARATOR_STRING \ - " -----------------------------\n" - -#define ONE_GIG (1024*1024*1024) -#define SOCK_TEST_SERVER_PORT 22000 -#define SOCK_TEST_LOCALHOST_IPADDR "127.0.0.1" - -#define SOCK_TEST_CFG_CTRL_MAGIC 0xfeedface -#define SOCK_TEST_CFG_NUM_WRITES_DEF 1000000 -#define SOCK_TEST_CFG_TXBUF_SIZE_DEF 8192 -#define SOCK_TEST_CFG_RXBUF_SIZE_DEF (64*SOCK_TEST_CFG_TXBUF_SIZE_DEF) -#define SOCK_TEST_CFG_BUF_SIZE_MIN 128 -#define SOCK_TEST_CFG_MAX_TEST_SCKTS 5 +#include <vcl/sock_test_common.h> #define SOCK_TEST_AF_UNIX_FILENAME "/tmp/ldp_server_af_unix_socket" #define SOCK_TEST_MIXED_EPOLL_DATA "Hello, world! (over an AF_UNIX socket)" #define SOCK_TEST_AF_UNIX_ACCEPT_DATA 0xaf0000af #define SOCK_TEST_AF_UNIX_FD_MASK 0x00af0000 -typedef enum -{ - SOCK_TEST_TYPE_NONE, - SOCK_TEST_TYPE_ECHO, - SOCK_TEST_TYPE_UNI, - SOCK_TEST_TYPE_BI, - SOCK_TEST_TYPE_EXIT, -} sock_test_t; - -typedef struct __attribute__ ((packed)) -{ - uint32_t magic; - uint32_t seq_num; - uint32_t test; - uint32_t ctrl_handle; - uint32_t num_test_sockets; - uint32_t verbose; - uint32_t address_ip6; - uint32_t transport_udp; - uint64_t rxbuf_size; - uint64_t txbuf_size; - uint64_t num_writes; - uint64_t total_bytes; -} sock_test_cfg_t; - -typedef struct -{ - uint64_t rx_xacts; - uint64_t rx_bytes; - uint32_t rx_eagain; - uint32_t rx_incomp; - uint64_t tx_xacts; - uint64_t tx_bytes; - uint32_t tx_eagain; - uint32_t tx_incomp; - struct timespec start; - struct timespec stop; -} sock_test_stats_t; - -typedef struct -{ - int fd; - uint32_t txbuf_size; - char *txbuf; - uint32_t rxbuf_size; - char *rxbuf; - sock_test_cfg_t cfg; - sock_test_stats_t stats; -} sock_test_socket_t; - -static inline void -sock_test_stats_accumulate (sock_test_stats_t * accum, - sock_test_stats_t * incr) -{ - accum->rx_xacts += incr->rx_xacts; - accum->rx_bytes += incr->rx_bytes; - accum->rx_eagain += incr->rx_eagain; - accum->rx_incomp += incr->rx_incomp; - accum->tx_xacts += incr->tx_xacts; - accum->tx_bytes += incr->tx_bytes; - accum->tx_eagain += incr->tx_eagain; - accum->tx_incomp += incr->tx_incomp; -} - -static inline void -sock_test_cfg_init (sock_test_cfg_t *cfg) -{ - cfg->magic = SOCK_TEST_CFG_CTRL_MAGIC; - cfg->test = SOCK_TEST_TYPE_NONE; - cfg->ctrl_handle = ~0; - cfg->num_test_sockets = 1; - cfg->verbose = 0; - cfg->rxbuf_size = SOCK_TEST_CFG_RXBUF_SIZE_DEF; - cfg->num_writes = SOCK_TEST_CFG_NUM_WRITES_DEF; - cfg->txbuf_size = SOCK_TEST_CFG_TXBUF_SIZE_DEF; - cfg->total_bytes = cfg->num_writes * cfg->txbuf_size; -} - -static inline int -sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg) -{ - /* Note: txbuf & rxbuf on server are the same buffer, - * so txbuf_size is not included in this check. - */ - return ((cfg->magic == valid_cfg->magic) - && (cfg->test == valid_cfg->test) - && (cfg->verbose == valid_cfg->verbose) - && (cfg->rxbuf_size == valid_cfg->rxbuf_size) - && (cfg->num_writes == valid_cfg->num_writes) - && (cfg->total_bytes == valid_cfg->total_bytes)); -} - -static inline void -sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf, - uint32_t *bufsize) -{ - uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size; - uint8_t *lb = realloc (*buf, (size_t) alloc_size); - - if (lb) - { - if (is_rxbuf) - cfg->rxbuf_size = *bufsize = alloc_size; - else - cfg->txbuf_size = *bufsize = alloc_size; - - *buf = lb; - } - else - { - int errno_val = errno; - perror ("ERROR in sock_test_buf_alloc()"); - fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation " - "failed (errno = %d)!\n" - " Using buffer size %d instead of desired" - " size (%d)\n", errno_val, *bufsize, alloc_size); - } -} - -static inline void -sock_test_socket_buf_alloc (sock_test_socket_t *socket) -{ - socket->rxbuf_size = socket->cfg.rxbuf_size; - socket->txbuf_size = socket->cfg.txbuf_size; - sock_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ , - (uint8_t **) &socket->txbuf, &socket->txbuf_size); - sock_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ , - (uint8_t **) &socket->rxbuf, &socket->rxbuf_size); -} - -static inline char * -sock_test_type_str (sock_test_t t) -{ - switch (t) - { - case SOCK_TEST_TYPE_NONE: - return "NONE"; - - case SOCK_TEST_TYPE_ECHO: - return "ECHO"; - - case SOCK_TEST_TYPE_UNI: - return "UNI"; - - case SOCK_TEST_TYPE_BI: - return "BI"; - - case SOCK_TEST_TYPE_EXIT: - return "EXIT"; - - default: - return "Unknown"; - } -} - -static inline void -sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client) -{ - char *spc = " "; - - printf (" test config (%p):\n" - SOCK_TEST_SEPARATOR_STRING - " magic: 0x%08x\n" - " seq_num: 0x%08x\n" - "%-5s test: %s (%d)\n" - " ctrl handle: %d (0x%x)\n" - "%-5s num test sockets: %u (0x%08x)\n" - "%-5s verbose: %s (%d)\n" - "%-5s rxbuf size: %lu (0x%08lx)\n" - "%-5s txbuf size: %lu (0x%08lx)\n" - "%-5s num writes: %lu (0x%08lx)\n" - " client tx bytes: %lu (0x%08lx)\n" - SOCK_TEST_SEPARATOR_STRING, - (void *) cfg, cfg->magic, cfg->seq_num, - is_client && (cfg->test == SOCK_TEST_TYPE_UNI) ? - "'"SOCK_TEST_TOKEN_RUN_UNI"'" : - is_client && (cfg->test == SOCK_TEST_TYPE_BI) ? - "'"SOCK_TEST_TOKEN_RUN_BI"'" : spc, - sock_test_type_str (cfg->test), cfg->test, - cfg->ctrl_handle, cfg->ctrl_handle, - is_client ? "'"SOCK_TEST_TOKEN_NUM_TEST_SCKTS"'" : spc, - cfg->num_test_sockets, cfg->num_test_sockets, - is_client ? "'"SOCK_TEST_TOKEN_VERBOSE"'" : spc, - cfg->verbose ? "on" : "off", cfg->verbose, - is_client ? "'"SOCK_TEST_TOKEN_RXBUF_SIZE"'" : spc, - cfg->rxbuf_size, cfg->rxbuf_size, - is_client ? "'"SOCK_TEST_TOKEN_TXBUF_SIZE"'" : spc, - cfg->txbuf_size, cfg->txbuf_size, - is_client ? "'"SOCK_TEST_TOKEN_NUM_WRITES"'" : spc, - cfg->num_writes, cfg->num_writes, - cfg->total_bytes, cfg->total_bytes); -} - -static inline void -sock_test_stats_dump (char * header, sock_test_stats_t * stats, - uint8_t show_rx, uint8_t show_tx, - uint8_t verbose) -{ - struct timespec diff; - double duration, rate; - uint64_t total_bytes; - - if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0) - { - diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1; - diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1000000000; - } - else - { - diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec; - diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec; - } - duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec); - - total_bytes = stats->tx_bytes + stats->rx_bytes; - rate = (double) total_bytes * 8 / duration / ONE_GIG; - printf ("\n%s: Streamed %lu bytes\n" - " in %lf seconds (%lf Gbps %s-duplex)!\n", - header, total_bytes, duration, rate, - (show_rx && show_tx) ? "full" : "half"); - - if (show_tx) - { - printf (SOCK_TEST_SEPARATOR_STRING - " tx stats (0x%p):\n" - SOCK_TEST_SEPARATOR_STRING - " writes: %lu (0x%08lx)\n" - " tx bytes: %lu (0x%08lx)\n" - " tx eagain: %u (0x%08x)\n" - " tx incomplete: %u (0x%08x)\n", - (void *)stats, stats->tx_xacts, stats->tx_xacts, - stats->tx_bytes, stats->tx_bytes, - stats->tx_eagain, stats->tx_eagain, - stats->tx_incomp, stats->tx_incomp); - } - if (show_rx) - { - printf (SOCK_TEST_SEPARATOR_STRING - " rx stats (0x%p):\n" - SOCK_TEST_SEPARATOR_STRING - " reads: %lu (0x%08lx)\n" - " rx bytes: %lu (0x%08lx)\n" - " rx eagain: %u (0x%08x)\n" - " rx incomplete: %u (0x%08x)\n", - (void *)stats, stats->rx_xacts, stats->rx_xacts, - stats->rx_bytes, stats->rx_bytes, - stats->rx_eagain, stats->rx_eagain, - stats->rx_incomp, stats->rx_incomp); - } - if (verbose) - printf (" start.tv_sec: %ld\n" - " start.tv_nsec: %ld\n" - " stop.tv_sec: %ld\n" - " stop.tv_nsec: %ld\n", - stats->start.tv_sec, stats->start.tv_nsec, - stats->stop.tv_sec, stats->stop.tv_nsec); - - printf (SOCK_TEST_SEPARATOR_STRING); - -#if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST) - printf (" af_unix xacts: %lu (0x%08lx)\n", - sock_server_main.af_unix_xacts); - - printf (SOCK_TEST_SEPARATOR_STRING); -#endif -} - static inline int sock_test_read (int fd, uint8_t *buf, uint32_t nbytes, sock_test_stats_t *stats) @@ -330,17 +37,7 @@ sock_test_read (int fd, uint8_t *buf, uint32_t nbytes, { if (stats) stats->rx_xacts++; -#ifdef VCL_TEST - rx_bytes = vppcom_session_read (fd, buf, nbytes); - - if (rx_bytes < 0) - { - errno = -rx_bytes; - rx_bytes = -1; - } -#else rx_bytes = read (fd, buf, nbytes); -#endif if (stats) { if ((rx_bytes == 0) || @@ -379,16 +76,7 @@ sock_test_write (int fd, uint8_t *buf, uint32_t nbytes, { if (stats) stats->tx_xacts++; -#ifdef VCL_TEST - rv = vppcom_session_write (fd, buf, nbytes_left); - if (rv < 0) - { - errno = -rv; - rv = -1; - } -#else rv = write (fd, buf, nbytes_left); -#endif if (rv < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) diff --git a/src/vcl/sock_test_common.h b/src/vcl/sock_test_common.h new file mode 100644 index 00000000000..7fbe52d0a93 --- /dev/null +++ b/src/vcl/sock_test_common.h @@ -0,0 +1,311 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __sock_test_common_h__ +#define __sock_test_common_h__ + +#include <netdb.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> + +#define SOCK_TEST_TOKEN_HELP "#H" +#define SOCK_TEST_TOKEN_EXIT "#X" +#define SOCK_TEST_TOKEN_VERBOSE "#V" +#define SOCK_TEST_TOKEN_TXBUF_SIZE "#T:" +#define SOCK_TEST_TOKEN_NUM_TEST_SCKTS "#I:" +#define SOCK_TEST_TOKEN_NUM_WRITES "#N:" +#define SOCK_TEST_TOKEN_RXBUF_SIZE "#R:" +#define SOCK_TEST_TOKEN_SHOW_CFG "#C" +#define SOCK_TEST_TOKEN_RUN_UNI "#U" +#define SOCK_TEST_TOKEN_RUN_BI "#B" + +#define SOCK_TEST_BANNER_STRING \ + "============================================\n" +#define SOCK_TEST_SEPARATOR_STRING \ + " -----------------------------\n" + +#define ONE_GIG (1024*1024*1024) +#define SOCK_TEST_SERVER_PORT 22000 +#define SOCK_TEST_LOCALHOST_IPADDR "127.0.0.1" + +#define SOCK_TEST_CFG_CTRL_MAGIC 0xfeedface +#define SOCK_TEST_CFG_NUM_WRITES_DEF 1000000 +#define SOCK_TEST_CFG_TXBUF_SIZE_DEF 8192 +#define SOCK_TEST_CFG_RXBUF_SIZE_DEF (64*SOCK_TEST_CFG_TXBUF_SIZE_DEF) +#define SOCK_TEST_CFG_BUF_SIZE_MIN 128 +#define SOCK_TEST_CFG_MAX_TEST_SCKTS 5 + +typedef enum +{ + SOCK_TEST_TYPE_NONE, + SOCK_TEST_TYPE_ECHO, + SOCK_TEST_TYPE_UNI, + SOCK_TEST_TYPE_BI, + SOCK_TEST_TYPE_EXIT, +} sock_test_t; + +typedef struct __attribute__ ((packed)) +{ + uint32_t magic; + uint32_t seq_num; + uint32_t test; + uint32_t ctrl_handle; + uint32_t num_test_sockets; + uint32_t verbose; + uint32_t address_ip6; + uint32_t transport_udp; + uint64_t rxbuf_size; + uint64_t txbuf_size; + uint64_t num_writes; + uint64_t total_bytes; +} sock_test_cfg_t; + +typedef struct +{ + uint64_t rx_xacts; + uint64_t rx_bytes; + uint32_t rx_eagain; + uint32_t rx_incomp; + uint64_t tx_xacts; + uint64_t tx_bytes; + uint32_t tx_eagain; + uint32_t tx_incomp; + struct timespec start; + struct timespec stop; +} sock_test_stats_t; + +typedef struct +{ + int fd; + uint32_t txbuf_size; + char *txbuf; + uint32_t rxbuf_size; + char *rxbuf; + sock_test_cfg_t cfg; + sock_test_stats_t stats; +} sock_test_socket_t; + +static inline void +sock_test_stats_accumulate (sock_test_stats_t * accum, + sock_test_stats_t * incr) +{ + accum->rx_xacts += incr->rx_xacts; + accum->rx_bytes += incr->rx_bytes; + accum->rx_eagain += incr->rx_eagain; + accum->rx_incomp += incr->rx_incomp; + accum->tx_xacts += incr->tx_xacts; + accum->tx_bytes += incr->tx_bytes; + accum->tx_eagain += incr->tx_eagain; + accum->tx_incomp += incr->tx_incomp; +} + +static inline void +sock_test_cfg_init (sock_test_cfg_t *cfg) +{ + cfg->magic = SOCK_TEST_CFG_CTRL_MAGIC; + cfg->test = SOCK_TEST_TYPE_NONE; + cfg->ctrl_handle = ~0; + cfg->num_test_sockets = 1; + cfg->verbose = 0; + cfg->rxbuf_size = SOCK_TEST_CFG_RXBUF_SIZE_DEF; + cfg->num_writes = SOCK_TEST_CFG_NUM_WRITES_DEF; + cfg->txbuf_size = SOCK_TEST_CFG_TXBUF_SIZE_DEF; + cfg->total_bytes = cfg->num_writes * cfg->txbuf_size; +} + +static inline int +sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg) +{ + /* Note: txbuf & rxbuf on server are the same buffer, + * so txbuf_size is not included in this check. + */ + return ((cfg->magic == valid_cfg->magic) + && (cfg->test == valid_cfg->test) + && (cfg->verbose == valid_cfg->verbose) + && (cfg->rxbuf_size == valid_cfg->rxbuf_size) + && (cfg->num_writes == valid_cfg->num_writes) + && (cfg->total_bytes == valid_cfg->total_bytes)); +} + +static inline void +sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf, + uint32_t *bufsize) +{ + uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size; + uint8_t *lb = realloc (*buf, (size_t) alloc_size); + + if (lb) + { + if (is_rxbuf) + cfg->rxbuf_size = *bufsize = alloc_size; + else + cfg->txbuf_size = *bufsize = alloc_size; + + *buf = lb; + } + else + { + int errno_val = errno; + perror ("ERROR in sock_test_buf_alloc()"); + fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation " + "failed (errno = %d)!\n" + " Using buffer size %d instead of desired" + " size (%d)\n", errno_val, *bufsize, alloc_size); + } +} + +static inline void +sock_test_socket_buf_alloc (sock_test_socket_t *socket) +{ + socket->rxbuf_size = socket->cfg.rxbuf_size; + socket->txbuf_size = socket->cfg.txbuf_size; + sock_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ , + (uint8_t **) &socket->txbuf, &socket->txbuf_size); + sock_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ , + (uint8_t **) &socket->rxbuf, &socket->rxbuf_size); +} + +static inline char * +sock_test_type_str (sock_test_t t) +{ + switch (t) + { + case SOCK_TEST_TYPE_NONE: + return "NONE"; + + case SOCK_TEST_TYPE_ECHO: + return "ECHO"; + + case SOCK_TEST_TYPE_UNI: + return "UNI"; + + case SOCK_TEST_TYPE_BI: + return "BI"; + + case SOCK_TEST_TYPE_EXIT: + return "EXIT"; + + default: + return "Unknown"; + } +} + +static inline void +sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client) +{ + char *spc = " "; + + printf (" test config (%p):\n" + SOCK_TEST_SEPARATOR_STRING + " magic: 0x%08x\n" + " seq_num: 0x%08x\n" + "%-5s test: %s (%d)\n" + " ctrl handle: %d (0x%x)\n" + "%-5s num test sockets: %u (0x%08x)\n" + "%-5s verbose: %s (%d)\n" + "%-5s rxbuf size: %lu (0x%08lx)\n" + "%-5s txbuf size: %lu (0x%08lx)\n" + "%-5s num writes: %lu (0x%08lx)\n" + " client tx bytes: %lu (0x%08lx)\n" + SOCK_TEST_SEPARATOR_STRING, + (void *) cfg, cfg->magic, cfg->seq_num, + is_client && (cfg->test == SOCK_TEST_TYPE_UNI) ? + "'"SOCK_TEST_TOKEN_RUN_UNI"'" : + is_client && (cfg->test == SOCK_TEST_TYPE_BI) ? + "'"SOCK_TEST_TOKEN_RUN_BI"'" : spc, + sock_test_type_str (cfg->test), cfg->test, + cfg->ctrl_handle, cfg->ctrl_handle, + is_client ? "'"SOCK_TEST_TOKEN_NUM_TEST_SCKTS"'" : spc, + cfg->num_test_sockets, cfg->num_test_sockets, + is_client ? "'"SOCK_TEST_TOKEN_VERBOSE"'" : spc, + cfg->verbose ? "on" : "off", cfg->verbose, + is_client ? "'"SOCK_TEST_TOKEN_RXBUF_SIZE"'" : spc, + cfg->rxbuf_size, cfg->rxbuf_size, + is_client ? "'"SOCK_TEST_TOKEN_TXBUF_SIZE"'" : spc, + cfg->txbuf_size, cfg->txbuf_size, + is_client ? "'"SOCK_TEST_TOKEN_NUM_WRITES"'" : spc, + cfg->num_writes, cfg->num_writes, + cfg->total_bytes, cfg->total_bytes); +} + +static inline void +sock_test_stats_dump (char * header, sock_test_stats_t * stats, + uint8_t show_rx, uint8_t show_tx, + uint8_t verbose) +{ + struct timespec diff; + double duration, rate; + uint64_t total_bytes; + + if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0) + { + diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1; + diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1000000000; + } + else + { + diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec; + diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec; + } + duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec); + + total_bytes = stats->tx_bytes + stats->rx_bytes; + rate = (double) total_bytes * 8 / duration / ONE_GIG; + printf ("\n%s: Streamed %lu bytes\n" + " in %lf seconds (%lf Gbps %s-duplex)!\n", + header, total_bytes, duration, rate, + (show_rx && show_tx) ? "full" : "half"); + + if (show_tx) + { + printf (SOCK_TEST_SEPARATOR_STRING + " tx stats (0x%p):\n" + SOCK_TEST_SEPARATOR_STRING + " writes: %lu (0x%08lx)\n" + " tx bytes: %lu (0x%08lx)\n" + " tx eagain: %u (0x%08x)\n" + " tx incomplete: %u (0x%08x)\n", + (void *)stats, stats->tx_xacts, stats->tx_xacts, + stats->tx_bytes, stats->tx_bytes, + stats->tx_eagain, stats->tx_eagain, + stats->tx_incomp, stats->tx_incomp); + } + if (show_rx) + { + printf (SOCK_TEST_SEPARATOR_STRING + " rx stats (0x%p):\n" + SOCK_TEST_SEPARATOR_STRING + " reads: %lu (0x%08lx)\n" + " rx bytes: %lu (0x%08lx)\n" + " rx eagain: %u (0x%08x)\n" + " rx incomplete: %u (0x%08x)\n", + (void *)stats, stats->rx_xacts, stats->rx_xacts, + stats->rx_bytes, stats->rx_bytes, + stats->rx_eagain, stats->rx_eagain, + stats->rx_incomp, stats->rx_incomp); + } + if (verbose) + printf (" start.tv_sec: %ld\n" + " start.tv_nsec: %ld\n" + " stop.tv_sec: %ld\n" + " stop.tv_nsec: %ld\n", + stats->start.tv_sec, stats->start.tv_nsec, + stats->stop.tv_sec, stats->stop.tv_nsec); + + printf (SOCK_TEST_SEPARATOR_STRING); +} + +#endif /* __sock_test_common_h__ */ diff --git a/src/vcl/vcl_test.h b/src/vcl/vcl_test.h new file mode 100644 index 00000000000..0f3bd2d3a40 --- /dev/null +++ b/src/vcl/vcl_test.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __vcl_test_h__ +#define __vcl_test_h__ + +#include <netdb.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <vcl/vppcom.h> +#include <vcl/sock_test_common.h> + +static inline int +vcl_test_read (int fd, uint8_t *buf, uint32_t nbytes, + sock_test_stats_t *stats) +{ + int rx_bytes, errno_val; + + do + { + if (stats) + stats->rx_xacts++; + rx_bytes = vppcom_session_read (fd, buf, nbytes); + + if (rx_bytes < 0) + { + errno = -rx_bytes; + rx_bytes = -1; + } + if (stats) + { + if ((rx_bytes == 0) || + ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))) + stats->rx_eagain++; + else if (rx_bytes < nbytes) + stats->rx_incomp++; + } + } + while ((rx_bytes == 0) || + ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))); + + if (rx_bytes < 0) + { + errno_val = errno; + perror ("ERROR in sock_test_read()"); + fprintf (stderr, "SOCK_TEST: ERROR: socket read " + "failed (errno = %d)!\n", errno_val); + errno = errno_val; + } + else if (stats) + stats->rx_bytes += rx_bytes; + + return (rx_bytes); +} + +static inline int +vcl_test_write (int fd, uint8_t *buf, uint32_t nbytes, + sock_test_stats_t *stats, uint32_t verbose) +{ + int tx_bytes = 0; + int nbytes_left = nbytes; + int rv, errno_val; + + do + { + if (stats) + stats->tx_xacts++; + rv = vppcom_session_write (fd, buf, nbytes_left); + if (rv < 0) + { + errno = -rv; + rv = -1; + } + if (rv < 0) + { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + { + if (stats) + stats->tx_eagain++; + continue; + } + else + break; + } + tx_bytes += rv; + + if (tx_bytes != nbytes) + { + nbytes_left = nbytes_left - rv; + if (stats) + stats->tx_incomp++; + if (verbose) + { + printf ("SOCK_TEST: WARNING: bytes written (%d) " + "!= bytes to write (%d)!\n", tx_bytes, nbytes); + } + } + + } while (tx_bytes != nbytes); + + if (tx_bytes < 0) + { + errno_val = errno; + perror ("ERROR in sock_test_write()"); + fprintf (stderr, "SOCK_TEST: ERROR: socket write failed " + "(errno = %d)!\n", errno_val); + } + else if (stats) + stats->tx_bytes += tx_bytes; + + return (tx_bytes); +} + +#endif /* __vcl_test_h__ */ diff --git a/src/vcl/vcl_test_client.c b/src/vcl/vcl_test_client.c index e1a4c6b74d4..af3e01dbb20 100644 --- a/src/vcl/vcl_test_client.c +++ b/src/vcl/vcl_test_client.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017-2018 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -13,10 +13,1098 @@ * limitations under the License. */ -#define VCL_TEST +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <ctype.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <stdio.h> +#include <time.h> +#include <arpa/inet.h> +#include <vcl/vcl_test.h> -#include <vcl/vppcom.h> -#include <vcl/sock_test_client.c> +typedef struct +{ + vppcom_endpt_t server_endpt; + struct sockaddr_storage server_addr; + uint32_t server_addr_size; + uint32_t cfg_seq_num; + sock_test_socket_t ctrl_socket; + sock_test_socket_t *test_socket; + uint32_t num_test_sockets; + uint8_t dump_cfg; +} sock_client_main_t; + +sock_client_main_t sock_client_main; + + +static int +sock_test_cfg_sync (sock_test_socket_t * socket) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + sock_test_cfg_t *rl_cfg = (sock_test_cfg_t *) socket->rxbuf; + int rx_bytes, tx_bytes; + + if (socket->cfg.verbose) + sock_test_cfg_dump (&socket->cfg, 1 /* is_client */ ); + + ctrl->cfg.seq_num = ++scm->cfg_seq_num; + if (socket->cfg.verbose) + { + printf ("CLIENT (fd %d): Sending config sent to server.\n", socket->fd); + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + } + tx_bytes = vcl_test_write (socket->fd, (uint8_t *) & ctrl->cfg, + sizeof (ctrl->cfg), NULL, ctrl->cfg.verbose); + if (tx_bytes < 0) + { + fprintf (stderr, "CLIENT (fd %d): ERROR: write test cfg failed (%d)!\n", + socket->fd, tx_bytes); + return tx_bytes; + } + + rx_bytes = vcl_test_read (socket->fd, (uint8_t *) socket->rxbuf, + sizeof (sock_test_cfg_t), NULL); + if (rx_bytes < 0) + return rx_bytes; + + if (rl_cfg->magic != SOCK_TEST_CFG_CTRL_MAGIC) + { + fprintf (stderr, "CLIENT (fd %d): ERROR: Bad server reply cfg " + "-- aborting!\n", socket->fd); + return -1; + } + if ((rx_bytes != sizeof (sock_test_cfg_t)) + || !sock_test_cfg_verify (rl_cfg, &ctrl->cfg)) + { + fprintf (stderr, "CLIENT (fd %d): ERROR: Invalid config received " + "from server!\n", socket->fd); + if (rx_bytes != sizeof (sock_test_cfg_t)) + { + fprintf (stderr, "\tRx bytes %d != cfg size %lu\n", + rx_bytes, sizeof (sock_test_cfg_t)); + } + else + { + sock_test_cfg_dump (rl_cfg, 1 /* is_client */ ); + fprintf (stderr, "CLIENT (fd %d): Valid config sent to server.\n", + socket->fd); + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + } + return -1; + } + else if (socket->cfg.verbose) + { + printf ("CLIENT (fd %d): Got config back from server.\n", socket->fd); + sock_test_cfg_dump (rl_cfg, 1 /* is_client */ ); + } + ctrl->cfg.ctrl_handle = ((ctrl->cfg.ctrl_handle == ~0) ? + rl_cfg->ctrl_handle : ctrl->cfg.ctrl_handle); + + return 0; +} + +static void +echo_test_client () +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + sock_test_socket_t *tsock; + int rx_bytes, tx_bytes, nbytes; + uint32_t i, n; + int rv; + int nfds = 0; + fd_set wr_fdset, rd_fdset; + fd_set _wfdset, *wfdset = &_wfdset; + fd_set _rfdset, *rfdset = &_rfdset; + + FD_ZERO (&wr_fdset); + FD_ZERO (&rd_fdset); + memset (&ctrl->stats, 0, sizeof (ctrl->stats)); + ctrl->cfg.total_bytes = nbytes = strlen (ctrl->txbuf) + 1; + for (n = 0; n != ctrl->cfg.num_test_sockets; n++) + { + tsock = &scm->test_socket[n]; + tsock->cfg = ctrl->cfg; + sock_test_socket_buf_alloc (tsock); + if (sock_test_cfg_sync (tsock)) + return; + + memcpy (tsock->txbuf, ctrl->txbuf, nbytes); + memset (&tsock->stats, 0, sizeof (tsock->stats)); + + FD_SET (tsock->fd, &wr_fdset); + FD_SET (tsock->fd, &rd_fdset); + nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds; + } + + nfds++; + clock_gettime (CLOCK_REALTIME, &ctrl->stats.start); + while (n) + { + _wfdset = wr_fdset; + _rfdset = rd_fdset; + + rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset, + NULL, 0); + if (rv < 0) + { + perror ("select()"); + fprintf (stderr, "\nCLIENT: ERROR: select() failed -- " + "aborting test!\n"); + return; + } + else if (rv == 0) + continue; + + for (i = 0; i < ctrl->cfg.num_test_sockets; i++) + { + tsock = &scm->test_socket[i]; + if (!((tsock->stats.stop.tv_sec == 0) && + (tsock->stats.stop.tv_nsec == 0))) + continue; + + if (FD_ISSET (tsock->fd, wfdset) && + (tsock->stats.tx_bytes < ctrl->cfg.total_bytes)) + + { + tx_bytes = + vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf, nbytes, + &tsock->stats, ctrl->cfg.verbose); + if (tx_bytes < 0) + { + fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) " + "failed -- aborting test!\n", tsock->fd); + return; + } + + printf ("CLIENT (fd %d): TX (%d bytes) - '%s'\n", + tsock->fd, tx_bytes, tsock->txbuf); + } + + if ((FD_ISSET (tsock->fd, rfdset)) && + (tsock->stats.rx_bytes < ctrl->cfg.total_bytes)) + { + rx_bytes = + vcl_test_read (tsock->fd, (uint8_t *) tsock->rxbuf, + nbytes, &tsock->stats); + if (rx_bytes > 0) + { + printf ("CLIENT (fd %d): RX (%d bytes) - '%s'\n", + tsock->fd, rx_bytes, tsock->rxbuf); + + if (tsock->stats.rx_bytes != tsock->stats.tx_bytes) + printf ("CLIENT: WARNING: bytes read (%lu) " + "!= bytes written (%lu)!\n", + tsock->stats.rx_bytes, tsock->stats.tx_bytes); + } + } + + if (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes) + { + clock_gettime (CLOCK_REALTIME, &tsock->stats.stop); + n--; + } + } + } + clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop); + + for (i = 0; i < ctrl->cfg.num_test_sockets; i++) + { + tsock = &scm->test_socket[i]; + tsock->stats.start = ctrl->stats.start; + + if (ctrl->cfg.verbose) + { + static char buf[64]; + + sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd); + sock_test_stats_dump (buf, &tsock->stats, + 1 /* show_rx */ , 1 /* show tx */ , + ctrl->cfg.verbose); + } + + sock_test_stats_accumulate (&ctrl->stats, &tsock->stats); + } + + if (ctrl->cfg.verbose) + { + sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats, + 1 /* show_rx */ , 1 /* show tx */ , + ctrl->cfg.verbose); + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + + if (ctrl->cfg.verbose > 1) + { + printf (" ctrl socket info\n" + SOCK_TEST_SEPARATOR_STRING + " fd: %d (0x%08x)\n" + " rxbuf: %p\n" + " rxbuf size: %u (0x%08x)\n" + " txbuf: %p\n" + " txbuf size: %u (0x%08x)\n" + SOCK_TEST_SEPARATOR_STRING, + ctrl->fd, (uint32_t) ctrl->fd, + ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size, + ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size); + } + } +} + +static void +stream_test_client (sock_test_t test) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + sock_test_socket_t *tsock; + int tx_bytes; + uint32_t i, n; + int rv; + int nfds = 0; + fd_set wr_fdset, rd_fdset; + fd_set _wfdset, *wfdset = &_wfdset; + fd_set _rfdset, *rfdset = (test == SOCK_TEST_TYPE_BI) ? &_rfdset : 0; + + ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; + ctrl->cfg.ctrl_handle = ~0; + + printf ("\n" SOCK_TEST_BANNER_STRING + "CLIENT (fd %d): %s-directional Stream Test!\n\n" + "CLIENT (fd %d): Sending config to server on ctrl socket...\n", + ctrl->fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni", ctrl->fd); + + if (sock_test_cfg_sync (ctrl)) + { + fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!"); + return; + } + + FD_ZERO (&wr_fdset); + FD_ZERO (&rd_fdset); + memset (&ctrl->stats, 0, sizeof (ctrl->stats)); + for (n = 0; n != ctrl->cfg.num_test_sockets; n++) + { + tsock = &scm->test_socket[n]; + tsock->cfg = ctrl->cfg; + sock_test_socket_buf_alloc (tsock); + printf ("CLIENT (fd %d): Sending config to server on " + "test socket %d...\n", tsock->fd, n); + sock_test_cfg_sync (tsock); + + /* Fill payload with incrementing uint32's */ + for (i = 0; i < tsock->txbuf_size; i++) + tsock->txbuf[i] = i & 0xff; + + memset (&tsock->stats, 0, sizeof (tsock->stats)); + FD_SET (tsock->fd, &wr_fdset); + FD_SET (tsock->fd, &rd_fdset); + nfds = ((tsock->fd + 1) > nfds) ? (tsock->fd + 1) : nfds; + } + + nfds++; + clock_gettime (CLOCK_REALTIME, &ctrl->stats.start); + while (n) + { + _wfdset = wr_fdset; + _rfdset = rd_fdset; + + rv = vppcom_select (nfds, (uint64_t *) rfdset, (uint64_t *) wfdset, + NULL, 0); + if (rv < 0) + { + perror ("select()"); + fprintf (stderr, "\nCLIENT: ERROR: select() failed -- " + "aborting test!\n"); + return; + } + else if (rv == 0) + continue; + + for (i = 0; i < ctrl->cfg.num_test_sockets; i++) + { + tsock = &scm->test_socket[i]; + if (!((tsock->stats.stop.tv_sec == 0) && + (tsock->stats.stop.tv_nsec == 0))) + continue; + + if ((test == SOCK_TEST_TYPE_BI) && + FD_ISSET (tsock->fd, rfdset) && + (tsock->stats.rx_bytes < ctrl->cfg.total_bytes)) + { + (void) vcl_test_read (tsock->fd, + (uint8_t *) tsock->rxbuf, + tsock->rxbuf_size, &tsock->stats); + } + + if (FD_ISSET (tsock->fd, wfdset) && + (tsock->stats.tx_bytes < ctrl->cfg.total_bytes)) + { + tx_bytes = + vcl_test_write (tsock->fd, (uint8_t *) tsock->txbuf, + ctrl->cfg.txbuf_size, &tsock->stats, + ctrl->cfg.verbose); + if (tx_bytes < 0) + { + fprintf (stderr, "\nCLIENT: ERROR: vcl_test_write(%d) " + "failed -- aborting test!\n", tsock->fd); + return; + } + } + + if (((test == SOCK_TEST_TYPE_UNI) && + (tsock->stats.tx_bytes >= ctrl->cfg.total_bytes)) || + ((test == SOCK_TEST_TYPE_BI) && + (tsock->stats.rx_bytes >= ctrl->cfg.total_bytes))) + { + clock_gettime (CLOCK_REALTIME, &tsock->stats.stop); + n--; + } + } + } + clock_gettime (CLOCK_REALTIME, &ctrl->stats.stop); + + printf ("CLIENT (fd %d): Sending config to server on ctrl socket...\n", + ctrl->fd); + + if (sock_test_cfg_sync (ctrl)) + { + fprintf (stderr, "CLIENT: ERROR: test cfg sync failed -- aborting!"); + return; + } + + for (i = 0; i < ctrl->cfg.num_test_sockets; i++) + { + tsock = &scm->test_socket[i]; + + if (ctrl->cfg.verbose) + { + static char buf[64]; + + sprintf (buf, "CLIENT (fd %d) RESULTS", tsock->fd); + sock_test_stats_dump (buf, &tsock->stats, + test == SOCK_TEST_TYPE_BI /* show_rx */ , + 1 /* show tx */ , ctrl->cfg.verbose); + } + + sock_test_stats_accumulate (&ctrl->stats, &tsock->stats); + } + + sock_test_stats_dump ("CLIENT RESULTS", &ctrl->stats, + test == SOCK_TEST_TYPE_BI /* show_rx */ , + 1 /* show tx */ , ctrl->cfg.verbose); + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + + if (ctrl->cfg.verbose) + { + printf (" ctrl socket info\n" + SOCK_TEST_SEPARATOR_STRING + " fd: %d (0x%08x)\n" + " rxbuf: %p\n" + " rxbuf size: %u (0x%08x)\n" + " txbuf: %p\n" + " txbuf size: %u (0x%08x)\n" + SOCK_TEST_SEPARATOR_STRING, + ctrl->fd, (uint32_t) ctrl->fd, + ctrl->rxbuf, ctrl->rxbuf_size, ctrl->rxbuf_size, + ctrl->txbuf, ctrl->txbuf_size, ctrl->txbuf_size); + } + + ctrl->cfg.test = SOCK_TEST_TYPE_ECHO; + if (sock_test_cfg_sync (ctrl)) + fprintf (stderr, "CLIENT: ERROR: post-test cfg sync failed!"); + + printf ("CLIENT (fd %d): %s-directional Stream Test Complete!\n" + SOCK_TEST_BANNER_STRING "\n", ctrl->fd, + test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); +} + +static void +exit_client (void) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + sock_test_socket_t *tsock; + int i; + + for (i = 0; i < ctrl->cfg.num_test_sockets; i++) + { + tsock = &scm->test_socket[i]; + tsock->cfg.test = SOCK_TEST_TYPE_EXIT; + + /* coverity[COPY_PASTE_ERROR] */ + if (ctrl->cfg.verbose) + { + printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", + tsock->fd); + sock_test_cfg_dump (&tsock->cfg, 1 /* is_client */ ); + } + (void) vcl_test_write (tsock->fd, (uint8_t *) & tsock->cfg, + sizeof (tsock->cfg), &tsock->stats, + ctrl->cfg.verbose); + } + + ctrl->cfg.test = SOCK_TEST_TYPE_EXIT; + if (ctrl->cfg.verbose) + { + printf ("\nCLIENT (fd %d): Sending exit cfg to server...\n", ctrl->fd); + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + } + (void) vcl_test_write (ctrl->fd, (uint8_t *) & ctrl->cfg, + sizeof (ctrl->cfg), &ctrl->stats, ctrl->cfg.verbose); + printf ("\nCLIENT: So long and thanks for all the fish!\n\n"); + sleep (1); +} + +static int +sock_test_connect_test_sockets (uint32_t num_test_sockets) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + sock_test_socket_t *tsock; + int i, rv, errno_val; + + if (num_test_sockets < 1) + { + errno = EINVAL; + return -1; + } + + if (num_test_sockets < scm->num_test_sockets) + { + for (i = scm->num_test_sockets - 1; i >= num_test_sockets; i--) + { + tsock = &scm->test_socket[i]; + vppcom_session_close (tsock->fd); + free (tsock->txbuf); + free (tsock->rxbuf); + } + } + + else if (num_test_sockets > scm->num_test_sockets) + { + tsock = realloc (scm->test_socket, + sizeof (sock_test_socket_t) * num_test_sockets); + if (!tsock) + { + errno_val = errno; + perror ("ERROR in sock_test_connect_test_sockets()"); + fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n", + errno_val); + return -1; + } + + if (!scm->test_socket) + memset (tsock, 0, sizeof (*tsock)); + + scm->test_socket = tsock; + for (i = scm->num_test_sockets; i < num_test_sockets; i++) + { + tsock = &scm->test_socket[i]; + tsock->fd = vppcom_session_create (ctrl->cfg.transport_udp ? + VPPCOM_PROTO_UDP : + VPPCOM_PROTO_TCP, + 1 /* is_nonblocking */ ); + if (tsock->fd < 0) + { + errno = -tsock->fd; + tsock->fd = -1; + } + if (tsock->fd < 0) + { + errno_val = errno; + perror ("ERROR in sock_test_connect_test_sockets()"); + fprintf (stderr, "CLIENT: ERROR: socket failed (errno = %d)!\n", + errno_val); + return tsock->fd; + } + + rv = vppcom_session_connect (tsock->fd, &scm->server_endpt); + if (rv) + { + errno = -rv; + rv = -1; + } + if (rv < 0) + { + errno_val = errno; + perror ("ERROR in sock_test_connect_test_sockets()"); + fprintf (stderr, "CLIENT: ERROR: connect failed " + "(errno = %d)!\n", errno_val); + return -1; + } + tsock->cfg = ctrl->cfg; + sock_test_socket_buf_alloc (tsock); + sock_test_cfg_sync (tsock); + + printf ("CLIENT (fd %d): Test socket %d connected.\n", + tsock->fd, i); + } + } + + scm->num_test_sockets = num_test_sockets; + printf ("CLIENT: All sockets (%d) connected!\n", scm->num_test_sockets + 1); + return 0; +} + +static void +dump_help (void) +{ +#define INDENT "\n " + + printf ("CLIENT: Test configuration commands:" + INDENT SOCK_TEST_TOKEN_HELP + "\t\t\tDisplay help." + INDENT SOCK_TEST_TOKEN_EXIT + "\t\t\tExit test client & server." + INDENT SOCK_TEST_TOKEN_SHOW_CFG + "\t\t\tShow the current test cfg." + INDENT SOCK_TEST_TOKEN_RUN_UNI + "\t\t\tRun the Uni-directional test." + INDENT SOCK_TEST_TOKEN_RUN_BI + "\t\t\tRun the Bi-directional test." + INDENT SOCK_TEST_TOKEN_VERBOSE + "\t\t\tToggle verbose setting." + INDENT SOCK_TEST_TOKEN_RXBUF_SIZE + "<rxbuf size>\tRx buffer size (bytes)." + INDENT SOCK_TEST_TOKEN_TXBUF_SIZE + "<txbuf size>\tTx buffer size (bytes)." + INDENT SOCK_TEST_TOKEN_NUM_WRITES + "<# of writes>\tNumber of txbuf writes to server." "\n"); +} + +static void +cfg_txbuf_size_set (void) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE); + uint64_t txbuf_size = strtoull ((const char *) p, NULL, 10); + + if (txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) + { + ctrl->cfg.txbuf_size = txbuf_size; + ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; + sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , + (uint8_t **) & ctrl->txbuf, &ctrl->txbuf_size); + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + } + else + fprintf (stderr, "CLIENT: ERROR: Invalid txbuf size (%lu) < " + "minimum buf size (%u)!\n", + txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); +} + +static void +cfg_num_writes_set (void) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_WRITES); + uint32_t num_writes = strtoul ((const char *) p, NULL, 10); + + if (num_writes > 0) + { + ctrl->cfg.num_writes = num_writes; + ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + } + else + { + fprintf (stderr, "CLIENT: ERROR: invalid num writes: %u\n", num_writes); + } +} + +static void +cfg_num_test_sockets_set (void) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS); + uint32_t num_test_sockets = strtoul ((const char *) p, NULL, 10); + + if ((num_test_sockets > 0) && + (num_test_sockets <= SOCK_TEST_CFG_MAX_TEST_SCKTS)) + { + ctrl->cfg.num_test_sockets = num_test_sockets; + sock_test_connect_test_sockets (num_test_sockets); + + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + } + else + { + fprintf (stderr, "CLIENT: ERROR: invalid num test sockets: " + "%u, (%d max)\n", + num_test_sockets, SOCK_TEST_CFG_MAX_TEST_SCKTS); + } +} + +static void +cfg_rxbuf_size_set (void) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + char *p = ctrl->txbuf + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE); + uint64_t rxbuf_size = strtoull ((const char *) p, NULL, 10); + + if (rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) + { + ctrl->cfg.rxbuf_size = rxbuf_size; + sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , + (uint8_t **) & ctrl->rxbuf, &ctrl->rxbuf_size); + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + } + else + fprintf (stderr, "CLIENT: ERROR: Invalid rxbuf size (%lu) < " + "minimum buf size (%u)!\n", + rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); +} + +static void +cfg_verbose_toggle (void) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + + ctrl->cfg.verbose = ctrl->cfg.verbose ? 0 : 1; + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + +} + +static sock_test_t +parse_input () +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + sock_test_t rv = SOCK_TEST_TYPE_NONE; + + if (!strncmp (SOCK_TEST_TOKEN_EXIT, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_EXIT))) + rv = SOCK_TEST_TYPE_EXIT; + + else if (!strncmp (SOCK_TEST_TOKEN_HELP, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_HELP))) + dump_help (); + + else if (!strncmp (SOCK_TEST_TOKEN_SHOW_CFG, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_SHOW_CFG))) + scm->dump_cfg = 1; + + else if (!strncmp (SOCK_TEST_TOKEN_VERBOSE, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_VERBOSE))) + cfg_verbose_toggle (); + + else if (!strncmp (SOCK_TEST_TOKEN_TXBUF_SIZE, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_TXBUF_SIZE))) + cfg_txbuf_size_set (); + + else if (!strncmp (SOCK_TEST_TOKEN_NUM_TEST_SCKTS, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_NUM_TEST_SCKTS))) + cfg_num_test_sockets_set (); + + else if (!strncmp (SOCK_TEST_TOKEN_NUM_WRITES, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_NUM_WRITES))) + cfg_num_writes_set (); + + else if (!strncmp (SOCK_TEST_TOKEN_RXBUF_SIZE, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_RXBUF_SIZE))) + cfg_rxbuf_size_set (); + + else if (!strncmp (SOCK_TEST_TOKEN_RUN_UNI, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_RUN_UNI))) + rv = ctrl->cfg.test = SOCK_TEST_TYPE_UNI; + + else if (!strncmp (SOCK_TEST_TOKEN_RUN_BI, ctrl->txbuf, + strlen (SOCK_TEST_TOKEN_RUN_BI))) + rv = ctrl->cfg.test = SOCK_TEST_TYPE_BI; + + else + rv = SOCK_TEST_TYPE_ECHO; + + return rv; +} + +void +print_usage_and_exit (void) +{ + fprintf (stderr, + "sock_test_client [OPTIONS] <ipaddr> <port>\n" + " OPTIONS\n" + " -h Print this message and exit.\n" + " -6 Use IPv6\n" + " -u Use UDP transport layer\n" + " -c Print test config before test.\n" + " -w <dir> Write test results to <dir>.\n" + " -X Exit after running test.\n" + " -E Run Echo test.\n" + " -N <num-writes> Test Cfg: number of writes.\n" + " -R <rxbuf-size> Test Cfg: rx buffer size.\n" + " -T <txbuf-size> Test Cfg: tx buffer size.\n" + " -U Run Uni-directional test.\n" + " -B Run Bi-directional test.\n" + " -V Verbose mode.\n"); + exit (1); +} + +int +main (int argc, char **argv) +{ + sock_client_main_t *scm = &sock_client_main; + sock_test_socket_t *ctrl = &scm->ctrl_socket; + int c, rv, errno_val; + sock_test_t post_test = SOCK_TEST_TYPE_NONE; + + sock_test_cfg_init (&ctrl->cfg); + sock_test_socket_buf_alloc (ctrl); + + opterr = 0; + while ((c = getopt (argc, argv, "chn:w:XE:I:N:R:T:UBV6D")) != -1) + switch (c) + { + case 'c': + scm->dump_cfg = 1; + break; + + case 's': + if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) + if (sscanf (optarg, "%u", &ctrl->cfg.num_test_sockets) != 1) + { + fprintf (stderr, "CLIENT: ERROR: Invalid value for " + "option -%c!\n", c); + print_usage_and_exit (); + } + if (!ctrl->cfg.num_test_sockets || + (ctrl->cfg.num_test_sockets > FD_SETSIZE)) + { + fprintf (stderr, "CLIENT: ERROR: Invalid number of " + "sockets (%d) specified for option -%c!\n" + " Valid range is 1 - %d\n", + ctrl->cfg.num_test_sockets, c, FD_SETSIZE); + print_usage_and_exit (); + } + break; + + case 'w': + fprintf (stderr, "CLIENT: Writing test results to files is TBD.\n"); + break; + + case 'X': + post_test = SOCK_TEST_TYPE_EXIT; + break; + + case 'E': + if (strlen (optarg) > ctrl->txbuf_size) + { + fprintf (stderr, "CLIENT: ERROR: Option -%c value " + "larger than txbuf size (%d)!\n", + optopt, ctrl->txbuf_size); + print_usage_and_exit (); + } + strcpy (ctrl->txbuf, optarg); + ctrl->cfg.test = SOCK_TEST_TYPE_ECHO; + break; + + case 'I': + if (sscanf (optarg, "0x%x", &ctrl->cfg.num_test_sockets) != 1) + if (sscanf (optarg, "%d", &ctrl->cfg.num_test_sockets) != 1) + { + fprintf (stderr, "CLIENT: ERROR: Invalid value for " + "option -%c!\n", c); + print_usage_and_exit (); + } + if (ctrl->cfg.num_test_sockets > SOCK_TEST_CFG_MAX_TEST_SCKTS) + { + fprintf (stderr, "CLIENT: ERROR: value greater than max " + "number test sockets (%d)!", + SOCK_TEST_CFG_MAX_TEST_SCKTS); + print_usage_and_exit (); + } + break; + + case 'N': + if (sscanf (optarg, "0x%lx", &ctrl->cfg.num_writes) != 1) + if (sscanf (optarg, "%ld", &ctrl->cfg.num_writes) != 1) + { + fprintf (stderr, "CLIENT: ERROR: Invalid value for " + "option -%c!\n", c); + print_usage_and_exit (); + } + ctrl->cfg.total_bytes = ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; + break; + + case 'R': + if (sscanf (optarg, "0x%lx", &ctrl->cfg.rxbuf_size) != 1) + if (sscanf (optarg, "%ld", &ctrl->cfg.rxbuf_size) != 1) + { + fprintf (stderr, "CLIENT: ERROR: Invalid value for " + "option -%c!\n", c); + print_usage_and_exit (); + } + if (ctrl->cfg.rxbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) + { + ctrl->rxbuf_size = ctrl->cfg.rxbuf_size; + sock_test_buf_alloc (&ctrl->cfg, 1 /* is_rxbuf */ , + (uint8_t **) & ctrl->rxbuf, + &ctrl->rxbuf_size); + } + else + { + fprintf (stderr, "CLIENT: ERROR: rxbuf size (%lu) " + "less than minumum (%u)\n", + ctrl->cfg.rxbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); + print_usage_and_exit (); + } + + break; + + case 'T': + if (sscanf (optarg, "0x%lx", &ctrl->cfg.txbuf_size) != 1) + if (sscanf (optarg, "%ld", &ctrl->cfg.txbuf_size) != 1) + { + fprintf (stderr, "CLIENT: ERROR: Invalid value " + "for option -%c!\n", c); + print_usage_and_exit (); + } + if (ctrl->cfg.txbuf_size >= SOCK_TEST_CFG_BUF_SIZE_MIN) + { + ctrl->txbuf_size = ctrl->cfg.txbuf_size; + sock_test_buf_alloc (&ctrl->cfg, 0 /* is_rxbuf */ , + (uint8_t **) & ctrl->txbuf, + &ctrl->txbuf_size); + ctrl->cfg.total_bytes = + ctrl->cfg.num_writes * ctrl->cfg.txbuf_size; + } + else + { + fprintf (stderr, "CLIENT: ERROR: txbuf size (%lu) " + "less than minumum (%u)!\n", + ctrl->cfg.txbuf_size, SOCK_TEST_CFG_BUF_SIZE_MIN); + print_usage_and_exit (); + } + break; + + case 'U': + ctrl->cfg.test = SOCK_TEST_TYPE_UNI; + break; + + case 'B': + ctrl->cfg.test = SOCK_TEST_TYPE_BI; + break; + + case 'V': + ctrl->cfg.verbose = 1; + break; + + case '6': + ctrl->cfg.address_ip6 = 1; + break; + + case 'D': + ctrl->cfg.transport_udp = 1; + break; + + case '?': + switch (optopt) + { + case 'E': + case 'I': + case 'N': + case 'R': + case 'T': + case 'w': + fprintf (stderr, "CLIENT: ERROR: Option -%c " + "requires an argument.\n", optopt); + break; + + default: + if (isprint (optopt)) + fprintf (stderr, "CLIENT: ERROR: Unknown " + "option `-%c'.\n", optopt); + else + fprintf (stderr, "CLIENT: ERROR: Unknown " + "option character `\\x%x'.\n", optopt); + } + /* fall thru */ + case 'h': + default: + print_usage_and_exit (); + } + + if (argc < (optind + 2)) + { + fprintf (stderr, "CLIENT: ERROR: Insufficient number of arguments!\n"); + print_usage_and_exit (); + } + + ctrl->fd = vppcom_app_create ("vcl_test_client"); + if (ctrl->fd < 0) + { + errno = -ctrl->fd; + ctrl->fd = -1; + } + else + { + ctrl->fd = vppcom_session_create (ctrl->cfg.transport_udp ? + VPPCOM_PROTO_UDP : + VPPCOM_PROTO_TCP, + 0 /* is_nonblocking */ ); + if (ctrl->fd < 0) + { + errno = -ctrl->fd; + ctrl->fd = -1; + } + } + + if (ctrl->fd < 0) + { + errno_val = errno; + perror ("ERROR in main()"); + fprintf (stderr, "CLIENT: ERROR: socket " + "failed (errno = %d)!\n", errno_val); + return ctrl->fd; + } + + memset (&scm->server_addr, 0, sizeof (scm->server_addr)); + if (ctrl->cfg.address_ip6) + { + struct sockaddr_in6 *server_addr = + (struct sockaddr_in6 *) &scm->server_addr; + scm->server_addr_size = sizeof (*server_addr); + server_addr->sin6_family = AF_INET6; + inet_pton (AF_INET6, argv[optind++], &(server_addr->sin6_addr)); + server_addr->sin6_port = htons (atoi (argv[optind])); + } + else + { + struct sockaddr_in *server_addr = + (struct sockaddr_in *) &scm->server_addr; + scm->server_addr_size = sizeof (*server_addr); + server_addr->sin_family = AF_INET; + inet_pton (AF_INET, argv[optind++], &(server_addr->sin_addr)); + server_addr->sin_port = htons (atoi (argv[optind])); + } + + if (ctrl->cfg.address_ip6) + { + struct sockaddr_in6 *server_addr = + (struct sockaddr_in6 *) &scm->server_addr; + scm->server_endpt.is_ip4 = 0; + scm->server_endpt.ip = (uint8_t *) & server_addr->sin6_addr; + scm->server_endpt.port = (uint16_t) server_addr->sin6_port; + } + else + { + struct sockaddr_in *server_addr = + (struct sockaddr_in *) &scm->server_addr; + scm->server_endpt.is_ip4 = 1; + scm->server_endpt.ip = (uint8_t *) & server_addr->sin_addr; + scm->server_endpt.port = (uint16_t) server_addr->sin_port; + } + + do + { + printf ("\nCLIENT: Connecting to server...\n"); + + rv = vppcom_session_connect (ctrl->fd, &scm->server_endpt); + if (rv) + { + errno = -rv; + rv = -1; + } + if (rv < 0) + { + errno_val = errno; + perror ("ERROR in main()"); + fprintf (stderr, "CLIENT: ERROR: connect failed (errno = %d)!\n", + errno_val); + return -1; + } + + sock_test_cfg_sync (ctrl); + printf ("CLIENT (fd %d): Control socket connected.\n", ctrl->fd); + } + while (rv < 0); + + sock_test_connect_test_sockets (ctrl->cfg.num_test_sockets); + + while (ctrl->cfg.test != SOCK_TEST_TYPE_EXIT) + { + if (scm->dump_cfg) + { + sock_test_cfg_dump (&ctrl->cfg, 1 /* is_client */ ); + scm->dump_cfg = 0; + } + + switch (ctrl->cfg.test) + { + case SOCK_TEST_TYPE_ECHO: + echo_test_client (); + break; + + case SOCK_TEST_TYPE_UNI: + case SOCK_TEST_TYPE_BI: + stream_test_client (ctrl->cfg.test); + break; + + case SOCK_TEST_TYPE_EXIT: + continue; + + case SOCK_TEST_TYPE_NONE: + default: + break; + } + switch (post_test) + { + case SOCK_TEST_TYPE_EXIT: + switch (ctrl->cfg.test) + { + case SOCK_TEST_TYPE_EXIT: + case SOCK_TEST_TYPE_UNI: + case SOCK_TEST_TYPE_BI: + case SOCK_TEST_TYPE_ECHO: + ctrl->cfg.test = SOCK_TEST_TYPE_EXIT; + continue; + + case SOCK_TEST_TYPE_NONE: + default: + break; + } + break; + + case SOCK_TEST_TYPE_NONE: + case SOCK_TEST_TYPE_ECHO: + case SOCK_TEST_TYPE_UNI: + case SOCK_TEST_TYPE_BI: + default: + break; + } + + memset (ctrl->txbuf, 0, ctrl->txbuf_size); + memset (ctrl->rxbuf, 0, ctrl->rxbuf_size); + + printf ("\nCLIENT: Type some characters and hit <return>\n" + "('" SOCK_TEST_TOKEN_HELP "' for help): "); + + if (fgets (ctrl->txbuf, ctrl->txbuf_size, stdin) != NULL) + { + if (strlen (ctrl->txbuf) == 1) + { + printf ("\nCLIENT: Nothing to send! Please try again...\n"); + continue; + } + ctrl->txbuf[strlen (ctrl->txbuf) - 1] = 0; // chomp the newline. + + /* Parse input for keywords */ + ctrl->cfg.test = parse_input (); + } + } + + exit_client (); + vppcom_session_close (ctrl->fd); + vppcom_app_destroy (); + return 0; +} /* * fd.io coding-style-patch-verification: ON diff --git a/src/vcl/vcl_test_server.c b/src/vcl/vcl_test_server.c index e91d2ecd7c8..5d37b46df93 100644 --- a/src/vcl/vcl_test_server.c +++ b/src/vcl/vcl_test_server.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Cisco and/or its affiliates. + * Copyright (c) 2017-2018 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -13,10 +13,635 @@ * limitations under the License. */ -#define VCL_TEST +#include <unistd.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <ctype.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <vcl/vcl_test.h> +#include <sys/epoll.h> -#include <vcl/vppcom.h> -#include <vcl/sock_test_server.c> +typedef struct +{ + uint8_t is_alloc; + int fd; + uint8_t *buf; + uint32_t buf_size; + sock_test_cfg_t cfg; + sock_test_stats_t stats; + vppcom_endpt_t endpt; + uint8_t ip[16]; +} sock_server_conn_t; + +typedef struct +{ + uint32_t port; + uint32_t address_ip6; + uint32_t transport_udp; +} sock_server_cfg_t; + +#define SOCK_SERVER_MAX_TEST_CONN 10 +#define SOCK_SERVER_MAX_EPOLL_EVENTS 10 +typedef struct +{ + int listen_fd; + sock_server_cfg_t cfg; + int epfd; + struct epoll_event listen_ev; + struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS]; + size_t num_conn; + size_t conn_pool_size; + sock_server_conn_t *conn_pool; + int nfds; + fd_set rd_fdset; + fd_set wr_fdset; + struct timeval timeout; +} sock_server_main_t; + +sock_server_main_t sock_server_main; + +static inline void +conn_pool_expand (size_t expand_size) +{ + sock_server_main_t *ssm = &sock_server_main; + sock_server_conn_t *conn_pool; + size_t new_size = ssm->conn_pool_size + expand_size; + int i; + + conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool)); + if (conn_pool) + { + for (i = ssm->conn_pool_size; i < new_size; i++) + { + sock_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); + conn->cfg.txbuf_size = conn->cfg.rxbuf_size; + } + + ssm->conn_pool = conn_pool; + ssm->conn_pool_size = new_size; + } + else + { + int errno_val = errno; + perror ("ERROR in conn_pool_expand()"); + fprintf (stderr, "SERVER: ERROR: Memory allocation " + "failed (errno = %d)!\n", errno_val); + } +} + +static inline sock_server_conn_t * +conn_pool_alloc (void) +{ + sock_server_main_t *ssm = &sock_server_main; + int i; + + for (i = 0; i < ssm->conn_pool_size; i++) + { + if (!ssm->conn_pool[i].is_alloc) + { + ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip; + ssm->conn_pool[i].is_alloc = 1; + return (&ssm->conn_pool[i]); + } + } + + return 0; +} + +static inline void +conn_pool_free (sock_server_conn_t * conn) +{ + conn->fd = 0; + conn->is_alloc = 0; +} + +static inline void +sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg) +{ + conn->cfg = *rx_cfg; + sock_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) + { + printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd); + sock_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); +} + +static void +stream_test_server_start_stop (sock_server_conn_t * conn, + sock_test_cfg_t * rx_cfg) +{ + sock_server_main_t *ssm = &sock_server_main; + int client_fd = conn->fd; + sock_test_t test = rx_cfg->test; + + if (rx_cfg->ctrl_handle == conn->fd) + { + int i; + clock_gettime (CLOCK_REALTIME, &conn->stats.stop); + + for (i = 0; i < ssm->conn_pool_size; i++) + { + sock_server_conn_t *tc = &ssm->conn_pool[i]; + + if (tc->cfg.ctrl_handle == conn->fd) + { + sock_test_stats_accumulate (&conn->stats, &tc->stats); + + if (conn->cfg.verbose) + { + static char buf[64]; + + sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd); + sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ , + test == SOCK_TEST_TYPE_BI + /* show tx */ , + conn->cfg.verbose); + } + } + } + + sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ , + (test == SOCK_TEST_TYPE_BI) /* show_tx */ , + conn->cfg.verbose); + sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); + if (conn->cfg.verbose) + { + printf (" sock server main\n" + SOCK_TEST_SEPARATOR_STRING + " buf: %p\n" + " buf size: %u (0x%08x)\n" + SOCK_TEST_SEPARATOR_STRING, + conn->buf, conn->buf_size, conn->buf_size); + } + + sync_config_and_reply (conn, rx_cfg); + printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n" + SOCK_TEST_BANNER_STRING "\n", conn->fd, + test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); + } + else + { + printf ("\n" SOCK_TEST_BANNER_STRING + "SERVER (fd %d): %s-directional Stream Test!\n" + " Sending client the test cfg to start streaming data...\n", + client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); + + rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd : + rx_cfg->ctrl_handle; + + sync_config_and_reply (conn, rx_cfg); + + /* read the 1st chunk, record start time */ + memset (&conn->stats, 0, sizeof (conn->stats)); + clock_gettime (CLOCK_REALTIME, &conn->stats.start); + } +} + + +static inline void +stream_test_server (sock_server_conn_t * conn, int rx_bytes) +{ + int client_fd = conn->fd; + sock_test_t test = conn->cfg.test; + + if (test == SOCK_TEST_TYPE_BI) + (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats, + conn->cfg.verbose); + + if (conn->stats.rx_bytes >= conn->cfg.total_bytes) + { + clock_gettime (CLOCK_REALTIME, &conn->stats.stop); + } +} + +static inline void +new_client (void) +{ + sock_server_main_t *ssm = &sock_server_main; + int client_fd; + sock_server_conn_t *conn; + + if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1)) + conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1); + + conn = conn_pool_alloc (); + if (!conn) + { + fprintf (stderr, "\nSERVER: ERROR: No free connections!\n"); + return; + } + + client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, 0); + if (client_fd < 0) + { + int errno_val; + errno_val = errno = -client_fd; + perror ("ERROR in new_client()"); + fprintf (stderr, "SERVER: ERROR: accept failed " + "(errno = %d)!\n", errno_val); + return; + } + + printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n", + client_fd, client_fd); + + conn->fd = client_fd; + + { + struct epoll_event ev; + int rv; + + ev.events = EPOLLIN; + ev.data.u64 = conn - ssm->conn_pool; + rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev); + if (rv < 0) + { + int errno_val; + errno_val = errno = -rv; + perror ("ERROR in new_client()"); + fprintf (stderr, "SERVER: ERROR: epoll_ctl failed (errno = %d)!\n", + errno_val); + } + else + ssm->nfds++; + } +} + +void +print_usage_and_exit (void) +{ + fprintf (stderr, + "sock_test_server [OPTIONS] <port>\n" + " OPTIONS\n" + " -h Print this message and exit.\n" + " -6 Use IPv6\n" + " -u Use UDP transport layer\n"); + exit (1); +} + +int +main (int argc, char **argv) +{ + sock_server_main_t *ssm = &sock_server_main; + int client_fd, rv, main_rv = 0; + int tx_bytes, rx_bytes, nbytes; + sock_server_conn_t *conn; + sock_test_cfg_t *rx_cfg; + uint32_t xtra = 0; + uint64_t xtra_bytes = 0; + struct sockaddr_storage servaddr; + int errno_val; + int c, v, i; + uint16_t port = SOCK_TEST_SERVER_PORT; + vppcom_endpt_t endpt; + + opterr = 0; + while ((c = getopt (argc, argv, "6D")) != -1) + switch (c) + { + case '6': + ssm->cfg.address_ip6 = 1; + break; + + case 'D': + ssm->cfg.transport_udp = 1; + break; + + case '?': + switch (optopt) + { + default: + if (isprint (optopt)) + fprintf (stderr, "SERVER: ERROR: Unknown " + "option `-%c'.\n", optopt); + else + fprintf (stderr, "SERVER: ERROR: Unknown " + "option character `\\x%x'.\n", optopt); + } + /* fall thru */ + case 'h': + default: + print_usage_and_exit (); + } + + if (argc < (optind + 1)) + { + fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n"); + print_usage_and_exit (); + } + + if (sscanf (argv[optind], "%d", &v) == 1) + port = (uint16_t) v; + else + { + fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]); + print_usage_and_exit (); + } + + conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1); + + rv = vppcom_app_create ("vcl_test_server"); + if (rv) + { + errno_val = errno = -rv; + perror ("ERROR in main()"); + fprintf (stderr, "SERVER: ERROR: vppcom_app_create() failed " + "(errno = %d)!\n", errno_val); + return -1; + } + else + { + ssm->listen_fd = vppcom_session_create (ssm->cfg.transport_udp ? + VPPCOM_PROTO_UDP : + VPPCOM_PROTO_TCP, + 0 /* is_nonblocking */ ); + } + if (ssm->listen_fd < 0) + { + errno_val = errno = -ssm->listen_fd; + perror ("ERROR in main()"); + fprintf (stderr, "SERVER: ERROR: vppcom_session_create() failed " + "(errno = %d)!\n", errno_val); + return -1; + } + + memset (&servaddr, 0, sizeof (servaddr)); + + if (ssm->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 (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 (port); + } + + if (ssm->cfg.address_ip6) + { + struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) &servaddr; + endpt.is_ip4 = 0; + endpt.ip = (uint8_t *) & server_addr->sin6_addr; + endpt.port = (uint16_t) server_addr->sin6_port; + } + else + { + struct sockaddr_in *server_addr = (struct sockaddr_in *) &servaddr; + endpt.is_ip4 = 1; + endpt.ip = (uint8_t *) & server_addr->sin_addr; + endpt.port = (uint16_t) server_addr->sin_port; + } + + rv = vppcom_session_bind (ssm->listen_fd, &endpt); + if (rv < 0) + { + errno_val = errno = -rv; + perror ("ERROR in main()"); + fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n", + errno_val); + return -1; + } + + rv = vppcom_session_listen (ssm->listen_fd, 10); + if (rv < 0) + { + errno_val = errno = -rv; + perror ("ERROR in main()"); + fprintf (stderr, "SERVER: ERROR: listen failed " + "(errno = %d)!\n", errno_val); + return -1; + } + + ssm->epfd = vppcom_epoll_create (); + if (ssm->epfd < 0) + { + errno_val = errno = -ssm->epfd; + perror ("ERROR in main()"); + fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n", + errno_val); + return -1; + } + + ssm->listen_ev.events = EPOLLIN; + ssm->listen_ev.data.u32 = ~0; + rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, + &ssm->listen_ev); + if (rv < 0) + { + errno_val = errno = -rv; + perror ("ERROR in main()"); + fprintf (stderr, "SERVER: ERROR: epoll_ctl failed " + "(errno = %d)!\n", errno_val); + return -1; + } + printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port); + + while (1) + { + int num_ev; + num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events, + SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0); + if (num_ev < 0) + { + errno = -num_ev; + perror ("epoll_wait()"); + fprintf (stderr, "\nSERVER: ERROR: epoll_wait() " + "failed -- aborting!\n"); + main_rv = -1; + goto done; + } + else if (num_ev == 0) + { + fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n"); + continue; + } + for (i = 0; i < num_ev; i++) + { + conn = &ssm->conn_pool[ssm->wait_events[i].data.u32]; + if (ssm->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP)) + { + vppcom_session_close (conn->fd); + continue; + } + if (ssm->wait_events[i].data.u32 == ~0) + { + new_client (); + continue; + } + client_fd = conn->fd; + + if (EPOLLIN & ssm->wait_events[i].events) + { + rx_bytes = vcl_test_read (client_fd, conn->buf, + conn->buf_size, &conn->stats); + if (rx_bytes > 0) + { + rx_cfg = (sock_test_cfg_t *) conn->buf; + if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC) + { + if (rx_cfg->verbose) + { + printf ("SERVER (fd %d): Received a cfg message!\n", + client_fd); + sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); + } + + if (rx_bytes != sizeof (*rx_cfg)) + { + printf ("SERVER (fd %d): Invalid cfg message " + "size (%d)!\n Should be %lu bytes.\n", + client_fd, rx_bytes, sizeof (*rx_cfg)); + conn->cfg.rxbuf_size = 0; + conn->cfg.num_writes = 0; + if (conn->cfg.verbose) + { + printf ("SERVER (fd %d): Replying to " + "cfg message!\n", client_fd); + sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); + } + vcl_test_write (client_fd, (uint8_t *) & conn->cfg, + sizeof (conn->cfg), NULL, + conn->cfg.verbose); + continue; + } + + switch (rx_cfg->test) + { + case SOCK_TEST_TYPE_NONE: + case SOCK_TEST_TYPE_ECHO: + sync_config_and_reply (conn, rx_cfg); + break; + + case SOCK_TEST_TYPE_BI: + case SOCK_TEST_TYPE_UNI: + stream_test_server_start_stop (conn, rx_cfg); + break; + + case SOCK_TEST_TYPE_EXIT: + printf ("SERVER: Have a great day, " + "connection %d!\n", client_fd); + vppcom_session_close (client_fd); + conn_pool_free (conn); + printf ("SERVER: Closed client fd %d\n", client_fd); + ssm->nfds--; + if (!ssm->nfds) + { + printf ("SERVER: All client connections " + "closed.\n\nSERVER: " + "May the force be with you!\n\n"); + goto done; + } + break; + + default: + fprintf (stderr, + "SERVER: ERROR: Unknown test type!\n"); + sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); + break; + } + continue; + } + + else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) || + (conn->cfg.test == SOCK_TEST_TYPE_BI)) + { + stream_test_server (conn, rx_bytes); + continue; + } + + else if (isascii (conn->buf[0])) + { + /* If it looks vaguely like a string, + * make sure it's terminated. + */ + ((char *) conn->buf)[rx_bytes < + conn->buf_size ? rx_bytes : + conn->buf_size - 1] = 0; + printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n", + conn->fd, rx_bytes, conn->buf); + } + } + else // rx_bytes < 0 + { + if (errno == ECONNRESET) + { + printf ("\nSERVER: Connection reset by remote peer.\n" + " Y'all have a great day now!\n\n"); + break; + } + else + continue; + } + + if (isascii (conn->buf[0])) + { + /* If it looks vaguely like a string, + * make sure it's terminated + */ + ((char *) conn->buf)[rx_bytes < + conn->buf_size ? rx_bytes : + conn->buf_size - 1] = 0; + if (xtra) + fprintf (stderr, "SERVER: ERROR: " + "FIFO not drained in previous test!\n" + " extra chunks %u (0x%x)\n" + " extra bytes %lu (0x%lx)\n", + xtra, xtra, xtra_bytes, xtra_bytes); + + xtra = 0; + xtra_bytes = 0; + + if (conn->cfg.verbose) + printf ("SERVER (fd %d): Echoing back\n", client_fd); + + nbytes = strlen ((const char *) conn->buf) + 1; + + tx_bytes = vcl_test_write (client_fd, conn->buf, + nbytes, &conn->stats, + conn->cfg.verbose); + if (tx_bytes >= 0) + printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n", + conn->fd, tx_bytes, conn->buf); + } + + else // Extraneous read data from non-echo tests??? + { + xtra++; + xtra_bytes += rx_bytes; + } + } + } + } + +done: + vppcom_session_close (ssm->listen_fd); + vppcom_app_destroy (); + + if (ssm->conn_pool) + free (ssm->conn_pool); + + return main_rv; +} /* * fd.io coding-style-patch-verification: ON |