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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <svm/svm_fifo_segment.h>
19 #include <vlibmemory/api.h>
20 #include <vpp/api/vpe_msg_enum.h>
21 #include <vnet/session/application_interface.h>
23 #define vl_typedefs /* define message structures */
24 #include <vpp/api/vpe_all_api_h.h>
27 /* declare message handlers for each api */
29 #define vl_endianfun /* define message structures */
30 #include <vpp/api/vpe_all_api_h.h>
33 /* instantiate all the print functions we know about */
34 #define vl_print(handle, ...)
36 #include <vpp/api/vpe_all_api_h.h>
39 /* Satisfy external references when not linking with -lvlib */
40 vlib_main_t vlib_global_main;
41 vlib_main_t **vlib_mains;
45 svm_fifo_t *server_rx_fifo;
46 svm_fifo_t *server_tx_fifo;
48 u32 vpp_session_handle;
62 unix_shared_memory_queue_t *vl_input_queue;
64 /* API client handle */
67 /* The URI we're playing with */
73 /* Hash table for disconnect processing */
74 uword *session_index_by_vpp_handles;
76 /* intermediate rx buffer */
79 /* URI for slave's connect */
82 u32 connected_session_index;
86 /* drop all packets */
90 unix_shared_memory_queue_t *our_event_queue;
92 /* $$$ single thread only for the moment */
93 unix_shared_memory_queue_t *vpp_event_queue;
97 /* For deadman timers */
98 clib_time_t clib_time;
100 /* State of the connection, shared between msg RX thread and main thread */
101 volatile connection_state_t state;
103 /* Signal variables */
104 volatile int time_to_stop;
105 volatile int time_to_print_stats;
107 u32 configured_segment_size;
109 /* VNET_API_ERROR_FOO -> "Foo" hash table */
110 uword *error_string_by_error_number;
112 u8 *connect_test_data;
113 pthread_t client_rx_thread_handle;
114 u32 client_bytes_received;
115 u8 test_return_packets;
119 svm_fifo_segment_main_t *segment_main;
120 } uri_tcp_test_main_t;
122 uri_tcp_test_main_t uri_tcp_test_main;
127 #define NITER 4000000
131 wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
134 #define TIMEOUT 600.0
136 #define TIMEOUT 600.0
139 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
141 while (clib_time_now (&utm->clib_time) < timeout)
143 if (utm->state == state)
145 if (utm->state == STATE_FAILED)
148 clib_warning ("timeout waiting for STATE_READY");
153 application_attach (uri_tcp_test_main_t * utm)
155 vl_api_application_attach_t *bmp;
156 u32 fifo_size = 3 << 20;
157 bmp = vl_msg_api_alloc (sizeof (*bmp));
158 memset (bmp, 0, sizeof (*bmp));
160 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
161 bmp->client_index = utm->my_client_index;
162 bmp->context = ntohl (0xfeedface);
163 bmp->options[SESSION_OPTIONS_FLAGS] =
164 SESSION_OPTIONS_FLAGS_USE_FIFO | SESSION_OPTIONS_FLAGS_ADD_SEGMENT;
165 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
166 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
167 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
168 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
169 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
173 application_detach (uri_tcp_test_main_t * utm)
175 vl_api_application_detach_t *bmp;
176 bmp = vl_msg_api_alloc (sizeof (*bmp));
177 memset (bmp, 0, sizeof (*bmp));
179 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
180 bmp->client_index = utm->my_client_index;
181 bmp->context = ntohl (0xfeedface);
182 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
186 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
189 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
190 svm_fifo_segment_create_args_t _a, *a = &_a;
195 uword *errp = hash_get (utm->error_string_by_error_number, mp->retval);
196 clib_warning ("attach failed: %s", *errp);
197 utm->state = STATE_FAILED;
201 if (mp->segment_name_length == 0)
203 clib_warning ("segment_name_length zero");
207 a->segment_name = (char *) mp->segment_name;
208 a->segment_size = mp->segment_size;
210 ASSERT (mp->app_event_queue_address);
212 /* Attach to the segment vpp created */
213 rv = svm_fifo_segment_attach (a);
216 clib_warning ("svm_fifo_segment_attach ('%s') failed",
221 utm->our_event_queue =
222 (unix_shared_memory_queue_t *) mp->app_event_queue_address;
227 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
231 clib_warning ("detach returned with err: %d", mp->retval);
235 init_error_string_table (uri_tcp_test_main_t * utm)
237 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
239 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
240 foreach_vnet_api_error;
243 hash_set (utm->error_string_by_error_number, 99, "Misc");
247 stop_signal (int signum)
249 uri_tcp_test_main_t *um = &uri_tcp_test_main;
251 um->time_to_stop = 1;
255 stats_signal (int signum)
257 uri_tcp_test_main_t *um = &uri_tcp_test_main;
259 um->time_to_print_stats = 1;
262 static clib_error_t *
263 setup_signal_handlers (void)
265 signal (SIGINT, stats_signal);
266 signal (SIGQUIT, stop_signal);
267 signal (SIGTERM, stop_signal);
273 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
275 clib_warning ("BUG");
279 connect_to_vpp (char *name)
281 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
282 api_main_t *am = &api_main;
284 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
287 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
288 utm->my_client_index = am->my_client_index;
294 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
296 svm_fifo_segment_create_args_t _a, *a = &_a;
299 a->segment_name = (char *) mp->segment_name;
300 a->segment_size = mp->segment_size;
301 /* Attach to the segment vpp created */
302 rv = svm_fifo_segment_attach (a);
305 clib_warning ("svm_fifo_segment_attach ('%s') failed",
309 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
314 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
316 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
318 vl_api_disconnect_session_reply_t *rmp;
322 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
326 session = pool_elt_at_index (utm->sessions, p[0]);
327 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
328 pool_put (utm->sessions, session);
332 clib_warning ("couldn't find session key %llx", mp->handle);
336 utm->time_to_stop = 1;
338 rmp = vl_msg_api_alloc (sizeof (*rmp));
339 memset (rmp, 0, sizeof (*rmp));
341 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
343 rmp->handle = mp->handle;
344 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
348 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
350 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
352 vl_api_reset_session_reply_t *rmp;
356 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
360 session = pool_elt_at_index (utm->sessions, p[0]);
361 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
362 pool_put (utm->sessions, session);
363 utm->time_to_stop = 1;
367 clib_warning ("couldn't find session key %llx", mp->handle);
371 rmp = vl_msg_api_alloc (sizeof (*rmp));
372 memset (rmp, 0, sizeof (*rmp));
373 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
375 rmp->handle = mp->handle;
376 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
380 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
381 session_fifo_event_t * e)
384 int n_read, bytes, i;
388 bytes = svm_fifo_max_dequeue (rx_fifo);
389 /* Allow enqueuing of new event */
390 svm_fifo_unset_event (rx_fifo);
395 n_read = svm_fifo_dequeue_nowait (rx_fifo, 0,
396 clib_min (vec_len (utm->rx_buf),
397 bytes), utm->rx_buf);
401 for (i = 0; i < n_read; i++)
403 if (utm->rx_buf[i] != ((utm->client_bytes_received + i) & 0xff))
405 clib_warning ("error at byte %lld, 0x%x not 0x%x",
406 utm->client_bytes_received + i,
408 ((utm->client_bytes_received + i) & 0xff));
411 utm->client_bytes_received += n_read;
417 // clib_warning ("weird!");
427 client_handle_event_queue (uri_tcp_test_main_t * utm)
429 session_fifo_event_t _e, *e = &_e;;
431 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
433 switch (e->event_type)
435 case FIFO_EVENT_SERVER_RX:
436 client_handle_fifo_event_rx (utm, e);
439 case FIFO_EVENT_SERVER_EXIT:
443 clib_warning ("unknown event type %d", e->event_type);
449 client_rx_thread_fn (void *arg)
451 session_fifo_event_t _e, *e = &_e;
452 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
454 utm->client_bytes_received = 0;
457 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
459 switch (e->event_type)
461 case FIFO_EVENT_SERVER_RX:
462 client_handle_fifo_event_rx (utm, e);
465 case FIFO_EVENT_SERVER_EXIT:
468 clib_warning ("unknown event type %d", e->event_type);
472 if (PREDICT_FALSE (utm->time_to_stop == 1))
480 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
482 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
485 svm_fifo_t *rx_fifo, *tx_fifo;
490 uword *errp = hash_get (utm->error_string_by_error_number, -mp->retval);
491 clib_warning ("connection failed with code: %s", *errp);
492 utm->state = STATE_FAILED;
496 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
497 mp->vpp_event_queue_address;
503 pool_get (utm->sessions, session);
504 session_index = session - utm->sessions;
506 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
507 rx_fifo->client_session_index = session_index;
508 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
509 tx_fifo->client_session_index = session_index;
511 session->server_rx_fifo = rx_fifo;
512 session->server_tx_fifo = tx_fifo;
513 session->vpp_session_handle = mp->handle;
516 utm->connected_session_index = session_index;
517 utm->state = STATE_READY;
519 /* Add it to lookup table */
520 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
522 /* Start RX thread */
523 rv = pthread_create (&utm->client_rx_thread_handle,
524 NULL /*attr */ , client_rx_thread_fn, 0);
527 clib_warning ("pthread_create returned %d", rv);
528 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
533 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
536 u8 *test_data = utm->connect_test_data;
538 int test_buf_offset = 0;
540 u32 queue_max_chunk = 64 << 10, actual_write;
541 session_fifo_event_t evt;
542 static int serial_number = 0;
545 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
546 if (bytes_to_snd > vec_len (test_data))
547 bytes_to_snd = vec_len (test_data);
549 while (bytes_to_snd > 0)
552 bytes_to_snd > queue_max_chunk ? queue_max_chunk : bytes_to_snd;
553 rv = svm_fifo_enqueue_nowait (tx_fifo, mypid, actual_write,
554 test_data + test_buf_offset);
559 test_buf_offset += rv;
562 if (svm_fifo_set_event (tx_fifo))
564 /* Fabricate TX event, send to vpp */
566 evt.event_type = FIFO_EVENT_SERVER_TX;
567 evt.event_id = serial_number++;
569 unix_shared_memory_queue_add (utm->vpp_event_queue,
571 0 /* do wait for mutex */ );
578 client_send_data (uri_tcp_test_main_t * utm)
580 u8 *test_data = utm->connect_test_data;
581 int mypid = getpid ();
584 u32 n_iterations, leftover;
587 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
588 tx_fifo = session->server_tx_fifo;
590 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
591 n_iterations = utm->bytes_to_send / vec_len (test_data);
593 for (i = 0; i < n_iterations; i++)
595 send_test_chunk (utm, tx_fifo, mypid, 0);
598 leftover = utm->bytes_to_send % vec_len (test_data);
600 send_test_chunk (utm, tx_fifo, mypid, leftover);
602 if (utm->test_return_packets)
604 f64 timeout = clib_time_now (&utm->clib_time) + 2;
606 /* Wait for the outstanding packets */
607 while (utm->client_bytes_received <
608 vec_len (test_data) * n_iterations + leftover)
610 if (clib_time_now (&utm->clib_time) > timeout)
612 clib_warning ("timed out waiting for the missing packets");
617 utm->time_to_stop = 1;
621 client_connect (uri_tcp_test_main_t * utm)
623 vl_api_connect_uri_t *cmp;
624 cmp = vl_msg_api_alloc (sizeof (*cmp));
625 memset (cmp, 0, sizeof (*cmp));
627 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
628 cmp->client_index = utm->my_client_index;
629 cmp->context = ntohl (0xfeedface);
630 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
631 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
635 client_disconnect (uri_tcp_test_main_t * utm)
637 session_t *connected_session;
638 vl_api_disconnect_session_t *dmp;
639 connected_session = pool_elt_at_index (utm->sessions,
640 utm->connected_session_index);
641 dmp = vl_msg_api_alloc (sizeof (*dmp));
642 memset (dmp, 0, sizeof (*dmp));
643 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
644 dmp->client_index = utm->my_client_index;
645 dmp->handle = connected_session->vpp_session_handle;
646 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
650 client_test (uri_tcp_test_main_t * utm)
654 application_attach (utm);
655 client_connect (utm);
657 if (wait_for_state_change (utm, STATE_READY))
663 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
664 for (i = 0; i < vec_len (utm->connect_test_data); i++)
665 utm->connect_test_data[i] = i & 0xff;
668 client_send_data (utm);
671 client_disconnect (utm);
673 if (wait_for_state_change (utm, STATE_START))
675 clib_warning ("Disconnect failed");
678 application_detach (utm);
682 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
684 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
688 uword *errp = hash_get (utm->error_string_by_error_number,
689 -clib_net_to_host_u32 (mp->retval));
690 clib_warning ("bind failed: %s", (char *) *errp);
691 utm->state = STATE_FAILED;
695 utm->state = STATE_READY;
699 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
701 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
704 clib_warning ("returned %d", ntohl (mp->retval));
706 utm->state = STATE_START;
710 format_ip4_address (u8 * s, va_list * args)
712 u8 *a = va_arg (*args, u8 *);
713 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
717 format_ip6_address (u8 * s, va_list * args)
719 ip6_address_t *a = va_arg (*args, ip6_address_t *);
720 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
722 i_max_n_zero = ARRAY_LEN (a->as_u16);
724 i_first_zero = i_max_n_zero;
726 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
728 u32 is_zero = a->as_u16[i] == 0;
729 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
735 if ((!is_zero && n_zeros > max_n_zeros)
736 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
738 i_max_n_zero = i_first_zero;
739 max_n_zeros = n_zeros;
740 i_first_zero = ARRAY_LEN (a->as_u16);
745 last_double_colon = 0;
746 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
748 if (i == i_max_n_zero && max_n_zeros > 1)
750 s = format (s, "::");
751 i += max_n_zeros - 1;
752 last_double_colon = 1;
756 s = format (s, "%s%x",
757 (last_double_colon || i == 0) ? "" : ":",
758 clib_net_to_host_u16 (a->as_u16[i]));
759 last_double_colon = 0;
766 /* Format an IP46 address. */
768 format_ip46_address (u8 * s, va_list * args)
770 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
771 ip46_type_t type = va_arg (*args, ip46_type_t);
777 is_ip4 = ip46_address_is_ip4 (ip46);
788 format (s, "%U", format_ip4_address, &ip46->ip4) :
789 format (s, "%U", format_ip6_address, &ip46->ip6);
793 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
795 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
796 vl_api_accept_session_reply_t *rmp;
797 svm_fifo_t *rx_fifo, *tx_fifo;
799 static f64 start_time;
803 if (start_time == 0.0)
804 start_time = clib_time_now (&utm->clib_time);
806 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
807 clib_warning ("Accepted session from: %s:%d", ip_str,
808 clib_net_to_host_u16 (mp->port));
809 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
810 mp->vpp_event_queue_address;
812 /* Allocate local session and set it up */
813 pool_get (utm->sessions, session);
814 session_index = session - utm->sessions;
816 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
817 rx_fifo->client_session_index = session_index;
818 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
819 tx_fifo->client_session_index = session_index;
821 session->server_rx_fifo = rx_fifo;
822 session->server_tx_fifo = tx_fifo;
824 /* Add it to lookup table */
825 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
827 utm->state = STATE_READY;
830 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
832 f64 now = clib_time_now (&utm->clib_time);
833 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
834 pool_elts (utm->sessions), now - start_time,
835 (f64) pool_elts (utm->sessions) / (now - start_time));
839 * Send accept reply to vpp
841 rmp = vl_msg_api_alloc (sizeof (*rmp));
842 memset (rmp, 0, sizeof (*rmp));
843 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
844 rmp->handle = mp->handle;
845 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
849 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
850 session_fifo_event_t * e)
852 svm_fifo_t *rx_fifo, *tx_fifo;
854 session_fifo_event_t evt;
855 unix_shared_memory_queue_t *q;
859 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
861 bytes = svm_fifo_max_dequeue (rx_fifo);
862 /* Allow enqueuing of a new event */
863 svm_fifo_unset_event (rx_fifo);
871 n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (utm->rx_buf),
876 if (utm->drop_packets)
879 /* Reflect if a non-drop session */
884 rv = svm_fifo_enqueue_nowait (tx_fifo, 0, n_read, utm->rx_buf);
886 while (rv <= 0 && !utm->time_to_stop);
888 /* If event wasn't set, add one */
889 if (svm_fifo_set_event (tx_fifo))
891 /* Fabricate TX event, send to vpp */
893 evt.event_type = FIFO_EVENT_SERVER_TX;
894 evt.event_id = e->event_id;
896 q = utm->vpp_event_queue;
897 unix_shared_memory_queue_add (q, (u8 *) & evt,
898 0 /* do wait for mutex */ );
902 while ((n_read < 0 || bytes > 0) && !utm->time_to_stop);
906 server_handle_event_queue (uri_tcp_test_main_t * utm)
908 session_fifo_event_t _e, *e = &_e;;
912 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
914 switch (e->event_type)
916 case FIFO_EVENT_SERVER_RX:
917 server_handle_fifo_event_rx (utm, e);
920 case FIFO_EVENT_SERVER_EXIT:
924 clib_warning ("unknown event type %d", e->event_type);
927 if (PREDICT_FALSE (utm->time_to_stop == 1))
929 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
931 utm->time_to_print_stats = 0;
932 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
938 server_listen (uri_tcp_test_main_t * utm)
940 vl_api_bind_uri_t *bmp;
941 bmp = vl_msg_api_alloc (sizeof (*bmp));
942 memset (bmp, 0, sizeof (*bmp));
944 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
945 bmp->client_index = utm->my_client_index;
946 bmp->context = ntohl (0xfeedface);
947 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
948 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
952 server_unbind (uri_tcp_test_main_t * utm)
954 vl_api_unbind_uri_t *ump;
956 ump = vl_msg_api_alloc (sizeof (*ump));
957 memset (ump, 0, sizeof (*ump));
959 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
960 ump->client_index = utm->my_client_index;
961 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
962 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
966 server_test (uri_tcp_test_main_t * utm)
968 application_attach (utm);
973 if (wait_for_state_change (utm, STATE_READY))
975 clib_warning ("timeout waiting for STATE_READY");
979 /* Enter handle event loop */
980 server_handle_event_queue (utm);
985 if (wait_for_state_change (utm, STATE_START))
987 clib_warning ("timeout waiting for STATE_START");
991 application_detach (utm);
993 fformat (stdout, "Test complete...\n");
997 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1000 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1002 clib_warning ("retval %d", ntohl (mp->retval));
1003 utm->state = STATE_START;
1006 #define foreach_uri_msg \
1007 _(BIND_URI_REPLY, bind_uri_reply) \
1008 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1009 _(ACCEPT_SESSION, accept_session) \
1010 _(CONNECT_URI_REPLY, connect_uri_reply) \
1011 _(DISCONNECT_SESSION, disconnect_session) \
1012 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1013 _(RESET_SESSION, reset_session) \
1014 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1015 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1016 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1019 uri_api_hookup (uri_tcp_test_main_t * utm)
1022 vl_msg_api_set_handlers(VL_API_##N, #n, \
1023 vl_api_##n##_t_handler, \
1025 vl_api_##n##_t_endian, \
1026 vl_api_##n##_t_print, \
1027 sizeof(vl_api_##n##_t), 1);
1033 main (int argc, char **argv)
1035 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1036 unformat_input_t _argv, *a = &_argv;
1039 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1040 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1041 u64 bytes_to_send = 64 << 10, mbytes;
1046 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1048 clib_mem_init (0, 256 << 20);
1050 heap = clib_mem_get_per_cpu_heap ();
1051 h = mheap_header (heap);
1053 /* make the main heap thread-safe */
1054 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1056 vec_validate (utm->rx_buf, 65536);
1058 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1060 utm->my_pid = getpid ();
1061 utm->configured_segment_size = 1 << 20;
1063 clib_time_init (&utm->clib_time);
1064 init_error_string_table (utm);
1065 svm_fifo_segment_init (0x200000000ULL, 20);
1066 unformat_init_command_line (a, argv);
1068 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1070 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1072 vl_set_memory_root_path ((char *) chroot_prefix);
1074 else if (unformat (a, "uri %s", &uri))
1076 else if (unformat (a, "segment-size %dM", &tmp))
1077 utm->configured_segment_size = tmp << 20;
1078 else if (unformat (a, "segment-size %dG", &tmp))
1079 utm->configured_segment_size = tmp << 30;
1080 else if (unformat (a, "master"))
1082 else if (unformat (a, "slave"))
1084 else if (unformat (a, "drop"))
1086 else if (unformat (a, "test"))
1087 test_return_packets = 1;
1088 else if (unformat (a, "mbytes %lld", &mbytes))
1090 bytes_to_send = mbytes << 20;
1092 else if (unformat (a, "gbytes %lld", &mbytes))
1094 bytes_to_send = mbytes << 30;
1098 fformat (stderr, "%s: usage [master|slave]\n");
1105 utm->uri = format (0, "%s%c", uri, 0);
1106 utm->connect_uri = format (0, "%s%c", uri, 0);
1110 utm->uri = format (0, "%s%c", bind_uri, 0);
1111 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1114 utm->i_am_master = i_am_master;
1115 utm->segment_main = &svm_fifo_segment_main;
1116 utm->drop_packets = drop_packets;
1117 utm->test_return_packets = test_return_packets;
1118 utm->bytes_to_send = bytes_to_send;
1120 setup_signal_handlers ();
1121 uri_api_hookup (utm);
1123 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
1126 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1130 if (i_am_master == 0)
1133 vl_client_disconnect_from_vlib ();
1137 /* $$$$ hack preallocation */
1138 for (i = 0; i < 200000; i++)
1140 pool_get (utm->sessions, session);
1141 memset (session, 0, sizeof (*session));
1143 for (i = 0; i < 200000; i++)
1144 pool_put_index (utm->sessions, i);
1148 vl_client_disconnect_from_vlib ();
1153 * fd.io coding-style-patch-verification: ON
1156 * eval: (c-set-style "gnu")