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