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