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>
62 svm_queue_t *vl_input_queue;
64 /* API client handle */
67 /* The URI we're playing with */
74 app_session_t *sessions;
76 /* Hash table for disconnect processing */
77 uword *session_index_by_vpp_handles;
80 svm_fifo_segment_private_t *seg;
82 /* intermediate rx buffer */
90 svm_queue_t *our_event_queue;
92 /* $$$ single thread only for the moment */
93 svm_queue_t *vpp_event_queue;
95 /* $$$$ hack: cut-through session index */
96 volatile u32 cut_through_session_index;
97 volatile u32 connected_session;
99 /* unique segment name counter */
100 u32 unique_segment_index;
105 pthread_t cut_through_thread_handle;
107 /* For deadman timers */
108 clib_time_t clib_time;
110 /* State of the connection, shared between msg RX thread and main thread */
111 volatile connection_state_t state;
113 volatile int time_to_stop;
114 volatile int time_to_print_stats;
116 u32 configured_segment_size;
118 /* VNET_API_ERROR_FOO -> "Foo" hash table */
119 uword *error_string_by_error_number;
122 svm_fifo_segment_main_t *segment_main;
124 u8 *connect_test_data;
126 uword *segments_table;
135 #define NITER 4000000
138 udp_echo_main_t udp_echo_main;
141 stop_signal (int signum)
143 udp_echo_main_t *um = &udp_echo_main;
145 um->time_to_stop = 1;
149 stats_signal (int signum)
151 udp_echo_main_t *um = &udp_echo_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 unformat_ip4_address (unformat_input_t * input, va_list * args)
168 u8 *result = va_arg (*args, u8 *);
171 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
174 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
186 unformat_ip6_address (unformat_input_t * input, va_list * args)
188 ip6_address_t *result = va_arg (*args, ip6_address_t *);
190 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
191 uword c, n_colon, double_colon_index;
193 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
194 double_colon_index = ARRAY_LEN (hex_quads);
195 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
198 if (c >= '0' && c <= '9')
200 else if (c >= 'a' && c <= 'f')
201 hex_digit = c + 10 - 'a';
202 else if (c >= 'A' && c <= 'F')
203 hex_digit = c + 10 - 'A';
204 else if (c == ':' && n_colon < 2)
208 unformat_put_input (input);
212 /* Too many hex quads. */
213 if (n_hex_quads >= ARRAY_LEN (hex_quads))
218 hex_quad = (hex_quad << 4) | hex_digit;
220 /* Hex quad must fit in 16 bits. */
221 if (n_hex_digits >= 4)
228 /* Save position of :: */
231 /* More than one :: ? */
232 if (double_colon_index < ARRAY_LEN (hex_quads))
234 double_colon_index = n_hex_quads;
237 if (n_colon > 0 && n_hex_digits > 0)
239 hex_quads[n_hex_quads++] = hex_quad;
245 if (n_hex_digits > 0)
246 hex_quads[n_hex_quads++] = hex_quad;
251 /* Expand :: to appropriate number of zero hex quads. */
252 if (double_colon_index < ARRAY_LEN (hex_quads))
254 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
256 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
257 hex_quads[n_zero + i] = hex_quads[i];
259 for (i = 0; i < n_zero; i++)
260 hex_quads[double_colon_index + i] = 0;
262 n_hex_quads = ARRAY_LEN (hex_quads);
265 /* Too few hex quads given. */
266 if (n_hex_quads < ARRAY_LEN (hex_quads))
269 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
270 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
277 unformat_uri (unformat_input_t * input, va_list * args)
279 session_endpoint_extended_t *sep = va_arg (*args,
280 session_endpoint_extended_t *);
284 if (unformat (input, "%s://%U/%d", &tmp, unformat_ip4_address, &sep->ip.ip4,
287 sep->port = clib_host_to_net_u16 (port);
291 else if (unformat (input, "%s://%U/%d", &tmp, unformat_ip6_address,
292 &sep->ip.ip6, &port))
294 sep->port = clib_host_to_net_u16 (port);
302 application_send_attach (udp_echo_main_t * utm)
304 vl_api_application_attach_t *bmp;
305 bmp = vl_msg_api_alloc (sizeof (*bmp));
306 memset (bmp, 0, sizeof (*bmp));
308 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
309 bmp->client_index = utm->my_client_index;
310 bmp->context = ntohl (0xfeedface);
311 bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ADD_SEGMENT;
312 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
313 bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
314 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2;
315 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = utm->fifo_size;
316 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = utm->fifo_size;
317 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
318 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
319 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 16768;
320 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
324 application_detach (udp_echo_main_t * utm)
326 vl_api_application_detach_t *bmp;
327 bmp = vl_msg_api_alloc (sizeof (*bmp));
328 memset (bmp, 0, sizeof (*bmp));
330 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
331 bmp->client_index = utm->my_client_index;
332 bmp->context = ntohl (0xfeedface);
333 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
337 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
340 udp_echo_main_t *utm = &udp_echo_main;
341 svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a;
346 clib_warning ("attach failed: %d", mp->retval);
347 utm->state = STATE_FAILED;
351 if (mp->segment_name_length == 0)
353 clib_warning ("segment_name_length zero");
357 a->segment_name = (char *) mp->segment_name;
358 a->segment_size = mp->segment_size;
360 ASSERT (mp->app_event_queue_address);
362 /* Attach to the segment vpp created */
363 rv = svm_fifo_segment_attach (a);
366 clib_warning ("svm_fifo_segment_attach ('%s') failed",
371 utm->our_event_queue =
372 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
376 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
380 clib_warning ("detach returned with err: %d", mp->retval);
384 format_api_error (u8 * s, va_list * args)
386 udp_echo_main_t *utm = va_arg (*args, udp_echo_main_t *);
387 i32 error = va_arg (*args, u32);
390 p = hash_get (utm->error_string_by_error_number, -error);
393 s = format (s, "%s", p[0]);
395 s = format (s, "%d", error);
400 wait_for_state_change (udp_echo_main_t * utm, connection_state_t state)
403 #define TIMEOUT 600.0
405 #define TIMEOUT 600.0
408 f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT;
410 while (clib_time_now (&utm->clib_time) < timeout)
412 if (utm->state == state)
418 u64 server_bytes_received, server_bytes_sent;
421 cut_through_thread_fn (void *arg)
426 u8 *my_copy_buffer = 0;
427 udp_echo_main_t *utm = &udp_echo_main;
432 while (utm->cut_through_session_index == ~0)
435 s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index);
437 rx_fifo = s->rx_fifo;
438 tx_fifo = s->tx_fifo;
440 vec_validate (my_copy_buffer, 64 * 1024 - 1);
444 /* We read from the tx fifo and write to the rx fifo */
447 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo,
448 vec_len (my_copy_buffer),
451 while (actual_transfer <= 0);
453 server_bytes_received += actual_transfer;
456 while (actual_transfer > 0)
458 rv = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
459 my_copy_buffer + buffer_offset);
462 actual_transfer -= rv;
464 server_bytes_sent += rv;
468 if (PREDICT_FALSE (utm->time_to_stop))
476 udp_client_connect (udp_echo_main_t * utm)
478 vl_api_connect_uri_t *cmp;
479 cmp = vl_msg_api_alloc (sizeof (*cmp));
480 memset (cmp, 0, sizeof (*cmp));
482 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
483 cmp->client_index = utm->my_client_index;
484 cmp->context = ntohl (0xfeedface);
485 memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri));
486 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp);
490 client_send_cut_through (udp_echo_main_t * utm, app_session_t * session)
494 u64 bytes_received = 0, bytes_sent = 0;
497 f64 before, after, delta, bytes_per_second;
498 svm_fifo_t *rx_fifo, *tx_fifo;
499 int buffer_offset, bytes_to_send = 0;
504 vec_validate (test_data, 64 * 1024 - 1);
505 for (i = 0; i < vec_len (test_data); i++)
506 test_data[i] = i & 0xff;
508 rx_fifo = session->rx_fifo;
509 tx_fifo = session->tx_fifo;
511 before = clib_time_now (&utm->clib_time);
513 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
515 for (i = 0; i < NITER; i++)
517 bytes_to_send = vec_len (test_data);
519 while (bytes_to_send > 0)
521 rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send,
522 test_data + buffer_offset);
532 bytes_to_read = svm_fifo_max_dequeue (rx_fifo);
533 bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ?
534 bytes_to_read : vec_len (utm->rx_buf);
537 while (bytes_to_read > 0)
539 rv = svm_fifo_dequeue_nowait (rx_fifo,
541 utm->rx_buf + buffer_offset);
546 bytes_received += rv;
550 while (bytes_received < bytes_sent)
553 svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf);
558 for (j = 0; j < rv; j++)
560 if (utm->rx_buf[j] != ((bytes_received + j) & 0xff))
562 clib_warning ("error at byte %lld, 0x%x not 0x%x",
565 ((bytes_received + j) & 0xff));
569 bytes_received += (u64) rv;
573 after = clib_time_now (&utm->clib_time);
574 delta = after - before;
575 bytes_per_second = 0.0;
578 bytes_per_second = (f64) bytes_received / delta;
581 "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n",
582 bytes_received, delta, bytes_per_second);
584 "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n",
585 bytes_sent, delta, bytes_per_second);
587 "client -> server -> client round trip: %.2f Gbit/sec \n\n",
588 (bytes_per_second * 8.0) / 1e9);
592 send_test_chunk (udp_echo_main_t * utm, app_session_t * s, u32 bytes)
594 u8 *test_data = utm->connect_test_data;
595 int test_buf_offset = 0;
597 u32 bytes_to_snd, enq_space, min_chunk;
600 min_chunk = clib_min (65536, s->tx_fifo->nitems);
601 bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes;
602 if (bytes_to_snd > vec_len (test_data))
603 bytes_to_snd = vec_len (test_data);
605 while (bytes_to_snd > 0 && !utm->time_to_stop)
607 enq_space = svm_fifo_max_enqueue (s->tx_fifo);
608 if (enq_space < clib_min (bytes_to_snd, min_chunk))
611 rv = app_send (s, test_data + test_buf_offset, bytes_to_snd, 0);
615 test_buf_offset += rv;
622 recv_test_chunk (udp_echo_main_t * utm, app_session_t * s)
624 app_recv (s, utm->rx_buf, vec_len (utm->rx_buf));
628 client_send_data (udp_echo_main_t * utm)
630 f64 start_time, end_time, delta;
631 app_session_t *session;
637 vec_validate (utm->connect_test_data, 1024 * 1024 - 1);
638 for (i = 0; i < vec_len (utm->connect_test_data); i++)
639 utm->connect_test_data[i] = i & 0xff;
641 test_data = utm->connect_test_data;
642 session = pool_elt_at_index (utm->sessions, utm->connected_session);
643 ASSERT (vec_len (test_data) > 0);
645 vec_validate (utm->rx_buf, vec_len (test_data) - 1);
646 n_iterations = utm->bytes_to_send / vec_len (test_data);
650 start_time = clib_time_now (&utm->clib_time);
651 for (i = 0; i < n_iterations; i++)
653 send_test_chunk (utm, session, 0);
654 if (utm->have_return)
655 recv_test_chunk (utm, session);
656 if (utm->time_to_stop)
660 if (utm->have_return)
662 f64 timeout = clib_time_now (&utm->clib_time) + 5;
663 while (clib_time_now (&utm->clib_time) < timeout)
664 recv_test_chunk (utm, session);
667 end_time = clib_time_now (&utm->clib_time);
668 delta = end_time - start_time;
669 transfer_type = utm->have_return ? "full-duplex" : "half-duplex";
670 clib_warning ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
671 utm->bytes_to_send, utm->bytes_to_send / (1ULL << 20),
672 utm->bytes_to_send / (1ULL << 30), delta);
673 clib_warning ("%.2f bytes/second %s", ((f64) utm->bytes_to_send) / (delta),
675 clib_warning ("%.4f gbit/second %s",
676 (((f64) utm->bytes_to_send * 8.0) / delta / 1e9),
681 client_test (udp_echo_main_t * utm)
683 app_session_t *session;
685 application_send_attach (utm);
686 udp_client_connect (utm);
688 if (wait_for_state_change (utm, STATE_READY))
690 clib_warning ("timeout waiting for STATE_READY");
694 if (utm->cut_through_session_index != ~0)
696 session = pool_elt_at_index (utm->sessions,
697 utm->cut_through_session_index);
698 client_send_cut_through (utm, session);
702 session = pool_elt_at_index (utm->sessions, utm->connected_session);
703 client_send_data (utm);
706 application_detach (utm);
710 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
712 udp_echo_main_t *utm = &udp_echo_main;
713 svm_fifo_t *rx_fifo, *tx_fifo;
714 app_session_t *session;
719 clib_warning ("bind failed: %d", mp->retval);
720 utm->state = STATE_FAILED;
724 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
725 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
727 pool_get (utm->sessions, session);
728 memset (session, 0, sizeof (*session));
729 session_index = session - utm->sessions;
731 rx_fifo->client_session_index = session_index;
732 tx_fifo->client_session_index = session_index;
733 session->rx_fifo = rx_fifo;
734 session->tx_fifo = tx_fifo;
735 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
736 sizeof (ip46_address_t));
737 session->transport.is_ip4 = mp->lcl_is_ip4;
738 session->transport.lcl_port = mp->lcl_port;
739 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_queue_t *);
741 utm->state = utm->is_connected ? STATE_BOUND : STATE_READY;
745 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
747 udp_echo_main_t *utm = &udp_echo_main;
748 svm_fifo_segment_create_args_t _a, *a = &_a;
749 svm_fifo_segment_private_t *seg;
753 memset (a, 0, sizeof (*a));
754 a->segment_name = (char *) mp->segment_name;
755 a->segment_size = mp->segment_size;
756 /* Attach to the segment vpp created */
757 rv = svm_fifo_segment_attach (a);
760 clib_warning ("svm_fifo_segment_attach ('%s') failed",
764 seg = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
765 clib_warning ("Mapped new segment '%s' size %d", seg->ssvm.name,
766 seg->ssvm.ssvm_size);
767 seg_name = format (0, "%s", (char *) mp->segment_name);
768 hash_set_mem (utm->segments_table, seg_name, a->new_segment_indices[0]);
773 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
775 udp_echo_main_t *utm = &udp_echo_main;
776 svm_fifo_segment_private_t *seg;
781 seg_name = format (0, "%s", mp->segment_name);
782 seg_indexp = hash_get_mem (utm->segments_table, seg_name);
785 clib_warning ("segment not mapped: %s", seg_name);
788 hash_unset_mem (utm->segments_table, seg_name);
789 seg = svm_fifo_segment_get_segment ((u32) seg_indexp[0]);
790 svm_fifo_segment_delete (seg);
791 clib_warning ("Unmapped segment '%s'", seg_name);
796 * Acting as server for redirected connect requests
799 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
802 udp_echo_main_t *utm = &udp_echo_main;
803 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
804 svm_fifo_segment_create_args_t _a, *a = &_a;
805 svm_fifo_segment_private_t *seg;
806 svm_queue_t *client_q;
807 vl_api_connect_session_reply_t *rmp;
808 app_session_t *session = 0;
811 /* Create the segment */
812 a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid,
813 utm->unique_segment_index++, 0);
814 a->segment_size = utm->configured_segment_size;
816 rv = svm_fifo_segment_create (a);
819 clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name);
820 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
824 vec_add2 (utm->seg, seg, 1);
826 segment_index = vec_len (sm->segments) - 1;
827 memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0]));
829 pool_get (utm->sessions, session);
831 session->rx_fifo = svm_fifo_segment_alloc_fifo
832 (utm->seg, 128 * 1024, FIFO_SEGMENT_RX_FREELIST);
833 ASSERT (session->rx_fifo);
835 session->tx_fifo = svm_fifo_segment_alloc_fifo
836 (utm->seg, 128 * 1024, FIFO_SEGMENT_TX_FREELIST);
837 ASSERT (session->tx_fifo);
839 session->rx_fifo->master_session_index = session - utm->sessions;
840 session->tx_fifo->master_session_index = session - utm->sessions;
841 utm->cut_through_session_index = session - utm->sessions;
843 rv = pthread_create (&utm->cut_through_thread_handle,
844 NULL /*attr */ , cut_through_thread_fn, 0);
847 clib_warning ("pthread_create returned %d", rv);
848 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
852 rmp = vl_msg_api_alloc (sizeof (*rmp));
853 memset (rmp, 0, sizeof (*rmp));
855 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
856 rmp->context = mp->context;
857 rmp->retval = ntohl (rv);
858 rmp->segment_name_length = vec_len (a->segment_name);
861 rmp->server_rx_fifo = pointer_to_uword (session->rx_fifo);
862 rmp->server_tx_fifo = pointer_to_uword (session->tx_fifo);
865 memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
867 vec_free (a->segment_name);
869 client_q = uword_to_pointer (mp->client_queue_address, svm_queue_t *);
870 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
874 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
876 udp_echo_main_t *utm = &udp_echo_main;
879 clib_warning ("returned %d", ntohl (mp->retval));
881 utm->state = STATE_START;
885 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
887 udp_echo_main_t *utm = &udp_echo_main;
888 vl_api_accept_session_reply_t *rmp;
889 svm_fifo_t *rx_fifo, *tx_fifo;
890 app_session_t *session;
891 static f64 start_time;
895 if (start_time == 0.0)
896 start_time = clib_time_now (&utm->clib_time);
898 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
900 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
901 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
903 pool_get (utm->sessions, session);
904 memset (session, 0, sizeof (*session));
905 session_index = session - utm->sessions;
907 /* Cut-through case */
908 if (mp->server_event_queue_address)
910 clib_warning ("cut-through session");
911 utm->our_event_queue = uword_to_pointer (mp->server_event_queue_address,
913 rx_fifo->master_session_index = session_index;
914 tx_fifo->master_session_index = session_index;
915 utm->cut_through_session_index = session_index;
916 session->rx_fifo = rx_fifo;
917 session->tx_fifo = tx_fifo;
919 rv = pthread_create (&utm->cut_through_thread_handle,
920 NULL /*attr */ , cut_through_thread_fn, 0);
923 clib_warning ("pthread_create returned %d", rv);
924 rv = VNET_API_ERROR_SYSCALL_ERROR_1;
930 rx_fifo->client_session_index = session_index;
931 tx_fifo->client_session_index = session_index;
932 session->rx_fifo = rx_fifo;
933 session->tx_fifo = tx_fifo;
934 clib_memcpy (&session->transport.rmt_ip, mp->ip,
935 sizeof (ip46_address_t));
936 session->transport.is_ip4 = mp->is_ip4;
937 session->transport.rmt_port = mp->port;
940 hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index);
941 if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0)
943 f64 now = clib_time_now (&utm->clib_time);
944 fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n",
945 pool_elts (utm->sessions), now - start_time,
946 (f64) pool_elts (utm->sessions) / (now - start_time));
949 rmp = vl_msg_api_alloc (sizeof (*rmp));
950 memset (rmp, 0, sizeof (*rmp));
951 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
952 rmp->handle = mp->handle;
953 rmp->context = mp->context;
955 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
957 CLIB_MEMORY_BARRIER ();
958 utm->state = STATE_READY;
962 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
964 udp_echo_main_t *utm = &udp_echo_main;
965 app_session_t *session;
966 vl_api_disconnect_session_reply_t *rmp;
970 p = hash_get (utm->session_index_by_vpp_handles, mp->handle);
974 session = pool_elt_at_index (utm->sessions, p[0]);
975 hash_unset (utm->session_index_by_vpp_handles, mp->handle);
976 pool_put (utm->sessions, session);
980 clib_warning ("couldn't find session key %llx", mp->handle);
984 rmp = vl_msg_api_alloc (sizeof (*rmp));
985 memset (rmp, 0, sizeof (*rmp));
986 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
988 rmp->handle = mp->handle;
989 rmp->context = mp->context;
990 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp);
994 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
996 udp_echo_main_t *utm = &udp_echo_main;
997 unformat_input_t _input, *input = &_input;
998 session_endpoint_extended_t _sep, *sep = &_sep;
999 app_session_t *session;
1001 ASSERT (utm->i_am_server == 0);
1005 clib_warning ("failed connect");
1009 ASSERT (mp->server_rx_fifo && mp->server_tx_fifo);
1011 pool_get (utm->sessions, session);
1012 session->rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1013 session->tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1014 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
1016 /* Cut-through case */
1017 if (mp->client_event_queue_address)
1019 clib_warning ("cut-through session");
1020 utm->cut_through_session_index = session - utm->sessions;
1021 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1023 utm->our_event_queue = uword_to_pointer (mp->client_event_queue_address,
1029 utm->connected_session = session - utm->sessions;
1030 utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1033 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
1034 sizeof (ip46_address_t));
1035 session->transport.is_ip4 = mp->is_ip4;
1036 session->transport.lcl_port = mp->lcl_port;
1038 unformat_init_vector (input, utm->connect_uri);
1039 if (!unformat (input, "%U", unformat_uri, sep))
1041 clib_warning ("can't figure out remote ip and port");
1042 utm->state = STATE_FAILED;
1043 unformat_free (input);
1046 unformat_free (input);
1047 clib_memcpy (&session->transport.rmt_ip, &sep->ip,
1048 sizeof (ip46_address_t));
1049 session->transport.rmt_port = sep->port;
1050 session->is_dgram = !utm->is_connected;
1052 utm->state = STATE_READY;
1055 #define foreach_tcp_echo_msg \
1056 _(BIND_URI_REPLY, bind_uri_reply) \
1057 _(CONNECT_URI, connect_uri) \
1058 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1059 _(UNBIND_URI_REPLY, unbind_uri_reply) \
1060 _(ACCEPT_SESSION, accept_session) \
1061 _(DISCONNECT_SESSION, disconnect_session) \
1062 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1063 _(UNMAP_SEGMENT, unmap_segment) \
1064 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1065 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1068 tcp_echo_api_hookup (udp_echo_main_t * utm)
1071 vl_msg_api_set_handlers(VL_API_##N, #n, \
1072 vl_api_##n##_t_handler, \
1074 vl_api_##n##_t_endian, \
1075 vl_api_##n##_t_print, \
1076 sizeof(vl_api_##n##_t), 1);
1077 foreach_tcp_echo_msg;
1083 connect_to_vpp (char *name)
1085 udp_echo_main_t *utm = &udp_echo_main;
1086 api_main_t *am = &api_main;
1088 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
1091 utm->vl_input_queue = am->shmem_hdr->vl_input_queue;
1092 utm->my_client_index = am->my_client_index;
1098 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
1100 clib_warning ("BUG");
1104 init_error_string_table (udp_echo_main_t * utm)
1106 utm->error_string_by_error_number = hash_create (0, sizeof (uword));
1108 #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s);
1109 foreach_vnet_api_error;
1112 hash_set (utm->error_string_by_error_number, 99, "Misc");
1116 server_handle_fifo_event_rx (udp_echo_main_t * utm, session_fifo_event_t * e)
1121 s = pool_elt_at_index (utm->sessions, e->fifo->client_session_index);
1122 app_recv (s, utm->rx_buf, vec_len (utm->rx_buf));
1128 rv = app_send_stream (s, utm->rx_buf, vec_len (utm->rx_buf), 0);
1130 while (rv == SVM_FIFO_FULL);
1135 server_handle_event_queue (udp_echo_main_t * utm)
1137 session_fifo_event_t _e, *e = &_e;
1139 while (utm->state != STATE_READY)
1144 svm_queue_sub (utm->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0);
1145 switch (e->event_type)
1147 case FIFO_EVENT_APP_RX:
1148 server_handle_fifo_event_rx (utm, e);
1151 case FIFO_EVENT_DISCONNECT:
1155 clib_warning ("unknown event type %d", e->event_type);
1158 if (PREDICT_FALSE (utm->time_to_stop == 1))
1160 if (PREDICT_FALSE (utm->time_to_print_stats == 1))
1162 utm->time_to_print_stats = 0;
1163 fformat (stdout, "%d connections\n", pool_elts (utm->sessions));
1169 server_unbind (udp_echo_main_t * utm)
1171 vl_api_unbind_uri_t *ump;
1173 ump = vl_msg_api_alloc (sizeof (*ump));
1174 memset (ump, 0, sizeof (*ump));
1176 ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
1177 ump->client_index = utm->my_client_index;
1178 memcpy (ump->uri, utm->listen_uri, vec_len (utm->listen_uri));
1179 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump);
1183 server_bind (udp_echo_main_t * utm)
1185 vl_api_bind_uri_t *bmp;
1187 bmp = vl_msg_api_alloc (sizeof (*bmp));
1188 memset (bmp, 0, sizeof (*bmp));
1190 bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
1191 bmp->client_index = utm->my_client_index;
1192 bmp->context = ntohl (0xfeedface);
1193 memcpy (bmp->uri, utm->listen_uri, vec_len (utm->listen_uri));
1194 vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp);
1198 udp_server_test (udp_echo_main_t * utm)
1200 u8 wait_for_state = utm->is_connected ? STATE_BOUND : STATE_READY;
1201 application_send_attach (utm);
1206 if (wait_for_state_change (utm, wait_for_state))
1208 clib_warning ("timeout waiting for state change");
1212 server_handle_event_queue (utm);
1215 server_unbind (utm);
1217 if (wait_for_state_change (utm, STATE_START))
1219 clib_warning ("timeout waiting for STATE_START");
1223 application_detach (utm);
1225 fformat (stdout, "Test complete...\n");
1229 main (int argc, char **argv)
1231 udp_echo_main_t *utm = &udp_echo_main;
1232 u8 *uri = (u8 *) "udp://6.0.1.1/1234";
1233 unformat_input_t _argv, *a = &_argv;
1234 int i_am_server = 1;
1235 app_session_t *session;
1243 clib_mem_init (0, 256 << 20);
1244 heap = clib_mem_get_per_cpu_heap ();
1245 h = mheap_header (heap);
1246 /* make the main heap thread-safe */
1247 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1248 svm_fifo_segment_main_init (0x200000000ULL, 20);
1250 vec_validate (utm->rx_buf, 8192);
1251 utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1252 utm->my_pid = getpid ();
1253 utm->configured_segment_size = 1 << 20;
1254 utm->segments_table = hash_create_vec (0, sizeof (u8), sizeof (u64));
1255 utm->have_return = 1;
1256 utm->bytes_to_send = 1024;
1257 utm->fifo_size = 128 << 10;
1258 utm->segment_main = &svm_fifo_segment_main;
1259 utm->cut_through_session_index = ~0;
1260 clib_time_init (&utm->clib_time);
1262 init_error_string_table (utm);
1263 unformat_init_command_line (a, argv);
1265 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
1267 if (unformat (a, "chroot prefix %s", &chroot_prefix))
1269 vl_set_memory_root_path ((char *) chroot_prefix);
1271 else if (unformat (a, "uri %s", &uri))
1273 else if (unformat (a, "segment-size %dM", &tmp))
1274 utm->configured_segment_size = tmp << 20;
1275 else if (unformat (a, "segment-size %dG", &tmp))
1276 utm->configured_segment_size = tmp << 30;
1277 else if (unformat (a, "server"))
1279 else if (unformat (a, "client"))
1281 else if (unformat (a, "no-return"))
1282 utm->have_return = 0;
1283 else if (unformat (a, "mbytes %d", &tmp))
1284 utm->bytes_to_send = (u64) tmp << 20;
1285 else if (unformat (a, "fifo-size %d", &tmp))
1286 utm->fifo_size = tmp << 10;
1289 fformat (stderr, "%s: usage [server|client]\n");
1294 utm->i_am_server = i_am_server;
1296 setup_signal_handlers ();
1297 tcp_echo_api_hookup (utm);
1301 utm->listen_uri = format (0, "%s%c", uri, 0);
1302 utm->is_connected = (utm->listen_uri[4] == 'c');
1303 app_name = "udp_echo_server";
1307 app_name = "udp_echo_client";
1308 utm->connect_uri = format (0, "%s%c", uri, 0);
1309 utm->is_connected = (utm->connect_uri[4] == 'c');
1311 if (connect_to_vpp (app_name) < 0)
1314 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
1318 if (i_am_server == 0)
1324 /* $$$$ hack preallocation */
1325 for (i = 0; i < 200000; i++)
1327 pool_get (utm->sessions, session);
1328 memset (session, 0, sizeof (*session));
1330 for (i = 0; i < 200000; i++)
1331 pool_put_index (utm->sessions, i);
1333 udp_server_test (utm);
1336 vl_client_disconnect_from_vlib ();
1340 #undef vl_api_version
1341 #define vl_api_version(n,v) static u32 vpe_api_version = v;
1342 #include <vpp/api/vpe.api.h>
1343 #undef vl_api_version
1346 vl_client_add_api_signatures (vl_api_memclnt_create_t * mp)
1349 * Send the main API signature in slot 0. This bit of code must
1350 * match the checks in ../vpe/api/api.c: vl_msg_api_version_check().
1352 mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version);
1362 * fd.io coding-style-patch-verification: ON
1365 * eval: (c-set-style "gnu")