Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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_h__ |
| 17 | #define __sock_test_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 test; |
| 64 | uint32_t ctrl_handle; |
| 65 | uint32_t num_test_sockets; |
| 66 | uint32_t verbose; |
| 67 | uint64_t rxbuf_size; |
| 68 | uint64_t txbuf_size; |
| 69 | uint64_t num_writes; |
| 70 | uint64_t total_bytes; |
| 71 | } sock_test_cfg_t; |
| 72 | |
| 73 | typedef struct |
| 74 | { |
| 75 | uint64_t rx_xacts; |
| 76 | uint64_t rx_bytes; |
| 77 | uint32_t rx_eagain; |
| 78 | uint32_t rx_incomp; |
| 79 | uint64_t tx_xacts; |
| 80 | uint64_t tx_bytes; |
| 81 | uint32_t tx_eagain; |
| 82 | uint32_t tx_incomp; |
| 83 | struct timespec start; |
| 84 | struct timespec stop; |
| 85 | } sock_test_stats_t; |
| 86 | |
| 87 | typedef struct |
| 88 | { |
| 89 | int fd; |
| 90 | uint32_t txbuf_size; |
| 91 | char *txbuf; |
| 92 | uint32_t rxbuf_size; |
| 93 | char *rxbuf; |
| 94 | sock_test_cfg_t cfg; |
| 95 | sock_test_stats_t stats; |
| 96 | } sock_test_socket_t; |
| 97 | |
| 98 | static inline void |
| 99 | sock_test_stats_accumulate (sock_test_stats_t * accum, |
| 100 | sock_test_stats_t * incr) |
| 101 | { |
| 102 | accum->rx_xacts += incr->rx_xacts; |
| 103 | accum->rx_bytes += incr->rx_bytes; |
| 104 | accum->rx_eagain += incr->rx_eagain; |
| 105 | accum->rx_incomp += incr->rx_incomp; |
| 106 | accum->tx_xacts += incr->tx_xacts; |
| 107 | accum->tx_bytes += incr->tx_bytes; |
| 108 | accum->tx_eagain += incr->tx_eagain; |
| 109 | accum->tx_incomp += incr->tx_incomp; |
| 110 | } |
| 111 | |
| 112 | static inline void |
| 113 | sock_test_cfg_init (sock_test_cfg_t *cfg) |
| 114 | { |
| 115 | cfg->magic = SOCK_TEST_CFG_CTRL_MAGIC; |
| 116 | cfg->test = SOCK_TEST_TYPE_NONE; |
| 117 | cfg->ctrl_handle = ~0; |
| 118 | cfg->num_test_sockets = 1; |
| 119 | cfg->verbose = 0; |
| 120 | cfg->rxbuf_size = SOCK_TEST_CFG_RXBUF_SIZE_DEF; |
| 121 | cfg->num_writes = SOCK_TEST_CFG_NUM_WRITES_DEF; |
| 122 | cfg->txbuf_size = SOCK_TEST_CFG_TXBUF_SIZE_DEF; |
| 123 | cfg->total_bytes = cfg->num_writes * cfg->txbuf_size; |
| 124 | } |
| 125 | |
| 126 | static inline int |
| 127 | sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg) |
| 128 | { |
| 129 | /* Note: txbuf & rxbuf on server are the same buffer, |
| 130 | * so txbuf_size is not included in this check. |
| 131 | */ |
| 132 | return ((cfg->magic == valid_cfg->magic) |
| 133 | && (cfg->test == valid_cfg->test) |
| 134 | && (cfg->verbose == valid_cfg->verbose) |
| 135 | && (cfg->rxbuf_size == valid_cfg->rxbuf_size) |
| 136 | && (cfg->num_writes == valid_cfg->num_writes) |
| 137 | && (cfg->total_bytes == valid_cfg->total_bytes)); |
| 138 | } |
| 139 | |
| 140 | static inline void |
| 141 | sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf, |
| 142 | uint32_t *bufsize) |
| 143 | { |
| 144 | uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size; |
| 145 | uint8_t *lb = realloc (*buf, (size_t) alloc_size); |
| 146 | |
| 147 | if (lb) |
| 148 | { |
| 149 | if (is_rxbuf) |
| 150 | cfg->rxbuf_size = *bufsize = alloc_size; |
| 151 | else |
| 152 | cfg->txbuf_size = *bufsize = alloc_size; |
| 153 | |
| 154 | *buf = lb; |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | int errno_val = errno; |
| 159 | perror ("ERROR in sock_test_buf_alloc()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 160 | fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation " |
| 161 | "failed (errno = %d)!\n" |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 162 | " Using buffer size %d instead of desired" |
| 163 | " size (%d)\n", errno_val, *bufsize, alloc_size); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static inline void |
| 168 | sock_test_socket_buf_alloc (sock_test_socket_t *socket) |
| 169 | { |
| 170 | socket->rxbuf_size = socket->cfg.rxbuf_size; |
| 171 | socket->txbuf_size = socket->cfg.txbuf_size; |
| 172 | sock_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ , |
| 173 | (uint8_t **) &socket->txbuf, &socket->txbuf_size); |
| 174 | sock_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ , |
| 175 | (uint8_t **) &socket->rxbuf, &socket->rxbuf_size); |
| 176 | } |
| 177 | |
| 178 | static inline char * |
| 179 | sock_test_type_str (sock_test_t t) |
| 180 | { |
| 181 | switch (t) |
| 182 | { |
| 183 | case SOCK_TEST_TYPE_NONE: |
| 184 | return "NONE"; |
| 185 | |
| 186 | case SOCK_TEST_TYPE_ECHO: |
| 187 | return "ECHO"; |
| 188 | |
| 189 | case SOCK_TEST_TYPE_UNI: |
| 190 | return "UNI"; |
| 191 | |
| 192 | case SOCK_TEST_TYPE_BI: |
| 193 | return "BI"; |
| 194 | |
| 195 | case SOCK_TEST_TYPE_EXIT: |
| 196 | return "EXIT"; |
| 197 | |
| 198 | default: |
| 199 | return "Unknown"; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static inline void |
| 204 | sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client) |
| 205 | { |
| 206 | char *spc = " "; |
| 207 | |
| 208 | printf (" test config (%p):\n" |
| 209 | SOCK_TEST_SEPARATOR_STRING |
| 210 | " magic: 0x%08x\n" |
| 211 | "%-5s test: %s (%d)\n" |
| 212 | " ctrl handle: %d (0x%x)\n" |
| 213 | "%-5s num test sockets: %u (0x%08x)\n" |
| 214 | "%-5s verbose: %s (%d)\n" |
| 215 | "%-5s rxbuf size: %lu (0x%08lx)\n" |
| 216 | "%-5s txbuf size: %lu (0x%08lx)\n" |
| 217 | "%-5s num writes: %lu (0x%08lx)\n" |
| 218 | " client tx bytes: %lu (0x%08lx)\n" |
| 219 | SOCK_TEST_SEPARATOR_STRING, |
| 220 | (void *) cfg, cfg->magic, |
| 221 | is_client && (cfg->test == SOCK_TEST_TYPE_UNI) ? |
| 222 | "'"SOCK_TEST_TOKEN_RUN_UNI"'" : |
| 223 | is_client && (cfg->test == SOCK_TEST_TYPE_BI) ? |
| 224 | "'"SOCK_TEST_TOKEN_RUN_BI"'" : spc, |
| 225 | sock_test_type_str (cfg->test), cfg->test, |
| 226 | cfg->ctrl_handle, cfg->ctrl_handle, |
| 227 | is_client ? "'"SOCK_TEST_TOKEN_NUM_TEST_SCKTS"'" : spc, |
| 228 | cfg->num_test_sockets, cfg->num_test_sockets, |
| 229 | is_client ? "'"SOCK_TEST_TOKEN_VERBOSE"'" : spc, |
| 230 | cfg->verbose ? "on" : "off", cfg->verbose, |
| 231 | is_client ? "'"SOCK_TEST_TOKEN_RXBUF_SIZE"'" : spc, |
| 232 | cfg->rxbuf_size, cfg->rxbuf_size, |
| 233 | is_client ? "'"SOCK_TEST_TOKEN_TXBUF_SIZE"'" : spc, |
| 234 | cfg->txbuf_size, cfg->txbuf_size, |
| 235 | is_client ? "'"SOCK_TEST_TOKEN_NUM_WRITES"'" : spc, |
| 236 | cfg->num_writes, cfg->num_writes, |
| 237 | cfg->total_bytes, cfg->total_bytes); |
| 238 | } |
| 239 | |
| 240 | static inline void |
| 241 | sock_test_stats_dump (char * header, sock_test_stats_t * stats, |
| 242 | uint8_t show_rx, uint8_t show_tx, |
| 243 | uint8_t verbose) |
| 244 | { |
| 245 | struct timespec diff; |
| 246 | double duration, rate; |
| 247 | uint64_t total_bytes; |
| 248 | |
| 249 | if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0) |
| 250 | { |
| 251 | diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1; |
| 252 | diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1000000000; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec; |
| 257 | diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec; |
| 258 | } |
| 259 | duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec); |
| 260 | |
| 261 | total_bytes = stats->tx_bytes + stats->rx_bytes; |
| 262 | rate = (double) total_bytes * 8 / duration / ONE_GIG; |
| 263 | printf ("\n%s: Streamed %lu bytes\n" |
| 264 | " in %lf seconds (%lf Gbps %s-duplex)!\n", |
| 265 | header, total_bytes, duration, rate, |
| 266 | (show_rx && show_tx) ? "full" : "half"); |
| 267 | |
| 268 | if (show_tx) |
| 269 | { |
| 270 | printf (SOCK_TEST_SEPARATOR_STRING |
| 271 | " tx stats (0x%p):\n" |
| 272 | SOCK_TEST_SEPARATOR_STRING |
| 273 | " writes: %lu (0x%08lx)\n" |
| 274 | " tx bytes: %lu (0x%08lx)\n" |
| 275 | " tx eagain: %u (0x%08x)\n" |
| 276 | " tx incomplete: %u (0x%08x)\n", |
| 277 | (void *)stats, stats->tx_xacts, stats->tx_xacts, |
| 278 | stats->tx_bytes, stats->tx_bytes, |
| 279 | stats->tx_eagain, stats->tx_eagain, |
| 280 | stats->tx_incomp, stats->tx_incomp); |
| 281 | } |
| 282 | if (show_rx) |
| 283 | { |
| 284 | printf (SOCK_TEST_SEPARATOR_STRING |
| 285 | " rx stats (0x%p):\n" |
| 286 | SOCK_TEST_SEPARATOR_STRING |
| 287 | " reads: %lu (0x%08lx)\n" |
| 288 | " rx bytes: %lu (0x%08lx)\n" |
| 289 | " rx eagain: %u (0x%08x)\n" |
| 290 | " rx incomplete: %u (0x%08x)\n", |
| 291 | (void *)stats, stats->rx_xacts, stats->rx_xacts, |
| 292 | stats->rx_bytes, stats->rx_bytes, |
| 293 | stats->rx_eagain, stats->rx_eagain, |
| 294 | stats->rx_incomp, stats->rx_incomp); |
| 295 | } |
| 296 | if (verbose) |
| 297 | printf (" start.tv_sec: %ld\n" |
| 298 | " start.tv_nsec: %ld\n" |
| 299 | " stop.tv_sec: %ld\n" |
| 300 | " stop.tv_nsec: %ld\n", |
| 301 | stats->start.tv_sec, stats->start.tv_nsec, |
| 302 | stats->stop.tv_sec, stats->stop.tv_nsec); |
| 303 | |
| 304 | printf (SOCK_TEST_SEPARATOR_STRING); |
| 305 | } |
| 306 | |
| 307 | static inline int |
| 308 | sock_test_read (int fd, uint8_t *buf, uint32_t nbytes, |
| 309 | sock_test_stats_t *stats) |
| 310 | { |
| 311 | int rx_bytes, errno_val; |
| 312 | |
| 313 | do |
| 314 | { |
| 315 | if (stats) |
| 316 | stats->rx_xacts++; |
| 317 | #ifdef VCL_TEST |
| 318 | rx_bytes = vppcom_session_read (fd, buf, nbytes); |
| 319 | |
| 320 | if (rx_bytes < 0) |
| 321 | { |
| 322 | errno = -rx_bytes; |
| 323 | rx_bytes = -1; |
| 324 | } |
| 325 | #else |
| 326 | rx_bytes = read (fd, buf, nbytes); |
| 327 | #endif |
| 328 | if (stats) |
| 329 | { |
| 330 | if ((rx_bytes == 0) || |
| 331 | ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))) |
| 332 | stats->rx_eagain++; |
| 333 | else if (rx_bytes < nbytes) |
| 334 | stats->rx_incomp++; |
| 335 | } |
| 336 | } |
| 337 | while ((rx_bytes == 0) || |
| 338 | ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))); |
| 339 | |
| 340 | if (rx_bytes < 0) |
| 341 | { |
| 342 | errno_val = errno; |
| 343 | perror ("ERROR in sock_test_read()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 344 | fprintf (stderr, "SOCK_TEST: ERROR: socket read " |
| 345 | "failed (errno = %d)!\n", errno_val); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 346 | errno = errno_val; |
| 347 | } |
| 348 | else if (stats) |
| 349 | stats->rx_bytes += rx_bytes; |
| 350 | |
| 351 | return (rx_bytes); |
| 352 | } |
| 353 | |
| 354 | static inline int |
| 355 | sock_test_write (int fd, uint8_t *buf, uint32_t nbytes, |
| 356 | sock_test_stats_t *stats, uint32_t verbose) |
| 357 | { |
| 358 | int tx_bytes = 0; |
| 359 | int nbytes_left = nbytes; |
| 360 | int rv, errno_val; |
| 361 | |
| 362 | do |
| 363 | { |
| 364 | if (stats) |
| 365 | stats->tx_xacts++; |
| 366 | #ifdef VCL_TEST |
| 367 | rv = vppcom_session_write (fd, buf, nbytes_left); |
| 368 | if (rv < 0) |
| 369 | { |
| 370 | errno = -rv; |
| 371 | rv = -1; |
| 372 | } |
| 373 | #else |
| 374 | rv = write (fd, buf, nbytes_left); |
| 375 | #endif |
| 376 | if (rv < 0) |
| 377 | { |
| 378 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 379 | { |
| 380 | if (stats) |
| 381 | stats->tx_eagain++; |
| 382 | continue; |
| 383 | } |
| 384 | else |
| 385 | break; |
| 386 | } |
| 387 | tx_bytes += rv; |
| 388 | |
| 389 | if (tx_bytes != nbytes) |
| 390 | { |
| 391 | nbytes_left = nbytes_left - rv; |
| 392 | if (stats) |
| 393 | stats->tx_incomp++; |
| 394 | if (verbose) |
| 395 | { |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 396 | printf ("SOCK_TEST: WARNING: bytes written (%d) " |
| 397 | "!= bytes to write (%d)!\n", tx_bytes, nbytes); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | } while (tx_bytes != nbytes); |
| 402 | |
| 403 | if (tx_bytes < 0) |
| 404 | { |
| 405 | errno_val = errno; |
| 406 | perror ("ERROR in sock_test_write()"); |
Dave Wallace | 048b1d6 | 2018-01-03 22:24:41 -0500 | [diff] [blame] | 407 | fprintf (stderr, "SOCK_TEST: ERROR: socket write failed " |
| 408 | "(errno = %d)!\n", errno_val); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 409 | } |
| 410 | else if (stats) |
| 411 | stats->tx_bytes += tx_bytes; |
| 412 | |
| 413 | return (tx_bytes); |
| 414 | } |
| 415 | |
| 416 | #endif /* __sock_test_h__ */ |