blob: 05b84d4674bdabb571f845ae9f090bc73a20fb4e [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;
Florin Coras5ffb9642021-12-03 17:24:13 -0800519 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700520 return sid;
521}
522
Florin Coras60116992018-08-27 09:52:18 -0700523static u32
Florin Coras134a9962018-08-28 11:32:04 -0700524vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700525{
526 vcl_session_t *session;
527 u32 sid = mp->context;
528
Florin Coras134a9962018-08-28 11:32:04 -0700529 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700530 if (mp->retval)
531 {
Florin Coras5e062572019-03-14 19:07:51 -0700532 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700533 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700534 if (session)
535 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700536 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700537 session->vpp_handle = mp->handle;
538 return sid;
539 }
540 else
541 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800542 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
543 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700544 return VCL_INVALID_SESSION_INDEX;
545 }
546 }
547
548 session->vpp_handle = mp->handle;
549 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500550 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
551 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700552 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700553 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700554 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800555
Florin Coras6e3c1f82020-01-15 01:30:46 +0000556 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700557 {
Florin Corasc547e912020-12-08 17:50:45 -0800558 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700559 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
560 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800561 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800562 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
563 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800564 session->session_state = VCL_STATE_DETACHED;
565 return VCL_INVALID_SESSION_INDEX;
566 }
Florin Coras60116992018-08-27 09:52:18 -0700567 }
568
Florin Coras05ecfcc2018-12-12 18:19:39 -0800569 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700570 return sid;
571}
572
Florin Corasdfae9f92019-02-20 19:48:31 -0800573static void
574vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
575{
576 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
577 vcl_session_t *s;
578
579 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000580 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800581 {
582 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
583 return;
584 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700585 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000586 {
587 /* Connected udp listener */
588 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700589 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000590 return;
591
592 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
593 return;
594 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800595
596 if (mp->retval)
597 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700598 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800599
600 if (mp->context != wrk->wrk_index)
601 VDBG (0, "wrong context");
602
603 vcl_session_table_del_vpp_handle (wrk, mp->handle);
604 vcl_session_free (wrk, s);
605}
606
Florin Coras68b7e582020-01-21 18:33:23 -0800607static void
608vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
609{
610 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
611 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800612 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800613
614 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
615 if (!s)
616 {
617 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
618 return;
619 }
620
Florin Coras1bb74942021-02-10 15:26:37 -0800621 /* Only validate if a value is provided */
622 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800623 {
Florin Coras1bb74942021-02-10 15:26:37 -0800624 fs_index = vcl_segment_table_lookup (mp->segment_handle);
625 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
626 {
627 VDBG (0, "segment %lx for session %u is not mounted!",
628 mp->segment_handle, s->session_index);
629 s->session_state = VCL_STATE_DETACHED;
630 return;
631 }
Florin Corasc547e912020-12-08 17:50:45 -0800632 }
633
Florin Coras57660d92020-04-04 22:45:34 +0000634 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800635
636 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
637 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800638
Florin Coras68b7e582020-01-21 18:33:23 -0800639 vcl_session_table_del_vpp_handle (wrk, mp->handle);
640 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
641
642 /* Generate new tx event if we have outstanding data */
643 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800644 app_send_io_evt_to_vpp (s->vpp_evt_q,
645 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800646 SESSION_IO_EVT_TX, SVM_Q_WAIT);
647
Florin Coras57660d92020-04-04 22:45:34 +0000648 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800649 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800650}
651
Florin Coras3c7d4f92018-12-14 11:28:43 -0800652static vcl_session_t *
653vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
654{
655 vcl_session_msg_t *vcl_msg;
656 vcl_session_t *session;
657
658 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
659 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800660 VWRN ("session overlap handle %lu state %u!", msg->handle,
661 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800662
663 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
664 if (!session)
665 {
666 VERR ("couldn't find listen session: listener handle %llx",
667 msg->listener_handle);
668 return 0;
669 }
670
671 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000672 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800673 vcl_msg->accepted_msg = *msg;
674 /* Session handle points to listener until fully accepted by app */
675 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
676
677 return session;
678}
679
680static vcl_session_t *
681vcl_session_disconnected_handler (vcl_worker_t * wrk,
682 session_disconnected_msg_t * msg)
683{
684 vcl_session_t *session;
685
686 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
687 if (!session)
688 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800689 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800690 return 0;
691 }
692
Florin Corasadcfb152020-02-12 08:50:29 +0000693 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700694 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000695 return 0;
696
Florin Coras3c7d4f92018-12-14 11:28:43 -0800697 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700698 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800699 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800700 if (!vcl_flag_accepted_session (session, msg->handle,
701 VCL_ACCEPTED_F_CLOSED))
702 VDBG (0, "session was not accepted!");
703 return 0;
704 }
705
Florin Corasadcfb152020-02-12 08:50:29 +0000706 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700707 if (session->session_state != VCL_STATE_DISCONNECT)
708 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000709
Florin Coras3c7d4f92018-12-14 11:28:43 -0800710 return session;
711}
712
liuyacan534468e2021-05-09 03:50:40 +0000713int
liuyacan55c952e2021-06-13 14:54:55 +0800714vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000715{
716 vcl_worker_t *wrk = vcl_worker_get_current ();
717 vcl_session_t *session;
718 vcl_session_state_t state;
719 u64 vpp_handle;
720
721 session = vcl_session_get_w_handle (wrk, session_handle);
722 if (PREDICT_FALSE (!session))
723 return VPPCOM_EBADFD;
724
725 vpp_handle = session->vpp_handle;
726 state = session->session_state;
727
728 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800729 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000730
731 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
732 {
733 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
734 return VPPCOM_EBADFD;
735 }
736
liuyacan55c952e2021-06-13 14:54:55 +0800737 if (how == SHUT_RD || how == SHUT_RDWR)
738 {
739 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
740 if (how == SHUT_RD)
741 return VPPCOM_OK;
742 }
743 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
744
liuyacan534468e2021-05-09 03:50:40 +0000745 if (PREDICT_TRUE (state == VCL_STATE_READY))
746 {
747 VDBG (1, "session %u [0x%llx]: sending shutdown...",
748 session->session_index, vpp_handle);
749
750 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000751 }
752
753 return VPPCOM_OK;
754}
755
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700756static int
757vppcom_session_disconnect (u32 session_handle)
758{
759 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700760 vcl_session_t *session, *listen_session;
761 vcl_session_state_t state;
762 u64 vpp_handle;
763
764 session = vcl_session_get_w_handle (wrk, session_handle);
765 if (!session)
766 return VPPCOM_EBADFD;
767
768 vpp_handle = session->vpp_handle;
769 state = session->session_state;
770
Florin Coras5ffb9642021-12-03 17:24:13 -0800771 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
772 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700773
774 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
775 {
776 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
777 return VPPCOM_EBADFD;
778 }
779
780 if (state == VCL_STATE_VPP_CLOSING)
781 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800782 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700783 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
784 session->session_index, vpp_handle);
785 }
786 else
787 {
788 /* Session doesn't have an event queue yet. Probably a non-blocking
789 * connect. Wait for the reply */
790 if (PREDICT_FALSE (!session->vpp_evt_q))
791 return VPPCOM_OK;
792
Florin Coras5ffb9642021-12-03 17:24:13 -0800793 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700794 session->session_index, vpp_handle);
795 vcl_send_session_disconnect (wrk, session);
796 }
797
798 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
799 {
800 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700801 if (listen_session)
802 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700803 }
804
805 return VPPCOM_OK;
806}
807
Florin Coras30e79c22019-01-02 19:31:22 -0800808static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700809vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
810{
811 session_cleanup_msg_t *msg;
812 vcl_session_t *session;
813
814 msg = (session_cleanup_msg_t *) data;
815 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
816 if (!session)
817 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800818 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700819 return;
820 }
821
Florin Coras36d49392020-04-24 23:00:11 +0000822 if (msg->type == SESSION_CLEANUP_TRANSPORT)
823 {
824 /* Transport was cleaned up before we confirmed close. Probably the
825 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700826 * Confirm close to make sure everything is cleaned up.
827 * Move to undetermined state to ensure that the session is not
828 * removed before both vpp and the app cleanup.
829 * - If the app closes first, the session is moved to CLOSED state
830 * and the session cleanup notification from vpp removes the
831 * session.
832 * - If vpp cleans up the session first, the session is moved to
833 * DETACHED state lower and subsequently the close from the app
834 * frees the session
835 */
836 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700837 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700838 vppcom_session_disconnect (vcl_session_handle (session));
839 session->session_state = VCL_STATE_UPDATED;
840 }
841 else if (session->session_state == VCL_STATE_DISCONNECT)
842 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800843 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700844 session->session_state = VCL_STATE_UPDATED;
845 }
Florin Coras36d49392020-04-24 23:00:11 +0000846 return;
847 }
848
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800849 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700850 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800851
852 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700853 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000854 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800855 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700856 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000857 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
858 return;
859 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800860
861 /* Session probably tracked with epoll, disconnect not yet handled and
862 * 1) both transport and session cleanup completed 2) app closed. Wait
863 * until message is drained to free the session.
864 * See @ref vcl_handle_mq_event */
865 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
866 {
867 session->flags |= VCL_SESSION_F_PENDING_FREE;
868 return;
869 }
870
Florin Coras9ace36d2019-10-28 13:14:17 -0700871 vcl_session_free (wrk, session);
872}
873
874static void
Florin Coras30e79c22019-01-02 19:31:22 -0800875vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
876{
877 session_req_worker_update_msg_t *msg;
878 vcl_session_t *s;
879
880 msg = (session_req_worker_update_msg_t *) data;
881 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
882 if (!s)
883 return;
884
885 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
886}
887
888static void
889vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
890{
891 session_worker_update_reply_msg_t *msg;
892 vcl_session_t *s;
893
894 msg = (session_worker_update_reply_msg_t *) data;
895 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
896 if (!s)
897 {
898 VDBG (0, "unknown handle 0x%llx", msg->handle);
899 return;
900 }
Florin Coras30e79c22019-01-02 19:31:22 -0800901
Florin Coras5f45e012019-01-23 09:21:30 -0800902 if (s->rx_fifo)
903 {
Florin Corasc547e912020-12-08 17:50:45 -0800904 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700905 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800906 {
907 VDBG (0, "failed to attach fifos for %u", s->session_index);
908 return;
909 }
Florin Coras5f45e012019-01-23 09:21:30 -0800910 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700911 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800912
Florin Coras30e79c22019-01-02 19:31:22 -0800913 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
914 s->vpp_handle, wrk->wrk_index);
915}
916
Florin Coras935ce752020-09-08 22:43:47 -0700917static int
918vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
919{
920
921 if (vcm->cfg.vpp_app_socket_api)
922 return vcl_sapi_recv_fds (wrk, fds, n_fds);
923
924 return vcl_bapi_recv_fds (wrk, fds, n_fds);
925}
926
Florin Corasc4c4cf52019-08-24 18:17:34 -0700927static void
928vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
929{
930 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
931 session_app_add_segment_msg_t *msg;
932 u64 segment_handle;
933 int fd = -1;
934
935 msg = (session_app_add_segment_msg_t *) data;
936
937 if (msg->fd_flags)
938 {
Florin Coras935ce752020-09-08 22:43:47 -0700939 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700940 seg_type = SSVM_SEGMENT_MEMFD;
941 }
942
943 segment_handle = msg->segment_handle;
944 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
945 {
946 clib_warning ("invalid segment handle");
947 return;
948 }
949
950 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
951 seg_type, fd))
952 {
953 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
954 return;
955 }
956
957 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
958 msg->segment_size);
959}
960
961static void
962vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
963{
964 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
965 vcl_segment_detach (msg->segment_handle);
966 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
967}
968
Florin Coras40c07ce2020-07-16 20:46:17 -0700969static void
970vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
971{
972 if (!vcm->wrk_rpc_fn)
973 return;
974
hanlina3a48962020-07-13 11:09:15 +0800975 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700976}
977
Florin Coras04ae8272021-04-12 19:55:37 -0700978static void
979vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
980{
981 session_transport_attr_reply_msg_t *mp;
982
983 if (!wrk->session_attr_op)
984 return;
985
986 mp = (session_transport_attr_reply_msg_t *) data;
987
988 wrk->session_attr_op_rv = mp->retval;
989 wrk->session_attr_op = 0;
990 wrk->session_attr_rv = mp->attr;
991}
992
Florin Coras86f04502018-09-12 16:08:01 -0700993static int
994vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700995{
Florin Coras54693d22018-07-17 10:46:29 -0700996 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700997 session_connected_msg_t *connected_msg;
998 session_reset_msg_t *reset_msg;
999 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001000 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001001 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001002
1003 switch (e->event_type)
1004 {
Florin Coras653e43f2019-03-04 10:56:23 -08001005 case SESSION_IO_EVT_RX:
1006 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001007 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001008 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001009 break;
Florin Coras86f04502018-09-12 16:08:01 -07001010 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001011 break;
Florin Corasb242d312020-10-26 15:35:40 -07001012 case SESSION_CTRL_EVT_BOUND:
1013 /* We can only wait for only one listen so not postponed */
1014 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1015 break;
Florin Coras54693d22018-07-17 10:46:29 -07001016 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001017 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1018 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1019 {
1020 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1021 *ecpy = *e;
1022 ecpy->postponed = 1;
1023 ecpy->session_index = s->session_index;
1024 }
Florin Coras54693d22018-07-17 10:46:29 -07001025 break;
1026 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001027 connected_msg = (session_connected_msg_t *) e->data;
1028 sid = vcl_session_connected_handler (wrk, connected_msg);
1029 if (!(s = vcl_session_get (wrk, sid)))
1030 break;
1031 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1032 {
1033 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1034 *ecpy = *e;
1035 ecpy->postponed = 1;
1036 ecpy->session_index = s->session_index;
1037 }
Florin Coras54693d22018-07-17 10:46:29 -07001038 break;
1039 case SESSION_CTRL_EVT_DISCONNECTED:
1040 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001041 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1042 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001043 if (s->session_state == VCL_STATE_CLOSED)
1044 break;
Florin Corasb242d312020-10-26 15:35:40 -07001045 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1046 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001047 s->session_state = VCL_STATE_VPP_CLOSING;
1048 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1049 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1050 *ecpy = *e;
1051 ecpy->postponed = 1;
1052 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001053 break;
1054 }
1055 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001056 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001057 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1058 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001059 break;
1060 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001061 reset_msg = (session_reset_msg_t *) e->data;
1062 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1063 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001064 if (s->session_state == VCL_STATE_CLOSED)
1065 break;
Florin Corasb242d312020-10-26 15:35:40 -07001066 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1067 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001068 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1069 s->session_state = VCL_STATE_DISCONNECT;
1070 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1071 *ecpy = *e;
1072 ecpy->postponed = 1;
1073 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001074 break;
1075 }
Florin Coras134a9962018-08-28 11:32:04 -07001076 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001077 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001078 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1079 vcl_session_unlisten_reply_handler (wrk, e->data);
1080 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001081 case SESSION_CTRL_EVT_MIGRATED:
1082 vcl_session_migrated_handler (wrk, e->data);
1083 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001084 case SESSION_CTRL_EVT_CLEANUP:
1085 vcl_session_cleanup_handler (wrk, e->data);
1086 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001087 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1088 vcl_session_req_worker_update_handler (wrk, e->data);
1089 break;
1090 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1091 vcl_session_worker_update_reply_handler (wrk, e->data);
1092 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001093 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1094 vcl_session_app_add_segment_handler (wrk, e->data);
1095 break;
1096 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1097 vcl_session_app_del_segment_handler (wrk, e->data);
1098 break;
hanlina3a48962020-07-13 11:09:15 +08001099 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001100 vcl_worker_rpc_handler (wrk, e->data);
1101 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001102 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1103 vcl_session_transport_attr_reply_handler (wrk, e->data);
1104 break;
Florin Coras54693d22018-07-17 10:46:29 -07001105 default:
1106 clib_warning ("unhandled %u", e->event_type);
1107 }
1108 return VPPCOM_OK;
1109}
1110
Florin Coras30e79c22019-01-02 19:31:22 -08001111static int
Florin Coras697faea2018-06-27 17:10:49 -07001112vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001113 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001114 f64 wait_for_time)
1115{
Florin Coras134a9962018-08-28 11:32:04 -07001116 vcl_worker_t *wrk = vcl_worker_get_current ();
1117 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001118 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001119 svm_msg_q_msg_t msg;
1120 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001121
Florin Coras697faea2018-06-27 17:10:49 -07001122 do
Dave Wallace543852a2017-08-03 02:11:34 -04001123 {
Florin Coras134a9962018-08-28 11:32:04 -07001124 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001125 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001126 {
Florin Coras070453d2018-08-24 17:04:27 -07001127 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001128 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001129 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001130 {
Florin Coras697faea2018-06-27 17:10:49 -07001131 return VPPCOM_OK;
1132 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001133 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001134 {
Florin Coras697faea2018-06-27 17:10:49 -07001135 return VPPCOM_ECONNREFUSED;
1136 }
Florin Coras54693d22018-07-17 10:46:29 -07001137
Florin Coras134a9962018-08-28 11:32:04 -07001138 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001139 {
1140 usleep (100);
1141 continue;
1142 }
Florin Coras134a9962018-08-28 11:32:04 -07001143 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001144 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001145 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001146 }
Florin Coras134a9962018-08-28 11:32:04 -07001147 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001148
Florin Coras05ecfcc2018-12-12 18:19:39 -08001149 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001150 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001151 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001152
Florin Coras697faea2018-06-27 17:10:49 -07001153 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001154}
1155
Florin Coras30e79c22019-01-02 19:31:22 -08001156static void
1157vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1158{
Florin Coras288eaab2019-02-03 15:26:14 -08001159 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001160 vcl_session_t *s;
1161 u32 *sip;
1162
1163 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1164 return;
1165
1166 vec_foreach (sip, wrk->pending_session_wrk_updates)
1167 {
1168 s = vcl_session_get (wrk, *sip);
1169 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1170 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001171 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1172 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001173 s->session_state = state;
1174 }
1175 vec_reset_length (wrk->pending_session_wrk_updates);
1176}
1177
Florin Corasf9240dc2019-01-15 08:03:17 -08001178void
Florin Coras5398dfb2021-01-25 20:31:27 -08001179vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001180{
Florin Coras30e79c22019-01-02 19:31:22 -08001181 svm_msg_q_msg_t *msg;
1182 session_event_t *e;
1183 svm_msg_q_t *mq;
1184 int i;
1185
1186 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001187 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001188
1189 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1190 {
1191 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1192 e = svm_msg_q_msg_data (mq, msg);
1193 vcl_handle_mq_event (wrk, e);
1194 svm_msg_q_free_msg (mq, msg);
1195 }
1196 vec_reset_length (wrk->mq_msg_vector);
1197 vcl_handle_pending_wrk_updates (wrk);
1198}
1199
Florin Coras5398dfb2021-01-25 20:31:27 -08001200void
1201vcl_flush_mq_events (void)
1202{
1203 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1204}
1205
Florin Coras697faea2018-06-27 17:10:49 -07001206static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001207vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001208{
Florin Coras134a9962018-08-28 11:32:04 -07001209 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001210 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001211 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001212 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001213
Florin Corasab2f6db2018-08-31 14:31:41 -07001214 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001215 if (!session)
1216 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001217
Florin Corasaaff5ee2019-07-08 13:00:00 -07001218 /* Flush pending accept events, if any */
1219 while (clib_fifo_elts (session->accept_evts_fifo))
1220 {
1221 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1222 accepted_msg = &evt->accepted_msg;
1223 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1224 vcl_send_session_accepted_reply (session->vpp_evt_q,
1225 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001226 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001227 }
1228 clib_fifo_free (session->accept_evts_fifo);
1229
Florin Coras458089b2019-08-21 16:20:44 -07001230 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001231
Florin Coras5e062572019-03-14 19:07:51 -07001232 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001233 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001234 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001235
1236 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001237 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001238
Florin Coras070453d2018-08-24 17:04:27 -07001239 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001240}
1241
Florin Coras940f78f2018-11-30 12:11:20 -08001242/**
1243 * Handle app exit
1244 *
1245 * Notify vpp of the disconnect and mark the worker as free. If we're the
1246 * last worker, do a full cleanup otherwise, since we're probably a forked
1247 * child, avoid syscalls as much as possible. We might've lost privileges.
1248 */
1249void
1250vppcom_app_exit (void)
1251{
1252 if (!pool_elts (vcm->workers))
1253 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001254 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1255 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001256 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001257}
1258
Florin Coras935ce752020-09-08 22:43:47 -07001259static int
1260vcl_api_attach (void)
1261{
1262 if (vcm->cfg.vpp_app_socket_api)
1263 return vcl_sapi_attach ();
1264
1265 return vcl_bapi_attach ();
1266}
1267
1268static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001269vcl_api_retry_attach (vcl_worker_t *wrk)
1270{
1271 vcl_session_t *s;
1272
1273 if (vcl_api_attach ())
1274 return;
1275
1276 /* Treat listeners as configuration that needs to be re-added to vpp */
1277 pool_foreach (s, wrk->sessions)
1278 {
1279 if (s->flags & VCL_SESSION_F_IS_VEP)
1280 continue;
1281 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1282 vppcom_session_listen (vcl_session_handle (s), 10);
1283 else
1284 VDBG (0, "internal error: unexpected state %d", s->session_state);
1285 }
1286}
1287
1288static void
1289vcl_api_handle_disconnect (vcl_worker_t *wrk)
1290{
1291 wrk->api_client_handle = ~0;
1292 vcl_worker_detach_sessions (wrk);
1293}
1294
1295static void
Florin Coras935ce752020-09-08 22:43:47 -07001296vcl_api_detach (vcl_worker_t * wrk)
1297{
Florin Corasd04ea442022-03-30 16:08:25 -07001298 if (wrk->api_client_handle == ~0)
1299 return;
1300
Florin Coras935ce752020-09-08 22:43:47 -07001301 vcl_send_app_detach (wrk);
1302
1303 if (vcm->cfg.vpp_app_socket_api)
1304 return vcl_sapi_detach (wrk);
1305
1306 return vcl_bapi_disconnect_from_vpp ();
1307}
1308
Dave Wallace543852a2017-08-03 02:11:34 -04001309/*
1310 * VPPCOM Public API functions
1311 */
1312int
Florin Coras66ec4672020-06-15 07:59:40 -07001313vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001314{
Dave Wallace543852a2017-08-03 02:11:34 -04001315 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001316 int rv;
1317
Florin Coras47c40e22018-11-26 17:01:36 -08001318 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001319 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001320 VDBG (1, "already initialized");
1321 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001322 }
1323
Florin Coras47c40e22018-11-26 17:01:36 -08001324 vcm->is_init = 1;
1325 vppcom_cfg (&vcm->cfg);
1326 vcl_cfg = &vcm->cfg;
1327
1328 vcm->main_cpu = pthread_self ();
1329 vcm->main_pid = getpid ();
1330 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001331 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1332 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001333 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1334 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001335 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001336 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001337 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001338
1339 /* Allocate default worker */
1340 vcl_worker_alloc_and_init ();
1341
Florin Coras935ce752020-09-08 22:43:47 -07001342 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001343 {
1344 vppcom_app_destroy ();
1345 return rv;
1346 }
Florin Coras47c40e22018-11-26 17:01:36 -08001347
1348 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001349 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001350
1351 return VPPCOM_OK;
1352}
1353
1354void
1355vppcom_app_destroy (void)
1356{
Florin Corasdc0ded72020-04-30 02:59:55 +00001357 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001358 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001359
Florin Coras940f78f2018-11-30 12:11:20 -08001360 if (!pool_elts (vcm->workers))
1361 return;
1362
Florin Coras0d427d82018-06-27 03:24:07 -07001363 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001364
Florin Corasdc0ded72020-04-30 02:59:55 +00001365 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001366
Florin Corasce815de2020-04-16 18:47:27 +00001367 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001368 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001369 if (current_wrk != wrk)
1370 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001371 }
Florin Corasce815de2020-04-16 18:47:27 +00001372 /* *INDENT-ON* */
1373
Florin Coras935ce752020-09-08 22:43:47 -07001374 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001375 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001376 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001377
Florin Coras0d427d82018-06-27 03:24:07 -07001378 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001379
1380 /*
1381 * Free the heap and fix vcm
1382 */
1383 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001384 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001385
1386 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001387 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001388}
1389
1390int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001391vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001392{
Florin Coras134a9962018-08-28 11:32:04 -07001393 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001394 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001395
Florin Coras134a9962018-08-28 11:32:04 -07001396 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001397
Florin Coras7e12d942018-06-27 14:32:43 -07001398 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001399 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001400 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001401 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301402 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001403
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001404 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001405 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001406
Florin Coras7e12d942018-06-27 14:32:43 -07001407 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1408 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001409
Florin Coras5e062572019-03-14 19:07:51 -07001410 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001411
Florin Coras134a9962018-08-28 11:32:04 -07001412 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001413}
1414
Florin Corasfe286f72021-06-04 10:07:55 -07001415static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001416vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001417{
Florin Corasfa3884f2021-06-22 20:04:46 -07001418 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001419
Florin Corasfa3884f2021-06-22 20:04:46 -07001420 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001421 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001422 wrk->ep_lt_current = s->session_index;
1423 s->vep.lt_next = s->session_index;
1424 s->vep.lt_prev = s->session_index;
1425 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001426 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001427
1428 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1429 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1430
1431 prev->vep.lt_next = s->session_index;
1432 s->vep.lt_prev = prev->session_index;
1433
1434 s->vep.lt_next = cur->session_index;
1435 cur->vep.lt_prev = s->session_index;
1436}
1437
1438static void
1439vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1440{
1441 vcl_session_t *prev, *next;
1442
1443 if (s->vep.lt_next == s->session_index)
1444 {
1445 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1446 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1447 return;
1448 }
1449
1450 prev = vcl_session_get (wrk, s->vep.lt_prev);
1451 next = vcl_session_get (wrk, s->vep.lt_next);
1452
1453 prev->vep.lt_next = next->session_index;
1454 next->vep.lt_prev = prev->session_index;
1455
1456 if (s->session_index == wrk->ep_lt_current)
1457 wrk->ep_lt_current = s->vep.lt_next;
1458
1459 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001460}
1461
Dave Wallace543852a2017-08-03 02:11:34 -04001462int
Florin Corasc127d5a2020-10-14 16:35:58 -07001463vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001464 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001465{
Florin Coras134a9962018-08-28 11:32:04 -07001466 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001467
Florin Coras6c3b2182020-10-19 18:36:48 -07001468 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001469
Florin Coras6c3b2182020-10-19 18:36:48 -07001470 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001471 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001472 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001473 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001474 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001475 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001476 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001477 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001478 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1479 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001480 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001481 }
Florin Corase4306b62020-10-28 12:51:10 -07001482 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001483 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001484
Florin Coras6c3b2182020-10-19 18:36:48 -07001485 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001486 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001487 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001488 if (rv < 0)
1489 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001490 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1491 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001492 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001493
Florin Coras9ace36d2019-10-28 13:14:17 -07001494 if (!do_disconnect)
1495 {
1496 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001497 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001498 goto cleanup;
1499 }
1500
Florin Coras6c3b2182020-10-19 18:36:48 -07001501 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001502 {
1503 rv = vppcom_session_unbind (sh);
1504 if (PREDICT_FALSE (rv < 0))
1505 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001506 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001507 vppcom_retval_str (rv));
1508 return rv;
1509 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001510 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001511 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001512 {
1513 rv = vppcom_session_disconnect (sh);
1514 if (PREDICT_FALSE (rv < 0))
1515 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001516 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001517 rv, vppcom_retval_str (rv));
1518 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001519 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001520 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001521 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001522 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001523 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001524 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001525 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001526
1527 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1528 goto free_session;
1529
1530 /* Disconnect/reset messages pending but vpp transport and session
1531 * cleanups already done. Free only after messages drained. */
1532 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001533 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001534
Florin Corasc127d5a2020-10-14 16:35:58 -07001535 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001536
Florin Coras9ace36d2019-10-28 13:14:17 -07001537 /* Session is removed only after vpp confirms the disconnect */
1538 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001539
Florin Corasf9240dc2019-01-15 08:03:17 -08001540cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001541 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001542free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001543 vcl_session_free (wrk, s);
1544 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001545
Dave Wallace543852a2017-08-03 02:11:34 -04001546 return rv;
1547}
1548
1549int
Florin Corasf9240dc2019-01-15 08:03:17 -08001550vppcom_session_close (uint32_t session_handle)
1551{
1552 vcl_worker_t *wrk = vcl_worker_get_current ();
1553 vcl_session_t *session;
1554
1555 session = vcl_session_get_w_handle (wrk, session_handle);
1556 if (!session)
1557 return VPPCOM_EBADFD;
1558 return vcl_session_cleanup (wrk, session, session_handle,
1559 1 /* do_disconnect */ );
1560}
1561
1562int
Florin Coras134a9962018-08-28 11:32:04 -07001563vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001564{
Florin Coras134a9962018-08-28 11:32:04 -07001565 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001566 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001567
1568 if (!ep || !ep->ip)
1569 return VPPCOM_EINVAL;
1570
Florin Coras134a9962018-08-28 11:32:04 -07001571 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001572 if (!session)
1573 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001574
Florin Coras6c3b2182020-10-19 18:36:48 -07001575 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001576 {
Florin Coras5e062572019-03-14 19:07:51 -07001577 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1578 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001579 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001580 }
1581
Florin Coras7e12d942018-06-27 14:32:43 -07001582 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001583 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001584 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1585 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001586 else
Dave Barach178cf492018-11-13 16:34:13 -05001587 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1588 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001589 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001590
Florin Coras60687192021-11-29 21:00:47 -08001591 VDBG (0,
1592 "session %u handle %u: binding to local %s address %U port %u, "
1593 "proto %s",
1594 session->session_index, session_handle,
1595 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1596 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001597 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1598 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001599 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001600 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001601
1602 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001603 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001604
Florin Coras070453d2018-08-24 17:04:27 -07001605 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001606}
1607
1608int
Florin Coras134a9962018-08-28 11:32:04 -07001609vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001610{
Florin Coras134a9962018-08-28 11:32:04 -07001611 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001612 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001613 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001614 int rv;
1615
Florin Coras134a9962018-08-28 11:32:04 -07001616 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001617 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001618 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001619
Dave Wallaceee45d412017-11-24 21:44:06 -05001620 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001621 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001622 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001623 VDBG (0, "session %u [0x%llx]: already in listen state!",
1624 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001625 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001626 }
1627
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001628 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001629
Florin Coras070453d2018-08-24 17:04:27 -07001630 /*
1631 * Send listen request to vpp and wait for reply
1632 */
Florin Coras458089b2019-08-21 16:20:44 -07001633 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001634 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001635 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001636 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001637
Florin Coras070453d2018-08-24 17:04:27 -07001638 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001639 {
Florin Coras134a9962018-08-28 11:32:04 -07001640 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001641 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1642 listen_sh, listen_session->vpp_handle, rv,
1643 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001644 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001645 }
1646
Florin Coras070453d2018-08-24 17:04:27 -07001647 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001648}
1649
Florin Coras134a9962018-08-28 11:32:04 -07001650static int
Florin Coras5e062572019-03-14 19:07:51 -07001651validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001652{
Florin Coras6c3b2182020-10-19 18:36:48 -07001653 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001654 {
Florin Coras5e062572019-03-14 19:07:51 -07001655 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1656 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001657 return VPPCOM_EBADFD;
1658 }
1659
Florin Corasc127d5a2020-10-14 16:35:58 -07001660 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001661 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001662 {
Florin Coras60687192021-11-29 21:00:47 -08001663 VDBG (0,
1664 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1665 " (%s)",
1666 ls->vpp_handle, ls->session_state,
1667 vcl_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001668 return VPPCOM_EBADFD;
1669 }
1670 return VPPCOM_OK;
1671}
1672
1673int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001674vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1675{
1676 if (!strcmp (proto_str, "TCP"))
1677 *proto = VPPCOM_PROTO_TCP;
1678 else if (!strcmp (proto_str, "tcp"))
1679 *proto = VPPCOM_PROTO_TCP;
1680 else if (!strcmp (proto_str, "UDP"))
1681 *proto = VPPCOM_PROTO_UDP;
1682 else if (!strcmp (proto_str, "udp"))
1683 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001684 else if (!strcmp (proto_str, "TLS"))
1685 *proto = VPPCOM_PROTO_TLS;
1686 else if (!strcmp (proto_str, "tls"))
1687 *proto = VPPCOM_PROTO_TLS;
1688 else if (!strcmp (proto_str, "QUIC"))
1689 *proto = VPPCOM_PROTO_QUIC;
1690 else if (!strcmp (proto_str, "quic"))
1691 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001692 else if (!strcmp (proto_str, "DTLS"))
1693 *proto = VPPCOM_PROTO_DTLS;
1694 else if (!strcmp (proto_str, "dtls"))
1695 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001696 else if (!strcmp (proto_str, "SRTP"))
1697 *proto = VPPCOM_PROTO_SRTP;
1698 else if (!strcmp (proto_str, "srtp"))
1699 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001700 else
1701 return 1;
1702 return 0;
1703}
1704
1705int
Florin Coras134a9962018-08-28 11:32:04 -07001706vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001707 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001708{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001709 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001710 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001711 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001712 vcl_session_t *listen_session = 0;
1713 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001714 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001715 u8 is_nonblocking;
1716 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001717
Florin Coras5398dfb2021-01-25 20:31:27 -08001718again:
1719
Florin Coras134a9962018-08-28 11:32:04 -07001720 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001721 if (!listen_session)
1722 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001723
Florin Coras134a9962018-08-28 11:32:04 -07001724 listen_session_index = listen_session->session_index;
1725 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001726 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001727
Florin Coras54693d22018-07-17 10:46:29 -07001728 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001729 {
Florin Coras54693d22018-07-17 10:46:29 -07001730 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001731 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001732 accepted_msg = evt->accepted_msg;
1733 goto handle;
1734 }
1735
Florin Corasac422d62020-10-19 20:51:36 -07001736 is_nonblocking = vcl_session_has_attr (listen_session,
1737 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001738 while (1)
1739 {
Carl Smith592a9092019-11-12 14:57:37 +13001740 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1741 return VPPCOM_EAGAIN;
1742
Florin Coras5398dfb2021-01-25 20:31:27 -08001743 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1744 vcl_worker_flush_mq_events (wrk);
1745 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001746 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001747
Florin Coras54693d22018-07-17 10:46:29 -07001748handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001749
Florin Coras00cca802019-06-06 09:38:44 -07001750 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1751 listen_session_index);
1752 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1753 return VPPCOM_ECONNABORTED;
1754
Florin Coras134a9962018-08-28 11:32:04 -07001755 listen_session = vcl_session_get (wrk, listen_session_index);
1756 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001757
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001758 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001759 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001760
Florin Coras5e062572019-03-14 19:07:51 -07001761 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1762 " flags %d, is_nonblocking %u", listen_session->session_index,
1763 listen_session->vpp_handle, client_session_index,
1764 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001765 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001766
Dave Wallace048b1d62018-01-03 22:24:41 -05001767 if (ep)
1768 {
Florin Coras7e12d942018-06-27 14:32:43 -07001769 ep->is_ip4 = client_session->transport.is_ip4;
1770 ep->port = client_session->transport.rmt_port;
1771 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001772 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1773 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001774 else
Dave Barach178cf492018-11-13 16:34:13 -05001775 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1776 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001777 }
Dave Wallace60caa062017-11-10 17:07:13 -05001778
Florin Coras60687192021-11-29 21:00:47 -08001779 VDBG (0,
1780 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1781 "local: %U:%u",
1782 listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001783 client_session_index, client_session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001784 vcl_format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001785 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001786 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001787 vcl_format_ip46_address, &client_session->transport.lcl_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.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001790 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1791 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001792
Florin Coras3c7d4f92018-12-14 11:28:43 -08001793 /*
1794 * Session might have been closed already
1795 */
1796 if (accept_flags)
1797 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001798 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001799 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001800 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001801 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001802 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001803 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001804}
1805
1806int
Florin Coras134a9962018-08-28 11:32:04 -07001807vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001808{
Florin Coras134a9962018-08-28 11:32:04 -07001809 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001810 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001811 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001812 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001813
Florin Coras134a9962018-08-28 11:32:04 -07001814 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001815 if (!session)
1816 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001817 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001818
Florin Coras6c3b2182020-10-19 18:36:48 -07001819 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001820 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001821 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001822 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001823 }
1824
Florin Corasc127d5a2020-10-14 16:35:58 -07001825 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001826 {
Florin Coras60687192021-11-29 21:00:47 -08001827 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001828 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1829 " state (%s)",
1830 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001831 vcl_format_ip46_address, &session->transport.rmt_ip,
1832 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001833 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001834 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001835 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001836 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001837 }
1838
Florin Coras0a1e1832020-03-29 18:54:04 +00001839 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001840 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001841 {
1842 if (session->session_type != VPPCOM_PROTO_UDP)
1843 return VPPCOM_EINVAL;
1844 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001845 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001846 }
1847
Florin Coras7e12d942018-06-27 14:32:43 -07001848 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001849 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001850 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001851 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001852 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001853
Florin Coras5ffb9642021-12-03 17:24:13 -08001854 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1855 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001856 &session->transport.rmt_ip,
1857 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001858 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001859 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001860
Florin Coras458089b2019-08-21 16:20:44 -07001861 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001862
Florin Corasac422d62020-10-19 20:51:36 -07001863 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001864 {
fanyfa49d59d2020-10-13 17:07:16 +08001865 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001866 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001867 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001868 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001869 return VPPCOM_EINPROGRESS;
1870 }
Florin Coras57c88932019-08-29 12:03:17 -07001871
1872 /*
1873 * Wait for reply from vpp if blocking
1874 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001875 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001876 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001877
Florin Coras134a9962018-08-28 11:32:04 -07001878 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001879 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1880 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001881
Dave Wallace4878cbe2017-11-21 03:45:09 -05001882 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001883}
1884
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001885int
1886vppcom_session_stream_connect (uint32_t session_handle,
1887 uint32_t parent_session_handle)
1888{
1889 vcl_worker_t *wrk = vcl_worker_get_current ();
1890 vcl_session_t *session, *parent_session;
1891 u32 session_index, parent_session_index;
1892 int rv;
1893
1894 session = vcl_session_get_w_handle (wrk, session_handle);
1895 if (!session)
1896 return VPPCOM_EBADFD;
1897 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1898 if (!parent_session)
1899 return VPPCOM_EBADFD;
1900
1901 session_index = session->session_index;
1902 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001903 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001904 {
1905 VDBG (0, "ERROR: cannot connect epoll session %u!",
1906 session->session_index);
1907 return VPPCOM_EBADFD;
1908 }
1909
Florin Corasc127d5a2020-10-14 16:35:58 -07001910 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001911 {
Florin Coras60687192021-11-29 21:00:47 -08001912 VDBG (0,
1913 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001914 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001915 session_handle, session->vpp_handle, parent_session_handle,
1916 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001917 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001918 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001919 return VPPCOM_OK;
1920 }
1921
1922 /* Connect to quic session specifics */
1923 session->transport.is_ip4 = parent_session->transport.is_ip4;
1924 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1925 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001926 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001927
1928 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1929 session_handle, parent_session_handle, parent_session->vpp_handle);
1930
1931 /*
1932 * Send connect request and wait for reply from vpp
1933 */
Florin Coras458089b2019-08-21 16:20:44 -07001934 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001935 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001936 vcm->cfg.session_timeout);
1937
1938 session->listener_index = parent_session_index;
1939 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001940 if (parent_session)
1941 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001942
1943 session = vcl_session_get (wrk, session_index);
1944 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1945 session->vpp_handle, rv ? "failed" : "succeeded");
1946
1947 return rv;
1948}
1949
Steven58f464e2017-10-25 12:33:12 -07001950static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001951vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001952 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001953{
Florin Coras134a9962018-08-28 11:32:04 -07001954 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001955 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001956 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001957 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001958 session_event_t *e;
1959 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001960 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001961
Florin Coras070453d2018-08-24 17:04:27 -07001962 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001963 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001964
Florin Coras134a9962018-08-28 11:32:04 -07001965 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001966 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001967 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001968
Florin Coras6d0106e2019-01-29 20:11:58 -08001969 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001970 {
Florin Coras5e062572019-03-14 19:07:51 -07001971 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001972 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001973 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08001974 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001975 }
1976
liuyacan55c952e2021-06-13 14:54:55 +08001977 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
1978 {
1979 /* Vpp would ack the incoming data and enqueue it for reading.
1980 * So even if SHUT_RD is set, we can still read() the data if
1981 * the session is ready.
1982 */
1983 if (!vcl_session_read_ready (s))
1984 {
1985 return 0;
1986 }
1987 }
1988
Florin Corasac422d62020-10-19 20:51:36 -07001989 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001990 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001991 mq = wrk->app_event_queue;
1992 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001993 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001994
Sirshak Das28aa5392019-02-05 01:33:33 -06001995 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001996 {
Florin Coras54693d22018-07-17 10:46:29 -07001997 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001998 {
Yu Pingb2955352019-12-04 06:49:04 +08001999 if (vcl_session_is_closing (s))
2000 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002001 if (is_ct)
2002 svm_fifo_unset_event (s->rx_fifo);
2003 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002004 return VPPCOM_EWOULDBLOCK;
2005 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002006 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002007 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002008 if (vcl_session_is_closing (s))
2009 return vcl_session_closing_error (s);
2010
Florin Corasd6894562020-10-28 00:37:15 -07002011 if (is_ct)
2012 svm_fifo_unset_event (s->rx_fifo);
2013 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002014
Florin Coras5398dfb2021-01-25 20:31:27 -08002015 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2016 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002017 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002018 }
Florin Coras54693d22018-07-17 10:46:29 -07002019
Florin Coras4a2c7942020-09-10 12:27:14 -07002020read_again:
2021
Florin Coras460dce62018-07-27 05:45:06 -07002022 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002023 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002024 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002025 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2026
2027 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002028
2029 if (peek)
2030 return rv;
2031
Florin Coras4a2c7942020-09-10 12:27:14 -07002032 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002033
Sirshak Das28aa5392019-02-05 01:33:33 -06002034 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002035 {
Florin Corasd6894562020-10-28 00:37:15 -07002036 if (is_ct)
2037 svm_fifo_unset_event (s->rx_fifo);
2038 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002039 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002040 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002041 {
Florin Corasc34118b2020-08-12 18:58:25 -07002042 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2043 e->event_type = SESSION_IO_EVT_RX;
2044 e->session_index = s->session_index;
2045 }
2046 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002047 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002048 {
2049 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002050 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002051 buf += rv;
2052 n -= rv;
2053 goto read_again;
2054 }
Florin Coras41c9e042018-09-11 00:10:41 -07002055
Florin Coras17ec5772020-08-12 20:46:29 -07002056 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002057 {
Florin Coras17ec5772020-08-12 20:46:29 -07002058 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002059 app_send_io_evt_to_vpp (s->vpp_evt_q,
2060 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002061 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002062 }
2063
Florin Coras5e062572019-03-14 19:07:51 -07002064 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2065 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002066
Florin Coras54693d22018-07-17 10:46:29 -07002067 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002068}
2069
Steven58f464e2017-10-25 12:33:12 -07002070int
Florin Coras134a9962018-08-28 11:32:04 -07002071vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002072{
Florin Coras134a9962018-08-28 11:32:04 -07002073 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002074}
2075
2076static int
Florin Coras134a9962018-08-28 11:32:04 -07002077vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002078{
Florin Coras134a9962018-08-28 11:32:04 -07002079 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002080}
2081
Florin Coras2cba8532018-09-11 16:33:36 -07002082int
2083vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002084 vppcom_data_segment_t * ds, uint32_t n_segments,
2085 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002086{
2087 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002088 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002089 vcl_session_t *s = 0;
2090 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002091 svm_msg_q_t *mq;
2092 u8 is_ct;
2093
2094 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002095 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002096 return VPPCOM_EBADFD;
2097
Florin Coras6d0106e2019-01-29 20:11:58 -08002098 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2099 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002100
Florin Corasac422d62020-10-19 20:51:36 -07002101 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002102 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002103 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002104 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002105 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002106
Sirshak Das28aa5392019-02-05 01:33:33 -06002107 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002108 {
2109 if (is_nonblocking)
2110 {
Florin Coras62877022020-10-28 21:22:04 -07002111 if (is_ct)
2112 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002113 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002114 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002115 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002116 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002117 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002118 if (vcl_session_is_closing (s))
2119 return vcl_session_closing_error (s);
2120
Florin Coras62877022020-10-28 21:22:04 -07002121 if (is_ct)
2122 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002123 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002124
Florin Coras5398dfb2021-01-25 20:31:27 -08002125 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2126 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002127 }
2128 }
2129
Florin Corasd1cc38d2020-10-11 11:05:04 -07002130 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002131 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002132 if (n_read < 0)
2133 return VPPCOM_EAGAIN;
2134
2135 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2136 {
Florin Coras62877022020-10-28 21:22:04 -07002137 if (is_ct)
2138 svm_fifo_unset_event (s->rx_fifo);
2139 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002140 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002141 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002142 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002143 {
2144 session_event_t *e;
2145 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2146 e->event_type = SESSION_IO_EVT_RX;
2147 e->session_index = s->session_index;
2148 }
2149 }
2150
2151 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002152 return n_read;
2153}
2154
2155void
Florin Corasd68faf82020-09-25 15:18:13 -07002156vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002157{
2158 vcl_worker_t *wrk = vcl_worker_get_current ();
2159 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002160 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002161
2162 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002163 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002164 return;
2165
Florin Coras62877022020-10-28 21:22:04 -07002166 is_ct = vcl_session_is_ct (s);
2167 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002168
Florin Coras8cc93212021-09-10 11:37:12 -07002169 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002170 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002171}
2172
Florin Coras7a2abce2020-04-05 19:25:44 +00002173always_inline u8
2174vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002175{
Florin Coras7a2abce2020-04-05 19:25:44 +00002176 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2177 if (is_dgram)
2178 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2179 else
2180 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002181}
2182
2183always_inline int
2184vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2185 size_t n, u8 is_flush, u8 is_dgram)
2186{
Florin Coras6d0106e2019-01-29 20:11:58 -08002187 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002188 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002189 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002190 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002191 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002192
Florin Coras0b0d28e2021-06-04 17:31:53 -07002193 /* Accept zero length writes but just return */
2194 if (PREDICT_FALSE (!n))
2195 return VPPCOM_OK;
2196
2197 if (PREDICT_FALSE (!buf))
2198 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002199
Florin Coras6c3b2182020-10-19 18:36:48 -07002200 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002201 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002202 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2203 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002204 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002205 }
2206
liuyacan55c952e2021-06-13 14:54:55 +08002207 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002208 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002209 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2210 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002211 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002212 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002213 }
2214
liuyacan55c952e2021-06-13 14:54:55 +08002215 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2216 {
2217 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2218 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002219 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002220 return VPPCOM_EPIPE;
2221 }
2222
Florin Coras0e88e852018-09-17 22:09:02 -07002223 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002224 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002225 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002226
Florin Coras653e43f2019-03-04 10:56:23 -08002227 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002228 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002229 {
Florin Coras54693d22018-07-17 10:46:29 -07002230 if (is_nonblocking)
2231 {
Florin Coras070453d2018-08-24 17:04:27 -07002232 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002233 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002234 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002235 {
Florin Coras2d379d82019-06-28 12:45:12 -07002236 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002237 if (vcl_session_is_closing (s))
2238 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002239
Florin Coras5398dfb2021-01-25 20:31:27 -08002240 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2241 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002242 }
Dave Wallace543852a2017-08-03 02:11:34 -04002243 }
Dave Wallace543852a2017-08-03 02:11:34 -04002244
Florin Coras653e43f2019-03-04 10:56:23 -08002245 et = SESSION_IO_EVT_TX;
2246 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002247 et = SESSION_IO_EVT_TX_FLUSH;
2248
Florin Coras7a2abce2020-04-05 19:25:44 +00002249 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002250 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002251 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002252 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002253 else
2254 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002255 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002256
Florin Coras14ed6df2019-03-06 21:13:42 -08002257 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002258 app_send_io_evt_to_vpp (
2259 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002260
Florin Coras56230092020-09-02 20:52:58 -07002261 /* The underlying fifo segment can run out of memory */
2262 if (PREDICT_FALSE (n_write < 0))
2263 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002264
Florin Coras6d0106e2019-01-29 20:11:58 -08002265 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2266 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002267
Florin Coras54693d22018-07-17 10:46:29 -07002268 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002269}
2270
Florin Coras42ceddb2018-12-12 10:56:01 -08002271int
2272vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2273{
Florin Coras7a2abce2020-04-05 19:25:44 +00002274 vcl_worker_t *wrk = vcl_worker_get_current ();
2275 vcl_session_t *s;
2276
2277 s = vcl_session_get_w_handle (wrk, session_handle);
2278 if (PREDICT_FALSE (!s))
2279 return VPPCOM_EBADFD;
2280
2281 return vppcom_session_write_inline (wrk, s, buf, n,
2282 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002283}
2284
Florin Corasb0f662f2018-12-27 14:51:46 -08002285int
2286vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2287{
Florin Coras7a2abce2020-04-05 19:25:44 +00002288 vcl_worker_t *wrk = vcl_worker_get_current ();
2289 vcl_session_t *s;
2290
2291 s = vcl_session_get_w_handle (wrk, session_handle);
2292 if (PREDICT_FALSE (!s))
2293 return VPPCOM_EBADFD;
2294
2295 return vppcom_session_write_inline (wrk, s, buf, n,
2296 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002297}
2298
Florin Corasc0737e92019-03-04 14:19:39 -08002299#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002300if (PREDICT_FALSE (!_s->rx_fifo)) \
2301 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002302if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2303 { \
2304 if (!vcl_session_is_ct (_s)) \
2305 { \
2306 svm_fifo_unset_event (_s->rx_fifo); \
2307 if (svm_fifo_is_empty (_s->rx_fifo)) \
2308 break; \
2309 } \
2310 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2311 { \
Florin Coras2f630182020-08-13 00:17:51 -07002312 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002313 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2314 break; \
2315 } \
2316 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002317
Florin Coras86f04502018-09-12 16:08:01 -07002318static void
2319vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2320 unsigned long n_bits, unsigned long *read_map,
2321 unsigned long *write_map,
2322 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002323{
2324 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002325 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002326 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002327 u32 sid;
2328
2329 switch (e->event_type)
2330 {
Florin Corasc0737e92019-03-04 14:19:39 -08002331 case SESSION_IO_EVT_RX:
2332 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002333 s = vcl_session_get (wrk, sid);
2334 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002335 break;
Florin Corasb242d312020-10-26 15:35:40 -07002336 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002337 if (sid < n_bits && read_map)
2338 {
David Johnsond9818dd2018-12-14 14:53:41 -05002339 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002340 *bits_set += 1;
2341 }
2342 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002343 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002344 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002345 s = vcl_session_get (wrk, sid);
2346 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002347 break;
2348 if (sid < n_bits && write_map)
2349 {
David Johnsond9818dd2018-12-14 14:53:41 -05002350 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002351 *bits_set += 1;
2352 }
2353 break;
Florin Coras86f04502018-09-12 16:08:01 -07002354 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002355 if (!e->postponed)
2356 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2357 else
2358 s = vcl_session_get (wrk, e->session_index);
2359 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002360 break;
Florin Corasb242d312020-10-26 15:35:40 -07002361 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002362 if (sid < n_bits && read_map)
2363 {
David Johnsond9818dd2018-12-14 14:53:41 -05002364 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002365 *bits_set += 1;
2366 }
2367 break;
2368 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002369 if (!e->postponed)
2370 {
2371 connected_msg = (session_connected_msg_t *) e->data;
2372 sid = vcl_session_connected_handler (wrk, connected_msg);
2373 }
2374 else
2375 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002376 if (sid == VCL_INVALID_SESSION_INDEX)
2377 break;
2378 if (sid < n_bits && write_map)
2379 {
2380 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2381 *bits_set += 1;
2382 }
Florin Coras86f04502018-09-12 16:08:01 -07002383 break;
2384 case SESSION_CTRL_EVT_DISCONNECTED:
2385 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002386 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2387 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002388 break;
Florin Corasb242d312020-10-26 15:35:40 -07002389 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002390 if (sid < n_bits && except_map)
2391 {
David Johnsond9818dd2018-12-14 14:53:41 -05002392 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002393 *bits_set += 1;
2394 }
2395 break;
2396 case SESSION_CTRL_EVT_RESET:
2397 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2398 if (sid < n_bits && except_map)
2399 {
David Johnsond9818dd2018-12-14 14:53:41 -05002400 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002401 *bits_set += 1;
2402 }
2403 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002404 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2405 vcl_session_unlisten_reply_handler (wrk, e->data);
2406 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002407 case SESSION_CTRL_EVT_MIGRATED:
2408 vcl_session_migrated_handler (wrk, e->data);
2409 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002410 case SESSION_CTRL_EVT_CLEANUP:
2411 vcl_session_cleanup_handler (wrk, e->data);
2412 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002413 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2414 vcl_session_worker_update_reply_handler (wrk, e->data);
2415 break;
2416 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2417 vcl_session_req_worker_update_handler (wrk, e->data);
2418 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002419 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2420 vcl_session_app_add_segment_handler (wrk, e->data);
2421 break;
2422 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2423 vcl_session_app_del_segment_handler (wrk, e->data);
2424 break;
hanlina3a48962020-07-13 11:09:15 +08002425 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002426 vcl_worker_rpc_handler (wrk, e->data);
2427 break;
Florin Coras86f04502018-09-12 16:08:01 -07002428 default:
2429 clib_warning ("unhandled: %u", e->event_type);
2430 break;
2431 }
2432}
2433
2434static int
2435vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2436 unsigned long n_bits, unsigned long *read_map,
2437 unsigned long *write_map, unsigned long *except_map,
2438 double time_to_wait, u32 * bits_set)
2439{
Florin Coras99368312018-08-02 10:45:44 -07002440 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002441 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002442 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002443
Florin Coras54693d22018-07-17 10:46:29 -07002444 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002445 {
Florin Coras54693d22018-07-17 10:46:29 -07002446 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002447 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002448
Florin Coras54693d22018-07-17 10:46:29 -07002449 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002450 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002451 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002452 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002453 else
2454 {
2455 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002456 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002457 }
2458 }
Florin Corase003a1b2019-06-05 10:47:16 -07002459 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002460
Florin Coras134a9962018-08-28 11:32:04 -07002461 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002462 {
Florin Coras134a9962018-08-28 11:32:04 -07002463 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002464 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002465 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2466 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002467 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002468 }
Florin Coras134a9962018-08-28 11:32:04 -07002469 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002470 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002471 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002472}
2473
Florin Coras99368312018-08-02 10:45:44 -07002474static int
Florin Coras294afe22019-01-07 17:49:17 -08002475vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2476 vcl_si_set * read_map, vcl_si_set * write_map,
2477 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002478 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002479{
Florin Coras14ed6df2019-03-06 21:13:42 -08002480 double wait = 0, start = 0;
2481
2482 if (!*bits_set)
2483 {
2484 wait = time_to_wait;
2485 start = clib_time_now (&wrk->clib_time);
2486 }
2487
2488 do
2489 {
2490 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2491 write_map, except_map, wait, bits_set);
2492 if (*bits_set)
2493 return *bits_set;
2494 if (wait == -1)
2495 continue;
2496
2497 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2498 }
2499 while (wait > 0);
2500
2501 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002502}
2503
2504static int
Florin Coras294afe22019-01-07 17:49:17 -08002505vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2506 vcl_si_set * read_map, vcl_si_set * write_map,
2507 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002508 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002509{
2510 vcl_mq_evt_conn_t *mqc;
2511 int __clib_unused n_read;
2512 int n_mq_evts, i;
2513 u64 buf;
2514
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002515 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2516 {
2517 vcl_api_retry_attach (wrk);
2518 return 0;
2519 }
2520
Florin Coras134a9962018-08-28 11:32:04 -07002521 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2522 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2523 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002524 for (i = 0; i < n_mq_evts; i++)
2525 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002526 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2527 {
2528 vcl_api_handle_disconnect (wrk);
2529 continue;
2530 }
2531
Florin Coras134a9962018-08-28 11:32:04 -07002532 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002533 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002534 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002535 except_map, 0, bits_set);
2536 }
2537
2538 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2539}
2540
Dave Wallace543852a2017-08-03 02:11:34 -04002541int
Florin Coras294afe22019-01-07 17:49:17 -08002542vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2543 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002544{
Florin Coras54693d22018-07-17 10:46:29 -07002545 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002546 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002547 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002548 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002549
Dave Wallace7876d392017-10-19 03:53:57 -04002550 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002551 {
Florin Coras134a9962018-08-28 11:32:04 -07002552 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002553 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002554 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2555 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002556 }
Dave Wallace7876d392017-10-19 03:53:57 -04002557 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002558 {
Florin Coras134a9962018-08-28 11:32:04 -07002559 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002560 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002561 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2562 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002563 }
Dave Wallace7876d392017-10-19 03:53:57 -04002564 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002565 {
Florin Coras134a9962018-08-28 11:32:04 -07002566 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002567 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002568 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2569 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002570 }
2571
Florin Coras54693d22018-07-17 10:46:29 -07002572 if (!n_bits)
2573 return 0;
2574
2575 if (!write_map)
2576 goto check_rd;
2577
Florin Coras096a1d52021-01-27 15:33:51 -08002578 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2579 {
2580 if (!(s = vcl_session_get (wrk, sid)))
2581 {
2582 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2583 bits_set++;
2584 continue;
2585 }
Florin Coras54693d22018-07-17 10:46:29 -07002586
Florin Coras096a1d52021-01-27 15:33:51 -08002587 if (vcl_session_write_ready (s))
2588 {
2589 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2590 bits_set++;
2591 }
2592 else
2593 {
2594 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2595 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2596 }
2597 }
Florin Coras54693d22018-07-17 10:46:29 -07002598
2599check_rd:
2600 if (!read_map)
2601 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002602
Florin Coras096a1d52021-01-27 15:33:51 -08002603 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2604 {
2605 if (!(s = vcl_session_get (wrk, sid)))
2606 {
2607 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2608 bits_set++;
2609 continue;
2610 }
Florin Coras54693d22018-07-17 10:46:29 -07002611
Florin Coras096a1d52021-01-27 15:33:51 -08002612 if (vcl_session_read_ready (s))
2613 {
2614 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2615 bits_set++;
2616 }
2617 }
Florin Coras54693d22018-07-17 10:46:29 -07002618
2619check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002620
Florin Coras86f04502018-09-12 16:08:01 -07002621 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2622 {
2623 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2624 read_map, write_map, except_map, &bits_set);
2625 }
2626 vec_reset_length (wrk->unhandled_evts_vector);
2627
Florin Coras99368312018-08-02 10:45:44 -07002628 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002629 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002630 time_to_wait, &bits_set);
2631 else
Florin Coras134a9962018-08-28 11:32:04 -07002632 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002633 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002634
Dave Wallace543852a2017-08-03 02:11:34 -04002635 return (bits_set);
2636}
2637
Dave Wallacef7f809c2017-10-03 01:48:42 -04002638static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002639vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002640{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002641 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002642 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002643 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002644
Florin Corasc0737e92019-03-04 14:19:39 -08002645 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002646 return;
2647
Florin Coras6c3b2182020-10-19 18:36:48 -07002648 s = vcl_session_get_w_handle (wrk, vep_handle);
2649 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002650 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002651 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002652 goto done;
2653 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002654 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002656 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002657 goto done;
2658 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002659 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002660 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002661 "{\n"
2662 " is_vep = %u\n"
2663 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002664 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002665 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2666 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002667
Florin Coras7e5e62b2019-07-30 14:08:23 -07002668 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002669 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002670 s = vcl_session_get_w_handle (wrk, sh);
2671 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002672 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002673 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002674 goto done;
2675 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002676 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002677 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002678 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002679 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002680 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002681 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002682 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002683 goto done;
2684 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002685 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002686 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2687 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002688 sh, s->vep.vep_sh, vep_handle);
2689 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002690 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002691 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002692 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002693 " next_sh = 0x%x (%u)\n"
2694 " prev_sh = 0x%x (%u)\n"
2695 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002696 " ev.events = 0x%x\n"
2697 " ev.data.u64 = 0x%llx\n"
2698 " et_mask = 0x%x\n"
2699 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002700 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002701 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2702 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002703 }
2704 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002705
2706done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002707 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002708}
2709
2710int
2711vppcom_epoll_create (void)
2712{
Florin Coras134a9962018-08-28 11:32:04 -07002713 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002714 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715
Florin Coras134a9962018-08-28 11:32:04 -07002716 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002717
Florin Coras6c3b2182020-10-19 18:36:48 -07002718 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002719 vep_session->vep.vep_sh = ~0;
2720 vep_session->vep.next_sh = ~0;
2721 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002722 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002723
Florin Corasa7a1a222018-12-30 17:11:31 -08002724 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2725 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002726
Florin Corasab2f6db2018-08-31 14:31:41 -07002727 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002728}
2729
2730int
Florin Coras134a9962018-08-28 11:32:04 -07002731vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002732 struct epoll_event *event)
2733{
Florin Coras134a9962018-08-28 11:32:04 -07002734 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002735 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002736 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002737 vcl_session_t *s;
2738 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002739
Florin Coras134a9962018-08-28 11:32:04 -07002740 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002741 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002742 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743 return VPPCOM_EINVAL;
2744 }
2745
Florin Coras134a9962018-08-28 11:32:04 -07002746 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002747 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002748 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002749 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002750 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002752 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002753 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002754 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002755 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002756 }
2757
Florin Coras134a9962018-08-28 11:32:04 -07002758 ASSERT (vep_session->vep.vep_sh == ~0);
2759 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002760
Florin Coras17ec5772020-08-12 20:46:29 -07002761 s = vcl_session_get_w_handle (wrk, session_handle);
2762 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002763 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002764 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002765 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002766 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002767 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002768 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002769 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002770 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 }
2772
2773 switch (op)
2774 {
2775 case EPOLL_CTL_ADD:
2776 if (PREDICT_FALSE (!event))
2777 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002778 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002779 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002780 }
Florin Coras2645f682021-06-04 15:59:41 -07002781 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2782 {
2783 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2784 rv = VPPCOM_EEXIST;
2785 goto done;
2786 }
Florin Coras134a9962018-08-28 11:32:04 -07002787 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002788 {
Florin Coras7e12d942018-06-27 14:32:43 -07002789 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002790 next_session = vcl_session_get_w_handle (wrk,
2791 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002792 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002793 {
Florin Coras5e062572019-03-14 19:07:51 -07002794 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002795 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002796 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002797 }
Florin Coras134a9962018-08-28 11:32:04 -07002798 ASSERT (next_session->vep.prev_sh == vep_handle);
2799 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002800 }
Florin Coras17ec5772020-08-12 20:46:29 -07002801 s->vep.next_sh = vep_session->vep.next_sh;
2802 s->vep.prev_sh = vep_handle;
2803 s->vep.vep_sh = vep_handle;
2804 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002805 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002806 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002807 s->flags &= ~VCL_SESSION_F_IS_VEP;
2808 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002809 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002810
Florin Coras17ec5772020-08-12 20:46:29 -07002811 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2812 if (txf && (event->events & EPOLLOUT))
2813 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002814
Florin Coras6e3c1f82020-01-15 01:30:46 +00002815 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002816 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002817 {
2818 session_event_t e = { 0 };
2819 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002820 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002821 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002822 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002823 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002824 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002825 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002826 {
2827 session_event_t e = { 0 };
2828 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002829 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002830 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002831 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2832 add_evt = 1;
2833 }
2834 if (!add_evt && vcl_session_is_closing (s))
2835 {
2836 session_event_t e = { 0 };
2837 if (s->session_state == VCL_STATE_VPP_CLOSING)
2838 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2839 else
2840 e.event_type = SESSION_CTRL_EVT_RESET;
2841 e.session_index = s->session_index;
2842 e.postponed = 1;
2843 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002844 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002845 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2846 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002847 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002848 break;
2849
2850 case EPOLL_CTL_MOD:
2851 if (PREDICT_FALSE (!event))
2852 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002853 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002854 rv = VPPCOM_EINVAL;
2855 goto done;
2856 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002857 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002858 {
Florin Coras5e062572019-03-14 19:07:51 -07002859 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002860 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002861 goto done;
2862 }
Florin Coras17ec5772020-08-12 20:46:29 -07002863 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002864 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002865 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002866 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002867 rv = VPPCOM_EINVAL;
2868 goto done;
2869 }
hanlin475c9d72019-12-26 11:44:28 +08002870
Florin Coras2645f682021-06-04 15:59:41 -07002871 /* Generate EPOLLOUT if session write ready nd event was not on */
2872 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2873 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002874 {
2875 session_event_t e = { 0 };
2876 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002877 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002878 vec_add1 (wrk->unhandled_evts_vector, e);
2879 }
Florin Coras2645f682021-06-04 15:59:41 -07002880 /* Generate EPOLLIN if session read ready and event was not on */
2881 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2882 (vcl_session_read_ready (s) > 0))
2883 {
2884 session_event_t e = { 0 };
2885 e.event_type = SESSION_IO_EVT_RX;
2886 e.session_index = s->session_index;
2887 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002888 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002889 }
Florin Coras17ec5772020-08-12 20:46:29 -07002890 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2891 s->vep.ev = *event;
2892 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002893 if (txf)
2894 {
2895 if (event->events & EPOLLOUT)
2896 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2897 else
2898 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2899 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002900 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2901 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002902 break;
2903
2904 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002905 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002906 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002907 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002908 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002909 goto done;
2910 }
Florin Coras17ec5772020-08-12 20:46:29 -07002911 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002912 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002913 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002914 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002915 rv = VPPCOM_EINVAL;
2916 goto done;
2917 }
2918
Florin Coras17ec5772020-08-12 20:46:29 -07002919 if (s->vep.prev_sh == vep_handle)
2920 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002921 else
2922 {
Florin Coras7e12d942018-06-27 14:32:43 -07002923 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002924 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002925 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002926 {
Florin Coras5e062572019-03-14 19:07:51 -07002927 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002928 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002929 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002930 }
Florin Coras134a9962018-08-28 11:32:04 -07002931 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002932 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002933 }
Florin Coras17ec5772020-08-12 20:46:29 -07002934 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002935 {
Florin Coras7e12d942018-06-27 14:32:43 -07002936 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002937 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002938 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002939 {
Florin Coras5e062572019-03-14 19:07:51 -07002940 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002941 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002942 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002943 }
Florin Coras134a9962018-08-28 11:32:04 -07002944 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002945 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002946 }
2947
Florin Corasfa3884f2021-06-22 20:04:46 -07002948 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2949 vcl_epoll_lt_del (wrk, s);
2950
Florin Coras17ec5772020-08-12 20:46:29 -07002951 memset (&s->vep, 0, sizeof (s->vep));
2952 s->vep.next_sh = ~0;
2953 s->vep.prev_sh = ~0;
2954 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002955 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002956 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002957
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002958 if (vcl_session_is_open (s))
2959 {
2960 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2961 if (txf)
2962 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2963 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002964
Florin Coras5e062572019-03-14 19:07:51 -07002965 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002966 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002967 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002968 break;
2969
2970 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002971 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002972 rv = VPPCOM_EINVAL;
2973 }
2974
Florin Coras134a9962018-08-28 11:32:04 -07002975 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002976
2977done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002978 return rv;
2979}
2980
Florin Coras86f04502018-09-12 16:08:01 -07002981static inline void
2982vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2983 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002984{
2985 session_disconnected_msg_t *disconnected_msg;
2986 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002987 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002988 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002989 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002990 u8 add_event = 0;
2991
2992 switch (e->event_type)
2993 {
Florin Coras653e43f2019-03-04 10:56:23 -08002994 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002995 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002996 s = vcl_session_get (wrk, sid);
2997 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002998 break;
Florin Corasb242d312020-10-26 15:35:40 -07002999 vcl_fifo_rx_evt_valid_or_break (s);
3000 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003001 if (!(EPOLLIN & s->vep.ev.events) ||
3002 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3003 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003004 break;
3005 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003006 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003007 session_evt_data = s->vep.ev.data.u64;
3008 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003009 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003010 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003011 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003012 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003013 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003014 break;
Florin Corasb242d312020-10-26 15:35:40 -07003015 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003016 if (!(EPOLLOUT & session_events))
3017 break;
3018 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003019 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003020 session_evt_data = s->vep.ev.data.u64;
3021 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3022 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003023 break;
Florin Coras86f04502018-09-12 16:08:01 -07003024 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003025 if (!e->postponed)
3026 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3027 else
3028 s = vcl_session_get (wrk, e->session_index);
3029 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003030 break;
Florin Corasb242d312020-10-26 15:35:40 -07003031 session_events = s->vep.ev.events;
3032 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003033 if (!(EPOLLIN & session_events) ||
3034 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003035 break;
Florin Coras86f04502018-09-12 16:08:01 -07003036 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003037 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003038 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003039 break;
3040 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003041 if (!e->postponed)
3042 {
3043 connected_msg = (session_connected_msg_t *) e->data;
3044 sid = vcl_session_connected_handler (wrk, connected_msg);
3045 }
3046 else
3047 sid = e->session_index;
3048 s = vcl_session_get (wrk, sid);
3049 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003050 break;
Florin Corasb242d312020-10-26 15:35:40 -07003051 session_events = s->vep.ev.events;
3052 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003053 if (!(EPOLLOUT & session_events))
3054 break;
3055 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003056 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003057 session_evt_data = s->vep.ev.data.u64;
3058 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003059 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003060 break;
3061 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003062 if (!e->postponed)
3063 {
3064 disconnected_msg = (session_disconnected_msg_t *) e->data;
3065 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3066 }
3067 else
3068 {
3069 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003070 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003071 }
3072 if (vcl_session_is_closed (s) ||
3073 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003074 {
Florin Coras74254b52021-12-08 11:03:08 -08003075 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003076 vcl_session_free (wrk, s);
3077 break;
3078 }
Florin Corasb242d312020-10-26 15:35:40 -07003079 sid = s->session_index;
3080 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003081 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003082 if (EPOLLRDHUP & session_events)
3083 {
3084 /* If app can distinguish between RDHUP and HUP,
3085 * we make finer control */
3086 events[*num_ev].events = EPOLLRDHUP;
3087 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3088 {
3089 events[*num_ev].events |= EPOLLHUP;
3090 }
3091 }
3092 else
3093 {
3094 events[*num_ev].events = EPOLLHUP;
3095 }
Florin Corasb242d312020-10-26 15:35:40 -07003096 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003097
Florin Coras86f04502018-09-12 16:08:01 -07003098 break;
3099 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003100 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003101 {
3102 sid =
3103 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3104 s = vcl_session_get (wrk, sid);
3105 }
Florin Coras87f76002021-06-28 19:13:29 -07003106 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003107 {
3108 sid = e->session_index;
3109 s = vcl_session_get (wrk, sid);
3110 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3111 }
Florin Coras87f76002021-06-28 19:13:29 -07003112 if (vcl_session_is_closed (s) ||
3113 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003114 {
Florin Coras74254b52021-12-08 11:03:08 -08003115 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003116 vcl_session_free (wrk, s);
3117 break;
3118 }
Florin Corasb242d312020-10-26 15:35:40 -07003119 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003120 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003121 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003122 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003123 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003124 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3125 vcl_session_unlisten_reply_handler (wrk, e->data);
3126 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003127 case SESSION_CTRL_EVT_MIGRATED:
3128 vcl_session_migrated_handler (wrk, e->data);
3129 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003130 case SESSION_CTRL_EVT_CLEANUP:
3131 vcl_session_cleanup_handler (wrk, e->data);
3132 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003133 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3134 vcl_session_req_worker_update_handler (wrk, e->data);
3135 break;
3136 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3137 vcl_session_worker_update_reply_handler (wrk, e->data);
3138 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003139 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3140 vcl_session_app_add_segment_handler (wrk, e->data);
3141 break;
3142 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3143 vcl_session_app_del_segment_handler (wrk, e->data);
3144 break;
hanlina3a48962020-07-13 11:09:15 +08003145 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003146 vcl_worker_rpc_handler (wrk, e->data);
3147 break;
Florin Coras86f04502018-09-12 16:08:01 -07003148 default:
3149 VDBG (0, "unhandled: %u", e->event_type);
3150 break;
3151 }
3152
3153 if (add_event)
3154 {
3155 events[*num_ev].data.u64 = session_evt_data;
3156 if (EPOLLONESHOT & session_events)
3157 {
Florin Corasb242d312020-10-26 15:35:40 -07003158 s = vcl_session_get (wrk, sid);
3159 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003160 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003161 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003162 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003163 s = vcl_session_get (wrk, sid);
3164 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3165 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003166 }
Florin Coras86f04502018-09-12 16:08:01 -07003167 *num_ev += 1;
3168 }
3169}
3170
3171static int
3172vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3173 struct epoll_event *events, u32 maxevents,
3174 double wait_for_time, u32 * num_ev)
3175{
Florin Coras99368312018-08-02 10:45:44 -07003176 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003177 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003178 int i;
3179
Florin Coras539663c2018-09-28 14:59:37 -07003180 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3181 goto handle_dequeued;
3182
Florin Coras54693d22018-07-17 10:46:29 -07003183 if (svm_msg_q_is_empty (mq))
3184 {
3185 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003186 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003187 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003188 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003189 else
3190 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003191 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003192 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003193 }
3194 }
Florin Corase003a1b2019-06-05 10:47:16 -07003195 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003196 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003197
Florin Coras539663c2018-09-28 14:59:37 -07003198handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003199 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003200 {
Florin Coras134a9962018-08-28 11:32:04 -07003201 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003202 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003203 if (*num_ev < maxevents)
3204 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3205 else
3206 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003207 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003208 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003209 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003210 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003211 return *num_ev;
3212}
3213
Florin Coras99368312018-08-02 10:45:44 -07003214static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003215vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3216 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003217{
Florin Corasccdb8b82021-04-28 13:01:06 -07003218 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003219
3220 if (!n_evts)
3221 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003222 if (timeout_ms > 0)
3223 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003224 }
3225
3226 do
3227 {
3228 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003229 timeout_ms, &n_evts);
3230 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003231 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003232 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003233 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003234
3235 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003236}
3237
3238static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003239vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3240 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003241{
Florin Coras99368312018-08-02 10:45:44 -07003242 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003243 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003244 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003245 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003246 u64 buf;
3247
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003248 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3249 {
3250 vcl_api_retry_attach (wrk);
3251 return n_evts;
3252 }
3253
Florin Coras134a9962018-08-28 11:32:04 -07003254 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003255 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003256 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003257 if (timeout_ms > 0)
3258 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003259 }
3260
Florin Corasb13ba132021-01-27 16:05:24 -08003261 do
3262 {
3263 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003264 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003265 if (n_mq_evts < 0)
3266 {
3267 VDBG (0, "epoll_wait error %u", errno);
3268 return n_evts;
3269 }
3270
3271 for (i = 0; i < n_mq_evts; i++)
3272 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003273 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3274 {
3275 /* api socket was closed */
3276 vcl_api_handle_disconnect (wrk);
3277 continue;
3278 }
3279
Florin Corasb13ba132021-01-27 16:05:24 -08003280 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3281 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3282 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3283 &n_evts);
3284 }
3285
Florin Corasccdb8b82021-04-28 13:01:06 -07003286 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003287 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003288 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003289 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003290
3291 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003292}
3293
Florin Corasfe286f72021-06-04 10:07:55 -07003294static void
Florin Corasfe286f72021-06-04 10:07:55 -07003295vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3296 int maxevents, u32 *n_evts)
3297{
Florin Coras734268f2021-06-30 07:54:29 -07003298 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003299 vcl_session_t *s;
3300 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003301 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003302
Florin Corasfa3884f2021-06-22 20:04:46 -07003303 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003304 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003305 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003306
Florin Corasfa3884f2021-06-22 20:04:46 -07003307 next = wrk->ep_lt_current;
3308 do
Florin Corasfe286f72021-06-04 10:07:55 -07003309 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003310 s = vcl_session_get (wrk, next);
3311 next = s->vep.lt_next;
3312
3313 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003314 {
3315 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003316 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003317 evt_data = s->vep.ev.data.u64;
3318 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003319 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003320 {
3321 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003322 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3323 evt_data = s->vep.ev.data.u64;
3324 }
3325 if (!add_event && s->session_state > VCL_STATE_READY)
3326 {
3327 add_event = 1;
3328 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003329 evt_data = s->vep.ev.data.u64;
3330 }
3331 if (add_event)
3332 {
3333 events[*n_evts].data.u64 = evt_data;
3334 *n_evts += 1;
3335 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003336 if (EPOLLONESHOT & s->vep.ev.events)
3337 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003338 if (*n_evts == maxevents)
3339 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003340 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003341 break;
3342 }
3343 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003344 else
3345 {
3346 vcl_epoll_lt_del (wrk, s);
3347 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3348 break;
3349 }
Florin Corasfe286f72021-06-04 10:07:55 -07003350 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003351 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003352}
3353
Dave Wallacef7f809c2017-10-03 01:48:42 -04003354int
Florin Coras134a9962018-08-28 11:32:04 -07003355vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003356 int maxevents, double wait_for_time)
3357{
Florin Coras134a9962018-08-28 11:32:04 -07003358 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003359 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003360 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003361 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003362
3363 if (PREDICT_FALSE (maxevents <= 0))
3364 {
Florin Coras5e062572019-03-14 19:07:51 -07003365 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003366 return VPPCOM_EINVAL;
3367 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003368
Florin Coras134a9962018-08-28 11:32:04 -07003369 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003370 if (!vep_session)
3371 return VPPCOM_EBADFD;
3372
Florin Coras6c3b2182020-10-19 18:36:48 -07003373 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003374 {
Florin Coras5e062572019-03-14 19:07:51 -07003375 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003376 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003377 }
Florin Coras54693d22018-07-17 10:46:29 -07003378
Florin Coras86f04502018-09-12 16:08:01 -07003379 if (vec_len (wrk->unhandled_evts_vector))
3380 {
3381 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3382 {
3383 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3384 events, &n_evts);
3385 if (n_evts == maxevents)
3386 {
Florin Corase003a1b2019-06-05 10:47:16 -07003387 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3388 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003389 }
3390 }
Florin Corase003a1b2019-06-05 10:47:16 -07003391 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003392 }
liuyacancba87102021-09-10 15:14:05 +08003393
3394 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3395 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3396
wanghanlin8919fec2021-03-18 20:00:41 +08003397 /* Request to only drain unhandled */
3398 if ((int) wait_for_time == -2)
3399 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003400
Florin Coras86f04502018-09-12 16:08:01 -07003401
Florin Corasfe286f72021-06-04 10:07:55 -07003402 if (vcm->cfg.use_mq_eventfd)
3403 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3404 wait_for_time);
3405 else
3406 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3407 wait_for_time);
3408
Florin Corasfe286f72021-06-04 10:07:55 -07003409 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003410}
3411
Dave Wallace35830af2017-10-09 01:43:42 -04003412int
Florin Coras134a9962018-08-28 11:32:04 -07003413vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003414 void *buffer, uint32_t * buflen)
3415{
Florin Coras134a9962018-08-28 11:32:04 -07003416 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003417 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003418 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003419 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003420 vcl_session_t *session;
3421 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003422
Florin Coras134a9962018-08-28 11:32:04 -07003423 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003424 if (!session)
3425 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003426
Dave Wallace35830af2017-10-09 01:43:42 -04003427 switch (op)
3428 {
3429 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003430 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003431 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3432 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003433 break;
3434
Dave Wallace227867f2017-11-13 21:21:53 -05003435 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003436 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003437 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3438 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003439 break;
3440
3441 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003442 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003443 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003444 *flags =
3445 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003446 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003447 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003448 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003449 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3450 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003451 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003452 }
3453 else
3454 rv = VPPCOM_EINVAL;
3455 break;
3456
3457 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003458 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003459 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003460 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003461 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003462 else
Florin Corasac422d62020-10-19 20:51:36 -07003463 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003464
Florin Coras5e062572019-03-14 19:07:51 -07003465 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3466 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003467 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003468 }
3469 else
3470 rv = VPPCOM_EINVAL;
3471 break;
3472
3473 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003474 if (PREDICT_TRUE (buffer && buflen &&
3475 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003476 {
Florin Coras7e12d942018-06-27 14:32:43 -07003477 ep->is_ip4 = session->transport.is_ip4;
3478 ep->port = session->transport.rmt_port;
3479 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003480 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3481 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003482 else
Dave Barach178cf492018-11-13 16:34:13 -05003483 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3484 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003485 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003486 VDBG (1,
3487 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3488 "addr = %U, port %u",
3489 session_handle, ep->is_ip4, vcl_format_ip46_address,
3490 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003491 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3492 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003493 }
3494 else
3495 rv = VPPCOM_EINVAL;
3496 break;
3497
3498 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003499 if (PREDICT_TRUE (buffer && buflen &&
3500 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003501 {
Florin Coras7e12d942018-06-27 14:32:43 -07003502 ep->is_ip4 = session->transport.is_ip4;
3503 ep->port = session->transport.lcl_port;
3504 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003505 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3506 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003507 else
Dave Barach178cf492018-11-13 16:34:13 -05003508 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3509 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003510 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003511 VDBG (1,
3512 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3513 " port %d",
3514 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003515 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003516 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3517 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003518 }
3519 else
3520 rv = VPPCOM_EINVAL;
3521 break;
Stevenb5a11602017-10-11 09:59:30 -07003522
Florin Corasef7cbf62019-10-17 09:56:27 -07003523 case VPPCOM_ATTR_SET_LCL_ADDR:
3524 if (PREDICT_TRUE (buffer && buflen &&
3525 (*buflen >= sizeof (*ep)) && ep->ip))
3526 {
3527 session->transport.is_ip4 = ep->is_ip4;
3528 session->transport.lcl_port = ep->port;
3529 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3530 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003531 VDBG (1,
3532 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3533 " port %d",
3534 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003535 &session->transport.lcl_ip,
3536 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3537 clib_net_to_host_u16 (ep->port));
3538 }
3539 else
3540 rv = VPPCOM_EINVAL;
3541 break;
3542
Dave Wallace048b1d62018-01-03 22:24:41 -05003543 case VPPCOM_ATTR_GET_LIBC_EPFD:
3544 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003545 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003546 break;
3547
3548 case VPPCOM_ATTR_SET_LIBC_EPFD:
3549 if (PREDICT_TRUE (buffer && buflen &&
3550 (*buflen == sizeof (session->libc_epfd))))
3551 {
3552 session->libc_epfd = *(int *) buffer;
3553 *buflen = sizeof (session->libc_epfd);
3554
Florin Coras5e062572019-03-14 19:07:51 -07003555 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3556 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003557 }
3558 else
3559 rv = VPPCOM_EINVAL;
3560 break;
3561
3562 case VPPCOM_ATTR_GET_PROTOCOL:
3563 if (buffer && buflen && (*buflen >= sizeof (int)))
3564 {
Florin Coras7e12d942018-06-27 14:32:43 -07003565 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003566 *buflen = sizeof (int);
3567
Florin Coras5e062572019-03-14 19:07:51 -07003568 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3569 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003570 }
3571 else
3572 rv = VPPCOM_EINVAL;
3573 break;
3574
3575 case VPPCOM_ATTR_GET_LISTEN:
3576 if (buffer && buflen && (*buflen >= sizeof (int)))
3577 {
Florin Corasac422d62020-10-19 20:51:36 -07003578 *(int *) buffer = vcl_session_has_attr (session,
3579 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003580 *buflen = sizeof (int);
3581
Florin Coras5e062572019-03-14 19:07:51 -07003582 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3583 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003584 }
3585 else
3586 rv = VPPCOM_EINVAL;
3587 break;
3588
3589 case VPPCOM_ATTR_GET_ERROR:
3590 if (buffer && buflen && (*buflen >= sizeof (int)))
3591 {
3592 *(int *) buffer = 0;
3593 *buflen = sizeof (int);
3594
Florin Coras5e062572019-03-14 19:07:51 -07003595 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3596 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003597 }
3598 else
3599 rv = VPPCOM_EINVAL;
3600 break;
3601
3602 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3603 if (buffer && buflen && (*buflen >= sizeof (u32)))
3604 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003605
3606 /* VPP-TBD */
3607 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003608 session->tx_fifo ?
3609 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003610 vcm->cfg.tx_fifo_size);
3611 *buflen = sizeof (u32);
3612
Florin Coras5e062572019-03-14 19:07:51 -07003613 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3614 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3615 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003616 }
3617 else
3618 rv = VPPCOM_EINVAL;
3619 break;
3620
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003621 case VPPCOM_ATTR_SET_DSCP:
3622 if (buffer && buflen && (*buflen >= sizeof (u8)))
3623 {
3624 session->dscp = *(u8 *) buffer;
3625
3626 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3627 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3628 }
3629 else
3630 rv = VPPCOM_EINVAL;
3631 break;
3632
Dave Wallace048b1d62018-01-03 22:24:41 -05003633 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3634 if (buffer && buflen && (*buflen == sizeof (u32)))
3635 {
3636 /* VPP-TBD */
3637 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003638 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3639 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3640 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003641 }
3642 else
3643 rv = VPPCOM_EINVAL;
3644 break;
3645
3646 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3647 if (buffer && buflen && (*buflen >= sizeof (u32)))
3648 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003649
3650 /* VPP-TBD */
3651 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003652 session->rx_fifo ?
3653 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003654 vcm->cfg.rx_fifo_size);
3655 *buflen = sizeof (u32);
3656
Florin Coras5e062572019-03-14 19:07:51 -07003657 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3658 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003659 }
3660 else
3661 rv = VPPCOM_EINVAL;
3662 break;
3663
3664 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3665 if (buffer && buflen && (*buflen == sizeof (u32)))
3666 {
3667 /* VPP-TBD */
3668 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003669 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3670 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3671 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003672 }
3673 else
3674 rv = VPPCOM_EINVAL;
3675 break;
3676
3677 case VPPCOM_ATTR_GET_REUSEADDR:
3678 if (buffer && buflen && (*buflen >= sizeof (int)))
3679 {
3680 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003681 *(int *) buffer = vcl_session_has_attr (session,
3682 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003683 *buflen = sizeof (int);
3684
Florin Coras5e062572019-03-14 19:07:51 -07003685 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3686 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003687 }
3688 else
3689 rv = VPPCOM_EINVAL;
3690 break;
3691
Stevenb5a11602017-10-11 09:59:30 -07003692 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003693 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003694 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003695 {
3696 /* VPP-TBD */
3697 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003698 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003699 else
Florin Corasac422d62020-10-19 20:51:36 -07003700 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003701
Florin Coras5e062572019-03-14 19:07:51 -07003702 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003703 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003704 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003705 }
3706 else
3707 rv = VPPCOM_EINVAL;
3708 break;
3709
3710 case VPPCOM_ATTR_GET_REUSEPORT:
3711 if (buffer && buflen && (*buflen >= sizeof (int)))
3712 {
3713 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003714 *(int *) buffer = vcl_session_has_attr (session,
3715 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003716 *buflen = sizeof (int);
3717
Florin Coras5e062572019-03-14 19:07:51 -07003718 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3719 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003720 }
3721 else
3722 rv = VPPCOM_EINVAL;
3723 break;
3724
3725 case VPPCOM_ATTR_SET_REUSEPORT:
3726 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003727 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003728 {
3729 /* VPP-TBD */
3730 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003731 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003732 else
Florin Corasac422d62020-10-19 20:51:36 -07003733 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003734
Florin Coras5e062572019-03-14 19:07:51 -07003735 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003736 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003737 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003738 }
3739 else
3740 rv = VPPCOM_EINVAL;
3741 break;
3742
3743 case VPPCOM_ATTR_GET_BROADCAST:
3744 if (buffer && buflen && (*buflen >= sizeof (int)))
3745 {
3746 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003747 *(int *) buffer = vcl_session_has_attr (session,
3748 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003749 *buflen = sizeof (int);
3750
Florin Coras5e062572019-03-14 19:07:51 -07003751 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3752 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003753 }
3754 else
3755 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003756 break;
3757
3758 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003759 if (buffer && buflen && (*buflen == sizeof (int)))
3760 {
3761 /* VPP-TBD */
3762 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003763 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003764 else
Florin Corasac422d62020-10-19 20:51:36 -07003765 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003766
Florin Coras5e062572019-03-14 19:07:51 -07003767 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003768 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003769 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003770 }
3771 else
3772 rv = VPPCOM_EINVAL;
3773 break;
3774
3775 case VPPCOM_ATTR_GET_V6ONLY:
3776 if (buffer && buflen && (*buflen >= sizeof (int)))
3777 {
3778 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003779 *(int *) buffer = vcl_session_has_attr (session,
3780 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003781 *buflen = sizeof (int);
3782
Florin Coras5e062572019-03-14 19:07:51 -07003783 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3784 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003785 }
3786 else
3787 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003788 break;
3789
3790 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003791 if (buffer && buflen && (*buflen == sizeof (int)))
3792 {
3793 /* VPP-TBD */
3794 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003795 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003796 else
Florin Corasac422d62020-10-19 20:51:36 -07003797 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003798
Florin Coras5e062572019-03-14 19:07:51 -07003799 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003800 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003801 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003802 }
3803 else
3804 rv = VPPCOM_EINVAL;
3805 break;
3806
3807 case VPPCOM_ATTR_GET_KEEPALIVE:
3808 if (buffer && buflen && (*buflen >= sizeof (int)))
3809 {
3810 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003811 *(int *) buffer = vcl_session_has_attr (session,
3812 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003813 *buflen = sizeof (int);
3814
Florin Coras5e062572019-03-14 19:07:51 -07003815 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3816 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 }
3818 else
3819 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003820 break;
Stevenbccd3392017-10-12 20:42:21 -07003821
3822 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003823 if (buffer && buflen && (*buflen == sizeof (int)))
3824 {
3825 /* VPP-TBD */
3826 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003827 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 else
Florin Corasac422d62020-10-19 20:51:36 -07003829 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003830
Florin Coras5e062572019-03-14 19:07:51 -07003831 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003832 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003833 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003834 }
3835 else
3836 rv = VPPCOM_EINVAL;
3837 break;
3838
3839 case VPPCOM_ATTR_GET_TCP_NODELAY:
3840 if (buffer && buflen && (*buflen >= sizeof (int)))
3841 {
3842 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003843 *(int *) buffer = vcl_session_has_attr (session,
3844 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003845 *buflen = sizeof (int);
3846
Florin Coras5e062572019-03-14 19:07:51 -07003847 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3848 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003849 }
3850 else
3851 rv = VPPCOM_EINVAL;
3852 break;
3853
3854 case VPPCOM_ATTR_SET_TCP_NODELAY:
3855 if (buffer && buflen && (*buflen == sizeof (int)))
3856 {
3857 /* VPP-TBD */
3858 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003859 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003860 else
Florin Corasac422d62020-10-19 20:51:36 -07003861 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003862
Florin Coras5e062572019-03-14 19:07:51 -07003863 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003864 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003865 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003866 }
3867 else
3868 rv = VPPCOM_EINVAL;
3869 break;
3870
3871 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3872 if (buffer && buflen && (*buflen >= sizeof (int)))
3873 {
3874 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003875 *(int *) buffer = vcl_session_has_attr (session,
3876 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003877 *buflen = sizeof (int);
3878
Florin Coras5e062572019-03-14 19:07:51 -07003879 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3880 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003881 }
3882 else
3883 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003884 break;
3885
3886 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003887 if (buffer && buflen && (*buflen == sizeof (int)))
3888 {
3889 /* VPP-TBD */
3890 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003891 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003892 else
Florin Corasac422d62020-10-19 20:51:36 -07003893 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003894
Florin Coras5e062572019-03-14 19:07:51 -07003895 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003896 vcl_session_has_attr (session,
3897 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003898 }
3899 else
3900 rv = VPPCOM_EINVAL;
3901 break;
3902
3903 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3904 if (buffer && buflen && (*buflen >= sizeof (int)))
3905 {
3906 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003907 *(int *) buffer = vcl_session_has_attr (session,
3908 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003909 *buflen = sizeof (int);
3910
Florin Coras5e062572019-03-14 19:07:51 -07003911 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3912 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003913 }
3914 else
3915 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003916 break;
3917
3918 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003919 if (buffer && buflen && (*buflen == sizeof (int)))
3920 {
3921 /* VPP-TBD */
3922 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003923 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003924 else
Florin Corasac422d62020-10-19 20:51:36 -07003925 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003926
Florin Coras5e062572019-03-14 19:07:51 -07003927 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003928 vcl_session_has_attr (session,
3929 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003930 }
3931 else
3932 rv = VPPCOM_EINVAL;
3933 break;
3934
3935 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003936 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003937 {
Florin Coras04ae8272021-04-12 19:55:37 -07003938 rv = VPPCOM_EINVAL;
3939 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003940 }
Florin Coras04ae8272021-04-12 19:55:37 -07003941
3942 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3943 tea.mss = *(u32 *) buffer;
3944 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3945 rv = VPPCOM_ENOPROTOOPT;
3946
3947 if (!rv)
3948 {
3949 *(u32 *) buffer = tea.mss;
3950 *buflen = sizeof (int);
3951 }
3952
3953 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3954 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003955 break;
3956
3957 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003958 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003959 {
Florin Coras04ae8272021-04-12 19:55:37 -07003960 rv = VPPCOM_EINVAL;
3961 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003962 }
Florin Coras04ae8272021-04-12 19:55:37 -07003963
3964 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3965 tea.mss = *(u32 *) buffer;
3966 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3967 rv = VPPCOM_ENOPROTOOPT;
3968
3969 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3970 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003971 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003972
Florin Coras1e966172020-05-16 18:18:14 +00003973 case VPPCOM_ATTR_SET_CONNECTED:
3974 session->flags |= VCL_SESSION_F_CONNECTED;
3975 break;
3976
Florin Corasa5a9efd2021-01-05 17:03:29 -08003977 case VPPCOM_ATTR_SET_CKPAIR:
3978 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3979 !vcl_session_has_crypto (session))
3980 {
3981 rv = VPPCOM_EINVAL;
3982 break;
3983 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003984 if (!session->ext_config)
3985 {
Florin Coras87f63892021-05-01 16:01:40 -07003986 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3987 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003988 }
3989 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3990 {
3991 rv = VPPCOM_EINVAL;
3992 break;
3993 }
3994
3995 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003996 break;
3997
Florin Coras6a6555a2021-01-28 11:39:27 -08003998 case VPPCOM_ATTR_SET_VRF:
3999 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4000 {
4001 rv = VPPCOM_EINVAL;
4002 break;
4003 }
4004 session->vrf = *(u32 *) buffer;
4005 break;
4006
4007 case VPPCOM_ATTR_GET_VRF:
4008 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4009 {
4010 rv = VPPCOM_EINVAL;
4011 break;
4012 }
4013 *(u32 *) buffer = session->vrf;
4014 *buflen = sizeof (u32);
4015 break;
4016
wanghanlin0674f852021-02-22 10:38:36 +08004017 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004018 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004019 {
Florin Corasd77325c2021-02-23 12:03:03 -08004020 rv = VPPCOM_EINVAL;
4021 break;
wanghanlin0674f852021-02-22 10:38:36 +08004022 }
Florin Corasd77325c2021-02-23 12:03:03 -08004023
4024 if (session->transport.is_ip4)
4025 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004026 else
Florin Corasd77325c2021-02-23 12:03:03 -08004027 *(int *) buffer = AF_INET6;
4028 *buflen = sizeof (int);
4029
wanghanlin0674f852021-02-22 10:38:36 +08004030 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4031 *buflen);
4032 break;
4033
Florin Coras87f63892021-05-01 16:01:40 -07004034 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4035 if (!(buffer && buflen && (*buflen > 0)))
4036 {
4037 rv = VPPCOM_EINVAL;
4038 break;
4039 }
4040 if (session->ext_config)
4041 {
4042 rv = VPPCOM_EINVAL;
4043 break;
4044 }
4045 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4046 *buflen + sizeof (u32));
4047 clib_memcpy (session->ext_config->data, buffer, *buflen);
4048 session->ext_config->len = *buflen;
4049 break;
4050
Dave Wallacee22aa742017-10-20 12:30:38 -04004051 default:
4052 rv = VPPCOM_EINVAL;
4053 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004054 }
4055
Dave Wallace35830af2017-10-09 01:43:42 -04004056 return rv;
4057}
4058
Stevenac1f96d2017-10-24 16:03:58 -07004059int
Florin Coras134a9962018-08-28 11:32:04 -07004060vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004061 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4062{
Florin Coras134a9962018-08-28 11:32:04 -07004063 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004064 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004065 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004066
4067 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004068 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004069 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004070 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004071 else
4072 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004073 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004074 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004075 }
4076
Florin Coras0a1e1832020-03-29 18:54:04 +00004077 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004078 {
Florin Coras5da10c42020-04-01 04:31:21 +00004079 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004080 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004081 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4082 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004083 else
Dave Barach178cf492018-11-13 16:34:13 -05004084 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4085 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004086 ep->is_ip4 = session->transport.is_ip4;
4087 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004088 }
Florin Coras460dce62018-07-27 05:45:06 -07004089
Stevenac1f96d2017-10-24 16:03:58 -07004090 return rv;
4091}
4092
4093int
Florin Coras134a9962018-08-28 11:32:04 -07004094vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004095 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4096{
Florin Coras7a2abce2020-04-05 19:25:44 +00004097 vcl_worker_t *wrk = vcl_worker_get_current ();
4098 vcl_session_t *s;
4099
4100 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004101 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004102 return VPPCOM_EBADFD;
4103
Dave Wallace617dffa2017-10-26 14:47:06 -04004104 if (ep)
4105 {
Florin Coras5824cc52020-10-20 18:44:41 -07004106 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004107 return VPPCOM_EINVAL;
4108
liuyacanbc0c7542021-08-02 20:15:05 +08004109 s->transport.is_ip4 = ep->is_ip4;
4110 s->transport.rmt_port = ep->port;
4111 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4112
Florin Coras5da10c42020-04-01 04:31:21 +00004113 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004114 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004115 {
Florin Coras5824cc52020-10-20 18:44:41 -07004116 u32 session_index = s->session_index;
4117 f64 timeout = vcm->cfg.session_timeout;
4118 int rv;
4119
Florin Coras5da10c42020-04-01 04:31:21 +00004120 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004121 rv = vppcom_wait_for_session_state_change (session_index,
4122 VCL_STATE_READY,
4123 timeout);
4124 if (rv < 0)
4125 return rv;
4126 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004127 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004128 }
4129
4130 if (flags)
4131 {
4132 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004133 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004134 }
4135
Florin Coras7a2abce2020-04-05 19:25:44 +00004136 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4137 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004138}
4139
Dave Wallace048b1d62018-01-03 22:24:41 -05004140int
4141vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4142{
Florin Coras134a9962018-08-28 11:32:04 -07004143 vcl_worker_t *wrk = vcl_worker_get_current ();
4144 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004145 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004146 svm_msg_q_msg_t msg;
4147 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004148 int rv, num_ev = 0;
4149
Florin Coras5e062572019-03-14 19:07:51 -07004150 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004151
4152 if (!vp)
4153 return VPPCOM_EFAULT;
4154
4155 do
4156 {
Florin Coras7e12d942018-06-27 14:32:43 -07004157 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004158
Florin Coras6917b942018-11-13 22:44:54 -08004159 /* Dequeue all events and drop all unhandled io events */
4160 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4161 {
4162 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4163 vcl_handle_mq_event (wrk, e);
4164 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4165 }
4166 vec_reset_length (wrk->unhandled_evts_vector);
4167
Dave Wallace048b1d62018-01-03 22:24:41 -05004168 for (i = 0; i < n_sids; i++)
4169 {
Florin Coras7baeb712019-01-04 17:05:43 -08004170 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004171 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004172 {
4173 vp[i].revents = POLLHUP;
4174 num_ev++;
4175 continue;
4176 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004177
Florin Coras6917b942018-11-13 22:44:54 -08004178 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004179
4180 if (POLLIN & vp[i].events)
4181 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004182 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004183 if (rv > 0)
4184 {
Florin Coras6917b942018-11-13 22:44:54 -08004185 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004186 num_ev++;
4187 }
4188 else if (rv < 0)
4189 {
4190 switch (rv)
4191 {
4192 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004193 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004194 break;
4195
4196 default:
Florin Coras6917b942018-11-13 22:44:54 -08004197 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004198 break;
4199 }
4200 num_ev++;
4201 }
4202 }
4203
4204 if (POLLOUT & vp[i].events)
4205 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004206 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004207 if (rv > 0)
4208 {
Florin Coras6917b942018-11-13 22:44:54 -08004209 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004210 num_ev++;
4211 }
4212 else if (rv < 0)
4213 {
4214 switch (rv)
4215 {
4216 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004217 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004218 break;
4219
4220 default:
Florin Coras6917b942018-11-13 22:44:54 -08004221 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004222 break;
4223 }
4224 num_ev++;
4225 }
4226 }
4227
Dave Wallace7e607a72018-06-18 18:41:32 -04004228 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004229 {
Florin Coras6917b942018-11-13 22:44:54 -08004230 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004231 num_ev++;
4232 }
4233 }
4234 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004235 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004236 }
4237 while ((num_ev == 0) && keep_trying);
4238
Dave Wallace048b1d62018-01-03 22:24:41 -05004239 return num_ev;
4240}
4241
Florin Coras99368312018-08-02 10:45:44 -07004242int
4243vppcom_mq_epoll_fd (void)
4244{
Florin Coras134a9962018-08-28 11:32:04 -07004245 vcl_worker_t *wrk = vcl_worker_get_current ();
4246 return wrk->mqs_epfd;
4247}
4248
4249int
Florin Coras30e79c22019-01-02 19:31:22 -08004250vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004251{
4252 return session_handle & 0xFFFFFF;
4253}
4254
4255int
Florin Coras30e79c22019-01-02 19:31:22 -08004256vppcom_session_worker (vcl_session_handle_t session_handle)
4257{
4258 return session_handle >> 24;
4259}
4260
4261int
Florin Coras134a9962018-08-28 11:32:04 -07004262vppcom_worker_register (void)
4263{
Florin Coras47c40e22018-11-26 17:01:36 -08004264 if (!vcl_worker_alloc_and_init ())
4265 return VPPCOM_EEXIST;
4266
Florin Coras47c40e22018-11-26 17:01:36 -08004267 if (vcl_worker_register_with_vpp ())
4268 return VPPCOM_EEXIST;
4269
4270 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004271}
4272
Florin Coras369db832019-07-08 12:34:45 -07004273void
4274vppcom_worker_unregister (void)
4275{
4276 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4277 vcl_set_worker_index (~0);
4278}
4279
Pivo6017ff02020-05-04 17:57:33 +02004280void
4281vppcom_worker_index_set (int index)
4282{
4283 vcl_set_worker_index (index);
4284}
4285
Florin Corasdfe4cf42018-11-28 22:13:45 -08004286int
4287vppcom_worker_index (void)
4288{
4289 return vcl_get_worker_index ();
4290}
4291
Florin Corase0982e52019-01-25 13:19:56 -08004292int
4293vppcom_worker_mqs_epfd (void)
4294{
4295 vcl_worker_t *wrk = vcl_worker_get_current ();
4296 if (!vcm->cfg.use_mq_eventfd)
4297 return -1;
4298 return wrk->mqs_epfd;
4299}
4300
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004301int
4302vppcom_session_is_connectable_listener (uint32_t session_handle)
4303{
4304 vcl_session_t *session;
4305 vcl_worker_t *wrk = vcl_worker_get_current ();
4306 session = vcl_session_get_w_handle (wrk, session_handle);
4307 if (!session)
4308 return VPPCOM_EBADFD;
4309 return vcl_session_is_connectable_listener (wrk, session);
4310}
4311
4312int
4313vppcom_session_listener (uint32_t session_handle)
4314{
4315 vcl_worker_t *wrk = vcl_worker_get_current ();
4316 vcl_session_t *listen_session, *session;
4317 session = vcl_session_get_w_handle (wrk, session_handle);
4318 if (!session)
4319 return VPPCOM_EBADFD;
4320 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4321 return VPPCOM_EBADFD;
4322 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4323 if (!listen_session)
4324 return VPPCOM_EBADFD;
4325 return vcl_session_handle (listen_session);
4326}
4327
4328int
4329vppcom_session_n_accepted (uint32_t session_handle)
4330{
4331 vcl_worker_t *wrk = vcl_worker_get_current ();
4332 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4333 if (!session)
4334 return VPPCOM_EBADFD;
4335 return session->n_accepted_sessions;
4336}
4337
Florin Coras66ec4672020-06-15 07:59:40 -07004338const char *
4339vppcom_proto_str (vppcom_proto_t proto)
4340{
4341 char const *proto_str;
4342
4343 switch (proto)
4344 {
4345 case VPPCOM_PROTO_TCP:
4346 proto_str = "TCP";
4347 break;
4348 case VPPCOM_PROTO_UDP:
4349 proto_str = "UDP";
4350 break;
4351 case VPPCOM_PROTO_TLS:
4352 proto_str = "TLS";
4353 break;
4354 case VPPCOM_PROTO_QUIC:
4355 proto_str = "QUIC";
4356 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004357 case VPPCOM_PROTO_DTLS:
4358 proto_str = "DTLS";
4359 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004360 case VPPCOM_PROTO_SRTP:
4361 proto_str = "SRTP";
4362 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004363 default:
4364 proto_str = "UNKNOWN";
4365 break;
4366 }
4367 return proto_str;
4368}
4369
4370const char *
4371vppcom_retval_str (int retval)
4372{
4373 char const *st;
4374
4375 switch (retval)
4376 {
4377 case VPPCOM_OK:
4378 st = "VPPCOM_OK";
4379 break;
4380
4381 case VPPCOM_EAGAIN:
4382 st = "VPPCOM_EAGAIN";
4383 break;
4384
4385 case VPPCOM_EFAULT:
4386 st = "VPPCOM_EFAULT";
4387 break;
4388
4389 case VPPCOM_ENOMEM:
4390 st = "VPPCOM_ENOMEM";
4391 break;
4392
4393 case VPPCOM_EINVAL:
4394 st = "VPPCOM_EINVAL";
4395 break;
4396
4397 case VPPCOM_EBADFD:
4398 st = "VPPCOM_EBADFD";
4399 break;
4400
4401 case VPPCOM_EAFNOSUPPORT:
4402 st = "VPPCOM_EAFNOSUPPORT";
4403 break;
4404
4405 case VPPCOM_ECONNABORTED:
4406 st = "VPPCOM_ECONNABORTED";
4407 break;
4408
4409 case VPPCOM_ECONNRESET:
4410 st = "VPPCOM_ECONNRESET";
4411 break;
4412
4413 case VPPCOM_ENOTCONN:
4414 st = "VPPCOM_ENOTCONN";
4415 break;
4416
4417 case VPPCOM_ECONNREFUSED:
4418 st = "VPPCOM_ECONNREFUSED";
4419 break;
4420
4421 case VPPCOM_ETIMEDOUT:
4422 st = "VPPCOM_ETIMEDOUT";
4423 break;
4424
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304425 case VPPCOM_EADDRINUSE:
4426 st = "VPPCOM_EADDRINUSE";
4427 break;
4428
Florin Coras66ec4672020-06-15 07:59:40 -07004429 default:
4430 st = "UNKNOWN_STATE";
4431 break;
4432 }
4433
4434 return st;
4435}
4436
Florin Corasa5a9efd2021-01-05 17:03:29 -08004437int
4438vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4439{
4440 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004441 return vcl_sapi_add_cert_key_pair (ckpair);
4442 else
4443 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004444}
4445
4446int
4447vppcom_del_cert_key_pair (uint32_t ckpair_index)
4448{
4449 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004450 return vcl_sapi_del_cert_key_pair (ckpair_index);
4451 else
4452 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004453}
4454
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304455int
4456vppcom_session_get_error (uint32_t session_handle)
4457{
4458 vcl_worker_t *wrk = vcl_worker_get_current ();
4459 vcl_session_t *session = 0;
4460
4461 session = vcl_session_get_w_handle (wrk, session_handle);
4462 if (!session)
4463 return VPPCOM_EBADFD;
4464
4465 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4466 {
4467 VWRN ("epoll session %u! will not have connect", session->session_index);
4468 return VPPCOM_EBADFD;
4469 }
4470
4471 if (session->vpp_error == SESSION_E_PORTINUSE)
4472 return VPPCOM_EADDRINUSE;
4473 else if (session->vpp_error == SESSION_E_REFUSED)
4474 return VPPCOM_ECONNREFUSED;
4475 else if (session->vpp_error != SESSION_E_NONE)
4476 return VPPCOM_EFAULT;
4477 else
4478 return VPPCOM_OK;
4479}
4480
Dave Wallacee22aa742017-10-20 12:30:38 -04004481/*
4482 * fd.io coding-style-patch-verification: ON
4483 *
4484 * Local Variables:
4485 * eval: (c-set-style "gnu")
4486 * End:
4487 */