blob: ae45be3b359da12021387e4c0d1435e9d56e6f00 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * 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 Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
Florin Coras5398dfb2021-01-25 20:31:27 -080028 u32 n_msgs = 0, sz, len;
Florin Coras30e79c22019-01-02 19:31:22 -080029
Florin Coras5398dfb2021-01-25 20:31:27 -080030 while ((sz = svm_msg_q_size (mq)))
Florin Coras30e79c22019-01-02 19:31:22 -080031 {
Florin Coras5398dfb2021-01-25 20:31:27 -080032 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 Coras30e79c22019-01-02 19:31:22 -080036 }
37 return n_msgs;
38}
39
Dave Wallace543852a2017-08-03 02:11:34 -040040
Florin Coras697faea2018-06-27 17:10:49 -070041
Florin Coras458089b2019-08-21 16:20:44 -070042static void
Florin Coras4ac25842021-04-19 17:34:54 -070043vcl_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
54static void
Florin Coras458089b2019-08-21 16:20:44 -070055vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
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 Corascc7c88e2020-09-15 15:56:51 -070065 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070066 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 Coras6a6555a2021-01-28 11:39:27 -080072 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +000073 if (s->flags & VCL_SESSION_F_CONNECTED)
74 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -070075 if (s->ext_config)
76 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -070077 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -070078 if (s->ext_config)
79 {
80 clib_mem_free (s->ext_config);
81 s->ext_config = 0;
82 }
Florin Coras458089b2019-08-21 16:20:44 -070083}
84
85static void
86vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
87{
88 app_session_evt_t _app_evt, *app_evt = &_app_evt;
89 session_connect_msg_t *mp;
90 svm_msg_q_t *mq;
91
92 mq = vcl_worker_ctrl_mq (wrk);
93 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
94 mp = (session_connect_msg_t *) app_evt->evt->data;
95 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070096 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070097 mp->context = s->session_index;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +000098 mp->dscp = s->dscp;
Florin Coras458089b2019-08-21 16:20:44 -070099 mp->wrk_index = wrk->vpp_wrk_index;
100 mp->is_ip4 = s->transport.is_ip4;
101 mp->parent_handle = s->parent_handle;
102 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700103 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700104 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000105 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700106 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800107 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000108 if (s->flags & VCL_SESSION_F_CONNECTED)
109 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700110 if (s->ext_config)
111 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700112 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700113
114 if (s->ext_config)
115 {
116 clib_mem_free (s->ext_config);
117 s->ext_config = 0;
118 }
Florin Coras458089b2019-08-21 16:20:44 -0700119}
120
121void
122vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
123{
124 app_session_evt_t _app_evt, *app_evt = &_app_evt;
125 session_unlisten_msg_t *mp;
126 svm_msg_q_t *mq;
127
128 mq = vcl_worker_ctrl_mq (wrk);
129 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
130 mp = (session_unlisten_msg_t *) app_evt->evt->data;
131 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700132 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700133 mp->wrk_index = wrk->vpp_wrk_index;
134 mp->handle = s->vpp_handle;
135 mp->context = wrk->wrk_index;
136 app_send_ctrl_evt_to_vpp (mq, app_evt);
137}
138
139static void
liuyacan534468e2021-05-09 03:50:40 +0000140vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
141{
142 app_session_evt_t _app_evt, *app_evt = &_app_evt;
143 session_shutdown_msg_t *mp;
144 svm_msg_q_t *mq;
145
146 /* Send to thread that owns the session */
147 mq = s->vpp_evt_q;
148 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
149 mp = (session_shutdown_msg_t *) app_evt->evt->data;
150 memset (mp, 0, sizeof (*mp));
151 mp->client_index = wrk->api_client_handle;
152 mp->handle = s->vpp_handle;
153 app_send_ctrl_evt_to_vpp (mq, app_evt);
154}
155
156static void
Florin Coras458089b2019-08-21 16:20:44 -0700157vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
158{
159 app_session_evt_t _app_evt, *app_evt = &_app_evt;
160 session_disconnect_msg_t *mp;
161 svm_msg_q_t *mq;
162
163 /* Send to thread that owns the session */
164 mq = s->vpp_evt_q;
165 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
166 mp = (session_disconnect_msg_t *) app_evt->evt->data;
167 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700168 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700169 mp->handle = s->vpp_handle;
170 app_send_ctrl_evt_to_vpp (mq, app_evt);
171}
172
173static void
174vcl_send_app_detach (vcl_worker_t * wrk)
175{
176 app_session_evt_t _app_evt, *app_evt = &_app_evt;
177 session_app_detach_msg_t *mp;
178 svm_msg_q_t *mq;
179
180 mq = vcl_worker_ctrl_mq (wrk);
181 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
182 mp = (session_app_detach_msg_t *) app_evt->evt->data;
183 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700184 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700185 app_send_ctrl_evt_to_vpp (mq, app_evt);
186}
Florin Coras697faea2018-06-27 17:10:49 -0700187
Florin Coras54693d22018-07-17 10:46:29 -0700188static void
189vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
190 session_handle_t handle, int retval)
191{
192 app_session_evt_t _app_evt, *app_evt = &_app_evt;
193 session_accepted_reply_msg_t *rmp;
194 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
195 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
196 rmp->handle = handle;
197 rmp->context = context;
198 rmp->retval = retval;
199 app_send_ctrl_evt_to_vpp (mq, app_evt);
200}
201
Florin Coras99368312018-08-02 10:45:44 -0700202static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800203vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
204 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700205{
206 app_session_evt_t _app_evt, *app_evt = &_app_evt;
207 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800208 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700209 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
210 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800211 rmp->handle = s->vpp_handle;
212 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700213 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800214 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700215}
216
Florin Corasc9fbd662018-08-24 12:59:56 -0700217static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800218vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
219 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700220{
221 app_session_evt_t _app_evt, *app_evt = &_app_evt;
222 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800223 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
224 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700225 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800226 rmp->handle = s->vpp_handle;
227 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700228 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800229 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700230}
231
Florin Coras30e79c22019-01-02 19:31:22 -0800232void
233vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
234 u32 wrk_index)
235{
236 app_session_evt_t _app_evt, *app_evt = &_app_evt;
237 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800238
Florin Coras52dd29f2020-11-18 19:02:17 -0800239 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
240 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800241 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700242 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800243 mp->handle = s->vpp_handle;
244 mp->req_wrk_index = wrk->vpp_wrk_index;
245 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800246 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800247}
248
hanlina3a48962020-07-13 11:09:15 +0800249int
Florin Coras40c07ce2020-07-16 20:46:17 -0700250vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
251{
252 app_session_evt_t _app_evt, *app_evt = &_app_evt;
253 session_app_wrk_rpc_msg_t *mp;
254 vcl_worker_t *dst_wrk, *wrk;
255 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800256 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700257
258 if (data_len > sizeof (mp->data))
259 goto done;
260
261 clib_spinlock_lock (&vcm->workers_lock);
262
263 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
264 if (!dst_wrk)
265 goto done;
266
267 wrk = vcl_worker_get_current ();
268 mq = vcl_worker_ctrl_mq (wrk);
269 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
270 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700271 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700272 mp->wrk_index = dst_wrk->vpp_wrk_index;
273 clib_memcpy (mp->data, data, data_len);
274 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800275 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700276
277done:
278 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800279 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700280}
281
Florin Coras04ae8272021-04-12 19:55:37 -0700282int
283vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
284 transport_endpt_attr_t *attr)
285{
286 app_session_evt_t _app_evt, *app_evt = &_app_evt;
287 session_transport_attr_msg_t *mp;
288 svm_msg_q_t *mq;
289 f64 timeout;
290
291 ASSERT (!wrk->session_attr_op);
Liangxing Wang22112772022-05-13 04:24:19 +0000292 mq = s->vpp_evt_q;
293 if (PREDICT_FALSE (!mq))
294 {
295 /* FIXME: attribute should be stored and sent once session is
296 * bound/connected to vpp */
297 return 0;
298 }
299
Florin Coras04ae8272021-04-12 19:55:37 -0700300 wrk->session_attr_op = 1;
301 wrk->session_attr_op_rv = -1;
302
Florin Coras04ae8272021-04-12 19:55:37 -0700303 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
304 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
305 memset (mp, 0, sizeof (*mp));
306 mp->client_index = wrk->api_client_handle;
307 mp->handle = s->vpp_handle;
308 mp->is_get = is_get;
309 mp->attr = *attr;
310 app_send_ctrl_evt_to_vpp (mq, app_evt);
311
312 timeout = clib_time_now (&wrk->clib_time) + 1;
313
314 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
315 vcl_flush_mq_events ();
316
317 if (!wrk->session_attr_op_rv && is_get)
318 *attr = wrk->session_attr_rv;
319
320 wrk->session_attr_op = 0;
321
322 return wrk->session_attr_op_rv;
323}
324
Florin Coras54693d22018-07-17 10:46:29 -0700325static u32
Florin Coras00cca802019-06-06 09:38:44 -0700326vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
327 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700328{
329 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700330 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700331
Florin Coras134a9962018-08-28 11:32:04 -0700332 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700333
Florin Coras00cca802019-06-06 09:38:44 -0700334 listen_session = vcl_session_get (wrk, ls_index);
335 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700336 {
Florin Coras00cca802019-06-06 09:38:44 -0700337 VDBG (0, "ERROR: listener handle %lu does not match session %u",
338 mp->listener_handle, ls_index);
339 goto error;
340 }
341
Florin Corasf6e284b2021-07-21 18:17:20 -0700342 if (vcl_segment_attach_session (
343 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
344 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700345 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800346 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
347 session->session_index, mp->handle);
Florin Coras00cca802019-06-06 09:38:44 -0700348 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700349 }
350
Florin Coras54693d22018-07-17 10:46:29 -0700351 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700352 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700353 session->transport.rmt_port = mp->rmt.port;
354 session->transport.is_ip4 = mp->rmt.is_ip4;
355 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500356 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700357
Florin Coras134a9962018-08-28 11:32:04 -0700358 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800359 session->transport.lcl_port = mp->lcl.port;
360 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700361 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200362 session->is_dgram = vcl_proto_is_dgram (session->session_type);
363 session->listener_index = listen_session->session_index;
364 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700365
Florin Coras54693d22018-07-17 10:46:29 -0700366 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
367
Florin Coras00cca802019-06-06 09:38:44 -0700368 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
369 session->vpp_handle, 0);
370
Florin Coras134a9962018-08-28 11:32:04 -0700371 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700372
373error:
Florin Corasb4624182020-12-11 13:58:12 -0800374 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
375 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700376 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
377 VNET_API_ERROR_INVALID_ARGUMENT);
378 vcl_session_free (wrk, session);
379 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700380}
381
382static u32
Florin Coras134a9962018-08-28 11:32:04 -0700383vcl_session_connected_handler (vcl_worker_t * wrk,
384 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700385{
Florin Coras99368312018-08-02 10:45:44 -0700386 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800387 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700388
389 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700390 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800391 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700392 {
Florin Coras8d42c752021-11-29 23:26:19 -0800393 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
394 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800395 /* Should not happen but if it does, force vpp session cleanup */
396 vcl_session_t tmp_session = {
397 .vpp_handle = mp->handle,
398 .vpp_evt_q = 0,
399 };
400 vcl_segment_attach_session (
401 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
402 mp->vpp_event_queue_address, mp->mq_index, 0, session);
403 if (tmp_session.vpp_evt_q)
404 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700405 return VCL_INVALID_SESSION_INDEX;
406 }
Florin Coras8d42c752021-11-29 23:26:19 -0800407
Florin Coras54693d22018-07-17 10:46:29 -0700408 if (mp->retval)
409 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800410 VDBG (0, "session %u: connect failed! %U", session_index,
Florin Coras8d42c752021-11-29 23:26:19 -0800411 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700412 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800413 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +0530414 session->vpp_error = mp->retval;
Florin Coras070453d2018-08-24 17:04:27 -0700415 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700416 }
417
Florin Corasdbc9c592019-09-25 16:37:43 -0700418 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800419
Florin Coras8d42c752021-11-29 23:26:19 -0800420 /* Add to lookup table. Even if something fails, session cannot be
421 * cleaned up prior to notifying vpp and going through the cleanup
422 * "procedure" see @ref vcl_session_cleanup_handler */
423 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
424
Florin Corasf6e284b2021-07-21 18:17:20 -0700425 if (vcl_segment_attach_session (
426 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
427 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800428 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800429 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
430 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800431 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700432 vcl_send_session_disconnect (wrk, session);
433 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800434 }
435
Florin Coras653e43f2019-03-04 10:56:23 -0800436 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700437 {
Florin Corasc547e912020-12-08 17:50:45 -0800438 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700439 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
440 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800441 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800442 VDBG (0, "session %u [0x%llx]: failed to attach ct fifos",
443 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800444 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700445 vcl_send_session_disconnect (wrk, session);
446 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800447 }
Florin Coras99368312018-08-02 10:45:44 -0700448 }
Florin Coras54693d22018-07-17 10:46:29 -0700449
Florin Coras09d18c22019-04-24 11:10:02 -0700450 session->transport.is_ip4 = mp->lcl.is_ip4;
451 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500452 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700453 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700454
455 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700456 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700457 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700458 vcl_send_session_disconnect (wrk, session);
459 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700460 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700461
Florin Coras5ffb9642021-12-03 17:24:13 -0800462 VDBG (0, "session %u [0x%llx] connected local: %U:%u remote %U:%u",
463 session->session_index, session->vpp_handle, vcl_format_ip46_address,
464 &session->transport.lcl_ip,
465 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
466 clib_net_to_host_u16 (session->transport.lcl_port),
467 vcl_format_ip46_address, &session->transport.rmt_ip,
468 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
469 clib_net_to_host_u16 (session->transport.rmt_port));
Florin Coras070453d2018-08-24 17:04:27 -0700470
Florin Coras54693d22018-07-17 10:46:29 -0700471 return session_index;
472}
473
Florin Coras3c7d4f92018-12-14 11:28:43 -0800474static int
475vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
476{
477 vcl_session_msg_t *accepted_msg;
478 int i;
479
480 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
481 {
482 accepted_msg = &session->accept_evts_fifo[i];
483 if (accepted_msg->accepted_msg.handle == handle)
484 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800485 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800486 return 1;
487 }
488 }
489 return 0;
490}
491
Florin Corasc9fbd662018-08-24 12:59:56 -0700492static u32
Florin Coras134a9962018-08-28 11:32:04 -0700493vcl_session_reset_handler (vcl_worker_t * wrk,
494 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700495{
496 vcl_session_t *session;
497 u32 sid;
498
Florin Coras134a9962018-08-28 11:32:04 -0700499 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
500 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700501 if (!session)
502 {
503 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
504 return VCL_INVALID_SESSION_INDEX;
505 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800506
507 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700508 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800509 {
510
511 if (!vcl_flag_accepted_session (session, reset_msg->handle,
512 VCL_ACCEPTED_F_RESET))
513 VDBG (0, "session was not accepted!");
514 return VCL_INVALID_SESSION_INDEX;
515 }
516
Florin Corasc127d5a2020-10-14 16:35:58 -0700517 if (session->session_state != VCL_STATE_CLOSED)
518 session->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +0800519
520 session->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Coras5ffb9642021-12-03 17:24:13 -0800521 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700522 return sid;
523}
524
Florin Coras60116992018-08-27 09:52:18 -0700525static u32
Florin Coras134a9962018-08-28 11:32:04 -0700526vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700527{
528 vcl_session_t *session;
529 u32 sid = mp->context;
530
Florin Coras134a9962018-08-28 11:32:04 -0700531 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700532 if (mp->retval)
533 {
Florin Coras5e062572019-03-14 19:07:51 -0700534 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700535 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700536 if (session)
537 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700538 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700539 session->vpp_handle = mp->handle;
540 return sid;
541 }
542 else
543 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800544 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
545 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700546 return VCL_INVALID_SESSION_INDEX;
547 }
548 }
549
550 session->vpp_handle = mp->handle;
551 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500552 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
553 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700554 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700555 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700556 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800557
Florin Coras6e3c1f82020-01-15 01:30:46 +0000558 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700559 {
Florin Corasc547e912020-12-08 17:50:45 -0800560 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700561 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
562 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800563 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800564 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
565 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800566 session->session_state = VCL_STATE_DETACHED;
567 return VCL_INVALID_SESSION_INDEX;
568 }
Florin Coras60116992018-08-27 09:52:18 -0700569 }
570
Florin Coras05ecfcc2018-12-12 18:19:39 -0800571 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700572 return sid;
573}
574
Florin Corasdfae9f92019-02-20 19:48:31 -0800575static void
576vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
577{
578 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
579 vcl_session_t *s;
580
581 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000582 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800583 {
584 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
585 return;
586 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700587 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000588 {
589 /* Connected udp listener */
590 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700591 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000592 return;
593
594 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
595 return;
596 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800597
598 if (mp->retval)
599 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700600 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800601
602 if (mp->context != wrk->wrk_index)
603 VDBG (0, "wrong context");
604
605 vcl_session_table_del_vpp_handle (wrk, mp->handle);
606 vcl_session_free (wrk, s);
607}
608
Florin Coras68b7e582020-01-21 18:33:23 -0800609static void
610vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
611{
612 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
613 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800614 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800615
616 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
617 if (!s)
618 {
619 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
620 return;
621 }
622
Florin Coras1bb74942021-02-10 15:26:37 -0800623 /* Only validate if a value is provided */
624 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800625 {
Florin Coras1bb74942021-02-10 15:26:37 -0800626 fs_index = vcl_segment_table_lookup (mp->segment_handle);
627 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
628 {
629 VDBG (0, "segment %lx for session %u is not mounted!",
630 mp->segment_handle, s->session_index);
631 s->session_state = VCL_STATE_DETACHED;
632 return;
633 }
Florin Corasc547e912020-12-08 17:50:45 -0800634 }
635
Florin Coras57660d92020-04-04 22:45:34 +0000636 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800637
638 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
639 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800640
Florin Coras68b7e582020-01-21 18:33:23 -0800641 vcl_session_table_del_vpp_handle (wrk, mp->handle);
642 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
643
644 /* Generate new tx event if we have outstanding data */
645 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800646 app_send_io_evt_to_vpp (s->vpp_evt_q,
647 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800648 SESSION_IO_EVT_TX, SVM_Q_WAIT);
649
Florin Coras57660d92020-04-04 22:45:34 +0000650 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800651 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800652}
653
Florin Coras3c7d4f92018-12-14 11:28:43 -0800654static vcl_session_t *
655vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
656{
657 vcl_session_msg_t *vcl_msg;
658 vcl_session_t *session;
659
660 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
661 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800662 VWRN ("session overlap handle %lu state %u!", msg->handle,
663 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800664
665 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
666 if (!session)
667 {
668 VERR ("couldn't find listen session: listener handle %llx",
669 msg->listener_handle);
670 return 0;
671 }
672
673 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000674 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800675 vcl_msg->accepted_msg = *msg;
676 /* Session handle points to listener until fully accepted by app */
677 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
678
679 return session;
680}
681
682static vcl_session_t *
683vcl_session_disconnected_handler (vcl_worker_t * wrk,
684 session_disconnected_msg_t * msg)
685{
686 vcl_session_t *session;
687
688 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
689 if (!session)
690 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800691 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800692 return 0;
693 }
694
Florin Corasadcfb152020-02-12 08:50:29 +0000695 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700696 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000697 return 0;
698
Florin Coras3c7d4f92018-12-14 11:28:43 -0800699 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700700 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800701 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800702 if (!vcl_flag_accepted_session (session, msg->handle,
703 VCL_ACCEPTED_F_CLOSED))
704 VDBG (0, "session was not accepted!");
705 return 0;
706 }
707
Florin Corasadcfb152020-02-12 08:50:29 +0000708 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700709 if (session->session_state != VCL_STATE_DISCONNECT)
710 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000711
Florin Coras3c7d4f92018-12-14 11:28:43 -0800712 return session;
713}
714
liuyacan534468e2021-05-09 03:50:40 +0000715int
liuyacan55c952e2021-06-13 14:54:55 +0800716vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000717{
718 vcl_worker_t *wrk = vcl_worker_get_current ();
719 vcl_session_t *session;
720 vcl_session_state_t state;
721 u64 vpp_handle;
722
723 session = vcl_session_get_w_handle (wrk, session_handle);
724 if (PREDICT_FALSE (!session))
725 return VPPCOM_EBADFD;
726
727 vpp_handle = session->vpp_handle;
728 state = session->session_state;
729
730 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800731 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000732
733 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
734 {
735 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
736 return VPPCOM_EBADFD;
737 }
738
liuyacan55c952e2021-06-13 14:54:55 +0800739 if (how == SHUT_RD || how == SHUT_RDWR)
740 {
741 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
742 if (how == SHUT_RD)
743 return VPPCOM_OK;
744 }
745 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
746
liuyacan534468e2021-05-09 03:50:40 +0000747 if (PREDICT_TRUE (state == VCL_STATE_READY))
748 {
749 VDBG (1, "session %u [0x%llx]: sending shutdown...",
750 session->session_index, vpp_handle);
751
752 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000753 }
754
755 return VPPCOM_OK;
756}
757
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700758static int
759vppcom_session_disconnect (u32 session_handle)
760{
761 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700762 vcl_session_t *session, *listen_session;
763 vcl_session_state_t state;
764 u64 vpp_handle;
765
766 session = vcl_session_get_w_handle (wrk, session_handle);
767 if (!session)
768 return VPPCOM_EBADFD;
769
770 vpp_handle = session->vpp_handle;
771 state = session->session_state;
772
Florin Coras5ffb9642021-12-03 17:24:13 -0800773 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
774 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700775
776 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
777 {
778 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
779 return VPPCOM_EBADFD;
780 }
781
782 if (state == VCL_STATE_VPP_CLOSING)
783 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800784 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700785 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
786 session->session_index, vpp_handle);
787 }
788 else
789 {
790 /* Session doesn't have an event queue yet. Probably a non-blocking
791 * connect. Wait for the reply */
792 if (PREDICT_FALSE (!session->vpp_evt_q))
793 return VPPCOM_OK;
794
Florin Coras5ffb9642021-12-03 17:24:13 -0800795 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700796 session->session_index, vpp_handle);
797 vcl_send_session_disconnect (wrk, session);
798 }
799
800 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
801 {
802 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700803 if (listen_session)
804 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700805 }
806
807 return VPPCOM_OK;
808}
809
Florin Coras30e79c22019-01-02 19:31:22 -0800810static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700811vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
812{
813 session_cleanup_msg_t *msg;
814 vcl_session_t *session;
815
816 msg = (session_cleanup_msg_t *) data;
817 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
818 if (!session)
819 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800820 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700821 return;
822 }
823
Florin Coras36d49392020-04-24 23:00:11 +0000824 if (msg->type == SESSION_CLEANUP_TRANSPORT)
825 {
826 /* Transport was cleaned up before we confirmed close. Probably the
827 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700828 * Confirm close to make sure everything is cleaned up.
829 * Move to undetermined state to ensure that the session is not
830 * removed before both vpp and the app cleanup.
831 * - If the app closes first, the session is moved to CLOSED state
832 * and the session cleanup notification from vpp removes the
833 * session.
834 * - If vpp cleans up the session first, the session is moved to
835 * DETACHED state lower and subsequently the close from the app
836 * frees the session
837 */
838 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700839 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700840 vppcom_session_disconnect (vcl_session_handle (session));
841 session->session_state = VCL_STATE_UPDATED;
842 }
843 else if (session->session_state == VCL_STATE_DISCONNECT)
844 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800845 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700846 session->session_state = VCL_STATE_UPDATED;
847 }
Florin Coras36d49392020-04-24 23:00:11 +0000848 return;
849 }
850
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800851 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700852 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800853
854 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700855 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000856 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800857 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700858 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000859 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
860 return;
861 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800862
863 /* Session probably tracked with epoll, disconnect not yet handled and
864 * 1) both transport and session cleanup completed 2) app closed. Wait
865 * until message is drained to free the session.
866 * See @ref vcl_handle_mq_event */
867 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
868 {
869 session->flags |= VCL_SESSION_F_PENDING_FREE;
870 return;
871 }
872
Florin Coras9ace36d2019-10-28 13:14:17 -0700873 vcl_session_free (wrk, session);
874}
875
876static void
Florin Coras30e79c22019-01-02 19:31:22 -0800877vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
878{
879 session_req_worker_update_msg_t *msg;
880 vcl_session_t *s;
881
882 msg = (session_req_worker_update_msg_t *) data;
883 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
884 if (!s)
885 return;
886
887 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
888}
889
890static void
891vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
892{
893 session_worker_update_reply_msg_t *msg;
894 vcl_session_t *s;
895
896 msg = (session_worker_update_reply_msg_t *) data;
897 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
898 if (!s)
899 {
900 VDBG (0, "unknown handle 0x%llx", msg->handle);
901 return;
902 }
Florin Coras30e79c22019-01-02 19:31:22 -0800903
Florin Coras5f45e012019-01-23 09:21:30 -0800904 if (s->rx_fifo)
905 {
Florin Corasc547e912020-12-08 17:50:45 -0800906 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700907 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800908 {
909 VDBG (0, "failed to attach fifos for %u", s->session_index);
910 return;
911 }
Florin Coras5f45e012019-01-23 09:21:30 -0800912 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700913 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800914
Florin Coras30e79c22019-01-02 19:31:22 -0800915 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
916 s->vpp_handle, wrk->wrk_index);
917}
918
Florin Coras935ce752020-09-08 22:43:47 -0700919static int
920vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
921{
922
923 if (vcm->cfg.vpp_app_socket_api)
924 return vcl_sapi_recv_fds (wrk, fds, n_fds);
925
926 return vcl_bapi_recv_fds (wrk, fds, n_fds);
927}
928
Florin Corasc4c4cf52019-08-24 18:17:34 -0700929static void
930vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
931{
932 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
933 session_app_add_segment_msg_t *msg;
934 u64 segment_handle;
935 int fd = -1;
936
937 msg = (session_app_add_segment_msg_t *) data;
938
939 if (msg->fd_flags)
940 {
Florin Coras935ce752020-09-08 22:43:47 -0700941 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700942 seg_type = SSVM_SEGMENT_MEMFD;
943 }
944
945 segment_handle = msg->segment_handle;
946 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
947 {
948 clib_warning ("invalid segment handle");
949 return;
950 }
951
952 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
953 seg_type, fd))
954 {
955 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
956 return;
957 }
958
959 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
960 msg->segment_size);
961}
962
963static void
964vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
965{
966 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
967 vcl_segment_detach (msg->segment_handle);
968 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
969}
970
Florin Coras40c07ce2020-07-16 20:46:17 -0700971static void
972vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
973{
974 if (!vcm->wrk_rpc_fn)
975 return;
976
hanlina3a48962020-07-13 11:09:15 +0800977 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700978}
979
Florin Coras04ae8272021-04-12 19:55:37 -0700980static void
981vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
982{
983 session_transport_attr_reply_msg_t *mp;
984
985 if (!wrk->session_attr_op)
986 return;
987
988 mp = (session_transport_attr_reply_msg_t *) data;
989
990 wrk->session_attr_op_rv = mp->retval;
991 wrk->session_attr_op = 0;
992 wrk->session_attr_rv = mp->attr;
993}
994
Florin Coras86f04502018-09-12 16:08:01 -0700995static int
996vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700997{
Florin Coras54693d22018-07-17 10:46:29 -0700998 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700999 session_connected_msg_t *connected_msg;
1000 session_reset_msg_t *reset_msg;
1001 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001002 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001003 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001004
1005 switch (e->event_type)
1006 {
Florin Coras653e43f2019-03-04 10:56:23 -08001007 case SESSION_IO_EVT_RX:
1008 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001009 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001010 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001011 break;
Florin Coras86f04502018-09-12 16:08:01 -07001012 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001013 break;
Florin Corasb242d312020-10-26 15:35:40 -07001014 case SESSION_CTRL_EVT_BOUND:
1015 /* We can only wait for only one listen so not postponed */
1016 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1017 break;
Florin Coras54693d22018-07-17 10:46:29 -07001018 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001019 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1020 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1021 {
1022 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1023 *ecpy = *e;
1024 ecpy->postponed = 1;
1025 ecpy->session_index = s->session_index;
1026 }
Florin Coras54693d22018-07-17 10:46:29 -07001027 break;
1028 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001029 connected_msg = (session_connected_msg_t *) e->data;
1030 sid = vcl_session_connected_handler (wrk, connected_msg);
1031 if (!(s = vcl_session_get (wrk, sid)))
1032 break;
1033 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1034 {
1035 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1036 *ecpy = *e;
1037 ecpy->postponed = 1;
1038 ecpy->session_index = s->session_index;
1039 }
Florin Coras54693d22018-07-17 10:46:29 -07001040 break;
1041 case SESSION_CTRL_EVT_DISCONNECTED:
1042 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001043 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1044 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001045 if (s->session_state == VCL_STATE_CLOSED)
1046 break;
Florin Corasb242d312020-10-26 15:35:40 -07001047 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1048 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001049 s->session_state = VCL_STATE_VPP_CLOSING;
1050 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1051 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1052 *ecpy = *e;
1053 ecpy->postponed = 1;
1054 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001055 break;
1056 }
1057 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001058 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001059 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1060 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001061 break;
1062 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001063 reset_msg = (session_reset_msg_t *) e->data;
1064 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1065 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001066 if (s->session_state == VCL_STATE_CLOSED)
1067 break;
Florin Corasb242d312020-10-26 15:35:40 -07001068 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1069 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001070 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1071 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001072 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001073 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1074 *ecpy = *e;
1075 ecpy->postponed = 1;
1076 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001077 break;
1078 }
Florin Coras134a9962018-08-28 11:32:04 -07001079 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001080 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001081 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1082 vcl_session_unlisten_reply_handler (wrk, e->data);
1083 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001084 case SESSION_CTRL_EVT_MIGRATED:
1085 vcl_session_migrated_handler (wrk, e->data);
1086 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001087 case SESSION_CTRL_EVT_CLEANUP:
1088 vcl_session_cleanup_handler (wrk, e->data);
1089 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001090 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1091 vcl_session_req_worker_update_handler (wrk, e->data);
1092 break;
1093 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1094 vcl_session_worker_update_reply_handler (wrk, e->data);
1095 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001096 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1097 vcl_session_app_add_segment_handler (wrk, e->data);
1098 break;
1099 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1100 vcl_session_app_del_segment_handler (wrk, e->data);
1101 break;
hanlina3a48962020-07-13 11:09:15 +08001102 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001103 vcl_worker_rpc_handler (wrk, e->data);
1104 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001105 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1106 vcl_session_transport_attr_reply_handler (wrk, e->data);
1107 break;
Florin Coras54693d22018-07-17 10:46:29 -07001108 default:
1109 clib_warning ("unhandled %u", e->event_type);
1110 }
1111 return VPPCOM_OK;
1112}
1113
Florin Coras30e79c22019-01-02 19:31:22 -08001114static int
Florin Coras697faea2018-06-27 17:10:49 -07001115vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001116 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001117 f64 wait_for_time)
1118{
Florin Coras134a9962018-08-28 11:32:04 -07001119 vcl_worker_t *wrk = vcl_worker_get_current ();
1120 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001121 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001122 svm_msg_q_msg_t msg;
1123 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001124
Florin Coras697faea2018-06-27 17:10:49 -07001125 do
Dave Wallace543852a2017-08-03 02:11:34 -04001126 {
Florin Coras134a9962018-08-28 11:32:04 -07001127 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001128 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001129 {
Florin Coras070453d2018-08-24 17:04:27 -07001130 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001131 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001132 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001133 {
Florin Coras697faea2018-06-27 17:10:49 -07001134 return VPPCOM_OK;
1135 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001136 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001137 {
Florin Coras697faea2018-06-27 17:10:49 -07001138 return VPPCOM_ECONNREFUSED;
1139 }
Florin Coras54693d22018-07-17 10:46:29 -07001140
Florin Coras134a9962018-08-28 11:32:04 -07001141 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001142 {
1143 usleep (100);
1144 continue;
1145 }
Florin Coras134a9962018-08-28 11:32:04 -07001146 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001147 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001148 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001149 }
Florin Coras134a9962018-08-28 11:32:04 -07001150 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001151
Florin Coras05ecfcc2018-12-12 18:19:39 -08001152 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001153 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001154 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001155
Florin Coras697faea2018-06-27 17:10:49 -07001156 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001157}
1158
Florin Coras30e79c22019-01-02 19:31:22 -08001159static void
1160vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1161{
Florin Coras288eaab2019-02-03 15:26:14 -08001162 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001163 vcl_session_t *s;
1164 u32 *sip;
1165
1166 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1167 return;
1168
1169 vec_foreach (sip, wrk->pending_session_wrk_updates)
1170 {
1171 s = vcl_session_get (wrk, *sip);
1172 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1173 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001174 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1175 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001176 s->session_state = state;
1177 }
1178 vec_reset_length (wrk->pending_session_wrk_updates);
1179}
1180
Florin Corasf9240dc2019-01-15 08:03:17 -08001181void
Florin Coras5398dfb2021-01-25 20:31:27 -08001182vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001183{
Florin Coras30e79c22019-01-02 19:31:22 -08001184 svm_msg_q_msg_t *msg;
1185 session_event_t *e;
1186 svm_msg_q_t *mq;
1187 int i;
1188
1189 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001190 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001191
1192 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1193 {
1194 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1195 e = svm_msg_q_msg_data (mq, msg);
1196 vcl_handle_mq_event (wrk, e);
1197 svm_msg_q_free_msg (mq, msg);
1198 }
1199 vec_reset_length (wrk->mq_msg_vector);
1200 vcl_handle_pending_wrk_updates (wrk);
1201}
1202
Florin Coras5398dfb2021-01-25 20:31:27 -08001203void
1204vcl_flush_mq_events (void)
1205{
1206 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1207}
1208
Florin Coras697faea2018-06-27 17:10:49 -07001209static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001210vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001211{
Florin Coras134a9962018-08-28 11:32:04 -07001212 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001213 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001214 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001215 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001216
Florin Corasab2f6db2018-08-31 14:31:41 -07001217 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001218 if (!session)
1219 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001220
Florin Corasaaff5ee2019-07-08 13:00:00 -07001221 /* Flush pending accept events, if any */
1222 while (clib_fifo_elts (session->accept_evts_fifo))
1223 {
1224 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1225 accepted_msg = &evt->accepted_msg;
1226 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1227 vcl_send_session_accepted_reply (session->vpp_evt_q,
1228 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001229 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001230 }
1231 clib_fifo_free (session->accept_evts_fifo);
1232
Florin Coras458089b2019-08-21 16:20:44 -07001233 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001234
Florin Coras5e062572019-03-14 19:07:51 -07001235 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001236 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001237 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001238
1239 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001240 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001241
Florin Coras070453d2018-08-24 17:04:27 -07001242 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001243}
1244
Florin Coras940f78f2018-11-30 12:11:20 -08001245/**
1246 * Handle app exit
1247 *
1248 * Notify vpp of the disconnect and mark the worker as free. If we're the
1249 * last worker, do a full cleanup otherwise, since we're probably a forked
1250 * child, avoid syscalls as much as possible. We might've lost privileges.
1251 */
1252void
1253vppcom_app_exit (void)
1254{
1255 if (!pool_elts (vcm->workers))
1256 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001257 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1258 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001259 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001260}
1261
Florin Coras935ce752020-09-08 22:43:47 -07001262static int
1263vcl_api_attach (void)
1264{
1265 if (vcm->cfg.vpp_app_socket_api)
1266 return vcl_sapi_attach ();
1267
1268 return vcl_bapi_attach ();
1269}
1270
1271static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001272vcl_api_retry_attach (vcl_worker_t *wrk)
1273{
1274 vcl_session_t *s;
1275
1276 if (vcl_api_attach ())
1277 return;
1278
1279 /* Treat listeners as configuration that needs to be re-added to vpp */
1280 pool_foreach (s, wrk->sessions)
1281 {
1282 if (s->flags & VCL_SESSION_F_IS_VEP)
1283 continue;
1284 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1285 vppcom_session_listen (vcl_session_handle (s), 10);
1286 else
1287 VDBG (0, "internal error: unexpected state %d", s->session_state);
1288 }
1289}
1290
1291static void
1292vcl_api_handle_disconnect (vcl_worker_t *wrk)
1293{
1294 wrk->api_client_handle = ~0;
1295 vcl_worker_detach_sessions (wrk);
1296}
1297
1298static void
Florin Coras935ce752020-09-08 22:43:47 -07001299vcl_api_detach (vcl_worker_t * wrk)
1300{
Florin Corasd04ea442022-03-30 16:08:25 -07001301 if (wrk->api_client_handle == ~0)
1302 return;
1303
Florin Coras935ce752020-09-08 22:43:47 -07001304 vcl_send_app_detach (wrk);
1305
1306 if (vcm->cfg.vpp_app_socket_api)
1307 return vcl_sapi_detach (wrk);
1308
1309 return vcl_bapi_disconnect_from_vpp ();
1310}
1311
Dave Wallace543852a2017-08-03 02:11:34 -04001312/*
1313 * VPPCOM Public API functions
1314 */
1315int
Florin Coras66ec4672020-06-15 07:59:40 -07001316vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001317{
Dave Wallace543852a2017-08-03 02:11:34 -04001318 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001319 int rv;
1320
Florin Coras47c40e22018-11-26 17:01:36 -08001321 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001322 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001323 VDBG (1, "already initialized");
1324 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001325 }
1326
Florin Coras47c40e22018-11-26 17:01:36 -08001327 vcm->is_init = 1;
1328 vppcom_cfg (&vcm->cfg);
1329 vcl_cfg = &vcm->cfg;
1330
1331 vcm->main_cpu = pthread_self ();
1332 vcm->main_pid = getpid ();
1333 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001334 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1335 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001336 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1337 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001338 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001339 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001340 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001341
1342 /* Allocate default worker */
1343 vcl_worker_alloc_and_init ();
1344
Florin Coras935ce752020-09-08 22:43:47 -07001345 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001346 {
1347 vppcom_app_destroy ();
1348 return rv;
1349 }
Florin Coras47c40e22018-11-26 17:01:36 -08001350
1351 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001352 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001353
1354 return VPPCOM_OK;
1355}
1356
1357void
1358vppcom_app_destroy (void)
1359{
Florin Corasdc0ded72020-04-30 02:59:55 +00001360 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001361 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001362
Florin Coras940f78f2018-11-30 12:11:20 -08001363 if (!pool_elts (vcm->workers))
1364 return;
1365
Florin Coras0d427d82018-06-27 03:24:07 -07001366 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001367
Florin Corasdc0ded72020-04-30 02:59:55 +00001368 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001369
Florin Corasce815de2020-04-16 18:47:27 +00001370 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001371 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001372 if (current_wrk != wrk)
1373 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001374 }
Florin Corasce815de2020-04-16 18:47:27 +00001375 /* *INDENT-ON* */
1376
Florin Coras935ce752020-09-08 22:43:47 -07001377 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001378 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001379 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001380
Florin Coras0d427d82018-06-27 03:24:07 -07001381 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001382
1383 /*
1384 * Free the heap and fix vcm
1385 */
1386 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001387 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001388
1389 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001390 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001391}
1392
1393int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001394vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001395{
Florin Coras134a9962018-08-28 11:32:04 -07001396 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001397 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001398
Florin Coras134a9962018-08-28 11:32:04 -07001399 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001400
Florin Coras7e12d942018-06-27 14:32:43 -07001401 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001402 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001403 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001404 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301405 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001406
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001407 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001408 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001409
Florin Coras7e12d942018-06-27 14:32:43 -07001410 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1411 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001412
Florin Coras5e062572019-03-14 19:07:51 -07001413 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001414
Florin Coras134a9962018-08-28 11:32:04 -07001415 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001416}
1417
Florin Corasfe286f72021-06-04 10:07:55 -07001418static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001419vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001420{
Florin Corasfa3884f2021-06-22 20:04:46 -07001421 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001422
Florin Corasfa3884f2021-06-22 20:04:46 -07001423 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001424 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001425 wrk->ep_lt_current = s->session_index;
1426 s->vep.lt_next = s->session_index;
1427 s->vep.lt_prev = s->session_index;
1428 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001429 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001430
1431 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1432 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1433
1434 prev->vep.lt_next = s->session_index;
1435 s->vep.lt_prev = prev->session_index;
1436
1437 s->vep.lt_next = cur->session_index;
1438 cur->vep.lt_prev = s->session_index;
1439}
1440
1441static void
1442vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1443{
1444 vcl_session_t *prev, *next;
1445
1446 if (s->vep.lt_next == s->session_index)
1447 {
1448 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1449 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1450 return;
1451 }
1452
1453 prev = vcl_session_get (wrk, s->vep.lt_prev);
1454 next = vcl_session_get (wrk, s->vep.lt_next);
1455
1456 prev->vep.lt_next = next->session_index;
1457 next->vep.lt_prev = prev->session_index;
1458
1459 if (s->session_index == wrk->ep_lt_current)
1460 wrk->ep_lt_current = s->vep.lt_next;
1461
1462 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001463}
1464
Dave Wallace543852a2017-08-03 02:11:34 -04001465int
Florin Corasc127d5a2020-10-14 16:35:58 -07001466vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001467 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001468{
Florin Coras134a9962018-08-28 11:32:04 -07001469 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001470
Florin Coras6c3b2182020-10-19 18:36:48 -07001471 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001472
Florin Coras6c3b2182020-10-19 18:36:48 -07001473 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001474 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001475 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001476 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001477 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001478 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001479 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001480 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001481 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1482 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001483 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001484 }
Florin Corase4306b62020-10-28 12:51:10 -07001485 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001486 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001487
Florin Coras6c3b2182020-10-19 18:36:48 -07001488 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001489 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001490 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001491 if (rv < 0)
1492 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001493 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1494 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001495 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001496
Florin Coras9ace36d2019-10-28 13:14:17 -07001497 if (!do_disconnect)
1498 {
1499 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001500 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001501 goto cleanup;
1502 }
1503
Florin Coras6c3b2182020-10-19 18:36:48 -07001504 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001505 {
1506 rv = vppcom_session_unbind (sh);
1507 if (PREDICT_FALSE (rv < 0))
1508 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001509 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001510 vppcom_retval_str (rv));
1511 return rv;
1512 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001513 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001514 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001515 {
1516 rv = vppcom_session_disconnect (sh);
1517 if (PREDICT_FALSE (rv < 0))
1518 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001519 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001520 rv, vppcom_retval_str (rv));
1521 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001522 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001523 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001524 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001525 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001526 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001527 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001528 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001529
1530 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1531 goto free_session;
1532
1533 /* Disconnect/reset messages pending but vpp transport and session
1534 * cleanups already done. Free only after messages drained. */
1535 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001536 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001537
Florin Corasc127d5a2020-10-14 16:35:58 -07001538 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001539
Florin Coras9ace36d2019-10-28 13:14:17 -07001540 /* Session is removed only after vpp confirms the disconnect */
1541 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001542
Florin Corasf9240dc2019-01-15 08:03:17 -08001543cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001544 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001545free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001546 vcl_session_free (wrk, s);
1547 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001548
Dave Wallace543852a2017-08-03 02:11:34 -04001549 return rv;
1550}
1551
1552int
Florin Corasf9240dc2019-01-15 08:03:17 -08001553vppcom_session_close (uint32_t session_handle)
1554{
1555 vcl_worker_t *wrk = vcl_worker_get_current ();
1556 vcl_session_t *session;
1557
1558 session = vcl_session_get_w_handle (wrk, session_handle);
1559 if (!session)
1560 return VPPCOM_EBADFD;
1561 return vcl_session_cleanup (wrk, session, session_handle,
1562 1 /* do_disconnect */ );
1563}
1564
1565int
Florin Coras134a9962018-08-28 11:32:04 -07001566vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001567{
Florin Coras134a9962018-08-28 11:32:04 -07001568 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001569 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001570
1571 if (!ep || !ep->ip)
1572 return VPPCOM_EINVAL;
1573
Florin Coras134a9962018-08-28 11:32:04 -07001574 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001575 if (!session)
1576 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001577
Florin Coras6c3b2182020-10-19 18:36:48 -07001578 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001579 {
Florin Coras5e062572019-03-14 19:07:51 -07001580 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1581 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001582 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001583 }
1584
Florin Coras7e12d942018-06-27 14:32:43 -07001585 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001586 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001587 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1588 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001589 else
Dave Barach178cf492018-11-13 16:34:13 -05001590 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1591 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001592 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001593
Florin Coras60687192021-11-29 21:00:47 -08001594 VDBG (0,
1595 "session %u handle %u: binding to local %s address %U port %u, "
1596 "proto %s",
1597 session->session_index, session_handle,
1598 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1599 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001600 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1601 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001602 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001603 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001604
1605 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001606 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001607
Florin Coras070453d2018-08-24 17:04:27 -07001608 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001609}
1610
1611int
Florin Coras134a9962018-08-28 11:32:04 -07001612vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001613{
Florin Coras134a9962018-08-28 11:32:04 -07001614 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001615 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001616 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001617 int rv;
1618
Florin Coras134a9962018-08-28 11:32:04 -07001619 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001620 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001621 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001622
Dave Wallaceee45d412017-11-24 21:44:06 -05001623 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001624 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001625 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001626 VDBG (0, "session %u [0x%llx]: already in listen state!",
1627 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001628 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001629 }
1630
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001631 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001632
Florin Coras070453d2018-08-24 17:04:27 -07001633 /*
1634 * Send listen request to vpp and wait for reply
1635 */
Florin Coras458089b2019-08-21 16:20:44 -07001636 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001637 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001638 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001639 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001640
Florin Coras070453d2018-08-24 17:04:27 -07001641 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001642 {
Florin Coras134a9962018-08-28 11:32:04 -07001643 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001644 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1645 listen_sh, listen_session->vpp_handle, rv,
1646 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001647 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001648 }
1649
Florin Coras070453d2018-08-24 17:04:27 -07001650 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001651}
1652
Florin Coras134a9962018-08-28 11:32:04 -07001653static int
Florin Coras5e062572019-03-14 19:07:51 -07001654validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001655{
Florin Coras6c3b2182020-10-19 18:36:48 -07001656 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001657 {
Florin Coras5e062572019-03-14 19:07:51 -07001658 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1659 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001660 return VPPCOM_EBADFD;
1661 }
1662
Florin Corasc127d5a2020-10-14 16:35:58 -07001663 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001664 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001665 {
Florin Coras60687192021-11-29 21:00:47 -08001666 VDBG (0,
1667 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1668 " (%s)",
1669 ls->vpp_handle, ls->session_state,
1670 vcl_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001671 return VPPCOM_EBADFD;
1672 }
1673 return VPPCOM_OK;
1674}
1675
1676int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001677vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1678{
1679 if (!strcmp (proto_str, "TCP"))
1680 *proto = VPPCOM_PROTO_TCP;
1681 else if (!strcmp (proto_str, "tcp"))
1682 *proto = VPPCOM_PROTO_TCP;
1683 else if (!strcmp (proto_str, "UDP"))
1684 *proto = VPPCOM_PROTO_UDP;
1685 else if (!strcmp (proto_str, "udp"))
1686 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001687 else if (!strcmp (proto_str, "TLS"))
1688 *proto = VPPCOM_PROTO_TLS;
1689 else if (!strcmp (proto_str, "tls"))
1690 *proto = VPPCOM_PROTO_TLS;
1691 else if (!strcmp (proto_str, "QUIC"))
1692 *proto = VPPCOM_PROTO_QUIC;
1693 else if (!strcmp (proto_str, "quic"))
1694 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001695 else if (!strcmp (proto_str, "DTLS"))
1696 *proto = VPPCOM_PROTO_DTLS;
1697 else if (!strcmp (proto_str, "dtls"))
1698 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001699 else if (!strcmp (proto_str, "SRTP"))
1700 *proto = VPPCOM_PROTO_SRTP;
1701 else if (!strcmp (proto_str, "srtp"))
1702 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001703 else
1704 return 1;
1705 return 0;
1706}
1707
1708int
Florin Coras134a9962018-08-28 11:32:04 -07001709vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001710 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001711{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001712 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001713 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001714 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001715 vcl_session_t *listen_session = 0;
1716 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001717 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001718 u8 is_nonblocking;
1719 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001720
Florin Coras5398dfb2021-01-25 20:31:27 -08001721again:
1722
Florin Coras134a9962018-08-28 11:32:04 -07001723 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001724 if (!listen_session)
1725 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001726
Florin Coras134a9962018-08-28 11:32:04 -07001727 listen_session_index = listen_session->session_index;
1728 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001729 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001730
Florin Coras54693d22018-07-17 10:46:29 -07001731 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001732 {
Florin Coras54693d22018-07-17 10:46:29 -07001733 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001734 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001735 accepted_msg = evt->accepted_msg;
1736 goto handle;
1737 }
1738
Florin Corasac422d62020-10-19 20:51:36 -07001739 is_nonblocking = vcl_session_has_attr (listen_session,
1740 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001741 while (1)
1742 {
Carl Smith592a9092019-11-12 14:57:37 +13001743 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1744 return VPPCOM_EAGAIN;
1745
Florin Coras5398dfb2021-01-25 20:31:27 -08001746 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1747 vcl_worker_flush_mq_events (wrk);
1748 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001749 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001750
Florin Coras54693d22018-07-17 10:46:29 -07001751handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001752
Florin Coras00cca802019-06-06 09:38:44 -07001753 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1754 listen_session_index);
1755 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1756 return VPPCOM_ECONNABORTED;
1757
Florin Coras134a9962018-08-28 11:32:04 -07001758 listen_session = vcl_session_get (wrk, listen_session_index);
1759 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001760
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001761 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001762 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001763
Florin Coras5e062572019-03-14 19:07:51 -07001764 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1765 " flags %d, is_nonblocking %u", listen_session->session_index,
1766 listen_session->vpp_handle, client_session_index,
1767 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001768 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001769
Dave Wallace048b1d62018-01-03 22:24:41 -05001770 if (ep)
1771 {
Florin Coras7e12d942018-06-27 14:32:43 -07001772 ep->is_ip4 = client_session->transport.is_ip4;
1773 ep->port = client_session->transport.rmt_port;
1774 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001775 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1776 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001777 else
Dave Barach178cf492018-11-13 16:34:13 -05001778 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1779 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001780 }
Dave Wallace60caa062017-11-10 17:07:13 -05001781
Florin Coras60687192021-11-29 21:00:47 -08001782 VDBG (0,
1783 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1784 "local: %U:%u",
1785 listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001786 client_session_index, client_session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001787 vcl_format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001788 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001789 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001790 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001791 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001792 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001793 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1794 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001795
Florin Coras3c7d4f92018-12-14 11:28:43 -08001796 /*
1797 * Session might have been closed already
1798 */
1799 if (accept_flags)
1800 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001801 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001802 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001803 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001804 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001805 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001806 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001807}
1808
1809int
Florin Coras134a9962018-08-28 11:32:04 -07001810vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001811{
Florin Coras134a9962018-08-28 11:32:04 -07001812 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001813 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001814 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001815 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001816
Florin Coras134a9962018-08-28 11:32:04 -07001817 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001818 if (!session)
1819 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001820 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001821
Florin Coras6c3b2182020-10-19 18:36:48 -07001822 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001823 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001824 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001825 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001826 }
1827
Florin Corasc127d5a2020-10-14 16:35:58 -07001828 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001829 {
Florin Coras60687192021-11-29 21:00:47 -08001830 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001831 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1832 " state (%s)",
1833 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001834 vcl_format_ip46_address, &session->transport.rmt_ip,
1835 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001836 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001837 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001838 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001839 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001840 }
1841
Florin Coras0a1e1832020-03-29 18:54:04 +00001842 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001843 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001844 {
1845 if (session->session_type != VPPCOM_PROTO_UDP)
1846 return VPPCOM_EINVAL;
1847 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001848 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001849 }
1850
Florin Coras7e12d942018-06-27 14:32:43 -07001851 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001852 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001853 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001854 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001855 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001856
Florin Coras5ffb9642021-12-03 17:24:13 -08001857 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1858 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001859 &session->transport.rmt_ip,
1860 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001861 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001862 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001863
Florin Coras458089b2019-08-21 16:20:44 -07001864 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001865
Florin Corasac422d62020-10-19 20:51:36 -07001866 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001867 {
fanyfa49d59d2020-10-13 17:07:16 +08001868 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001869 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001870 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001871 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001872 return VPPCOM_EINPROGRESS;
1873 }
Florin Coras57c88932019-08-29 12:03:17 -07001874
1875 /*
1876 * Wait for reply from vpp if blocking
1877 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001878 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001879 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001880
Florin Coras134a9962018-08-28 11:32:04 -07001881 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001882 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1883 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001884
Dave Wallace4878cbe2017-11-21 03:45:09 -05001885 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001886}
1887
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001888int
1889vppcom_session_stream_connect (uint32_t session_handle,
1890 uint32_t parent_session_handle)
1891{
1892 vcl_worker_t *wrk = vcl_worker_get_current ();
1893 vcl_session_t *session, *parent_session;
1894 u32 session_index, parent_session_index;
1895 int rv;
1896
1897 session = vcl_session_get_w_handle (wrk, session_handle);
1898 if (!session)
1899 return VPPCOM_EBADFD;
1900 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1901 if (!parent_session)
1902 return VPPCOM_EBADFD;
1903
1904 session_index = session->session_index;
1905 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001906 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001907 {
1908 VDBG (0, "ERROR: cannot connect epoll session %u!",
1909 session->session_index);
1910 return VPPCOM_EBADFD;
1911 }
1912
Florin Corasc127d5a2020-10-14 16:35:58 -07001913 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001914 {
Florin Coras60687192021-11-29 21:00:47 -08001915 VDBG (0,
1916 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001917 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001918 session_handle, session->vpp_handle, parent_session_handle,
1919 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001920 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001921 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001922 return VPPCOM_OK;
1923 }
1924
1925 /* Connect to quic session specifics */
1926 session->transport.is_ip4 = parent_session->transport.is_ip4;
1927 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1928 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001929 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001930
1931 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1932 session_handle, parent_session_handle, parent_session->vpp_handle);
1933
1934 /*
1935 * Send connect request and wait for reply from vpp
1936 */
Florin Coras458089b2019-08-21 16:20:44 -07001937 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001938 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001939 vcm->cfg.session_timeout);
1940
1941 session->listener_index = parent_session_index;
1942 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001943 if (parent_session)
1944 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001945
1946 session = vcl_session_get (wrk, session_index);
1947 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1948 session->vpp_handle, rv ? "failed" : "succeeded");
1949
1950 return rv;
1951}
1952
Steven58f464e2017-10-25 12:33:12 -07001953static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001954vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001955 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001956{
Florin Coras134a9962018-08-28 11:32:04 -07001957 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001958 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001959 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001960 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001961 session_event_t *e;
1962 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001963 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001964
Florin Coras070453d2018-08-24 17:04:27 -07001965 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001966 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001967
Florin Coras134a9962018-08-28 11:32:04 -07001968 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001969 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001970 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001971
Florin Coras6d0106e2019-01-29 20:11:58 -08001972 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001973 {
Florin Coras5e062572019-03-14 19:07:51 -07001974 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001975 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001976 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08001977 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001978 }
1979
liuyacan55c952e2021-06-13 14:54:55 +08001980 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
1981 {
1982 /* Vpp would ack the incoming data and enqueue it for reading.
1983 * So even if SHUT_RD is set, we can still read() the data if
1984 * the session is ready.
1985 */
1986 if (!vcl_session_read_ready (s))
1987 {
1988 return 0;
1989 }
1990 }
1991
Florin Corasac422d62020-10-19 20:51:36 -07001992 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001993 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001994 mq = wrk->app_event_queue;
1995 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001996 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001997
Sirshak Das28aa5392019-02-05 01:33:33 -06001998 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001999 {
Florin Coras54693d22018-07-17 10:46:29 -07002000 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002001 {
Yu Pingb2955352019-12-04 06:49:04 +08002002 if (vcl_session_is_closing (s))
2003 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002004 if (is_ct)
2005 svm_fifo_unset_event (s->rx_fifo);
2006 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002007 return VPPCOM_EWOULDBLOCK;
2008 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002009 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002010 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002011 if (vcl_session_is_closing (s))
2012 return vcl_session_closing_error (s);
2013
Florin Corasd6894562020-10-28 00:37:15 -07002014 if (is_ct)
2015 svm_fifo_unset_event (s->rx_fifo);
2016 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002017
Florin Coras5398dfb2021-01-25 20:31:27 -08002018 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2019 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002020 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002021 }
Florin Coras54693d22018-07-17 10:46:29 -07002022
Florin Coras4a2c7942020-09-10 12:27:14 -07002023read_again:
2024
Florin Coras460dce62018-07-27 05:45:06 -07002025 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002026 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002027 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002028 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2029
2030 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002031
2032 if (peek)
2033 return rv;
2034
Florin Coras4a2c7942020-09-10 12:27:14 -07002035 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002036
Sirshak Das28aa5392019-02-05 01:33:33 -06002037 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002038 {
Florin Corasd6894562020-10-28 00:37:15 -07002039 if (is_ct)
2040 svm_fifo_unset_event (s->rx_fifo);
2041 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002042 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002043 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002044 {
Florin Corasc34118b2020-08-12 18:58:25 -07002045 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2046 e->event_type = SESSION_IO_EVT_RX;
2047 e->session_index = s->session_index;
2048 }
2049 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002050 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002051 {
2052 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002053 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002054 buf += rv;
2055 n -= rv;
2056 goto read_again;
2057 }
Florin Coras41c9e042018-09-11 00:10:41 -07002058
Florin Coras17ec5772020-08-12 20:46:29 -07002059 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002060 {
Florin Coras17ec5772020-08-12 20:46:29 -07002061 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002062 app_send_io_evt_to_vpp (s->vpp_evt_q,
2063 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002064 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002065 }
2066
Florin Coras5e062572019-03-14 19:07:51 -07002067 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2068 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002069
Florin Coras54693d22018-07-17 10:46:29 -07002070 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002071}
2072
Steven58f464e2017-10-25 12:33:12 -07002073int
Florin Coras134a9962018-08-28 11:32:04 -07002074vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002075{
Florin Coras134a9962018-08-28 11:32:04 -07002076 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002077}
2078
2079static int
Florin Coras134a9962018-08-28 11:32:04 -07002080vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002081{
Florin Coras134a9962018-08-28 11:32:04 -07002082 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002083}
2084
Florin Coras2cba8532018-09-11 16:33:36 -07002085int
2086vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002087 vppcom_data_segment_t * ds, uint32_t n_segments,
2088 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002089{
2090 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002091 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002092 vcl_session_t *s = 0;
2093 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002094 svm_msg_q_t *mq;
2095 u8 is_ct;
2096
2097 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002098 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002099 return VPPCOM_EBADFD;
2100
Florin Coras6d0106e2019-01-29 20:11:58 -08002101 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2102 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002103
Florin Corasac422d62020-10-19 20:51:36 -07002104 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002105 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002106 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002107 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002108 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002109
Sirshak Das28aa5392019-02-05 01:33:33 -06002110 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002111 {
2112 if (is_nonblocking)
2113 {
Florin Coras62877022020-10-28 21:22:04 -07002114 if (is_ct)
2115 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002116 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002117 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002118 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002119 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002120 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002121 if (vcl_session_is_closing (s))
2122 return vcl_session_closing_error (s);
2123
Florin Coras62877022020-10-28 21:22:04 -07002124 if (is_ct)
2125 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002126 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002127
Florin Coras5398dfb2021-01-25 20:31:27 -08002128 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2129 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002130 }
2131 }
2132
Florin Corasd1cc38d2020-10-11 11:05:04 -07002133 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002134 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002135 if (n_read < 0)
2136 return VPPCOM_EAGAIN;
2137
2138 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2139 {
Florin Coras62877022020-10-28 21:22:04 -07002140 if (is_ct)
2141 svm_fifo_unset_event (s->rx_fifo);
2142 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002143 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002144 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002145 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002146 {
2147 session_event_t *e;
2148 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2149 e->event_type = SESSION_IO_EVT_RX;
2150 e->session_index = s->session_index;
2151 }
2152 }
2153
2154 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002155 return n_read;
2156}
2157
2158void
Florin Corasd68faf82020-09-25 15:18:13 -07002159vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002160{
2161 vcl_worker_t *wrk = vcl_worker_get_current ();
2162 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002163 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002164
2165 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002166 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002167 return;
2168
Florin Coras62877022020-10-28 21:22:04 -07002169 is_ct = vcl_session_is_ct (s);
2170 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002171
Florin Coras8cc93212021-09-10 11:37:12 -07002172 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002173 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002174}
2175
Florin Coras7a2abce2020-04-05 19:25:44 +00002176always_inline u8
2177vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002178{
Florin Coras7a2abce2020-04-05 19:25:44 +00002179 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2180 if (is_dgram)
2181 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2182 else
2183 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002184}
2185
2186always_inline int
2187vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2188 size_t n, u8 is_flush, u8 is_dgram)
2189{
Florin Coras6d0106e2019-01-29 20:11:58 -08002190 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002191 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002192 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002193 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002194 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002195
Florin Coras0b0d28e2021-06-04 17:31:53 -07002196 /* Accept zero length writes but just return */
2197 if (PREDICT_FALSE (!n))
2198 return VPPCOM_OK;
2199
2200 if (PREDICT_FALSE (!buf))
2201 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002202
Florin Coras6c3b2182020-10-19 18:36:48 -07002203 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002204 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002205 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2206 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002207 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002208 }
2209
liuyacan55c952e2021-06-13 14:54:55 +08002210 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002211 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002212 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2213 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002214 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002215 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002216 }
2217
liuyacan55c952e2021-06-13 14:54:55 +08002218 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2219 {
2220 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2221 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002222 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002223 return VPPCOM_EPIPE;
2224 }
2225
Florin Coras0e88e852018-09-17 22:09:02 -07002226 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002227 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002228 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002229
Florin Coras653e43f2019-03-04 10:56:23 -08002230 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002231 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002232 {
Florin Coras54693d22018-07-17 10:46:29 -07002233 if (is_nonblocking)
2234 {
Florin Coras070453d2018-08-24 17:04:27 -07002235 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002236 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002237 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002238 {
Florin Coras2d379d82019-06-28 12:45:12 -07002239 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002240 if (vcl_session_is_closing (s))
2241 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002242
Florin Coras5398dfb2021-01-25 20:31:27 -08002243 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2244 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002245 }
Dave Wallace543852a2017-08-03 02:11:34 -04002246 }
Dave Wallace543852a2017-08-03 02:11:34 -04002247
Florin Coras653e43f2019-03-04 10:56:23 -08002248 et = SESSION_IO_EVT_TX;
2249 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002250 et = SESSION_IO_EVT_TX_FLUSH;
2251
Florin Coras7a2abce2020-04-05 19:25:44 +00002252 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002253 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002254 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002255 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002256 else
2257 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002258 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002259
Florin Coras14ed6df2019-03-06 21:13:42 -08002260 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002261 app_send_io_evt_to_vpp (
2262 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002263
Florin Coras56230092020-09-02 20:52:58 -07002264 /* The underlying fifo segment can run out of memory */
2265 if (PREDICT_FALSE (n_write < 0))
2266 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002267
Florin Coras6d0106e2019-01-29 20:11:58 -08002268 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2269 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002270
Florin Coras54693d22018-07-17 10:46:29 -07002271 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002272}
2273
Florin Coras42ceddb2018-12-12 10:56:01 -08002274int
2275vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2276{
Florin Coras7a2abce2020-04-05 19:25:44 +00002277 vcl_worker_t *wrk = vcl_worker_get_current ();
2278 vcl_session_t *s;
2279
2280 s = vcl_session_get_w_handle (wrk, session_handle);
2281 if (PREDICT_FALSE (!s))
2282 return VPPCOM_EBADFD;
2283
2284 return vppcom_session_write_inline (wrk, s, buf, n,
2285 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002286}
2287
Florin Corasb0f662f2018-12-27 14:51:46 -08002288int
2289vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2290{
Florin Coras7a2abce2020-04-05 19:25:44 +00002291 vcl_worker_t *wrk = vcl_worker_get_current ();
2292 vcl_session_t *s;
2293
2294 s = vcl_session_get_w_handle (wrk, session_handle);
2295 if (PREDICT_FALSE (!s))
2296 return VPPCOM_EBADFD;
2297
2298 return vppcom_session_write_inline (wrk, s, buf, n,
2299 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002300}
2301
Florin Corasc0737e92019-03-04 14:19:39 -08002302#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002303if (PREDICT_FALSE (!_s->rx_fifo)) \
2304 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002305if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2306 { \
2307 if (!vcl_session_is_ct (_s)) \
2308 { \
2309 svm_fifo_unset_event (_s->rx_fifo); \
2310 if (svm_fifo_is_empty (_s->rx_fifo)) \
2311 break; \
2312 } \
2313 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2314 { \
Florin Coras2f630182020-08-13 00:17:51 -07002315 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002316 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2317 break; \
2318 } \
2319 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002320
Florin Coras86f04502018-09-12 16:08:01 -07002321static void
2322vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2323 unsigned long n_bits, unsigned long *read_map,
2324 unsigned long *write_map,
2325 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002326{
2327 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002328 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002329 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002330 u32 sid;
2331
2332 switch (e->event_type)
2333 {
Florin Corasc0737e92019-03-04 14:19:39 -08002334 case SESSION_IO_EVT_RX:
2335 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002336 s = vcl_session_get (wrk, sid);
2337 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002338 break;
Florin Corasb242d312020-10-26 15:35:40 -07002339 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002340 if (sid < n_bits && read_map)
2341 {
David Johnsond9818dd2018-12-14 14:53:41 -05002342 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002343 *bits_set += 1;
2344 }
2345 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002346 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002347 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002348 s = vcl_session_get (wrk, sid);
2349 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002350 break;
2351 if (sid < n_bits && write_map)
2352 {
David Johnsond9818dd2018-12-14 14:53:41 -05002353 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002354 *bits_set += 1;
2355 }
2356 break;
Florin Coras86f04502018-09-12 16:08:01 -07002357 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002358 if (!e->postponed)
2359 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2360 else
2361 s = vcl_session_get (wrk, e->session_index);
2362 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002363 break;
Florin Corasb242d312020-10-26 15:35:40 -07002364 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002365 if (sid < n_bits && read_map)
2366 {
David Johnsond9818dd2018-12-14 14:53:41 -05002367 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002368 *bits_set += 1;
2369 }
2370 break;
2371 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002372 if (!e->postponed)
2373 {
2374 connected_msg = (session_connected_msg_t *) e->data;
2375 sid = vcl_session_connected_handler (wrk, connected_msg);
2376 }
2377 else
2378 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002379 if (sid == VCL_INVALID_SESSION_INDEX)
2380 break;
2381 if (sid < n_bits && write_map)
2382 {
2383 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2384 *bits_set += 1;
2385 }
Florin Coras86f04502018-09-12 16:08:01 -07002386 break;
2387 case SESSION_CTRL_EVT_DISCONNECTED:
2388 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002389 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2390 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002391 break;
Florin Corasb242d312020-10-26 15:35:40 -07002392 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002393 if (sid < n_bits && except_map)
2394 {
David Johnsond9818dd2018-12-14 14:53:41 -05002395 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002396 *bits_set += 1;
2397 }
2398 break;
2399 case SESSION_CTRL_EVT_RESET:
2400 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2401 if (sid < n_bits && except_map)
2402 {
David Johnsond9818dd2018-12-14 14:53:41 -05002403 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002404 *bits_set += 1;
2405 }
2406 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002407 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2408 vcl_session_unlisten_reply_handler (wrk, e->data);
2409 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002410 case SESSION_CTRL_EVT_MIGRATED:
2411 vcl_session_migrated_handler (wrk, e->data);
2412 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002413 case SESSION_CTRL_EVT_CLEANUP:
2414 vcl_session_cleanup_handler (wrk, e->data);
2415 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002416 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2417 vcl_session_worker_update_reply_handler (wrk, e->data);
2418 break;
2419 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2420 vcl_session_req_worker_update_handler (wrk, e->data);
2421 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002422 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2423 vcl_session_app_add_segment_handler (wrk, e->data);
2424 break;
2425 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2426 vcl_session_app_del_segment_handler (wrk, e->data);
2427 break;
hanlina3a48962020-07-13 11:09:15 +08002428 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002429 vcl_worker_rpc_handler (wrk, e->data);
2430 break;
Florin Coras86f04502018-09-12 16:08:01 -07002431 default:
2432 clib_warning ("unhandled: %u", e->event_type);
2433 break;
2434 }
2435}
2436
2437static int
2438vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2439 unsigned long n_bits, unsigned long *read_map,
2440 unsigned long *write_map, unsigned long *except_map,
2441 double time_to_wait, u32 * bits_set)
2442{
Florin Coras99368312018-08-02 10:45:44 -07002443 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002444 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002445 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002446
Florin Coras54693d22018-07-17 10:46:29 -07002447 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002448 {
Florin Coras54693d22018-07-17 10:46:29 -07002449 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002450 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002451
Florin Coras54693d22018-07-17 10:46:29 -07002452 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002453 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002454 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002455 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002456 else
2457 {
2458 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002459 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002460 }
2461 }
Florin Corase003a1b2019-06-05 10:47:16 -07002462 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002463
Florin Coras134a9962018-08-28 11:32:04 -07002464 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002465 {
Florin Coras134a9962018-08-28 11:32:04 -07002466 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002467 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002468 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2469 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002470 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002471 }
Florin Coras134a9962018-08-28 11:32:04 -07002472 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002473 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002474 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002475}
2476
Florin Coras99368312018-08-02 10:45:44 -07002477static int
Florin Coras294afe22019-01-07 17:49:17 -08002478vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2479 vcl_si_set * read_map, vcl_si_set * write_map,
2480 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002481 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002482{
Florin Coras14ed6df2019-03-06 21:13:42 -08002483 double wait = 0, start = 0;
2484
2485 if (!*bits_set)
2486 {
2487 wait = time_to_wait;
2488 start = clib_time_now (&wrk->clib_time);
2489 }
2490
2491 do
2492 {
2493 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2494 write_map, except_map, wait, bits_set);
2495 if (*bits_set)
2496 return *bits_set;
2497 if (wait == -1)
2498 continue;
2499
2500 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2501 }
2502 while (wait > 0);
2503
2504 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002505}
2506
2507static int
Florin Coras294afe22019-01-07 17:49:17 -08002508vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2509 vcl_si_set * read_map, vcl_si_set * write_map,
2510 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002511 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002512{
2513 vcl_mq_evt_conn_t *mqc;
2514 int __clib_unused n_read;
2515 int n_mq_evts, i;
2516 u64 buf;
2517
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002518 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2519 {
2520 vcl_api_retry_attach (wrk);
2521 return 0;
2522 }
2523
Florin Coras134a9962018-08-28 11:32:04 -07002524 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2525 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2526 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002527 for (i = 0; i < n_mq_evts; i++)
2528 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002529 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2530 {
2531 vcl_api_handle_disconnect (wrk);
2532 continue;
2533 }
2534
Florin Coras134a9962018-08-28 11:32:04 -07002535 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002536 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002537 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002538 except_map, 0, bits_set);
2539 }
2540
2541 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2542}
2543
Dave Wallace543852a2017-08-03 02:11:34 -04002544int
Florin Coras294afe22019-01-07 17:49:17 -08002545vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2546 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002547{
Florin Coras54693d22018-07-17 10:46:29 -07002548 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002549 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002550 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002551 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002552
Dave Wallace7876d392017-10-19 03:53:57 -04002553 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002554 {
Florin Coras134a9962018-08-28 11:32:04 -07002555 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002556 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002557 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2558 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002559 }
Dave Wallace7876d392017-10-19 03:53:57 -04002560 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002561 {
Florin Coras134a9962018-08-28 11:32:04 -07002562 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002563 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002564 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2565 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002566 }
Dave Wallace7876d392017-10-19 03:53:57 -04002567 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002568 {
Florin Coras134a9962018-08-28 11:32:04 -07002569 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002570 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002571 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2572 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002573 }
2574
Florin Coras54693d22018-07-17 10:46:29 -07002575 if (!n_bits)
2576 return 0;
2577
2578 if (!write_map)
2579 goto check_rd;
2580
Florin Coras096a1d52021-01-27 15:33:51 -08002581 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2582 {
2583 if (!(s = vcl_session_get (wrk, sid)))
2584 {
2585 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2586 bits_set++;
2587 continue;
2588 }
Florin Coras54693d22018-07-17 10:46:29 -07002589
Florin Coras096a1d52021-01-27 15:33:51 -08002590 if (vcl_session_write_ready (s))
2591 {
2592 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2593 bits_set++;
2594 }
2595 else
2596 {
2597 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2598 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2599 }
2600 }
Florin Coras54693d22018-07-17 10:46:29 -07002601
2602check_rd:
2603 if (!read_map)
2604 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002605
Florin Coras096a1d52021-01-27 15:33:51 -08002606 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2607 {
2608 if (!(s = vcl_session_get (wrk, sid)))
2609 {
2610 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2611 bits_set++;
2612 continue;
2613 }
Florin Coras54693d22018-07-17 10:46:29 -07002614
Florin Coras096a1d52021-01-27 15:33:51 -08002615 if (vcl_session_read_ready (s))
2616 {
2617 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2618 bits_set++;
2619 }
2620 }
Florin Coras54693d22018-07-17 10:46:29 -07002621
2622check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002623
Florin Coras86f04502018-09-12 16:08:01 -07002624 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2625 {
2626 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2627 read_map, write_map, except_map, &bits_set);
2628 }
2629 vec_reset_length (wrk->unhandled_evts_vector);
2630
Florin Coras99368312018-08-02 10:45:44 -07002631 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002632 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002633 time_to_wait, &bits_set);
2634 else
Florin Coras134a9962018-08-28 11:32:04 -07002635 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002636 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002637
Dave Wallace543852a2017-08-03 02:11:34 -04002638 return (bits_set);
2639}
2640
Dave Wallacef7f809c2017-10-03 01:48:42 -04002641static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002642vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002643{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002644 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002645 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002646 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002647
Florin Corasc0737e92019-03-04 14:19:39 -08002648 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002649 return;
2650
Florin Coras6c3b2182020-10-19 18:36:48 -07002651 s = vcl_session_get_w_handle (wrk, vep_handle);
2652 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002653 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002654 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655 goto done;
2656 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002657 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002658 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002659 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002660 goto done;
2661 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002662 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002663 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002664 "{\n"
2665 " is_vep = %u\n"
2666 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002667 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002668 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2669 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002670
Florin Coras7e5e62b2019-07-30 14:08:23 -07002671 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002672 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002673 s = vcl_session_get_w_handle (wrk, sh);
2674 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002676 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002677 goto done;
2678 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002679 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002680 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002681 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002682 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002683 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002684 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002685 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002686 goto done;
2687 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002688 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002689 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2690 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002691 sh, s->vep.vep_sh, vep_handle);
2692 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002693 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002694 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002695 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002696 " next_sh = 0x%x (%u)\n"
2697 " prev_sh = 0x%x (%u)\n"
2698 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002699 " ev.events = 0x%x\n"
2700 " ev.data.u64 = 0x%llx\n"
2701 " et_mask = 0x%x\n"
2702 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002703 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002704 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2705 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002706 }
2707 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002708
2709done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002710 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002711}
2712
2713int
2714vppcom_epoll_create (void)
2715{
Florin Coras134a9962018-08-28 11:32:04 -07002716 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002717 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002718
Florin Coras134a9962018-08-28 11:32:04 -07002719 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002720
Florin Coras6c3b2182020-10-19 18:36:48 -07002721 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002722 vep_session->vep.vep_sh = ~0;
2723 vep_session->vep.next_sh = ~0;
2724 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002725 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002726
Florin Corasa7a1a222018-12-30 17:11:31 -08002727 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2728 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002729
Florin Corasab2f6db2018-08-31 14:31:41 -07002730 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002731}
2732
2733int
Florin Coras134a9962018-08-28 11:32:04 -07002734vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002735 struct epoll_event *event)
2736{
Florin Coras134a9962018-08-28 11:32:04 -07002737 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002738 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002739 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002740 vcl_session_t *s;
2741 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002742
Florin Coras134a9962018-08-28 11:32:04 -07002743 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002744 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002745 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746 return VPPCOM_EINVAL;
2747 }
2748
Florin Coras134a9962018-08-28 11:32:04 -07002749 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002750 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002752 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002753 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002754 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002755 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002756 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002757 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002758 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002759 }
2760
Florin Coras134a9962018-08-28 11:32:04 -07002761 ASSERT (vep_session->vep.vep_sh == ~0);
2762 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002763
Florin Coras17ec5772020-08-12 20:46:29 -07002764 s = vcl_session_get_w_handle (wrk, session_handle);
2765 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002766 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002767 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002768 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002770 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002772 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002773 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002774 }
2775
2776 switch (op)
2777 {
2778 case EPOLL_CTL_ADD:
2779 if (PREDICT_FALSE (!event))
2780 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002781 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002782 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002783 }
Florin Coras2645f682021-06-04 15:59:41 -07002784 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2785 {
2786 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2787 rv = VPPCOM_EEXIST;
2788 goto done;
2789 }
Florin Coras134a9962018-08-28 11:32:04 -07002790 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791 {
Florin Coras7e12d942018-06-27 14:32:43 -07002792 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002793 next_session = vcl_session_get_w_handle (wrk,
2794 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002795 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002796 {
Florin Coras5e062572019-03-14 19:07:51 -07002797 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002798 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002799 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002800 }
Florin Coras134a9962018-08-28 11:32:04 -07002801 ASSERT (next_session->vep.prev_sh == vep_handle);
2802 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002803 }
Florin Coras17ec5772020-08-12 20:46:29 -07002804 s->vep.next_sh = vep_session->vep.next_sh;
2805 s->vep.prev_sh = vep_handle;
2806 s->vep.vep_sh = vep_handle;
2807 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002808 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002809 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002810 s->flags &= ~VCL_SESSION_F_IS_VEP;
2811 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002812 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002813
Florin Coras17ec5772020-08-12 20:46:29 -07002814 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2815 if (txf && (event->events & EPOLLOUT))
2816 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002817
Florin Coras6e3c1f82020-01-15 01:30:46 +00002818 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002819 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002820 {
2821 session_event_t e = { 0 };
2822 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002823 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002824 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002825 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002826 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002827 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002828 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002829 {
2830 session_event_t e = { 0 };
2831 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002832 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002833 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002834 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2835 add_evt = 1;
2836 }
2837 if (!add_evt && vcl_session_is_closing (s))
2838 {
2839 session_event_t e = { 0 };
2840 if (s->session_state == VCL_STATE_VPP_CLOSING)
2841 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2842 else
2843 e.event_type = SESSION_CTRL_EVT_RESET;
2844 e.session_index = s->session_index;
2845 e.postponed = 1;
2846 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002847 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002848 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2849 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002850 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002851 break;
2852
2853 case EPOLL_CTL_MOD:
2854 if (PREDICT_FALSE (!event))
2855 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002856 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002857 rv = VPPCOM_EINVAL;
2858 goto done;
2859 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002860 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002861 {
Florin Coras5e062572019-03-14 19:07:51 -07002862 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002863 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002864 goto done;
2865 }
Florin Coras17ec5772020-08-12 20:46:29 -07002866 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002867 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002868 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002869 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002870 rv = VPPCOM_EINVAL;
2871 goto done;
2872 }
hanlin475c9d72019-12-26 11:44:28 +08002873
Florin Coras2645f682021-06-04 15:59:41 -07002874 /* Generate EPOLLOUT if session write ready nd event was not on */
2875 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2876 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002877 {
2878 session_event_t e = { 0 };
2879 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002880 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002881 vec_add1 (wrk->unhandled_evts_vector, e);
2882 }
Florin Coras2645f682021-06-04 15:59:41 -07002883 /* Generate EPOLLIN if session read ready and event was not on */
2884 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2885 (vcl_session_read_ready (s) > 0))
2886 {
2887 session_event_t e = { 0 };
2888 e.event_type = SESSION_IO_EVT_RX;
2889 e.session_index = s->session_index;
2890 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002891 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002892 }
Florin Coras17ec5772020-08-12 20:46:29 -07002893 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2894 s->vep.ev = *event;
2895 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002896 if (txf)
2897 {
2898 if (event->events & EPOLLOUT)
2899 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2900 else
2901 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2902 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002903 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2904 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002905 break;
2906
2907 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002908 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002909 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002910 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002911 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002912 goto done;
2913 }
Florin Coras17ec5772020-08-12 20:46:29 -07002914 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002915 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002916 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002917 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002918 rv = VPPCOM_EINVAL;
2919 goto done;
2920 }
2921
Florin Coras17ec5772020-08-12 20:46:29 -07002922 if (s->vep.prev_sh == vep_handle)
2923 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002924 else
2925 {
Florin Coras7e12d942018-06-27 14:32:43 -07002926 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002927 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002928 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002929 {
Florin Coras5e062572019-03-14 19:07:51 -07002930 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002931 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002932 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002933 }
Florin Coras134a9962018-08-28 11:32:04 -07002934 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002935 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002936 }
Florin Coras17ec5772020-08-12 20:46:29 -07002937 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002938 {
Florin Coras7e12d942018-06-27 14:32:43 -07002939 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002940 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002941 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002942 {
Florin Coras5e062572019-03-14 19:07:51 -07002943 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002944 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002945 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002946 }
Florin Coras134a9962018-08-28 11:32:04 -07002947 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002948 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002949 }
2950
Florin Corasfa3884f2021-06-22 20:04:46 -07002951 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2952 vcl_epoll_lt_del (wrk, s);
2953
Florin Coras17ec5772020-08-12 20:46:29 -07002954 memset (&s->vep, 0, sizeof (s->vep));
2955 s->vep.next_sh = ~0;
2956 s->vep.prev_sh = ~0;
2957 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002958 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002959 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002960
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002961 if (vcl_session_is_open (s))
2962 {
2963 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2964 if (txf)
2965 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2966 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002967
Florin Coras5e062572019-03-14 19:07:51 -07002968 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002969 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002970 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002971 break;
2972
2973 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002974 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002975 rv = VPPCOM_EINVAL;
2976 }
2977
Florin Coras134a9962018-08-28 11:32:04 -07002978 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002979
2980done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002981 return rv;
2982}
2983
Florin Coras86f04502018-09-12 16:08:01 -07002984static inline void
2985vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2986 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002987{
2988 session_disconnected_msg_t *disconnected_msg;
2989 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002990 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002991 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002992 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002993 u8 add_event = 0;
2994
2995 switch (e->event_type)
2996 {
Florin Coras653e43f2019-03-04 10:56:23 -08002997 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002998 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002999 s = vcl_session_get (wrk, sid);
3000 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003001 break;
Florin Corasb242d312020-10-26 15:35:40 -07003002 vcl_fifo_rx_evt_valid_or_break (s);
3003 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003004 if (!(EPOLLIN & s->vep.ev.events) ||
3005 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3006 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003007 break;
3008 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003009 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003010 session_evt_data = s->vep.ev.data.u64;
3011 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003012 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003013 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003014 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003015 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003016 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003017 break;
Florin Corasb242d312020-10-26 15:35:40 -07003018 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003019 if (!(EPOLLOUT & session_events))
3020 break;
3021 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003022 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003023 session_evt_data = s->vep.ev.data.u64;
3024 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3025 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003026 break;
Florin Coras86f04502018-09-12 16:08:01 -07003027 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003028 if (!e->postponed)
3029 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3030 else
3031 s = vcl_session_get (wrk, e->session_index);
3032 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003033 break;
Florin Corasb242d312020-10-26 15:35:40 -07003034 session_events = s->vep.ev.events;
3035 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003036 if (!(EPOLLIN & session_events) ||
3037 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003038 break;
Florin Coras86f04502018-09-12 16:08:01 -07003039 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003040 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003041 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003042 break;
3043 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003044 if (!e->postponed)
3045 {
3046 connected_msg = (session_connected_msg_t *) e->data;
3047 sid = vcl_session_connected_handler (wrk, connected_msg);
3048 }
3049 else
3050 sid = e->session_index;
3051 s = vcl_session_get (wrk, sid);
3052 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003053 break;
Florin Corasb242d312020-10-26 15:35:40 -07003054 session_events = s->vep.ev.events;
3055 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003056 if (!(EPOLLOUT & session_events))
3057 break;
3058 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003059 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003060 session_evt_data = s->vep.ev.data.u64;
3061 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003062 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003063 break;
3064 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003065 if (!e->postponed)
3066 {
3067 disconnected_msg = (session_disconnected_msg_t *) e->data;
3068 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3069 }
3070 else
3071 {
3072 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003073 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003074 }
3075 if (vcl_session_is_closed (s) ||
3076 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003077 {
Florin Coras74254b52021-12-08 11:03:08 -08003078 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003079 vcl_session_free (wrk, s);
3080 break;
3081 }
Florin Corasb242d312020-10-26 15:35:40 -07003082 sid = s->session_index;
3083 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003084 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003085 if (EPOLLRDHUP & session_events)
3086 {
3087 /* If app can distinguish between RDHUP and HUP,
3088 * we make finer control */
3089 events[*num_ev].events = EPOLLRDHUP;
3090 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3091 {
3092 events[*num_ev].events |= EPOLLHUP;
3093 }
3094 }
3095 else
3096 {
3097 events[*num_ev].events = EPOLLHUP;
3098 }
Florin Corasb242d312020-10-26 15:35:40 -07003099 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003100
Florin Coras86f04502018-09-12 16:08:01 -07003101 break;
3102 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003103 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003104 {
3105 sid =
3106 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3107 s = vcl_session_get (wrk, sid);
3108 }
Florin Coras87f76002021-06-28 19:13:29 -07003109 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003110 {
3111 sid = e->session_index;
3112 s = vcl_session_get (wrk, sid);
3113 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3114 }
Florin Coras87f76002021-06-28 19:13:29 -07003115 if (vcl_session_is_closed (s) ||
3116 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003117 {
Florin Coras74254b52021-12-08 11:03:08 -08003118 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003119 vcl_session_free (wrk, s);
3120 break;
3121 }
Florin Corasb242d312020-10-26 15:35:40 -07003122 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003123 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003124 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3125 if ((EPOLLRDHUP & session_events) &&
3126 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3127 {
Yacan Liuab157702022-09-26 16:41:32 +08003128 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003129 }
3130 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3131 {
3132 events[*num_ev].events |= EPOLLIN;
3133 }
Florin Corasb242d312020-10-26 15:35:40 -07003134 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003135 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003136 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3137 vcl_session_unlisten_reply_handler (wrk, e->data);
3138 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003139 case SESSION_CTRL_EVT_MIGRATED:
3140 vcl_session_migrated_handler (wrk, e->data);
3141 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003142 case SESSION_CTRL_EVT_CLEANUP:
3143 vcl_session_cleanup_handler (wrk, e->data);
3144 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003145 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3146 vcl_session_req_worker_update_handler (wrk, e->data);
3147 break;
3148 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3149 vcl_session_worker_update_reply_handler (wrk, e->data);
3150 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003151 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3152 vcl_session_app_add_segment_handler (wrk, e->data);
3153 break;
3154 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3155 vcl_session_app_del_segment_handler (wrk, e->data);
3156 break;
hanlina3a48962020-07-13 11:09:15 +08003157 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003158 vcl_worker_rpc_handler (wrk, e->data);
3159 break;
Florin Coras86f04502018-09-12 16:08:01 -07003160 default:
3161 VDBG (0, "unhandled: %u", e->event_type);
3162 break;
3163 }
3164
3165 if (add_event)
3166 {
3167 events[*num_ev].data.u64 = session_evt_data;
3168 if (EPOLLONESHOT & session_events)
3169 {
Florin Corasb242d312020-10-26 15:35:40 -07003170 s = vcl_session_get (wrk, sid);
3171 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003172 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003173 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003174 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003175 s = vcl_session_get (wrk, sid);
3176 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3177 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003178 }
Florin Coras86f04502018-09-12 16:08:01 -07003179 *num_ev += 1;
3180 }
3181}
3182
3183static int
3184vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3185 struct epoll_event *events, u32 maxevents,
3186 double wait_for_time, u32 * num_ev)
3187{
Florin Coras99368312018-08-02 10:45:44 -07003188 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003189 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003190 int i;
3191
Florin Coras539663c2018-09-28 14:59:37 -07003192 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3193 goto handle_dequeued;
3194
Florin Coras54693d22018-07-17 10:46:29 -07003195 if (svm_msg_q_is_empty (mq))
3196 {
3197 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003198 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003199 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003200 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003201 else
3202 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003203 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003204 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003205 }
3206 }
Florin Corase003a1b2019-06-05 10:47:16 -07003207 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003208 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003209
Florin Coras539663c2018-09-28 14:59:37 -07003210handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003211 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003212 {
Florin Coras134a9962018-08-28 11:32:04 -07003213 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003214 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003215 if (*num_ev < maxevents)
3216 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3217 else
3218 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003219 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003220 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003221 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003222 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003223 return *num_ev;
3224}
3225
Florin Coras99368312018-08-02 10:45:44 -07003226static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003227vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3228 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003229{
Florin Corasccdb8b82021-04-28 13:01:06 -07003230 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003231
3232 if (!n_evts)
3233 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003234 if (timeout_ms > 0)
3235 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003236 }
3237
3238 do
3239 {
3240 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003241 timeout_ms, &n_evts);
3242 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003243 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003244 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003245 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003246
3247 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003248}
3249
3250static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003251vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3252 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003253{
Florin Coras99368312018-08-02 10:45:44 -07003254 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003255 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003256 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003257 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003258 u64 buf;
3259
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003260 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3261 {
3262 vcl_api_retry_attach (wrk);
3263 return n_evts;
3264 }
3265
Florin Coras134a9962018-08-28 11:32:04 -07003266 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003267 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003268 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003269 if (timeout_ms > 0)
3270 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003271 }
3272
Florin Corasb13ba132021-01-27 16:05:24 -08003273 do
3274 {
3275 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003276 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003277 if (n_mq_evts < 0)
3278 {
3279 VDBG (0, "epoll_wait error %u", errno);
3280 return n_evts;
3281 }
3282
3283 for (i = 0; i < n_mq_evts; i++)
3284 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003285 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3286 {
3287 /* api socket was closed */
3288 vcl_api_handle_disconnect (wrk);
3289 continue;
3290 }
3291
Florin Corasb13ba132021-01-27 16:05:24 -08003292 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3293 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3294 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3295 &n_evts);
3296 }
3297
Florin Corasccdb8b82021-04-28 13:01:06 -07003298 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003299 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003300 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003301 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003302
3303 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003304}
3305
Florin Corasfe286f72021-06-04 10:07:55 -07003306static void
Florin Corasfe286f72021-06-04 10:07:55 -07003307vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3308 int maxevents, u32 *n_evts)
3309{
Florin Coras734268f2021-06-30 07:54:29 -07003310 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003311 vcl_session_t *s;
3312 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003313 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003314
Florin Corasfa3884f2021-06-22 20:04:46 -07003315 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003316 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003317 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003318
Florin Corasfa3884f2021-06-22 20:04:46 -07003319 next = wrk->ep_lt_current;
3320 do
Florin Corasfe286f72021-06-04 10:07:55 -07003321 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003322 s = vcl_session_get (wrk, next);
3323 next = s->vep.lt_next;
3324
3325 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003326 {
3327 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003328 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003329 evt_data = s->vep.ev.data.u64;
3330 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003331 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003332 {
3333 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003334 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3335 evt_data = s->vep.ev.data.u64;
3336 }
3337 if (!add_event && s->session_state > VCL_STATE_READY)
3338 {
3339 add_event = 1;
3340 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003341 evt_data = s->vep.ev.data.u64;
3342 }
3343 if (add_event)
3344 {
3345 events[*n_evts].data.u64 = evt_data;
3346 *n_evts += 1;
3347 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003348 if (EPOLLONESHOT & s->vep.ev.events)
3349 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003350 if (*n_evts == maxevents)
3351 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003352 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003353 break;
3354 }
3355 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003356 else
3357 {
3358 vcl_epoll_lt_del (wrk, s);
3359 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3360 break;
3361 }
Florin Corasfe286f72021-06-04 10:07:55 -07003362 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003363 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003364}
3365
Dave Wallacef7f809c2017-10-03 01:48:42 -04003366int
Florin Coras134a9962018-08-28 11:32:04 -07003367vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003368 int maxevents, double wait_for_time)
3369{
Florin Coras134a9962018-08-28 11:32:04 -07003370 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003371 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003372 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003373 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003374
3375 if (PREDICT_FALSE (maxevents <= 0))
3376 {
Florin Coras5e062572019-03-14 19:07:51 -07003377 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003378 return VPPCOM_EINVAL;
3379 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003380
Florin Coras134a9962018-08-28 11:32:04 -07003381 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003382 if (!vep_session)
3383 return VPPCOM_EBADFD;
3384
Florin Coras6c3b2182020-10-19 18:36:48 -07003385 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003386 {
Florin Coras5e062572019-03-14 19:07:51 -07003387 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003388 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003389 }
Florin Coras54693d22018-07-17 10:46:29 -07003390
Florin Coras86f04502018-09-12 16:08:01 -07003391 if (vec_len (wrk->unhandled_evts_vector))
3392 {
3393 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3394 {
3395 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3396 events, &n_evts);
3397 if (n_evts == maxevents)
3398 {
Florin Corase003a1b2019-06-05 10:47:16 -07003399 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3400 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003401 }
3402 }
Florin Corase003a1b2019-06-05 10:47:16 -07003403 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003404 }
liuyacancba87102021-09-10 15:14:05 +08003405
3406 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3407 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3408
wanghanlin8919fec2021-03-18 20:00:41 +08003409 /* Request to only drain unhandled */
3410 if ((int) wait_for_time == -2)
3411 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003412
Florin Coras86f04502018-09-12 16:08:01 -07003413
Florin Corasfe286f72021-06-04 10:07:55 -07003414 if (vcm->cfg.use_mq_eventfd)
3415 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3416 wait_for_time);
3417 else
3418 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3419 wait_for_time);
3420
Florin Corasfe286f72021-06-04 10:07:55 -07003421 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003422}
3423
Dave Wallace35830af2017-10-09 01:43:42 -04003424int
Florin Coras134a9962018-08-28 11:32:04 -07003425vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003426 void *buffer, uint32_t * buflen)
3427{
Florin Coras134a9962018-08-28 11:32:04 -07003428 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003429 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003430 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003431 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003432 vcl_session_t *session;
3433 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003434
Florin Coras134a9962018-08-28 11:32:04 -07003435 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003436 if (!session)
3437 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003438
Dave Wallace35830af2017-10-09 01:43:42 -04003439 switch (op)
3440 {
3441 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003442 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003443 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3444 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003445 break;
3446
Dave Wallace227867f2017-11-13 21:21:53 -05003447 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003448 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003449 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3450 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003451 break;
3452
3453 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003454 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003455 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003456 *flags =
3457 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003458 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003459 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003460 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003461 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3462 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003463 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003464 }
3465 else
3466 rv = VPPCOM_EINVAL;
3467 break;
3468
3469 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003470 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003471 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003472 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003473 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003474 else
Florin Corasac422d62020-10-19 20:51:36 -07003475 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003476
Florin Coras5e062572019-03-14 19:07:51 -07003477 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3478 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003479 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003480 }
3481 else
3482 rv = VPPCOM_EINVAL;
3483 break;
3484
3485 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003486 if (PREDICT_TRUE (buffer && buflen &&
3487 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003488 {
Florin Coras7e12d942018-06-27 14:32:43 -07003489 ep->is_ip4 = session->transport.is_ip4;
3490 ep->port = session->transport.rmt_port;
3491 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003492 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3493 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003494 else
Dave Barach178cf492018-11-13 16:34:13 -05003495 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3496 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003497 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003498 VDBG (1,
3499 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3500 "addr = %U, port %u",
3501 session_handle, ep->is_ip4, vcl_format_ip46_address,
3502 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003503 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3504 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003505 }
3506 else
3507 rv = VPPCOM_EINVAL;
3508 break;
3509
3510 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003511 if (PREDICT_TRUE (buffer && buflen &&
3512 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003513 {
Florin Coras7e12d942018-06-27 14:32:43 -07003514 ep->is_ip4 = session->transport.is_ip4;
3515 ep->port = session->transport.lcl_port;
3516 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003517 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3518 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003519 else
Dave Barach178cf492018-11-13 16:34:13 -05003520 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3521 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003522 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003523 VDBG (1,
3524 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3525 " port %d",
3526 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003527 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003528 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3529 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003530 }
3531 else
3532 rv = VPPCOM_EINVAL;
3533 break;
Stevenb5a11602017-10-11 09:59:30 -07003534
Florin Corasef7cbf62019-10-17 09:56:27 -07003535 case VPPCOM_ATTR_SET_LCL_ADDR:
3536 if (PREDICT_TRUE (buffer && buflen &&
3537 (*buflen >= sizeof (*ep)) && ep->ip))
3538 {
3539 session->transport.is_ip4 = ep->is_ip4;
3540 session->transport.lcl_port = ep->port;
3541 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3542 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003543 VDBG (1,
3544 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3545 " port %d",
3546 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003547 &session->transport.lcl_ip,
3548 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3549 clib_net_to_host_u16 (ep->port));
3550 }
3551 else
3552 rv = VPPCOM_EINVAL;
3553 break;
3554
Dave Wallace048b1d62018-01-03 22:24:41 -05003555 case VPPCOM_ATTR_GET_LIBC_EPFD:
3556 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003557 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003558 break;
3559
3560 case VPPCOM_ATTR_SET_LIBC_EPFD:
3561 if (PREDICT_TRUE (buffer && buflen &&
3562 (*buflen == sizeof (session->libc_epfd))))
3563 {
3564 session->libc_epfd = *(int *) buffer;
3565 *buflen = sizeof (session->libc_epfd);
3566
Florin Coras5e062572019-03-14 19:07:51 -07003567 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3568 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003569 }
3570 else
3571 rv = VPPCOM_EINVAL;
3572 break;
3573
3574 case VPPCOM_ATTR_GET_PROTOCOL:
3575 if (buffer && buflen && (*buflen >= sizeof (int)))
3576 {
Florin Coras7e12d942018-06-27 14:32:43 -07003577 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003578 *buflen = sizeof (int);
3579
Florin Coras5e062572019-03-14 19:07:51 -07003580 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3581 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003582 }
3583 else
3584 rv = VPPCOM_EINVAL;
3585 break;
3586
3587 case VPPCOM_ATTR_GET_LISTEN:
3588 if (buffer && buflen && (*buflen >= sizeof (int)))
3589 {
Florin Corasac422d62020-10-19 20:51:36 -07003590 *(int *) buffer = vcl_session_has_attr (session,
3591 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003592 *buflen = sizeof (int);
3593
Florin Coras5e062572019-03-14 19:07:51 -07003594 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3595 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003596 }
3597 else
3598 rv = VPPCOM_EINVAL;
3599 break;
3600
3601 case VPPCOM_ATTR_GET_ERROR:
3602 if (buffer && buflen && (*buflen >= sizeof (int)))
3603 {
3604 *(int *) buffer = 0;
3605 *buflen = sizeof (int);
3606
Florin Coras5e062572019-03-14 19:07:51 -07003607 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3608 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003609 }
3610 else
3611 rv = VPPCOM_EINVAL;
3612 break;
3613
3614 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3615 if (buffer && buflen && (*buflen >= sizeof (u32)))
3616 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003617
3618 /* VPP-TBD */
3619 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003620 session->tx_fifo ?
3621 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003622 vcm->cfg.tx_fifo_size);
3623 *buflen = sizeof (u32);
3624
Florin Coras5e062572019-03-14 19:07:51 -07003625 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3626 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3627 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003628 }
3629 else
3630 rv = VPPCOM_EINVAL;
3631 break;
3632
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003633 case VPPCOM_ATTR_SET_DSCP:
3634 if (buffer && buflen && (*buflen >= sizeof (u8)))
3635 {
3636 session->dscp = *(u8 *) buffer;
3637
3638 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3639 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3640 }
3641 else
3642 rv = VPPCOM_EINVAL;
3643 break;
3644
Dave Wallace048b1d62018-01-03 22:24:41 -05003645 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3646 if (buffer && buflen && (*buflen == sizeof (u32)))
3647 {
3648 /* VPP-TBD */
3649 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003650 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3651 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3652 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003653 }
3654 else
3655 rv = VPPCOM_EINVAL;
3656 break;
3657
3658 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3659 if (buffer && buflen && (*buflen >= sizeof (u32)))
3660 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003661
3662 /* VPP-TBD */
3663 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003664 session->rx_fifo ?
3665 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003666 vcm->cfg.rx_fifo_size);
3667 *buflen = sizeof (u32);
3668
Florin Coras5e062572019-03-14 19:07:51 -07003669 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3670 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003671 }
3672 else
3673 rv = VPPCOM_EINVAL;
3674 break;
3675
3676 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3677 if (buffer && buflen && (*buflen == sizeof (u32)))
3678 {
3679 /* VPP-TBD */
3680 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003681 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3682 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3683 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003684 }
3685 else
3686 rv = VPPCOM_EINVAL;
3687 break;
3688
3689 case VPPCOM_ATTR_GET_REUSEADDR:
3690 if (buffer && buflen && (*buflen >= sizeof (int)))
3691 {
3692 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003693 *(int *) buffer = vcl_session_has_attr (session,
3694 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003695 *buflen = sizeof (int);
3696
Florin Coras5e062572019-03-14 19:07:51 -07003697 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3698 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003699 }
3700 else
3701 rv = VPPCOM_EINVAL;
3702 break;
3703
Stevenb5a11602017-10-11 09:59:30 -07003704 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003705 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003706 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003707 {
3708 /* VPP-TBD */
3709 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003710 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003711 else
Florin Corasac422d62020-10-19 20:51:36 -07003712 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003713
Florin Coras5e062572019-03-14 19:07:51 -07003714 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003715 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003716 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003717 }
3718 else
3719 rv = VPPCOM_EINVAL;
3720 break;
3721
3722 case VPPCOM_ATTR_GET_REUSEPORT:
3723 if (buffer && buflen && (*buflen >= sizeof (int)))
3724 {
3725 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003726 *(int *) buffer = vcl_session_has_attr (session,
3727 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003728 *buflen = sizeof (int);
3729
Florin Coras5e062572019-03-14 19:07:51 -07003730 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3731 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003732 }
3733 else
3734 rv = VPPCOM_EINVAL;
3735 break;
3736
3737 case VPPCOM_ATTR_SET_REUSEPORT:
3738 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003739 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003740 {
3741 /* VPP-TBD */
3742 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003743 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003744 else
Florin Corasac422d62020-10-19 20:51:36 -07003745 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003746
Florin Coras5e062572019-03-14 19:07:51 -07003747 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003748 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003749 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003750 }
3751 else
3752 rv = VPPCOM_EINVAL;
3753 break;
3754
3755 case VPPCOM_ATTR_GET_BROADCAST:
3756 if (buffer && buflen && (*buflen >= sizeof (int)))
3757 {
3758 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003759 *(int *) buffer = vcl_session_has_attr (session,
3760 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003761 *buflen = sizeof (int);
3762
Florin Coras5e062572019-03-14 19:07:51 -07003763 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3764 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003765 }
3766 else
3767 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003768 break;
3769
3770 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003771 if (buffer && buflen && (*buflen == sizeof (int)))
3772 {
3773 /* VPP-TBD */
3774 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003775 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003776 else
Florin Corasac422d62020-10-19 20:51:36 -07003777 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003778
Florin Coras5e062572019-03-14 19:07:51 -07003779 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003780 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003781 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003782 }
3783 else
3784 rv = VPPCOM_EINVAL;
3785 break;
3786
3787 case VPPCOM_ATTR_GET_V6ONLY:
3788 if (buffer && buflen && (*buflen >= sizeof (int)))
3789 {
3790 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003791 *(int *) buffer = vcl_session_has_attr (session,
3792 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003793 *buflen = sizeof (int);
3794
Florin Coras5e062572019-03-14 19:07:51 -07003795 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3796 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003797 }
3798 else
3799 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003800 break;
3801
3802 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003803 if (buffer && buflen && (*buflen == sizeof (int)))
3804 {
3805 /* VPP-TBD */
3806 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003807 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003808 else
Florin Corasac422d62020-10-19 20:51:36 -07003809 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003810
Florin Coras5e062572019-03-14 19:07:51 -07003811 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003812 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003813 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003814 }
3815 else
3816 rv = VPPCOM_EINVAL;
3817 break;
3818
3819 case VPPCOM_ATTR_GET_KEEPALIVE:
3820 if (buffer && buflen && (*buflen >= sizeof (int)))
3821 {
3822 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003823 *(int *) buffer = vcl_session_has_attr (session,
3824 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003825 *buflen = sizeof (int);
3826
Florin Coras5e062572019-03-14 19:07:51 -07003827 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3828 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003829 }
3830 else
3831 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003832 break;
Stevenbccd3392017-10-12 20:42:21 -07003833
3834 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003835 if (buffer && buflen && (*buflen == sizeof (int)))
3836 {
3837 /* VPP-TBD */
3838 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003839 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003840 else
Florin Corasac422d62020-10-19 20:51:36 -07003841 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003842
Florin Coras5e062572019-03-14 19:07:51 -07003843 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003844 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003845 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003846 }
3847 else
3848 rv = VPPCOM_EINVAL;
3849 break;
3850
3851 case VPPCOM_ATTR_GET_TCP_NODELAY:
3852 if (buffer && buflen && (*buflen >= sizeof (int)))
3853 {
3854 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003855 *(int *) buffer = vcl_session_has_attr (session,
3856 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003857 *buflen = sizeof (int);
3858
Florin Coras5e062572019-03-14 19:07:51 -07003859 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3860 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003861 }
3862 else
3863 rv = VPPCOM_EINVAL;
3864 break;
3865
3866 case VPPCOM_ATTR_SET_TCP_NODELAY:
3867 if (buffer && buflen && (*buflen == sizeof (int)))
3868 {
3869 /* VPP-TBD */
3870 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003871 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003872 else
Florin Corasac422d62020-10-19 20:51:36 -07003873 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003874
Florin Coras5e062572019-03-14 19:07:51 -07003875 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003876 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003877 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003878 }
3879 else
3880 rv = VPPCOM_EINVAL;
3881 break;
3882
3883 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3884 if (buffer && buflen && (*buflen >= sizeof (int)))
3885 {
3886 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003887 *(int *) buffer = vcl_session_has_attr (session,
3888 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003889 *buflen = sizeof (int);
3890
Florin Coras5e062572019-03-14 19:07:51 -07003891 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3892 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003893 }
3894 else
3895 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003896 break;
3897
3898 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003899 if (buffer && buflen && (*buflen == sizeof (int)))
3900 {
3901 /* VPP-TBD */
3902 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003903 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003904 else
Florin Corasac422d62020-10-19 20:51:36 -07003905 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003906
Florin Coras5e062572019-03-14 19:07:51 -07003907 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003908 vcl_session_has_attr (session,
3909 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003910 }
3911 else
3912 rv = VPPCOM_EINVAL;
3913 break;
3914
3915 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3916 if (buffer && buflen && (*buflen >= sizeof (int)))
3917 {
3918 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003919 *(int *) buffer = vcl_session_has_attr (session,
3920 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003921 *buflen = sizeof (int);
3922
Florin Coras5e062572019-03-14 19:07:51 -07003923 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3924 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003925 }
3926 else
3927 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003928 break;
3929
3930 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003931 if (buffer && buflen && (*buflen == sizeof (int)))
3932 {
3933 /* VPP-TBD */
3934 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003935 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003936 else
Florin Corasac422d62020-10-19 20:51:36 -07003937 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003938
Florin Coras5e062572019-03-14 19:07:51 -07003939 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003940 vcl_session_has_attr (session,
3941 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003942 }
3943 else
3944 rv = VPPCOM_EINVAL;
3945 break;
3946
3947 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003948 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003949 {
Florin Coras04ae8272021-04-12 19:55:37 -07003950 rv = VPPCOM_EINVAL;
3951 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003952 }
Florin Coras04ae8272021-04-12 19:55:37 -07003953
3954 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3955 tea.mss = *(u32 *) buffer;
3956 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3957 rv = VPPCOM_ENOPROTOOPT;
3958
3959 if (!rv)
3960 {
3961 *(u32 *) buffer = tea.mss;
3962 *buflen = sizeof (int);
3963 }
3964
3965 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3966 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003967 break;
3968
3969 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003970 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003971 {
Florin Coras04ae8272021-04-12 19:55:37 -07003972 rv = VPPCOM_EINVAL;
3973 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003974 }
Florin Coras04ae8272021-04-12 19:55:37 -07003975
3976 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3977 tea.mss = *(u32 *) buffer;
3978 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3979 rv = VPPCOM_ENOPROTOOPT;
3980
3981 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3982 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003983 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003984
Florin Coras1e966172020-05-16 18:18:14 +00003985 case VPPCOM_ATTR_SET_CONNECTED:
3986 session->flags |= VCL_SESSION_F_CONNECTED;
3987 break;
3988
Florin Corasa5a9efd2021-01-05 17:03:29 -08003989 case VPPCOM_ATTR_SET_CKPAIR:
3990 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3991 !vcl_session_has_crypto (session))
3992 {
3993 rv = VPPCOM_EINVAL;
3994 break;
3995 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003996 if (!session->ext_config)
3997 {
Florin Coras87f63892021-05-01 16:01:40 -07003998 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3999 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004000 }
4001 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4002 {
4003 rv = VPPCOM_EINVAL;
4004 break;
4005 }
4006
4007 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004008 break;
4009
Florin Coras6a6555a2021-01-28 11:39:27 -08004010 case VPPCOM_ATTR_SET_VRF:
4011 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4012 {
4013 rv = VPPCOM_EINVAL;
4014 break;
4015 }
4016 session->vrf = *(u32 *) buffer;
4017 break;
4018
4019 case VPPCOM_ATTR_GET_VRF:
4020 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4021 {
4022 rv = VPPCOM_EINVAL;
4023 break;
4024 }
4025 *(u32 *) buffer = session->vrf;
4026 *buflen = sizeof (u32);
4027 break;
4028
wanghanlin0674f852021-02-22 10:38:36 +08004029 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004030 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004031 {
Florin Corasd77325c2021-02-23 12:03:03 -08004032 rv = VPPCOM_EINVAL;
4033 break;
wanghanlin0674f852021-02-22 10:38:36 +08004034 }
Florin Corasd77325c2021-02-23 12:03:03 -08004035
4036 if (session->transport.is_ip4)
4037 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004038 else
Florin Corasd77325c2021-02-23 12:03:03 -08004039 *(int *) buffer = AF_INET6;
4040 *buflen = sizeof (int);
4041
wanghanlin0674f852021-02-22 10:38:36 +08004042 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4043 *buflen);
4044 break;
4045
Florin Coras87f63892021-05-01 16:01:40 -07004046 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4047 if (!(buffer && buflen && (*buflen > 0)))
4048 {
4049 rv = VPPCOM_EINVAL;
4050 break;
4051 }
4052 if (session->ext_config)
4053 {
4054 rv = VPPCOM_EINVAL;
4055 break;
4056 }
4057 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4058 *buflen + sizeof (u32));
4059 clib_memcpy (session->ext_config->data, buffer, *buflen);
4060 session->ext_config->len = *buflen;
4061 break;
4062
Dave Wallacee22aa742017-10-20 12:30:38 -04004063 default:
4064 rv = VPPCOM_EINVAL;
4065 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004066 }
4067
Dave Wallace35830af2017-10-09 01:43:42 -04004068 return rv;
4069}
4070
Stevenac1f96d2017-10-24 16:03:58 -07004071int
Florin Coras134a9962018-08-28 11:32:04 -07004072vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004073 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4074{
Florin Coras134a9962018-08-28 11:32:04 -07004075 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004076 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004077 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004078
4079 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004080 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004081 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004082 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004083 else
4084 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004085 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004086 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004087 }
4088
Florin Coras0a1e1832020-03-29 18:54:04 +00004089 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004090 {
Florin Coras5da10c42020-04-01 04:31:21 +00004091 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004092 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004093 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4094 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004095 else
Dave Barach178cf492018-11-13 16:34:13 -05004096 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4097 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004098 ep->is_ip4 = session->transport.is_ip4;
4099 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004100 }
Florin Coras460dce62018-07-27 05:45:06 -07004101
Stevenac1f96d2017-10-24 16:03:58 -07004102 return rv;
4103}
4104
4105int
Florin Coras134a9962018-08-28 11:32:04 -07004106vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004107 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4108{
Florin Coras7a2abce2020-04-05 19:25:44 +00004109 vcl_worker_t *wrk = vcl_worker_get_current ();
4110 vcl_session_t *s;
4111
4112 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004113 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004114 return VPPCOM_EBADFD;
4115
Dave Wallace617dffa2017-10-26 14:47:06 -04004116 if (ep)
4117 {
Florin Coras5824cc52020-10-20 18:44:41 -07004118 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004119 return VPPCOM_EINVAL;
4120
liuyacanbc0c7542021-08-02 20:15:05 +08004121 s->transport.is_ip4 = ep->is_ip4;
4122 s->transport.rmt_port = ep->port;
4123 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4124
Florin Coras5da10c42020-04-01 04:31:21 +00004125 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004126 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004127 {
Florin Coras5824cc52020-10-20 18:44:41 -07004128 u32 session_index = s->session_index;
4129 f64 timeout = vcm->cfg.session_timeout;
4130 int rv;
4131
Florin Coras5da10c42020-04-01 04:31:21 +00004132 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004133 rv = vppcom_wait_for_session_state_change (session_index,
4134 VCL_STATE_READY,
4135 timeout);
4136 if (rv < 0)
4137 return rv;
4138 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004139 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004140 }
4141
4142 if (flags)
4143 {
4144 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004145 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004146 }
4147
Florin Coras7a2abce2020-04-05 19:25:44 +00004148 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4149 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004150}
4151
Dave Wallace048b1d62018-01-03 22:24:41 -05004152int
4153vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4154{
Florin Coras134a9962018-08-28 11:32:04 -07004155 vcl_worker_t *wrk = vcl_worker_get_current ();
4156 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004157 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004158 svm_msg_q_msg_t msg;
4159 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004160 int rv, num_ev = 0;
4161
Florin Coras5e062572019-03-14 19:07:51 -07004162 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004163
4164 if (!vp)
4165 return VPPCOM_EFAULT;
4166
4167 do
4168 {
Florin Coras7e12d942018-06-27 14:32:43 -07004169 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004170
Florin Coras6917b942018-11-13 22:44:54 -08004171 /* Dequeue all events and drop all unhandled io events */
4172 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4173 {
4174 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4175 vcl_handle_mq_event (wrk, e);
4176 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4177 }
4178 vec_reset_length (wrk->unhandled_evts_vector);
4179
Dave Wallace048b1d62018-01-03 22:24:41 -05004180 for (i = 0; i < n_sids; i++)
4181 {
Florin Coras7baeb712019-01-04 17:05:43 -08004182 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004183 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004184 {
4185 vp[i].revents = POLLHUP;
4186 num_ev++;
4187 continue;
4188 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004189
Florin Coras6917b942018-11-13 22:44:54 -08004190 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004191
4192 if (POLLIN & vp[i].events)
4193 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004194 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004195 if (rv > 0)
4196 {
Florin Coras6917b942018-11-13 22:44:54 -08004197 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004198 num_ev++;
4199 }
4200 else if (rv < 0)
4201 {
4202 switch (rv)
4203 {
4204 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004205 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004206 break;
4207
4208 default:
Florin Coras6917b942018-11-13 22:44:54 -08004209 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004210 break;
4211 }
4212 num_ev++;
4213 }
4214 }
4215
4216 if (POLLOUT & vp[i].events)
4217 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004218 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004219 if (rv > 0)
4220 {
Florin Coras6917b942018-11-13 22:44:54 -08004221 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004222 num_ev++;
4223 }
4224 else if (rv < 0)
4225 {
4226 switch (rv)
4227 {
4228 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004229 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004230 break;
4231
4232 default:
Florin Coras6917b942018-11-13 22:44:54 -08004233 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004234 break;
4235 }
4236 num_ev++;
4237 }
4238 }
4239
Dave Wallace7e607a72018-06-18 18:41:32 -04004240 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004241 {
Florin Coras6917b942018-11-13 22:44:54 -08004242 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004243 num_ev++;
4244 }
4245 }
4246 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004247 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004248 }
4249 while ((num_ev == 0) && keep_trying);
4250
Dave Wallace048b1d62018-01-03 22:24:41 -05004251 return num_ev;
4252}
4253
Florin Coras99368312018-08-02 10:45:44 -07004254int
4255vppcom_mq_epoll_fd (void)
4256{
Florin Coras134a9962018-08-28 11:32:04 -07004257 vcl_worker_t *wrk = vcl_worker_get_current ();
4258 return wrk->mqs_epfd;
4259}
4260
4261int
Florin Coras30e79c22019-01-02 19:31:22 -08004262vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004263{
4264 return session_handle & 0xFFFFFF;
4265}
4266
4267int
Florin Coras30e79c22019-01-02 19:31:22 -08004268vppcom_session_worker (vcl_session_handle_t session_handle)
4269{
4270 return session_handle >> 24;
4271}
4272
4273int
Florin Coras134a9962018-08-28 11:32:04 -07004274vppcom_worker_register (void)
4275{
Florin Coras47c40e22018-11-26 17:01:36 -08004276 if (!vcl_worker_alloc_and_init ())
4277 return VPPCOM_EEXIST;
4278
Florin Coras47c40e22018-11-26 17:01:36 -08004279 if (vcl_worker_register_with_vpp ())
4280 return VPPCOM_EEXIST;
4281
4282 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004283}
4284
Florin Coras369db832019-07-08 12:34:45 -07004285void
4286vppcom_worker_unregister (void)
4287{
4288 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4289 vcl_set_worker_index (~0);
4290}
4291
Pivo6017ff02020-05-04 17:57:33 +02004292void
4293vppcom_worker_index_set (int index)
4294{
4295 vcl_set_worker_index (index);
4296}
4297
Florin Corasdfe4cf42018-11-28 22:13:45 -08004298int
4299vppcom_worker_index (void)
4300{
4301 return vcl_get_worker_index ();
4302}
4303
Florin Corase0982e52019-01-25 13:19:56 -08004304int
4305vppcom_worker_mqs_epfd (void)
4306{
4307 vcl_worker_t *wrk = vcl_worker_get_current ();
4308 if (!vcm->cfg.use_mq_eventfd)
4309 return -1;
4310 return wrk->mqs_epfd;
4311}
4312
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004313int
4314vppcom_session_is_connectable_listener (uint32_t session_handle)
4315{
4316 vcl_session_t *session;
4317 vcl_worker_t *wrk = vcl_worker_get_current ();
4318 session = vcl_session_get_w_handle (wrk, session_handle);
4319 if (!session)
4320 return VPPCOM_EBADFD;
4321 return vcl_session_is_connectable_listener (wrk, session);
4322}
4323
4324int
4325vppcom_session_listener (uint32_t session_handle)
4326{
4327 vcl_worker_t *wrk = vcl_worker_get_current ();
4328 vcl_session_t *listen_session, *session;
4329 session = vcl_session_get_w_handle (wrk, session_handle);
4330 if (!session)
4331 return VPPCOM_EBADFD;
4332 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4333 return VPPCOM_EBADFD;
4334 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4335 if (!listen_session)
4336 return VPPCOM_EBADFD;
4337 return vcl_session_handle (listen_session);
4338}
4339
4340int
4341vppcom_session_n_accepted (uint32_t session_handle)
4342{
4343 vcl_worker_t *wrk = vcl_worker_get_current ();
4344 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4345 if (!session)
4346 return VPPCOM_EBADFD;
4347 return session->n_accepted_sessions;
4348}
4349
Florin Coras66ec4672020-06-15 07:59:40 -07004350const char *
4351vppcom_proto_str (vppcom_proto_t proto)
4352{
4353 char const *proto_str;
4354
4355 switch (proto)
4356 {
4357 case VPPCOM_PROTO_TCP:
4358 proto_str = "TCP";
4359 break;
4360 case VPPCOM_PROTO_UDP:
4361 proto_str = "UDP";
4362 break;
4363 case VPPCOM_PROTO_TLS:
4364 proto_str = "TLS";
4365 break;
4366 case VPPCOM_PROTO_QUIC:
4367 proto_str = "QUIC";
4368 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004369 case VPPCOM_PROTO_DTLS:
4370 proto_str = "DTLS";
4371 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004372 case VPPCOM_PROTO_SRTP:
4373 proto_str = "SRTP";
4374 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004375 default:
4376 proto_str = "UNKNOWN";
4377 break;
4378 }
4379 return proto_str;
4380}
4381
4382const char *
4383vppcom_retval_str (int retval)
4384{
4385 char const *st;
4386
4387 switch (retval)
4388 {
4389 case VPPCOM_OK:
4390 st = "VPPCOM_OK";
4391 break;
4392
4393 case VPPCOM_EAGAIN:
4394 st = "VPPCOM_EAGAIN";
4395 break;
4396
4397 case VPPCOM_EFAULT:
4398 st = "VPPCOM_EFAULT";
4399 break;
4400
4401 case VPPCOM_ENOMEM:
4402 st = "VPPCOM_ENOMEM";
4403 break;
4404
4405 case VPPCOM_EINVAL:
4406 st = "VPPCOM_EINVAL";
4407 break;
4408
4409 case VPPCOM_EBADFD:
4410 st = "VPPCOM_EBADFD";
4411 break;
4412
4413 case VPPCOM_EAFNOSUPPORT:
4414 st = "VPPCOM_EAFNOSUPPORT";
4415 break;
4416
4417 case VPPCOM_ECONNABORTED:
4418 st = "VPPCOM_ECONNABORTED";
4419 break;
4420
4421 case VPPCOM_ECONNRESET:
4422 st = "VPPCOM_ECONNRESET";
4423 break;
4424
4425 case VPPCOM_ENOTCONN:
4426 st = "VPPCOM_ENOTCONN";
4427 break;
4428
4429 case VPPCOM_ECONNREFUSED:
4430 st = "VPPCOM_ECONNREFUSED";
4431 break;
4432
4433 case VPPCOM_ETIMEDOUT:
4434 st = "VPPCOM_ETIMEDOUT";
4435 break;
4436
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304437 case VPPCOM_EADDRINUSE:
4438 st = "VPPCOM_EADDRINUSE";
4439 break;
4440
Florin Coras66ec4672020-06-15 07:59:40 -07004441 default:
4442 st = "UNKNOWN_STATE";
4443 break;
4444 }
4445
4446 return st;
4447}
4448
Florin Corasa5a9efd2021-01-05 17:03:29 -08004449int
4450vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4451{
4452 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004453 return vcl_sapi_add_cert_key_pair (ckpair);
4454 else
4455 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004456}
4457
4458int
4459vppcom_del_cert_key_pair (uint32_t ckpair_index)
4460{
4461 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004462 return vcl_sapi_del_cert_key_pair (ckpair_index);
4463 else
4464 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004465}
4466
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304467int
4468vppcom_session_get_error (uint32_t session_handle)
4469{
4470 vcl_worker_t *wrk = vcl_worker_get_current ();
4471 vcl_session_t *session = 0;
4472
4473 session = vcl_session_get_w_handle (wrk, session_handle);
4474 if (!session)
4475 return VPPCOM_EBADFD;
4476
4477 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4478 {
4479 VWRN ("epoll session %u! will not have connect", session->session_index);
4480 return VPPCOM_EBADFD;
4481 }
4482
4483 if (session->vpp_error == SESSION_E_PORTINUSE)
4484 return VPPCOM_EADDRINUSE;
4485 else if (session->vpp_error == SESSION_E_REFUSED)
4486 return VPPCOM_ECONNREFUSED;
4487 else if (session->vpp_error != SESSION_E_NONE)
4488 return VPPCOM_EFAULT;
4489 else
4490 return VPPCOM_OK;
4491}
4492
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004493int
4494vppcom_worker_is_detached (void)
4495{
4496 vcl_worker_t *wrk = vcl_worker_get_current ();
4497
4498 if (!vcm->cfg.use_mq_eventfd)
4499 return VPPCOM_ENOTSUP;
4500
4501 return wrk->api_client_handle == ~0;
4502}
4503
Dave Wallacee22aa742017-10-20 12:30:38 -04004504/*
4505 * fd.io coding-style-patch-verification: ON
4506 *
4507 * Local Variables:
4508 * eval: (c-set-style "gnu")
4509 * End:
4510 */