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