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__
19 #include <hs_apps/hs_test.h>
25 #include <vcl/vppcom.h>
27 #define vtfail(_fn, _rv) \
30 perror ("ERROR when calling " _fn); \
31 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
35 #define vterr(_fn, _rv) \
38 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
41 #define vtwrn(_fmt, _args...) \
42 fprintf (stderr, "\nERROR: " _fmt "\n", ##_args) \
44 #define vtinf(_fmt, _args...) \
45 fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
47 #define vt_atomic_add(_ptr, _val) \
48 __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
50 #define VCL_TEST_SERVER_PORT 22000
51 #define VCL_TEST_LOCALHOST_IPADDR "127.0.0.1"
53 #define VCL_TEST_CFG_BUF_SIZE_MIN 128
54 #define VCL_TEST_CFG_MAX_TEST_SESS ((uint32_t) 1e6)
55 #define VCL_TEST_CFG_MAX_SELECT_SESS 512
56 #define VCL_TEST_CFG_INIT_TEST_SESS 512
57 #define VCL_TEST_CFG_MAX_EPOLL_EVENTS 16
59 #define VCL_TEST_CTRL_LISTENER (~0 - 1)
60 #define VCL_TEST_DATA_LISTENER (~0)
61 #define VCL_TEST_DELAY_DISCONNECT 1
73 struct timespec start;
77 typedef struct vcl_test_session
82 uint8_t noblk_connect : 1;
84 int (*read) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
85 int (*write) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
91 vcl_test_stats_t stats;
92 vcl_test_stats_t old_stats;
94 struct vcl_test_session *next;
97 vppcom_data_segment_t ds[2];
101 static __thread int __wrk_index = 0;
104 vcl_test_worker_index (void)
111 int (*init) (hs_test_cfg_t *cfg);
112 int (*open) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
113 int (*listen) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
114 int (*accept) (int listen_fd, vcl_test_session_t *ts);
115 int (*close) (vcl_test_session_t *ts);
116 } vcl_test_proto_vft_t;
120 vcl_test_session_t *qsessions;
121 uint32_t n_qsessions;
127 const vcl_test_proto_vft_t *protos[VPPCOM_PROTO_SRTP + 1];
128 uint32_t ckpair_index;
133 extern vcl_test_main_t vcl_test_main;
135 #define VCL_TEST_REGISTER_PROTO(proto, vft) \
136 static void __attribute__ ((constructor)) vcl_test_init_##proto (void) \
138 vcl_test_main.protos[proto] = &vft; \
142 vcl_test_stats_accumulate (vcl_test_stats_t * accum, vcl_test_stats_t * incr)
144 accum->rx_xacts += incr->rx_xacts;
145 accum->rx_bytes += incr->rx_bytes;
146 accum->rx_eagain += incr->rx_eagain;
147 accum->rx_incomp += incr->rx_incomp;
148 accum->tx_xacts += incr->tx_xacts;
149 accum->tx_bytes += incr->tx_bytes;
150 accum->tx_eagain += incr->tx_eagain;
151 accum->tx_incomp += incr->tx_incomp;
155 vcl_test_buf_alloc (hs_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf,
158 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
159 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
164 cfg->rxbuf_size = *bufsize = alloc_size;
166 cfg->txbuf_size = *bufsize = alloc_size;
172 vtwrn ("realloc failed. using buffer size %d instead of %u",
173 *bufsize, alloc_size);
178 vcl_test_session_buf_alloc (vcl_test_session_t *ts)
180 ts->rxbuf_size = ts->cfg.rxbuf_size;
181 ts->txbuf_size = ts->cfg.txbuf_size;
182 vcl_test_buf_alloc (&ts->cfg, 0 /* is_rxbuf */, (uint8_t **) &ts->txbuf,
184 vcl_test_buf_alloc (&ts->cfg, 1 /* is_rxbuf */, (uint8_t **) &ts->rxbuf,
189 vcl_test_session_buf_free (vcl_test_session_t *ts)
198 vcl_test_stats_dump (char *header, vcl_test_stats_t * stats,
199 uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
201 struct timespec diff;
202 double duration, rate;
203 uint64_t total_bytes;
205 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
207 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
208 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1e9;
212 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
213 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
215 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
217 total_bytes = stats->tx_bytes + stats->rx_bytes;
218 rate = (double) total_bytes *8 / duration / 1e9;
219 printf ("\n%s: Streamed %lu bytes\n"
220 " in %lf seconds (%lf Gbps %s-duplex)!\n",
221 header, total_bytes, duration, rate,
222 (show_rx && show_tx) ? "full" : "half");
226 printf (HS_TEST_SEPARATOR_STRING
227 " tx stats (0x%p):\n" HS_TEST_SEPARATOR_STRING
228 " writes: %lu (0x%08lx)\n"
229 " tx bytes: %lu (0x%08lx)\n"
230 " tx eagain: %u (0x%08x)\n"
231 " tx incomplete: %u (0x%08x)\n",
232 (void *) stats, stats->tx_xacts, stats->tx_xacts,
233 stats->tx_bytes, stats->tx_bytes, stats->tx_eagain,
234 stats->tx_eagain, stats->tx_incomp, stats->tx_incomp);
238 printf (HS_TEST_SEPARATOR_STRING
239 " rx stats (0x%p):\n" HS_TEST_SEPARATOR_STRING
240 " reads: %lu (0x%08lx)\n"
241 " rx bytes: %lu (0x%08lx)\n"
242 " rx eagain: %u (0x%08x)\n"
243 " rx incomplete: %u (0x%08x)\n",
244 (void *) stats, stats->rx_xacts, stats->rx_xacts,
245 stats->rx_bytes, stats->rx_bytes, stats->rx_eagain,
246 stats->rx_eagain, stats->rx_incomp, stats->rx_incomp);
249 printf (" start.tv_sec: %ld\n"
250 " start.tv_nsec: %ld\n"
251 " stop.tv_sec: %ld\n"
252 " stop.tv_nsec: %ld\n",
253 stats->start.tv_sec, stats->start.tv_nsec,
254 stats->stop.tv_sec, stats->stop.tv_nsec);
256 printf (HS_TEST_SEPARATOR_STRING);
260 vcl_test_time_diff (struct timespec *old, struct timespec *new)
263 if ((new->tv_nsec - old->tv_nsec) < 0)
265 sec = new->tv_sec - old->tv_sec - 1;
266 nsec = new->tv_nsec - old->tv_nsec + 1e9;
270 sec = new->tv_sec - old->tv_sec;
271 nsec = new->tv_nsec - old->tv_nsec;
273 return (double) sec + (1e-9 * nsec);
277 vcl_test_stats_dump_inc (vcl_test_session_t *ts, int is_rx)
279 vcl_test_stats_t *old, *new;
280 double duration, rate;
281 uint64_t total_bytes;
284 old = &ts->old_stats;
286 duration = vcl_test_time_diff (&old->stop, &new->stop);
290 total_bytes = new->rx_bytes - old->rx_bytes;
291 dir_str = "Received";
295 total_bytes = new->tx_bytes - old->tx_bytes;
299 rate = (double) total_bytes * 8 / duration / 1e9;
300 printf ("%d: %s %lu Mbytes in %.2lf seconds %.2lf Gbps\n", ts->fd, dir_str,
301 (uint64_t) (total_bytes / 1e6), duration, rate);
305 vcl_comp_tspec (struct timespec *a, struct timespec *b)
307 if (a->tv_sec < b->tv_sec)
309 else if (a->tv_sec > b->tv_sec)
311 else if (a->tv_nsec < b->tv_nsec)
313 else if (a->tv_nsec > b->tv_nsec)
320 vcl_test_read (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
322 vcl_test_stats_t *stats = &ts->stats;
323 int rv, rx_bytes = 0;
328 rv = vppcom_session_read (ts->fd, buf, nbytes);
332 if (errno == EAGAIN || errno == EWOULDBLOCK)
338 vterr ("vppcom_session_read()", -errno);
348 stats->rx_bytes += rx_bytes;
354 vcl_test_read_ds (vcl_test_session_t *ts)
356 vcl_test_stats_t *stats = &ts->stats;
362 rx_bytes = vppcom_session_read_segments (ts->fd, ts->ds, 2, ~0);
369 if ((rx_bytes == 0) ||
371 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
374 while ((rx_bytes == 0) ||
375 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
379 vterr ("vppcom_session_read()", -errno);
382 stats->rx_bytes += rx_bytes;
388 vcl_test_write (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
390 int tx_bytes = 0, nbytes_left = nbytes, rv;
391 vcl_test_stats_t *stats = &ts->stats;
396 rv = vppcom_session_write (ts->fd, buf, nbytes_left);
400 if ((errno == EAGAIN || errno == EWOULDBLOCK))
406 nbytes_left = nbytes_left - rv;
408 if (rv < nbytes_left)
411 while (tx_bytes != nbytes);
415 vterr ("vpcom_session_write", -errno);
418 stats->tx_bytes += tx_bytes;
429 "CLIENT: Test configuration commands:" INDENT VCL_TEST_TOKEN_HELP
430 "\t\t\tDisplay help." INDENT VCL_TEST_TOKEN_EXIT
431 "\t\t\tExit test client & server." INDENT VCL_TEST_TOKEN_SHOW_CFG
432 "\t\t\tShow the current test cfg." INDENT HS_TEST_TOKEN_RUN_UNI
433 "\t\t\tRun the Uni-directional test." INDENT HS_TEST_TOKEN_RUN_BI
434 "\t\t\tRun the Bi-directional test." INDENT VCL_TEST_TOKEN_VERBOSE
435 "\t\t\tToggle verbose setting." INDENT VCL_TEST_TOKEN_RXBUF_SIZE
436 "<rxbuf size>\tRx buffer size (bytes)." INDENT VCL_TEST_TOKEN_TXBUF_SIZE
437 "<txbuf size>\tTx buffer size (bytes)." INDENT VCL_TEST_TOKEN_NUM_WRITES
438 "<# of writes>\tNumber of txbuf writes to server."
442 #endif /* __vcl_test_h__ */
445 * fd.io coding-style-patch-verification: ON
448 * eval: (c-set-style "gnu")