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