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 512
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"
83 VCL_TEST_TYPE_EXIT_CLIENT,
93 typedef struct __attribute__ ((packed))
100 uint32_t num_test_sessions;
101 uint32_t num_test_sessions_perq;
102 uint32_t num_test_qsessions;
104 uint32_t address_ip6;
105 uint32_t transport_udp;
109 uint64_t total_bytes;
122 struct timespec start;
123 struct timespec stop;
126 typedef struct vcl_test_session
132 int (*read) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
133 int (*write) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
139 vcl_test_stats_t stats;
140 vcl_test_stats_t old_stats;
142 vppcom_endpt_t endpt;
144 vppcom_data_segment_t ds[2];
146 } vcl_test_session_t;
148 static __thread int __wrk_index = 0;
151 vcl_test_worker_index (void)
158 int (*init) (vcl_test_cfg_t *cfg);
159 int (*open) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
160 int (*listen) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
161 int (*accept) (int listen_fd, vcl_test_session_t *ts);
162 int (*close) (vcl_test_session_t *ts);
163 } vcl_test_proto_vft_t;
167 vcl_test_session_t *qsessions;
168 uint32_t n_qsessions;
174 const vcl_test_proto_vft_t *protos[VPPCOM_PROTO_SRTP + 1];
175 uint32_t ckpair_index;
180 extern vcl_test_main_t vcl_test_main;
182 #define VCL_TEST_REGISTER_PROTO(proto, vft) \
183 static void __attribute__ ((constructor)) vcl_test_init_##proto (void) \
185 vcl_test_main.protos[proto] = &vft; \
189 vcl_test_stats_accumulate (vcl_test_stats_t * accum, vcl_test_stats_t * incr)
191 accum->rx_xacts += incr->rx_xacts;
192 accum->rx_bytes += incr->rx_bytes;
193 accum->rx_eagain += incr->rx_eagain;
194 accum->rx_incomp += incr->rx_incomp;
195 accum->tx_xacts += incr->tx_xacts;
196 accum->tx_bytes += incr->tx_bytes;
197 accum->tx_eagain += incr->tx_eagain;
198 accum->tx_incomp += incr->tx_incomp;
202 vcl_test_cfg_init (vcl_test_cfg_t * cfg)
204 cfg->magic = VCL_TEST_CFG_CTRL_MAGIC;
205 cfg->test = VCL_TEST_TYPE_UNI;
206 cfg->ctrl_handle = ~0;
207 cfg->num_test_sessions = 1;
208 cfg->num_test_sessions_perq = 1;
210 cfg->rxbuf_size = VCL_TEST_CFG_RXBUF_SIZE_DEF;
211 cfg->num_writes = VCL_TEST_CFG_NUM_WRITES_DEF;
212 cfg->txbuf_size = VCL_TEST_CFG_TXBUF_SIZE_DEF;
213 cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
217 vcl_test_cfg_verify (vcl_test_cfg_t * cfg, vcl_test_cfg_t * valid_cfg)
219 /* Note: txbuf & rxbuf on server are the same buffer,
220 * so txbuf_size is not included in this check.
222 return ((cfg->magic == valid_cfg->magic)
223 && (cfg->test == valid_cfg->test)
224 && (cfg->verbose == valid_cfg->verbose)
225 && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
226 && (cfg->num_writes == valid_cfg->num_writes)
227 && (cfg->total_bytes == valid_cfg->total_bytes));
231 vcl_test_buf_alloc (vcl_test_cfg_t * cfg, uint8_t is_rxbuf, uint8_t ** buf,
234 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
235 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
240 cfg->rxbuf_size = *bufsize = alloc_size;
242 cfg->txbuf_size = *bufsize = alloc_size;
248 vtwrn ("realloc failed. using buffer size %d instead of %u",
249 *bufsize, alloc_size);
254 vcl_test_session_buf_alloc (vcl_test_session_t *ts)
256 ts->rxbuf_size = ts->cfg.rxbuf_size;
257 ts->txbuf_size = ts->cfg.txbuf_size;
258 vcl_test_buf_alloc (&ts->cfg, 0 /* is_rxbuf */, (uint8_t **) &ts->txbuf,
260 vcl_test_buf_alloc (&ts->cfg, 1 /* is_rxbuf */, (uint8_t **) &ts->rxbuf,
265 vcl_test_session_buf_free (vcl_test_session_t *ts)
274 vcl_test_type_str (vcl_test_t t)
278 case VCL_TEST_TYPE_NONE:
281 case VCL_TEST_TYPE_ECHO:
284 case VCL_TEST_TYPE_UNI:
287 case VCL_TEST_TYPE_BI:
290 case VCL_TEST_TYPE_EXIT:
299 vcl_test_cfg_dump (vcl_test_cfg_t * cfg, uint8_t is_client)
303 printf (" test config (%p):\n"
304 VCL_TEST_SEPARATOR_STRING
307 "%-5s test: %s (%d)\n"
308 " ctrl handle: %d (0x%x)\n"
309 "%-5s num test sockets: %u (0x%08x)\n"
310 "%-5s verbose: %s (%d)\n"
311 "%-5s rxbuf size: %lu (0x%08lx)\n"
312 "%-5s txbuf size: %lu (0x%08lx)\n"
313 "%-5s num writes: %lu (0x%08lx)\n"
314 " client tx bytes: %lu (0x%08lx)\n"
315 VCL_TEST_SEPARATOR_STRING,
316 (void *) cfg, cfg->magic, cfg->seq_num,
317 is_client && (cfg->test == VCL_TEST_TYPE_UNI) ?
318 "'" VCL_TEST_TOKEN_RUN_UNI "'" :
319 is_client && (cfg->test == VCL_TEST_TYPE_BI) ?
320 "'" VCL_TEST_TOKEN_RUN_BI "'" : spc,
321 vcl_test_type_str (cfg->test), cfg->test,
322 cfg->ctrl_handle, cfg->ctrl_handle,
323 is_client ? "'" VCL_TEST_TOKEN_NUM_TEST_SESS "'" : spc,
324 cfg->num_test_sessions, cfg->num_test_sessions,
325 is_client ? "'" VCL_TEST_TOKEN_VERBOSE "'" : spc,
326 cfg->verbose ? "on" : "off", cfg->verbose,
327 is_client ? "'" VCL_TEST_TOKEN_RXBUF_SIZE "'" : spc,
328 cfg->rxbuf_size, cfg->rxbuf_size,
329 is_client ? "'" VCL_TEST_TOKEN_TXBUF_SIZE "'" : spc,
330 cfg->txbuf_size, cfg->txbuf_size,
331 is_client ? "'" VCL_TEST_TOKEN_NUM_WRITES "'" : spc,
332 cfg->num_writes, cfg->num_writes,
333 cfg->total_bytes, cfg->total_bytes);
337 vcl_test_stats_dump (char *header, vcl_test_stats_t * stats,
338 uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
340 struct timespec diff;
341 double duration, rate;
342 uint64_t total_bytes;
344 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
346 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
347 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1e9;
351 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
352 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
354 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
356 total_bytes = stats->tx_bytes + stats->rx_bytes;
357 rate = (double) total_bytes *8 / duration / 1e9;
358 printf ("\n%s: Streamed %lu bytes\n"
359 " in %lf seconds (%lf Gbps %s-duplex)!\n",
360 header, total_bytes, duration, rate,
361 (show_rx && show_tx) ? "full" : "half");
365 printf (VCL_TEST_SEPARATOR_STRING
366 " tx stats (0x%p):\n"
367 VCL_TEST_SEPARATOR_STRING
368 " writes: %lu (0x%08lx)\n"
369 " tx bytes: %lu (0x%08lx)\n"
370 " tx eagain: %u (0x%08x)\n"
371 " tx incomplete: %u (0x%08x)\n",
372 (void *) stats, stats->tx_xacts, stats->tx_xacts,
373 stats->tx_bytes, stats->tx_bytes,
374 stats->tx_eagain, stats->tx_eagain,
375 stats->tx_incomp, stats->tx_incomp);
379 printf (VCL_TEST_SEPARATOR_STRING
380 " rx stats (0x%p):\n"
381 VCL_TEST_SEPARATOR_STRING
382 " reads: %lu (0x%08lx)\n"
383 " rx bytes: %lu (0x%08lx)\n"
384 " rx eagain: %u (0x%08x)\n"
385 " rx incomplete: %u (0x%08x)\n",
386 (void *) stats, stats->rx_xacts, stats->rx_xacts,
387 stats->rx_bytes, stats->rx_bytes,
388 stats->rx_eagain, stats->rx_eagain,
389 stats->rx_incomp, stats->rx_incomp);
392 printf (" start.tv_sec: %ld\n"
393 " start.tv_nsec: %ld\n"
394 " stop.tv_sec: %ld\n"
395 " stop.tv_nsec: %ld\n",
396 stats->start.tv_sec, stats->start.tv_nsec,
397 stats->stop.tv_sec, stats->stop.tv_nsec);
399 printf (VCL_TEST_SEPARATOR_STRING);
403 vcl_test_time_diff (struct timespec *old, struct timespec *new)
406 if ((new->tv_nsec - old->tv_nsec) < 0)
408 sec = new->tv_sec - old->tv_sec - 1;
409 nsec = new->tv_nsec - old->tv_nsec + 1e9;
413 sec = new->tv_sec - old->tv_sec;
414 nsec = new->tv_nsec - old->tv_nsec;
416 return (double) sec + (1e-9 * nsec);
420 vcl_test_stats_dump_inc (vcl_test_session_t *ts, int is_rx)
422 vcl_test_stats_t *old, *new;
423 double duration, rate;
424 uint64_t total_bytes;
427 old = &ts->old_stats;
429 duration = vcl_test_time_diff (&old->stop, &new->stop);
433 total_bytes = new->rx_bytes - old->rx_bytes;
434 dir_str = "Received";
438 total_bytes = new->tx_bytes - old->tx_bytes;
442 rate = (double) total_bytes * 8 / duration / 1e9;
443 printf ("%d: %s %lu Mbytes in %.2lf seconds %.2lf Gbps\n", ts->fd, dir_str,
444 (uint64_t) (total_bytes / 1e6), duration, rate);
448 vcl_comp_tspec (struct timespec *a, struct timespec *b)
450 if (a->tv_sec < b->tv_sec)
452 else if (a->tv_sec > b->tv_sec)
454 else if (a->tv_nsec < b->tv_nsec)
456 else if (a->tv_nsec > b->tv_nsec)
463 vcl_test_read (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
465 vcl_test_stats_t *stats = &ts->stats;
466 int rv, rx_bytes = 0;
471 rv = vppcom_session_read (ts->fd, buf, nbytes);
475 if (errno == EAGAIN || errno == EWOULDBLOCK)
481 vterr ("vppcom_session_read()", -errno);
491 stats->rx_bytes += rx_bytes;
497 vcl_test_read_ds (vcl_test_session_t *ts)
499 vcl_test_stats_t *stats = &ts->stats;
505 rx_bytes = vppcom_session_read_segments (ts->fd, ts->ds, 2, ~0);
512 if ((rx_bytes == 0) ||
514 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
517 while ((rx_bytes == 0) ||
518 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
522 vterr ("vppcom_session_read()", -errno);
525 stats->rx_bytes += rx_bytes;
531 vcl_test_write (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
533 int tx_bytes = 0, nbytes_left = nbytes, rv;
534 vcl_test_stats_t *stats = &ts->stats;
539 rv = vppcom_session_write (ts->fd, buf, nbytes_left);
543 if ((errno == EAGAIN || errno == EWOULDBLOCK))
549 nbytes_left = nbytes_left - rv;
551 if (rv < nbytes_left)
554 while (tx_bytes != nbytes);
558 vterr ("vpcom_session_write", -errno);
561 stats->tx_bytes += tx_bytes;
571 printf ("CLIENT: Test configuration commands:"
572 INDENT VCL_TEST_TOKEN_HELP
573 "\t\t\tDisplay help."
574 INDENT VCL_TEST_TOKEN_EXIT
575 "\t\t\tExit test client & server."
576 INDENT VCL_TEST_TOKEN_SHOW_CFG
577 "\t\t\tShow the current test cfg."
578 INDENT VCL_TEST_TOKEN_RUN_UNI
579 "\t\t\tRun the Uni-directional test."
580 INDENT VCL_TEST_TOKEN_RUN_BI
581 "\t\t\tRun the Bi-directional test."
582 INDENT VCL_TEST_TOKEN_VERBOSE
583 "\t\t\tToggle verbose setting."
584 INDENT VCL_TEST_TOKEN_RXBUF_SIZE
585 "<rxbuf size>\tRx buffer size (bytes)."
586 INDENT VCL_TEST_TOKEN_TXBUF_SIZE
587 "<txbuf size>\tTx buffer size (bytes)."
588 INDENT VCL_TEST_TOKEN_NUM_WRITES
589 "<# of writes>\tNumber of txbuf writes to server." "\n");
592 #endif /* __vcl_test_h__ */
595 * fd.io coding-style-patch-verification: ON
598 * eval: (c-set-style "gnu")