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