Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [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 |
| 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 <stdlib.h> |
| 18 | #include <signal.h> |
| 19 | #include <svm/svm_fifo_segment.h> |
| 20 | #include <vlibmemory/api.h> |
| 21 | #include <vpp/api/vpe_msg_enum.h> |
| 22 | #include <vnet/session/application_interface.h> |
| 23 | #include <uri/vppcom.h> |
| 24 | #include <vlib/unix/unix.h> |
| 25 | #include <vppinfra/vec_bootstrap.h> |
| 26 | |
| 27 | #define vl_typedefs /* define message structures */ |
| 28 | #include <vpp/api/vpe_all_api_h.h> |
| 29 | #undef vl_typedefs |
| 30 | |
| 31 | /* declare message handlers for each api */ |
| 32 | |
| 33 | #define vl_endianfun /* define message structures */ |
| 34 | #include <vpp/api/vpe_all_api_h.h> |
| 35 | #undef vl_endianfun |
| 36 | |
| 37 | /* instantiate all the print functions we know about */ |
| 38 | #define vl_print(handle, ...) |
| 39 | #define vl_printfun |
| 40 | #include <vpp/api/vpe_all_api_h.h> |
| 41 | #undef vl_printfun |
| 42 | |
| 43 | #if (CLIB_DEBUG > 0) |
| 44 | /* Set VPPCOM_DEBUG 2 for connection debug, 3 for read/write debug output */ |
| 45 | #define VPPCOM_DEBUG 1 |
| 46 | #else |
| 47 | #define VPPCOM_DEBUG 0 |
| 48 | #endif |
| 49 | |
| 50 | /* |
| 51 | * VPPCOM Private definitions and functions. |
| 52 | */ |
| 53 | typedef enum |
| 54 | { |
| 55 | STATE_APP_START, |
| 56 | STATE_APP_CONN_VPP, |
| 57 | STATE_APP_ENABLED, |
| 58 | STATE_APP_ATTACHED, |
| 59 | } app_state_t; |
| 60 | |
| 61 | typedef enum |
| 62 | { |
| 63 | STATE_START, |
| 64 | STATE_CONNECT, |
| 65 | STATE_LISTEN, |
| 66 | STATE_ACCEPT, |
| 67 | STATE_DISCONNECT, |
| 68 | STATE_FAILED |
| 69 | } session_state_t; |
| 70 | |
| 71 | typedef struct |
| 72 | { |
| 73 | volatile session_state_t state; |
| 74 | |
| 75 | svm_fifo_t *server_rx_fifo; |
| 76 | svm_fifo_t *server_tx_fifo; |
| 77 | u32 sm_seg_index; |
| 78 | u64 vpp_session_handle; |
| 79 | unix_shared_memory_queue_t *event_queue; |
| 80 | |
| 81 | /* Socket configuration state */ |
| 82 | u8 is_server; |
| 83 | u8 is_listen; |
| 84 | u8 is_cut_thru; |
| 85 | u8 is_nonblocking; |
| 86 | u32 vrf; |
| 87 | u8 is_ip4; |
| 88 | u8 ip[16]; |
| 89 | u16 port; |
| 90 | u8 proto; |
| 91 | u64 client_queue_address; |
| 92 | u64 options[16]; |
| 93 | } session_t; |
| 94 | |
| 95 | typedef struct vppcom_cfg_t_ |
| 96 | { |
| 97 | u64 heapsize; |
| 98 | u64 segment_baseva; |
| 99 | u32 segment_size; |
| 100 | u32 add_segment_size; |
| 101 | u32 preallocated_fifo_pairs; |
| 102 | u32 rx_fifo_size; |
| 103 | u32 tx_fifo_size; |
| 104 | u32 event_queue_size; |
| 105 | u32 listen_queue_size; |
| 106 | f64 app_timeout; |
| 107 | f64 session_timeout; |
| 108 | f64 accept_timeout; |
| 109 | } vppcom_cfg_t; |
| 110 | |
| 111 | typedef struct vppcom_main_t_ |
| 112 | { |
| 113 | u8 init; |
| 114 | u32 *client_session_index_fifo; |
| 115 | volatile u32 bind_session_index; |
| 116 | u32 tx_event_id; |
| 117 | int main_cpu; |
| 118 | |
| 119 | /* vpe input queue */ |
| 120 | unix_shared_memory_queue_t *vl_input_queue; |
| 121 | |
| 122 | /* API client handle */ |
| 123 | u32 my_client_index; |
| 124 | |
| 125 | /* Session pool */ |
| 126 | clib_spinlock_t sessions_lockp; |
| 127 | session_t *sessions; |
| 128 | |
| 129 | /* Hash table for disconnect processing */ |
| 130 | uword *session_index_by_vpp_handles; |
| 131 | |
| 132 | /* Select bitmaps */ |
| 133 | clib_bitmap_t *rd_bitmap; |
| 134 | clib_bitmap_t *wr_bitmap; |
| 135 | clib_bitmap_t *ex_bitmap; |
| 136 | |
| 137 | /* Our event queue */ |
| 138 | unix_shared_memory_queue_t *app_event_queue; |
| 139 | |
| 140 | /* unique segment name counter */ |
| 141 | u32 unique_segment_index; |
| 142 | |
| 143 | pid_t my_pid; |
| 144 | |
| 145 | /* For deadman timers */ |
| 146 | clib_time_t clib_time; |
| 147 | |
| 148 | /* State of the connection, shared between msg RX thread and main thread */ |
| 149 | volatile app_state_t app_state; |
| 150 | |
| 151 | vppcom_cfg_t cfg; |
| 152 | |
| 153 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
| 154 | uword *error_string_by_error_number; |
| 155 | } vppcom_main_t; |
| 156 | |
| 157 | vppcom_main_t vppcom_main = {.my_client_index = ~0 }; |
| 158 | |
| 159 | static const char * |
| 160 | vppcom_app_state_str (app_state_t state) |
| 161 | { |
| 162 | char *st; |
| 163 | |
| 164 | switch (state) |
| 165 | { |
| 166 | case STATE_APP_START: |
| 167 | st = "STATE_APP_START"; |
| 168 | break; |
| 169 | |
| 170 | case STATE_APP_CONN_VPP: |
| 171 | st = "STATE_APP_CONN_VPP"; |
| 172 | break; |
| 173 | |
| 174 | case STATE_APP_ENABLED: |
| 175 | st = "STATE_APP_ENABLED"; |
| 176 | break; |
| 177 | |
| 178 | case STATE_APP_ATTACHED: |
| 179 | st = "STATE_APP_ATTACHED"; |
| 180 | break; |
| 181 | |
| 182 | default: |
| 183 | st = "UNKNOWN_APP_STATE"; |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | return st; |
| 188 | } |
| 189 | |
| 190 | static const char * |
| 191 | vppcom_session_state_str (session_state_t state) |
| 192 | { |
| 193 | char *st; |
| 194 | |
| 195 | switch (state) |
| 196 | { |
| 197 | case STATE_START: |
| 198 | st = "STATE_START"; |
| 199 | break; |
| 200 | |
| 201 | case STATE_CONNECT: |
| 202 | st = "STATE_CONNECT"; |
| 203 | break; |
| 204 | |
| 205 | case STATE_LISTEN: |
| 206 | st = "STATE_LISTEN"; |
| 207 | break; |
| 208 | |
| 209 | case STATE_ACCEPT: |
| 210 | st = "STATE_ACCEPT"; |
| 211 | break; |
| 212 | |
| 213 | case STATE_DISCONNECT: |
| 214 | st = "STATE_DISCONNECT"; |
| 215 | break; |
| 216 | |
| 217 | case STATE_FAILED: |
| 218 | st = "STATE_FAILED"; |
| 219 | break; |
| 220 | |
| 221 | default: |
| 222 | st = "UNKNOWN_STATE"; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | return st; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * VPPCOM Utility Functions |
| 231 | */ |
| 232 | static inline int |
| 233 | vppcom_session_at_index (u32 session_index, session_t * volatile *sess) |
| 234 | { |
| 235 | vppcom_main_t *vcm = &vppcom_main; |
| 236 | |
| 237 | /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */ |
| 238 | if (PREDICT_FALSE ((session_index == ~0) || |
| 239 | pool_is_free_index (vcm->sessions, session_index))) |
| 240 | { |
| 241 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 242 | vcm->my_pid, session_index); |
| 243 | return VPPCOM_EBADFD; |
| 244 | } |
| 245 | *sess = pool_elt_at_index (vcm->sessions, session_index); |
| 246 | return VPPCOM_OK; |
| 247 | } |
| 248 | |
| 249 | static int |
| 250 | vppcom_connect_to_vpp (char *app_name) |
| 251 | { |
| 252 | api_main_t *am = &api_main; |
| 253 | vppcom_main_t *vcm = &vppcom_main; |
| 254 | |
| 255 | if (VPPCOM_DEBUG > 0) |
| 256 | printf ("\nConnecting to VPP api..."); |
| 257 | if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0) |
| 258 | { |
| 259 | clib_warning ("[%d] connect to vpp (%s) failed!", |
| 260 | vcm->my_pid, app_name); |
| 261 | return VPPCOM_ECONNREFUSED; |
| 262 | } |
| 263 | |
| 264 | vcm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 265 | vcm->my_client_index = am->my_client_index; |
| 266 | if (VPPCOM_DEBUG > 0) |
| 267 | printf (" connected!\n"); |
| 268 | |
| 269 | vcm->app_state = STATE_APP_CONN_VPP; |
| 270 | return VPPCOM_OK; |
| 271 | } |
| 272 | |
| 273 | static u8 * |
| 274 | format_api_error (u8 * s, va_list * args) |
| 275 | { |
| 276 | vppcom_main_t *vcm = &vppcom_main; |
| 277 | i32 error = va_arg (*args, u32); |
| 278 | uword *p; |
| 279 | |
| 280 | p = hash_get (vcm->error_string_by_error_number, -error); |
| 281 | |
| 282 | if (p) |
| 283 | s = format (s, "%s (%d)", p[0], error); |
| 284 | else |
| 285 | s = format (s, "%d", error); |
| 286 | return s; |
| 287 | } |
| 288 | |
| 289 | static void |
| 290 | vppcom_init_error_string_table (void) |
| 291 | { |
| 292 | vppcom_main_t *vcm = &vppcom_main; |
| 293 | |
| 294 | vcm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 295 | |
| 296 | #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s); |
| 297 | foreach_vnet_api_error; |
| 298 | #undef _ |
| 299 | |
| 300 | hash_set (vcm->error_string_by_error_number, 99, "Misc"); |
| 301 | } |
| 302 | |
| 303 | static inline int |
| 304 | vppcom_wait_for_app_state_change (app_state_t app_state) |
| 305 | { |
| 306 | vppcom_main_t *vcm = &vppcom_main; |
| 307 | f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout; |
| 308 | |
| 309 | while (clib_time_now (&vcm->clib_time) < timeout) |
| 310 | { |
| 311 | if (vcm->app_state == app_state) |
| 312 | return VPPCOM_OK; |
| 313 | } |
| 314 | if (VPPCOM_DEBUG > 0) |
| 315 | clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid, |
| 316 | vppcom_app_state_str (app_state), app_state); |
| 317 | return VPPCOM_ETIMEDOUT; |
| 318 | } |
| 319 | |
| 320 | static inline int |
| 321 | vppcom_wait_for_session_state_change (u32 session_index, |
| 322 | session_state_t state, |
| 323 | f64 wait_for_time) |
| 324 | { |
| 325 | vppcom_main_t *vcm = &vppcom_main; |
| 326 | f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time; |
| 327 | session_t *volatile session; |
| 328 | int rv; |
| 329 | |
| 330 | do |
| 331 | { |
| 332 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 333 | rv = vppcom_session_at_index (session_index, &session); |
| 334 | if (PREDICT_FALSE (rv)) |
| 335 | { |
| 336 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 337 | return rv; |
| 338 | } |
| 339 | if (session->state == state) |
| 340 | { |
| 341 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 342 | return VPPCOM_OK; |
| 343 | } |
| 344 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 345 | } |
| 346 | while (clib_time_now (&vcm->clib_time) < timeout); |
| 347 | |
| 348 | if (VPPCOM_DEBUG > 0) |
| 349 | clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid, |
| 350 | vppcom_session_state_str (state), state); |
| 351 | return VPPCOM_ETIMEDOUT; |
| 352 | } |
| 353 | |
| 354 | static inline int |
| 355 | vppcom_wait_for_client_session_index (f64 wait_for_time) |
| 356 | { |
| 357 | vppcom_main_t *vcm = &vppcom_main; |
| 358 | f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time; |
| 359 | |
| 360 | do |
| 361 | { |
| 362 | if (clib_fifo_elts (vcm->client_session_index_fifo)) |
| 363 | return VPPCOM_OK; |
| 364 | } |
| 365 | while (clib_time_now (&vcm->clib_time) < timeout); |
| 366 | |
| 367 | if (wait_for_time == 0) |
| 368 | return VPPCOM_EAGAIN; |
| 369 | |
| 370 | if (VPPCOM_DEBUG > 0) |
| 371 | clib_warning ("[%d] timeout waiting for client_session_index", |
| 372 | vcm->my_pid); |
| 373 | return VPPCOM_ETIMEDOUT; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * VPP-API message functions |
| 378 | */ |
| 379 | static void |
| 380 | vppcom_send_session_enable_disable (u8 is_enable) |
| 381 | { |
| 382 | vppcom_main_t *vcm = &vppcom_main; |
| 383 | vl_api_session_enable_disable_t *bmp; |
| 384 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 385 | memset (bmp, 0, sizeof (*bmp)); |
| 386 | |
| 387 | bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE); |
| 388 | bmp->client_index = vcm->my_client_index; |
| 389 | bmp->context = htonl (0xfeedface); |
| 390 | bmp->is_enable = is_enable; |
| 391 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 392 | } |
| 393 | |
| 394 | static int |
| 395 | vppcom_app_session_enable (void) |
| 396 | { |
| 397 | vppcom_main_t *vcm = &vppcom_main; |
| 398 | int rv; |
| 399 | |
| 400 | if (vcm->app_state != STATE_APP_ENABLED) |
| 401 | { |
| 402 | vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ ); |
| 403 | rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED); |
| 404 | if (PREDICT_FALSE (rv)) |
| 405 | { |
| 406 | if (VPPCOM_DEBUG > 0) |
| 407 | clib_warning ("[%d] Session enable timed out, rv = %s (%d)", |
| 408 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 409 | return rv; |
| 410 | } |
| 411 | } |
| 412 | return VPPCOM_OK; |
| 413 | } |
| 414 | |
| 415 | static void |
| 416 | vl_api_session_enable_disable_reply_t_handler |
| 417 | (vl_api_session_enable_disable_reply_t * mp) |
| 418 | { |
| 419 | vppcom_main_t *vcm = &vppcom_main; |
| 420 | |
| 421 | if (mp->retval) |
| 422 | { |
| 423 | clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid, |
| 424 | format_api_error, ntohl (mp->retval)); |
| 425 | } |
| 426 | else |
| 427 | vcm->app_state = STATE_APP_ENABLED; |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | vppcom_app_send_attach (void) |
| 432 | { |
| 433 | vppcom_main_t *vcm = &vppcom_main; |
| 434 | vl_api_application_attach_t *bmp; |
| 435 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 436 | memset (bmp, 0, sizeof (*bmp)); |
| 437 | |
| 438 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 439 | bmp->client_index = vcm->my_client_index; |
| 440 | bmp->context = htonl (0xfeedface); |
| 441 | bmp->options[APP_OPTIONS_FLAGS] = |
| 442 | APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT; |
| 443 | bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 444 | bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 445 | bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 446 | bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 447 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 448 | } |
| 449 | |
| 450 | static int |
| 451 | vppcom_app_attach (void) |
| 452 | { |
| 453 | vppcom_main_t *vcm = &vppcom_main; |
| 454 | int rv; |
| 455 | |
| 456 | vppcom_app_send_attach (); |
| 457 | rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED); |
| 458 | if (PREDICT_FALSE (rv)) |
| 459 | { |
| 460 | if (VPPCOM_DEBUG > 0) |
| 461 | clib_warning ("[%d] application attach timed out, rv = %s (%d)", |
| 462 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 463 | return rv; |
| 464 | } |
| 465 | return VPPCOM_OK; |
| 466 | } |
| 467 | |
| 468 | static void |
| 469 | vppcom_app_detach (void) |
| 470 | { |
| 471 | vppcom_main_t *vcm = &vppcom_main; |
| 472 | vl_api_application_detach_t *bmp; |
| 473 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 474 | memset (bmp, 0, sizeof (*bmp)); |
| 475 | |
| 476 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 477 | bmp->client_index = vcm->my_client_index; |
| 478 | bmp->context = htonl (0xfeedface); |
| 479 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 480 | } |
| 481 | |
| 482 | static void |
| 483 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 484 | mp) |
| 485 | { |
| 486 | vppcom_main_t *vcm = &vppcom_main; |
| 487 | static svm_fifo_segment_create_args_t _a; |
| 488 | svm_fifo_segment_create_args_t *a = &_a; |
| 489 | int rv; |
| 490 | |
| 491 | memset (a, 0, sizeof (*a)); |
| 492 | if (mp->retval) |
| 493 | { |
| 494 | clib_warning ("[%d] attach failed: %U", vcm->my_pid, |
| 495 | format_api_error, ntohl (mp->retval)); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | if (mp->segment_name_length == 0) |
| 500 | { |
| 501 | clib_warning ("[%d] segment_name_length zero", vcm->my_pid); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | a->segment_name = (char *) mp->segment_name; |
| 506 | a->segment_size = mp->segment_size; |
| 507 | |
| 508 | ASSERT (mp->app_event_queue_address); |
| 509 | |
| 510 | /* Attach to the segment vpp created */ |
| 511 | rv = svm_fifo_segment_attach (a); |
| 512 | vec_reset_length (a->new_segment_indices); |
| 513 | if (PREDICT_FALSE (rv)) |
| 514 | { |
| 515 | clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid, |
| 516 | mp->segment_name); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | vcm->app_event_queue = |
| 521 | uword_to_pointer (mp->app_event_queue_address, |
| 522 | unix_shared_memory_queue_t *); |
| 523 | |
| 524 | vcm->app_state = STATE_APP_ATTACHED; |
| 525 | } |
| 526 | |
| 527 | static void |
| 528 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 529 | mp) |
| 530 | { |
| 531 | vppcom_main_t *vcm = &vppcom_main; |
| 532 | |
| 533 | if (mp->retval) |
| 534 | clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error, |
| 535 | ntohl (mp->retval)); |
| 536 | |
| 537 | vcm->app_state = STATE_APP_ENABLED; |
| 538 | } |
| 539 | |
| 540 | static void |
| 541 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 542 | mp) |
| 543 | { |
| 544 | vppcom_main_t *vcm = &vppcom_main; |
| 545 | uword *p; |
| 546 | |
| 547 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 548 | if (p) |
| 549 | { |
| 550 | session_t *session = 0; |
| 551 | int rv; |
| 552 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 553 | rv = vppcom_session_at_index (p[0], &session); |
| 554 | if (PREDICT_FALSE (rv)) |
| 555 | { |
| 556 | if (VPPCOM_DEBUG > 1) |
| 557 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 558 | vcm->my_pid, p[0]); |
| 559 | } |
| 560 | hash_unset (vcm->session_index_by_vpp_handles, mp->handle); |
| 561 | session->state = STATE_DISCONNECT; |
| 562 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | if (VPPCOM_DEBUG > 1) |
| 567 | clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid, |
| 568 | mp->handle); |
| 569 | } |
| 570 | |
| 571 | if (mp->retval) |
| 572 | clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid, |
| 573 | format_api_error, ntohl (mp->retval)); |
| 574 | } |
| 575 | |
| 576 | static void |
| 577 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 578 | { |
| 579 | vppcom_main_t *vcm = &vppcom_main; |
| 580 | static svm_fifo_segment_create_args_t _a; |
| 581 | svm_fifo_segment_create_args_t *a = &_a; |
| 582 | int rv; |
| 583 | |
| 584 | memset (a, 0, sizeof (*a)); |
| 585 | a->segment_name = (char *) mp->segment_name; |
| 586 | a->segment_size = mp->segment_size; |
| 587 | /* Attach to the segment vpp created */ |
| 588 | rv = svm_fifo_segment_attach (a); |
| 589 | vec_reset_length (a->new_segment_indices); |
| 590 | if (PREDICT_FALSE (rv)) |
| 591 | { |
| 592 | clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", |
| 593 | vcm->my_pid, mp->segment_name); |
| 594 | return; |
| 595 | } |
| 596 | if (VPPCOM_DEBUG > 1) |
| 597 | clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid, |
| 598 | mp->segment_name, mp->segment_size); |
| 599 | } |
| 600 | |
| 601 | static void |
| 602 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 603 | { |
| 604 | vppcom_main_t *vcm = &vppcom_main; |
| 605 | session_t *session = 0; |
| 606 | vl_api_disconnect_session_reply_t *rmp; |
| 607 | uword *p; |
| 608 | int rv = 0; |
| 609 | |
| 610 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 611 | if (p) |
| 612 | { |
| 613 | int rval; |
| 614 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 615 | rval = vppcom_session_at_index (p[0], &session); |
| 616 | if (PREDICT_FALSE (rval)) |
| 617 | { |
| 618 | if (VPPCOM_DEBUG > 1) |
| 619 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 620 | vcm->my_pid, p[0]); |
| 621 | } |
| 622 | else |
| 623 | pool_put (vcm->sessions, session); |
| 624 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 625 | hash_unset (vcm->session_index_by_vpp_handles, mp->handle); |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid, |
| 630 | mp->handle); |
| 631 | rv = -11; |
| 632 | } |
| 633 | |
| 634 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 635 | memset (rmp, 0, sizeof (*rmp)); |
| 636 | |
| 637 | rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY); |
| 638 | rmp->retval = htonl (rv); |
| 639 | rmp->handle = mp->handle; |
| 640 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 641 | } |
| 642 | |
| 643 | static void |
| 644 | vl_api_reset_session_t_handler (vl_api_reset_session_t * mp) |
| 645 | { |
| 646 | vppcom_main_t *vcm = &vppcom_main; |
| 647 | session_t *session = 0; |
| 648 | vl_api_reset_session_reply_t *rmp; |
| 649 | uword *p; |
| 650 | int rv = 0; |
| 651 | |
| 652 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 653 | if (p) |
| 654 | { |
| 655 | int rval; |
| 656 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 657 | rval = vppcom_session_at_index (p[0], &session); |
| 658 | if (PREDICT_FALSE (rval)) |
| 659 | { |
| 660 | if (VPPCOM_DEBUG > 1) |
| 661 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 662 | vcm->my_pid, p[0]); |
| 663 | } |
| 664 | else |
| 665 | pool_put (vcm->sessions, session); |
| 666 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 667 | hash_unset (vcm->session_index_by_vpp_handles, mp->handle); |
| 668 | } |
| 669 | else |
| 670 | { |
| 671 | clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid, |
| 672 | mp->handle); |
| 673 | rv = -11; |
| 674 | } |
| 675 | |
| 676 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 677 | memset (rmp, 0, sizeof (*rmp)); |
| 678 | rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY); |
| 679 | rmp->retval = htonl (rv); |
| 680 | rmp->handle = mp->handle; |
| 681 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 682 | } |
| 683 | |
| 684 | static void |
| 685 | vl_api_connect_sock_reply_t_handler (vl_api_connect_sock_reply_t * mp) |
| 686 | { |
| 687 | vppcom_main_t *vcm = &vppcom_main; |
| 688 | session_t *session; |
| 689 | u32 session_index; |
| 690 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 691 | u8 is_cut_thru = 0; |
| 692 | int rv; |
| 693 | |
| 694 | if (mp->retval) |
| 695 | { |
| 696 | clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error, |
| 697 | ntohl (mp->retval)); |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | session_index = ntohl (mp->app_connect); |
| 702 | if (VPPCOM_DEBUG > 1) |
| 703 | clib_warning ("[%d] app_connect = %d 0x%08x", vcm->my_pid, |
| 704 | session_index, session_index); |
| 705 | |
| 706 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 707 | if (pool_is_free_index (vcm->sessions, session_index)) |
| 708 | { |
| 709 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 710 | if (VPPCOM_DEBUG > 1) |
| 711 | clib_warning ("[%d] invalid session, sid %d is closed!", |
| 712 | vcm->my_pid, session_index); |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | /* We've been redirected */ |
| 717 | if (mp->segment_name_length > 0) |
| 718 | { |
| 719 | static svm_fifo_segment_create_args_t _a; |
| 720 | svm_fifo_segment_create_args_t *a = &_a; |
| 721 | |
| 722 | is_cut_thru = 1; |
| 723 | memset (a, 0, sizeof (*a)); |
| 724 | a->segment_name = (char *) mp->segment_name; |
| 725 | if (VPPCOM_DEBUG > 1) |
| 726 | clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid, |
| 727 | a->segment_name); |
| 728 | rv = svm_fifo_segment_attach (a); |
| 729 | vec_reset_length (a->new_segment_indices); |
| 730 | if (PREDICT_FALSE (rv)) |
| 731 | { |
| 732 | clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed", |
| 733 | vcm->my_pid, a->segment_name); |
| 734 | return; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Setup session |
| 740 | */ |
| 741 | if (VPPCOM_DEBUG > 1) |
| 742 | clib_warning ("[%d] client sid %d", vcm->my_pid, session_index); |
| 743 | |
| 744 | session = pool_elt_at_index (vcm->sessions, session_index); |
| 745 | session->is_cut_thru = is_cut_thru; |
| 746 | session->event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 747 | unix_shared_memory_queue_t *); |
| 748 | |
| 749 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 750 | rx_fifo->client_session_index = session_index; |
| 751 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 752 | tx_fifo->client_session_index = session_index; |
| 753 | |
| 754 | session->server_rx_fifo = rx_fifo; |
| 755 | session->server_tx_fifo = tx_fifo; |
| 756 | session->vpp_session_handle = mp->handle; |
| 757 | session->state = STATE_CONNECT; |
| 758 | |
| 759 | /* Add it to lookup table */ |
| 760 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 761 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 762 | } |
| 763 | |
| 764 | static void |
| 765 | vppcom_send_connect_sock (session_t * session, u32 session_index) |
| 766 | { |
| 767 | vppcom_main_t *vcm = &vppcom_main; |
| 768 | vl_api_connect_sock_t *cmp; |
| 769 | |
| 770 | /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */ |
| 771 | session->is_server = 0; |
| 772 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 773 | memset (cmp, 0, sizeof (*cmp)); |
| 774 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK); |
| 775 | cmp->client_index = vcm->my_client_index; |
| 776 | cmp->context = htonl (0xfeedface); |
| 777 | cmp->app_connect = session_index; |
| 778 | |
| 779 | if (VPPCOM_DEBUG > 1) |
| 780 | clib_warning ("[%d] session_index = %d 0x%08x, app_connect = %d 0x%08x", |
| 781 | vcm->my_pid, session_index, session_index, |
| 782 | cmp->app_connect, cmp->app_connect); |
| 783 | |
| 784 | cmp->vrf = session->vrf; |
| 785 | cmp->is_ip4 = session->is_ip4; |
Dave Wallace | 6d5c4cd | 2017-08-15 16:56:29 -0400 | [diff] [blame] | 786 | clib_memcpy (cmp->ip, session->ip, sizeof (cmp->ip)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 787 | cmp->port = session->port; |
| 788 | cmp->proto = session->proto; |
| 789 | clib_memcpy (cmp->options, session->options, sizeof (cmp->options)); |
| 790 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp); |
| 791 | } |
| 792 | |
| 793 | static int |
| 794 | vppcom_send_disconnect (u32 session_index) |
| 795 | { |
| 796 | vppcom_main_t *vcm = &vppcom_main; |
| 797 | vl_api_disconnect_session_t *dmp; |
| 798 | session_t *session = 0; |
| 799 | int rv; |
| 800 | |
| 801 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 802 | rv = vppcom_session_at_index (session_index, &session); |
| 803 | if (PREDICT_FALSE (rv)) |
| 804 | { |
| 805 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 806 | if (VPPCOM_DEBUG > 1) |
| 807 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 808 | vcm->my_pid, session_index); |
| 809 | return rv; |
| 810 | } |
| 811 | |
| 812 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 813 | memset (dmp, 0, sizeof (*dmp)); |
| 814 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 815 | dmp->client_index = vcm->my_client_index; |
| 816 | dmp->handle = session->vpp_session_handle; |
| 817 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 818 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp); |
| 819 | return VPPCOM_OK; |
| 820 | } |
| 821 | |
| 822 | static void |
| 823 | vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp) |
| 824 | { |
| 825 | vppcom_main_t *vcm = &vppcom_main; |
| 826 | session_t *session = 0; |
| 827 | int rv; |
| 828 | |
| 829 | if (mp->retval) |
| 830 | clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error, |
| 831 | ntohl (mp->retval)); |
| 832 | |
| 833 | ASSERT (vcm->bind_session_index != ~0); |
| 834 | |
| 835 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 836 | rv = vppcom_session_at_index (vcm->bind_session_index, &session); |
| 837 | if (rv == VPPCOM_OK) |
| 838 | { |
| 839 | session->vpp_session_handle = mp->handle; |
| 840 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, |
| 841 | vcm->bind_session_index); |
| 842 | session->state = mp->retval ? STATE_FAILED : STATE_LISTEN; |
| 843 | vcm->bind_session_index = ~0; |
| 844 | } |
| 845 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 846 | } |
| 847 | |
| 848 | static void |
| 849 | vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp) |
| 850 | { |
| 851 | vppcom_main_t *vcm = &vppcom_main; |
| 852 | session_t *session = 0; |
| 853 | int rv; |
| 854 | |
| 855 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 856 | rv = vppcom_session_at_index (vcm->bind_session_index, &session); |
| 857 | if (PREDICT_FALSE (rv)) |
| 858 | { |
| 859 | if (VPPCOM_DEBUG > 1) |
| 860 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 861 | vcm->my_pid, vcm->bind_session_index); |
| 862 | } |
| 863 | |
| 864 | if (mp->retval) |
| 865 | clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error, |
| 866 | ntohl (mp->retval)); |
| 867 | |
| 868 | vcm->bind_session_index = ~0; |
| 869 | session->state = STATE_START; |
| 870 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 871 | } |
| 872 | |
| 873 | u8 * |
| 874 | format_ip4_address (u8 * s, va_list * args) |
| 875 | { |
| 876 | u8 *a = va_arg (*args, u8 *); |
| 877 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 878 | } |
| 879 | |
| 880 | u8 * |
| 881 | format_ip6_address (u8 * s, va_list * args) |
| 882 | { |
| 883 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 884 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 885 | |
| 886 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 887 | max_n_zeros = 0; |
| 888 | i_first_zero = i_max_n_zero; |
| 889 | n_zeros = 0; |
| 890 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 891 | { |
| 892 | u32 is_zero = a->as_u16[i] == 0; |
| 893 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 894 | { |
| 895 | i_first_zero = i; |
| 896 | n_zeros = 0; |
| 897 | } |
| 898 | n_zeros += is_zero; |
| 899 | if ((!is_zero && n_zeros > max_n_zeros) |
| 900 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 901 | { |
| 902 | i_max_n_zero = i_first_zero; |
| 903 | max_n_zeros = n_zeros; |
| 904 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 905 | n_zeros = 0; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | last_double_colon = 0; |
| 910 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 911 | { |
| 912 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 913 | { |
| 914 | s = format (s, "::"); |
| 915 | i += max_n_zeros - 1; |
| 916 | last_double_colon = 1; |
| 917 | } |
| 918 | else |
| 919 | { |
| 920 | s = format (s, "%s%x", |
| 921 | (last_double_colon || i == 0) ? "" : ":", |
| 922 | clib_net_to_host_u16 (a->as_u16[i])); |
| 923 | last_double_colon = 0; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | return s; |
| 928 | } |
| 929 | |
| 930 | /* Format an IP46 address. */ |
| 931 | u8 * |
| 932 | format_ip46_address (u8 * s, va_list * args) |
| 933 | { |
| 934 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 935 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 936 | int is_ip4 = 1; |
| 937 | |
| 938 | switch (type) |
| 939 | { |
| 940 | case IP46_TYPE_ANY: |
| 941 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 942 | break; |
| 943 | case IP46_TYPE_IP4: |
| 944 | is_ip4 = 1; |
| 945 | break; |
| 946 | case IP46_TYPE_IP6: |
| 947 | is_ip4 = 0; |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | return is_ip4 ? |
| 952 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 953 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 954 | } |
| 955 | |
| 956 | static void |
| 957 | vl_api_accept_session_t_handler (vl_api_accept_session_t * mp) |
| 958 | { |
| 959 | vppcom_main_t *vcm = &vppcom_main; |
| 960 | vl_api_accept_session_reply_t *rmp; |
| 961 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 962 | session_t *session; |
| 963 | u32 session_index; |
| 964 | int rv = 0; |
| 965 | |
| 966 | if (!clib_fifo_free_elts (vcm->client_session_index_fifo)) |
| 967 | { |
| 968 | clib_warning ("[%d] client session queue is full!", vcm->my_pid); |
| 969 | rv = VNET_API_ERROR_QUEUE_FULL; |
| 970 | goto send_reply; |
| 971 | } |
| 972 | |
| 973 | if (VPPCOM_DEBUG > 1) |
| 974 | { |
| 975 | u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4); |
| 976 | clib_warning ("[%d] accepted session from: %s:%d", vcm->my_pid, ip_str, |
| 977 | clib_net_to_host_u16 (mp->port)); |
| 978 | vec_free (ip_str); |
| 979 | } |
| 980 | |
| 981 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 982 | /* Allocate local session and set it up */ |
| 983 | pool_get (vcm->sessions, session); |
| 984 | memset (session, 0, sizeof (*session)); |
| 985 | session_index = session - vcm->sessions; |
| 986 | |
| 987 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 988 | rx_fifo->client_session_index = session_index; |
| 989 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 990 | tx_fifo->client_session_index = session_index; |
| 991 | |
| 992 | session->server_rx_fifo = rx_fifo; |
| 993 | session->server_tx_fifo = tx_fifo; |
| 994 | session->event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 995 | unix_shared_memory_queue_t *); |
| 996 | session->state = STATE_ACCEPT; |
| 997 | session->is_cut_thru = 0; |
| 998 | session->port = ntohs (mp->port); |
| 999 | session->is_ip4 = mp->is_ip4; |
Dave Wallace | 6d5c4cd | 2017-08-15 16:56:29 -0400 | [diff] [blame] | 1000 | clib_memcpy (session->ip, mp->ip, sizeof (session->ip)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1001 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1002 | |
| 1003 | /* Add it to lookup table */ |
| 1004 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 1005 | |
| 1006 | clib_fifo_add1 (vcm->client_session_index_fifo, session_index); |
| 1007 | |
| 1008 | /* |
| 1009 | * Send accept reply to vpp |
| 1010 | */ |
| 1011 | send_reply: |
| 1012 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 1013 | memset (rmp, 0, sizeof (*rmp)); |
| 1014 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
| 1015 | rmp->retval = htonl (rv); |
| 1016 | rmp->handle = mp->handle; |
| 1017 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * Acting as server for redirected connect requests |
| 1022 | */ |
| 1023 | static void |
| 1024 | vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) |
| 1025 | { |
| 1026 | static svm_fifo_segment_create_args_t _a; |
| 1027 | svm_fifo_segment_create_args_t *a = &_a; |
| 1028 | vppcom_main_t *vcm = &vppcom_main; |
| 1029 | u32 session_index; |
| 1030 | svm_fifo_segment_private_t *seg; |
| 1031 | unix_shared_memory_queue_t *client_q; |
| 1032 | vl_api_connect_sock_reply_t *rmp; |
| 1033 | session_t *session = 0; |
| 1034 | int rv = 0; |
| 1035 | svm_fifo_t *rx_fifo; |
| 1036 | svm_fifo_t *tx_fifo; |
| 1037 | unix_shared_memory_queue_t *event_q = 0; |
| 1038 | |
| 1039 | if (!clib_fifo_free_elts (vcm->client_session_index_fifo)) |
| 1040 | { |
| 1041 | if (VPPCOM_DEBUG > 1) |
| 1042 | clib_warning ("[%d] client session queue is full!", vcm->my_pid); |
| 1043 | rv = VNET_API_ERROR_QUEUE_FULL; |
| 1044 | goto send_reply; |
| 1045 | } |
| 1046 | |
| 1047 | /* Create the segment */ |
| 1048 | memset (a, 0, sizeof (*a)); |
| 1049 | a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c", |
| 1050 | vcm->my_pid, vcm->unique_segment_index++, |
| 1051 | 0); |
| 1052 | a->segment_size = vcm->cfg.segment_size; |
| 1053 | a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs; |
| 1054 | a->rx_fifo_size = vcm->cfg.rx_fifo_size; |
| 1055 | a->tx_fifo_size = vcm->cfg.tx_fifo_size; |
| 1056 | |
| 1057 | rv = svm_fifo_segment_create (a); |
| 1058 | if (PREDICT_FALSE (rv)) |
| 1059 | { |
| 1060 | if (VPPCOM_DEBUG > 1) |
| 1061 | clib_warning ("[%d] svm_fifo_segment_create ('%s') failed", |
| 1062 | vcm->my_pid, a->segment_name); |
| 1063 | vec_reset_length (a->new_segment_indices); |
| 1064 | rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 1065 | goto send_reply; |
| 1066 | } |
| 1067 | |
| 1068 | if (VPPCOM_DEBUG > 1) |
| 1069 | clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name); |
| 1070 | |
| 1071 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1072 | pool_get (vcm->sessions, session); |
| 1073 | memset (session, 0, sizeof (*session)); |
| 1074 | session_index = session - vcm->sessions; |
| 1075 | |
| 1076 | session->sm_seg_index = a->new_segment_indices[0]; |
| 1077 | vec_reset_length (a->new_segment_indices); |
| 1078 | |
| 1079 | seg = svm_fifo_get_segment (session->sm_seg_index); |
| 1080 | rx_fifo = session->server_rx_fifo = |
| 1081 | svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size, |
| 1082 | FIFO_SEGMENT_RX_FREELIST); |
| 1083 | if (PREDICT_FALSE (!session->server_rx_fifo)) |
| 1084 | { |
| 1085 | svm_fifo_segment_delete (seg); |
| 1086 | clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)", |
| 1087 | vcm->my_pid, vcm->cfg.rx_fifo_size, |
| 1088 | vcm->cfg.rx_fifo_size); |
| 1089 | rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 1090 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1091 | goto send_reply; |
| 1092 | } |
| 1093 | |
| 1094 | tx_fifo = session->server_tx_fifo = |
| 1095 | svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size, |
| 1096 | FIFO_SEGMENT_TX_FREELIST); |
| 1097 | if (PREDICT_FALSE (!session->server_tx_fifo)) |
| 1098 | { |
| 1099 | svm_fifo_segment_delete (seg); |
| 1100 | if (VPPCOM_DEBUG > 1) |
| 1101 | clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)", |
| 1102 | vcm->my_pid, vcm->cfg.tx_fifo_size, |
| 1103 | vcm->cfg.tx_fifo_size); |
| 1104 | rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 1105 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1106 | goto send_reply; |
| 1107 | } |
| 1108 | |
| 1109 | session->server_rx_fifo->master_session_index = session_index; |
| 1110 | session->server_tx_fifo->master_session_index = session_index; |
| 1111 | session->client_queue_address = mp->client_queue_address; |
| 1112 | session->is_cut_thru = 1; |
| 1113 | session->is_server = 1; |
| 1114 | session->is_ip4 = mp->is_ip4; |
| 1115 | session->port = mp->port; |
| 1116 | { |
| 1117 | void *oldheap; |
| 1118 | ssvm_shared_header_t *sh = seg->ssvm.sh; |
| 1119 | |
| 1120 | ssvm_lock_non_recursive (sh, 1); |
| 1121 | oldheap = ssvm_push_heap (sh); |
| 1122 | event_q = session->event_queue = |
| 1123 | unix_shared_memory_queue_init (vcm->cfg.event_queue_size, |
| 1124 | sizeof (session_fifo_event_t), |
| 1125 | vcm->my_pid, 0 /* signal not sent */ ); |
| 1126 | ssvm_pop_heap (oldheap); |
| 1127 | ssvm_unlock_non_recursive (sh); |
| 1128 | } |
Dave Wallace | 6d5c4cd | 2017-08-15 16:56:29 -0400 | [diff] [blame] | 1129 | clib_memcpy (session->ip, mp->ip, sizeof (session->ip)); |
| 1130 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1131 | session->state = STATE_ACCEPT; |
| 1132 | if (VPPCOM_DEBUG > 1) |
| 1133 | clib_warning ("[%d] Connected cut-thru to client: sid %d", |
| 1134 | vcm->my_pid, session_index); |
| 1135 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1136 | clib_fifo_add1 (vcm->client_session_index_fifo, session_index); |
| 1137 | |
| 1138 | send_reply: |
| 1139 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 1140 | memset (rmp, 0, sizeof (*rmp)); |
| 1141 | |
| 1142 | rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK_REPLY); |
| 1143 | rmp->context = mp->context; |
| 1144 | rmp->app_connect = htonl (mp->app_connect); |
| 1145 | rmp->retval = htonl (rv); |
| 1146 | rmp->segment_name_length = vec_len (a->segment_name); |
| 1147 | clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name)); |
| 1148 | vec_reset_length (a->segment_name); |
| 1149 | |
| 1150 | if (event_q) |
| 1151 | { |
| 1152 | rmp->vpp_event_queue_address = pointer_to_uword (event_q); |
| 1153 | rmp->server_rx_fifo = pointer_to_uword (rx_fifo); |
| 1154 | rmp->server_tx_fifo = pointer_to_uword (tx_fifo); |
| 1155 | } |
| 1156 | client_q = |
| 1157 | uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *); |
| 1158 | |
| 1159 | ASSERT (client_q); |
| 1160 | vl_msg_api_send_shmem (client_q, (u8 *) & rmp); |
| 1161 | } |
| 1162 | |
| 1163 | static void |
| 1164 | vppcom_send_bind_sock (session_t * session) |
| 1165 | { |
| 1166 | vppcom_main_t *vcm = &vppcom_main; |
| 1167 | vl_api_bind_sock_t *bmp; |
| 1168 | |
| 1169 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 1170 | session->is_server = 1; |
| 1171 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 1172 | memset (bmp, 0, sizeof (*bmp)); |
| 1173 | |
| 1174 | bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK); |
| 1175 | bmp->client_index = vcm->my_client_index; |
| 1176 | bmp->context = htonl (0xfeedface); |
| 1177 | bmp->vrf = session->vrf; |
| 1178 | bmp->is_ip4 = session->is_ip4; |
Dave Wallace | 6d5c4cd | 2017-08-15 16:56:29 -0400 | [diff] [blame] | 1179 | clib_memcpy (bmp->ip, session->ip, sizeof (bmp->ip)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1180 | bmp->port = session->port; |
| 1181 | bmp->proto = session->proto; |
| 1182 | clib_memcpy (bmp->options, session->options, sizeof (bmp->options)); |
| 1183 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 1184 | } |
| 1185 | |
| 1186 | static void |
| 1187 | vppcom_send_unbind_sock (u32 session_index) |
| 1188 | { |
| 1189 | vppcom_main_t *vcm = &vppcom_main; |
| 1190 | vl_api_unbind_sock_t *ump; |
| 1191 | session_t *session = 0; |
| 1192 | int rv; |
| 1193 | |
| 1194 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1195 | rv = vppcom_session_at_index (session_index, &session); |
| 1196 | if (PREDICT_FALSE (rv)) |
| 1197 | { |
| 1198 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1199 | if (VPPCOM_DEBUG > 0) |
| 1200 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 1201 | vcm->my_pid, session_index); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 1206 | memset (ump, 0, sizeof (*ump)); |
| 1207 | |
| 1208 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK); |
| 1209 | ump->client_index = vcm->my_client_index; |
| 1210 | ump->handle = session->vpp_session_handle; |
| 1211 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1212 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump); |
| 1213 | } |
| 1214 | |
| 1215 | static int |
| 1216 | vppcom_session_unbind_cut_thru (session_t * session) |
| 1217 | { |
| 1218 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 1219 | svm_fifo_segment_private_t *seg; |
| 1220 | int rv = VPPCOM_OK; |
| 1221 | |
| 1222 | seg = vec_elt_at_index (sm->segments, session->sm_seg_index); |
| 1223 | svm_fifo_segment_free_fifo (seg, session->server_rx_fifo, |
| 1224 | FIFO_SEGMENT_RX_FREELIST); |
| 1225 | svm_fifo_segment_free_fifo (seg, session->server_tx_fifo, |
| 1226 | FIFO_SEGMENT_TX_FREELIST); |
| 1227 | svm_fifo_segment_delete (seg); |
| 1228 | |
| 1229 | return rv; |
| 1230 | } |
| 1231 | |
| 1232 | static int |
| 1233 | vppcom_session_unbind (u32 session_index) |
| 1234 | { |
| 1235 | vppcom_main_t *vcm = &vppcom_main; |
| 1236 | int rv; |
| 1237 | |
| 1238 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1239 | if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index))) |
| 1240 | { |
| 1241 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1242 | if (VPPCOM_DEBUG > 1) |
| 1243 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 1244 | vcm->my_pid, session_index); |
| 1245 | return VPPCOM_EBADFD; |
| 1246 | } |
| 1247 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1248 | |
| 1249 | vcm->bind_session_index = session_index; |
| 1250 | vppcom_send_unbind_sock (session_index); |
| 1251 | rv = vppcom_wait_for_session_state_change (session_index, STATE_START, |
| 1252 | vcm->cfg.session_timeout); |
| 1253 | if (PREDICT_FALSE (rv)) |
| 1254 | { |
| 1255 | vcm->bind_session_index = ~0; |
| 1256 | if (VPPCOM_DEBUG > 0) |
| 1257 | clib_warning ("[%d] server unbind timed out, rv = %s (%d)", |
| 1258 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 1259 | return rv; |
| 1260 | } |
| 1261 | return VPPCOM_OK; |
| 1262 | } |
| 1263 | |
| 1264 | static int |
| 1265 | vppcom_session_disconnect (u32 session_index) |
| 1266 | { |
| 1267 | vppcom_main_t *vcm = &vppcom_main; |
| 1268 | int rv; |
| 1269 | |
| 1270 | rv = vppcom_send_disconnect (session_index); |
| 1271 | if (PREDICT_FALSE (rv)) |
| 1272 | return rv; |
| 1273 | |
| 1274 | rv = vppcom_wait_for_session_state_change (session_index, STATE_DISCONNECT, |
| 1275 | vcm->cfg.session_timeout); |
| 1276 | if (PREDICT_FALSE (rv)) |
| 1277 | { |
| 1278 | if (VPPCOM_DEBUG > 0) |
| 1279 | clib_warning ("[%d] client disconnect timed out, rv = %s (%d)", |
| 1280 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 1281 | return rv; |
| 1282 | } |
| 1283 | |
| 1284 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1285 | pool_put_index (vcm->sessions, session_index); |
| 1286 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1287 | return VPPCOM_OK; |
| 1288 | } |
| 1289 | |
| 1290 | #define foreach_sock_msg \ |
| 1291 | _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \ |
| 1292 | _(BIND_SOCK_REPLY, bind_sock_reply) \ |
| 1293 | _(UNBIND_SOCK_REPLY, unbind_sock_reply) \ |
| 1294 | _(ACCEPT_SESSION, accept_session) \ |
| 1295 | _(CONNECT_SOCK, connect_sock) \ |
| 1296 | _(CONNECT_SOCK_REPLY, connect_sock_reply) \ |
| 1297 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 1298 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 1299 | _(RESET_SESSION, reset_session) \ |
| 1300 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 1301 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 1302 | _(MAP_ANOTHER_SEGMENT, map_another_segment) |
| 1303 | |
| 1304 | static void |
| 1305 | vppcom_api_hookup (void) |
| 1306 | { |
| 1307 | #define _(N,n) \ |
| 1308 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1309 | vl_api_##n##_t_handler, \ |
| 1310 | vl_noop_handler, \ |
| 1311 | vl_api_##n##_t_endian, \ |
| 1312 | vl_api_##n##_t_print, \ |
| 1313 | sizeof(vl_api_##n##_t), 1); |
| 1314 | foreach_sock_msg; |
| 1315 | #undef _ |
| 1316 | } |
| 1317 | |
| 1318 | static void |
| 1319 | vppcom_cfg_init (vppcom_cfg_t * vcl_cfg) |
| 1320 | { |
| 1321 | ASSERT (vcl_cfg); |
| 1322 | |
| 1323 | vcl_cfg->heapsize = (256ULL << 20); |
| 1324 | vcl_cfg->segment_baseva = 0x200000000ULL; |
| 1325 | vcl_cfg->segment_size = (256 << 20); |
| 1326 | vcl_cfg->add_segment_size = (128 << 20); |
| 1327 | vcl_cfg->preallocated_fifo_pairs = 8; |
| 1328 | vcl_cfg->rx_fifo_size = (1 << 20); |
| 1329 | vcl_cfg->tx_fifo_size = (1 << 20); |
| 1330 | vcl_cfg->event_queue_size = 2048; |
| 1331 | vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32); |
| 1332 | vcl_cfg->app_timeout = 10 * 60.0; |
| 1333 | vcl_cfg->session_timeout = 10 * 60.0; |
| 1334 | vcl_cfg->accept_timeout = 60.0; |
| 1335 | } |
| 1336 | |
| 1337 | static void |
| 1338 | vppcom_cfg_heapsize (char *conf_fname) |
| 1339 | { |
| 1340 | vppcom_main_t *vcm = &vppcom_main; |
| 1341 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 1342 | FILE *fp; |
| 1343 | char inbuf[4096]; |
| 1344 | int argc = 1; |
| 1345 | char **argv = NULL; |
| 1346 | char *arg = NULL; |
| 1347 | char *p; |
| 1348 | int i; |
| 1349 | u8 *sizep; |
| 1350 | u32 size; |
| 1351 | |
| 1352 | fp = fopen (conf_fname, "r"); |
| 1353 | if (fp == NULL) |
| 1354 | { |
| 1355 | if (VPPCOM_DEBUG > 0) |
| 1356 | fprintf (stderr, "open configuration file '%s' failed\n", conf_fname); |
| 1357 | goto defaulted; |
| 1358 | } |
| 1359 | argv = calloc (1, sizeof (char *)); |
| 1360 | if (argv == NULL) |
| 1361 | goto defaulted; |
| 1362 | |
| 1363 | while (1) |
| 1364 | { |
| 1365 | if (fgets (inbuf, 4096, fp) == 0) |
| 1366 | break; |
| 1367 | p = strtok (inbuf, " \t\n"); |
| 1368 | while (p != NULL) |
| 1369 | { |
| 1370 | if (*p == '#') |
| 1371 | break; |
| 1372 | argc++; |
| 1373 | char **tmp = realloc (argv, argc * sizeof (char *)); |
| 1374 | if (tmp == NULL) |
| 1375 | { |
| 1376 | fclose (fp); |
| 1377 | goto defaulted; |
| 1378 | } |
| 1379 | argv = tmp; |
| 1380 | arg = strndup (p, 1024); |
| 1381 | if (arg == NULL) |
| 1382 | { |
| 1383 | fclose (fp); |
| 1384 | goto defaulted; |
| 1385 | } |
| 1386 | argv[argc - 1] = arg; |
| 1387 | p = strtok (NULL, " \t\n"); |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | fclose (fp); |
| 1392 | |
| 1393 | char **tmp = realloc (argv, (argc + 1) * sizeof (char *)); |
| 1394 | if (tmp == NULL) |
| 1395 | goto defaulted; |
| 1396 | argv = tmp; |
| 1397 | argv[argc] = NULL; |
| 1398 | |
| 1399 | /* |
| 1400 | * Look for and parse the "heapsize" config parameter. |
| 1401 | * Manual since none of the clib infra has been bootstrapped yet. |
| 1402 | * |
| 1403 | * Format: heapsize <nn>[mM][gG] |
| 1404 | */ |
| 1405 | |
| 1406 | for (i = 1; i < (argc - 1); i++) |
| 1407 | { |
| 1408 | if (!strncmp (argv[i], "heapsize", 8)) |
| 1409 | { |
| 1410 | sizep = (u8 *) argv[i + 1]; |
| 1411 | size = 0; |
| 1412 | while (*sizep >= '0' && *sizep <= '9') |
| 1413 | { |
| 1414 | size *= 10; |
| 1415 | size += *sizep++ - '0'; |
| 1416 | } |
| 1417 | if (size == 0) |
| 1418 | { |
| 1419 | if (VPPCOM_DEBUG > 0) |
| 1420 | clib_warning ("[%d] parse error '%s %s', " |
| 1421 | "using default heapsize %lld (0x%llx)", |
| 1422 | vcm->my_pid, argv[i], argv[i + 1], |
| 1423 | vcl_cfg->heapsize, vcl_cfg->heapsize); |
| 1424 | goto defaulted; |
| 1425 | } |
| 1426 | |
| 1427 | if (*sizep == 'g' || *sizep == 'G') |
| 1428 | vcl_cfg->heapsize = size << 30; |
| 1429 | else if (*sizep == 'm' || *sizep == 'M') |
| 1430 | vcl_cfg->heapsize = size << 20; |
| 1431 | else |
| 1432 | { |
| 1433 | if (VPPCOM_DEBUG > 0) |
| 1434 | clib_warning ("[%d] parse error '%s %s', " |
| 1435 | "using default heapsize %lld (0x%llx)", |
| 1436 | vcm->my_pid, argv[i], argv[i + 1], |
| 1437 | vcl_cfg->heapsize, vcl_cfg->heapsize); |
| 1438 | goto defaulted; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | defaulted: |
| 1444 | if (!clib_mem_init (0, vcl_cfg->heapsize)) |
| 1445 | clib_warning ("[%d] vppcom heap allocation failure!", vcm->my_pid); |
| 1446 | else if (VPPCOM_DEBUG > 0) |
| 1447 | clib_warning ("[%d] allocated vppcom heapsize %lld (0x%llx)", |
| 1448 | vcm->my_pid, vcl_cfg->heapsize, vcl_cfg->heapsize); |
| 1449 | } |
| 1450 | |
| 1451 | static void |
| 1452 | vppcom_cfg_read (char *conf_fname) |
| 1453 | { |
| 1454 | vppcom_main_t *vcm = &vppcom_main; |
| 1455 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 1456 | int fd; |
| 1457 | unformat_input_t _input, *input = &_input; |
| 1458 | unformat_input_t _line_input, *line_input = &_line_input; |
| 1459 | u8 vc_cfg_input = 0; |
| 1460 | u8 *chroot_path; |
| 1461 | struct stat s; |
| 1462 | u32 uid, gid; |
| 1463 | |
| 1464 | fd = open (conf_fname, O_RDONLY); |
| 1465 | if (fd < 0) |
| 1466 | { |
| 1467 | if (VPPCOM_DEBUG > 0) |
| 1468 | clib_warning ("[%d] open configuration file '%s' failed!", |
| 1469 | vcm->my_pid, conf_fname); |
| 1470 | goto file_done; |
| 1471 | } |
| 1472 | |
| 1473 | if (fstat (fd, &s) < 0) |
| 1474 | { |
| 1475 | if (VPPCOM_DEBUG > 0) |
| 1476 | clib_warning ("[%d] failed to stat `%s'", vcm->my_pid, conf_fname); |
| 1477 | goto file_done; |
| 1478 | } |
| 1479 | |
| 1480 | if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) |
| 1481 | { |
| 1482 | if (VPPCOM_DEBUG > 0) |
| 1483 | clib_warning ("[%d] not a regular file `%s'", vcm->my_pid, |
| 1484 | conf_fname); |
| 1485 | goto file_done; |
| 1486 | } |
| 1487 | |
| 1488 | unformat_init_unix_file (input, fd); |
| 1489 | |
| 1490 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1491 | { |
| 1492 | unformat_user (input, unformat_line_input, line_input); |
| 1493 | unformat_skip_white_space (line_input); |
| 1494 | |
| 1495 | if (unformat (line_input, "vppcom {")) |
| 1496 | { |
| 1497 | vc_cfg_input = 1; |
| 1498 | continue; |
| 1499 | } |
| 1500 | |
| 1501 | if (vc_cfg_input) |
| 1502 | { |
| 1503 | if (unformat (line_input, "heapsize %s", &chroot_path)) |
| 1504 | { |
| 1505 | vec_terminate_c_string (chroot_path); |
| 1506 | if (VPPCOM_DEBUG > 0) |
| 1507 | clib_warning ("[%d] configured heapsize %s, " |
| 1508 | "actual heapsize %lld (0x%llx)", |
| 1509 | vcm->my_pid, chroot_path, vcl_cfg->heapsize, |
| 1510 | vcl_cfg->heapsize); |
| 1511 | vec_free (chroot_path); |
| 1512 | } |
| 1513 | else if (unformat (line_input, "api-prefix %s", &chroot_path)) |
| 1514 | { |
| 1515 | vec_terminate_c_string (chroot_path); |
| 1516 | vl_set_memory_root_path ((char *) chroot_path); |
| 1517 | if (VPPCOM_DEBUG > 0) |
| 1518 | clib_warning ("[%d] configured api-prefix %s", |
| 1519 | vcm->my_pid, chroot_path); |
| 1520 | chroot_path = 0; /* Don't vec_free() it! */ |
| 1521 | } |
| 1522 | else if (unformat (line_input, "uid %d", &uid)) |
| 1523 | { |
| 1524 | vl_set_memory_uid (uid); |
| 1525 | if (VPPCOM_DEBUG > 0) |
| 1526 | clib_warning ("[%d] configured uid %d", vcm->my_pid, uid); |
| 1527 | } |
| 1528 | else if (unformat (line_input, "gid %d", &gid)) |
| 1529 | { |
| 1530 | vl_set_memory_gid (gid); |
| 1531 | if (VPPCOM_DEBUG > 0) |
| 1532 | clib_warning ("[%d] configured gid %d", vcm->my_pid, gid); |
| 1533 | } |
| 1534 | else if (unformat (line_input, "segment-baseva 0x%llx", |
| 1535 | &vcl_cfg->segment_baseva)) |
| 1536 | { |
| 1537 | if (VPPCOM_DEBUG > 0) |
| 1538 | clib_warning ("[%d] configured segment_baseva 0x%llx", |
| 1539 | vcm->my_pid, vcl_cfg->segment_baseva); |
| 1540 | } |
| 1541 | else if (unformat (line_input, "segment-size 0x%lx", |
| 1542 | &vcl_cfg->segment_size)) |
| 1543 | { |
| 1544 | if (VPPCOM_DEBUG > 0) |
| 1545 | clib_warning ("[%d] configured segment_size 0x%lx (%ld)", |
| 1546 | vcm->my_pid, vcl_cfg->segment_size, |
| 1547 | vcl_cfg->segment_size); |
| 1548 | } |
| 1549 | else if (unformat (line_input, "segment-size %ld", |
| 1550 | &vcl_cfg->segment_size)) |
| 1551 | { |
| 1552 | if (VPPCOM_DEBUG > 0) |
| 1553 | clib_warning ("[%d] configured segment_size %ld (0x%lx)", |
| 1554 | vcm->my_pid, vcl_cfg->segment_size, |
| 1555 | vcl_cfg->segment_size); |
| 1556 | } |
| 1557 | else if (unformat (line_input, "add-segment-size 0x%lx", |
| 1558 | &vcl_cfg->add_segment_size)) |
| 1559 | { |
| 1560 | if (VPPCOM_DEBUG > 0) |
| 1561 | clib_warning |
| 1562 | ("[%d] configured add_segment_size 0x%lx (%ld)", |
| 1563 | vcm->my_pid, vcl_cfg->add_segment_size, |
| 1564 | vcl_cfg->add_segment_size); |
| 1565 | } |
| 1566 | else if (unformat (line_input, "add-segment-size %ld", |
| 1567 | &vcl_cfg->add_segment_size)) |
| 1568 | { |
| 1569 | if (VPPCOM_DEBUG > 0) |
| 1570 | clib_warning |
| 1571 | ("[%d] configured add_segment_size %ld (0x%lx)", |
| 1572 | vcm->my_pid, vcl_cfg->add_segment_size, |
| 1573 | vcl_cfg->add_segment_size); |
| 1574 | } |
| 1575 | else if (unformat (line_input, "preallocated-fifo-pairs %d", |
| 1576 | &vcl_cfg->preallocated_fifo_pairs)) |
| 1577 | { |
| 1578 | if (VPPCOM_DEBUG > 0) |
| 1579 | clib_warning ("[%d] configured preallocated_fifo_pairs " |
| 1580 | "%d (0x%x)", vcm->my_pid, |
| 1581 | vcl_cfg->preallocated_fifo_pairs, |
| 1582 | vcl_cfg->preallocated_fifo_pairs); |
| 1583 | } |
| 1584 | else if (unformat (line_input, "rx-fifo-size 0x%lx", |
| 1585 | &vcl_cfg->rx_fifo_size)) |
| 1586 | { |
| 1587 | if (VPPCOM_DEBUG > 0) |
| 1588 | clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)", |
| 1589 | vcm->my_pid, vcl_cfg->rx_fifo_size, |
| 1590 | vcl_cfg->rx_fifo_size); |
| 1591 | } |
| 1592 | else if (unformat (line_input, "rx-fifo-size %ld", |
| 1593 | &vcl_cfg->rx_fifo_size)) |
| 1594 | { |
| 1595 | if (VPPCOM_DEBUG > 0) |
| 1596 | clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)", |
| 1597 | vcm->my_pid, vcl_cfg->rx_fifo_size, |
| 1598 | vcl_cfg->rx_fifo_size); |
| 1599 | } |
| 1600 | else if (unformat (line_input, "tx-fifo-size 0x%lx", |
| 1601 | &vcl_cfg->tx_fifo_size)) |
| 1602 | { |
| 1603 | if (VPPCOM_DEBUG > 0) |
| 1604 | clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)", |
| 1605 | vcm->my_pid, vcl_cfg->tx_fifo_size, |
| 1606 | vcl_cfg->tx_fifo_size); |
| 1607 | } |
| 1608 | else if (unformat (line_input, "tx-fifo-size %ld", |
| 1609 | &vcl_cfg->tx_fifo_size)) |
| 1610 | { |
| 1611 | if (VPPCOM_DEBUG > 0) |
| 1612 | clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)", |
| 1613 | vcm->my_pid, vcl_cfg->tx_fifo_size, |
| 1614 | vcl_cfg->tx_fifo_size); |
| 1615 | } |
| 1616 | else if (unformat (line_input, "event-queue-size 0x%lx", |
| 1617 | &vcl_cfg->event_queue_size)) |
| 1618 | { |
| 1619 | if (VPPCOM_DEBUG > 0) |
| 1620 | clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)", |
| 1621 | vcm->my_pid, vcl_cfg->event_queue_size, |
| 1622 | vcl_cfg->event_queue_size); |
| 1623 | } |
| 1624 | else if (unformat (line_input, "event-queue-size %ld", |
| 1625 | &vcl_cfg->event_queue_size)) |
| 1626 | { |
| 1627 | if (VPPCOM_DEBUG > 0) |
| 1628 | clib_warning ("[%d] configured event_queue_size %ld (0x%lx)", |
| 1629 | vcm->my_pid, vcl_cfg->event_queue_size, |
| 1630 | vcl_cfg->event_queue_size); |
| 1631 | } |
| 1632 | else if (unformat (line_input, "listen-queue-size 0x%lx", |
| 1633 | &vcl_cfg->listen_queue_size)) |
| 1634 | { |
| 1635 | if (VPPCOM_DEBUG > 0) |
| 1636 | clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)", |
| 1637 | vcm->my_pid, vcl_cfg->listen_queue_size, |
| 1638 | vcl_cfg->listen_queue_size); |
| 1639 | } |
| 1640 | else if (unformat (line_input, "listen-queue-size %ld", |
| 1641 | &vcl_cfg->listen_queue_size)) |
| 1642 | { |
| 1643 | if (VPPCOM_DEBUG > 0) |
| 1644 | clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)", |
| 1645 | vcm->my_pid, vcl_cfg->listen_queue_size, |
| 1646 | vcl_cfg->listen_queue_size); |
| 1647 | } |
| 1648 | else if (unformat (line_input, "app-timeout %f", |
| 1649 | &vcl_cfg->app_timeout)) |
| 1650 | { |
| 1651 | if (VPPCOM_DEBUG > 0) |
| 1652 | clib_warning ("[%d] configured app_timeout %f", |
| 1653 | vcm->my_pid, vcl_cfg->app_timeout); |
| 1654 | } |
| 1655 | else if (unformat (line_input, "session-timeout %f", |
| 1656 | &vcl_cfg->session_timeout)) |
| 1657 | { |
| 1658 | if (VPPCOM_DEBUG > 0) |
| 1659 | clib_warning ("[%d] configured session_timeout %f", |
| 1660 | vcm->my_pid, vcl_cfg->session_timeout); |
| 1661 | } |
| 1662 | else if (unformat (line_input, "accept-timeout %f", |
| 1663 | &vcl_cfg->accept_timeout)) |
| 1664 | { |
| 1665 | if (VPPCOM_DEBUG > 0) |
| 1666 | clib_warning ("[%d] configured accept_timeout %f", |
| 1667 | vcm->my_pid, vcl_cfg->accept_timeout); |
| 1668 | } |
| 1669 | else if (unformat (line_input, "}")) |
| 1670 | { |
| 1671 | vc_cfg_input = 0; |
| 1672 | if (VPPCOM_DEBUG > 0) |
| 1673 | clib_warning ("[%d] completed parsing vppcom config!", |
| 1674 | vcm->my_pid); |
| 1675 | goto input_done; |
| 1676 | } |
| 1677 | else |
| 1678 | { |
| 1679 | if (line_input->buffer[line_input->index] != '#') |
| 1680 | { |
| 1681 | clib_warning ("[%d] Unknown vppcom config option: '%s'", |
| 1682 | vcm->my_pid, (char *) |
| 1683 | &line_input->buffer[line_input->index]); |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | input_done: |
| 1690 | unformat_free (input); |
| 1691 | |
| 1692 | file_done: |
| 1693 | if (fd > 0) |
| 1694 | close (fd); |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * VPPCOM Public API functions |
| 1699 | */ |
| 1700 | int |
| 1701 | vppcom_app_create (char *app_name) |
| 1702 | { |
| 1703 | vppcom_main_t *vcm = &vppcom_main; |
| 1704 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 1705 | u8 *heap; |
| 1706 | mheap_t *h; |
| 1707 | int rv; |
| 1708 | |
| 1709 | if (!vcm->init) |
| 1710 | { |
| 1711 | char *conf_fname; |
| 1712 | |
| 1713 | vcm->init = 1; |
| 1714 | vcm->my_pid = getpid (); |
| 1715 | clib_fifo_validate (vcm->client_session_index_fifo, |
| 1716 | vcm->cfg.listen_queue_size); |
| 1717 | vppcom_cfg_init (vcl_cfg); |
| 1718 | conf_fname = getenv (VPPCOM_CONF_ENV); |
| 1719 | if (!conf_fname) |
| 1720 | { |
| 1721 | conf_fname = VPPCOM_CONF_DEFAULT; |
| 1722 | if (VPPCOM_DEBUG > 0) |
| 1723 | clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid, |
| 1724 | VPPCOM_CONF_ENV); |
| 1725 | } |
| 1726 | vppcom_cfg_heapsize (conf_fname); |
| 1727 | vppcom_cfg_read (conf_fname); |
| 1728 | vcm->bind_session_index = ~0; |
| 1729 | vcm->main_cpu = os_get_thread_index (); |
| 1730 | heap = clib_mem_get_per_cpu_heap (); |
| 1731 | h = mheap_header (heap); |
| 1732 | |
| 1733 | /* make the main heap thread-safe */ |
| 1734 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 1735 | |
| 1736 | vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
| 1737 | |
| 1738 | clib_time_init (&vcm->clib_time); |
| 1739 | vppcom_init_error_string_table (); |
| 1740 | svm_fifo_segment_init (vcl_cfg->segment_baseva, |
| 1741 | 20 /* timeout in secs */ ); |
| 1742 | clib_spinlock_init (&vcm->sessions_lockp); |
| 1743 | vppcom_api_hookup (); |
| 1744 | } |
| 1745 | |
| 1746 | if (vcm->my_client_index == ~0) |
| 1747 | { |
| 1748 | vcm->app_state = STATE_APP_START; |
| 1749 | rv = vppcom_connect_to_vpp (app_name); |
| 1750 | if (rv) |
| 1751 | { |
| 1752 | clib_warning ("[%s] couldn't connect to VPP.", vcm->my_pid); |
| 1753 | return rv; |
| 1754 | } |
| 1755 | |
| 1756 | if (VPPCOM_DEBUG > 0) |
| 1757 | clib_warning ("[%d] sending session enable", vcm->my_pid); |
| 1758 | |
| 1759 | rv = vppcom_app_session_enable (); |
| 1760 | if (rv) |
| 1761 | { |
| 1762 | clib_warning ("[%d] vppcom_app_session_enable() failed!", |
| 1763 | vcm->my_pid); |
| 1764 | return rv; |
| 1765 | } |
| 1766 | |
| 1767 | if (VPPCOM_DEBUG > 0) |
| 1768 | clib_warning ("[%d] sending app attach", vcm->my_pid); |
| 1769 | |
| 1770 | rv = vppcom_app_attach (); |
| 1771 | if (rv) |
| 1772 | { |
| 1773 | clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid); |
| 1774 | return rv; |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | if (VPPCOM_DEBUG > 0) |
| 1779 | clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)", |
| 1780 | vcm->my_pid, app_name, vcm->my_client_index, |
| 1781 | vcm->my_client_index); |
| 1782 | |
| 1783 | return VPPCOM_OK; |
| 1784 | } |
| 1785 | |
| 1786 | void |
| 1787 | vppcom_app_destroy (void) |
| 1788 | { |
| 1789 | vppcom_main_t *vcm = &vppcom_main; |
| 1790 | int rv; |
| 1791 | |
| 1792 | if (vcm->my_client_index == ~0) |
| 1793 | return; |
| 1794 | |
| 1795 | if (VPPCOM_DEBUG > 0) |
| 1796 | clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)", |
| 1797 | vcm->my_pid, vcm->my_client_index, vcm->my_client_index); |
| 1798 | |
| 1799 | vppcom_app_detach (); |
| 1800 | rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED); |
| 1801 | if (PREDICT_FALSE (rv)) |
| 1802 | { |
| 1803 | if (VPPCOM_DEBUG > 0) |
| 1804 | clib_warning ("[%d] application detach timed out, rv = %s (%d)", |
| 1805 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 1806 | } |
| 1807 | vl_client_disconnect_from_vlib (); |
| 1808 | vcm->my_client_index = ~0; |
| 1809 | vcm->app_state = STATE_APP_START; |
| 1810 | } |
| 1811 | |
| 1812 | int |
| 1813 | vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking) |
| 1814 | { |
| 1815 | vppcom_main_t *vcm = &vppcom_main; |
| 1816 | session_t *session; |
| 1817 | u32 session_index; |
| 1818 | |
| 1819 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1820 | pool_get (vcm->sessions, session); |
| 1821 | session_index = session - vcm->sessions; |
| 1822 | |
| 1823 | session->vrf = vrf; |
| 1824 | session->proto = proto; |
| 1825 | session->state = STATE_START; |
| 1826 | session->is_nonblocking = is_nonblocking; |
| 1827 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1828 | |
| 1829 | if (VPPCOM_DEBUG > 0) |
| 1830 | clib_warning ("[%d] sid %d", vcm->my_pid, session_index); |
| 1831 | |
| 1832 | return (int) session_index; |
| 1833 | } |
| 1834 | |
| 1835 | int |
| 1836 | vppcom_session_close (uint32_t session_index) |
| 1837 | { |
| 1838 | vppcom_main_t *vcm = &vppcom_main; |
| 1839 | session_t *session = 0; |
| 1840 | int rv; |
| 1841 | |
| 1842 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1843 | rv = vppcom_session_at_index (session_index, &session); |
| 1844 | if (PREDICT_FALSE (rv)) |
| 1845 | { |
| 1846 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1847 | if (VPPCOM_DEBUG > 0) |
| 1848 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 1849 | vcm->my_pid, session_index); |
| 1850 | return rv; |
| 1851 | } |
| 1852 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1853 | |
| 1854 | if (VPPCOM_DEBUG > 0) |
| 1855 | clib_warning ("[%d] sid %d", vcm->my_pid, session_index); |
| 1856 | |
| 1857 | if (session->is_cut_thru) |
| 1858 | { |
| 1859 | if (session->is_server) |
| 1860 | rv = vppcom_session_unbind_cut_thru (session); |
| 1861 | } |
| 1862 | else |
| 1863 | { |
| 1864 | rv = (session->is_server) ? |
| 1865 | vppcom_session_unbind (session_index) : |
| 1866 | vppcom_session_disconnect (session_index); |
| 1867 | } |
| 1868 | |
| 1869 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1870 | pool_put_index (vcm->sessions, session_index); |
| 1871 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1872 | return rv; |
| 1873 | } |
| 1874 | |
| 1875 | int |
| 1876 | vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep) |
| 1877 | { |
| 1878 | vppcom_main_t *vcm = &vppcom_main; |
| 1879 | session_t *session = 0; |
| 1880 | int rv; |
| 1881 | |
| 1882 | if (!ep || !ep->ip) |
| 1883 | return VPPCOM_EINVAL; |
| 1884 | |
| 1885 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1886 | rv = vppcom_session_at_index (session_index, &session); |
| 1887 | if (PREDICT_FALSE (rv)) |
| 1888 | { |
| 1889 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1890 | if (VPPCOM_DEBUG > 0) |
| 1891 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 1892 | vcm->my_pid, session_index); |
| 1893 | return rv; |
| 1894 | } |
| 1895 | |
| 1896 | if (VPPCOM_DEBUG > 0) |
| 1897 | clib_warning ("[%d] sid %d", vcm->my_pid, session_index); |
| 1898 | |
| 1899 | session->vrf = ep->vrf; |
| 1900 | session->is_ip4 = ep->is_ip4; |
| 1901 | memset (session->ip, 0, sizeof (*session->ip)); |
Dave Wallace | 6d5c4cd | 2017-08-15 16:56:29 -0400 | [diff] [blame] | 1902 | clib_memcpy (session->ip, ep->ip, sizeof (session->ip)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1903 | session->port = ep->port; |
| 1904 | |
| 1905 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1906 | return VPPCOM_OK; |
| 1907 | } |
| 1908 | |
| 1909 | int |
| 1910 | vppcom_session_listen (uint32_t session_index, uint32_t q_len) |
| 1911 | { |
| 1912 | vppcom_main_t *vcm = &vppcom_main; |
| 1913 | session_t *session = 0; |
| 1914 | int rv; |
| 1915 | |
| 1916 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1917 | rv = vppcom_session_at_index (session_index, &session); |
| 1918 | if (PREDICT_FALSE (rv)) |
| 1919 | { |
| 1920 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1921 | if (VPPCOM_DEBUG > 0) |
| 1922 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 1923 | vcm->my_pid, session_index); |
| 1924 | return rv; |
| 1925 | } |
| 1926 | |
| 1927 | if (VPPCOM_DEBUG > 0) |
| 1928 | clib_warning ("[%d] sid %d", vcm->my_pid, session_index); |
| 1929 | |
| 1930 | ASSERT (vcm->bind_session_index == ~0); |
| 1931 | vcm->bind_session_index = session_index; |
| 1932 | vppcom_send_bind_sock (session); |
| 1933 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1934 | rv = vppcom_wait_for_session_state_change (session_index, STATE_LISTEN, |
| 1935 | vcm->cfg.session_timeout); |
| 1936 | if (PREDICT_FALSE (rv)) |
| 1937 | { |
| 1938 | vcm->bind_session_index = ~0; |
| 1939 | if (VPPCOM_DEBUG > 0) |
| 1940 | clib_warning ("[%d] server listen timed out, rv = %d (%d)", |
| 1941 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 1942 | return rv; |
| 1943 | } |
| 1944 | |
| 1945 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1946 | rv = vppcom_session_at_index (session_index, &session); |
| 1947 | if (PREDICT_FALSE (rv)) |
| 1948 | { |
| 1949 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1950 | if (VPPCOM_DEBUG > 0) |
| 1951 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 1952 | vcm->my_pid, session_index); |
| 1953 | return rv; |
| 1954 | } |
| 1955 | session->is_listen = 1; |
| 1956 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1957 | clib_fifo_validate (vcm->client_session_index_fifo, q_len); |
| 1958 | |
| 1959 | return VPPCOM_OK; |
| 1960 | } |
| 1961 | |
| 1962 | int |
| 1963 | vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep, |
| 1964 | double wait_for_time) |
| 1965 | { |
| 1966 | vppcom_main_t *vcm = &vppcom_main; |
| 1967 | session_t *session = 0; |
| 1968 | u32 client_session_index; |
| 1969 | int rv; |
| 1970 | f64 wait_for; |
| 1971 | |
| 1972 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1973 | rv = vppcom_session_at_index (listen_session_index, &session); |
| 1974 | if (PREDICT_FALSE (rv)) |
| 1975 | { |
| 1976 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1977 | if (VPPCOM_DEBUG > 0) |
| 1978 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 1979 | vcm->my_pid, listen_session_index); |
| 1980 | return rv; |
| 1981 | } |
| 1982 | |
| 1983 | if (session->state != STATE_LISTEN) |
| 1984 | { |
| 1985 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1986 | if (VPPCOM_DEBUG > 0) |
| 1987 | clib_warning ("[%d] session not in listen state, state = %s", |
| 1988 | vcm->my_pid, vppcom_session_state_str (session->state)); |
| 1989 | return VPPCOM_EBADFD; |
| 1990 | } |
| 1991 | wait_for = session->is_nonblocking ? 0 : |
| 1992 | (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time; |
| 1993 | |
| 1994 | if (VPPCOM_DEBUG > 0) |
| 1995 | clib_warning ("[%d] sid %d, state %s (%d)", vcm->my_pid, |
| 1996 | listen_session_index, |
| 1997 | vppcom_session_state_str (session->state), session->state); |
| 1998 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1999 | |
| 2000 | while (1) |
| 2001 | { |
| 2002 | rv = vppcom_wait_for_client_session_index (wait_for); |
| 2003 | if (rv) |
| 2004 | { |
| 2005 | if ((VPPCOM_DEBUG > 0)) |
| 2006 | clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)", |
| 2007 | vcm->my_pid, listen_session_index, |
| 2008 | vppcom_retval_str (rv), rv); |
| 2009 | if ((wait_for == 0) || (wait_for_time > 0)) |
| 2010 | return rv; |
| 2011 | } |
| 2012 | else |
| 2013 | break; |
| 2014 | } |
| 2015 | |
| 2016 | clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index); |
| 2017 | |
| 2018 | session = 0; |
| 2019 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2020 | rv = vppcom_session_at_index (client_session_index, &session); |
| 2021 | ASSERT (rv == VPPCOM_OK); |
| 2022 | ASSERT (session->is_server); |
| 2023 | |
| 2024 | if (VPPCOM_DEBUG > 0) |
| 2025 | clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid, |
| 2026 | client_session_index); |
| 2027 | |
| 2028 | ep->vrf = session->vrf; |
| 2029 | ep->is_cut_thru = session->is_cut_thru; |
| 2030 | ep->is_ip4 = session->is_ip4; |
| 2031 | ep->port = session->port; |
| 2032 | memset (ep->ip, 0, sizeof (ip6_address_t)); |
Dave Wallace | 6d5c4cd | 2017-08-15 16:56:29 -0400 | [diff] [blame] | 2033 | clib_memcpy (ep->ip, session->ip, sizeof (ip6_address_t)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2034 | session->state = STATE_LISTEN; |
| 2035 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2036 | return (int) client_session_index; |
| 2037 | } |
| 2038 | |
| 2039 | int |
| 2040 | vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep) |
| 2041 | { |
| 2042 | vppcom_main_t *vcm = &vppcom_main; |
| 2043 | session_t *session = 0; |
| 2044 | int rv; |
| 2045 | ip46_address_t *ip46; |
| 2046 | |
| 2047 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2048 | rv = vppcom_session_at_index (session_index, &session); |
| 2049 | if (PREDICT_FALSE (rv)) |
| 2050 | { |
| 2051 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2052 | if (VPPCOM_DEBUG > 0) |
| 2053 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 2054 | vcm->my_pid, session_index); |
| 2055 | return rv; |
| 2056 | } |
| 2057 | |
| 2058 | if (session->state == STATE_CONNECT) |
| 2059 | { |
| 2060 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2061 | if (VPPCOM_DEBUG > 0) |
| 2062 | clib_warning ("[%d] session, sid (%d) already connected!", |
| 2063 | vcm->my_pid, session_index); |
| 2064 | return VPPCOM_OK; |
| 2065 | } |
| 2066 | |
| 2067 | session->vrf = server_ep->vrf; |
| 2068 | session->is_ip4 = server_ep->is_ip4; |
| 2069 | ip46 = (ip46_address_t *) session->ip; |
| 2070 | *ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip); |
| 2071 | session->port = server_ep->port; |
| 2072 | |
| 2073 | if (VPPCOM_DEBUG > 0) |
| 2074 | { |
| 2075 | u8 *ip_str = format (0, "%U", format_ip46_address, |
| 2076 | &session->ip, session->is_ip4); |
| 2077 | clib_warning ("[%d] connect sid %d to %s server port %d", |
| 2078 | vcm->my_pid, session_index, ip_str, |
| 2079 | clib_net_to_host_u16 (session->port)); |
| 2080 | vec_free (ip_str); |
| 2081 | } |
| 2082 | |
| 2083 | vppcom_send_connect_sock (session, session_index); |
| 2084 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2085 | rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT, |
| 2086 | vcm->cfg.session_timeout); |
| 2087 | if (PREDICT_FALSE (rv)) |
| 2088 | { |
| 2089 | if (VPPCOM_DEBUG > 0) |
| 2090 | clib_warning ("[%d] connect timed out, rv = %s (%d)", |
| 2091 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 2092 | return rv; |
| 2093 | } |
| 2094 | return VPPCOM_OK; |
| 2095 | } |
| 2096 | |
| 2097 | int |
| 2098 | vppcom_session_read (uint32_t session_index, void *buf, int n) |
| 2099 | { |
| 2100 | session_fifo_event_t _e, *e = &_e; |
| 2101 | vppcom_main_t *vcm = &vppcom_main; |
| 2102 | session_t *session = 0; |
| 2103 | svm_fifo_t *rx_fifo; |
| 2104 | int n_read = 0; |
| 2105 | int rv; |
| 2106 | char *fifo_str; |
| 2107 | |
| 2108 | ASSERT (buf); |
| 2109 | |
| 2110 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2111 | rv = vppcom_session_at_index (session_index, &session); |
| 2112 | if (PREDICT_FALSE (rv)) |
| 2113 | { |
| 2114 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2115 | if (VPPCOM_DEBUG > 0) |
| 2116 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 2117 | vcm->my_pid, session_index); |
| 2118 | return rv; |
| 2119 | } |
| 2120 | |
| 2121 | if (session->is_cut_thru) |
| 2122 | { |
| 2123 | rx_fifo = session->is_server ? session->server_rx_fifo : |
| 2124 | session->server_tx_fifo; |
| 2125 | fifo_str = session->is_server ? "server_rx_fifo" : "server_tx_fifo"; |
| 2126 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2127 | |
| 2128 | n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf); |
| 2129 | |
| 2130 | if (n_read <= 0) |
| 2131 | return VPPCOM_EAGAIN; |
| 2132 | |
| 2133 | } |
| 2134 | else |
| 2135 | { |
| 2136 | rv = unix_shared_memory_queue_sub (session->event_queue, (u8 *) e, |
| 2137 | 1 /* nowait */ ); |
| 2138 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2139 | if (rv < 0) |
| 2140 | return VPPCOM_EAGAIN; |
| 2141 | |
| 2142 | switch (e->event_type) |
| 2143 | { |
| 2144 | case FIFO_EVENT_APP_RX: |
| 2145 | rx_fifo = e->fifo; |
| 2146 | fifo_str = "app_rx_fifo"; |
| 2147 | n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf); |
| 2148 | break; |
| 2149 | |
| 2150 | case FIFO_EVENT_DISCONNECT: |
| 2151 | return VPPCOM_ECONNRESET; |
| 2152 | |
| 2153 | default: |
| 2154 | if (VPPCOM_DEBUG > 0) |
| 2155 | clib_warning ("[%d] unknown event type %d", vcm->my_pid, |
| 2156 | e->event_type); |
| 2157 | return VPPCOM_EAGAIN; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | if (VPPCOM_DEBUG > 2) |
| 2162 | clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid, |
| 2163 | session_index, n_read, fifo_str, rx_fifo); |
| 2164 | return n_read; |
| 2165 | } |
| 2166 | |
| 2167 | static inline int |
| 2168 | vppcom_session_read_ready (session_t * session, u32 session_index) |
| 2169 | { |
| 2170 | session_fifo_event_t _e, *e = &_e; |
| 2171 | vppcom_main_t *vcm = &vppcom_main; |
| 2172 | svm_fifo_t *rx_fifo; |
| 2173 | int rv; |
| 2174 | int ready = 0; |
| 2175 | |
| 2176 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 2177 | if (session->is_cut_thru) |
| 2178 | { |
| 2179 | rx_fifo = session->is_server ? session->server_rx_fifo : |
| 2180 | session->server_tx_fifo; |
| 2181 | |
| 2182 | ready = svm_fifo_max_dequeue (rx_fifo); |
| 2183 | } |
| 2184 | else if (session->is_listen) |
| 2185 | ready = clib_fifo_elts (vcm->client_session_index_fifo); |
| 2186 | else |
| 2187 | { |
| 2188 | rv = unix_shared_memory_queue_sub (vcm->app_event_queue, (u8 *) e, |
| 2189 | 1 /* nowait */ ); |
| 2190 | if (rv >= 0) |
| 2191 | { |
| 2192 | switch (e->event_type) |
| 2193 | { |
| 2194 | case FIFO_EVENT_APP_RX: |
| 2195 | rx_fifo = e->fifo; |
| 2196 | ready = svm_fifo_max_dequeue (rx_fifo); |
| 2197 | break; |
| 2198 | |
| 2199 | case FIFO_EVENT_DISCONNECT: |
| 2200 | return VPPCOM_ECONNRESET; |
| 2201 | |
| 2202 | default: |
| 2203 | clib_warning ("[%d] unknown event type %d", vcm->my_pid, |
| 2204 | e->event_type); |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | if (VPPCOM_DEBUG > 2) |
| 2210 | clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid, |
| 2211 | session_index, |
| 2212 | session->is_server ? "server_rx_fifo" : "server_tx_fifo", |
| 2213 | rx_fifo, ready); |
| 2214 | |
| 2215 | return ready; |
| 2216 | } |
| 2217 | |
| 2218 | int |
| 2219 | vppcom_session_write (uint32_t session_index, void *buf, int n) |
| 2220 | { |
| 2221 | vppcom_main_t *vcm = &vppcom_main; |
| 2222 | session_t *session = 0; |
| 2223 | svm_fifo_t *tx_fifo; |
| 2224 | unix_shared_memory_queue_t *q; |
| 2225 | session_fifo_event_t evt; |
| 2226 | int rv; |
| 2227 | char *fifo_str; |
| 2228 | u8 is_nonblocking; |
| 2229 | |
| 2230 | ASSERT (buf); |
| 2231 | |
| 2232 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2233 | rv = vppcom_session_at_index (session_index, &session); |
| 2234 | if (PREDICT_FALSE (rv)) |
| 2235 | { |
| 2236 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2237 | if (VPPCOM_DEBUG > 0) |
| 2238 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 2239 | vcm->my_pid, session_index); |
| 2240 | return rv; |
| 2241 | } |
| 2242 | |
| 2243 | tx_fifo = ((!session->is_cut_thru || session->is_server) ? |
| 2244 | session->server_tx_fifo : session->server_rx_fifo); |
| 2245 | fifo_str = ((!session->is_cut_thru || session->is_server) ? |
| 2246 | "server_tx_fifo" : "server_rx_fifo"); |
| 2247 | is_nonblocking = session->is_nonblocking; |
| 2248 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2249 | |
| 2250 | do |
| 2251 | { |
| 2252 | rv = svm_fifo_enqueue_nowait (tx_fifo, n, buf); |
| 2253 | } |
| 2254 | while (!is_nonblocking && (rv <= 0)); |
| 2255 | |
| 2256 | /* If event wasn't set, add one */ |
| 2257 | if ((rv > 0) && svm_fifo_set_event (tx_fifo)) |
| 2258 | { |
| 2259 | int rval; |
| 2260 | |
| 2261 | /* Fabricate TX event, send to vpp */ |
| 2262 | evt.fifo = tx_fifo; |
| 2263 | evt.event_type = FIFO_EVENT_APP_TX; |
| 2264 | evt.event_id = vcm->tx_event_id++; |
| 2265 | |
| 2266 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2267 | rval = vppcom_session_at_index (session_index, &session); |
| 2268 | if (PREDICT_FALSE (rval)) |
| 2269 | { |
| 2270 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2271 | if (VPPCOM_DEBUG > 1) |
| 2272 | clib_warning ("[%d] invalid session, sid (%d) has been closed!", |
| 2273 | vcm->my_pid, session_index); |
| 2274 | return rval; |
| 2275 | } |
| 2276 | q = session->event_queue; |
| 2277 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2278 | ASSERT (q); |
| 2279 | unix_shared_memory_queue_add (q, (u8 *) & evt, |
| 2280 | 0 /* do wait for mutex */ ); |
| 2281 | } |
| 2282 | |
| 2283 | if (VPPCOM_DEBUG > 2) |
| 2284 | clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid, |
| 2285 | session_index, rv, fifo_str, tx_fifo); |
| 2286 | |
| 2287 | return rv; |
| 2288 | } |
| 2289 | |
| 2290 | static inline int |
| 2291 | vppcom_session_write_ready (session_t * session, u32 session_index) |
| 2292 | { |
| 2293 | vppcom_main_t *vcm = &vppcom_main; |
| 2294 | svm_fifo_t *tx_fifo; |
| 2295 | int rv; |
| 2296 | |
| 2297 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 2298 | tx_fifo = ((!session->is_cut_thru || session->is_server) ? |
| 2299 | session->server_tx_fifo : session->server_rx_fifo); |
| 2300 | |
| 2301 | rv = svm_fifo_max_enqueue (tx_fifo); |
| 2302 | |
| 2303 | if (VPPCOM_DEBUG > 2) |
| 2304 | clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid, |
| 2305 | session_index, |
| 2306 | session->is_server ? "server_tx_fifo" : "server_rx_fifo", |
| 2307 | tx_fifo, rv); |
| 2308 | return rv; |
| 2309 | } |
| 2310 | |
| 2311 | int |
| 2312 | vppcom_select (unsigned long n_bits, unsigned long *read_map, |
| 2313 | unsigned long *write_map, unsigned long *except_map, |
| 2314 | double time_to_wait) |
| 2315 | { |
| 2316 | vppcom_main_t *vcm = &vppcom_main; |
| 2317 | u32 session_index; |
| 2318 | session_t *session = 0; |
| 2319 | int rv, bits_set = 0; |
| 2320 | f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait; |
| 2321 | u32 minbits = clib_max (n_bits, BITS (uword)); |
| 2322 | |
| 2323 | ASSERT (sizeof (clib_bitmap_t) == sizeof (long int)); |
| 2324 | |
| 2325 | if (read_map) |
| 2326 | { |
| 2327 | clib_bitmap_validate (vcm->rd_bitmap, minbits); |
| 2328 | clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap)); |
| 2329 | memset (read_map, 0, vec_len (vcm->rd_bitmap)); |
| 2330 | } |
| 2331 | if (write_map) |
| 2332 | { |
| 2333 | clib_bitmap_validate (vcm->wr_bitmap, minbits); |
| 2334 | clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap)); |
| 2335 | memset (write_map, 0, vec_len (vcm->wr_bitmap)); |
| 2336 | } |
| 2337 | if (except_map) |
| 2338 | { |
| 2339 | clib_bitmap_validate (vcm->ex_bitmap, minbits); |
| 2340 | clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap)); |
| 2341 | memset (except_map, 0, vec_len (vcm->ex_bitmap)); |
| 2342 | } |
| 2343 | |
| 2344 | do |
| 2345 | { |
| 2346 | /* *INDENT-OFF* */ |
| 2347 | clib_bitmap_foreach (session_index, vcm->rd_bitmap, |
| 2348 | ({ |
| 2349 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2350 | rv = vppcom_session_at_index (session_index, &session); |
| 2351 | if (rv < 0) |
| 2352 | { |
| 2353 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2354 | if (VPPCOM_DEBUG > 1) |
| 2355 | clib_warning ("[%d] session %d specified in " |
| 2356 | "read_map is closed.", vcm->my_pid, |
| 2357 | session_index); |
| 2358 | bits_set = VPPCOM_EBADFD; |
| 2359 | goto select_done; |
| 2360 | } |
| 2361 | |
| 2362 | rv = vppcom_session_read_ready (session, session_index); |
| 2363 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2364 | if (vcm->ex_bitmap && |
| 2365 | clib_bitmap_get (vcm->ex_bitmap, session_index) && (rv < 0)) |
| 2366 | { |
| 2367 | // TBD: clib_warning |
| 2368 | clib_bitmap_set_no_check (except_map, session_index, 1); |
| 2369 | bits_set++; |
| 2370 | } |
| 2371 | else if (rv > 0) |
| 2372 | { |
| 2373 | // TBD: clib_warning |
| 2374 | clib_bitmap_set_no_check (read_map, session_index, 1); |
| 2375 | bits_set++; |
| 2376 | } |
| 2377 | })); |
| 2378 | |
| 2379 | clib_bitmap_foreach (session_index, vcm->wr_bitmap, |
| 2380 | ({ |
| 2381 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2382 | rv = vppcom_session_at_index (session_index, &session); |
| 2383 | if (rv < 0) |
| 2384 | { |
| 2385 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2386 | if (VPPCOM_DEBUG > 0) |
| 2387 | clib_warning ("[%d] session %d specified in " |
| 2388 | "write_map is closed.", vcm->my_pid, |
| 2389 | session_index); |
| 2390 | bits_set = VPPCOM_EBADFD; |
| 2391 | goto select_done; |
| 2392 | } |
| 2393 | |
| 2394 | rv = vppcom_session_write_ready (session, session_index); |
| 2395 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2396 | if (rv > 0) |
| 2397 | { |
| 2398 | // TBD: clib_warning |
| 2399 | clib_bitmap_set_no_check (write_map, session_index, 1); |
| 2400 | bits_set++; |
| 2401 | } |
| 2402 | })); |
| 2403 | |
| 2404 | clib_bitmap_foreach (session_index, vcm->ex_bitmap, |
| 2405 | ({ |
| 2406 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2407 | rv = vppcom_session_at_index (session_index, &session); |
| 2408 | if (rv < 0) |
| 2409 | { |
| 2410 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2411 | if (VPPCOM_DEBUG > 1) |
| 2412 | clib_warning ("[%d] session %d specified in " |
| 2413 | "except_map is closed.", vcm->my_pid, |
| 2414 | session_index); |
| 2415 | bits_set = VPPCOM_EBADFD; |
| 2416 | goto select_done; |
| 2417 | } |
| 2418 | |
| 2419 | rv = vppcom_session_read_ready (session, session_index); |
| 2420 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2421 | if (rv < 0) |
| 2422 | { |
| 2423 | // TBD: clib_warning |
| 2424 | clib_bitmap_set_no_check (except_map, session_index, 1); |
| 2425 | bits_set++; |
| 2426 | } |
| 2427 | })); |
| 2428 | /* *INDENT-ON* */ |
| 2429 | } |
| 2430 | while (clib_time_now (&vcm->clib_time) < timeout); |
| 2431 | |
| 2432 | select_done: |
| 2433 | return (bits_set); |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * fd.io coding-style-patch-verification: ON |
| 2438 | * |
| 2439 | * Local Variables: |
| 2440 | * eval: (c-set-style "gnu") |
| 2441 | * End: |
| 2442 | */ |