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