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