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