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