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 <vppinfra/clib.h>
20 #include <vppinfra/format.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/time.h>
23 #include <vppinfra/macros.h>
24 #include <vnet/vnet.h>
25 #include <vlib/vlib.h>
26 #include <vlib/unix/unix.h>
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
29 #include <vpp/api/vpe_msg_enum.h>
30 #include <svm/svm_fifo_segment.h>
32 #include <vnet/session/application_interface.h>
34 #define vl_typedefs /* define message structures */
35 #include <vpp/api/vpe_all_api_h.h>
38 /* declare message handlers for each api */
40 #define vl_endianfun /* define message structures */
41 #include <vpp/api/vpe_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
47 #include <vpp/api/vpe_all_api_h.h>
61 svm_fifo_t *server_rx_fifo;
62 svm_fifo_t *server_tx_fifo;
68 svm_queue_t *vl_input_queue;
70 /* API client handle */
73 /* The URI we're playing with */
79 /* Hash table for disconnect processing */
80 uword *session_index_by_vpp_handles;
83 svm_fifo_segment_private_t *seg;
85 /* intermediate rx buffer */
94 svm_queue_t *our_event_queue;
96 /* $$$ single thread only for the moment */
97 svm_queue_t *vpp_event_queue;
99 /* $$$$ hack: cut-through session index */
100 volatile u32 cut_through_session_index;
101 volatile u32 connected_session;
103 /* unique segment name counter */
104 u32 unique_segment_index;
109 pthread_t cut_through_thread_handle;
111 /* For deadman timers */
112 clib_time_t clib_time;
114 /* State of the connection, shared between msg RX thread and main thread */
115 volatile connection_state_t state;
117 volatile int time_to_stop;
118 volatile int time_to_print_stats;
120 u32 configured_segment_size;
122 /* VNET_API_ERROR_FOO -> "Foo" hash table */
123 uword *error_string_by_error_number;
126 svm_fifo_segment_main_t *segment_main;
128 u8 *connect_test_data;
129 } uri_udp_test_main_t;
134 #define NITER 4000000
137 uri_udp_test_main_t uri_udp_test_main;
140 stop_signal (int signum)
142 uri_udp_test_main_t *um = &uri_udp_test_main;
144 um->time_to_stop = 1;
148 stats_signal (int signum)
150 uri_udp_test_main_t *um = &uri_udp_test_main;
152 um->time_to_print_stats = 1;
155 static clib_error_t *
156 setup_signal_handlers (void)
158 signal (SIGINT, stats_signal);
159 signal (SIGQUIT, stop_signal);
160 signal (SIGTERM, stop_signal);
166 application_send_attach (uri_udp_test_main_t * utm)
168 vl_api_application_attach_t *bmp;
169 u32 fifo_size = 1 << 20;
170 bmp = vl_msg_api_alloc (sizeof (*bmp));
171 memset (bmp, 0, sizeof (*bmp));
173 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
174 bmp->client_index = utm->my_client_index;
175 bmp->context = ntohl (0xfeedface);
176 bmp->options[APP_OPTIONS_FLAGS] =
177 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT;
178 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2;
179 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
180 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
181 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
182 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
183 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 16768;
184 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
188 application_detach (uri_udp_test_main_t * utm)
190 vl_api_application_detach_t *bmp;
191 bmp = vl_msg_api_alloc (sizeof (*bmp));
192 memset (bmp, 0, sizeof (*bmp));
194 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
195 bmp->client_index = utm->my_client_index;
196 bmp->context = ntohl (0xfeedface);
197 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
201 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
204 uri_udp_test_main_t *utm = &uri_udp_test_main;
205 svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a;
210 clib_warning ("attach failed: %d", mp->retval);
211 utm->state = STATE_FAILED;
215 if (mp->segment_name_length == 0)
217 clib_warning ("segment_name_length zero");
221 a->segment_name = (char *) mp->segment_name;
222 a->segment_size = mp->segment_size;
224 ASSERT (mp->app_event_queue_address);
226 /* Attach to the segment vpp created */
227 rv = svm_fifo_segment_attach (a);
230 clib_warning ("svm_fifo_segment_attach ('%s') failed",
235 utm->our_event_queue =
236 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
240 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
244 clib_warning ("detach returned with err: %d", mp->retval);
248 format_api_error (u8 * s, va_list * args)
250 uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
251 i32 error = va_arg (*args, u32);
254 p = hash_get (utm->error_string_by_error_number, -error);
257 s = format (s, "%s", p[0]);
259 s = format (s, "%d", error);
264 wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
267 #define TIMEOUT 600.0
269 #define TIMEOUT 600.0
272 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
274 while (clib_time_now (&utm->clib_time) < timeout)
276 if (utm->state == state)
282 u64 server_bytes_received, server_bytes_sent;
285 cut_through_thread_fn (void *arg)
290 u8 *my_copy_buffer = 0;
291 uri_udp_test_main_t *utm = &uri_udp_test_main;
296 while (utm->cut_through_session_index == ~0)
299 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
301 rx_fifo = s->server_rx_fifo;
302 tx_fifo = s->server_tx_fifo;
304 vec_validate (my_copy_buffer, 64 * 1024 - 1);
308 /* We read from the tx fifo and write to the rx fifo */
311 actual_transfer = svm_fifo_dequeue_nowait (tx_fifo,
312 vec_len (my_copy_buffer),
315 while (actual_transfer <= 0);
317 server_bytes_received += actual_transfer;
320 while (actual_transfer > 0)
322 rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer,
323 my_copy_buffer + buffer_offset);
326 actual_transfer -= rv;
328 server_bytes_sent += rv;
332 if (PREDICT_FALSE (utm->time_to_stop))
340 udp_client_connect (uri_udp_test_main_t * utm)
342 vl_api_connect_uri_t *cmp;
343 cmp = vl_msg_api_alloc (sizeof (*cmp));
344 memset (cmp, 0, sizeof (*cmp));
346 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
347 cmp->client_index = utm->my_client_index;
348 cmp->context = ntohl (0xfeedface);
349 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
350 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
354 client_send_cut_through (uri_udp_test_main_t * utm, session_t * session)
358 u64 bytes_received = 0, bytes_sent = 0;
361 f64 before, after, delta, bytes_per_second;
362 svm_fifo_t *rx_fifo, *tx_fifo;
363 int buffer_offset, bytes_to_send = 0;
368 vec_validate (test_data, 64 * 1024 - 1);
369 for (i = 0; i < vec_len (test_data); i++)
370 test_data[i] = i & 0xff;
372 rx_fifo = session->server_rx_fifo;
373 tx_fifo = session->server_tx_fifo;
375 before = clib_time_now (&utm->clib_time);
377 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
379 for (i = 0; i < NITER; i++)
381 bytes_to_send = vec_len (test_data);
383 while (bytes_to_send > 0)
385 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
386 test_data + buffer_offset);
396 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
397 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
398 bytes_to_read : vec_len (utm->rx_buf);
401 while (bytes_to_read > 0)
403 rv = svm_fifo_dequeue_nowait (rx_fifo,
405 utm->rx_buf + buffer_offset);
410 bytes_received += rv;
414 while (bytes_received < bytes_sent)
417 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
422 for (j = 0; j < rv; j++)
424 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
426 clib_warning ("error at byte %lld, 0x%x not 0x%x",
429 ((bytes_received + j) & 0xff));
433 bytes_received += (u64) rv;
437 after = clib_time_now (&utm->clib_time);
438 delta = after - before;
439 bytes_per_second = 0.0;
442 bytes_per_second = (f64) bytes_received / delta;
445 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
446 bytes_received, delta, bytes_per_second);
448 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
449 bytes_sent, delta, bytes_per_second);
451 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
452 (bytes_per_second * 8.0) / 1e9);
456 send_test_chunk (uri_udp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
459 u8 *test_data = utm->connect_test_data;
461 int test_buf_offset = 0;
463 u32 queue_max_chunk = 128 << 10, actual_write;
464 session_fifo_event_t evt;
467 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
468 if (bytes_to_snd > vec_len (test_data))
469 bytes_to_snd = vec_len (test_data);
471 while (bytes_to_snd > 0 && !utm->time_to_stop)
473 actual_write = (bytes_to_snd > queue_max_chunk) ?
474 queue_max_chunk : bytes_to_snd;
475 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
476 test_data + test_buf_offset);
481 test_buf_offset += rv;
484 if (svm_fifo_set_event (tx_fifo))
486 /* Fabricate TX event, send to vpp */
488 evt.event_type = FIFO_EVENT_APP_TX;
490 svm_queue_add (utm->vpp_event_queue,
491 (u8 *) & evt, 0 /* do wait for mutex */ );
498 recv_test_chunk (uri_udp_test_main_t * utm, session_t * session)
501 int buffer_offset, bytes_to_read = 0, rv;
503 rx_fifo = session->server_rx_fifo;
504 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
506 vec_len (utm->rx_buf) > bytes_to_read ?
507 bytes_to_read : vec_len (utm->rx_buf);
510 while (bytes_to_read > 0)
512 rv = svm_fifo_dequeue_nowait (rx_fifo, bytes_to_read,
513 utm->rx_buf + buffer_offset);
523 client_send_data (uri_udp_test_main_t * utm)
526 int mypid = getpid ();
532 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
533 for (i = 0; i < vec_len (utm->connect_test_data); i++)
534 utm->connect_test_data[i] = i & 0xff;
536 test_data = utm->connect_test_data;
537 session = pool_elt_at_index (utm->sessions, utm->connected_session);
538 tx_fifo = session->server_tx_fifo;
540 ASSERT (vec_len (test_data) > 0);
542 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
543 n_iterations = NITER;
545 for (i = 0; i < n_iterations; i++)
547 send_test_chunk (utm, tx_fifo, mypid, 0);
548 recv_test_chunk (utm, session);
549 if (utm->time_to_stop)
553 f64 timeout = clib_time_now (&utm->clib_time) + 5;
554 while (clib_time_now (&utm->clib_time) < timeout)
556 recv_test_chunk (utm, session);
562 client_test (uri_udp_test_main_t * utm)
566 application_send_attach (utm);
567 udp_client_connect (utm);
569 if (wait_for_state_change (utm, STATE_READY))
571 clib_warning ("timeout waiting for STATE_READY");
575 if (utm->cut_through_session_index != ~0)
577 session = pool_elt_at_index (utm->sessions,
578 utm->cut_through_session_index);
579 client_send_cut_through (utm, session);
583 session = pool_elt_at_index (utm->sessions, utm->connected_session);
584 client_send_data (utm);
587 application_detach (utm);
591 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
593 uri_udp_test_main_t *utm = &uri_udp_test_main;
597 clib_warning ("bind failed: %d", mp->retval);
598 utm->state = STATE_FAILED;
602 utm->state = STATE_BOUND;
606 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
608 svm_fifo_segment_create_args_t _a, *a = &_a;
611 memset (a, 0, sizeof (*a));
612 a->segment_name = (char *) mp->segment_name;
613 a->segment_size = mp->segment_size;
614 /* Attach to the segment vpp created */
615 rv = svm_fifo_segment_attach (a);
618 clib_warning ("svm_fifo_segment_attach ('%s') failed",
622 clib_warning ("Mapped new segment '%s' size %d", mp->segment_name,
627 * Acting as server for redirected connect requests
630 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
633 uri_udp_test_main_t *utm = &uri_udp_test_main;
634 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
635 svm_fifo_segment_create_args_t _a, *a = &_a;
636 svm_fifo_segment_private_t *seg;
637 svm_queue_t *client_q;
638 vl_api_connect_session_reply_t *rmp;
639 session_t *session = 0;
642 /* Create the segment */
643 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
644 utm->unique_segment_index++, 0);
645 a->segment_size = utm->configured_segment_size;
647 rv = svm_fifo_segment_create (a);
650 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
651 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
655 vec_add2 (utm->seg, seg, 1);
657 segment_index = vec_len (sm->segments) - 1;
658 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
660 pool_get (utm->sessions, session);
662 session->server_rx_fifo = svm_fifo_segment_alloc_fifo
663 (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
664 ASSERT (session->server_rx_fifo);
666 session->server_tx_fifo = svm_fifo_segment_alloc_fifo
667 (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
668 ASSERT (session->server_tx_fifo);
670 session->server_rx_fifo->master_session_index = session - utm->sessions;
671 session->server_tx_fifo->master_session_index = session - utm->sessions;
672 utm->cut_through_session_index = session - utm->sessions;
674 rv = pthread_create (&utm->cut_through_thread_handle,
675 NULL /*attr */ , cut_through_thread_fn, 0);
678 clib_warning ("pthread_create returned %d", rv);
679 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
683 rmp = vl_msg_api_alloc (sizeof (*rmp));
684 memset (rmp, 0, sizeof (*rmp));
686 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
687 rmp->context = mp->context;
688 rmp->retval = ntohl (rv);
689 rmp->segment_name_length = vec_len (a->segment_name);
692 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
693 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
696 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
698 vec_free (a->segment_name);
700 client_q = uword_to_pointer (mp->client_queue_address, svm_queue_t *);
701 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
705 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
707 uri_udp_test_main_t *utm = &uri_udp_test_main;
710 clib_warning ("returned %d", ntohl (mp->retval));
712 utm->state = STATE_START;
716 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
718 uri_udp_test_main_t *utm = &uri_udp_test_main;
719 vl_api_accept_session_reply_t *rmp;
720 svm_fifo_t *rx_fifo, *tx_fifo;
722 static f64 start_time;
724 if (start_time == 0.0)
725 start_time = clib_time_now (&utm->clib_time);
727 utm->vpp_event_queue =
728 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
730 pool_get (utm->sessions, session);
732 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
733 rx_fifo->client_session_index = session - utm->sessions;
734 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
735 tx_fifo->client_session_index = session - utm->sessions;
737 session->server_rx_fifo = rx_fifo;
738 session->server_tx_fifo = tx_fifo;
740 hash_set (utm->session_index_by_vpp_handles, mp->handle,
741 session - utm->sessions);
743 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
745 f64 now = clib_time_now (&utm->clib_time);
746 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
747 pool_elts (utm->sessions), now - start_time,
748 (f64) pool_elts (utm->sessions) / (now - start_time));
751 rmp = vl_msg_api_alloc (sizeof (*rmp));
752 memset (rmp, 0, sizeof (*rmp));
753 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
754 rmp->handle = mp->handle;
755 rmp->context = mp->context;
756 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
758 CLIB_MEMORY_BARRIER ();
759 utm->state = STATE_READY;
763 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
765 uri_udp_test_main_t *utm = &uri_udp_test_main;
767 vl_api_disconnect_session_reply_t *rmp;
771 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
775 session = pool_elt_at_index (utm->sessions, p[0]);
776 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
777 pool_put (utm->sessions, session);
781 clib_warning ("couldn't find session key %llx", mp->handle);
785 rmp = vl_msg_api_alloc (sizeof (*rmp));
786 memset (rmp, 0, sizeof (*rmp));
787 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
789 rmp->handle = mp->handle;
790 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
794 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
796 uri_udp_test_main_t *utm = &uri_udp_test_main;
799 ASSERT (utm->i_am_master == 0);
803 clib_warning ("failed connect");
807 /* We've been redirected */
808 if (mp->segment_name_length > 0)
810 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
811 svm_fifo_segment_create_args_t _a, *a = &_a;
813 svm_fifo_segment_private_t *seg;
816 memset (a, 0, sizeof (*a));
817 a->segment_name = (char *) mp->segment_name;
821 rv = svm_fifo_segment_attach (a);
824 clib_warning ("sm_fifo_segment_create ('%v') failed",
829 segment_index = a->new_segment_indices[0];
830 vec_add2 (utm->seg, seg, 1);
831 memcpy (seg, sm->segments + segment_index, sizeof (*seg));
835 pool_get (utm->sessions, session);
836 session->server_rx_fifo = uword_to_pointer (mp->server_rx_fifo,
838 ASSERT (session->server_rx_fifo);
839 session->server_tx_fifo = uword_to_pointer (mp->server_tx_fifo,
841 ASSERT (session->server_tx_fifo);
843 if (mp->segment_name_length > 0)
844 utm->cut_through_session_index = session - utm->sessions;
847 utm->connected_session = session - utm->sessions;
848 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
851 utm->state = STATE_READY;
854 #define foreach_tcp_echo_msg \
855 _(BIND_URI_REPLY, bind_uri_reply) \
856 _(CONNECT_URI, connect_uri) \
857 _(CONNECT_SESSION_REPLY, connect_session_reply) \
858 _(UNBIND_URI_REPLY, unbind_uri_reply) \
859 _(ACCEPT_SESSION, accept_session) \
860 _(DISCONNECT_SESSION, disconnect_session) \
861 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
862 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
863 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
866 tcp_echo_api_hookup (uri_udp_test_main_t * utm)
869 vl_msg_api_set_handlers(VL_API_##N, #n, \
870 vl_api_##n##_t_handler, \
872 vl_api_##n##_t_endian, \
873 vl_api_##n##_t_print, \
874 sizeof(vl_api_##n##_t), 1);
875 foreach_tcp_echo_msg;
881 connect_to_vpp (char *name)
883 uri_udp_test_main_t *utm = &uri_udp_test_main;
884 api_main_t *am = &api_main;
886 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
889 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
890 utm->my_client_index = am->my_client_index;
896 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
898 clib_warning ("BUG");
902 init_error_string_table (uri_udp_test_main_t * utm)
904 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
906 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
907 foreach_vnet_api_error;
910 hash_set (utm->error_string_by_error_number, 99, "Misc");
914 server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
915 session_fifo_event_t * e)
917 svm_fifo_t *rx_fifo, *tx_fifo;
919 session_fifo_event_t evt;
924 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
925 svm_fifo_unset_event (rx_fifo);
929 nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
935 rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
939 if (svm_fifo_set_event (tx_fifo))
941 /* Fabricate TX event, send to vpp */
943 evt.event_type = FIFO_EVENT_APP_TX;
944 q = utm->vpp_event_queue;
945 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
950 server_handle_event_queue (uri_udp_test_main_t * utm)
952 session_fifo_event_t _e, *e = &_e;
954 while (utm->state != STATE_READY)
959 svm_queue_sub (utm->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
960 switch (e->event_type)
962 case FIFO_EVENT_APP_RX:
963 server_handle_fifo_event_rx (utm, e);
966 case FIFO_EVENT_DISCONNECT:
970 clib_warning ("unknown event type %d", e->event_type);
973 if (PREDICT_FALSE (utm->time_to_stop == 1))
975 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
977 utm->time_to_print_stats = 0;
978 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
984 server_unbind (uri_udp_test_main_t * utm)
986 vl_api_unbind_uri_t *ump;
988 ump = vl_msg_api_alloc (sizeof (*ump));
989 memset (ump, 0, sizeof (*ump));
991 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
992 ump->client_index = utm->my_client_index;
993 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
994 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
998 server_bind (uri_udp_test_main_t * utm)
1000 vl_api_bind_uri_t *bmp;
1002 bmp = vl_msg_api_alloc (sizeof (*bmp));
1003 memset (bmp, 0, sizeof (*bmp));
1005 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1006 bmp->client_index = utm->my_client_index;
1007 bmp->context = ntohl (0xfeedface);
1008 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1009 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1013 udp_server_test (uri_udp_test_main_t * utm)
1016 application_send_attach (utm);
1021 if (wait_for_state_change (utm, STATE_BOUND))
1023 clib_warning ("timeout waiting for STATE_BOUND");
1027 server_handle_event_queue (utm);
1030 server_unbind (utm);
1032 if (wait_for_state_change (utm, STATE_START))
1034 clib_warning ("timeout waiting for STATE_START");
1038 application_detach (utm);
1040 fformat (stdout, "Test complete...\n");
1044 main (int argc, char **argv)
1046 uri_udp_test_main_t *utm = &uri_udp_test_main;
1047 u8 *bind_name = (u8 *) "udp://0.0.0.0/1234";
1048 unformat_input_t _argv, *a = &_argv;
1049 int i_am_master = 1;
1058 clib_mem_init (0, 256 << 20);
1060 heap = clib_mem_get_per_cpu_heap ();
1061 h = mheap_header (heap);
1063 /* make the main heap thread-safe */
1064 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1066 vec_validate (utm->rx_buf, 8192);
1068 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1069 utm->my_pid = getpid ();
1070 utm->configured_segment_size = 1 << 20;
1072 clib_time_init (&utm->clib_time);
1073 init_error_string_table (utm);
1074 svm_fifo_segment_main_init (0x200000000ULL, 20);
1075 unformat_init_command_line (a, argv);
1077 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1079 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1081 vl_set_memory_root_path ((char *) chroot_prefix);
1083 else if (unformat (a, "uri %s", &bind_name))
1085 else if (unformat (a, "segment-size %dM", &tmp))
1086 utm->configured_segment_size = tmp << 20;
1087 else if (unformat (a, "segment-size %dG", &tmp))
1088 utm->configured_segment_size = tmp << 30;
1089 else if (unformat (a, "master"))
1091 else if (unformat (a, "slave"))
1095 fformat (stderr, "%s: usage [master|slave]\n");
1100 utm->cut_through_session_index = ~0;
1101 utm->uri = format (0, "%s%c", bind_name, 0);
1102 utm->i_am_master = i_am_master;
1103 utm->segment_main = &svm_fifo_segment_main;
1104 utm->connect_uri = format (0, "udp://6.0.1.2/1234%c", 0);
1106 setup_signal_handlers ();
1107 tcp_echo_api_hookup (utm);
1109 app_name = i_am_master ? "udp_echo_master" : "udp_echo_slave";
1110 if (connect_to_vpp (app_name) < 0)
1113 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1117 if (i_am_master == 0)
1123 /* $$$$ hack preallocation */
1124 for (i = 0; i < 200000; i++)
1126 pool_get (utm->sessions, session);
1127 memset (session, 0, sizeof (*session));
1129 for (i = 0; i < 200000; i++)
1130 pool_put_index (utm->sessions, i);
1132 udp_server_test (utm);
1134 vl_client_disconnect_from_vlib ();
1138 #undef vl_api_version
1139 #define vl_api_version(n,v) static u32 vpe_api_version = v;
1140 #include <vpp/api/vpe.api.h>
1141 #undef vl_api_version
1144 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1147 * Send the main API signature in slot 0. This bit of code must
1148 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1150 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1160 * fd.io coding-style-patch-verification: ON
1163 * eval: (c-set-style "gnu")