hsa: vcl test client epoll worker loop
[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 <netdb.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <vcl/vppcom.h>
25
26 #define vtfail(_fn, _rv)                                                \
27 {                                                                       \
28   errno = -_rv;                                                         \
29   perror ("ERROR when calling " _fn);                                   \
30   fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv);    \
31   exit (1);                                                             \
32 }
33
34 #define vterr(_fn, _rv)                                                 \
35 {                                                                       \
36   errno = -_rv;                                                         \
37   fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv);    \
38 }
39
40 #define vtwrn(_fmt, _args...)                                           \
41   fprintf (stderr, "\nERROR: " _fmt "\n", ##_args)                      \
42
43 #define vtinf(_fmt, _args...)                                           \
44   fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
45
46 #define vt_atomic_add(_ptr, _val)                                       \
47   __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
48
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"
59
60 #define VCL_TEST_SERVER_PORT            22000
61 #define VCL_TEST_LOCALHOST_IPADDR       "127.0.0.1"
62
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      ((uint32_t) 1e6)
69 #define VCL_TEST_CFG_MAX_SELECT_SESS    512
70 #define VCL_TEST_CFG_MAX_EPOLL_EVENTS   16
71
72 #define VCL_TEST_CTRL_LISTENER          (~0 - 1)
73 #define VCL_TEST_DATA_LISTENER          (~0)
74 #define VCL_TEST_DELAY_DISCONNECT       1
75 #define VCL_TEST_SEPARATOR_STRING       \
76   "  -----------------------------\n"
77 typedef enum
78 {
79   VCL_TEST_TYPE_NONE,
80   VCL_TEST_TYPE_ECHO,
81   VCL_TEST_TYPE_UNI,
82   VCL_TEST_TYPE_BI,
83   VCL_TEST_TYPE_EXIT,
84   VCL_TEST_TYPE_EXIT_CLIENT,
85 } vcl_test_t;
86
87 typedef enum
88 {
89   VCL_TEST_CMD_SYNC,
90   VCL_TEST_CMD_START,
91   VCL_TEST_CMD_STOP,
92 } vcl_test_cmd_t;
93
94 typedef struct __attribute__ ((packed))
95 {
96   uint32_t magic;
97   uint32_t seq_num;
98   uint32_t test;
99   uint32_t cmd;
100   uint32_t ctrl_handle;
101   uint32_t num_test_sessions;
102   uint32_t num_test_sessions_perq;
103   uint32_t num_test_qsessions;
104   uint32_t verbose;
105   uint32_t address_ip6;
106   uint32_t transport_udp;
107   uint64_t rxbuf_size;
108   uint64_t txbuf_size;
109   uint64_t num_writes;
110   uint64_t total_bytes;
111 } vcl_test_cfg_t;
112
113 typedef struct
114 {
115   uint64_t rx_xacts;
116   uint64_t rx_bytes;
117   uint32_t rx_eagain;
118   uint32_t rx_incomp;
119   uint64_t tx_xacts;
120   uint64_t tx_bytes;
121   uint32_t tx_eagain;
122   uint32_t tx_incomp;
123   struct timespec start;
124   struct timespec stop;
125 } vcl_test_stats_t;
126
127 typedef struct vcl_test_session
128 {
129   uint8_t is_done;
130   uint8_t is_alloc : 1;
131   uint8_t is_open : 1;
132   uint8_t noblk_connect : 1;
133   int fd;
134   int (*read) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
135   int (*write) (struct vcl_test_session *ts, void *buf, uint32_t buflen);
136   uint32_t txbuf_size;
137   uint32_t rxbuf_size;
138   char *txbuf;
139   char *rxbuf;
140   vcl_test_cfg_t cfg;
141   vcl_test_stats_t stats;
142   vcl_test_stats_t old_stats;
143   int session_index;
144   struct vcl_test_session *next;
145   vppcom_endpt_t endpt;
146   uint8_t ip[16];
147   vppcom_data_segment_t ds[2];
148   void *opaque;
149 } vcl_test_session_t;
150
151 static __thread int __wrk_index = 0;
152
153 static inline int
154 vcl_test_worker_index (void)
155 {
156   return __wrk_index;
157 }
158
159 typedef struct
160 {
161   int (*init) (vcl_test_cfg_t *cfg);
162   int (*open) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
163   int (*listen) (vcl_test_session_t *ts, vppcom_endpt_t *endpt);
164   int (*accept) (int listen_fd, vcl_test_session_t *ts);
165   int (*close) (vcl_test_session_t *ts);
166 } vcl_test_proto_vft_t;
167
168 typedef struct
169 {
170   vcl_test_session_t *qsessions;
171   uint32_t n_qsessions;
172   uint32_t n_sessions;
173 } vcl_test_wrk_t;
174
175 typedef struct
176 {
177   const vcl_test_proto_vft_t *protos[VPPCOM_PROTO_SRTP + 1];
178   uint32_t ckpair_index;
179   vcl_test_cfg_t cfg;
180   vcl_test_wrk_t *wrk;
181 } vcl_test_main_t;
182
183 extern vcl_test_main_t vcl_test_main;
184
185 #define VCL_TEST_REGISTER_PROTO(proto, vft)                                   \
186   static void __attribute__ ((constructor)) vcl_test_init_##proto (void)      \
187   {                                                                           \
188     vcl_test_main.protos[proto] = &vft;                                       \
189   }
190
191 static inline void
192 vcl_test_stats_accumulate (vcl_test_stats_t * accum, vcl_test_stats_t * incr)
193 {
194   accum->rx_xacts += incr->rx_xacts;
195   accum->rx_bytes += incr->rx_bytes;
196   accum->rx_eagain += incr->rx_eagain;
197   accum->rx_incomp += incr->rx_incomp;
198   accum->tx_xacts += incr->tx_xacts;
199   accum->tx_bytes += incr->tx_bytes;
200   accum->tx_eagain += incr->tx_eagain;
201   accum->tx_incomp += incr->tx_incomp;
202 }
203
204 static inline void
205 vcl_test_cfg_init (vcl_test_cfg_t * cfg)
206 {
207   cfg->magic = VCL_TEST_CFG_CTRL_MAGIC;
208   cfg->test = VCL_TEST_TYPE_UNI;
209   cfg->ctrl_handle = ~0;
210   cfg->num_test_sessions = 1;
211   cfg->num_test_sessions_perq = 1;
212   cfg->verbose = 0;
213   cfg->rxbuf_size = VCL_TEST_CFG_RXBUF_SIZE_DEF;
214   cfg->num_writes = VCL_TEST_CFG_NUM_WRITES_DEF;
215   cfg->txbuf_size = VCL_TEST_CFG_TXBUF_SIZE_DEF;
216   cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
217 }
218
219 static inline int
220 vcl_test_cfg_verify (vcl_test_cfg_t * cfg, vcl_test_cfg_t * valid_cfg)
221 {
222   /* Note: txbuf & rxbuf on server are the same buffer,
223    *       so txbuf_size is not included in this check.
224    */
225   return ((cfg->magic == valid_cfg->magic)
226           && (cfg->test == valid_cfg->test)
227           && (cfg->verbose == valid_cfg->verbose)
228           && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
229           && (cfg->num_writes == valid_cfg->num_writes)
230           && (cfg->total_bytes == valid_cfg->total_bytes));
231 }
232
233 static inline void
234 vcl_test_buf_alloc (vcl_test_cfg_t * cfg, uint8_t is_rxbuf, uint8_t ** buf,
235                     uint32_t * bufsize)
236 {
237   uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
238   uint8_t *lb = realloc (*buf, (size_t) alloc_size);
239
240   if (lb)
241     {
242       if (is_rxbuf)
243         cfg->rxbuf_size = *bufsize = alloc_size;
244       else
245         cfg->txbuf_size = *bufsize = alloc_size;
246
247       *buf = lb;
248     }
249   else
250     {
251       vtwrn ("realloc failed. using buffer size %d instead of %u",
252              *bufsize, alloc_size);
253     }
254 }
255
256 static inline void
257 vcl_test_session_buf_alloc (vcl_test_session_t *ts)
258 {
259   ts->rxbuf_size = ts->cfg.rxbuf_size;
260   ts->txbuf_size = ts->cfg.txbuf_size;
261   vcl_test_buf_alloc (&ts->cfg, 0 /* is_rxbuf */, (uint8_t **) &ts->txbuf,
262                       &ts->txbuf_size);
263   vcl_test_buf_alloc (&ts->cfg, 1 /* is_rxbuf */, (uint8_t **) &ts->rxbuf,
264                       &ts->rxbuf_size);
265 }
266
267 static inline void
268 vcl_test_session_buf_free (vcl_test_session_t *ts)
269 {
270   free (ts->rxbuf);
271   free (ts->txbuf);
272   ts->rxbuf = 0;
273   ts->txbuf = 0;
274 }
275
276 static inline char *
277 vcl_test_type_str (vcl_test_t t)
278 {
279   switch (t)
280     {
281     case VCL_TEST_TYPE_NONE:
282       return "NONE";
283
284     case VCL_TEST_TYPE_ECHO:
285       return "ECHO";
286
287     case VCL_TEST_TYPE_UNI:
288       return "UNI";
289
290     case VCL_TEST_TYPE_BI:
291       return "BI";
292
293     case VCL_TEST_TYPE_EXIT:
294       return "EXIT";
295
296     default:
297       return "Unknown";
298     }
299 }
300
301 static inline void
302 vcl_test_cfg_dump (vcl_test_cfg_t * cfg, uint8_t is_client)
303 {
304   char *spc = "     ";
305
306   printf ("  test config (%p):\n"
307           VCL_TEST_SEPARATOR_STRING
308           "                 magic:  0x%08x\n"
309           "               seq_num:  0x%08x\n"
310           "%-5s             test:  %s (%d)\n"
311           "           ctrl handle:  %d (0x%x)\n"
312           "%-5s num test sockets:  %u (0x%08x)\n"
313           "%-5s          verbose:  %s (%d)\n"
314           "%-5s       rxbuf size:  %lu (0x%08lx)\n"
315           "%-5s       txbuf size:  %lu (0x%08lx)\n"
316           "%-5s       num writes:  %lu (0x%08lx)\n"
317           "       client tx bytes:  %lu (0x%08lx)\n"
318           VCL_TEST_SEPARATOR_STRING,
319           (void *) cfg, cfg->magic, cfg->seq_num,
320           is_client && (cfg->test == VCL_TEST_TYPE_UNI) ?
321           "'" VCL_TEST_TOKEN_RUN_UNI "'" :
322           is_client && (cfg->test == VCL_TEST_TYPE_BI) ?
323           "'" VCL_TEST_TOKEN_RUN_BI "'" : spc,
324           vcl_test_type_str (cfg->test), cfg->test,
325           cfg->ctrl_handle, cfg->ctrl_handle,
326           is_client ? "'" VCL_TEST_TOKEN_NUM_TEST_SESS "'" : spc,
327           cfg->num_test_sessions, cfg->num_test_sessions,
328           is_client ? "'" VCL_TEST_TOKEN_VERBOSE "'" : spc,
329           cfg->verbose ? "on" : "off", cfg->verbose,
330           is_client ? "'" VCL_TEST_TOKEN_RXBUF_SIZE "'" : spc,
331           cfg->rxbuf_size, cfg->rxbuf_size,
332           is_client ? "'" VCL_TEST_TOKEN_TXBUF_SIZE "'" : spc,
333           cfg->txbuf_size, cfg->txbuf_size,
334           is_client ? "'" VCL_TEST_TOKEN_NUM_WRITES "'" : spc,
335           cfg->num_writes, cfg->num_writes,
336           cfg->total_bytes, cfg->total_bytes);
337 }
338
339 static inline void
340 vcl_test_stats_dump (char *header, vcl_test_stats_t * stats,
341                      uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
342 {
343   struct timespec diff;
344   double duration, rate;
345   uint64_t total_bytes;
346
347   if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
348     {
349       diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
350       diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1e9;
351     }
352   else
353     {
354       diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
355       diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
356     }
357   duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
358
359   total_bytes = stats->tx_bytes + stats->rx_bytes;
360   rate = (double) total_bytes *8 / duration / 1e9;
361   printf ("\n%s: Streamed %lu bytes\n"
362           "  in %lf seconds (%lf Gbps %s-duplex)!\n",
363           header, total_bytes, duration, rate,
364           (show_rx && show_tx) ? "full" : "half");
365
366   if (show_tx)
367     {
368       printf (VCL_TEST_SEPARATOR_STRING
369               "  tx stats (0x%p):\n"
370               VCL_TEST_SEPARATOR_STRING
371               "         writes:  %lu (0x%08lx)\n"
372               "       tx bytes:  %lu (0x%08lx)\n"
373               "      tx eagain:  %u (0x%08x)\n"
374               "  tx incomplete:  %u (0x%08x)\n",
375               (void *) stats, stats->tx_xacts, stats->tx_xacts,
376               stats->tx_bytes, stats->tx_bytes,
377               stats->tx_eagain, stats->tx_eagain,
378               stats->tx_incomp, stats->tx_incomp);
379     }
380   if (show_rx)
381     {
382       printf (VCL_TEST_SEPARATOR_STRING
383               "  rx stats (0x%p):\n"
384               VCL_TEST_SEPARATOR_STRING
385               "          reads:  %lu (0x%08lx)\n"
386               "       rx bytes:  %lu (0x%08lx)\n"
387               "      rx eagain:  %u (0x%08x)\n"
388               "  rx incomplete:  %u (0x%08x)\n",
389               (void *) stats, stats->rx_xacts, stats->rx_xacts,
390               stats->rx_bytes, stats->rx_bytes,
391               stats->rx_eagain, stats->rx_eagain,
392               stats->rx_incomp, stats->rx_incomp);
393     }
394   if (verbose)
395     printf ("   start.tv_sec:  %ld\n"
396             "  start.tv_nsec:  %ld\n"
397             "    stop.tv_sec:  %ld\n"
398             "   stop.tv_nsec:  %ld\n",
399             stats->start.tv_sec, stats->start.tv_nsec,
400             stats->stop.tv_sec, stats->stop.tv_nsec);
401
402   printf (VCL_TEST_SEPARATOR_STRING);
403 }
404
405 static inline double
406 vcl_test_time_diff (struct timespec *old, struct timespec *new)
407 {
408   uint64_t sec, nsec;
409   if ((new->tv_nsec - old->tv_nsec) < 0)
410     {
411       sec = new->tv_sec - old->tv_sec - 1;
412       nsec = new->tv_nsec - old->tv_nsec + 1e9;
413     }
414   else
415     {
416       sec = new->tv_sec - old->tv_sec;
417       nsec = new->tv_nsec - old->tv_nsec;
418     }
419   return (double) sec + (1e-9 * nsec);
420 }
421
422 static inline void
423 vcl_test_stats_dump_inc (vcl_test_session_t *ts, int is_rx)
424 {
425   vcl_test_stats_t *old, *new;
426   double duration, rate;
427   uint64_t total_bytes;
428   char *dir_str;
429
430   old = &ts->old_stats;
431   new = &ts->stats;
432   duration = vcl_test_time_diff (&old->stop, &new->stop);
433
434   if (is_rx)
435     {
436       total_bytes = new->rx_bytes - old->rx_bytes;
437       dir_str = "Received";
438     }
439   else
440     {
441       total_bytes = new->tx_bytes - old->tx_bytes;
442       dir_str = "Sent";
443     }
444
445   rate = (double) total_bytes * 8 / duration / 1e9;
446   printf ("%d: %s %lu Mbytes in %.2lf seconds %.2lf Gbps\n", ts->fd, dir_str,
447           (uint64_t) (total_bytes / 1e6), duration, rate);
448 }
449
450 static inline int
451 vcl_comp_tspec (struct timespec *a, struct timespec *b)
452 {
453   if (a->tv_sec < b->tv_sec)
454     return -1;
455   else if (a->tv_sec > b->tv_sec)
456     return 1;
457   else if (a->tv_nsec < b->tv_nsec)
458     return -1;
459   else if (a->tv_nsec > b->tv_nsec)
460     return 1;
461   else
462     return 0;
463 }
464
465 static inline int
466 vcl_test_read (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
467 {
468   vcl_test_stats_t *stats = &ts->stats;
469   int rv, rx_bytes = 0;
470
471   do
472     {
473       stats->rx_xacts++;
474       rv = vppcom_session_read (ts->fd, buf, nbytes);
475       if (rv <= 0)
476         {
477           errno = -rv;
478           if (errno == EAGAIN || errno == EWOULDBLOCK)
479             {
480               stats->rx_eagain++;
481               continue;
482             }
483
484           vterr ("vppcom_session_read()", -errno);
485           break;
486         }
487
488       rx_bytes = rv;
489       if (rv < nbytes)
490         stats->rx_incomp++;
491     }
492   while (!rx_bytes);
493
494   stats->rx_bytes += rx_bytes;
495
496   return (rx_bytes);
497 }
498
499 static inline int
500 vcl_test_read_ds (vcl_test_session_t *ts)
501 {
502   vcl_test_stats_t *stats = &ts->stats;
503   int rx_bytes;
504
505   do
506     {
507       stats->rx_xacts++;
508       rx_bytes = vppcom_session_read_segments (ts->fd, ts->ds, 2, ~0);
509
510       if (rx_bytes < 0)
511         {
512           errno = -rx_bytes;
513           rx_bytes = -1;
514         }
515           if ((rx_bytes == 0) ||
516               ((rx_bytes < 0)
517                && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
518             stats->rx_eagain++;
519     }
520   while ((rx_bytes == 0) ||
521          ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
522
523   if (rx_bytes < 0)
524     {
525       vterr ("vppcom_session_read()", -errno);
526     }
527   else
528     stats->rx_bytes += rx_bytes;
529
530   return (rx_bytes);
531 }
532
533 static inline int
534 vcl_test_write (vcl_test_session_t *ts, void *buf, uint32_t nbytes)
535 {
536   int tx_bytes = 0, nbytes_left = nbytes, rv;
537   vcl_test_stats_t *stats = &ts->stats;
538
539   do
540     {
541       stats->tx_xacts++;
542       rv = vppcom_session_write (ts->fd, buf, nbytes_left);
543       if (rv < 0)
544         {
545           errno = -rv;
546           if ((errno == EAGAIN || errno == EWOULDBLOCK))
547             stats->tx_eagain++;
548           break;
549         }
550       tx_bytes += rv;
551
552       nbytes_left = nbytes_left - rv;
553       buf += rv;
554       if (rv < nbytes_left)
555         stats->tx_incomp++;
556     }
557   while (tx_bytes != nbytes);
558
559   if (tx_bytes < 0)
560     {
561       vterr ("vpcom_session_write", -errno);
562     }
563   else
564     stats->tx_bytes += tx_bytes;
565
566   return (tx_bytes);
567 }
568
569 static inline void
570 dump_help (void)
571 {
572 #define INDENT "\n  "
573
574   printf ("CLIENT: Test configuration commands:"
575           INDENT VCL_TEST_TOKEN_HELP
576           "\t\t\tDisplay help."
577           INDENT VCL_TEST_TOKEN_EXIT
578           "\t\t\tExit test client & server."
579           INDENT VCL_TEST_TOKEN_SHOW_CFG
580           "\t\t\tShow the current test cfg."
581           INDENT VCL_TEST_TOKEN_RUN_UNI
582           "\t\t\tRun the Uni-directional test."
583           INDENT VCL_TEST_TOKEN_RUN_BI
584           "\t\t\tRun the Bi-directional test."
585           INDENT VCL_TEST_TOKEN_VERBOSE
586           "\t\t\tToggle verbose setting."
587           INDENT VCL_TEST_TOKEN_RXBUF_SIZE
588           "<rxbuf size>\tRx buffer size (bytes)."
589           INDENT VCL_TEST_TOKEN_TXBUF_SIZE
590           "<txbuf size>\tTx buffer size (bytes)."
591           INDENT VCL_TEST_TOKEN_NUM_WRITES
592           "<# of writes>\tNumber of txbuf writes to server." "\n");
593 }
594
595 #endif /* __vcl_test_h__ */
596
597 /*
598  * fd.io coding-style-patch-verification: ON
599  *
600  * Local Variables:
601  * eval: (c-set-style "gnu")
602  * End:
603  */