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