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 <signal.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 18 | #include <svm/svm_fifo_segment.h> |
| 19 | #include <vlibmemory/api.h> |
| 20 | #include <vpp/api/vpe_msg_enum.h> |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 21 | #include <vnet/session/application_interface.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 22 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 23 | #define vl_typedefs /* define message structures */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 24 | #include <vpp/api/vpe_all_api_h.h> |
| 25 | #undef vl_typedefs |
| 26 | |
| 27 | /* declare message handlers for each api */ |
| 28 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 29 | #define vl_endianfun /* define message structures */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 30 | #include <vpp/api/vpe_all_api_h.h> |
| 31 | #undef vl_endianfun |
| 32 | |
| 33 | /* instantiate all the print functions we know about */ |
| 34 | #define vl_print(handle, ...) |
| 35 | #define vl_printfun |
| 36 | #include <vpp/api/vpe_all_api_h.h> |
| 37 | #undef vl_printfun |
| 38 | |
| 39 | /* Satisfy external references when not linking with -lvlib */ |
| 40 | vlib_main_t vlib_global_main; |
| 41 | vlib_main_t **vlib_mains; |
| 42 | |
| 43 | typedef struct |
| 44 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 45 | svm_fifo_t *server_rx_fifo; |
| 46 | svm_fifo_t *server_tx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 47 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 48 | u64 vpp_session_handle; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 49 | } session_t; |
| 50 | |
| 51 | typedef enum |
| 52 | { |
| 53 | STATE_START, |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 54 | STATE_ATTACHED, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 55 | STATE_READY, |
| 56 | STATE_DISCONNECTING, |
| 57 | STATE_FAILED |
| 58 | } connection_state_t; |
| 59 | |
| 60 | typedef struct |
| 61 | { |
| 62 | /* vpe input queue */ |
| 63 | unix_shared_memory_queue_t *vl_input_queue; |
| 64 | |
| 65 | /* API client handle */ |
| 66 | u32 my_client_index; |
| 67 | |
| 68 | /* The URI we're playing with */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 69 | u8 *uri; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 70 | |
| 71 | /* Session pool */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 72 | session_t *sessions; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 73 | |
| 74 | /* Hash table for disconnect processing */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 75 | uword *session_index_by_vpp_handles; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 76 | |
| 77 | /* intermediate rx buffer */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 78 | u8 *rx_buf; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 79 | |
| 80 | /* URI for slave's connect */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 81 | u8 *connect_uri; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 82 | |
| 83 | u32 connected_session_index; |
| 84 | |
| 85 | int i_am_master; |
| 86 | |
| 87 | /* drop all packets */ |
| 88 | int drop_packets; |
| 89 | |
| 90 | /* Our event queue */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 91 | unix_shared_memory_queue_t *our_event_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 92 | |
| 93 | /* $$$ single thread only for the moment */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 94 | unix_shared_memory_queue_t *vpp_event_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 95 | |
| 96 | pid_t my_pid; |
| 97 | |
| 98 | /* For deadman timers */ |
| 99 | clib_time_t clib_time; |
| 100 | |
| 101 | /* State of the connection, shared between msg RX thread and main thread */ |
| 102 | volatile connection_state_t state; |
| 103 | |
| 104 | /* Signal variables */ |
| 105 | volatile int time_to_stop; |
| 106 | volatile int time_to_print_stats; |
| 107 | |
| 108 | u32 configured_segment_size; |
| 109 | |
| 110 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 111 | uword *error_string_by_error_number; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 112 | |
| 113 | u8 *connect_test_data; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 114 | pthread_t client_rx_thread_handle; |
| 115 | u32 client_bytes_received; |
| 116 | u8 test_return_packets; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 117 | u64 bytes_to_send; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 118 | |
| 119 | /* convenience */ |
| 120 | svm_fifo_segment_main_t *segment_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 121 | } uri_tcp_test_main_t; |
| 122 | |
| 123 | uri_tcp_test_main_t uri_tcp_test_main; |
| 124 | |
| 125 | #if CLIB_DEBUG > 0 |
| 126 | #define NITER 10000 |
| 127 | #else |
| 128 | #define NITER 4000000 |
| 129 | #endif |
| 130 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 131 | static u8 * |
| 132 | format_api_error (u8 * s, va_list * args) |
| 133 | { |
| 134 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 135 | i32 error = va_arg (*args, u32); |
| 136 | uword *p; |
| 137 | |
| 138 | p = hash_get (utm->error_string_by_error_number, -error); |
| 139 | |
| 140 | if (p) |
| 141 | s = format (s, "%s", p[0]); |
| 142 | else |
| 143 | s = format (s, "%d", error); |
| 144 | return s; |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | init_error_string_table (uri_tcp_test_main_t * utm) |
| 149 | { |
| 150 | utm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 151 | |
| 152 | #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s); |
| 153 | foreach_vnet_api_error; |
| 154 | #undef _ |
| 155 | |
| 156 | hash_set (utm->error_string_by_error_number, 99, "Misc"); |
| 157 | } |
| 158 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 159 | int |
| 160 | wait_for_state_change (uri_tcp_test_main_t * utm, connection_state_t state) |
| 161 | { |
| 162 | #if CLIB_DEBUG > 0 |
| 163 | #define TIMEOUT 600.0 |
| 164 | #else |
| 165 | #define TIMEOUT 600.0 |
| 166 | #endif |
| 167 | |
| 168 | f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT; |
| 169 | |
| 170 | while (clib_time_now (&utm->clib_time) < timeout) |
| 171 | { |
| 172 | if (utm->state == state) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 173 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 174 | if (utm->state == STATE_FAILED) |
| 175 | return -1; |
flyingeagle23 | a07779f | 2017-04-26 20:22:04 +0800 | [diff] [blame] | 176 | if (utm->time_to_stop == 1) |
| 177 | return -1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 178 | } |
| 179 | clib_warning ("timeout waiting for STATE_READY"); |
| 180 | return -1; |
| 181 | } |
| 182 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 183 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 184 | application_send_attach (uri_tcp_test_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 185 | { |
| 186 | vl_api_application_attach_t *bmp; |
| 187 | u32 fifo_size = 3 << 20; |
| 188 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 189 | memset (bmp, 0, sizeof (*bmp)); |
| 190 | |
| 191 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 192 | bmp->client_index = utm->my_client_index; |
| 193 | bmp->context = ntohl (0xfeedface); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 194 | bmp->options[APP_OPTIONS_FLAGS] = |
| 195 | APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 196 | bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size; |
| 197 | bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size; |
| 198 | bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20; |
| 199 | bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20; |
| 200 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 201 | } |
| 202 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 203 | int |
| 204 | application_attach (uri_tcp_test_main_t * utm) |
| 205 | { |
| 206 | application_send_attach (utm); |
| 207 | if (wait_for_state_change (utm, STATE_ATTACHED)) |
| 208 | { |
| 209 | clib_warning ("timeout waiting for STATE_ATTACHED"); |
| 210 | return -1; |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 215 | void |
| 216 | application_detach (uri_tcp_test_main_t * utm) |
| 217 | { |
| 218 | vl_api_application_detach_t *bmp; |
| 219 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 220 | memset (bmp, 0, sizeof (*bmp)); |
| 221 | |
| 222 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 223 | bmp->client_index = utm->my_client_index; |
| 224 | bmp->context = ntohl (0xfeedface); |
| 225 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 230 | mp) |
| 231 | { |
| 232 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 233 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 234 | int rv; |
| 235 | |
| 236 | if (mp->retval) |
| 237 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 238 | clib_warning ("attach failed: %U", format_api_error, |
| 239 | clib_net_to_host_u32 (mp->retval)); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 240 | utm->state = STATE_FAILED; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (mp->segment_name_length == 0) |
| 245 | { |
| 246 | clib_warning ("segment_name_length zero"); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | a->segment_name = (char *) mp->segment_name; |
| 251 | a->segment_size = mp->segment_size; |
| 252 | |
| 253 | ASSERT (mp->app_event_queue_address); |
| 254 | |
| 255 | /* Attach to the segment vpp created */ |
| 256 | rv = svm_fifo_segment_attach (a); |
| 257 | if (rv) |
| 258 | { |
| 259 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 260 | mp->segment_name); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | utm->our_event_queue = |
| 265 | (unix_shared_memory_queue_t *) mp->app_event_queue_address; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 266 | utm->state = STATE_ATTACHED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void |
| 270 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 271 | mp) |
| 272 | { |
| 273 | if (mp->retval) |
| 274 | clib_warning ("detach returned with err: %d", mp->retval); |
| 275 | } |
| 276 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 277 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 278 | stop_signal (int signum) |
| 279 | { |
| 280 | uri_tcp_test_main_t *um = &uri_tcp_test_main; |
| 281 | |
| 282 | um->time_to_stop = 1; |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | stats_signal (int signum) |
| 287 | { |
| 288 | uri_tcp_test_main_t *um = &uri_tcp_test_main; |
| 289 | |
| 290 | um->time_to_print_stats = 1; |
| 291 | } |
| 292 | |
| 293 | static clib_error_t * |
| 294 | setup_signal_handlers (void) |
| 295 | { |
| 296 | signal (SIGINT, stats_signal); |
| 297 | signal (SIGQUIT, stop_signal); |
| 298 | signal (SIGTERM, stop_signal); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 305 | { |
| 306 | clib_warning ("BUG"); |
| 307 | } |
| 308 | |
| 309 | int |
| 310 | connect_to_vpp (char *name) |
| 311 | { |
| 312 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 313 | api_main_t *am = &api_main; |
| 314 | |
| 315 | if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0) |
| 316 | return -1; |
| 317 | |
| 318 | utm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 319 | utm->my_client_index = am->my_client_index; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 325 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 326 | { |
| 327 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 328 | int rv; |
| 329 | |
| 330 | a->segment_name = (char *) mp->segment_name; |
| 331 | a->segment_size = mp->segment_size; |
| 332 | /* Attach to the segment vpp created */ |
| 333 | rv = svm_fifo_segment_attach (a); |
| 334 | if (rv) |
| 335 | { |
| 336 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 337 | mp->segment_name); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | clib_warning ("Mapped new segment '%s' size %d", mp->segment_name, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 341 | mp->segment_size); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static void |
| 345 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 346 | { |
| 347 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 348 | session_t *session; |
| 349 | vl_api_disconnect_session_reply_t *rmp; |
| 350 | uword *p; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 351 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 352 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 353 | p = hash_get (utm->session_index_by_vpp_handles, mp->handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 354 | |
| 355 | if (p) |
| 356 | { |
| 357 | session = pool_elt_at_index (utm->sessions, p[0]); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 358 | hash_unset (utm->session_index_by_vpp_handles, mp->handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 359 | pool_put (utm->sessions, session); |
| 360 | } |
| 361 | else |
| 362 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 363 | clib_warning ("couldn't find session key %llx", mp->handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 364 | rv = -11; |
| 365 | } |
| 366 | |
| 367 | utm->time_to_stop = 1; |
| 368 | |
| 369 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 370 | memset (rmp, 0, sizeof (*rmp)); |
| 371 | |
| 372 | rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY); |
| 373 | rmp->retval = rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 374 | rmp->handle = mp->handle; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 375 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp); |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | vl_api_reset_session_t_handler (vl_api_reset_session_t * mp) |
| 380 | { |
| 381 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 382 | session_t *session; |
| 383 | vl_api_reset_session_reply_t *rmp; |
| 384 | uword *p; |
| 385 | int rv = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 386 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 387 | p = hash_get (utm->session_index_by_vpp_handles, mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 388 | |
| 389 | if (p) |
| 390 | { |
| 391 | session = pool_elt_at_index (utm->sessions, p[0]); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 392 | hash_unset (utm->session_index_by_vpp_handles, mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 393 | pool_put (utm->sessions, session); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 394 | utm->time_to_stop = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 395 | } |
| 396 | else |
| 397 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 398 | clib_warning ("couldn't find session key %llx", mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 399 | rv = -11; |
| 400 | } |
| 401 | |
| 402 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 403 | memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 404 | rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 405 | rmp->retval = rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 406 | rmp->handle = mp->handle; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 407 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 411 | client_handle_fifo_event_rx (uri_tcp_test_main_t * utm, |
| 412 | session_fifo_event_t * e) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 413 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 414 | svm_fifo_t *rx_fifo; |
| 415 | int n_read, bytes, i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | |
| 417 | rx_fifo = e->fifo; |
| 418 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 419 | bytes = svm_fifo_max_dequeue (rx_fifo); |
| 420 | /* Allow enqueuing of new event */ |
| 421 | svm_fifo_unset_event (rx_fifo); |
| 422 | |
| 423 | /* Read the bytes */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 424 | do |
| 425 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 426 | n_read = svm_fifo_dequeue_nowait (rx_fifo, |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 427 | clib_min (vec_len (utm->rx_buf), |
| 428 | bytes), utm->rx_buf); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 429 | if (n_read > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 430 | { |
| 431 | bytes -= n_read; |
| 432 | for (i = 0; i < n_read; i++) |
| 433 | { |
| 434 | if (utm->rx_buf[i] != ((utm->client_bytes_received + i) & 0xff)) |
| 435 | { |
| 436 | clib_warning ("error at byte %lld, 0x%x not 0x%x", |
| 437 | utm->client_bytes_received + i, |
| 438 | utm->rx_buf[i], |
| 439 | ((utm->client_bytes_received + i) & 0xff)); |
| 440 | } |
| 441 | } |
| 442 | utm->client_bytes_received += n_read; |
| 443 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 444 | else |
| 445 | { |
| 446 | if (n_read == -2) |
| 447 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 448 | // clib_warning ("weird!"); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 452 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 453 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 454 | while (bytes > 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 458 | client_handle_event_queue (uri_tcp_test_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 459 | { |
| 460 | session_fifo_event_t _e, *e = &_e;; |
| 461 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 462 | unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e, |
| 463 | 0 /* nowait */ ); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 464 | switch (e->event_type) |
| 465 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 466 | case FIFO_EVENT_APP_RX: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 467 | client_handle_fifo_event_rx (utm, e); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 468 | break; |
| 469 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 470 | case FIFO_EVENT_DISCONNECT: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 471 | return; |
| 472 | |
| 473 | default: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 474 | clib_warning ("unknown event type %d", e->event_type); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 479 | static void * |
| 480 | client_rx_thread_fn (void *arg) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 481 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 482 | session_fifo_event_t _e, *e = &_e; |
| 483 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 484 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 485 | utm->client_bytes_received = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 486 | while (1) |
| 487 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 488 | unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e, |
| 489 | 0 /* nowait */ ); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 490 | switch (e->event_type) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 491 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 492 | case FIFO_EVENT_APP_RX: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 493 | client_handle_fifo_event_rx (utm, e); |
| 494 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 495 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 496 | case FIFO_EVENT_DISCONNECT: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 497 | return 0; |
| 498 | default: |
| 499 | clib_warning ("unknown event type %d", e->event_type); |
| 500 | break; |
| 501 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 502 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 503 | if (PREDICT_FALSE (utm->time_to_stop == 1)) |
| 504 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 505 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 506 | pthread_exit (0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 509 | |
| 510 | static void |
| 511 | vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp) |
| 512 | { |
| 513 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 514 | session_t *session; |
| 515 | u32 session_index; |
| 516 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 517 | int rv; |
| 518 | |
| 519 | if (mp->retval) |
| 520 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 521 | clib_warning ("connection failed with code: %U", format_api_error, |
| 522 | clib_net_to_host_u32 (mp->retval)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 523 | utm->state = STATE_FAILED; |
| 524 | return; |
| 525 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 526 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 527 | utm->vpp_event_queue = (unix_shared_memory_queue_t *) |
| 528 | mp->vpp_event_queue_address; |
| 529 | |
| 530 | /* |
| 531 | * Setup session |
| 532 | */ |
| 533 | |
| 534 | pool_get (utm->sessions, session); |
| 535 | session_index = session - utm->sessions; |
| 536 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 537 | rx_fifo = (svm_fifo_t *) mp->server_rx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 538 | rx_fifo->client_session_index = session_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 539 | tx_fifo = (svm_fifo_t *) mp->server_tx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 540 | tx_fifo->client_session_index = session_index; |
| 541 | |
| 542 | session->server_rx_fifo = rx_fifo; |
| 543 | session->server_tx_fifo = tx_fifo; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 544 | session->vpp_session_handle = mp->handle; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 545 | |
| 546 | /* Save handle */ |
| 547 | utm->connected_session_index = session_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 548 | utm->state = STATE_READY; |
| 549 | |
| 550 | /* Add it to lookup table */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 551 | hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 552 | |
| 553 | /* Start RX thread */ |
| 554 | rv = pthread_create (&utm->client_rx_thread_handle, |
| 555 | NULL /*attr */ , client_rx_thread_fn, 0); |
| 556 | if (rv) |
| 557 | { |
| 558 | clib_warning ("pthread_create returned %d", rv); |
| 559 | rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 560 | } |
| 561 | } |
| 562 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 563 | static void |
| 564 | send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid, |
| 565 | u32 bytes) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 566 | { |
| 567 | u8 *test_data = utm->connect_test_data; |
| 568 | u64 bytes_sent = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 569 | int test_buf_offset = 0; |
| 570 | u32 bytes_to_snd; |
| 571 | u32 queue_max_chunk = 64 << 10, actual_write; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 572 | session_fifo_event_t evt; |
| 573 | static int serial_number = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 574 | int rv; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 575 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 576 | bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes; |
| 577 | if (bytes_to_snd > vec_len (test_data)) |
| 578 | bytes_to_snd = vec_len (test_data); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 579 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 580 | while (bytes_to_snd > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 581 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 582 | actual_write = |
| 583 | bytes_to_snd > queue_max_chunk ? queue_max_chunk : bytes_to_snd; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 584 | rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write, |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 585 | test_data + test_buf_offset); |
| 586 | |
| 587 | if (rv > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 588 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 589 | bytes_to_snd -= rv; |
| 590 | test_buf_offset += rv; |
| 591 | bytes_sent += rv; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 592 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 593 | if (svm_fifo_set_event (tx_fifo)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 594 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 595 | /* Fabricate TX event, send to vpp */ |
| 596 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 597 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 598 | evt.event_id = serial_number++; |
| 599 | |
| 600 | unix_shared_memory_queue_add (utm->vpp_event_queue, |
| 601 | (u8 *) & evt, |
| 602 | 0 /* do wait for mutex */ ); |
| 603 | } |
| 604 | } |
| 605 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | void |
| 609 | client_send_data (uri_tcp_test_main_t * utm) |
| 610 | { |
| 611 | u8 *test_data = utm->connect_test_data; |
| 612 | int mypid = getpid (); |
| 613 | session_t *session; |
| 614 | svm_fifo_t *tx_fifo; |
| 615 | u32 n_iterations, leftover; |
| 616 | int i; |
| 617 | |
| 618 | session = pool_elt_at_index (utm->sessions, utm->connected_session_index); |
| 619 | tx_fifo = session->server_tx_fifo; |
| 620 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 621 | ASSERT (vec_len (test_data) > 0); |
| 622 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 623 | vec_validate (utm->rx_buf, vec_len (test_data) - 1); |
| 624 | n_iterations = utm->bytes_to_send / vec_len (test_data); |
| 625 | |
| 626 | for (i = 0; i < n_iterations; i++) |
| 627 | { |
| 628 | send_test_chunk (utm, tx_fifo, mypid, 0); |
| 629 | } |
| 630 | |
| 631 | leftover = utm->bytes_to_send % vec_len (test_data); |
| 632 | if (leftover) |
| 633 | send_test_chunk (utm, tx_fifo, mypid, leftover); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 634 | |
| 635 | if (utm->test_return_packets) |
| 636 | { |
| 637 | f64 timeout = clib_time_now (&utm->clib_time) + 2; |
| 638 | |
| 639 | /* Wait for the outstanding packets */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 640 | while (utm->client_bytes_received < |
| 641 | vec_len (test_data) * n_iterations + leftover) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 642 | { |
| 643 | if (clib_time_now (&utm->clib_time) > timeout) |
| 644 | { |
| 645 | clib_warning ("timed out waiting for the missing packets"); |
| 646 | break; |
| 647 | } |
| 648 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 649 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 650 | utm->time_to_stop = 1; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 654 | client_send_connect (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 655 | { |
| 656 | vl_api_connect_uri_t *cmp; |
| 657 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 658 | memset (cmp, 0, sizeof (*cmp)); |
| 659 | |
| 660 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
| 661 | cmp->client_index = utm->my_client_index; |
| 662 | cmp->context = ntohl (0xfeedface); |
| 663 | memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri)); |
| 664 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp); |
| 665 | } |
| 666 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 667 | int |
| 668 | client_connect (uri_tcp_test_main_t * utm) |
| 669 | { |
| 670 | client_send_connect (utm); |
| 671 | if (wait_for_state_change (utm, STATE_READY)) |
| 672 | { |
| 673 | clib_warning ("Connect failed"); |
| 674 | return -1; |
| 675 | } |
| 676 | return 0; |
| 677 | } |
| 678 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 679 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 680 | client_send_disconnect (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 681 | { |
| 682 | session_t *connected_session; |
| 683 | vl_api_disconnect_session_t *dmp; |
| 684 | connected_session = pool_elt_at_index (utm->sessions, |
| 685 | utm->connected_session_index); |
| 686 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 687 | memset (dmp, 0, sizeof (*dmp)); |
| 688 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 689 | dmp->client_index = utm->my_client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 690 | dmp->handle = connected_session->vpp_session_handle; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 691 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp); |
| 692 | } |
| 693 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 694 | int |
| 695 | client_disconnect (uri_tcp_test_main_t * utm) |
| 696 | { |
| 697 | client_send_disconnect (utm); |
| 698 | if (wait_for_state_change (utm, STATE_START)) |
| 699 | { |
| 700 | clib_warning ("Disconnect failed"); |
| 701 | return -1; |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 706 | static void |
| 707 | client_test (uri_tcp_test_main_t * utm) |
| 708 | { |
| 709 | int i; |
| 710 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 711 | if (application_attach (utm)) |
| 712 | return; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 713 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 714 | if (client_connect (utm)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 715 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 716 | application_detach (utm); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | |
| 720 | /* Init test data */ |
| 721 | vec_validate (utm->connect_test_data, 64 * 1024 - 1); |
| 722 | for (i = 0; i < vec_len (utm->connect_test_data); i++) |
| 723 | utm->connect_test_data[i] = i & 0xff; |
| 724 | |
| 725 | /* Start send */ |
| 726 | client_send_data (utm); |
| 727 | |
| 728 | /* Disconnect */ |
| 729 | client_disconnect (utm); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 730 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 731 | application_detach (utm); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static void |
| 735 | vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp) |
| 736 | { |
| 737 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 738 | |
| 739 | if (mp->retval) |
| 740 | { |
flyingeagle23 | a07779f | 2017-04-26 20:22:04 +0800 | [diff] [blame] | 741 | clib_warning ("bind failed: %U", format_api_error, |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 742 | clib_net_to_host_u32 (mp->retval)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 743 | utm->state = STATE_FAILED; |
| 744 | return; |
| 745 | } |
| 746 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 747 | utm->state = STATE_READY; |
| 748 | } |
| 749 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 750 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 751 | vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 752 | { |
| 753 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 754 | |
| 755 | if (mp->retval != 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 756 | clib_warning ("returned %d", ntohl (mp->retval)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 757 | |
| 758 | utm->state = STATE_START; |
| 759 | } |
| 760 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 761 | u8 * |
| 762 | format_ip4_address (u8 * s, va_list * args) |
| 763 | { |
| 764 | u8 *a = va_arg (*args, u8 *); |
| 765 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 766 | } |
| 767 | |
| 768 | u8 * |
| 769 | format_ip6_address (u8 * s, va_list * args) |
| 770 | { |
| 771 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 772 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 773 | |
| 774 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 775 | max_n_zeros = 0; |
| 776 | i_first_zero = i_max_n_zero; |
| 777 | n_zeros = 0; |
| 778 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 779 | { |
| 780 | u32 is_zero = a->as_u16[i] == 0; |
| 781 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 782 | { |
| 783 | i_first_zero = i; |
| 784 | n_zeros = 0; |
| 785 | } |
| 786 | n_zeros += is_zero; |
| 787 | if ((!is_zero && n_zeros > max_n_zeros) |
| 788 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 789 | { |
| 790 | i_max_n_zero = i_first_zero; |
| 791 | max_n_zeros = n_zeros; |
| 792 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 793 | n_zeros = 0; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | last_double_colon = 0; |
| 798 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 799 | { |
| 800 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 801 | { |
| 802 | s = format (s, "::"); |
| 803 | i += max_n_zeros - 1; |
| 804 | last_double_colon = 1; |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | s = format (s, "%s%x", |
| 809 | (last_double_colon || i == 0) ? "" : ":", |
| 810 | clib_net_to_host_u16 (a->as_u16[i])); |
| 811 | last_double_colon = 0; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | return s; |
| 816 | } |
| 817 | |
| 818 | /* Format an IP46 address. */ |
| 819 | u8 * |
| 820 | format_ip46_address (u8 * s, va_list * args) |
| 821 | { |
| 822 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 823 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 824 | int is_ip4 = 1; |
| 825 | |
| 826 | switch (type) |
| 827 | { |
| 828 | case IP46_TYPE_ANY: |
| 829 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 830 | break; |
| 831 | case IP46_TYPE_IP4: |
| 832 | is_ip4 = 1; |
| 833 | break; |
| 834 | case IP46_TYPE_IP6: |
| 835 | is_ip4 = 0; |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | return is_ip4 ? |
| 840 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 841 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 842 | } |
| 843 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 844 | static void |
| 845 | vl_api_accept_session_t_handler (vl_api_accept_session_t * mp) |
| 846 | { |
| 847 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 848 | vl_api_accept_session_reply_t *rmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 849 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 850 | session_t *session; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 851 | static f64 start_time; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 852 | u32 session_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 853 | u8 *ip_str; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 854 | |
| 855 | if (start_time == 0.0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 856 | start_time = clib_time_now (&utm->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 857 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 858 | ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4); |
| 859 | clib_warning ("Accepted session from: %s:%d", ip_str, |
| 860 | clib_net_to_host_u16 (mp->port)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 861 | utm->vpp_event_queue = (unix_shared_memory_queue_t *) |
| 862 | mp->vpp_event_queue_address; |
| 863 | |
| 864 | /* Allocate local session and set it up */ |
| 865 | pool_get (utm->sessions, session); |
| 866 | session_index = session - utm->sessions; |
| 867 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 868 | rx_fifo = (svm_fifo_t *) mp->server_rx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 869 | rx_fifo->client_session_index = session_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 870 | tx_fifo = (svm_fifo_t *) mp->server_tx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 871 | tx_fifo->client_session_index = session_index; |
| 872 | |
| 873 | session->server_rx_fifo = rx_fifo; |
| 874 | session->server_tx_fifo = tx_fifo; |
| 875 | |
| 876 | /* Add it to lookup table */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 877 | hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 878 | |
| 879 | utm->state = STATE_READY; |
| 880 | |
| 881 | /* Stats printing */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 882 | if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 883 | { |
| 884 | f64 now = clib_time_now (&utm->clib_time); |
| 885 | fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n", |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 886 | pool_elts (utm->sessions), now - start_time, |
| 887 | (f64) pool_elts (utm->sessions) / (now - start_time)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 888 | } |
| 889 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 890 | /* |
| 891 | * Send accept reply to vpp |
| 892 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 893 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 894 | memset (rmp, 0, sizeof (*rmp)); |
| 895 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 896 | rmp->handle = mp->handle; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 897 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 901 | server_handle_fifo_event_rx (uri_tcp_test_main_t * utm, |
| 902 | session_fifo_event_t * e) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 903 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 904 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 905 | int n_read; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 906 | session_fifo_event_t evt; |
| 907 | unix_shared_memory_queue_t *q; |
| 908 | int rv, bytes; |
| 909 | |
| 910 | rx_fifo = e->fifo; |
| 911 | tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo; |
| 912 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 913 | bytes = svm_fifo_max_dequeue (rx_fifo); |
| 914 | /* Allow enqueuing of a new event */ |
| 915 | svm_fifo_unset_event (rx_fifo); |
| 916 | |
| 917 | if (bytes == 0) |
| 918 | return; |
| 919 | |
| 920 | /* Read the bytes */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 921 | do |
| 922 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 923 | n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 924 | utm->rx_buf); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 925 | if (n_read > 0) |
| 926 | bytes -= n_read; |
| 927 | |
| 928 | if (utm->drop_packets) |
| 929 | continue; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 930 | |
| 931 | /* Reflect if a non-drop session */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 932 | if (n_read > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 933 | { |
| 934 | do |
| 935 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 936 | rv = svm_fifo_enqueue_nowait (tx_fifo, n_read, utm->rx_buf); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 937 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 938 | while (rv <= 0 && !utm->time_to_stop); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 939 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 940 | /* If event wasn't set, add one */ |
| 941 | if (svm_fifo_set_event (tx_fifo)) |
| 942 | { |
| 943 | /* Fabricate TX event, send to vpp */ |
| 944 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 945 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 946 | evt.event_id = e->event_id; |
| 947 | |
| 948 | q = utm->vpp_event_queue; |
| 949 | unix_shared_memory_queue_add (q, (u8 *) & evt, |
| 950 | 0 /* do wait for mutex */ ); |
| 951 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 952 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 953 | } |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 954 | while ((n_read < 0 || bytes > 0) && !utm->time_to_stop); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | void |
| 958 | server_handle_event_queue (uri_tcp_test_main_t * utm) |
| 959 | { |
| 960 | session_fifo_event_t _e, *e = &_e;; |
| 961 | |
| 962 | while (1) |
| 963 | { |
| 964 | unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e, |
| 965 | 0 /* nowait */ ); |
| 966 | switch (e->event_type) |
| 967 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 968 | case FIFO_EVENT_APP_RX: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 969 | server_handle_fifo_event_rx (utm, e); |
| 970 | break; |
| 971 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 972 | case FIFO_EVENT_DISCONNECT: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 973 | return; |
| 974 | |
| 975 | default: |
| 976 | clib_warning ("unknown event type %d", e->event_type); |
| 977 | break; |
| 978 | } |
| 979 | if (PREDICT_FALSE (utm->time_to_stop == 1)) |
| 980 | break; |
| 981 | if (PREDICT_FALSE (utm->time_to_print_stats == 1)) |
| 982 | { |
| 983 | utm->time_to_print_stats = 0; |
| 984 | fformat (stdout, "%d connections\n", pool_elts (utm->sessions)); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 990 | server_send_listen (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 991 | { |
| 992 | vl_api_bind_uri_t *bmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 993 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 994 | memset (bmp, 0, sizeof (*bmp)); |
| 995 | |
| 996 | bmp->_vl_msg_id = ntohs (VL_API_BIND_URI); |
| 997 | bmp->client_index = utm->my_client_index; |
| 998 | bmp->context = ntohl (0xfeedface); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 999 | memcpy (bmp->uri, utm->uri, vec_len (utm->uri)); |
| 1000 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 1001 | } |
| 1002 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1003 | int |
| 1004 | server_listen (uri_tcp_test_main_t * utm) |
| 1005 | { |
| 1006 | server_send_listen (utm); |
| 1007 | if (wait_for_state_change (utm, STATE_READY)) |
| 1008 | { |
| 1009 | clib_warning ("timeout waiting for STATE_READY"); |
| 1010 | return -1; |
| 1011 | } |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1015 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1016 | server_send_unbind (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1017 | { |
| 1018 | vl_api_unbind_uri_t *ump; |
| 1019 | |
| 1020 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 1021 | memset (ump, 0, sizeof (*ump)); |
| 1022 | |
| 1023 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI); |
| 1024 | ump->client_index = utm->my_client_index; |
| 1025 | memcpy (ump->uri, utm->uri, vec_len (utm->uri)); |
| 1026 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump); |
| 1027 | } |
| 1028 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1029 | int |
| 1030 | server_unbind (uri_tcp_test_main_t * utm) |
| 1031 | { |
| 1032 | server_send_unbind (utm); |
| 1033 | if (wait_for_state_change (utm, STATE_START)) |
| 1034 | { |
| 1035 | clib_warning ("timeout waiting for STATE_START"); |
| 1036 | return -1; |
| 1037 | } |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1041 | void |
| 1042 | server_test (uri_tcp_test_main_t * utm) |
| 1043 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1044 | if (application_attach (utm)) |
| 1045 | return; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1046 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1047 | /* Bind to uri */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1048 | if (server_listen (utm)) |
| 1049 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1050 | |
| 1051 | /* Enter handle event loop */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1052 | server_handle_event_queue (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1053 | |
| 1054 | /* Cleanup */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1055 | server_send_unbind (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1056 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1057 | application_detach (utm); |
| 1058 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1059 | fformat (stdout, "Test complete...\n"); |
| 1060 | } |
| 1061 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1062 | static void |
| 1063 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 1064 | mp) |
| 1065 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1066 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 1067 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1068 | clib_warning ("retval %d", ntohl (mp->retval)); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1069 | utm->state = STATE_START; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | #define foreach_uri_msg \ |
| 1073 | _(BIND_URI_REPLY, bind_uri_reply) \ |
| 1074 | _(UNBIND_URI_REPLY, unbind_uri_reply) \ |
| 1075 | _(ACCEPT_SESSION, accept_session) \ |
| 1076 | _(CONNECT_URI_REPLY, connect_uri_reply) \ |
| 1077 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 1078 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 1079 | _(RESET_SESSION, reset_session) \ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1080 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 1081 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 1082 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1083 | |
| 1084 | void |
| 1085 | uri_api_hookup (uri_tcp_test_main_t * utm) |
| 1086 | { |
| 1087 | #define _(N,n) \ |
| 1088 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1089 | vl_api_##n##_t_handler, \ |
| 1090 | vl_noop_handler, \ |
| 1091 | vl_api_##n##_t_endian, \ |
| 1092 | vl_api_##n##_t_print, \ |
| 1093 | sizeof(vl_api_##n##_t), 1); |
| 1094 | foreach_uri_msg; |
| 1095 | #undef _ |
| 1096 | } |
| 1097 | |
| 1098 | int |
| 1099 | main (int argc, char **argv) |
| 1100 | { |
| 1101 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 1102 | unformat_input_t _argv, *a = &_argv; |
| 1103 | u8 *chroot_prefix; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1104 | u8 *heap, *uri = 0; |
| 1105 | u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234"; |
| 1106 | u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234"; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1107 | u64 bytes_to_send = 64 << 10, mbytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1108 | u32 tmp; |
| 1109 | mheap_t *h; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1110 | session_t *session; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1111 | int i; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1112 | int i_am_master = 1, drop_packets = 0, test_return_packets = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1113 | |
| 1114 | clib_mem_init (0, 256 << 20); |
| 1115 | |
| 1116 | heap = clib_mem_get_per_cpu_heap (); |
| 1117 | h = mheap_header (heap); |
| 1118 | |
| 1119 | /* make the main heap thread-safe */ |
| 1120 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 1121 | |
| 1122 | vec_validate (utm->rx_buf, 65536); |
| 1123 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1124 | utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1125 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1126 | utm->my_pid = getpid (); |
| 1127 | utm->configured_segment_size = 1 << 20; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1128 | |
| 1129 | clib_time_init (&utm->clib_time); |
| 1130 | init_error_string_table (utm); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1131 | svm_fifo_segment_init (0x200000000ULL, 20); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1132 | unformat_init_command_line (a, argv); |
| 1133 | |
| 1134 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 1135 | { |
| 1136 | if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1137 | { |
| 1138 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 1139 | } |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1140 | else if (unformat (a, "uri %s", &uri)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1141 | ; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1142 | else if (unformat (a, "segment-size %dM", &tmp)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1143 | utm->configured_segment_size = tmp << 20; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1144 | else if (unformat (a, "segment-size %dG", &tmp)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1145 | utm->configured_segment_size = tmp << 30; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1146 | else if (unformat (a, "master")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1147 | i_am_master = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1148 | else if (unformat (a, "slave")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1149 | i_am_master = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1150 | else if (unformat (a, "drop")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1151 | drop_packets = 1; |
| 1152 | else if (unformat (a, "test")) |
| 1153 | test_return_packets = 1; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1154 | else if (unformat (a, "mbytes %lld", &mbytes)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1155 | { |
| 1156 | bytes_to_send = mbytes << 20; |
| 1157 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1158 | else if (unformat (a, "gbytes %lld", &mbytes)) |
| 1159 | { |
| 1160 | bytes_to_send = mbytes << 30; |
| 1161 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1162 | else |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1163 | { |
| 1164 | fformat (stderr, "%s: usage [master|slave]\n"); |
| 1165 | exit (1); |
| 1166 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1167 | } |
| 1168 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1169 | if (uri) |
| 1170 | { |
| 1171 | utm->uri = format (0, "%s%c", uri, 0); |
| 1172 | utm->connect_uri = format (0, "%s%c", uri, 0); |
| 1173 | } |
| 1174 | else |
| 1175 | { |
| 1176 | utm->uri = format (0, "%s%c", bind_uri, 0); |
| 1177 | utm->connect_uri = format (0, "%s%c", connect_uri, 0); |
| 1178 | } |
| 1179 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1180 | utm->i_am_master = i_am_master; |
| 1181 | utm->segment_main = &svm_fifo_segment_main; |
| 1182 | utm->drop_packets = drop_packets; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1183 | utm->test_return_packets = test_return_packets; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1184 | utm->bytes_to_send = bytes_to_send; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1185 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1186 | setup_signal_handlers (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1187 | uri_api_hookup (utm); |
| 1188 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1189 | if (connect_to_vpp (i_am_master ? "uri_tcp_server" : "uri_tcp_client") < 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1190 | { |
| 1191 | svm_region_exit (); |
| 1192 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 1193 | exit (1); |
| 1194 | } |
| 1195 | |
| 1196 | if (i_am_master == 0) |
| 1197 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1198 | client_test (utm); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1199 | vl_client_disconnect_from_vlib (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1200 | exit (0); |
| 1201 | } |
| 1202 | |
| 1203 | /* $$$$ hack preallocation */ |
| 1204 | for (i = 0; i < 200000; i++) |
| 1205 | { |
| 1206 | pool_get (utm->sessions, session); |
| 1207 | memset (session, 0, sizeof (*session)); |
| 1208 | } |
| 1209 | for (i = 0; i < 200000; i++) |
| 1210 | pool_put_index (utm->sessions, i); |
| 1211 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1212 | server_test (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1213 | |
| 1214 | vl_client_disconnect_from_vlib (); |
| 1215 | exit (0); |
| 1216 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1217 | |
| 1218 | /* |
| 1219 | * fd.io coding-style-patch-verification: ON |
| 1220 | * |
| 1221 | * Local Variables: |
| 1222 | * eval: (c-set-style "gnu") |
| 1223 | * End: |
| 1224 | */ |