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)
176 if (utm->time_to_stop == 1)
179 clib_warning ("timeout waiting for STATE_READY");
184 application_send_attach (uri_tcp_test_main_t * utm)
186 vl_api_application_attach_t *bmp;
187 u32 fifo_size = 3 << 20;
188 bmp = vl_msg_api_alloc (sizeof (*bmp));
189 memset (bmp, 0, sizeof (*bmp));
191 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
192 bmp->client_index = utm->my_client_index;
193 bmp->context = ntohl (0xfeedface);
194 bmp->options[APP_OPTIONS_FLAGS] =
195 APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
196 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
197 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
198 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
199 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
200 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
204 application_attach (uri_tcp_test_main_t * utm)
206 application_send_attach (utm);
207 if (wait_for_state_change (utm, STATE_ATTACHED))
209 clib_warning ("timeout waiting for STATE_ATTACHED");
216 application_detach (uri_tcp_test_main_t * utm)
218 vl_api_application_detach_t *bmp;
219 bmp = vl_msg_api_alloc (sizeof (*bmp));
220 memset (bmp, 0, sizeof (*bmp));
222 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
223 bmp->client_index = utm->my_client_index;
224 bmp->context = ntohl (0xfeedface);
225 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
229 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
232 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
233 svm_fifo_segment_create_args_t _a, *a = &_a;
238 clib_warning ("attach failed: %U", format_api_error,
239 clib_net_to_host_u32 (mp->retval));
240 utm->state = STATE_FAILED;
244 if (mp->segment_name_length == 0)
246 clib_warning ("segment_name_length zero");
250 a->segment_name = (char *) mp->segment_name;
251 a->segment_size = mp->segment_size;
253 ASSERT (mp->app_event_queue_address);
255 /* Attach to the segment vpp created */
256 rv = svm_fifo_segment_attach (a);
259 clib_warning ("svm_fifo_segment_attach ('%s') failed",
264 utm->our_event_queue =
265 (unix_shared_memory_queue_t *) mp->app_event_queue_address;
266 utm->state = STATE_ATTACHED;
270 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
274 clib_warning ("detach returned with err: %d", mp->retval);
278 stop_signal (int signum)
280 uri_tcp_test_main_t *um = &uri_tcp_test_main;
282 um->time_to_stop = 1;
286 stats_signal (int signum)
288 uri_tcp_test_main_t *um = &uri_tcp_test_main;
290 um->time_to_print_stats = 1;
293 static clib_error_t *
294 setup_signal_handlers (void)
296 signal (SIGINT, stats_signal);
297 signal (SIGQUIT, stop_signal);
298 signal (SIGTERM, stop_signal);
304 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
306 clib_warning ("BUG");
310 connect_to_vpp (char *name)
312 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
313 api_main_t *am = &api_main;
315 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
318 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
319 utm->my_client_index = am->my_client_index;
325 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
327 svm_fifo_segment_create_args_t _a, *a = &_a;
330 a->segment_name = (char *) mp->segment_name;
331 a->segment_size = mp->segment_size;
332 /* Attach to the segment vpp created */
333 rv = svm_fifo_segment_attach (a);
336 clib_warning ("svm_fifo_segment_attach ('%s') failed",
340 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
345 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
347 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
349 vl_api_disconnect_session_reply_t *rmp;
353 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
357 session = pool_elt_at_index (utm->sessions, p[0]);
358 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
359 pool_put (utm->sessions, session);
363 clib_warning ("couldn't find session key %llx", mp->handle);
367 utm->time_to_stop = 1;
369 rmp = vl_msg_api_alloc (sizeof (*rmp));
370 memset (rmp, 0, sizeof (*rmp));
372 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
374 rmp->handle = mp->handle;
375 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
379 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
381 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
383 vl_api_reset_session_reply_t *rmp;
387 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
391 session = pool_elt_at_index (utm->sessions, p[0]);
392 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
393 pool_put (utm->sessions, session);
394 utm->time_to_stop = 1;
398 clib_warning ("couldn't find session key %llx", mp->handle);
402 rmp = vl_msg_api_alloc (sizeof (*rmp));
403 memset (rmp, 0, sizeof (*rmp));
404 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
406 rmp->handle = mp->handle;
407 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
411 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
412 session_fifo_event_t * e)
415 int n_read, bytes, i;
419 bytes = svm_fifo_max_dequeue (rx_fifo);
420 /* Allow enqueuing of new event */
421 svm_fifo_unset_event (rx_fifo);
426 n_read = svm_fifo_dequeue_nowait (rx_fifo,
427 clib_min (vec_len (utm->rx_buf),
428 bytes), utm->rx_buf);
432 for (i = 0; i < n_read; i++)
434 if (utm->rx_buf[i] != ((utm->client_bytes_received + i) & 0xff))
436 clib_warning ("error at byte %lld, 0x%x not 0x%x",
437 utm->client_bytes_received + i,
439 ((utm->client_bytes_received + i) & 0xff));
442 utm->client_bytes_received += n_read;
448 // clib_warning ("weird!");
458 client_handle_event_queue (uri_tcp_test_main_t * utm)
460 session_fifo_event_t _e, *e = &_e;;
462 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
464 switch (e->event_type)
466 case FIFO_EVENT_APP_RX:
467 client_handle_fifo_event_rx (utm, e);
470 case FIFO_EVENT_DISCONNECT:
474 clib_warning ("unknown event type %d", e->event_type);
480 client_rx_thread_fn (void *arg)
482 session_fifo_event_t _e, *e = &_e;
483 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
485 utm->client_bytes_received = 0;
488 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
490 switch (e->event_type)
492 case FIFO_EVENT_APP_RX:
493 client_handle_fifo_event_rx (utm, e);
496 case FIFO_EVENT_DISCONNECT:
499 clib_warning ("unknown event type %d", e->event_type);
503 if (PREDICT_FALSE (utm->time_to_stop == 1))
511 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
513 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
516 svm_fifo_t *rx_fifo, *tx_fifo;
521 clib_warning ("connection failed with code: %U", format_api_error,
522 clib_net_to_host_u32 (mp->retval));
523 utm->state = STATE_FAILED;
527 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
528 mp->vpp_event_queue_address;
534 pool_get (utm->sessions, session);
535 session_index = session - utm->sessions;
537 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
538 rx_fifo->client_session_index = session_index;
539 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
540 tx_fifo->client_session_index = session_index;
542 session->server_rx_fifo = rx_fifo;
543 session->server_tx_fifo = tx_fifo;
544 session->vpp_session_handle = mp->handle;
547 utm->connected_session_index = session_index;
548 utm->state = STATE_READY;
550 /* Add it to lookup table */
551 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
553 /* Start RX thread */
554 rv = pthread_create (&utm->client_rx_thread_handle,
555 NULL /*attr */ , client_rx_thread_fn, 0);
558 clib_warning ("pthread_create returned %d", rv);
559 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
564 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
567 u8 *test_data = utm->connect_test_data;
569 int test_buf_offset = 0;
571 u32 queue_max_chunk = 64 << 10, actual_write;
572 session_fifo_event_t evt;
573 static int serial_number = 0;
576 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
577 if (bytes_to_snd > vec_len (test_data))
578 bytes_to_snd = vec_len (test_data);
580 while (bytes_to_snd > 0)
583 bytes_to_snd > queue_max_chunk ? queue_max_chunk : bytes_to_snd;
584 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
585 test_data + test_buf_offset);
590 test_buf_offset += rv;
593 if (svm_fifo_set_event (tx_fifo))
595 /* Fabricate TX event, send to vpp */
597 evt.event_type = FIFO_EVENT_APP_TX;
598 evt.event_id = serial_number++;
600 unix_shared_memory_queue_add (utm->vpp_event_queue,
602 0 /* do wait for mutex */ );
609 client_send_data (uri_tcp_test_main_t * utm)
611 u8 *test_data = utm->connect_test_data;
612 int mypid = getpid ();
615 u32 n_iterations, leftover;
618 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
619 tx_fifo = session->server_tx_fifo;
621 ASSERT (vec_len (test_data) > 0);
623 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
624 n_iterations = utm->bytes_to_send / vec_len (test_data);
626 for (i = 0; i < n_iterations; i++)
628 send_test_chunk (utm, tx_fifo, mypid, 0);
631 leftover = utm->bytes_to_send % vec_len (test_data);
633 send_test_chunk (utm, tx_fifo, mypid, leftover);
635 if (utm->test_return_packets)
637 f64 timeout = clib_time_now (&utm->clib_time) + 2;
639 /* Wait for the outstanding packets */
640 while (utm->client_bytes_received <
641 vec_len (test_data) * n_iterations + leftover)
643 if (clib_time_now (&utm->clib_time) > timeout)
645 clib_warning ("timed out waiting for the missing packets");
650 utm->time_to_stop = 1;
654 client_send_connect (uri_tcp_test_main_t * utm)
656 vl_api_connect_uri_t *cmp;
657 cmp = vl_msg_api_alloc (sizeof (*cmp));
658 memset (cmp, 0, sizeof (*cmp));
660 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
661 cmp->client_index = utm->my_client_index;
662 cmp->context = ntohl (0xfeedface);
663 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
664 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
668 client_connect (uri_tcp_test_main_t * utm)
670 client_send_connect (utm);
671 if (wait_for_state_change (utm, STATE_READY))
673 clib_warning ("Connect failed");
680 client_send_disconnect (uri_tcp_test_main_t * utm)
682 session_t *connected_session;
683 vl_api_disconnect_session_t *dmp;
684 connected_session = pool_elt_at_index (utm->sessions,
685 utm->connected_session_index);
686 dmp = vl_msg_api_alloc (sizeof (*dmp));
687 memset (dmp, 0, sizeof (*dmp));
688 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
689 dmp->client_index = utm->my_client_index;
690 dmp->handle = connected_session->vpp_session_handle;
691 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
695 client_disconnect (uri_tcp_test_main_t * utm)
697 client_send_disconnect (utm);
698 if (wait_for_state_change (utm, STATE_START))
700 clib_warning ("Disconnect failed");
707 client_test (uri_tcp_test_main_t * utm)
711 if (application_attach (utm))
714 if (client_connect (utm))
716 application_detach (utm);
721 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
722 for (i = 0; i < vec_len (utm->connect_test_data); i++)
723 utm->connect_test_data[i] = i & 0xff;
726 client_send_data (utm);
729 client_disconnect (utm);
731 application_detach (utm);
735 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
737 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
741 clib_warning ("bind failed: %U", format_api_error,
742 clib_net_to_host_u32 (mp->retval));
743 utm->state = STATE_FAILED;
747 utm->state = STATE_READY;
751 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
753 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
756 clib_warning ("returned %d", ntohl (mp->retval));
758 utm->state = STATE_START;
762 format_ip4_address (u8 * s, va_list * args)
764 u8 *a = va_arg (*args, u8 *);
765 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
769 format_ip6_address (u8 * s, va_list * args)
771 ip6_address_t *a = va_arg (*args, ip6_address_t *);
772 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
774 i_max_n_zero = ARRAY_LEN (a->as_u16);
776 i_first_zero = i_max_n_zero;
778 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
780 u32 is_zero = a->as_u16[i] == 0;
781 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
787 if ((!is_zero && n_zeros > max_n_zeros)
788 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
790 i_max_n_zero = i_first_zero;
791 max_n_zeros = n_zeros;
792 i_first_zero = ARRAY_LEN (a->as_u16);
797 last_double_colon = 0;
798 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
800 if (i == i_max_n_zero && max_n_zeros > 1)
802 s = format (s, "::");
803 i += max_n_zeros - 1;
804 last_double_colon = 1;
808 s = format (s, "%s%x",
809 (last_double_colon || i == 0) ? "" : ":",
810 clib_net_to_host_u16 (a->as_u16[i]));
811 last_double_colon = 0;
818 /* Format an IP46 address. */
820 format_ip46_address (u8 * s, va_list * args)
822 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
823 ip46_type_t type = va_arg (*args, ip46_type_t);
829 is_ip4 = ip46_address_is_ip4 (ip46);
840 format (s, "%U", format_ip4_address, &ip46->ip4) :
841 format (s, "%U", format_ip6_address, &ip46->ip6);
845 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
847 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
848 vl_api_accept_session_reply_t *rmp;
849 svm_fifo_t *rx_fifo, *tx_fifo;
851 static f64 start_time;
855 if (start_time == 0.0)
856 start_time = clib_time_now (&utm->clib_time);
858 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
859 clib_warning ("Accepted session from: %s:%d", ip_str,
860 clib_net_to_host_u16 (mp->port));
861 utm->vpp_event_queue = (unix_shared_memory_queue_t *)
862 mp->vpp_event_queue_address;
864 /* Allocate local session and set it up */
865 pool_get (utm->sessions, session);
866 session_index = session - utm->sessions;
868 rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
869 rx_fifo->client_session_index = session_index;
870 tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
871 tx_fifo->client_session_index = session_index;
873 session->server_rx_fifo = rx_fifo;
874 session->server_tx_fifo = tx_fifo;
876 /* Add it to lookup table */
877 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
879 utm->state = STATE_READY;
882 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
884 f64 now = clib_time_now (&utm->clib_time);
885 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
886 pool_elts (utm->sessions), now - start_time,
887 (f64) pool_elts (utm->sessions) / (now - start_time));
891 * Send accept reply to vpp
893 rmp = vl_msg_api_alloc (sizeof (*rmp));
894 memset (rmp, 0, sizeof (*rmp));
895 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
896 rmp->handle = mp->handle;
897 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
901 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
902 session_fifo_event_t * e)
904 svm_fifo_t *rx_fifo, *tx_fifo;
906 session_fifo_event_t evt;
907 unix_shared_memory_queue_t *q;
911 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
913 bytes = svm_fifo_max_dequeue (rx_fifo);
914 /* Allow enqueuing of a new event */
915 svm_fifo_unset_event (rx_fifo);
923 n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
928 if (utm->drop_packets)
931 /* Reflect if a non-drop session */
936 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read, utm->rx_buf);
938 while (rv <= 0 && !utm->time_to_stop);
940 /* If event wasn't set, add one */
941 if (svm_fifo_set_event (tx_fifo))
943 /* Fabricate TX event, send to vpp */
945 evt.event_type = FIFO_EVENT_APP_TX;
946 evt.event_id = e->event_id;
948 q = utm->vpp_event_queue;
949 unix_shared_memory_queue_add (q, (u8 *) & evt,
950 0 /* do wait for mutex */ );
954 while ((n_read < 0 || bytes > 0) && !utm->time_to_stop);
958 server_handle_event_queue (uri_tcp_test_main_t * utm)
960 session_fifo_event_t _e, *e = &_e;;
964 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
966 switch (e->event_type)
968 case FIFO_EVENT_APP_RX:
969 server_handle_fifo_event_rx (utm, e);
972 case FIFO_EVENT_DISCONNECT:
976 clib_warning ("unknown event type %d", e->event_type);
979 if (PREDICT_FALSE (utm->time_to_stop == 1))
981 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
983 utm->time_to_print_stats = 0;
984 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
990 server_send_listen (uri_tcp_test_main_t * utm)
992 vl_api_bind_uri_t *bmp;
993 bmp = vl_msg_api_alloc (sizeof (*bmp));
994 memset (bmp, 0, sizeof (*bmp));
996 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
997 bmp->client_index = utm->my_client_index;
998 bmp->context = ntohl (0xfeedface);
999 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1000 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1004 server_listen (uri_tcp_test_main_t * utm)
1006 server_send_listen (utm);
1007 if (wait_for_state_change (utm, STATE_READY))
1009 clib_warning ("timeout waiting for STATE_READY");
1016 server_send_unbind (uri_tcp_test_main_t * utm)
1018 vl_api_unbind_uri_t *ump;
1020 ump = vl_msg_api_alloc (sizeof (*ump));
1021 memset (ump, 0, sizeof (*ump));
1023 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1024 ump->client_index = utm->my_client_index;
1025 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1026 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1030 server_unbind (uri_tcp_test_main_t * utm)
1032 server_send_unbind (utm);
1033 if (wait_for_state_change (utm, STATE_START))
1035 clib_warning ("timeout waiting for STATE_START");
1042 server_test (uri_tcp_test_main_t * utm)
1044 if (application_attach (utm))
1048 if (server_listen (utm))
1051 /* Enter handle event loop */
1052 server_handle_event_queue (utm);
1055 server_send_unbind (utm);
1057 application_detach (utm);
1059 fformat (stdout, "Test complete...\n");
1063 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1066 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1068 clib_warning ("retval %d", ntohl (mp->retval));
1069 utm->state = STATE_START;
1072 #define foreach_uri_msg \
1073 _(BIND_URI_REPLY, bind_uri_reply) \
1074 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1075 _(ACCEPT_SESSION, accept_session) \
1076 _(CONNECT_URI_REPLY, connect_uri_reply) \
1077 _(DISCONNECT_SESSION, disconnect_session) \
1078 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1079 _(RESET_SESSION, reset_session) \
1080 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1081 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1082 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1085 uri_api_hookup (uri_tcp_test_main_t * utm)
1088 vl_msg_api_set_handlers(VL_API_##N, #n, \
1089 vl_api_##n##_t_handler, \
1091 vl_api_##n##_t_endian, \
1092 vl_api_##n##_t_print, \
1093 sizeof(vl_api_##n##_t), 1);
1099 main (int argc, char **argv)
1101 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1102 unformat_input_t _argv, *a = &_argv;
1105 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1106 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1107 u64 bytes_to_send = 64 << 10, mbytes;
1112 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1114 clib_mem_init (0, 256 << 20);
1116 heap = clib_mem_get_per_cpu_heap ();
1117 h = mheap_header (heap);
1119 /* make the main heap thread-safe */
1120 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1122 vec_validate (utm->rx_buf, 65536);
1124 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1126 utm->my_pid = getpid ();
1127 utm->configured_segment_size = 1 << 20;
1129 clib_time_init (&utm->clib_time);
1130 init_error_string_table (utm);
1131 svm_fifo_segment_init (0x200000000ULL, 20);
1132 unformat_init_command_line (a, argv);
1134 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1136 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1138 vl_set_memory_root_path ((char *) chroot_prefix);
1140 else if (unformat (a, "uri %s", &uri))
1142 else if (unformat (a, "segment-size %dM", &tmp))
1143 utm->configured_segment_size = tmp << 20;
1144 else if (unformat (a, "segment-size %dG", &tmp))
1145 utm->configured_segment_size = tmp << 30;
1146 else if (unformat (a, "master"))
1148 else if (unformat (a, "slave"))
1150 else if (unformat (a, "drop"))
1152 else if (unformat (a, "test"))
1153 test_return_packets = 1;
1154 else if (unformat (a, "mbytes %lld", &mbytes))
1156 bytes_to_send = mbytes << 20;
1158 else if (unformat (a, "gbytes %lld", &mbytes))
1160 bytes_to_send = mbytes << 30;
1164 fformat (stderr, "%s: usage [master|slave]\n");
1171 utm->uri = format (0, "%s%c", uri, 0);
1172 utm->connect_uri = format (0, "%s%c", uri, 0);
1176 utm->uri = format (0, "%s%c", bind_uri, 0);
1177 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1180 utm->i_am_master = i_am_master;
1181 utm->segment_main = &svm_fifo_segment_main;
1182 utm->drop_packets = drop_packets;
1183 utm->test_return_packets = test_return_packets;
1184 utm->bytes_to_send = bytes_to_send;
1186 setup_signal_handlers ();
1187 uri_api_hookup (utm);
1189 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
1192 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1196 if (i_am_master == 0)
1199 vl_client_disconnect_from_vlib ();
1203 /* $$$$ hack preallocation */
1204 for (i = 0; i < 200000; i++)
1206 pool_get (utm->sessions, session);
1207 memset (session, 0, sizeof (*session));
1209 for (i = 0; i < 200000; i++)
1210 pool_put_index (utm->sessions, i);
1214 vl_client_disconnect_from_vlib ();
1219 * fd.io coding-style-patch-verification: ON
1222 * eval: (c-set-style "gnu")