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