0149448c91641211baaa380cf77aad0231317270
[vpp.git] / src / tests / vnet / session / quic_echo.c
1 /*
2  * Copyright (c) 2019 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 <vlibmemory/api.h>
21
22 #include <vpp/api/vpe_msg_enum.h>
23 #include <svm/fifo_segment.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 #define QUIC_ECHO_DBG 1
42 #define DBG(_fmt, _args...)                     \
43     if (QUIC_ECHO_DBG)                          \
44       clib_warning (_fmt, ##_args)
45
46 typedef struct
47 {
48   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
49 #define _(type, name) type name;
50   foreach_app_session_field
51 #undef _
52   u64 vpp_session_handle;
53   u64 bytes_sent;
54   u64 bytes_to_send;
55   volatile u64 bytes_received;
56   volatile u64 bytes_to_receive;
57   f64 start;
58 } echo_session_t;
59
60 typedef enum
61 {
62   STATE_START,
63   STATE_ATTACHED,
64   STATE_LISTEN,
65   STATE_READY,
66   STATE_DISCONNECTING,
67   STATE_FAILED,
68   STATE_DETACHED
69 } connection_state_t;
70
71 enum quic_session_type_t
72 {
73   QUIC_SESSION_TYPE_QUIC = 0,
74   QUIC_SESSION_TYPE_STREAM = 1,
75   QUIC_SESSION_TYPE_LISTEN = INT32_MAX,
76 };
77
78 typedef struct
79 {
80   /* vpe input queue */
81   svm_queue_t *vl_input_queue;
82
83   /* API client handle */
84   u32 my_client_index;
85
86   /* The URI we're playing with */
87   u8 *uri;
88
89   /* Session pool */
90   echo_session_t *sessions;
91
92   /* Hash table for disconnect processing */
93   uword *session_index_by_vpp_handles;
94
95   /* Hash table for shared segment_names */
96   uword *shared_segment_handles;
97   clib_spinlock_t segment_handles_lock;
98
99   /* intermediate rx buffer */
100   u8 *rx_buf;
101
102   /* URI for slave's connect */
103   u8 *connect_uri;
104
105   u32 connected_session_index;
106
107   int i_am_master;
108
109   /* drop all packets */
110   int no_return;
111
112   /* Our event queue */
113   svm_msg_q_t *our_event_queue;
114
115   u8 *socket_name;
116
117   pid_t my_pid;
118
119   /* For deadman timers */
120   clib_time_t clib_time;
121
122   /* State of the connection, shared between msg RX thread and main thread */
123   volatile connection_state_t state;
124
125   /* Signal variables */
126   volatile int time_to_stop;
127   volatile int time_to_print_stats;
128
129   u32 configured_segment_size;
130
131   /* VNET_API_ERROR_FOO -> "Foo" hash table */
132   uword *error_string_by_error_number;
133
134   u8 *connect_test_data;
135   pthread_t *client_thread_handles;
136   u32 *thread_args;
137   u32 client_bytes_received;
138   u8 test_return_packets;
139   u64 bytes_to_send;
140   u32 fifo_size;
141   u32 quic_streams;
142   u8 *appns_id;
143   u64 appns_flags;
144   u64 appns_secret;
145
146   u32 n_clients;
147   u64 tx_total;
148   u64 rx_total;
149
150   volatile u32 n_clients_connected;
151   volatile u32 n_active_clients;
152
153
154   /** Flag that decides if socket, instead of svm, api is used to connect to
155    * vpp. If sock api is used, shm binary api is subsequently bootstrapped
156    * and all other messages are exchanged using shm IPC. */
157   u8 use_sock_api;
158   int max_test_msg;
159
160   fifo_segment_main_t segment_main;
161 } echo_main_t;
162
163 echo_main_t echo_main;
164
165 #if CLIB_DEBUG > 0
166 #define NITER 10000
167 #else
168 #define NITER 4000000
169 #endif
170
171 static u8 *
172 format_api_error (u8 * s, va_list * args)
173 {
174   echo_main_t *em = &echo_main;
175   i32 error = va_arg (*args, u32);
176   uword *p;
177
178   p = hash_get (em->error_string_by_error_number, -error);
179
180   if (p)
181     s = format (s, "%s", p[0]);
182   else
183     s = format (s, "%d", error);
184   return s;
185 }
186
187 static void
188 init_error_string_table (echo_main_t * em)
189 {
190   em->error_string_by_error_number = hash_create (0, sizeof (uword));
191
192 #define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s);
193   foreach_vnet_api_error;
194 #undef _
195
196   hash_set (em->error_string_by_error_number, 99, "Misc");
197 }
198
199 static void handle_mq_event (session_event_t * e);
200
201 #if CLIB_DEBUG > 0
202 #define TIMEOUT 10.0
203 #else
204 #define TIMEOUT 10.0
205 #endif
206
207 static int
208 wait_for_segment_allocation (u64 segment_handle)
209 {
210   echo_main_t *em = &echo_main;
211   f64 timeout;
212   timeout = clib_time_now (&em->clib_time) + TIMEOUT;
213   uword *segment_present;
214   DBG ("Waiting for segment %lx...", segment_handle);
215   while (clib_time_now (&em->clib_time) < timeout)
216     {
217       clib_spinlock_lock (&em->segment_handles_lock);
218       segment_present = hash_get (em->shared_segment_handles, segment_handle);
219       clib_spinlock_unlock (&em->segment_handles_lock);
220       if (segment_present != 0)
221         return 0;
222       if (em->time_to_stop == 1)
223         return 0;
224     }
225   DBG ("timeout waiting for segment_allocation %lx", segment_handle);
226   return -1;
227 }
228
229 static int
230 wait_for_disconnected_sessions (echo_main_t * em)
231 {
232   f64 timeout;
233   timeout = clib_time_now (&em->clib_time) + TIMEOUT;
234   while (clib_time_now (&em->clib_time) < timeout)
235     {
236       if (hash_elts (em->session_index_by_vpp_handles) == 0)
237         return 0;
238     }
239   DBG ("timeout waiting for disconnected_sessions");
240   return -1;
241 }
242
243 static int
244 wait_for_state_change (echo_main_t * em, connection_state_t state)
245 {
246   svm_msg_q_msg_t msg;
247   session_event_t *e;
248   f64 timeout;
249   timeout = clib_time_now (&em->clib_time) + TIMEOUT;
250
251   while (clib_time_now (&em->clib_time) < timeout)
252     {
253       if (em->state == state)
254         return 0;
255       if (em->state == STATE_FAILED)
256         return -1;
257       if (em->time_to_stop == 1)
258         return 0;
259       if (!em->our_event_queue || em->state < STATE_ATTACHED)
260         continue;
261
262       if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_NOWAIT, 0))
263         continue;
264       e = svm_msg_q_msg_data (em->our_event_queue, &msg);
265       handle_mq_event (e);
266       svm_msg_q_free_msg (em->our_event_queue, &msg);
267     }
268   clib_warning ("timeout waiting for state %d", state);
269   return -1;
270 }
271
272 void
273 application_send_attach (echo_main_t * em)
274 {
275   vl_api_application_attach_t *bmp;
276   vl_api_application_tls_cert_add_t *cert_mp;
277   vl_api_application_tls_key_add_t *key_mp;
278
279   bmp = vl_msg_api_alloc (sizeof (*bmp));
280   clib_memset (bmp, 0, sizeof (*bmp));
281
282   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
283   bmp->client_index = em->my_client_index;
284   bmp->context = ntohl (0xfeedface);
285   bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
286   bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
287   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
288   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size;
289   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size;
290   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
291   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
292   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256;
293   if (em->appns_id)
294     {
295       bmp->namespace_id_len = vec_len (em->appns_id);
296       clib_memcpy_fast (bmp->namespace_id, em->appns_id,
297                         bmp->namespace_id_len);
298       bmp->options[APP_OPTIONS_FLAGS] |= em->appns_flags;
299       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = em->appns_secret;
300     }
301   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
302
303   cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
304   clib_memset (cert_mp, 0, sizeof (*cert_mp));
305   cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
306   cert_mp->client_index = em->my_client_index;
307   cert_mp->context = ntohl (0xfeedface);
308   cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
309   clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
310   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
311
312   key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
313   clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
314   key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
315   key_mp->client_index = em->my_client_index;
316   key_mp->context = ntohl (0xfeedface);
317   key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
318   clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
319   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
320 }
321
322 static int
323 application_attach (echo_main_t * em)
324 {
325   application_send_attach (em);
326   if (wait_for_state_change (em, STATE_ATTACHED))
327     {
328       clib_warning ("timeout waiting for STATE_ATTACHED");
329       return -1;
330     }
331   return 0;
332 }
333
334 void
335 application_detach (echo_main_t * em)
336 {
337   vl_api_application_detach_t *bmp;
338   bmp = vl_msg_api_alloc (sizeof (*bmp));
339   clib_memset (bmp, 0, sizeof (*bmp));
340
341   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
342   bmp->client_index = em->my_client_index;
343   bmp->context = ntohl (0xfeedface);
344   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
345
346   DBG ("%s", "Sent detach");
347 }
348
349 static int
350 ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
351 {
352   fifo_segment_create_args_t _a, *a = &_a;
353   fifo_segment_main_t *sm = &echo_main.segment_main;
354   int rv;
355
356   clib_memset (a, 0, sizeof (*a));
357   a->segment_name = (char *) name;
358   a->segment_type = type;
359
360   if (type == SSVM_SEGMENT_MEMFD)
361     a->memfd_fd = fd;
362
363   if ((rv = fifo_segment_attach (sm, a)))
364     {
365       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
366       return rv;
367     }
368   vec_reset_length (a->new_segment_indices);
369   return 0;
370 }
371
372 static void
373 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
374                                            mp)
375 {
376   echo_main_t *em = &echo_main;
377   int *fds = 0;
378   u32 n_fds = 0;
379   u64 segment_handle;
380   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
381   DBG ("Attached returned app %u", htons (mp->app_index));
382
383   if (mp->retval)
384     {
385       clib_warning ("attach failed: %U", format_api_error,
386                     clib_net_to_host_u32 (mp->retval));
387       goto failed;
388     }
389
390   if (mp->segment_name_length == 0)
391     {
392       clib_warning ("segment_name_length zero");
393       goto failed;
394     }
395
396   ASSERT (mp->app_event_queue_address);
397   em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
398                                           svm_msg_q_t *);
399
400   if (mp->n_fds)
401     {
402       vec_validate (fds, mp->n_fds);
403       vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
404
405       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
406         if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
407           goto failed;
408
409       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
410         if (ssvm_segment_attach ((char *) mp->segment_name,
411                                  SSVM_SEGMENT_MEMFD, fds[n_fds++]))
412           goto failed;
413
414       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
415         svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
416
417       vec_free (fds);
418     }
419   else
420     {
421       if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
422                                -1))
423         goto failed;
424     }
425   clib_spinlock_lock (&em->segment_handles_lock);
426   hash_set (em->shared_segment_handles, segment_handle, 1);
427   clib_spinlock_unlock (&em->segment_handles_lock);
428   DBG ("Mapped new segment %lx", segment_handle);
429
430   em->state = STATE_ATTACHED;
431   return;
432 failed:
433   em->state = STATE_FAILED;
434   return;
435 }
436
437 static void
438 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
439                                            mp)
440 {
441   if (mp->retval)
442     clib_warning ("detach returned with err: %d", mp->retval);
443   echo_main.state = STATE_DETACHED;
444 }
445
446 static void
447 stop_signal (int signum)
448 {
449   echo_main_t *um = &echo_main;
450   um->time_to_stop = 1;
451 }
452
453 static void
454 stats_signal (int signum)
455 {
456   echo_main_t *um = &echo_main;
457   um->time_to_print_stats = 1;
458 }
459
460 static clib_error_t *
461 setup_signal_handlers (void)
462 {
463   signal (SIGUSR2, stats_signal);
464   signal (SIGINT, stop_signal);
465   signal (SIGQUIT, stop_signal);
466   signal (SIGTERM, stop_signal);
467   return 0;
468 }
469
470 void
471 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
472 {
473   clib_warning ("BUG");
474 }
475
476 int
477 connect_to_vpp (char *name)
478 {
479   echo_main_t *em = &echo_main;
480   api_main_t *am = &api_main;
481
482   if (em->use_sock_api)
483     {
484       if (vl_socket_client_connect ((char *) em->socket_name, name,
485                                     0 /* default rx, tx buffer */ ))
486         {
487           clib_warning ("socket connect failed");
488           return -1;
489         }
490
491       if (vl_socket_client_init_shm (0, 1 /* want_pthread */ ))
492         {
493           clib_warning ("init shm api failed");
494           return -1;
495         }
496     }
497   else
498     {
499       if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
500         {
501           clib_warning ("shmem connect failed");
502           return -1;
503         }
504     }
505   em->vl_input_queue = am->shmem_hdr->vl_input_queue;
506   em->my_client_index = am->my_client_index;
507   return 0;
508 }
509
510 void
511 disconnect_from_vpp (echo_main_t * em)
512 {
513   if (em->use_sock_api)
514     vl_socket_client_disconnect ();
515   else
516     vl_client_disconnect_from_vlib ();
517 }
518
519 static void
520 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
521 {
522   fifo_segment_main_t *sm = &echo_main.segment_main;
523   fifo_segment_create_args_t _a, *a = &_a;
524   echo_main_t *em = &echo_main;
525   int rv;
526   int *fds = 0;
527   u64 segment_handle;
528   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
529
530   if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
531     {
532       vec_validate (fds, 1);
533       vl_socket_client_recv_fd_msg (fds, 1, 5);
534       if (ssvm_segment_attach
535           ((char *) mp->segment_name, SSVM_SEGMENT_MEMFD, fds[0]))
536         clib_warning
537           ("svm_fifo_segment_attach ('%s') failed on SSVM_SEGMENT_MEMFD",
538            mp->segment_name);
539       clib_spinlock_lock (&em->segment_handles_lock);
540       hash_set (em->shared_segment_handles, segment_handle, 1);
541       clib_spinlock_unlock (&em->segment_handles_lock);
542       vec_free (fds);
543       DBG ("Mapped new segment %lx", segment_handle);
544       return;
545     }
546
547   clib_memset (a, 0, sizeof (*a));
548   a->segment_name = (char *) mp->segment_name;
549   a->segment_size = mp->segment_size;
550   /* Attach to the segment vpp created */
551   rv = fifo_segment_attach (sm, a);
552   if (rv)
553     {
554       clib_warning ("svm_fifo_segment_attach ('%s') failed",
555                     mp->segment_name);
556       return;
557     }
558   clib_spinlock_lock (&em->segment_handles_lock);
559   hash_set (em->shared_segment_handles, mp->segment_name, 1);
560   clib_spinlock_unlock (&em->segment_handles_lock);
561   clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
562                 mp->segment_size);
563 }
564
565 static void
566 session_print_stats (echo_main_t * em, echo_session_t * session)
567 {
568   f64 deltat;
569   u64 bytes;
570
571   deltat = clib_time_now (&em->clib_time) - session->start;
572   bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send;
573   fformat (stdout, "Finished in %.6f\n", deltat);
574   fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
575 }
576
577 static void
578 test_recv_bytes (echo_main_t * em, echo_session_t * s, u8 * rx_buf,
579                  u32 n_read)
580 {
581   int i;
582   for (i = 0; i < n_read; i++)
583     {
584       if (rx_buf[i] != ((s->bytes_received + i) & 0xff)
585           && em->max_test_msg > 0)
586         {
587           clib_warning ("error at byte %lld, 0x%x not 0x%x",
588                         s->bytes_received + i, rx_buf[i],
589                         ((s->bytes_received + i) & 0xff));
590           em->max_test_msg--;
591           if (em->max_test_msg == 0)
592             clib_warning ("Too many errors, hiding next ones");
593         }
594     }
595 }
596
597 static void
598 recv_data_chunk (echo_main_t * em, echo_session_t * s, u8 * rx_buf)
599 {
600   int n_to_read, n_read;
601
602   n_to_read = svm_fifo_max_dequeue (s->rx_fifo);
603   if (!n_to_read)
604     return;
605
606   do
607     {
608       n_read = app_recv_stream ((app_session_t *) s, rx_buf,
609                                 vec_len (rx_buf));
610
611       if (n_read > 0)
612         {
613           if (em->test_return_packets)
614             test_recv_bytes (em, s, rx_buf, n_read);
615
616           n_to_read -= n_read;
617
618           s->bytes_received += n_read;
619           ASSERT (s->bytes_to_receive >= n_read);
620           s->bytes_to_receive -= n_read;
621         }
622       else
623         break;
624     }
625   while (n_to_read > 0);
626 }
627
628 static void
629 send_data_chunk (echo_main_t * em, echo_session_t * s)
630 {
631   u64 test_buf_len, bytes_this_chunk, test_buf_offset;
632   u8 *test_data = em->connect_test_data;
633   int n_sent;
634
635   test_buf_len = vec_len (test_data);
636   test_buf_offset = s->bytes_sent % test_buf_len;
637   bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
638                                s->bytes_to_send);
639
640   n_sent = app_send_stream ((app_session_t *) s, test_data + test_buf_offset,
641                             bytes_this_chunk, 0);
642
643   if (n_sent > 0)
644     {
645       s->bytes_to_send -= n_sent;
646       s->bytes_sent += n_sent;
647     }
648 }
649
650 /*
651  * Rx/Tx polling thread per connection
652  */
653 static void *
654 client_thread_fn (void *arg)
655 {
656   echo_main_t *em = &echo_main;
657   static u8 *rx_buf = 0;
658   u32 session_index = *(u32 *) arg;
659   echo_session_t *s;
660
661   vec_validate (rx_buf, 1 << 20);
662
663   while (!em->time_to_stop && em->state != STATE_READY)
664     ;
665
666   s = pool_elt_at_index (em->sessions, session_index);
667   while (!em->time_to_stop)
668     {
669       send_data_chunk (em, s);
670       recv_data_chunk (em, s, rx_buf);
671       if (!s->bytes_to_send && !s->bytes_to_receive)
672         break;
673     }
674
675   DBG ("session %d done send %lu to do, %lu done || recv %lu to do, %lu done",
676        session_index, s->bytes_to_send, s->bytes_sent, s->bytes_to_receive,
677        s->bytes_received);
678   em->tx_total += s->bytes_sent;
679   em->rx_total += s->bytes_received;
680   em->n_active_clients--;
681
682   pthread_exit (0);
683 }
684
685 void
686 client_send_connect (echo_main_t * em, u8 * uri, u32 opaque)
687 {
688   vl_api_connect_uri_t *cmp;
689   cmp = vl_msg_api_alloc (sizeof (*cmp));
690   clib_memset (cmp, 0, sizeof (*cmp));
691
692   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
693   cmp->client_index = em->my_client_index;
694   cmp->context = ntohl (opaque);
695   memcpy (cmp->uri, uri, vec_len (uri));
696   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
697 }
698
699 void
700 client_send_disconnect (echo_main_t * em, echo_session_t * s)
701 {
702   vl_api_disconnect_session_t *dmp;
703   dmp = vl_msg_api_alloc (sizeof (*dmp));
704   clib_memset (dmp, 0, sizeof (*dmp));
705   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
706   dmp->client_index = em->my_client_index;
707   dmp->handle = s->vpp_session_handle;
708   DBG ("Sending Session disonnect handle %lu", dmp->handle);
709   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
710 }
711
712 int
713 client_disconnect (echo_main_t * em, echo_session_t * s)
714 {
715   client_send_disconnect (em, s);
716   pool_put (em->sessions, s);
717   clib_memset (s, 0xfe, sizeof (*s));
718   return 0;
719 }
720
721 static void
722 session_bound_handler (session_bound_msg_t * mp)
723 {
724   echo_main_t *em = &echo_main;
725
726   if (mp->retval)
727     {
728       clib_warning ("bind failed: %U", format_api_error,
729                     clib_net_to_host_u32 (mp->retval));
730       em->state = STATE_FAILED;
731       return;
732     }
733
734   clib_warning ("listening on %U:%u", format_ip46_address, mp->lcl_ip,
735                 mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
736                 clib_net_to_host_u16 (mp->lcl_port));
737   em->state = STATE_READY;
738 }
739
740 static void
741 quic_qsession_accepted_handler (session_accepted_msg_t * mp)
742 {
743   DBG ("Accept on QSession index %u", mp->handle);
744 }
745
746
747 static void
748 session_accepted_handler (session_accepted_msg_t * mp)
749 {
750   app_session_evt_t _app_evt, *app_evt = &_app_evt;
751   session_accepted_reply_msg_t *rmp;
752   svm_fifo_t *rx_fifo, *tx_fifo;
753   echo_main_t *em = &echo_main;
754   echo_session_t *session;
755   static f64 start_time;
756   u32 session_index;
757   u64 segment_handle;
758   u8 *ip_str;
759
760   segment_handle = mp->segment_handle;
761
762   if (start_time == 0.0)
763     start_time = clib_time_now (&em->clib_time);
764
765   ip_str = format (0, "%U", format_ip46_address, &mp->rmt.ip, mp->rmt.is_ip4);
766   clib_warning ("Accepted session from: %s:%d", ip_str,
767                 clib_net_to_host_u16 (mp->rmt.port));
768
769   /* Allocate local session and set it up */
770   pool_get (em->sessions, session);
771   session_index = session - em->sessions;
772
773   if (wait_for_segment_allocation (segment_handle))
774     {
775       clib_warning ("timeout waiting for segment allocation %lu",
776                     segment_handle);
777       return;
778     }
779   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
780   rx_fifo->client_session_index = session_index;
781   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
782   tx_fifo->client_session_index = session_index;
783
784   session->rx_fifo = rx_fifo;
785   session->tx_fifo = tx_fifo;
786   session->vpp_session_handle = mp->handle;
787   session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
788                                          svm_msg_q_t *);
789
790   /* Add it to lookup table */
791   DBG ("Accepted session handle %lx, Listener %lx idx %lu", mp->handle,
792        mp->listener_handle, session_index);
793   hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
794
795   /*
796    * Send accept reply to vpp
797    */
798   app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
799                              SESSION_CTRL_EVT_ACCEPTED_REPLY);
800   rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
801   rmp->handle = mp->handle;
802   rmp->context = mp->context;
803   app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
804
805   /* TODO : this is very ugly */
806   if (mp->rmt.is_ip4 != 255)
807     return quic_qsession_accepted_handler (mp);
808   DBG ("SSession handle is %lu", mp->handle);
809
810   em->state = STATE_READY;
811
812   /* Stats printing */
813   if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0)
814     {
815       f64 now = clib_time_now (&em->clib_time);
816       fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
817                pool_elts (em->sessions), now - start_time,
818                (f64) pool_elts (em->sessions) / (now - start_time));
819     }
820
821   session->bytes_received = 0;
822   session->start = clib_time_now (&em->clib_time);
823 }
824
825 static void
826 quic_session_connected_handler (session_connected_msg_t * mp)
827 {
828   echo_main_t *em = &echo_main;
829   u8 *uri = format (0, "QUIC://session/%lu", mp->handle);
830   DBG ("QSession Connect : %s", uri);
831   client_send_connect (em, uri, QUIC_SESSION_TYPE_STREAM);
832 }
833
834 static void
835 session_connected_handler (session_connected_msg_t * mp)
836 {
837   echo_main_t *em = &echo_main;
838   echo_session_t *session;
839   u32 session_index;
840   svm_fifo_t *rx_fifo, *tx_fifo;
841   int rv;
842   u64 segment_handle;
843   segment_handle = mp->segment_handle;
844
845   if (mp->retval)
846     {
847       clib_warning ("connection failed with code: %U", format_api_error,
848                     clib_net_to_host_u32 (mp->retval));
849       em->state = STATE_FAILED;
850       return;
851     }
852
853   /*
854    * Setup session
855    */
856
857   pool_get (em->sessions, session);
858   clib_memset (session, 0, sizeof (*session));
859   session_index = session - em->sessions;
860   DBG ("Setting session_index %lu", session_index);
861
862   if (wait_for_segment_allocation (segment_handle))
863     {
864       clib_warning ("timeout waiting for segment allocation %lu",
865                     segment_handle);
866       return;
867     }
868   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
869   rx_fifo->client_session_index = session_index;
870   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
871   tx_fifo->client_session_index = session_index;
872
873   session->rx_fifo = rx_fifo;
874   session->tx_fifo = tx_fifo;
875   session->vpp_session_handle = mp->handle;
876   session->start = clib_time_now (&em->clib_time);
877   session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
878                                          svm_msg_q_t *);
879
880   DBG ("Connected session handle %lx, idx %lu", mp->handle, session_index);
881   hash_set (em->session_index_by_vpp_handles, mp->handle, session_index);
882
883   if (mp->context == QUIC_SESSION_TYPE_QUIC)
884     return quic_session_connected_handler (mp);
885
886   DBG ("SSession Connected");
887
888   /*
889    * Start RX thread
890    */
891   em->thread_args[em->n_clients_connected] = session_index;
892   rv = pthread_create (&em->client_thread_handles[em->n_clients_connected],
893                        NULL /*attr */ , client_thread_fn,
894                        (void *) &em->thread_args[em->n_clients_connected]);
895   if (rv)
896     {
897       clib_warning ("pthread_create returned %d", rv);
898       return;
899     }
900
901   em->n_clients_connected += 1;
902   clib_warning ("session %u (0x%llx) connected with local ip %U port %d",
903                 session_index, mp->handle, format_ip46_address, &mp->lcl.ip,
904                 mp->lcl.is_ip4, clib_net_to_host_u16 (mp->lcl.port));
905 }
906
907 static void
908 session_disconnected_handler (session_disconnected_msg_t * mp)
909 {
910   app_session_evt_t _app_evt, *app_evt = &_app_evt;
911   session_disconnected_reply_msg_t *rmp;
912   echo_main_t *em = &echo_main;
913   echo_session_t *session = 0;
914   uword *p;
915   int rv = 0;
916   DBG ("Disonnected session handle %lx", mp->handle);
917   p = hash_get (em->session_index_by_vpp_handles, mp->handle);
918   if (!p)
919     {
920       clib_warning ("couldn't find session key %llx", mp->handle);
921       return;
922     }
923
924   session = pool_elt_at_index (em->sessions, p[0]);
925   hash_unset (em->session_index_by_vpp_handles, mp->handle);
926
927   pool_put (em->sessions, session);
928
929   app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
930                              SESSION_CTRL_EVT_DISCONNECTED_REPLY);
931   rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
932   rmp->retval = rv;
933   rmp->handle = mp->handle;
934   rmp->context = mp->context;
935   app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
936
937   session_print_stats (em, session);
938 }
939
940 static void
941 session_reset_handler (session_reset_msg_t * mp)
942 {
943   app_session_evt_t _app_evt, *app_evt = &_app_evt;
944   echo_main_t *em = &echo_main;
945   session_reset_reply_msg_t *rmp;
946   echo_session_t *session = 0;
947   uword *p;
948   int rv = 0;
949
950   DBG ("Reset session handle %lx", mp->handle);
951   p = hash_get (em->session_index_by_vpp_handles, mp->handle);
952
953   if (p)
954     {
955       session = pool_elt_at_index (em->sessions, p[0]);
956       clib_warning ("got reset");
957       /* Cleanup later */
958       em->time_to_stop = 1;
959     }
960   else
961     {
962       clib_warning ("couldn't find session key %llx", mp->handle);
963       return;
964     }
965
966   app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt,
967                              SESSION_CTRL_EVT_RESET_REPLY);
968   rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
969   rmp->retval = rv;
970   rmp->handle = mp->handle;
971   app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt);
972 }
973
974 static void
975 handle_mq_event (session_event_t * e)
976 {
977   switch (e->event_type)
978     {
979     case SESSION_CTRL_EVT_BOUND:
980       DBG ("SESSION_CTRL_EVT_BOUND");
981       session_bound_handler ((session_bound_msg_t *) e->data);
982       break;
983     case SESSION_CTRL_EVT_ACCEPTED:
984       DBG ("SESSION_CTRL_EVT_ACCEPTED");
985       session_accepted_handler ((session_accepted_msg_t *) e->data);
986       break;
987     case SESSION_CTRL_EVT_CONNECTED:
988       DBG ("SESSION_CTRL_EVT_CONNECTED");
989       session_connected_handler ((session_connected_msg_t *) e->data);
990       break;
991     case SESSION_CTRL_EVT_DISCONNECTED:
992       DBG ("SESSION_CTRL_EVT_DISCONNECTED");
993       session_disconnected_handler ((session_disconnected_msg_t *) e->data);
994       break;
995     case SESSION_CTRL_EVT_RESET:
996       DBG ("SESSION_CTRL_EVT_RESET");
997       session_reset_handler ((session_reset_msg_t *) e->data);
998       break;
999     default:
1000       clib_warning ("unhandled %u", e->event_type);
1001     }
1002 }
1003
1004 static void
1005 clients_run (echo_main_t * em)
1006 {
1007   f64 start_time, deltat, timeout = 100.0;
1008   svm_msg_q_msg_t msg;
1009   session_event_t *e;
1010   echo_session_t *s;
1011   hash_pair_t *p;
1012   int i;
1013
1014   /* Init test data */
1015   vec_validate (em->connect_test_data, 1024 * 1024 - 1);
1016   for (i = 0; i < vec_len (em->connect_test_data); i++)
1017     em->connect_test_data[i] = i & 0xff;
1018
1019   /*
1020    * Attach and connect the clients
1021    */
1022   if (application_attach (em))
1023     return;
1024
1025   for (i = 0; i < em->n_clients; i++)
1026     client_send_connect (em, em->connect_uri, QUIC_SESSION_TYPE_QUIC);
1027
1028   start_time = clib_time_now (&em->clib_time);
1029   while (em->n_clients_connected < em->n_clients
1030          && (clib_time_now (&em->clib_time) - start_time < timeout)
1031          && em->state != STATE_FAILED && em->time_to_stop != 1)
1032
1033     {
1034       int rc = svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_TIMEDWAIT, 1);
1035       if (rc == ETIMEDOUT && em->time_to_stop)
1036         break;
1037       if (rc == ETIMEDOUT)
1038         continue;
1039       e = svm_msg_q_msg_data (em->our_event_queue, &msg);
1040       handle_mq_event (e);
1041       svm_msg_q_free_msg (em->our_event_queue, &msg);
1042     }
1043
1044   if (em->n_clients_connected != em->n_clients)
1045     {
1046       clib_warning ("failed to initialize all connections");
1047       return;
1048     }
1049
1050   /*
1051    * Initialize connections
1052    */
1053   DBG ("Initialize connections on %u clients", em->n_clients);
1054
1055   /* *INDENT-OFF* */
1056   hash_foreach_pair (p, em->session_index_by_vpp_handles,
1057                 ({
1058       s = pool_elt_at_index (em->sessions, p->value[0]);
1059       s->bytes_to_send = em->bytes_to_send;
1060       if (!em->no_return)
1061         s->bytes_to_receive = em->bytes_to_send;
1062                 }));
1063   /* *INDENT-ON* */
1064   em->n_active_clients = em->n_clients_connected;
1065
1066   /*
1067    * Wait for client threads to send the data
1068    */
1069   DBG ("Waiting for data on %u clients", em->n_active_clients);
1070   start_time = clib_time_now (&em->clib_time);
1071   em->state = STATE_READY;
1072   while (em->n_active_clients)
1073     if (!svm_msg_q_is_empty (em->our_event_queue))
1074       {
1075         if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_TIMEDWAIT, 0))
1076           {
1077             clib_warning ("svm msg q returned");
1078             continue;
1079           }
1080         e = svm_msg_q_msg_data (em->our_event_queue, &msg);
1081         if (e->event_type != FIFO_EVENT_APP_RX)
1082           handle_mq_event (e);
1083         svm_msg_q_free_msg (em->our_event_queue, &msg);
1084       }
1085
1086   /* *INDENT-OFF* */
1087   hash_foreach_pair (p, em->session_index_by_vpp_handles,
1088                 ({
1089       s = pool_elt_at_index (em->sessions, p->value[0]);
1090       DBG ("Sending disconnect on session %lu", p->key);
1091       client_disconnect (em, s);
1092                 }));
1093   /* *INDENT-ON* */
1094
1095   /*
1096    * Stats and detach
1097    */
1098   deltat = clib_time_now (&em->clib_time) - start_time;
1099   fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds\n",
1100            em->tx_total, em->tx_total / (1ULL << 20),
1101            em->tx_total / (1ULL << 30), deltat);
1102   fformat (stdout, "%.4f Gbit/second\n", (em->tx_total * 8.0) / deltat / 1e9);
1103
1104   wait_for_disconnected_sessions (em);
1105   application_detach (em);
1106 }
1107
1108 static void
1109 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
1110 {
1111   echo_main_t *em = &echo_main;
1112
1113   if (mp->retval)
1114     {
1115       clib_warning ("bind failed: %U", format_api_error,
1116                     clib_net_to_host_u32 (mp->retval));
1117       em->state = STATE_FAILED;
1118       return;
1119     }
1120
1121   em->state = STATE_READY;
1122 }
1123
1124 static void
1125 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
1126 {
1127   echo_main_t *em = &echo_main;
1128
1129   if (mp->retval != 0)
1130     clib_warning ("returned %d", ntohl (mp->retval));
1131
1132   em->state = STATE_START;
1133 }
1134
1135 u8 *
1136 format_ip4_address (u8 * s, va_list * args)
1137 {
1138   u8 *a = va_arg (*args, u8 *);
1139   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1140 }
1141
1142 u8 *
1143 format_ip6_address (u8 * s, va_list * args)
1144 {
1145   ip6_address_t *a = va_arg (*args, ip6_address_t *);
1146   u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1147
1148   i_max_n_zero = ARRAY_LEN (a->as_u16);
1149   max_n_zeros = 0;
1150   i_first_zero = i_max_n_zero;
1151   n_zeros = 0;
1152   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1153     {
1154       u32 is_zero = a->as_u16[i] == 0;
1155       if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1156         {
1157           i_first_zero = i;
1158           n_zeros = 0;
1159         }
1160       n_zeros += is_zero;
1161       if ((!is_zero && n_zeros > max_n_zeros)
1162           || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1163         {
1164           i_max_n_zero = i_first_zero;
1165           max_n_zeros = n_zeros;
1166           i_first_zero = ARRAY_LEN (a->as_u16);
1167           n_zeros = 0;
1168         }
1169     }
1170
1171   last_double_colon = 0;
1172   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1173     {
1174       if (i == i_max_n_zero && max_n_zeros > 1)
1175         {
1176           s = format (s, "::");
1177           i += max_n_zeros - 1;
1178           last_double_colon = 1;
1179         }
1180       else
1181         {
1182           s = format (s, "%s%x",
1183                       (last_double_colon || i == 0) ? "" : ":",
1184                       clib_net_to_host_u16 (a->as_u16[i]));
1185           last_double_colon = 0;
1186         }
1187     }
1188
1189   return s;
1190 }
1191
1192 /* Format an IP46 address. */
1193 u8 *
1194 format_ip46_address (u8 * s, va_list * args)
1195 {
1196   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1197   ip46_type_t type = va_arg (*args, ip46_type_t);
1198   int is_ip4 = 1;
1199
1200   switch (type)
1201     {
1202     case IP46_TYPE_ANY:
1203       is_ip4 = ip46_address_is_ip4 (ip46);
1204       break;
1205     case IP46_TYPE_IP4:
1206       is_ip4 = 1;
1207       break;
1208     case IP46_TYPE_IP6:
1209       is_ip4 = 0;
1210       break;
1211     }
1212
1213   return is_ip4 ?
1214     format (s, "%U", format_ip4_address, &ip46->ip4) :
1215     format (s, "%U", format_ip6_address, &ip46->ip6);
1216 }
1217
1218 static void
1219 server_handle_rx (echo_main_t * em, session_event_t * e)
1220 {
1221   int n_read, max_dequeue, n_sent;
1222   u32 offset, to_dequeue;
1223   echo_session_t *s;
1224   s = pool_elt_at_index (em->sessions, e->session_index);
1225
1226   /* Clear event only once. Otherwise, if we do it in the loop by calling
1227    * app_recv_stream, we may end up with a lot of unhandled rx events on the
1228    * message queue */
1229   svm_fifo_unset_event (s->rx_fifo);
1230
1231   max_dequeue = svm_fifo_max_dequeue (s->rx_fifo);
1232   if (PREDICT_FALSE (!max_dequeue))
1233     return;
1234   do
1235     {
1236       /* The options here are to limit ourselves to max_dequeue or read
1237        * even the data that was enqueued while we were dequeueing and which
1238        * now has an rx event in the mq. Either of the two work. */
1239       to_dequeue = clib_min (max_dequeue, vec_len (em->rx_buf));
1240       n_read = app_recv_stream_raw (s->rx_fifo, em->rx_buf, to_dequeue,
1241                                     0 /* clear evt */ , 0 /* peek */ );
1242
1243       if (n_read > 0)
1244         {
1245           if (em->test_return_packets)
1246             test_recv_bytes (em, s, em->rx_buf, n_read);
1247
1248           max_dequeue -= n_read;
1249           s->bytes_received += n_read;
1250         }
1251       else
1252         break;
1253
1254       /* Reflect if a non-drop session */
1255       if (!em->no_return && n_read > 0)
1256         {
1257           offset = 0;
1258           do
1259             {
1260               n_sent = app_send_stream ((app_session_t *) s,
1261                                         &em->rx_buf[offset],
1262                                         n_read, SVM_Q_WAIT);
1263               if (n_sent > 0)
1264                 {
1265                   n_read -= n_sent;
1266                   offset += n_sent;
1267                 }
1268             }
1269           while ((n_sent <= 0 || n_read > 0) && !em->time_to_stop);
1270         }
1271     }
1272   while (max_dequeue > 0 && !em->time_to_stop);
1273 }
1274
1275 static void
1276 server_handle_mq (echo_main_t * em)
1277 {
1278   svm_msg_q_msg_t msg;
1279   session_event_t *e;
1280
1281   while (1)
1282     {
1283       int rc = svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_TIMEDWAIT, 1);
1284       if (PREDICT_FALSE (rc == ETIMEDOUT && em->time_to_stop))
1285         break;
1286       if (PREDICT_FALSE (em->time_to_print_stats == 1))
1287         {
1288           em->time_to_print_stats = 0;
1289           fformat (stdout, "%d connections\n", pool_elts (em->sessions));
1290         }
1291       if (rc == ETIMEDOUT)
1292         continue;
1293       e = svm_msg_q_msg_data (em->our_event_queue, &msg);
1294       switch (e->event_type)
1295         {
1296         case SESSION_IO_EVT_RX:
1297           DBG ("SESSION_IO_EVT_RX");
1298           server_handle_rx (em, e);
1299           break;
1300         default:
1301           handle_mq_event (e);
1302           break;
1303         }
1304       svm_msg_q_free_msg (em->our_event_queue, &msg);
1305     }
1306 }
1307
1308 void
1309 server_send_listen (echo_main_t * em)
1310 {
1311   vl_api_bind_uri_t *bmp;
1312   bmp = vl_msg_api_alloc (sizeof (*bmp));
1313   clib_memset (bmp, 0, sizeof (*bmp));
1314
1315   bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1316   bmp->client_index = em->my_client_index;
1317   bmp->context = ntohl (0xfeedface);
1318   memcpy (bmp->uri, em->uri, vec_len (em->uri));
1319   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
1320 }
1321
1322 int
1323 server_listen (echo_main_t * em)
1324 {
1325   server_send_listen (em);
1326   if (wait_for_state_change (em, STATE_READY))
1327     {
1328       clib_warning ("timeout waiting for STATE_READY");
1329       return -1;
1330     }
1331   return 0;
1332 }
1333
1334 void
1335 server_send_unbind (echo_main_t * em)
1336 {
1337   vl_api_unbind_uri_t *ump;
1338
1339   ump = vl_msg_api_alloc (sizeof (*ump));
1340   clib_memset (ump, 0, sizeof (*ump));
1341
1342   ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1343   ump->client_index = em->my_client_index;
1344   memcpy (ump->uri, em->uri, vec_len (em->uri));
1345   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
1346 }
1347
1348 void
1349 server_run (echo_main_t * em)
1350 {
1351   echo_session_t *session;
1352   int i;
1353
1354   /* $$$$ hack preallocation */
1355   for (i = 0; i < 200000; i++)
1356     {
1357       pool_get (em->sessions, session);
1358       clib_memset (session, 0, sizeof (*session));
1359     }
1360   for (i = 0; i < 200000; i++)
1361     pool_put_index (em->sessions, i);
1362
1363   if (application_attach (em))
1364     return;
1365
1366   /* Bind to uri */
1367   if (server_listen (em))
1368     return;
1369
1370   /* Enter handle event loop */
1371   server_handle_mq (em);
1372
1373   /* Cleanup */
1374   server_send_unbind (em);
1375
1376   application_detach (em);
1377
1378   fformat (stdout, "Test complete...\n");
1379 }
1380
1381 static void
1382 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1383                                            mp)
1384 {
1385   echo_main_t *em = &echo_main;
1386   uword *p;
1387   DBG ("Got disonnected reply for session handle %lu", mp->handle);
1388
1389   if (mp->retval)
1390     {
1391       clib_warning ("vpp complained about disconnect: %d",
1392                     ntohl (mp->retval));
1393       return;
1394     }
1395
1396   em->state = STATE_START;
1397
1398   p = hash_get (em->session_index_by_vpp_handles, mp->handle);
1399   if (p)
1400     {
1401       hash_unset (em->session_index_by_vpp_handles, mp->handle);
1402     }
1403   else
1404     {
1405       clib_warning ("couldn't find session key %llx", mp->handle);
1406     }
1407 }
1408
1409 static void
1410   vl_api_application_tls_cert_add_reply_t_handler
1411   (vl_api_application_tls_cert_add_reply_t * mp)
1412 {
1413   if (mp->retval)
1414     clib_warning ("failed to add tls cert");
1415 }
1416
1417 static void
1418   vl_api_application_tls_key_add_reply_t_handler
1419   (vl_api_application_tls_key_add_reply_t * mp)
1420 {
1421   if (mp->retval)
1422     clib_warning ("failed to add tls key");
1423 }
1424
1425 #define foreach_quic_echo_msg                                           \
1426 _(BIND_URI_REPLY, bind_uri_reply)                                       \
1427 _(UNBIND_URI_REPLY, unbind_uri_reply)                                   \
1428 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
1429 _(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
1430 _(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
1431 _(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
1432 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)       \
1433 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)         \
1434
1435 void
1436 quic_echo_api_hookup (echo_main_t * em)
1437 {
1438 #define _(N,n)                                                  \
1439     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1440                            vl_api_##n##_t_handler,              \
1441                            vl_noop_handler,                     \
1442                            vl_api_##n##_t_endian,               \
1443                            vl_api_##n##_t_print,                \
1444                            sizeof(vl_api_##n##_t), 1);
1445   foreach_quic_echo_msg;
1446 #undef _
1447 }
1448
1449 int
1450 main (int argc, char **argv)
1451 {
1452   int i_am_server = 1, test_return_packets = 0;
1453   echo_main_t *em = &echo_main;
1454   fifo_segment_main_t *sm = &em->segment_main;
1455   unformat_input_t _argv, *a = &_argv;
1456   u8 *chroot_prefix;
1457   u8 *uri = 0;
1458   u8 *bind_uri = (u8 *) "quic://0.0.0.0/1234";
1459   u8 *connect_uri = (u8 *) "quic://6.0.1.1/1234";
1460   u64 bytes_to_send = 64 << 10, mbytes;
1461   char *app_name;
1462   u32 tmp;
1463
1464   clib_mem_init_thread_safe (0, 256 << 20);
1465
1466   clib_memset (em, 0, sizeof (*em));
1467   em->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1468   em->shared_segment_handles = hash_create (0, sizeof (uword));
1469   clib_spinlock_init (&em->segment_handles_lock);
1470   em->my_pid = getpid ();
1471   em->socket_name = 0;
1472   em->use_sock_api = 1;
1473   em->fifo_size = 64 << 10;
1474   em->n_clients = 1;
1475   em->max_test_msg = 50;
1476   em->quic_streams = 1;
1477
1478   clib_time_init (&em->clib_time);
1479   init_error_string_table (em);
1480   fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20);
1481   unformat_init_command_line (a, argv);
1482
1483   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1484     {
1485       if (unformat (a, "chroot prefix %s", &chroot_prefix))
1486         {
1487           vl_set_memory_root_path ((char *) chroot_prefix);
1488         }
1489       else if (unformat (a, "uri %s", &uri))
1490         ;
1491       else if (unformat (a, "server"))
1492         i_am_server = 1;
1493       else if (unformat (a, "client"))
1494         i_am_server = 0;
1495       else if (unformat (a, "no-return"))
1496         em->no_return = 1;
1497       else if (unformat (a, "test-bytes"))
1498         test_return_packets = 1;
1499       else if (unformat (a, "bytes %lld", &mbytes))
1500         {
1501           bytes_to_send = mbytes;
1502         }
1503       else if (unformat (a, "mbytes %lld", &mbytes))
1504         {
1505           bytes_to_send = mbytes << 20;
1506         }
1507       else if (unformat (a, "gbytes %lld", &mbytes))
1508         {
1509           bytes_to_send = mbytes << 30;
1510         }
1511       else if (unformat (a, "socket-name %s", &em->socket_name))
1512         ;
1513       else if (unformat (a, "use-svm-api"))
1514         em->use_sock_api = 0;
1515       else if (unformat (a, "fifo-size %d", &tmp))
1516         em->fifo_size = tmp << 10;
1517       else if (unformat (a, "nclients %d", &em->n_clients))
1518         ;
1519       else if (unformat (a, "appns %_%v%_", &em->appns_id))
1520         ;
1521       else if (unformat (a, "all-scope"))
1522         em->appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
1523                             | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
1524       else if (unformat (a, "local-scope"))
1525         em->appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
1526       else if (unformat (a, "global-scope"))
1527         em->appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
1528       else if (unformat (a, "secret %lu", &em->appns_secret))
1529         ;
1530       else if (unformat (a, "quic-streams %d", &em->quic_streams))
1531         ;
1532       else
1533         {
1534           fformat (stderr, "%s: usage [master|slave]\n", argv[0]);
1535           exit (1);
1536         }
1537     }
1538
1539   if (!em->socket_name)
1540     em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
1541
1542   if (uri)
1543     {
1544       em->uri = format (0, "%s%c", uri, 0);
1545       em->connect_uri = format (0, "%s%c", uri, 0);
1546     }
1547   else
1548     {
1549       em->uri = format (0, "%s%c", bind_uri, 0);
1550       em->connect_uri = format (0, "%s%c", connect_uri, 0);
1551     }
1552
1553   em->i_am_master = i_am_server;
1554   em->test_return_packets = test_return_packets;
1555   em->bytes_to_send = bytes_to_send;
1556   em->time_to_stop = 0;
1557   vec_validate (em->rx_buf, 4 << 20);
1558   vec_validate (em->client_thread_handles, em->n_clients - 1);
1559   vec_validate (em->thread_args, em->n_clients - 1);
1560
1561   setup_signal_handlers ();
1562   quic_echo_api_hookup (em);
1563
1564   app_name = i_am_server ? "quic_echo_server" : "quic_echo_client";
1565   if (connect_to_vpp (app_name) < 0)
1566     {
1567       svm_region_exit ();
1568       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1569       exit (1);
1570     }
1571
1572   if (i_am_server == 0)
1573     clients_run (em);
1574   else
1575     server_run (em);
1576
1577   /* Make sure detach finishes */
1578   wait_for_state_change (em, STATE_DETACHED);
1579
1580   disconnect_from_vpp (em);
1581   exit (0);
1582 }
1583
1584 /*
1585  * fd.io coding-style-patch-verification: ON
1586  *
1587  * Local Variables:
1588  * eval: (c-set-style "gnu")
1589  * End:
1590  */