Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef __sock_test_common_h__ |
| 17 | #define __sock_test_common_h__ |
| 18 | |
| 19 | #include <netdb.h> |
| 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #define SOCK_TEST_TOKEN_HELP "#H" |
| 25 | #define SOCK_TEST_TOKEN_EXIT "#X" |
| 26 | #define SOCK_TEST_TOKEN_VERBOSE "#V" |
| 27 | #define SOCK_TEST_TOKEN_TXBUF_SIZE "#T:" |
| 28 | #define SOCK_TEST_TOKEN_NUM_TEST_SCKTS "#I:" |
| 29 | #define SOCK_TEST_TOKEN_NUM_WRITES "#N:" |
| 30 | #define SOCK_TEST_TOKEN_RXBUF_SIZE "#R:" |
| 31 | #define SOCK_TEST_TOKEN_SHOW_CFG "#C" |
| 32 | #define SOCK_TEST_TOKEN_RUN_UNI "#U" |
| 33 | #define SOCK_TEST_TOKEN_RUN_BI "#B" |
| 34 | |
| 35 | #define SOCK_TEST_BANNER_STRING \ |
| 36 | "============================================\n" |
| 37 | #define SOCK_TEST_SEPARATOR_STRING \ |
| 38 | " -----------------------------\n" |
| 39 | |
| 40 | #define ONE_GIG (1024*1024*1024) |
| 41 | #define SOCK_TEST_SERVER_PORT 22000 |
| 42 | #define SOCK_TEST_LOCALHOST_IPADDR "127.0.0.1" |
| 43 | |
| 44 | #define SOCK_TEST_CFG_CTRL_MAGIC 0xfeedface |
| 45 | #define SOCK_TEST_CFG_NUM_WRITES_DEF 1000000 |
| 46 | #define SOCK_TEST_CFG_TXBUF_SIZE_DEF 8192 |
| 47 | #define SOCK_TEST_CFG_RXBUF_SIZE_DEF (64*SOCK_TEST_CFG_TXBUF_SIZE_DEF) |
| 48 | #define SOCK_TEST_CFG_BUF_SIZE_MIN 128 |
| 49 | #define SOCK_TEST_CFG_MAX_TEST_SCKTS 5 |
| 50 | |
| 51 | typedef enum |
| 52 | { |
| 53 | SOCK_TEST_TYPE_NONE, |
| 54 | SOCK_TEST_TYPE_ECHO, |
| 55 | SOCK_TEST_TYPE_UNI, |
| 56 | SOCK_TEST_TYPE_BI, |
| 57 | SOCK_TEST_TYPE_EXIT, |
| 58 | } sock_test_t; |
| 59 | |
| 60 | typedef struct __attribute__ ((packed)) |
| 61 | { |
| 62 | uint32_t magic; |
| 63 | uint32_t seq_num; |
| 64 | uint32_t test; |
| 65 | uint32_t ctrl_handle; |
| 66 | uint32_t num_test_sockets; |
| 67 | uint32_t verbose; |
| 68 | uint32_t address_ip6; |
| 69 | uint32_t transport_udp; |
| 70 | uint64_t rxbuf_size; |
| 71 | uint64_t txbuf_size; |
| 72 | uint64_t num_writes; |
| 73 | uint64_t total_bytes; |
| 74 | } sock_test_cfg_t; |
| 75 | |
| 76 | typedef struct |
| 77 | { |
| 78 | uint64_t rx_xacts; |
| 79 | uint64_t rx_bytes; |
| 80 | uint32_t rx_eagain; |
| 81 | uint32_t rx_incomp; |
| 82 | uint64_t tx_xacts; |
| 83 | uint64_t tx_bytes; |
| 84 | uint32_t tx_eagain; |
| 85 | uint32_t tx_incomp; |
| 86 | struct timespec start; |
| 87 | struct timespec stop; |
| 88 | } sock_test_stats_t; |
| 89 | |
| 90 | typedef struct |
| 91 | { |
| 92 | int fd; |
| 93 | uint32_t txbuf_size; |
| 94 | char *txbuf; |
| 95 | uint32_t rxbuf_size; |
| 96 | char *rxbuf; |
| 97 | sock_test_cfg_t cfg; |
| 98 | sock_test_stats_t stats; |
| 99 | } sock_test_socket_t; |
| 100 | |
| 101 | static inline void |
| 102 | sock_test_stats_accumulate (sock_test_stats_t * accum, |
| 103 | sock_test_stats_t * incr) |
| 104 | { |
| 105 | accum->rx_xacts += incr->rx_xacts; |
| 106 | accum->rx_bytes += incr->rx_bytes; |
| 107 | accum->rx_eagain += incr->rx_eagain; |
| 108 | accum->rx_incomp += incr->rx_incomp; |
| 109 | accum->tx_xacts += incr->tx_xacts; |
| 110 | accum->tx_bytes += incr->tx_bytes; |
| 111 | accum->tx_eagain += incr->tx_eagain; |
| 112 | accum->tx_incomp += incr->tx_incomp; |
| 113 | } |
| 114 | |
| 115 | static inline void |
| 116 | sock_test_cfg_init (sock_test_cfg_t *cfg) |
| 117 | { |
| 118 | cfg->magic = SOCK_TEST_CFG_CTRL_MAGIC; |
| 119 | cfg->test = SOCK_TEST_TYPE_NONE; |
| 120 | cfg->ctrl_handle = ~0; |
| 121 | cfg->num_test_sockets = 1; |
| 122 | cfg->verbose = 0; |
| 123 | cfg->rxbuf_size = SOCK_TEST_CFG_RXBUF_SIZE_DEF; |
| 124 | cfg->num_writes = SOCK_TEST_CFG_NUM_WRITES_DEF; |
| 125 | cfg->txbuf_size = SOCK_TEST_CFG_TXBUF_SIZE_DEF; |
| 126 | cfg->total_bytes = cfg->num_writes * cfg->txbuf_size; |
| 127 | } |
| 128 | |
| 129 | static inline int |
| 130 | sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg) |
| 131 | { |
| 132 | /* Note: txbuf & rxbuf on server are the same buffer, |
| 133 | * so txbuf_size is not included in this check. |
| 134 | */ |
| 135 | return ((cfg->magic == valid_cfg->magic) |
| 136 | && (cfg->test == valid_cfg->test) |
| 137 | && (cfg->verbose == valid_cfg->verbose) |
| 138 | && (cfg->rxbuf_size == valid_cfg->rxbuf_size) |
| 139 | && (cfg->num_writes == valid_cfg->num_writes) |
| 140 | && (cfg->total_bytes == valid_cfg->total_bytes)); |
| 141 | } |
| 142 | |
| 143 | static inline void |
| 144 | sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf, |
| 145 | uint32_t *bufsize) |
| 146 | { |
| 147 | uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size; |
| 148 | uint8_t *lb = realloc (*buf, (size_t) alloc_size); |
| 149 | |
| 150 | if (lb) |
| 151 | { |
| 152 | if (is_rxbuf) |
| 153 | cfg->rxbuf_size = *bufsize = alloc_size; |
| 154 | else |
| 155 | cfg->txbuf_size = *bufsize = alloc_size; |
| 156 | |
| 157 | *buf = lb; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | int errno_val = errno; |
| 162 | perror ("ERROR in sock_test_buf_alloc()"); |
| 163 | fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation " |
| 164 | "failed (errno = %d)!\n" |
| 165 | " Using buffer size %d instead of desired" |
| 166 | " size (%d)\n", errno_val, *bufsize, alloc_size); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static inline void |
| 171 | sock_test_socket_buf_alloc (sock_test_socket_t *socket) |
| 172 | { |
| 173 | socket->rxbuf_size = socket->cfg.rxbuf_size; |
| 174 | socket->txbuf_size = socket->cfg.txbuf_size; |
| 175 | sock_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ , |
| 176 | (uint8_t **) &socket->txbuf, &socket->txbuf_size); |
| 177 | sock_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ , |
| 178 | (uint8_t **) &socket->rxbuf, &socket->rxbuf_size); |
| 179 | } |
| 180 | |
| 181 | static inline char * |
| 182 | sock_test_type_str (sock_test_t t) |
| 183 | { |
| 184 | switch (t) |
| 185 | { |
| 186 | case SOCK_TEST_TYPE_NONE: |
| 187 | return "NONE"; |
| 188 | |
| 189 | case SOCK_TEST_TYPE_ECHO: |
| 190 | return "ECHO"; |
| 191 | |
| 192 | case SOCK_TEST_TYPE_UNI: |
| 193 | return "UNI"; |
| 194 | |
| 195 | case SOCK_TEST_TYPE_BI: |
| 196 | return "BI"; |
| 197 | |
| 198 | case SOCK_TEST_TYPE_EXIT: |
| 199 | return "EXIT"; |
| 200 | |
| 201 | default: |
| 202 | return "Unknown"; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static inline void |
| 207 | sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client) |
| 208 | { |
| 209 | char *spc = " "; |
| 210 | |
| 211 | printf (" test config (%p):\n" |
| 212 | SOCK_TEST_SEPARATOR_STRING |
| 213 | " magic: 0x%08x\n" |
| 214 | " seq_num: 0x%08x\n" |
| 215 | "%-5s test: %s (%d)\n" |
| 216 | " ctrl handle: %d (0x%x)\n" |
| 217 | "%-5s num test sockets: %u (0x%08x)\n" |
| 218 | "%-5s verbose: %s (%d)\n" |
| 219 | "%-5s rxbuf size: %lu (0x%08lx)\n" |
| 220 | "%-5s txbuf size: %lu (0x%08lx)\n" |
| 221 | "%-5s num writes: %lu (0x%08lx)\n" |
| 222 | " client tx bytes: %lu (0x%08lx)\n" |
| 223 | SOCK_TEST_SEPARATOR_STRING, |
| 224 | (void *) cfg, cfg->magic, cfg->seq_num, |
| 225 | is_client && (cfg->test == SOCK_TEST_TYPE_UNI) ? |
| 226 | "'"SOCK_TEST_TOKEN_RUN_UNI"'" : |
| 227 | is_client && (cfg->test == SOCK_TEST_TYPE_BI) ? |
| 228 | "'"SOCK_TEST_TOKEN_RUN_BI"'" : spc, |
| 229 | sock_test_type_str (cfg->test), cfg->test, |
| 230 | cfg->ctrl_handle, cfg->ctrl_handle, |
| 231 | is_client ? "'"SOCK_TEST_TOKEN_NUM_TEST_SCKTS"'" : spc, |
| 232 | cfg->num_test_sockets, cfg->num_test_sockets, |
| 233 | is_client ? "'"SOCK_TEST_TOKEN_VERBOSE"'" : spc, |
| 234 | cfg->verbose ? "on" : "off", cfg->verbose, |
| 235 | is_client ? "'"SOCK_TEST_TOKEN_RXBUF_SIZE"'" : spc, |
| 236 | cfg->rxbuf_size, cfg->rxbuf_size, |
| 237 | is_client ? "'"SOCK_TEST_TOKEN_TXBUF_SIZE"'" : spc, |
| 238 | cfg->txbuf_size, cfg->txbuf_size, |
| 239 | is_client ? "'"SOCK_TEST_TOKEN_NUM_WRITES"'" : spc, |
| 240 | cfg->num_writes, cfg->num_writes, |
| 241 | cfg->total_bytes, cfg->total_bytes); |
| 242 | } |
| 243 | |
| 244 | static inline void |
| 245 | sock_test_stats_dump (char * header, sock_test_stats_t * stats, |
| 246 | uint8_t show_rx, uint8_t show_tx, |
| 247 | uint8_t verbose) |
| 248 | { |
| 249 | struct timespec diff; |
| 250 | double duration, rate; |
| 251 | uint64_t total_bytes; |
| 252 | |
| 253 | if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0) |
| 254 | { |
| 255 | diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1; |
| 256 | diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1000000000; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec; |
| 261 | diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec; |
| 262 | } |
| 263 | duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec); |
| 264 | |
| 265 | total_bytes = stats->tx_bytes + stats->rx_bytes; |
| 266 | rate = (double) total_bytes * 8 / duration / ONE_GIG; |
| 267 | printf ("\n%s: Streamed %lu bytes\n" |
| 268 | " in %lf seconds (%lf Gbps %s-duplex)!\n", |
| 269 | header, total_bytes, duration, rate, |
| 270 | (show_rx && show_tx) ? "full" : "half"); |
| 271 | |
| 272 | if (show_tx) |
| 273 | { |
| 274 | printf (SOCK_TEST_SEPARATOR_STRING |
| 275 | " tx stats (0x%p):\n" |
| 276 | SOCK_TEST_SEPARATOR_STRING |
| 277 | " writes: %lu (0x%08lx)\n" |
| 278 | " tx bytes: %lu (0x%08lx)\n" |
| 279 | " tx eagain: %u (0x%08x)\n" |
| 280 | " tx incomplete: %u (0x%08x)\n", |
| 281 | (void *)stats, stats->tx_xacts, stats->tx_xacts, |
| 282 | stats->tx_bytes, stats->tx_bytes, |
| 283 | stats->tx_eagain, stats->tx_eagain, |
| 284 | stats->tx_incomp, stats->tx_incomp); |
| 285 | } |
| 286 | if (show_rx) |
| 287 | { |
| 288 | printf (SOCK_TEST_SEPARATOR_STRING |
| 289 | " rx stats (0x%p):\n" |
| 290 | SOCK_TEST_SEPARATOR_STRING |
| 291 | " reads: %lu (0x%08lx)\n" |
| 292 | " rx bytes: %lu (0x%08lx)\n" |
| 293 | " rx eagain: %u (0x%08x)\n" |
| 294 | " rx incomplete: %u (0x%08x)\n", |
| 295 | (void *)stats, stats->rx_xacts, stats->rx_xacts, |
| 296 | stats->rx_bytes, stats->rx_bytes, |
| 297 | stats->rx_eagain, stats->rx_eagain, |
| 298 | stats->rx_incomp, stats->rx_incomp); |
| 299 | } |
| 300 | if (verbose) |
| 301 | printf (" start.tv_sec: %ld\n" |
| 302 | " start.tv_nsec: %ld\n" |
| 303 | " stop.tv_sec: %ld\n" |
| 304 | " stop.tv_nsec: %ld\n", |
| 305 | stats->start.tv_sec, stats->start.tv_nsec, |
| 306 | stats->stop.tv_sec, stats->stop.tv_nsec); |
| 307 | |
| 308 | printf (SOCK_TEST_SEPARATOR_STRING); |
| 309 | } |
| 310 | |
| 311 | #endif /* __sock_test_common_h__ */ |