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