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 svm_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 svm_queue_t *our_event_queue;
93 /* $$$ single thread only for the moment */
94 svm_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, svm_queue_t *);
275 utm->state = STATE_ATTACHED;
279 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
283 clib_warning ("detach returned with err: %d", mp->retval);
287 stop_signal (int signum)
289 uri_tcp_test_main_t *um = &uri_tcp_test_main;
291 um->time_to_stop = 1;
295 stats_signal (int signum)
297 uri_tcp_test_main_t *um = &uri_tcp_test_main;
299 um->time_to_print_stats = 1;
302 static clib_error_t *
303 setup_signal_handlers (void)
305 signal (SIGINT, stats_signal);
306 signal (SIGQUIT, stop_signal);
307 signal (SIGTERM, stop_signal);
313 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
315 clib_warning ("BUG");
319 connect_to_vpp (char *name)
321 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
322 api_main_t *am = &api_main;
324 if (utm->use_sock_api)
326 if (vl_socket_client_connect ((char *) utm->socket_name, name,
327 0 /* default rx, tx buffer */ ))
330 return vl_socket_client_init_shm (0);
334 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
336 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
337 utm->my_client_index = am->my_client_index;
343 disconnect_from_vpp (uri_tcp_test_main_t * utm)
345 if (utm->use_sock_api)
346 vl_socket_client_disconnect ();
348 vl_client_disconnect_from_vlib ();
352 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
354 svm_fifo_segment_create_args_t _a, *a = &_a;
357 memset (a, 0, sizeof (*a));
358 a->segment_name = (char *) mp->segment_name;
359 a->segment_size = mp->segment_size;
360 /* Attach to the segment vpp created */
361 rv = svm_fifo_segment_attach (a);
364 clib_warning ("svm_fifo_segment_attach ('%s') failed",
368 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
373 session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
378 deltat = clib_time_now (&utm->clib_time) - session->start;
379 bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
380 fformat (stdout, "Finished in %.6f\n", deltat);
381 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
385 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
387 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
388 session_t *session = 0;
389 vl_api_disconnect_session_reply_t *rmp;
393 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
397 session = pool_elt_at_index (utm->sessions, p[0]);
398 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
399 pool_put (utm->sessions, session);
403 clib_warning ("couldn't find session key %llx", mp->handle);
407 // utm->time_to_stop = 1;
409 rmp = vl_msg_api_alloc (sizeof (*rmp));
410 memset (rmp, 0, sizeof (*rmp));
412 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
414 rmp->handle = mp->handle;
415 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
418 session_print_stats (utm, session);
422 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
424 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
425 vl_api_reset_session_reply_t *rmp;
429 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
433 clib_warning ("got reset");
435 utm->time_to_stop = 1;
439 clib_warning ("couldn't find session key %llx", mp->handle);
443 rmp = vl_msg_api_alloc (sizeof (*rmp));
444 memset (rmp, 0, sizeof (*rmp));
445 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
447 rmp->handle = mp->handle;
448 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
452 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
453 session_fifo_event_t * e)
456 int n_read, bytes, i;
460 bytes = svm_fifo_max_dequeue (rx_fifo);
461 /* Allow enqueuing of new event */
462 svm_fifo_unset_event (rx_fifo);
467 n_read = svm_fifo_dequeue_nowait (rx_fifo,
468 clib_min (vec_len (utm->rx_buf),
469 bytes), utm->rx_buf);
473 if (utm->test_return_packets)
475 for (i = 0; i < n_read; i++)
478 != ((utm->client_bytes_received + i) & 0xff))
480 clib_warning ("error at byte %lld, 0x%x not 0x%x",
481 utm->client_bytes_received + i,
483 ((utm->client_bytes_received +
488 utm->client_bytes_received += n_read;
494 // clib_warning ("weird!");
504 client_handle_event_queue (uri_tcp_test_main_t * utm)
506 session_fifo_event_t _e, *e = &_e;;
508 svm_queue_sub (utm->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
509 switch (e->event_type)
511 case FIFO_EVENT_APP_RX:
512 client_handle_fifo_event_rx (utm, e);
515 case FIFO_EVENT_DISCONNECT:
519 clib_warning ("unknown event type %d", e->event_type);
525 client_rx_thread_fn (void *arg)
527 session_fifo_event_t _e, *e = &_e;
528 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
530 utm->client_bytes_received = 0;
533 svm_queue_sub (utm->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
534 switch (e->event_type)
536 case FIFO_EVENT_APP_RX:
537 client_handle_fifo_event_rx (utm, e);
540 case FIFO_EVENT_DISCONNECT:
543 clib_warning ("unknown event type %d", e->event_type);
547 if (PREDICT_FALSE (utm->time_to_stop == 1))
555 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
557 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
560 svm_fifo_t *rx_fifo, *tx_fifo;
565 clib_warning ("connection failed with code: %U", format_api_error,
566 clib_net_to_host_u32 (mp->retval));
567 utm->state = STATE_FAILED;
572 clib_warning ("connected with local ip %U port %d", format_ip46_address,
573 mp->lcl_ip, mp->is_ip4,
574 clib_net_to_host_u16 (mp->lcl_port));
577 utm->vpp_event_queue =
578 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
584 pool_get (utm->sessions, session);
585 session_index = session - utm->sessions;
587 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
588 rx_fifo->client_session_index = session_index;
589 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
590 tx_fifo->client_session_index = session_index;
592 session->server_rx_fifo = rx_fifo;
593 session->server_tx_fifo = tx_fifo;
594 session->vpp_session_handle = mp->handle;
595 session->start = clib_time_now (&utm->clib_time);
598 utm->connected_session_index = session_index;
599 utm->state = STATE_READY;
601 /* Add it to lookup table */
602 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
604 /* Start RX thread */
605 rv = pthread_create (&utm->client_rx_thread_handle,
606 NULL /*attr */ , client_rx_thread_fn, 0);
609 clib_warning ("pthread_create returned %d", rv);
610 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
615 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
618 u8 *test_data = utm->connect_test_data;
620 int test_buf_offset = 0;
622 u32 queue_max_chunk = 128 << 10, actual_write;
623 session_fifo_event_t evt;
626 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
627 if (bytes_to_snd > vec_len (test_data))
628 bytes_to_snd = vec_len (test_data);
630 while (bytes_to_snd > 0 && !utm->time_to_stop)
632 actual_write = (bytes_to_snd > queue_max_chunk) ?
633 queue_max_chunk : bytes_to_snd;
634 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
635 test_data + test_buf_offset);
640 test_buf_offset += rv;
643 if (svm_fifo_set_event (tx_fifo))
645 /* Fabricate TX event, send to vpp */
647 evt.event_type = FIFO_EVENT_APP_TX;
649 svm_queue_add (utm->vpp_event_queue,
650 (u8 *) & evt, 0 /* do wait for mutex */ );
657 client_send_data (uri_tcp_test_main_t * utm)
659 u8 *test_data = utm->connect_test_data;
660 int mypid = getpid ();
663 u32 n_iterations, leftover;
666 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
667 tx_fifo = session->server_tx_fifo;
669 ASSERT (vec_len (test_data) > 0);
671 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
672 n_iterations = utm->bytes_to_send / vec_len (test_data);
674 for (i = 0; i < n_iterations; i++)
676 send_test_chunk (utm, tx_fifo, mypid, 0);
677 if (utm->time_to_stop)
681 leftover = utm->bytes_to_send % vec_len (test_data);
683 send_test_chunk (utm, tx_fifo, mypid, leftover);
685 if (!utm->drop_packets)
687 f64 timeout = clib_time_now (&utm->clib_time) + 10;
689 /* Wait for the outstanding packets */
690 while (utm->client_bytes_received <
691 vec_len (test_data) * n_iterations + leftover)
693 if (clib_time_now (&utm->clib_time) > timeout)
695 clib_warning ("timed out waiting for the missing packets");
700 utm->time_to_stop = 1;
704 client_send_connect (uri_tcp_test_main_t * utm)
706 vl_api_connect_uri_t *cmp;
707 cmp = vl_msg_api_alloc (sizeof (*cmp));
708 memset (cmp, 0, sizeof (*cmp));
710 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
711 cmp->client_index = utm->my_client_index;
712 cmp->context = ntohl (0xfeedface);
713 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
714 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
718 client_connect (uri_tcp_test_main_t * utm)
720 client_send_connect (utm);
721 if (wait_for_state_change (utm, STATE_READY))
723 clib_warning ("Connect failed");
730 client_send_disconnect (uri_tcp_test_main_t * utm)
732 session_t *connected_session;
733 vl_api_disconnect_session_t *dmp;
734 connected_session = pool_elt_at_index (utm->sessions,
735 utm->connected_session_index);
736 dmp = vl_msg_api_alloc (sizeof (*dmp));
737 memset (dmp, 0, sizeof (*dmp));
738 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
739 dmp->client_index = utm->my_client_index;
740 dmp->handle = connected_session->vpp_session_handle;
741 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
745 client_disconnect (uri_tcp_test_main_t * utm)
747 client_send_disconnect (utm);
748 clib_warning ("Sent disconnect");
749 if (wait_for_state_change (utm, STATE_START))
751 clib_warning ("Disconnect failed");
758 client_run (uri_tcp_test_main_t * utm)
762 if (application_attach (utm))
765 if (client_connect (utm))
767 application_detach (utm);
772 vec_validate (utm->connect_test_data, 128 * 1024 - 1);
773 for (i = 0; i < vec_len (utm->connect_test_data); i++)
774 utm->connect_test_data[i] = i & 0xff;
777 client_send_data (utm);
780 client_disconnect (utm);
782 application_detach (utm);
786 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
788 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
792 clib_warning ("bind failed: %U", format_api_error,
793 clib_net_to_host_u32 (mp->retval));
794 utm->state = STATE_FAILED;
798 utm->state = STATE_READY;
802 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
804 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
807 clib_warning ("returned %d", ntohl (mp->retval));
809 utm->state = STATE_START;
813 format_ip4_address (u8 * s, va_list * args)
815 u8 *a = va_arg (*args, u8 *);
816 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
820 format_ip6_address (u8 * s, va_list * args)
822 ip6_address_t *a = va_arg (*args, ip6_address_t *);
823 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
825 i_max_n_zero = ARRAY_LEN (a->as_u16);
827 i_first_zero = i_max_n_zero;
829 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
831 u32 is_zero = a->as_u16[i] == 0;
832 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
838 if ((!is_zero && n_zeros > max_n_zeros)
839 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
841 i_max_n_zero = i_first_zero;
842 max_n_zeros = n_zeros;
843 i_first_zero = ARRAY_LEN (a->as_u16);
848 last_double_colon = 0;
849 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
851 if (i == i_max_n_zero && max_n_zeros > 1)
853 s = format (s, "::");
854 i += max_n_zeros - 1;
855 last_double_colon = 1;
859 s = format (s, "%s%x",
860 (last_double_colon || i == 0) ? "" : ":",
861 clib_net_to_host_u16 (a->as_u16[i]));
862 last_double_colon = 0;
869 /* Format an IP46 address. */
871 format_ip46_address (u8 * s, va_list * args)
873 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
874 ip46_type_t type = va_arg (*args, ip46_type_t);
880 is_ip4 = ip46_address_is_ip4 (ip46);
891 format (s, "%U", format_ip4_address, &ip46->ip4) :
892 format (s, "%U", format_ip6_address, &ip46->ip6);
896 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
898 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
899 vl_api_accept_session_reply_t *rmp;
900 svm_fifo_t *rx_fifo, *tx_fifo;
902 static f64 start_time;
906 if (start_time == 0.0)
907 start_time = clib_time_now (&utm->clib_time);
909 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
910 clib_warning ("Accepted session from: %s:%d", ip_str,
911 clib_net_to_host_u16 (mp->port));
912 utm->vpp_event_queue =
913 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
915 /* Allocate local session and set it up */
916 pool_get (utm->sessions, session);
917 session_index = session - utm->sessions;
919 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
920 rx_fifo->client_session_index = session_index;
921 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
922 tx_fifo->client_session_index = session_index;
924 session->server_rx_fifo = rx_fifo;
925 session->server_tx_fifo = tx_fifo;
927 /* Add it to lookup table */
928 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
930 utm->state = STATE_READY;
933 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
935 f64 now = clib_time_now (&utm->clib_time);
936 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
937 pool_elts (utm->sessions), now - start_time,
938 (f64) pool_elts (utm->sessions) / (now - start_time));
942 * Send accept reply to vpp
944 rmp = vl_msg_api_alloc (sizeof (*rmp));
945 memset (rmp, 0, sizeof (*rmp));
946 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
947 rmp->handle = mp->handle;
948 rmp->context = mp->context;
949 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
951 session->bytes_received = 0;
952 session->start = clib_time_now (&utm->clib_time);
956 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
957 session_fifo_event_t * e)
959 svm_fifo_t *rx_fifo, *tx_fifo;
961 session_fifo_event_t evt;
965 u32 max_dequeue, offset, max_transfer, rx_buf_len;
967 rx_buf_len = vec_len (utm->rx_buf);
969 session = &utm->sessions[rx_fifo->client_session_index];
970 tx_fifo = session->server_tx_fifo;
972 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
973 /* Allow enqueuing of a new event */
974 svm_fifo_unset_event (rx_fifo);
976 if (PREDICT_FALSE (max_dequeue == 0))
981 /* Read the max_dequeue */
984 max_transfer = clib_min (rx_buf_len, max_dequeue);
985 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
988 max_dequeue -= n_read;
989 session->bytes_received += n_read;
992 /* Reflect if a non-drop session */
993 if (!utm->drop_packets && n_read > 0)
998 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
999 &utm->rx_buf[offset]);
1006 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
1008 /* If event wasn't set, add one */
1009 if (svm_fifo_set_event (tx_fifo))
1011 /* Fabricate TX event, send to vpp */
1013 evt.event_type = FIFO_EVENT_APP_TX;
1015 q = utm->vpp_event_queue;
1016 svm_queue_add (q, (u8 *) & evt, 1 /* do wait for mutex */ );
1020 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
1024 server_handle_event_queue (uri_tcp_test_main_t * utm)
1026 session_fifo_event_t _e, *e = &_e;
1030 svm_queue_sub (utm->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
1031 switch (e->event_type)
1033 case FIFO_EVENT_APP_RX:
1034 server_handle_fifo_event_rx (utm, e);
1037 case FIFO_EVENT_DISCONNECT:
1041 clib_warning ("unknown event type %d", e->event_type);
1044 if (PREDICT_FALSE (utm->time_to_stop == 1))
1046 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1048 utm->time_to_print_stats = 0;
1049 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1055 server_send_listen (uri_tcp_test_main_t * utm)
1057 vl_api_bind_uri_t *bmp;
1058 bmp = vl_msg_api_alloc (sizeof (*bmp));
1059 memset (bmp, 0, sizeof (*bmp));
1061 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1062 bmp->client_index = utm->my_client_index;
1063 bmp->context = ntohl (0xfeedface);
1064 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1065 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1069 server_listen (uri_tcp_test_main_t * utm)
1071 server_send_listen (utm);
1072 if (wait_for_state_change (utm, STATE_READY))
1074 clib_warning ("timeout waiting for STATE_READY");
1081 server_send_unbind (uri_tcp_test_main_t * utm)
1083 vl_api_unbind_uri_t *ump;
1085 ump = vl_msg_api_alloc (sizeof (*ump));
1086 memset (ump, 0, sizeof (*ump));
1088 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1089 ump->client_index = utm->my_client_index;
1090 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1091 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1095 server_unbind (uri_tcp_test_main_t * utm)
1097 server_send_unbind (utm);
1098 if (wait_for_state_change (utm, STATE_START))
1100 clib_warning ("timeout waiting for STATE_START");
1107 server_run (uri_tcp_test_main_t * utm)
1112 /* $$$$ hack preallocation */
1113 for (i = 0; i < 200000; i++)
1115 pool_get (utm->sessions, session);
1116 memset (session, 0, sizeof (*session));
1118 for (i = 0; i < 200000; i++)
1119 pool_put_index (utm->sessions, i);
1121 if (application_attach (utm))
1125 if (server_listen (utm))
1128 /* Enter handle event loop */
1129 server_handle_event_queue (utm);
1132 server_send_unbind (utm);
1134 application_detach (utm);
1136 fformat (stdout, "Test complete...\n");
1140 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1143 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1148 clib_warning ("vpp complained about disconnect: %d",
1149 ntohl (mp->retval));
1152 utm->state = STATE_START;
1153 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1155 session_print_stats (utm, session);
1158 #define foreach_uri_msg \
1159 _(BIND_URI_REPLY, bind_uri_reply) \
1160 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1161 _(ACCEPT_SESSION, accept_session) \
1162 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1163 _(DISCONNECT_SESSION, disconnect_session) \
1164 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1165 _(RESET_SESSION, reset_session) \
1166 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1167 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1168 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1171 uri_api_hookup (uri_tcp_test_main_t * utm)
1174 vl_msg_api_set_handlers(VL_API_##N, #n, \
1175 vl_api_##n##_t_handler, \
1177 vl_api_##n##_t_endian, \
1178 vl_api_##n##_t_print, \
1179 sizeof(vl_api_##n##_t), 1);
1185 main (int argc, char **argv)
1187 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1188 unformat_input_t _argv, *a = &_argv;
1191 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1192 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1193 u64 bytes_to_send = 64 << 10, mbytes;
1196 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1198 clib_mem_init (0, 256 << 20);
1200 heap = clib_mem_get_per_cpu_heap ();
1201 h = mheap_header (heap);
1203 /* make the main heap thread-safe */
1204 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1206 vec_validate (utm->rx_buf, 128 << 10);
1208 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1210 utm->my_pid = getpid ();
1211 utm->configured_segment_size = 1 << 20;
1212 utm->socket_name = 0;
1213 utm->use_sock_api = 1;
1215 clib_time_init (&utm->clib_time);
1216 init_error_string_table (utm);
1217 svm_fifo_segment_init (0x200000000ULL, 20);
1218 unformat_init_command_line (a, argv);
1220 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1222 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1224 vl_set_memory_root_path ((char *) chroot_prefix);
1226 else if (unformat (a, "uri %s", &uri))
1228 else if (unformat (a, "segment-size %dM", &tmp))
1229 utm->configured_segment_size = tmp << 20;
1230 else if (unformat (a, "segment-size %dG", &tmp))
1231 utm->configured_segment_size = tmp << 30;
1232 else if (unformat (a, "master"))
1234 else if (unformat (a, "slave"))
1236 else if (unformat (a, "drop"))
1238 else if (unformat (a, "test"))
1239 test_return_packets = 1;
1240 else if (unformat (a, "mbytes %lld", &mbytes))
1242 bytes_to_send = mbytes << 20;
1244 else if (unformat (a, "gbytes %lld", &mbytes))
1246 bytes_to_send = mbytes << 30;
1248 else if (unformat (a, "socket-name %s", &utm->socket_name))
1250 else if (unformat (a, "use-svm-api"))
1251 utm->use_sock_api = 0;
1254 fformat (stderr, "%s: usage [master|slave]\n");
1259 if (!utm->socket_name)
1260 utm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
1264 utm->uri = format (0, "%s%c", uri, 0);
1265 utm->connect_uri = format (0, "%s%c", uri, 0);
1269 utm->uri = format (0, "%s%c", bind_uri, 0);
1270 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1273 utm->i_am_master = i_am_master;
1274 utm->segment_main = &svm_fifo_segment_main;
1275 utm->drop_packets = drop_packets;
1276 utm->test_return_packets = test_return_packets;
1277 utm->bytes_to_send = bytes_to_send;
1278 utm->time_to_stop = 0;
1280 setup_signal_handlers ();
1281 uri_api_hookup (utm);
1283 if (connect_to_vpp (i_am_master ? "tcp_echo_server" : "tcp_echo_client") <
1287 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1291 if (i_am_master == 0)
1296 disconnect_from_vpp (utm);
1301 * fd.io coding-style-patch-verification: ON
1304 * eval: (c-set-style "gnu")