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