ip6: fix ip6-michain trace function
[vpp.git] / src / plugins / hs_apps / vcl / vcl_test.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef __vcl_test_h__
17 #define __vcl_test_h__
18
19 #include <hs_apps/hs_test.h>
20 #include <netdb.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <vcl/vppcom.h>
26
27 #define vtfail(_fn, _rv)                                                \
28 {                                                                       \
29   errno = -_rv;                                                         \
30   perror ("ERROR when calling " _fn);                                   \
31   fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv);    \
32   exit (1);                                                             \
33 }
34
35 #define vterr(_fn, _rv)                                                 \
36 {                                                                       \
37   errno = -_rv;                                                         \
38   fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv);    \
39 }
40
41 #define vtwrn(_fmt, _args...)                                           \
42   fprintf (stderr, "\nERROR: " _fmt "\n", ##_args)                      \
43
44 #define vtinf(_fmt, _args...)                                           \
45   fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
46
47 #define vt_atomic_add(_ptr, _val)                                       \
48   __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
49
50 #define VCL_TEST_SERVER_PORT            22000
51 #define VCL_TEST_LOCALHOST_IPADDR       "127.0.0.1"
52
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
58
59 #define VCL_TEST_CTRL_LISTENER          (~0 - 1)
60 #define VCL_TEST_DATA_LISTENER          (~0)
61 #define VCL_TEST_DELAY_DISCONNECT       1
62
63 typedef struct
64 {
65   uint64_t rx_xacts;
66   uint64_t rx_bytes;
67   uint32_t rx_eagain;
68   uint32_t rx_incomp;
69   uint64_t tx_xacts;
70   uint64_t tx_bytes;
71   uint32_t tx_eagain;
72   uint32_t tx_incomp;
73   struct timespec start;
74   struct timespec stop;
75 } vcl_test_stats_t;
76
77 typedef struct vcl_test_session
78 {
79   uint8_t is_done;
80   uint8_t is_alloc : 1;
81   uint8_t is_open : 1;
82   uint8_t noblk_connect : 1;
83   int fd;
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);
86   uint32_t txbuf_size;
87   uint32_t rxbuf_size;
88   char *txbuf;
89   char *rxbuf;
90   hs_test_cfg_t cfg;
91   vcl_test_stats_t stats;
92   vcl_test_stats_t old_stats;
93   int session_index;
94   struct vcl_test_session *next;
95   vppcom_endpt_t endpt;
96   uint8_t ip[16];
97   vppcom_data_segment_t ds[2];
98   void *opaque;
99 } vcl_test_session_t;
100
101 static __thread int __wrk_index = 0;
102
103 static inline int
104 vcl_test_worker_index (void)
105 {
106   return __wrk_index;
107 }
108
109 typedef struct
110 {
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;
117
118 typedef struct
119 {
120   vcl_test_session_t *qsessions;
121   uint32_t n_qsessions;
122   uint32_t n_sessions;
123 } vcl_test_wrk_t;
124
125 typedef struct
126 {
127   const vcl_test_proto_vft_t *protos[VPPCOM_PROTO_SRTP + 1];
128   uint32_t ckpair_index;
129   hs_test_cfg_t cfg;
130   vcl_test_wrk_t *wrk;
131 } vcl_test_main_t;
132
133 extern vcl_test_main_t vcl_test_main;
134
135 #define VCL_TEST_REGISTER_PROTO(proto, vft)                                   \
136   static void __attribute__ ((constructor)) vcl_test_init_##proto (void)      \
137   {                                                                           \
138     vcl_test_main.protos[proto] = &vft;                                       \
139   }
140
141 static inline void
142 vcl_test_stats_accumulate (vcl_test_stats_t * accum, vcl_test_stats_t * incr)
143 {
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;
152 }
153
154 static inline void
155 vcl_test_buf_alloc (hs_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf,
156                     uint32_t *bufsize)
157 {
158   uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
159   uint8_t *lb = realloc (*buf, (size_t) alloc_size);
160
161   if (lb)
162     {
163       if (is_rxbuf)
164         cfg->rxbuf_size = *bufsize = alloc_size;
165       else
166         cfg->txbuf_size = *bufsize = alloc_size;
167
168       *buf = lb;
169     }
170   else
171     {
172       vtwrn ("realloc failed. using buffer size %d instead of %u",
173              *bufsize, alloc_size);
174     }
175 }
176
177 static inline void
178 vcl_test_session_buf_alloc (vcl_test_session_t *ts)
179 {
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,
183                       &ts->txbuf_size);
184   vcl_test_buf_alloc (&ts->cfg, 1 /* is_rxbuf */, (uint8_t **) &ts->rxbuf,
185                       &ts->rxbuf_size);
186 }
187
188 static inline void
189 vcl_test_session_buf_free (vcl_test_session_t *ts)
190 {
191   free (ts->rxbuf);
192   free (ts->txbuf);
193   ts->rxbuf = 0;
194   ts->txbuf = 0;
195 }
196
197 static inline void
198 vcl_test_stats_dump (char *header, vcl_test_stats_t * stats,
199                      uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
200 {
201   struct timespec diff;
202   double duration, rate;
203   uint64_t total_bytes;
204
205   if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
206     {
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;
209     }
210   else
211     {
212       diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
213       diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
214     }
215   duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
216
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");
223
224   if (show_tx)
225     {
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);
235     }
236   if (show_rx)
237     {
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);
247     }
248   if (verbose)
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);
255
256   printf (HS_TEST_SEPARATOR_STRING);
257 }
258
259 static inline double
260 vcl_test_time_diff (struct timespec *old, struct timespec *new)
261 {
262   uint64_t sec, nsec;
263   if ((new->tv_nsec - old->tv_nsec) < 0)
264     {
265       sec = new->tv_sec - old->tv_sec - 1;
266       nsec = new->tv_nsec - old->tv_nsec + 1e9;
267     }
268   else
269     {
270       sec = new->tv_sec - old->tv_sec;
271       nsec = new->tv_nsec - old->tv_nsec;
272     }
273   return (double) sec + (1e-9 * nsec);
274 }
275
276 static inline void
277 vcl_test_stats_dump_inc (vcl_test_session_t *ts, int is_rx)
278 {
279   vcl_test_stats_t *old, *new;
280   double duration, rate;
281   uint64_t total_bytes;
282   char *dir_str;
283
284   old = &ts->old_stats;
285   new = &ts->stats;
286   duration = vcl_test_time_diff (&old->stop, &new->stop);
287
288   if (is_rx)
289     {
290       total_bytes = new->rx_bytes - old->rx_bytes;
291       dir_str = "Received";
292     }
293   else
294     {
295       total_bytes = new->tx_bytes - old->tx_bytes;
296       dir_str = "Sent";
297     }
298
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);
302 }
303
304 static inline int
305 vcl_comp_tspec (struct timespec *a, struct timespec *b)
306 {
307   if (a->tv_sec < b->tv_sec)
308     return -1;
309   else if (a->tv_sec > b->tv_sec)
310     return 1;
311   else if (a->tv_nsec < b->tv_nsec)
312     return -1;
313   else if (a->tv_nsec > b->tv_nsec)
314     return 1;
315   else
316     return 0;
317 }
318
319 static inline int
320 vcl_test_read (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
321 {
322   vcl_test_stats_t *stats = &ts->stats;
323   int rv, rx_bytes = 0;
324
325   do
326     {
327       stats->rx_xacts++;
328       rv = vppcom_session_read (ts->fd, buf, nbytes);
329       if (rv <= 0)
330         {
331           errno = -rv;
332           if (errno == EAGAIN || errno == EWOULDBLOCK)
333             {
334               stats->rx_eagain++;
335               continue;
336             }
337
338           vterr ("vppcom_session_read()", -errno);
339           break;
340         }
341
342       rx_bytes = rv;
343       if (rv < nbytes)
344         stats->rx_incomp++;
345     }
346   while (!rx_bytes);
347
348   stats->rx_bytes += rx_bytes;
349
350   return (rx_bytes);
351 }
352
353 static inline int
354 vcl_test_read_ds (vcl_test_session_t *ts)
355 {
356   vcl_test_stats_t *stats = &ts->stats;
357   int rx_bytes;
358
359   do
360     {
361       stats->rx_xacts++;
362       rx_bytes = vppcom_session_read_segments (ts->fd, ts->ds, 2, ~0);
363
364       if (rx_bytes < 0)
365         {
366           errno = -rx_bytes;
367           rx_bytes = -1;
368         }
369           if ((rx_bytes == 0) ||
370               ((rx_bytes < 0)
371                && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
372             stats->rx_eagain++;
373     }
374   while ((rx_bytes == 0) ||
375          ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
376
377   if (rx_bytes < 0)
378     {
379       vterr ("vppcom_session_read()", -errno);
380     }
381   else
382     stats->rx_bytes += rx_bytes;
383
384   return (rx_bytes);
385 }
386
387 static inline int
388 vcl_test_write (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
389 {
390   int tx_bytes = 0, nbytes_left = nbytes, rv;
391   vcl_test_stats_t *stats = &ts->stats;
392
393   do
394     {
395       stats->tx_xacts++;
396       rv = vppcom_session_write (ts->fd, buf, nbytes_left);
397       if (rv < 0)
398         {
399           errno = -rv;
400           if ((errno == EAGAIN || errno == EWOULDBLOCK))
401             stats->tx_eagain++;
402           break;
403         }
404       tx_bytes += rv;
405
406       nbytes_left = nbytes_left - rv;
407       buf += rv;
408       if (rv < nbytes_left)
409         stats->tx_incomp++;
410     }
411   while (tx_bytes != nbytes);
412
413   if (tx_bytes < 0)
414     {
415       vterr ("vpcom_session_write", -errno);
416     }
417   else
418     stats->tx_bytes += tx_bytes;
419
420   return (tx_bytes);
421 }
422
423 static inline void
424 dump_help (void)
425 {
426 #define INDENT "\n  "
427
428   printf (
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."
439     "\n");
440 }
441
442 #endif /* __vcl_test_h__ */
443
444 /*
445  * fd.io coding-style-patch-verification: ON
446  *
447  * Local Variables:
448  * eval: (c-set-style "gnu")
449  * End:
450  */