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[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size;
196 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size;
197 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
198 bmp->options[SESSION_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 a->segment_name = (char *) mp->segment_name;
250 a->segment_size = mp->segment_size;
252 ASSERT (mp->app_event_queue_address);
254 /* Attach to the segment vpp created */
255 rv = svm_fifo_segment_attach (a);
258 clib_warning ("svm_fifo_segment_attach ('%s') failed",
263 utm->our_event_queue =
264 uword_to_pointer (mp->app_event_queue_address,
265 unix_shared_memory_queue_t *);
266 utm->state = STATE_ATTACHED;
270 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
274 clib_warning ("detach returned with err: %d", mp->retval);
278 stop_signal (int signum)
280 uri_tcp_test_main_t *um = &uri_tcp_test_main;
282 um->time_to_stop = 1;
286 stats_signal (int signum)
288 uri_tcp_test_main_t *um = &uri_tcp_test_main;
290 um->time_to_print_stats = 1;
293 static clib_error_t *
294 setup_signal_handlers (void)
296 signal (SIGINT, stats_signal);
297 signal (SIGQUIT, stop_signal);
298 signal (SIGTERM, stop_signal);
304 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
306 clib_warning ("BUG");
310 connect_to_vpp (char *name)
312 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
313 api_main_t *am = &api_main;
315 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
318 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
319 utm->my_client_index = am->my_client_index;
325 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
327 svm_fifo_segment_create_args_t _a, *a = &_a;
330 memset (a, 0, sizeof (*a));
331 a->segment_name = (char *) mp->segment_name;
332 a->segment_size = mp->segment_size;
333 /* Attach to the segment vpp created */
334 rv = svm_fifo_segment_attach (a);
337 clib_warning ("svm_fifo_segment_attach ('%s') failed",
341 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
346 session_print_stats (uri_tcp_test_main_t * utm, session_t * session)
351 deltat = clib_time_now (&utm->clib_time) - session->start;
352 bytes = utm->i_am_master ? session->bytes_received : utm->bytes_to_send;
353 fformat (stdout, "Finished in %.6f\n", deltat);
354 fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9);
358 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
360 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
361 session_t *session = 0;
362 vl_api_disconnect_session_reply_t *rmp;
366 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
370 session = pool_elt_at_index (utm->sessions, p[0]);
371 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
372 pool_put (utm->sessions, session);
376 clib_warning ("couldn't find session key %llx", mp->handle);
380 // utm->time_to_stop = 1;
382 rmp = vl_msg_api_alloc (sizeof (*rmp));
383 memset (rmp, 0, sizeof (*rmp));
385 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
387 rmp->handle = mp->handle;
388 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
391 session_print_stats (utm, session);
395 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
397 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
398 vl_api_reset_session_reply_t *rmp;
402 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
406 clib_warning ("got reset");
408 utm->time_to_stop = 1;
412 clib_warning ("couldn't find session key %llx", mp->handle);
416 rmp = vl_msg_api_alloc (sizeof (*rmp));
417 memset (rmp, 0, sizeof (*rmp));
418 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
420 rmp->handle = mp->handle;
421 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
425 client_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
426 session_fifo_event_t * e)
429 int n_read, bytes, i;
433 bytes = svm_fifo_max_dequeue (rx_fifo);
434 /* Allow enqueuing of new event */
435 svm_fifo_unset_event (rx_fifo);
440 n_read = svm_fifo_dequeue_nowait (rx_fifo,
441 clib_min (vec_len (utm->rx_buf),
442 bytes), utm->rx_buf);
446 if (utm->test_return_packets)
448 for (i = 0; i < n_read; i++)
451 != ((utm->client_bytes_received + i) & 0xff))
453 clib_warning ("error at byte %lld, 0x%x not 0x%x",
454 utm->client_bytes_received + i,
456 ((utm->client_bytes_received +
461 utm->client_bytes_received += n_read;
467 // clib_warning ("weird!");
477 client_handle_event_queue (uri_tcp_test_main_t * utm)
479 session_fifo_event_t _e, *e = &_e;;
481 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
483 switch (e->event_type)
485 case FIFO_EVENT_APP_RX:
486 client_handle_fifo_event_rx (utm, e);
489 case FIFO_EVENT_DISCONNECT:
493 clib_warning ("unknown event type %d", e->event_type);
499 client_rx_thread_fn (void *arg)
501 session_fifo_event_t _e, *e = &_e;
502 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
504 utm->client_bytes_received = 0;
507 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
509 switch (e->event_type)
511 case FIFO_EVENT_APP_RX:
512 client_handle_fifo_event_rx (utm, e);
515 case FIFO_EVENT_DISCONNECT:
518 clib_warning ("unknown event type %d", e->event_type);
522 if (PREDICT_FALSE (utm->time_to_stop == 1))
530 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
532 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
535 svm_fifo_t *rx_fifo, *tx_fifo;
540 clib_warning ("connection failed with code: %U", format_api_error,
541 clib_net_to_host_u32 (mp->retval));
542 utm->state = STATE_FAILED;
546 utm->vpp_event_queue =
547 uword_to_pointer (mp->vpp_event_queue_address,
548 unix_shared_memory_queue_t *);
554 pool_get (utm->sessions, session);
555 session_index = session - utm->sessions;
557 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
558 rx_fifo->client_session_index = session_index;
559 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
560 tx_fifo->client_session_index = session_index;
562 session->server_rx_fifo = rx_fifo;
563 session->server_tx_fifo = tx_fifo;
564 session->vpp_session_handle = mp->handle;
565 session->start = clib_time_now (&utm->clib_time);
568 utm->connected_session_index = session_index;
569 utm->state = STATE_READY;
571 /* Add it to lookup table */
572 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
574 /* Start RX thread */
575 rv = pthread_create (&utm->client_rx_thread_handle,
576 NULL /*attr */ , client_rx_thread_fn, 0);
579 clib_warning ("pthread_create returned %d", rv);
580 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
585 send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
588 u8 *test_data = utm->connect_test_data;
590 int test_buf_offset = 0;
592 u32 queue_max_chunk = 128 << 10, actual_write;
593 session_fifo_event_t evt;
596 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
597 if (bytes_to_snd > vec_len (test_data))
598 bytes_to_snd = vec_len (test_data);
600 while (bytes_to_snd > 0 && !utm->time_to_stop)
602 actual_write = (bytes_to_snd > queue_max_chunk) ?
603 queue_max_chunk : bytes_to_snd;
604 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
605 test_data + test_buf_offset);
610 test_buf_offset += rv;
613 if (svm_fifo_set_event (tx_fifo))
615 /* Fabricate TX event, send to vpp */
617 evt.event_type = FIFO_EVENT_APP_TX;
619 unix_shared_memory_queue_add (utm->vpp_event_queue,
621 0 /* do wait for mutex */ );
628 client_send_data (uri_tcp_test_main_t * utm)
630 u8 *test_data = utm->connect_test_data;
631 int mypid = getpid ();
634 u32 n_iterations, leftover;
637 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
638 tx_fifo = session->server_tx_fifo;
640 ASSERT (vec_len (test_data) > 0);
642 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
643 n_iterations = utm->bytes_to_send / vec_len (test_data);
645 for (i = 0; i < n_iterations; i++)
647 send_test_chunk (utm, tx_fifo, mypid, 0);
648 if (utm->time_to_stop)
652 leftover = utm->bytes_to_send % vec_len (test_data);
654 send_test_chunk (utm, tx_fifo, mypid, leftover);
656 if (!utm->drop_packets)
658 f64 timeout = clib_time_now (&utm->clib_time) + 10;
660 /* Wait for the outstanding packets */
661 while (utm->client_bytes_received <
662 vec_len (test_data) * n_iterations + leftover)
664 if (clib_time_now (&utm->clib_time) > timeout)
666 clib_warning ("timed out waiting for the missing packets");
671 utm->time_to_stop = 1;
675 client_send_connect (uri_tcp_test_main_t * utm)
677 vl_api_connect_uri_t *cmp;
678 cmp = vl_msg_api_alloc (sizeof (*cmp));
679 memset (cmp, 0, sizeof (*cmp));
681 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
682 cmp->client_index = utm->my_client_index;
683 cmp->context = ntohl (0xfeedface);
684 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
685 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
689 client_connect (uri_tcp_test_main_t * utm)
691 client_send_connect (utm);
692 if (wait_for_state_change (utm, STATE_READY))
694 clib_warning ("Connect failed");
701 client_send_disconnect (uri_tcp_test_main_t * utm)
703 session_t *connected_session;
704 vl_api_disconnect_session_t *dmp;
705 connected_session = pool_elt_at_index (utm->sessions,
706 utm->connected_session_index);
707 dmp = vl_msg_api_alloc (sizeof (*dmp));
708 memset (dmp, 0, sizeof (*dmp));
709 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
710 dmp->client_index = utm->my_client_index;
711 dmp->handle = connected_session->vpp_session_handle;
712 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp);
716 client_disconnect (uri_tcp_test_main_t * utm)
718 client_send_disconnect (utm);
719 clib_warning ("Sent disconnect");
720 if (wait_for_state_change (utm, STATE_START))
722 clib_warning ("Disconnect failed");
729 client_test (uri_tcp_test_main_t * utm)
733 if (application_attach (utm))
736 if (client_connect (utm))
738 application_detach (utm);
743 vec_validate (utm->connect_test_data, 128 * 1024 - 1);
744 for (i = 0; i < vec_len (utm->connect_test_data); i++)
745 utm->connect_test_data[i] = i & 0xff;
748 client_send_data (utm);
751 client_disconnect (utm);
753 application_detach (utm);
757 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
759 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
763 clib_warning ("bind failed: %U", format_api_error,
764 clib_net_to_host_u32 (mp->retval));
765 utm->state = STATE_FAILED;
769 utm->state = STATE_READY;
773 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
775 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
778 clib_warning ("returned %d", ntohl (mp->retval));
780 utm->state = STATE_START;
784 format_ip4_address (u8 * s, va_list * args)
786 u8 *a = va_arg (*args, u8 *);
787 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
791 format_ip6_address (u8 * s, va_list * args)
793 ip6_address_t *a = va_arg (*args, ip6_address_t *);
794 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
796 i_max_n_zero = ARRAY_LEN (a->as_u16);
798 i_first_zero = i_max_n_zero;
800 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
802 u32 is_zero = a->as_u16[i] == 0;
803 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
809 if ((!is_zero && n_zeros > max_n_zeros)
810 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
812 i_max_n_zero = i_first_zero;
813 max_n_zeros = n_zeros;
814 i_first_zero = ARRAY_LEN (a->as_u16);
819 last_double_colon = 0;
820 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
822 if (i == i_max_n_zero && max_n_zeros > 1)
824 s = format (s, "::");
825 i += max_n_zeros - 1;
826 last_double_colon = 1;
830 s = format (s, "%s%x",
831 (last_double_colon || i == 0) ? "" : ":",
832 clib_net_to_host_u16 (a->as_u16[i]));
833 last_double_colon = 0;
840 /* Format an IP46 address. */
842 format_ip46_address (u8 * s, va_list * args)
844 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
845 ip46_type_t type = va_arg (*args, ip46_type_t);
851 is_ip4 = ip46_address_is_ip4 (ip46);
862 format (s, "%U", format_ip4_address, &ip46->ip4) :
863 format (s, "%U", format_ip6_address, &ip46->ip6);
867 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
869 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
870 vl_api_accept_session_reply_t *rmp;
871 svm_fifo_t *rx_fifo, *tx_fifo;
873 static f64 start_time;
877 if (start_time == 0.0)
878 start_time = clib_time_now (&utm->clib_time);
880 ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
881 clib_warning ("Accepted session from: %s:%d", ip_str,
882 clib_net_to_host_u16 (mp->port));
883 utm->vpp_event_queue =
884 uword_to_pointer (mp->vpp_event_queue_address,
885 unix_shared_memory_queue_t *);
887 /* Allocate local session and set it up */
888 pool_get (utm->sessions, session);
889 session_index = session - utm->sessions;
891 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
892 rx_fifo->client_session_index = session_index;
893 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
894 tx_fifo->client_session_index = session_index;
896 session->server_rx_fifo = rx_fifo;
897 session->server_tx_fifo = tx_fifo;
899 /* Add it to lookup table */
900 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
902 utm->state = STATE_READY;
905 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
907 f64 now = clib_time_now (&utm->clib_time);
908 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
909 pool_elts (utm->sessions), now - start_time,
910 (f64) pool_elts (utm->sessions) / (now - start_time));
914 * Send accept reply to vpp
916 rmp = vl_msg_api_alloc (sizeof (*rmp));
917 memset (rmp, 0, sizeof (*rmp));
918 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
919 rmp->handle = mp->handle;
920 rmp->context = mp->context;
921 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
923 session->bytes_received = 0;
924 session->start = clib_time_now (&utm->clib_time);
928 server_handle_fifo_event_rx (uri_tcp_test_main_t * utm,
929 session_fifo_event_t * e)
931 svm_fifo_t *rx_fifo, *tx_fifo;
933 session_fifo_event_t evt;
934 unix_shared_memory_queue_t *q;
937 u32 max_dequeue, offset, max_transfer, rx_buf_len;
939 rx_buf_len = vec_len (utm->rx_buf);
941 session = &utm->sessions[rx_fifo->client_session_index];
942 tx_fifo = session->server_tx_fifo;
944 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
945 /* Allow enqueuing of a new event */
946 svm_fifo_unset_event (rx_fifo);
948 if (PREDICT_FALSE (max_dequeue == 0))
953 /* Read the max_dequeue */
956 max_transfer = clib_min (rx_buf_len, max_dequeue);
957 n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, utm->rx_buf);
960 max_dequeue -= n_read;
961 session->bytes_received += n_read;
964 /* Reflect if a non-drop session */
965 if (!utm->drop_packets && n_read > 0)
970 rv = svm_fifo_enqueue_nowait (tx_fifo, n_read,
971 &utm->rx_buf[offset]);
978 while ((rv <= 0 || n_read > 0) && !utm->time_to_stop);
980 /* If event wasn't set, add one */
981 if (svm_fifo_set_event (tx_fifo))
983 /* Fabricate TX event, send to vpp */
985 evt.event_type = FIFO_EVENT_APP_TX;
987 q = utm->vpp_event_queue;
988 unix_shared_memory_queue_add (q, (u8 *) & evt,
989 1 /* do wait for mutex */ );
993 while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop);
997 server_handle_event_queue (uri_tcp_test_main_t * utm)
999 session_fifo_event_t _e, *e = &_e;
1003 unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e,
1005 switch (e->event_type)
1007 case FIFO_EVENT_APP_RX:
1008 server_handle_fifo_event_rx (utm, e);
1011 case FIFO_EVENT_DISCONNECT:
1015 clib_warning ("unknown event type %d", e->event_type);
1018 if (PREDICT_FALSE (utm->time_to_stop == 1))
1020 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1022 utm->time_to_print_stats = 0;
1023 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1029 server_send_listen (uri_tcp_test_main_t * utm)
1031 vl_api_bind_uri_t *bmp;
1032 bmp = vl_msg_api_alloc (sizeof (*bmp));
1033 memset (bmp, 0, sizeof (*bmp));
1035 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1036 bmp->client_index = utm->my_client_index;
1037 bmp->context = ntohl (0xfeedface);
1038 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1039 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1043 server_listen (uri_tcp_test_main_t * utm)
1045 server_send_listen (utm);
1046 if (wait_for_state_change (utm, STATE_READY))
1048 clib_warning ("timeout waiting for STATE_READY");
1055 server_send_unbind (uri_tcp_test_main_t * utm)
1057 vl_api_unbind_uri_t *ump;
1059 ump = vl_msg_api_alloc (sizeof (*ump));
1060 memset (ump, 0, sizeof (*ump));
1062 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1063 ump->client_index = utm->my_client_index;
1064 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1065 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1069 server_unbind (uri_tcp_test_main_t * utm)
1071 server_send_unbind (utm);
1072 if (wait_for_state_change (utm, STATE_START))
1074 clib_warning ("timeout waiting for STATE_START");
1081 server_test (uri_tcp_test_main_t * utm)
1083 if (application_attach (utm))
1087 if (server_listen (utm))
1090 /* Enter handle event loop */
1091 server_handle_event_queue (utm);
1094 server_send_unbind (utm);
1096 application_detach (utm);
1098 fformat (stdout, "Test complete...\n");
1102 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1105 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1110 clib_warning ("vpp complained about disconnect: %d",
1111 ntohl (mp->retval));
1114 utm->state = STATE_START;
1115 session = pool_elt_at_index (utm->sessions, utm->connected_session_index);
1117 session_print_stats (utm, session);
1120 #define foreach_uri_msg \
1121 _(BIND_URI_REPLY, bind_uri_reply) \
1122 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1123 _(ACCEPT_SESSION, accept_session) \
1124 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1125 _(DISCONNECT_SESSION, disconnect_session) \
1126 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1127 _(RESET_SESSION, reset_session) \
1128 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1129 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1130 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1133 uri_api_hookup (uri_tcp_test_main_t * utm)
1136 vl_msg_api_set_handlers(VL_API_##N, #n, \
1137 vl_api_##n##_t_handler, \
1139 vl_api_##n##_t_endian, \
1140 vl_api_##n##_t_print, \
1141 sizeof(vl_api_##n##_t), 1);
1147 main (int argc, char **argv)
1149 uri_tcp_test_main_t *utm = &uri_tcp_test_main;
1150 unformat_input_t _argv, *a = &_argv;
1153 u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234";
1154 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
1155 u64 bytes_to_send = 64 << 10, mbytes;
1160 int i_am_master = 1, drop_packets = 0, test_return_packets = 0;
1162 clib_mem_init (0, 256 << 20);
1164 heap = clib_mem_get_per_cpu_heap ();
1165 h = mheap_header (heap);
1167 /* make the main heap thread-safe */
1168 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1170 vec_validate (utm->rx_buf, 128 << 10);
1172 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1174 utm->my_pid = getpid ();
1175 utm->configured_segment_size = 1 << 20;
1177 clib_time_init (&utm->clib_time);
1178 init_error_string_table (utm);
1179 svm_fifo_segment_init (0x200000000ULL, 20);
1180 unformat_init_command_line (a, argv);
1182 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1184 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1186 vl_set_memory_root_path ((char *) chroot_prefix);
1188 else if (unformat (a, "uri %s", &uri))
1190 else if (unformat (a, "segment-size %dM", &tmp))
1191 utm->configured_segment_size = tmp << 20;
1192 else if (unformat (a, "segment-size %dG", &tmp))
1193 utm->configured_segment_size = tmp << 30;
1194 else if (unformat (a, "master"))
1196 else if (unformat (a, "slave"))
1198 else if (unformat (a, "drop"))
1200 else if (unformat (a, "test"))
1201 test_return_packets = 1;
1202 else if (unformat (a, "mbytes %lld", &mbytes))
1204 bytes_to_send = mbytes << 20;
1206 else if (unformat (a, "gbytes %lld", &mbytes))
1208 bytes_to_send = mbytes << 30;
1212 fformat (stderr, "%s: usage [master|slave]\n");
1219 utm->uri = format (0, "%s%c", uri, 0);
1220 utm->connect_uri = format (0, "%s%c", uri, 0);
1224 utm->uri = format (0, "%s%c", bind_uri, 0);
1225 utm->connect_uri = format (0, "%s%c", connect_uri, 0);
1228 utm->i_am_master = i_am_master;
1229 utm->segment_main = &svm_fifo_segment_main;
1230 utm->drop_packets = drop_packets;
1231 utm->test_return_packets = test_return_packets;
1232 utm->bytes_to_send = bytes_to_send;
1233 utm->time_to_stop = 0;
1235 setup_signal_handlers ();
1236 uri_api_hookup (utm);
1238 if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0)
1241 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1245 if (i_am_master == 0)
1248 vl_client_disconnect_from_vlib ();
1252 /* $$$$ hack preallocation */
1253 for (i = 0; i < 200000; i++)
1255 pool_get (utm->sessions, session);
1256 memset (session, 0, sizeof (*session));
1258 for (i = 0; i < 200000; i++)
1259 pool_put_index (utm->sessions, i);
1263 vl_client_disconnect_from_vlib ();
1268 * fd.io coding-style-patch-verification: ON
1271 * eval: (c-set-style "gnu")