Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1 | /* |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 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 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 71 | typedef struct epoll_event vppcom_epoll_event_t; |
| 72 | |
| 73 | typedef struct |
| 74 | { |
| 75 | u32 next_sid; |
| 76 | u32 prev_sid; |
| 77 | u32 vep_idx; |
| 78 | vppcom_epoll_event_t ev; |
| 79 | #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT) |
| 80 | u32 et_mask; |
| 81 | } vppcom_epoll_t; |
| 82 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 83 | typedef struct |
| 84 | { |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 85 | u8 is_ip4; |
| 86 | ip46_address_t ip46; |
| 87 | } vppcom_ip46_t; |
| 88 | |
| 89 | typedef struct |
| 90 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 91 | volatile session_state_t state; |
| 92 | |
| 93 | svm_fifo_t *server_rx_fifo; |
| 94 | svm_fifo_t *server_tx_fifo; |
| 95 | u32 sm_seg_index; |
| 96 | u64 vpp_session_handle; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 97 | unix_shared_memory_queue_t *vpp_event_queue; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 98 | |
| 99 | /* Socket configuration state */ |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 100 | /* TBD: covert 'is_*' vars to bit in u8 flags; */ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 101 | u8 is_server; |
| 102 | u8 is_listen; |
| 103 | u8 is_cut_thru; |
| 104 | u8 is_nonblocking; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 105 | u8 is_vep; |
| 106 | u8 is_vep_session; |
| 107 | u32 wait_cont_idx; |
| 108 | vppcom_epoll_t vep; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 109 | u32 vrf; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 110 | vppcom_ip46_t lcl_addr; |
| 111 | vppcom_ip46_t peer_addr; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 112 | u16 port; |
| 113 | u8 proto; |
| 114 | u64 client_queue_address; |
| 115 | u64 options[16]; |
| 116 | } session_t; |
| 117 | |
| 118 | typedef struct vppcom_cfg_t_ |
| 119 | { |
| 120 | u64 heapsize; |
| 121 | u64 segment_baseva; |
| 122 | u32 segment_size; |
| 123 | u32 add_segment_size; |
| 124 | u32 preallocated_fifo_pairs; |
| 125 | u32 rx_fifo_size; |
| 126 | u32 tx_fifo_size; |
| 127 | u32 event_queue_size; |
| 128 | u32 listen_queue_size; |
| 129 | f64 app_timeout; |
| 130 | f64 session_timeout; |
| 131 | f64 accept_timeout; |
| 132 | } vppcom_cfg_t; |
| 133 | |
| 134 | typedef struct vppcom_main_t_ |
| 135 | { |
| 136 | u8 init; |
| 137 | u32 *client_session_index_fifo; |
| 138 | volatile u32 bind_session_index; |
| 139 | u32 tx_event_id; |
| 140 | int main_cpu; |
| 141 | |
| 142 | /* vpe input queue */ |
| 143 | unix_shared_memory_queue_t *vl_input_queue; |
| 144 | |
| 145 | /* API client handle */ |
| 146 | u32 my_client_index; |
| 147 | |
| 148 | /* Session pool */ |
| 149 | clib_spinlock_t sessions_lockp; |
| 150 | session_t *sessions; |
| 151 | |
| 152 | /* Hash table for disconnect processing */ |
| 153 | uword *session_index_by_vpp_handles; |
| 154 | |
| 155 | /* Select bitmaps */ |
| 156 | clib_bitmap_t *rd_bitmap; |
| 157 | clib_bitmap_t *wr_bitmap; |
| 158 | clib_bitmap_t *ex_bitmap; |
| 159 | |
| 160 | /* Our event queue */ |
| 161 | unix_shared_memory_queue_t *app_event_queue; |
| 162 | |
| 163 | /* unique segment name counter */ |
| 164 | u32 unique_segment_index; |
| 165 | |
| 166 | pid_t my_pid; |
| 167 | |
| 168 | /* For deadman timers */ |
| 169 | clib_time_t clib_time; |
| 170 | |
| 171 | /* State of the connection, shared between msg RX thread and main thread */ |
| 172 | volatile app_state_t app_state; |
| 173 | |
| 174 | vppcom_cfg_t cfg; |
| 175 | |
| 176 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
| 177 | uword *error_string_by_error_number; |
| 178 | } vppcom_main_t; |
| 179 | |
| 180 | vppcom_main_t vppcom_main = {.my_client_index = ~0 }; |
| 181 | |
| 182 | static const char * |
| 183 | vppcom_app_state_str (app_state_t state) |
| 184 | { |
| 185 | char *st; |
| 186 | |
| 187 | switch (state) |
| 188 | { |
| 189 | case STATE_APP_START: |
| 190 | st = "STATE_APP_START"; |
| 191 | break; |
| 192 | |
| 193 | case STATE_APP_CONN_VPP: |
| 194 | st = "STATE_APP_CONN_VPP"; |
| 195 | break; |
| 196 | |
| 197 | case STATE_APP_ENABLED: |
| 198 | st = "STATE_APP_ENABLED"; |
| 199 | break; |
| 200 | |
| 201 | case STATE_APP_ATTACHED: |
| 202 | st = "STATE_APP_ATTACHED"; |
| 203 | break; |
| 204 | |
| 205 | default: |
| 206 | st = "UNKNOWN_APP_STATE"; |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | return st; |
| 211 | } |
| 212 | |
| 213 | static const char * |
| 214 | vppcom_session_state_str (session_state_t state) |
| 215 | { |
| 216 | char *st; |
| 217 | |
| 218 | switch (state) |
| 219 | { |
| 220 | case STATE_START: |
| 221 | st = "STATE_START"; |
| 222 | break; |
| 223 | |
| 224 | case STATE_CONNECT: |
| 225 | st = "STATE_CONNECT"; |
| 226 | break; |
| 227 | |
| 228 | case STATE_LISTEN: |
| 229 | st = "STATE_LISTEN"; |
| 230 | break; |
| 231 | |
| 232 | case STATE_ACCEPT: |
| 233 | st = "STATE_ACCEPT"; |
| 234 | break; |
| 235 | |
| 236 | case STATE_DISCONNECT: |
| 237 | st = "STATE_DISCONNECT"; |
| 238 | break; |
| 239 | |
| 240 | case STATE_FAILED: |
| 241 | st = "STATE_FAILED"; |
| 242 | break; |
| 243 | |
| 244 | default: |
| 245 | st = "UNKNOWN_STATE"; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | return st; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * VPPCOM Utility Functions |
| 254 | */ |
| 255 | static inline int |
| 256 | vppcom_session_at_index (u32 session_index, session_t * volatile *sess) |
| 257 | { |
| 258 | vppcom_main_t *vcm = &vppcom_main; |
| 259 | |
| 260 | /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */ |
| 261 | if (PREDICT_FALSE ((session_index == ~0) || |
| 262 | pool_is_free_index (vcm->sessions, session_index))) |
| 263 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 264 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 265 | vcm->my_pid, session_index); |
| 266 | return VPPCOM_EBADFD; |
| 267 | } |
| 268 | *sess = pool_elt_at_index (vcm->sessions, session_index); |
| 269 | return VPPCOM_OK; |
| 270 | } |
| 271 | |
| 272 | static int |
| 273 | vppcom_connect_to_vpp (char *app_name) |
| 274 | { |
| 275 | api_main_t *am = &api_main; |
| 276 | vppcom_main_t *vcm = &vppcom_main; |
| 277 | |
| 278 | if (VPPCOM_DEBUG > 0) |
| 279 | printf ("\nConnecting to VPP api..."); |
| 280 | if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0) |
| 281 | { |
| 282 | clib_warning ("[%d] connect to vpp (%s) failed!", |
| 283 | vcm->my_pid, app_name); |
| 284 | return VPPCOM_ECONNREFUSED; |
| 285 | } |
| 286 | |
| 287 | vcm->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 288 | vcm->my_client_index = am->my_client_index; |
| 289 | if (VPPCOM_DEBUG > 0) |
| 290 | printf (" connected!\n"); |
| 291 | |
| 292 | vcm->app_state = STATE_APP_CONN_VPP; |
| 293 | return VPPCOM_OK; |
| 294 | } |
| 295 | |
| 296 | static u8 * |
| 297 | format_api_error (u8 * s, va_list * args) |
| 298 | { |
| 299 | vppcom_main_t *vcm = &vppcom_main; |
| 300 | i32 error = va_arg (*args, u32); |
| 301 | uword *p; |
| 302 | |
| 303 | p = hash_get (vcm->error_string_by_error_number, -error); |
| 304 | |
| 305 | if (p) |
| 306 | s = format (s, "%s (%d)", p[0], error); |
| 307 | else |
| 308 | s = format (s, "%d", error); |
| 309 | return s; |
| 310 | } |
| 311 | |
| 312 | static void |
| 313 | vppcom_init_error_string_table (void) |
| 314 | { |
| 315 | vppcom_main_t *vcm = &vppcom_main; |
| 316 | |
| 317 | vcm->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 318 | |
| 319 | #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s); |
| 320 | foreach_vnet_api_error; |
| 321 | #undef _ |
| 322 | |
| 323 | hash_set (vcm->error_string_by_error_number, 99, "Misc"); |
| 324 | } |
| 325 | |
| 326 | static inline int |
| 327 | vppcom_wait_for_app_state_change (app_state_t app_state) |
| 328 | { |
| 329 | vppcom_main_t *vcm = &vppcom_main; |
| 330 | f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout; |
| 331 | |
| 332 | while (clib_time_now (&vcm->clib_time) < timeout) |
| 333 | { |
| 334 | if (vcm->app_state == app_state) |
| 335 | return VPPCOM_OK; |
| 336 | } |
| 337 | if (VPPCOM_DEBUG > 0) |
| 338 | clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid, |
| 339 | vppcom_app_state_str (app_state), app_state); |
| 340 | return VPPCOM_ETIMEDOUT; |
| 341 | } |
| 342 | |
| 343 | static inline int |
| 344 | vppcom_wait_for_session_state_change (u32 session_index, |
| 345 | session_state_t state, |
| 346 | f64 wait_for_time) |
| 347 | { |
| 348 | vppcom_main_t *vcm = &vppcom_main; |
| 349 | f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time; |
| 350 | session_t *volatile session; |
| 351 | int rv; |
| 352 | |
| 353 | do |
| 354 | { |
| 355 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 356 | rv = vppcom_session_at_index (session_index, &session); |
| 357 | if (PREDICT_FALSE (rv)) |
| 358 | { |
| 359 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 360 | return rv; |
| 361 | } |
| 362 | if (session->state == state) |
| 363 | { |
| 364 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 365 | return VPPCOM_OK; |
| 366 | } |
| 367 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 368 | } |
| 369 | while (clib_time_now (&vcm->clib_time) < timeout); |
| 370 | |
| 371 | if (VPPCOM_DEBUG > 0) |
| 372 | clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid, |
| 373 | vppcom_session_state_str (state), state); |
| 374 | return VPPCOM_ETIMEDOUT; |
| 375 | } |
| 376 | |
| 377 | static inline int |
| 378 | vppcom_wait_for_client_session_index (f64 wait_for_time) |
| 379 | { |
| 380 | vppcom_main_t *vcm = &vppcom_main; |
| 381 | f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time; |
| 382 | |
| 383 | do |
| 384 | { |
| 385 | if (clib_fifo_elts (vcm->client_session_index_fifo)) |
| 386 | return VPPCOM_OK; |
| 387 | } |
| 388 | while (clib_time_now (&vcm->clib_time) < timeout); |
| 389 | |
| 390 | if (wait_for_time == 0) |
| 391 | return VPPCOM_EAGAIN; |
| 392 | |
| 393 | if (VPPCOM_DEBUG > 0) |
| 394 | clib_warning ("[%d] timeout waiting for client_session_index", |
| 395 | vcm->my_pid); |
| 396 | return VPPCOM_ETIMEDOUT; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * VPP-API message functions |
| 401 | */ |
| 402 | static void |
| 403 | vppcom_send_session_enable_disable (u8 is_enable) |
| 404 | { |
| 405 | vppcom_main_t *vcm = &vppcom_main; |
| 406 | vl_api_session_enable_disable_t *bmp; |
| 407 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 408 | memset (bmp, 0, sizeof (*bmp)); |
| 409 | |
| 410 | bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE); |
| 411 | bmp->client_index = vcm->my_client_index; |
| 412 | bmp->context = htonl (0xfeedface); |
| 413 | bmp->is_enable = is_enable; |
| 414 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 415 | } |
| 416 | |
| 417 | static int |
| 418 | vppcom_app_session_enable (void) |
| 419 | { |
| 420 | vppcom_main_t *vcm = &vppcom_main; |
| 421 | int rv; |
| 422 | |
| 423 | if (vcm->app_state != STATE_APP_ENABLED) |
| 424 | { |
| 425 | vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ ); |
| 426 | rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED); |
| 427 | if (PREDICT_FALSE (rv)) |
| 428 | { |
| 429 | if (VPPCOM_DEBUG > 0) |
| 430 | clib_warning ("[%d] Session enable timed out, rv = %s (%d)", |
| 431 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 432 | return rv; |
| 433 | } |
| 434 | } |
| 435 | return VPPCOM_OK; |
| 436 | } |
| 437 | |
| 438 | static void |
| 439 | vl_api_session_enable_disable_reply_t_handler |
| 440 | (vl_api_session_enable_disable_reply_t * mp) |
| 441 | { |
| 442 | vppcom_main_t *vcm = &vppcom_main; |
| 443 | |
| 444 | if (mp->retval) |
| 445 | { |
| 446 | clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid, |
| 447 | format_api_error, ntohl (mp->retval)); |
| 448 | } |
| 449 | else |
| 450 | vcm->app_state = STATE_APP_ENABLED; |
| 451 | } |
| 452 | |
| 453 | static void |
| 454 | vppcom_app_send_attach (void) |
| 455 | { |
| 456 | vppcom_main_t *vcm = &vppcom_main; |
| 457 | vl_api_application_attach_t *bmp; |
| 458 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 459 | memset (bmp, 0, sizeof (*bmp)); |
| 460 | |
| 461 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
| 462 | bmp->client_index = vcm->my_client_index; |
| 463 | bmp->context = htonl (0xfeedface); |
| 464 | bmp->options[APP_OPTIONS_FLAGS] = |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 465 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT | |
| 466 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE | APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 467 | bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size; |
| 468 | bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size; |
| 469 | bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size; |
| 470 | bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size; |
| 471 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 472 | } |
| 473 | |
| 474 | static int |
| 475 | vppcom_app_attach (void) |
| 476 | { |
| 477 | vppcom_main_t *vcm = &vppcom_main; |
| 478 | int rv; |
| 479 | |
| 480 | vppcom_app_send_attach (); |
| 481 | rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED); |
| 482 | if (PREDICT_FALSE (rv)) |
| 483 | { |
| 484 | if (VPPCOM_DEBUG > 0) |
| 485 | clib_warning ("[%d] application attach timed out, rv = %s (%d)", |
| 486 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 487 | return rv; |
| 488 | } |
| 489 | return VPPCOM_OK; |
| 490 | } |
| 491 | |
| 492 | static void |
| 493 | vppcom_app_detach (void) |
| 494 | { |
| 495 | vppcom_main_t *vcm = &vppcom_main; |
| 496 | vl_api_application_detach_t *bmp; |
| 497 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 498 | memset (bmp, 0, sizeof (*bmp)); |
| 499 | |
| 500 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
| 501 | bmp->client_index = vcm->my_client_index; |
| 502 | bmp->context = htonl (0xfeedface); |
| 503 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 504 | } |
| 505 | |
| 506 | static void |
| 507 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 508 | mp) |
| 509 | { |
| 510 | vppcom_main_t *vcm = &vppcom_main; |
| 511 | static svm_fifo_segment_create_args_t _a; |
| 512 | svm_fifo_segment_create_args_t *a = &_a; |
| 513 | int rv; |
| 514 | |
| 515 | memset (a, 0, sizeof (*a)); |
| 516 | if (mp->retval) |
| 517 | { |
| 518 | clib_warning ("[%d] attach failed: %U", vcm->my_pid, |
| 519 | format_api_error, ntohl (mp->retval)); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | if (mp->segment_name_length == 0) |
| 524 | { |
| 525 | clib_warning ("[%d] segment_name_length zero", vcm->my_pid); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | a->segment_name = (char *) mp->segment_name; |
| 530 | a->segment_size = mp->segment_size; |
| 531 | |
| 532 | ASSERT (mp->app_event_queue_address); |
| 533 | |
| 534 | /* Attach to the segment vpp created */ |
| 535 | rv = svm_fifo_segment_attach (a); |
| 536 | vec_reset_length (a->new_segment_indices); |
| 537 | if (PREDICT_FALSE (rv)) |
| 538 | { |
| 539 | clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid, |
| 540 | mp->segment_name); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | vcm->app_event_queue = |
| 545 | uword_to_pointer (mp->app_event_queue_address, |
| 546 | unix_shared_memory_queue_t *); |
| 547 | |
| 548 | vcm->app_state = STATE_APP_ATTACHED; |
| 549 | } |
| 550 | |
| 551 | static void |
| 552 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 553 | mp) |
| 554 | { |
| 555 | vppcom_main_t *vcm = &vppcom_main; |
| 556 | |
| 557 | if (mp->retval) |
| 558 | clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error, |
| 559 | ntohl (mp->retval)); |
| 560 | |
| 561 | vcm->app_state = STATE_APP_ENABLED; |
| 562 | } |
| 563 | |
| 564 | static void |
| 565 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 566 | mp) |
| 567 | { |
| 568 | vppcom_main_t *vcm = &vppcom_main; |
| 569 | uword *p; |
| 570 | |
| 571 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 572 | if (p) |
| 573 | { |
| 574 | session_t *session = 0; |
| 575 | int rv; |
| 576 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 577 | rv = vppcom_session_at_index (p[0], &session); |
| 578 | if (PREDICT_FALSE (rv)) |
| 579 | { |
| 580 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 581 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 582 | vcm->my_pid, p[0]); |
| 583 | } |
| 584 | hash_unset (vcm->session_index_by_vpp_handles, mp->handle); |
| 585 | session->state = STATE_DISCONNECT; |
| 586 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | if (VPPCOM_DEBUG > 1) |
| 591 | clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid, |
| 592 | mp->handle); |
| 593 | } |
| 594 | |
| 595 | if (mp->retval) |
| 596 | clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid, |
| 597 | format_api_error, ntohl (mp->retval)); |
| 598 | } |
| 599 | |
| 600 | static void |
| 601 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
| 602 | { |
| 603 | vppcom_main_t *vcm = &vppcom_main; |
| 604 | static svm_fifo_segment_create_args_t _a; |
| 605 | svm_fifo_segment_create_args_t *a = &_a; |
| 606 | int rv; |
| 607 | |
| 608 | memset (a, 0, sizeof (*a)); |
| 609 | a->segment_name = (char *) mp->segment_name; |
| 610 | a->segment_size = mp->segment_size; |
| 611 | /* Attach to the segment vpp created */ |
| 612 | rv = svm_fifo_segment_attach (a); |
| 613 | vec_reset_length (a->new_segment_indices); |
| 614 | if (PREDICT_FALSE (rv)) |
| 615 | { |
| 616 | clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", |
| 617 | vcm->my_pid, mp->segment_name); |
| 618 | return; |
| 619 | } |
| 620 | if (VPPCOM_DEBUG > 1) |
| 621 | clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid, |
| 622 | mp->segment_name, mp->segment_size); |
| 623 | } |
| 624 | |
| 625 | static void |
| 626 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 627 | { |
| 628 | vppcom_main_t *vcm = &vppcom_main; |
| 629 | session_t *session = 0; |
| 630 | vl_api_disconnect_session_reply_t *rmp; |
| 631 | uword *p; |
| 632 | int rv = 0; |
| 633 | |
| 634 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 635 | if (p) |
| 636 | { |
| 637 | int rval; |
| 638 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 639 | rval = vppcom_session_at_index (p[0], &session); |
| 640 | if (PREDICT_FALSE (rval)) |
| 641 | { |
| 642 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 643 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 644 | vcm->my_pid, p[0]); |
| 645 | } |
| 646 | else |
| 647 | pool_put (vcm->sessions, session); |
| 648 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 649 | hash_unset (vcm->session_index_by_vpp_handles, mp->handle); |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid, |
| 654 | mp->handle); |
| 655 | rv = -11; |
| 656 | } |
| 657 | |
| 658 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 659 | memset (rmp, 0, sizeof (*rmp)); |
| 660 | |
| 661 | rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY); |
| 662 | rmp->retval = htonl (rv); |
| 663 | rmp->handle = mp->handle; |
| 664 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 665 | } |
| 666 | |
| 667 | static void |
| 668 | vl_api_reset_session_t_handler (vl_api_reset_session_t * mp) |
| 669 | { |
| 670 | vppcom_main_t *vcm = &vppcom_main; |
| 671 | session_t *session = 0; |
| 672 | vl_api_reset_session_reply_t *rmp; |
| 673 | uword *p; |
| 674 | int rv = 0; |
| 675 | |
| 676 | p = hash_get (vcm->session_index_by_vpp_handles, mp->handle); |
| 677 | if (p) |
| 678 | { |
| 679 | int rval; |
| 680 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 681 | rval = vppcom_session_at_index (p[0], &session); |
| 682 | if (PREDICT_FALSE (rval)) |
| 683 | { |
| 684 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 685 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 686 | vcm->my_pid, p[0]); |
| 687 | } |
| 688 | else |
| 689 | pool_put (vcm->sessions, session); |
| 690 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 691 | hash_unset (vcm->session_index_by_vpp_handles, mp->handle); |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid, |
| 696 | mp->handle); |
| 697 | rv = -11; |
| 698 | } |
| 699 | |
| 700 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 701 | memset (rmp, 0, sizeof (*rmp)); |
| 702 | rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY); |
| 703 | rmp->retval = htonl (rv); |
| 704 | rmp->handle = mp->handle; |
| 705 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 706 | } |
| 707 | |
| 708 | static void |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 709 | vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 710 | { |
| 711 | vppcom_main_t *vcm = &vppcom_main; |
| 712 | session_t *session; |
| 713 | u32 session_index; |
| 714 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 715 | u8 is_cut_thru = 0; |
| 716 | int rv; |
| 717 | |
| 718 | if (mp->retval) |
| 719 | { |
| 720 | clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error, |
| 721 | ntohl (mp->retval)); |
| 722 | return; |
| 723 | } |
| 724 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 725 | session_index = mp->context; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 726 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 727 | clib_warning ("[%d] session_index = %d 0x%08x", vcm->my_pid, |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 728 | session_index, session_index); |
| 729 | |
| 730 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 731 | if (pool_is_free_index (vcm->sessions, session_index)) |
| 732 | { |
| 733 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 734 | if (VPPCOM_DEBUG > 1) |
| 735 | clib_warning ("[%d] invalid session, sid %d is closed!", |
| 736 | vcm->my_pid, session_index); |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | /* We've been redirected */ |
| 741 | if (mp->segment_name_length > 0) |
| 742 | { |
| 743 | static svm_fifo_segment_create_args_t _a; |
| 744 | svm_fifo_segment_create_args_t *a = &_a; |
| 745 | |
| 746 | is_cut_thru = 1; |
| 747 | memset (a, 0, sizeof (*a)); |
| 748 | a->segment_name = (char *) mp->segment_name; |
| 749 | if (VPPCOM_DEBUG > 1) |
| 750 | clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid, |
| 751 | a->segment_name); |
| 752 | rv = svm_fifo_segment_attach (a); |
| 753 | vec_reset_length (a->new_segment_indices); |
| 754 | if (PREDICT_FALSE (rv)) |
| 755 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 756 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 757 | clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed", |
| 758 | vcm->my_pid, a->segment_name); |
| 759 | return; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Setup session |
| 765 | */ |
| 766 | if (VPPCOM_DEBUG > 1) |
| 767 | clib_warning ("[%d] client sid %d", vcm->my_pid, session_index); |
| 768 | |
| 769 | session = pool_elt_at_index (vcm->sessions, session_index); |
| 770 | session->is_cut_thru = is_cut_thru; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 771 | session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 772 | unix_shared_memory_queue_t *); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 773 | |
| 774 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 775 | rx_fifo->client_session_index = session_index; |
| 776 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 777 | tx_fifo->client_session_index = session_index; |
| 778 | |
| 779 | session->server_rx_fifo = rx_fifo; |
| 780 | session->server_tx_fifo = tx_fifo; |
| 781 | session->vpp_session_handle = mp->handle; |
| 782 | session->state = STATE_CONNECT; |
| 783 | |
| 784 | /* Add it to lookup table */ |
| 785 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 786 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 787 | } |
| 788 | |
| 789 | static void |
| 790 | vppcom_send_connect_sock (session_t * session, u32 session_index) |
| 791 | { |
| 792 | vppcom_main_t *vcm = &vppcom_main; |
| 793 | vl_api_connect_sock_t *cmp; |
| 794 | |
| 795 | /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */ |
| 796 | session->is_server = 0; |
| 797 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 798 | memset (cmp, 0, sizeof (*cmp)); |
| 799 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK); |
| 800 | cmp->client_index = vcm->my_client_index; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 801 | cmp->context = session_index; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 802 | |
| 803 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 804 | clib_warning ("[%d] session_index = %d 0x%08x", |
| 805 | vcm->my_pid, session_index, session_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 806 | |
| 807 | cmp->vrf = session->vrf; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 808 | cmp->is_ip4 = session->peer_addr.is_ip4; |
| 809 | clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 810 | cmp->port = session->port; |
| 811 | cmp->proto = session->proto; |
| 812 | clib_memcpy (cmp->options, session->options, sizeof (cmp->options)); |
| 813 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp); |
| 814 | } |
| 815 | |
| 816 | static int |
| 817 | vppcom_send_disconnect (u32 session_index) |
| 818 | { |
| 819 | vppcom_main_t *vcm = &vppcom_main; |
| 820 | vl_api_disconnect_session_t *dmp; |
| 821 | session_t *session = 0; |
| 822 | int rv; |
| 823 | |
| 824 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 825 | rv = vppcom_session_at_index (session_index, &session); |
| 826 | if (PREDICT_FALSE (rv)) |
| 827 | { |
| 828 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 829 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 830 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 831 | vcm->my_pid, session_index); |
| 832 | return rv; |
| 833 | } |
| 834 | |
| 835 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 836 | memset (dmp, 0, sizeof (*dmp)); |
| 837 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 838 | dmp->client_index = vcm->my_client_index; |
| 839 | dmp->handle = session->vpp_session_handle; |
| 840 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 841 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp); |
| 842 | return VPPCOM_OK; |
| 843 | } |
| 844 | |
| 845 | static void |
| 846 | vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp) |
| 847 | { |
| 848 | vppcom_main_t *vcm = &vppcom_main; |
| 849 | session_t *session = 0; |
| 850 | int rv; |
| 851 | |
| 852 | if (mp->retval) |
| 853 | clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error, |
| 854 | ntohl (mp->retval)); |
| 855 | |
| 856 | ASSERT (vcm->bind_session_index != ~0); |
| 857 | |
| 858 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 859 | rv = vppcom_session_at_index (vcm->bind_session_index, &session); |
| 860 | if (rv == VPPCOM_OK) |
| 861 | { |
| 862 | session->vpp_session_handle = mp->handle; |
| 863 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, |
| 864 | vcm->bind_session_index); |
| 865 | session->state = mp->retval ? STATE_FAILED : STATE_LISTEN; |
| 866 | vcm->bind_session_index = ~0; |
| 867 | } |
| 868 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 869 | } |
| 870 | |
| 871 | static void |
| 872 | vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp) |
| 873 | { |
| 874 | vppcom_main_t *vcm = &vppcom_main; |
| 875 | session_t *session = 0; |
| 876 | int rv; |
| 877 | |
| 878 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 879 | rv = vppcom_session_at_index (vcm->bind_session_index, &session); |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 880 | if (rv == VPPCOM_OK) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 881 | { |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 882 | if ((VPPCOM_DEBUG > 1) && (mp->retval)) |
| 883 | clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error, |
| 884 | ntohl (mp->retval)); |
| 885 | |
| 886 | vcm->bind_session_index = ~0; |
| 887 | session->state = STATE_START; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 888 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 889 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 890 | } |
| 891 | |
| 892 | u8 * |
| 893 | format_ip4_address (u8 * s, va_list * args) |
| 894 | { |
| 895 | u8 *a = va_arg (*args, u8 *); |
| 896 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 897 | } |
| 898 | |
| 899 | u8 * |
| 900 | format_ip6_address (u8 * s, va_list * args) |
| 901 | { |
| 902 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 903 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 904 | |
| 905 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 906 | max_n_zeros = 0; |
| 907 | i_first_zero = i_max_n_zero; |
| 908 | n_zeros = 0; |
| 909 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 910 | { |
| 911 | u32 is_zero = a->as_u16[i] == 0; |
| 912 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 913 | { |
| 914 | i_first_zero = i; |
| 915 | n_zeros = 0; |
| 916 | } |
| 917 | n_zeros += is_zero; |
| 918 | if ((!is_zero && n_zeros > max_n_zeros) |
| 919 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 920 | { |
| 921 | i_max_n_zero = i_first_zero; |
| 922 | max_n_zeros = n_zeros; |
| 923 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 924 | n_zeros = 0; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | last_double_colon = 0; |
| 929 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 930 | { |
| 931 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 932 | { |
| 933 | s = format (s, "::"); |
| 934 | i += max_n_zeros - 1; |
| 935 | last_double_colon = 1; |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | s = format (s, "%s%x", |
| 940 | (last_double_colon || i == 0) ? "" : ":", |
| 941 | clib_net_to_host_u16 (a->as_u16[i])); |
| 942 | last_double_colon = 0; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | return s; |
| 947 | } |
| 948 | |
| 949 | /* Format an IP46 address. */ |
| 950 | u8 * |
| 951 | format_ip46_address (u8 * s, va_list * args) |
| 952 | { |
| 953 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 954 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 955 | int is_ip4 = 1; |
| 956 | |
| 957 | switch (type) |
| 958 | { |
| 959 | case IP46_TYPE_ANY: |
| 960 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 961 | break; |
| 962 | case IP46_TYPE_IP4: |
| 963 | is_ip4 = 1; |
| 964 | break; |
| 965 | case IP46_TYPE_IP6: |
| 966 | is_ip4 = 0; |
| 967 | break; |
| 968 | } |
| 969 | |
| 970 | return is_ip4 ? |
| 971 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 972 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 973 | } |
| 974 | |
| 975 | static void |
| 976 | vl_api_accept_session_t_handler (vl_api_accept_session_t * mp) |
| 977 | { |
| 978 | vppcom_main_t *vcm = &vppcom_main; |
| 979 | vl_api_accept_session_reply_t *rmp; |
| 980 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 981 | session_t *session; |
| 982 | u32 session_index; |
| 983 | int rv = 0; |
| 984 | |
| 985 | if (!clib_fifo_free_elts (vcm->client_session_index_fifo)) |
| 986 | { |
| 987 | clib_warning ("[%d] client session queue is full!", vcm->my_pid); |
| 988 | rv = VNET_API_ERROR_QUEUE_FULL; |
| 989 | goto send_reply; |
| 990 | } |
| 991 | |
| 992 | if (VPPCOM_DEBUG > 1) |
| 993 | { |
| 994 | u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4); |
| 995 | clib_warning ("[%d] accepted session from: %s:%d", vcm->my_pid, ip_str, |
| 996 | clib_net_to_host_u16 (mp->port)); |
| 997 | vec_free (ip_str); |
| 998 | } |
| 999 | |
| 1000 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1001 | /* Allocate local session and set it up */ |
| 1002 | pool_get (vcm->sessions, session); |
| 1003 | memset (session, 0, sizeof (*session)); |
| 1004 | session_index = session - vcm->sessions; |
| 1005 | |
| 1006 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
| 1007 | rx_fifo->client_session_index = session_index; |
| 1008 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
| 1009 | tx_fifo->client_session_index = session_index; |
| 1010 | |
| 1011 | session->server_rx_fifo = rx_fifo; |
| 1012 | session->server_tx_fifo = tx_fifo; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1013 | session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address, |
| 1014 | unix_shared_memory_queue_t *); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1015 | session->state = STATE_ACCEPT; |
| 1016 | session->is_cut_thru = 0; |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 1017 | session->is_server = 1; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1018 | session->port = ntohs (mp->port); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 1019 | session->peer_addr.is_ip4 = mp->is_ip4; |
| 1020 | clib_memcpy (&session->peer_addr.ip46, mp->ip, |
| 1021 | sizeof (session->peer_addr.ip46)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1022 | |
| 1023 | /* Add it to lookup table */ |
| 1024 | hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index); |
| 1025 | |
| 1026 | clib_fifo_add1 (vcm->client_session_index_fifo, session_index); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1027 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1028 | |
| 1029 | /* |
| 1030 | * Send accept reply to vpp |
| 1031 | */ |
| 1032 | send_reply: |
| 1033 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 1034 | memset (rmp, 0, sizeof (*rmp)); |
| 1035 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
| 1036 | rmp->retval = htonl (rv); |
| 1037 | rmp->handle = mp->handle; |
| 1038 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp); |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * Acting as server for redirected connect requests |
| 1043 | */ |
| 1044 | static void |
| 1045 | vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) |
| 1046 | { |
| 1047 | static svm_fifo_segment_create_args_t _a; |
| 1048 | svm_fifo_segment_create_args_t *a = &_a; |
| 1049 | vppcom_main_t *vcm = &vppcom_main; |
| 1050 | u32 session_index; |
| 1051 | svm_fifo_segment_private_t *seg; |
| 1052 | unix_shared_memory_queue_t *client_q; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1053 | vl_api_connect_session_reply_t *rmp; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1054 | session_t *session = 0; |
| 1055 | int rv = 0; |
| 1056 | svm_fifo_t *rx_fifo; |
| 1057 | svm_fifo_t *tx_fifo; |
| 1058 | unix_shared_memory_queue_t *event_q = 0; |
| 1059 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1060 | clib_spinlock_lock (&vcm->sessions_lockp); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1061 | if (!clib_fifo_free_elts (vcm->client_session_index_fifo)) |
| 1062 | { |
| 1063 | if (VPPCOM_DEBUG > 1) |
| 1064 | clib_warning ("[%d] client session queue is full!", vcm->my_pid); |
| 1065 | rv = VNET_API_ERROR_QUEUE_FULL; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1066 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1067 | goto send_reply; |
| 1068 | } |
| 1069 | |
| 1070 | /* Create the segment */ |
| 1071 | memset (a, 0, sizeof (*a)); |
| 1072 | a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c", |
| 1073 | vcm->my_pid, vcm->unique_segment_index++, |
| 1074 | 0); |
| 1075 | a->segment_size = vcm->cfg.segment_size; |
| 1076 | a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs; |
| 1077 | a->rx_fifo_size = vcm->cfg.rx_fifo_size; |
| 1078 | a->tx_fifo_size = vcm->cfg.tx_fifo_size; |
| 1079 | |
| 1080 | rv = svm_fifo_segment_create (a); |
| 1081 | if (PREDICT_FALSE (rv)) |
| 1082 | { |
| 1083 | if (VPPCOM_DEBUG > 1) |
| 1084 | clib_warning ("[%d] svm_fifo_segment_create ('%s') failed", |
| 1085 | vcm->my_pid, a->segment_name); |
| 1086 | vec_reset_length (a->new_segment_indices); |
| 1087 | rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 1088 | goto send_reply; |
| 1089 | } |
| 1090 | |
| 1091 | if (VPPCOM_DEBUG > 1) |
| 1092 | clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name); |
| 1093 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1094 | pool_get (vcm->sessions, session); |
| 1095 | memset (session, 0, sizeof (*session)); |
| 1096 | session_index = session - vcm->sessions; |
| 1097 | |
| 1098 | session->sm_seg_index = a->new_segment_indices[0]; |
| 1099 | vec_reset_length (a->new_segment_indices); |
| 1100 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 1101 | seg = svm_fifo_segment_get_segment (session->sm_seg_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1102 | rx_fifo = session->server_rx_fifo = |
| 1103 | svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size, |
| 1104 | FIFO_SEGMENT_RX_FREELIST); |
| 1105 | if (PREDICT_FALSE (!session->server_rx_fifo)) |
| 1106 | { |
| 1107 | svm_fifo_segment_delete (seg); |
| 1108 | clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)", |
| 1109 | vcm->my_pid, vcm->cfg.rx_fifo_size, |
| 1110 | vcm->cfg.rx_fifo_size); |
| 1111 | rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 1112 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1113 | goto send_reply; |
| 1114 | } |
| 1115 | |
| 1116 | tx_fifo = session->server_tx_fifo = |
| 1117 | svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size, |
| 1118 | FIFO_SEGMENT_TX_FREELIST); |
| 1119 | if (PREDICT_FALSE (!session->server_tx_fifo)) |
| 1120 | { |
| 1121 | svm_fifo_segment_delete (seg); |
| 1122 | if (VPPCOM_DEBUG > 1) |
| 1123 | clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)", |
| 1124 | vcm->my_pid, vcm->cfg.tx_fifo_size, |
| 1125 | vcm->cfg.tx_fifo_size); |
| 1126 | rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED; |
| 1127 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1128 | goto send_reply; |
| 1129 | } |
| 1130 | |
| 1131 | session->server_rx_fifo->master_session_index = session_index; |
| 1132 | session->server_tx_fifo->master_session_index = session_index; |
| 1133 | session->client_queue_address = mp->client_queue_address; |
| 1134 | session->is_cut_thru = 1; |
| 1135 | session->is_server = 1; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1136 | session->port = mp->port; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 1137 | session->peer_addr.is_ip4 = mp->is_ip4; |
| 1138 | clib_memcpy (&session->peer_addr.ip46, mp->ip, |
| 1139 | sizeof (session->peer_addr.ip46)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1140 | { |
| 1141 | void *oldheap; |
| 1142 | ssvm_shared_header_t *sh = seg->ssvm.sh; |
| 1143 | |
| 1144 | ssvm_lock_non_recursive (sh, 1); |
| 1145 | oldheap = ssvm_push_heap (sh); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1146 | event_q = session->vpp_event_queue = |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1147 | unix_shared_memory_queue_init (vcm->cfg.event_queue_size, |
| 1148 | sizeof (session_fifo_event_t), |
| 1149 | vcm->my_pid, 0 /* signal not sent */ ); |
| 1150 | ssvm_pop_heap (oldheap); |
| 1151 | ssvm_unlock_non_recursive (sh); |
| 1152 | } |
Dave Wallace | 6d5c4cd | 2017-08-15 16:56:29 -0400 | [diff] [blame] | 1153 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1154 | session->state = STATE_ACCEPT; |
| 1155 | if (VPPCOM_DEBUG > 1) |
| 1156 | clib_warning ("[%d] Connected cut-thru to client: sid %d", |
| 1157 | vcm->my_pid, session_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1158 | clib_fifo_add1 (vcm->client_session_index_fifo, session_index); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1159 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1160 | |
| 1161 | send_reply: |
| 1162 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 1163 | memset (rmp, 0, sizeof (*rmp)); |
| 1164 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1165 | rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1166 | rmp->context = mp->context; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1167 | rmp->retval = htonl (rv); |
| 1168 | rmp->segment_name_length = vec_len (a->segment_name); |
| 1169 | clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name)); |
| 1170 | vec_reset_length (a->segment_name); |
| 1171 | |
| 1172 | if (event_q) |
| 1173 | { |
| 1174 | rmp->vpp_event_queue_address = pointer_to_uword (event_q); |
| 1175 | rmp->server_rx_fifo = pointer_to_uword (rx_fifo); |
| 1176 | rmp->server_tx_fifo = pointer_to_uword (tx_fifo); |
| 1177 | } |
| 1178 | client_q = |
| 1179 | uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *); |
| 1180 | |
| 1181 | ASSERT (client_q); |
| 1182 | vl_msg_api_send_shmem (client_q, (u8 *) & rmp); |
| 1183 | } |
| 1184 | |
| 1185 | static void |
| 1186 | vppcom_send_bind_sock (session_t * session) |
| 1187 | { |
| 1188 | vppcom_main_t *vcm = &vppcom_main; |
| 1189 | vl_api_bind_sock_t *bmp; |
| 1190 | |
| 1191 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 1192 | session->is_server = 1; |
| 1193 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 1194 | memset (bmp, 0, sizeof (*bmp)); |
| 1195 | |
| 1196 | bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK); |
| 1197 | bmp->client_index = vcm->my_client_index; |
| 1198 | bmp->context = htonl (0xfeedface); |
| 1199 | bmp->vrf = session->vrf; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 1200 | bmp->is_ip4 = session->lcl_addr.is_ip4; |
| 1201 | clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1202 | bmp->port = session->port; |
| 1203 | bmp->proto = session->proto; |
| 1204 | clib_memcpy (bmp->options, session->options, sizeof (bmp->options)); |
| 1205 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp); |
| 1206 | } |
| 1207 | |
| 1208 | static void |
| 1209 | vppcom_send_unbind_sock (u32 session_index) |
| 1210 | { |
| 1211 | vppcom_main_t *vcm = &vppcom_main; |
| 1212 | vl_api_unbind_sock_t *ump; |
| 1213 | session_t *session = 0; |
| 1214 | int rv; |
| 1215 | |
| 1216 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1217 | rv = vppcom_session_at_index (session_index, &session); |
| 1218 | if (PREDICT_FALSE (rv)) |
| 1219 | { |
| 1220 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1221 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1222 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1223 | vcm->my_pid, session_index); |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 1228 | memset (ump, 0, sizeof (*ump)); |
| 1229 | |
| 1230 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK); |
| 1231 | ump->client_index = vcm->my_client_index; |
| 1232 | ump->handle = session->vpp_session_handle; |
| 1233 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1234 | vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump); |
| 1235 | } |
| 1236 | |
| 1237 | static int |
| 1238 | vppcom_session_unbind_cut_thru (session_t * session) |
| 1239 | { |
| 1240 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 1241 | svm_fifo_segment_private_t *seg; |
| 1242 | int rv = VPPCOM_OK; |
| 1243 | |
| 1244 | seg = vec_elt_at_index (sm->segments, session->sm_seg_index); |
| 1245 | svm_fifo_segment_free_fifo (seg, session->server_rx_fifo, |
| 1246 | FIFO_SEGMENT_RX_FREELIST); |
| 1247 | svm_fifo_segment_free_fifo (seg, session->server_tx_fifo, |
| 1248 | FIFO_SEGMENT_TX_FREELIST); |
| 1249 | svm_fifo_segment_delete (seg); |
| 1250 | |
| 1251 | return rv; |
| 1252 | } |
| 1253 | |
| 1254 | static int |
| 1255 | vppcom_session_unbind (u32 session_index) |
| 1256 | { |
| 1257 | vppcom_main_t *vcm = &vppcom_main; |
| 1258 | int rv; |
| 1259 | |
| 1260 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1261 | if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index))) |
| 1262 | { |
| 1263 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1264 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1265 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1266 | vcm->my_pid, session_index); |
| 1267 | return VPPCOM_EBADFD; |
| 1268 | } |
| 1269 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1270 | |
| 1271 | vcm->bind_session_index = session_index; |
| 1272 | vppcom_send_unbind_sock (session_index); |
| 1273 | rv = vppcom_wait_for_session_state_change (session_index, STATE_START, |
| 1274 | vcm->cfg.session_timeout); |
| 1275 | if (PREDICT_FALSE (rv)) |
| 1276 | { |
| 1277 | vcm->bind_session_index = ~0; |
| 1278 | if (VPPCOM_DEBUG > 0) |
| 1279 | clib_warning ("[%d] server unbind timed out, rv = %s (%d)", |
| 1280 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 1281 | return rv; |
| 1282 | } |
| 1283 | return VPPCOM_OK; |
| 1284 | } |
| 1285 | |
| 1286 | static int |
| 1287 | vppcom_session_disconnect (u32 session_index) |
| 1288 | { |
| 1289 | vppcom_main_t *vcm = &vppcom_main; |
| 1290 | int rv; |
| 1291 | |
| 1292 | rv = vppcom_send_disconnect (session_index); |
| 1293 | if (PREDICT_FALSE (rv)) |
| 1294 | return rv; |
| 1295 | |
| 1296 | rv = vppcom_wait_for_session_state_change (session_index, STATE_DISCONNECT, |
| 1297 | vcm->cfg.session_timeout); |
| 1298 | if (PREDICT_FALSE (rv)) |
| 1299 | { |
| 1300 | if (VPPCOM_DEBUG > 0) |
| 1301 | clib_warning ("[%d] client disconnect timed out, rv = %s (%d)", |
| 1302 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 1303 | return rv; |
| 1304 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1305 | return VPPCOM_OK; |
| 1306 | } |
| 1307 | |
| 1308 | #define foreach_sock_msg \ |
| 1309 | _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \ |
| 1310 | _(BIND_SOCK_REPLY, bind_sock_reply) \ |
| 1311 | _(UNBIND_SOCK_REPLY, unbind_sock_reply) \ |
| 1312 | _(ACCEPT_SESSION, accept_session) \ |
| 1313 | _(CONNECT_SOCK, connect_sock) \ |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1314 | _(CONNECT_SESSION_REPLY, connect_session_reply) \ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1315 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 1316 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 1317 | _(RESET_SESSION, reset_session) \ |
| 1318 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 1319 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 1320 | _(MAP_ANOTHER_SEGMENT, map_another_segment) |
| 1321 | |
| 1322 | static void |
| 1323 | vppcom_api_hookup (void) |
| 1324 | { |
| 1325 | #define _(N,n) \ |
| 1326 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1327 | vl_api_##n##_t_handler, \ |
| 1328 | vl_noop_handler, \ |
| 1329 | vl_api_##n##_t_endian, \ |
| 1330 | vl_api_##n##_t_print, \ |
| 1331 | sizeof(vl_api_##n##_t), 1); |
| 1332 | foreach_sock_msg; |
| 1333 | #undef _ |
| 1334 | } |
| 1335 | |
| 1336 | static void |
| 1337 | vppcom_cfg_init (vppcom_cfg_t * vcl_cfg) |
| 1338 | { |
| 1339 | ASSERT (vcl_cfg); |
| 1340 | |
| 1341 | vcl_cfg->heapsize = (256ULL << 20); |
| 1342 | vcl_cfg->segment_baseva = 0x200000000ULL; |
| 1343 | vcl_cfg->segment_size = (256 << 20); |
| 1344 | vcl_cfg->add_segment_size = (128 << 20); |
| 1345 | vcl_cfg->preallocated_fifo_pairs = 8; |
| 1346 | vcl_cfg->rx_fifo_size = (1 << 20); |
| 1347 | vcl_cfg->tx_fifo_size = (1 << 20); |
| 1348 | vcl_cfg->event_queue_size = 2048; |
| 1349 | vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32); |
| 1350 | vcl_cfg->app_timeout = 10 * 60.0; |
| 1351 | vcl_cfg->session_timeout = 10 * 60.0; |
| 1352 | vcl_cfg->accept_timeout = 60.0; |
| 1353 | } |
| 1354 | |
| 1355 | static void |
| 1356 | vppcom_cfg_heapsize (char *conf_fname) |
| 1357 | { |
| 1358 | vppcom_main_t *vcm = &vppcom_main; |
| 1359 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 1360 | FILE *fp; |
| 1361 | char inbuf[4096]; |
| 1362 | int argc = 1; |
| 1363 | char **argv = NULL; |
| 1364 | char *arg = NULL; |
| 1365 | char *p; |
| 1366 | int i; |
| 1367 | u8 *sizep; |
| 1368 | u32 size; |
| 1369 | |
| 1370 | fp = fopen (conf_fname, "r"); |
| 1371 | if (fp == NULL) |
| 1372 | { |
| 1373 | if (VPPCOM_DEBUG > 0) |
| 1374 | fprintf (stderr, "open configuration file '%s' failed\n", conf_fname); |
| 1375 | goto defaulted; |
| 1376 | } |
| 1377 | argv = calloc (1, sizeof (char *)); |
| 1378 | if (argv == NULL) |
| 1379 | goto defaulted; |
| 1380 | |
| 1381 | while (1) |
| 1382 | { |
| 1383 | if (fgets (inbuf, 4096, fp) == 0) |
| 1384 | break; |
| 1385 | p = strtok (inbuf, " \t\n"); |
| 1386 | while (p != NULL) |
| 1387 | { |
| 1388 | if (*p == '#') |
| 1389 | break; |
| 1390 | argc++; |
| 1391 | char **tmp = realloc (argv, argc * sizeof (char *)); |
| 1392 | if (tmp == NULL) |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 1393 | goto defaulted; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1394 | argv = tmp; |
| 1395 | arg = strndup (p, 1024); |
| 1396 | if (arg == NULL) |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 1397 | goto defaulted; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1398 | argv[argc - 1] = arg; |
| 1399 | p = strtok (NULL, " \t\n"); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | fclose (fp); |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 1404 | fp = NULL; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1405 | |
| 1406 | char **tmp = realloc (argv, (argc + 1) * sizeof (char *)); |
| 1407 | if (tmp == NULL) |
| 1408 | goto defaulted; |
| 1409 | argv = tmp; |
| 1410 | argv[argc] = NULL; |
| 1411 | |
| 1412 | /* |
| 1413 | * Look for and parse the "heapsize" config parameter. |
| 1414 | * Manual since none of the clib infra has been bootstrapped yet. |
| 1415 | * |
| 1416 | * Format: heapsize <nn>[mM][gG] |
| 1417 | */ |
| 1418 | |
| 1419 | for (i = 1; i < (argc - 1); i++) |
| 1420 | { |
| 1421 | if (!strncmp (argv[i], "heapsize", 8)) |
| 1422 | { |
| 1423 | sizep = (u8 *) argv[i + 1]; |
| 1424 | size = 0; |
| 1425 | while (*sizep >= '0' && *sizep <= '9') |
| 1426 | { |
| 1427 | size *= 10; |
| 1428 | size += *sizep++ - '0'; |
| 1429 | } |
| 1430 | if (size == 0) |
| 1431 | { |
| 1432 | if (VPPCOM_DEBUG > 0) |
| 1433 | clib_warning ("[%d] parse error '%s %s', " |
| 1434 | "using default heapsize %lld (0x%llx)", |
| 1435 | vcm->my_pid, argv[i], argv[i + 1], |
| 1436 | vcl_cfg->heapsize, vcl_cfg->heapsize); |
| 1437 | goto defaulted; |
| 1438 | } |
| 1439 | |
| 1440 | if (*sizep == 'g' || *sizep == 'G') |
| 1441 | vcl_cfg->heapsize = size << 30; |
| 1442 | else if (*sizep == 'm' || *sizep == 'M') |
| 1443 | vcl_cfg->heapsize = size << 20; |
| 1444 | else |
| 1445 | { |
| 1446 | if (VPPCOM_DEBUG > 0) |
| 1447 | clib_warning ("[%d] parse error '%s %s', " |
| 1448 | "using default heapsize %lld (0x%llx)", |
| 1449 | vcm->my_pid, argv[i], argv[i + 1], |
| 1450 | vcl_cfg->heapsize, vcl_cfg->heapsize); |
| 1451 | goto defaulted; |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | defaulted: |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 1457 | if (fp != NULL) |
| 1458 | fclose (fp); |
| 1459 | if (argv != NULL) |
| 1460 | free (argv); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1461 | if (!clib_mem_init (0, vcl_cfg->heapsize)) |
| 1462 | clib_warning ("[%d] vppcom heap allocation failure!", vcm->my_pid); |
| 1463 | else if (VPPCOM_DEBUG > 0) |
| 1464 | clib_warning ("[%d] allocated vppcom heapsize %lld (0x%llx)", |
| 1465 | vcm->my_pid, vcl_cfg->heapsize, vcl_cfg->heapsize); |
| 1466 | } |
| 1467 | |
| 1468 | static void |
| 1469 | vppcom_cfg_read (char *conf_fname) |
| 1470 | { |
| 1471 | vppcom_main_t *vcm = &vppcom_main; |
| 1472 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 1473 | int fd; |
| 1474 | unformat_input_t _input, *input = &_input; |
| 1475 | unformat_input_t _line_input, *line_input = &_line_input; |
| 1476 | u8 vc_cfg_input = 0; |
| 1477 | u8 *chroot_path; |
| 1478 | struct stat s; |
| 1479 | u32 uid, gid; |
| 1480 | |
| 1481 | fd = open (conf_fname, O_RDONLY); |
| 1482 | if (fd < 0) |
| 1483 | { |
| 1484 | if (VPPCOM_DEBUG > 0) |
| 1485 | clib_warning ("[%d] open configuration file '%s' failed!", |
| 1486 | vcm->my_pid, conf_fname); |
| 1487 | goto file_done; |
| 1488 | } |
| 1489 | |
| 1490 | if (fstat (fd, &s) < 0) |
| 1491 | { |
| 1492 | if (VPPCOM_DEBUG > 0) |
| 1493 | clib_warning ("[%d] failed to stat `%s'", vcm->my_pid, conf_fname); |
| 1494 | goto file_done; |
| 1495 | } |
| 1496 | |
| 1497 | if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) |
| 1498 | { |
| 1499 | if (VPPCOM_DEBUG > 0) |
| 1500 | clib_warning ("[%d] not a regular file `%s'", vcm->my_pid, |
| 1501 | conf_fname); |
| 1502 | goto file_done; |
| 1503 | } |
| 1504 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1505 | unformat_init_clib_file (input, fd); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1506 | |
| 1507 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 1508 | { |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 1509 | (void) unformat_user (input, unformat_line_input, line_input); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1510 | unformat_skip_white_space (line_input); |
| 1511 | |
| 1512 | if (unformat (line_input, "vppcom {")) |
| 1513 | { |
| 1514 | vc_cfg_input = 1; |
| 1515 | continue; |
| 1516 | } |
| 1517 | |
| 1518 | if (vc_cfg_input) |
| 1519 | { |
| 1520 | if (unformat (line_input, "heapsize %s", &chroot_path)) |
| 1521 | { |
| 1522 | vec_terminate_c_string (chroot_path); |
| 1523 | if (VPPCOM_DEBUG > 0) |
| 1524 | clib_warning ("[%d] configured heapsize %s, " |
| 1525 | "actual heapsize %lld (0x%llx)", |
| 1526 | vcm->my_pid, chroot_path, vcl_cfg->heapsize, |
| 1527 | vcl_cfg->heapsize); |
| 1528 | vec_free (chroot_path); |
| 1529 | } |
| 1530 | else if (unformat (line_input, "api-prefix %s", &chroot_path)) |
| 1531 | { |
| 1532 | vec_terminate_c_string (chroot_path); |
| 1533 | vl_set_memory_root_path ((char *) chroot_path); |
| 1534 | if (VPPCOM_DEBUG > 0) |
| 1535 | clib_warning ("[%d] configured api-prefix %s", |
| 1536 | vcm->my_pid, chroot_path); |
| 1537 | chroot_path = 0; /* Don't vec_free() it! */ |
| 1538 | } |
| 1539 | else if (unformat (line_input, "uid %d", &uid)) |
| 1540 | { |
| 1541 | vl_set_memory_uid (uid); |
| 1542 | if (VPPCOM_DEBUG > 0) |
| 1543 | clib_warning ("[%d] configured uid %d", vcm->my_pid, uid); |
| 1544 | } |
| 1545 | else if (unformat (line_input, "gid %d", &gid)) |
| 1546 | { |
| 1547 | vl_set_memory_gid (gid); |
| 1548 | if (VPPCOM_DEBUG > 0) |
| 1549 | clib_warning ("[%d] configured gid %d", vcm->my_pid, gid); |
| 1550 | } |
| 1551 | else if (unformat (line_input, "segment-baseva 0x%llx", |
| 1552 | &vcl_cfg->segment_baseva)) |
| 1553 | { |
| 1554 | if (VPPCOM_DEBUG > 0) |
| 1555 | clib_warning ("[%d] configured segment_baseva 0x%llx", |
| 1556 | vcm->my_pid, vcl_cfg->segment_baseva); |
| 1557 | } |
| 1558 | else if (unformat (line_input, "segment-size 0x%lx", |
| 1559 | &vcl_cfg->segment_size)) |
| 1560 | { |
| 1561 | if (VPPCOM_DEBUG > 0) |
| 1562 | clib_warning ("[%d] configured segment_size 0x%lx (%ld)", |
| 1563 | vcm->my_pid, vcl_cfg->segment_size, |
| 1564 | vcl_cfg->segment_size); |
| 1565 | } |
| 1566 | else if (unformat (line_input, "segment-size %ld", |
| 1567 | &vcl_cfg->segment_size)) |
| 1568 | { |
| 1569 | if (VPPCOM_DEBUG > 0) |
| 1570 | clib_warning ("[%d] configured segment_size %ld (0x%lx)", |
| 1571 | vcm->my_pid, vcl_cfg->segment_size, |
| 1572 | vcl_cfg->segment_size); |
| 1573 | } |
| 1574 | else if (unformat (line_input, "add-segment-size 0x%lx", |
| 1575 | &vcl_cfg->add_segment_size)) |
| 1576 | { |
| 1577 | if (VPPCOM_DEBUG > 0) |
| 1578 | clib_warning |
| 1579 | ("[%d] configured add_segment_size 0x%lx (%ld)", |
| 1580 | vcm->my_pid, vcl_cfg->add_segment_size, |
| 1581 | vcl_cfg->add_segment_size); |
| 1582 | } |
| 1583 | else if (unformat (line_input, "add-segment-size %ld", |
| 1584 | &vcl_cfg->add_segment_size)) |
| 1585 | { |
| 1586 | if (VPPCOM_DEBUG > 0) |
| 1587 | clib_warning |
| 1588 | ("[%d] configured add_segment_size %ld (0x%lx)", |
| 1589 | vcm->my_pid, vcl_cfg->add_segment_size, |
| 1590 | vcl_cfg->add_segment_size); |
| 1591 | } |
| 1592 | else if (unformat (line_input, "preallocated-fifo-pairs %d", |
| 1593 | &vcl_cfg->preallocated_fifo_pairs)) |
| 1594 | { |
| 1595 | if (VPPCOM_DEBUG > 0) |
| 1596 | clib_warning ("[%d] configured preallocated_fifo_pairs " |
| 1597 | "%d (0x%x)", vcm->my_pid, |
| 1598 | vcl_cfg->preallocated_fifo_pairs, |
| 1599 | vcl_cfg->preallocated_fifo_pairs); |
| 1600 | } |
| 1601 | else if (unformat (line_input, "rx-fifo-size 0x%lx", |
| 1602 | &vcl_cfg->rx_fifo_size)) |
| 1603 | { |
| 1604 | if (VPPCOM_DEBUG > 0) |
| 1605 | clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)", |
| 1606 | vcm->my_pid, vcl_cfg->rx_fifo_size, |
| 1607 | vcl_cfg->rx_fifo_size); |
| 1608 | } |
| 1609 | else if (unformat (line_input, "rx-fifo-size %ld", |
| 1610 | &vcl_cfg->rx_fifo_size)) |
| 1611 | { |
| 1612 | if (VPPCOM_DEBUG > 0) |
| 1613 | clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)", |
| 1614 | vcm->my_pid, vcl_cfg->rx_fifo_size, |
| 1615 | vcl_cfg->rx_fifo_size); |
| 1616 | } |
| 1617 | else if (unformat (line_input, "tx-fifo-size 0x%lx", |
| 1618 | &vcl_cfg->tx_fifo_size)) |
| 1619 | { |
| 1620 | if (VPPCOM_DEBUG > 0) |
| 1621 | clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)", |
| 1622 | vcm->my_pid, vcl_cfg->tx_fifo_size, |
| 1623 | vcl_cfg->tx_fifo_size); |
| 1624 | } |
| 1625 | else if (unformat (line_input, "tx-fifo-size %ld", |
| 1626 | &vcl_cfg->tx_fifo_size)) |
| 1627 | { |
| 1628 | if (VPPCOM_DEBUG > 0) |
| 1629 | clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)", |
| 1630 | vcm->my_pid, vcl_cfg->tx_fifo_size, |
| 1631 | vcl_cfg->tx_fifo_size); |
| 1632 | } |
| 1633 | else if (unformat (line_input, "event-queue-size 0x%lx", |
| 1634 | &vcl_cfg->event_queue_size)) |
| 1635 | { |
| 1636 | if (VPPCOM_DEBUG > 0) |
| 1637 | clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)", |
| 1638 | vcm->my_pid, vcl_cfg->event_queue_size, |
| 1639 | vcl_cfg->event_queue_size); |
| 1640 | } |
| 1641 | else if (unformat (line_input, "event-queue-size %ld", |
| 1642 | &vcl_cfg->event_queue_size)) |
| 1643 | { |
| 1644 | if (VPPCOM_DEBUG > 0) |
| 1645 | clib_warning ("[%d] configured event_queue_size %ld (0x%lx)", |
| 1646 | vcm->my_pid, vcl_cfg->event_queue_size, |
| 1647 | vcl_cfg->event_queue_size); |
| 1648 | } |
| 1649 | else if (unformat (line_input, "listen-queue-size 0x%lx", |
| 1650 | &vcl_cfg->listen_queue_size)) |
| 1651 | { |
| 1652 | if (VPPCOM_DEBUG > 0) |
| 1653 | clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)", |
| 1654 | vcm->my_pid, vcl_cfg->listen_queue_size, |
| 1655 | vcl_cfg->listen_queue_size); |
| 1656 | } |
| 1657 | else if (unformat (line_input, "listen-queue-size %ld", |
| 1658 | &vcl_cfg->listen_queue_size)) |
| 1659 | { |
| 1660 | if (VPPCOM_DEBUG > 0) |
| 1661 | clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)", |
| 1662 | vcm->my_pid, vcl_cfg->listen_queue_size, |
| 1663 | vcl_cfg->listen_queue_size); |
| 1664 | } |
| 1665 | else if (unformat (line_input, "app-timeout %f", |
| 1666 | &vcl_cfg->app_timeout)) |
| 1667 | { |
| 1668 | if (VPPCOM_DEBUG > 0) |
| 1669 | clib_warning ("[%d] configured app_timeout %f", |
| 1670 | vcm->my_pid, vcl_cfg->app_timeout); |
| 1671 | } |
| 1672 | else if (unformat (line_input, "session-timeout %f", |
| 1673 | &vcl_cfg->session_timeout)) |
| 1674 | { |
| 1675 | if (VPPCOM_DEBUG > 0) |
| 1676 | clib_warning ("[%d] configured session_timeout %f", |
| 1677 | vcm->my_pid, vcl_cfg->session_timeout); |
| 1678 | } |
| 1679 | else if (unformat (line_input, "accept-timeout %f", |
| 1680 | &vcl_cfg->accept_timeout)) |
| 1681 | { |
| 1682 | if (VPPCOM_DEBUG > 0) |
| 1683 | clib_warning ("[%d] configured accept_timeout %f", |
| 1684 | vcm->my_pid, vcl_cfg->accept_timeout); |
| 1685 | } |
| 1686 | else if (unformat (line_input, "}")) |
| 1687 | { |
| 1688 | vc_cfg_input = 0; |
| 1689 | if (VPPCOM_DEBUG > 0) |
| 1690 | clib_warning ("[%d] completed parsing vppcom config!", |
| 1691 | vcm->my_pid); |
| 1692 | goto input_done; |
| 1693 | } |
| 1694 | else |
| 1695 | { |
| 1696 | if (line_input->buffer[line_input->index] != '#') |
| 1697 | { |
| 1698 | clib_warning ("[%d] Unknown vppcom config option: '%s'", |
| 1699 | vcm->my_pid, (char *) |
| 1700 | &line_input->buffer[line_input->index]); |
| 1701 | } |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | input_done: |
| 1707 | unformat_free (input); |
| 1708 | |
| 1709 | file_done: |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 1710 | if (fd >= 0) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1711 | close (fd); |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | * VPPCOM Public API functions |
| 1716 | */ |
| 1717 | int |
| 1718 | vppcom_app_create (char *app_name) |
| 1719 | { |
| 1720 | vppcom_main_t *vcm = &vppcom_main; |
| 1721 | vppcom_cfg_t *vcl_cfg = &vcm->cfg; |
| 1722 | u8 *heap; |
| 1723 | mheap_t *h; |
| 1724 | int rv; |
| 1725 | |
| 1726 | if (!vcm->init) |
| 1727 | { |
| 1728 | char *conf_fname; |
| 1729 | |
| 1730 | vcm->init = 1; |
| 1731 | vcm->my_pid = getpid (); |
| 1732 | clib_fifo_validate (vcm->client_session_index_fifo, |
| 1733 | vcm->cfg.listen_queue_size); |
| 1734 | vppcom_cfg_init (vcl_cfg); |
| 1735 | conf_fname = getenv (VPPCOM_CONF_ENV); |
| 1736 | if (!conf_fname) |
| 1737 | { |
| 1738 | conf_fname = VPPCOM_CONF_DEFAULT; |
| 1739 | if (VPPCOM_DEBUG > 0) |
| 1740 | clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid, |
| 1741 | VPPCOM_CONF_ENV); |
| 1742 | } |
| 1743 | vppcom_cfg_heapsize (conf_fname); |
| 1744 | vppcom_cfg_read (conf_fname); |
| 1745 | vcm->bind_session_index = ~0; |
| 1746 | vcm->main_cpu = os_get_thread_index (); |
| 1747 | heap = clib_mem_get_per_cpu_heap (); |
| 1748 | h = mheap_header (heap); |
| 1749 | |
| 1750 | /* make the main heap thread-safe */ |
| 1751 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 1752 | |
| 1753 | vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
| 1754 | |
| 1755 | clib_time_init (&vcm->clib_time); |
| 1756 | vppcom_init_error_string_table (); |
| 1757 | svm_fifo_segment_init (vcl_cfg->segment_baseva, |
| 1758 | 20 /* timeout in secs */ ); |
| 1759 | clib_spinlock_init (&vcm->sessions_lockp); |
| 1760 | vppcom_api_hookup (); |
| 1761 | } |
| 1762 | |
| 1763 | if (vcm->my_client_index == ~0) |
| 1764 | { |
| 1765 | vcm->app_state = STATE_APP_START; |
| 1766 | rv = vppcom_connect_to_vpp (app_name); |
| 1767 | if (rv) |
| 1768 | { |
| 1769 | clib_warning ("[%s] couldn't connect to VPP.", vcm->my_pid); |
| 1770 | return rv; |
| 1771 | } |
| 1772 | |
| 1773 | if (VPPCOM_DEBUG > 0) |
| 1774 | clib_warning ("[%d] sending session enable", vcm->my_pid); |
| 1775 | |
| 1776 | rv = vppcom_app_session_enable (); |
| 1777 | if (rv) |
| 1778 | { |
| 1779 | clib_warning ("[%d] vppcom_app_session_enable() failed!", |
| 1780 | vcm->my_pid); |
| 1781 | return rv; |
| 1782 | } |
| 1783 | |
| 1784 | if (VPPCOM_DEBUG > 0) |
| 1785 | clib_warning ("[%d] sending app attach", vcm->my_pid); |
| 1786 | |
| 1787 | rv = vppcom_app_attach (); |
| 1788 | if (rv) |
| 1789 | { |
| 1790 | clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid); |
| 1791 | return rv; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | if (VPPCOM_DEBUG > 0) |
| 1796 | clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)", |
| 1797 | vcm->my_pid, app_name, vcm->my_client_index, |
| 1798 | vcm->my_client_index); |
| 1799 | |
| 1800 | return VPPCOM_OK; |
| 1801 | } |
| 1802 | |
| 1803 | void |
| 1804 | vppcom_app_destroy (void) |
| 1805 | { |
| 1806 | vppcom_main_t *vcm = &vppcom_main; |
| 1807 | int rv; |
| 1808 | |
| 1809 | if (vcm->my_client_index == ~0) |
| 1810 | return; |
| 1811 | |
| 1812 | if (VPPCOM_DEBUG > 0) |
| 1813 | clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)", |
| 1814 | vcm->my_pid, vcm->my_client_index, vcm->my_client_index); |
| 1815 | |
| 1816 | vppcom_app_detach (); |
| 1817 | rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED); |
| 1818 | if (PREDICT_FALSE (rv)) |
| 1819 | { |
| 1820 | if (VPPCOM_DEBUG > 0) |
| 1821 | clib_warning ("[%d] application detach timed out, rv = %s (%d)", |
| 1822 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 1823 | } |
| 1824 | vl_client_disconnect_from_vlib (); |
| 1825 | vcm->my_client_index = ~0; |
| 1826 | vcm->app_state = STATE_APP_START; |
| 1827 | } |
| 1828 | |
| 1829 | int |
| 1830 | vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking) |
| 1831 | { |
| 1832 | vppcom_main_t *vcm = &vppcom_main; |
| 1833 | session_t *session; |
| 1834 | u32 session_index; |
| 1835 | |
| 1836 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1837 | pool_get (vcm->sessions, session); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1838 | memset (session, 0, sizeof (*session)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1839 | session_index = session - vcm->sessions; |
| 1840 | |
| 1841 | session->vrf = vrf; |
| 1842 | session->proto = proto; |
| 1843 | session->state = STATE_START; |
| 1844 | session->is_nonblocking = is_nonblocking; |
| 1845 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1846 | |
| 1847 | if (VPPCOM_DEBUG > 0) |
| 1848 | clib_warning ("[%d] sid %d", vcm->my_pid, session_index); |
| 1849 | |
| 1850 | return (int) session_index; |
| 1851 | } |
| 1852 | |
| 1853 | int |
| 1854 | vppcom_session_close (uint32_t session_index) |
| 1855 | { |
| 1856 | vppcom_main_t *vcm = &vppcom_main; |
| 1857 | session_t *session = 0; |
| 1858 | int rv; |
| 1859 | |
| 1860 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1861 | rv = vppcom_session_at_index (session_index, &session); |
| 1862 | if (PREDICT_FALSE (rv)) |
| 1863 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1864 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1865 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1866 | vcm->my_pid, session_index); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1867 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1868 | goto done; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1869 | } |
| 1870 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1871 | |
| 1872 | if (VPPCOM_DEBUG > 0) |
| 1873 | clib_warning ("[%d] sid %d", vcm->my_pid, session_index); |
| 1874 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1875 | if (session->is_vep) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1876 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1877 | u32 next_sid; |
| 1878 | for (next_sid = session->vep.next_sid; next_sid != ~0; |
| 1879 | next_sid = session->vep.next_sid) |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 1880 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1881 | rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0); |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 1882 | if ((VPPCOM_DEBUG > 0) && (rv < 0)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1883 | clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, " |
| 1884 | "rv = %s (%d)", session_index, next_sid, |
| 1885 | vcm->my_pid, session_index, |
| 1886 | vppcom_retval_str (rv), rv); |
| 1887 | |
| 1888 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1889 | rv = vppcom_session_at_index (session_index, &session); |
| 1890 | if (PREDICT_FALSE (rv)) |
| 1891 | { |
| 1892 | if (VPPCOM_DEBUG > 0) |
| 1893 | clib_warning |
| 1894 | ("[%d] invalid session, sid (%u) has been closed!", |
| 1895 | vcm->my_pid, session_index); |
| 1896 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1897 | goto done; |
| 1898 | } |
| 1899 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1900 | } |
| 1901 | } |
| 1902 | else |
| 1903 | { |
| 1904 | if (session->is_vep_session) |
| 1905 | { |
| 1906 | u32 vep_idx = session->vep.vep_idx; |
| 1907 | rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0); |
| 1908 | if ((VPPCOM_DEBUG > 0) && (rv < 0)) |
| 1909 | clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, " |
| 1910 | "rv = %s (%d)", vep_idx, session_index, |
| 1911 | vcm->my_pid, session_index, |
| 1912 | vppcom_retval_str (rv), rv); |
| 1913 | |
| 1914 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1915 | rv = vppcom_session_at_index (session_index, &session); |
| 1916 | if (PREDICT_FALSE (rv)) |
| 1917 | { |
| 1918 | if (VPPCOM_DEBUG > 0) |
| 1919 | clib_warning |
| 1920 | ("[%d] invalid session, sid (%u) has been closed!", |
| 1921 | vcm->my_pid, session_index); |
| 1922 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1923 | goto done; |
| 1924 | } |
| 1925 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1926 | } |
| 1927 | |
| 1928 | if (session->is_cut_thru) |
| 1929 | { |
| 1930 | if (session->is_server) |
| 1931 | { |
| 1932 | rv = vppcom_session_unbind_cut_thru (session); |
| 1933 | if ((VPPCOM_DEBUG > 0) && (rv < 0)) |
| 1934 | clib_warning ("[%d] unbind cut-thru (session %d) failed, " |
| 1935 | "rv = %s (%d)", |
| 1936 | vcm->my_pid, session_index, |
| 1937 | vppcom_retval_str (rv), rv); |
| 1938 | } |
| 1939 | } |
| 1940 | else if (session->is_server) |
| 1941 | { |
| 1942 | rv = vppcom_session_unbind (session_index); |
| 1943 | if ((VPPCOM_DEBUG > 0) && (rv < 0)) |
| 1944 | clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)", |
| 1945 | vcm->my_pid, session_index, |
| 1946 | vppcom_retval_str (rv), rv); |
| 1947 | } |
| 1948 | else |
| 1949 | { |
| 1950 | rv = vppcom_session_disconnect (session_index); |
| 1951 | if ((VPPCOM_DEBUG > 0) && (rv < 0)) |
| 1952 | clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)", |
Dave Wallace | 1948161 | 2017-09-15 18:47:44 -0400 | [diff] [blame] | 1953 | vcm->my_pid, session_index, |
| 1954 | vppcom_retval_str (rv), rv); |
| 1955 | } |
| 1956 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1957 | pool_put_index (vcm->sessions, session_index); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1958 | done: |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1959 | return rv; |
| 1960 | } |
| 1961 | |
| 1962 | int |
| 1963 | vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep) |
| 1964 | { |
| 1965 | vppcom_main_t *vcm = &vppcom_main; |
| 1966 | session_t *session = 0; |
| 1967 | int rv; |
| 1968 | |
| 1969 | if (!ep || !ep->ip) |
| 1970 | return VPPCOM_EINVAL; |
| 1971 | |
| 1972 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 1973 | rv = vppcom_session_at_index (session_index, &session); |
| 1974 | if (PREDICT_FALSE (rv)) |
| 1975 | { |
| 1976 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1977 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 1978 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1979 | vcm->my_pid, session_index); |
| 1980 | return rv; |
| 1981 | } |
| 1982 | |
| 1983 | if (VPPCOM_DEBUG > 0) |
| 1984 | clib_warning ("[%d] sid %d", vcm->my_pid, session_index); |
| 1985 | |
| 1986 | session->vrf = ep->vrf; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 1987 | session->lcl_addr.is_ip4 = ep->is_ip4; |
| 1988 | session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1989 | session->port = ep->port; |
| 1990 | |
| 1991 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 1992 | return VPPCOM_OK; |
| 1993 | } |
| 1994 | |
| 1995 | int |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1996 | vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1997 | { |
| 1998 | vppcom_main_t *vcm = &vppcom_main; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 1999 | session_t *listen_session = 0; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2000 | int rv; |
| 2001 | |
| 2002 | clib_spinlock_lock (&vcm->sessions_lockp); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2003 | rv = vppcom_session_at_index (listen_session_index, &listen_session); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2004 | if (PREDICT_FALSE (rv)) |
| 2005 | { |
| 2006 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2007 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2008 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2009 | vcm->my_pid, listen_session_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2010 | return rv; |
| 2011 | } |
| 2012 | |
| 2013 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2014 | clib_warning ("[%d] sid %d", vcm->my_pid, listen_session_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2015 | |
| 2016 | ASSERT (vcm->bind_session_index == ~0); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2017 | vcm->bind_session_index = listen_session_index; |
| 2018 | vppcom_send_bind_sock (listen_session); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2019 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2020 | rv = |
| 2021 | vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN, |
| 2022 | vcm->cfg.session_timeout); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2023 | if (PREDICT_FALSE (rv)) |
| 2024 | { |
| 2025 | vcm->bind_session_index = ~0; |
| 2026 | if (VPPCOM_DEBUG > 0) |
| 2027 | clib_warning ("[%d] server listen timed out, rv = %d (%d)", |
| 2028 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 2029 | return rv; |
| 2030 | } |
| 2031 | |
| 2032 | clib_spinlock_lock (&vcm->sessions_lockp); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2033 | rv = vppcom_session_at_index (listen_session_index, &listen_session); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2034 | if (PREDICT_FALSE (rv)) |
| 2035 | { |
| 2036 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2037 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2038 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2039 | vcm->my_pid, listen_session_index); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2040 | return rv; |
| 2041 | } |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2042 | listen_session->is_listen = 1; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2043 | clib_fifo_validate (vcm->client_session_index_fifo, q_len); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2044 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2045 | |
| 2046 | return VPPCOM_OK; |
| 2047 | } |
| 2048 | |
| 2049 | int |
| 2050 | vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep, |
| 2051 | double wait_for_time) |
| 2052 | { |
| 2053 | vppcom_main_t *vcm = &vppcom_main; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2054 | session_t *listen_session = 0; |
| 2055 | session_t *client_session = 0; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2056 | u32 client_session_index; |
| 2057 | int rv; |
| 2058 | f64 wait_for; |
| 2059 | |
| 2060 | clib_spinlock_lock (&vcm->sessions_lockp); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2061 | rv = vppcom_session_at_index (listen_session_index, &listen_session); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2062 | if (PREDICT_FALSE (rv)) |
| 2063 | { |
| 2064 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2065 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2066 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2067 | vcm->my_pid, listen_session_index); |
| 2068 | return rv; |
| 2069 | } |
| 2070 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2071 | if (listen_session->state != STATE_LISTEN) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2072 | { |
| 2073 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2074 | if (VPPCOM_DEBUG > 0) |
| 2075 | clib_warning ("[%d] session not in listen state, state = %s", |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2076 | vcm->my_pid, |
| 2077 | vppcom_session_state_str (listen_session->state)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2078 | return VPPCOM_EBADFD; |
| 2079 | } |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2080 | wait_for = listen_session->is_nonblocking ? 0 : |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2081 | (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time; |
| 2082 | |
| 2083 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2084 | clib_warning ("[%d] sid %d: %s (%d)", vcm->my_pid, |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2085 | listen_session_index, |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2086 | vppcom_session_state_str (listen_session->state), |
| 2087 | listen_session->state); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2088 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2089 | |
| 2090 | while (1) |
| 2091 | { |
| 2092 | rv = vppcom_wait_for_client_session_index (wait_for); |
| 2093 | if (rv) |
| 2094 | { |
| 2095 | if ((VPPCOM_DEBUG > 0)) |
| 2096 | clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)", |
| 2097 | vcm->my_pid, listen_session_index, |
| 2098 | vppcom_retval_str (rv), rv); |
| 2099 | if ((wait_for == 0) || (wait_for_time > 0)) |
| 2100 | return rv; |
| 2101 | } |
| 2102 | else |
| 2103 | break; |
| 2104 | } |
| 2105 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2106 | clib_spinlock_lock (&vcm->sessions_lockp); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2107 | clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2108 | rv = vppcom_session_at_index (client_session_index, &client_session); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2109 | ASSERT (rv == VPPCOM_OK); |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2110 | ASSERT (client_session->peer_addr.is_ip4 == |
| 2111 | listen_session->lcl_addr.is_ip4); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2112 | |
| 2113 | if (VPPCOM_DEBUG > 0) |
| 2114 | clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid, |
| 2115 | client_session_index); |
| 2116 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2117 | ep->vrf = client_session->vrf; |
| 2118 | ep->is_cut_thru = client_session->is_cut_thru; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2119 | ep->is_ip4 = client_session->peer_addr.is_ip4; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2120 | ep->port = client_session->port; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2121 | if (client_session->peer_addr.is_ip4) |
| 2122 | clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4, |
| 2123 | sizeof (ip4_address_t)); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2124 | else |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2125 | clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6, |
| 2126 | sizeof (ip6_address_t)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2127 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2128 | return (int) client_session_index; |
| 2129 | } |
| 2130 | |
| 2131 | int |
| 2132 | vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep) |
| 2133 | { |
| 2134 | vppcom_main_t *vcm = &vppcom_main; |
| 2135 | session_t *session = 0; |
| 2136 | int rv; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2137 | |
| 2138 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2139 | rv = vppcom_session_at_index (session_index, &session); |
| 2140 | if (PREDICT_FALSE (rv)) |
| 2141 | { |
| 2142 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2143 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2144 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2145 | vcm->my_pid, session_index); |
| 2146 | return rv; |
| 2147 | } |
| 2148 | |
| 2149 | if (session->state == STATE_CONNECT) |
| 2150 | { |
| 2151 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2152 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2153 | clib_warning ("[%d] session, sid (%u) already connected!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2154 | vcm->my_pid, session_index); |
| 2155 | return VPPCOM_OK; |
| 2156 | } |
| 2157 | |
| 2158 | session->vrf = server_ep->vrf; |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2159 | session->peer_addr.is_ip4 = server_ep->is_ip4; |
| 2160 | session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2161 | session->port = server_ep->port; |
| 2162 | |
| 2163 | if (VPPCOM_DEBUG > 0) |
| 2164 | { |
| 2165 | u8 *ip_str = format (0, "%U", format_ip46_address, |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 2166 | &session->peer_addr.ip46, |
| 2167 | session->peer_addr.is_ip4); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2168 | clib_warning ("[%d] connect sid %d to %s server port %d", |
| 2169 | vcm->my_pid, session_index, ip_str, |
| 2170 | clib_net_to_host_u16 (session->port)); |
| 2171 | vec_free (ip_str); |
| 2172 | } |
| 2173 | |
| 2174 | vppcom_send_connect_sock (session, session_index); |
| 2175 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2176 | rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT, |
| 2177 | vcm->cfg.session_timeout); |
| 2178 | if (PREDICT_FALSE (rv)) |
| 2179 | { |
| 2180 | if (VPPCOM_DEBUG > 0) |
| 2181 | clib_warning ("[%d] connect timed out, rv = %s (%d)", |
| 2182 | vcm->my_pid, vppcom_retval_str (rv), rv); |
| 2183 | return rv; |
| 2184 | } |
| 2185 | return VPPCOM_OK; |
| 2186 | } |
| 2187 | |
| 2188 | int |
| 2189 | vppcom_session_read (uint32_t session_index, void *buf, int n) |
| 2190 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2191 | vppcom_main_t *vcm = &vppcom_main; |
| 2192 | session_t *session = 0; |
| 2193 | svm_fifo_t *rx_fifo; |
| 2194 | int n_read = 0; |
| 2195 | int rv; |
| 2196 | char *fifo_str; |
| 2197 | |
| 2198 | ASSERT (buf); |
| 2199 | |
| 2200 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2201 | rv = vppcom_session_at_index (session_index, &session); |
| 2202 | if (PREDICT_FALSE (rv)) |
| 2203 | { |
| 2204 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2205 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2206 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2207 | vcm->my_pid, session_index); |
| 2208 | return rv; |
| 2209 | } |
| 2210 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2211 | if (session->state == STATE_DISCONNECT) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2212 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2213 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2214 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2215 | clib_warning ("[%d] sid (%u) has been closed by remote peer!", |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2216 | vcm->my_pid, session_index); |
| 2217 | return VPPCOM_ECONNRESET; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2218 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2219 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2220 | rx_fifo = ((!session->is_cut_thru || session->is_server) ? |
| 2221 | session->server_rx_fifo : session->server_tx_fifo); |
| 2222 | fifo_str = ((!session->is_cut_thru || session->is_server) ? |
| 2223 | "server_rx_fifo" : "server_tx_fifo"); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2224 | |
| 2225 | do |
| 2226 | { |
| 2227 | n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf); |
| 2228 | } |
| 2229 | while (!session->is_nonblocking && (n_read <= 0)); |
| 2230 | |
| 2231 | if (n_read <= 0) |
| 2232 | session->vep.et_mask |= EPOLLIN; |
| 2233 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2234 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2235 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2236 | if ((VPPCOM_DEBUG > 2) && (n_read > 0)) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2237 | clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid, |
| 2238 | session_index, n_read, fifo_str, rx_fifo); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2239 | |
| 2240 | return (n_read <= 0) ? VPPCOM_EAGAIN : n_read; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | static inline int |
| 2244 | vppcom_session_read_ready (session_t * session, u32 session_index) |
| 2245 | { |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2246 | vppcom_main_t *vcm = &vppcom_main; |
| 2247 | svm_fifo_t *rx_fifo; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2248 | int ready = 0; |
| 2249 | |
| 2250 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2251 | if (session->state == STATE_DISCONNECT) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2252 | { |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2253 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2254 | clib_warning ("[%d] sid (%u) has been closed by remote peer!", |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2255 | vcm->my_pid, session_index); |
| 2256 | return VPPCOM_ECONNRESET; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2257 | } |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2258 | |
| 2259 | if (session->is_listen) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2260 | ready = clib_fifo_elts (vcm->client_session_index_fifo); |
| 2261 | else |
| 2262 | { |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2263 | rx_fifo = ((!session->is_cut_thru || session->is_server) ? |
| 2264 | session->server_rx_fifo : session->server_tx_fifo); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2265 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2266 | ready = svm_fifo_max_dequeue (rx_fifo); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2267 | } |
| 2268 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2269 | if (VPPCOM_DEBUG > 3) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2270 | clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid, |
| 2271 | session_index, |
| 2272 | session->is_server ? "server_rx_fifo" : "server_tx_fifo", |
| 2273 | rx_fifo, ready); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2274 | if (ready == 0) |
| 2275 | session->vep.et_mask |= EPOLLIN; |
| 2276 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2277 | return ready; |
| 2278 | } |
| 2279 | |
| 2280 | int |
| 2281 | vppcom_session_write (uint32_t session_index, void *buf, int n) |
| 2282 | { |
| 2283 | vppcom_main_t *vcm = &vppcom_main; |
| 2284 | session_t *session = 0; |
| 2285 | svm_fifo_t *tx_fifo; |
| 2286 | unix_shared_memory_queue_t *q; |
| 2287 | session_fifo_event_t evt; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2288 | int rv, n_write; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2289 | char *fifo_str; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2290 | |
| 2291 | ASSERT (buf); |
| 2292 | |
| 2293 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2294 | rv = vppcom_session_at_index (session_index, &session); |
| 2295 | if (PREDICT_FALSE (rv)) |
| 2296 | { |
| 2297 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2298 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2299 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2300 | vcm->my_pid, session_index); |
| 2301 | return rv; |
| 2302 | } |
| 2303 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2304 | if (session->state == STATE_DISCONNECT) |
| 2305 | { |
| 2306 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2307 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2308 | clib_warning ("[%d] sid (%u) has been closed by remote peer!", |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2309 | vcm->my_pid, session_index); |
| 2310 | return VPPCOM_ECONNRESET; |
| 2311 | } |
| 2312 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2313 | tx_fifo = ((!session->is_cut_thru || session->is_server) ? |
| 2314 | session->server_tx_fifo : session->server_rx_fifo); |
| 2315 | fifo_str = ((!session->is_cut_thru || session->is_server) ? |
| 2316 | "server_tx_fifo" : "server_rx_fifo"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2317 | do |
| 2318 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2319 | n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2320 | } |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2321 | while (!session->is_nonblocking && (n_write <= 0)); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2322 | |
| 2323 | /* If event wasn't set, add one */ |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2324 | if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo)) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2325 | { |
| 2326 | int rval; |
| 2327 | |
| 2328 | /* Fabricate TX event, send to vpp */ |
| 2329 | evt.fifo = tx_fifo; |
| 2330 | evt.event_type = FIFO_EVENT_APP_TX; |
| 2331 | evt.event_id = vcm->tx_event_id++; |
| 2332 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2333 | rval = vppcom_session_at_index (session_index, &session); |
| 2334 | if (PREDICT_FALSE (rval)) |
| 2335 | { |
| 2336 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2337 | if (VPPCOM_DEBUG > 1) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2338 | clib_warning ("[%d] invalid session, sid (%u) has been closed!", |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2339 | vcm->my_pid, session_index); |
| 2340 | return rval; |
| 2341 | } |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2342 | q = session->vpp_event_queue; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2343 | ASSERT (q); |
| 2344 | unix_shared_memory_queue_add (q, (u8 *) & evt, |
| 2345 | 0 /* do wait for mutex */ ); |
| 2346 | } |
| 2347 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2348 | if (n_write <= 0) |
| 2349 | session->vep.et_mask |= EPOLLOUT; |
| 2350 | |
| 2351 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2352 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2353 | if (VPPCOM_DEBUG > 2) |
| 2354 | clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid, |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2355 | session_index, n_write, fifo_str, tx_fifo); |
| 2356 | return n_write; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | static inline int |
| 2360 | vppcom_session_write_ready (session_t * session, u32 session_index) |
| 2361 | { |
| 2362 | vppcom_main_t *vcm = &vppcom_main; |
| 2363 | svm_fifo_t *tx_fifo; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2364 | char *fifo_str; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2365 | int ready; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2366 | |
| 2367 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2368 | if (session->state == STATE_DISCONNECT) |
| 2369 | { |
| 2370 | if (VPPCOM_DEBUG > 0) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2371 | clib_warning ("[%d] sid (%u) has been closed by remote peer!", |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2372 | vcm->my_pid, session_index); |
| 2373 | return VPPCOM_ECONNRESET; |
| 2374 | } |
| 2375 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2376 | tx_fifo = ((!session->is_cut_thru || session->is_server) ? |
| 2377 | session->server_tx_fifo : session->server_rx_fifo); |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2378 | fifo_str = ((!session->is_cut_thru || session->is_server) ? |
| 2379 | "server_tx_fifo" : "server_rx_fifo"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2380 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2381 | ready = svm_fifo_max_enqueue (tx_fifo); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2382 | |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 2383 | if (VPPCOM_DEBUG > 3) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2384 | clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid, |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2385 | session_index, fifo_str, tx_fifo, ready); |
| 2386 | if (ready == 0) |
| 2387 | session->vep.et_mask |= EPOLLOUT; |
| 2388 | |
| 2389 | return ready; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | int |
| 2393 | vppcom_select (unsigned long n_bits, unsigned long *read_map, |
| 2394 | unsigned long *write_map, unsigned long *except_map, |
| 2395 | double time_to_wait) |
| 2396 | { |
| 2397 | vppcom_main_t *vcm = &vppcom_main; |
| 2398 | u32 session_index; |
| 2399 | session_t *session = 0; |
| 2400 | int rv, bits_set = 0; |
| 2401 | f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait; |
| 2402 | u32 minbits = clib_max (n_bits, BITS (uword)); |
| 2403 | |
| 2404 | ASSERT (sizeof (clib_bitmap_t) == sizeof (long int)); |
| 2405 | |
| 2406 | if (read_map) |
| 2407 | { |
| 2408 | clib_bitmap_validate (vcm->rd_bitmap, minbits); |
| 2409 | clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap)); |
| 2410 | memset (read_map, 0, vec_len (vcm->rd_bitmap)); |
| 2411 | } |
| 2412 | if (write_map) |
| 2413 | { |
| 2414 | clib_bitmap_validate (vcm->wr_bitmap, minbits); |
| 2415 | clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap)); |
| 2416 | memset (write_map, 0, vec_len (vcm->wr_bitmap)); |
| 2417 | } |
| 2418 | if (except_map) |
| 2419 | { |
| 2420 | clib_bitmap_validate (vcm->ex_bitmap, minbits); |
| 2421 | clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap)); |
| 2422 | memset (except_map, 0, vec_len (vcm->ex_bitmap)); |
| 2423 | } |
| 2424 | |
| 2425 | do |
| 2426 | { |
| 2427 | /* *INDENT-OFF* */ |
| 2428 | clib_bitmap_foreach (session_index, vcm->rd_bitmap, |
| 2429 | ({ |
| 2430 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2431 | rv = vppcom_session_at_index (session_index, &session); |
| 2432 | if (rv < 0) |
| 2433 | { |
| 2434 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2435 | if (VPPCOM_DEBUG > 1) |
| 2436 | clib_warning ("[%d] session %d specified in " |
| 2437 | "read_map is closed.", vcm->my_pid, |
| 2438 | session_index); |
| 2439 | bits_set = VPPCOM_EBADFD; |
| 2440 | goto select_done; |
| 2441 | } |
| 2442 | |
| 2443 | rv = vppcom_session_read_ready (session, session_index); |
| 2444 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2445 | if (vcm->ex_bitmap && |
| 2446 | clib_bitmap_get (vcm->ex_bitmap, session_index) && (rv < 0)) |
| 2447 | { |
| 2448 | // TBD: clib_warning |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 2449 | /* coverity[FORWARD_NULL] */ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2450 | clib_bitmap_set_no_check (except_map, session_index, 1); |
| 2451 | bits_set++; |
| 2452 | } |
| 2453 | else if (rv > 0) |
| 2454 | { |
| 2455 | // TBD: clib_warning |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 2456 | /* coverity[FORWARD_NULL] */ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2457 | clib_bitmap_set_no_check (read_map, session_index, 1); |
| 2458 | bits_set++; |
| 2459 | } |
| 2460 | })); |
| 2461 | |
| 2462 | clib_bitmap_foreach (session_index, vcm->wr_bitmap, |
| 2463 | ({ |
| 2464 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2465 | rv = vppcom_session_at_index (session_index, &session); |
| 2466 | if (rv < 0) |
| 2467 | { |
| 2468 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2469 | if (VPPCOM_DEBUG > 0) |
| 2470 | clib_warning ("[%d] session %d specified in " |
| 2471 | "write_map is closed.", vcm->my_pid, |
| 2472 | session_index); |
| 2473 | bits_set = VPPCOM_EBADFD; |
| 2474 | goto select_done; |
| 2475 | } |
| 2476 | |
| 2477 | rv = vppcom_session_write_ready (session, session_index); |
| 2478 | clib_spinlock_unlock (&vcm->sessions_lockp); |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 2479 | if (rv > 0 ) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2480 | { |
| 2481 | // TBD: clib_warning |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 2482 | /* coverity[FORWARD_NULL] */ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2483 | clib_bitmap_set_no_check (write_map, session_index, 1); |
| 2484 | bits_set++; |
| 2485 | } |
| 2486 | })); |
| 2487 | |
| 2488 | clib_bitmap_foreach (session_index, vcm->ex_bitmap, |
| 2489 | ({ |
| 2490 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2491 | rv = vppcom_session_at_index (session_index, &session); |
| 2492 | if (rv < 0) |
| 2493 | { |
| 2494 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2495 | if (VPPCOM_DEBUG > 1) |
| 2496 | clib_warning ("[%d] session %d specified in " |
| 2497 | "except_map is closed.", vcm->my_pid, |
| 2498 | session_index); |
| 2499 | bits_set = VPPCOM_EBADFD; |
| 2500 | goto select_done; |
| 2501 | } |
| 2502 | |
| 2503 | rv = vppcom_session_read_ready (session, session_index); |
| 2504 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2505 | if (rv < 0) |
| 2506 | { |
| 2507 | // TBD: clib_warning |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 2508 | /* coverity[FORWARD_NULL] */ |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 2509 | clib_bitmap_set_no_check (except_map, session_index, 1); |
| 2510 | bits_set++; |
| 2511 | } |
| 2512 | })); |
| 2513 | /* *INDENT-ON* */ |
| 2514 | } |
| 2515 | while (clib_time_now (&vcm->clib_time) < timeout); |
| 2516 | |
| 2517 | select_done: |
| 2518 | return (bits_set); |
| 2519 | } |
| 2520 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 2521 | static inline void |
| 2522 | vep_verify_epoll_chain (u32 vep_idx) |
| 2523 | { |
| 2524 | session_t *session; |
| 2525 | vppcom_epoll_t *vep; |
| 2526 | int rv; |
| 2527 | u32 sid; |
| 2528 | |
| 2529 | if (VPPCOM_DEBUG < 1) |
| 2530 | return; |
| 2531 | |
| 2532 | /* Assumes caller has acquired spinlock: vcm->sessions_lockp */ |
| 2533 | rv = vppcom_session_at_index (vep_idx, &session); |
| 2534 | if (PREDICT_FALSE (rv)) |
| 2535 | { |
| 2536 | clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx); |
| 2537 | goto done; |
| 2538 | } |
| 2539 | if (PREDICT_FALSE (!session->is_vep)) |
| 2540 | { |
| 2541 | clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx); |
| 2542 | goto done; |
| 2543 | } |
| 2544 | if (VPPCOM_DEBUG > 1) |
| 2545 | clib_warning ("vep_idx (%u): Dumping epoll chain\n" |
| 2546 | "{\n" |
| 2547 | " is_vep = %u\n" |
| 2548 | " is_vep_session = %u\n" |
| 2549 | " wait_cont_idx = 0x%x (%u)\n" |
| 2550 | "}\n", |
| 2551 | vep_idx, session->is_vep, session->is_vep_session, |
| 2552 | session->wait_cont_idx, session->wait_cont_idx); |
| 2553 | do |
| 2554 | { |
| 2555 | vep = &session->vep; |
| 2556 | if (session->is_vep_session) |
| 2557 | { |
| 2558 | if (VPPCOM_DEBUG > 1) |
| 2559 | clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n" |
| 2560 | "{\n" |
| 2561 | " next_sid = 0x%x (%u)\n" |
| 2562 | " prev_sid = 0x%x (%u)\n" |
| 2563 | " vep_idx = 0x%x (%u)\n" |
| 2564 | " ev.events = 0x%x\n" |
| 2565 | " ev.data.u64 = 0x%llx\n" |
| 2566 | " et_mask = 0x%x\n" |
| 2567 | "}\n", |
| 2568 | vep_idx, sid, sid, |
| 2569 | vep->next_sid, vep->next_sid, |
| 2570 | vep->prev_sid, vep->prev_sid, |
| 2571 | vep->vep_idx, vep->vep_idx, |
| 2572 | vep->ev.events, vep->ev.data.u64, vep->et_mask); |
| 2573 | } |
| 2574 | sid = vep->next_sid; |
| 2575 | if (sid != ~0) |
| 2576 | { |
| 2577 | rv = vppcom_session_at_index (sid, &session); |
| 2578 | if (PREDICT_FALSE (rv)) |
| 2579 | { |
| 2580 | clib_warning ("ERROR: Invalid sid (%u)!", sid); |
| 2581 | goto done; |
| 2582 | } |
| 2583 | if (PREDICT_FALSE (session->is_vep)) |
| 2584 | clib_warning ("ERROR: sid (%u) is a vep!", vep_idx); |
| 2585 | else if (PREDICT_FALSE (!session->is_vep_session)) |
| 2586 | { |
| 2587 | clib_warning ("ERROR: session (%u) is not a vep session!", sid); |
| 2588 | goto done; |
| 2589 | } |
| 2590 | if (PREDICT_FALSE (session->vep.vep_idx != vep_idx)) |
| 2591 | clib_warning ("ERROR: session (%u) vep_idx (%u) != " |
| 2592 | "vep_idx (%u)!", |
| 2593 | sid, session->vep.vep_idx, vep_idx); |
| 2594 | } |
| 2595 | } |
| 2596 | while (sid != ~0); |
| 2597 | |
| 2598 | done: |
| 2599 | if (VPPCOM_DEBUG > 1) |
| 2600 | clib_warning ("vep_idx (%u): Dump complete!", vep_idx); |
| 2601 | } |
| 2602 | |
| 2603 | int |
| 2604 | vppcom_epoll_create (void) |
| 2605 | { |
| 2606 | vppcom_main_t *vcm = &vppcom_main; |
| 2607 | session_t *vep_session; |
| 2608 | u32 vep_idx; |
| 2609 | |
| 2610 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2611 | pool_get (vcm->sessions, vep_session); |
| 2612 | memset (vep_session, 0, sizeof (*vep_session)); |
| 2613 | vep_idx = vep_session - vcm->sessions; |
| 2614 | |
| 2615 | vep_session->is_vep = 1; |
| 2616 | vep_session->vep.vep_idx = ~0; |
| 2617 | vep_session->vep.next_sid = ~0; |
| 2618 | vep_session->vep.prev_sid = ~0; |
| 2619 | vep_session->wait_cont_idx = ~0; |
| 2620 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2621 | |
| 2622 | if (VPPCOM_DEBUG > 0) |
| 2623 | clib_warning ("Created vep_idx %u!", vep_idx); |
| 2624 | |
| 2625 | return (vep_idx); |
| 2626 | } |
| 2627 | |
| 2628 | int |
| 2629 | vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index, |
| 2630 | struct epoll_event *event) |
| 2631 | { |
| 2632 | vppcom_main_t *vcm = &vppcom_main; |
| 2633 | session_t *vep_session; |
| 2634 | session_t *session; |
| 2635 | int rv; |
| 2636 | |
| 2637 | if (vep_idx == session_index) |
| 2638 | { |
| 2639 | if (VPPCOM_DEBUG > 0) |
| 2640 | clib_warning ("ERROR: vep_idx == session_index (%u)!", vep_idx); |
| 2641 | return VPPCOM_EINVAL; |
| 2642 | } |
| 2643 | |
| 2644 | clib_spinlock_lock (&vcm->sessions_lockp); |
| 2645 | rv = vppcom_session_at_index (vep_idx, &vep_session); |
| 2646 | if (PREDICT_FALSE (rv)) |
| 2647 | { |
| 2648 | if (VPPCOM_DEBUG > 0) |
| 2649 | clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx); |
| 2650 | goto done; |
| 2651 | } |
| 2652 | if (PREDICT_FALSE (!vep_session->is_vep)) |
| 2653 | { |
| 2654 | if (VPPCOM_DEBUG > 0) |
| 2655 | clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx); |
| 2656 | rv = VPPCOM_EINVAL; |
| 2657 | goto done; |
| 2658 | } |
| 2659 | |
| 2660 | ASSERT (vep_session->vep.vep_idx == ~0); |
| 2661 | ASSERT (vep_session->vep.prev_sid == ~0); |
| 2662 | |
| 2663 | rv = vppcom_session_at_index (session_index, &session); |
| 2664 | if (PREDICT_FALSE (rv)) |
| 2665 | { |
| 2666 | if (VPPCOM_DEBUG > 0) |
| 2667 | clib_warning ("ERROR: Invalid session_index (%u)!", session_index); |
| 2668 | goto done; |
| 2669 | } |
| 2670 | if (PREDICT_FALSE (session->is_vep)) |
| 2671 | { |
| 2672 | if (VPPCOM_DEBUG > 0) |
| 2673 | clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx); |
| 2674 | rv = VPPCOM_EINVAL; |
| 2675 | goto done; |
| 2676 | } |
| 2677 | |
| 2678 | switch (op) |
| 2679 | { |
| 2680 | case EPOLL_CTL_ADD: |
| 2681 | if (PREDICT_FALSE (!event)) |
| 2682 | { |
| 2683 | clib_warning ("NULL pointer to epoll_event structure!"); |
| 2684 | rv = VPPCOM_EINVAL; |
| 2685 | goto done; |
| 2686 | } |
| 2687 | if (vep_session->vep.next_sid != ~0) |
| 2688 | { |
| 2689 | session_t *next_session; |
| 2690 | rv = vppcom_session_at_index (vep_session->vep.next_sid, |
| 2691 | &next_session); |
| 2692 | if (PREDICT_FALSE (rv)) |
| 2693 | { |
| 2694 | if (VPPCOM_DEBUG > 0) |
| 2695 | clib_warning ("EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on" |
| 2696 | " vep_idx (%u)!", vep_session->vep.next_sid, |
| 2697 | vep_idx); |
| 2698 | goto done; |
| 2699 | } |
| 2700 | ASSERT (next_session->vep.prev_sid == vep_idx); |
| 2701 | next_session->vep.prev_sid = session_index; |
| 2702 | } |
| 2703 | session->vep.next_sid = vep_session->vep.next_sid; |
| 2704 | session->vep.prev_sid = vep_idx; |
| 2705 | session->vep.vep_idx = vep_idx; |
| 2706 | session->vep.et_mask = VEP_DEFAULT_ET_MASK; |
| 2707 | session->vep.ev = *event; |
| 2708 | session->is_vep_session = 1; |
| 2709 | vep_session->vep.next_sid = session_index; |
| 2710 | if (VPPCOM_DEBUG > 1) |
| 2711 | clib_warning ("EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x," |
| 2712 | " data 0x%llx!", vep_idx, session_index, |
| 2713 | event->events, event->data.u64); |
| 2714 | break; |
| 2715 | |
| 2716 | case EPOLL_CTL_MOD: |
| 2717 | if (PREDICT_FALSE (!event)) |
| 2718 | { |
| 2719 | clib_warning ("NULL pointer to epoll_event structure!"); |
| 2720 | rv = VPPCOM_EINVAL; |
| 2721 | goto done; |
| 2722 | } |
| 2723 | if (PREDICT_FALSE (!session->is_vep_session && |
| 2724 | (session->vep.vep_idx != vep_idx))) |
| 2725 | { |
| 2726 | if (VPPCOM_DEBUG > 0) |
| 2727 | { |
| 2728 | if (!session->is_vep_session) |
| 2729 | clib_warning ("EPOLL_CTL_MOD: session (%u) is not " |
| 2730 | "a vep session!", session_index); |
| 2731 | else |
| 2732 | clib_warning ("EPOLL_CTL_MOD: session (%u) vep_idx (%u) != " |
| 2733 | "vep_idx (%u)!", session_index, |
| 2734 | session->vep.vep_idx, vep_idx); |
| 2735 | } |
| 2736 | rv = VPPCOM_EINVAL; |
| 2737 | goto done; |
| 2738 | } |
| 2739 | session->vep.et_mask = VEP_DEFAULT_ET_MASK; |
| 2740 | session->vep.ev = *event; |
| 2741 | if (VPPCOM_DEBUG > 1) |
| 2742 | clib_warning ("EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x," |
| 2743 | " data 0x%llx!", vep_idx, session_index, |
| 2744 | event->events, event->data.u64); |
| 2745 | break; |
| 2746 | |
| 2747 | case EPOLL_CTL_DEL: |
| 2748 | if (PREDICT_FALSE (!session->is_vep_session && |
| 2749 | (session->vep.vep_idx != vep_idx))) |
| 2750 | { |
| 2751 | if (VPPCOM_DEBUG > 0) |
| 2752 | { |
| 2753 | if (!session->is_vep_session) |
| 2754 | clib_warning ("EPOLL_CTL_DEL: session (%u) is not " |
| 2755 | "a vep session!", session_index); |
| 2756 | else |
| 2757 | clib_warning ("EPOLL_CTL_DEL: session (%u) vep_idx (%u) != " |
| 2758 | "vep_idx (%u)!", session_index, |
| 2759 | session->vep.vep_idx, vep_idx); |
| 2760 | } |
| 2761 | rv = VPPCOM_EINVAL; |
| 2762 | goto done; |
| 2763 | } |
| 2764 | |
| 2765 | vep_session->wait_cont_idx = |
| 2766 | (vep_session->wait_cont_idx == session_index) ? |
| 2767 | session->vep.next_sid : vep_session->wait_cont_idx; |
| 2768 | |
| 2769 | if (session->vep.prev_sid == vep_idx) |
| 2770 | vep_session->vep.next_sid = session->vep.next_sid; |
| 2771 | else |
| 2772 | { |
| 2773 | session_t *prev_session; |
| 2774 | rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session); |
| 2775 | if (PREDICT_FALSE (rv)) |
| 2776 | { |
| 2777 | if (VPPCOM_DEBUG > 0) |
| 2778 | clib_warning ("EPOLL_CTL_DEL: Invalid vep.prev_sid (%u) on" |
| 2779 | " sid (%u)!", session->vep.prev_sid, |
| 2780 | session_index); |
| 2781 | goto done; |
| 2782 | } |
| 2783 | ASSERT (prev_session->vep.next_sid == session_index); |
| 2784 | prev_session->vep.next_sid = session->vep.next_sid; |
| 2785 | } |
| 2786 | if (session->vep.next_sid != ~0) |
| 2787 | { |
| 2788 | session_t *next_session; |
| 2789 | rv = vppcom_session_at_index (session->vep.next_sid, &next_session); |
| 2790 | if (PREDICT_FALSE (rv)) |
| 2791 | { |
| 2792 | if (VPPCOM_DEBUG > 0) |
| 2793 | clib_warning ("EPOLL_CTL_DEL: Invalid vep.next_sid (%u) on" |
| 2794 | " sid (%u)!", session->vep.next_sid, |
| 2795 | session_index); |
| 2796 | goto done; |
| 2797 | } |
| 2798 | ASSERT (next_session->vep.prev_sid == session_index); |
| 2799 | next_session->vep.prev_sid = session->vep.prev_sid; |
| 2800 | } |
| 2801 | |
| 2802 | memset (&session->vep, 0, sizeof (session->vep)); |
| 2803 | session->vep.next_sid = ~0; |
| 2804 | session->vep.prev_sid = ~0; |
| 2805 | session->vep.vep_idx = ~0; |
| 2806 | session->is_vep_session = 0; |
| 2807 | if (VPPCOM_DEBUG > 1) |
| 2808 | clib_warning ("EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_idx, |
| 2809 | session_index); |
| 2810 | break; |
| 2811 | |
| 2812 | default: |
| 2813 | clib_warning ("Invalid operation (%d)!", op); |
| 2814 | rv = VPPCOM_EINVAL; |
| 2815 | } |
| 2816 | |
| 2817 | vep_verify_epoll_chain (vep_idx); |
| 2818 | |
| 2819 | done: |
| 2820 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2821 | return rv; |
| 2822 | } |
| 2823 | |
| 2824 | #define VCL_LOCK_AND_GET_SESSION(I, S) \ |
| 2825 | do { \ |
| 2826 | vppcom_main_t *vcm = &vppcom_main; \ |
| 2827 | \ |
| 2828 | clib_spinlock_lock (&vcm->sessions_lockp); \ |
| 2829 | rv = vppcom_session_at_index (I, S); \ |
| 2830 | if (PREDICT_FALSE (rv)) \ |
| 2831 | { \ |
| 2832 | clib_spinlock_unlock (&vcm->sessions_lockp); \ |
| 2833 | \ |
| 2834 | if (VPPCOM_DEBUG > 0) \ |
| 2835 | clib_warning ("ERROR: Invalid ##I (%u)!", I); \ |
| 2836 | \ |
| 2837 | goto done; \ |
| 2838 | } \ |
| 2839 | } while (0) |
| 2840 | |
| 2841 | int |
| 2842 | vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events, |
| 2843 | int maxevents, double wait_for_time) |
| 2844 | { |
| 2845 | vppcom_main_t *vcm = &vppcom_main; |
| 2846 | session_t *vep_session; |
| 2847 | int rv; |
| 2848 | f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time; |
| 2849 | int num_ev = 0; |
| 2850 | u32 vep_next_sid, wait_cont_idx; |
| 2851 | u8 is_vep; |
| 2852 | |
| 2853 | if (PREDICT_FALSE (maxevents <= 0)) |
| 2854 | { |
| 2855 | if (VPPCOM_DEBUG > 0) |
| 2856 | clib_warning ("ERROR: Invalid maxevents (%d)!", maxevents); |
| 2857 | return VPPCOM_EINVAL; |
| 2858 | } |
| 2859 | if (PREDICT_FALSE (wait_for_time < 0)) |
| 2860 | { |
| 2861 | if (VPPCOM_DEBUG > 0) |
| 2862 | clib_warning ("ERROR: Invalid wait_for_time (%f)!", wait_for_time); |
| 2863 | return VPPCOM_EINVAL; |
| 2864 | } |
| 2865 | memset (events, 0, sizeof (*events) * maxevents); |
| 2866 | |
| 2867 | VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session); |
| 2868 | vep_next_sid = vep_session->vep.next_sid; |
| 2869 | is_vep = vep_session->is_vep; |
| 2870 | wait_cont_idx = vep_session->wait_cont_idx; |
| 2871 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2872 | |
| 2873 | if (PREDICT_FALSE (!is_vep)) |
| 2874 | { |
| 2875 | if (VPPCOM_DEBUG > 0) |
| 2876 | clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx); |
| 2877 | rv = VPPCOM_EINVAL; |
| 2878 | goto done; |
| 2879 | } |
| 2880 | if ((VPPCOM_DEBUG > 0) && (PREDICT_FALSE (vep_next_sid == ~0))) |
| 2881 | { |
| 2882 | clib_warning ("WARNING: vep_idx (%u) is empty!", vep_idx); |
| 2883 | goto done; |
| 2884 | } |
| 2885 | |
| 2886 | do |
| 2887 | { |
| 2888 | u32 sid; |
| 2889 | u32 next_sid = ~0; |
| 2890 | session_t *session; |
| 2891 | |
| 2892 | for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx; |
| 2893 | sid != ~0; sid = next_sid) |
| 2894 | { |
| 2895 | u32 session_events, et_mask, clear_et_mask, session_vep_idx; |
| 2896 | u8 add_event, is_vep_session; |
| 2897 | int ready; |
| 2898 | u64 session_ev_data; |
| 2899 | |
| 2900 | VCL_LOCK_AND_GET_SESSION (sid, &session); |
| 2901 | next_sid = session->vep.next_sid; |
| 2902 | session_events = session->vep.ev.events; |
| 2903 | et_mask = session->vep.et_mask; |
| 2904 | is_vep = session->is_vep; |
| 2905 | is_vep_session = session->is_vep_session; |
| 2906 | session_vep_idx = session->vep.vep_idx; |
| 2907 | session_ev_data = session->vep.ev.data.u64; |
| 2908 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2909 | |
| 2910 | if (PREDICT_FALSE (is_vep)) |
| 2911 | { |
| 2912 | if (VPPCOM_DEBUG > 0) |
| 2913 | clib_warning ("ERROR: sid (%u) is a vep!", vep_idx); |
| 2914 | rv = VPPCOM_EINVAL; |
| 2915 | goto done; |
| 2916 | } |
| 2917 | if (PREDICT_FALSE (!is_vep_session)) |
| 2918 | { |
| 2919 | if (VPPCOM_DEBUG > 0) |
| 2920 | clib_warning ("EPOLL_CTL_MOD: session (%u) is not " |
| 2921 | "a vep session!", sid); |
| 2922 | rv = VPPCOM_EINVAL; |
| 2923 | goto done; |
| 2924 | } |
| 2925 | if (PREDICT_FALSE (session_vep_idx != vep_idx)) |
| 2926 | { |
| 2927 | clib_warning ("EPOLL_CTL_MOD: session (%u) " |
| 2928 | "vep_idx (%u) != vep_idx (%u)!", |
| 2929 | sid, session->vep.vep_idx, vep_idx); |
| 2930 | rv = VPPCOM_EINVAL; |
| 2931 | goto done; |
| 2932 | } |
| 2933 | |
| 2934 | add_event = clear_et_mask = 0; |
| 2935 | |
| 2936 | if ((EPOLLIN & session_events) && (EPOLLIN & et_mask)) |
| 2937 | { |
| 2938 | VCL_LOCK_AND_GET_SESSION (sid, &session); |
| 2939 | ready = vppcom_session_read_ready (session, sid); |
| 2940 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2941 | if (ready > 0) |
| 2942 | { |
| 2943 | add_event = 1; |
| 2944 | events[num_ev].events |= EPOLLIN; |
| 2945 | if (EPOLLET & session_events) |
| 2946 | clear_et_mask |= EPOLLIN; |
| 2947 | } |
| 2948 | else if (ready < 0) |
| 2949 | { |
| 2950 | add_event = 1; |
| 2951 | switch (ready) |
| 2952 | { |
| 2953 | case VPPCOM_ECONNRESET: |
| 2954 | events[num_ev].events |= EPOLLHUP | EPOLLRDHUP; |
| 2955 | break; |
| 2956 | |
| 2957 | default: |
| 2958 | events[num_ev].events |= EPOLLERR; |
| 2959 | break; |
| 2960 | } |
| 2961 | } |
| 2962 | } |
| 2963 | |
| 2964 | if ((EPOLLOUT & session_events) && (EPOLLOUT & et_mask)) |
| 2965 | { |
| 2966 | VCL_LOCK_AND_GET_SESSION (sid, &session); |
| 2967 | ready = vppcom_session_write_ready (session, sid); |
| 2968 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 2969 | if (ready > 0) |
| 2970 | { |
| 2971 | add_event = 1; |
| 2972 | events[num_ev].events |= EPOLLOUT; |
| 2973 | if (EPOLLET & session_events) |
| 2974 | clear_et_mask |= EPOLLOUT; |
| 2975 | } |
| 2976 | else if (ready < 0) |
| 2977 | { |
| 2978 | add_event = 1; |
| 2979 | switch (ready) |
| 2980 | { |
| 2981 | case VPPCOM_ECONNRESET: |
| 2982 | events[num_ev].events |= EPOLLHUP; |
| 2983 | break; |
| 2984 | |
| 2985 | default: |
| 2986 | events[num_ev].events |= EPOLLERR; |
| 2987 | break; |
| 2988 | } |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | if (add_event) |
| 2993 | { |
| 2994 | events[num_ev].data.u64 = session_ev_data; |
| 2995 | if (EPOLLONESHOT & session_events) |
| 2996 | { |
| 2997 | VCL_LOCK_AND_GET_SESSION (sid, &session); |
| 2998 | session->vep.ev.events = 0; |
| 2999 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 3000 | } |
| 3001 | num_ev++; |
| 3002 | if (num_ev == maxevents) |
| 3003 | { |
| 3004 | VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session); |
| 3005 | vep_session->wait_cont_idx = next_sid; |
| 3006 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 3007 | goto done; |
| 3008 | } |
| 3009 | } |
| 3010 | if (wait_cont_idx != ~0) |
| 3011 | { |
| 3012 | if (next_sid == ~0) |
| 3013 | next_sid = vep_next_sid; |
| 3014 | else if (next_sid == wait_cont_idx) |
| 3015 | next_sid = ~0; |
| 3016 | } |
| 3017 | } |
| 3018 | } |
| 3019 | while ((num_ev == 0) && (clib_time_now (&vcm->clib_time) <= timeout)); |
| 3020 | |
| 3021 | if (wait_cont_idx != ~0) |
| 3022 | { |
| 3023 | VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session); |
| 3024 | vep_session->wait_cont_idx = ~0; |
| 3025 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 3026 | } |
| 3027 | done: |
| 3028 | return (rv != VPPCOM_OK) ? rv : num_ev; |
| 3029 | } |
| 3030 | |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 3031 | int |
| 3032 | vppcom_session_attr (uint32_t session_index, uint32_t op, |
| 3033 | void *buffer, uint32_t * buflen) |
| 3034 | { |
| 3035 | vppcom_main_t *vcm = &vppcom_main; |
| 3036 | session_t *session; |
| 3037 | int rv = VPPCOM_OK; |
| 3038 | u32 *flags = buffer; |
| 3039 | vppcom_ip46_t *vcl_addr = buffer; |
| 3040 | |
| 3041 | VCL_LOCK_AND_GET_SESSION (session_index, &session); |
| 3042 | switch (op) |
| 3043 | { |
| 3044 | case VPPCOM_ATTR_GET_NREAD: |
| 3045 | rv = vppcom_session_read_ready (session, session_index); |
| 3046 | if (VPPCOM_DEBUG > 0) |
| 3047 | clib_warning ("VPPCOM_ATTR_GET_NREAD: nread = %d", rv); |
| 3048 | |
| 3049 | break; |
| 3050 | |
| 3051 | case VPPCOM_ATTR_PEEK_NREAD: |
| 3052 | /* TBD */ |
| 3053 | break; |
| 3054 | |
| 3055 | case VPPCOM_ATTR_GET_FLAGS: |
| 3056 | if (buffer && buflen && (*buflen >= sizeof (*flags))) |
| 3057 | { |
| 3058 | *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0); |
| 3059 | *buflen = sizeof (*flags); |
| 3060 | if (VPPCOM_DEBUG > 0) |
| 3061 | clib_warning ("VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, " |
| 3062 | "is_nonblocking = %u", *flags, |
| 3063 | session->is_nonblocking); |
| 3064 | } |
| 3065 | else |
| 3066 | rv = VPPCOM_EINVAL; |
| 3067 | break; |
| 3068 | |
| 3069 | case VPPCOM_ATTR_SET_FLAGS: |
| 3070 | if (buffer && buflen && (*buflen >= sizeof (*flags))) |
| 3071 | { |
| 3072 | session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0; |
| 3073 | if (VPPCOM_DEBUG > 0) |
| 3074 | clib_warning ("VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, " |
| 3075 | "is_nonblocking = %u", *flags, |
| 3076 | session->is_nonblocking); |
| 3077 | } |
| 3078 | else |
| 3079 | rv = VPPCOM_EINVAL; |
| 3080 | break; |
| 3081 | |
| 3082 | case VPPCOM_ATTR_GET_PEER_ADDR: |
| 3083 | if (buffer && buflen && (*buflen >= sizeof (*vcl_addr))) |
| 3084 | { |
| 3085 | *vcl_addr = session->peer_addr; |
| 3086 | *buflen = sizeof (*vcl_addr); |
| 3087 | if (VPPCOM_DEBUG > 0) |
| 3088 | clib_warning ("VPPCOM_ATTR_GET_PEER_ADDR: is_ip4 = %u, " |
| 3089 | "addr = %U", vcl_addr->is_ip4, format_ip46_address, |
| 3090 | &vcl_addr->ip46, vcl_addr->is_ip4); |
| 3091 | } |
| 3092 | else |
| 3093 | rv = VPPCOM_EINVAL; |
| 3094 | break; |
| 3095 | |
| 3096 | case VPPCOM_ATTR_GET_LCL_ADDR: |
| 3097 | if (buffer && buflen && (*buflen >= sizeof (*vcl_addr))) |
| 3098 | { |
| 3099 | *vcl_addr = session->lcl_addr; |
| 3100 | *buflen = sizeof (*vcl_addr); |
| 3101 | if (VPPCOM_DEBUG > 0) |
| 3102 | clib_warning ("VPPCOM_ATTR_GET_LCL_ADDR: is_ip4 = %u, " |
| 3103 | "addr = %U", vcl_addr->is_ip4, format_ip46_address, |
| 3104 | &vcl_addr->ip46, vcl_addr->is_ip4); |
| 3105 | } |
| 3106 | else |
| 3107 | rv = VPPCOM_EINVAL; |
| 3108 | break; |
| 3109 | } |
| 3110 | |
| 3111 | done: |
| 3112 | clib_spinlock_unlock (&vcm->sessions_lockp); |
| 3113 | return rv; |
| 3114 | } |
| 3115 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 3116 | /* |
| 3117 | * fd.io coding-style-patch-verification: ON |
| 3118 | * |
| 3119 | * Local Variables: |
| 3120 | * eval: (c-set-style "gnu") |
| 3121 | * End: |
| 3122 | */ |