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 u64 vpp_session_handle;
63 unix_shared_memory_queue_t *vl_input_queue;
65 /* API client handle */
68 /* The URI we're playing with */
74 /* Hash table for disconnect processing */
75 uword *session_index_by_vpp_handles;
77 /* intermediate rx buffer */
80 /* URI for slave's connect */
83 u32 connected_session_index;
87 /* drop all packets */
91 unix_shared_memory_queue_t *our_event_queue;
93 /* $$$ single thread only for the moment */
94 unix_shared_memory_queue_t *vpp_event_queue;
98 /* For deadman timers */
99 clib_time_t clib_time;
101 /* State of the connection, shared between msg RX thread and main thread */
102 volatile connection_state_t state;
104 /* Signal variables */
105 volatile int time_to_stop;
106 volatile int time_to_print_stats;
108 u32 configured_segment_size;
110 /* VNET_API_ERROR_FOO -> "Foo" hash table */
111 uword *error_string_by_error_number;
113 u8 *connect_test_data;
114 pthread_t client_rx_thread_handle;
115 u32 client_bytes_received;
116 u8 test_return_packets;
120 svm_fifo_segment_main_t *segment_main;
121 } uri_tcp_test_main_t;
123 uri_tcp_test_main_t uri_tcp_test_main;
128 #define NITER 4000000
132 format_api_error (u8 * s, va_list * args)
134 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
135 i32 error = va_arg (*args, u32);
138 p = hash_get (utm->error_string_by_error_number, -error);
141 s = format (s, "%s", p[0]);
143 s = format (s, "%d", error);
148 init_error_string_table (uri_tcp_test_main_t * utm)
150 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
152 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
153 foreach_vnet_api_error;
156 hash_set (utm->error_string_by_error_number, 99, "Misc");
160 wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
163 #define TIMEOUT 600.0
165 #define TIMEOUT 600.0
168 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
170 while (clib_time_now (&utm->clib_time) < timeout)
172 if (utm->state == state)
174 if (utm->state == STATE_FAILED)
177 clib_warning ("timeout waiting for STATE_READY");
182 application_send_attach (uri_tcp_test_main_t * utm)
184 vl_api_application_attach_t *bmp;
185 u32 fifo_size = 3 << 20;
186 bmp = vl_msg_api_alloc (sizeof (*bmp));
187 memset (bmp, 0, sizeof (*bmp));
189 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
190 bmp->client_index = utm->my_client_index;
191 bmp->context = ntohl (0xfeedface);
192 bmp->options[APP_OPTIONS_FLAGS] =
193 APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
194 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
195 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
196 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
197 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
198 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
202 application_attach (uri_tcp_test_main_t * utm)
204 application_send_attach (utm);
205 if (wait_for_state_change (utm, STATE_ATTACHED))
207 clib_warning ("timeout waiting for STATE_ATTACHED");
214 application_detach (uri_tcp_test_main_t * utm)
216 vl_api_application_detach_t *bmp;
217 bmp = vl_msg_api_alloc (sizeof (*bmp));
218 memset (bmp, 0, sizeof (*bmp));
220 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
221 bmp->client_index = utm->my_client_index;
222 bmp->context = ntohl (0xfeedface);
223 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
227 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
230 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
231 svm_fifo_segment_create_args_t _a, *a = &_a;
236 clib_warning ("attach failed: %U", format_api_error,
237 clib_net_to_host_u32 (mp->retval));
238 utm->state = STATE_FAILED;
242 if (mp->segment_name_length == 0)
244 clib_warning ("segment_name_length zero");
248 a->segment_name = (char *) mp->segment_name;
249 a->segment_size = mp->segment_size;
251 ASSERT (mp->app_event_queue_address);
253 /* Attach to the segment vpp created */
254 rv = svm_fifo_segment_attach (a);
257 clib_warning ("svm_fifo_segment_attach ('%s') failed",
262 utm->our_event_queue =
263 (unix_shared_memory_queue_t *) mp->app_event_queue_address;
264 utm->state = STATE_ATTACHED;
268 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
272 clib_warning ("detach returned with err: %d", mp->retval);
276 stop_signal (int signum)
278 uri_tcp_test_main_t *um = &uri_tcp_test_main;
280 um->time_to_stop = 1;
284 stats_signal (int signum)
286 uri_tcp_test_main_t *um = &uri_tcp_test_main;
288 um->time_to_print_stats = 1;
291 static clib_error_t *
292 setup_signal_handlers (void)
294 signal (SIGINT, stats_signal);
295 signal (SIGQUIT, stop_signal);
296 signal (SIGTERM, stop_signal);
302 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
304 clib_warning ("BUG");
308 connect_to_vpp (char *name)
310 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
311 api_main_t *am = &api_main;
313 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
316 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
317 utm->my_client_index = am->my_client_index;
323 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
325 svm_fifo_segment_create_args_t _a, *a = &_a;
328 a->segment_name = (char *) mp->segment_name;
329 a->segment_size = mp->segment_size;
330 /* Attach to the segment vpp created */
331 rv = svm_fifo_segment_attach (a);
334 clib_warning ("svm_fifo_segment_attach ('%s') failed",
338 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
343 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
345 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
347 vl_api_disconnect_session_reply_t *rmp;
351 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
355 session = pool_elt_at_index (utm->sessions, p[0]);
356 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
357 pool_put (utm->sessions, session);
361 clib_warning ("couldn't find session key %llx", mp->handle);
365 utm->time_to_stop = 1;
367 rmp = vl_msg_api_alloc (sizeof (*rmp));
368 memset (rmp, 0, sizeof (*rmp));
370 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
372 rmp->handle = mp->handle;
373 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
377 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
379 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
381 vl_api_reset_session_reply_t *rmp;
385 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
389 session = pool_elt_at_index (utm->sessions, p[0]);
390 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
391 pool_put (utm->sessions, session);
392 utm->time_to_stop = 1;
396 clib_warning ("couldn't find session key %llx", mp->handle);
400 rmp = vl_msg_api_alloc (sizeof (*rmp));
401 memset (rmp, 0, sizeof (*rmp));
402 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
404 rmp->handle = mp->handle;
405 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
409 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
410 session_fifo_event_t * e)
413 int n_read, bytes, i;
417 bytes = svm_fifo_max_dequeue (rx_fifo);
418 /* Allow enqueuing of new event */
419 svm_fifo_unset_event (rx_fifo);
424 n_read = svm_fifo_dequeue_nowait (rx_fifo,
425 clib_min (vec_len (utm->rx_buf),
426 bytes), utm->rx_buf);
430 for (i = 0; i < n_read; i++)
432 if (utm->rx_buf[i] != ((utm->client_bytes_received + i) & 0xff))
434 clib_warning ("error at byte %lld, 0x%x not 0x%x",
435 utm->client_bytes_received + i,
437 ((utm->client_bytes_received + i) & 0xff));
440 utm->client_bytes_received += n_read;
446 // clib_warning ("weird!");
456 client_handle_event_queue (uri_tcp_test_main_t * utm)
458 session_fifo_event_t _e, *e = &_e;;
460 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
462 switch (e->event_type)
464 case FIFO_EVENT_APP_RX:
465 client_handle_fifo_event_rx (utm, e);
468 case FIFO_EVENT_DISCONNECT:
472 clib_warning ("unknown event type %d", e->event_type);
478 client_rx_thread_fn (void *arg)
480 session_fifo_event_t _e, *e = &_e;
481 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
483 utm->client_bytes_received = 0;
486 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
488 switch (e->event_type)
490 case FIFO_EVENT_APP_RX:
491 client_handle_fifo_event_rx (utm, e);
494 case FIFO_EVENT_DISCONNECT:
497 clib_warning ("unknown event type %d", e->event_type);
501 if (PREDICT_FALSE (utm->time_to_stop == 1))
509 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
511 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
514 svm_fifo_t *rx_fifo, *tx_fifo;
519 clib_warning ("connection failed with code: %U", format_api_error,
520 clib_net_to_host_u32 (mp->retval));
521 utm->state = STATE_FAILED;
525 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
526 mp->vpp_event_queue_address;
532 pool_get (utm->sessions, session);
533 session_index = session - utm->sessions;
535 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
536 rx_fifo->client_session_index = session_index;
537 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
538 tx_fifo->client_session_index = session_index;
540 session->server_rx_fifo = rx_fifo;
541 session->server_tx_fifo = tx_fifo;
542 session->vpp_session_handle = mp->handle;
545 utm->connected_session_index = session_index;
546 utm->state = STATE_READY;
548 /* Add it to lookup table */
549 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
551 /* Start RX thread */
552 rv = pthread_create (&utm->client_rx_thread_handle,
553 NULL /*attr */ , client_rx_thread_fn, 0);
556 clib_warning ("pthread_create returned %d", rv);
557 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
562 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
565 u8 *test_data = utm->connect_test_data;
567 int test_buf_offset = 0;
569 u32 queue_max_chunk = 64 << 10, actual_write;
570 session_fifo_event_t evt;
571 static int serial_number = 0;
574 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
575 if (bytes_to_snd > vec_len (test_data))
576 bytes_to_snd = vec_len (test_data);
578 while (bytes_to_snd > 0)
581 bytes_to_snd > queue_max_chunk ? queue_max_chunk : bytes_to_snd;
582 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
583 test_data + test_buf_offset);
588 test_buf_offset += rv;
591 if (svm_fifo_set_event (tx_fifo))
593 /* Fabricate TX event, send to vpp */
595 evt.event_type = FIFO_EVENT_APP_TX;
596 evt.event_id = serial_number++;
598 unix_shared_memory_queue_add (utm->vpp_event_queue,
600 0 /* do wait for mutex */ );
607 client_send_data (uri_tcp_test_main_t * utm)
609 u8 *test_data = utm->connect_test_data;
610 int mypid = getpid ();
613 u32 n_iterations, leftover;
616 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
617 tx_fifo = session->server_tx_fifo;
619 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
620 n_iterations = utm->bytes_to_send / vec_len (test_data);
622 for (i = 0; i < n_iterations; i++)
624 send_test_chunk (utm, tx_fifo, mypid, 0);
627 leftover = utm->bytes_to_send % vec_len (test_data);
629 send_test_chunk (utm, tx_fifo, mypid, leftover);
631 if (utm->test_return_packets)
633 f64 timeout = clib_time_now (&utm->clib_time) + 2;
635 /* Wait for the outstanding packets */
636 while (utm->client_bytes_received <
637 vec_len (test_data) * n_iterations + leftover)
639 if (clib_time_now (&utm->clib_time) > timeout)
641 clib_warning ("timed out waiting for the missing packets");
646 utm->time_to_stop = 1;
650 client_send_connect (uri_tcp_test_main_t * utm)
652 vl_api_connect_uri_t *cmp;
653 cmp = vl_msg_api_alloc (sizeof (*cmp));
654 memset (cmp, 0, sizeof (*cmp));
656 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
657 cmp->client_index = utm->my_client_index;
658 cmp->context = ntohl (0xfeedface);
659 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
660 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
664 client_connect (uri_tcp_test_main_t * utm)
666 client_send_connect (utm);
667 if (wait_for_state_change (utm, STATE_READY))
669 clib_warning ("Connect failed");
676 client_send_disconnect (uri_tcp_test_main_t * utm)
678 session_t *connected_session;
679 vl_api_disconnect_session_t *dmp;
680 connected_session = pool_elt_at_index (utm->sessions,
681 utm->connected_session_index);
682 dmp = vl_msg_api_alloc (sizeof (*dmp));
683 memset (dmp, 0, sizeof (*dmp));
684 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
685 dmp->client_index = utm->my_client_index;
686 dmp->handle = connected_session->vpp_session_handle;
687 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
691 client_disconnect (uri_tcp_test_main_t * utm)
693 client_send_disconnect (utm);
694 if (wait_for_state_change (utm, STATE_START))
696 clib_warning ("Disconnect failed");
703 client_test (uri_tcp_test_main_t * utm)
707 if (application_attach (utm))
710 if (client_connect (utm))
712 application_detach (utm);
717 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
718 for (i = 0; i < vec_len (utm->connect_test_data); i++)
719 utm->connect_test_data[i] = i & 0xff;
722 client_send_data (utm);
725 client_disconnect (utm);
727 application_detach (utm);
731 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
733 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
737 clib_warning ("bind failed: %s", format_api_error,
738 clib_net_to_host_u32 (mp->retval));
739 utm->state = STATE_FAILED;
743 utm->state = STATE_READY;
747 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
749 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
752 clib_warning ("returned %d", ntohl (mp->retval));
754 utm->state = STATE_START;
758 format_ip4_address (u8 * s, va_list * args)
760 u8 *a = va_arg (*args, u8 *);
761 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
765 format_ip6_address (u8 * s, va_list * args)
767 ip6_address_t *a = va_arg (*args, ip6_address_t *);
768 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
770 i_max_n_zero = ARRAY_LEN (a->as_u16);
772 i_first_zero = i_max_n_zero;
774 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
776 u32 is_zero = a->as_u16[i] == 0;
777 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
783 if ((!is_zero && n_zeros > max_n_zeros)
784 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
786 i_max_n_zero = i_first_zero;
787 max_n_zeros = n_zeros;
788 i_first_zero = ARRAY_LEN (a->as_u16);
793 last_double_colon = 0;
794 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
796 if (i == i_max_n_zero && max_n_zeros > 1)
798 s = format (s, "::");
799 i += max_n_zeros - 1;
800 last_double_colon = 1;
804 s = format (s, "%s%x",
805 (last_double_colon || i == 0) ? "" : ":",
806 clib_net_to_host_u16 (a->as_u16[i]));
807 last_double_colon = 0;
814 /* Format an IP46 address. */
816 format_ip46_address (u8 * s, va_list * args)
818 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
819 ip46_type_t type = va_arg (*args, ip46_type_t);
825 is_ip4 = ip46_address_is_ip4 (ip46);
836 format (s, "%U", format_ip4_address, &ip46->ip4) :
837 format (s, "%U", format_ip6_address, &ip46->ip6);
841 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
843 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
844 vl_api_accept_session_reply_t *rmp;
845 svm_fifo_t *rx_fifo, *tx_fifo;
847 static f64 start_time;
851 if (start_time == 0.0)
852 start_time = clib_time_now (&utm->clib_time);
854 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
855 clib_warning ("Accepted session from: %s:%d", ip_str,
856 clib_net_to_host_u16 (mp->port));
857 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
858 mp->vpp_event_queue_address;
860 /* Allocate local session and set it up */
861 pool_get (utm->sessions, session);
862 session_index = session - utm->sessions;
864 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
865 rx_fifo->client_session_index = session_index;
866 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
867 tx_fifo->client_session_index = session_index;
869 session->server_rx_fifo = rx_fifo;
870 session->server_tx_fifo = tx_fifo;
872 /* Add it to lookup table */
873 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
875 utm->state = STATE_READY;
878 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
880 f64 now = clib_time_now (&utm->clib_time);
881 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
882 pool_elts (utm->sessions), now - start_time,
883 (f64) pool_elts (utm->sessions) / (now - start_time));
887 * Send accept reply to vpp
889 rmp = vl_msg_api_alloc (sizeof (*rmp));
890 memset (rmp, 0, sizeof (*rmp));
891 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
892 rmp->handle = mp->handle;
893 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
897 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
898 session_fifo_event_t * e)
900 svm_fifo_t *rx_fifo, *tx_fifo;
902 session_fifo_event_t evt;
903 unix_shared_memory_queue_t *q;
907 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
909 bytes = svm_fifo_max_dequeue (rx_fifo);
910 /* Allow enqueuing of a new event */
911 svm_fifo_unset_event (rx_fifo);
919 n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
924 if (utm->drop_packets)
927 /* Reflect if a non-drop session */
932 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read, utm->rx_buf);
934 while (rv <= 0 && !utm->time_to_stop);
936 /* If event wasn't set, add one */
937 if (svm_fifo_set_event (tx_fifo))
939 /* Fabricate TX event, send to vpp */
941 evt.event_type = FIFO_EVENT_APP_TX;
942 evt.event_id = e->event_id;
944 q = utm->vpp_event_queue;
945 unix_shared_memory_queue_add (q, (u8 *) & evt,
946 0 /* do wait for mutex */ );
950 while ((n_read < 0 || bytes > 0) && !utm->time_to_stop);
954 server_handle_event_queue (uri_tcp_test_main_t * utm)
956 session_fifo_event_t _e, *e = &_e;;
960 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
962 switch (e->event_type)
964 case FIFO_EVENT_APP_RX:
965 server_handle_fifo_event_rx (utm, e);
968 case FIFO_EVENT_DISCONNECT:
972 clib_warning ("unknown event type %d", e->event_type);
975 if (PREDICT_FALSE (utm->time_to_stop == 1))
977 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
979 utm->time_to_print_stats = 0;
980 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
986 server_send_listen (uri_tcp_test_main_t * utm)
988 vl_api_bind_uri_t *bmp;
989 bmp = vl_msg_api_alloc (sizeof (*bmp));
990 memset (bmp, 0, sizeof (*bmp));
992 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
993 bmp->client_index = utm->my_client_index;
994 bmp->context = ntohl (0xfeedface);
995 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
996 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1000 server_listen (uri_tcp_test_main_t * utm)
1002 server_send_listen (utm);
1003 if (wait_for_state_change (utm, STATE_READY))
1005 clib_warning ("timeout waiting for STATE_READY");
1012 server_send_unbind (uri_tcp_test_main_t * utm)
1014 vl_api_unbind_uri_t *ump;
1016 ump = vl_msg_api_alloc (sizeof (*ump));
1017 memset (ump, 0, sizeof (*ump));
1019 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1020 ump->client_index = utm->my_client_index;
1021 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1022 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1026 server_unbind (uri_tcp_test_main_t * utm)
1028 server_send_unbind (utm);
1029 if (wait_for_state_change (utm, STATE_START))
1031 clib_warning ("timeout waiting for STATE_START");
1038 server_test (uri_tcp_test_main_t * utm)
1040 if (application_attach (utm))
1044 if (server_listen (utm))
1047 /* Enter handle event loop */
1048 server_handle_event_queue (utm);
1051 server_send_unbind (utm);
1053 application_detach (utm);
1055 fformat (stdout, "Test complete...\n");
1059 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1062 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1064 clib_warning ("retval %d", ntohl (mp->retval));
1065 utm->state = STATE_START;
1068 #define foreach_uri_msg \
1069 _(BIND_URI_REPLY, bind_uri_reply) \
1070 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1071 _(ACCEPT_SESSION, accept_session) \
1072 _(CONNECT_URI_REPLY, connect_uri_reply) \
1073 _(DISCONNECT_SESSION, disconnect_session) \
1074 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1075 _(RESET_SESSION, reset_session) \
1076 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1077 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1078 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1081 uri_api_hookup (uri_tcp_test_main_t * utm)
1084 vl_msg_api_set_handlers(VL_API_##N, #n, \
1085 vl_api_##n##_t_handler, \
1087 vl_api_##n##_t_endian, \
1088 vl_api_##n##_t_print, \
1089 sizeof(vl_api_##n##_t), 1);
1095 main (int argc, char **argv)
1097 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1098 unformat_input_t _argv, *a = &_argv;
1101 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1102 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1103 u64 bytes_to_send = 64 << 10, mbytes;
1108 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1110 clib_mem_init (0, 256 << 20);
1112 heap = clib_mem_get_per_cpu_heap ();
1113 h = mheap_header (heap);
1115 /* make the main heap thread-safe */
1116 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1118 vec_validate (utm->rx_buf, 65536);
1120 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1122 utm->my_pid = getpid ();
1123 utm->configured_segment_size = 1 << 20;
1125 clib_time_init (&utm->clib_time);
1126 init_error_string_table (utm);
1127 svm_fifo_segment_init (0x200000000ULL, 20);
1128 unformat_init_command_line (a, argv);
1130 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1132 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1134 vl_set_memory_root_path ((char *) chroot_prefix);
1136 else if (unformat (a, "uri %s", &uri))
1138 else if (unformat (a, "segment-size %dM", &tmp))
1139 utm->configured_segment_size = tmp << 20;
1140 else if (unformat (a, "segment-size %dG", &tmp))
1141 utm->configured_segment_size = tmp << 30;
1142 else if (unformat (a, "master"))
1144 else if (unformat (a, "slave"))
1146 else if (unformat (a, "drop"))
1148 else if (unformat (a, "test"))
1149 test_return_packets = 1;
1150 else if (unformat (a, "mbytes %lld", &mbytes))
1152 bytes_to_send = mbytes << 20;
1154 else if (unformat (a, "gbytes %lld", &mbytes))
1156 bytes_to_send = mbytes << 30;
1160 fformat (stderr, "%s: usage [master|slave]\n");
1167 utm->uri = format (0, "%s%c", uri, 0);
1168 utm->connect_uri = format (0, "%s%c", uri, 0);
1172 utm->uri = format (0, "%s%c", bind_uri, 0);
1173 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1176 utm->i_am_master = i_am_master;
1177 utm->segment_main = &svm_fifo_segment_main;
1178 utm->drop_packets = drop_packets;
1179 utm->test_return_packets = test_return_packets;
1180 utm->bytes_to_send = bytes_to_send;
1182 setup_signal_handlers ();
1183 uri_api_hookup (utm);
1185 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
1188 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1192 if (i_am_master == 0)
1195 vl_client_disconnect_from_vlib ();
1199 /* $$$$ hack preallocation */
1200 for (i = 0; i < 200000; i++)
1202 pool_get (utm->sessions, session);
1203 memset (session, 0, sizeof (*session));
1205 for (i = 0; i < 200000; i++)
1206 pool_put_index (utm->sessions, i);
1210 vl_client_disconnect_from_vlib ();
1215 * fd.io coding-style-patch-verification: ON
1218 * eval: (c-set-style "gnu")