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