2 * Copyright (c) 2017-2021 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__
24 #include <vcl/vppcom.h>
26 #define vtfail(_fn, _rv) \
29 perror ("ERROR when calling " _fn); \
30 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
34 #define vterr(_fn, _rv) \
37 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
40 #define vtwrn(_fmt, _args...) \
41 fprintf (stderr, "\nERROR: " _fmt "\n", ##_args) \
43 #define vtinf(_fmt, _args...) \
44 fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
46 #define vt_atomic_add(_ptr, _val) \
47 __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
49 #define VCL_TEST_TOKEN_HELP "#H"
50 #define VCL_TEST_TOKEN_EXIT "#X"
51 #define VCL_TEST_TOKEN_VERBOSE "#V"
52 #define VCL_TEST_TOKEN_TXBUF_SIZE "#T:"
53 #define VCL_TEST_TOKEN_NUM_TEST_SESS "#I:"
54 #define VCL_TEST_TOKEN_NUM_WRITES "#N:"
55 #define VCL_TEST_TOKEN_RXBUF_SIZE "#R:"
56 #define VCL_TEST_TOKEN_SHOW_CFG "#C"
57 #define VCL_TEST_TOKEN_RUN_UNI "#U"
58 #define VCL_TEST_TOKEN_RUN_BI "#B"
60 #define VCL_TEST_SERVER_PORT 22000
61 #define VCL_TEST_LOCALHOST_IPADDR "127.0.0.1"
63 #define VCL_TEST_CFG_CTRL_MAGIC 0xfeedface
64 #define VCL_TEST_CFG_NUM_WRITES_DEF 1000000
65 #define VCL_TEST_CFG_TXBUF_SIZE_DEF 8192
66 #define VCL_TEST_CFG_RXBUF_SIZE_DEF (64*VCL_TEST_CFG_TXBUF_SIZE_DEF)
67 #define VCL_TEST_CFG_BUF_SIZE_MIN 128
68 #define VCL_TEST_CFG_MAX_TEST_SESS 32
69 #define VCL_TEST_CFG_MAX_EPOLL_EVENTS 16
71 #define VCL_TEST_CTRL_LISTENER (~0 - 1)
72 #define VCL_TEST_DATA_LISTENER (~0)
73 #define VCL_TEST_DELAY_DISCONNECT 1
74 #define VCL_TEST_SEPARATOR_STRING \
75 " -----------------------------\n"
92 typedef struct __attribute__ ((packed))
99 uint32_t num_test_sessions;
100 uint32_t num_test_sessions_perq;
101 uint32_t num_test_qsessions;
103 uint32_t address_ip6;
104 uint32_t transport_udp;
108 uint64_t total_bytes;
121 struct timespec start;
122 struct timespec stop;
125 typedef struct vcl_test_session
130 int (*read) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
131 int (*write) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
137 vcl_test_stats_t stats;
139 vppcom_endpt_t endpt;
141 vppcom_data_segment_t ds[2];
142 } vcl_test_session_t;
144 static __thread int __wrk_index = 0;
147 vcl_test_worker_index (void)
154 int (*init) (vcl_test_cfg_t *cfg);
155 int (*open) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
156 int (*listen) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
157 int (*accept) (int listen_fd, vcl_test_session_t *ts);
158 int (*close) (vcl_test_session_t *ts);
159 } vcl_test_proto_vft_t;
163 vcl_test_session_t *qsessions;
164 uint32_t n_qsessions;
170 const vcl_test_proto_vft_t *protos[VPPCOM_PROTO_DTLS + 1];
171 uint32_t ckpair_index;
176 extern vcl_test_main_t vcl_test_main;
178 #define VCL_TEST_REGISTER_PROTO(proto, vft) \
179 static void __attribute__ ((constructor)) vcl_test_init_##proto (void) \
181 vcl_test_main.protos[proto] = &vft; \
185 vcl_test_stats_accumulate (vcl_test_stats_t * accum, vcl_test_stats_t * incr)
187 accum->rx_xacts += incr->rx_xacts;
188 accum->rx_bytes += incr->rx_bytes;
189 accum->rx_eagain += incr->rx_eagain;
190 accum->rx_incomp += incr->rx_incomp;
191 accum->tx_xacts += incr->tx_xacts;
192 accum->tx_bytes += incr->tx_bytes;
193 accum->tx_eagain += incr->tx_eagain;
194 accum->tx_incomp += incr->tx_incomp;
198 vcl_test_cfg_init (vcl_test_cfg_t * cfg)
200 cfg->magic = VCL_TEST_CFG_CTRL_MAGIC;
201 cfg->test = VCL_TEST_TYPE_NONE;
202 cfg->ctrl_handle = ~0;
203 cfg->num_test_sessions = 1;
204 cfg->num_test_sessions_perq = 1;
206 cfg->rxbuf_size = VCL_TEST_CFG_RXBUF_SIZE_DEF;
207 cfg->num_writes = VCL_TEST_CFG_NUM_WRITES_DEF;
208 cfg->txbuf_size = VCL_TEST_CFG_TXBUF_SIZE_DEF;
209 cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
213 vcl_test_cfg_verify (vcl_test_cfg_t * cfg, vcl_test_cfg_t * valid_cfg)
215 /* Note: txbuf & rxbuf on server are the same buffer,
216 * so txbuf_size is not included in this check.
218 return ((cfg->magic == valid_cfg->magic)
219 && (cfg->test == valid_cfg->test)
220 && (cfg->verbose == valid_cfg->verbose)
221 && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
222 && (cfg->num_writes == valid_cfg->num_writes)
223 && (cfg->total_bytes == valid_cfg->total_bytes));
227 vcl_test_buf_alloc (vcl_test_cfg_t * cfg, uint8_t is_rxbuf, uint8_t ** buf,
230 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
231 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
236 cfg->rxbuf_size = *bufsize = alloc_size;
238 cfg->txbuf_size = *bufsize = alloc_size;
244 vtwrn ("realloc failed. using buffer size %d instead of %u",
245 *bufsize, alloc_size);
250 vcl_test_session_buf_alloc (vcl_test_session_t * socket)
252 socket->rxbuf_size = socket->cfg.rxbuf_size;
253 socket->txbuf_size = socket->cfg.txbuf_size;
254 vcl_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ ,
255 (uint8_t **) & socket->txbuf, &socket->txbuf_size);
256 vcl_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ ,
257 (uint8_t **) & socket->rxbuf, &socket->rxbuf_size);
261 vcl_test_type_str (vcl_test_t t)
265 case VCL_TEST_TYPE_NONE:
268 case VCL_TEST_TYPE_ECHO:
271 case VCL_TEST_TYPE_UNI:
274 case VCL_TEST_TYPE_BI:
277 case VCL_TEST_TYPE_EXIT:
286 vcl_test_cfg_dump (vcl_test_cfg_t * cfg, uint8_t is_client)
290 printf (" test config (%p):\n"
291 VCL_TEST_SEPARATOR_STRING
294 "%-5s test: %s (%d)\n"
295 " ctrl handle: %d (0x%x)\n"
296 "%-5s num test sockets: %u (0x%08x)\n"
297 "%-5s verbose: %s (%d)\n"
298 "%-5s rxbuf size: %lu (0x%08lx)\n"
299 "%-5s txbuf size: %lu (0x%08lx)\n"
300 "%-5s num writes: %lu (0x%08lx)\n"
301 " client tx bytes: %lu (0x%08lx)\n"
302 VCL_TEST_SEPARATOR_STRING,
303 (void *) cfg, cfg->magic, cfg->seq_num,
304 is_client && (cfg->test == VCL_TEST_TYPE_UNI) ?
305 "'" VCL_TEST_TOKEN_RUN_UNI "'" :
306 is_client && (cfg->test == VCL_TEST_TYPE_BI) ?
307 "'" VCL_TEST_TOKEN_RUN_BI "'" : spc,
308 vcl_test_type_str (cfg->test), cfg->test,
309 cfg->ctrl_handle, cfg->ctrl_handle,
310 is_client ? "'" VCL_TEST_TOKEN_NUM_TEST_SESS "'" : spc,
311 cfg->num_test_sessions, cfg->num_test_sessions,
312 is_client ? "'" VCL_TEST_TOKEN_VERBOSE "'" : spc,
313 cfg->verbose ? "on" : "off", cfg->verbose,
314 is_client ? "'" VCL_TEST_TOKEN_RXBUF_SIZE "'" : spc,
315 cfg->rxbuf_size, cfg->rxbuf_size,
316 is_client ? "'" VCL_TEST_TOKEN_TXBUF_SIZE "'" : spc,
317 cfg->txbuf_size, cfg->txbuf_size,
318 is_client ? "'" VCL_TEST_TOKEN_NUM_WRITES "'" : spc,
319 cfg->num_writes, cfg->num_writes,
320 cfg->total_bytes, cfg->total_bytes);
324 vcl_test_stats_dump (char *header, vcl_test_stats_t * stats,
325 uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
327 struct timespec diff;
328 double duration, rate;
329 uint64_t total_bytes;
331 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
333 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
334 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1e9;
338 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
339 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
341 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
343 total_bytes = stats->tx_bytes + stats->rx_bytes;
344 rate = (double) total_bytes *8 / duration / 1e9;
345 printf ("\n%s: Streamed %lu bytes\n"
346 " in %lf seconds (%lf Gbps %s-duplex)!\n",
347 header, total_bytes, duration, rate,
348 (show_rx && show_tx) ? "full" : "half");
352 printf (VCL_TEST_SEPARATOR_STRING
353 " tx stats (0x%p):\n"
354 VCL_TEST_SEPARATOR_STRING
355 " writes: %lu (0x%08lx)\n"
356 " tx bytes: %lu (0x%08lx)\n"
357 " tx eagain: %u (0x%08x)\n"
358 " tx incomplete: %u (0x%08x)\n",
359 (void *) stats, stats->tx_xacts, stats->tx_xacts,
360 stats->tx_bytes, stats->tx_bytes,
361 stats->tx_eagain, stats->tx_eagain,
362 stats->tx_incomp, stats->tx_incomp);
366 printf (VCL_TEST_SEPARATOR_STRING
367 " rx stats (0x%p):\n"
368 VCL_TEST_SEPARATOR_STRING
369 " reads: %lu (0x%08lx)\n"
370 " rx bytes: %lu (0x%08lx)\n"
371 " rx eagain: %u (0x%08x)\n"
372 " rx incomplete: %u (0x%08x)\n",
373 (void *) stats, stats->rx_xacts, stats->rx_xacts,
374 stats->rx_bytes, stats->rx_bytes,
375 stats->rx_eagain, stats->rx_eagain,
376 stats->rx_incomp, stats->rx_incomp);
379 printf (" start.tv_sec: %ld\n"
380 " start.tv_nsec: %ld\n"
381 " stop.tv_sec: %ld\n"
382 " stop.tv_nsec: %ld\n",
383 stats->start.tv_sec, stats->start.tv_nsec,
384 stats->stop.tv_sec, stats->stop.tv_nsec);
386 printf (VCL_TEST_SEPARATOR_STRING);
390 vcl_comp_tspec (struct timespec *a, struct timespec *b)
392 if (a->tv_sec < b->tv_sec)
394 else if (a->tv_sec > b->tv_sec)
396 else if (a->tv_nsec < b->tv_nsec)
398 else if (a->tv_nsec > b->tv_nsec)
405 vcl_test_read (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
407 vcl_test_stats_t *stats = &ts->stats;
413 rx_bytes = vppcom_session_read (ts->fd, buf, nbytes);
420 if ((rx_bytes == 0) ||
421 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
423 else if (rx_bytes < nbytes)
426 while ((rx_bytes == 0) ||
427 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
431 vterr ("vppcom_session_read()", -errno);
434 stats->rx_bytes += rx_bytes;
440 vcl_test_read_ds (vcl_test_session_t *ts)
442 vcl_test_stats_t *stats = &ts->stats;
448 rx_bytes = vppcom_session_read_segments (ts->fd, ts->ds, 2, ~0);
455 if ((rx_bytes == 0) ||
457 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
460 while ((rx_bytes == 0) ||
461 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
465 vterr ("vppcom_session_read()", -errno);
468 stats->rx_bytes += rx_bytes;
474 vcl_test_write (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
476 int tx_bytes = 0, nbytes_left = nbytes, rv;
477 vcl_test_stats_t *stats = &ts->stats;
482 rv = vppcom_session_write (ts->fd, buf, nbytes_left);
486 if ((errno == EAGAIN || errno == EWOULDBLOCK))
492 nbytes_left = nbytes_left - rv;
496 while (tx_bytes != nbytes);
500 vterr ("vpcom_session_write", -errno);
503 stats->tx_bytes += tx_bytes;
513 printf ("CLIENT: Test configuration commands:"
514 INDENT VCL_TEST_TOKEN_HELP
515 "\t\t\tDisplay help."
516 INDENT VCL_TEST_TOKEN_EXIT
517 "\t\t\tExit test client & server."
518 INDENT VCL_TEST_TOKEN_SHOW_CFG
519 "\t\t\tShow the current test cfg."
520 INDENT VCL_TEST_TOKEN_RUN_UNI
521 "\t\t\tRun the Uni-directional test."
522 INDENT VCL_TEST_TOKEN_RUN_BI
523 "\t\t\tRun the Bi-directional test."
524 INDENT VCL_TEST_TOKEN_VERBOSE
525 "\t\t\tToggle verbose setting."
526 INDENT VCL_TEST_TOKEN_RXBUF_SIZE
527 "<rxbuf size>\tRx buffer size (bytes)."
528 INDENT VCL_TEST_TOKEN_TXBUF_SIZE
529 "<txbuf size>\tTx buffer size (bytes)."
530 INDENT VCL_TEST_TOKEN_NUM_WRITES
531 "<# of writes>\tNumber of txbuf writes to server." "\n");
534 #endif /* __vcl_test_h__ */
537 * fd.io coding-style-patch-verification: ON
540 * eval: (c-set-style "gnu")