VPP-598: tcp stack initial commit
[vpp.git] / src / uri / uri_udp_test.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 <signal.h>
19 #include <vppinfra/clib.h>
20 #include <vppinfra/format.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/time.h>
23 #include <vppinfra/macros.h>
24 #include <vnet/vnet.h>
25 #include <vlib/vlib.h>
26 #include <vlib/unix/unix.h>
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
29 #include <vpp-api/vpe_msg_enum.h>
30 #include <svm_fifo_segment.h>
31
32 #include <vnet/uri/uri.h>
33
34 #define vl_typedefs             /* define message structures */
35 #include <vpp-api/vpe_all_api_h.h>
36 #undef vl_typedefs
37
38 /* declare message handlers for each api */
39
40 #define vl_endianfun            /* define message structures */
41 #include <vpp-api/vpe_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
46 #define vl_printfun
47 #include <vpp-api/vpe_all_api_h.h>
48 #undef vl_printfun
49
50 /* Satisfy external references when not linking with -lvlib */
51 vlib_main_t vlib_global_main;
52 vlib_main_t **vlib_mains;
53
54 typedef enum
55 {
56   STATE_START,
57   STATE_READY,
58   STATE_DISCONNECTING,
59 } connection_state_t;
60
61 typedef struct
62 {
63   svm_fifo_t *server_rx_fifo;
64   svm_fifo_t *server_tx_fifo;
65 } session_t;
66
67 typedef struct
68 {
69   /* vpe input queue */
70   unix_shared_memory_queue_t *vl_input_queue;
71
72   /* API client handle */
73   u32 my_client_index;
74
75   /* The URI we're playing with */
76   u8 *uri;
77
78   /* Session pool */
79   session_t *sessions;
80
81   /* Hash table for disconnect processing */
82   uword *session_index_by_vpp_handles;
83
84   /* fifo segment */
85   svm_fifo_segment_private_t *seg;
86
87   /* intermediate rx buffer */
88   u8 *rx_buf;
89
90   /* Our event queue */
91   unix_shared_memory_queue_t *our_event_queue;
92
93   /* $$$ single thread only for the moment */
94   unix_shared_memory_queue_t *vpp_event_queue;
95
96   /* For deadman timers */
97   clib_time_t clib_time;
98
99   /* State of the connection, shared between msg RX thread and main thread */
100   volatile connection_state_t state;
101
102   volatile int time_to_stop;
103   volatile int time_to_print_stats;
104
105   /* VNET_API_ERROR_FOO -> "Foo" hash table */
106   uword *error_string_by_error_number;
107 } uri_udp_test_main_t;
108
109 #if CLIB_DEBUG > 0
110 #define NITER 1000
111 #else
112 #define NITER 1000000
113 #endif
114
115 uri_udp_test_main_t uri_udp_test_main;
116
117 static void
118 stop_signal (int signum)
119 {
120   uri_udp_test_main_t *um = &uri_udp_test_main;
121
122   um->time_to_stop = 1;
123 }
124
125 static void
126 stats_signal (int signum)
127 {
128   uri_udp_test_main_t *um = &uri_udp_test_main;
129
130   um->time_to_print_stats = 1;
131 }
132
133 static clib_error_t *
134 setup_signal_handlers (void)
135 {
136   signal (SIGINT, stats_signal);
137   signal (SIGQUIT, stop_signal);
138   signal (SIGTERM, stop_signal);
139
140   return 0;
141 }
142
143 u8 *
144 format_api_error (u8 * s, va_list * args)
145 {
146   uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
147   i32 error = va_arg (*args, u32);
148   uword *p;
149
150   p = hash_get (utm->error_string_by_error_number, -error);
151
152   if (p)
153     s = format (s, "%s", p[0]);
154   else
155     s = format (s, "%d", error);
156   return s;
157 }
158
159 int
160 wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
161 {
162   f64 timeout = clib_time_now (&utm->clib_time) + 5.0;
163
164   while (clib_time_now (&utm->clib_time) < timeout)
165     {
166       if (utm->state == state)
167         return 0;
168     }
169   return -1;
170 }
171
172 static void
173 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
174 {
175   uri_udp_test_main_t *utm = &uri_udp_test_main;
176   svm_fifo_segment_create_args_t _a, *a = &_a;
177   int rv;
178
179   if (mp->segment_name_length == 0)
180     {
181       clib_warning ("segment_name_length zero");
182       return;
183     }
184
185   a->segment_name = (char *) mp->segment_name;
186
187   /* Attach to the segment vpp created */
188   rv = svm_fifo_segment_attach (a);
189   if (rv)
190     {
191       clib_warning ("sm_fifo_segment_create ('%s') failed", mp->segment_name);
192       return;
193     }
194
195   utm->our_event_queue = (unix_shared_memory_queue_t *)
196     mp->server_event_queue_address;
197
198   utm->state = STATE_READY;
199 }
200
201 static void
202 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
203 {
204   uri_udp_test_main_t *utm = &uri_udp_test_main;
205
206   if (mp->retval != 0)
207     clib_warning ("returned %d", ntohl (mp->retval));
208
209   utm->state = STATE_START;
210 }
211
212 static void
213 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
214 {
215   uri_udp_test_main_t *utm = &uri_udp_test_main;
216   vl_api_accept_session_reply_t *rmp;
217   svm_fifo_t *rx_fifo, *tx_fifo;
218   session_t *session;
219   static f64 start_time;
220   u64 key;
221
222   if (start_time == 0.0)
223     start_time = clib_time_now (&utm->clib_time);
224
225   utm->vpp_event_queue = (unix_shared_memory_queue_t *)
226     mp->vpp_event_queue_address;
227
228   pool_get (utm->sessions, session);
229
230   rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
231   rx_fifo->client_session_index = session - utm->sessions;
232   tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
233   tx_fifo->client_session_index = session - utm->sessions;
234
235   session->server_rx_fifo = rx_fifo;
236   session->server_tx_fifo = tx_fifo;
237
238   key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
239
240   hash_set (utm->session_index_by_vpp_handles, key, session - utm->sessions);
241
242   utm->state = STATE_READY;
243
244   if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
245     {
246       f64 now = clib_time_now (&utm->clib_time);
247       fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
248                pool_elts (utm->sessions), now - start_time,
249                (f64) pool_elts (utm->sessions) / (now - start_time));
250     }
251
252   rmp = vl_msg_api_alloc (sizeof (*rmp));
253   memset (rmp, 0, sizeof (*rmp));
254   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
255   rmp->session_type = mp->session_type;
256   rmp->session_index = mp->session_index;
257   rmp->session_thread_index = mp->session_thread_index;
258   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
259 }
260
261 static void
262 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
263 {
264   uri_udp_test_main_t *utm = &uri_udp_test_main;
265   session_t *session;
266   vl_api_disconnect_session_reply_t *rmp;
267   uword *p;
268   int rv = 0;
269   u64 key;
270
271   key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
272
273   p = hash_get (utm->session_index_by_vpp_handles, key);
274
275   if (p)
276     {
277       session = pool_elt_at_index (utm->sessions, p[0]);
278       hash_unset (utm->session_index_by_vpp_handles, key);
279       pool_put (utm->sessions, session);
280     }
281   else
282     {
283       clib_warning ("couldn't find session key %llx", key);
284       rv = -11;
285     }
286
287   rmp = vl_msg_api_alloc (sizeof (*rmp));
288   memset (rmp, 0, sizeof (*rmp));
289   rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
290   rmp->retval = rv;
291   rmp->session_index = mp->session_index;
292   rmp->session_thread_index = mp->session_thread_index;
293   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
294 }
295
296 #define foreach_uri_msg                         \
297 _(BIND_URI_REPLY, bind_uri_reply)               \
298 _(UNBIND_URI_REPLY, unbind_uri_reply)           \
299 _(ACCEPT_SESSION, accept_session)               \
300 _(DISCONNECT_SESSION, disconnect_session)
301
302 void
303 uri_api_hookup (uri_udp_test_main_t * utm)
304 {
305 #define _(N,n)                                                  \
306     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
307                            vl_api_##n##_t_handler,              \
308                            vl_noop_handler,                     \
309                            vl_api_##n##_t_endian,               \
310                            vl_api_##n##_t_print,                \
311                            sizeof(vl_api_##n##_t), 1);
312   foreach_uri_msg;
313 #undef _
314
315 }
316
317
318 int
319 connect_to_vpp (char *name)
320 {
321   uri_udp_test_main_t *utm = &uri_udp_test_main;
322   api_main_t *am = &api_main;
323
324   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
325     return -1;
326
327   utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
328   utm->my_client_index = am->my_client_index;
329
330   return 0;
331 }
332
333 void
334 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
335 {
336   clib_warning ("BUG");
337 }
338
339 static void
340 init_error_string_table (uri_udp_test_main_t * utm)
341 {
342   utm->error_string_by_error_number = hash_create (0, sizeof (uword));
343
344 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
345   foreach_vnet_api_error;
346 #undef _
347
348   hash_set (utm->error_string_by_error_number, 99, "Misc");
349 }
350
351 void
352 handle_fifo_event_server_rx (uri_udp_test_main_t * utm,
353                              session_fifo_event_t * e)
354 {
355   svm_fifo_t *rx_fifo, *tx_fifo;
356   int nbytes;
357
358   session_fifo_event_t evt;
359   unix_shared_memory_queue_t *q;
360   int rv;
361
362   rx_fifo = e->fifo;
363   tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
364
365   do
366     {
367       nbytes = svm_fifo_dequeue_nowait (rx_fifo, 0,
368                                         vec_len (utm->rx_buf), utm->rx_buf);
369     }
370   while (nbytes <= 0);
371   do
372     {
373       rv = svm_fifo_enqueue_nowait (tx_fifo, 0, nbytes, utm->rx_buf);
374     }
375   while (rv == -2);
376
377   /* Fabricate TX event, send to vpp */
378   evt.fifo = tx_fifo;
379   evt.event_type = FIFO_EVENT_SERVER_TX;
380   /* $$$$ for event logging */
381   evt.enqueue_length = nbytes;
382   evt.event_id = e->event_id;
383   q = utm->vpp_event_queue;
384   unix_shared_memory_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
385 }
386
387 void
388 handle_event_queue (uri_udp_test_main_t * utm)
389 {
390   session_fifo_event_t _e, *e = &_e;;
391
392   while (1)
393     {
394       unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
395                                     0 /* nowait */ );
396       switch (e->event_type)
397         {
398         case FIFO_EVENT_SERVER_RX:
399           handle_fifo_event_server_rx (utm, e);
400           break;
401
402         case FIFO_EVENT_SERVER_EXIT:
403           return;
404
405         default:
406           clib_warning ("unknown event type %d", e->event_type);
407           break;
408         }
409       if (PREDICT_FALSE (utm->time_to_stop == 1))
410         break;
411       if (PREDICT_FALSE (utm->time_to_print_stats == 1))
412         {
413           utm->time_to_print_stats = 0;
414           fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
415         }
416     }
417 }
418
419 void
420 uri_udp_test (uri_udp_test_main_t * utm)
421 {
422   vl_api_bind_uri_t *bmp;
423   vl_api_unbind_uri_t *ump;
424
425   bmp = vl_msg_api_alloc (sizeof (*bmp));
426   memset (bmp, 0, sizeof (*bmp));
427
428   bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
429   bmp->client_index = utm->my_client_index;
430   bmp->context = ntohl (0xfeedface);
431   bmp->segment_size = 2 << 30;
432   memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
433   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
434
435   if (wait_for_state_change (utm, STATE_READY))
436     {
437       clib_warning ("timeout waiting for STATE_READY");
438       return;
439     }
440
441   handle_event_queue (utm);
442
443   ump = vl_msg_api_alloc (sizeof (*ump));
444   memset (ump, 0, sizeof (*ump));
445
446   ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
447   ump->client_index = utm->my_client_index;
448   memcpy (ump->uri, utm->uri, vec_len (utm->uri));
449   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
450
451   if (wait_for_state_change (utm, STATE_START))
452     {
453       clib_warning ("timeout waiting for STATE_START");
454       return;
455     }
456
457   fformat (stdout, "Test complete...\n");
458 }
459
460 int
461 main (int argc, char **argv)
462 {
463   uri_udp_test_main_t *utm = &uri_udp_test_main;
464   unformat_input_t _argv, *a = &_argv;
465   u8 *chroot_prefix;
466   u8 *heap;
467   u8 *bind_name = (u8 *) "udp4:1234";
468   mheap_t *h;
469   session_t *session;
470   int i;
471
472   clib_mem_init (0, 256 << 20);
473
474   heap = clib_mem_get_per_cpu_heap ();
475   h = mheap_header (heap);
476
477   /* make the main heap thread-safe */
478   h->flags |= MHEAP_FLAG_THREAD_SAFE;
479
480   vec_validate (utm->rx_buf, 8192);
481
482   utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
483
484   clib_time_init (&utm->clib_time);
485   init_error_string_table (utm);
486   svm_fifo_segment_init (0x200000000ULL, 20);
487   unformat_init_command_line (a, argv);
488
489   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
490     {
491       if (unformat (a, "chroot prefix %s", &chroot_prefix))
492         {
493           vl_set_memory_root_path ((char *) chroot_prefix);
494         }
495       else if (unformat (a, "uri %s", &bind_name))
496         ;
497       else
498         {
499           fformat (stderr, "%s: usage [master|slave]\n");
500           exit (1);
501         }
502     }
503
504   utm->uri = format (0, "%s%c", bind_name, 0);
505
506   setup_signal_handlers ();
507
508   uri_api_hookup (utm);
509
510   if (connect_to_vpp ("uri_udp_test") < 0)
511     {
512       svm_region_exit ();
513       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
514       exit (1);
515     }
516
517   /* $$$$ hack preallocation */
518   for (i = 0; i < 200000; i++)
519     {
520       pool_get (utm->sessions, session);
521       memset (session, 0, sizeof (*session));
522     }
523   for (i = 0; i < 200000; i++)
524     pool_put_index (utm->sessions, i);
525
526   uri_udp_test (utm);
527
528   vl_client_disconnect_from_vlib ();
529   exit (0);
530 }
531
532 #undef vl_api_version
533 #define vl_api_version(n,v) static u32 vpe_api_version = v;
534 #include <vpp-api/vpe.api.h>
535 #undef vl_api_version
536
537 void
538 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
539 {
540   /*
541    * Send the main API signature in slot 0. This bit of code must
542    * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
543    */
544   mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
545 }
546
547 /*
548  * fd.io coding-style-patch-verification: ON
549  *
550  * Local Variables:
551  * eval: (c-set-style "gnu")
552  * End:
553  */