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