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