VPP-659 TCP improvements
[vpp.git] / src / uri / uri_tcp_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 <signal.h>
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <svm/svm_fifo_segment.h>
21 #include <vlibmemory/api.h>
22 #include <vpp/api/vpe_msg_enum.h>
23 #include <vnet/session/application_interface.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 /* Satisfy external references when not linking with -lvlib */
42 vlib_main_t vlib_global_main;
43 vlib_main_t **vlib_mains;
44
45 typedef struct
46 {
47   svm_fifo_t *server_rx_fifo;
48   svm_fifo_t *server_tx_fifo;
49
50   u32 vpp_session_index;
51   u32 vpp_session_thread;
52 } session_t;
53
54 typedef enum
55 {
56   STATE_START,
57   STATE_READY,
58   STATE_DISCONNECTING,
59   STATE_FAILED
60 } connection_state_t;
61
62 typedef struct
63 {
64   /* vpe input queue */
65   unix_shared_memory_queue_t *vl_input_queue;
66
67   /* API client handle */
68   u32 my_client_index;
69
70   /* The URI we're playing with */
71   u8 *uri;
72
73   /* Session pool */
74   session_t *sessions;
75
76   /* Hash table for disconnect processing */
77   uword *session_index_by_vpp_handles;
78
79   /* intermediate rx buffer */
80   u8 *rx_buf;
81
82   /* URI for slave's connect */
83   u8 *connect_uri;
84
85   u32 connected_session_index;
86
87   int i_am_master;
88
89   /* drop all packets */
90   int drop_packets;
91
92   /* Our event queue */
93   unix_shared_memory_queue_t *our_event_queue;
94
95   /* $$$ single thread only for the moment */
96   unix_shared_memory_queue_t *vpp_event_queue;
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
120   /* convenience */
121   svm_fifo_segment_main_t *segment_main;
122 } uri_tcp_test_main_t;
123
124 uri_tcp_test_main_t uri_tcp_test_main;
125
126 #if CLIB_DEBUG > 0
127 #define NITER 10000
128 #else
129 #define NITER 4000000
130 #endif
131
132 int
133 wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
134 {
135 #if CLIB_DEBUG > 0
136 #define TIMEOUT 600.0
137 #else
138 #define TIMEOUT 600.0
139 #endif
140
141   f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
142
143   while (clib_time_now (&utm->clib_time) < timeout)
144     {
145       if (utm->state == state)
146         return 0;
147       if (utm->state == STATE_FAILED)
148         return -1;
149     }
150   clib_warning ("timeout waiting for STATE_READY");
151   return -1;
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 static void
167 stop_signal (int signum)
168 {
169   uri_tcp_test_main_t *um = &uri_tcp_test_main;
170
171   um->time_to_stop = 1;
172 }
173
174 static void
175 stats_signal (int signum)
176 {
177   uri_tcp_test_main_t *um = &uri_tcp_test_main;
178
179   um->time_to_print_stats = 1;
180 }
181
182 static clib_error_t *
183 setup_signal_handlers (void)
184 {
185   signal (SIGINT, stats_signal);
186   signal (SIGQUIT, stop_signal);
187   signal (SIGTERM, stop_signal);
188
189   return 0;
190 }
191
192 void
193 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
194 {
195   clib_warning ("BUG");
196 }
197
198 int
199 connect_to_vpp (char *name)
200 {
201   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
202   api_main_t *am = &api_main;
203
204   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
205     return -1;
206
207   utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
208   utm->my_client_index = am->my_client_index;
209
210   return 0;
211 }
212
213 static void
214 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
215 {
216   svm_fifo_segment_create_args_t _a, *a = &_a;
217   int rv;
218
219   a->segment_name = (char *) mp->segment_name;
220   a->segment_size = mp->segment_size;
221   /* Attach to the segment vpp created */
222   rv = svm_fifo_segment_attach (a);
223   if (rv)
224     {
225       clib_warning ("svm_fifo_segment_attach ('%s') failed",
226                     mp->segment_name);
227       return;
228     }
229   clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
230                 mp->segment_size);
231 }
232
233 static void
234 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
235 {
236   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
237   session_t *session;
238   vl_api_disconnect_session_reply_t *rmp;
239   uword *p;
240   int rv = 0;
241   u64 key;
242
243   key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
244
245   p = hash_get (utm->session_index_by_vpp_handles, key);
246
247   if (p)
248     {
249       session = pool_elt_at_index (utm->sessions, p[0]);
250       hash_unset (utm->session_index_by_vpp_handles, key);
251       pool_put (utm->sessions, session);
252     }
253   else
254     {
255       clib_warning ("couldn't find session key %llx", key);
256       rv = -11;
257     }
258
259   utm->time_to_stop = 1;
260
261   rmp = vl_msg_api_alloc (sizeof (*rmp));
262   memset (rmp, 0, sizeof (*rmp));
263
264   rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
265   rmp->retval = rv;
266   rmp->session_index = mp->session_index;
267   rmp->session_thread_index = mp->session_thread_index;
268   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
269 }
270
271 static void
272 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
273 {
274   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
275   session_t *session;
276   vl_api_reset_session_reply_t *rmp;
277   uword *p;
278   int rv = 0;
279   u64 key;
280
281   key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
282
283   p = hash_get (utm->session_index_by_vpp_handles, key);
284
285   if (p)
286     {
287       session = pool_elt_at_index (utm->sessions, p[0]);
288       hash_unset (utm->session_index_by_vpp_handles, key);
289       pool_put (utm->sessions, session);
290       utm->time_to_stop = 1;
291     }
292   else
293     {
294       clib_warning ("couldn't find session key %llx", key);
295       rv = -11;
296     }
297
298   rmp = vl_msg_api_alloc (sizeof (*rmp));
299   memset (rmp, 0, sizeof (*rmp));
300   rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
301   rmp->retval = rv;
302   rmp->session_index = mp->session_index;
303   rmp->session_thread_index = mp->session_thread_index;
304   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
305 }
306
307 void
308 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
309                              session_fifo_event_t * e)
310 {
311   svm_fifo_t *rx_fifo;
312   int n_read, bytes, i;
313
314   rx_fifo = e->fifo;
315
316   bytes = e->enqueue_length;
317   do
318     {
319       n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (utm->rx_buf),
320                                         utm->rx_buf);
321       if (n_read > 0)
322         {
323           bytes -= n_read;
324           for (i = 0; i < n_read; i++)
325             {
326               if (utm->rx_buf[i] != ((utm->client_bytes_received + i) & 0xff))
327                 {
328                   clib_warning ("error at byte %lld, 0x%x not 0x%x",
329                                 utm->client_bytes_received + i,
330                                 utm->rx_buf[i],
331                                 ((utm->client_bytes_received + i) & 0xff));
332                 }
333             }
334           utm->client_bytes_received += n_read;
335         }
336
337     }
338   while (n_read < 0 || bytes > 0);
339 }
340
341 void
342 client_handle_event_queue (uri_tcp_test_main_t * utm)
343 {
344   session_fifo_event_t _e, *e = &_e;;
345
346   unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
347                                 0 /* nowait */ );
348   switch (e->event_type)
349     {
350     case FIFO_EVENT_SERVER_RX:
351       client_handle_fifo_event_rx (utm, e);
352       break;
353
354     case FIFO_EVENT_SERVER_EXIT:
355       return;
356
357     default:
358       clib_warning ("unknown event type %d", e->event_type);
359       break;
360     }
361 }
362
363 static void *
364 client_rx_thread_fn (void *arg)
365 {
366   session_fifo_event_t _e, *e = &_e;
367   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
368
369   utm->client_bytes_received = 0;
370   while (1)
371     {
372       unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
373                                     0 /* nowait */ );
374       switch (e->event_type)
375         {
376         case FIFO_EVENT_SERVER_RX:
377           client_handle_fifo_event_rx (utm, e);
378           break;
379
380         case FIFO_EVENT_SERVER_EXIT:
381           return 0;
382         default:
383           clib_warning ("unknown event type %d", e->event_type);
384           break;
385         }
386
387       if (PREDICT_FALSE (utm->time_to_stop == 1))
388         break;
389     }
390   pthread_exit (0);
391 }
392
393
394 static void
395 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
396 {
397   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
398   svm_fifo_segment_create_args_t _a, *a = &_a;
399   session_t *session;
400   u32 session_index;
401   svm_fifo_t *rx_fifo, *tx_fifo;
402   int rv;
403   u64 key;
404
405   if (mp->retval)
406     {
407       clib_warning ("connection failed with code: %d", mp->retval);
408       utm->state = STATE_FAILED;
409       return;
410     }
411
412   /*
413    * Attatch to segment
414    */
415
416   if (mp->segment_name_length == 0)
417     {
418       clib_warning ("segment_name_length zero");
419       utm->state = STATE_FAILED;
420       return;
421     }
422
423   a->segment_name = (char *) mp->segment_name;
424   a->segment_size = mp->segment_size;
425
426   ASSERT (mp->client_event_queue_address);
427
428   /* Attach to the segment vpp created */
429   rv = svm_fifo_segment_attach (a);
430   if (rv)
431     {
432       clib_warning ("svm_fifo_segment_attach ('%s') failed",
433                     mp->segment_name);
434       return;
435     }
436
437   /*
438    * Save the queues
439    */
440
441   utm->our_event_queue = (unix_shared_memory_queue_t *)
442     mp->client_event_queue_address;
443
444   utm->vpp_event_queue = (unix_shared_memory_queue_t *)
445     mp->vpp_event_queue_address;
446
447   /*
448    * Setup session
449    */
450
451   pool_get (utm->sessions, session);
452   session_index = session - utm->sessions;
453
454   rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
455   rx_fifo->client_session_index = session_index;
456   tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
457   tx_fifo->client_session_index = session_index;
458
459   session->server_rx_fifo = rx_fifo;
460   session->server_tx_fifo = tx_fifo;
461   session->vpp_session_index = mp->session_index;
462   session->vpp_session_thread = mp->session_thread_index;
463
464   /* Save handle */
465   utm->connected_session_index = session_index;
466   utm->state = STATE_READY;
467
468   /* Add it to lookup table */
469   key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
470   hash_set (utm->session_index_by_vpp_handles, key, session_index);
471
472   /* Start RX thread */
473   rv = pthread_create (&utm->client_rx_thread_handle,
474                        NULL /*attr */ , client_rx_thread_fn, 0);
475   if (rv)
476     {
477       clib_warning ("pthread_create returned %d", rv);
478       rv = VNET_API_ERROR_SYSCALL_ERROR_1;
479     }
480 }
481
482 void
483 client_send_data (uri_tcp_test_main_t * utm)
484 {
485   u8 *test_data = utm->connect_test_data;
486   u64 bytes_sent = 0;
487   int rv;
488   int mypid = getpid ();
489   session_t *session;
490   svm_fifo_t *tx_fifo;
491   int buffer_offset, bytes_to_send = 0;
492   session_fifo_event_t evt;
493   static int serial_number = 0;
494   int i;
495   u32 max_chunk = 64 << 10, write;
496
497   session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
498   tx_fifo = session->server_tx_fifo;
499
500   vec_validate (utm->rx_buf, vec_len (test_data) - 1);
501
502   for (i = 0; i < 1; i++)
503     {
504       bytes_to_send = vec_len (test_data);
505       buffer_offset = 0;
506       while (bytes_to_send > 0)
507         {
508           write = bytes_to_send > max_chunk ? max_chunk : bytes_to_send;
509           rv = svm_fifo_enqueue_nowait (tx_fifo, mypid, write,
510                                         test_data + buffer_offset);
511
512           if (rv > 0)
513             {
514               bytes_to_send -= rv;
515               buffer_offset += rv;
516               bytes_sent += rv;
517
518               /* Fabricate TX event, send to vpp */
519               evt.fifo = tx_fifo;
520               evt.event_type = FIFO_EVENT_SERVER_TX;
521               /* $$$$ for event logging */
522               evt.enqueue_length = rv;
523               evt.event_id = serial_number++;
524
525               unix_shared_memory_queue_add (utm->vpp_event_queue,
526                                             (u8 *) & evt,
527                                             0 /* do wait for mutex */ );
528             }
529         }
530     }
531
532   if (utm->test_return_packets)
533     {
534       f64 timeout = clib_time_now (&utm->clib_time) + 2;
535
536       /* Wait for the outstanding packets */
537       while (utm->client_bytes_received < vec_len (test_data))
538         {
539           if (clib_time_now (&utm->clib_time) > timeout)
540             {
541               clib_warning ("timed out waiting for the missing packets");
542               break;
543             }
544         }
545
546       utm->time_to_stop = 1;
547     }
548 }
549
550 void
551 client_connect (uri_tcp_test_main_t * utm)
552 {
553   vl_api_connect_uri_t *cmp;
554   cmp = vl_msg_api_alloc (sizeof (*cmp));
555   memset (cmp, 0, sizeof (*cmp));
556
557   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
558   cmp->client_index = utm->my_client_index;
559   cmp->context = ntohl (0xfeedface);
560   memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
561   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
562 }
563
564 void
565 client_disconnect (uri_tcp_test_main_t * utm)
566 {
567   session_t *connected_session;
568   vl_api_disconnect_session_t *dmp;
569   connected_session = pool_elt_at_index (utm->sessions,
570                                          utm->connected_session_index);
571   dmp = vl_msg_api_alloc (sizeof (*dmp));
572   memset (dmp, 0, sizeof (*dmp));
573   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
574   dmp->client_index = utm->my_client_index;
575   dmp->session_index = connected_session->vpp_session_index;
576   dmp->session_thread_index = connected_session->vpp_session_thread;
577   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
578 }
579
580 static void
581 client_test (uri_tcp_test_main_t * utm)
582 {
583   int i;
584
585   client_connect (utm);
586
587   if (wait_for_state_change (utm, STATE_READY))
588     {
589       return;
590     }
591
592   /* Init test data */
593   vec_validate (utm->connect_test_data, 64 * 1024 - 1);
594   for (i = 0; i < vec_len (utm->connect_test_data); i++)
595     utm->connect_test_data[i] = i & 0xff;
596
597   /* Start send */
598   client_send_data (utm);
599
600   /* Disconnect */
601   client_disconnect (utm);
602 }
603
604 static void
605 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
606 {
607   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
608   svm_fifo_segment_create_args_t _a, *a = &_a;
609   int rv;
610
611   if (mp->retval)
612     {
613       clib_warning ("bind failed: %d", mp->retval);
614       utm->state = STATE_FAILED;
615       return;
616     }
617
618   if (mp->segment_name_length == 0)
619     {
620       clib_warning ("segment_name_length zero");
621       return;
622     }
623
624   a->segment_name = (char *) mp->segment_name;
625   a->segment_size = mp->segment_size;
626
627   ASSERT (mp->server_event_queue_address);
628
629   /* Attach to the segment vpp created */
630   rv = svm_fifo_segment_attach (a);
631   if (rv)
632     {
633       clib_warning ("svm_fifo_segment_attach ('%s') failed",
634                     mp->segment_name);
635       return;
636     }
637
638   utm->our_event_queue =
639     (unix_shared_memory_queue_t *) mp->server_event_queue_address;
640
641   utm->state = STATE_READY;
642 }
643
644 static void
645 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
646 {
647   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
648
649   if (mp->retval != 0)
650     clib_warning ("returned %d", ntohl (mp->retval));
651
652   utm->state = STATE_START;
653 }
654
655 static void
656 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
657 {
658   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
659   vl_api_accept_session_reply_t *rmp;
660   svm_fifo_t *rx_fifo, *tx_fifo;
661   session_t *session;
662   static f64 start_time;
663   u64 key;
664   u32 session_index;
665
666   if (start_time == 0.0)
667     start_time = clib_time_now (&utm->clib_time);
668
669   utm->vpp_event_queue = (unix_shared_memory_queue_t *)
670     mp->vpp_event_queue_address;
671
672   /* Allocate local session and set it up */
673   pool_get (utm->sessions, session);
674   session_index = session - utm->sessions;
675
676   rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
677   rx_fifo->client_session_index = session_index;
678   tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
679   tx_fifo->client_session_index = session_index;
680
681   session->server_rx_fifo = rx_fifo;
682   session->server_tx_fifo = tx_fifo;
683
684   /* Add it to lookup table */
685   key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
686   hash_set (utm->session_index_by_vpp_handles, key, session_index);
687
688   utm->state = STATE_READY;
689
690   /* Stats printing */
691   if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
692     {
693       f64 now = clib_time_now (&utm->clib_time);
694       fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
695                pool_elts (utm->sessions), now - start_time,
696                (f64) pool_elts (utm->sessions) / (now - start_time));
697     }
698
699   /*
700    * Send accept reply to vpp
701    */
702   rmp = vl_msg_api_alloc (sizeof (*rmp));
703   memset (rmp, 0, sizeof (*rmp));
704   rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
705   rmp->session_type = mp->session_type;
706   rmp->session_index = mp->session_index;
707   rmp->session_thread_index = mp->session_thread_index;
708   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
709 }
710
711 void
712 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
713                              session_fifo_event_t * e)
714 {
715   svm_fifo_t *rx_fifo, *tx_fifo;
716   int n_read;
717
718   session_fifo_event_t evt;
719   unix_shared_memory_queue_t *q;
720   int rv, bytes;
721
722   rx_fifo = e->fifo;
723   tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
724
725   bytes = e->enqueue_length;
726   do
727     {
728       n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (utm->rx_buf),
729                                         utm->rx_buf);
730
731       /* Reflect if a non-drop session */
732       if (!utm->drop_packets && n_read > 0)
733         {
734           do
735             {
736               rv = svm_fifo_enqueue_nowait (tx_fifo, 0, n_read, utm->rx_buf);
737             }
738           while (rv == -2 && !utm->time_to_stop);
739
740           /* Fabricate TX event, send to vpp */
741           evt.fifo = tx_fifo;
742           evt.event_type = FIFO_EVENT_SERVER_TX;
743           /* $$$$ for event logging */
744           evt.enqueue_length = n_read;
745           evt.event_id = e->event_id;
746           q = utm->vpp_event_queue;
747           unix_shared_memory_queue_add (q, (u8 *) & evt,
748                                         0 /* do wait for mutex */ );
749         }
750
751       if (n_read > 0)
752         bytes -= n_read;
753     }
754   while ((n_read < 0 || bytes > 0) && !utm->time_to_stop);
755 }
756
757 void
758 server_handle_event_queue (uri_tcp_test_main_t * utm)
759 {
760   session_fifo_event_t _e, *e = &_e;;
761
762   while (1)
763     {
764       unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
765                                     0 /* nowait */ );
766       switch (e->event_type)
767         {
768         case FIFO_EVENT_SERVER_RX:
769           server_handle_fifo_event_rx (utm, e);
770           break;
771
772         case FIFO_EVENT_SERVER_EXIT:
773           return;
774
775         default:
776           clib_warning ("unknown event type %d", e->event_type);
777           break;
778         }
779       if (PREDICT_FALSE (utm->time_to_stop == 1))
780         break;
781       if (PREDICT_FALSE (utm->time_to_print_stats == 1))
782         {
783           utm->time_to_print_stats = 0;
784           fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
785         }
786     }
787 }
788
789 void
790 server_bind (uri_tcp_test_main_t * utm)
791 {
792   vl_api_bind_uri_t *bmp;
793   u32 fifo_size = 3 << 20;
794   bmp = vl_msg_api_alloc (sizeof (*bmp));
795   memset (bmp, 0, sizeof (*bmp));
796
797   bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
798   bmp->client_index = utm->my_client_index;
799   bmp->context = ntohl (0xfeedface);
800   bmp->initial_segment_size = 256 << 20;        /* size of initial segment */
801   bmp->options[SESSION_OPTIONS_FLAGS] =
802     SESSION_OPTIONS_FLAGS_USE_FIFO | SESSION_OPTIONS_FLAGS_ADD_SEGMENT;
803   bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
804   bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
805   bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
806   memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
807   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
808 }
809
810 void
811 server_unbind (uri_tcp_test_main_t * utm)
812 {
813   vl_api_unbind_uri_t *ump;
814
815   ump = vl_msg_api_alloc (sizeof (*ump));
816   memset (ump, 0, sizeof (*ump));
817
818   ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
819   ump->client_index = utm->my_client_index;
820   memcpy (ump->uri, utm->uri, vec_len (utm->uri));
821   vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
822 }
823
824 void
825 server_test (uri_tcp_test_main_t * utm)
826 {
827   /* Bind to uri */
828   server_bind (utm);
829
830   if (wait_for_state_change (utm, STATE_READY))
831     {
832       clib_warning ("timeout waiting for STATE_READY");
833       return;
834     }
835
836   /* Enter handle event loop */
837   server_handle_event_queue (utm);
838
839   /* Cleanup */
840   server_unbind (utm);
841
842   if (wait_for_state_change (utm, STATE_START))
843     {
844       clib_warning ("timeout waiting for STATE_START");
845       return;
846     }
847
848   fformat (stdout, "Test complete...\n");
849 }
850
851 #define foreach_uri_msg                         \
852 _(BIND_URI_REPLY, bind_uri_reply)               \
853 _(UNBIND_URI_REPLY, unbind_uri_reply)           \
854 _(ACCEPT_SESSION, accept_session)               \
855 _(CONNECT_URI_REPLY, connect_uri_reply)         \
856 _(DISCONNECT_SESSION, disconnect_session)       \
857 _(RESET_SESSION, reset_session)                 \
858 _(MAP_ANOTHER_SEGMENT, map_another_segment)
859
860 void
861 uri_api_hookup (uri_tcp_test_main_t * utm)
862 {
863 #define _(N,n)                                                  \
864     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
865                            vl_api_##n##_t_handler,              \
866                            vl_noop_handler,                     \
867                            vl_api_##n##_t_endian,               \
868                            vl_api_##n##_t_print,                \
869                            sizeof(vl_api_##n##_t), 1);
870   foreach_uri_msg;
871 #undef _
872 }
873
874 int
875 main (int argc, char **argv)
876 {
877   uri_tcp_test_main_t *utm = &uri_tcp_test_main;
878   unformat_input_t _argv, *a = &_argv;
879   u8 *chroot_prefix;
880   u8 *heap;
881   u8 *bind_name = (u8 *) "tcp://0.0.0.0/1234";
882   u32 tmp;
883   mheap_t *h;
884   session_t *session;
885   int i;
886   int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
887
888   clib_mem_init (0, 256 << 20);
889
890   heap = clib_mem_get_per_cpu_heap ();
891   h = mheap_header (heap);
892
893   /* make the main heap thread-safe */
894   h->flags |= MHEAP_FLAG_THREAD_SAFE;
895
896   vec_validate (utm->rx_buf, 65536);
897
898   utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
899
900   utm->my_pid = getpid ();
901   utm->configured_segment_size = 1 << 20;
902
903   clib_time_init (&utm->clib_time);
904   init_error_string_table (utm);
905   svm_fifo_segment_init (0x200000000ULL, 20);
906   unformat_init_command_line (a, argv);
907
908   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
909     {
910       if (unformat (a, "chroot prefix %s", &chroot_prefix))
911         {
912           vl_set_memory_root_path ((char *) chroot_prefix);
913         }
914       else if (unformat (a, "uri %s", &bind_name))
915         ;
916       else if (unformat (a, "segment-size %dM", &tmp))
917         utm->configured_segment_size = tmp << 20;
918       else if (unformat (a, "segment-size %dG", &tmp))
919         utm->configured_segment_size = tmp << 30;
920       else if (unformat (a, "master"))
921         i_am_master = 1;
922       else if (unformat (a, "slave"))
923         i_am_master = 0;
924       else if (unformat (a, "drop"))
925         drop_packets = 1;
926       else if (unformat (a, "test"))
927         test_return_packets = 1;
928       else
929         {
930           fformat (stderr, "%s: usage [master|slave]\n");
931           exit (1);
932         }
933     }
934
935   utm->uri = format (0, "%s%c", bind_name, 0);
936   utm->i_am_master = i_am_master;
937   utm->segment_main = &svm_fifo_segment_main;
938   utm->drop_packets = drop_packets;
939   utm->test_return_packets = test_return_packets;
940   utm->connect_uri = format (0, "tcp://6.0.1.2/1234%c", 0);
941
942   setup_signal_handlers ();
943   uri_api_hookup (utm);
944
945   if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
946     {
947       svm_region_exit ();
948       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
949       exit (1);
950     }
951
952   if (i_am_master == 0)
953     {
954       client_test (utm);
955       exit (0);
956     }
957
958   /* $$$$ hack preallocation */
959   for (i = 0; i < 200000; i++)
960     {
961       pool_get (utm->sessions, session);
962       memset (session, 0, sizeof (*session));
963     }
964   for (i = 0; i < 200000; i++)
965     pool_put_index (utm->sessions, i);
966
967   server_test (utm);
968
969   vl_client_disconnect_from_vlib ();
970   exit (0);
971 }
972
973 /*
974  * fd.io coding-style-patch-verification: ON
975  *
976  * Local Variables:
977  * eval: (c-set-style "gnu")
978  * End:
979  */