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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __sock_test_h__
17 #define __sock_test_h__
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"
35 #define SOCK_TEST_BANNER_STRING \
36 "============================================\n"
37 #define SOCK_TEST_SEPARATOR_STRING \
38 " -----------------------------\n"
40 #define ONE_GIG (1024*1024*1024)
41 #define SOCK_TEST_SERVER_PORT 22000
42 #define SOCK_TEST_LOCALHOST_IPADDR "127.0.0.1"
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
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
65 typedef struct __attribute__ ((packed))
70 uint32_t num_test_sockets;
73 uint32_t transport_udp;
90 struct timespec start;
102 sock_test_stats_t stats;
103 } sock_test_socket_t;
106 sock_test_stats_accumulate (sock_test_stats_t * accum,
107 sock_test_stats_t * incr)
109 accum->rx_xacts += incr->rx_xacts;
110 accum->rx_bytes += incr->rx_bytes;
111 accum->rx_eagain += incr->rx_eagain;
112 accum->rx_incomp += incr->rx_incomp;
113 accum->tx_xacts += incr->tx_xacts;
114 accum->tx_bytes += incr->tx_bytes;
115 accum->tx_eagain += incr->tx_eagain;
116 accum->tx_incomp += incr->tx_incomp;
120 sock_test_cfg_init (sock_test_cfg_t *cfg)
122 cfg->magic = SOCK_TEST_CFG_CTRL_MAGIC;
123 cfg->test = SOCK_TEST_TYPE_NONE;
124 cfg->ctrl_handle = ~0;
125 cfg->num_test_sockets = 1;
127 cfg->rxbuf_size = SOCK_TEST_CFG_RXBUF_SIZE_DEF;
128 cfg->num_writes = SOCK_TEST_CFG_NUM_WRITES_DEF;
129 cfg->txbuf_size = SOCK_TEST_CFG_TXBUF_SIZE_DEF;
130 cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
134 sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg)
136 /* Note: txbuf & rxbuf on server are the same buffer,
137 * so txbuf_size is not included in this check.
139 return ((cfg->magic == valid_cfg->magic)
140 && (cfg->test == valid_cfg->test)
141 && (cfg->verbose == valid_cfg->verbose)
142 && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
143 && (cfg->num_writes == valid_cfg->num_writes)
144 && (cfg->total_bytes == valid_cfg->total_bytes));
148 sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf,
151 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
152 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
157 cfg->rxbuf_size = *bufsize = alloc_size;
159 cfg->txbuf_size = *bufsize = alloc_size;
165 int errno_val = errno;
166 perror ("ERROR in sock_test_buf_alloc()");
167 fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation "
168 "failed (errno = %d)!\n"
169 " Using buffer size %d instead of desired"
170 " size (%d)\n", errno_val, *bufsize, alloc_size);
175 sock_test_socket_buf_alloc (sock_test_socket_t *socket)
177 socket->rxbuf_size = socket->cfg.rxbuf_size;
178 socket->txbuf_size = socket->cfg.txbuf_size;
179 sock_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ ,
180 (uint8_t **) &socket->txbuf, &socket->txbuf_size);
181 sock_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ ,
182 (uint8_t **) &socket->rxbuf, &socket->rxbuf_size);
186 sock_test_type_str (sock_test_t t)
190 case SOCK_TEST_TYPE_NONE:
193 case SOCK_TEST_TYPE_ECHO:
196 case SOCK_TEST_TYPE_UNI:
199 case SOCK_TEST_TYPE_BI:
202 case SOCK_TEST_TYPE_EXIT:
211 sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client)
215 printf (" test config (%p):\n"
216 SOCK_TEST_SEPARATOR_STRING
218 "%-5s test: %s (%d)\n"
219 " ctrl handle: %d (0x%x)\n"
220 "%-5s num test sockets: %u (0x%08x)\n"
221 "%-5s verbose: %s (%d)\n"
222 "%-5s rxbuf size: %lu (0x%08lx)\n"
223 "%-5s txbuf size: %lu (0x%08lx)\n"
224 "%-5s num writes: %lu (0x%08lx)\n"
225 " client tx bytes: %lu (0x%08lx)\n"
226 SOCK_TEST_SEPARATOR_STRING,
227 (void *) cfg, cfg->magic,
228 is_client && (cfg->test == SOCK_TEST_TYPE_UNI) ?
229 "'"SOCK_TEST_TOKEN_RUN_UNI"'" :
230 is_client && (cfg->test == SOCK_TEST_TYPE_BI) ?
231 "'"SOCK_TEST_TOKEN_RUN_BI"'" : spc,
232 sock_test_type_str (cfg->test), cfg->test,
233 cfg->ctrl_handle, cfg->ctrl_handle,
234 is_client ? "'"SOCK_TEST_TOKEN_NUM_TEST_SCKTS"'" : spc,
235 cfg->num_test_sockets, cfg->num_test_sockets,
236 is_client ? "'"SOCK_TEST_TOKEN_VERBOSE"'" : spc,
237 cfg->verbose ? "on" : "off", cfg->verbose,
238 is_client ? "'"SOCK_TEST_TOKEN_RXBUF_SIZE"'" : spc,
239 cfg->rxbuf_size, cfg->rxbuf_size,
240 is_client ? "'"SOCK_TEST_TOKEN_TXBUF_SIZE"'" : spc,
241 cfg->txbuf_size, cfg->txbuf_size,
242 is_client ? "'"SOCK_TEST_TOKEN_NUM_WRITES"'" : spc,
243 cfg->num_writes, cfg->num_writes,
244 cfg->total_bytes, cfg->total_bytes);
248 sock_test_stats_dump (char * header, sock_test_stats_t * stats,
249 uint8_t show_rx, uint8_t show_tx,
252 struct timespec diff;
253 double duration, rate;
254 uint64_t total_bytes;
256 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
258 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
259 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1000000000;
263 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
264 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
266 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
268 total_bytes = stats->tx_bytes + stats->rx_bytes;
269 rate = (double) total_bytes * 8 / duration / ONE_GIG;
270 printf ("\n%s: Streamed %lu bytes\n"
271 " in %lf seconds (%lf Gbps %s-duplex)!\n",
272 header, total_bytes, duration, rate,
273 (show_rx && show_tx) ? "full" : "half");
277 printf (SOCK_TEST_SEPARATOR_STRING
278 " tx stats (0x%p):\n"
279 SOCK_TEST_SEPARATOR_STRING
280 " writes: %lu (0x%08lx)\n"
281 " tx bytes: %lu (0x%08lx)\n"
282 " tx eagain: %u (0x%08x)\n"
283 " tx incomplete: %u (0x%08x)\n",
284 (void *)stats, stats->tx_xacts, stats->tx_xacts,
285 stats->tx_bytes, stats->tx_bytes,
286 stats->tx_eagain, stats->tx_eagain,
287 stats->tx_incomp, stats->tx_incomp);
291 printf (SOCK_TEST_SEPARATOR_STRING
292 " rx stats (0x%p):\n"
293 SOCK_TEST_SEPARATOR_STRING
294 " reads: %lu (0x%08lx)\n"
295 " rx bytes: %lu (0x%08lx)\n"
296 " rx eagain: %u (0x%08x)\n"
297 " rx incomplete: %u (0x%08x)\n",
298 (void *)stats, stats->rx_xacts, stats->rx_xacts,
299 stats->rx_bytes, stats->rx_bytes,
300 stats->rx_eagain, stats->rx_eagain,
301 stats->rx_incomp, stats->rx_incomp);
304 printf (" start.tv_sec: %ld\n"
305 " start.tv_nsec: %ld\n"
306 " stop.tv_sec: %ld\n"
307 " stop.tv_nsec: %ld\n",
308 stats->start.tv_sec, stats->start.tv_nsec,
309 stats->stop.tv_sec, stats->stop.tv_nsec);
311 printf (SOCK_TEST_SEPARATOR_STRING);
313 #if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
314 printf (" af_unix xacts: %lu (0x%08lx)\n",
315 sock_server_main.af_unix_xacts);
317 printf (SOCK_TEST_SEPARATOR_STRING);
322 sock_test_read (int fd, uint8_t *buf, uint32_t nbytes,
323 sock_test_stats_t *stats)
325 int rx_bytes, errno_val;
332 rx_bytes = vppcom_session_read (fd, buf, nbytes);
340 rx_bytes = read (fd, buf, nbytes);
344 if ((rx_bytes == 0) ||
345 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
347 else if (rx_bytes < nbytes)
351 while ((rx_bytes == 0) ||
352 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
357 perror ("ERROR in sock_test_read()");
358 fprintf (stderr, "SOCK_TEST: ERROR: socket read "
359 "failed (errno = %d)!\n", errno_val);
363 stats->rx_bytes += rx_bytes;
369 sock_test_write (int fd, uint8_t *buf, uint32_t nbytes,
370 sock_test_stats_t *stats, uint32_t verbose)
373 int nbytes_left = nbytes;
381 rv = vppcom_session_write (fd, buf, nbytes_left);
388 rv = write (fd, buf, nbytes_left);
392 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
403 if (tx_bytes != nbytes)
405 nbytes_left = nbytes_left - rv;
410 printf ("SOCK_TEST: WARNING: bytes written (%d) "
411 "!= bytes to write (%d)!\n", tx_bytes, nbytes);
415 } while (tx_bytes != nbytes);
420 perror ("ERROR in sock_test_write()");
421 fprintf (stderr, "SOCK_TEST: ERROR: socket write failed "
422 "(errno = %d)!\n", errno_val);
425 stats->tx_bytes += tx_bytes;
430 #endif /* __sock_test_h__ */