aboutsummaryrefslogtreecommitdiffstats
path: root/src/vcl/sock_test_common.h
diff options
context:
space:
mode:
authorDave Wallace <dwallacelf@gmail.com>2018-06-24 21:21:21 -0400
committerFlorin Coras <florin.coras@gmail.com>2018-06-25 20:11:05 +0000
commite4d5a652a392c9f74501da5778a7fe161b183476 (patch)
tree32cdee1112347b920db4410f43fbe3079eeb443d /src/vcl/sock_test_common.h
parent4c5422ee52960fa151611aa94a76e363e1be98bd (diff)
VCL: Refactor VCL test (part 1)
Change-Id: I6a326e24ed953b1cef63bd4010a3bedd6c4a7b1c Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
Diffstat (limited to 'src/vcl/sock_test_common.h')
-rw-r--r--src/vcl/sock_test_common.h311
1 files changed, 311 insertions, 0 deletions
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__ */