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