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;
88 struct timespec start;
100 sock_test_stats_t stats;
101 } sock_test_socket_t;
104 sock_test_stats_accumulate (sock_test_stats_t * accum,
105 sock_test_stats_t * incr)
107 accum->rx_xacts += incr->rx_xacts;
108 accum->rx_bytes += incr->rx_bytes;
109 accum->rx_eagain += incr->rx_eagain;
110 accum->rx_incomp += incr->rx_incomp;
111 accum->tx_xacts += incr->tx_xacts;
112 accum->tx_bytes += incr->tx_bytes;
113 accum->tx_eagain += incr->tx_eagain;
114 accum->tx_incomp += incr->tx_incomp;
118 sock_test_cfg_init (sock_test_cfg_t *cfg)
120 cfg->magic = SOCK_TEST_CFG_CTRL_MAGIC;
121 cfg->test = SOCK_TEST_TYPE_NONE;
122 cfg->ctrl_handle = ~0;
123 cfg->num_test_sockets = 1;
125 cfg->rxbuf_size = SOCK_TEST_CFG_RXBUF_SIZE_DEF;
126 cfg->num_writes = SOCK_TEST_CFG_NUM_WRITES_DEF;
127 cfg->txbuf_size = SOCK_TEST_CFG_TXBUF_SIZE_DEF;
128 cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
132 sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg)
134 /* Note: txbuf & rxbuf on server are the same buffer,
135 * so txbuf_size is not included in this check.
137 return ((cfg->magic == valid_cfg->magic)
138 && (cfg->test == valid_cfg->test)
139 && (cfg->verbose == valid_cfg->verbose)
140 && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
141 && (cfg->num_writes == valid_cfg->num_writes)
142 && (cfg->total_bytes == valid_cfg->total_bytes));
146 sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf,
149 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
150 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
155 cfg->rxbuf_size = *bufsize = alloc_size;
157 cfg->txbuf_size = *bufsize = alloc_size;
163 int errno_val = errno;
164 perror ("ERROR in sock_test_buf_alloc()");
165 fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation "
166 "failed (errno = %d)!\n"
167 " Using buffer size %d instead of desired"
168 " size (%d)\n", errno_val, *bufsize, alloc_size);
173 sock_test_socket_buf_alloc (sock_test_socket_t *socket)
175 socket->rxbuf_size = socket->cfg.rxbuf_size;
176 socket->txbuf_size = socket->cfg.txbuf_size;
177 sock_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ ,
178 (uint8_t **) &socket->txbuf, &socket->txbuf_size);
179 sock_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ ,
180 (uint8_t **) &socket->rxbuf, &socket->rxbuf_size);
184 sock_test_type_str (sock_test_t t)
188 case SOCK_TEST_TYPE_NONE:
191 case SOCK_TEST_TYPE_ECHO:
194 case SOCK_TEST_TYPE_UNI:
197 case SOCK_TEST_TYPE_BI:
200 case SOCK_TEST_TYPE_EXIT:
209 sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client)
213 printf (" test config (%p):\n"
214 SOCK_TEST_SEPARATOR_STRING
216 "%-5s test: %s (%d)\n"
217 " ctrl handle: %d (0x%x)\n"
218 "%-5s num test sockets: %u (0x%08x)\n"
219 "%-5s verbose: %s (%d)\n"
220 "%-5s rxbuf size: %lu (0x%08lx)\n"
221 "%-5s txbuf size: %lu (0x%08lx)\n"
222 "%-5s num writes: %lu (0x%08lx)\n"
223 " client tx bytes: %lu (0x%08lx)\n"
224 SOCK_TEST_SEPARATOR_STRING,
225 (void *) cfg, cfg->magic,
226 is_client && (cfg->test == SOCK_TEST_TYPE_UNI) ?
227 "'"SOCK_TEST_TOKEN_RUN_UNI"'" :
228 is_client && (cfg->test == SOCK_TEST_TYPE_BI) ?
229 "'"SOCK_TEST_TOKEN_RUN_BI"'" : spc,
230 sock_test_type_str (cfg->test), cfg->test,
231 cfg->ctrl_handle, cfg->ctrl_handle,
232 is_client ? "'"SOCK_TEST_TOKEN_NUM_TEST_SCKTS"'" : spc,
233 cfg->num_test_sockets, cfg->num_test_sockets,
234 is_client ? "'"SOCK_TEST_TOKEN_VERBOSE"'" : spc,
235 cfg->verbose ? "on" : "off", cfg->verbose,
236 is_client ? "'"SOCK_TEST_TOKEN_RXBUF_SIZE"'" : spc,
237 cfg->rxbuf_size, cfg->rxbuf_size,
238 is_client ? "'"SOCK_TEST_TOKEN_TXBUF_SIZE"'" : spc,
239 cfg->txbuf_size, cfg->txbuf_size,
240 is_client ? "'"SOCK_TEST_TOKEN_NUM_WRITES"'" : spc,
241 cfg->num_writes, cfg->num_writes,
242 cfg->total_bytes, cfg->total_bytes);
246 sock_test_stats_dump (char * header, sock_test_stats_t * stats,
247 uint8_t show_rx, uint8_t show_tx,
250 struct timespec diff;
251 double duration, rate;
252 uint64_t total_bytes;
254 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
256 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
257 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1000000000;
261 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
262 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
264 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
266 total_bytes = stats->tx_bytes + stats->rx_bytes;
267 rate = (double) total_bytes * 8 / duration / ONE_GIG;
268 printf ("\n%s: Streamed %lu bytes\n"
269 " in %lf seconds (%lf Gbps %s-duplex)!\n",
270 header, total_bytes, duration, rate,
271 (show_rx && show_tx) ? "full" : "half");
275 printf (SOCK_TEST_SEPARATOR_STRING
276 " tx stats (0x%p):\n"
277 SOCK_TEST_SEPARATOR_STRING
278 " writes: %lu (0x%08lx)\n"
279 " tx bytes: %lu (0x%08lx)\n"
280 " tx eagain: %u (0x%08x)\n"
281 " tx incomplete: %u (0x%08x)\n",
282 (void *)stats, stats->tx_xacts, stats->tx_xacts,
283 stats->tx_bytes, stats->tx_bytes,
284 stats->tx_eagain, stats->tx_eagain,
285 stats->tx_incomp, stats->tx_incomp);
289 printf (SOCK_TEST_SEPARATOR_STRING
290 " rx stats (0x%p):\n"
291 SOCK_TEST_SEPARATOR_STRING
292 " reads: %lu (0x%08lx)\n"
293 " rx bytes: %lu (0x%08lx)\n"
294 " rx eagain: %u (0x%08x)\n"
295 " rx incomplete: %u (0x%08x)\n",
296 (void *)stats, stats->rx_xacts, stats->rx_xacts,
297 stats->rx_bytes, stats->rx_bytes,
298 stats->rx_eagain, stats->rx_eagain,
299 stats->rx_incomp, stats->rx_incomp);
302 printf (" start.tv_sec: %ld\n"
303 " start.tv_nsec: %ld\n"
304 " stop.tv_sec: %ld\n"
305 " stop.tv_nsec: %ld\n",
306 stats->start.tv_sec, stats->start.tv_nsec,
307 stats->stop.tv_sec, stats->stop.tv_nsec);
309 printf (SOCK_TEST_SEPARATOR_STRING);
311 #if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
312 printf (" af_unix xacts: %lu (0x%08lx)\n",
313 sock_server_main.af_unix_xacts);
315 printf (SOCK_TEST_SEPARATOR_STRING);
320 sock_test_read (int fd, uint8_t *buf, uint32_t nbytes,
321 sock_test_stats_t *stats)
323 int rx_bytes, errno_val;
330 rx_bytes = vppcom_session_read (fd, buf, nbytes);
338 rx_bytes = read (fd, buf, nbytes);
342 if ((rx_bytes == 0) ||
343 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
345 else if (rx_bytes < nbytes)
349 while ((rx_bytes == 0) ||
350 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
355 perror ("ERROR in sock_test_read()");
356 fprintf (stderr, "SOCK_TEST: ERROR: socket read "
357 "failed (errno = %d)!\n", errno_val);
361 stats->rx_bytes += rx_bytes;
367 sock_test_write (int fd, uint8_t *buf, uint32_t nbytes,
368 sock_test_stats_t *stats, uint32_t verbose)
371 int nbytes_left = nbytes;
379 rv = vppcom_session_write (fd, buf, nbytes_left);
386 rv = write (fd, buf, nbytes_left);
390 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
401 if (tx_bytes != nbytes)
403 nbytes_left = nbytes_left - rv;
408 printf ("SOCK_TEST: WARNING: bytes written (%d) "
409 "!= bytes to write (%d)!\n", tx_bytes, nbytes);
413 } while (tx_bytes != nbytes);
418 perror ("ERROR in sock_test_write()");
419 fprintf (stderr, "SOCK_TEST: ERROR: socket write failed "
420 "(errno = %d)!\n", errno_val);
423 stats->tx_bytes += tx_bytes;
428 #endif /* __sock_test_h__ */