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;
130 uword *segments_table;
131 } uri_udp_test_main_t;
136 #define NITER 4000000
139 uri_udp_test_main_t uri_udp_test_main;
142 stop_signal (int signum)
144 uri_udp_test_main_t *um = &uri_udp_test_main;
146 um->time_to_stop = 1;
150 stats_signal (int signum)
152 uri_udp_test_main_t *um = &uri_udp_test_main;
154 um->time_to_print_stats = 1;
157 static clib_error_t *
158 setup_signal_handlers (void)
160 signal (SIGINT, stats_signal);
161 signal (SIGQUIT, stop_signal);
162 signal (SIGTERM, stop_signal);
168 application_send_attach (uri_udp_test_main_t * utm)
170 vl_api_application_attach_t *bmp;
171 u32 fifo_size = 1 << 20;
172 bmp = vl_msg_api_alloc (sizeof (*bmp));
173 memset (bmp, 0, sizeof (*bmp));
175 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
176 bmp->client_index = utm->my_client_index;
177 bmp->context = ntohl (0xfeedface);
178 bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ADD_SEGMENT;
179 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
180 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
181 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2;
182 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size;
183 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size;
184 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
185 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
186 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 16768;
187 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
191 application_detach (uri_udp_test_main_t * utm)
193 vl_api_application_detach_t *bmp;
194 bmp = vl_msg_api_alloc (sizeof (*bmp));
195 memset (bmp, 0, sizeof (*bmp));
197 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
198 bmp->client_index = utm->my_client_index;
199 bmp->context = ntohl (0xfeedface);
200 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
204 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
207 uri_udp_test_main_t *utm = &uri_udp_test_main;
208 svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a;
213 clib_warning ("attach failed: %d", mp->retval);
214 utm->state = STATE_FAILED;
218 if (mp->segment_name_length == 0)
220 clib_warning ("segment_name_length zero");
224 a->segment_name = (char *) mp->segment_name;
225 a->segment_size = mp->segment_size;
227 ASSERT (mp->app_event_queue_address);
229 /* Attach to the segment vpp created */
230 rv = svm_fifo_segment_attach (a);
233 clib_warning ("svm_fifo_segment_attach ('%s') failed",
238 utm->our_event_queue =
239 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
243 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
247 clib_warning ("detach returned with err: %d", mp->retval);
251 format_api_error (u8 * s, va_list * args)
253 uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *);
254 i32 error = va_arg (*args, u32);
257 p = hash_get (utm->error_string_by_error_number, -error);
260 s = format (s, "%s", p[0]);
262 s = format (s, "%d", error);
267 wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state)
270 #define TIMEOUT 600.0
272 #define TIMEOUT 600.0
275 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
277 while (clib_time_now (&utm->clib_time) < timeout)
279 if (utm->state == state)
285 u64 server_bytes_received, server_bytes_sent;
288 cut_through_thread_fn (void *arg)
293 u8 *my_copy_buffer = 0;
294 uri_udp_test_main_t *utm = &uri_udp_test_main;
299 while (utm->cut_through_session_index == ~0)
302 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
304 rx_fifo = s->server_rx_fifo;
305 tx_fifo = s->server_tx_fifo;
307 vec_validate (my_copy_buffer, 64 * 1024 - 1);
311 /* We read from the tx fifo and write to the rx fifo */
314 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo,
315 vec_len (my_copy_buffer),
318 while (actual_transfer <= 0);
320 server_bytes_received += actual_transfer;
323 while (actual_transfer > 0)
325 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
326 my_copy_buffer + buffer_offset);
329 actual_transfer -= rv;
331 server_bytes_sent += rv;
335 if (PREDICT_FALSE (utm->time_to_stop))
343 udp_client_connect (uri_udp_test_main_t * utm)
345 vl_api_connect_uri_t *cmp;
346 cmp = vl_msg_api_alloc (sizeof (*cmp));
347 memset (cmp, 0, sizeof (*cmp));
349 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
350 cmp->client_index = utm->my_client_index;
351 cmp->context = ntohl (0xfeedface);
352 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
353 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
357 client_send_cut_through (uri_udp_test_main_t * utm, session_t * session)
361 u64 bytes_received = 0, bytes_sent = 0;
364 f64 before, after, delta, bytes_per_second;
365 svm_fifo_t *rx_fifo, *tx_fifo;
366 int buffer_offset, bytes_to_send = 0;
371 vec_validate (test_data, 64 * 1024 - 1);
372 for (i = 0; i < vec_len (test_data); i++)
373 test_data[i] = i & 0xff;
375 rx_fifo = session->server_rx_fifo;
376 tx_fifo = session->server_tx_fifo;
378 before = clib_time_now (&utm->clib_time);
380 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
382 for (i = 0; i < NITER; i++)
384 bytes_to_send = vec_len (test_data);
386 while (bytes_to_send > 0)
388 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
389 test_data + buffer_offset);
399 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
400 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
401 bytes_to_read : vec_len (utm->rx_buf);
404 while (bytes_to_read > 0)
406 rv = svm_fifo_dequeue_nowait (rx_fifo,
408 utm->rx_buf + buffer_offset);
413 bytes_received += rv;
417 while (bytes_received < bytes_sent)
420 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
425 for (j = 0; j < rv; j++)
427 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
429 clib_warning ("error at byte %lld, 0x%x not 0x%x",
432 ((bytes_received + j) & 0xff));
436 bytes_received += (u64) rv;
440 after = clib_time_now (&utm->clib_time);
441 delta = after - before;
442 bytes_per_second = 0.0;
445 bytes_per_second = (f64) bytes_received / delta;
448 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
449 bytes_received, delta, bytes_per_second);
451 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
452 bytes_sent, delta, bytes_per_second);
454 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
455 (bytes_per_second * 8.0) / 1e9);
459 send_test_chunk (uri_udp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid,
462 u8 *test_data = utm->connect_test_data;
464 int test_buf_offset = 0;
466 u32 queue_max_chunk = 128 << 10, actual_write;
467 session_fifo_event_t evt;
470 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
471 if (bytes_to_snd > vec_len (test_data))
472 bytes_to_snd = vec_len (test_data);
474 while (bytes_to_snd > 0 && !utm->time_to_stop)
476 actual_write = (bytes_to_snd > queue_max_chunk) ?
477 queue_max_chunk : bytes_to_snd;
478 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write,
479 test_data + test_buf_offset);
484 test_buf_offset += rv;
487 if (svm_fifo_set_event (tx_fifo))
489 /* Fabricate TX event, send to vpp */
491 evt.event_type = FIFO_EVENT_APP_TX;
493 svm_queue_add (utm->vpp_event_queue,
494 (u8 *) & evt, 0 /* do wait for mutex */ );
501 recv_test_chunk (uri_udp_test_main_t * utm, session_t * session)
504 int buffer_offset, bytes_to_read = 0, rv;
506 rx_fifo = session->server_rx_fifo;
507 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
509 vec_len (utm->rx_buf) > bytes_to_read ?
510 bytes_to_read : vec_len (utm->rx_buf);
513 while (bytes_to_read > 0)
515 rv = svm_fifo_dequeue_nowait (rx_fifo, bytes_to_read,
516 utm->rx_buf + buffer_offset);
526 client_send_data (uri_udp_test_main_t * utm)
529 int mypid = getpid ();
535 vec_validate (utm->connect_test_data, 64 * 1024 - 1);
536 for (i = 0; i < vec_len (utm->connect_test_data); i++)
537 utm->connect_test_data[i] = i & 0xff;
539 test_data = utm->connect_test_data;
540 session = pool_elt_at_index (utm->sessions, utm->connected_session);
541 tx_fifo = session->server_tx_fifo;
543 ASSERT (vec_len (test_data) > 0);
545 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
546 n_iterations = NITER;
548 for (i = 0; i < n_iterations; i++)
550 send_test_chunk (utm, tx_fifo, mypid, 0);
551 recv_test_chunk (utm, session);
552 if (utm->time_to_stop)
556 f64 timeout = clib_time_now (&utm->clib_time) + 5;
557 while (clib_time_now (&utm->clib_time) < timeout)
559 recv_test_chunk (utm, session);
565 client_test (uri_udp_test_main_t * utm)
569 application_send_attach (utm);
570 udp_client_connect (utm);
572 if (wait_for_state_change (utm, STATE_READY))
574 clib_warning ("timeout waiting for STATE_READY");
578 if (utm->cut_through_session_index != ~0)
580 session = pool_elt_at_index (utm->sessions,
581 utm->cut_through_session_index);
582 client_send_cut_through (utm, session);
586 session = pool_elt_at_index (utm->sessions, utm->connected_session);
587 client_send_data (utm);
590 application_detach (utm);
594 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
596 uri_udp_test_main_t *utm = &uri_udp_test_main;
600 clib_warning ("bind failed: %d", mp->retval);
601 utm->state = STATE_FAILED;
605 utm->state = STATE_BOUND;
609 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
611 uri_udp_test_main_t *utm = &uri_udp_test_main;
612 svm_fifo_segment_create_args_t _a, *a = &_a;
613 svm_fifo_segment_private_t *seg;
617 memset (a, 0, sizeof (*a));
618 a->segment_name = (char *) mp->segment_name;
619 a->segment_size = mp->segment_size;
620 /* Attach to the segment vpp created */
621 rv = svm_fifo_segment_attach (a);
624 clib_warning ("svm_fifo_segment_attach ('%s') failed",
628 seg = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
629 clib_warning ("Mapped new segment '%s' size %d", seg->ssvm.name,
630 seg->ssvm.ssvm_size);
631 seg_name = format (0, "%s", (char *) mp->segment_name);
632 hash_set_mem (utm->segments_table, seg_name, a->new_segment_indices[0]);
637 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
639 uri_udp_test_main_t *utm = &uri_udp_test_main;
640 svm_fifo_segment_private_t *seg;
645 seg_name = format (0, "%s", mp->segment_name);
646 seg_indexp = hash_get_mem (utm->segments_table, seg_name);
649 clib_warning ("segment not mapped: %s", seg_name);
652 hash_unset_mem (utm->segments_table, seg_name);
653 seg = svm_fifo_segment_get_segment ((u32) seg_indexp[0]);
654 svm_fifo_segment_delete (seg);
655 clib_warning ("Unmapped segment '%s'", seg_name);
660 * Acting as server for redirected connect requests
663 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
666 uri_udp_test_main_t *utm = &uri_udp_test_main;
667 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
668 svm_fifo_segment_create_args_t _a, *a = &_a;
669 svm_fifo_segment_private_t *seg;
670 svm_queue_t *client_q;
671 vl_api_connect_session_reply_t *rmp;
672 session_t *session = 0;
675 /* Create the segment */
676 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
677 utm->unique_segment_index++, 0);
678 a->segment_size = utm->configured_segment_size;
680 rv = svm_fifo_segment_create (a);
683 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
684 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
688 vec_add2 (utm->seg, seg, 1);
690 segment_index = vec_len (sm->segments) - 1;
691 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
693 pool_get (utm->sessions, session);
695 session->server_rx_fifo = svm_fifo_segment_alloc_fifo
696 (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
697 ASSERT (session->server_rx_fifo);
699 session->server_tx_fifo = svm_fifo_segment_alloc_fifo
700 (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
701 ASSERT (session->server_tx_fifo);
703 session->server_rx_fifo->master_session_index = session - utm->sessions;
704 session->server_tx_fifo->master_session_index = session - utm->sessions;
705 utm->cut_through_session_index = session - utm->sessions;
707 rv = pthread_create (&utm->cut_through_thread_handle,
708 NULL /*attr */ , cut_through_thread_fn, 0);
711 clib_warning ("pthread_create returned %d", rv);
712 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
716 rmp = vl_msg_api_alloc (sizeof (*rmp));
717 memset (rmp, 0, sizeof (*rmp));
719 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
720 rmp->context = mp->context;
721 rmp->retval = ntohl (rv);
722 rmp->segment_name_length = vec_len (a->segment_name);
725 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
726 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
729 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
731 vec_free (a->segment_name);
733 client_q = uword_to_pointer (mp->client_queue_address, svm_queue_t *);
734 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
738 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
740 uri_udp_test_main_t *utm = &uri_udp_test_main;
743 clib_warning ("returned %d", ntohl (mp->retval));
745 utm->state = STATE_START;
749 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
751 uri_udp_test_main_t *utm = &uri_udp_test_main;
752 vl_api_accept_session_reply_t *rmp;
753 svm_fifo_t *rx_fifo, *tx_fifo;
755 static f64 start_time;
759 if (start_time == 0.0)
760 start_time = clib_time_now (&utm->clib_time);
762 utm->vpp_event_queue =
763 uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *);
765 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
766 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
768 pool_get (utm->sessions, session);
769 memset (session, 0, sizeof (*session));
770 session_index = session - utm->sessions;
772 /* Cut-through case */
773 if (mp->server_event_queue_address)
775 clib_warning ("cut-through session");
776 utm->our_event_queue = uword_to_pointer (mp->server_event_queue_address,
778 rx_fifo->master_session_index = session_index;
779 tx_fifo->master_session_index = session_index;
780 utm->cut_through_session_index = session_index;
781 session->server_rx_fifo = rx_fifo;
782 session->server_tx_fifo = tx_fifo;
784 rv = pthread_create (&utm->cut_through_thread_handle,
785 NULL /*attr */ , cut_through_thread_fn, 0);
788 clib_warning ("pthread_create returned %d", rv);
789 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
794 rx_fifo->client_session_index = session_index;
795 tx_fifo->client_session_index = session_index;
796 session->server_rx_fifo = rx_fifo;
797 session->server_tx_fifo = tx_fifo;
800 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
801 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
803 f64 now = clib_time_now (&utm->clib_time);
804 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
805 pool_elts (utm->sessions), now - start_time,
806 (f64) pool_elts (utm->sessions) / (now - start_time));
809 rmp = vl_msg_api_alloc (sizeof (*rmp));
810 memset (rmp, 0, sizeof (*rmp));
811 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
812 rmp->handle = mp->handle;
813 rmp->context = mp->context;
815 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
817 CLIB_MEMORY_BARRIER ();
818 utm->state = STATE_READY;
822 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
824 uri_udp_test_main_t *utm = &uri_udp_test_main;
826 vl_api_disconnect_session_reply_t *rmp;
830 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
834 session = pool_elt_at_index (utm->sessions, p[0]);
835 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
836 pool_put (utm->sessions, session);
840 clib_warning ("couldn't find session key %llx", mp->handle);
844 rmp = vl_msg_api_alloc (sizeof (*rmp));
845 memset (rmp, 0, sizeof (*rmp));
846 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
848 rmp->handle = mp->handle;
849 rmp->context = mp->context;
850 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
854 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
856 uri_udp_test_main_t *utm = &uri_udp_test_main;
859 ASSERT (utm->i_am_master == 0);
863 clib_warning ("failed connect");
867 pool_get (utm->sessions, session);
868 session->server_rx_fifo = uword_to_pointer (mp->server_rx_fifo,
870 ASSERT (session->server_rx_fifo);
871 session->server_tx_fifo = uword_to_pointer (mp->server_tx_fifo,
873 ASSERT (session->server_tx_fifo);
875 /* Cut-through case */
876 if (mp->client_event_queue_address)
878 clib_warning ("cut-through session");
879 utm->cut_through_session_index = session - utm->sessions;
880 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
882 utm->our_event_queue = uword_to_pointer (mp->client_event_queue_address,
887 utm->connected_session = session - utm->sessions;
888 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
891 utm->state = STATE_READY;
894 #define foreach_tcp_echo_msg \
895 _(BIND_URI_REPLY, bind_uri_reply) \
896 _(CONNECT_URI, connect_uri) \
897 _(CONNECT_SESSION_REPLY, connect_session_reply) \
898 _(UNBIND_URI_REPLY, unbind_uri_reply) \
899 _(ACCEPT_SESSION, accept_session) \
900 _(DISCONNECT_SESSION, disconnect_session) \
901 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
902 _(UNMAP_SEGMENT, unmap_segment) \
903 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
904 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
907 tcp_echo_api_hookup (uri_udp_test_main_t * utm)
910 vl_msg_api_set_handlers(VL_API_##N, #n, \
911 vl_api_##n##_t_handler, \
913 vl_api_##n##_t_endian, \
914 vl_api_##n##_t_print, \
915 sizeof(vl_api_##n##_t), 1);
916 foreach_tcp_echo_msg;
922 connect_to_vpp (char *name)
924 uri_udp_test_main_t *utm = &uri_udp_test_main;
925 api_main_t *am = &api_main;
927 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
930 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
931 utm->my_client_index = am->my_client_index;
937 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
939 clib_warning ("BUG");
943 init_error_string_table (uri_udp_test_main_t * utm)
945 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
947 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
948 foreach_vnet_api_error;
951 hash_set (utm->error_string_by_error_number, 99, "Misc");
955 server_handle_fifo_event_rx (uri_udp_test_main_t * utm,
956 session_fifo_event_t * e)
958 svm_fifo_t *rx_fifo, *tx_fifo;
960 session_fifo_event_t evt;
965 tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo;
966 svm_fifo_unset_event (rx_fifo);
970 nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf),
976 rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf);
980 if (svm_fifo_set_event (tx_fifo))
982 /* Fabricate TX event, send to vpp */
984 evt.event_type = FIFO_EVENT_APP_TX;
985 q = utm->vpp_event_queue;
986 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
991 server_handle_event_queue (uri_udp_test_main_t * utm)
993 session_fifo_event_t _e, *e = &_e;
995 while (utm->state != STATE_READY)
1000 svm_queue_sub (utm->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
1001 switch (e->event_type)
1003 case FIFO_EVENT_APP_RX:
1004 server_handle_fifo_event_rx (utm, e);
1007 case FIFO_EVENT_DISCONNECT:
1011 clib_warning ("unknown event type %d", e->event_type);
1014 if (PREDICT_FALSE (utm->time_to_stop == 1))
1016 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1018 utm->time_to_print_stats = 0;
1019 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1025 server_unbind (uri_udp_test_main_t * utm)
1027 vl_api_unbind_uri_t *ump;
1029 ump = vl_msg_api_alloc (sizeof (*ump));
1030 memset (ump, 0, sizeof (*ump));
1032 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1033 ump->client_index = utm->my_client_index;
1034 memcpy (ump->uri, utm->uri, vec_len (utm->uri));
1035 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1039 server_bind (uri_udp_test_main_t * utm)
1041 vl_api_bind_uri_t *bmp;
1043 bmp = vl_msg_api_alloc (sizeof (*bmp));
1044 memset (bmp, 0, sizeof (*bmp));
1046 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1047 bmp->client_index = utm->my_client_index;
1048 bmp->context = ntohl (0xfeedface);
1049 memcpy (bmp->uri, utm->uri, vec_len (utm->uri));
1050 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1054 udp_server_test (uri_udp_test_main_t * utm)
1057 application_send_attach (utm);
1062 if (wait_for_state_change (utm, STATE_BOUND))
1064 clib_warning ("timeout waiting for STATE_BOUND");
1068 server_handle_event_queue (utm);
1071 server_unbind (utm);
1073 if (wait_for_state_change (utm, STATE_START))
1075 clib_warning ("timeout waiting for STATE_START");
1079 application_detach (utm);
1081 fformat (stdout, "Test complete...\n");
1085 main (int argc, char **argv)
1087 uri_udp_test_main_t *utm = &uri_udp_test_main;
1088 u8 *bind_name = (u8 *) "udp://0.0.0.0/1234";
1089 unformat_input_t _argv, *a = &_argv;
1090 int i_am_master = 1;
1099 clib_mem_init (0, 256 << 20);
1101 heap = clib_mem_get_per_cpu_heap ();
1102 h = mheap_header (heap);
1104 /* make the main heap thread-safe */
1105 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1107 vec_validate (utm->rx_buf, 8192);
1109 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1110 utm->my_pid = getpid ();
1111 utm->configured_segment_size = 1 << 20;
1112 utm->segments_table = hash_create_vec (0, sizeof (u8), sizeof (u64));
1114 clib_time_init (&utm->clib_time);
1115 init_error_string_table (utm);
1116 svm_fifo_segment_main_init (0x200000000ULL, 20);
1117 unformat_init_command_line (a, argv);
1119 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1121 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1123 vl_set_memory_root_path ((char *) chroot_prefix);
1125 else if (unformat (a, "uri %s", &bind_name))
1127 else if (unformat (a, "segment-size %dM", &tmp))
1128 utm->configured_segment_size = tmp << 20;
1129 else if (unformat (a, "segment-size %dG", &tmp))
1130 utm->configured_segment_size = tmp << 30;
1131 else if (unformat (a, "master"))
1133 else if (unformat (a, "slave"))
1137 fformat (stderr, "%s: usage [master|slave]\n");
1142 utm->cut_through_session_index = ~0;
1143 utm->uri = format (0, "%s%c", bind_name, 0);
1144 utm->i_am_master = i_am_master;
1145 utm->segment_main = &svm_fifo_segment_main;
1146 utm->connect_uri = format (0, "udp://6.0.1.2/1234%c", 0);
1148 setup_signal_handlers ();
1149 tcp_echo_api_hookup (utm);
1151 app_name = i_am_master ? "udp_echo_master" : "udp_echo_slave";
1152 if (connect_to_vpp (app_name) < 0)
1155 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1159 if (i_am_master == 0)
1165 /* $$$$ hack preallocation */
1166 for (i = 0; i < 200000; i++)
1168 pool_get (utm->sessions, session);
1169 memset (session, 0, sizeof (*session));
1171 for (i = 0; i < 200000; i++)
1172 pool_put_index (utm->sessions, i);
1174 udp_server_test (utm);
1176 vl_client_disconnect_from_vlib ();
1180 #undef vl_api_version
1181 #define vl_api_version(n,v) static u32 vpe_api_version = v;
1182 #include <vpp/api/vpe.api.h>
1183 #undef vl_api_version
1186 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1189 * Send the main API signature in slot 0. This bit of code must
1190 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1192 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1202 * fd.io coding-style-patch-verification: ON
1205 * eval: (c-set-style "gnu")