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