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))
71 uint32_t num_test_sockets;
74 uint32_t transport_udp;
91 struct timespec start;
103 sock_test_stats_t stats;
104 } sock_test_socket_t;
107 sock_test_stats_accumulate (sock_test_stats_t * accum,
108 sock_test_stats_t * incr)
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;
121 sock_test_cfg_init (sock_test_cfg_t *cfg)
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;
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;
135 sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg)
137 /* Note: txbuf & rxbuf on server are the same buffer,
138 * so txbuf_size is not included in this check.
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));
149 sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf,
152 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
153 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
158 cfg->rxbuf_size = *bufsize = alloc_size;
160 cfg->txbuf_size = *bufsize = alloc_size;
166 int errno_val = errno;
167 perror ("ERROR in sock_test_buf_alloc()");
168 fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation "
169 "failed (errno = %d)!\n"
170 " Using buffer size %d instead of desired"
171 " size (%d)\n", errno_val, *bufsize, alloc_size);
176 sock_test_socket_buf_alloc (sock_test_socket_t *socket)
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);
187 sock_test_type_str (sock_test_t t)
191 case SOCK_TEST_TYPE_NONE:
194 case SOCK_TEST_TYPE_ECHO:
197 case SOCK_TEST_TYPE_UNI:
200 case SOCK_TEST_TYPE_BI:
203 case SOCK_TEST_TYPE_EXIT:
212 sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client)
216 printf (" test config (%p):\n"
217 SOCK_TEST_SEPARATOR_STRING
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,
229 (void *) cfg, cfg->magic, cfg->seq_num,
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);
250 sock_test_stats_dump (char * header, sock_test_stats_t * stats,
251 uint8_t show_rx, uint8_t show_tx,
254 struct timespec diff;
255 double duration, rate;
256 uint64_t total_bytes;
258 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
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;
265 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
266 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
268 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
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");
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);
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);
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);
313 printf (SOCK_TEST_SEPARATOR_STRING);
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);
319 printf (SOCK_TEST_SEPARATOR_STRING);
324 sock_test_read (int fd, uint8_t *buf, uint32_t nbytes,
325 sock_test_stats_t *stats)
327 int rx_bytes, errno_val;
334 rx_bytes = vppcom_session_read (fd, buf, nbytes);
342 rx_bytes = read (fd, buf, nbytes);
346 if ((rx_bytes == 0) ||
347 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
349 else if (rx_bytes < nbytes)
353 while ((rx_bytes == 0) ||
354 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
359 perror ("ERROR in sock_test_read()");
360 fprintf (stderr, "SOCK_TEST: ERROR: socket read "
361 "failed (errno = %d)!\n", errno_val);
365 stats->rx_bytes += rx_bytes;
371 sock_test_write (int fd, uint8_t *buf, uint32_t nbytes,
372 sock_test_stats_t *stats, uint32_t verbose)
375 int nbytes_left = nbytes;
383 rv = vppcom_session_write (fd, buf, nbytes_left);
390 rv = write (fd, buf, nbytes_left);
394 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
405 if (tx_bytes != nbytes)
407 nbytes_left = nbytes_left - rv;
412 printf ("SOCK_TEST: WARNING: bytes written (%d) "
413 "!= bytes to write (%d)!\n", tx_bytes, nbytes);
417 } while (tx_bytes != nbytes);
422 perror ("ERROR in sock_test_write()");
423 fprintf (stderr, "SOCK_TEST: ERROR: socket write failed "
424 "(errno = %d)!\n", errno_val);
427 stats->tx_bytes += tx_bytes;
432 #endif /* __sock_test_h__ */