Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <signal.h> |
| 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> |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 29 | #include <vpp/api/vpe_msg_enum.h> |
| 30 | #include <svm/svm_fifo_segment.h> |
| 31 | #include <pthread.h> |
| 32 | #include <vnet/session/application_interface.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 33 | |
| 34 | #define vl_typedefs /* define message structures */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 35 | #include <vpp/api/vpe_all_api_h.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 36 | #undef vl_typedefs |
| 37 | |
| 38 | /* declare message handlers for each api */ |
| 39 | |
| 40 | #define vl_endianfun /* define message structures */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 41 | #include <vpp/api/vpe_all_api_h.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 42 | #undef vl_endianfun |
| 43 | |
| 44 | /* instantiate all the print functions we know about */ |
| 45 | #define vl_print(handle, ...) |
| 46 | #define vl_printfun |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 47 | #include <vpp/api/vpe_all_api_h.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 48 | #undef vl_printfun |
| 49 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 50 | typedef enum |
| 51 | { |
| 52 | STATE_START, |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 53 | STATE_ATTACHED, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 54 | STATE_BOUND, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 55 | STATE_READY, |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 56 | STATE_FAILED, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 57 | STATE_DISCONNECTING, |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 58 | STATE_DETACHED |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 59 | } connection_state_t; |
| 60 | |
| 61 | typedef struct |
| 62 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 63 | /* vpe input queue */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 64 | svm_queue_t *vl_input_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 65 | |
| 66 | /* API client handle */ |
| 67 | u32 my_client_index; |
| 68 | |
| 69 | /* The URI we're playing with */ |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 70 | u8 *listen_uri; |
| 71 | |
| 72 | /* URI for connect */ |
| 73 | u8 *connect_uri; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 74 | |
| 75 | /* Session pool */ |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 76 | app_session_t *sessions; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 77 | |
| 78 | /* Hash table for disconnect processing */ |
| 79 | uword *session_index_by_vpp_handles; |
| 80 | |
| 81 | /* fifo segment */ |
| 82 | svm_fifo_segment_private_t *seg; |
| 83 | |
| 84 | /* intermediate rx buffer */ |
| 85 | u8 *rx_buf; |
| 86 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 87 | u32 fifo_size; |
| 88 | int i_am_server; |
| 89 | u8 is_connected; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 90 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 91 | /* Our event queue */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 92 | svm_msg_q_t *our_event_queue; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 93 | svm_msg_q_t *ct_event_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 94 | |
| 95 | /* $$$ single thread only for the moment */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 96 | svm_msg_q_t *vpp_event_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 97 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 98 | /* $$$$ hack: cut-through session index */ |
| 99 | volatile u32 cut_through_session_index; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 100 | volatile u32 connected_session; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 101 | |
| 102 | /* unique segment name counter */ |
| 103 | u32 unique_segment_index; |
| 104 | |
| 105 | pid_t my_pid; |
| 106 | |
| 107 | /* pthread handle */ |
| 108 | pthread_t cut_through_thread_handle; |
| 109 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 110 | /* For deadman timers */ |
| 111 | clib_time_t clib_time; |
| 112 | |
| 113 | /* State of the connection, shared between msg RX thread and main thread */ |
| 114 | volatile connection_state_t state; |
| 115 | |
| 116 | volatile int time_to_stop; |
| 117 | volatile int time_to_print_stats; |
| 118 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 119 | u32 configured_segment_size; |
| 120 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 121 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
| 122 | uword *error_string_by_error_number; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 123 | |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 124 | svm_fifo_segment_main_t segment_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 125 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 126 | u8 *connect_test_data; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 127 | |
| 128 | uword *segments_table; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 129 | u8 do_echo; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 130 | u8 have_return; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 131 | u64 total_to_send; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 132 | u64 bytes_to_send; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 133 | u64 bytes_sent; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 134 | } udp_echo_main_t; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 135 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 136 | udp_echo_main_t udp_echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 137 | |
| 138 | static void |
| 139 | stop_signal (int signum) |
| 140 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 141 | udp_echo_main_t *um = &udp_echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 142 | |
| 143 | um->time_to_stop = 1; |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | stats_signal (int signum) |
| 148 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 149 | udp_echo_main_t *um = &udp_echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 150 | um->time_to_print_stats = 1; |
| 151 | } |
| 152 | |
| 153 | static clib_error_t * |
| 154 | setup_signal_handlers (void) |
| 155 | { |
| 156 | signal (SIGINT, stats_signal); |
| 157 | signal (SIGQUIT, stop_signal); |
| 158 | signal (SIGTERM, stop_signal); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 163 | uword |
| 164 | unformat_ip4_address (unformat_input_t * input, va_list * args) |
| 165 | { |
| 166 | u8 *result = va_arg (*args, u8 *); |
| 167 | unsigned a[4]; |
| 168 | |
| 169 | if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3])) |
| 170 | return 0; |
| 171 | |
| 172 | if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256) |
| 173 | return 0; |
| 174 | |
| 175 | result[0] = a[0]; |
| 176 | result[1] = a[1]; |
| 177 | result[2] = a[2]; |
| 178 | result[3] = a[3]; |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | uword |
| 184 | unformat_ip6_address (unformat_input_t * input, va_list * args) |
| 185 | { |
| 186 | ip6_address_t *result = va_arg (*args, ip6_address_t *); |
| 187 | u16 hex_quads[8]; |
| 188 | uword hex_quad, n_hex_quads, hex_digit, n_hex_digits; |
| 189 | uword c, n_colon, double_colon_index; |
| 190 | |
| 191 | n_hex_quads = hex_quad = n_hex_digits = n_colon = 0; |
| 192 | double_colon_index = ARRAY_LEN (hex_quads); |
| 193 | while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT) |
| 194 | { |
| 195 | hex_digit = 16; |
| 196 | if (c >= '0' && c <= '9') |
| 197 | hex_digit = c - '0'; |
| 198 | else if (c >= 'a' && c <= 'f') |
| 199 | hex_digit = c + 10 - 'a'; |
| 200 | else if (c >= 'A' && c <= 'F') |
| 201 | hex_digit = c + 10 - 'A'; |
| 202 | else if (c == ':' && n_colon < 2) |
| 203 | n_colon++; |
| 204 | else |
| 205 | { |
| 206 | unformat_put_input (input); |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | /* Too many hex quads. */ |
| 211 | if (n_hex_quads >= ARRAY_LEN (hex_quads)) |
| 212 | return 0; |
| 213 | |
| 214 | if (hex_digit < 16) |
| 215 | { |
| 216 | hex_quad = (hex_quad << 4) | hex_digit; |
| 217 | |
| 218 | /* Hex quad must fit in 16 bits. */ |
| 219 | if (n_hex_digits >= 4) |
| 220 | return 0; |
| 221 | |
| 222 | n_colon = 0; |
| 223 | n_hex_digits++; |
| 224 | } |
| 225 | |
| 226 | /* Save position of :: */ |
| 227 | if (n_colon == 2) |
| 228 | { |
| 229 | /* More than one :: ? */ |
| 230 | if (double_colon_index < ARRAY_LEN (hex_quads)) |
| 231 | return 0; |
| 232 | double_colon_index = n_hex_quads; |
| 233 | } |
| 234 | |
| 235 | if (n_colon > 0 && n_hex_digits > 0) |
| 236 | { |
| 237 | hex_quads[n_hex_quads++] = hex_quad; |
| 238 | hex_quad = 0; |
| 239 | n_hex_digits = 0; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (n_hex_digits > 0) |
| 244 | hex_quads[n_hex_quads++] = hex_quad; |
| 245 | |
| 246 | { |
| 247 | word i; |
| 248 | |
| 249 | /* Expand :: to appropriate number of zero hex quads. */ |
| 250 | if (double_colon_index < ARRAY_LEN (hex_quads)) |
| 251 | { |
| 252 | word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads; |
| 253 | |
| 254 | for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--) |
| 255 | hex_quads[n_zero + i] = hex_quads[i]; |
| 256 | |
| 257 | for (i = 0; i < n_zero; i++) |
| 258 | hex_quads[double_colon_index + i] = 0; |
| 259 | |
| 260 | n_hex_quads = ARRAY_LEN (hex_quads); |
| 261 | } |
| 262 | |
| 263 | /* Too few hex quads given. */ |
| 264 | if (n_hex_quads < ARRAY_LEN (hex_quads)) |
| 265 | return 0; |
| 266 | |
| 267 | for (i = 0; i < ARRAY_LEN (hex_quads); i++) |
| 268 | result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]); |
| 269 | |
| 270 | return 1; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | uword |
| 275 | unformat_uri (unformat_input_t * input, va_list * args) |
| 276 | { |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 277 | session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 278 | u32 port; |
| 279 | char *tmp; |
| 280 | |
| 281 | if (unformat (input, "%s://%U/%d", &tmp, unformat_ip4_address, &sep->ip.ip4, |
| 282 | &port)) |
| 283 | { |
| 284 | sep->port = clib_host_to_net_u16 (port); |
| 285 | sep->is_ip4 = 1; |
| 286 | return 1; |
| 287 | } |
| 288 | else if (unformat (input, "%s://%U/%d", &tmp, unformat_ip6_address, |
| 289 | &sep->ip.ip6, &port)) |
| 290 | { |
| 291 | sep->port = clib_host_to_net_u16 (port); |
| 292 | sep->is_ip4 = 0; |
| 293 | return 1; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 298 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 299 | application_send_attach (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 300 | { |
| 301 | vl_api_application_attach_t *bmp; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 302 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 303 | clib_memset (bmp, 0, sizeof (*bmp)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 304 | |
| 305 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 306 | bmp->client_index = utm->my_client_index; |
| 307 | bmp->context = ntohl (0xfeedface); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 308 | bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ADD_SEGMENT; |
| 309 | bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 310 | bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 311 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 312 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = utm->fifo_size; |
| 313 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = utm->fifo_size; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 314 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20; |
| 315 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20; |
| 316 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 16768; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 317 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 318 | } |
| 319 | |
| 320 | void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 321 | application_detach (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 322 | { |
| 323 | vl_api_application_detach_t *bmp; |
| 324 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 325 | clib_memset (bmp, 0, sizeof (*bmp)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 326 | |
| 327 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 328 | bmp->client_index = utm->my_client_index; |
| 329 | bmp->context = ntohl (0xfeedface); |
| 330 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 331 | } |
| 332 | |
| 333 | static void |
| 334 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 335 | mp) |
| 336 | { |
Florin Coras | 84a30ef | 2018-01-24 10:49:23 -0800 | [diff] [blame] | 337 | svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 338 | udp_echo_main_t *utm = &udp_echo_main; |
| 339 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 340 | int rv; |
| 341 | |
| 342 | if (mp->retval) |
| 343 | { |
| 344 | clib_warning ("attach failed: %d", mp->retval); |
| 345 | utm->state = STATE_FAILED; |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (mp->segment_name_length == 0) |
| 350 | { |
| 351 | clib_warning ("segment_name_length zero"); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | a->segment_name = (char *) mp->segment_name; |
| 356 | a->segment_size = mp->segment_size; |
| 357 | |
| 358 | ASSERT (mp->app_event_queue_address); |
| 359 | |
| 360 | /* Attach to the segment vpp created */ |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 361 | rv = svm_fifo_segment_attach (sm, a); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 362 | if (rv) |
| 363 | { |
| 364 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 365 | mp->segment_name); |
| 366 | return; |
| 367 | } |
| 368 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 369 | utm->our_event_queue = uword_to_pointer (mp->app_event_queue_address, |
| 370 | svm_msg_q_t *); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 371 | utm->state = STATE_ATTACHED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void |
| 375 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 376 | mp) |
| 377 | { |
| 378 | if (mp->retval) |
| 379 | clib_warning ("detach returned with err: %d", mp->retval); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 380 | udp_echo_main.state = STATE_DETACHED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 383 | u8 * |
| 384 | format_api_error (u8 * s, va_list * args) |
| 385 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 386 | udp_echo_main_t *utm = va_arg (*args, udp_echo_main_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 387 | i32 error = va_arg (*args, u32); |
| 388 | uword *p; |
| 389 | |
| 390 | p = hash_get (utm->error_string_by_error_number, -error); |
| 391 | |
| 392 | if (p) |
| 393 | s = format (s, "%s", p[0]); |
| 394 | else |
| 395 | s = format (s, "%d", error); |
| 396 | return s; |
| 397 | } |
| 398 | |
| 399 | int |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 400 | wait_for_state_change (udp_echo_main_t * utm, connection_state_t state) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 401 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 402 | #if CLIB_DEBUG > 0 |
| 403 | #define TIMEOUT 600.0 |
| 404 | #else |
| 405 | #define TIMEOUT 600.0 |
| 406 | #endif |
| 407 | |
| 408 | f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 409 | |
| 410 | while (clib_time_now (&utm->clib_time) < timeout) |
| 411 | { |
| 412 | if (utm->state == state) |
| 413 | return 0; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 414 | if (utm->state == STATE_FAILED) |
| 415 | return -1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | } |
| 417 | return -1; |
| 418 | } |
| 419 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 420 | u64 server_bytes_received, server_bytes_sent; |
| 421 | |
| 422 | static void * |
| 423 | cut_through_thread_fn (void *arg) |
| 424 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 425 | app_session_t *s; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 426 | svm_fifo_t *rx_fifo; |
| 427 | svm_fifo_t *tx_fifo; |
| 428 | u8 *my_copy_buffer = 0; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 429 | udp_echo_main_t *utm = &udp_echo_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 430 | i32 actual_transfer; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 431 | int rv, do_dequeue = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 432 | u32 buffer_offset; |
| 433 | |
| 434 | while (utm->cut_through_session_index == ~0) |
| 435 | ; |
| 436 | |
| 437 | s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index); |
| 438 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 439 | rx_fifo = s->rx_fifo; |
| 440 | tx_fifo = s->tx_fifo; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 441 | |
| 442 | vec_validate (my_copy_buffer, 64 * 1024 - 1); |
| 443 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 444 | while (1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 445 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 446 | do |
| 447 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 448 | /* We read from the tx fifo and write to the rx fifo */ |
| 449 | if (utm->have_return || do_dequeue) |
| 450 | actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, |
| 451 | vec_len |
| 452 | (my_copy_buffer), |
| 453 | my_copy_buffer); |
| 454 | else |
| 455 | { |
| 456 | /* We don't do anything with the data, drop it */ |
| 457 | actual_transfer = svm_fifo_max_dequeue (rx_fifo); |
| 458 | svm_fifo_dequeue_drop (rx_fifo, actual_transfer); |
| 459 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 460 | } |
| 461 | while (actual_transfer <= 0); |
| 462 | |
| 463 | server_bytes_received += actual_transfer; |
| 464 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 465 | if (utm->have_return) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 466 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 467 | buffer_offset = 0; |
| 468 | while (actual_transfer > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 469 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 470 | rv = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer, |
| 471 | my_copy_buffer + buffer_offset); |
| 472 | if (rv > 0) |
| 473 | { |
| 474 | actual_transfer -= rv; |
| 475 | buffer_offset += rv; |
| 476 | server_bytes_sent += rv; |
| 477 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 478 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 479 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 480 | } |
| 481 | if (PREDICT_FALSE (utm->time_to_stop)) |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | pthread_exit (0); |
| 486 | } |
| 487 | |
| 488 | static void |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 489 | session_accepted_handler (session_accepted_msg_t * mp) |
| 490 | { |
| 491 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 492 | udp_echo_main_t *utm = &udp_echo_main; |
| 493 | session_accepted_reply_msg_t *rmp; |
| 494 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 495 | app_session_t *session; |
| 496 | static f64 start_time; |
| 497 | u32 session_index; |
| 498 | int rv = 0; |
| 499 | |
| 500 | if (start_time == 0.0) |
| 501 | start_time = clib_time_now (&utm->clib_time); |
| 502 | |
| 503 | utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 504 | svm_msg_q_t *); |
| 505 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 506 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 507 | |
| 508 | pool_get (utm->sessions, session); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 509 | clib_memset (session, 0, sizeof (*session)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 510 | session_index = session - utm->sessions; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 511 | session->session_index = session_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 512 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 513 | rx_fifo->client_session_index = session_index; |
| 514 | tx_fifo->client_session_index = session_index; |
| 515 | session->rx_fifo = rx_fifo; |
| 516 | session->tx_fifo = tx_fifo; |
| 517 | clib_memcpy_fast (&session->transport.rmt_ip, mp->ip, |
| 518 | sizeof (ip46_address_t)); |
| 519 | session->transport.is_ip4 = mp->is_ip4; |
| 520 | session->transport.rmt_port = mp->port; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 521 | |
| 522 | hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index); |
| 523 | if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0) |
| 524 | { |
| 525 | f64 now = clib_time_now (&utm->clib_time); |
| 526 | fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n", |
| 527 | pool_elts (utm->sessions), now - start_time, |
| 528 | (f64) pool_elts (utm->sessions) / (now - start_time)); |
| 529 | } |
| 530 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 531 | app_alloc_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt, |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 532 | SESSION_CTRL_EVT_ACCEPTED_REPLY); |
| 533 | rmp = (session_accepted_reply_msg_t *) app_evt->evt->data; |
| 534 | rmp->handle = mp->handle; |
| 535 | rmp->context = mp->context; |
| 536 | rmp->retval = rv; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 537 | app_send_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 538 | |
| 539 | CLIB_MEMORY_BARRIER (); |
| 540 | utm->state = STATE_READY; |
| 541 | } |
| 542 | |
| 543 | static void |
| 544 | session_disconnected_handler (session_disconnected_msg_t * mp) |
| 545 | { |
| 546 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 547 | udp_echo_main_t *utm = &udp_echo_main; |
| 548 | session_disconnected_reply_msg_t *rmp; |
| 549 | app_session_t *session; |
| 550 | uword *p; |
| 551 | int rv = 0; |
| 552 | |
| 553 | p = hash_get (utm->session_index_by_vpp_handles, mp->handle); |
| 554 | |
| 555 | if (p) |
| 556 | { |
| 557 | session = pool_elt_at_index (utm->sessions, p[0]); |
| 558 | hash_unset (utm->session_index_by_vpp_handles, mp->handle); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 559 | clib_warning ("disconnecting %u", session->session_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 560 | pool_put (utm->sessions, session); |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | clib_warning ("couldn't find session key %llx", mp->handle); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt, |
| 569 | SESSION_CTRL_EVT_DISCONNECTED_REPLY); |
| 570 | rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data; |
| 571 | rmp->retval = rv; |
| 572 | rmp->handle = mp->handle; |
| 573 | rmp->context = mp->context; |
| 574 | app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt); |
| 575 | } |
| 576 | |
| 577 | static void |
| 578 | session_connected_handler (session_connected_msg_t * mp) |
| 579 | { |
| 580 | udp_echo_main_t *utm = &udp_echo_main; |
| 581 | unformat_input_t _input, *input = &_input; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 582 | session_endpoint_cfg_t _sep, *sep = &_sep; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 583 | app_session_t *session; |
| 584 | |
| 585 | ASSERT (utm->i_am_server == 0); |
| 586 | |
| 587 | if (mp->retval) |
| 588 | { |
| 589 | clib_warning ("failed connect"); |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | ASSERT (mp->server_rx_fifo && mp->server_tx_fifo); |
| 594 | |
| 595 | pool_get (utm->sessions, session); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 596 | session->session_index = session - utm->sessions; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 597 | session->rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 598 | session->tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 599 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 600 | /* Cut-through case */ |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 601 | if (mp->ct_rx_fifo) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 602 | { |
| 603 | clib_warning ("cut-through session"); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 604 | session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 605 | svm_msg_q_t *); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 606 | utm->cut_through_session_index = session->session_index; |
| 607 | session->is_dgram = 0; |
| 608 | sleep (1); |
| 609 | session->rx_fifo->client_session_index = session->session_index; |
| 610 | session->tx_fifo->client_session_index = session->session_index; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 611 | /* TODO use ct fifos */ |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 612 | } |
| 613 | else |
| 614 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 615 | utm->connected_session = session->session_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 616 | utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 617 | svm_msg_q_t *); |
| 618 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 619 | session->rx_fifo->client_session_index = session->session_index; |
| 620 | session->tx_fifo->client_session_index = session->session_index; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 621 | clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip, |
| 622 | sizeof (ip46_address_t)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 623 | session->transport.is_ip4 = mp->is_ip4; |
| 624 | session->transport.lcl_port = mp->lcl_port; |
| 625 | |
| 626 | unformat_init_vector (input, utm->connect_uri); |
| 627 | if (!unformat (input, "%U", unformat_uri, sep)) |
| 628 | { |
| 629 | clib_warning ("can't figure out remote ip and port"); |
| 630 | utm->state = STATE_FAILED; |
| 631 | unformat_free (input); |
| 632 | return; |
| 633 | } |
| 634 | unformat_free (input); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 635 | clib_memcpy_fast (&session->transport.rmt_ip, &sep->ip, |
| 636 | sizeof (ip46_address_t)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 637 | session->transport.rmt_port = sep->port; |
| 638 | session->is_dgram = !utm->is_connected; |
| 639 | } |
| 640 | utm->state = STATE_READY; |
| 641 | } |
| 642 | |
| 643 | static void |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 644 | session_bound_handler (session_bound_msg_t * mp) |
| 645 | { |
| 646 | udp_echo_main_t *utm = &udp_echo_main; |
| 647 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 648 | app_session_t *session; |
| 649 | u32 session_index; |
| 650 | |
| 651 | if (mp->retval) |
| 652 | { |
| 653 | clib_warning ("bind failed: %d", mp->retval); |
| 654 | utm->state = STATE_FAILED; |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *); |
| 659 | tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *); |
| 660 | |
| 661 | pool_get (utm->sessions, session); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 662 | clib_memset (session, 0, sizeof (*session)); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 663 | session_index = session - utm->sessions; |
| 664 | |
| 665 | rx_fifo->client_session_index = session_index; |
| 666 | tx_fifo->client_session_index = session_index; |
| 667 | session->rx_fifo = rx_fifo; |
| 668 | session->tx_fifo = tx_fifo; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 669 | clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip, |
| 670 | sizeof (ip46_address_t)); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 671 | session->transport.is_ip4 = mp->lcl_is_ip4; |
| 672 | session->transport.lcl_port = mp->lcl_port; |
| 673 | session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *); |
| 674 | |
| 675 | utm->state = utm->is_connected ? STATE_BOUND : STATE_READY; |
| 676 | } |
| 677 | |
| 678 | static void |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 679 | handle_mq_event (session_event_t * e) |
| 680 | { |
| 681 | switch (e->event_type) |
| 682 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 683 | case SESSION_CTRL_EVT_BOUND: |
| 684 | session_bound_handler ((session_bound_msg_t *) e->data); |
| 685 | break; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 686 | case SESSION_CTRL_EVT_ACCEPTED: |
| 687 | session_accepted_handler ((session_accepted_msg_t *) e->data); |
| 688 | break; |
| 689 | case SESSION_CTRL_EVT_CONNECTED: |
| 690 | session_connected_handler ((session_connected_msg_t *) e->data); |
| 691 | break; |
| 692 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 693 | session_disconnected_handler ((session_disconnected_msg_t *) e->data); |
| 694 | break; |
| 695 | default: |
| 696 | clib_warning ("unhandled %u", e->event_type); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static void |
| 701 | udp_client_send_connect (udp_echo_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 702 | { |
| 703 | vl_api_connect_uri_t *cmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 704 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 705 | clib_memset (cmp, 0, sizeof (*cmp)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 706 | |
| 707 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
| 708 | cmp->client_index = utm->my_client_index; |
| 709 | cmp->context = ntohl (0xfeedface); |
| 710 | memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri)); |
| 711 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 712 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 713 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 714 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 715 | send_test_chunk (udp_echo_main_t * utm, app_session_t * s, u32 bytes) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 716 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 717 | u64 test_buf_len, bytes_this_chunk, test_buf_offset; |
| 718 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 719 | u8 *test_data = utm->connect_test_data; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 720 | u32 bytes_to_snd, enq_space, min_chunk; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 721 | int written; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 722 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 723 | test_buf_len = vec_len (test_data); |
| 724 | test_buf_offset = utm->bytes_sent % test_buf_len; |
| 725 | bytes_this_chunk = clib_min (test_buf_len - test_buf_offset, |
| 726 | utm->bytes_to_send); |
| 727 | enq_space = svm_fifo_max_enqueue (s->tx_fifo); |
| 728 | bytes_this_chunk = clib_min (bytes_this_chunk, enq_space); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 729 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 730 | written = app_send (s, test_data + test_buf_offset, bytes_this_chunk, |
| 731 | SVM_Q_WAIT); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 732 | if (written > 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 733 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 734 | utm->bytes_to_send -= written; |
| 735 | utm->bytes_sent += written; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 740 | recv_test_chunk (udp_echo_main_t * utm, app_session_t * s) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 741 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 742 | app_recv (s, utm->rx_buf, vec_len (utm->rx_buf)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 746 | client_send_data (udp_echo_main_t * utm, u32 session_index) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 747 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 748 | f64 start_time, end_time, delta; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 749 | app_session_t *session; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 750 | char *transfer_type; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 751 | u8 *test_data; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 752 | int i; |
| 753 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 754 | vec_validate_aligned (utm->connect_test_data, 1024 * 1024 - 1, |
| 755 | CLIB_CACHE_LINE_BYTES); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 756 | for (i = 0; i < vec_len (utm->connect_test_data); i++) |
| 757 | utm->connect_test_data[i] = i & 0xff; |
| 758 | |
| 759 | test_data = utm->connect_test_data; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 760 | session = pool_elt_at_index (utm->sessions, session_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 761 | ASSERT (vec_len (test_data) > 0); |
| 762 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 763 | utm->total_to_send = utm->bytes_to_send; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 764 | vec_validate (utm->rx_buf, vec_len (test_data) - 1); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 765 | start_time = clib_time_now (&utm->clib_time); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 766 | while (!utm->time_to_stop && utm->bytes_to_send) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 767 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 768 | send_test_chunk (utm, session, 0); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 769 | if (utm->have_return) |
| 770 | recv_test_chunk (utm, session); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 771 | if (utm->time_to_stop) |
| 772 | break; |
| 773 | } |
| 774 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 775 | if (utm->have_return) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 776 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 777 | f64 timeout = clib_time_now (&utm->clib_time) + 5; |
| 778 | while (clib_time_now (&utm->clib_time) < timeout) |
| 779 | recv_test_chunk (utm, session); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 782 | end_time = clib_time_now (&utm->clib_time); |
| 783 | delta = end_time - start_time; |
| 784 | transfer_type = utm->have_return ? "full-duplex" : "half-duplex"; |
| 785 | clib_warning ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 786 | utm->total_to_send, utm->total_to_send / (1ULL << 20), |
| 787 | utm->total_to_send / (1ULL << 30), delta); |
| 788 | clib_warning ("%.2f bytes/second %s", ((f64) utm->total_to_send) / (delta), |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 789 | transfer_type); |
| 790 | clib_warning ("%.4f gbit/second %s", |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 791 | (((f64) utm->total_to_send * 8.0) / delta / 1e9), |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 792 | transfer_type); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 795 | static int |
| 796 | application_attach (udp_echo_main_t * utm) |
| 797 | { |
| 798 | application_send_attach (utm); |
| 799 | if (wait_for_state_change (utm, STATE_ATTACHED)) |
| 800 | { |
| 801 | clib_warning ("timeout waiting for STATE_ATTACHED"); |
| 802 | return -1; |
| 803 | } |
| 804 | return 0; |
| 805 | } |
| 806 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 807 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 808 | client_test (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 809 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 810 | f64 start_time, timeout = 100.0; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 811 | app_session_t *session; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 812 | svm_msg_q_msg_t msg; |
| 813 | session_event_t *e; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 814 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 815 | if (application_attach (utm)) |
| 816 | return; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 817 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 818 | udp_client_send_connect (utm); |
| 819 | |
| 820 | start_time = clib_time_now (&utm->clib_time); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 821 | while (pool_elts (utm->sessions) != 1 && utm->state != STATE_FAILED) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 822 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 823 | svm_msg_q_sub (utm->our_event_queue, &msg, SVM_Q_WAIT, 0); |
| 824 | e = svm_msg_q_msg_data (utm->our_event_queue, &msg); |
| 825 | handle_mq_event (e); |
| 826 | svm_msg_q_free_msg (utm->our_event_queue, &msg); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 827 | |
| 828 | if (clib_time_now (&utm->clib_time) - start_time >= timeout) |
| 829 | break; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 832 | if (utm->cut_through_session_index != ~0) |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 833 | client_send_data (utm, utm->cut_through_session_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 834 | else |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 835 | client_send_data (utm, utm->connected_session); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 836 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 837 | application_detach (utm); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 838 | wait_for_state_change (utm, STATE_DETACHED); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 842 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 843 | { |
| 844 | svm_fifo_segment_create_args_t _a, *a = &_a; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 845 | udp_echo_main_t *utm = &udp_echo_main; |
| 846 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 847 | svm_fifo_segment_private_t *seg; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 848 | int rv; |
| 849 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 850 | clib_memset (a, 0, sizeof (*a)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 851 | a->segment_name = (char *) mp->segment_name; |
| 852 | a->segment_size = mp->segment_size; |
| 853 | /* Attach to the segment vpp created */ |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 854 | rv = svm_fifo_segment_attach (sm, a); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 855 | if (rv) |
| 856 | { |
| 857 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 858 | mp->segment_name); |
| 859 | return; |
| 860 | } |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 861 | seg = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 862 | clib_warning ("Mapped new segment '%s' size %d", seg->ssvm.name, |
| 863 | seg->ssvm.ssvm_size); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 864 | hash_set (utm->segments_table, clib_net_to_host_u64 (mp->segment_handle), |
| 865 | a->new_segment_indices[0]); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static void |
| 869 | vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp) |
| 870 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 871 | udp_echo_main_t *utm = &udp_echo_main; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 872 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 873 | svm_fifo_segment_private_t *seg; |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 874 | uword *seg_indexp; |
| 875 | u64 segment_handle; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 876 | |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 877 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 878 | seg_indexp = hash_get (utm->segments_table, segment_handle); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 879 | if (!seg_indexp) |
| 880 | { |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 881 | clib_warning ("segment not mapped: %s", segment_handle); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 882 | return; |
| 883 | } |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 884 | hash_unset (utm->segments_table, segment_handle); |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 885 | seg = svm_fifo_segment_get_segment (sm, (u32) seg_indexp[0]); |
| 886 | svm_fifo_segment_delete (sm, seg); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 887 | clib_warning ("Unmapped segment '%s'", segment_handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 888 | } |
| 889 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 890 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 891 | vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp) |
| 892 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 893 | udp_echo_main_t *utm = &udp_echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 894 | |
| 895 | if (mp->retval != 0) |
| 896 | clib_warning ("returned %d", ntohl (mp->retval)); |
| 897 | |
| 898 | utm->state = STATE_START; |
| 899 | } |
| 900 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 901 | static void |
| 902 | vl_api_app_cut_through_registration_add_t_handler |
| 903 | (vl_api_app_cut_through_registration_add_t * mp) |
| 904 | { |
| 905 | |
| 906 | } |
| 907 | |
| 908 | #define foreach_tcp_echo_msg \ |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 909 | _(UNBIND_URI_REPLY, unbind_uri_reply) \ |
| 910 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
| 911 | _(UNMAP_SEGMENT, unmap_segment) \ |
| 912 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 913 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 914 | _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 915 | |
| 916 | void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 917 | tcp_echo_api_hookup (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 918 | { |
| 919 | #define _(N,n) \ |
| 920 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 921 | vl_api_##n##_t_handler, \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 922 | vl_noop_handler, \ |
| 923 | vl_api_##n##_t_endian, \ |
| 924 | vl_api_##n##_t_print, \ |
| 925 | sizeof(vl_api_##n##_t), 1); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 926 | foreach_tcp_echo_msg; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 927 | #undef _ |
| 928 | |
| 929 | } |
| 930 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 931 | int |
| 932 | connect_to_vpp (char *name) |
| 933 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 934 | udp_echo_main_t *utm = &udp_echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 935 | api_main_t *am = &api_main; |
| 936 | |
| 937 | if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0) |
| 938 | return -1; |
| 939 | |
| 940 | utm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 941 | utm->my_client_index = am->my_client_index; |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | void |
| 947 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 948 | { |
| 949 | clib_warning ("BUG"); |
| 950 | } |
| 951 | |
| 952 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 953 | init_error_string_table (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 954 | { |
| 955 | utm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 956 | |
| 957 | #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s); |
| 958 | foreach_vnet_api_error; |
| 959 | #undef _ |
| 960 | |
| 961 | hash_set (utm->error_string_by_error_number, 99, "Misc"); |
| 962 | } |
| 963 | |
| 964 | void |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 965 | server_handle_fifo_event_rx (udp_echo_main_t * utm, u32 session_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 966 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 967 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 968 | int n_read; |
| 969 | app_session_t *session; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 970 | int rv; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 971 | u32 max_dequeue, offset, max_transfer, rx_buf_len; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 972 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 973 | session = pool_elt_at_index (utm->sessions, session_index); |
| 974 | rx_buf_len = vec_len (utm->rx_buf); |
| 975 | rx_fifo = session->rx_fifo; |
| 976 | tx_fifo = session->tx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 977 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 978 | max_dequeue = svm_fifo_max_dequeue (rx_fifo); |
| 979 | /* Allow enqueuing of a new event */ |
| 980 | svm_fifo_unset_event (rx_fifo); |
| 981 | |
| 982 | if (PREDICT_FALSE (!max_dequeue)) |
| 983 | return; |
| 984 | |
| 985 | /* Read the max_dequeue */ |
| 986 | do |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 987 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 988 | max_transfer = clib_min (rx_buf_len, max_dequeue); |
| 989 | if (session->is_dgram) |
| 990 | n_read = app_recv_dgram_raw (rx_fifo, utm->rx_buf, max_transfer, |
| 991 | &session->transport, 0, 0); |
| 992 | else |
| 993 | n_read = app_recv_stream_raw (rx_fifo, utm->rx_buf, max_transfer, 0, |
| 994 | 0); |
| 995 | |
| 996 | if (n_read > 0) |
| 997 | max_dequeue -= n_read; |
| 998 | |
| 999 | /* Reflect if a non-drop session */ |
| 1000 | if (utm->have_return && n_read > 0) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1001 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1002 | offset = 0; |
| 1003 | do |
| 1004 | { |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1005 | rv = app_send (session, &utm->rx_buf[offset], n_read, |
| 1006 | SVM_Q_WAIT); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1007 | if (rv > 0) |
| 1008 | { |
| 1009 | n_read -= rv; |
| 1010 | offset += rv; |
| 1011 | } |
| 1012 | } |
| 1013 | while ((rv <= 0 || n_read > 0) && !utm->time_to_stop); |
| 1014 | |
| 1015 | /* If event wasn't set, add one */ |
| 1016 | if (svm_fifo_set_event (tx_fifo)) |
Florin Coras | c0737e9 | 2019-03-04 14:19:39 -0800 | [diff] [blame^] | 1017 | app_send_io_evt_to_vpp (session->vpp_evt_q, |
| 1018 | tx_fifo->master_session_index, |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1019 | SESSION_IO_EVT_TX, SVM_Q_WAIT); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1020 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1021 | } |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1022 | while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1025 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1026 | server_handle_event_queue (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1027 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1028 | session_event_t *e; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1029 | svm_msg_q_msg_t msg; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1030 | svm_msg_q_t *mq = utm->our_event_queue; |
| 1031 | int i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1032 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1033 | while (utm->state != STATE_READY) |
| 1034 | sleep (5); |
| 1035 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1036 | while (1) |
| 1037 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1038 | if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0)) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1039 | { |
| 1040 | clib_warning ("svm msg q returned"); |
| 1041 | continue; |
| 1042 | } |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1043 | e = svm_msg_q_msg_data (mq, &msg); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1044 | switch (e->event_type) |
| 1045 | { |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1046 | case SESSION_IO_EVT_RX: |
Florin Coras | c0737e9 | 2019-03-04 14:19:39 -0800 | [diff] [blame^] | 1047 | server_handle_fifo_event_rx (utm, e->session_index); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1048 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1049 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1050 | default: |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1051 | handle_mq_event (e); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1052 | break; |
| 1053 | } |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1054 | svm_msg_q_free_msg (mq, &msg); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1055 | if (PREDICT_FALSE (utm->time_to_stop == 1)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1056 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1057 | if (PREDICT_FALSE (utm->time_to_print_stats == 1)) |
| 1058 | { |
| 1059 | utm->time_to_print_stats = 0; |
| 1060 | fformat (stdout, "%d connections\n", pool_elts (utm->sessions)); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1065 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1066 | server_unbind (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1067 | { |
| 1068 | vl_api_unbind_uri_t *ump; |
| 1069 | |
| 1070 | ump = vl_msg_api_alloc (sizeof (*ump)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1071 | clib_memset (ump, 0, sizeof (*ump)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1072 | |
| 1073 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI); |
| 1074 | ump->client_index = utm->my_client_index; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1075 | memcpy (ump->uri, utm->listen_uri, vec_len (utm->listen_uri)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1076 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump); |
| 1077 | } |
| 1078 | |
| 1079 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1080 | server_bind (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1081 | { |
| 1082 | vl_api_bind_uri_t *bmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1083 | |
| 1084 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1085 | clib_memset (bmp, 0, sizeof (*bmp)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1086 | |
| 1087 | bmp->_vl_msg_id = ntohs (VL_API_BIND_URI); |
| 1088 | bmp->client_index = utm->my_client_index; |
| 1089 | bmp->context = ntohl (0xfeedface); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1090 | memcpy (bmp->uri, utm->listen_uri, vec_len (utm->listen_uri)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1091 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1095 | udp_server_test (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1096 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1097 | u8 wait_for_state = utm->is_connected ? STATE_BOUND : STATE_READY; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1098 | application_send_attach (utm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1099 | |
| 1100 | /* Bind to uri */ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1101 | server_bind (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1102 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1103 | if (wait_for_state_change (utm, wait_for_state)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1104 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1105 | clib_warning ("timeout waiting for state change"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1106 | return; |
| 1107 | } |
| 1108 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1109 | server_handle_event_queue (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1110 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1111 | /* Cleanup */ |
| 1112 | server_unbind (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1113 | |
| 1114 | if (wait_for_state_change (utm, STATE_START)) |
| 1115 | { |
| 1116 | clib_warning ("timeout waiting for STATE_START"); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1120 | application_detach (utm); |
| 1121 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1122 | fformat (stdout, "Test complete...\n"); |
| 1123 | } |
| 1124 | |
| 1125 | int |
| 1126 | main (int argc, char **argv) |
| 1127 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1128 | udp_echo_main_t *utm = &udp_echo_main; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 1129 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1130 | u8 *uri = (u8 *) "udp://6.0.1.1/1234"; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1131 | unformat_input_t _argv, *a = &_argv; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1132 | int i_am_server = 1; |
| 1133 | app_session_t *session; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1134 | u8 *chroot_prefix; |
| 1135 | char *app_name; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1136 | u32 tmp; |
| 1137 | int i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1138 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1139 | clib_mem_init_thread_safe (0, 256 << 20); |
| 1140 | |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 1141 | svm_fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1142 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1143 | vec_validate (utm->rx_buf, 128 << 10); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1144 | utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1145 | utm->my_pid = getpid (); |
| 1146 | utm->configured_segment_size = 1 << 20; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1147 | utm->have_return = 1; |
| 1148 | utm->bytes_to_send = 1024; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1149 | utm->fifo_size = 128 << 10; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1150 | utm->cut_through_session_index = ~0; |
| 1151 | clib_time_init (&utm->clib_time); |
| 1152 | |
| 1153 | init_error_string_table (utm); |
| 1154 | unformat_init_command_line (a, argv); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1155 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1156 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 1157 | { |
| 1158 | if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 1159 | { |
| 1160 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 1161 | } |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1162 | else if (unformat (a, "uri %s", &uri)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1163 | ; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1164 | else if (unformat (a, "segment-size %dM", &tmp)) |
| 1165 | utm->configured_segment_size = tmp << 20; |
| 1166 | else if (unformat (a, "segment-size %dG", &tmp)) |
| 1167 | utm->configured_segment_size = tmp << 30; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1168 | else if (unformat (a, "server")) |
| 1169 | i_am_server = 1; |
| 1170 | else if (unformat (a, "client")) |
| 1171 | i_am_server = 0; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1172 | else if (unformat (a, "no-return")) |
| 1173 | utm->have_return = 0; |
| 1174 | else if (unformat (a, "mbytes %d", &tmp)) |
| 1175 | utm->bytes_to_send = (u64) tmp << 20; |
| 1176 | else if (unformat (a, "fifo-size %d", &tmp)) |
| 1177 | utm->fifo_size = tmp << 10; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1178 | else |
| 1179 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1180 | fformat (stderr, "%s: usage [server|client]\n"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1181 | exit (1); |
| 1182 | } |
| 1183 | } |
| 1184 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1185 | utm->i_am_server = i_am_server; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1186 | |
| 1187 | setup_signal_handlers (); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1188 | tcp_echo_api_hookup (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1189 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1190 | if (i_am_server) |
| 1191 | { |
| 1192 | utm->listen_uri = format (0, "%s%c", uri, 0); |
| 1193 | utm->is_connected = (utm->listen_uri[4] == 'c'); |
| 1194 | app_name = "udp_echo_server"; |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | app_name = "udp_echo_client"; |
| 1199 | utm->connect_uri = format (0, "%s%c", uri, 0); |
| 1200 | utm->is_connected = (utm->connect_uri[4] == 'c'); |
| 1201 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1202 | if (connect_to_vpp (app_name) < 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1203 | { |
| 1204 | svm_region_exit (); |
| 1205 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 1206 | exit (1); |
| 1207 | } |
| 1208 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1209 | if (i_am_server == 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1210 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1211 | client_test (utm); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1212 | goto done; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1215 | /* $$$$ hack preallocation */ |
| 1216 | for (i = 0; i < 200000; i++) |
| 1217 | { |
| 1218 | pool_get (utm->sessions, session); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1219 | clib_memset (session, 0, sizeof (*session)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1220 | } |
| 1221 | for (i = 0; i < 200000; i++) |
| 1222 | pool_put_index (utm->sessions, i); |
| 1223 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1224 | udp_server_test (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1225 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1226 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1227 | vl_client_disconnect_from_vlib (); |
| 1228 | exit (0); |
| 1229 | } |
| 1230 | |
| 1231 | #undef vl_api_version |
| 1232 | #define vl_api_version(n,v) static u32 vpe_api_version = v; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1233 | #include <vpp/api/vpe.api.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1234 | #undef vl_api_version |
| 1235 | |
| 1236 | void |
| 1237 | vl_client_add_api_signatures (vl_api_memclnt_create_t * mp) |
| 1238 | { |
| 1239 | /* |
| 1240 | * Send the main API signature in slot 0. This bit of code must |
| 1241 | * match the checks in ../vpe/api/api.c: vl_msg_api_version_check(). |
| 1242 | */ |
| 1243 | mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version); |
| 1244 | } |
| 1245 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1246 | u32 |
| 1247 | vl (void *p) |
| 1248 | { |
| 1249 | return vec_len (p); |
| 1250 | } |
| 1251 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1252 | /* |
| 1253 | * fd.io coding-style-patch-verification: ON |
| 1254 | * |
| 1255 | * Local Variables: |
| 1256 | * eval: (c-set-style "gnu") |
| 1257 | * End: |
| 1258 | */ |