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 <setjmp.h> |
| 18 | #include <signal.h> |
| 19 | #include <vppinfra/clib.h> |
| 20 | #include <vppinfra/format.h> |
| 21 | #include <vppinfra/error.h> |
| 22 | #include <vppinfra/time.h> |
| 23 | #include <vppinfra/macros.h> |
| 24 | #include <vnet/vnet.h> |
| 25 | #include <vlib/vlib.h> |
| 26 | #include <vlib/unix/unix.h> |
| 27 | #include <vlibapi/api.h> |
| 28 | #include <vlibmemory/api.h> |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 29 | #include <vpp/api/vpe_msg_enum.h> |
| 30 | #include <svm/svm_fifo_segment.h> |
| 31 | #include <pthread.h> |
| 32 | #include <vnet/session/application_interface.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 33 | |
| 34 | #define vl_typedefs /* define message structures */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 35 | #include <vpp/api/vpe_all_api_h.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 36 | #undef vl_typedefs |
| 37 | |
| 38 | /* declare message handlers for each api */ |
| 39 | |
| 40 | #define vl_endianfun /* define message structures */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 41 | #include <vpp/api/vpe_all_api_h.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 42 | #undef vl_endianfun |
| 43 | |
| 44 | /* instantiate all the print functions we know about */ |
| 45 | #define vl_print(handle, ...) |
| 46 | #define vl_printfun |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 47 | #include <vpp/api/vpe_all_api_h.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 48 | #undef vl_printfun |
| 49 | |
| 50 | /* Satisfy external references when not linking with -lvlib */ |
| 51 | vlib_main_t vlib_global_main; |
| 52 | vlib_main_t **vlib_mains; |
| 53 | |
| 54 | typedef enum |
| 55 | { |
| 56 | STATE_START, |
| 57 | STATE_READY, |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 58 | STATE_FAILED, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 59 | STATE_DISCONNECTING, |
| 60 | } connection_state_t; |
| 61 | |
| 62 | typedef struct |
| 63 | { |
| 64 | svm_fifo_t *server_rx_fifo; |
| 65 | svm_fifo_t *server_tx_fifo; |
| 66 | } session_t; |
| 67 | |
| 68 | typedef struct |
| 69 | { |
| 70 | /* vpe input queue */ |
| 71 | unix_shared_memory_queue_t *vl_input_queue; |
| 72 | |
| 73 | /* API client handle */ |
| 74 | u32 my_client_index; |
| 75 | |
| 76 | /* The URI we're playing with */ |
| 77 | u8 *uri; |
| 78 | |
| 79 | /* Session pool */ |
| 80 | session_t *sessions; |
| 81 | |
| 82 | /* Hash table for disconnect processing */ |
| 83 | uword *session_index_by_vpp_handles; |
| 84 | |
| 85 | /* fifo segment */ |
| 86 | svm_fifo_segment_private_t *seg; |
| 87 | |
| 88 | /* intermediate rx buffer */ |
| 89 | u8 *rx_buf; |
| 90 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 91 | /* URI for connect */ |
| 92 | u8 *connect_uri; |
| 93 | |
| 94 | int i_am_master; |
| 95 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 96 | /* Our event queue */ |
| 97 | unix_shared_memory_queue_t *our_event_queue; |
| 98 | |
| 99 | /* $$$ single thread only for the moment */ |
| 100 | unix_shared_memory_queue_t *vpp_event_queue; |
| 101 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 102 | /* $$$$ hack: cut-through session index */ |
| 103 | volatile u32 cut_through_session_index; |
| 104 | |
| 105 | /* unique segment name counter */ |
| 106 | u32 unique_segment_index; |
| 107 | |
| 108 | pid_t my_pid; |
| 109 | |
| 110 | /* pthread handle */ |
| 111 | pthread_t cut_through_thread_handle; |
| 112 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 113 | /* For deadman timers */ |
| 114 | clib_time_t clib_time; |
| 115 | |
| 116 | /* State of the connection, shared between msg RX thread and main thread */ |
| 117 | volatile connection_state_t state; |
| 118 | |
| 119 | volatile int time_to_stop; |
| 120 | volatile int time_to_print_stats; |
| 121 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 122 | u32 configured_segment_size; |
| 123 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 124 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
| 125 | uword *error_string_by_error_number; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 126 | |
| 127 | /* convenience */ |
| 128 | svm_fifo_segment_main_t *segment_main; |
| 129 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 130 | } uri_udp_test_main_t; |
| 131 | |
| 132 | #if CLIB_DEBUG > 0 |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 133 | #define NITER 10000 |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 134 | #else |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 135 | #define NITER 4000000 |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | uri_udp_test_main_t uri_udp_test_main; |
| 139 | |
| 140 | static void |
| 141 | stop_signal (int signum) |
| 142 | { |
| 143 | uri_udp_test_main_t *um = &uri_udp_test_main; |
| 144 | |
| 145 | um->time_to_stop = 1; |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | stats_signal (int signum) |
| 150 | { |
| 151 | uri_udp_test_main_t *um = &uri_udp_test_main; |
| 152 | |
| 153 | um->time_to_print_stats = 1; |
| 154 | } |
| 155 | |
| 156 | static clib_error_t * |
| 157 | setup_signal_handlers (void) |
| 158 | { |
| 159 | signal (SIGINT, stats_signal); |
| 160 | signal (SIGQUIT, stop_signal); |
| 161 | signal (SIGTERM, stop_signal); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 166 | void |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 167 | application_send_attach (uri_udp_test_main_t * utm) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 168 | { |
| 169 | vl_api_application_attach_t *bmp; |
| 170 | u32 fifo_size = 3 << 20; |
| 171 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 172 | memset (bmp, 0, sizeof (*bmp)); |
| 173 | |
| 174 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 175 | bmp->client_index = utm->my_client_index; |
| 176 | bmp->context = ntohl (0xfeedface); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 177 | bmp->options[APP_OPTIONS_FLAGS] = |
| 178 | APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 179 | bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = fifo_size; |
| 180 | bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = fifo_size; |
| 181 | bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20; |
| 182 | bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 20; |
| 183 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | application_detach (uri_udp_test_main_t * utm) |
| 188 | { |
| 189 | vl_api_application_detach_t *bmp; |
| 190 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 191 | memset (bmp, 0, sizeof (*bmp)); |
| 192 | |
| 193 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 194 | bmp->client_index = utm->my_client_index; |
| 195 | bmp->context = ntohl (0xfeedface); |
| 196 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 201 | mp) |
| 202 | { |
| 203 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 204 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 205 | int rv; |
| 206 | |
| 207 | if (mp->retval) |
| 208 | { |
| 209 | clib_warning ("attach failed: %d", mp->retval); |
| 210 | utm->state = STATE_FAILED; |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (mp->segment_name_length == 0) |
| 215 | { |
| 216 | clib_warning ("segment_name_length zero"); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | a->segment_name = (char *) mp->segment_name; |
| 221 | a->segment_size = mp->segment_size; |
| 222 | |
| 223 | ASSERT (mp->app_event_queue_address); |
| 224 | |
| 225 | /* Attach to the segment vpp created */ |
| 226 | rv = svm_fifo_segment_attach (a); |
| 227 | if (rv) |
| 228 | { |
| 229 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 230 | mp->segment_name); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | utm->our_event_queue = |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 235 | uword_to_pointer (mp->app_event_queue_address, |
| 236 | unix_shared_memory_queue_t *); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void |
| 240 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 241 | mp) |
| 242 | { |
| 243 | if (mp->retval) |
| 244 | clib_warning ("detach returned with err: %d", mp->retval); |
| 245 | } |
| 246 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 247 | u8 * |
| 248 | format_api_error (u8 * s, va_list * args) |
| 249 | { |
| 250 | uri_udp_test_main_t *utm = va_arg (*args, uri_udp_test_main_t *); |
| 251 | i32 error = va_arg (*args, u32); |
| 252 | uword *p; |
| 253 | |
| 254 | p = hash_get (utm->error_string_by_error_number, -error); |
| 255 | |
| 256 | if (p) |
| 257 | s = format (s, "%s", p[0]); |
| 258 | else |
| 259 | s = format (s, "%d", error); |
| 260 | return s; |
| 261 | } |
| 262 | |
| 263 | int |
| 264 | wait_for_state_change (uri_udp_test_main_t * utm, connection_state_t state) |
| 265 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 266 | #if CLIB_DEBUG > 0 |
| 267 | #define TIMEOUT 600.0 |
| 268 | #else |
| 269 | #define TIMEOUT 600.0 |
| 270 | #endif |
| 271 | |
| 272 | f64 timeout = clib_time_now (&utm->clib_time) + TIMEOUT; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 273 | |
| 274 | while (clib_time_now (&utm->clib_time) < timeout) |
| 275 | { |
| 276 | if (utm->state == state) |
| 277 | return 0; |
| 278 | } |
| 279 | return -1; |
| 280 | } |
| 281 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 282 | u64 server_bytes_received, server_bytes_sent; |
| 283 | |
| 284 | static void * |
| 285 | cut_through_thread_fn (void *arg) |
| 286 | { |
| 287 | session_t *s; |
| 288 | svm_fifo_t *rx_fifo; |
| 289 | svm_fifo_t *tx_fifo; |
| 290 | u8 *my_copy_buffer = 0; |
| 291 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 292 | i32 actual_transfer; |
| 293 | int rv; |
| 294 | u32 buffer_offset; |
| 295 | |
| 296 | while (utm->cut_through_session_index == ~0) |
| 297 | ; |
| 298 | |
| 299 | s = pool_elt_at_index (utm->sessions, utm->cut_through_session_index); |
| 300 | |
| 301 | rx_fifo = s->server_rx_fifo; |
| 302 | tx_fifo = s->server_tx_fifo; |
| 303 | |
| 304 | vec_validate (my_copy_buffer, 64 * 1024 - 1); |
| 305 | |
| 306 | while (true) |
| 307 | { |
| 308 | /* We read from the tx fifo and write to the rx fifo */ |
| 309 | do |
| 310 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 311 | actual_transfer = svm_fifo_dequeue_nowait (tx_fifo, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 312 | vec_len (my_copy_buffer), |
| 313 | my_copy_buffer); |
| 314 | } |
| 315 | while (actual_transfer <= 0); |
| 316 | |
| 317 | server_bytes_received += actual_transfer; |
| 318 | |
| 319 | buffer_offset = 0; |
| 320 | while (actual_transfer > 0) |
| 321 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 322 | rv = svm_fifo_enqueue_nowait (rx_fifo, actual_transfer, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 323 | my_copy_buffer + buffer_offset); |
| 324 | if (rv > 0) |
| 325 | { |
| 326 | actual_transfer -= rv; |
| 327 | buffer_offset += rv; |
| 328 | server_bytes_sent += rv; |
| 329 | } |
| 330 | |
| 331 | } |
| 332 | if (PREDICT_FALSE (utm->time_to_stop)) |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | pthread_exit (0); |
| 337 | } |
| 338 | |
| 339 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 340 | udp_client_connect (uri_udp_test_main_t * utm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 341 | { |
| 342 | vl_api_connect_uri_t *cmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 343 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 344 | memset (cmp, 0, sizeof (*cmp)); |
| 345 | |
| 346 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
| 347 | cmp->client_index = utm->my_client_index; |
| 348 | cmp->context = ntohl (0xfeedface); |
| 349 | memcpy (cmp->uri, utm->connect_uri, vec_len (utm->connect_uri)); |
| 350 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & cmp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 351 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 352 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 353 | static void |
| 354 | client_send (uri_udp_test_main_t * utm, session_t * session) |
| 355 | { |
| 356 | int i; |
| 357 | u8 *test_data = 0; |
| 358 | u64 bytes_received = 0, bytes_sent = 0; |
| 359 | i32 bytes_to_read; |
| 360 | int rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 361 | f64 before, after, delta, bytes_per_second; |
| 362 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 363 | int buffer_offset, bytes_to_send = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 364 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 365 | /* |
| 366 | * Prepare test data |
| 367 | */ |
| 368 | vec_validate (test_data, 64 * 1024 - 1); |
| 369 | for (i = 0; i < vec_len (test_data); i++) |
| 370 | test_data[i] = i & 0xff; |
| 371 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 372 | rx_fifo = session->server_rx_fifo; |
| 373 | tx_fifo = session->server_tx_fifo; |
| 374 | |
| 375 | before = clib_time_now (&utm->clib_time); |
| 376 | |
| 377 | vec_validate (utm->rx_buf, vec_len (test_data) - 1); |
| 378 | |
| 379 | for (i = 0; i < NITER; i++) |
| 380 | { |
| 381 | bytes_to_send = vec_len (test_data); |
| 382 | buffer_offset = 0; |
| 383 | while (bytes_to_send > 0) |
| 384 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 385 | rv = svm_fifo_enqueue_nowait (tx_fifo, bytes_to_send, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 386 | test_data + buffer_offset); |
| 387 | |
| 388 | if (rv > 0) |
| 389 | { |
| 390 | bytes_to_send -= rv; |
| 391 | buffer_offset += rv; |
| 392 | bytes_sent += rv; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | bytes_to_read = svm_fifo_max_dequeue (rx_fifo); |
| 397 | |
| 398 | bytes_to_read = vec_len (utm->rx_buf) > bytes_to_read ? |
| 399 | bytes_to_read : vec_len (utm->rx_buf); |
| 400 | |
| 401 | buffer_offset = 0; |
| 402 | while (bytes_to_read > 0) |
| 403 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 404 | rv = svm_fifo_dequeue_nowait (rx_fifo, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 405 | bytes_to_read, |
| 406 | utm->rx_buf + buffer_offset); |
| 407 | if (rv > 0) |
| 408 | { |
| 409 | bytes_to_read -= rv; |
| 410 | buffer_offset += rv; |
| 411 | bytes_received += rv; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | while (bytes_received < bytes_sent) |
| 416 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 417 | rv = |
| 418 | svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), utm->rx_buf); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 419 | if (rv > 0) |
| 420 | { |
| 421 | #if CLIB_DEBUG > 0 |
| 422 | int j; |
| 423 | for (j = 0; j < rv; j++) |
| 424 | { |
| 425 | if (utm->rx_buf[j] != ((bytes_received + j) & 0xff)) |
| 426 | { |
| 427 | clib_warning ("error at byte %lld, 0x%x not 0x%x", |
| 428 | bytes_received + j, |
| 429 | utm->rx_buf[j], |
| 430 | ((bytes_received + j) & 0xff)); |
| 431 | } |
| 432 | } |
| 433 | #endif |
| 434 | bytes_received += (u64) rv; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | after = clib_time_now (&utm->clib_time); |
| 439 | delta = after - before; |
| 440 | bytes_per_second = 0.0; |
| 441 | |
| 442 | if (delta > 0.0) |
| 443 | bytes_per_second = (f64) bytes_received / delta; |
| 444 | |
| 445 | fformat (stdout, |
| 446 | "Done: %lld recv bytes in %.2f seconds, %.2f bytes/sec...\n\n", |
| 447 | bytes_received, delta, bytes_per_second); |
| 448 | fformat (stdout, |
| 449 | "Done: %lld sent bytes in %.2f seconds, %.2f bytes/sec...\n\n", |
| 450 | bytes_sent, delta, bytes_per_second); |
| 451 | fformat (stdout, |
| 452 | "client -> server -> client round trip: %.2f Gbit/sec \n\n", |
| 453 | (bytes_per_second * 8.0) / 1e9); |
| 454 | } |
| 455 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 456 | static void |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 457 | uri_udp_client_test (uri_udp_test_main_t * utm) |
| 458 | { |
| 459 | session_t *session; |
| 460 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 461 | application_send_attach (utm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 462 | udp_client_connect (utm); |
| 463 | |
| 464 | if (wait_for_state_change (utm, STATE_READY)) |
| 465 | { |
| 466 | clib_warning ("timeout waiting for STATE_READY"); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | /* Only works with cut through sessions */ |
| 471 | session = pool_elt_at_index (utm->sessions, utm->cut_through_session_index); |
| 472 | |
| 473 | client_send (utm, session); |
| 474 | application_detach (utm); |
| 475 | } |
| 476 | |
| 477 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 478 | vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp) |
| 479 | { |
| 480 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 481 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 482 | if (mp->retval) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 483 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 484 | clib_warning ("bind failed: %d", mp->retval); |
| 485 | utm->state = STATE_FAILED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 489 | utm->state = STATE_READY; |
| 490 | } |
| 491 | |
| 492 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 493 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 494 | { |
| 495 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 496 | int rv; |
| 497 | |
| 498 | a->segment_name = (char *) mp->segment_name; |
| 499 | a->segment_size = mp->segment_size; |
| 500 | /* Attach to the segment vpp created */ |
| 501 | rv = svm_fifo_segment_attach (a); |
| 502 | if (rv) |
| 503 | { |
| 504 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 505 | mp->segment_name); |
| 506 | return; |
| 507 | } |
| 508 | clib_warning ("Mapped new segment '%s' size %d", mp->segment_name, |
| 509 | mp->segment_size); |
| 510 | } |
| 511 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 512 | /** |
| 513 | * Acting as server for redirected connect requests |
| 514 | */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 515 | static void |
| 516 | vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) |
| 517 | { |
| 518 | u32 segment_index; |
| 519 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 520 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 521 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 522 | svm_fifo_segment_private_t *seg; |
| 523 | unix_shared_memory_queue_t *client_q; |
| 524 | vl_api_connect_uri_reply_t *rmp; |
| 525 | session_t *session; |
| 526 | int rv = 0; |
| 527 | |
| 528 | /* Create the segment */ |
| 529 | a->segment_name = (char *) format (0, "%d:segment%d%c", utm->my_pid, |
| 530 | utm->unique_segment_index++, 0); |
| 531 | a->segment_size = utm->configured_segment_size; |
| 532 | |
| 533 | rv = svm_fifo_segment_create (a); |
| 534 | if (rv) |
| 535 | { |
| 536 | clib_warning ("sm_fifo_segment_create ('%s') failed", a->segment_name); |
| 537 | rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 538 | goto send_reply; |
| 539 | } |
| 540 | |
| 541 | vec_add2 (utm->seg, seg, 1); |
| 542 | |
| 543 | segment_index = vec_len (sm->segments) - 1; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 544 | memcpy (seg, sm->segments + segment_index, sizeof (utm->seg[0])); |
| 545 | |
| 546 | pool_get (utm->sessions, session); |
| 547 | |
| 548 | /* |
| 549 | * By construction the master's idea of the rx fifo ends up in |
| 550 | * fsh->fifos[0], and the master's idea of the tx fifo ends up in |
| 551 | * fsh->fifos[1]. |
| 552 | */ |
| 553 | session->server_rx_fifo = svm_fifo_segment_alloc_fifo (utm->seg, |
| 554 | 128 * 1024); |
| 555 | ASSERT (session->server_rx_fifo); |
| 556 | |
| 557 | session->server_tx_fifo = svm_fifo_segment_alloc_fifo (utm->seg, |
| 558 | 128 * 1024); |
| 559 | ASSERT (session->server_tx_fifo); |
| 560 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 561 | session->server_rx_fifo->master_session_index = session - utm->sessions; |
| 562 | session->server_tx_fifo->master_session_index = session - utm->sessions; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 563 | utm->cut_through_session_index = session - utm->sessions; |
| 564 | |
| 565 | rv = pthread_create (&utm->cut_through_thread_handle, |
| 566 | NULL /*attr */ , cut_through_thread_fn, 0); |
| 567 | if (rv) |
| 568 | { |
| 569 | clib_warning ("pthread_create returned %d", rv); |
| 570 | rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 571 | } |
| 572 | |
| 573 | send_reply: |
| 574 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 575 | memset (rmp, 0, sizeof (*rmp)); |
| 576 | |
| 577 | rmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI_REPLY); |
| 578 | rmp->context = mp->context; |
| 579 | rmp->retval = ntohl (rv); |
| 580 | rmp->segment_name_length = vec_len (a->segment_name); |
| 581 | memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name)); |
| 582 | |
| 583 | vec_free (a->segment_name); |
| 584 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 585 | client_q = |
| 586 | uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 587 | vl_msg_api_send_shmem (client_q, (u8 *) & rmp); |
| 588 | } |
| 589 | |
| 590 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 591 | vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp) |
| 592 | { |
| 593 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 594 | |
| 595 | if (mp->retval != 0) |
| 596 | clib_warning ("returned %d", ntohl (mp->retval)); |
| 597 | |
| 598 | utm->state = STATE_START; |
| 599 | } |
| 600 | |
| 601 | static void |
| 602 | vl_api_accept_session_t_handler (vl_api_accept_session_t * mp) |
| 603 | { |
| 604 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 605 | vl_api_accept_session_reply_t *rmp; |
| 606 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 607 | session_t *session; |
| 608 | static f64 start_time; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 609 | |
| 610 | if (start_time == 0.0) |
| 611 | start_time = clib_time_now (&utm->clib_time); |
| 612 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 613 | utm->vpp_event_queue = |
| 614 | uword_to_pointer (mp->vpp_event_queue_address, |
| 615 | unix_shared_memory_queue_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 616 | |
| 617 | pool_get (utm->sessions, session); |
| 618 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 619 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 620 | rx_fifo->client_session_index = session - utm->sessions; |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 621 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 622 | tx_fifo->client_session_index = session - utm->sessions; |
| 623 | |
| 624 | session->server_rx_fifo = rx_fifo; |
| 625 | session->server_tx_fifo = tx_fifo; |
| 626 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 627 | hash_set (utm->session_index_by_vpp_handles, mp->handle, |
| 628 | session - utm->sessions); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 629 | |
| 630 | utm->state = STATE_READY; |
| 631 | |
| 632 | if (pool_elts (utm->sessions) && (pool_elts (utm->sessions) % 20000) == 0) |
| 633 | { |
| 634 | f64 now = clib_time_now (&utm->clib_time); |
| 635 | fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n", |
| 636 | pool_elts (utm->sessions), now - start_time, |
| 637 | (f64) pool_elts (utm->sessions) / (now - start_time)); |
| 638 | } |
| 639 | |
| 640 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 641 | memset (rmp, 0, sizeof (*rmp)); |
| 642 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 643 | rmp->handle = mp->handle; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 644 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp); |
| 645 | } |
| 646 | |
| 647 | static void |
| 648 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 649 | { |
| 650 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 651 | session_t *session; |
| 652 | vl_api_disconnect_session_reply_t *rmp; |
| 653 | uword *p; |
| 654 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 655 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 656 | p = hash_get (utm->session_index_by_vpp_handles, mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 657 | |
| 658 | if (p) |
| 659 | { |
| 660 | session = pool_elt_at_index (utm->sessions, p[0]); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 661 | hash_unset (utm->session_index_by_vpp_handles, mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 662 | pool_put (utm->sessions, session); |
| 663 | } |
| 664 | else |
| 665 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 666 | clib_warning ("couldn't find session key %llx", mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 667 | rv = -11; |
| 668 | } |
| 669 | |
| 670 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 671 | memset (rmp, 0, sizeof (*rmp)); |
| 672 | rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY); |
| 673 | rmp->retval = rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 674 | rmp->handle = mp->handle; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 675 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & rmp); |
| 676 | } |
| 677 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 678 | static void |
| 679 | vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp) |
| 680 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 681 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 682 | |
| 683 | ASSERT (utm->i_am_master == 0); |
| 684 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 685 | /* We've been redirected */ |
| 686 | if (mp->segment_name_length > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 687 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 688 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 689 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 690 | u32 segment_index; |
| 691 | session_t *session; |
| 692 | ssvm_shared_header_t *sh; |
| 693 | svm_fifo_segment_private_t *seg; |
| 694 | svm_fifo_segment_header_t *fsh; |
| 695 | int rv; |
| 696 | |
| 697 | memset (a, 0, sizeof (*a)); |
| 698 | a->segment_name = (char *) mp->segment_name; |
| 699 | |
| 700 | sleep (1); |
| 701 | |
| 702 | rv = svm_fifo_segment_attach (a); |
| 703 | if (rv) |
| 704 | { |
| 705 | clib_warning ("sm_fifo_segment_create ('%v') failed", |
| 706 | mp->segment_name); |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | segment_index = vec_len (sm->segments) - 1; |
| 711 | vec_add2 (utm->seg, seg, 1); |
| 712 | |
| 713 | memcpy (seg, sm->segments + segment_index, sizeof (*seg)); |
| 714 | sh = seg->ssvm.sh; |
| 715 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
| 716 | |
| 717 | while (vec_len (fsh->fifos) < 2) |
| 718 | sleep (1); |
| 719 | |
| 720 | pool_get (utm->sessions, session); |
| 721 | utm->cut_through_session_index = session - utm->sessions; |
| 722 | |
| 723 | session->server_rx_fifo = (svm_fifo_t *) fsh->fifos[0]; |
| 724 | ASSERT (session->server_rx_fifo); |
| 725 | session->server_tx_fifo = (svm_fifo_t *) fsh->fifos[1]; |
| 726 | ASSERT (session->server_tx_fifo); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 729 | /* security: could unlink /dev/shm/<mp->segment_name> here, maybe */ |
| 730 | |
| 731 | utm->state = STATE_READY; |
| 732 | } |
| 733 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 734 | #define foreach_uri_msg \ |
| 735 | _(BIND_URI_REPLY, bind_uri_reply) \ |
| 736 | _(CONNECT_URI, connect_uri) \ |
| 737 | _(CONNECT_URI_REPLY, connect_uri_reply) \ |
| 738 | _(UNBIND_URI_REPLY, unbind_uri_reply) \ |
| 739 | _(ACCEPT_SESSION, accept_session) \ |
| 740 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 741 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
| 742 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 743 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 744 | |
| 745 | void |
| 746 | uri_api_hookup (uri_udp_test_main_t * utm) |
| 747 | { |
| 748 | #define _(N,n) \ |
| 749 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 750 | vl_api_##n##_t_handler, \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 751 | vl_noop_handler, \ |
| 752 | vl_api_##n##_t_endian, \ |
| 753 | vl_api_##n##_t_print, \ |
| 754 | sizeof(vl_api_##n##_t), 1); |
| 755 | foreach_uri_msg; |
| 756 | #undef _ |
| 757 | |
| 758 | } |
| 759 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 760 | int |
| 761 | connect_to_vpp (char *name) |
| 762 | { |
| 763 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 764 | api_main_t *am = &api_main; |
| 765 | |
| 766 | if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0) |
| 767 | return -1; |
| 768 | |
| 769 | utm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 770 | utm->my_client_index = am->my_client_index; |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | void |
| 776 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 777 | { |
| 778 | clib_warning ("BUG"); |
| 779 | } |
| 780 | |
| 781 | static void |
| 782 | init_error_string_table (uri_udp_test_main_t * utm) |
| 783 | { |
| 784 | utm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 785 | |
| 786 | #define _(n,v,s) hash_set (utm->error_string_by_error_number, -v, s); |
| 787 | foreach_vnet_api_error; |
| 788 | #undef _ |
| 789 | |
| 790 | hash_set (utm->error_string_by_error_number, 99, "Misc"); |
| 791 | } |
| 792 | |
| 793 | void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 794 | server_handle_fifo_event_rx (uri_udp_test_main_t * utm, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 795 | session_fifo_event_t * e) |
| 796 | { |
| 797 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 798 | int nbytes; |
| 799 | |
| 800 | session_fifo_event_t evt; |
| 801 | unix_shared_memory_queue_t *q; |
| 802 | int rv; |
| 803 | |
| 804 | rx_fifo = e->fifo; |
| 805 | tx_fifo = utm->sessions[rx_fifo->client_session_index].server_tx_fifo; |
| 806 | |
| 807 | do |
| 808 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 809 | nbytes = svm_fifo_dequeue_nowait (rx_fifo, vec_len (utm->rx_buf), |
| 810 | utm->rx_buf); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 811 | } |
| 812 | while (nbytes <= 0); |
| 813 | do |
| 814 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 815 | rv = svm_fifo_enqueue_nowait (tx_fifo, nbytes, utm->rx_buf); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 816 | } |
| 817 | while (rv == -2); |
| 818 | |
| 819 | /* Fabricate TX event, send to vpp */ |
| 820 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 821 | evt.event_type = FIFO_EVENT_APP_TX; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 822 | evt.event_id = e->event_id; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 823 | |
| 824 | if (svm_fifo_set_event (tx_fifo)) |
| 825 | { |
| 826 | q = utm->vpp_event_queue; |
| 827 | unix_shared_memory_queue_add (q, (u8 *) & evt, |
| 828 | 0 /* do wait for mutex */ ); |
| 829 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 833 | server_handle_event_queue (uri_udp_test_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 834 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 835 | session_fifo_event_t _e, *e = &_e; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 836 | |
| 837 | while (1) |
| 838 | { |
| 839 | unix_shared_memory_queue_sub (utm->our_event_queue, (u8 *) e, |
| 840 | 0 /* nowait */ ); |
| 841 | switch (e->event_type) |
| 842 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 843 | case FIFO_EVENT_APP_RX: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 844 | server_handle_fifo_event_rx (utm, e); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 845 | break; |
| 846 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 847 | case FIFO_EVENT_DISCONNECT: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 848 | return; |
| 849 | |
| 850 | default: |
| 851 | clib_warning ("unknown event type %d", e->event_type); |
| 852 | break; |
| 853 | } |
| 854 | if (PREDICT_FALSE (utm->time_to_stop == 1)) |
| 855 | break; |
| 856 | if (PREDICT_FALSE (utm->time_to_print_stats == 1)) |
| 857 | { |
| 858 | utm->time_to_print_stats = 0; |
| 859 | fformat (stdout, "%d connections\n", pool_elts (utm->sessions)); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 864 | static void |
| 865 | server_unbind (uri_udp_test_main_t * utm) |
| 866 | { |
| 867 | vl_api_unbind_uri_t *ump; |
| 868 | |
| 869 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 870 | memset (ump, 0, sizeof (*ump)); |
| 871 | |
| 872 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI); |
| 873 | ump->client_index = utm->my_client_index; |
| 874 | memcpy (ump->uri, utm->uri, vec_len (utm->uri)); |
| 875 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & ump); |
| 876 | } |
| 877 | |
| 878 | static void |
| 879 | server_listen (uri_udp_test_main_t * utm) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 880 | { |
| 881 | vl_api_bind_uri_t *bmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 882 | |
| 883 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 884 | memset (bmp, 0, sizeof (*bmp)); |
| 885 | |
| 886 | bmp->_vl_msg_id = ntohs (VL_API_BIND_URI); |
| 887 | bmp->client_index = utm->my_client_index; |
| 888 | bmp->context = ntohl (0xfeedface); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 889 | memcpy (bmp->uri, utm->uri, vec_len (utm->uri)); |
| 890 | vl_msg_api_send_shmem (utm->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | void |
| 894 | udp_server_test (uri_udp_test_main_t * utm) |
| 895 | { |
| 896 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 897 | application_send_attach (utm); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 898 | |
| 899 | /* Bind to uri */ |
| 900 | server_listen (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 901 | |
| 902 | if (wait_for_state_change (utm, STATE_READY)) |
| 903 | { |
| 904 | clib_warning ("timeout waiting for STATE_READY"); |
| 905 | return; |
| 906 | } |
| 907 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 908 | server_handle_event_queue (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 909 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 910 | /* Cleanup */ |
| 911 | server_unbind (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 912 | |
| 913 | if (wait_for_state_change (utm, STATE_START)) |
| 914 | { |
| 915 | clib_warning ("timeout waiting for STATE_START"); |
| 916 | return; |
| 917 | } |
| 918 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 919 | application_detach (utm); |
| 920 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 921 | fformat (stdout, "Test complete...\n"); |
| 922 | } |
| 923 | |
| 924 | int |
| 925 | main (int argc, char **argv) |
| 926 | { |
| 927 | uri_udp_test_main_t *utm = &uri_udp_test_main; |
| 928 | unformat_input_t _argv, *a = &_argv; |
| 929 | u8 *chroot_prefix; |
| 930 | u8 *heap; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 931 | u8 *bind_name = (u8 *) "udp://0.0.0.0/1234"; |
| 932 | u32 tmp; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 933 | mheap_t *h; |
| 934 | session_t *session; |
| 935 | int i; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 936 | int i_am_master = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 937 | |
| 938 | clib_mem_init (0, 256 << 20); |
| 939 | |
| 940 | heap = clib_mem_get_per_cpu_heap (); |
| 941 | h = mheap_header (heap); |
| 942 | |
| 943 | /* make the main heap thread-safe */ |
| 944 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 945 | |
| 946 | vec_validate (utm->rx_buf, 8192); |
| 947 | |
| 948 | utm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
| 949 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 950 | utm->my_pid = getpid (); |
| 951 | utm->configured_segment_size = 1 << 20; |
| 952 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 953 | clib_time_init (&utm->clib_time); |
| 954 | init_error_string_table (utm); |
| 955 | svm_fifo_segment_init (0x200000000ULL, 20); |
| 956 | unformat_init_command_line (a, argv); |
| 957 | |
| 958 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 959 | { |
| 960 | if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 961 | { |
| 962 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 963 | } |
| 964 | else if (unformat (a, "uri %s", &bind_name)) |
| 965 | ; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 966 | else if (unformat (a, "segment-size %dM", &tmp)) |
| 967 | utm->configured_segment_size = tmp << 20; |
| 968 | else if (unformat (a, "segment-size %dG", &tmp)) |
| 969 | utm->configured_segment_size = tmp << 30; |
| 970 | else if (unformat (a, "master")) |
| 971 | i_am_master = 1; |
| 972 | else if (unformat (a, "slave")) |
| 973 | i_am_master = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 974 | else |
| 975 | { |
| 976 | fformat (stderr, "%s: usage [master|slave]\n"); |
| 977 | exit (1); |
| 978 | } |
| 979 | } |
| 980 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 981 | utm->cut_through_session_index = ~0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 982 | utm->uri = format (0, "%s%c", bind_name, 0); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 983 | utm->i_am_master = i_am_master; |
| 984 | utm->segment_main = &svm_fifo_segment_main; |
| 985 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 986 | utm->connect_uri = format (0, "udp://6.0.0.1/1234%c", 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 987 | |
| 988 | setup_signal_handlers (); |
| 989 | |
| 990 | uri_api_hookup (utm); |
| 991 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 992 | if (connect_to_vpp (i_am_master ? "uri_udp_master" : "uri_udp_slave") < 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 993 | { |
| 994 | svm_region_exit (); |
| 995 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 996 | exit (1); |
| 997 | } |
| 998 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 999 | if (i_am_master == 0) |
| 1000 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1001 | uri_udp_client_test (utm); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1002 | exit (0); |
| 1003 | } |
| 1004 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1005 | /* $$$$ hack preallocation */ |
| 1006 | for (i = 0; i < 200000; i++) |
| 1007 | { |
| 1008 | pool_get (utm->sessions, session); |
| 1009 | memset (session, 0, sizeof (*session)); |
| 1010 | } |
| 1011 | for (i = 0; i < 200000; i++) |
| 1012 | pool_put_index (utm->sessions, i); |
| 1013 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1014 | udp_server_test (utm); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1015 | |
| 1016 | vl_client_disconnect_from_vlib (); |
| 1017 | exit (0); |
| 1018 | } |
| 1019 | |
| 1020 | #undef vl_api_version |
| 1021 | #define vl_api_version(n,v) static u32 vpe_api_version = v; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1022 | #include <vpp/api/vpe.api.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1023 | #undef vl_api_version |
| 1024 | |
| 1025 | void |
| 1026 | vl_client_add_api_signatures (vl_api_memclnt_create_t * mp) |
| 1027 | { |
| 1028 | /* |
| 1029 | * Send the main API signature in slot 0. This bit of code must |
| 1030 | * match the checks in ../vpe/api/api.c: vl_msg_api_version_check(). |
| 1031 | */ |
| 1032 | mp->api_versions[0] = clib_host_to_net_u32 (vpe_api_version); |
| 1033 | } |
| 1034 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1035 | u32 |
| 1036 | vl (void *p) |
| 1037 | { |
| 1038 | return vec_len (p); |
| 1039 | } |
| 1040 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1041 | /* |
| 1042 | * fd.io coding-style-patch-verification: ON |
| 1043 | * |
| 1044 | * Local Variables: |
| 1045 | * eval: (c-set-style "gnu") |
| 1046 | * End: |
| 1047 | */ |