blob: 0e81749224e915e4659c1dedc6cf32c3e2c1083f [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);
292 wrk->session_attr_op = 1;
293 wrk->session_attr_op_rv = -1;
294
295 mq = s->vpp_evt_q;
296 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
297 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
298 memset (mp, 0, sizeof (*mp));
299 mp->client_index = wrk->api_client_handle;
300 mp->handle = s->vpp_handle;
301 mp->is_get = is_get;
302 mp->attr = *attr;
303 app_send_ctrl_evt_to_vpp (mq, app_evt);
304
305 timeout = clib_time_now (&wrk->clib_time) + 1;
306
307 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
308 vcl_flush_mq_events ();
309
310 if (!wrk->session_attr_op_rv && is_get)
311 *attr = wrk->session_attr_rv;
312
313 wrk->session_attr_op = 0;
314
315 return wrk->session_attr_op_rv;
316}
317
Florin Coras54693d22018-07-17 10:46:29 -0700318static u32
Florin Coras00cca802019-06-06 09:38:44 -0700319vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
320 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700321{
322 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700323 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700324
Florin Coras134a9962018-08-28 11:32:04 -0700325 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700326
Florin Coras00cca802019-06-06 09:38:44 -0700327 listen_session = vcl_session_get (wrk, ls_index);
328 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700329 {
Florin Coras00cca802019-06-06 09:38:44 -0700330 VDBG (0, "ERROR: listener handle %lu does not match session %u",
331 mp->listener_handle, ls_index);
332 goto error;
333 }
334
Florin Corasf6e284b2021-07-21 18:17:20 -0700335 if (vcl_segment_attach_session (
336 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
337 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700338 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800339 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
340 session->session_index, mp->handle);
Florin Coras00cca802019-06-06 09:38:44 -0700341 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700342 }
343
Florin Coras54693d22018-07-17 10:46:29 -0700344 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700345 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700346 session->transport.rmt_port = mp->rmt.port;
347 session->transport.is_ip4 = mp->rmt.is_ip4;
348 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500349 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700350
Florin Coras134a9962018-08-28 11:32:04 -0700351 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800352 session->transport.lcl_port = mp->lcl.port;
353 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700354 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200355 session->is_dgram = vcl_proto_is_dgram (session->session_type);
356 session->listener_index = listen_session->session_index;
357 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700358
Florin Coras54693d22018-07-17 10:46:29 -0700359 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
360
Florin Coras00cca802019-06-06 09:38:44 -0700361 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
362 session->vpp_handle, 0);
363
Florin Coras134a9962018-08-28 11:32:04 -0700364 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700365
366error:
Florin Corasb4624182020-12-11 13:58:12 -0800367 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
368 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700369 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
370 VNET_API_ERROR_INVALID_ARGUMENT);
371 vcl_session_free (wrk, session);
372 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700373}
374
375static u32
Florin Coras134a9962018-08-28 11:32:04 -0700376vcl_session_connected_handler (vcl_worker_t * wrk,
377 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700378{
Florin Coras99368312018-08-02 10:45:44 -0700379 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800380 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700381
382 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700383 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800384 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700385 {
Florin Coras8d42c752021-11-29 23:26:19 -0800386 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
387 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800388 /* Should not happen but if it does, force vpp session cleanup */
389 vcl_session_t tmp_session = {
390 .vpp_handle = mp->handle,
391 .vpp_evt_q = 0,
392 };
393 vcl_segment_attach_session (
394 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
395 mp->vpp_event_queue_address, mp->mq_index, 0, session);
396 if (tmp_session.vpp_evt_q)
397 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700398 return VCL_INVALID_SESSION_INDEX;
399 }
Florin Coras8d42c752021-11-29 23:26:19 -0800400
Florin Coras54693d22018-07-17 10:46:29 -0700401 if (mp->retval)
402 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800403 VDBG (0, "session %u: connect failed! %U", session_index,
Florin Coras8d42c752021-11-29 23:26:19 -0800404 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700405 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800406 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras070453d2018-08-24 17:04:27 -0700407 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700408 }
409
Florin Corasdbc9c592019-09-25 16:37:43 -0700410 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800411
Florin Coras8d42c752021-11-29 23:26:19 -0800412 /* Add to lookup table. Even if something fails, session cannot be
413 * cleaned up prior to notifying vpp and going through the cleanup
414 * "procedure" see @ref vcl_session_cleanup_handler */
415 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
416
Florin Corasf6e284b2021-07-21 18:17:20 -0700417 if (vcl_segment_attach_session (
418 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
419 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800420 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800421 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
422 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800423 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700424 vcl_send_session_disconnect (wrk, session);
425 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800426 }
427
Florin Coras653e43f2019-03-04 10:56:23 -0800428 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700429 {
Florin Corasc547e912020-12-08 17:50:45 -0800430 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700431 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
432 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800433 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800434 VDBG (0, "session %u [0x%llx]: failed to attach ct fifos",
435 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800436 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700437 vcl_send_session_disconnect (wrk, session);
438 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800439 }
Florin Coras99368312018-08-02 10:45:44 -0700440 }
Florin Coras54693d22018-07-17 10:46:29 -0700441
Florin Coras09d18c22019-04-24 11:10:02 -0700442 session->transport.is_ip4 = mp->lcl.is_ip4;
443 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500444 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700445 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700446
447 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700448 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700449 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700450 vcl_send_session_disconnect (wrk, session);
451 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700452 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700453
Florin Coras5ffb9642021-12-03 17:24:13 -0800454 VDBG (0, "session %u [0x%llx] connected local: %U:%u remote %U:%u",
455 session->session_index, session->vpp_handle, vcl_format_ip46_address,
456 &session->transport.lcl_ip,
457 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
458 clib_net_to_host_u16 (session->transport.lcl_port),
459 vcl_format_ip46_address, &session->transport.rmt_ip,
460 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
461 clib_net_to_host_u16 (session->transport.rmt_port));
Florin Coras070453d2018-08-24 17:04:27 -0700462
Florin Coras54693d22018-07-17 10:46:29 -0700463 return session_index;
464}
465
Florin Coras3c7d4f92018-12-14 11:28:43 -0800466static int
467vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
468{
469 vcl_session_msg_t *accepted_msg;
470 int i;
471
472 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
473 {
474 accepted_msg = &session->accept_evts_fifo[i];
475 if (accepted_msg->accepted_msg.handle == handle)
476 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800477 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800478 return 1;
479 }
480 }
481 return 0;
482}
483
Florin Corasc9fbd662018-08-24 12:59:56 -0700484static u32
Florin Coras134a9962018-08-28 11:32:04 -0700485vcl_session_reset_handler (vcl_worker_t * wrk,
486 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700487{
488 vcl_session_t *session;
489 u32 sid;
490
Florin Coras134a9962018-08-28 11:32:04 -0700491 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
492 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700493 if (!session)
494 {
495 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
496 return VCL_INVALID_SESSION_INDEX;
497 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800498
499 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700500 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800501 {
502
503 if (!vcl_flag_accepted_session (session, reset_msg->handle,
504 VCL_ACCEPTED_F_RESET))
505 VDBG (0, "session was not accepted!");
506 return VCL_INVALID_SESSION_INDEX;
507 }
508
Florin Corasc127d5a2020-10-14 16:35:58 -0700509 if (session->session_state != VCL_STATE_CLOSED)
510 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras5ffb9642021-12-03 17:24:13 -0800511 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700512 return sid;
513}
514
Florin Coras60116992018-08-27 09:52:18 -0700515static u32
Florin Coras134a9962018-08-28 11:32:04 -0700516vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700517{
518 vcl_session_t *session;
519 u32 sid = mp->context;
520
Florin Coras134a9962018-08-28 11:32:04 -0700521 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700522 if (mp->retval)
523 {
Florin Coras5e062572019-03-14 19:07:51 -0700524 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700525 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700526 if (session)
527 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700528 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700529 session->vpp_handle = mp->handle;
530 return sid;
531 }
532 else
533 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800534 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
535 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700536 return VCL_INVALID_SESSION_INDEX;
537 }
538 }
539
540 session->vpp_handle = mp->handle;
541 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500542 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
543 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700544 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700545 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700546 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800547
Florin Coras6e3c1f82020-01-15 01:30:46 +0000548 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700549 {
Florin Corasc547e912020-12-08 17:50:45 -0800550 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700551 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
552 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800553 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800554 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
555 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800556 session->session_state = VCL_STATE_DETACHED;
557 return VCL_INVALID_SESSION_INDEX;
558 }
Florin Coras60116992018-08-27 09:52:18 -0700559 }
560
Florin Coras05ecfcc2018-12-12 18:19:39 -0800561 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700562 return sid;
563}
564
Florin Corasdfae9f92019-02-20 19:48:31 -0800565static void
566vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
567{
568 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
569 vcl_session_t *s;
570
571 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000572 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800573 {
574 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
575 return;
576 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700577 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000578 {
579 /* Connected udp listener */
580 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700581 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000582 return;
583
584 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
585 return;
586 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800587
588 if (mp->retval)
589 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700590 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800591
592 if (mp->context != wrk->wrk_index)
593 VDBG (0, "wrong context");
594
595 vcl_session_table_del_vpp_handle (wrk, mp->handle);
596 vcl_session_free (wrk, s);
597}
598
Florin Coras68b7e582020-01-21 18:33:23 -0800599static void
600vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
601{
602 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
603 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800604 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800605
606 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
607 if (!s)
608 {
609 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
610 return;
611 }
612
Florin Coras1bb74942021-02-10 15:26:37 -0800613 /* Only validate if a value is provided */
614 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800615 {
Florin Coras1bb74942021-02-10 15:26:37 -0800616 fs_index = vcl_segment_table_lookup (mp->segment_handle);
617 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
618 {
619 VDBG (0, "segment %lx for session %u is not mounted!",
620 mp->segment_handle, s->session_index);
621 s->session_state = VCL_STATE_DETACHED;
622 return;
623 }
Florin Corasc547e912020-12-08 17:50:45 -0800624 }
625
Florin Coras57660d92020-04-04 22:45:34 +0000626 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800627
628 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
629 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800630
Florin Coras68b7e582020-01-21 18:33:23 -0800631 vcl_session_table_del_vpp_handle (wrk, mp->handle);
632 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
633
634 /* Generate new tx event if we have outstanding data */
635 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800636 app_send_io_evt_to_vpp (s->vpp_evt_q,
637 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800638 SESSION_IO_EVT_TX, SVM_Q_WAIT);
639
Florin Coras57660d92020-04-04 22:45:34 +0000640 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800641 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800642}
643
Florin Coras3c7d4f92018-12-14 11:28:43 -0800644static vcl_session_t *
645vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
646{
647 vcl_session_msg_t *vcl_msg;
648 vcl_session_t *session;
649
650 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
651 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800652 VWRN ("session overlap handle %lu state %u!", msg->handle,
653 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800654
655 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
656 if (!session)
657 {
658 VERR ("couldn't find listen session: listener handle %llx",
659 msg->listener_handle);
660 return 0;
661 }
662
663 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000664 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800665 vcl_msg->accepted_msg = *msg;
666 /* Session handle points to listener until fully accepted by app */
667 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
668
669 return session;
670}
671
672static vcl_session_t *
673vcl_session_disconnected_handler (vcl_worker_t * wrk,
674 session_disconnected_msg_t * msg)
675{
676 vcl_session_t *session;
677
678 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
679 if (!session)
680 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800681 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800682 return 0;
683 }
684
Florin Corasadcfb152020-02-12 08:50:29 +0000685 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700686 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000687 return 0;
688
Florin Coras3c7d4f92018-12-14 11:28:43 -0800689 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700690 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800691 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800692 if (!vcl_flag_accepted_session (session, msg->handle,
693 VCL_ACCEPTED_F_CLOSED))
694 VDBG (0, "session was not accepted!");
695 return 0;
696 }
697
Florin Corasadcfb152020-02-12 08:50:29 +0000698 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700699 if (session->session_state != VCL_STATE_DISCONNECT)
700 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000701
Florin Coras3c7d4f92018-12-14 11:28:43 -0800702 return session;
703}
704
liuyacan534468e2021-05-09 03:50:40 +0000705int
liuyacan55c952e2021-06-13 14:54:55 +0800706vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000707{
708 vcl_worker_t *wrk = vcl_worker_get_current ();
709 vcl_session_t *session;
710 vcl_session_state_t state;
711 u64 vpp_handle;
712
713 session = vcl_session_get_w_handle (wrk, session_handle);
714 if (PREDICT_FALSE (!session))
715 return VPPCOM_EBADFD;
716
717 vpp_handle = session->vpp_handle;
718 state = session->session_state;
719
720 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800721 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000722
723 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
724 {
725 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
726 return VPPCOM_EBADFD;
727 }
728
liuyacan55c952e2021-06-13 14:54:55 +0800729 if (how == SHUT_RD || how == SHUT_RDWR)
730 {
731 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
732 if (how == SHUT_RD)
733 return VPPCOM_OK;
734 }
735 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
736
liuyacan534468e2021-05-09 03:50:40 +0000737 if (PREDICT_TRUE (state == VCL_STATE_READY))
738 {
739 VDBG (1, "session %u [0x%llx]: sending shutdown...",
740 session->session_index, vpp_handle);
741
742 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000743 }
744
745 return VPPCOM_OK;
746}
747
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700748static int
749vppcom_session_disconnect (u32 session_handle)
750{
751 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700752 vcl_session_t *session, *listen_session;
753 vcl_session_state_t state;
754 u64 vpp_handle;
755
756 session = vcl_session_get_w_handle (wrk, session_handle);
757 if (!session)
758 return VPPCOM_EBADFD;
759
760 vpp_handle = session->vpp_handle;
761 state = session->session_state;
762
Florin Coras5ffb9642021-12-03 17:24:13 -0800763 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
764 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700765
766 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
767 {
768 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
769 return VPPCOM_EBADFD;
770 }
771
772 if (state == VCL_STATE_VPP_CLOSING)
773 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800774 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700775 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
776 session->session_index, vpp_handle);
777 }
778 else
779 {
780 /* Session doesn't have an event queue yet. Probably a non-blocking
781 * connect. Wait for the reply */
782 if (PREDICT_FALSE (!session->vpp_evt_q))
783 return VPPCOM_OK;
784
Florin Coras5ffb9642021-12-03 17:24:13 -0800785 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700786 session->session_index, vpp_handle);
787 vcl_send_session_disconnect (wrk, session);
788 }
789
790 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
791 {
792 listen_session = vcl_session_get (wrk, session->listener_index);
793 listen_session->n_accepted_sessions--;
794 }
795
796 return VPPCOM_OK;
797}
798
Florin Coras30e79c22019-01-02 19:31:22 -0800799static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700800vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
801{
802 session_cleanup_msg_t *msg;
803 vcl_session_t *session;
804
805 msg = (session_cleanup_msg_t *) data;
806 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
807 if (!session)
808 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800809 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700810 return;
811 }
812
Florin Coras36d49392020-04-24 23:00:11 +0000813 if (msg->type == SESSION_CLEANUP_TRANSPORT)
814 {
815 /* Transport was cleaned up before we confirmed close. Probably the
816 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700817 * Confirm close to make sure everything is cleaned up.
818 * Move to undetermined state to ensure that the session is not
819 * removed before both vpp and the app cleanup.
820 * - If the app closes first, the session is moved to CLOSED state
821 * and the session cleanup notification from vpp removes the
822 * session.
823 * - If vpp cleans up the session first, the session is moved to
824 * DETACHED state lower and subsequently the close from the app
825 * frees the session
826 */
827 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700828 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700829 vppcom_session_disconnect (vcl_session_handle (session));
830 session->session_state = VCL_STATE_UPDATED;
831 }
832 else if (session->session_state == VCL_STATE_DISCONNECT)
833 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800834 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700835 session->session_state = VCL_STATE_UPDATED;
836 }
Florin Coras36d49392020-04-24 23:00:11 +0000837 return;
838 }
839
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800840 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700841 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800842
843 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700844 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000845 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800846 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700847 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000848 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
849 return;
850 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800851
852 /* Session probably tracked with epoll, disconnect not yet handled and
853 * 1) both transport and session cleanup completed 2) app closed. Wait
854 * until message is drained to free the session.
855 * See @ref vcl_handle_mq_event */
856 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
857 {
858 session->flags |= VCL_SESSION_F_PENDING_FREE;
859 return;
860 }
861
Florin Coras9ace36d2019-10-28 13:14:17 -0700862 vcl_session_free (wrk, session);
863}
864
865static void
Florin Coras30e79c22019-01-02 19:31:22 -0800866vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
867{
868 session_req_worker_update_msg_t *msg;
869 vcl_session_t *s;
870
871 msg = (session_req_worker_update_msg_t *) data;
872 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
873 if (!s)
874 return;
875
876 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
877}
878
879static void
880vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
881{
882 session_worker_update_reply_msg_t *msg;
883 vcl_session_t *s;
884
885 msg = (session_worker_update_reply_msg_t *) data;
886 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
887 if (!s)
888 {
889 VDBG (0, "unknown handle 0x%llx", msg->handle);
890 return;
891 }
Florin Coras30e79c22019-01-02 19:31:22 -0800892
Florin Coras5f45e012019-01-23 09:21:30 -0800893 if (s->rx_fifo)
894 {
Florin Corasc547e912020-12-08 17:50:45 -0800895 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700896 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800897 {
898 VDBG (0, "failed to attach fifos for %u", s->session_index);
899 return;
900 }
Florin Coras5f45e012019-01-23 09:21:30 -0800901 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700902 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800903
Florin Coras30e79c22019-01-02 19:31:22 -0800904 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
905 s->vpp_handle, wrk->wrk_index);
906}
907
Florin Coras935ce752020-09-08 22:43:47 -0700908static int
909vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
910{
911
912 if (vcm->cfg.vpp_app_socket_api)
913 return vcl_sapi_recv_fds (wrk, fds, n_fds);
914
915 return vcl_bapi_recv_fds (wrk, fds, n_fds);
916}
917
Florin Corasc4c4cf52019-08-24 18:17:34 -0700918static void
919vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
920{
921 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
922 session_app_add_segment_msg_t *msg;
923 u64 segment_handle;
924 int fd = -1;
925
926 msg = (session_app_add_segment_msg_t *) data;
927
928 if (msg->fd_flags)
929 {
Florin Coras935ce752020-09-08 22:43:47 -0700930 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700931 seg_type = SSVM_SEGMENT_MEMFD;
932 }
933
934 segment_handle = msg->segment_handle;
935 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
936 {
937 clib_warning ("invalid segment handle");
938 return;
939 }
940
941 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
942 seg_type, fd))
943 {
944 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
945 return;
946 }
947
948 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
949 msg->segment_size);
950}
951
952static void
953vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
954{
955 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
956 vcl_segment_detach (msg->segment_handle);
957 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
958}
959
Florin Coras40c07ce2020-07-16 20:46:17 -0700960static void
961vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
962{
963 if (!vcm->wrk_rpc_fn)
964 return;
965
hanlina3a48962020-07-13 11:09:15 +0800966 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700967}
968
Florin Coras04ae8272021-04-12 19:55:37 -0700969static void
970vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
971{
972 session_transport_attr_reply_msg_t *mp;
973
974 if (!wrk->session_attr_op)
975 return;
976
977 mp = (session_transport_attr_reply_msg_t *) data;
978
979 wrk->session_attr_op_rv = mp->retval;
980 wrk->session_attr_op = 0;
981 wrk->session_attr_rv = mp->attr;
982}
983
Florin Coras86f04502018-09-12 16:08:01 -0700984static int
985vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700986{
Florin Coras54693d22018-07-17 10:46:29 -0700987 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700988 session_connected_msg_t *connected_msg;
989 session_reset_msg_t *reset_msg;
990 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700991 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700992 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700993
994 switch (e->event_type)
995 {
Florin Coras653e43f2019-03-04 10:56:23 -0800996 case SESSION_IO_EVT_RX:
997 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700998 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -0700999 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001000 break;
Florin Coras86f04502018-09-12 16:08:01 -07001001 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001002 break;
Florin Corasb242d312020-10-26 15:35:40 -07001003 case SESSION_CTRL_EVT_BOUND:
1004 /* We can only wait for only one listen so not postponed */
1005 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1006 break;
Florin Coras54693d22018-07-17 10:46:29 -07001007 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001008 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1009 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1010 {
1011 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1012 *ecpy = *e;
1013 ecpy->postponed = 1;
1014 ecpy->session_index = s->session_index;
1015 }
Florin Coras54693d22018-07-17 10:46:29 -07001016 break;
1017 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001018 connected_msg = (session_connected_msg_t *) e->data;
1019 sid = vcl_session_connected_handler (wrk, connected_msg);
1020 if (!(s = vcl_session_get (wrk, sid)))
1021 break;
1022 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1023 {
1024 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1025 *ecpy = *e;
1026 ecpy->postponed = 1;
1027 ecpy->session_index = s->session_index;
1028 }
Florin Coras54693d22018-07-17 10:46:29 -07001029 break;
1030 case SESSION_CTRL_EVT_DISCONNECTED:
1031 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001032 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1033 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001034 if (s->session_state == VCL_STATE_CLOSED)
1035 break;
Florin Corasb242d312020-10-26 15:35:40 -07001036 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1037 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001038 s->session_state = VCL_STATE_VPP_CLOSING;
1039 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1040 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1041 *ecpy = *e;
1042 ecpy->postponed = 1;
1043 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001044 break;
1045 }
1046 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001047 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001048 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1049 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001050 break;
1051 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001052 reset_msg = (session_reset_msg_t *) e->data;
1053 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1054 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001055 if (s->session_state == VCL_STATE_CLOSED)
1056 break;
Florin Corasb242d312020-10-26 15:35:40 -07001057 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1058 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001059 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1060 s->session_state = VCL_STATE_DISCONNECT;
1061 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1062 *ecpy = *e;
1063 ecpy->postponed = 1;
1064 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001065 break;
1066 }
Florin Coras134a9962018-08-28 11:32:04 -07001067 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001068 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001069 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1070 vcl_session_unlisten_reply_handler (wrk, e->data);
1071 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001072 case SESSION_CTRL_EVT_MIGRATED:
1073 vcl_session_migrated_handler (wrk, e->data);
1074 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001075 case SESSION_CTRL_EVT_CLEANUP:
1076 vcl_session_cleanup_handler (wrk, e->data);
1077 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001078 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1079 vcl_session_req_worker_update_handler (wrk, e->data);
1080 break;
1081 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1082 vcl_session_worker_update_reply_handler (wrk, e->data);
1083 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001084 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1085 vcl_session_app_add_segment_handler (wrk, e->data);
1086 break;
1087 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1088 vcl_session_app_del_segment_handler (wrk, e->data);
1089 break;
hanlina3a48962020-07-13 11:09:15 +08001090 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001091 vcl_worker_rpc_handler (wrk, e->data);
1092 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001093 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1094 vcl_session_transport_attr_reply_handler (wrk, e->data);
1095 break;
Florin Coras54693d22018-07-17 10:46:29 -07001096 default:
1097 clib_warning ("unhandled %u", e->event_type);
1098 }
1099 return VPPCOM_OK;
1100}
1101
Florin Coras30e79c22019-01-02 19:31:22 -08001102static int
Florin Coras697faea2018-06-27 17:10:49 -07001103vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001104 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001105 f64 wait_for_time)
1106{
Florin Coras134a9962018-08-28 11:32:04 -07001107 vcl_worker_t *wrk = vcl_worker_get_current ();
1108 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001109 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001110 svm_msg_q_msg_t msg;
1111 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001112
Florin Coras697faea2018-06-27 17:10:49 -07001113 do
Dave Wallace543852a2017-08-03 02:11:34 -04001114 {
Florin Coras134a9962018-08-28 11:32:04 -07001115 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001116 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001117 {
Florin Coras070453d2018-08-24 17:04:27 -07001118 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001119 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001120 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001121 {
Florin Coras697faea2018-06-27 17:10:49 -07001122 return VPPCOM_OK;
1123 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001124 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001125 {
Florin Coras697faea2018-06-27 17:10:49 -07001126 return VPPCOM_ECONNREFUSED;
1127 }
Florin Coras54693d22018-07-17 10:46:29 -07001128
Florin Coras134a9962018-08-28 11:32:04 -07001129 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001130 {
1131 usleep (100);
1132 continue;
1133 }
Florin Coras134a9962018-08-28 11:32:04 -07001134 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001135 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001136 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001137 }
Florin Coras134a9962018-08-28 11:32:04 -07001138 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001139
Florin Coras05ecfcc2018-12-12 18:19:39 -08001140 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001141 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001142 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001143
Florin Coras697faea2018-06-27 17:10:49 -07001144 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001145}
1146
Florin Coras30e79c22019-01-02 19:31:22 -08001147static void
1148vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1149{
Florin Coras288eaab2019-02-03 15:26:14 -08001150 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001151 vcl_session_t *s;
1152 u32 *sip;
1153
1154 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1155 return;
1156
1157 vec_foreach (sip, wrk->pending_session_wrk_updates)
1158 {
1159 s = vcl_session_get (wrk, *sip);
1160 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1161 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001162 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1163 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001164 s->session_state = state;
1165 }
1166 vec_reset_length (wrk->pending_session_wrk_updates);
1167}
1168
Florin Corasf9240dc2019-01-15 08:03:17 -08001169void
Florin Coras5398dfb2021-01-25 20:31:27 -08001170vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001171{
Florin Coras30e79c22019-01-02 19:31:22 -08001172 svm_msg_q_msg_t *msg;
1173 session_event_t *e;
1174 svm_msg_q_t *mq;
1175 int i;
1176
1177 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001178 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001179
1180 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1181 {
1182 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1183 e = svm_msg_q_msg_data (mq, msg);
1184 vcl_handle_mq_event (wrk, e);
1185 svm_msg_q_free_msg (mq, msg);
1186 }
1187 vec_reset_length (wrk->mq_msg_vector);
1188 vcl_handle_pending_wrk_updates (wrk);
1189}
1190
Florin Coras5398dfb2021-01-25 20:31:27 -08001191void
1192vcl_flush_mq_events (void)
1193{
1194 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1195}
1196
Florin Coras697faea2018-06-27 17:10:49 -07001197static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001198vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001199{
Florin Coras134a9962018-08-28 11:32:04 -07001200 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001201 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001202 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001203 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001204
Florin Corasab2f6db2018-08-31 14:31:41 -07001205 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001206 if (!session)
1207 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001208
Florin Corasaaff5ee2019-07-08 13:00:00 -07001209 /* Flush pending accept events, if any */
1210 while (clib_fifo_elts (session->accept_evts_fifo))
1211 {
1212 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1213 accepted_msg = &evt->accepted_msg;
1214 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1215 vcl_send_session_accepted_reply (session->vpp_evt_q,
1216 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001217 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001218 }
1219 clib_fifo_free (session->accept_evts_fifo);
1220
Florin Coras458089b2019-08-21 16:20:44 -07001221 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001222
Florin Coras5e062572019-03-14 19:07:51 -07001223 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001224 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001225 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001226
1227 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001228 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001229
Florin Coras070453d2018-08-24 17:04:27 -07001230 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001231}
1232
Florin Coras940f78f2018-11-30 12:11:20 -08001233/**
1234 * Handle app exit
1235 *
1236 * Notify vpp of the disconnect and mark the worker as free. If we're the
1237 * last worker, do a full cleanup otherwise, since we're probably a forked
1238 * child, avoid syscalls as much as possible. We might've lost privileges.
1239 */
1240void
1241vppcom_app_exit (void)
1242{
1243 if (!pool_elts (vcm->workers))
1244 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001245 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1246 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001247 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001248}
1249
Florin Coras935ce752020-09-08 22:43:47 -07001250static int
1251vcl_api_attach (void)
1252{
1253 if (vcm->cfg.vpp_app_socket_api)
1254 return vcl_sapi_attach ();
1255
1256 return vcl_bapi_attach ();
1257}
1258
1259static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001260vcl_api_retry_attach (vcl_worker_t *wrk)
1261{
1262 vcl_session_t *s;
1263
1264 if (vcl_api_attach ())
1265 return;
1266
1267 /* Treat listeners as configuration that needs to be re-added to vpp */
1268 pool_foreach (s, wrk->sessions)
1269 {
1270 if (s->flags & VCL_SESSION_F_IS_VEP)
1271 continue;
1272 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1273 vppcom_session_listen (vcl_session_handle (s), 10);
1274 else
1275 VDBG (0, "internal error: unexpected state %d", s->session_state);
1276 }
1277}
1278
1279static void
1280vcl_api_handle_disconnect (vcl_worker_t *wrk)
1281{
1282 wrk->api_client_handle = ~0;
1283 vcl_worker_detach_sessions (wrk);
1284}
1285
1286static void
Florin Coras935ce752020-09-08 22:43:47 -07001287vcl_api_detach (vcl_worker_t * wrk)
1288{
1289 vcl_send_app_detach (wrk);
1290
1291 if (vcm->cfg.vpp_app_socket_api)
1292 return vcl_sapi_detach (wrk);
1293
1294 return vcl_bapi_disconnect_from_vpp ();
1295}
1296
Dave Wallace543852a2017-08-03 02:11:34 -04001297/*
1298 * VPPCOM Public API functions
1299 */
1300int
Florin Coras66ec4672020-06-15 07:59:40 -07001301vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001302{
Dave Wallace543852a2017-08-03 02:11:34 -04001303 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001304 int rv;
1305
Florin Coras47c40e22018-11-26 17:01:36 -08001306 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001307 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001308 VDBG (1, "already initialized");
1309 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001310 }
1311
Florin Coras47c40e22018-11-26 17:01:36 -08001312 vcm->is_init = 1;
1313 vppcom_cfg (&vcm->cfg);
1314 vcl_cfg = &vcm->cfg;
1315
1316 vcm->main_cpu = pthread_self ();
1317 vcm->main_pid = getpid ();
1318 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001319 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1320 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001321 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1322 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001323 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001324 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001325 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001326
1327 /* Allocate default worker */
1328 vcl_worker_alloc_and_init ();
1329
Florin Coras935ce752020-09-08 22:43:47 -07001330 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001331 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001332
1333 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001334 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001335
1336 return VPPCOM_OK;
1337}
1338
1339void
1340vppcom_app_destroy (void)
1341{
Florin Corasdc0ded72020-04-30 02:59:55 +00001342 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001343 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001344
Florin Coras940f78f2018-11-30 12:11:20 -08001345 if (!pool_elts (vcm->workers))
1346 return;
1347
Florin Coras0d427d82018-06-27 03:24:07 -07001348 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001349
Florin Corasdc0ded72020-04-30 02:59:55 +00001350 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001351
Florin Corasce815de2020-04-16 18:47:27 +00001352 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001353 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001354 if (current_wrk != wrk)
1355 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001356 }
Florin Corasce815de2020-04-16 18:47:27 +00001357 /* *INDENT-ON* */
1358
Florin Coras935ce752020-09-08 22:43:47 -07001359 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001360 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1361
Florin Coras0d427d82018-06-27 03:24:07 -07001362 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001363
1364 /*
1365 * Free the heap and fix vcm
1366 */
1367 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001368 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001369
1370 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001371 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001372}
1373
1374int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001375vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001376{
Florin Coras134a9962018-08-28 11:32:04 -07001377 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001378 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001379
Florin Coras134a9962018-08-28 11:32:04 -07001380 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001381
Florin Coras7e12d942018-06-27 14:32:43 -07001382 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001383 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001384 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001385 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001386
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001387 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001388 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001389
Florin Coras7e12d942018-06-27 14:32:43 -07001390 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1391 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001392
Florin Coras5e062572019-03-14 19:07:51 -07001393 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001394
Florin Coras134a9962018-08-28 11:32:04 -07001395 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001396}
1397
Florin Corasfe286f72021-06-04 10:07:55 -07001398static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001399vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001400{
Florin Corasfa3884f2021-06-22 20:04:46 -07001401 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001402
Florin Corasfa3884f2021-06-22 20:04:46 -07001403 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001404 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001405 wrk->ep_lt_current = s->session_index;
1406 s->vep.lt_next = s->session_index;
1407 s->vep.lt_prev = s->session_index;
1408 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001409 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001410
1411 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1412 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1413
1414 prev->vep.lt_next = s->session_index;
1415 s->vep.lt_prev = prev->session_index;
1416
1417 s->vep.lt_next = cur->session_index;
1418 cur->vep.lt_prev = s->session_index;
1419}
1420
1421static void
1422vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1423{
1424 vcl_session_t *prev, *next;
1425
1426 if (s->vep.lt_next == s->session_index)
1427 {
1428 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1429 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1430 return;
1431 }
1432
1433 prev = vcl_session_get (wrk, s->vep.lt_prev);
1434 next = vcl_session_get (wrk, s->vep.lt_next);
1435
1436 prev->vep.lt_next = next->session_index;
1437 next->vep.lt_prev = prev->session_index;
1438
1439 if (s->session_index == wrk->ep_lt_current)
1440 wrk->ep_lt_current = s->vep.lt_next;
1441
1442 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001443}
1444
Dave Wallace543852a2017-08-03 02:11:34 -04001445int
Florin Corasc127d5a2020-10-14 16:35:58 -07001446vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001447 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001448{
Florin Coras134a9962018-08-28 11:32:04 -07001449 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001450
Florin Coras6c3b2182020-10-19 18:36:48 -07001451 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001452
Florin Coras6c3b2182020-10-19 18:36:48 -07001453 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001454 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001455 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001456 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001457 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001458 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001459 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001460 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001461 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1462 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001463 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001464 }
Florin Corase4306b62020-10-28 12:51:10 -07001465 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001466 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001467
Florin Coras6c3b2182020-10-19 18:36:48 -07001468 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001469 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001470 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001471 if (rv < 0)
1472 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001473 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1474 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001475 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001476
Florin Coras9ace36d2019-10-28 13:14:17 -07001477 if (!do_disconnect)
1478 {
1479 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001480 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001481 goto cleanup;
1482 }
1483
Florin Coras6c3b2182020-10-19 18:36:48 -07001484 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001485 {
1486 rv = vppcom_session_unbind (sh);
1487 if (PREDICT_FALSE (rv < 0))
1488 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001489 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001490 vppcom_retval_str (rv));
1491 return rv;
1492 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001493 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001494 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001495 {
1496 rv = vppcom_session_disconnect (sh);
1497 if (PREDICT_FALSE (rv < 0))
1498 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001499 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001500 rv, vppcom_retval_str (rv));
1501 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001502 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001503 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001504 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001505 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001506 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001507 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001508 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001509
1510 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1511 goto free_session;
1512
1513 /* Disconnect/reset messages pending but vpp transport and session
1514 * cleanups already done. Free only after messages drained. */
1515 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001516 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001517
Florin Corasc127d5a2020-10-14 16:35:58 -07001518 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001519
Florin Coras9ace36d2019-10-28 13:14:17 -07001520 /* Session is removed only after vpp confirms the disconnect */
1521 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001522
Florin Corasf9240dc2019-01-15 08:03:17 -08001523cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001524 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001525free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001526 vcl_session_free (wrk, s);
1527 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001528
Dave Wallace543852a2017-08-03 02:11:34 -04001529 return rv;
1530}
1531
1532int
Florin Corasf9240dc2019-01-15 08:03:17 -08001533vppcom_session_close (uint32_t session_handle)
1534{
1535 vcl_worker_t *wrk = vcl_worker_get_current ();
1536 vcl_session_t *session;
1537
1538 session = vcl_session_get_w_handle (wrk, session_handle);
1539 if (!session)
1540 return VPPCOM_EBADFD;
1541 return vcl_session_cleanup (wrk, session, session_handle,
1542 1 /* do_disconnect */ );
1543}
1544
1545int
Florin Coras134a9962018-08-28 11:32:04 -07001546vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001547{
Florin Coras134a9962018-08-28 11:32:04 -07001548 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001549 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001550
1551 if (!ep || !ep->ip)
1552 return VPPCOM_EINVAL;
1553
Florin Coras134a9962018-08-28 11:32:04 -07001554 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001555 if (!session)
1556 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001557
Florin Coras6c3b2182020-10-19 18:36:48 -07001558 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001559 {
Florin Coras5e062572019-03-14 19:07:51 -07001560 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1561 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001562 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001563 }
1564
Florin Coras7e12d942018-06-27 14:32:43 -07001565 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001566 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001567 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1568 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001569 else
Dave Barach178cf492018-11-13 16:34:13 -05001570 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1571 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001572 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001573
Florin Coras60687192021-11-29 21:00:47 -08001574 VDBG (0,
1575 "session %u handle %u: binding to local %s address %U port %u, "
1576 "proto %s",
1577 session->session_index, session_handle,
1578 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1579 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001580 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1581 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001582 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001583 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001584
1585 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001586 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001587
Florin Coras070453d2018-08-24 17:04:27 -07001588 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001589}
1590
1591int
Florin Coras134a9962018-08-28 11:32:04 -07001592vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001593{
Florin Coras134a9962018-08-28 11:32:04 -07001594 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001595 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001596 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001597 int rv;
1598
Florin Coras134a9962018-08-28 11:32:04 -07001599 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001600 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001601 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001602
Dave Wallaceee45d412017-11-24 21:44:06 -05001603 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001604 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001605 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001606 VDBG (0, "session %u [0x%llx]: already in listen state!",
1607 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001608 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001609 }
1610
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001611 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001612
Florin Coras070453d2018-08-24 17:04:27 -07001613 /*
1614 * Send listen request to vpp and wait for reply
1615 */
Florin Coras458089b2019-08-21 16:20:44 -07001616 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001617 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001618 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001619 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001620
Florin Coras070453d2018-08-24 17:04:27 -07001621 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001622 {
Florin Coras134a9962018-08-28 11:32:04 -07001623 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001624 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1625 listen_sh, listen_session->vpp_handle, rv,
1626 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001627 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001628 }
1629
Florin Coras070453d2018-08-24 17:04:27 -07001630 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001631}
1632
Florin Coras134a9962018-08-28 11:32:04 -07001633static int
Florin Coras5e062572019-03-14 19:07:51 -07001634validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001635{
Florin Coras6c3b2182020-10-19 18:36:48 -07001636 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001637 {
Florin Coras5e062572019-03-14 19:07:51 -07001638 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1639 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001640 return VPPCOM_EBADFD;
1641 }
1642
Florin Corasc127d5a2020-10-14 16:35:58 -07001643 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001644 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001645 {
Florin Coras60687192021-11-29 21:00:47 -08001646 VDBG (0,
1647 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1648 " (%s)",
1649 ls->vpp_handle, ls->session_state,
1650 vcl_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001651 return VPPCOM_EBADFD;
1652 }
1653 return VPPCOM_OK;
1654}
1655
1656int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001657vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1658{
1659 if (!strcmp (proto_str, "TCP"))
1660 *proto = VPPCOM_PROTO_TCP;
1661 else if (!strcmp (proto_str, "tcp"))
1662 *proto = VPPCOM_PROTO_TCP;
1663 else if (!strcmp (proto_str, "UDP"))
1664 *proto = VPPCOM_PROTO_UDP;
1665 else if (!strcmp (proto_str, "udp"))
1666 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001667 else if (!strcmp (proto_str, "TLS"))
1668 *proto = VPPCOM_PROTO_TLS;
1669 else if (!strcmp (proto_str, "tls"))
1670 *proto = VPPCOM_PROTO_TLS;
1671 else if (!strcmp (proto_str, "QUIC"))
1672 *proto = VPPCOM_PROTO_QUIC;
1673 else if (!strcmp (proto_str, "quic"))
1674 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001675 else if (!strcmp (proto_str, "DTLS"))
1676 *proto = VPPCOM_PROTO_DTLS;
1677 else if (!strcmp (proto_str, "dtls"))
1678 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001679 else if (!strcmp (proto_str, "SRTP"))
1680 *proto = VPPCOM_PROTO_SRTP;
1681 else if (!strcmp (proto_str, "srtp"))
1682 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001683 else
1684 return 1;
1685 return 0;
1686}
1687
1688int
Florin Coras134a9962018-08-28 11:32:04 -07001689vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001690 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001691{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001692 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001693 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001694 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001695 vcl_session_t *listen_session = 0;
1696 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001697 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001698 u8 is_nonblocking;
1699 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001700
Florin Coras5398dfb2021-01-25 20:31:27 -08001701again:
1702
Florin Coras134a9962018-08-28 11:32:04 -07001703 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001704 if (!listen_session)
1705 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001706
Florin Coras134a9962018-08-28 11:32:04 -07001707 listen_session_index = listen_session->session_index;
1708 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001709 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001710
Florin Coras54693d22018-07-17 10:46:29 -07001711 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001712 {
Florin Coras54693d22018-07-17 10:46:29 -07001713 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001714 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001715 accepted_msg = evt->accepted_msg;
1716 goto handle;
1717 }
1718
Florin Corasac422d62020-10-19 20:51:36 -07001719 is_nonblocking = vcl_session_has_attr (listen_session,
1720 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001721 while (1)
1722 {
Carl Smith592a9092019-11-12 14:57:37 +13001723 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1724 return VPPCOM_EAGAIN;
1725
Florin Coras5398dfb2021-01-25 20:31:27 -08001726 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1727 vcl_worker_flush_mq_events (wrk);
1728 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001729 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001730
Florin Coras54693d22018-07-17 10:46:29 -07001731handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001732
Florin Coras00cca802019-06-06 09:38:44 -07001733 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1734 listen_session_index);
1735 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1736 return VPPCOM_ECONNABORTED;
1737
Florin Coras134a9962018-08-28 11:32:04 -07001738 listen_session = vcl_session_get (wrk, listen_session_index);
1739 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001740
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001741 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001742 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001743
Florin Coras5e062572019-03-14 19:07:51 -07001744 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1745 " flags %d, is_nonblocking %u", listen_session->session_index,
1746 listen_session->vpp_handle, client_session_index,
1747 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001748 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001749
Dave Wallace048b1d62018-01-03 22:24:41 -05001750 if (ep)
1751 {
Florin Coras7e12d942018-06-27 14:32:43 -07001752 ep->is_ip4 = client_session->transport.is_ip4;
1753 ep->port = client_session->transport.rmt_port;
1754 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001755 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1756 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001757 else
Dave Barach178cf492018-11-13 16:34:13 -05001758 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1759 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001760 }
Dave Wallace60caa062017-11-10 17:07:13 -05001761
Florin Coras60687192021-11-29 21:00:47 -08001762 VDBG (0,
1763 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1764 "local: %U:%u",
1765 listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001766 client_session_index, client_session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001767 vcl_format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001768 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001769 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001770 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001771 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001772 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001773 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1774 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001775
Florin Coras3c7d4f92018-12-14 11:28:43 -08001776 /*
1777 * Session might have been closed already
1778 */
1779 if (accept_flags)
1780 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001781 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001782 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001783 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001784 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001785 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001786 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001787}
1788
1789int
Florin Coras134a9962018-08-28 11:32:04 -07001790vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001791{
Florin Coras134a9962018-08-28 11:32:04 -07001792 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001793 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001794 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001795 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001796
Florin Coras134a9962018-08-28 11:32:04 -07001797 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001798 if (!session)
1799 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001800 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001801
Florin Coras6c3b2182020-10-19 18:36:48 -07001802 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001803 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001804 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001805 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001806 }
1807
Florin Corasc127d5a2020-10-14 16:35:58 -07001808 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001809 {
Florin Coras60687192021-11-29 21:00:47 -08001810 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001811 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1812 " state (%s)",
1813 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001814 vcl_format_ip46_address, &session->transport.rmt_ip,
1815 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001816 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001817 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001818 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001819 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001820 }
1821
Florin Coras0a1e1832020-03-29 18:54:04 +00001822 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001823 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001824 {
1825 if (session->session_type != VPPCOM_PROTO_UDP)
1826 return VPPCOM_EINVAL;
1827 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001828 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001829 }
1830
Florin Coras7e12d942018-06-27 14:32:43 -07001831 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001832 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001833 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001834 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001835 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001836
Florin Coras5ffb9642021-12-03 17:24:13 -08001837 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1838 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001839 &session->transport.rmt_ip,
1840 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001841 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001842 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001843
Florin Coras458089b2019-08-21 16:20:44 -07001844 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001845
Florin Corasac422d62020-10-19 20:51:36 -07001846 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001847 {
fanyfa49d59d2020-10-13 17:07:16 +08001848 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001849 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001850 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001851 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001852 return VPPCOM_EINPROGRESS;
1853 }
Florin Coras57c88932019-08-29 12:03:17 -07001854
1855 /*
1856 * Wait for reply from vpp if blocking
1857 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001858 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001859 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001860
Florin Coras134a9962018-08-28 11:32:04 -07001861 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001862 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1863 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001864
Dave Wallace4878cbe2017-11-21 03:45:09 -05001865 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001866}
1867
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001868int
1869vppcom_session_stream_connect (uint32_t session_handle,
1870 uint32_t parent_session_handle)
1871{
1872 vcl_worker_t *wrk = vcl_worker_get_current ();
1873 vcl_session_t *session, *parent_session;
1874 u32 session_index, parent_session_index;
1875 int rv;
1876
1877 session = vcl_session_get_w_handle (wrk, session_handle);
1878 if (!session)
1879 return VPPCOM_EBADFD;
1880 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1881 if (!parent_session)
1882 return VPPCOM_EBADFD;
1883
1884 session_index = session->session_index;
1885 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001886 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001887 {
1888 VDBG (0, "ERROR: cannot connect epoll session %u!",
1889 session->session_index);
1890 return VPPCOM_EBADFD;
1891 }
1892
Florin Corasc127d5a2020-10-14 16:35:58 -07001893 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001894 {
Florin Coras60687192021-11-29 21:00:47 -08001895 VDBG (0,
1896 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001897 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001898 session_handle, session->vpp_handle, parent_session_handle,
1899 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001900 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001901 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001902 return VPPCOM_OK;
1903 }
1904
1905 /* Connect to quic session specifics */
1906 session->transport.is_ip4 = parent_session->transport.is_ip4;
1907 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1908 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001909 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001910
1911 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1912 session_handle, parent_session_handle, parent_session->vpp_handle);
1913
1914 /*
1915 * Send connect request and wait for reply from vpp
1916 */
Florin Coras458089b2019-08-21 16:20:44 -07001917 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001918 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001919 vcm->cfg.session_timeout);
1920
1921 session->listener_index = parent_session_index;
1922 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001923 if (parent_session)
1924 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001925
1926 session = vcl_session_get (wrk, session_index);
1927 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1928 session->vpp_handle, rv ? "failed" : "succeeded");
1929
1930 return rv;
1931}
1932
Steven58f464e2017-10-25 12:33:12 -07001933static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001934vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001935 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001936{
Florin Coras134a9962018-08-28 11:32:04 -07001937 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001938 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001939 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001940 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001941 session_event_t *e;
1942 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001943 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001944
Florin Coras070453d2018-08-24 17:04:27 -07001945 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001946 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001947
Florin Coras134a9962018-08-28 11:32:04 -07001948 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001949 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001950 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001951
Florin Coras6d0106e2019-01-29 20:11:58 -08001952 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001953 {
Florin Coras5e062572019-03-14 19:07:51 -07001954 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001955 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001956 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08001957 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001958 }
1959
liuyacan55c952e2021-06-13 14:54:55 +08001960 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
1961 {
1962 /* Vpp would ack the incoming data and enqueue it for reading.
1963 * So even if SHUT_RD is set, we can still read() the data if
1964 * the session is ready.
1965 */
1966 if (!vcl_session_read_ready (s))
1967 {
1968 return 0;
1969 }
1970 }
1971
Florin Corasac422d62020-10-19 20:51:36 -07001972 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001973 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001974 mq = wrk->app_event_queue;
1975 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001976 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001977
Sirshak Das28aa5392019-02-05 01:33:33 -06001978 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001979 {
Florin Coras54693d22018-07-17 10:46:29 -07001980 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001981 {
Yu Pingb2955352019-12-04 06:49:04 +08001982 if (vcl_session_is_closing (s))
1983 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001984 if (is_ct)
1985 svm_fifo_unset_event (s->rx_fifo);
1986 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001987 return VPPCOM_EWOULDBLOCK;
1988 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001989 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001990 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001991 if (vcl_session_is_closing (s))
1992 return vcl_session_closing_error (s);
1993
Florin Corasd6894562020-10-28 00:37:15 -07001994 if (is_ct)
1995 svm_fifo_unset_event (s->rx_fifo);
1996 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001997
Florin Coras5398dfb2021-01-25 20:31:27 -08001998 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1999 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002000 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002001 }
Florin Coras54693d22018-07-17 10:46:29 -07002002
Florin Coras4a2c7942020-09-10 12:27:14 -07002003read_again:
2004
Florin Coras460dce62018-07-27 05:45:06 -07002005 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002006 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002007 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002008 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2009
2010 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002011
2012 if (peek)
2013 return rv;
2014
Florin Coras4a2c7942020-09-10 12:27:14 -07002015 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002016
Sirshak Das28aa5392019-02-05 01:33:33 -06002017 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002018 {
Florin Corasd6894562020-10-28 00:37:15 -07002019 if (is_ct)
2020 svm_fifo_unset_event (s->rx_fifo);
2021 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002022 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002023 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002024 {
Florin Corasc34118b2020-08-12 18:58:25 -07002025 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2026 e->event_type = SESSION_IO_EVT_RX;
2027 e->session_index = s->session_index;
2028 }
2029 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002030 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002031 {
2032 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002033 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002034 buf += rv;
2035 n -= rv;
2036 goto read_again;
2037 }
Florin Coras41c9e042018-09-11 00:10:41 -07002038
Florin Coras17ec5772020-08-12 20:46:29 -07002039 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002040 {
Florin Coras17ec5772020-08-12 20:46:29 -07002041 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002042 app_send_io_evt_to_vpp (s->vpp_evt_q,
2043 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002044 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002045 }
2046
Florin Coras5e062572019-03-14 19:07:51 -07002047 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2048 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002049
Florin Coras54693d22018-07-17 10:46:29 -07002050 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002051}
2052
Steven58f464e2017-10-25 12:33:12 -07002053int
Florin Coras134a9962018-08-28 11:32:04 -07002054vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002055{
Florin Coras134a9962018-08-28 11:32:04 -07002056 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002057}
2058
2059static int
Florin Coras134a9962018-08-28 11:32:04 -07002060vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002061{
Florin Coras134a9962018-08-28 11:32:04 -07002062 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002063}
2064
Florin Coras2cba8532018-09-11 16:33:36 -07002065int
2066vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002067 vppcom_data_segment_t * ds, uint32_t n_segments,
2068 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002069{
2070 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002071 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002072 vcl_session_t *s = 0;
2073 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002074 svm_msg_q_t *mq;
2075 u8 is_ct;
2076
2077 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002078 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002079 return VPPCOM_EBADFD;
2080
Florin Coras6d0106e2019-01-29 20:11:58 -08002081 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2082 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002083
Florin Corasac422d62020-10-19 20:51:36 -07002084 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002085 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002086 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002087 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002088 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002089
Sirshak Das28aa5392019-02-05 01:33:33 -06002090 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002091 {
2092 if (is_nonblocking)
2093 {
Florin Coras62877022020-10-28 21:22:04 -07002094 if (is_ct)
2095 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002096 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002097 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002098 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002099 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002100 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002101 if (vcl_session_is_closing (s))
2102 return vcl_session_closing_error (s);
2103
Florin Coras62877022020-10-28 21:22:04 -07002104 if (is_ct)
2105 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002106 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002107
Florin Coras5398dfb2021-01-25 20:31:27 -08002108 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2109 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002110 }
2111 }
2112
Florin Corasd1cc38d2020-10-11 11:05:04 -07002113 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002114 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002115 if (n_read < 0)
2116 return VPPCOM_EAGAIN;
2117
2118 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2119 {
Florin Coras62877022020-10-28 21:22:04 -07002120 if (is_ct)
2121 svm_fifo_unset_event (s->rx_fifo);
2122 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002123 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002124 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002125 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002126 {
2127 session_event_t *e;
2128 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2129 e->event_type = SESSION_IO_EVT_RX;
2130 e->session_index = s->session_index;
2131 }
2132 }
2133
2134 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002135 return n_read;
2136}
2137
2138void
Florin Corasd68faf82020-09-25 15:18:13 -07002139vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002140{
2141 vcl_worker_t *wrk = vcl_worker_get_current ();
2142 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002143 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002144
2145 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002146 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002147 return;
2148
Florin Coras62877022020-10-28 21:22:04 -07002149 is_ct = vcl_session_is_ct (s);
2150 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002151
Florin Coras8cc93212021-09-10 11:37:12 -07002152 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002153 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002154}
2155
Florin Coras7a2abce2020-04-05 19:25:44 +00002156always_inline u8
2157vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002158{
Florin Coras7a2abce2020-04-05 19:25:44 +00002159 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2160 if (is_dgram)
2161 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2162 else
2163 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002164}
2165
2166always_inline int
2167vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2168 size_t n, u8 is_flush, u8 is_dgram)
2169{
Florin Coras6d0106e2019-01-29 20:11:58 -08002170 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002171 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002172 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002173 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002174 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002175
Florin Coras0b0d28e2021-06-04 17:31:53 -07002176 /* Accept zero length writes but just return */
2177 if (PREDICT_FALSE (!n))
2178 return VPPCOM_OK;
2179
2180 if (PREDICT_FALSE (!buf))
2181 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002182
Florin Coras6c3b2182020-10-19 18:36:48 -07002183 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002184 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002185 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2186 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002187 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002188 }
2189
liuyacan55c952e2021-06-13 14:54:55 +08002190 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002191 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002192 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2193 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002194 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002195 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002196 }
2197
liuyacan55c952e2021-06-13 14:54:55 +08002198 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2199 {
2200 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2201 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002202 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002203 return VPPCOM_EPIPE;
2204 }
2205
Florin Coras0e88e852018-09-17 22:09:02 -07002206 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002207 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002208 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002209
Florin Coras653e43f2019-03-04 10:56:23 -08002210 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002211 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002212 {
Florin Coras54693d22018-07-17 10:46:29 -07002213 if (is_nonblocking)
2214 {
Florin Coras070453d2018-08-24 17:04:27 -07002215 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002216 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002217 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002218 {
Florin Coras2d379d82019-06-28 12:45:12 -07002219 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002220 if (vcl_session_is_closing (s))
2221 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002222
Florin Coras5398dfb2021-01-25 20:31:27 -08002223 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2224 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002225 }
Dave Wallace543852a2017-08-03 02:11:34 -04002226 }
Dave Wallace543852a2017-08-03 02:11:34 -04002227
Florin Coras653e43f2019-03-04 10:56:23 -08002228 et = SESSION_IO_EVT_TX;
2229 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002230 et = SESSION_IO_EVT_TX_FLUSH;
2231
Florin Coras7a2abce2020-04-05 19:25:44 +00002232 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002233 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002234 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002235 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002236 else
2237 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002238 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002239
Florin Coras14ed6df2019-03-06 21:13:42 -08002240 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002241 app_send_io_evt_to_vpp (
2242 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002243
Florin Coras56230092020-09-02 20:52:58 -07002244 /* The underlying fifo segment can run out of memory */
2245 if (PREDICT_FALSE (n_write < 0))
2246 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002247
Florin Coras6d0106e2019-01-29 20:11:58 -08002248 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2249 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002250
Florin Coras54693d22018-07-17 10:46:29 -07002251 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002252}
2253
Florin Coras42ceddb2018-12-12 10:56:01 -08002254int
2255vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2256{
Florin Coras7a2abce2020-04-05 19:25:44 +00002257 vcl_worker_t *wrk = vcl_worker_get_current ();
2258 vcl_session_t *s;
2259
2260 s = vcl_session_get_w_handle (wrk, session_handle);
2261 if (PREDICT_FALSE (!s))
2262 return VPPCOM_EBADFD;
2263
2264 return vppcom_session_write_inline (wrk, s, buf, n,
2265 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002266}
2267
Florin Corasb0f662f2018-12-27 14:51:46 -08002268int
2269vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2270{
Florin Coras7a2abce2020-04-05 19:25:44 +00002271 vcl_worker_t *wrk = vcl_worker_get_current ();
2272 vcl_session_t *s;
2273
2274 s = vcl_session_get_w_handle (wrk, session_handle);
2275 if (PREDICT_FALSE (!s))
2276 return VPPCOM_EBADFD;
2277
2278 return vppcom_session_write_inline (wrk, s, buf, n,
2279 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002280}
2281
Florin Corasc0737e92019-03-04 14:19:39 -08002282#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002283if (PREDICT_FALSE (!_s->rx_fifo)) \
2284 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002285if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2286 { \
2287 if (!vcl_session_is_ct (_s)) \
2288 { \
2289 svm_fifo_unset_event (_s->rx_fifo); \
2290 if (svm_fifo_is_empty (_s->rx_fifo)) \
2291 break; \
2292 } \
2293 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2294 { \
Florin Coras2f630182020-08-13 00:17:51 -07002295 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002296 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2297 break; \
2298 } \
2299 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002300
Florin Coras86f04502018-09-12 16:08:01 -07002301static void
2302vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2303 unsigned long n_bits, unsigned long *read_map,
2304 unsigned long *write_map,
2305 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002306{
2307 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002308 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002309 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002310 u32 sid;
2311
2312 switch (e->event_type)
2313 {
Florin Corasc0737e92019-03-04 14:19:39 -08002314 case SESSION_IO_EVT_RX:
2315 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002316 s = vcl_session_get (wrk, sid);
2317 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002318 break;
Florin Corasb242d312020-10-26 15:35:40 -07002319 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002320 if (sid < n_bits && read_map)
2321 {
David Johnsond9818dd2018-12-14 14:53:41 -05002322 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002323 *bits_set += 1;
2324 }
2325 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002326 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002327 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002328 s = vcl_session_get (wrk, sid);
2329 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002330 break;
2331 if (sid < n_bits && write_map)
2332 {
David Johnsond9818dd2018-12-14 14:53:41 -05002333 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002334 *bits_set += 1;
2335 }
2336 break;
Florin Coras86f04502018-09-12 16:08:01 -07002337 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002338 if (!e->postponed)
2339 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2340 else
2341 s = vcl_session_get (wrk, e->session_index);
2342 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002343 break;
Florin Corasb242d312020-10-26 15:35:40 -07002344 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002345 if (sid < n_bits && read_map)
2346 {
David Johnsond9818dd2018-12-14 14:53:41 -05002347 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002348 *bits_set += 1;
2349 }
2350 break;
2351 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002352 if (!e->postponed)
2353 {
2354 connected_msg = (session_connected_msg_t *) e->data;
2355 sid = vcl_session_connected_handler (wrk, connected_msg);
2356 }
2357 else
2358 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002359 if (sid == VCL_INVALID_SESSION_INDEX)
2360 break;
2361 if (sid < n_bits && write_map)
2362 {
2363 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2364 *bits_set += 1;
2365 }
Florin Coras86f04502018-09-12 16:08:01 -07002366 break;
2367 case SESSION_CTRL_EVT_DISCONNECTED:
2368 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002369 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2370 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002371 break;
Florin Corasb242d312020-10-26 15:35:40 -07002372 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002373 if (sid < n_bits && except_map)
2374 {
David Johnsond9818dd2018-12-14 14:53:41 -05002375 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002376 *bits_set += 1;
2377 }
2378 break;
2379 case SESSION_CTRL_EVT_RESET:
2380 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2381 if (sid < n_bits && except_map)
2382 {
David Johnsond9818dd2018-12-14 14:53:41 -05002383 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002384 *bits_set += 1;
2385 }
2386 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002387 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2388 vcl_session_unlisten_reply_handler (wrk, e->data);
2389 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002390 case SESSION_CTRL_EVT_MIGRATED:
2391 vcl_session_migrated_handler (wrk, e->data);
2392 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002393 case SESSION_CTRL_EVT_CLEANUP:
2394 vcl_session_cleanup_handler (wrk, e->data);
2395 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002396 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2397 vcl_session_worker_update_reply_handler (wrk, e->data);
2398 break;
2399 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2400 vcl_session_req_worker_update_handler (wrk, e->data);
2401 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002402 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2403 vcl_session_app_add_segment_handler (wrk, e->data);
2404 break;
2405 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2406 vcl_session_app_del_segment_handler (wrk, e->data);
2407 break;
hanlina3a48962020-07-13 11:09:15 +08002408 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002409 vcl_worker_rpc_handler (wrk, e->data);
2410 break;
Florin Coras86f04502018-09-12 16:08:01 -07002411 default:
2412 clib_warning ("unhandled: %u", e->event_type);
2413 break;
2414 }
2415}
2416
2417static int
2418vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2419 unsigned long n_bits, unsigned long *read_map,
2420 unsigned long *write_map, unsigned long *except_map,
2421 double time_to_wait, u32 * bits_set)
2422{
Florin Coras99368312018-08-02 10:45:44 -07002423 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002424 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002425 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002426
Florin Coras54693d22018-07-17 10:46:29 -07002427 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002428 {
Florin Coras54693d22018-07-17 10:46:29 -07002429 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002430 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002431
Florin Coras54693d22018-07-17 10:46:29 -07002432 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002433 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002434 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002435 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002436 else
2437 {
2438 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002439 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002440 }
2441 }
Florin Corase003a1b2019-06-05 10:47:16 -07002442 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002443
Florin Coras134a9962018-08-28 11:32:04 -07002444 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002445 {
Florin Coras134a9962018-08-28 11:32:04 -07002446 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002447 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002448 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2449 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002450 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002451 }
Florin Coras134a9962018-08-28 11:32:04 -07002452 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002453 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002454 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002455}
2456
Florin Coras99368312018-08-02 10:45:44 -07002457static int
Florin Coras294afe22019-01-07 17:49:17 -08002458vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2459 vcl_si_set * read_map, vcl_si_set * write_map,
2460 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002461 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002462{
Florin Coras14ed6df2019-03-06 21:13:42 -08002463 double wait = 0, start = 0;
2464
2465 if (!*bits_set)
2466 {
2467 wait = time_to_wait;
2468 start = clib_time_now (&wrk->clib_time);
2469 }
2470
2471 do
2472 {
2473 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2474 write_map, except_map, wait, bits_set);
2475 if (*bits_set)
2476 return *bits_set;
2477 if (wait == -1)
2478 continue;
2479
2480 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2481 }
2482 while (wait > 0);
2483
2484 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002485}
2486
2487static int
Florin Coras294afe22019-01-07 17:49:17 -08002488vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2489 vcl_si_set * read_map, vcl_si_set * write_map,
2490 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002491 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002492{
2493 vcl_mq_evt_conn_t *mqc;
2494 int __clib_unused n_read;
2495 int n_mq_evts, i;
2496 u64 buf;
2497
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002498 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2499 {
2500 vcl_api_retry_attach (wrk);
2501 return 0;
2502 }
2503
Florin Coras134a9962018-08-28 11:32:04 -07002504 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2505 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2506 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002507 for (i = 0; i < n_mq_evts; i++)
2508 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002509 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2510 {
2511 vcl_api_handle_disconnect (wrk);
2512 continue;
2513 }
2514
Florin Coras134a9962018-08-28 11:32:04 -07002515 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002516 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002517 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002518 except_map, 0, bits_set);
2519 }
2520
2521 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2522}
2523
Dave Wallace543852a2017-08-03 02:11:34 -04002524int
Florin Coras294afe22019-01-07 17:49:17 -08002525vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2526 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002527{
Florin Coras54693d22018-07-17 10:46:29 -07002528 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002529 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002530 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002531 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002532
Dave Wallace7876d392017-10-19 03:53:57 -04002533 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002534 {
Florin Coras134a9962018-08-28 11:32:04 -07002535 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002536 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002537 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2538 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002539 }
Dave Wallace7876d392017-10-19 03:53:57 -04002540 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002541 {
Florin Coras134a9962018-08-28 11:32:04 -07002542 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002543 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002544 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2545 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002546 }
Dave Wallace7876d392017-10-19 03:53:57 -04002547 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002548 {
Florin Coras134a9962018-08-28 11:32:04 -07002549 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002550 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002551 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2552 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002553 }
2554
Florin Coras54693d22018-07-17 10:46:29 -07002555 if (!n_bits)
2556 return 0;
2557
2558 if (!write_map)
2559 goto check_rd;
2560
Florin Coras096a1d52021-01-27 15:33:51 -08002561 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2562 {
2563 if (!(s = vcl_session_get (wrk, sid)))
2564 {
2565 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2566 bits_set++;
2567 continue;
2568 }
Florin Coras54693d22018-07-17 10:46:29 -07002569
Florin Coras096a1d52021-01-27 15:33:51 -08002570 if (vcl_session_write_ready (s))
2571 {
2572 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2573 bits_set++;
2574 }
2575 else
2576 {
2577 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2578 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2579 }
2580 }
Florin Coras54693d22018-07-17 10:46:29 -07002581
2582check_rd:
2583 if (!read_map)
2584 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002585
Florin Coras096a1d52021-01-27 15:33:51 -08002586 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2587 {
2588 if (!(s = vcl_session_get (wrk, sid)))
2589 {
2590 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2591 bits_set++;
2592 continue;
2593 }
Florin Coras54693d22018-07-17 10:46:29 -07002594
Florin Coras096a1d52021-01-27 15:33:51 -08002595 if (vcl_session_read_ready (s))
2596 {
2597 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2598 bits_set++;
2599 }
2600 }
Florin Coras54693d22018-07-17 10:46:29 -07002601
2602check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002603
Florin Coras86f04502018-09-12 16:08:01 -07002604 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2605 {
2606 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2607 read_map, write_map, except_map, &bits_set);
2608 }
2609 vec_reset_length (wrk->unhandled_evts_vector);
2610
Florin Coras99368312018-08-02 10:45:44 -07002611 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002612 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002613 time_to_wait, &bits_set);
2614 else
Florin Coras134a9962018-08-28 11:32:04 -07002615 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002616 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002617
Dave Wallace543852a2017-08-03 02:11:34 -04002618 return (bits_set);
2619}
2620
Dave Wallacef7f809c2017-10-03 01:48:42 -04002621static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002622vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002623{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002624 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002625 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002626 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002627
Florin Corasc0737e92019-03-04 14:19:39 -08002628 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002629 return;
2630
Florin Coras6c3b2182020-10-19 18:36:48 -07002631 s = vcl_session_get_w_handle (wrk, vep_handle);
2632 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002633 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002634 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002635 goto done;
2636 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002637 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002638 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002639 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002640 goto done;
2641 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002642 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002643 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002644 "{\n"
2645 " is_vep = %u\n"
2646 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002647 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002648 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2649 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002650
Florin Coras7e5e62b2019-07-30 14:08:23 -07002651 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002652 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002653 s = vcl_session_get_w_handle (wrk, sh);
2654 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002656 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002657 goto done;
2658 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002659 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002660 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002661 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002662 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002663 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002664 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002665 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002666 goto done;
2667 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002668 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002669 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2670 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002671 sh, s->vep.vep_sh, vep_handle);
2672 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002673 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002674 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002675 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002676 " next_sh = 0x%x (%u)\n"
2677 " prev_sh = 0x%x (%u)\n"
2678 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002679 " ev.events = 0x%x\n"
2680 " ev.data.u64 = 0x%llx\n"
2681 " et_mask = 0x%x\n"
2682 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002683 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002684 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2685 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686 }
2687 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688
2689done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002690 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691}
2692
2693int
2694vppcom_epoll_create (void)
2695{
Florin Coras134a9962018-08-28 11:32:04 -07002696 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002697 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002698
Florin Coras134a9962018-08-28 11:32:04 -07002699 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002700
Florin Coras6c3b2182020-10-19 18:36:48 -07002701 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002702 vep_session->vep.vep_sh = ~0;
2703 vep_session->vep.next_sh = ~0;
2704 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002705 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002706
Florin Corasa7a1a222018-12-30 17:11:31 -08002707 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2708 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002709
Florin Corasab2f6db2018-08-31 14:31:41 -07002710 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002711}
2712
2713int
Florin Coras134a9962018-08-28 11:32:04 -07002714vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 struct epoll_event *event)
2716{
Florin Coras134a9962018-08-28 11:32:04 -07002717 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002718 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002719 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002720 vcl_session_t *s;
2721 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002722
Florin Coras134a9962018-08-28 11:32:04 -07002723 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002724 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002725 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002726 return VPPCOM_EINVAL;
2727 }
2728
Florin Coras134a9962018-08-28 11:32:04 -07002729 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002730 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002731 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002732 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002733 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002734 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002735 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002736 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002737 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002738 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002739 }
2740
Florin Coras134a9962018-08-28 11:32:04 -07002741 ASSERT (vep_session->vep.vep_sh == ~0);
2742 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743
Florin Coras17ec5772020-08-12 20:46:29 -07002744 s = vcl_session_get_w_handle (wrk, session_handle);
2745 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002747 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002748 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002749 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002750 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002752 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002753 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002754 }
2755
2756 switch (op)
2757 {
2758 case EPOLL_CTL_ADD:
2759 if (PREDICT_FALSE (!event))
2760 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002761 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002762 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002763 }
Florin Coras2645f682021-06-04 15:59:41 -07002764 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2765 {
2766 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2767 rv = VPPCOM_EEXIST;
2768 goto done;
2769 }
Florin Coras134a9962018-08-28 11:32:04 -07002770 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 {
Florin Coras7e12d942018-06-27 14:32:43 -07002772 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002773 next_session = vcl_session_get_w_handle (wrk,
2774 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002775 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 {
Florin Coras5e062572019-03-14 19:07:51 -07002777 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002778 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002779 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002780 }
Florin Coras134a9962018-08-28 11:32:04 -07002781 ASSERT (next_session->vep.prev_sh == vep_handle);
2782 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002783 }
Florin Coras17ec5772020-08-12 20:46:29 -07002784 s->vep.next_sh = vep_session->vep.next_sh;
2785 s->vep.prev_sh = vep_handle;
2786 s->vep.vep_sh = vep_handle;
2787 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002788 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002789 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002790 s->flags &= ~VCL_SESSION_F_IS_VEP;
2791 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002792 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002793
Florin Coras17ec5772020-08-12 20:46:29 -07002794 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2795 if (txf && (event->events & EPOLLOUT))
2796 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002797
Florin Coras6e3c1f82020-01-15 01:30:46 +00002798 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002799 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002800 {
2801 session_event_t e = { 0 };
2802 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002803 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002804 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002805 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002806 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002807 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002808 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002809 {
2810 session_event_t e = { 0 };
2811 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002812 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002813 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002814 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2815 add_evt = 1;
2816 }
2817 if (!add_evt && vcl_session_is_closing (s))
2818 {
2819 session_event_t e = { 0 };
2820 if (s->session_state == VCL_STATE_VPP_CLOSING)
2821 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2822 else
2823 e.event_type = SESSION_CTRL_EVT_RESET;
2824 e.session_index = s->session_index;
2825 e.postponed = 1;
2826 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002827 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002828 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2829 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002830 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002831 break;
2832
2833 case EPOLL_CTL_MOD:
2834 if (PREDICT_FALSE (!event))
2835 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002836 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002837 rv = VPPCOM_EINVAL;
2838 goto done;
2839 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002840 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002841 {
Florin Coras5e062572019-03-14 19:07:51 -07002842 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002843 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002844 goto done;
2845 }
Florin Coras17ec5772020-08-12 20:46:29 -07002846 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002847 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002848 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002849 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002850 rv = VPPCOM_EINVAL;
2851 goto done;
2852 }
hanlin475c9d72019-12-26 11:44:28 +08002853
Florin Coras2645f682021-06-04 15:59:41 -07002854 /* Generate EPOLLOUT if session write ready nd event was not on */
2855 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2856 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002857 {
2858 session_event_t e = { 0 };
2859 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002860 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002861 vec_add1 (wrk->unhandled_evts_vector, e);
2862 }
Florin Coras2645f682021-06-04 15:59:41 -07002863 /* Generate EPOLLIN if session read ready and event was not on */
2864 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2865 (vcl_session_read_ready (s) > 0))
2866 {
2867 session_event_t e = { 0 };
2868 e.event_type = SESSION_IO_EVT_RX;
2869 e.session_index = s->session_index;
2870 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002871 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002872 }
Florin Coras17ec5772020-08-12 20:46:29 -07002873 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2874 s->vep.ev = *event;
2875 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002876 if (txf)
2877 {
2878 if (event->events & EPOLLOUT)
2879 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2880 else
2881 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2882 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002883 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2884 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002885 break;
2886
2887 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002888 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002889 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002890 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002891 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002892 goto done;
2893 }
Florin Coras17ec5772020-08-12 20:46:29 -07002894 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002895 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002896 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002897 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002898 rv = VPPCOM_EINVAL;
2899 goto done;
2900 }
2901
Florin Coras17ec5772020-08-12 20:46:29 -07002902 if (s->vep.prev_sh == vep_handle)
2903 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002904 else
2905 {
Florin Coras7e12d942018-06-27 14:32:43 -07002906 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002907 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002908 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002909 {
Florin Coras5e062572019-03-14 19:07:51 -07002910 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002911 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002912 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002913 }
Florin Coras134a9962018-08-28 11:32:04 -07002914 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002915 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002916 }
Florin Coras17ec5772020-08-12 20:46:29 -07002917 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002918 {
Florin Coras7e12d942018-06-27 14:32:43 -07002919 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002920 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002921 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002922 {
Florin Coras5e062572019-03-14 19:07:51 -07002923 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002924 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002925 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002926 }
Florin Coras134a9962018-08-28 11:32:04 -07002927 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002928 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002929 }
2930
Florin Corasfa3884f2021-06-22 20:04:46 -07002931 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2932 vcl_epoll_lt_del (wrk, s);
2933
Florin Coras17ec5772020-08-12 20:46:29 -07002934 memset (&s->vep, 0, sizeof (s->vep));
2935 s->vep.next_sh = ~0;
2936 s->vep.prev_sh = ~0;
2937 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002938 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002939 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002940
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002941 if (vcl_session_is_open (s))
2942 {
2943 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2944 if (txf)
2945 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2946 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002947
Florin Coras5e062572019-03-14 19:07:51 -07002948 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002949 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002950 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002951 break;
2952
2953 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002954 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002955 rv = VPPCOM_EINVAL;
2956 }
2957
Florin Coras134a9962018-08-28 11:32:04 -07002958 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002959
2960done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002961 return rv;
2962}
2963
Florin Coras86f04502018-09-12 16:08:01 -07002964static inline void
2965vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2966 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002967{
2968 session_disconnected_msg_t *disconnected_msg;
2969 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002970 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002971 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002972 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002973 u8 add_event = 0;
2974
2975 switch (e->event_type)
2976 {
Florin Coras653e43f2019-03-04 10:56:23 -08002977 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002978 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002979 s = vcl_session_get (wrk, sid);
2980 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002981 break;
Florin Corasb242d312020-10-26 15:35:40 -07002982 vcl_fifo_rx_evt_valid_or_break (s);
2983 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07002984 if (!(EPOLLIN & s->vep.ev.events) ||
2985 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
2986 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07002987 break;
2988 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08002989 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002990 session_evt_data = s->vep.ev.data.u64;
2991 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002992 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002993 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002994 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002995 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08002996 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002997 break;
Florin Corasb242d312020-10-26 15:35:40 -07002998 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002999 if (!(EPOLLOUT & session_events))
3000 break;
3001 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003002 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003003 session_evt_data = s->vep.ev.data.u64;
3004 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3005 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003006 break;
Florin Coras86f04502018-09-12 16:08:01 -07003007 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003008 if (!e->postponed)
3009 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3010 else
3011 s = vcl_session_get (wrk, e->session_index);
3012 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003013 break;
Florin Corasb242d312020-10-26 15:35:40 -07003014 session_events = s->vep.ev.events;
3015 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003016 if (!(EPOLLIN & session_events) ||
3017 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003018 break;
Florin Coras86f04502018-09-12 16:08:01 -07003019 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003020 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003021 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003022 break;
3023 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003024 if (!e->postponed)
3025 {
3026 connected_msg = (session_connected_msg_t *) e->data;
3027 sid = vcl_session_connected_handler (wrk, connected_msg);
3028 }
3029 else
3030 sid = e->session_index;
3031 s = vcl_session_get (wrk, sid);
3032 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003033 break;
Florin Corasb242d312020-10-26 15:35:40 -07003034 session_events = s->vep.ev.events;
3035 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003036 if (!(EPOLLOUT & session_events))
3037 break;
3038 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003039 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003040 session_evt_data = s->vep.ev.data.u64;
3041 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003042 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003043 break;
3044 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003045 if (!e->postponed)
3046 {
3047 disconnected_msg = (session_disconnected_msg_t *) e->data;
3048 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3049 }
3050 else
3051 {
3052 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003053 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003054 }
3055 if (vcl_session_is_closed (s) ||
3056 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003057 {
Florin Coras74254b52021-12-08 11:03:08 -08003058 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003059 vcl_session_free (wrk, s);
3060 break;
3061 }
Florin Corasb242d312020-10-26 15:35:40 -07003062 sid = s->session_index;
3063 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003064 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003065 if (EPOLLRDHUP & session_events)
3066 {
3067 /* If app can distinguish between RDHUP and HUP,
3068 * we make finer control */
3069 events[*num_ev].events = EPOLLRDHUP;
3070 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3071 {
3072 events[*num_ev].events |= EPOLLHUP;
3073 }
3074 }
3075 else
3076 {
3077 events[*num_ev].events = EPOLLHUP;
3078 }
Florin Corasb242d312020-10-26 15:35:40 -07003079 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003080
Florin Coras86f04502018-09-12 16:08:01 -07003081 break;
3082 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003083 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003084 {
3085 sid =
3086 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3087 s = vcl_session_get (wrk, sid);
3088 }
Florin Coras87f76002021-06-28 19:13:29 -07003089 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003090 {
3091 sid = e->session_index;
3092 s = vcl_session_get (wrk, sid);
3093 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3094 }
Florin Coras87f76002021-06-28 19:13:29 -07003095 if (vcl_session_is_closed (s) ||
3096 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003097 {
Florin Coras74254b52021-12-08 11:03:08 -08003098 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003099 vcl_session_free (wrk, s);
3100 break;
3101 }
Florin Corasb242d312020-10-26 15:35:40 -07003102 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003103 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003104 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003105 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003106 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003107 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3108 vcl_session_unlisten_reply_handler (wrk, e->data);
3109 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003110 case SESSION_CTRL_EVT_MIGRATED:
3111 vcl_session_migrated_handler (wrk, e->data);
3112 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003113 case SESSION_CTRL_EVT_CLEANUP:
3114 vcl_session_cleanup_handler (wrk, e->data);
3115 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003116 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3117 vcl_session_req_worker_update_handler (wrk, e->data);
3118 break;
3119 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3120 vcl_session_worker_update_reply_handler (wrk, e->data);
3121 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003122 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3123 vcl_session_app_add_segment_handler (wrk, e->data);
3124 break;
3125 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3126 vcl_session_app_del_segment_handler (wrk, e->data);
3127 break;
hanlina3a48962020-07-13 11:09:15 +08003128 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003129 vcl_worker_rpc_handler (wrk, e->data);
3130 break;
Florin Coras86f04502018-09-12 16:08:01 -07003131 default:
3132 VDBG (0, "unhandled: %u", e->event_type);
3133 break;
3134 }
3135
3136 if (add_event)
3137 {
3138 events[*num_ev].data.u64 = session_evt_data;
3139 if (EPOLLONESHOT & session_events)
3140 {
Florin Corasb242d312020-10-26 15:35:40 -07003141 s = vcl_session_get (wrk, sid);
3142 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003143 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003144 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003145 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003146 s = vcl_session_get (wrk, sid);
3147 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3148 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003149 }
Florin Coras86f04502018-09-12 16:08:01 -07003150 *num_ev += 1;
3151 }
3152}
3153
3154static int
3155vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3156 struct epoll_event *events, u32 maxevents,
3157 double wait_for_time, u32 * num_ev)
3158{
Florin Coras99368312018-08-02 10:45:44 -07003159 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003160 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003161 int i;
3162
Florin Coras539663c2018-09-28 14:59:37 -07003163 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3164 goto handle_dequeued;
3165
Florin Coras54693d22018-07-17 10:46:29 -07003166 if (svm_msg_q_is_empty (mq))
3167 {
3168 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003169 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003170 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003171 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003172 else
3173 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003174 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003175 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003176 }
3177 }
Florin Corase003a1b2019-06-05 10:47:16 -07003178 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003179 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003180
Florin Coras539663c2018-09-28 14:59:37 -07003181handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003182 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003183 {
Florin Coras134a9962018-08-28 11:32:04 -07003184 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003185 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003186 if (*num_ev < maxevents)
3187 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3188 else
3189 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003190 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003191 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003192 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003193 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003194 return *num_ev;
3195}
3196
Florin Coras99368312018-08-02 10:45:44 -07003197static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003198vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3199 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003200{
Florin Corasccdb8b82021-04-28 13:01:06 -07003201 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003202
3203 if (!n_evts)
3204 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003205 if (timeout_ms > 0)
3206 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003207 }
3208
3209 do
3210 {
3211 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003212 timeout_ms, &n_evts);
3213 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003214 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003215 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003216 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003217
3218 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003219}
3220
3221static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003222vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3223 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003224{
Florin Coras99368312018-08-02 10:45:44 -07003225 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003226 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003227 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003228 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003229 u64 buf;
3230
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003231 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3232 {
3233 vcl_api_retry_attach (wrk);
3234 return n_evts;
3235 }
3236
Florin Coras134a9962018-08-28 11:32:04 -07003237 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003238 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003239 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003240 if (timeout_ms > 0)
3241 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003242 }
3243
Florin Corasb13ba132021-01-27 16:05:24 -08003244 do
3245 {
3246 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003247 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003248 if (n_mq_evts < 0)
3249 {
3250 VDBG (0, "epoll_wait error %u", errno);
3251 return n_evts;
3252 }
3253
3254 for (i = 0; i < n_mq_evts; i++)
3255 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003256 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3257 {
3258 /* api socket was closed */
3259 vcl_api_handle_disconnect (wrk);
3260 continue;
3261 }
3262
Florin Corasb13ba132021-01-27 16:05:24 -08003263 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3264 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3265 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3266 &n_evts);
3267 }
3268
Florin Corasccdb8b82021-04-28 13:01:06 -07003269 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003270 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003271 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003272 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003273
3274 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003275}
3276
Florin Corasfe286f72021-06-04 10:07:55 -07003277static void
Florin Corasfe286f72021-06-04 10:07:55 -07003278vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3279 int maxevents, u32 *n_evts)
3280{
Florin Coras734268f2021-06-30 07:54:29 -07003281 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003282 vcl_session_t *s;
3283 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003284 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003285
Florin Corasfa3884f2021-06-22 20:04:46 -07003286 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003287 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003288 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003289
Florin Corasfa3884f2021-06-22 20:04:46 -07003290 next = wrk->ep_lt_current;
3291 do
Florin Corasfe286f72021-06-04 10:07:55 -07003292 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003293 s = vcl_session_get (wrk, next);
3294 next = s->vep.lt_next;
3295
3296 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003297 {
3298 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003299 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003300 evt_data = s->vep.ev.data.u64;
3301 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003302 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003303 {
3304 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003305 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3306 evt_data = s->vep.ev.data.u64;
3307 }
3308 if (!add_event && s->session_state > VCL_STATE_READY)
3309 {
3310 add_event = 1;
3311 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003312 evt_data = s->vep.ev.data.u64;
3313 }
3314 if (add_event)
3315 {
3316 events[*n_evts].data.u64 = evt_data;
3317 *n_evts += 1;
3318 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003319 if (EPOLLONESHOT & s->vep.ev.events)
3320 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003321 if (*n_evts == maxevents)
3322 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003323 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003324 break;
3325 }
3326 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003327 else
3328 {
3329 vcl_epoll_lt_del (wrk, s);
3330 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3331 break;
3332 }
Florin Corasfe286f72021-06-04 10:07:55 -07003333 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003334 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003335}
3336
Dave Wallacef7f809c2017-10-03 01:48:42 -04003337int
Florin Coras134a9962018-08-28 11:32:04 -07003338vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003339 int maxevents, double wait_for_time)
3340{
Florin Coras134a9962018-08-28 11:32:04 -07003341 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003342 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003343 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003344 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003345
3346 if (PREDICT_FALSE (maxevents <= 0))
3347 {
Florin Coras5e062572019-03-14 19:07:51 -07003348 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003349 return VPPCOM_EINVAL;
3350 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003351
Florin Coras134a9962018-08-28 11:32:04 -07003352 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003353 if (!vep_session)
3354 return VPPCOM_EBADFD;
3355
Florin Coras6c3b2182020-10-19 18:36:48 -07003356 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003357 {
Florin Coras5e062572019-03-14 19:07:51 -07003358 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003359 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003360 }
Florin Coras54693d22018-07-17 10:46:29 -07003361
Florin Coras86f04502018-09-12 16:08:01 -07003362 if (vec_len (wrk->unhandled_evts_vector))
3363 {
3364 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3365 {
3366 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3367 events, &n_evts);
3368 if (n_evts == maxevents)
3369 {
Florin Corase003a1b2019-06-05 10:47:16 -07003370 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3371 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003372 }
3373 }
Florin Corase003a1b2019-06-05 10:47:16 -07003374 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003375 }
liuyacancba87102021-09-10 15:14:05 +08003376
3377 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3378 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3379
wanghanlin8919fec2021-03-18 20:00:41 +08003380 /* Request to only drain unhandled */
3381 if ((int) wait_for_time == -2)
3382 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003383
Florin Coras86f04502018-09-12 16:08:01 -07003384
Florin Corasfe286f72021-06-04 10:07:55 -07003385 if (vcm->cfg.use_mq_eventfd)
3386 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3387 wait_for_time);
3388 else
3389 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3390 wait_for_time);
3391
Florin Corasfe286f72021-06-04 10:07:55 -07003392 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003393}
3394
Dave Wallace35830af2017-10-09 01:43:42 -04003395int
Florin Coras134a9962018-08-28 11:32:04 -07003396vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003397 void *buffer, uint32_t * buflen)
3398{
Florin Coras134a9962018-08-28 11:32:04 -07003399 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003400 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003401 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003402 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003403 vcl_session_t *session;
3404 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003405
Florin Coras134a9962018-08-28 11:32:04 -07003406 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003407 if (!session)
3408 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003409
Dave Wallace35830af2017-10-09 01:43:42 -04003410 switch (op)
3411 {
3412 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003413 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003414 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3415 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003416 break;
3417
Dave Wallace227867f2017-11-13 21:21:53 -05003418 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003419 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003420 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3421 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003422 break;
3423
3424 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003425 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003426 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003427 *flags =
3428 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003429 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003430 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003431 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003432 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3433 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003434 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003435 }
3436 else
3437 rv = VPPCOM_EINVAL;
3438 break;
3439
3440 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003441 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003442 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003443 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003444 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003445 else
Florin Corasac422d62020-10-19 20:51:36 -07003446 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003447
Florin Coras5e062572019-03-14 19:07:51 -07003448 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3449 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003450 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003451 }
3452 else
3453 rv = VPPCOM_EINVAL;
3454 break;
3455
3456 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003457 if (PREDICT_TRUE (buffer && buflen &&
3458 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003459 {
Florin Coras7e12d942018-06-27 14:32:43 -07003460 ep->is_ip4 = session->transport.is_ip4;
3461 ep->port = session->transport.rmt_port;
3462 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003463 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3464 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003465 else
Dave Barach178cf492018-11-13 16:34:13 -05003466 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3467 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003468 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003469 VDBG (1,
3470 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3471 "addr = %U, port %u",
3472 session_handle, ep->is_ip4, vcl_format_ip46_address,
3473 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003474 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3475 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003476 }
3477 else
3478 rv = VPPCOM_EINVAL;
3479 break;
3480
3481 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003482 if (PREDICT_TRUE (buffer && buflen &&
3483 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003484 {
Florin Coras7e12d942018-06-27 14:32:43 -07003485 ep->is_ip4 = session->transport.is_ip4;
3486 ep->port = session->transport.lcl_port;
3487 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003488 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3489 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003490 else
Dave Barach178cf492018-11-13 16:34:13 -05003491 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3492 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003493 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003494 VDBG (1,
3495 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3496 " port %d",
3497 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003498 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003499 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3500 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003501 }
3502 else
3503 rv = VPPCOM_EINVAL;
3504 break;
Stevenb5a11602017-10-11 09:59:30 -07003505
Florin Corasef7cbf62019-10-17 09:56:27 -07003506 case VPPCOM_ATTR_SET_LCL_ADDR:
3507 if (PREDICT_TRUE (buffer && buflen &&
3508 (*buflen >= sizeof (*ep)) && ep->ip))
3509 {
3510 session->transport.is_ip4 = ep->is_ip4;
3511 session->transport.lcl_port = ep->port;
3512 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3513 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003514 VDBG (1,
3515 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3516 " port %d",
3517 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003518 &session->transport.lcl_ip,
3519 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3520 clib_net_to_host_u16 (ep->port));
3521 }
3522 else
3523 rv = VPPCOM_EINVAL;
3524 break;
3525
Dave Wallace048b1d62018-01-03 22:24:41 -05003526 case VPPCOM_ATTR_GET_LIBC_EPFD:
3527 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003528 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003529 break;
3530
3531 case VPPCOM_ATTR_SET_LIBC_EPFD:
3532 if (PREDICT_TRUE (buffer && buflen &&
3533 (*buflen == sizeof (session->libc_epfd))))
3534 {
3535 session->libc_epfd = *(int *) buffer;
3536 *buflen = sizeof (session->libc_epfd);
3537
Florin Coras5e062572019-03-14 19:07:51 -07003538 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3539 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003540 }
3541 else
3542 rv = VPPCOM_EINVAL;
3543 break;
3544
3545 case VPPCOM_ATTR_GET_PROTOCOL:
3546 if (buffer && buflen && (*buflen >= sizeof (int)))
3547 {
Florin Coras7e12d942018-06-27 14:32:43 -07003548 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003549 *buflen = sizeof (int);
3550
Florin Coras5e062572019-03-14 19:07:51 -07003551 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3552 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003553 }
3554 else
3555 rv = VPPCOM_EINVAL;
3556 break;
3557
3558 case VPPCOM_ATTR_GET_LISTEN:
3559 if (buffer && buflen && (*buflen >= sizeof (int)))
3560 {
Florin Corasac422d62020-10-19 20:51:36 -07003561 *(int *) buffer = vcl_session_has_attr (session,
3562 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003563 *buflen = sizeof (int);
3564
Florin Coras5e062572019-03-14 19:07:51 -07003565 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3566 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003567 }
3568 else
3569 rv = VPPCOM_EINVAL;
3570 break;
3571
3572 case VPPCOM_ATTR_GET_ERROR:
3573 if (buffer && buflen && (*buflen >= sizeof (int)))
3574 {
3575 *(int *) buffer = 0;
3576 *buflen = sizeof (int);
3577
Florin Coras5e062572019-03-14 19:07:51 -07003578 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3579 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003580 }
3581 else
3582 rv = VPPCOM_EINVAL;
3583 break;
3584
3585 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3586 if (buffer && buflen && (*buflen >= sizeof (u32)))
3587 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003588
3589 /* VPP-TBD */
3590 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003591 session->tx_fifo ?
3592 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003593 vcm->cfg.tx_fifo_size);
3594 *buflen = sizeof (u32);
3595
Florin Coras5e062572019-03-14 19:07:51 -07003596 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3597 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3598 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003599 }
3600 else
3601 rv = VPPCOM_EINVAL;
3602 break;
3603
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003604 case VPPCOM_ATTR_SET_DSCP:
3605 if (buffer && buflen && (*buflen >= sizeof (u8)))
3606 {
3607 session->dscp = *(u8 *) buffer;
3608
3609 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3610 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3611 }
3612 else
3613 rv = VPPCOM_EINVAL;
3614 break;
3615
Dave Wallace048b1d62018-01-03 22:24:41 -05003616 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3617 if (buffer && buflen && (*buflen == sizeof (u32)))
3618 {
3619 /* VPP-TBD */
3620 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003621 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3622 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3623 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003624 }
3625 else
3626 rv = VPPCOM_EINVAL;
3627 break;
3628
3629 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3630 if (buffer && buflen && (*buflen >= sizeof (u32)))
3631 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003632
3633 /* VPP-TBD */
3634 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003635 session->rx_fifo ?
3636 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003637 vcm->cfg.rx_fifo_size);
3638 *buflen = sizeof (u32);
3639
Florin Coras5e062572019-03-14 19:07:51 -07003640 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3641 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003642 }
3643 else
3644 rv = VPPCOM_EINVAL;
3645 break;
3646
3647 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3648 if (buffer && buflen && (*buflen == sizeof (u32)))
3649 {
3650 /* VPP-TBD */
3651 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003652 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3653 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3654 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003655 }
3656 else
3657 rv = VPPCOM_EINVAL;
3658 break;
3659
3660 case VPPCOM_ATTR_GET_REUSEADDR:
3661 if (buffer && buflen && (*buflen >= sizeof (int)))
3662 {
3663 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003664 *(int *) buffer = vcl_session_has_attr (session,
3665 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003666 *buflen = sizeof (int);
3667
Florin Coras5e062572019-03-14 19:07:51 -07003668 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3669 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003670 }
3671 else
3672 rv = VPPCOM_EINVAL;
3673 break;
3674
Stevenb5a11602017-10-11 09:59:30 -07003675 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003676 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003677 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003678 {
3679 /* VPP-TBD */
3680 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003681 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003682 else
Florin Corasac422d62020-10-19 20:51:36 -07003683 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003684
Florin Coras5e062572019-03-14 19:07:51 -07003685 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003686 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003687 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003688 }
3689 else
3690 rv = VPPCOM_EINVAL;
3691 break;
3692
3693 case VPPCOM_ATTR_GET_REUSEPORT:
3694 if (buffer && buflen && (*buflen >= sizeof (int)))
3695 {
3696 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003697 *(int *) buffer = vcl_session_has_attr (session,
3698 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003699 *buflen = sizeof (int);
3700
Florin Coras5e062572019-03-14 19:07:51 -07003701 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3702 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003703 }
3704 else
3705 rv = VPPCOM_EINVAL;
3706 break;
3707
3708 case VPPCOM_ATTR_SET_REUSEPORT:
3709 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003710 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003711 {
3712 /* VPP-TBD */
3713 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003714 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003715 else
Florin Corasac422d62020-10-19 20:51:36 -07003716 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003717
Florin Coras5e062572019-03-14 19:07:51 -07003718 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003719 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003720 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003721 }
3722 else
3723 rv = VPPCOM_EINVAL;
3724 break;
3725
3726 case VPPCOM_ATTR_GET_BROADCAST:
3727 if (buffer && buflen && (*buflen >= sizeof (int)))
3728 {
3729 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003730 *(int *) buffer = vcl_session_has_attr (session,
3731 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003732 *buflen = sizeof (int);
3733
Florin Coras5e062572019-03-14 19:07:51 -07003734 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3735 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003736 }
3737 else
3738 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003739 break;
3740
3741 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003742 if (buffer && buflen && (*buflen == sizeof (int)))
3743 {
3744 /* VPP-TBD */
3745 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003746 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003747 else
Florin Corasac422d62020-10-19 20:51:36 -07003748 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003749
Florin Coras5e062572019-03-14 19:07:51 -07003750 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003751 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003752 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003753 }
3754 else
3755 rv = VPPCOM_EINVAL;
3756 break;
3757
3758 case VPPCOM_ATTR_GET_V6ONLY:
3759 if (buffer && buflen && (*buflen >= sizeof (int)))
3760 {
3761 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003762 *(int *) buffer = vcl_session_has_attr (session,
3763 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003764 *buflen = sizeof (int);
3765
Florin Coras5e062572019-03-14 19:07:51 -07003766 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3767 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003768 }
3769 else
3770 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003771 break;
3772
3773 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003774 if (buffer && buflen && (*buflen == sizeof (int)))
3775 {
3776 /* VPP-TBD */
3777 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003778 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003779 else
Florin Corasac422d62020-10-19 20:51:36 -07003780 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003781
Florin Coras5e062572019-03-14 19:07:51 -07003782 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003783 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003784 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003785 }
3786 else
3787 rv = VPPCOM_EINVAL;
3788 break;
3789
3790 case VPPCOM_ATTR_GET_KEEPALIVE:
3791 if (buffer && buflen && (*buflen >= sizeof (int)))
3792 {
3793 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003794 *(int *) buffer = vcl_session_has_attr (session,
3795 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003796 *buflen = sizeof (int);
3797
Florin Coras5e062572019-03-14 19:07:51 -07003798 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3799 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003800 }
3801 else
3802 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003803 break;
Stevenbccd3392017-10-12 20:42:21 -07003804
3805 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003806 if (buffer && buflen && (*buflen == sizeof (int)))
3807 {
3808 /* VPP-TBD */
3809 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003810 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 else
Florin Corasac422d62020-10-19 20:51:36 -07003812 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003813
Florin Coras5e062572019-03-14 19:07:51 -07003814 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003815 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003816 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 }
3818 else
3819 rv = VPPCOM_EINVAL;
3820 break;
3821
3822 case VPPCOM_ATTR_GET_TCP_NODELAY:
3823 if (buffer && buflen && (*buflen >= sizeof (int)))
3824 {
3825 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003826 *(int *) buffer = vcl_session_has_attr (session,
3827 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 *buflen = sizeof (int);
3829
Florin Coras5e062572019-03-14 19:07:51 -07003830 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3831 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 }
3833 else
3834 rv = VPPCOM_EINVAL;
3835 break;
3836
3837 case VPPCOM_ATTR_SET_TCP_NODELAY:
3838 if (buffer && buflen && (*buflen == sizeof (int)))
3839 {
3840 /* VPP-TBD */
3841 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003842 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 else
Florin Corasac422d62020-10-19 20:51:36 -07003844 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003845
Florin Coras5e062572019-03-14 19:07:51 -07003846 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003847 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003848 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003849 }
3850 else
3851 rv = VPPCOM_EINVAL;
3852 break;
3853
3854 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3855 if (buffer && buflen && (*buflen >= sizeof (int)))
3856 {
3857 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003858 *(int *) buffer = vcl_session_has_attr (session,
3859 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003860 *buflen = sizeof (int);
3861
Florin Coras5e062572019-03-14 19:07:51 -07003862 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3863 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003864 }
3865 else
3866 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003867 break;
3868
3869 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003870 if (buffer && buflen && (*buflen == sizeof (int)))
3871 {
3872 /* VPP-TBD */
3873 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003874 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003875 else
Florin Corasac422d62020-10-19 20:51:36 -07003876 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003877
Florin Coras5e062572019-03-14 19:07:51 -07003878 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003879 vcl_session_has_attr (session,
3880 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003881 }
3882 else
3883 rv = VPPCOM_EINVAL;
3884 break;
3885
3886 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3887 if (buffer && buflen && (*buflen >= sizeof (int)))
3888 {
3889 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003890 *(int *) buffer = vcl_session_has_attr (session,
3891 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003892 *buflen = sizeof (int);
3893
Florin Coras5e062572019-03-14 19:07:51 -07003894 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3895 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003896 }
3897 else
3898 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003899 break;
3900
3901 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003902 if (buffer && buflen && (*buflen == sizeof (int)))
3903 {
3904 /* VPP-TBD */
3905 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003906 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003907 else
Florin Corasac422d62020-10-19 20:51:36 -07003908 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003909
Florin Coras5e062572019-03-14 19:07:51 -07003910 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003911 vcl_session_has_attr (session,
3912 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003913 }
3914 else
3915 rv = VPPCOM_EINVAL;
3916 break;
3917
3918 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003919 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003920 {
Florin Coras04ae8272021-04-12 19:55:37 -07003921 rv = VPPCOM_EINVAL;
3922 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003923 }
Florin Coras04ae8272021-04-12 19:55:37 -07003924
3925 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3926 tea.mss = *(u32 *) buffer;
3927 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3928 rv = VPPCOM_ENOPROTOOPT;
3929
3930 if (!rv)
3931 {
3932 *(u32 *) buffer = tea.mss;
3933 *buflen = sizeof (int);
3934 }
3935
3936 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3937 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003938 break;
3939
3940 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003941 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003942 {
Florin Coras04ae8272021-04-12 19:55:37 -07003943 rv = VPPCOM_EINVAL;
3944 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003945 }
Florin Coras04ae8272021-04-12 19:55:37 -07003946
3947 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3948 tea.mss = *(u32 *) buffer;
3949 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3950 rv = VPPCOM_ENOPROTOOPT;
3951
3952 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3953 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003954 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003955
Florin Coras1e966172020-05-16 18:18:14 +00003956 case VPPCOM_ATTR_SET_CONNECTED:
3957 session->flags |= VCL_SESSION_F_CONNECTED;
3958 break;
3959
Florin Corasa5a9efd2021-01-05 17:03:29 -08003960 case VPPCOM_ATTR_SET_CKPAIR:
3961 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3962 !vcl_session_has_crypto (session))
3963 {
3964 rv = VPPCOM_EINVAL;
3965 break;
3966 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003967 if (!session->ext_config)
3968 {
Florin Coras87f63892021-05-01 16:01:40 -07003969 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3970 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003971 }
3972 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3973 {
3974 rv = VPPCOM_EINVAL;
3975 break;
3976 }
3977
3978 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003979 break;
3980
Florin Coras6a6555a2021-01-28 11:39:27 -08003981 case VPPCOM_ATTR_SET_VRF:
3982 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3983 {
3984 rv = VPPCOM_EINVAL;
3985 break;
3986 }
3987 session->vrf = *(u32 *) buffer;
3988 break;
3989
3990 case VPPCOM_ATTR_GET_VRF:
3991 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3992 {
3993 rv = VPPCOM_EINVAL;
3994 break;
3995 }
3996 *(u32 *) buffer = session->vrf;
3997 *buflen = sizeof (u32);
3998 break;
3999
wanghanlin0674f852021-02-22 10:38:36 +08004000 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004001 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004002 {
Florin Corasd77325c2021-02-23 12:03:03 -08004003 rv = VPPCOM_EINVAL;
4004 break;
wanghanlin0674f852021-02-22 10:38:36 +08004005 }
Florin Corasd77325c2021-02-23 12:03:03 -08004006
4007 if (session->transport.is_ip4)
4008 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004009 else
Florin Corasd77325c2021-02-23 12:03:03 -08004010 *(int *) buffer = AF_INET6;
4011 *buflen = sizeof (int);
4012
wanghanlin0674f852021-02-22 10:38:36 +08004013 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4014 *buflen);
4015 break;
4016
Florin Coras87f63892021-05-01 16:01:40 -07004017 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4018 if (!(buffer && buflen && (*buflen > 0)))
4019 {
4020 rv = VPPCOM_EINVAL;
4021 break;
4022 }
4023 if (session->ext_config)
4024 {
4025 rv = VPPCOM_EINVAL;
4026 break;
4027 }
4028 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4029 *buflen + sizeof (u32));
4030 clib_memcpy (session->ext_config->data, buffer, *buflen);
4031 session->ext_config->len = *buflen;
4032 break;
4033
Dave Wallacee22aa742017-10-20 12:30:38 -04004034 default:
4035 rv = VPPCOM_EINVAL;
4036 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004037 }
4038
Dave Wallace35830af2017-10-09 01:43:42 -04004039 return rv;
4040}
4041
Stevenac1f96d2017-10-24 16:03:58 -07004042int
Florin Coras134a9962018-08-28 11:32:04 -07004043vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004044 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4045{
Florin Coras134a9962018-08-28 11:32:04 -07004046 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004047 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004048 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004049
4050 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004051 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004052 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004053 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004054 else
4055 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004056 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004057 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004058 }
4059
Florin Coras0a1e1832020-03-29 18:54:04 +00004060 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004061 {
Florin Coras5da10c42020-04-01 04:31:21 +00004062 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004063 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004064 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4065 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004066 else
Dave Barach178cf492018-11-13 16:34:13 -05004067 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4068 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004069 ep->is_ip4 = session->transport.is_ip4;
4070 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004071 }
Florin Coras460dce62018-07-27 05:45:06 -07004072
Stevenac1f96d2017-10-24 16:03:58 -07004073 return rv;
4074}
4075
4076int
Florin Coras134a9962018-08-28 11:32:04 -07004077vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004078 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4079{
Florin Coras7a2abce2020-04-05 19:25:44 +00004080 vcl_worker_t *wrk = vcl_worker_get_current ();
4081 vcl_session_t *s;
4082
4083 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004084 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004085 return VPPCOM_EBADFD;
4086
Dave Wallace617dffa2017-10-26 14:47:06 -04004087 if (ep)
4088 {
Florin Coras5824cc52020-10-20 18:44:41 -07004089 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004090 return VPPCOM_EINVAL;
4091
liuyacanbc0c7542021-08-02 20:15:05 +08004092 s->transport.is_ip4 = ep->is_ip4;
4093 s->transport.rmt_port = ep->port;
4094 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4095
Florin Coras5da10c42020-04-01 04:31:21 +00004096 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004097 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004098 {
Florin Coras5824cc52020-10-20 18:44:41 -07004099 u32 session_index = s->session_index;
4100 f64 timeout = vcm->cfg.session_timeout;
4101 int rv;
4102
Florin Coras5da10c42020-04-01 04:31:21 +00004103 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004104 rv = vppcom_wait_for_session_state_change (session_index,
4105 VCL_STATE_READY,
4106 timeout);
4107 if (rv < 0)
4108 return rv;
4109 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004110 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004111 }
4112
4113 if (flags)
4114 {
4115 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004116 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004117 }
4118
Florin Coras7a2abce2020-04-05 19:25:44 +00004119 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4120 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004121}
4122
Dave Wallace048b1d62018-01-03 22:24:41 -05004123int
4124vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4125{
Florin Coras134a9962018-08-28 11:32:04 -07004126 vcl_worker_t *wrk = vcl_worker_get_current ();
4127 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004128 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004129 svm_msg_q_msg_t msg;
4130 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004131 int rv, num_ev = 0;
4132
Florin Coras5e062572019-03-14 19:07:51 -07004133 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004134
4135 if (!vp)
4136 return VPPCOM_EFAULT;
4137
4138 do
4139 {
Florin Coras7e12d942018-06-27 14:32:43 -07004140 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004141
Florin Coras6917b942018-11-13 22:44:54 -08004142 /* Dequeue all events and drop all unhandled io events */
4143 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4144 {
4145 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4146 vcl_handle_mq_event (wrk, e);
4147 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4148 }
4149 vec_reset_length (wrk->unhandled_evts_vector);
4150
Dave Wallace048b1d62018-01-03 22:24:41 -05004151 for (i = 0; i < n_sids; i++)
4152 {
Florin Coras7baeb712019-01-04 17:05:43 -08004153 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004154 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004155 {
4156 vp[i].revents = POLLHUP;
4157 num_ev++;
4158 continue;
4159 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004160
Florin Coras6917b942018-11-13 22:44:54 -08004161 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004162
4163 if (POLLIN & vp[i].events)
4164 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004165 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004166 if (rv > 0)
4167 {
Florin Coras6917b942018-11-13 22:44:54 -08004168 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004169 num_ev++;
4170 }
4171 else if (rv < 0)
4172 {
4173 switch (rv)
4174 {
4175 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004176 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004177 break;
4178
4179 default:
Florin Coras6917b942018-11-13 22:44:54 -08004180 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004181 break;
4182 }
4183 num_ev++;
4184 }
4185 }
4186
4187 if (POLLOUT & vp[i].events)
4188 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004189 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004190 if (rv > 0)
4191 {
Florin Coras6917b942018-11-13 22:44:54 -08004192 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004193 num_ev++;
4194 }
4195 else if (rv < 0)
4196 {
4197 switch (rv)
4198 {
4199 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004200 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004201 break;
4202
4203 default:
Florin Coras6917b942018-11-13 22:44:54 -08004204 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004205 break;
4206 }
4207 num_ev++;
4208 }
4209 }
4210
Dave Wallace7e607a72018-06-18 18:41:32 -04004211 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004212 {
Florin Coras6917b942018-11-13 22:44:54 -08004213 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004214 num_ev++;
4215 }
4216 }
4217 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004218 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004219 }
4220 while ((num_ev == 0) && keep_trying);
4221
Dave Wallace048b1d62018-01-03 22:24:41 -05004222 return num_ev;
4223}
4224
Florin Coras99368312018-08-02 10:45:44 -07004225int
4226vppcom_mq_epoll_fd (void)
4227{
Florin Coras134a9962018-08-28 11:32:04 -07004228 vcl_worker_t *wrk = vcl_worker_get_current ();
4229 return wrk->mqs_epfd;
4230}
4231
4232int
Florin Coras30e79c22019-01-02 19:31:22 -08004233vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004234{
4235 return session_handle & 0xFFFFFF;
4236}
4237
4238int
Florin Coras30e79c22019-01-02 19:31:22 -08004239vppcom_session_worker (vcl_session_handle_t session_handle)
4240{
4241 return session_handle >> 24;
4242}
4243
4244int
Florin Coras134a9962018-08-28 11:32:04 -07004245vppcom_worker_register (void)
4246{
Florin Coras47c40e22018-11-26 17:01:36 -08004247 if (!vcl_worker_alloc_and_init ())
4248 return VPPCOM_EEXIST;
4249
Florin Coras47c40e22018-11-26 17:01:36 -08004250 if (vcl_worker_register_with_vpp ())
4251 return VPPCOM_EEXIST;
4252
4253 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004254}
4255
Florin Coras369db832019-07-08 12:34:45 -07004256void
4257vppcom_worker_unregister (void)
4258{
4259 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4260 vcl_set_worker_index (~0);
4261}
4262
Pivo6017ff02020-05-04 17:57:33 +02004263void
4264vppcom_worker_index_set (int index)
4265{
4266 vcl_set_worker_index (index);
4267}
4268
Florin Corasdfe4cf42018-11-28 22:13:45 -08004269int
4270vppcom_worker_index (void)
4271{
4272 return vcl_get_worker_index ();
4273}
4274
Florin Corase0982e52019-01-25 13:19:56 -08004275int
4276vppcom_worker_mqs_epfd (void)
4277{
4278 vcl_worker_t *wrk = vcl_worker_get_current ();
4279 if (!vcm->cfg.use_mq_eventfd)
4280 return -1;
4281 return wrk->mqs_epfd;
4282}
4283
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004284int
4285vppcom_session_is_connectable_listener (uint32_t session_handle)
4286{
4287 vcl_session_t *session;
4288 vcl_worker_t *wrk = vcl_worker_get_current ();
4289 session = vcl_session_get_w_handle (wrk, session_handle);
4290 if (!session)
4291 return VPPCOM_EBADFD;
4292 return vcl_session_is_connectable_listener (wrk, session);
4293}
4294
4295int
4296vppcom_session_listener (uint32_t session_handle)
4297{
4298 vcl_worker_t *wrk = vcl_worker_get_current ();
4299 vcl_session_t *listen_session, *session;
4300 session = vcl_session_get_w_handle (wrk, session_handle);
4301 if (!session)
4302 return VPPCOM_EBADFD;
4303 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4304 return VPPCOM_EBADFD;
4305 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4306 if (!listen_session)
4307 return VPPCOM_EBADFD;
4308 return vcl_session_handle (listen_session);
4309}
4310
4311int
4312vppcom_session_n_accepted (uint32_t session_handle)
4313{
4314 vcl_worker_t *wrk = vcl_worker_get_current ();
4315 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4316 if (!session)
4317 return VPPCOM_EBADFD;
4318 return session->n_accepted_sessions;
4319}
4320
Florin Coras66ec4672020-06-15 07:59:40 -07004321const char *
4322vppcom_proto_str (vppcom_proto_t proto)
4323{
4324 char const *proto_str;
4325
4326 switch (proto)
4327 {
4328 case VPPCOM_PROTO_TCP:
4329 proto_str = "TCP";
4330 break;
4331 case VPPCOM_PROTO_UDP:
4332 proto_str = "UDP";
4333 break;
4334 case VPPCOM_PROTO_TLS:
4335 proto_str = "TLS";
4336 break;
4337 case VPPCOM_PROTO_QUIC:
4338 proto_str = "QUIC";
4339 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004340 case VPPCOM_PROTO_DTLS:
4341 proto_str = "DTLS";
4342 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004343 case VPPCOM_PROTO_SRTP:
4344 proto_str = "SRTP";
4345 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004346 default:
4347 proto_str = "UNKNOWN";
4348 break;
4349 }
4350 return proto_str;
4351}
4352
4353const char *
4354vppcom_retval_str (int retval)
4355{
4356 char const *st;
4357
4358 switch (retval)
4359 {
4360 case VPPCOM_OK:
4361 st = "VPPCOM_OK";
4362 break;
4363
4364 case VPPCOM_EAGAIN:
4365 st = "VPPCOM_EAGAIN";
4366 break;
4367
4368 case VPPCOM_EFAULT:
4369 st = "VPPCOM_EFAULT";
4370 break;
4371
4372 case VPPCOM_ENOMEM:
4373 st = "VPPCOM_ENOMEM";
4374 break;
4375
4376 case VPPCOM_EINVAL:
4377 st = "VPPCOM_EINVAL";
4378 break;
4379
4380 case VPPCOM_EBADFD:
4381 st = "VPPCOM_EBADFD";
4382 break;
4383
4384 case VPPCOM_EAFNOSUPPORT:
4385 st = "VPPCOM_EAFNOSUPPORT";
4386 break;
4387
4388 case VPPCOM_ECONNABORTED:
4389 st = "VPPCOM_ECONNABORTED";
4390 break;
4391
4392 case VPPCOM_ECONNRESET:
4393 st = "VPPCOM_ECONNRESET";
4394 break;
4395
4396 case VPPCOM_ENOTCONN:
4397 st = "VPPCOM_ENOTCONN";
4398 break;
4399
4400 case VPPCOM_ECONNREFUSED:
4401 st = "VPPCOM_ECONNREFUSED";
4402 break;
4403
4404 case VPPCOM_ETIMEDOUT:
4405 st = "VPPCOM_ETIMEDOUT";
4406 break;
4407
4408 default:
4409 st = "UNKNOWN_STATE";
4410 break;
4411 }
4412
4413 return st;
4414}
4415
Florin Corasa5a9efd2021-01-05 17:03:29 -08004416int
4417vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4418{
4419 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004420 return vcl_sapi_add_cert_key_pair (ckpair);
4421 else
4422 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004423}
4424
4425int
4426vppcom_del_cert_key_pair (uint32_t ckpair_index)
4427{
4428 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004429 return vcl_sapi_del_cert_key_pair (ckpair_index);
4430 else
4431 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004432}
4433
Dave Wallacee22aa742017-10-20 12:30:38 -04004434/*
4435 * fd.io coding-style-patch-verification: ON
4436 *
4437 * Local Variables:
4438 * eval: (c-set-style "gnu")
4439 * End:
4440 */