sock api: add infra for bootstrapping shm clients
[vpp.git] / src / tests / vnet / session / tcp_echo.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 <signal.h>
18
19 #include <vnet/session/application_interface.h>
20 #include <svm/svm_fifo_segment.h>
21 #include <vlibmemory/api.h>
22
23 #include <vpp/api/vpe_msg_enum.h>
24
25 #define vl_typedefs             /* define message structures */
26 #include <vpp/api/vpe_all_api_h.h>
27 #undef vl_typedefs
28
29 /* declare message handlers for each api */
30
31 #define vl_endianfun            /* define message structures */
32 #include <vpp/api/vpe_all_api_h.h>
33 #undef vl_endianfun
34
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...)
37 #define vl_printfun
38 #include <vpp/api/vpe_all_api_h.h>
39 #undef vl_printfun
40
41 typedef struct
42 {
43   svm_fifo_t *server_rx_fifo;
44   svm_fifo_t *server_tx_fifo;
45
46   u64 vpp_session_handle;
47   u64 bytes_received;
48   f64 start;
49 } session_t;
50
51 typedef enum
52 {
53   STATE_START,
54   STATE_ATTACHED,
55   STATE_READY,
56   STATE_DISCONNECTING,
57   STATE_FAILED
58 } connection_state_t;
59
60 typedef struct
61 {
62   /* vpe input queue */
63   unix_shared_memory_queue_t *vl_input_queue;
64
65   /* API client handle */
66   u32 my_client_index;
67
68   /* The URI we're playing with */
69   u8 *uri;
70
71   /* Session pool */
72   session_t *sessions;
73
74   /* Hash table for disconnect processing */
75   uword *session_index_by_vpp_handles;
76
77   /* intermediate rx buffer */
78   u8 *rx_buf;
79
80   /* URI for slave's connect */
81   u8 *connect_uri;
82
83   u32 connected_session_index;
84
85   int i_am_master;
86
87   /* drop all packets */
88   int drop_packets;
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   u8 *socket_name;
97
98   pid_t my_pid;
99
100   /* For deadman timers */
101   clib_time_t clib_time;
102
103   /* State of the connection, shared between msg RX thread and main thread */
104   volatile connection_state_t state;
105
106   /* Signal variables */
107   volatile int time_to_stop;
108   volatile int time_to_print_stats;
109
110   u32 configured_segment_size;
111
112   /* VNET_API_ERROR_FOO -> "Foo" hash table */
113   uword *error_string_by_error_number;
114
115   u8 *connect_test_data;
116   pthread_t client_rx_thread_handle;
117   u32 client_bytes_received;
118   u8 test_return_packets;
119   u64 bytes_to_send;
120
121   /** Flag that decides if socket, instead of svm, api is used to connect to
122    * vpp. If sock api is used, shm binary api is subsequently bootstrapped
123    * and all other messages are exchanged using shm IPC. */
124   u8 use_sock_api;
125
126   /* convenience */
127   svm_fifo_segment_main_t *segment_main;
128 } uri_tcp_test_main_t;
129
130 uri_tcp_test_main_t uri_tcp_test_main;
131
132 #if CLIB_DEBUG > 0
133 #define NITER 10000
134 #else
135 #define NITER 4000000
136 #endif
137
138 static u8 *
139 format_api_error (u8 * s, va_list * args)
140 {
141   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
142   i32 error = va_arg (*args, u32);
143   uword *p;
144
145   p = hash_get (utm->error_string_by_error_number, -error);
146
147   if (p)
148     s = format (s, "%s", p[0]);
149   else
150     s = format (s, "%d", error);
151   return s;
152 }
153
154 static void
155 init_error_string_table (uri_tcp_test_main_t * utm)
156 {
157   utm->error_string_by_error_number = hash_create (0, sizeof (uword));
158
159 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
160   foreach_vnet_api_error;
161 #undef _
162
163   hash_set (utm->error_string_by_error_number, 99, "Misc");
164 }
165
166 int
167 wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
168 {
169 #if CLIB_DEBUG > 0
170 #define TIMEOUT 600.0
171 #else
172 #define TIMEOUT 600.0
173 #endif
174
175   f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
176
177   while (clib_time_now (&utm->clib_time) < timeout)
178     {
179       if (utm->state == state)
180         return 0;
181       if (utm->state == STATE_FAILED)
182         return -1;
183       if (utm->time_to_stop == 1)
184         return 0;
185     }
186   clib_warning ("timeout waiting for STATE_READY");
187   return -1;
188 }
189
190 void
191 application_send_attach (uri_tcp_test_main_t * utm)
192 {
193   vl_api_application_attach_t *bmp;
194   u32 fifo_size = 4 << 20;
195   bmp = vl_msg_api_alloc (sizeof (*bmp));
196   memset (bmp, 0, sizeof (*bmp));
197
198   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
199   bmp->client_index = utm->my_client_index;
200   bmp->context = ntohl (0xfeedface);
201   bmp->options[APP_OPTIONS_FLAGS] =
202     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
203   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
204   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
205   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
206   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
207   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
208   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
209 }
210
211 int
212 application_attach (uri_tcp_test_main_t * utm)
213 {
214   application_send_attach (utm);
215   if (wait_for_state_change (utm, STATE_ATTACHED))
216     {
217       clib_warning ("timeout waiting for STATE_ATTACHED");
218       return -1;
219     }
220   return 0;
221 }
222
223 void
224 application_detach (uri_tcp_test_main_t * utm)
225 {
226   vl_api_application_detach_t *bmp;
227   bmp = vl_msg_api_alloc (sizeof (*bmp));
228   memset (bmp, 0, sizeof (*bmp));
229
230   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
231   bmp->client_index = utm->my_client_index;
232   bmp->context = ntohl (0xfeedface);
233   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
234 }
235
236 static void
237 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
238                                            mp)
239 {
240   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
241   svm_fifo_segment_create_args_t _a, *a = &_a;
242   int rv;
243
244   if (mp->retval)
245     {
246       clib_warning ("attach failed: %U", format_api_error,
247                     clib_net_to_host_u32 (mp->retval));
248       utm->state = STATE_FAILED;
249       return;
250     }
251
252   if (mp->segment_name_length == 0)
253     {
254       clib_warning ("segment_name_length zero");
255       return;
256     }
257
258   memset (a, 0, sizeof (*a));
259   a->segment_name = (char *) mp->segment_name;
260   a->segment_size = mp->segment_size;
261
262   ASSERT (mp->app_event_queue_address);
263
264   /* Attach to the segment vpp created */
265   rv = svm_fifo_segment_attach (a);
266   if (rv)
267     {
268       clib_warning ("svm_fifo_segment_attach ('%s') failed",
269                     mp->segment_name);
270       return;
271     }
272
273   utm->our_event_queue =
274     uword_to_pointer (mp->app_event_queue_address,
275                       unix_shared_memory_queue_t *);
276   utm->state = STATE_ATTACHED;
277 }
278
279 static void
280 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
281                                            mp)
282 {
283   if (mp->retval)
284     clib_warning ("detach returned with err: %d", mp->retval);
285 }
286
287 static void
288 stop_signal (int signum)
289 {
290   uri_tcp_test_main_t *um = &uri_tcp_test_main;
291
292   um->time_to_stop = 1;
293 }
294
295 static void
296 stats_signal (int signum)
297 {
298   uri_tcp_test_main_t *um = &uri_tcp_test_main;
299
300   um->time_to_print_stats = 1;
301 }
302
303 static clib_error_t *
304 setup_signal_handlers (void)
305 {
306   signal (SIGINT, stats_signal);
307   signal (SIGQUIT, stop_signal);
308   signal (SIGTERM, stop_signal);
309
310   return 0;
311 }
312
313 void
314 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
315 {
316   clib_warning ("BUG");
317 }
318
319 int
320 connect_to_vpp (char *name)
321 {
322   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
323   api_main_t *am = &api_main;
324
325   if (utm->use_sock_api)
326     {
327       if (vl_socket_client_connect ((char *) utm->socket_name, name,
328                                     0 /* default rx, tx buffer */ ))
329         return -1;
330
331       return vl_socket_client_init_shm (0);
332     }
333   else
334     {
335       if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
336         return -1;
337       utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
338       utm->my_client_index = am->my_client_index;
339     }
340   return 0;
341 }
342
343 void
344 disconnect_from_vpp (uri_tcp_test_main_t * utm)
345 {
346   if (utm->use_sock_api)
347     vl_socket_client_disconnect ();
348   else
349     vl_client_disconnect_from_vlib ();
350 }
351
352 static void
353 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
354 {
355   svm_fifo_segment_create_args_t _a, *a = &_a;
356   int rv;
357
358   memset (a, 0, sizeof (*a));
359   a->segment_name = (char *) mp->segment_name;
360   a->segment_size = mp->segment_size;
361   /* Attach to the segment vpp created */
362   rv = svm_fifo_segment_attach (a);
363   if (rv)
364     {
365       clib_warning ("svm_fifo_segment_attach ('%s') failed",
366                     mp->segment_name);
367       return;
368     }
369   clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
370                 mp->segment_size);
371 }
372
373 static void
374 session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
375 {
376   f64 deltat;
377   u64 bytes;
378
379   deltat = clib_time_now (&utm->clib_time) - session->start;
380   bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
381   fformat (stdout, "Finished in %.6f\n", deltat);
382   fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
383 }
384
385 static void
386 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
387 {
388   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
389   session_t *session = 0;
390   vl_api_disconnect_session_reply_t *rmp;
391   uword *p;
392   int rv = 0;
393
394   p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
395
396   if (p)
397     {
398       session = pool_elt_at_index (utm->sessions, p[0]);
399       hash_unset (utm->session_index_by_vpp_handles, mp->handle);
400       pool_put (utm->sessions, session);
401     }
402   else
403     {
404       clib_warning ("couldn't find session key %llx", mp->handle);
405       rv = -11;
406     }
407
408 //  utm->time_to_stop = 1;
409
410   rmp = vl_msg_api_alloc (sizeof (*rmp));
411   memset (rmp, 0, sizeof (*rmp));
412
413   rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
414   rmp->retval = rv;
415   rmp->handle = mp->handle;
416   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
417
418   if (session)
419     session_print_stats (utm, session);
420 }
421
422 static void
423 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
424 {
425   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
426   vl_api_reset_session_reply_t *rmp;
427   uword *p;
428   int rv = 0;
429
430   p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
431
432   if (p)
433     {
434       clib_warning ("got reset");
435       /* Cleanup later */
436       utm->time_to_stop = 1;
437     }
438   else
439     {
440       clib_warning ("couldn't find session key %llx", mp->handle);
441       rv = -11;
442     }
443
444   rmp = vl_msg_api_alloc (sizeof (*rmp));
445   memset (rmp, 0, sizeof (*rmp));
446   rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
447   rmp->retval = rv;
448   rmp->handle = mp->handle;
449   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
450 }
451
452 void
453 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
454                              session_fifo_event_t * e)
455 {
456   svm_fifo_t *rx_fifo;
457   int n_read, bytes, i;
458
459   rx_fifo = e->fifo;
460
461   bytes = svm_fifo_max_dequeue (rx_fifo);
462   /* Allow enqueuing of new event */
463   svm_fifo_unset_event (rx_fifo);
464
465   /* Read the bytes */
466   do
467     {
468       n_read = svm_fifo_dequeue_nowait (rx_fifo,
469                                         clib_min (vec_len (utm->rx_buf),
470                                                   bytes), utm->rx_buf);
471       if (n_read > 0)
472         {
473           bytes -= n_read;
474           if (utm->test_return_packets)
475             {
476               for (i = 0; i < n_read; i++)
477                 {
478                   if (utm->rx_buf[i]
479                       != ((utm->client_bytes_received + i) & 0xff))
480                     {
481                       clib_warning ("error at byte %lld, 0x%x not 0x%x",
482                                     utm->client_bytes_received + i,
483                                     utm->rx_buf[i],
484                                     ((utm->client_bytes_received +
485                                       i) & 0xff));
486                     }
487                 }
488             }
489           utm->client_bytes_received += n_read;
490         }
491       else
492         {
493           if (n_read == -2)
494             {
495 //            clib_warning ("weird!");
496               break;
497             }
498         }
499
500     }
501   while (bytes > 0);
502 }
503
504 void
505 client_handle_event_queue (uri_tcp_test_main_t * utm)
506 {
507   session_fifo_event_t _e, *e = &_e;;
508
509   unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
510                                 0 /* nowait */ );
511   switch (e->event_type)
512     {
513     case FIFO_EVENT_APP_RX:
514       client_handle_fifo_event_rx (utm, e);
515       break;
516
517     case FIFO_EVENT_DISCONNECT:
518       return;
519
520     default:
521       clib_warning ("unknown event type %d", e->event_type);
522       break;
523     }
524 }
525
526 static void *
527 client_rx_thread_fn (void *arg)
528 {
529   session_fifo_event_t _e, *e = &_e;
530   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
531
532   utm->client_bytes_received = 0;
533   while (1)
534     {
535       unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
536                                     0 /* nowait */ );
537       switch (e->event_type)
538         {
539         case FIFO_EVENT_APP_RX:
540           client_handle_fifo_event_rx (utm, e);
541           break;
542
543         case FIFO_EVENT_DISCONNECT:
544           return 0;
545         default:
546           clib_warning ("unknown event type %d", e->event_type);
547           break;
548         }
549
550       if (PREDICT_FALSE (utm->time_to_stop == 1))
551         break;
552     }
553   pthread_exit (0);
554 }
555
556
557 static void
558 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
559 {
560   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
561   session_t *session;
562   u32 session_index;
563   svm_fifo_t *rx_fifo, *tx_fifo;
564   int rv;
565
566   if (mp->retval)
567     {
568       clib_warning ("connection failed with code: %U", format_api_error,
569                     clib_net_to_host_u32 (mp->retval));
570       utm->state = STATE_FAILED;
571       return;
572     }
573   else
574     {
575       clib_warning ("connected with local ip %U port %d", format_ip46_address,
576                     mp->lcl_ip, mp->is_ip4,
577                     clib_net_to_host_u16 (mp->lcl_port));
578     }
579
580   utm->vpp_event_queue =
581     uword_to_pointer (mp->vpp_event_queue_address,
582                       unix_shared_memory_queue_t *);
583
584   /*
585    * Setup session
586    */
587
588   pool_get (utm->sessions, session);
589   session_index = session - utm->sessions;
590
591   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
592   rx_fifo->client_session_index = session_index;
593   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
594   tx_fifo->client_session_index = session_index;
595
596   session->server_rx_fifo = rx_fifo;
597   session->server_tx_fifo = tx_fifo;
598   session->vpp_session_handle = mp->handle;
599   session->start = clib_time_now (&utm->clib_time);
600
601   /* Save handle */
602   utm->connected_session_index = session_index;
603   utm->state = STATE_READY;
604
605   /* Add it to lookup table */
606   hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
607
608   /* Start RX thread */
609   rv = pthread_create (&utm->client_rx_thread_handle,
610                        NULL /*attr */ , client_rx_thread_fn, 0);
611   if (rv)
612     {
613       clib_warning ("pthread_create returned %d", rv);
614       rv = VNET_API_ERROR_SYSCALL_ERROR_1;
615     }
616 }
617
618 static void
619 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
620                  u32 bytes)
621 {
622   u8 *test_data = utm->connect_test_data;
623   u64 bytes_sent = 0;
624   int test_buf_offset = 0;
625   u32 bytes_to_snd;
626   u32 queue_max_chunk = 128 << 10, actual_write;
627   session_fifo_event_t evt;
628   int rv;
629
630   bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
631   if (bytes_to_snd > vec_len (test_data))
632     bytes_to_snd = vec_len (test_data);
633
634   while (bytes_to_snd > 0 && !utm->time_to_stop)
635     {
636       actual_write = (bytes_to_snd > queue_max_chunk) ?
637         queue_max_chunk : bytes_to_snd;
638       rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
639                                     test_data + test_buf_offset);
640
641       if (rv > 0)
642         {
643           bytes_to_snd -= rv;
644           test_buf_offset += rv;
645           bytes_sent += rv;
646
647           if (svm_fifo_set_event (tx_fifo))
648             {
649               /* Fabricate TX event, send to vpp */
650               evt.fifo = tx_fifo;
651               evt.event_type = FIFO_EVENT_APP_TX;
652
653               unix_shared_memory_queue_add (utm->vpp_event_queue,
654                                             (u8 *) & evt,
655                                             0 /* do wait for mutex */ );
656             }
657         }
658     }
659 }
660
661 void
662 client_send_data (uri_tcp_test_main_t * utm)
663 {
664   u8 *test_data = utm->connect_test_data;
665   int mypid = getpid ();
666   session_t *session;
667   svm_fifo_t *tx_fifo;
668   u32 n_iterations, leftover;
669   int i;
670
671   session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
672   tx_fifo = session->server_tx_fifo;
673
674   ASSERT (vec_len (test_data) > 0);
675
676   vec_validate (utm->rx_buf, vec_len (test_data) - 1);
677   n_iterations = utm->bytes_to_send / vec_len (test_data);
678
679   for (i = 0; i < n_iterations; i++)
680     {
681       send_test_chunk (utm, tx_fifo, mypid, 0);
682       if (utm->time_to_stop)
683         break;
684     }
685
686   leftover = utm->bytes_to_send % vec_len (test_data);
687   if (leftover)
688     send_test_chunk (utm, tx_fifo, mypid, leftover);
689
690   if (!utm->drop_packets)
691     {
692       f64 timeout = clib_time_now (&utm->clib_time) + 10;
693
694       /* Wait for the outstanding packets */
695       while (utm->client_bytes_received <
696              vec_len (test_data) * n_iterations + leftover)
697         {
698           if (clib_time_now (&utm->clib_time) > timeout)
699             {
700               clib_warning ("timed out waiting for the missing packets");
701               break;
702             }
703         }
704     }
705   utm->time_to_stop = 1;
706 }
707
708 void
709 client_send_connect (uri_tcp_test_main_t * utm)
710 {
711   vl_api_connect_uri_t *cmp;
712   cmp = vl_msg_api_alloc (sizeof (*cmp));
713   memset (cmp, 0, sizeof (*cmp));
714
715   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
716   cmp->client_index = utm->my_client_index;
717   cmp->context = ntohl (0xfeedface);
718   memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
719   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
720 }
721
722 int
723 client_connect (uri_tcp_test_main_t * utm)
724 {
725   client_send_connect (utm);
726   if (wait_for_state_change (utm, STATE_READY))
727     {
728       clib_warning ("Connect failed");
729       return -1;
730     }
731   return 0;
732 }
733
734 void
735 client_send_disconnect (uri_tcp_test_main_t * utm)
736 {
737   session_t *connected_session;
738   vl_api_disconnect_session_t *dmp;
739   connected_session = pool_elt_at_index (utm->sessions,
740                                          utm->connected_session_index);
741   dmp = vl_msg_api_alloc (sizeof (*dmp));
742   memset (dmp, 0, sizeof (*dmp));
743   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
744   dmp->client_index = utm->my_client_index;
745   dmp->handle = connected_session->vpp_session_handle;
746   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
747 }
748
749 int
750 client_disconnect (uri_tcp_test_main_t * utm)
751 {
752   client_send_disconnect (utm);
753   clib_warning ("Sent disconnect");
754   if (wait_for_state_change (utm, STATE_START))
755     {
756       clib_warning ("Disconnect failed");
757       return -1;
758     }
759   return 0;
760 }
761
762 static void
763 client_run (uri_tcp_test_main_t * utm)
764 {
765   int i;
766
767   if (application_attach (utm))
768     return;
769
770   if (client_connect (utm))
771     {
772       application_detach (utm);
773       return;
774     }
775
776   /* Init test data */
777   vec_validate (utm->connect_test_data, 128 * 1024 - 1);
778   for (i = 0; i < vec_len (utm->connect_test_data); i++)
779     utm->connect_test_data[i] = i & 0xff;
780
781   /* Start send */
782   client_send_data (utm);
783
784   /* Disconnect */
785   client_disconnect (utm);
786
787   application_detach (utm);
788 }
789
790 static void
791 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
792 {
793   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
794
795   if (mp->retval)
796     {
797       clib_warning ("bind failed: %U", format_api_error,
798                     clib_net_to_host_u32 (mp->retval));
799       utm->state = STATE_FAILED;
800       return;
801     }
802
803   utm->state = STATE_READY;
804 }
805
806 static void
807 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
808 {
809   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
810
811   if (mp->retval != 0)
812     clib_warning ("returned %d", ntohl (mp->retval));
813
814   utm->state = STATE_START;
815 }
816
817 u8 *
818 format_ip4_address (u8 * s, va_list * args)
819 {
820   u8 *a = va_arg (*args, u8 *);
821   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
822 }
823
824 u8 *
825 format_ip6_address (u8 * s, va_list * args)
826 {
827   ip6_address_t *a = va_arg (*args, ip6_address_t *);
828   u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
829
830   i_max_n_zero = ARRAY_LEN (a->as_u16);
831   max_n_zeros = 0;
832   i_first_zero = i_max_n_zero;
833   n_zeros = 0;
834   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
835     {
836       u32 is_zero = a->as_u16[i] == 0;
837       if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
838         {
839           i_first_zero = i;
840           n_zeros = 0;
841         }
842       n_zeros += is_zero;
843       if ((!is_zero && n_zeros > max_n_zeros)
844           || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
845         {
846           i_max_n_zero = i_first_zero;
847           max_n_zeros = n_zeros;
848           i_first_zero = ARRAY_LEN (a->as_u16);
849           n_zeros = 0;
850         }
851     }
852
853   last_double_colon = 0;
854   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
855     {
856       if (i == i_max_n_zero && max_n_zeros > 1)
857         {
858           s = format (s, "::");
859           i += max_n_zeros - 1;
860           last_double_colon = 1;
861         }
862       else
863         {
864           s = format (s, "%s%x",
865                       (last_double_colon || i == 0) ? "" : ":",
866                       clib_net_to_host_u16 (a->as_u16[i]));
867           last_double_colon = 0;
868         }
869     }
870
871   return s;
872 }
873
874 /* Format an IP46 address. */
875 u8 *
876 format_ip46_address (u8 * s, va_list * args)
877 {
878   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
879   ip46_type_t type = va_arg (*args, ip46_type_t);
880   int is_ip4 = 1;
881
882   switch (type)
883     {
884     case IP46_TYPE_ANY:
885       is_ip4 = ip46_address_is_ip4 (ip46);
886       break;
887     case IP46_TYPE_IP4:
888       is_ip4 = 1;
889       break;
890     case IP46_TYPE_IP6:
891       is_ip4 = 0;
892       break;
893     }
894
895   return is_ip4 ?
896     format (s, "%U", format_ip4_address, &ip46->ip4) :
897     format (s, "%U", format_ip6_address, &ip46->ip6);
898 }
899
900 static void
901 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
902 {
903   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
904   vl_api_accept_session_reply_t *rmp;
905   svm_fifo_t *rx_fifo, *tx_fifo;
906   session_t *session;
907   static f64 start_time;
908   u32 session_index;
909   u8 *ip_str;
910
911   if (start_time == 0.0)
912     start_time = clib_time_now (&utm->clib_time);
913
914   ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
915   clib_warning ("Accepted session from: %s:%d", ip_str,
916                 clib_net_to_host_u16 (mp->port));
917   utm->vpp_event_queue =
918     uword_to_pointer (mp->vpp_event_queue_address,
919                       unix_shared_memory_queue_t *);
920
921   /* Allocate local session and set it up */
922   pool_get (utm->sessions, session);
923   session_index = session - utm->sessions;
924
925   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
926   rx_fifo->client_session_index = session_index;
927   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
928   tx_fifo->client_session_index = session_index;
929
930   session->server_rx_fifo = rx_fifo;
931   session->server_tx_fifo = tx_fifo;
932
933   /* Add it to lookup table */
934   hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
935
936   utm->state = STATE_READY;
937
938   /* Stats printing */
939   if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
940     {
941       f64 now = clib_time_now (&utm->clib_time);
942       fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
943                pool_elts (utm->sessions), now - start_time,
944                (f64) pool_elts (utm->sessions) / (now - start_time));
945     }
946
947   /*
948    * Send accept reply to vpp
949    */
950   rmp = vl_msg_api_alloc (sizeof (*rmp));
951   memset (rmp, 0, sizeof (*rmp));
952   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
953   rmp->handle = mp->handle;
954   rmp->context = mp->context;
955   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
956
957   session->bytes_received = 0;
958   session->start = clib_time_now (&utm->clib_time);
959 }
960
961 void
962 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
963                              session_fifo_event_t * e)
964 {
965   svm_fifo_t *rx_fifo, *tx_fifo;
966   int n_read;
967   session_fifo_event_t evt;
968   unix_shared_memory_queue_t *q;
969   session_t *session;
970   int rv;
971   u32 max_dequeue, offset, max_transfer, rx_buf_len;
972
973   rx_buf_len = vec_len (utm->rx_buf);
974   rx_fifo = e->fifo;
975   session = &utm->sessions[rx_fifo->client_session_index];
976   tx_fifo = session->server_tx_fifo;
977
978   max_dequeue = svm_fifo_max_dequeue (rx_fifo);
979   /* Allow enqueuing of a new event */
980   svm_fifo_unset_event (rx_fifo);
981
982   if (PREDICT_FALSE (max_dequeue == 0))
983     {
984       return;
985     }
986
987   /* Read the max_dequeue */
988   do
989     {
990       max_transfer = clib_min (rx_buf_len, max_dequeue);
991       n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
992       if (n_read > 0)
993         {
994           max_dequeue -= n_read;
995           session->bytes_received += n_read;
996         }
997
998       /* Reflect if a non-drop session */
999       if (!utm->drop_packets && n_read > 0)
1000         {
1001           offset = 0;
1002           do
1003             {
1004               rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
1005                                             &utm->rx_buf[offset]);
1006               if (rv > 0)
1007                 {
1008                   n_read -= rv;
1009                   offset += rv;
1010                 }
1011             }
1012           while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
1013
1014           /* If event wasn't set, add one */
1015           if (svm_fifo_set_event (tx_fifo))
1016             {
1017               /* Fabricate TX event, send to vpp */
1018               evt.fifo = tx_fifo;
1019               evt.event_type = FIFO_EVENT_APP_TX;
1020
1021               q = utm->vpp_event_queue;
1022               unix_shared_memory_queue_add (q, (u8 *) & evt,
1023                                             1 /* do wait for mutex */ );
1024             }
1025         }
1026     }
1027   while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
1028 }
1029
1030 void
1031 server_handle_event_queue (uri_tcp_test_main_t * utm)
1032 {
1033   session_fifo_event_t _e, *e = &_e;
1034
1035   while (1)
1036     {
1037       unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
1038                                     0 /* nowait */ );
1039       switch (e->event_type)
1040         {
1041         case FIFO_EVENT_APP_RX:
1042           server_handle_fifo_event_rx (utm, e);
1043           break;
1044
1045         case FIFO_EVENT_DISCONNECT:
1046           return;
1047
1048         default:
1049           clib_warning ("unknown event type %d", e->event_type);
1050           break;
1051         }
1052       if (PREDICT_FALSE (utm->time_to_stop == 1))
1053         break;
1054       if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1055         {
1056           utm->time_to_print_stats = 0;
1057           fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1058         }
1059     }
1060 }
1061
1062 void
1063 server_send_listen (uri_tcp_test_main_t * utm)
1064 {
1065   vl_api_bind_uri_t *bmp;
1066   bmp = vl_msg_api_alloc (sizeof (*bmp));
1067   memset (bmp, 0, sizeof (*bmp));
1068
1069   bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1070   bmp->client_index = utm->my_client_index;
1071   bmp->context = ntohl (0xfeedface);
1072   memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1073   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1074 }
1075
1076 int
1077 server_listen (uri_tcp_test_main_t * utm)
1078 {
1079   server_send_listen (utm);
1080   if (wait_for_state_change (utm, STATE_READY))
1081     {
1082       clib_warning ("timeout waiting for STATE_READY");
1083       return -1;
1084     }
1085   return 0;
1086 }
1087
1088 void
1089 server_send_unbind (uri_tcp_test_main_t * utm)
1090 {
1091   vl_api_unbind_uri_t *ump;
1092
1093   ump = vl_msg_api_alloc (sizeof (*ump));
1094   memset (ump, 0, sizeof (*ump));
1095
1096   ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1097   ump->client_index = utm->my_client_index;
1098   memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1099   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1100 }
1101
1102 int
1103 server_unbind (uri_tcp_test_main_t * utm)
1104 {
1105   server_send_unbind (utm);
1106   if (wait_for_state_change (utm, STATE_START))
1107     {
1108       clib_warning ("timeout waiting for STATE_START");
1109       return -1;
1110     }
1111   return 0;
1112 }
1113
1114 void
1115 server_run (uri_tcp_test_main_t * utm)
1116 {
1117   session_t *session;
1118   int i;
1119
1120   /* $$$$ hack preallocation */
1121   for (i = 0; i < 200000; i++)
1122     {
1123       pool_get (utm->sessions, session);
1124       memset (session, 0, sizeof (*session));
1125     }
1126   for (i = 0; i < 200000; i++)
1127     pool_put_index (utm->sessions, i);
1128
1129   if (application_attach (utm))
1130     return;
1131
1132   /* Bind to uri */
1133   if (server_listen (utm))
1134     return;
1135
1136   /* Enter handle event loop */
1137   server_handle_event_queue (utm);
1138
1139   /* Cleanup */
1140   server_send_unbind (utm);
1141
1142   application_detach (utm);
1143
1144   fformat (stdout, "Test complete...\n");
1145 }
1146
1147 static void
1148 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1149                                            mp)
1150 {
1151   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1152   session_t *session;
1153
1154   if (mp->retval)
1155     {
1156       clib_warning ("vpp complained about disconnect: %d",
1157                     ntohl (mp->retval));
1158     }
1159
1160   utm->state = STATE_START;
1161   session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1162   if (session)
1163     session_print_stats (utm, session);
1164 }
1165
1166 #define foreach_uri_msg                                         \
1167 _(BIND_URI_REPLY, bind_uri_reply)                               \
1168 _(UNBIND_URI_REPLY, unbind_uri_reply)                           \
1169 _(ACCEPT_SESSION, accept_session)                               \
1170 _(CONNECT_SESSION_REPLY, connect_session_reply)                 \
1171 _(DISCONNECT_SESSION, disconnect_session)                       \
1172 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)           \
1173 _(RESET_SESSION, reset_session)                                 \
1174 _(APPLICATION_ATTACH_REPLY, application_attach_reply)           \
1175 _(APPLICATION_DETACH_REPLY, application_detach_reply)           \
1176 _(MAP_ANOTHER_SEGMENT, map_another_segment)                     \
1177
1178 void
1179 uri_api_hookup (uri_tcp_test_main_t * utm)
1180 {
1181 #define _(N,n)                                                  \
1182     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1183                            vl_api_##n##_t_handler,              \
1184                            vl_noop_handler,                     \
1185                            vl_api_##n##_t_endian,               \
1186                            vl_api_##n##_t_print,                \
1187                            sizeof(vl_api_##n##_t), 1);
1188   foreach_uri_msg;
1189 #undef _
1190 }
1191
1192 int
1193 main (int argc, char **argv)
1194 {
1195   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1196   unformat_input_t _argv, *a = &_argv;
1197   u8 *chroot_prefix;
1198   u8 *heap, *uri = 0;
1199   u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1200   u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1201   u64 bytes_to_send = 64 << 10, mbytes;
1202   u32 tmp;
1203   mheap_t *h;
1204   int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1205
1206   clib_mem_init (0, 256 << 20);
1207
1208   heap = clib_mem_get_per_cpu_heap ();
1209   h = mheap_header (heap);
1210
1211   /* make the main heap thread-safe */
1212   h->flags |= MHEAP_FLAG_THREAD_SAFE;
1213
1214   vec_validate (utm->rx_buf, 128 << 10);
1215
1216   utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1217
1218   utm->my_pid = getpid ();
1219   utm->configured_segment_size = 1 << 20;
1220   utm->socket_name = 0;
1221   utm->use_sock_api = 1;
1222
1223   clib_time_init (&utm->clib_time);
1224   init_error_string_table (utm);
1225   svm_fifo_segment_init (0x200000000ULL, 20);
1226   unformat_init_command_line (a, argv);
1227
1228   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1229     {
1230       if (unformat (a, "chroot prefix %s", &chroot_prefix))
1231         {
1232           vl_set_memory_root_path ((char *) chroot_prefix);
1233         }
1234       else if (unformat (a, "uri %s", &uri))
1235         ;
1236       else if (unformat (a, "segment-size %dM", &tmp))
1237         utm->configured_segment_size = tmp << 20;
1238       else if (unformat (a, "segment-size %dG", &tmp))
1239         utm->configured_segment_size = tmp << 30;
1240       else if (unformat (a, "master"))
1241         i_am_master = 1;
1242       else if (unformat (a, "slave"))
1243         i_am_master = 0;
1244       else if (unformat (a, "drop"))
1245         drop_packets = 1;
1246       else if (unformat (a, "test"))
1247         test_return_packets = 1;
1248       else if (unformat (a, "mbytes %lld", &mbytes))
1249         {
1250           bytes_to_send = mbytes << 20;
1251         }
1252       else if (unformat (a, "gbytes %lld", &mbytes))
1253         {
1254           bytes_to_send = mbytes << 30;
1255         }
1256       else if (unformat (a, "socket-name %s", &utm->socket_name))
1257         ;
1258       else if (unformat (a, "use-svm-api"))
1259         utm->use_sock_api = 0;
1260       else
1261         {
1262           fformat (stderr, "%s: usage [master|slave]\n");
1263           exit (1);
1264         }
1265     }
1266
1267   if (!utm->socket_name)
1268     utm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
1269
1270   if (uri)
1271     {
1272       utm->uri = format (0, "%s%c", uri, 0);
1273       utm->connect_uri = format (0, "%s%c", uri, 0);
1274     }
1275   else
1276     {
1277       utm->uri = format (0, "%s%c", bind_uri, 0);
1278       utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1279     }
1280
1281   utm->i_am_master = i_am_master;
1282   utm->segment_main = &svm_fifo_segment_main;
1283   utm->drop_packets = drop_packets;
1284   utm->test_return_packets = test_return_packets;
1285   utm->bytes_to_send = bytes_to_send;
1286   utm->time_to_stop = 0;
1287
1288   setup_signal_handlers ();
1289   uri_api_hookup (utm);
1290
1291   if (connect_to_vpp (i_am_master ? "tcp_echo_server" : "tcp_echo_client") <
1292       0)
1293     {
1294       svm_region_exit ();
1295       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1296       exit (1);
1297     }
1298
1299   if (i_am_master == 0)
1300     client_run (utm);
1301   else
1302     server_run (utm);
1303
1304   disconnect_from_vpp (utm);
1305   exit (0);
1306 }
1307
1308 /*
1309  * fd.io coding-style-patch-verification: ON
1310  *
1311  * Local Variables:
1312  * eval: (c-set-style "gnu")
1313  * End:
1314  */