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