0729c310f50c376fe1d3aee0484997f04fec8b8e
[vpp.git] / src / vcl / sock_test.h
1 /*
2  * Copyright (c) 2017 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 __sock_test_h__
17 #define __sock_test_h__
18
19 #include <netdb.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #define SOCK_TEST_TOKEN_HELP           "#H"
25 #define SOCK_TEST_TOKEN_EXIT           "#X"
26 #define SOCK_TEST_TOKEN_VERBOSE        "#V"
27 #define SOCK_TEST_TOKEN_TXBUF_SIZE     "#T:"
28 #define SOCK_TEST_TOKEN_NUM_TEST_SCKTS "#I:"
29 #define SOCK_TEST_TOKEN_NUM_WRITES     "#N:"
30 #define SOCK_TEST_TOKEN_RXBUF_SIZE     "#R:"
31 #define SOCK_TEST_TOKEN_SHOW_CFG       "#C"
32 #define SOCK_TEST_TOKEN_RUN_UNI        "#U"
33 #define SOCK_TEST_TOKEN_RUN_BI         "#B"
34
35 #define SOCK_TEST_BANNER_STRING \
36   "============================================\n"
37 #define SOCK_TEST_SEPARATOR_STRING \
38   "  -----------------------------\n"
39
40 #define ONE_GIG                       (1024*1024*1024)
41 #define SOCK_TEST_SERVER_PORT         22000
42 #define SOCK_TEST_LOCALHOST_IPADDR    "127.0.0.1"
43
44 #define SOCK_TEST_CFG_CTRL_MAGIC      0xfeedface
45 #define SOCK_TEST_CFG_NUM_WRITES_DEF  1000000
46 #define SOCK_TEST_CFG_TXBUF_SIZE_DEF  8192
47 #define SOCK_TEST_CFG_RXBUF_SIZE_DEF  (64*SOCK_TEST_CFG_TXBUF_SIZE_DEF)
48 #define SOCK_TEST_CFG_BUF_SIZE_MIN    128
49 #define SOCK_TEST_CFG_MAX_TEST_SCKTS  5
50
51 #define SOCK_TEST_AF_UNIX_FILENAME    "/tmp/ldp_server_af_unix_socket"
52 #define SOCK_TEST_MIXED_EPOLL_DATA    "Hello, world! (over an AF_UNIX socket)"
53 #define SOCK_TEST_AF_UNIX_ACCEPT_DATA 0xaf0000af
54 #define SOCK_TEST_AF_UNIX_FD_MASK     0x00af0000
55
56 typedef enum
57 {
58   SOCK_TEST_TYPE_NONE,
59   SOCK_TEST_TYPE_ECHO,
60   SOCK_TEST_TYPE_UNI,
61   SOCK_TEST_TYPE_BI,
62   SOCK_TEST_TYPE_EXIT,
63 } sock_test_t;
64
65 typedef struct  __attribute__ ((packed))
66 {
67   uint32_t magic;
68   uint32_t test;
69   uint32_t ctrl_handle;
70   uint32_t num_test_sockets;
71   uint32_t verbose;
72   uint64_t rxbuf_size;
73   uint64_t txbuf_size;
74   uint64_t num_writes;
75   uint64_t total_bytes;
76 } sock_test_cfg_t;
77
78 typedef struct
79 {
80   uint64_t rx_xacts;
81   uint64_t rx_bytes;
82   uint32_t rx_eagain;
83   uint32_t rx_incomp;
84   uint64_t tx_xacts;
85   uint64_t tx_bytes;
86   uint32_t tx_eagain;
87   uint32_t tx_incomp;
88   struct timespec start;
89   struct timespec stop;
90 } sock_test_stats_t;
91
92 typedef struct
93 {
94   int fd;
95   uint32_t txbuf_size;
96   char *txbuf;
97   uint32_t rxbuf_size;
98   char *rxbuf;
99   sock_test_cfg_t cfg;
100   sock_test_stats_t stats;
101 } sock_test_socket_t;
102
103 static inline void
104 sock_test_stats_accumulate (sock_test_stats_t * accum,
105                             sock_test_stats_t * incr)
106 {
107   accum->rx_xacts += incr->rx_xacts;
108   accum->rx_bytes += incr->rx_bytes;
109   accum->rx_eagain += incr->rx_eagain;
110   accum->rx_incomp += incr->rx_incomp;
111   accum->tx_xacts += incr->tx_xacts;
112   accum->tx_bytes += incr->tx_bytes;
113   accum->tx_eagain += incr->tx_eagain;
114   accum->tx_incomp += incr->tx_incomp;
115 }
116
117 static inline void
118 sock_test_cfg_init (sock_test_cfg_t *cfg)
119 {
120   cfg->magic = SOCK_TEST_CFG_CTRL_MAGIC;
121   cfg->test = SOCK_TEST_TYPE_NONE;
122   cfg->ctrl_handle = ~0;
123   cfg->num_test_sockets = 1;
124   cfg->verbose = 0;
125   cfg->rxbuf_size = SOCK_TEST_CFG_RXBUF_SIZE_DEF;
126   cfg->num_writes = SOCK_TEST_CFG_NUM_WRITES_DEF;
127   cfg->txbuf_size = SOCK_TEST_CFG_TXBUF_SIZE_DEF;
128   cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
129 }
130
131 static inline int
132 sock_test_cfg_verify (sock_test_cfg_t *cfg, sock_test_cfg_t *valid_cfg)
133 {
134   /* Note: txbuf & rxbuf on server are the same buffer,
135    *       so txbuf_size is not included in this check.
136    */
137   return ((cfg->magic == valid_cfg->magic)
138           && (cfg->test == valid_cfg->test)
139           && (cfg->verbose == valid_cfg->verbose)
140           && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
141           && (cfg->num_writes == valid_cfg->num_writes)
142           && (cfg->total_bytes == valid_cfg->total_bytes));
143 }
144
145 static inline void
146 sock_test_buf_alloc (sock_test_cfg_t *cfg, uint8_t is_rxbuf, uint8_t **buf,
147                      uint32_t *bufsize)
148 {
149   uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
150   uint8_t *lb = realloc (*buf, (size_t) alloc_size);
151
152   if (lb)
153     {
154       if (is_rxbuf)
155         cfg->rxbuf_size = *bufsize = alloc_size;
156       else
157         cfg->txbuf_size = *bufsize = alloc_size;
158       
159       *buf = lb;
160     }
161   else
162     {
163       int errno_val = errno;
164       perror ("ERROR in sock_test_buf_alloc()");
165       fprintf (stderr, "SOCK_TEST: ERROR: Buffer allocation "
166                "failed (errno = %d)!\n"
167                "       Using buffer size %d instead of desired"
168                " size (%d)\n", errno_val, *bufsize, alloc_size);
169     }
170 }
171
172 static inline void
173 sock_test_socket_buf_alloc (sock_test_socket_t *socket)
174 {
175   socket->rxbuf_size = socket->cfg.rxbuf_size;
176   socket->txbuf_size = socket->cfg.txbuf_size;
177   sock_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ ,
178                        (uint8_t **) &socket->txbuf, &socket->txbuf_size);
179   sock_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ ,
180                        (uint8_t **) &socket->rxbuf, &socket->rxbuf_size);
181 }
182
183 static inline char *
184 sock_test_type_str (sock_test_t t)
185 {
186   switch (t)
187     {
188     case SOCK_TEST_TYPE_NONE:
189       return "NONE";
190
191     case SOCK_TEST_TYPE_ECHO:
192       return "ECHO";
193
194     case SOCK_TEST_TYPE_UNI:
195       return "UNI";
196
197     case SOCK_TEST_TYPE_BI:
198       return "BI";
199
200     case SOCK_TEST_TYPE_EXIT:
201       return "EXIT";
202
203     default:
204       return "Unknown";
205     }
206 }
207
208 static inline void
209 sock_test_cfg_dump (sock_test_cfg_t * cfg, uint8_t is_client)
210 {
211   char *spc = "     ";
212   
213   printf ("  test config (%p):\n"
214           SOCK_TEST_SEPARATOR_STRING
215           "                 magic:  0x%08x\n"
216           "%-5s             test:  %s (%d)\n"
217           "           ctrl handle:  %d (0x%x)\n"
218           "%-5s num test sockets:  %u (0x%08x)\n"
219           "%-5s          verbose:  %s (%d)\n"
220           "%-5s       rxbuf size:  %lu (0x%08lx)\n"
221           "%-5s       txbuf size:  %lu (0x%08lx)\n"
222           "%-5s       num writes:  %lu (0x%08lx)\n"
223           "       client tx bytes:  %lu (0x%08lx)\n"
224           SOCK_TEST_SEPARATOR_STRING,
225           (void *) cfg, cfg->magic,
226           is_client && (cfg->test == SOCK_TEST_TYPE_UNI) ?
227           "'"SOCK_TEST_TOKEN_RUN_UNI"'" :
228           is_client && (cfg->test == SOCK_TEST_TYPE_BI) ?
229            "'"SOCK_TEST_TOKEN_RUN_BI"'" : spc,
230           sock_test_type_str (cfg->test), cfg->test,
231           cfg->ctrl_handle, cfg->ctrl_handle,
232           is_client ? "'"SOCK_TEST_TOKEN_NUM_TEST_SCKTS"'" : spc,
233           cfg->num_test_sockets, cfg->num_test_sockets,
234           is_client ? "'"SOCK_TEST_TOKEN_VERBOSE"'" : spc,
235           cfg->verbose ? "on" : "off", cfg->verbose,
236           is_client ? "'"SOCK_TEST_TOKEN_RXBUF_SIZE"'" : spc,
237           cfg->rxbuf_size, cfg->rxbuf_size,
238           is_client ? "'"SOCK_TEST_TOKEN_TXBUF_SIZE"'" : spc,
239           cfg->txbuf_size, cfg->txbuf_size,
240           is_client ? "'"SOCK_TEST_TOKEN_NUM_WRITES"'" : spc,
241           cfg->num_writes, cfg->num_writes,
242           cfg->total_bytes, cfg->total_bytes);
243 }
244
245 static inline void
246 sock_test_stats_dump (char * header, sock_test_stats_t * stats,
247                       uint8_t show_rx, uint8_t show_tx,
248                       uint8_t verbose)
249 {
250   struct timespec diff;
251   double duration, rate;
252   uint64_t total_bytes;
253   
254   if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
255     {
256       diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
257       diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1000000000;
258     }
259   else
260     {
261       diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
262       diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
263     }
264   duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
265
266   total_bytes = stats->tx_bytes + stats->rx_bytes;
267   rate = (double) total_bytes * 8 / duration / ONE_GIG;
268   printf ("\n%s: Streamed %lu bytes\n"
269           "  in %lf seconds (%lf Gbps %s-duplex)!\n",
270               header, total_bytes, duration, rate,
271           (show_rx && show_tx) ? "full" : "half");
272
273   if (show_tx)
274     {
275       printf (SOCK_TEST_SEPARATOR_STRING
276               "  tx stats (0x%p):\n"
277               SOCK_TEST_SEPARATOR_STRING
278               "         writes:  %lu (0x%08lx)\n"
279               "       tx bytes:  %lu (0x%08lx)\n"
280               "      tx eagain:  %u (0x%08x)\n"
281               "  tx incomplete:  %u (0x%08x)\n",
282               (void *)stats, stats->tx_xacts, stats->tx_xacts,
283               stats->tx_bytes, stats->tx_bytes,
284               stats->tx_eagain, stats->tx_eagain,
285               stats->tx_incomp, stats->tx_incomp);
286     }
287   if (show_rx)
288     {
289       printf (SOCK_TEST_SEPARATOR_STRING
290               "  rx stats (0x%p):\n"
291               SOCK_TEST_SEPARATOR_STRING
292               "          reads:  %lu (0x%08lx)\n"
293               "       rx bytes:  %lu (0x%08lx)\n"
294               "      rx eagain:  %u (0x%08x)\n"
295               "  rx incomplete:  %u (0x%08x)\n",
296               (void *)stats, stats->rx_xacts, stats->rx_xacts,
297               stats->rx_bytes, stats->rx_bytes,
298               stats->rx_eagain, stats->rx_eagain,
299               stats->rx_incomp, stats->rx_incomp);
300     }
301   if (verbose)
302     printf ("   start.tv_sec:  %ld\n"
303             "  start.tv_nsec:  %ld\n"
304             "    stop.tv_sec:  %ld\n"
305             "   stop.tv_nsec:  %ld\n",
306             stats->start.tv_sec, stats->start.tv_nsec,
307             stats->stop.tv_sec, stats->stop.tv_nsec);
308             
309   printf (SOCK_TEST_SEPARATOR_STRING);
310   
311 #if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST)
312   printf ("  af_unix xacts:  %lu (0x%08lx)\n",
313           sock_server_main.af_unix_xacts);
314           
315   printf (SOCK_TEST_SEPARATOR_STRING);
316 #endif
317 }
318
319 static inline int
320 sock_test_read (int fd, uint8_t *buf, uint32_t nbytes,
321                 sock_test_stats_t *stats)
322 {
323   int rx_bytes, errno_val;
324   
325   do
326     {
327       if (stats)
328         stats->rx_xacts++;
329 #ifdef VCL_TEST
330       rx_bytes = vppcom_session_read (fd, buf, nbytes);
331
332       if (rx_bytes < 0)
333         {
334           errno = -rx_bytes;
335           rx_bytes = -1;
336         }
337 #else
338       rx_bytes = read (fd, buf, nbytes);
339 #endif
340       if (stats)
341         {
342           if ((rx_bytes == 0) ||
343               ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
344             stats->rx_eagain++;
345           else if (rx_bytes < nbytes)
346             stats->rx_incomp++;
347         }
348     }
349   while ((rx_bytes == 0) ||
350          ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
351   
352   if (rx_bytes < 0)
353     {
354       errno_val = errno;
355       perror ("ERROR in sock_test_read()");
356       fprintf (stderr, "SOCK_TEST: ERROR: socket read "
357                "failed (errno = %d)!\n", errno_val);
358       errno = errno_val;
359     }
360   else if (stats)
361     stats->rx_bytes += rx_bytes;
362
363   return (rx_bytes);
364 }
365
366 static inline int
367 sock_test_write (int fd, uint8_t *buf, uint32_t nbytes,
368                  sock_test_stats_t *stats, uint32_t verbose)
369 {
370   int tx_bytes = 0;
371   int nbytes_left = nbytes;
372   int rv, errno_val;
373
374   do
375     {
376       if (stats)
377         stats->tx_xacts++;
378 #ifdef VCL_TEST
379       rv = vppcom_session_write (fd, buf, nbytes_left);
380       if (rv < 0)
381         {
382           errno = -rv;
383           rv = -1;
384         }
385 #else
386       rv = write (fd, buf, nbytes_left);
387 #endif
388       if (rv < 0)
389         {
390           if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
391             {
392               if (stats)
393                 stats->tx_eagain++;
394               continue;
395             }
396           else
397             break;
398         }
399       tx_bytes += rv;
400      
401       if (tx_bytes != nbytes)
402         {
403           nbytes_left = nbytes_left - rv;
404           if (stats)
405             stats->tx_incomp++;
406           if (verbose)
407             {
408               printf ("SOCK_TEST: WARNING: bytes written (%d) "
409                       "!= bytes to write (%d)!\n", tx_bytes, nbytes);
410             }
411         }
412      
413     } while (tx_bytes != nbytes);
414
415   if (tx_bytes < 0)
416     {
417       errno_val = errno;
418       perror ("ERROR in sock_test_write()");
419       fprintf (stderr, "SOCK_TEST: ERROR: socket write failed "
420                "(errno = %d)!\n", errno_val);
421     }
422   else if (stats)
423     stats->tx_bytes += tx_bytes;
424   
425   return (tx_bytes);
426 }
427
428 #endif /* __sock_test_h__ */