VPP-598: tcp stack initial commit
[vpp.git] / src / uri / uritest.c
1 /*
2  * Copyright (c) 2016 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 #include <stdio.h>
17 #include <setjmp.h>
18 #include <vppinfra/clib.h>
19 #include <vppinfra/format.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/time.h>
22 #include <vppinfra/macros.h>
23 #include <vnet/vnet.h>
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vpp-api/vpe_msg_enum.h>
29 #include <svm_fifo_segment.h>
30
31 #define vl_typedefs             /* define message structures */
32 #include <vpp-api/vpe_all_api_h.h>
33 #undef vl_typedefs
34
35 /* declare message handlers for each api */
36
37 #define vl_endianfun            /* define message structures */
38 #include <vpp-api/vpe_all_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
43 #define vl_printfun
44 #include <vpp-api/vpe_all_api_h.h>
45 #undef vl_printfun
46
47 typedef enum
48 {
49   STATE_START,
50   STATE_READY,
51   STATE_DISCONNECTING,
52 } connection_state_t;
53
54 typedef struct
55 {
56   /* vpe input queue */
57   unix_shared_memory_queue_t *vl_input_queue;
58
59   /* API client handle */
60   u32 my_client_index;
61
62   /* role */
63   int i_am_master;
64
65   /* The URI we're playing with */
66   u8 *uri;
67
68   /* fifo segment */
69   svm_fifo_segment_private_t *seg;
70
71   svm_fifo_t *rx_fifo;
72   svm_fifo_t *tx_fifo;
73
74   /* For deadman timers */
75   clib_time_t clib_time;
76
77   /* State of the connection, shared between msg RX thread and main thread */
78   volatile connection_state_t state;
79
80   /* VNET_API_ERROR_FOO -> "Foo" hash table */
81   uword *error_string_by_error_number;
82 } uritest_main_t;
83
84 #if CLIB_DEBUG > 0
85 #define NITER 1000
86 #else
87 #define NITER 1000000
88 #endif
89
90 uritest_main_t uritest_main;
91
92 u8 *
93 format_api_error (u8 * s, va_list * args)
94 {
95   uritest_main_t *utm = va_arg (*args, uritest_main_t *);
96   i32 error = va_arg (*args, u32);
97   uword *p;
98
99   p = hash_get (utm->error_string_by_error_number, -error);
100
101   if (p)
102     s = format (s, "%s", p[0]);
103   else
104     s = format (s, "%d", error);
105   return s;
106 }
107
108 int
109 wait_for_state_change (uritest_main_t * utm, connection_state_t state)
110 {
111   f64 timeout = clib_time_now (&utm->clib_time) + 1.0;
112
113   while (clib_time_now (&utm->clib_time) < timeout)
114     {
115       if (utm->state == state)
116         return 0;
117     }
118   return -1;
119 }
120
121 static void
122 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
123 {
124   uritest_main_t *utm = &uritest_main;
125   svm_fifo_segment_create_args_t _a, *a = &_a;
126   int rv;
127
128   ASSERT (utm->i_am_master);
129
130   if (mp->segment_name_length == 0)
131     {
132       clib_warning ("segment_name_length zero");
133       return;
134     }
135
136   a->segment_name = (char *) mp->segment_name;
137   a->segment_size = mp->segment_size;
138
139   /* Create the segment */
140   rv = svm_fifo_segment_create (a);
141   if (rv)
142     {
143       clib_warning ("sm_fifo_segment_create ('%s') failed", mp->segment_name);
144       return;
145     }
146
147   vec_validate (utm->seg, 0);
148
149   memcpy (utm->seg, a->rv, sizeof (*utm->seg));
150
151   /*
152    * By construction the master's idea of the rx fifo ends up in
153    * fsh->fifos[0], and the master's idea of the tx fifo ends up in
154    * fsh->fifos[1].
155    */
156   utm->rx_fifo = svm_fifo_segment_alloc_fifo (utm->seg, 10240);
157   ASSERT (utm->rx_fifo);
158
159   utm->tx_fifo = svm_fifo_segment_alloc_fifo (utm->seg, 10240);
160   ASSERT (utm->tx_fifo);
161
162   utm->state = STATE_READY;
163 }
164
165 static void
166 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
167 {
168   uritest_main_t *utm = &uritest_main;
169   svm_fifo_segment_create_args_t _a, *a = &_a;
170   ssvm_shared_header_t *sh;
171   svm_fifo_segment_header_t *fsh;
172   int rv;
173
174   ASSERT (utm->i_am_master == 0);
175
176   if (mp->segment_name_length == 0)
177     {
178       clib_warning ("segment_name_length zero");
179       return;
180     }
181
182   memset (a, 0, sizeof (*a));
183
184   a->segment_name = (char *) mp->segment_name;
185
186   rv = svm_fifo_segment_attach (a);
187   if (rv)
188     {
189       clib_warning ("sm_fifo_segment_create ('%s') failed", mp->segment_name);
190       return;
191     }
192
193   vec_validate (utm->seg, 0);
194
195   memcpy (utm->seg, a->rv, sizeof (*utm->seg));
196   sh = utm->seg->ssvm.sh;
197   fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
198
199   while (vec_len (fsh->fifos) < 2)
200     sleep (1);
201
202   utm->rx_fifo = (svm_fifo_t *) fsh->fifos[1];
203   ASSERT (utm->rx_fifo);
204   utm->tx_fifo = (svm_fifo_t *) fsh->fifos[0];
205   ASSERT (utm->tx_fifo);
206
207   /* security: could unlink /dev/shm/<mp->segment_name> here, maybe */
208
209   utm->state = STATE_READY;
210 }
211
212 static void
213 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
214 {
215   uritest_main_t *utm = &uritest_main;
216
217   if (mp->retval != 0)
218     clib_warning ("returned %d", ntohl (mp->retval));
219
220   utm->state = STATE_START;
221 }
222
223 #define foreach_uri_msg                         \
224 _(BIND_URI_REPLY, bind_uri_reply)               \
225 _(CONNECT_URI_REPLY, connect_uri_reply)         \
226 _(UNBIND_URI_REPLY, unbind_uri_reply)
227
228 void
229 uri_api_hookup (uritest_main_t * utm)
230 {
231 #define _(N,n)                                                  \
232     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
233                            vl_api_##n##_t_handler,              \
234                            vl_noop_handler,                     \
235                            vl_api_##n##_t_endian,               \
236                            vl_api_##n##_t_print,                \
237                            sizeof(vl_api_##n##_t), 1);
238   foreach_uri_msg;
239 #undef _
240
241 }
242
243
244 int
245 connect_to_vpp (char *name)
246 {
247   uritest_main_t *utm = &uritest_main;
248   api_main_t *am = &api_main;
249
250   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
251     return -1;
252
253   utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
254   utm->my_client_index = am->my_client_index;
255
256   return 0;
257 }
258
259 void
260 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
261 {
262   clib_warning ("BUG");
263 }
264
265 static void
266 init_error_string_table (uritest_main_t * utm)
267 {
268   utm->error_string_by_error_number = hash_create (0, sizeof (uword));
269
270 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
271   foreach_vnet_api_error;
272 #undef _
273
274   hash_set (utm->error_string_by_error_number, 99, "Misc");
275 }
276
277 void
278 uritest_master (uritest_main_t * utm)
279 {
280   vl_api_bind_uri_t *bmp;
281   vl_api_unbind_uri_t *ump;
282   int i;
283   u8 *test_data = 0;
284   u8 *reply = 0;
285   u32 reply_len;
286   int mypid = getpid ();
287
288   for (i = 0; i < 2048; i++)
289     vec_add1 (test_data, 'a' + (i % 32));
290
291   bmp = vl_msg_api_alloc (sizeof (*bmp));
292   memset (bmp, 0, sizeof (*bmp));
293
294   bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
295   bmp->client_index = utm->my_client_index;
296   bmp->context = ntohl (0xfeedface);
297   bmp->segment_size = 256 << 10;
298   memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
299   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
300
301   if (wait_for_state_change (utm, STATE_READY))
302     {
303       clib_warning ("timeout waiting for STATE_READY");
304       return;
305     }
306
307   for (i = 0; i < NITER; i++)
308     svm_fifo_enqueue (utm->tx_fifo, mypid, vec_len (test_data), test_data);
309
310   vec_validate (reply, 0);
311
312   reply_len = svm_fifo_dequeue (utm->rx_fifo, mypid, vec_len (reply), reply);
313
314   if (reply_len != 1)
315     clib_warning ("reply length %d", reply_len);
316
317   if (reply[0] == 1)
318     fformat (stdout, "Test OK...");
319
320   ump = vl_msg_api_alloc (sizeof (*ump));
321   memset (ump, 0, sizeof (*ump));
322
323   ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
324   ump->client_index = utm->my_client_index;
325   memcpy (ump->uri, utm->uri, vec_len (utm->uri));
326   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
327
328   if (wait_for_state_change (utm, STATE_START))
329     {
330       clib_warning ("timeout waiting for STATE_READY");
331       return;
332     }
333
334   fformat (stdout, "Master done...\n");
335 }
336
337 void
338 uritest_slave (uritest_main_t * utm)
339 {
340   vl_api_connect_uri_t *cmp;
341   int i, j;
342   u8 *test_data = 0;
343   u8 *reply = 0;
344   u32 bytes_received = 0;
345   u32 actual_bytes;
346   int mypid = getpid ();
347   u8 ok;
348   f64 before, after, delta, bytes_per_second;
349
350   vec_validate (test_data, 4095);
351
352   cmp = vl_msg_api_alloc (sizeof (*cmp));
353   memset (cmp, 0, sizeof (*cmp));
354
355   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
356   cmp->client_index = utm->my_client_index;
357   cmp->context = ntohl (0xfeedface);
358   memcpy (cmp->uri, utm->uri, vec_len (utm->uri));
359   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
360
361   if (wait_for_state_change (utm, STATE_READY))
362     {
363       clib_warning ("timeout waiting for STATE_READY");
364       return;
365     }
366
367   ok = 1;
368   before = clib_time_now (&utm->clib_time);
369   for (i = 0; i < NITER; i++)
370     {
371       actual_bytes = svm_fifo_dequeue (utm->rx_fifo, mypid,
372                                        vec_len (test_data), test_data);
373       j = 0;
374       while (j < actual_bytes)
375         {
376           if (test_data[j] != ('a' + (bytes_received % 32)))
377             ok = 0;
378           bytes_received++;
379           j++;
380         }
381       if (bytes_received == NITER * 2048)
382         break;
383     }
384
385   vec_add1 (reply, ok);
386
387   svm_fifo_enqueue (utm->tx_fifo, mypid, vec_len (reply), reply);
388   after = clib_time_now (&utm->clib_time);
389   delta = after - before;
390   bytes_per_second = 0.0;
391
392   if (delta > 0.0)
393     bytes_per_second = (f64) bytes_received / delta;
394
395   fformat (stdout,
396            "Slave done, %d bytes in %.2f seconds, %.2f bytes/sec...\n",
397            bytes_received, delta, bytes_per_second);
398 }
399
400 int
401 main (int argc, char **argv)
402 {
403   uritest_main_t *utm = &uritest_main;
404   unformat_input_t _argv, *a = &_argv;
405   u8 *chroot_prefix;
406   u8 *heap;
407   char *bind_name = "fifo:uritest";
408   mheap_t *h;
409   int i_am_master = 0;
410
411   clib_mem_init (0, 128 << 20);
412
413   heap = clib_mem_get_per_cpu_heap ();
414   h = mheap_header (heap);
415
416   /* make the main heap thread-safe */
417   h->flags |= MHEAP_FLAG_THREAD_SAFE;
418
419   clib_time_init (&utm->clib_time);
420   init_error_string_table (utm);
421   svm_fifo_segment_init (0x200000000ULL, 20);
422   unformat_init_command_line (a, argv);
423
424   utm->uri = format (0, "%s%c", bind_name, 0);
425
426   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
427     {
428       if (unformat (a, "master"))
429         i_am_master = 1;
430       else if (unformat (a, "slave"))
431         i_am_master = 0;
432       else if (unformat (a, "chroot prefix %s", &chroot_prefix))
433         {
434           vl_set_memory_root_path ((char *) chroot_prefix);
435         }
436       else
437         {
438           fformat (stderr, "%s: usage [master|slave]\n");
439           exit (1);
440         }
441     }
442
443   uri_api_hookup (utm);
444
445   if (connect_to_vpp (i_am_master ? "uritest_master" : "uritest_slave") < 0)
446     {
447       svm_region_exit ();
448       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
449       exit (1);
450     }
451
452   utm->i_am_master = i_am_master;
453
454   if (i_am_master)
455     uritest_master (utm);
456   else
457     uritest_slave (utm);
458
459   vl_client_disconnect_from_vlib ();
460   exit (0);
461 }
462
463 #undef vl_api_version
464 #define vl_api_version(n,v) static u32 vpe_api_version = v;
465 #include <vpp-api/vpe.api.h>
466 #undef vl_api_version
467
468 void
469 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
470 {
471   /*
472    * Send the main API signature in slot 0. This bit of code must
473    * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
474    */
475   mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
476 }
477
478 /*
479  * fd.io coding-style-patch-verification: ON
480  *
481  * Local Variables:
482  * eval: (c-set-style "gnu")
483  * End:
484  */