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