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 | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 311 | bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 312 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 2; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 313 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = utm->fifo_size; |
| 314 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = utm->fifo_size; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 315 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20; |
| 316 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20; |
| 317 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 16768; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 318 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 319 | } |
| 320 | |
| 321 | void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 322 | application_detach (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 323 | { |
| 324 | vl_api_application_detach_t *bmp; |
| 325 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 326 | clib_memset (bmp, 0, sizeof (*bmp)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 327 | |
| 328 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 329 | bmp->client_index = utm->my_client_index; |
| 330 | bmp->context = ntohl (0xfeedface); |
| 331 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 332 | } |
| 333 | |
| 334 | static void |
| 335 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 336 | mp) |
| 337 | { |
Florin Coras | 84a30ef | 2018-01-24 10:49:23 -0800 | [diff] [blame] | 338 | svm_fifo_segment_create_args_t _a = { 0 }, *a = &_a; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 339 | udp_echo_main_t *utm = &udp_echo_main; |
| 340 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 341 | int rv; |
| 342 | |
| 343 | if (mp->retval) |
| 344 | { |
| 345 | clib_warning ("attach failed: %d", mp->retval); |
| 346 | utm->state = STATE_FAILED; |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (mp->segment_name_length == 0) |
| 351 | { |
| 352 | clib_warning ("segment_name_length zero"); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | a->segment_name = (char *) mp->segment_name; |
| 357 | a->segment_size = mp->segment_size; |
| 358 | |
| 359 | ASSERT (mp->app_event_queue_address); |
| 360 | |
| 361 | /* Attach to the segment vpp created */ |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 362 | rv = svm_fifo_segment_attach (sm, a); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 363 | if (rv) |
| 364 | { |
| 365 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 366 | mp->segment_name); |
| 367 | return; |
| 368 | } |
| 369 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 370 | utm->our_event_queue = uword_to_pointer (mp->app_event_queue_address, |
| 371 | svm_msg_q_t *); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 372 | utm->state = STATE_ATTACHED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void |
| 376 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 377 | mp) |
| 378 | { |
| 379 | if (mp->retval) |
| 380 | clib_warning ("detach returned with err: %d", mp->retval); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 381 | udp_echo_main.state = STATE_DETACHED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 384 | u8 * |
| 385 | format_api_error (u8 * s, va_list * args) |
| 386 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 387 | udp_echo_main_t *utm = va_arg (*args, udp_echo_main_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 388 | i32 error = va_arg (*args, u32); |
| 389 | uword *p; |
| 390 | |
| 391 | p = hash_get (utm->error_string_by_error_number, -error); |
| 392 | |
| 393 | if (p) |
| 394 | s = format (s, "%s", p[0]); |
| 395 | else |
| 396 | s = format (s, "%d", error); |
| 397 | return s; |
| 398 | } |
| 399 | |
| 400 | int |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 401 | 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] | 402 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 403 | #if CLIB_DEBUG > 0 |
| 404 | #define TIMEOUT 600.0 |
| 405 | #else |
| 406 | #define TIMEOUT 600.0 |
| 407 | #endif |
| 408 | |
| 409 | f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 410 | |
| 411 | while (clib_time_now (&utm->clib_time) < timeout) |
| 412 | { |
| 413 | if (utm->state == state) |
| 414 | return 0; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 415 | if (utm->state == STATE_FAILED) |
| 416 | return -1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 417 | } |
| 418 | return -1; |
| 419 | } |
| 420 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 421 | u64 server_bytes_received, server_bytes_sent; |
| 422 | |
| 423 | static void * |
| 424 | cut_through_thread_fn (void *arg) |
| 425 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 426 | app_session_t *s; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 427 | svm_fifo_t *rx_fifo; |
| 428 | svm_fifo_t *tx_fifo; |
| 429 | u8 *my_copy_buffer = 0; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 430 | udp_echo_main_t *utm = &udp_echo_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 431 | i32 actual_transfer; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 432 | int rv, do_dequeue = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 433 | u32 buffer_offset; |
| 434 | |
| 435 | while (utm->cut_through_session_index == ~0) |
| 436 | ; |
| 437 | |
| 438 | s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index); |
| 439 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 440 | rx_fifo = s->rx_fifo; |
| 441 | tx_fifo = s->tx_fifo; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 442 | |
| 443 | vec_validate (my_copy_buffer, 64 * 1024 - 1); |
| 444 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 445 | while (1) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 446 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 447 | do |
| 448 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 449 | /* We read from the tx fifo and write to the rx fifo */ |
| 450 | if (utm->have_return || do_dequeue) |
| 451 | actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, |
| 452 | vec_len |
| 453 | (my_copy_buffer), |
| 454 | my_copy_buffer); |
| 455 | else |
| 456 | { |
| 457 | /* We don't do anything with the data, drop it */ |
| 458 | actual_transfer = svm_fifo_max_dequeue (rx_fifo); |
| 459 | svm_fifo_dequeue_drop (rx_fifo, actual_transfer); |
| 460 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 461 | } |
| 462 | while (actual_transfer <= 0); |
| 463 | |
| 464 | server_bytes_received += actual_transfer; |
| 465 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 466 | if (utm->have_return) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 467 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 468 | buffer_offset = 0; |
| 469 | while (actual_transfer > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 470 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 471 | rv = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer, |
| 472 | my_copy_buffer + buffer_offset); |
| 473 | if (rv > 0) |
| 474 | { |
| 475 | actual_transfer -= rv; |
| 476 | buffer_offset += rv; |
| 477 | server_bytes_sent += rv; |
| 478 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 479 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 480 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 481 | } |
| 482 | if (PREDICT_FALSE (utm->time_to_stop)) |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | pthread_exit (0); |
| 487 | } |
| 488 | |
| 489 | static void |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 490 | session_accepted_handler (session_accepted_msg_t * mp) |
| 491 | { |
| 492 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 493 | udp_echo_main_t *utm = &udp_echo_main; |
| 494 | session_accepted_reply_msg_t *rmp; |
| 495 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 496 | app_session_t *session; |
| 497 | static f64 start_time; |
| 498 | u32 session_index; |
| 499 | int rv = 0; |
| 500 | |
| 501 | if (start_time == 0.0) |
| 502 | start_time = clib_time_now (&utm->clib_time); |
| 503 | |
| 504 | utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 505 | svm_msg_q_t *); |
| 506 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 507 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 508 | |
| 509 | pool_get (utm->sessions, session); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 510 | clib_memset (session, 0, sizeof (*session)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 511 | session_index = session - utm->sessions; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 512 | session->session_index = session_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 513 | |
| 514 | /* Cut-through case */ |
| 515 | if (mp->server_event_queue_address) |
| 516 | { |
| 517 | clib_warning ("cut-through session"); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 518 | session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address, |
| 519 | svm_msg_q_t *); |
| 520 | sleep (1); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 521 | rx_fifo->master_session_index = session_index; |
| 522 | tx_fifo->master_session_index = session_index; |
| 523 | utm->cut_through_session_index = session_index; |
| 524 | session->rx_fifo = rx_fifo; |
| 525 | session->tx_fifo = tx_fifo; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 526 | session->is_dgram = 0; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 527 | |
| 528 | rv = pthread_create (&utm->cut_through_thread_handle, |
| 529 | NULL /*attr */ , cut_through_thread_fn, 0); |
| 530 | if (rv) |
| 531 | { |
| 532 | clib_warning ("pthread_create returned %d", rv); |
| 533 | rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 534 | } |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 535 | } |
| 536 | else |
| 537 | { |
| 538 | rx_fifo->client_session_index = session_index; |
| 539 | tx_fifo->client_session_index = session_index; |
| 540 | session->rx_fifo = rx_fifo; |
| 541 | session->tx_fifo = tx_fifo; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 542 | clib_memcpy_fast (&session->transport.rmt_ip, mp->ip, |
| 543 | sizeof (ip46_address_t)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 544 | session->transport.is_ip4 = mp->is_ip4; |
| 545 | session->transport.rmt_port = mp->port; |
| 546 | } |
| 547 | |
| 548 | hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index); |
| 549 | if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0) |
| 550 | { |
| 551 | f64 now = clib_time_now (&utm->clib_time); |
| 552 | fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n", |
| 553 | pool_elts (utm->sessions), now - start_time, |
| 554 | (f64) pool_elts (utm->sessions) / (now - start_time)); |
| 555 | } |
| 556 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 557 | app_alloc_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt, |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 558 | SESSION_CTRL_EVT_ACCEPTED_REPLY); |
| 559 | rmp = (session_accepted_reply_msg_t *) app_evt->evt->data; |
| 560 | rmp->handle = mp->handle; |
| 561 | rmp->context = mp->context; |
| 562 | rmp->retval = rv; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 563 | app_send_ctrl_evt_to_vpp (utm->vpp_event_queue, app_evt); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 564 | |
| 565 | CLIB_MEMORY_BARRIER (); |
| 566 | utm->state = STATE_READY; |
| 567 | } |
| 568 | |
| 569 | static void |
| 570 | session_disconnected_handler (session_disconnected_msg_t * mp) |
| 571 | { |
| 572 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 573 | udp_echo_main_t *utm = &udp_echo_main; |
| 574 | session_disconnected_reply_msg_t *rmp; |
| 575 | app_session_t *session; |
| 576 | uword *p; |
| 577 | int rv = 0; |
| 578 | |
| 579 | p = hash_get (utm->session_index_by_vpp_handles, mp->handle); |
| 580 | |
| 581 | if (p) |
| 582 | { |
| 583 | session = pool_elt_at_index (utm->sessions, p[0]); |
| 584 | hash_unset (utm->session_index_by_vpp_handles, mp->handle); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 585 | clib_warning ("disconnecting %u", session->session_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 586 | pool_put (utm->sessions, session); |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | clib_warning ("couldn't find session key %llx", mp->handle); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt, |
| 595 | SESSION_CTRL_EVT_DISCONNECTED_REPLY); |
| 596 | rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data; |
| 597 | rmp->retval = rv; |
| 598 | rmp->handle = mp->handle; |
| 599 | rmp->context = mp->context; |
| 600 | app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt); |
| 601 | } |
| 602 | |
| 603 | static void |
| 604 | session_connected_handler (session_connected_msg_t * mp) |
| 605 | { |
| 606 | udp_echo_main_t *utm = &udp_echo_main; |
| 607 | unformat_input_t _input, *input = &_input; |
Florin Coras | 5665ced | 2018-10-25 18:03:45 -0700 | [diff] [blame] | 608 | session_endpoint_cfg_t _sep, *sep = &_sep; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 609 | app_session_t *session; |
| 610 | |
| 611 | ASSERT (utm->i_am_server == 0); |
| 612 | |
| 613 | if (mp->retval) |
| 614 | { |
| 615 | clib_warning ("failed connect"); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | ASSERT (mp->server_rx_fifo && mp->server_tx_fifo); |
| 620 | |
| 621 | pool_get (utm->sessions, session); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 622 | session->session_index = session - utm->sessions; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 623 | session->rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 624 | 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] | 625 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 626 | /* Cut-through case */ |
| 627 | if (mp->client_event_queue_address) |
| 628 | { |
| 629 | clib_warning ("cut-through session"); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 630 | session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address, |
| 631 | svm_msg_q_t *); |
| 632 | utm->ct_event_queue = uword_to_pointer (mp->client_event_queue_address, |
| 633 | svm_msg_q_t *); |
| 634 | utm->cut_through_session_index = session->session_index; |
| 635 | session->is_dgram = 0; |
| 636 | sleep (1); |
| 637 | session->rx_fifo->client_session_index = session->session_index; |
| 638 | session->tx_fifo->client_session_index = session->session_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 639 | } |
| 640 | else |
| 641 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 642 | utm->connected_session = session->session_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 643 | utm->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 644 | svm_msg_q_t *); |
| 645 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 646 | session->rx_fifo->client_session_index = session->session_index; |
| 647 | session->tx_fifo->client_session_index = session->session_index; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 648 | clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip, |
| 649 | sizeof (ip46_address_t)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 650 | session->transport.is_ip4 = mp->is_ip4; |
| 651 | session->transport.lcl_port = mp->lcl_port; |
| 652 | |
| 653 | unformat_init_vector (input, utm->connect_uri); |
| 654 | if (!unformat (input, "%U", unformat_uri, sep)) |
| 655 | { |
| 656 | clib_warning ("can't figure out remote ip and port"); |
| 657 | utm->state = STATE_FAILED; |
| 658 | unformat_free (input); |
| 659 | return; |
| 660 | } |
| 661 | unformat_free (input); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 662 | clib_memcpy_fast (&session->transport.rmt_ip, &sep->ip, |
| 663 | sizeof (ip46_address_t)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 664 | session->transport.rmt_port = sep->port; |
| 665 | session->is_dgram = !utm->is_connected; |
| 666 | } |
| 667 | utm->state = STATE_READY; |
| 668 | } |
| 669 | |
| 670 | static void |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 671 | session_bound_handler (session_bound_msg_t * mp) |
| 672 | { |
| 673 | udp_echo_main_t *utm = &udp_echo_main; |
| 674 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 675 | app_session_t *session; |
| 676 | u32 session_index; |
| 677 | |
| 678 | if (mp->retval) |
| 679 | { |
| 680 | clib_warning ("bind failed: %d", mp->retval); |
| 681 | utm->state = STATE_FAILED; |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *); |
| 686 | tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *); |
| 687 | |
| 688 | pool_get (utm->sessions, session); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 689 | clib_memset (session, 0, sizeof (*session)); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 690 | session_index = session - utm->sessions; |
| 691 | |
| 692 | rx_fifo->client_session_index = session_index; |
| 693 | tx_fifo->client_session_index = session_index; |
| 694 | session->rx_fifo = rx_fifo; |
| 695 | session->tx_fifo = tx_fifo; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 696 | clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip, |
| 697 | sizeof (ip46_address_t)); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 698 | session->transport.is_ip4 = mp->lcl_is_ip4; |
| 699 | session->transport.lcl_port = mp->lcl_port; |
| 700 | session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *); |
| 701 | |
| 702 | utm->state = utm->is_connected ? STATE_BOUND : STATE_READY; |
| 703 | } |
| 704 | |
| 705 | static void |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 706 | handle_mq_event (session_event_t * e) |
| 707 | { |
| 708 | switch (e->event_type) |
| 709 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 710 | case SESSION_CTRL_EVT_BOUND: |
| 711 | session_bound_handler ((session_bound_msg_t *) e->data); |
| 712 | break; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 713 | case SESSION_CTRL_EVT_ACCEPTED: |
| 714 | session_accepted_handler ((session_accepted_msg_t *) e->data); |
| 715 | break; |
| 716 | case SESSION_CTRL_EVT_CONNECTED: |
| 717 | session_connected_handler ((session_connected_msg_t *) e->data); |
| 718 | break; |
| 719 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 720 | session_disconnected_handler ((session_disconnected_msg_t *) e->data); |
| 721 | break; |
| 722 | default: |
| 723 | clib_warning ("unhandled %u", e->event_type); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static void |
| 728 | udp_client_send_connect (udp_echo_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 729 | { |
| 730 | vl_api_connect_uri_t *cmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 731 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 732 | clib_memset (cmp, 0, sizeof (*cmp)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 733 | |
| 734 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
| 735 | cmp->client_index = utm->my_client_index; |
| 736 | cmp->context = ntohl (0xfeedface); |
| 737 | memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri)); |
| 738 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 739 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 740 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 741 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 742 | 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] | 743 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 744 | u64 test_buf_len, bytes_this_chunk, test_buf_offset; |
| 745 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 746 | u8 *test_data = utm->connect_test_data; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 747 | u32 bytes_to_snd, enq_space, min_chunk; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 748 | session_evt_type_t et = FIFO_EVENT_APP_TX; |
| 749 | int written; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 750 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 751 | test_buf_len = vec_len (test_data); |
| 752 | test_buf_offset = utm->bytes_sent % test_buf_len; |
| 753 | bytes_this_chunk = clib_min (test_buf_len - test_buf_offset, |
| 754 | utm->bytes_to_send); |
| 755 | enq_space = svm_fifo_max_enqueue (s->tx_fifo); |
| 756 | bytes_this_chunk = clib_min (bytes_this_chunk, enq_space); |
| 757 | et += (s->session_index == utm->cut_through_session_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 758 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 759 | if (s->is_dgram) |
| 760 | written = app_send_dgram_raw (s->tx_fifo, &s->transport, s->vpp_evt_q, |
| 761 | test_data + test_buf_offset, |
| 762 | bytes_this_chunk, et, SVM_Q_WAIT); |
| 763 | else |
| 764 | written = app_send_stream_raw (s->tx_fifo, s->vpp_evt_q, |
| 765 | test_data + test_buf_offset, |
| 766 | bytes_this_chunk, et, SVM_Q_WAIT); |
| 767 | |
| 768 | if (written > 0) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 769 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 770 | utm->bytes_to_send -= written; |
| 771 | utm->bytes_sent += written; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
| 775 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 776 | recv_test_chunk (udp_echo_main_t * utm, app_session_t * s) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 777 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 778 | app_recv (s, utm->rx_buf, vec_len (utm->rx_buf)); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | void |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 782 | client_send_data (udp_echo_main_t * utm, u32 session_index) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 783 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 784 | f64 start_time, end_time, delta; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 785 | app_session_t *session; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 786 | char *transfer_type; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 787 | u8 *test_data; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 788 | int i; |
| 789 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 790 | vec_validate_aligned (utm->connect_test_data, 1024 * 1024 - 1, |
| 791 | CLIB_CACHE_LINE_BYTES); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 792 | for (i = 0; i < vec_len (utm->connect_test_data); i++) |
| 793 | utm->connect_test_data[i] = i & 0xff; |
| 794 | |
| 795 | test_data = utm->connect_test_data; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 796 | session = pool_elt_at_index (utm->sessions, session_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 797 | ASSERT (vec_len (test_data) > 0); |
| 798 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 799 | utm->total_to_send = utm->bytes_to_send; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 800 | vec_validate (utm->rx_buf, vec_len (test_data) - 1); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 801 | start_time = clib_time_now (&utm->clib_time); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 802 | while (!utm->time_to_stop && utm->bytes_to_send) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 803 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 804 | send_test_chunk (utm, session, 0); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 805 | if (utm->have_return) |
| 806 | recv_test_chunk (utm, session); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 807 | if (utm->time_to_stop) |
| 808 | break; |
| 809 | } |
| 810 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 811 | if (utm->have_return) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 812 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 813 | f64 timeout = clib_time_now (&utm->clib_time) + 5; |
| 814 | while (clib_time_now (&utm->clib_time) < timeout) |
| 815 | recv_test_chunk (utm, session); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 818 | end_time = clib_time_now (&utm->clib_time); |
| 819 | delta = end_time - start_time; |
| 820 | transfer_type = utm->have_return ? "full-duplex" : "half-duplex"; |
| 821 | clib_warning ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 822 | utm->total_to_send, utm->total_to_send / (1ULL << 20), |
| 823 | utm->total_to_send / (1ULL << 30), delta); |
| 824 | clib_warning ("%.2f bytes/second %s", ((f64) utm->total_to_send) / (delta), |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 825 | transfer_type); |
| 826 | clib_warning ("%.4f gbit/second %s", |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 827 | (((f64) utm->total_to_send * 8.0) / delta / 1e9), |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 828 | transfer_type); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 831 | static int |
| 832 | application_attach (udp_echo_main_t * utm) |
| 833 | { |
| 834 | application_send_attach (utm); |
| 835 | if (wait_for_state_change (utm, STATE_ATTACHED)) |
| 836 | { |
| 837 | clib_warning ("timeout waiting for STATE_ATTACHED"); |
| 838 | return -1; |
| 839 | } |
| 840 | return 0; |
| 841 | } |
| 842 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 843 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 844 | client_test (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 845 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 846 | f64 start_time, timeout = 100.0; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 847 | app_session_t *session; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 848 | svm_msg_q_msg_t msg; |
| 849 | session_event_t *e; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 850 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 851 | if (application_attach (utm)) |
| 852 | return; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 853 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 854 | udp_client_send_connect (utm); |
| 855 | |
| 856 | start_time = clib_time_now (&utm->clib_time); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 857 | while (pool_elts (utm->sessions) != 1 && utm->state != STATE_FAILED) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 858 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 859 | svm_msg_q_sub (utm->our_event_queue, &msg, SVM_Q_WAIT, 0); |
| 860 | e = svm_msg_q_msg_data (utm->our_event_queue, &msg); |
| 861 | handle_mq_event (e); |
| 862 | svm_msg_q_free_msg (utm->our_event_queue, &msg); |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 863 | |
| 864 | if (clib_time_now (&utm->clib_time) - start_time >= timeout) |
| 865 | break; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 868 | if (utm->cut_through_session_index != ~0) |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 869 | client_send_data (utm, utm->cut_through_session_index); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 870 | else |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 871 | client_send_data (utm, utm->connected_session); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 872 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 873 | application_detach (utm); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 874 | wait_for_state_change (utm, STATE_DETACHED); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 878 | vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp) |
| 879 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 880 | udp_echo_main_t *utm = &udp_echo_main; |
| 881 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 882 | app_session_t *session; |
| 883 | u32 session_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 884 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 885 | if (mp->retval) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 886 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 887 | clib_warning ("bind failed: %d", mp->retval); |
| 888 | utm->state = STATE_FAILED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 892 | rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *); |
| 893 | tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *); |
| 894 | |
| 895 | pool_get (utm->sessions, session); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 896 | clib_memset (session, 0, sizeof (*session)); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 897 | session_index = session - utm->sessions; |
| 898 | |
| 899 | rx_fifo->client_session_index = session_index; |
| 900 | tx_fifo->client_session_index = session_index; |
| 901 | session->rx_fifo = rx_fifo; |
| 902 | session->tx_fifo = tx_fifo; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 903 | clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip, |
| 904 | sizeof (ip46_address_t)); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 905 | session->transport.is_ip4 = mp->lcl_is_ip4; |
| 906 | session->transport.lcl_port = mp->lcl_port; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 907 | session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 908 | |
| 909 | utm->state = utm->is_connected ? STATE_BOUND : STATE_READY; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 913 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 914 | { |
| 915 | svm_fifo_segment_create_args_t _a, *a = &_a; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 916 | udp_echo_main_t *utm = &udp_echo_main; |
| 917 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 918 | svm_fifo_segment_private_t *seg; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 919 | int rv; |
| 920 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 921 | clib_memset (a, 0, sizeof (*a)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 922 | a->segment_name = (char *) mp->segment_name; |
| 923 | a->segment_size = mp->segment_size; |
| 924 | /* Attach to the segment vpp created */ |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 925 | rv = svm_fifo_segment_attach (sm, a); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 926 | if (rv) |
| 927 | { |
| 928 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 929 | mp->segment_name); |
| 930 | return; |
| 931 | } |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 932 | seg = svm_fifo_segment_get_segment (sm, a->new_segment_indices[0]); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 933 | clib_warning ("Mapped new segment '%s' size %d", seg->ssvm.name, |
| 934 | seg->ssvm.ssvm_size); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 935 | hash_set (utm->segments_table, clib_net_to_host_u64 (mp->segment_handle), |
| 936 | a->new_segment_indices[0]); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | static void |
| 940 | vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp) |
| 941 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 942 | udp_echo_main_t *utm = &udp_echo_main; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 943 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 944 | svm_fifo_segment_private_t *seg; |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 945 | uword *seg_indexp; |
| 946 | u64 segment_handle; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 947 | |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 948 | segment_handle = clib_net_to_host_u64 (mp->segment_handle); |
| 949 | seg_indexp = hash_get (utm->segments_table, segment_handle); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 950 | if (!seg_indexp) |
| 951 | { |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 952 | clib_warning ("segment not mapped: %s", segment_handle); |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 953 | return; |
| 954 | } |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 955 | hash_unset (utm->segments_table, segment_handle); |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 956 | seg = svm_fifo_segment_get_segment (sm, (u32) seg_indexp[0]); |
| 957 | svm_fifo_segment_delete (sm, seg); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 958 | clib_warning ("Unmapped segment '%s'", segment_handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 961 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 962 | vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp) |
| 963 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 964 | udp_echo_main_t *utm = &udp_echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 965 | |
| 966 | if (mp->retval != 0) |
| 967 | clib_warning ("returned %d", ntohl (mp->retval)); |
| 968 | |
| 969 | utm->state = STATE_START; |
| 970 | } |
| 971 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 972 | static void |
| 973 | vl_api_app_cut_through_registration_add_t_handler |
| 974 | (vl_api_app_cut_through_registration_add_t * mp) |
| 975 | { |
| 976 | |
| 977 | } |
| 978 | |
| 979 | #define foreach_tcp_echo_msg \ |
| 980 | _(BIND_URI_REPLY, bind_uri_reply) \ |
| 981 | _(UNBIND_URI_REPLY, unbind_uri_reply) \ |
| 982 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
| 983 | _(UNMAP_SEGMENT, unmap_segment) \ |
| 984 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 985 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 986 | _(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 987 | |
| 988 | void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 989 | tcp_echo_api_hookup (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 990 | { |
| 991 | #define _(N,n) \ |
| 992 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 993 | vl_api_##n##_t_handler, \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 994 | vl_noop_handler, \ |
| 995 | vl_api_##n##_t_endian, \ |
| 996 | vl_api_##n##_t_print, \ |
| 997 | sizeof(vl_api_##n##_t), 1); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 998 | foreach_tcp_echo_msg; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 999 | #undef _ |
| 1000 | |
| 1001 | } |
| 1002 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1003 | int |
| 1004 | connect_to_vpp (char *name) |
| 1005 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1006 | udp_echo_main_t *utm = &udp_echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1007 | api_main_t *am = &api_main; |
| 1008 | |
| 1009 | if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0) |
| 1010 | return -1; |
| 1011 | |
| 1012 | utm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 1013 | utm->my_client_index = am->my_client_index; |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | void |
| 1019 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 1020 | { |
| 1021 | clib_warning ("BUG"); |
| 1022 | } |
| 1023 | |
| 1024 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1025 | init_error_string_table (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1026 | { |
| 1027 | utm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 1028 | |
| 1029 | #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s); |
| 1030 | foreach_vnet_api_error; |
| 1031 | #undef _ |
| 1032 | |
| 1033 | hash_set (utm->error_string_by_error_number, 99, "Misc"); |
| 1034 | } |
| 1035 | |
| 1036 | void |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1037 | 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] | 1038 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1039 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 1040 | int n_read; |
| 1041 | app_session_t *session; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1042 | int rv; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1043 | u32 max_dequeue, offset, max_transfer, rx_buf_len; |
| 1044 | session_evt_type_t et = FIFO_EVENT_APP_TX; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1045 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1046 | session = pool_elt_at_index (utm->sessions, session_index); |
| 1047 | rx_buf_len = vec_len (utm->rx_buf); |
| 1048 | rx_fifo = session->rx_fifo; |
| 1049 | tx_fifo = session->tx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1050 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1051 | et += (session->session_index == utm->cut_through_session_index); |
| 1052 | |
| 1053 | max_dequeue = svm_fifo_max_dequeue (rx_fifo); |
| 1054 | /* Allow enqueuing of a new event */ |
| 1055 | svm_fifo_unset_event (rx_fifo); |
| 1056 | |
| 1057 | if (PREDICT_FALSE (!max_dequeue)) |
| 1058 | return; |
| 1059 | |
| 1060 | /* Read the max_dequeue */ |
| 1061 | do |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1062 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1063 | max_transfer = clib_min (rx_buf_len, max_dequeue); |
| 1064 | if (session->is_dgram) |
| 1065 | n_read = app_recv_dgram_raw (rx_fifo, utm->rx_buf, max_transfer, |
| 1066 | &session->transport, 0, 0); |
| 1067 | else |
| 1068 | n_read = app_recv_stream_raw (rx_fifo, utm->rx_buf, max_transfer, 0, |
| 1069 | 0); |
| 1070 | |
| 1071 | if (n_read > 0) |
| 1072 | max_dequeue -= n_read; |
| 1073 | |
| 1074 | /* Reflect if a non-drop session */ |
| 1075 | if (utm->have_return && n_read > 0) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1076 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1077 | offset = 0; |
| 1078 | do |
| 1079 | { |
| 1080 | if (session->is_dgram) |
| 1081 | rv = app_send_dgram_raw (tx_fifo, &session->transport, |
| 1082 | session->vpp_evt_q, |
| 1083 | &utm->rx_buf[offset], n_read, et, |
| 1084 | SVM_Q_WAIT); |
| 1085 | else |
| 1086 | rv = app_send_stream_raw (tx_fifo, session->vpp_evt_q, |
| 1087 | &utm->rx_buf[offset], n_read, et, |
| 1088 | SVM_Q_WAIT); |
| 1089 | if (rv > 0) |
| 1090 | { |
| 1091 | n_read -= rv; |
| 1092 | offset += rv; |
| 1093 | } |
| 1094 | } |
| 1095 | while ((rv <= 0 || n_read > 0) && !utm->time_to_stop); |
| 1096 | |
| 1097 | /* If event wasn't set, add one */ |
| 1098 | if (svm_fifo_set_event (tx_fifo)) |
| 1099 | app_send_io_evt_to_vpp (session->vpp_evt_q, tx_fifo, |
| 1100 | et, SVM_Q_WAIT); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1101 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1102 | } |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1103 | while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1104 | } |
| 1105 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1106 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1107 | server_handle_event_queue (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1108 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1109 | session_event_t *e; |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1110 | svm_msg_q_msg_t msg; |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1111 | svm_msg_q_t *mq = utm->our_event_queue; |
| 1112 | int i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1113 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1114 | while (utm->state != STATE_READY) |
| 1115 | sleep (5); |
| 1116 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1117 | while (1) |
| 1118 | { |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1119 | if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0)) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 1120 | { |
| 1121 | clib_warning ("svm msg q returned"); |
| 1122 | continue; |
| 1123 | } |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1124 | e = svm_msg_q_msg_data (mq, &msg); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1125 | switch (e->event_type) |
| 1126 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1127 | case FIFO_EVENT_APP_RX: |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1128 | server_handle_fifo_event_rx (utm, e->fifo->client_session_index); |
| 1129 | break; |
| 1130 | case SESSION_IO_EVT_CT_TX: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1131 | break; |
| 1132 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1133 | default: |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 1134 | handle_mq_event (e); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1135 | break; |
| 1136 | } |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1137 | svm_msg_q_free_msg (mq, &msg); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1138 | if (PREDICT_FALSE (utm->time_to_stop == 1)) |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1139 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1140 | if (PREDICT_FALSE (utm->time_to_print_stats == 1)) |
| 1141 | { |
| 1142 | utm->time_to_print_stats = 0; |
| 1143 | fformat (stdout, "%d connections\n", pool_elts (utm->sessions)); |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1148 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1149 | server_unbind (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1150 | { |
| 1151 | vl_api_unbind_uri_t *ump; |
| 1152 | |
| 1153 | ump = vl_msg_api_alloc (sizeof (*ump)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1154 | clib_memset (ump, 0, sizeof (*ump)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1155 | |
| 1156 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI); |
| 1157 | ump->client_index = utm->my_client_index; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1158 | memcpy (ump->uri, utm->listen_uri, vec_len (utm->listen_uri)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1159 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump); |
| 1160 | } |
| 1161 | |
| 1162 | static void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1163 | server_bind (udp_echo_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1164 | { |
| 1165 | vl_api_bind_uri_t *bmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1166 | |
| 1167 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1168 | clib_memset (bmp, 0, sizeof (*bmp)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1169 | |
| 1170 | bmp->_vl_msg_id = ntohs (VL_API_BIND_URI); |
| 1171 | bmp->client_index = utm->my_client_index; |
| 1172 | bmp->context = ntohl (0xfeedface); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1173 | memcpy (bmp->uri, utm->listen_uri, vec_len (utm->listen_uri)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1174 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | void |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1178 | udp_server_test (udp_echo_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1179 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1180 | u8 wait_for_state = utm->is_connected ? STATE_BOUND : STATE_READY; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1181 | application_send_attach (utm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1182 | |
| 1183 | /* Bind to uri */ |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1184 | server_bind (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1185 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1186 | if (wait_for_state_change (utm, wait_for_state)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1187 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1188 | clib_warning ("timeout waiting for state change"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1189 | return; |
| 1190 | } |
| 1191 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1192 | server_handle_event_queue (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1193 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1194 | /* Cleanup */ |
| 1195 | server_unbind (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1196 | |
| 1197 | if (wait_for_state_change (utm, STATE_START)) |
| 1198 | { |
| 1199 | clib_warning ("timeout waiting for STATE_START"); |
| 1200 | return; |
| 1201 | } |
| 1202 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1203 | application_detach (utm); |
| 1204 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1205 | fformat (stdout, "Test complete...\n"); |
| 1206 | } |
| 1207 | |
| 1208 | int |
| 1209 | main (int argc, char **argv) |
| 1210 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1211 | udp_echo_main_t *utm = &udp_echo_main; |
Florin Coras | adc74d7 | 2018-12-02 13:36:00 -0800 | [diff] [blame] | 1212 | svm_fifo_segment_main_t *sm = &utm->segment_main; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1213 | u8 *uri = (u8 *) "udp://6.0.1.1/1234"; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1214 | unformat_input_t _argv, *a = &_argv; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1215 | int i_am_server = 1; |
| 1216 | app_session_t *session; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1217 | u8 *chroot_prefix; |
| 1218 | char *app_name; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1219 | u32 tmp; |
| 1220 | int i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1221 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1222 | clib_mem_init_thread_safe (0, 256 << 20); |
| 1223 | |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 1224 | svm_fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1225 | |
Florin Coras | a44d6b1 | 2018-10-03 14:29:10 -0700 | [diff] [blame] | 1226 | vec_validate (utm->rx_buf, 128 << 10); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1227 | utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1228 | utm->my_pid = getpid (); |
| 1229 | utm->configured_segment_size = 1 << 20; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1230 | utm->have_return = 1; |
| 1231 | utm->bytes_to_send = 1024; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1232 | utm->fifo_size = 128 << 10; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1233 | utm->cut_through_session_index = ~0; |
| 1234 | clib_time_init (&utm->clib_time); |
| 1235 | |
| 1236 | init_error_string_table (utm); |
| 1237 | unformat_init_command_line (a, argv); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1238 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1239 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 1240 | { |
| 1241 | if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 1242 | { |
| 1243 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 1244 | } |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1245 | else if (unformat (a, "uri %s", &uri)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1246 | ; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1247 | else if (unformat (a, "segment-size %dM", &tmp)) |
| 1248 | utm->configured_segment_size = tmp << 20; |
| 1249 | else if (unformat (a, "segment-size %dG", &tmp)) |
| 1250 | utm->configured_segment_size = tmp << 30; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1251 | else if (unformat (a, "server")) |
| 1252 | i_am_server = 1; |
| 1253 | else if (unformat (a, "client")) |
| 1254 | i_am_server = 0; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1255 | else if (unformat (a, "no-return")) |
| 1256 | utm->have_return = 0; |
| 1257 | else if (unformat (a, "mbytes %d", &tmp)) |
| 1258 | utm->bytes_to_send = (u64) tmp << 20; |
| 1259 | else if (unformat (a, "fifo-size %d", &tmp)) |
| 1260 | utm->fifo_size = tmp << 10; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1261 | else |
| 1262 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1263 | fformat (stderr, "%s: usage [server|client]\n"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1264 | exit (1); |
| 1265 | } |
| 1266 | } |
| 1267 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1268 | utm->i_am_server = i_am_server; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1269 | |
| 1270 | setup_signal_handlers (); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1271 | tcp_echo_api_hookup (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1272 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1273 | if (i_am_server) |
| 1274 | { |
| 1275 | utm->listen_uri = format (0, "%s%c", uri, 0); |
| 1276 | utm->is_connected = (utm->listen_uri[4] == 'c'); |
| 1277 | app_name = "udp_echo_server"; |
| 1278 | } |
| 1279 | else |
| 1280 | { |
| 1281 | app_name = "udp_echo_client"; |
| 1282 | utm->connect_uri = format (0, "%s%c", uri, 0); |
| 1283 | utm->is_connected = (utm->connect_uri[4] == 'c'); |
| 1284 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1285 | if (connect_to_vpp (app_name) < 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1286 | { |
| 1287 | svm_region_exit (); |
| 1288 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 1289 | exit (1); |
| 1290 | } |
| 1291 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 1292 | if (i_am_server == 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1293 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1294 | client_test (utm); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1295 | goto done; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1298 | /* $$$$ hack preallocation */ |
| 1299 | for (i = 0; i < 200000; i++) |
| 1300 | { |
| 1301 | pool_get (utm->sessions, session); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1302 | clib_memset (session, 0, sizeof (*session)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1303 | } |
| 1304 | for (i = 0; i < 200000; i++) |
| 1305 | pool_put_index (utm->sessions, i); |
| 1306 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1307 | udp_server_test (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1308 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 1309 | done: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1310 | vl_client_disconnect_from_vlib (); |
| 1311 | exit (0); |
| 1312 | } |
| 1313 | |
| 1314 | #undef vl_api_version |
| 1315 | #define vl_api_version(n,v) static u32 vpe_api_version = v; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1316 | #include <vpp/api/vpe.api.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1317 | #undef vl_api_version |
| 1318 | |
| 1319 | void |
| 1320 | vl_client_add_api_signatures (vl_api_memclnt_create_t * mp) |
| 1321 | { |
| 1322 | /* |
| 1323 | * Send the main API signature in slot 0. This bit of code must |
| 1324 | * match the checks in ../vpe/api/api.c: vl_msg_api_version_check(). |
| 1325 | */ |
| 1326 | mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version); |
| 1327 | } |
| 1328 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1329 | u32 |
| 1330 | vl (void *p) |
| 1331 | { |
| 1332 | return vec_len (p); |
| 1333 | } |
| 1334 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1335 | /* |
| 1336 | * fd.io coding-style-patch-verification: ON |
| 1337 | * |
| 1338 | * Local Variables: |
| 1339 | * eval: (c-set-style "gnu") |
| 1340 | * End: |
| 1341 | */ |