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.
19 #include <vnet/session/application_interface.h>
20 #include <svm/svm_fifo_segment.h>
21 #include <vlibmemory/api.h>
23 #include <vpp/api/vpe_msg_enum.h>
25 #define vl_typedefs /* define message structures */
26 #include <vpp/api/vpe_all_api_h.h>
29 /* declare message handlers for each api */
31 #define vl_endianfun /* define message structures */
32 #include <vpp/api/vpe_all_api_h.h>
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...)
38 #include <vpp/api/vpe_all_api_h.h>
43 svm_fifo_t *server_rx_fifo;
44 svm_fifo_t *server_tx_fifo;
46 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;
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;
121 /** Flag that decides if socket, instead of svm, api is used to connect to
122 * vpp. If sock api is used, shm binary api is subsequently bootstrapped
123 * and all other messages are exchanged using shm IPC. */
127 svm_fifo_segment_main_t *segment_main;
128 } uri_tcp_test_main_t;
130 uri_tcp_test_main_t uri_tcp_test_main;
135 #define NITER 4000000
139 format_api_error (u8 * s, va_list * args)
141 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
142 i32 error = va_arg (*args, u32);
145 p = hash_get (utm->error_string_by_error_number, -error);
148 s = format (s, "%s", p[0]);
150 s = format (s, "%d", error);
155 init_error_string_table (uri_tcp_test_main_t * utm)
157 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
159 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
160 foreach_vnet_api_error;
163 hash_set (utm->error_string_by_error_number, 99, "Misc");
167 wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
170 #define TIMEOUT 600.0
172 #define TIMEOUT 600.0
175 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
177 while (clib_time_now (&utm->clib_time) < timeout)
179 if (utm->state == state)
181 if (utm->state == STATE_FAILED)
183 if (utm->time_to_stop == 1)
186 clib_warning ("timeout waiting for STATE_READY");
191 application_send_attach (uri_tcp_test_main_t * utm)
193 vl_api_application_attach_t *bmp;
194 u32 fifo_size = 4 << 20;
195 bmp = vl_msg_api_alloc (sizeof (*bmp));
196 memset (bmp, 0, sizeof (*bmp));
198 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
199 bmp->client_index = utm->my_client_index;
200 bmp->context = ntohl (0xfeedface);
201 bmp->options[APP_OPTIONS_FLAGS] =
202 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
203 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
204 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
205 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
206 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
207 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
208 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
212 application_attach (uri_tcp_test_main_t * utm)
214 application_send_attach (utm);
215 if (wait_for_state_change (utm, STATE_ATTACHED))
217 clib_warning ("timeout waiting for STATE_ATTACHED");
224 application_detach (uri_tcp_test_main_t * utm)
226 vl_api_application_detach_t *bmp;
227 bmp = vl_msg_api_alloc (sizeof (*bmp));
228 memset (bmp, 0, sizeof (*bmp));
230 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
231 bmp->client_index = utm->my_client_index;
232 bmp->context = ntohl (0xfeedface);
233 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
237 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
240 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
241 svm_fifo_segment_create_args_t _a, *a = &_a;
246 clib_warning ("attach failed: %U", format_api_error,
247 clib_net_to_host_u32 (mp->retval));
248 utm->state = STATE_FAILED;
252 if (mp->segment_name_length == 0)
254 clib_warning ("segment_name_length zero");
258 memset (a, 0, sizeof (*a));
259 a->segment_name = (char *) mp->segment_name;
260 a->segment_size = mp->segment_size;
262 ASSERT (mp->app_event_queue_address);
264 /* Attach to the segment vpp created */
265 rv = svm_fifo_segment_attach (a);
268 clib_warning ("svm_fifo_segment_attach ('%s') failed",
273 utm->our_event_queue =
274 uword_to_pointer (mp->app_event_queue_address,
275 unix_shared_memory_queue_t *);
276 utm->state = STATE_ATTACHED;
280 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
284 clib_warning ("detach returned with err: %d", mp->retval);
288 stop_signal (int signum)
290 uri_tcp_test_main_t *um = &uri_tcp_test_main;
292 um->time_to_stop = 1;
296 stats_signal (int signum)
298 uri_tcp_test_main_t *um = &uri_tcp_test_main;
300 um->time_to_print_stats = 1;
303 static clib_error_t *
304 setup_signal_handlers (void)
306 signal (SIGINT, stats_signal);
307 signal (SIGQUIT, stop_signal);
308 signal (SIGTERM, stop_signal);
314 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
316 clib_warning ("BUG");
320 connect_to_vpp (char *name)
322 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
323 api_main_t *am = &api_main;
325 if (utm->use_sock_api)
327 if (vl_socket_client_connect ((char *) utm->socket_name, name,
328 0 /* default rx, tx buffer */ ))
331 return vl_socket_client_init_shm (0);
335 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
337 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
338 utm->my_client_index = am->my_client_index;
344 disconnect_from_vpp (uri_tcp_test_main_t * utm)
346 if (utm->use_sock_api)
347 vl_socket_client_disconnect ();
349 vl_client_disconnect_from_vlib ();
353 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
355 svm_fifo_segment_create_args_t _a, *a = &_a;
358 memset (a, 0, sizeof (*a));
359 a->segment_name = (char *) mp->segment_name;
360 a->segment_size = mp->segment_size;
361 /* Attach to the segment vpp created */
362 rv = svm_fifo_segment_attach (a);
365 clib_warning ("svm_fifo_segment_attach ('%s') failed",
369 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
374 session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
379 deltat = clib_time_now (&utm->clib_time) - session->start;
380 bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
381 fformat (stdout, "Finished in %.6f\n", deltat);
382 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
386 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
388 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
389 session_t *session = 0;
390 vl_api_disconnect_session_reply_t *rmp;
394 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
398 session = pool_elt_at_index (utm->sessions, p[0]);
399 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
400 pool_put (utm->sessions, session);
404 clib_warning ("couldn't find session key %llx", mp->handle);
408 // utm->time_to_stop = 1;
410 rmp = vl_msg_api_alloc (sizeof (*rmp));
411 memset (rmp, 0, sizeof (*rmp));
413 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
415 rmp->handle = mp->handle;
416 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
419 session_print_stats (utm, session);
423 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
425 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
426 vl_api_reset_session_reply_t *rmp;
430 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
434 clib_warning ("got reset");
436 utm->time_to_stop = 1;
440 clib_warning ("couldn't find session key %llx", mp->handle);
444 rmp = vl_msg_api_alloc (sizeof (*rmp));
445 memset (rmp, 0, sizeof (*rmp));
446 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
448 rmp->handle = mp->handle;
449 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
453 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
454 session_fifo_event_t * e)
457 int n_read, bytes, i;
461 bytes = svm_fifo_max_dequeue (rx_fifo);
462 /* Allow enqueuing of new event */
463 svm_fifo_unset_event (rx_fifo);
468 n_read = svm_fifo_dequeue_nowait (rx_fifo,
469 clib_min (vec_len (utm->rx_buf),
470 bytes), utm->rx_buf);
474 if (utm->test_return_packets)
476 for (i = 0; i < n_read; i++)
479 != ((utm->client_bytes_received + i) & 0xff))
481 clib_warning ("error at byte %lld, 0x%x not 0x%x",
482 utm->client_bytes_received + i,
484 ((utm->client_bytes_received +
489 utm->client_bytes_received += n_read;
495 // clib_warning ("weird!");
505 client_handle_event_queue (uri_tcp_test_main_t * utm)
507 session_fifo_event_t _e, *e = &_e;;
509 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
511 switch (e->event_type)
513 case FIFO_EVENT_APP_RX:
514 client_handle_fifo_event_rx (utm, e);
517 case FIFO_EVENT_DISCONNECT:
521 clib_warning ("unknown event type %d", e->event_type);
527 client_rx_thread_fn (void *arg)
529 session_fifo_event_t _e, *e = &_e;
530 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
532 utm->client_bytes_received = 0;
535 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
537 switch (e->event_type)
539 case FIFO_EVENT_APP_RX:
540 client_handle_fifo_event_rx (utm, e);
543 case FIFO_EVENT_DISCONNECT:
546 clib_warning ("unknown event type %d", e->event_type);
550 if (PREDICT_FALSE (utm->time_to_stop == 1))
558 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
560 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
563 svm_fifo_t *rx_fifo, *tx_fifo;
568 clib_warning ("connection failed with code: %U", format_api_error,
569 clib_net_to_host_u32 (mp->retval));
570 utm->state = STATE_FAILED;
575 clib_warning ("connected with local ip %U port %d", format_ip46_address,
576 mp->lcl_ip, mp->is_ip4,
577 clib_net_to_host_u16 (mp->lcl_port));
580 utm->vpp_event_queue =
581 uword_to_pointer (mp->vpp_event_queue_address,
582 unix_shared_memory_queue_t *);
588 pool_get (utm->sessions, session);
589 session_index = session - utm->sessions;
591 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
592 rx_fifo->client_session_index = session_index;
593 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
594 tx_fifo->client_session_index = session_index;
596 session->server_rx_fifo = rx_fifo;
597 session->server_tx_fifo = tx_fifo;
598 session->vpp_session_handle = mp->handle;
599 session->start = clib_time_now (&utm->clib_time);
602 utm->connected_session_index = session_index;
603 utm->state = STATE_READY;
605 /* Add it to lookup table */
606 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
608 /* Start RX thread */
609 rv = pthread_create (&utm->client_rx_thread_handle,
610 NULL /*attr */ , client_rx_thread_fn, 0);
613 clib_warning ("pthread_create returned %d", rv);
614 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
619 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
622 u8 *test_data = utm->connect_test_data;
624 int test_buf_offset = 0;
626 u32 queue_max_chunk = 128 << 10, actual_write;
627 session_fifo_event_t evt;
630 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
631 if (bytes_to_snd > vec_len (test_data))
632 bytes_to_snd = vec_len (test_data);
634 while (bytes_to_snd > 0 && !utm->time_to_stop)
636 actual_write = (bytes_to_snd > queue_max_chunk) ?
637 queue_max_chunk : bytes_to_snd;
638 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
639 test_data + test_buf_offset);
644 test_buf_offset += rv;
647 if (svm_fifo_set_event (tx_fifo))
649 /* Fabricate TX event, send to vpp */
651 evt.event_type = FIFO_EVENT_APP_TX;
653 unix_shared_memory_queue_add (utm->vpp_event_queue,
655 0 /* do wait for mutex */ );
662 client_send_data (uri_tcp_test_main_t * utm)
664 u8 *test_data = utm->connect_test_data;
665 int mypid = getpid ();
668 u32 n_iterations, leftover;
671 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
672 tx_fifo = session->server_tx_fifo;
674 ASSERT (vec_len (test_data) > 0);
676 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
677 n_iterations = utm->bytes_to_send / vec_len (test_data);
679 for (i = 0; i < n_iterations; i++)
681 send_test_chunk (utm, tx_fifo, mypid, 0);
682 if (utm->time_to_stop)
686 leftover = utm->bytes_to_send % vec_len (test_data);
688 send_test_chunk (utm, tx_fifo, mypid, leftover);
690 if (!utm->drop_packets)
692 f64 timeout = clib_time_now (&utm->clib_time) + 10;
694 /* Wait for the outstanding packets */
695 while (utm->client_bytes_received <
696 vec_len (test_data) * n_iterations + leftover)
698 if (clib_time_now (&utm->clib_time) > timeout)
700 clib_warning ("timed out waiting for the missing packets");
705 utm->time_to_stop = 1;
709 client_send_connect (uri_tcp_test_main_t * utm)
711 vl_api_connect_uri_t *cmp;
712 cmp = vl_msg_api_alloc (sizeof (*cmp));
713 memset (cmp, 0, sizeof (*cmp));
715 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
716 cmp->client_index = utm->my_client_index;
717 cmp->context = ntohl (0xfeedface);
718 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
719 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
723 client_connect (uri_tcp_test_main_t * utm)
725 client_send_connect (utm);
726 if (wait_for_state_change (utm, STATE_READY))
728 clib_warning ("Connect failed");
735 client_send_disconnect (uri_tcp_test_main_t * utm)
737 session_t *connected_session;
738 vl_api_disconnect_session_t *dmp;
739 connected_session = pool_elt_at_index (utm->sessions,
740 utm->connected_session_index);
741 dmp = vl_msg_api_alloc (sizeof (*dmp));
742 memset (dmp, 0, sizeof (*dmp));
743 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
744 dmp->client_index = utm->my_client_index;
745 dmp->handle = connected_session->vpp_session_handle;
746 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
750 client_disconnect (uri_tcp_test_main_t * utm)
752 client_send_disconnect (utm);
753 clib_warning ("Sent disconnect");
754 if (wait_for_state_change (utm, STATE_START))
756 clib_warning ("Disconnect failed");
763 client_run (uri_tcp_test_main_t * utm)
767 if (application_attach (utm))
770 if (client_connect (utm))
772 application_detach (utm);
777 vec_validate (utm->connect_test_data, 128 * 1024 - 1);
778 for (i = 0; i < vec_len (utm->connect_test_data); i++)
779 utm->connect_test_data[i] = i & 0xff;
782 client_send_data (utm);
785 client_disconnect (utm);
787 application_detach (utm);
791 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
793 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
797 clib_warning ("bind failed: %U", format_api_error,
798 clib_net_to_host_u32 (mp->retval));
799 utm->state = STATE_FAILED;
803 utm->state = STATE_READY;
807 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
809 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
812 clib_warning ("returned %d", ntohl (mp->retval));
814 utm->state = STATE_START;
818 format_ip4_address (u8 * s, va_list * args)
820 u8 *a = va_arg (*args, u8 *);
821 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
825 format_ip6_address (u8 * s, va_list * args)
827 ip6_address_t *a = va_arg (*args, ip6_address_t *);
828 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
830 i_max_n_zero = ARRAY_LEN (a->as_u16);
832 i_first_zero = i_max_n_zero;
834 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
836 u32 is_zero = a->as_u16[i] == 0;
837 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
843 if ((!is_zero && n_zeros > max_n_zeros)
844 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
846 i_max_n_zero = i_first_zero;
847 max_n_zeros = n_zeros;
848 i_first_zero = ARRAY_LEN (a->as_u16);
853 last_double_colon = 0;
854 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
856 if (i == i_max_n_zero && max_n_zeros > 1)
858 s = format (s, "::");
859 i += max_n_zeros - 1;
860 last_double_colon = 1;
864 s = format (s, "%s%x",
865 (last_double_colon || i == 0) ? "" : ":",
866 clib_net_to_host_u16 (a->as_u16[i]));
867 last_double_colon = 0;
874 /* Format an IP46 address. */
876 format_ip46_address (u8 * s, va_list * args)
878 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
879 ip46_type_t type = va_arg (*args, ip46_type_t);
885 is_ip4 = ip46_address_is_ip4 (ip46);
896 format (s, "%U", format_ip4_address, &ip46->ip4) :
897 format (s, "%U", format_ip6_address, &ip46->ip6);
901 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
903 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
904 vl_api_accept_session_reply_t *rmp;
905 svm_fifo_t *rx_fifo, *tx_fifo;
907 static f64 start_time;
911 if (start_time == 0.0)
912 start_time = clib_time_now (&utm->clib_time);
914 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
915 clib_warning ("Accepted session from: %s:%d", ip_str,
916 clib_net_to_host_u16 (mp->port));
917 utm->vpp_event_queue =
918 uword_to_pointer (mp->vpp_event_queue_address,
919 unix_shared_memory_queue_t *);
921 /* Allocate local session and set it up */
922 pool_get (utm->sessions, session);
923 session_index = session - utm->sessions;
925 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
926 rx_fifo->client_session_index = session_index;
927 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
928 tx_fifo->client_session_index = session_index;
930 session->server_rx_fifo = rx_fifo;
931 session->server_tx_fifo = tx_fifo;
933 /* Add it to lookup table */
934 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
936 utm->state = STATE_READY;
939 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
941 f64 now = clib_time_now (&utm->clib_time);
942 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
943 pool_elts (utm->sessions), now - start_time,
944 (f64) pool_elts (utm->sessions) / (now - start_time));
948 * Send accept reply to vpp
950 rmp = vl_msg_api_alloc (sizeof (*rmp));
951 memset (rmp, 0, sizeof (*rmp));
952 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
953 rmp->handle = mp->handle;
954 rmp->context = mp->context;
955 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
957 session->bytes_received = 0;
958 session->start = clib_time_now (&utm->clib_time);
962 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
963 session_fifo_event_t * e)
965 svm_fifo_t *rx_fifo, *tx_fifo;
967 session_fifo_event_t evt;
968 unix_shared_memory_queue_t *q;
971 u32 max_dequeue, offset, max_transfer, rx_buf_len;
973 rx_buf_len = vec_len (utm->rx_buf);
975 session = &utm->sessions[rx_fifo->client_session_index];
976 tx_fifo = session->server_tx_fifo;
978 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
979 /* Allow enqueuing of a new event */
980 svm_fifo_unset_event (rx_fifo);
982 if (PREDICT_FALSE (max_dequeue == 0))
987 /* Read the max_dequeue */
990 max_transfer = clib_min (rx_buf_len, max_dequeue);
991 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
994 max_dequeue -= n_read;
995 session->bytes_received += n_read;
998 /* Reflect if a non-drop session */
999 if (!utm->drop_packets && n_read > 0)
1004 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
1005 &utm->rx_buf[offset]);
1012 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
1014 /* If event wasn't set, add one */
1015 if (svm_fifo_set_event (tx_fifo))
1017 /* Fabricate TX event, send to vpp */
1019 evt.event_type = FIFO_EVENT_APP_TX;
1021 q = utm->vpp_event_queue;
1022 unix_shared_memory_queue_add (q, (u8 *) & evt,
1023 1 /* do wait for mutex */ );
1027 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
1031 server_handle_event_queue (uri_tcp_test_main_t * utm)
1033 session_fifo_event_t _e, *e = &_e;
1037 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
1039 switch (e->event_type)
1041 case FIFO_EVENT_APP_RX:
1042 server_handle_fifo_event_rx (utm, e);
1045 case FIFO_EVENT_DISCONNECT:
1049 clib_warning ("unknown event type %d", e->event_type);
1052 if (PREDICT_FALSE (utm->time_to_stop == 1))
1054 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1056 utm->time_to_print_stats = 0;
1057 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1063 server_send_listen (uri_tcp_test_main_t * utm)
1065 vl_api_bind_uri_t *bmp;
1066 bmp = vl_msg_api_alloc (sizeof (*bmp));
1067 memset (bmp, 0, sizeof (*bmp));
1069 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1070 bmp->client_index = utm->my_client_index;
1071 bmp->context = ntohl (0xfeedface);
1072 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1073 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1077 server_listen (uri_tcp_test_main_t * utm)
1079 server_send_listen (utm);
1080 if (wait_for_state_change (utm, STATE_READY))
1082 clib_warning ("timeout waiting for STATE_READY");
1089 server_send_unbind (uri_tcp_test_main_t * utm)
1091 vl_api_unbind_uri_t *ump;
1093 ump = vl_msg_api_alloc (sizeof (*ump));
1094 memset (ump, 0, sizeof (*ump));
1096 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1097 ump->client_index = utm->my_client_index;
1098 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1099 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1103 server_unbind (uri_tcp_test_main_t * utm)
1105 server_send_unbind (utm);
1106 if (wait_for_state_change (utm, STATE_START))
1108 clib_warning ("timeout waiting for STATE_START");
1115 server_run (uri_tcp_test_main_t * utm)
1120 /* $$$$ hack preallocation */
1121 for (i = 0; i < 200000; i++)
1123 pool_get (utm->sessions, session);
1124 memset (session, 0, sizeof (*session));
1126 for (i = 0; i < 200000; i++)
1127 pool_put_index (utm->sessions, i);
1129 if (application_attach (utm))
1133 if (server_listen (utm))
1136 /* Enter handle event loop */
1137 server_handle_event_queue (utm);
1140 server_send_unbind (utm);
1142 application_detach (utm);
1144 fformat (stdout, "Test complete...\n");
1148 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1151 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1156 clib_warning ("vpp complained about disconnect: %d",
1157 ntohl (mp->retval));
1160 utm->state = STATE_START;
1161 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1163 session_print_stats (utm, session);
1166 #define foreach_uri_msg \
1167 _(BIND_URI_REPLY, bind_uri_reply) \
1168 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1169 _(ACCEPT_SESSION, accept_session) \
1170 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1171 _(DISCONNECT_SESSION, disconnect_session) \
1172 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1173 _(RESET_SESSION, reset_session) \
1174 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1175 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1176 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1179 uri_api_hookup (uri_tcp_test_main_t * utm)
1182 vl_msg_api_set_handlers(VL_API_##N, #n, \
1183 vl_api_##n##_t_handler, \
1185 vl_api_##n##_t_endian, \
1186 vl_api_##n##_t_print, \
1187 sizeof(vl_api_##n##_t), 1);
1193 main (int argc, char **argv)
1195 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1196 unformat_input_t _argv, *a = &_argv;
1199 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1200 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1201 u64 bytes_to_send = 64 << 10, mbytes;
1204 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1206 clib_mem_init (0, 256 << 20);
1208 heap = clib_mem_get_per_cpu_heap ();
1209 h = mheap_header (heap);
1211 /* make the main heap thread-safe */
1212 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1214 vec_validate (utm->rx_buf, 128 << 10);
1216 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1218 utm->my_pid = getpid ();
1219 utm->configured_segment_size = 1 << 20;
1220 utm->socket_name = 0;
1221 utm->use_sock_api = 1;
1223 clib_time_init (&utm->clib_time);
1224 init_error_string_table (utm);
1225 svm_fifo_segment_init (0x200000000ULL, 20);
1226 unformat_init_command_line (a, argv);
1228 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1230 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1232 vl_set_memory_root_path ((char *) chroot_prefix);
1234 else if (unformat (a, "uri %s", &uri))
1236 else if (unformat (a, "segment-size %dM", &tmp))
1237 utm->configured_segment_size = tmp << 20;
1238 else if (unformat (a, "segment-size %dG", &tmp))
1239 utm->configured_segment_size = tmp << 30;
1240 else if (unformat (a, "master"))
1242 else if (unformat (a, "slave"))
1244 else if (unformat (a, "drop"))
1246 else if (unformat (a, "test"))
1247 test_return_packets = 1;
1248 else if (unformat (a, "mbytes %lld", &mbytes))
1250 bytes_to_send = mbytes << 20;
1252 else if (unformat (a, "gbytes %lld", &mbytes))
1254 bytes_to_send = mbytes << 30;
1256 else if (unformat (a, "socket-name %s", &utm->socket_name))
1258 else if (unformat (a, "use-svm-api"))
1259 utm->use_sock_api = 0;
1262 fformat (stderr, "%s: usage [master|slave]\n");
1267 if (!utm->socket_name)
1268 utm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
1272 utm->uri = format (0, "%s%c", uri, 0);
1273 utm->connect_uri = format (0, "%s%c", uri, 0);
1277 utm->uri = format (0, "%s%c", bind_uri, 0);
1278 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1281 utm->i_am_master = i_am_master;
1282 utm->segment_main = &svm_fifo_segment_main;
1283 utm->drop_packets = drop_packets;
1284 utm->test_return_packets = test_return_packets;
1285 utm->bytes_to_send = bytes_to_send;
1286 utm->time_to_stop = 0;
1288 setup_signal_handlers ();
1289 uri_api_hookup (utm);
1291 if (connect_to_vpp (i_am_master ? "tcp_echo_server" : "tcp_echo_client") <
1295 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1299 if (i_am_master == 0)
1304 disconnect_from_vpp (utm);
1309 * fd.io coding-style-patch-verification: ON
1312 * eval: (c-set-style "gnu")