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>
41 svm_fifo_t *server_rx_fifo;
42 svm_fifo_t *server_tx_fifo;
44 u64 vpp_session_handle;
61 unix_shared_memory_queue_t *vl_input_queue;
63 /* API client handle */
66 /* The URI we're playing with */
72 /* Hash table for disconnect processing */
73 uword *session_index_by_vpp_handles;
75 /* intermediate rx buffer */
78 /* URI for slave's connect */
81 u32 connected_session_index;
85 /* drop all packets */
89 unix_shared_memory_queue_t *our_event_queue;
91 /* $$$ single thread only for the moment */
92 unix_shared_memory_queue_t *vpp_event_queue;
96 /* For deadman timers */
97 clib_time_t clib_time;
99 /* State of the connection, shared between msg RX thread and main thread */
100 volatile connection_state_t state;
102 /* Signal variables */
103 volatile int time_to_stop;
104 volatile int time_to_print_stats;
106 u32 configured_segment_size;
108 /* VNET_API_ERROR_FOO -> "Foo" hash table */
109 uword *error_string_by_error_number;
111 u8 *connect_test_data;
112 pthread_t client_rx_thread_handle;
113 u32 client_bytes_received;
114 u8 test_return_packets;
118 svm_fifo_segment_main_t *segment_main;
119 } uri_tcp_test_main_t;
121 uri_tcp_test_main_t uri_tcp_test_main;
126 #define NITER 4000000
130 format_api_error (u8 * s, va_list * args)
132 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
133 i32 error = va_arg (*args, u32);
136 p = hash_get (utm->error_string_by_error_number, -error);
139 s = format (s, "%s", p[0]);
141 s = format (s, "%d", error);
146 init_error_string_table (uri_tcp_test_main_t * utm)
148 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
150 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
151 foreach_vnet_api_error;
154 hash_set (utm->error_string_by_error_number, 99, "Misc");
158 wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state)
161 #define TIMEOUT 600.0
163 #define TIMEOUT 600.0
166 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
168 while (clib_time_now (&utm->clib_time) < timeout)
170 if (utm->state == state)
172 if (utm->state == STATE_FAILED)
174 if (utm->time_to_stop == 1)
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 = 4 << 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_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
194 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
195 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
196 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
197 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
198 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
199 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
203 application_attach (uri_tcp_test_main_t * utm)
205 application_send_attach (utm);
206 if (wait_for_state_change (utm, STATE_ATTACHED))
208 clib_warning ("timeout waiting for STATE_ATTACHED");
215 application_detach (uri_tcp_test_main_t * utm)
217 vl_api_application_detach_t *bmp;
218 bmp = vl_msg_api_alloc (sizeof (*bmp));
219 memset (bmp, 0, sizeof (*bmp));
221 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
222 bmp->client_index = utm->my_client_index;
223 bmp->context = ntohl (0xfeedface);
224 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
228 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
231 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
232 svm_fifo_segment_create_args_t _a, *a = &_a;
237 clib_warning ("attach failed: %U", format_api_error,
238 clib_net_to_host_u32 (mp->retval));
239 utm->state = STATE_FAILED;
243 if (mp->segment_name_length == 0)
245 clib_warning ("segment_name_length zero");
249 memset (a, 0, sizeof (*a));
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 uword_to_pointer (mp->app_event_queue_address,
266 unix_shared_memory_queue_t *);
267 utm->state = STATE_ATTACHED;
271 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
275 clib_warning ("detach returned with err: %d", mp->retval);
279 stop_signal (int signum)
281 uri_tcp_test_main_t *um = &uri_tcp_test_main;
283 um->time_to_stop = 1;
287 stats_signal (int signum)
289 uri_tcp_test_main_t *um = &uri_tcp_test_main;
291 um->time_to_print_stats = 1;
294 static clib_error_t *
295 setup_signal_handlers (void)
297 signal (SIGINT, stats_signal);
298 signal (SIGQUIT, stop_signal);
299 signal (SIGTERM, stop_signal);
305 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
307 clib_warning ("BUG");
311 connect_to_vpp (char *name)
313 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
314 api_main_t *am = &api_main;
316 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
319 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
320 utm->my_client_index = am->my_client_index;
326 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
328 svm_fifo_segment_create_args_t _a, *a = &_a;
331 memset (a, 0, sizeof (*a));
332 a->segment_name = (char *) mp->segment_name;
333 a->segment_size = mp->segment_size;
334 /* Attach to the segment vpp created */
335 rv = svm_fifo_segment_attach (a);
338 clib_warning ("svm_fifo_segment_attach ('%s') failed",
342 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
347 session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
352 deltat = clib_time_now (&utm->clib_time) - session->start;
353 bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
354 fformat (stdout, "Finished in %.6f\n", deltat);
355 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
359 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
361 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
362 session_t *session = 0;
363 vl_api_disconnect_session_reply_t *rmp;
367 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
371 session = pool_elt_at_index (utm->sessions, p[0]);
372 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
373 pool_put (utm->sessions, session);
377 clib_warning ("couldn't find session key %llx", mp->handle);
381 // utm->time_to_stop = 1;
383 rmp = vl_msg_api_alloc (sizeof (*rmp));
384 memset (rmp, 0, sizeof (*rmp));
386 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
388 rmp->handle = mp->handle;
389 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
392 session_print_stats (utm, session);
396 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
398 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
399 vl_api_reset_session_reply_t *rmp;
403 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
407 clib_warning ("got reset");
409 utm->time_to_stop = 1;
413 clib_warning ("couldn't find session key %llx", mp->handle);
417 rmp = vl_msg_api_alloc (sizeof (*rmp));
418 memset (rmp, 0, sizeof (*rmp));
419 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
421 rmp->handle = mp->handle;
422 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
426 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
427 session_fifo_event_t * e)
430 int n_read, bytes, i;
434 bytes = svm_fifo_max_dequeue (rx_fifo);
435 /* Allow enqueuing of new event */
436 svm_fifo_unset_event (rx_fifo);
441 n_read = svm_fifo_dequeue_nowait (rx_fifo,
442 clib_min (vec_len (utm->rx_buf),
443 bytes), utm->rx_buf);
447 if (utm->test_return_packets)
449 for (i = 0; i < n_read; i++)
452 != ((utm->client_bytes_received + i) & 0xff))
454 clib_warning ("error at byte %lld, 0x%x not 0x%x",
455 utm->client_bytes_received + i,
457 ((utm->client_bytes_received +
462 utm->client_bytes_received += n_read;
468 // clib_warning ("weird!");
478 client_handle_event_queue (uri_tcp_test_main_t * utm)
480 session_fifo_event_t _e, *e = &_e;;
482 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
484 switch (e->event_type)
486 case FIFO_EVENT_APP_RX:
487 client_handle_fifo_event_rx (utm, e);
490 case FIFO_EVENT_DISCONNECT:
494 clib_warning ("unknown event type %d", e->event_type);
500 client_rx_thread_fn (void *arg)
502 session_fifo_event_t _e, *e = &_e;
503 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
505 utm->client_bytes_received = 0;
508 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
510 switch (e->event_type)
512 case FIFO_EVENT_APP_RX:
513 client_handle_fifo_event_rx (utm, e);
516 case FIFO_EVENT_DISCONNECT:
519 clib_warning ("unknown event type %d", e->event_type);
523 if (PREDICT_FALSE (utm->time_to_stop == 1))
531 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
533 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
536 svm_fifo_t *rx_fifo, *tx_fifo;
541 clib_warning ("connection failed with code: %U", format_api_error,
542 clib_net_to_host_u32 (mp->retval));
543 utm->state = STATE_FAILED;
548 clib_warning ("connected with local ip %U port %d", format_ip46_address,
549 mp->lcl_ip, mp->is_ip4,
550 clib_net_to_host_u16 (mp->lcl_port));
553 utm->vpp_event_queue =
554 uword_to_pointer (mp->vpp_event_queue_address,
555 unix_shared_memory_queue_t *);
561 pool_get (utm->sessions, session);
562 session_index = session - utm->sessions;
564 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
565 rx_fifo->client_session_index = session_index;
566 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
567 tx_fifo->client_session_index = session_index;
569 session->server_rx_fifo = rx_fifo;
570 session->server_tx_fifo = tx_fifo;
571 session->vpp_session_handle = mp->handle;
572 session->start = clib_time_now (&utm->clib_time);
575 utm->connected_session_index = session_index;
576 utm->state = STATE_READY;
578 /* Add it to lookup table */
579 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
581 /* Start RX thread */
582 rv = pthread_create (&utm->client_rx_thread_handle,
583 NULL /*attr */ , client_rx_thread_fn, 0);
586 clib_warning ("pthread_create returned %d", rv);
587 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
592 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
595 u8 *test_data = utm->connect_test_data;
597 int test_buf_offset = 0;
599 u32 queue_max_chunk = 128 << 10, actual_write;
600 session_fifo_event_t evt;
603 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
604 if (bytes_to_snd > vec_len (test_data))
605 bytes_to_snd = vec_len (test_data);
607 while (bytes_to_snd > 0 && !utm->time_to_stop)
609 actual_write = (bytes_to_snd > queue_max_chunk) ?
610 queue_max_chunk : bytes_to_snd;
611 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
612 test_data + test_buf_offset);
617 test_buf_offset += rv;
620 if (svm_fifo_set_event (tx_fifo))
622 /* Fabricate TX event, send to vpp */
624 evt.event_type = FIFO_EVENT_APP_TX;
626 unix_shared_memory_queue_add (utm->vpp_event_queue,
628 0 /* do wait for mutex */ );
635 client_send_data (uri_tcp_test_main_t * utm)
637 u8 *test_data = utm->connect_test_data;
638 int mypid = getpid ();
641 u32 n_iterations, leftover;
644 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
645 tx_fifo = session->server_tx_fifo;
647 ASSERT (vec_len (test_data) > 0);
649 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
650 n_iterations = utm->bytes_to_send / vec_len (test_data);
652 for (i = 0; i < n_iterations; i++)
654 send_test_chunk (utm, tx_fifo, mypid, 0);
655 if (utm->time_to_stop)
659 leftover = utm->bytes_to_send % vec_len (test_data);
661 send_test_chunk (utm, tx_fifo, mypid, leftover);
663 if (!utm->drop_packets)
665 f64 timeout = clib_time_now (&utm->clib_time) + 10;
667 /* Wait for the outstanding packets */
668 while (utm->client_bytes_received <
669 vec_len (test_data) * n_iterations + leftover)
671 if (clib_time_now (&utm->clib_time) > timeout)
673 clib_warning ("timed out waiting for the missing packets");
678 utm->time_to_stop = 1;
682 client_send_connect (uri_tcp_test_main_t * utm)
684 vl_api_connect_uri_t *cmp;
685 cmp = vl_msg_api_alloc (sizeof (*cmp));
686 memset (cmp, 0, sizeof (*cmp));
688 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
689 cmp->client_index = utm->my_client_index;
690 cmp->context = ntohl (0xfeedface);
691 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
692 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
696 client_connect (uri_tcp_test_main_t * utm)
698 client_send_connect (utm);
699 if (wait_for_state_change (utm, STATE_READY))
701 clib_warning ("Connect failed");
708 client_send_disconnect (uri_tcp_test_main_t * utm)
710 session_t *connected_session;
711 vl_api_disconnect_session_t *dmp;
712 connected_session = pool_elt_at_index (utm->sessions,
713 utm->connected_session_index);
714 dmp = vl_msg_api_alloc (sizeof (*dmp));
715 memset (dmp, 0, sizeof (*dmp));
716 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
717 dmp->client_index = utm->my_client_index;
718 dmp->handle = connected_session->vpp_session_handle;
719 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
723 client_disconnect (uri_tcp_test_main_t * utm)
725 client_send_disconnect (utm);
726 clib_warning ("Sent disconnect");
727 if (wait_for_state_change (utm, STATE_START))
729 clib_warning ("Disconnect failed");
736 client_test (uri_tcp_test_main_t * utm)
740 if (application_attach (utm))
743 if (client_connect (utm))
745 application_detach (utm);
750 vec_validate (utm->connect_test_data, 128 * 1024 - 1);
751 for (i = 0; i < vec_len (utm->connect_test_data); i++)
752 utm->connect_test_data[i] = i & 0xff;
755 client_send_data (utm);
758 client_disconnect (utm);
760 application_detach (utm);
764 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
766 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
770 clib_warning ("bind failed: %U", format_api_error,
771 clib_net_to_host_u32 (mp->retval));
772 utm->state = STATE_FAILED;
776 utm->state = STATE_READY;
780 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
782 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
785 clib_warning ("returned %d", ntohl (mp->retval));
787 utm->state = STATE_START;
791 format_ip4_address (u8 * s, va_list * args)
793 u8 *a = va_arg (*args, u8 *);
794 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
798 format_ip6_address (u8 * s, va_list * args)
800 ip6_address_t *a = va_arg (*args, ip6_address_t *);
801 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
803 i_max_n_zero = ARRAY_LEN (a->as_u16);
805 i_first_zero = i_max_n_zero;
807 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
809 u32 is_zero = a->as_u16[i] == 0;
810 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
816 if ((!is_zero && n_zeros > max_n_zeros)
817 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
819 i_max_n_zero = i_first_zero;
820 max_n_zeros = n_zeros;
821 i_first_zero = ARRAY_LEN (a->as_u16);
826 last_double_colon = 0;
827 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
829 if (i == i_max_n_zero && max_n_zeros > 1)
831 s = format (s, "::");
832 i += max_n_zeros - 1;
833 last_double_colon = 1;
837 s = format (s, "%s%x",
838 (last_double_colon || i == 0) ? "" : ":",
839 clib_net_to_host_u16 (a->as_u16[i]));
840 last_double_colon = 0;
847 /* Format an IP46 address. */
849 format_ip46_address (u8 * s, va_list * args)
851 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
852 ip46_type_t type = va_arg (*args, ip46_type_t);
858 is_ip4 = ip46_address_is_ip4 (ip46);
869 format (s, "%U", format_ip4_address, &ip46->ip4) :
870 format (s, "%U", format_ip6_address, &ip46->ip6);
874 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
876 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
877 vl_api_accept_session_reply_t *rmp;
878 svm_fifo_t *rx_fifo, *tx_fifo;
880 static f64 start_time;
884 if (start_time == 0.0)
885 start_time = clib_time_now (&utm->clib_time);
887 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
888 clib_warning ("Accepted session from: %s:%d", ip_str,
889 clib_net_to_host_u16 (mp->port));
890 utm->vpp_event_queue =
891 uword_to_pointer (mp->vpp_event_queue_address,
892 unix_shared_memory_queue_t *);
894 /* Allocate local session and set it up */
895 pool_get (utm->sessions, session);
896 session_index = session - utm->sessions;
898 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
899 rx_fifo->client_session_index = session_index;
900 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
901 tx_fifo->client_session_index = session_index;
903 session->server_rx_fifo = rx_fifo;
904 session->server_tx_fifo = tx_fifo;
906 /* Add it to lookup table */
907 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
909 utm->state = STATE_READY;
912 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
914 f64 now = clib_time_now (&utm->clib_time);
915 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
916 pool_elts (utm->sessions), now - start_time,
917 (f64) pool_elts (utm->sessions) / (now - start_time));
921 * Send accept reply to vpp
923 rmp = vl_msg_api_alloc (sizeof (*rmp));
924 memset (rmp, 0, sizeof (*rmp));
925 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
926 rmp->handle = mp->handle;
927 rmp->context = mp->context;
928 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
930 session->bytes_received = 0;
931 session->start = clib_time_now (&utm->clib_time);
935 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
936 session_fifo_event_t * e)
938 svm_fifo_t *rx_fifo, *tx_fifo;
940 session_fifo_event_t evt;
941 unix_shared_memory_queue_t *q;
944 u32 max_dequeue, offset, max_transfer, rx_buf_len;
946 rx_buf_len = vec_len (utm->rx_buf);
948 session = &utm->sessions[rx_fifo->client_session_index];
949 tx_fifo = session->server_tx_fifo;
951 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
952 /* Allow enqueuing of a new event */
953 svm_fifo_unset_event (rx_fifo);
955 if (PREDICT_FALSE (max_dequeue == 0))
960 /* Read the max_dequeue */
963 max_transfer = clib_min (rx_buf_len, max_dequeue);
964 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
967 max_dequeue -= n_read;
968 session->bytes_received += n_read;
971 /* Reflect if a non-drop session */
972 if (!utm->drop_packets && n_read > 0)
977 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
978 &utm->rx_buf[offset]);
985 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
987 /* If event wasn't set, add one */
988 if (svm_fifo_set_event (tx_fifo))
990 /* Fabricate TX event, send to vpp */
992 evt.event_type = FIFO_EVENT_APP_TX;
994 q = utm->vpp_event_queue;
995 unix_shared_memory_queue_add (q, (u8 *) & evt,
996 1 /* do wait for mutex */ );
1000 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
1004 server_handle_event_queue (uri_tcp_test_main_t * utm)
1006 session_fifo_event_t _e, *e = &_e;
1010 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
1012 switch (e->event_type)
1014 case FIFO_EVENT_APP_RX:
1015 server_handle_fifo_event_rx (utm, e);
1018 case FIFO_EVENT_DISCONNECT:
1022 clib_warning ("unknown event type %d", e->event_type);
1025 if (PREDICT_FALSE (utm->time_to_stop == 1))
1027 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1029 utm->time_to_print_stats = 0;
1030 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1036 server_send_listen (uri_tcp_test_main_t * utm)
1038 vl_api_bind_uri_t *bmp;
1039 bmp = vl_msg_api_alloc (sizeof (*bmp));
1040 memset (bmp, 0, sizeof (*bmp));
1042 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1043 bmp->client_index = utm->my_client_index;
1044 bmp->context = ntohl (0xfeedface);
1045 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1046 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1050 server_listen (uri_tcp_test_main_t * utm)
1052 server_send_listen (utm);
1053 if (wait_for_state_change (utm, STATE_READY))
1055 clib_warning ("timeout waiting for STATE_READY");
1062 server_send_unbind (uri_tcp_test_main_t * utm)
1064 vl_api_unbind_uri_t *ump;
1066 ump = vl_msg_api_alloc (sizeof (*ump));
1067 memset (ump, 0, sizeof (*ump));
1069 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1070 ump->client_index = utm->my_client_index;
1071 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1072 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1076 server_unbind (uri_tcp_test_main_t * utm)
1078 server_send_unbind (utm);
1079 if (wait_for_state_change (utm, STATE_START))
1081 clib_warning ("timeout waiting for STATE_START");
1088 server_test (uri_tcp_test_main_t * utm)
1090 if (application_attach (utm))
1094 if (server_listen (utm))
1097 /* Enter handle event loop */
1098 server_handle_event_queue (utm);
1101 server_send_unbind (utm);
1103 application_detach (utm);
1105 fformat (stdout, "Test complete...\n");
1109 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1112 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1117 clib_warning ("vpp complained about disconnect: %d",
1118 ntohl (mp->retval));
1121 utm->state = STATE_START;
1122 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1124 session_print_stats (utm, session);
1127 #define foreach_uri_msg \
1128 _(BIND_URI_REPLY, bind_uri_reply) \
1129 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1130 _(ACCEPT_SESSION, accept_session) \
1131 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1132 _(DISCONNECT_SESSION, disconnect_session) \
1133 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1134 _(RESET_SESSION, reset_session) \
1135 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1136 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1137 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1140 uri_api_hookup (uri_tcp_test_main_t * utm)
1143 vl_msg_api_set_handlers(VL_API_##N, #n, \
1144 vl_api_##n##_t_handler, \
1146 vl_api_##n##_t_endian, \
1147 vl_api_##n##_t_print, \
1148 sizeof(vl_api_##n##_t), 1);
1154 main (int argc, char **argv)
1156 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1157 unformat_input_t _argv, *a = &_argv;
1160 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1161 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1162 u64 bytes_to_send = 64 << 10, mbytes;
1167 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1169 clib_mem_init (0, 256 << 20);
1171 heap = clib_mem_get_per_cpu_heap ();
1172 h = mheap_header (heap);
1174 /* make the main heap thread-safe */
1175 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1177 vec_validate (utm->rx_buf, 128 << 10);
1179 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1181 utm->my_pid = getpid ();
1182 utm->configured_segment_size = 1 << 20;
1184 clib_time_init (&utm->clib_time);
1185 init_error_string_table (utm);
1186 svm_fifo_segment_init (0x200000000ULL, 20);
1187 unformat_init_command_line (a, argv);
1189 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1191 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1193 vl_set_memory_root_path ((char *) chroot_prefix);
1195 else if (unformat (a, "uri %s", &uri))
1197 else if (unformat (a, "segment-size %dM", &tmp))
1198 utm->configured_segment_size = tmp << 20;
1199 else if (unformat (a, "segment-size %dG", &tmp))
1200 utm->configured_segment_size = tmp << 30;
1201 else if (unformat (a, "master"))
1203 else if (unformat (a, "slave"))
1205 else if (unformat (a, "drop"))
1207 else if (unformat (a, "test"))
1208 test_return_packets = 1;
1209 else if (unformat (a, "mbytes %lld", &mbytes))
1211 bytes_to_send = mbytes << 20;
1213 else if (unformat (a, "gbytes %lld", &mbytes))
1215 bytes_to_send = mbytes << 30;
1219 fformat (stderr, "%s: usage [master|slave]\n");
1226 utm->uri = format (0, "%s%c", uri, 0);
1227 utm->connect_uri = format (0, "%s%c", uri, 0);
1231 utm->uri = format (0, "%s%c", bind_uri, 0);
1232 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1235 utm->i_am_master = i_am_master;
1236 utm->segment_main = &svm_fifo_segment_main;
1237 utm->drop_packets = drop_packets;
1238 utm->test_return_packets = test_return_packets;
1239 utm->bytes_to_send = bytes_to_send;
1240 utm->time_to_stop = 0;
1242 setup_signal_handlers ();
1243 uri_api_hookup (utm);
1245 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
1248 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1252 if (i_am_master == 0)
1255 vl_client_disconnect_from_vlib ();
1259 /* $$$$ hack preallocation */
1260 for (i = 0; i < 200000; i++)
1262 pool_get (utm->sessions, session);
1263 memset (session, 0, sizeof (*session));
1265 for (i = 0; i < 200000; i++)
1266 pool_put_index (utm->sessions, i);
1270 vl_client_disconnect_from_vlib ();
1275 * fd.io coding-style-patch-verification: ON
1278 * eval: (c-set-style "gnu")