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 __vcl_test_h__
17 #define __vcl_test_h__
23 #include <vcl/vppcom.h>
25 #define vtfail(_fn, _rv) \
28 perror ("ERROR when calling " _fn); \
29 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
33 #define vterr(_fn, _rv) \
36 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
39 #define vtwrn(_fmt, _args...) \
40 fprintf (stderr, "\nERROR: " _fmt "\n", ##_args) \
42 #define vtinf(_fmt, _args...) \
43 fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
45 #define vt_atomic_add(_ptr, _val) \
46 __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
48 #define VCL_TEST_TOKEN_HELP "#H"
49 #define VCL_TEST_TOKEN_EXIT "#X"
50 #define VCL_TEST_TOKEN_VERBOSE "#V"
51 #define VCL_TEST_TOKEN_TXBUF_SIZE "#T:"
52 #define VCL_TEST_TOKEN_NUM_TEST_SESS "#I:"
53 #define VCL_TEST_TOKEN_NUM_WRITES "#N:"
54 #define VCL_TEST_TOKEN_RXBUF_SIZE "#R:"
55 #define VCL_TEST_TOKEN_SHOW_CFG "#C"
56 #define VCL_TEST_TOKEN_RUN_UNI "#U"
57 #define VCL_TEST_TOKEN_RUN_BI "#B"
59 #define VCL_TEST_SERVER_PORT 22000
60 #define VCL_TEST_LOCALHOST_IPADDR "127.0.0.1"
62 #define VCL_TEST_CFG_CTRL_MAGIC 0xfeedface
63 #define VCL_TEST_CFG_NUM_WRITES_DEF 1000000
64 #define VCL_TEST_CFG_TXBUF_SIZE_DEF 8192
65 #define VCL_TEST_CFG_RXBUF_SIZE_DEF (64*VCL_TEST_CFG_TXBUF_SIZE_DEF)
66 #define VCL_TEST_CFG_BUF_SIZE_MIN 128
67 #define VCL_TEST_CFG_MAX_TEST_SESS 32
68 #define VCL_TEST_CFG_MAX_EPOLL_EVENTS 16
70 #define VCL_TEST_DELAY_DISCONNECT 1
71 #define VCL_TEST_SEPARATOR_STRING \
72 " -----------------------------\n"
82 typedef struct __attribute__ ((packed))
88 uint32_t num_test_sessions;
91 uint32_t transport_udp;
108 struct timespec start;
109 struct timespec stop;
120 vcl_test_stats_t stats;
121 } vcl_test_session_t;
124 vcl_test_stats_accumulate (vcl_test_stats_t * accum, vcl_test_stats_t * incr)
126 accum->rx_xacts += incr->rx_xacts;
127 accum->rx_bytes += incr->rx_bytes;
128 accum->rx_eagain += incr->rx_eagain;
129 accum->rx_incomp += incr->rx_incomp;
130 accum->tx_xacts += incr->tx_xacts;
131 accum->tx_bytes += incr->tx_bytes;
132 accum->tx_eagain += incr->tx_eagain;
133 accum->tx_incomp += incr->tx_incomp;
137 vcl_test_cfg_init (vcl_test_cfg_t * cfg)
139 cfg->magic = VCL_TEST_CFG_CTRL_MAGIC;
140 cfg->test = VCL_TEST_TYPE_NONE;
141 cfg->ctrl_handle = ~0;
142 cfg->num_test_sessions = 1;
144 cfg->rxbuf_size = VCL_TEST_CFG_RXBUF_SIZE_DEF;
145 cfg->num_writes = VCL_TEST_CFG_NUM_WRITES_DEF;
146 cfg->txbuf_size = VCL_TEST_CFG_TXBUF_SIZE_DEF;
147 cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
151 vcl_test_cfg_verify (vcl_test_cfg_t * cfg, vcl_test_cfg_t * valid_cfg)
153 /* Note: txbuf & rxbuf on server are the same buffer,
154 * so txbuf_size is not included in this check.
156 return ((cfg->magic == valid_cfg->magic)
157 && (cfg->test == valid_cfg->test)
158 && (cfg->verbose == valid_cfg->verbose)
159 && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
160 && (cfg->num_writes == valid_cfg->num_writes)
161 && (cfg->total_bytes == valid_cfg->total_bytes));
165 vcl_test_buf_alloc (vcl_test_cfg_t * cfg, uint8_t is_rxbuf, uint8_t ** buf,
168 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
169 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
174 cfg->rxbuf_size = *bufsize = alloc_size;
176 cfg->txbuf_size = *bufsize = alloc_size;
182 vtwrn ("realloc failed. using buffer size %d instead of %u",
183 *bufsize, alloc_size);
188 vcl_test_session_buf_alloc (vcl_test_session_t * socket)
190 socket->rxbuf_size = socket->cfg.rxbuf_size;
191 socket->txbuf_size = socket->cfg.txbuf_size;
192 vcl_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ ,
193 (uint8_t **) & socket->txbuf, &socket->txbuf_size);
194 vcl_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ ,
195 (uint8_t **) & socket->rxbuf, &socket->rxbuf_size);
199 vcl_test_type_str (vcl_test_t t)
203 case VCL_TEST_TYPE_NONE:
206 case VCL_TEST_TYPE_ECHO:
209 case VCL_TEST_TYPE_UNI:
212 case VCL_TEST_TYPE_BI:
215 case VCL_TEST_TYPE_EXIT:
224 vcl_test_cfg_dump (vcl_test_cfg_t * cfg, uint8_t is_client)
228 printf (" test config (%p):\n"
229 VCL_TEST_SEPARATOR_STRING
232 "%-5s test: %s (%d)\n"
233 " ctrl handle: %d (0x%x)\n"
234 "%-5s num test sockets: %u (0x%08x)\n"
235 "%-5s verbose: %s (%d)\n"
236 "%-5s rxbuf size: %lu (0x%08lx)\n"
237 "%-5s txbuf size: %lu (0x%08lx)\n"
238 "%-5s num writes: %lu (0x%08lx)\n"
239 " client tx bytes: %lu (0x%08lx)\n"
240 VCL_TEST_SEPARATOR_STRING,
241 (void *) cfg, cfg->magic, cfg->seq_num,
242 is_client && (cfg->test == VCL_TEST_TYPE_UNI) ?
243 "'" VCL_TEST_TOKEN_RUN_UNI "'" :
244 is_client && (cfg->test == VCL_TEST_TYPE_BI) ?
245 "'" VCL_TEST_TOKEN_RUN_BI "'" : spc,
246 vcl_test_type_str (cfg->test), cfg->test,
247 cfg->ctrl_handle, cfg->ctrl_handle,
248 is_client ? "'" VCL_TEST_TOKEN_NUM_TEST_SESS "'" : spc,
249 cfg->num_test_sessions, cfg->num_test_sessions,
250 is_client ? "'" VCL_TEST_TOKEN_VERBOSE "'" : spc,
251 cfg->verbose ? "on" : "off", cfg->verbose,
252 is_client ? "'" VCL_TEST_TOKEN_RXBUF_SIZE "'" : spc,
253 cfg->rxbuf_size, cfg->rxbuf_size,
254 is_client ? "'" VCL_TEST_TOKEN_TXBUF_SIZE "'" : spc,
255 cfg->txbuf_size, cfg->txbuf_size,
256 is_client ? "'" VCL_TEST_TOKEN_NUM_WRITES "'" : spc,
257 cfg->num_writes, cfg->num_writes,
258 cfg->total_bytes, cfg->total_bytes);
262 vcl_test_stats_dump (char *header, vcl_test_stats_t * stats,
263 uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
265 struct timespec diff;
266 double duration, rate;
267 uint64_t total_bytes;
269 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
271 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
272 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1e9;
276 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
277 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
279 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
281 total_bytes = stats->tx_bytes + stats->rx_bytes;
282 rate = (double) total_bytes *8 / duration / 1e9;
283 printf ("\n%s: Streamed %lu bytes\n"
284 " in %lf seconds (%lf Gbps %s-duplex)!\n",
285 header, total_bytes, duration, rate,
286 (show_rx && show_tx) ? "full" : "half");
290 printf (VCL_TEST_SEPARATOR_STRING
291 " tx stats (0x%p):\n"
292 VCL_TEST_SEPARATOR_STRING
293 " writes: %lu (0x%08lx)\n"
294 " tx bytes: %lu (0x%08lx)\n"
295 " tx eagain: %u (0x%08x)\n"
296 " tx incomplete: %u (0x%08x)\n",
297 (void *) stats, stats->tx_xacts, stats->tx_xacts,
298 stats->tx_bytes, stats->tx_bytes,
299 stats->tx_eagain, stats->tx_eagain,
300 stats->tx_incomp, stats->tx_incomp);
304 printf (VCL_TEST_SEPARATOR_STRING
305 " rx stats (0x%p):\n"
306 VCL_TEST_SEPARATOR_STRING
307 " reads: %lu (0x%08lx)\n"
308 " rx bytes: %lu (0x%08lx)\n"
309 " rx eagain: %u (0x%08x)\n"
310 " rx incomplete: %u (0x%08x)\n",
311 (void *) stats, stats->rx_xacts, stats->rx_xacts,
312 stats->rx_bytes, stats->rx_bytes,
313 stats->rx_eagain, stats->rx_eagain,
314 stats->rx_incomp, stats->rx_incomp);
317 printf (" start.tv_sec: %ld\n"
318 " start.tv_nsec: %ld\n"
319 " stop.tv_sec: %ld\n"
320 " stop.tv_nsec: %ld\n",
321 stats->start.tv_sec, stats->start.tv_nsec,
322 stats->stop.tv_sec, stats->stop.tv_nsec);
324 printf (VCL_TEST_SEPARATOR_STRING);
328 vcl_comp_tspec (struct timespec *a, struct timespec *b)
330 if (a->tv_sec < b->tv_sec)
332 else if (a->tv_sec > b->tv_sec)
334 else if (a->tv_nsec < b->tv_nsec)
336 else if (a->tv_nsec > b->tv_nsec)
343 vcl_test_read (int fd, uint8_t * buf, uint32_t nbytes,
344 vcl_test_stats_t * stats)
346 int rx_bytes, errno_val;
352 rx_bytes = vppcom_session_read (fd, buf, nbytes);
361 if ((rx_bytes == 0) ||
363 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
365 else if (rx_bytes < nbytes)
369 while ((rx_bytes == 0) ||
370 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
374 vterr ("vppcom_session_read()", -errno);
377 stats->rx_bytes += rx_bytes;
383 vcl_test_read_ds (int fd, vppcom_data_segments_t ds, vcl_test_stats_t * stats)
385 int rx_bytes, errno_val;
391 rx_bytes = vppcom_session_read_segments (fd, ds);
400 if ((rx_bytes == 0) ||
402 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
406 while ((rx_bytes == 0) ||
407 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
411 vterr ("vppcom_session_read()", -errno);
414 stats->rx_bytes += rx_bytes;
420 vcl_test_write (int fd, uint8_t * buf, uint32_t nbytes,
421 vcl_test_stats_t * stats, uint32_t verbose)
423 int tx_bytes = 0, nbytes_left = nbytes, rv;
429 rv = vppcom_session_write (fd, buf, nbytes_left);
437 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
448 if (tx_bytes != nbytes)
450 nbytes_left = nbytes_left - rv;
456 while (tx_bytes != nbytes);
460 vterr ("vpcom_session_write", -errno);
463 stats->tx_bytes += tx_bytes;
468 #endif /* __vcl_test_h__ */
471 * fd.io coding-style-patch-verification: ON
474 * eval: (c-set-style "gnu")