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;
65 unix_shared_memory_queue_t *vl_input_queue;
67 /* API client handle */
70 /* The URI we're playing with */
76 /* Hash table for disconnect processing */
77 uword *session_index_by_vpp_handles;
79 /* intermediate rx buffer */
82 /* URI for slave's connect */
85 u32 connected_session_index;
89 /* drop all packets */
93 unix_shared_memory_queue_t *our_event_queue;
95 /* $$$ single thread only for the moment */
96 unix_shared_memory_queue_t *vpp_event_queue;
100 /* For deadman timers */
101 clib_time_t clib_time;
103 /* State of the connection, shared between msg RX thread and main thread */
104 volatile connection_state_t state;
106 /* Signal variables */
107 volatile int time_to_stop;
108 volatile int time_to_print_stats;
110 u32 configured_segment_size;
112 /* VNET_API_ERROR_FOO -> "Foo" hash table */
113 uword *error_string_by_error_number;
115 u8 *connect_test_data;
116 pthread_t client_rx_thread_handle;
117 u32 client_bytes_received;
118 u8 test_return_packets;
122 svm_fifo_segment_main_t *segment_main;
123 } uri_tcp_test_main_t;
125 uri_tcp_test_main_t uri_tcp_test_main;
130 #define NITER 4000000
134 format_api_error (u8 * s, va_list * args)
136 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
137 i32 error = va_arg (*args, u32);
140 p = hash_get (utm->error_string_by_error_number, -error);
143 s = format (s, "%s", p[0]);
145 s = format (s, "%d", error);
150 init_error_string_table (uri_tcp_test_main_t * utm)
152 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
154 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
155 foreach_vnet_api_error;
158 hash_set (utm->error_string_by_error_number, 99, "Misc");
162 wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
165 #define TIMEOUT 600.0
167 #define TIMEOUT 600.0
170 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
172 while (clib_time_now (&utm->clib_time) < timeout)
174 if (utm->state == state)
176 if (utm->state == STATE_FAILED)
178 if (utm->time_to_stop == 1)
181 clib_warning ("timeout waiting for STATE_READY");
186 application_send_attach (uri_tcp_test_main_t * utm)
188 vl_api_application_attach_t *bmp;
189 u32 fifo_size = 4 << 20;
190 bmp = vl_msg_api_alloc (sizeof (*bmp));
191 memset (bmp, 0, sizeof (*bmp));
193 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
194 bmp->client_index = utm->my_client_index;
195 bmp->context = ntohl (0xfeedface);
196 bmp->options[APP_OPTIONS_FLAGS] =
197 APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
198 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
199 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
200 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
201 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
202 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20;
203 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
207 application_attach (uri_tcp_test_main_t * utm)
209 application_send_attach (utm);
210 if (wait_for_state_change (utm, STATE_ATTACHED))
212 clib_warning ("timeout waiting for STATE_ATTACHED");
219 application_detach (uri_tcp_test_main_t * utm)
221 vl_api_application_detach_t *bmp;
222 bmp = vl_msg_api_alloc (sizeof (*bmp));
223 memset (bmp, 0, sizeof (*bmp));
225 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
226 bmp->client_index = utm->my_client_index;
227 bmp->context = ntohl (0xfeedface);
228 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
232 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
235 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
236 svm_fifo_segment_create_args_t _a, *a = &_a;
241 clib_warning ("attach failed: %U", format_api_error,
242 clib_net_to_host_u32 (mp->retval));
243 utm->state = STATE_FAILED;
247 if (mp->segment_name_length == 0)
249 clib_warning ("segment_name_length zero");
253 a->segment_name = (char *) mp->segment_name;
254 a->segment_size = mp->segment_size;
256 ASSERT (mp->app_event_queue_address);
258 /* Attach to the segment vpp created */
259 rv = svm_fifo_segment_attach (a);
262 clib_warning ("svm_fifo_segment_attach ('%s') failed",
267 utm->our_event_queue =
268 uword_to_pointer (mp->app_event_queue_address,
269 unix_shared_memory_queue_t *);
270 utm->state = STATE_ATTACHED;
274 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
278 clib_warning ("detach returned with err: %d", mp->retval);
282 stop_signal (int signum)
284 uri_tcp_test_main_t *um = &uri_tcp_test_main;
286 um->time_to_stop = 1;
290 stats_signal (int signum)
292 uri_tcp_test_main_t *um = &uri_tcp_test_main;
294 um->time_to_print_stats = 1;
297 static clib_error_t *
298 setup_signal_handlers (void)
300 signal (SIGINT, stats_signal);
301 signal (SIGQUIT, stop_signal);
302 signal (SIGTERM, stop_signal);
308 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
310 clib_warning ("BUG");
314 connect_to_vpp (char *name)
316 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
317 api_main_t *am = &api_main;
319 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
322 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
323 utm->my_client_index = am->my_client_index;
329 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
331 svm_fifo_segment_create_args_t _a, *a = &_a;
334 a->segment_name = (char *) mp->segment_name;
335 a->segment_size = mp->segment_size;
336 /* Attach to the segment vpp created */
337 rv = svm_fifo_segment_attach (a);
340 clib_warning ("svm_fifo_segment_attach ('%s') failed",
344 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
349 session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
354 deltat = clib_time_now (&utm->clib_time) - session->start;
355 bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
356 fformat (stdout, "Finished in %.6f\n", deltat);
357 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
361 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
363 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
364 session_t *session = 0;
365 vl_api_disconnect_session_reply_t *rmp;
369 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
373 session = pool_elt_at_index (utm->sessions, p[0]);
374 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
375 pool_put (utm->sessions, session);
379 clib_warning ("couldn't find session key %llx", mp->handle);
383 // utm->time_to_stop = 1;
385 rmp = vl_msg_api_alloc (sizeof (*rmp));
386 memset (rmp, 0, sizeof (*rmp));
388 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
390 rmp->handle = mp->handle;
391 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
394 session_print_stats (utm, session);
398 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
400 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
401 vl_api_reset_session_reply_t *rmp;
405 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
409 clib_warning ("got reset");
411 utm->time_to_stop = 1;
415 clib_warning ("couldn't find session key %llx", mp->handle);
419 rmp = vl_msg_api_alloc (sizeof (*rmp));
420 memset (rmp, 0, sizeof (*rmp));
421 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
423 rmp->handle = mp->handle;
424 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
428 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
429 session_fifo_event_t * e)
432 int n_read, bytes, i;
436 bytes = svm_fifo_max_dequeue (rx_fifo);
437 /* Allow enqueuing of new event */
438 svm_fifo_unset_event (rx_fifo);
443 n_read = svm_fifo_dequeue_nowait (rx_fifo,
444 clib_min (vec_len (utm->rx_buf),
445 bytes), utm->rx_buf);
449 if (utm->test_return_packets)
451 for (i = 0; i < n_read; i++)
454 != ((utm->client_bytes_received + i) & 0xff))
456 clib_warning ("error at byte %lld, 0x%x not 0x%x",
457 utm->client_bytes_received + i,
459 ((utm->client_bytes_received +
464 utm->client_bytes_received += n_read;
470 // clib_warning ("weird!");
480 client_handle_event_queue (uri_tcp_test_main_t * utm)
482 session_fifo_event_t _e, *e = &_e;;
484 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
486 switch (e->event_type)
488 case FIFO_EVENT_APP_RX:
489 client_handle_fifo_event_rx (utm, e);
492 case FIFO_EVENT_DISCONNECT:
496 clib_warning ("unknown event type %d", e->event_type);
502 client_rx_thread_fn (void *arg)
504 session_fifo_event_t _e, *e = &_e;
505 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
507 utm->client_bytes_received = 0;
510 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
512 switch (e->event_type)
514 case FIFO_EVENT_APP_RX:
515 client_handle_fifo_event_rx (utm, e);
518 case FIFO_EVENT_DISCONNECT:
521 clib_warning ("unknown event type %d", e->event_type);
525 if (PREDICT_FALSE (utm->time_to_stop == 1))
533 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
535 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
538 svm_fifo_t *rx_fifo, *tx_fifo;
543 clib_warning ("connection failed with code: %U", format_api_error,
544 clib_net_to_host_u32 (mp->retval));
545 utm->state = STATE_FAILED;
549 utm->vpp_event_queue =
550 uword_to_pointer (mp->vpp_event_queue_address,
551 unix_shared_memory_queue_t *);
557 pool_get (utm->sessions, session);
558 session_index = session - utm->sessions;
560 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
561 rx_fifo->client_session_index = session_index;
562 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
563 tx_fifo->client_session_index = session_index;
565 session->server_rx_fifo = rx_fifo;
566 session->server_tx_fifo = tx_fifo;
567 session->vpp_session_handle = mp->handle;
568 session->start = clib_time_now (&utm->clib_time);
571 utm->connected_session_index = session_index;
572 utm->state = STATE_READY;
574 /* Add it to lookup table */
575 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
577 /* Start RX thread */
578 rv = pthread_create (&utm->client_rx_thread_handle,
579 NULL /*attr */ , client_rx_thread_fn, 0);
582 clib_warning ("pthread_create returned %d", rv);
583 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
588 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
591 u8 *test_data = utm->connect_test_data;
593 int test_buf_offset = 0;
595 u32 queue_max_chunk = 128 << 10, actual_write;
596 session_fifo_event_t evt;
597 static int serial_number = 0;
600 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
601 if (bytes_to_snd > vec_len (test_data))
602 bytes_to_snd = vec_len (test_data);
604 while (bytes_to_snd > 0 && !utm->time_to_stop)
606 actual_write = (bytes_to_snd > queue_max_chunk) ?
607 queue_max_chunk : bytes_to_snd;
608 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
609 test_data + test_buf_offset);
614 test_buf_offset += rv;
617 if (svm_fifo_set_event (tx_fifo))
619 /* Fabricate TX event, send to vpp */
621 evt.event_type = FIFO_EVENT_APP_TX;
622 evt.event_id = serial_number++;
624 unix_shared_memory_queue_add (utm->vpp_event_queue,
626 0 /* do wait for mutex */ );
633 client_send_data (uri_tcp_test_main_t * utm)
635 u8 *test_data = utm->connect_test_data;
636 int mypid = getpid ();
639 u32 n_iterations, leftover;
642 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
643 tx_fifo = session->server_tx_fifo;
645 ASSERT (vec_len (test_data) > 0);
647 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
648 n_iterations = utm->bytes_to_send / vec_len (test_data);
650 for (i = 0; i < n_iterations; i++)
652 send_test_chunk (utm, tx_fifo, mypid, 0);
653 if (utm->time_to_stop)
657 leftover = utm->bytes_to_send % vec_len (test_data);
659 send_test_chunk (utm, tx_fifo, mypid, leftover);
661 if (!utm->drop_packets)
663 f64 timeout = clib_time_now (&utm->clib_time) + 10;
665 /* Wait for the outstanding packets */
666 while (utm->client_bytes_received <
667 vec_len (test_data) * n_iterations + leftover)
669 if (clib_time_now (&utm->clib_time) > timeout)
671 clib_warning ("timed out waiting for the missing packets");
676 utm->time_to_stop = 1;
680 client_send_connect (uri_tcp_test_main_t * utm)
682 vl_api_connect_uri_t *cmp;
683 cmp = vl_msg_api_alloc (sizeof (*cmp));
684 memset (cmp, 0, sizeof (*cmp));
686 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
687 cmp->client_index = utm->my_client_index;
688 cmp->context = ntohl (0xfeedface);
689 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
690 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
694 client_connect (uri_tcp_test_main_t * utm)
696 client_send_connect (utm);
697 if (wait_for_state_change (utm, STATE_READY))
699 clib_warning ("Connect failed");
706 client_send_disconnect (uri_tcp_test_main_t * utm)
708 session_t *connected_session;
709 vl_api_disconnect_session_t *dmp;
710 connected_session = pool_elt_at_index (utm->sessions,
711 utm->connected_session_index);
712 dmp = vl_msg_api_alloc (sizeof (*dmp));
713 memset (dmp, 0, sizeof (*dmp));
714 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
715 dmp->client_index = utm->my_client_index;
716 dmp->handle = connected_session->vpp_session_handle;
717 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
721 client_disconnect (uri_tcp_test_main_t * utm)
723 client_send_disconnect (utm);
724 clib_warning ("Sent disconnect");
725 if (wait_for_state_change (utm, STATE_START))
727 clib_warning ("Disconnect failed");
734 client_test (uri_tcp_test_main_t * utm)
738 if (application_attach (utm))
741 if (client_connect (utm))
743 application_detach (utm);
748 vec_validate (utm->connect_test_data, 128 * 1024 - 1);
749 for (i = 0; i < vec_len (utm->connect_test_data); i++)
750 utm->connect_test_data[i] = i & 0xff;
753 client_send_data (utm);
756 client_disconnect (utm);
758 application_detach (utm);
762 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
764 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
768 clib_warning ("bind failed: %U", format_api_error,
769 clib_net_to_host_u32 (mp->retval));
770 utm->state = STATE_FAILED;
774 utm->state = STATE_READY;
778 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
780 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
783 clib_warning ("returned %d", ntohl (mp->retval));
785 utm->state = STATE_START;
789 format_ip4_address (u8 * s, va_list * args)
791 u8 *a = va_arg (*args, u8 *);
792 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
796 format_ip6_address (u8 * s, va_list * args)
798 ip6_address_t *a = va_arg (*args, ip6_address_t *);
799 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
801 i_max_n_zero = ARRAY_LEN (a->as_u16);
803 i_first_zero = i_max_n_zero;
805 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
807 u32 is_zero = a->as_u16[i] == 0;
808 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
814 if ((!is_zero && n_zeros > max_n_zeros)
815 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
817 i_max_n_zero = i_first_zero;
818 max_n_zeros = n_zeros;
819 i_first_zero = ARRAY_LEN (a->as_u16);
824 last_double_colon = 0;
825 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
827 if (i == i_max_n_zero && max_n_zeros > 1)
829 s = format (s, "::");
830 i += max_n_zeros - 1;
831 last_double_colon = 1;
835 s = format (s, "%s%x",
836 (last_double_colon || i == 0) ? "" : ":",
837 clib_net_to_host_u16 (a->as_u16[i]));
838 last_double_colon = 0;
845 /* Format an IP46 address. */
847 format_ip46_address (u8 * s, va_list * args)
849 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
850 ip46_type_t type = va_arg (*args, ip46_type_t);
856 is_ip4 = ip46_address_is_ip4 (ip46);
867 format (s, "%U", format_ip4_address, &ip46->ip4) :
868 format (s, "%U", format_ip6_address, &ip46->ip6);
872 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
874 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
875 vl_api_accept_session_reply_t *rmp;
876 svm_fifo_t *rx_fifo, *tx_fifo;
878 static f64 start_time;
882 if (start_time == 0.0)
883 start_time = clib_time_now (&utm->clib_time);
885 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
886 clib_warning ("Accepted session from: %s:%d", ip_str,
887 clib_net_to_host_u16 (mp->port));
888 utm->vpp_event_queue =
889 uword_to_pointer (mp->vpp_event_queue_address,
890 unix_shared_memory_queue_t *);
892 /* Allocate local session and set it up */
893 pool_get (utm->sessions, session);
894 session_index = session - utm->sessions;
896 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
897 rx_fifo->client_session_index = session_index;
898 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
899 tx_fifo->client_session_index = session_index;
901 session->server_rx_fifo = rx_fifo;
902 session->server_tx_fifo = tx_fifo;
904 /* Add it to lookup table */
905 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
907 utm->state = STATE_READY;
910 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
912 f64 now = clib_time_now (&utm->clib_time);
913 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
914 pool_elts (utm->sessions), now - start_time,
915 (f64) pool_elts (utm->sessions) / (now - start_time));
919 * Send accept reply to vpp
921 rmp = vl_msg_api_alloc (sizeof (*rmp));
922 memset (rmp, 0, sizeof (*rmp));
923 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
924 rmp->handle = mp->handle;
925 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
927 session->bytes_received = 0;
928 session->start = clib_time_now (&utm->clib_time);
932 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
933 session_fifo_event_t * e)
935 svm_fifo_t *rx_fifo, *tx_fifo;
937 session_fifo_event_t evt;
938 unix_shared_memory_queue_t *q;
941 u32 max_dequeue, offset, max_transfer, rx_buf_len;
943 rx_buf_len = vec_len (utm->rx_buf);
945 session = &utm->sessions[rx_fifo->client_session_index];
946 tx_fifo = session->server_tx_fifo;
948 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
949 /* Allow enqueuing of a new event */
950 svm_fifo_unset_event (rx_fifo);
952 if (PREDICT_FALSE (max_dequeue == 0))
957 /* Read the max_dequeue */
960 max_transfer = clib_min (rx_buf_len, max_dequeue);
961 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
964 max_dequeue -= n_read;
965 session->bytes_received += n_read;
968 /* Reflect if a non-drop session */
969 if (!utm->drop_packets && n_read > 0)
974 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
975 &utm->rx_buf[offset]);
982 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
984 /* If event wasn't set, add one */
985 if (svm_fifo_set_event (tx_fifo))
987 /* Fabricate TX event, send to vpp */
989 evt.event_type = FIFO_EVENT_APP_TX;
990 evt.event_id = e->event_id;
992 q = utm->vpp_event_queue;
993 unix_shared_memory_queue_add (q, (u8 *) & evt,
994 1 /* do wait for mutex */ );
998 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
1002 server_handle_event_queue (uri_tcp_test_main_t * utm)
1004 session_fifo_event_t _e, *e = &_e;;
1008 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
1010 switch (e->event_type)
1012 case FIFO_EVENT_APP_RX:
1013 server_handle_fifo_event_rx (utm, e);
1016 case FIFO_EVENT_DISCONNECT:
1020 clib_warning ("unknown event type %d", e->event_type);
1023 if (PREDICT_FALSE (utm->time_to_stop == 1))
1025 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1027 utm->time_to_print_stats = 0;
1028 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1034 server_send_listen (uri_tcp_test_main_t * utm)
1036 vl_api_bind_uri_t *bmp;
1037 bmp = vl_msg_api_alloc (sizeof (*bmp));
1038 memset (bmp, 0, sizeof (*bmp));
1040 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1041 bmp->client_index = utm->my_client_index;
1042 bmp->context = ntohl (0xfeedface);
1043 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1044 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1048 server_listen (uri_tcp_test_main_t * utm)
1050 server_send_listen (utm);
1051 if (wait_for_state_change (utm, STATE_READY))
1053 clib_warning ("timeout waiting for STATE_READY");
1060 server_send_unbind (uri_tcp_test_main_t * utm)
1062 vl_api_unbind_uri_t *ump;
1064 ump = vl_msg_api_alloc (sizeof (*ump));
1065 memset (ump, 0, sizeof (*ump));
1067 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1068 ump->client_index = utm->my_client_index;
1069 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1070 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1074 server_unbind (uri_tcp_test_main_t * utm)
1076 server_send_unbind (utm);
1077 if (wait_for_state_change (utm, STATE_START))
1079 clib_warning ("timeout waiting for STATE_START");
1086 server_test (uri_tcp_test_main_t * utm)
1088 if (application_attach (utm))
1092 if (server_listen (utm))
1095 /* Enter handle event loop */
1096 server_handle_event_queue (utm);
1099 server_send_unbind (utm);
1101 application_detach (utm);
1103 fformat (stdout, "Test complete...\n");
1107 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1110 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1115 clib_warning ("vpp complained about disconnect: %d",
1116 ntohl (mp->retval));
1119 utm->state = STATE_START;
1120 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1122 session_print_stats (utm, session);
1125 #define foreach_uri_msg \
1126 _(BIND_URI_REPLY, bind_uri_reply) \
1127 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1128 _(ACCEPT_SESSION, accept_session) \
1129 _(CONNECT_URI_REPLY, connect_uri_reply) \
1130 _(DISCONNECT_SESSION, disconnect_session) \
1131 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1132 _(RESET_SESSION, reset_session) \
1133 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1134 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1135 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1138 uri_api_hookup (uri_tcp_test_main_t * utm)
1141 vl_msg_api_set_handlers(VL_API_##N, #n, \
1142 vl_api_##n##_t_handler, \
1144 vl_api_##n##_t_endian, \
1145 vl_api_##n##_t_print, \
1146 sizeof(vl_api_##n##_t), 1);
1152 main (int argc, char **argv)
1154 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1155 unformat_input_t _argv, *a = &_argv;
1158 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1159 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1160 u64 bytes_to_send = 64 << 10, mbytes;
1165 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1167 clib_mem_init (0, 256 << 20);
1169 heap = clib_mem_get_per_cpu_heap ();
1170 h = mheap_header (heap);
1172 /* make the main heap thread-safe */
1173 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1175 vec_validate (utm->rx_buf, 128 << 10);
1177 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1179 utm->my_pid = getpid ();
1180 utm->configured_segment_size = 1 << 20;
1182 clib_time_init (&utm->clib_time);
1183 init_error_string_table (utm);
1184 svm_fifo_segment_init (0x200000000ULL, 20);
1185 unformat_init_command_line (a, argv);
1187 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1189 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1191 vl_set_memory_root_path ((char *) chroot_prefix);
1193 else if (unformat (a, "uri %s", &uri))
1195 else if (unformat (a, "segment-size %dM", &tmp))
1196 utm->configured_segment_size = tmp << 20;
1197 else if (unformat (a, "segment-size %dG", &tmp))
1198 utm->configured_segment_size = tmp << 30;
1199 else if (unformat (a, "master"))
1201 else if (unformat (a, "slave"))
1203 else if (unformat (a, "drop"))
1205 else if (unformat (a, "test"))
1206 test_return_packets = 1;
1207 else if (unformat (a, "mbytes %lld", &mbytes))
1209 bytes_to_send = mbytes << 20;
1211 else if (unformat (a, "gbytes %lld", &mbytes))
1213 bytes_to_send = mbytes << 30;
1217 fformat (stderr, "%s: usage [master|slave]\n");
1224 utm->uri = format (0, "%s%c", uri, 0);
1225 utm->connect_uri = format (0, "%s%c", uri, 0);
1229 utm->uri = format (0, "%s%c", bind_uri, 0);
1230 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1233 utm->i_am_master = i_am_master;
1234 utm->segment_main = &svm_fifo_segment_main;
1235 utm->drop_packets = drop_packets;
1236 utm->test_return_packets = test_return_packets;
1237 utm->bytes_to_send = bytes_to_send;
1238 utm->time_to_stop = 0;
1240 setup_signal_handlers ();
1241 uri_api_hookup (utm);
1243 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
1246 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1250 if (i_am_master == 0)
1253 vl_client_disconnect_from_vlib ();
1257 /* $$$$ hack preallocation */
1258 for (i = 0; i < 200000; i++)
1260 pool_get (utm->sessions, session);
1261 memset (session, 0, sizeof (*session));
1263 for (i = 0; i < 200000; i++)
1264 pool_put_index (utm->sessions, i);
1268 vl_client_disconnect_from_vlib ();
1273 * fd.io coding-style-patch-verification: ON
1276 * eval: (c-set-style "gnu")