Nathan Skrzypczak | 60f3e65 | 2019-03-19 13:57:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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> |
| 18 | |
| 19 | #include <vnet/session/application_interface.h> |
| 20 | #include <svm/svm_fifo_segment.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include <vpp/api/vpe_msg_enum.h> |
| 24 | |
| 25 | #define vl_typedefs /* define message structures */ |
| 26 | #include <vpp/api/vpe_all_api_h.h> |
| 27 | #undef vl_typedefs |
| 28 | |
| 29 | /* declare message handlers for each api */ |
| 30 | |
| 31 | #define vl_endianfun /* define message structures */ |
| 32 | #include <vpp/api/vpe_all_api_h.h> |
| 33 | #undef vl_endianfun |
| 34 | |
| 35 | /* instantiate all the print functions we know about */ |
| 36 | #define vl_print(handle, ...) |
| 37 | #define vl_printfun |
| 38 | #include <vpp/api/vpe_all_api_h.h> |
| 39 | #undef vl_printfun |
| 40 | |
| 41 | #define TCP_ECHO_DBG 0 |
| 42 | #define DBG(_fmt,_args...) \ |
| 43 | if (TCP_ECHO_DBG) \ |
| 44 | clib_warning (_fmt, _args) |
| 45 | |
| 46 | typedef struct |
| 47 | { |
| 48 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 49 | #define _(type, name) type name; |
| 50 | foreach_app_session_field |
| 51 | #undef _ |
| 52 | u64 vpp_session_handle; |
| 53 | u64 bytes_sent; |
| 54 | u64 bytes_to_send; |
| 55 | volatile u64 bytes_received; |
| 56 | volatile u64 bytes_to_receive; |
| 57 | f64 start; |
| 58 | } echo_session_t; |
| 59 | |
| 60 | typedef enum |
| 61 | { |
| 62 | STATE_START, |
| 63 | STATE_ATTACHED, |
| 64 | STATE_LISTEN, |
| 65 | STATE_READY, |
| 66 | STATE_DISCONNECTING, |
| 67 | STATE_FAILED, |
| 68 | STATE_DETACHED |
| 69 | } connection_state_t; |
| 70 | |
| 71 | typedef struct |
| 72 | { |
| 73 | /* vpe input queue */ |
| 74 | svm_queue_t *vl_input_queue; |
| 75 | |
| 76 | /* API client handle */ |
| 77 | u32 my_client_index; |
| 78 | |
| 79 | /* The URI we're playing with */ |
| 80 | u8 *uri; |
| 81 | |
| 82 | /* Session pool */ |
| 83 | echo_session_t *sessions; |
| 84 | |
| 85 | /* Hash table for disconnect processing */ |
| 86 | uword *session_index_by_vpp_handles; |
| 87 | |
| 88 | /* intermediate rx buffer */ |
| 89 | u8 *rx_buf; |
| 90 | |
| 91 | /* URI for slave's connect */ |
| 92 | u8 *connect_uri; |
| 93 | |
| 94 | u32 connected_session_index; |
| 95 | |
| 96 | int i_am_master; |
| 97 | |
| 98 | /* drop all packets */ |
| 99 | int no_return; |
| 100 | |
| 101 | /* Our event queue */ |
| 102 | svm_msg_q_t *our_event_queue; |
| 103 | |
| 104 | u8 *socket_name; |
| 105 | |
| 106 | pid_t my_pid; |
| 107 | |
| 108 | /* For deadman timers */ |
| 109 | clib_time_t clib_time; |
| 110 | |
| 111 | /* State of the connection, shared between msg RX thread and main thread */ |
| 112 | volatile connection_state_t state; |
| 113 | |
| 114 | /* Signal variables */ |
| 115 | volatile int time_to_stop; |
| 116 | volatile int time_to_print_stats; |
| 117 | |
| 118 | u32 configured_segment_size; |
| 119 | |
| 120 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
| 121 | uword *error_string_by_error_number; |
| 122 | |
| 123 | u8 *connect_test_data; |
| 124 | pthread_t *client_thread_handles; |
| 125 | u32 *thread_args; |
| 126 | u32 client_bytes_received; |
| 127 | u8 test_return_packets; |
| 128 | u64 bytes_to_send; |
| 129 | u32 fifo_size; |
| 130 | |
| 131 | u32 n_clients; |
| 132 | u64 tx_total; |
| 133 | u64 rx_total; |
| 134 | |
| 135 | volatile u32 n_clients_connected; |
| 136 | volatile u32 n_active_clients; |
| 137 | |
| 138 | |
| 139 | /** Flag that decides if socket, instead of svm, api is used to connect to |
| 140 | * vpp. If sock api is used, shm binary api is subsequently bootstrapped |
| 141 | * and all other messages are exchanged using shm IPC. */ |
| 142 | u8 use_sock_api; |
| 143 | |
| 144 | svm_fifo_segment_main_t segment_main; |
| 145 | } echo_main_t; |
| 146 | |
| 147 | echo_main_t echo_main; |
| 148 | |
| 149 | #if CLIB_DEBUG > 0 |
| 150 | #define NITER 10000 |
| 151 | #else |
| 152 | #define NITER 4000000 |
| 153 | #endif |
| 154 | |
| 155 | static u8 * |
| 156 | format_api_error (u8 * s, va_list * args) |
| 157 | { |
| 158 | echo_main_t *em = &echo_main; |
| 159 | i32 error = va_arg (*args, u32); |
| 160 | uword *p; |
| 161 | |
| 162 | p = hash_get (em->error_string_by_error_number, -error); |
| 163 | |
| 164 | if (p) |
| 165 | s = format (s, "%s", p[0]); |
| 166 | else |
| 167 | s = format (s, "%d", error); |
| 168 | return s; |
| 169 | } |
| 170 | |
| 171 | static void |
| 172 | init_error_string_table (echo_main_t * em) |
| 173 | { |
| 174 | em->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 175 | |
| 176 | #define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s); |
| 177 | foreach_vnet_api_error; |
| 178 | #undef _ |
| 179 | |
| 180 | hash_set (em->error_string_by_error_number, 99, "Misc"); |
| 181 | } |
| 182 | |
| 183 | static void handle_mq_event (session_event_t * e); |
| 184 | |
| 185 | static int |
| 186 | wait_for_state_change (echo_main_t * em, connection_state_t state) |
| 187 | { |
| 188 | svm_msg_q_msg_t msg; |
| 189 | session_event_t *e; |
| 190 | f64 timeout; |
| 191 | |
| 192 | #if CLIB_DEBUG > 0 |
| 193 | #define TIMEOUT 600.0 |
| 194 | #else |
| 195 | #define TIMEOUT 600.0 |
| 196 | #endif |
| 197 | |
| 198 | timeout = clib_time_now (&em->clib_time) + TIMEOUT; |
| 199 | |
| 200 | while (clib_time_now (&em->clib_time) < timeout) |
| 201 | { |
| 202 | if (em->state == state) |
| 203 | return 0; |
| 204 | if (em->state == STATE_FAILED) |
| 205 | return -1; |
| 206 | if (em->time_to_stop == 1) |
| 207 | return 0; |
| 208 | if (!em->our_event_queue || em->state < STATE_ATTACHED) |
| 209 | continue; |
| 210 | |
| 211 | if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_NOWAIT, 0)) |
| 212 | continue; |
| 213 | e = svm_msg_q_msg_data (em->our_event_queue, &msg); |
| 214 | handle_mq_event (e); |
| 215 | svm_msg_q_free_msg (em->our_event_queue, &msg); |
| 216 | } |
| 217 | clib_warning ("timeout waiting for state %d", state); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | application_send_attach (echo_main_t * em) |
| 223 | { |
| 224 | vl_api_application_attach_t *bmp; |
| 225 | vl_api_application_tls_cert_add_t *cert_mp; |
| 226 | vl_api_application_tls_key_add_t *key_mp; |
| 227 | |
| 228 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 229 | clib_memset (bmp, 0, sizeof (*bmp)); |
| 230 | |
| 231 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 232 | bmp->client_index = em->my_client_index; |
| 233 | bmp->context = ntohl (0xfeedface); |
| 234 | bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT; |
| 235 | bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT; |
| 236 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16; |
| 237 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size; |
| 238 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size; |
| 239 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20; |
| 240 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20; |
| 241 | bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = 256; |
| 242 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp); |
| 243 | |
| 244 | cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len); |
| 245 | clib_memset (cert_mp, 0, sizeof (*cert_mp)); |
| 246 | cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD); |
| 247 | cert_mp->client_index = em->my_client_index; |
| 248 | cert_mp->context = ntohl (0xfeedface); |
| 249 | cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len); |
| 250 | clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len); |
| 251 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp); |
| 252 | |
| 253 | key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len); |
| 254 | clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len); |
| 255 | key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD); |
| 256 | key_mp->client_index = em->my_client_index; |
| 257 | key_mp->context = ntohl (0xfeedface); |
| 258 | key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len); |
| 259 | clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len); |
| 260 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp); |
| 261 | } |
| 262 | |
| 263 | static int |
| 264 | application_attach (echo_main_t * em) |
| 265 | { |
| 266 | application_send_attach (em); |
| 267 | if (wait_for_state_change (em, STATE_ATTACHED)) |
| 268 | { |
| 269 | clib_warning ("timeout waiting for STATE_ATTACHED"); |
| 270 | return -1; |
| 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | void |
| 276 | application_detach (echo_main_t * em) |
| 277 | { |
| 278 | vl_api_application_detach_t *bmp; |
| 279 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 280 | clib_memset (bmp, 0, sizeof (*bmp)); |
| 281 | |
| 282 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 283 | bmp->client_index = em->my_client_index; |
| 284 | bmp->context = ntohl (0xfeedface); |
| 285 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp); |
| 286 | |
| 287 | DBG ("%s", "Sent detach"); |
| 288 | } |
| 289 | |
| 290 | static int |
| 291 | ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd) |
| 292 | { |
| 293 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 294 | svm_fifo_segment_main_t *sm = &echo_main.segment_main; |
| 295 | int rv; |
| 296 | |
| 297 | clib_memset (a, 0, sizeof (*a)); |
| 298 | a->segment_name = (char *) name; |
| 299 | a->segment_type = type; |
| 300 | |
| 301 | if (type == SSVM_SEGMENT_MEMFD) |
| 302 | a->memfd_fd = fd; |
| 303 | |
| 304 | if ((rv = svm_fifo_segment_attach (sm, a))) |
| 305 | { |
| 306 | clib_warning ("svm_fifo_segment_attach ('%s') failed", name); |
| 307 | return rv; |
| 308 | } |
| 309 | |
| 310 | vec_reset_length (a->new_segment_indices); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 316 | mp) |
| 317 | { |
| 318 | echo_main_t *em = &echo_main; |
| 319 | int *fds = 0; |
| 320 | u32 n_fds = 0; |
| 321 | |
| 322 | if (mp->retval) |
| 323 | { |
| 324 | clib_warning ("attach failed: %U", format_api_error, |
| 325 | clib_net_to_host_u32 (mp->retval)); |
| 326 | goto failed; |
| 327 | } |
| 328 | |
| 329 | if (mp->segment_name_length == 0) |
| 330 | { |
| 331 | clib_warning ("segment_name_length zero"); |
| 332 | goto failed; |
| 333 | } |
| 334 | |
| 335 | ASSERT (mp->app_event_queue_address); |
| 336 | em->our_event_queue = uword_to_pointer (mp->app_event_queue_address, |
| 337 | svm_msg_q_t *); |
| 338 | |
| 339 | if (mp->n_fds) |
| 340 | { |
| 341 | vec_validate (fds, mp->n_fds); |
| 342 | vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5); |
| 343 | |
| 344 | if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT) |
| 345 | if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
| 346 | goto failed; |
| 347 | |
| 348 | if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT) |
| 349 | if (ssvm_segment_attach ((char *) mp->segment_name, |
| 350 | SSVM_SEGMENT_MEMFD, fds[n_fds++])) |
| 351 | goto failed; |
| 352 | |
| 353 | if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD) |
| 354 | svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]); |
| 355 | |
| 356 | vec_free (fds); |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM, |
| 361 | -1)) |
| 362 | goto failed; |
| 363 | } |
| 364 | |
| 365 | em->state = STATE_ATTACHED; |
| 366 | return; |
| 367 | failed: |
| 368 | em->state = STATE_FAILED; |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | static void |
| 373 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 374 | mp) |
| 375 | { |
| 376 | if (mp->retval) |
| 377 | clib_warning ("detach returned with err: %d", mp->retval); |
| 378 | echo_main.state = STATE_DETACHED; |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | stop_signal (int signum) |
| 383 | { |
| 384 | echo_main_t *um = &echo_main; |
| 385 | um->time_to_stop = 1; |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | stats_signal (int signum) |
| 390 | { |
| 391 | echo_main_t *um = &echo_main; |
| 392 | um->time_to_print_stats = 1; |
| 393 | } |
| 394 | |
| 395 | static clib_error_t * |
| 396 | setup_signal_handlers (void) |
| 397 | { |
| 398 | signal (SIGUSR2, stats_signal); |
| 399 | signal (SIGINT, stop_signal); |
| 400 | signal (SIGQUIT, stop_signal); |
| 401 | signal (SIGTERM, stop_signal); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 407 | { |
| 408 | clib_warning ("BUG"); |
| 409 | } |
| 410 | |
| 411 | int |
| 412 | connect_to_vpp (char *name) |
| 413 | { |
| 414 | echo_main_t *em = &echo_main; |
| 415 | api_main_t *am = &api_main; |
| 416 | |
| 417 | if (em->use_sock_api) |
| 418 | { |
| 419 | if (vl_socket_client_connect ((char *) em->socket_name, name, |
| 420 | 0 /* default rx, tx buffer */ )) |
| 421 | { |
| 422 | clib_warning ("socket connect failed"); |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | if (vl_socket_client_init_shm (0, 1 /* want_pthread */ )) |
| 427 | { |
| 428 | clib_warning ("init shm api failed"); |
| 429 | return -1; |
| 430 | } |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0) |
| 435 | { |
| 436 | clib_warning ("shmem connect failed"); |
| 437 | return -1; |
| 438 | } |
| 439 | } |
| 440 | em->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 441 | em->my_client_index = am->my_client_index; |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | void |
| 446 | disconnect_from_vpp (echo_main_t * em) |
| 447 | { |
| 448 | if (em->use_sock_api) |
| 449 | vl_socket_client_disconnect (); |
| 450 | else |
| 451 | vl_client_disconnect_from_vlib (); |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 456 | { |
| 457 | svm_fifo_segment_main_t *sm = &echo_main.segment_main; |
| 458 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 459 | int rv; |
| 460 | |
| 461 | clib_memset (a, 0, sizeof (*a)); |
| 462 | a->segment_name = (char *) mp->segment_name; |
| 463 | a->segment_size = mp->segment_size; |
| 464 | /* Attach to the segment vpp created */ |
| 465 | rv = svm_fifo_segment_attach (sm, a); |
| 466 | if (rv) |
| 467 | { |
| 468 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
| 469 | mp->segment_name); |
| 470 | return; |
| 471 | } |
| 472 | clib_warning ("Mapped new segment '%s' size %d", mp->segment_name, |
| 473 | mp->segment_size); |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | session_print_stats (echo_main_t * em, echo_session_t * session) |
| 478 | { |
| 479 | f64 deltat; |
| 480 | u64 bytes; |
| 481 | |
| 482 | deltat = clib_time_now (&em->clib_time) - session->start; |
| 483 | bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send; |
| 484 | fformat (stdout, "Finished in %.6f\n", deltat); |
| 485 | fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9); |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | test_recv_bytes (echo_session_t * s, u8 * rx_buf, u32 n_read) |
| 490 | { |
| 491 | int i; |
| 492 | for (i = 0; i < n_read; i++) |
| 493 | { |
| 494 | if (rx_buf[i] != ((s->bytes_received + i) & 0xff)) |
| 495 | { |
| 496 | clib_warning ("error at byte %lld, 0x%x not 0x%x", |
| 497 | s->bytes_received + i, rx_buf[i], |
| 498 | ((s->bytes_received + i) & 0xff)); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | recv_data_chunk (echo_main_t * em, echo_session_t * s, u8 * rx_buf) |
| 505 | { |
| 506 | int n_to_read, n_read; |
| 507 | |
| 508 | n_to_read = svm_fifo_max_dequeue (s->rx_fifo); |
| 509 | if (!n_to_read) |
| 510 | return; |
| 511 | |
| 512 | do |
| 513 | { |
| 514 | n_read = app_recv_stream ((app_session_t *) s, rx_buf, |
| 515 | vec_len (rx_buf)); |
| 516 | |
| 517 | if (n_read > 0) |
| 518 | { |
| 519 | if (em->test_return_packets) |
| 520 | test_recv_bytes (s, rx_buf, n_read); |
| 521 | |
| 522 | n_to_read -= n_read; |
| 523 | |
| 524 | s->bytes_received += n_read; |
| 525 | s->bytes_to_receive -= n_read; |
| 526 | } |
| 527 | else |
| 528 | break; |
| 529 | } |
| 530 | while (n_to_read > 0); |
| 531 | } |
| 532 | |
| 533 | static void |
| 534 | send_data_chunk (echo_main_t * em, echo_session_t * s) |
| 535 | { |
| 536 | u64 test_buf_len, bytes_this_chunk, test_buf_offset; |
| 537 | u8 *test_data = em->connect_test_data; |
| 538 | int n_sent; |
| 539 | |
| 540 | test_buf_len = vec_len (test_data); |
| 541 | test_buf_offset = s->bytes_sent % test_buf_len; |
| 542 | bytes_this_chunk = clib_min (test_buf_len - test_buf_offset, |
| 543 | s->bytes_to_send); |
| 544 | |
| 545 | n_sent = app_send_stream ((app_session_t *) s, test_data + test_buf_offset, |
| 546 | bytes_this_chunk, 0); |
| 547 | |
| 548 | if (n_sent > 0) |
| 549 | { |
| 550 | s->bytes_to_send -= n_sent; |
| 551 | s->bytes_sent += n_sent; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Rx/Tx polling thread per connection |
| 557 | */ |
| 558 | static void * |
| 559 | client_thread_fn (void *arg) |
| 560 | { |
| 561 | echo_main_t *em = &echo_main; |
| 562 | static u8 *rx_buf = 0; |
| 563 | u32 session_index = *(u32 *) arg; |
| 564 | echo_session_t *s; |
| 565 | |
| 566 | vec_validate (rx_buf, 1 << 20); |
| 567 | |
| 568 | while (!em->time_to_stop && em->state != STATE_READY) |
| 569 | ; |
| 570 | |
| 571 | s = pool_elt_at_index (em->sessions, session_index); |
| 572 | while (!em->time_to_stop) |
| 573 | { |
| 574 | send_data_chunk (em, s); |
| 575 | recv_data_chunk (em, s, rx_buf); |
| 576 | if (!s->bytes_to_send && !s->bytes_to_receive) |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | clib_warning ("GOT OUT"); |
| 581 | DBG ("session %d done", session_index); |
| 582 | em->tx_total += s->bytes_sent; |
| 583 | em->rx_total += s->bytes_received; |
| 584 | em->n_active_clients--; |
| 585 | |
| 586 | pthread_exit (0); |
| 587 | } |
| 588 | |
| 589 | void |
| 590 | client_send_connect (echo_main_t * em) |
| 591 | { |
| 592 | vl_api_connect_uri_t *cmp; |
| 593 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 594 | clib_memset (cmp, 0, sizeof (*cmp)); |
| 595 | |
| 596 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
| 597 | cmp->client_index = em->my_client_index; |
| 598 | cmp->context = ntohl (0xfeedface); |
| 599 | memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri)); |
| 600 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp); |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | client_send_disconnect (echo_main_t * em, echo_session_t * s) |
| 605 | { |
| 606 | vl_api_disconnect_session_t *dmp; |
| 607 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 608 | clib_memset (dmp, 0, sizeof (*dmp)); |
| 609 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 610 | dmp->client_index = em->my_client_index; |
| 611 | dmp->handle = s->vpp_session_handle; |
| 612 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp); |
| 613 | } |
| 614 | |
| 615 | int |
| 616 | client_disconnect (echo_main_t * em, echo_session_t * s) |
| 617 | { |
| 618 | client_send_disconnect (em, s); |
| 619 | pool_put (em->sessions, s); |
| 620 | clib_memset (s, 0xfe, sizeof (*s)); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static void |
| 625 | session_bound_handler (session_bound_msg_t * mp) |
| 626 | { |
| 627 | echo_main_t *em = &echo_main; |
| 628 | |
| 629 | if (mp->retval) |
| 630 | { |
| 631 | clib_warning ("bind failed: %U", format_api_error, |
| 632 | clib_net_to_host_u32 (mp->retval)); |
| 633 | em->state = STATE_FAILED; |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | clib_warning ("listening on %U:%u", format_ip46_address, mp->lcl_ip, |
| 638 | mp->lcl_is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6, mp->lcl_port); |
| 639 | em->state = STATE_READY; |
| 640 | } |
| 641 | |
| 642 | static void |
| 643 | session_accepted_handler (session_accepted_msg_t * mp) |
| 644 | { |
| 645 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 646 | session_accepted_reply_msg_t *rmp; |
| 647 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 648 | echo_main_t *em = &echo_main; |
| 649 | echo_session_t *session; |
| 650 | static f64 start_time; |
| 651 | u32 session_index; |
| 652 | u8 *ip_str; |
| 653 | |
| 654 | if (start_time == 0.0) |
| 655 | start_time = clib_time_now (&em->clib_time); |
| 656 | |
| 657 | ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4); |
| 658 | clib_warning ("Accepted session from: %s:%d", ip_str, |
| 659 | clib_net_to_host_u16 (mp->port)); |
| 660 | |
| 661 | /* Allocate local session and set it up */ |
| 662 | pool_get (em->sessions, session); |
| 663 | session_index = session - em->sessions; |
| 664 | |
| 665 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 666 | rx_fifo->client_session_index = session_index; |
| 667 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 668 | tx_fifo->client_session_index = session_index; |
| 669 | |
| 670 | session->rx_fifo = rx_fifo; |
| 671 | session->tx_fifo = tx_fifo; |
| 672 | session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, |
| 673 | svm_msg_q_t *); |
| 674 | |
| 675 | /* Add it to lookup table */ |
| 676 | hash_set (em->session_index_by_vpp_handles, mp->handle, session_index); |
| 677 | |
| 678 | em->state = STATE_READY; |
| 679 | |
| 680 | /* Stats printing */ |
| 681 | if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0) |
| 682 | { |
| 683 | f64 now = clib_time_now (&em->clib_time); |
| 684 | fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n", |
| 685 | pool_elts (em->sessions), now - start_time, |
| 686 | (f64) pool_elts (em->sessions) / (now - start_time)); |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Send accept reply to vpp |
| 691 | */ |
| 692 | app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt, |
| 693 | SESSION_CTRL_EVT_ACCEPTED_REPLY); |
| 694 | rmp = (session_accepted_reply_msg_t *) app_evt->evt->data; |
| 695 | rmp->handle = mp->handle; |
| 696 | rmp->context = mp->context; |
| 697 | app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt); |
| 698 | |
| 699 | session->bytes_received = 0; |
| 700 | session->start = clib_time_now (&em->clib_time); |
| 701 | } |
| 702 | |
| 703 | static void |
| 704 | session_connected_handler (session_connected_msg_t * mp) |
| 705 | { |
| 706 | echo_main_t *em = &echo_main; |
| 707 | echo_session_t *session; |
| 708 | u32 session_index; |
| 709 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 710 | int rv; |
| 711 | |
| 712 | if (mp->retval) |
| 713 | { |
| 714 | clib_warning ("connection failed with code: %U", format_api_error, |
| 715 | clib_net_to_host_u32 (mp->retval)); |
| 716 | em->state = STATE_FAILED; |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Setup session |
| 722 | */ |
| 723 | |
| 724 | pool_get (em->sessions, session); |
| 725 | clib_memset (session, 0, sizeof (*session)); |
| 726 | session_index = session - em->sessions; |
| 727 | |
| 728 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 729 | rx_fifo->client_session_index = session_index; |
| 730 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 731 | tx_fifo->client_session_index = session_index; |
| 732 | |
| 733 | session->rx_fifo = rx_fifo; |
| 734 | session->tx_fifo = tx_fifo; |
| 735 | session->vpp_session_handle = mp->handle; |
| 736 | session->start = clib_time_now (&em->clib_time); |
| 737 | session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address, |
| 738 | svm_msg_q_t *); |
| 739 | |
| 740 | hash_set (em->session_index_by_vpp_handles, mp->handle, session_index); |
| 741 | |
| 742 | /* |
| 743 | * Start RX thread |
| 744 | */ |
| 745 | em->thread_args[em->n_clients_connected] = session_index; |
| 746 | rv = pthread_create (&em->client_thread_handles[em->n_clients_connected], |
| 747 | NULL /*attr */ , client_thread_fn, |
| 748 | (void *) &em->thread_args[em->n_clients_connected]); |
| 749 | if (rv) |
| 750 | { |
| 751 | clib_warning ("pthread_create returned %d", rv); |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | em->n_clients_connected += 1; |
| 756 | clib_warning ("session %u (0x%llx) connected with local ip %U port %d", |
| 757 | session_index, mp->handle, format_ip46_address, mp->lcl_ip, |
| 758 | mp->is_ip4, clib_net_to_host_u16 (mp->lcl_port)); |
| 759 | } |
| 760 | |
| 761 | static void |
| 762 | session_disconnected_handler (session_disconnected_msg_t * mp) |
| 763 | { |
| 764 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 765 | session_disconnected_reply_msg_t *rmp; |
| 766 | echo_main_t *em = &echo_main; |
| 767 | echo_session_t *session = 0; |
| 768 | uword *p; |
| 769 | int rv = 0; |
| 770 | |
| 771 | p = hash_get (em->session_index_by_vpp_handles, mp->handle); |
| 772 | if (!p) |
| 773 | { |
| 774 | clib_warning ("couldn't find session key %llx", mp->handle); |
| 775 | return; |
| 776 | } |
| 777 | |
| 778 | session = pool_elt_at_index (em->sessions, p[0]); |
| 779 | hash_unset (em->session_index_by_vpp_handles, mp->handle); |
| 780 | pool_put (em->sessions, session); |
| 781 | |
| 782 | app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt, |
| 783 | SESSION_CTRL_EVT_DISCONNECTED_REPLY); |
| 784 | rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data; |
| 785 | rmp->retval = rv; |
| 786 | rmp->handle = mp->handle; |
| 787 | rmp->context = mp->context; |
| 788 | app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt); |
| 789 | |
| 790 | session_print_stats (em, session); |
| 791 | } |
| 792 | |
| 793 | static void |
| 794 | session_reset_handler (session_reset_msg_t * mp) |
| 795 | { |
| 796 | app_session_evt_t _app_evt, *app_evt = &_app_evt; |
| 797 | echo_main_t *em = &echo_main; |
| 798 | session_reset_reply_msg_t *rmp; |
| 799 | echo_session_t *session = 0; |
| 800 | uword *p; |
| 801 | int rv = 0; |
| 802 | |
| 803 | p = hash_get (em->session_index_by_vpp_handles, mp->handle); |
| 804 | |
| 805 | if (p) |
| 806 | { |
| 807 | session = pool_elt_at_index (em->sessions, p[0]); |
| 808 | clib_warning ("got reset"); |
| 809 | /* Cleanup later */ |
| 810 | em->time_to_stop = 1; |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | clib_warning ("couldn't find session key %llx", mp->handle); |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | app_alloc_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt, |
| 819 | SESSION_CTRL_EVT_RESET_REPLY); |
| 820 | rmp = (session_reset_reply_msg_t *) app_evt->evt->data; |
| 821 | rmp->retval = rv; |
| 822 | rmp->handle = mp->handle; |
| 823 | app_send_ctrl_evt_to_vpp (session->vpp_evt_q, app_evt); |
| 824 | } |
| 825 | |
| 826 | static void |
| 827 | handle_mq_event (session_event_t * e) |
| 828 | { |
| 829 | switch (e->event_type) |
| 830 | { |
| 831 | case SESSION_CTRL_EVT_BOUND: |
| 832 | session_bound_handler ((session_bound_msg_t *) e->data); |
| 833 | break; |
| 834 | case SESSION_CTRL_EVT_ACCEPTED: |
| 835 | session_accepted_handler ((session_accepted_msg_t *) e->data); |
| 836 | break; |
| 837 | case SESSION_CTRL_EVT_CONNECTED: |
| 838 | session_connected_handler ((session_connected_msg_t *) e->data); |
| 839 | break; |
| 840 | case SESSION_CTRL_EVT_DISCONNECTED: |
| 841 | session_disconnected_handler ((session_disconnected_msg_t *) e->data); |
| 842 | break; |
| 843 | case SESSION_CTRL_EVT_RESET: |
| 844 | session_reset_handler ((session_reset_msg_t *) e->data); |
| 845 | break; |
| 846 | default: |
| 847 | clib_warning ("unhandled %u", e->event_type); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | static void |
| 852 | clients_run (echo_main_t * em) |
| 853 | { |
| 854 | f64 start_time, deltat, timeout = 100.0; |
| 855 | svm_msg_q_msg_t msg; |
| 856 | session_event_t *e; |
| 857 | echo_session_t *s; |
| 858 | int i; |
| 859 | |
| 860 | /* Init test data */ |
| 861 | vec_validate (em->connect_test_data, 1024 * 1024 - 1); |
| 862 | for (i = 0; i < vec_len (em->connect_test_data); i++) |
| 863 | em->connect_test_data[i] = i & 0xff; |
| 864 | |
| 865 | /* |
| 866 | * Attach and connect the clients |
| 867 | */ |
| 868 | if (application_attach (em)) |
| 869 | return; |
| 870 | |
| 871 | for (i = 0; i < em->n_clients; i++) |
| 872 | client_send_connect (em); |
| 873 | |
| 874 | start_time = clib_time_now (&em->clib_time); |
| 875 | while (em->n_clients_connected < em->n_clients |
| 876 | && (clib_time_now (&em->clib_time) - start_time < timeout) |
| 877 | && em->state != STATE_FAILED && em->time_to_stop != 1) |
| 878 | |
| 879 | { |
| 880 | int rc = svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_TIMEDWAIT, 1); |
| 881 | if (rc == ETIMEDOUT && em->time_to_stop) |
| 882 | break; |
| 883 | if (rc == ETIMEDOUT) |
| 884 | continue; |
| 885 | e = svm_msg_q_msg_data (em->our_event_queue, &msg); |
| 886 | handle_mq_event (e); |
| 887 | svm_msg_q_free_msg (em->our_event_queue, &msg); |
| 888 | } |
| 889 | |
| 890 | if (em->n_clients_connected != em->n_clients) |
| 891 | { |
| 892 | clib_warning ("failed to initialize all connections"); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Initialize connections |
| 898 | */ |
| 899 | for (i = 0; i < em->n_clients; i++) |
| 900 | { |
| 901 | s = pool_elt_at_index (em->sessions, i); |
| 902 | s->bytes_to_send = em->bytes_to_send; |
| 903 | if (!em->no_return) |
| 904 | s->bytes_to_receive = em->bytes_to_send; |
| 905 | } |
| 906 | em->n_active_clients = em->n_clients_connected; |
| 907 | |
| 908 | /* |
| 909 | * Wait for client threads to send the data |
| 910 | */ |
| 911 | start_time = clib_time_now (&em->clib_time); |
| 912 | em->state = STATE_READY; |
| 913 | while (em->n_active_clients) |
| 914 | if (!svm_msg_q_is_empty (em->our_event_queue)) |
| 915 | { |
| 916 | if (svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_TIMEDWAIT, 0)) |
| 917 | { |
| 918 | clib_warning ("svm msg q returned"); |
| 919 | continue; |
| 920 | } |
| 921 | e = svm_msg_q_msg_data (em->our_event_queue, &msg); |
| 922 | if (e->event_type != FIFO_EVENT_APP_RX) |
| 923 | handle_mq_event (e); |
| 924 | svm_msg_q_free_msg (em->our_event_queue, &msg); |
| 925 | } |
| 926 | |
| 927 | for (i = 0; i < em->n_clients; i++) |
| 928 | { |
| 929 | s = pool_elt_at_index (em->sessions, i); |
| 930 | client_disconnect (em, s); |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * Stats and detach |
| 935 | */ |
| 936 | deltat = clib_time_now (&em->clib_time) - start_time; |
| 937 | fformat (stdout, "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds\n", |
| 938 | em->tx_total, em->tx_total / (1ULL << 20), |
| 939 | em->tx_total / (1ULL << 30), deltat); |
| 940 | fformat (stdout, "%.4f Gbit/second\n", (em->tx_total * 8.0) / deltat / 1e9); |
| 941 | |
| 942 | application_detach (em); |
| 943 | } |
| 944 | |
| 945 | static void |
| 946 | vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp) |
| 947 | { |
| 948 | echo_main_t *em = &echo_main; |
| 949 | |
| 950 | if (mp->retval) |
| 951 | { |
| 952 | clib_warning ("bind failed: %U", format_api_error, |
| 953 | clib_net_to_host_u32 (mp->retval)); |
| 954 | em->state = STATE_FAILED; |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | em->state = STATE_READY; |
| 959 | } |
| 960 | |
| 961 | static void |
| 962 | vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp) |
| 963 | { |
| 964 | echo_main_t *em = &echo_main; |
| 965 | |
| 966 | if (mp->retval != 0) |
| 967 | clib_warning ("returned %d", ntohl (mp->retval)); |
| 968 | |
| 969 | em->state = STATE_START; |
| 970 | } |
| 971 | |
| 972 | u8 * |
| 973 | format_ip4_address (u8 * s, va_list * args) |
| 974 | { |
| 975 | u8 *a = va_arg (*args, u8 *); |
| 976 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 977 | } |
| 978 | |
| 979 | u8 * |
| 980 | format_ip6_address (u8 * s, va_list * args) |
| 981 | { |
| 982 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 983 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 984 | |
| 985 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 986 | max_n_zeros = 0; |
| 987 | i_first_zero = i_max_n_zero; |
| 988 | n_zeros = 0; |
| 989 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 990 | { |
| 991 | u32 is_zero = a->as_u16[i] == 0; |
| 992 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 993 | { |
| 994 | i_first_zero = i; |
| 995 | n_zeros = 0; |
| 996 | } |
| 997 | n_zeros += is_zero; |
| 998 | if ((!is_zero && n_zeros > max_n_zeros) |
| 999 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 1000 | { |
| 1001 | i_max_n_zero = i_first_zero; |
| 1002 | max_n_zeros = n_zeros; |
| 1003 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 1004 | n_zeros = 0; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | last_double_colon = 0; |
| 1009 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 1010 | { |
| 1011 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 1012 | { |
| 1013 | s = format (s, "::"); |
| 1014 | i += max_n_zeros - 1; |
| 1015 | last_double_colon = 1; |
| 1016 | } |
| 1017 | else |
| 1018 | { |
| 1019 | s = format (s, "%s%x", |
| 1020 | (last_double_colon || i == 0) ? "" : ":", |
| 1021 | clib_net_to_host_u16 (a->as_u16[i])); |
| 1022 | last_double_colon = 0; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | return s; |
| 1027 | } |
| 1028 | |
| 1029 | /* Format an IP46 address. */ |
| 1030 | u8 * |
| 1031 | format_ip46_address (u8 * s, va_list * args) |
| 1032 | { |
| 1033 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 1034 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 1035 | int is_ip4 = 1; |
| 1036 | |
| 1037 | switch (type) |
| 1038 | { |
| 1039 | case IP46_TYPE_ANY: |
| 1040 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 1041 | break; |
| 1042 | case IP46_TYPE_IP4: |
| 1043 | is_ip4 = 1; |
| 1044 | break; |
| 1045 | case IP46_TYPE_IP6: |
| 1046 | is_ip4 = 0; |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | return is_ip4 ? |
| 1051 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 1052 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | server_handle_rx (echo_main_t * em, session_event_t * e) |
| 1057 | { |
| 1058 | int n_read, max_dequeue, n_sent; |
| 1059 | u32 offset, to_dequeue; |
| 1060 | echo_session_t *s; |
| 1061 | |
| 1062 | s = pool_elt_at_index (em->sessions, e->session_index); |
| 1063 | |
| 1064 | /* Clear event only once. Otherwise, if we do it in the loop by calling |
| 1065 | * app_recv_stream, we may end up with a lot of unhandled rx events on the |
| 1066 | * message queue */ |
| 1067 | svm_fifo_unset_event (s->rx_fifo); |
| 1068 | |
| 1069 | max_dequeue = svm_fifo_max_dequeue (s->rx_fifo); |
| 1070 | if (PREDICT_FALSE (!max_dequeue)) |
| 1071 | return; |
| 1072 | |
| 1073 | do |
| 1074 | { |
| 1075 | /* The options here are to limit ourselves to max_dequeue or read |
| 1076 | * even the data that was enqueued while we were dequeueing and which |
| 1077 | * now has an rx event in the mq. Either of the two work. */ |
| 1078 | to_dequeue = clib_min (max_dequeue, vec_len (em->rx_buf)); |
| 1079 | n_read = app_recv_stream_raw (s->rx_fifo, em->rx_buf, to_dequeue, |
| 1080 | 0 /* clear evt */ , 0 /* peek */ ); |
| 1081 | if (n_read > 0) |
| 1082 | { |
| 1083 | max_dequeue -= n_read; |
| 1084 | s->bytes_received += n_read; |
| 1085 | } |
| 1086 | else |
| 1087 | break; |
| 1088 | |
| 1089 | /* Reflect if a non-drop session */ |
| 1090 | if (!em->no_return && n_read > 0) |
| 1091 | { |
| 1092 | offset = 0; |
| 1093 | do |
| 1094 | { |
| 1095 | n_sent = app_send_stream ((app_session_t *) s, |
| 1096 | &em->rx_buf[offset], |
| 1097 | n_read, SVM_Q_WAIT); |
| 1098 | if (n_sent > 0) |
| 1099 | { |
| 1100 | n_read -= n_sent; |
| 1101 | offset += n_sent; |
| 1102 | } |
| 1103 | } |
| 1104 | while ((n_sent <= 0 || n_read > 0) && !em->time_to_stop); |
| 1105 | } |
| 1106 | } |
| 1107 | while (max_dequeue > 0 && !em->time_to_stop); |
| 1108 | } |
| 1109 | |
| 1110 | static void |
| 1111 | server_handle_mq (echo_main_t * em) |
| 1112 | { |
| 1113 | svm_msg_q_msg_t msg; |
| 1114 | session_event_t *e; |
| 1115 | |
| 1116 | while (1) |
| 1117 | { |
| 1118 | int rc = svm_msg_q_sub (em->our_event_queue, &msg, SVM_Q_TIMEDWAIT, 1); |
| 1119 | if (PREDICT_FALSE (rc == ETIMEDOUT && em->time_to_stop)) |
| 1120 | break; |
| 1121 | if (PREDICT_FALSE (em->time_to_print_stats == 1)) |
| 1122 | { |
| 1123 | em->time_to_print_stats = 0; |
| 1124 | fformat (stdout, "%d connections\n", pool_elts (em->sessions)); |
| 1125 | } |
| 1126 | if (rc == ETIMEDOUT) |
| 1127 | continue; |
| 1128 | e = svm_msg_q_msg_data (em->our_event_queue, &msg); |
| 1129 | clib_warning ("Event %d", e->event_type); |
| 1130 | switch (e->event_type) |
| 1131 | { |
| 1132 | case FIFO_EVENT_APP_RX: |
| 1133 | server_handle_rx (em, e); |
| 1134 | break; |
| 1135 | default: |
| 1136 | handle_mq_event (e); |
| 1137 | break; |
| 1138 | } |
| 1139 | svm_msg_q_free_msg (em->our_event_queue, &msg); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | void |
| 1144 | server_send_listen (echo_main_t * em) |
| 1145 | { |
| 1146 | vl_api_bind_uri_t *bmp; |
| 1147 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 1148 | clib_memset (bmp, 0, sizeof (*bmp)); |
| 1149 | |
| 1150 | bmp->_vl_msg_id = ntohs (VL_API_BIND_URI); |
| 1151 | bmp->client_index = em->my_client_index; |
| 1152 | bmp->context = ntohl (0xfeedface); |
| 1153 | memcpy (bmp->uri, em->uri, vec_len (em->uri)); |
| 1154 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp); |
| 1155 | } |
| 1156 | |
| 1157 | int |
| 1158 | server_listen (echo_main_t * em) |
| 1159 | { |
| 1160 | server_send_listen (em); |
| 1161 | if (wait_for_state_change (em, STATE_READY)) |
| 1162 | { |
| 1163 | clib_warning ("timeout waiting for STATE_READY"); |
| 1164 | return -1; |
| 1165 | } |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | void |
| 1170 | server_send_unbind (echo_main_t * em) |
| 1171 | { |
| 1172 | vl_api_unbind_uri_t *ump; |
| 1173 | |
| 1174 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 1175 | clib_memset (ump, 0, sizeof (*ump)); |
| 1176 | |
| 1177 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI); |
| 1178 | ump->client_index = em->my_client_index; |
| 1179 | memcpy (ump->uri, em->uri, vec_len (em->uri)); |
| 1180 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump); |
| 1181 | } |
| 1182 | |
| 1183 | void |
| 1184 | server_run (echo_main_t * em) |
| 1185 | { |
| 1186 | echo_session_t *session; |
| 1187 | int i; |
| 1188 | |
| 1189 | /* $$$$ hack preallocation */ |
| 1190 | for (i = 0; i < 200000; i++) |
| 1191 | { |
| 1192 | pool_get (em->sessions, session); |
| 1193 | clib_memset (session, 0, sizeof (*session)); |
| 1194 | } |
| 1195 | for (i = 0; i < 200000; i++) |
| 1196 | pool_put_index (em->sessions, i); |
| 1197 | |
| 1198 | if (application_attach (em)) |
| 1199 | return; |
| 1200 | |
| 1201 | /* Bind to uri */ |
| 1202 | if (server_listen (em)) |
| 1203 | return; |
| 1204 | |
| 1205 | /* Enter handle event loop */ |
| 1206 | server_handle_mq (em); |
| 1207 | |
| 1208 | /* Cleanup */ |
| 1209 | server_send_unbind (em); |
| 1210 | |
| 1211 | application_detach (em); |
| 1212 | |
| 1213 | fformat (stdout, "Test complete...\n"); |
| 1214 | } |
| 1215 | |
| 1216 | static void |
| 1217 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 1218 | mp) |
| 1219 | { |
| 1220 | echo_main_t *em = &echo_main; |
| 1221 | uword *p; |
| 1222 | |
| 1223 | if (mp->retval) |
| 1224 | { |
| 1225 | clib_warning ("vpp complained about disconnect: %d", |
| 1226 | ntohl (mp->retval)); |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | em->state = STATE_START; |
| 1231 | |
| 1232 | p = hash_get (em->session_index_by_vpp_handles, mp->handle); |
| 1233 | if (p) |
| 1234 | { |
| 1235 | hash_unset (em->session_index_by_vpp_handles, mp->handle); |
| 1236 | } |
| 1237 | else |
| 1238 | { |
| 1239 | clib_warning ("couldn't find session key %llx", mp->handle); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | static void |
| 1244 | vl_api_application_tls_cert_add_reply_t_handler |
| 1245 | (vl_api_application_tls_cert_add_reply_t * mp) |
| 1246 | { |
| 1247 | if (mp->retval) |
| 1248 | clib_warning ("failed to add tls cert"); |
| 1249 | } |
| 1250 | |
| 1251 | static void |
| 1252 | vl_api_application_tls_key_add_reply_t_handler |
| 1253 | (vl_api_application_tls_key_add_reply_t * mp) |
| 1254 | { |
| 1255 | if (mp->retval) |
| 1256 | clib_warning ("failed to add tls key"); |
| 1257 | } |
| 1258 | |
| 1259 | #define foreach_quic_echo_msg \ |
| 1260 | _(BIND_URI_REPLY, bind_uri_reply) \ |
| 1261 | _(UNBIND_URI_REPLY, unbind_uri_reply) \ |
| 1262 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 1263 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 1264 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 1265 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
| 1266 | _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply) \ |
| 1267 | _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply) \ |
| 1268 | |
| 1269 | void |
| 1270 | quic_echo_api_hookup (echo_main_t * em) |
| 1271 | { |
| 1272 | #define _(N,n) \ |
| 1273 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1274 | vl_api_##n##_t_handler, \ |
| 1275 | vl_noop_handler, \ |
| 1276 | vl_api_##n##_t_endian, \ |
| 1277 | vl_api_##n##_t_print, \ |
| 1278 | sizeof(vl_api_##n##_t), 1); |
| 1279 | foreach_quic_echo_msg; |
| 1280 | #undef _ |
| 1281 | } |
| 1282 | |
| 1283 | int |
| 1284 | main (int argc, char **argv) |
| 1285 | { |
| 1286 | int i_am_server = 1, test_return_packets = 0; |
| 1287 | echo_main_t *em = &echo_main; |
| 1288 | svm_fifo_segment_main_t *sm = &em->segment_main; |
| 1289 | unformat_input_t _argv, *a = &_argv; |
| 1290 | u8 *chroot_prefix; |
| 1291 | u8 *uri = 0; |
| 1292 | u8 *bind_uri = (u8 *) "quic://0.0.0.0/1234"; |
| 1293 | u8 *connect_uri = (u8 *) "quic://6.0.1.1/1234"; |
| 1294 | u64 bytes_to_send = 64 << 10, mbytes; |
| 1295 | char *app_name; |
| 1296 | u32 tmp; |
| 1297 | |
| 1298 | clib_mem_init_thread_safe (0, 256 << 20); |
| 1299 | |
| 1300 | clib_memset (em, 0, sizeof (*em)); |
| 1301 | em->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
| 1302 | em->my_pid = getpid (); |
| 1303 | em->configured_segment_size = 1 << 20; |
| 1304 | em->socket_name = 0; |
| 1305 | em->use_sock_api = 1; |
| 1306 | em->fifo_size = 64 << 10; |
| 1307 | em->n_clients = 1; |
| 1308 | |
| 1309 | clib_time_init (&em->clib_time); |
| 1310 | init_error_string_table (em); |
| 1311 | svm_fifo_segment_main_init (sm, HIGH_SEGMENT_BASEVA, 20); |
| 1312 | unformat_init_command_line (a, argv); |
| 1313 | |
| 1314 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 1315 | { |
| 1316 | if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 1317 | { |
| 1318 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 1319 | } |
| 1320 | else if (unformat (a, "uri %s", &uri)) |
| 1321 | ; |
| 1322 | else if (unformat (a, "segment-size %dM", &tmp)) |
| 1323 | em->configured_segment_size = tmp << 20; |
| 1324 | else if (unformat (a, "segment-size %dG", &tmp)) |
| 1325 | em->configured_segment_size = tmp << 30; |
| 1326 | else if (unformat (a, "server")) |
| 1327 | i_am_server = 1; |
| 1328 | else if (unformat (a, "client")) |
| 1329 | i_am_server = 0; |
| 1330 | else if (unformat (a, "no-return")) |
| 1331 | em->no_return = 1; |
| 1332 | else if (unformat (a, "test")) |
| 1333 | test_return_packets = 1; |
| 1334 | else if (unformat (a, "bytes %lld", &mbytes)) |
| 1335 | { |
| 1336 | bytes_to_send = mbytes; |
| 1337 | } |
| 1338 | else if (unformat (a, "mbytes %lld", &mbytes)) |
| 1339 | { |
| 1340 | bytes_to_send = mbytes << 20; |
| 1341 | } |
| 1342 | else if (unformat (a, "gbytes %lld", &mbytes)) |
| 1343 | { |
| 1344 | bytes_to_send = mbytes << 30; |
| 1345 | } |
| 1346 | else if (unformat (a, "socket-name %s", &em->socket_name)) |
| 1347 | ; |
| 1348 | else if (unformat (a, "use-svm-api")) |
| 1349 | em->use_sock_api = 0; |
| 1350 | else if (unformat (a, "fifo-size %d", &tmp)) |
| 1351 | em->fifo_size = tmp << 10; |
| 1352 | else if (unformat (a, "nclients %d", &em->n_clients)) |
| 1353 | ; |
| 1354 | else |
| 1355 | { |
| 1356 | fformat (stderr, "%s: usage [master|slave]\n", argv[0]); |
| 1357 | exit (1); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | if (!em->socket_name) |
| 1362 | em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0); |
| 1363 | |
| 1364 | if (uri) |
| 1365 | { |
| 1366 | em->uri = format (0, "%s%c", uri, 0); |
| 1367 | em->connect_uri = format (0, "%s%c", uri, 0); |
| 1368 | } |
| 1369 | else |
| 1370 | { |
| 1371 | em->uri = format (0, "%s%c", bind_uri, 0); |
| 1372 | em->connect_uri = format (0, "%s%c", connect_uri, 0); |
| 1373 | } |
| 1374 | |
| 1375 | em->i_am_master = i_am_server; |
| 1376 | em->test_return_packets = test_return_packets; |
| 1377 | em->bytes_to_send = bytes_to_send; |
| 1378 | em->time_to_stop = 0; |
| 1379 | vec_validate (em->rx_buf, 4 << 20); |
| 1380 | vec_validate (em->client_thread_handles, em->n_clients - 1); |
| 1381 | vec_validate (em->thread_args, em->n_clients - 1); |
| 1382 | |
| 1383 | setup_signal_handlers (); |
| 1384 | quic_echo_api_hookup (em); |
| 1385 | |
| 1386 | app_name = i_am_server ? "quic_echo_server" : "quic_echo_client"; |
| 1387 | if (connect_to_vpp (app_name) < 0) |
| 1388 | { |
| 1389 | svm_region_exit (); |
| 1390 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 1391 | exit (1); |
| 1392 | } |
| 1393 | |
| 1394 | if (i_am_server == 0) |
| 1395 | clients_run (em); |
| 1396 | else |
| 1397 | server_run (em); |
| 1398 | |
| 1399 | /* Make sure detach finishes */ |
| 1400 | wait_for_state_change (em, STATE_DETACHED); |
| 1401 | |
| 1402 | disconnect_from_vpp (em); |
| 1403 | exit (0); |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * fd.io coding-style-patch-verification: ON |
| 1408 | * |
| 1409 | * Local Variables: |
| 1410 | * eval: (c-set-style "gnu") |
| 1411 | * End: |
| 1412 | */ |