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