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 | ade70e4 | 2017-10-14 18:56:41 -0700 | [diff] [blame] | 545 | else |
| 546 | { |
| 547 | clib_warning ("connected with local ip %U port %d", format_ip46_address, |
| 548 | mp->lcl_ip, mp->is_ip4, |
| 549 | clib_net_to_host_u16 (mp->lcl_port)); |
| 550 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 551 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 552 | utm->vpp_event_queue = |
| 553 | uword_to_pointer (mp->vpp_event_queue_address, |
| 554 | unix_shared_memory_queue_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 555 | |
| 556 | /* |
| 557 | * Setup session |
| 558 | */ |
| 559 | |
| 560 | pool_get (utm->sessions, session); |
| 561 | session_index = session - utm->sessions; |
| 562 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 563 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 564 | rx_fifo->client_session_index = session_index; |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 565 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 566 | tx_fifo->client_session_index = session_index; |
| 567 | |
| 568 | session->server_rx_fifo = rx_fifo; |
| 569 | session->server_tx_fifo = tx_fifo; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 570 | session->vpp_session_handle = mp->handle; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 571 | session->start = clib_time_now (&utm->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 572 | |
| 573 | /* Save handle */ |
| 574 | utm->connected_session_index = session_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 575 | utm->state = STATE_READY; |
| 576 | |
| 577 | /* Add it to lookup table */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 578 | hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 579 | |
| 580 | /* Start RX thread */ |
| 581 | rv = pthread_create (&utm->client_rx_thread_handle, |
| 582 | NULL /*attr */ , client_rx_thread_fn, 0); |
| 583 | if (rv) |
| 584 | { |
| 585 | clib_warning ("pthread_create returned %d", rv); |
| 586 | rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 587 | } |
| 588 | } |
| 589 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 590 | static void |
| 591 | send_test_chunk (uri_tcp_test_main_t * utm, svm_fifo_t * tx_fifo, int mypid, |
| 592 | u32 bytes) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 593 | { |
| 594 | u8 *test_data = utm->connect_test_data; |
| 595 | u64 bytes_sent = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 596 | int test_buf_offset = 0; |
| 597 | u32 bytes_to_snd; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 598 | u32 queue_max_chunk = 128 << 10, actual_write; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 599 | session_fifo_event_t evt; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 600 | int rv; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 601 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 602 | bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes; |
| 603 | if (bytes_to_snd > vec_len (test_data)) |
| 604 | bytes_to_snd = vec_len (test_data); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 605 | |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 606 | while (bytes_to_snd > 0 && !utm->time_to_stop) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 607 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 608 | actual_write = (bytes_to_snd > queue_max_chunk) ? |
| 609 | queue_max_chunk : bytes_to_snd; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 610 | rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write, |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 611 | test_data + test_buf_offset); |
| 612 | |
| 613 | if (rv > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 614 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 615 | bytes_to_snd -= rv; |
| 616 | test_buf_offset += rv; |
| 617 | bytes_sent += rv; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 618 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 619 | if (svm_fifo_set_event (tx_fifo)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 620 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 621 | /* Fabricate TX event, send to vpp */ |
| 622 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 623 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 624 | |
| 625 | unix_shared_memory_queue_add (utm->vpp_event_queue, |
| 626 | (u8 *) & evt, |
| 627 | 0 /* do wait for mutex */ ); |
| 628 | } |
| 629 | } |
| 630 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void |
| 634 | client_send_data (uri_tcp_test_main_t * utm) |
| 635 | { |
| 636 | u8 *test_data = utm->connect_test_data; |
| 637 | int mypid = getpid (); |
| 638 | session_t *session; |
| 639 | svm_fifo_t *tx_fifo; |
| 640 | u32 n_iterations, leftover; |
| 641 | int i; |
| 642 | |
| 643 | session = pool_elt_at_index (utm->sessions, utm->connected_session_index); |
| 644 | tx_fifo = session->server_tx_fifo; |
| 645 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 646 | ASSERT (vec_len (test_data) > 0); |
| 647 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 648 | vec_validate (utm->rx_buf, vec_len (test_data) - 1); |
| 649 | n_iterations = utm->bytes_to_send / vec_len (test_data); |
| 650 | |
| 651 | for (i = 0; i < n_iterations; i++) |
| 652 | { |
| 653 | send_test_chunk (utm, tx_fifo, mypid, 0); |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 654 | if (utm->time_to_stop) |
| 655 | break; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | leftover = utm->bytes_to_send % vec_len (test_data); |
| 659 | if (leftover) |
| 660 | send_test_chunk (utm, tx_fifo, mypid, leftover); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 661 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 662 | if (!utm->drop_packets) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 663 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 664 | f64 timeout = clib_time_now (&utm->clib_time) + 10; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 665 | |
| 666 | /* Wait for the outstanding packets */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 667 | while (utm->client_bytes_received < |
| 668 | vec_len (test_data) * n_iterations + leftover) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 669 | { |
| 670 | if (clib_time_now (&utm->clib_time) > timeout) |
| 671 | { |
| 672 | clib_warning ("timed out waiting for the missing packets"); |
| 673 | break; |
| 674 | } |
| 675 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 676 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 677 | utm->time_to_stop = 1; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 681 | client_send_connect (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 682 | { |
| 683 | vl_api_connect_uri_t *cmp; |
| 684 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 685 | memset (cmp, 0, sizeof (*cmp)); |
| 686 | |
| 687 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
| 688 | cmp->client_index = utm->my_client_index; |
| 689 | cmp->context = ntohl (0xfeedface); |
| 690 | memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri)); |
| 691 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp); |
| 692 | } |
| 693 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 694 | int |
| 695 | client_connect (uri_tcp_test_main_t * utm) |
| 696 | { |
| 697 | client_send_connect (utm); |
| 698 | if (wait_for_state_change (utm, STATE_READY)) |
| 699 | { |
| 700 | clib_warning ("Connect failed"); |
| 701 | return -1; |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 706 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 707 | client_send_disconnect (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 708 | { |
| 709 | session_t *connected_session; |
| 710 | vl_api_disconnect_session_t *dmp; |
| 711 | connected_session = pool_elt_at_index (utm->sessions, |
| 712 | utm->connected_session_index); |
| 713 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 714 | memset (dmp, 0, sizeof (*dmp)); |
| 715 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 716 | dmp->client_index = utm->my_client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 717 | dmp->handle = connected_session->vpp_session_handle; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 718 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & dmp); |
| 719 | } |
| 720 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 721 | int |
| 722 | client_disconnect (uri_tcp_test_main_t * utm) |
| 723 | { |
| 724 | client_send_disconnect (utm); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 725 | clib_warning ("Sent disconnect"); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 726 | if (wait_for_state_change (utm, STATE_START)) |
| 727 | { |
| 728 | clib_warning ("Disconnect failed"); |
| 729 | return -1; |
| 730 | } |
| 731 | return 0; |
| 732 | } |
| 733 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 734 | static void |
| 735 | client_test (uri_tcp_test_main_t * utm) |
| 736 | { |
| 737 | int i; |
| 738 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 739 | if (application_attach (utm)) |
| 740 | return; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 741 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 742 | if (client_connect (utm)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 743 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 744 | application_detach (utm); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 745 | return; |
| 746 | } |
| 747 | |
| 748 | /* Init test data */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 749 | vec_validate (utm->connect_test_data, 128 * 1024 - 1); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 750 | for (i = 0; i < vec_len (utm->connect_test_data); i++) |
| 751 | utm->connect_test_data[i] = i & 0xff; |
| 752 | |
| 753 | /* Start send */ |
| 754 | client_send_data (utm); |
| 755 | |
| 756 | /* Disconnect */ |
| 757 | client_disconnect (utm); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 758 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 759 | application_detach (utm); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static void |
| 763 | vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp) |
| 764 | { |
| 765 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 766 | |
| 767 | if (mp->retval) |
| 768 | { |
flyingeagle23 | a07779f | 2017-04-26 20:22:04 +0800 | [diff] [blame] | 769 | clib_warning ("bind failed: %U", format_api_error, |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 770 | clib_net_to_host_u32 (mp->retval)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 771 | utm->state = STATE_FAILED; |
| 772 | return; |
| 773 | } |
| 774 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 775 | utm->state = STATE_READY; |
| 776 | } |
| 777 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 778 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 779 | 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] | 780 | { |
| 781 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 782 | |
| 783 | if (mp->retval != 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 784 | clib_warning ("returned %d", ntohl (mp->retval)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 785 | |
| 786 | utm->state = STATE_START; |
| 787 | } |
| 788 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 789 | u8 * |
| 790 | format_ip4_address (u8 * s, va_list * args) |
| 791 | { |
| 792 | u8 *a = va_arg (*args, u8 *); |
| 793 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 794 | } |
| 795 | |
| 796 | u8 * |
| 797 | format_ip6_address (u8 * s, va_list * args) |
| 798 | { |
| 799 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 800 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 801 | |
| 802 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 803 | max_n_zeros = 0; |
| 804 | i_first_zero = i_max_n_zero; |
| 805 | n_zeros = 0; |
| 806 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 807 | { |
| 808 | u32 is_zero = a->as_u16[i] == 0; |
| 809 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 810 | { |
| 811 | i_first_zero = i; |
| 812 | n_zeros = 0; |
| 813 | } |
| 814 | n_zeros += is_zero; |
| 815 | if ((!is_zero && n_zeros > max_n_zeros) |
| 816 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 817 | { |
| 818 | i_max_n_zero = i_first_zero; |
| 819 | max_n_zeros = n_zeros; |
| 820 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 821 | n_zeros = 0; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | last_double_colon = 0; |
| 826 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 827 | { |
| 828 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 829 | { |
| 830 | s = format (s, "::"); |
| 831 | i += max_n_zeros - 1; |
| 832 | last_double_colon = 1; |
| 833 | } |
| 834 | else |
| 835 | { |
| 836 | s = format (s, "%s%x", |
| 837 | (last_double_colon || i == 0) ? "" : ":", |
| 838 | clib_net_to_host_u16 (a->as_u16[i])); |
| 839 | last_double_colon = 0; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return s; |
| 844 | } |
| 845 | |
| 846 | /* Format an IP46 address. */ |
| 847 | u8 * |
| 848 | format_ip46_address (u8 * s, va_list * args) |
| 849 | { |
| 850 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 851 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 852 | int is_ip4 = 1; |
| 853 | |
| 854 | switch (type) |
| 855 | { |
| 856 | case IP46_TYPE_ANY: |
| 857 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 858 | break; |
| 859 | case IP46_TYPE_IP4: |
| 860 | is_ip4 = 1; |
| 861 | break; |
| 862 | case IP46_TYPE_IP6: |
| 863 | is_ip4 = 0; |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | return is_ip4 ? |
| 868 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 869 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 870 | } |
| 871 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 872 | static void |
| 873 | vl_api_accept_session_t_handler (vl_api_accept_session_t * mp) |
| 874 | { |
| 875 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 876 | vl_api_accept_session_reply_t *rmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 877 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 878 | session_t *session; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 879 | static f64 start_time; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 880 | u32 session_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 881 | u8 *ip_str; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 882 | |
| 883 | if (start_time == 0.0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 884 | start_time = clib_time_now (&utm->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 885 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 886 | ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4); |
| 887 | clib_warning ("Accepted session from: %s:%d", ip_str, |
| 888 | clib_net_to_host_u16 (mp->port)); |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 889 | utm->vpp_event_queue = |
| 890 | uword_to_pointer (mp->vpp_event_queue_address, |
| 891 | unix_shared_memory_queue_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 892 | |
| 893 | /* Allocate local session and set it up */ |
| 894 | pool_get (utm->sessions, session); |
| 895 | session_index = session - utm->sessions; |
| 896 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 897 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 898 | rx_fifo->client_session_index = session_index; |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 899 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 900 | tx_fifo->client_session_index = session_index; |
| 901 | |
| 902 | session->server_rx_fifo = rx_fifo; |
| 903 | session->server_tx_fifo = tx_fifo; |
| 904 | |
| 905 | /* Add it to lookup table */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 906 | hash_set (utm->session_index_by_vpp_handles, mp->handle, session_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 907 | |
| 908 | utm->state = STATE_READY; |
| 909 | |
| 910 | /* Stats printing */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 911 | if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 912 | { |
| 913 | f64 now = clib_time_now (&utm->clib_time); |
| 914 | fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n", |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 915 | pool_elts (utm->sessions), now - start_time, |
| 916 | (f64) pool_elts (utm->sessions) / (now - start_time)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 917 | } |
| 918 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 919 | /* |
| 920 | * Send accept reply to vpp |
| 921 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 922 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 923 | memset (rmp, 0, sizeof (*rmp)); |
| 924 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 925 | rmp->handle = mp->handle; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 926 | rmp->context = mp->context; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 927 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 928 | |
| 929 | session->bytes_received = 0; |
| 930 | session->start = clib_time_now (&utm->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 934 | server_handle_fifo_event_rx (uri_tcp_test_main_t * utm, |
| 935 | session_fifo_event_t * e) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 936 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 937 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 938 | int n_read; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 939 | session_fifo_event_t evt; |
| 940 | unix_shared_memory_queue_t *q; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 941 | session_t *session; |
| 942 | int rv; |
| 943 | u32 max_dequeue, offset, max_transfer, rx_buf_len; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 944 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 945 | rx_buf_len = vec_len (utm->rx_buf); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 946 | rx_fifo = e->fifo; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 947 | session = &utm->sessions[rx_fifo->client_session_index]; |
| 948 | tx_fifo = session->server_tx_fifo; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 949 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 950 | max_dequeue = svm_fifo_max_dequeue (rx_fifo); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 951 | /* Allow enqueuing of a new event */ |
| 952 | svm_fifo_unset_event (rx_fifo); |
| 953 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 954 | if (PREDICT_FALSE (max_dequeue == 0)) |
| 955 | { |
| 956 | return; |
| 957 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 958 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 959 | /* Read the max_dequeue */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 960 | do |
| 961 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 962 | max_transfer = clib_min (rx_buf_len, max_dequeue); |
| 963 | 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] | 964 | if (n_read > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 965 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 966 | max_dequeue -= n_read; |
| 967 | session->bytes_received += n_read; |
| 968 | } |
| 969 | |
| 970 | /* Reflect if a non-drop session */ |
| 971 | if (!utm->drop_packets && n_read > 0) |
| 972 | { |
| 973 | offset = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 974 | do |
| 975 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 976 | rv = svm_fifo_enqueue_nowait (tx_fifo, n_read, |
| 977 | &utm->rx_buf[offset]); |
| 978 | if (rv > 0) |
| 979 | { |
| 980 | n_read -= rv; |
| 981 | offset += rv; |
| 982 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 983 | } |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 984 | while ((rv <= 0 || n_read > 0) && !utm->time_to_stop); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 985 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 986 | /* If event wasn't set, add one */ |
| 987 | if (svm_fifo_set_event (tx_fifo)) |
| 988 | { |
| 989 | /* Fabricate TX event, send to vpp */ |
| 990 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 991 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 992 | |
| 993 | q = utm->vpp_event_queue; |
| 994 | unix_shared_memory_queue_add (q, (u8 *) & evt, |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 995 | 1 /* do wait for mutex */ ); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 996 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 997 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 998 | } |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 999 | while ((n_read < 0 || max_dequeue > 0) && !utm->time_to_stop); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | void |
| 1003 | server_handle_event_queue (uri_tcp_test_main_t * utm) |
| 1004 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1005 | session_fifo_event_t _e, *e = &_e; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1006 | |
| 1007 | while (1) |
| 1008 | { |
| 1009 | unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e, |
| 1010 | 0 /* nowait */ ); |
| 1011 | switch (e->event_type) |
| 1012 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1013 | case FIFO_EVENT_APP_RX: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1014 | server_handle_fifo_event_rx (utm, e); |
| 1015 | break; |
| 1016 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1017 | case FIFO_EVENT_DISCONNECT: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1018 | return; |
| 1019 | |
| 1020 | default: |
| 1021 | clib_warning ("unknown event type %d", e->event_type); |
| 1022 | break; |
| 1023 | } |
| 1024 | if (PREDICT_FALSE (utm->time_to_stop == 1)) |
| 1025 | break; |
| 1026 | if (PREDICT_FALSE (utm->time_to_print_stats == 1)) |
| 1027 | { |
| 1028 | utm->time_to_print_stats = 0; |
| 1029 | fformat (stdout, "%d connections\n", pool_elts (utm->sessions)); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1035 | server_send_listen (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1036 | { |
| 1037 | vl_api_bind_uri_t *bmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1038 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 1039 | memset (bmp, 0, sizeof (*bmp)); |
| 1040 | |
| 1041 | bmp->_vl_msg_id = ntohs (VL_API_BIND_URI); |
| 1042 | bmp->client_index = utm->my_client_index; |
| 1043 | bmp->context = ntohl (0xfeedface); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1044 | memcpy (bmp->uri, utm->uri, vec_len (utm->uri)); |
| 1045 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 1046 | } |
| 1047 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1048 | int |
| 1049 | server_listen (uri_tcp_test_main_t * utm) |
| 1050 | { |
| 1051 | server_send_listen (utm); |
| 1052 | if (wait_for_state_change (utm, STATE_READY)) |
| 1053 | { |
| 1054 | clib_warning ("timeout waiting for STATE_READY"); |
| 1055 | return -1; |
| 1056 | } |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1060 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1061 | server_send_unbind (uri_tcp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1062 | { |
| 1063 | vl_api_unbind_uri_t *ump; |
| 1064 | |
| 1065 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 1066 | memset (ump, 0, sizeof (*ump)); |
| 1067 | |
| 1068 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI); |
| 1069 | ump->client_index = utm->my_client_index; |
| 1070 | memcpy (ump->uri, utm->uri, vec_len (utm->uri)); |
| 1071 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump); |
| 1072 | } |
| 1073 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1074 | int |
| 1075 | server_unbind (uri_tcp_test_main_t * utm) |
| 1076 | { |
| 1077 | server_send_unbind (utm); |
| 1078 | if (wait_for_state_change (utm, STATE_START)) |
| 1079 | { |
| 1080 | clib_warning ("timeout waiting for STATE_START"); |
| 1081 | return -1; |
| 1082 | } |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1086 | void |
| 1087 | server_test (uri_tcp_test_main_t * utm) |
| 1088 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1089 | if (application_attach (utm)) |
| 1090 | return; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1091 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1092 | /* Bind to uri */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1093 | if (server_listen (utm)) |
| 1094 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1095 | |
| 1096 | /* Enter handle event loop */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1097 | server_handle_event_queue (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1098 | |
| 1099 | /* Cleanup */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1100 | server_send_unbind (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1101 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1102 | application_detach (utm); |
| 1103 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1104 | fformat (stdout, "Test complete...\n"); |
| 1105 | } |
| 1106 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1107 | static void |
| 1108 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 1109 | mp) |
| 1110 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1111 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1112 | session_t *session; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1113 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1114 | if (mp->retval) |
| 1115 | { |
| 1116 | clib_warning ("vpp complained about disconnect: %d", |
| 1117 | ntohl (mp->retval)); |
| 1118 | } |
| 1119 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1120 | utm->state = STATE_START; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1121 | session = pool_elt_at_index (utm->sessions, utm->connected_session_index); |
| 1122 | if (session) |
| 1123 | session_print_stats (utm, session); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | #define foreach_uri_msg \ |
| 1127 | _(BIND_URI_REPLY, bind_uri_reply) \ |
| 1128 | _(UNBIND_URI_REPLY, unbind_uri_reply) \ |
| 1129 | _(ACCEPT_SESSION, accept_session) \ |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1130 | _(CONNECT_SESSION_REPLY, connect_session_reply) \ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1131 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 1132 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 1133 | _(RESET_SESSION, reset_session) \ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1134 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 1135 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 1136 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1137 | |
| 1138 | void |
| 1139 | uri_api_hookup (uri_tcp_test_main_t * utm) |
| 1140 | { |
| 1141 | #define _(N,n) \ |
| 1142 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1143 | vl_api_##n##_t_handler, \ |
| 1144 | vl_noop_handler, \ |
| 1145 | vl_api_##n##_t_endian, \ |
| 1146 | vl_api_##n##_t_print, \ |
| 1147 | sizeof(vl_api_##n##_t), 1); |
| 1148 | foreach_uri_msg; |
| 1149 | #undef _ |
| 1150 | } |
| 1151 | |
| 1152 | int |
| 1153 | main (int argc, char **argv) |
| 1154 | { |
| 1155 | uri_tcp_test_main_t *utm = &uri_tcp_test_main; |
| 1156 | unformat_input_t _argv, *a = &_argv; |
| 1157 | u8 *chroot_prefix; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1158 | u8 *heap, *uri = 0; |
| 1159 | u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234"; |
| 1160 | u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234"; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1161 | u64 bytes_to_send = 64 << 10, mbytes; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1162 | u32 tmp; |
| 1163 | mheap_t *h; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1164 | session_t *session; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1165 | int i; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1166 | int i_am_master = 1, drop_packets = 0, test_return_packets = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1167 | |
| 1168 | clib_mem_init (0, 256 << 20); |
| 1169 | |
| 1170 | heap = clib_mem_get_per_cpu_heap (); |
| 1171 | h = mheap_header (heap); |
| 1172 | |
| 1173 | /* make the main heap thread-safe */ |
| 1174 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 1175 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1176 | vec_validate (utm->rx_buf, 128 << 10); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1177 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1178 | utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1179 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1180 | utm->my_pid = getpid (); |
| 1181 | utm->configured_segment_size = 1 << 20; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1182 | |
| 1183 | clib_time_init (&utm->clib_time); |
| 1184 | init_error_string_table (utm); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1185 | svm_fifo_segment_init (0x200000000ULL, 20); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1186 | unformat_init_command_line (a, argv); |
| 1187 | |
| 1188 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 1189 | { |
| 1190 | if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1191 | { |
| 1192 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 1193 | } |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1194 | else if (unformat (a, "uri %s", &uri)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1195 | ; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1196 | else if (unformat (a, "segment-size %dM", &tmp)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1197 | utm->configured_segment_size = tmp << 20; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1198 | else if (unformat (a, "segment-size %dG", &tmp)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1199 | utm->configured_segment_size = tmp << 30; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1200 | else if (unformat (a, "master")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1201 | i_am_master = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1202 | else if (unformat (a, "slave")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1203 | i_am_master = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1204 | else if (unformat (a, "drop")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1205 | drop_packets = 1; |
| 1206 | else if (unformat (a, "test")) |
| 1207 | test_return_packets = 1; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1208 | else if (unformat (a, "mbytes %lld", &mbytes)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1209 | { |
| 1210 | bytes_to_send = mbytes << 20; |
| 1211 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1212 | else if (unformat (a, "gbytes %lld", &mbytes)) |
| 1213 | { |
| 1214 | bytes_to_send = mbytes << 30; |
| 1215 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1216 | else |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1217 | { |
| 1218 | fformat (stderr, "%s: usage [master|slave]\n"); |
| 1219 | exit (1); |
| 1220 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1221 | } |
| 1222 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1223 | if (uri) |
| 1224 | { |
| 1225 | utm->uri = format (0, "%s%c", uri, 0); |
| 1226 | utm->connect_uri = format (0, "%s%c", uri, 0); |
| 1227 | } |
| 1228 | else |
| 1229 | { |
| 1230 | utm->uri = format (0, "%s%c", bind_uri, 0); |
| 1231 | utm->connect_uri = format (0, "%s%c", connect_uri, 0); |
| 1232 | } |
| 1233 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1234 | utm->i_am_master = i_am_master; |
| 1235 | utm->segment_main = &svm_fifo_segment_main; |
| 1236 | utm->drop_packets = drop_packets; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1237 | utm->test_return_packets = test_return_packets; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1238 | utm->bytes_to_send = bytes_to_send; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1239 | utm->time_to_stop = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1240 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1241 | setup_signal_handlers (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1242 | uri_api_hookup (utm); |
| 1243 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1244 | 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] | 1245 | { |
| 1246 | svm_region_exit (); |
| 1247 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 1248 | exit (1); |
| 1249 | } |
| 1250 | |
| 1251 | if (i_am_master == 0) |
| 1252 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1253 | client_test (utm); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1254 | vl_client_disconnect_from_vlib (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1255 | exit (0); |
| 1256 | } |
| 1257 | |
| 1258 | /* $$$$ hack preallocation */ |
| 1259 | for (i = 0; i < 200000; i++) |
| 1260 | { |
| 1261 | pool_get (utm->sessions, session); |
| 1262 | memset (session, 0, sizeof (*session)); |
| 1263 | } |
| 1264 | for (i = 0; i < 200000; i++) |
| 1265 | pool_put_index (utm->sessions, i); |
| 1266 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1267 | server_test (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1268 | |
| 1269 | vl_client_disconnect_from_vlib (); |
| 1270 | exit (0); |
| 1271 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1272 | |
| 1273 | /* |
| 1274 | * fd.io coding-style-patch-verification: ON |
| 1275 | * |
| 1276 | * Local Variables: |
| 1277 | * eval: (c-set-style "gnu") |
| 1278 | * End: |
| 1279 | */ |