blob: 44543bb86ef8eb7970023e0e4a7f0c86dc525b27 [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
1260vcl_api_detach (vcl_worker_t * wrk)
1261{
1262 vcl_send_app_detach (wrk);
1263
1264 if (vcm->cfg.vpp_app_socket_api)
1265 return vcl_sapi_detach (wrk);
1266
1267 return vcl_bapi_disconnect_from_vpp ();
1268}
1269
Dave Wallace543852a2017-08-03 02:11:34 -04001270/*
1271 * VPPCOM Public API functions
1272 */
1273int
Florin Coras66ec4672020-06-15 07:59:40 -07001274vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001275{
Dave Wallace543852a2017-08-03 02:11:34 -04001276 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001277 int rv;
1278
Florin Coras47c40e22018-11-26 17:01:36 -08001279 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001280 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001281 VDBG (1, "already initialized");
1282 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001283 }
1284
Florin Coras47c40e22018-11-26 17:01:36 -08001285 vcm->is_init = 1;
1286 vppcom_cfg (&vcm->cfg);
1287 vcl_cfg = &vcm->cfg;
1288
1289 vcm->main_cpu = pthread_self ();
1290 vcm->main_pid = getpid ();
1291 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001292 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1293 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001294 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1295 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001296 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001297 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001298 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001299
1300 /* Allocate default worker */
1301 vcl_worker_alloc_and_init ();
1302
Florin Coras935ce752020-09-08 22:43:47 -07001303 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001304 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001305
1306 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001307 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001308
1309 return VPPCOM_OK;
1310}
1311
1312void
1313vppcom_app_destroy (void)
1314{
Florin Corasdc0ded72020-04-30 02:59:55 +00001315 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001316 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001317
Florin Coras940f78f2018-11-30 12:11:20 -08001318 if (!pool_elts (vcm->workers))
1319 return;
1320
Florin Coras0d427d82018-06-27 03:24:07 -07001321 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001322
Florin Corasdc0ded72020-04-30 02:59:55 +00001323 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001324
Florin Corasce815de2020-04-16 18:47:27 +00001325 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001326 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001327 if (current_wrk != wrk)
1328 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001329 }
Florin Corasce815de2020-04-16 18:47:27 +00001330 /* *INDENT-ON* */
1331
Florin Coras935ce752020-09-08 22:43:47 -07001332 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001333 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1334
Florin Coras0d427d82018-06-27 03:24:07 -07001335 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001336
1337 /*
1338 * Free the heap and fix vcm
1339 */
1340 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001341 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001342
1343 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001344 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001345}
1346
1347int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001348vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001349{
Florin Coras134a9962018-08-28 11:32:04 -07001350 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001351 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001352
Florin Coras134a9962018-08-28 11:32:04 -07001353 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001354
Florin Coras7e12d942018-06-27 14:32:43 -07001355 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001356 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001357 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001358 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001359
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001360 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001361 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001362
Florin Coras7e12d942018-06-27 14:32:43 -07001363 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1364 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001365
Florin Coras5e062572019-03-14 19:07:51 -07001366 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001367
Florin Coras134a9962018-08-28 11:32:04 -07001368 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001369}
1370
Florin Corasfe286f72021-06-04 10:07:55 -07001371static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001372vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001373{
Florin Corasfa3884f2021-06-22 20:04:46 -07001374 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001375
Florin Corasfa3884f2021-06-22 20:04:46 -07001376 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001377 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001378 wrk->ep_lt_current = s->session_index;
1379 s->vep.lt_next = s->session_index;
1380 s->vep.lt_prev = s->session_index;
1381 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001382 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001383
1384 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1385 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1386
1387 prev->vep.lt_next = s->session_index;
1388 s->vep.lt_prev = prev->session_index;
1389
1390 s->vep.lt_next = cur->session_index;
1391 cur->vep.lt_prev = s->session_index;
1392}
1393
1394static void
1395vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1396{
1397 vcl_session_t *prev, *next;
1398
1399 if (s->vep.lt_next == s->session_index)
1400 {
1401 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1402 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1403 return;
1404 }
1405
1406 prev = vcl_session_get (wrk, s->vep.lt_prev);
1407 next = vcl_session_get (wrk, s->vep.lt_next);
1408
1409 prev->vep.lt_next = next->session_index;
1410 next->vep.lt_prev = prev->session_index;
1411
1412 if (s->session_index == wrk->ep_lt_current)
1413 wrk->ep_lt_current = s->vep.lt_next;
1414
1415 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001416}
1417
Dave Wallace543852a2017-08-03 02:11:34 -04001418int
Florin Corasc127d5a2020-10-14 16:35:58 -07001419vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001420 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001421{
Florin Coras134a9962018-08-28 11:32:04 -07001422 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001423
Florin Coras6c3b2182020-10-19 18:36:48 -07001424 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001425
Florin Coras6c3b2182020-10-19 18:36:48 -07001426 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001427 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001428 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001429 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001430 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001431 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001432 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001433 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001434 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1435 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001436 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001437 }
Florin Corase4306b62020-10-28 12:51:10 -07001438 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001439 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001440
Florin Coras6c3b2182020-10-19 18:36:48 -07001441 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001442 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001443 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001444 if (rv < 0)
1445 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001446 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1447 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001448 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001449
Florin Coras9ace36d2019-10-28 13:14:17 -07001450 if (!do_disconnect)
1451 {
1452 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001453 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001454 goto cleanup;
1455 }
1456
Florin Coras6c3b2182020-10-19 18:36:48 -07001457 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001458 {
1459 rv = vppcom_session_unbind (sh);
1460 if (PREDICT_FALSE (rv < 0))
1461 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001462 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001463 vppcom_retval_str (rv));
1464 return rv;
1465 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001466 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001467 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001468 {
1469 rv = vppcom_session_disconnect (sh);
1470 if (PREDICT_FALSE (rv < 0))
1471 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001472 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001473 rv, vppcom_retval_str (rv));
1474 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001475 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001476 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001477 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001478 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001479 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001480 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001481 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001482
1483 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1484 goto free_session;
1485
1486 /* Disconnect/reset messages pending but vpp transport and session
1487 * cleanups already done. Free only after messages drained. */
1488 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001489 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001490
Florin Corasc127d5a2020-10-14 16:35:58 -07001491 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001492
Florin Coras9ace36d2019-10-28 13:14:17 -07001493 /* Session is removed only after vpp confirms the disconnect */
1494 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001495
Florin Corasf9240dc2019-01-15 08:03:17 -08001496cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001497 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001498free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001499 vcl_session_free (wrk, s);
1500 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001501
Dave Wallace543852a2017-08-03 02:11:34 -04001502 return rv;
1503}
1504
1505int
Florin Corasf9240dc2019-01-15 08:03:17 -08001506vppcom_session_close (uint32_t session_handle)
1507{
1508 vcl_worker_t *wrk = vcl_worker_get_current ();
1509 vcl_session_t *session;
1510
1511 session = vcl_session_get_w_handle (wrk, session_handle);
1512 if (!session)
1513 return VPPCOM_EBADFD;
1514 return vcl_session_cleanup (wrk, session, session_handle,
1515 1 /* do_disconnect */ );
1516}
1517
1518int
Florin Coras134a9962018-08-28 11:32:04 -07001519vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001520{
Florin Coras134a9962018-08-28 11:32:04 -07001521 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001522 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001523
1524 if (!ep || !ep->ip)
1525 return VPPCOM_EINVAL;
1526
Florin Coras134a9962018-08-28 11:32:04 -07001527 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001528 if (!session)
1529 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001530
Florin Coras6c3b2182020-10-19 18:36:48 -07001531 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001532 {
Florin Coras5e062572019-03-14 19:07:51 -07001533 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1534 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001535 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001536 }
1537
Florin Coras7e12d942018-06-27 14:32:43 -07001538 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001539 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001540 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1541 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001542 else
Dave Barach178cf492018-11-13 16:34:13 -05001543 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1544 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001545 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001546
Florin Coras60687192021-11-29 21:00:47 -08001547 VDBG (0,
1548 "session %u handle %u: binding to local %s address %U port %u, "
1549 "proto %s",
1550 session->session_index, session_handle,
1551 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1552 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001553 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1554 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001555 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001556 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001557
1558 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001559 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001560
Florin Coras070453d2018-08-24 17:04:27 -07001561 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001562}
1563
1564int
Florin Coras134a9962018-08-28 11:32:04 -07001565vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001566{
Florin Coras134a9962018-08-28 11:32:04 -07001567 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001568 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001569 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001570 int rv;
1571
Florin Coras134a9962018-08-28 11:32:04 -07001572 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001573 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001574 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001575
Dave Wallaceee45d412017-11-24 21:44:06 -05001576 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001577 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001578 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001579 VDBG (0, "session %u [0x%llx]: already in listen state!",
1580 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001581 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001582 }
1583
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001584 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001585
Florin Coras070453d2018-08-24 17:04:27 -07001586 /*
1587 * Send listen request to vpp and wait for reply
1588 */
Florin Coras458089b2019-08-21 16:20:44 -07001589 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001590 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001591 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001592 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001593
Florin Coras070453d2018-08-24 17:04:27 -07001594 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001595 {
Florin Coras134a9962018-08-28 11:32:04 -07001596 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001597 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1598 listen_sh, listen_session->vpp_handle, rv,
1599 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001600 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001601 }
1602
Florin Coras070453d2018-08-24 17:04:27 -07001603 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001604}
1605
Florin Coras134a9962018-08-28 11:32:04 -07001606static int
Florin Coras5e062572019-03-14 19:07:51 -07001607validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001608{
Florin Coras6c3b2182020-10-19 18:36:48 -07001609 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001610 {
Florin Coras5e062572019-03-14 19:07:51 -07001611 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1612 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001613 return VPPCOM_EBADFD;
1614 }
1615
Florin Corasc127d5a2020-10-14 16:35:58 -07001616 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001617 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001618 {
Florin Coras60687192021-11-29 21:00:47 -08001619 VDBG (0,
1620 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1621 " (%s)",
1622 ls->vpp_handle, ls->session_state,
1623 vcl_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001624 return VPPCOM_EBADFD;
1625 }
1626 return VPPCOM_OK;
1627}
1628
1629int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001630vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1631{
1632 if (!strcmp (proto_str, "TCP"))
1633 *proto = VPPCOM_PROTO_TCP;
1634 else if (!strcmp (proto_str, "tcp"))
1635 *proto = VPPCOM_PROTO_TCP;
1636 else if (!strcmp (proto_str, "UDP"))
1637 *proto = VPPCOM_PROTO_UDP;
1638 else if (!strcmp (proto_str, "udp"))
1639 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001640 else if (!strcmp (proto_str, "TLS"))
1641 *proto = VPPCOM_PROTO_TLS;
1642 else if (!strcmp (proto_str, "tls"))
1643 *proto = VPPCOM_PROTO_TLS;
1644 else if (!strcmp (proto_str, "QUIC"))
1645 *proto = VPPCOM_PROTO_QUIC;
1646 else if (!strcmp (proto_str, "quic"))
1647 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001648 else if (!strcmp (proto_str, "DTLS"))
1649 *proto = VPPCOM_PROTO_DTLS;
1650 else if (!strcmp (proto_str, "dtls"))
1651 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001652 else if (!strcmp (proto_str, "SRTP"))
1653 *proto = VPPCOM_PROTO_SRTP;
1654 else if (!strcmp (proto_str, "srtp"))
1655 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001656 else
1657 return 1;
1658 return 0;
1659}
1660
1661int
Florin Coras134a9962018-08-28 11:32:04 -07001662vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001663 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001664{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001665 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001666 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001667 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001668 vcl_session_t *listen_session = 0;
1669 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001670 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001671 u8 is_nonblocking;
1672 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001673
Florin Coras5398dfb2021-01-25 20:31:27 -08001674again:
1675
Florin Coras134a9962018-08-28 11:32:04 -07001676 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001677 if (!listen_session)
1678 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001679
Florin Coras134a9962018-08-28 11:32:04 -07001680 listen_session_index = listen_session->session_index;
1681 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001682 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001683
Florin Coras54693d22018-07-17 10:46:29 -07001684 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001685 {
Florin Coras54693d22018-07-17 10:46:29 -07001686 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001687 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001688 accepted_msg = evt->accepted_msg;
1689 goto handle;
1690 }
1691
Florin Corasac422d62020-10-19 20:51:36 -07001692 is_nonblocking = vcl_session_has_attr (listen_session,
1693 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001694 while (1)
1695 {
Carl Smith592a9092019-11-12 14:57:37 +13001696 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1697 return VPPCOM_EAGAIN;
1698
Florin Coras5398dfb2021-01-25 20:31:27 -08001699 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1700 vcl_worker_flush_mq_events (wrk);
1701 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001702 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001703
Florin Coras54693d22018-07-17 10:46:29 -07001704handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001705
Florin Coras00cca802019-06-06 09:38:44 -07001706 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1707 listen_session_index);
1708 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1709 return VPPCOM_ECONNABORTED;
1710
Florin Coras134a9962018-08-28 11:32:04 -07001711 listen_session = vcl_session_get (wrk, listen_session_index);
1712 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001713
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001714 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001715 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001716
Florin Coras5e062572019-03-14 19:07:51 -07001717 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1718 " flags %d, is_nonblocking %u", listen_session->session_index,
1719 listen_session->vpp_handle, client_session_index,
1720 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001721 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001722
Dave Wallace048b1d62018-01-03 22:24:41 -05001723 if (ep)
1724 {
Florin Coras7e12d942018-06-27 14:32:43 -07001725 ep->is_ip4 = client_session->transport.is_ip4;
1726 ep->port = client_session->transport.rmt_port;
1727 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001728 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1729 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001730 else
Dave Barach178cf492018-11-13 16:34:13 -05001731 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1732 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001733 }
Dave Wallace60caa062017-11-10 17:07:13 -05001734
Florin Coras60687192021-11-29 21:00:47 -08001735 VDBG (0,
1736 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1737 "local: %U:%u",
1738 listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001739 client_session_index, client_session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001740 vcl_format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001741 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001742 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001743 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001744 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001745 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001746 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1747 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001748
Florin Coras3c7d4f92018-12-14 11:28:43 -08001749 /*
1750 * Session might have been closed already
1751 */
1752 if (accept_flags)
1753 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001754 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001755 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001756 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001757 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001758 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001759 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001760}
1761
1762int
Florin Coras134a9962018-08-28 11:32:04 -07001763vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001764{
Florin Coras134a9962018-08-28 11:32:04 -07001765 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001766 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001767 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001768 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001769
Florin Coras134a9962018-08-28 11:32:04 -07001770 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001771 if (!session)
1772 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001773 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001774
Florin Coras6c3b2182020-10-19 18:36:48 -07001775 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001776 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001777 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001778 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001779 }
1780
Florin Corasc127d5a2020-10-14 16:35:58 -07001781 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001782 {
Florin Coras60687192021-11-29 21:00:47 -08001783 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001784 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1785 " state (%s)",
1786 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001787 vcl_format_ip46_address, &session->transport.rmt_ip,
1788 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001789 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001790 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001791 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001792 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001793 }
1794
Florin Coras0a1e1832020-03-29 18:54:04 +00001795 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001796 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001797 {
1798 if (session->session_type != VPPCOM_PROTO_UDP)
1799 return VPPCOM_EINVAL;
1800 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001801 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001802 }
1803
Florin Coras7e12d942018-06-27 14:32:43 -07001804 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001805 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001806 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001807 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001808 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001809
Florin Coras5ffb9642021-12-03 17:24:13 -08001810 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1811 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001812 &session->transport.rmt_ip,
1813 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001814 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001815 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001816
Florin Coras458089b2019-08-21 16:20:44 -07001817 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001818
Florin Corasac422d62020-10-19 20:51:36 -07001819 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001820 {
fanyfa49d59d2020-10-13 17:07:16 +08001821 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001822 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001823 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001824 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001825 return VPPCOM_EINPROGRESS;
1826 }
Florin Coras57c88932019-08-29 12:03:17 -07001827
1828 /*
1829 * Wait for reply from vpp if blocking
1830 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001831 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001832 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001833
Florin Coras134a9962018-08-28 11:32:04 -07001834 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001835 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1836 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001837
Dave Wallace4878cbe2017-11-21 03:45:09 -05001838 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001839}
1840
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001841int
1842vppcom_session_stream_connect (uint32_t session_handle,
1843 uint32_t parent_session_handle)
1844{
1845 vcl_worker_t *wrk = vcl_worker_get_current ();
1846 vcl_session_t *session, *parent_session;
1847 u32 session_index, parent_session_index;
1848 int rv;
1849
1850 session = vcl_session_get_w_handle (wrk, session_handle);
1851 if (!session)
1852 return VPPCOM_EBADFD;
1853 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1854 if (!parent_session)
1855 return VPPCOM_EBADFD;
1856
1857 session_index = session->session_index;
1858 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001859 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001860 {
1861 VDBG (0, "ERROR: cannot connect epoll session %u!",
1862 session->session_index);
1863 return VPPCOM_EBADFD;
1864 }
1865
Florin Corasc127d5a2020-10-14 16:35:58 -07001866 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001867 {
Florin Coras60687192021-11-29 21:00:47 -08001868 VDBG (0,
1869 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001870 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001871 session_handle, session->vpp_handle, parent_session_handle,
1872 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001873 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001874 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001875 return VPPCOM_OK;
1876 }
1877
1878 /* Connect to quic session specifics */
1879 session->transport.is_ip4 = parent_session->transport.is_ip4;
1880 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1881 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001882 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001883
1884 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1885 session_handle, parent_session_handle, parent_session->vpp_handle);
1886
1887 /*
1888 * Send connect request and wait for reply from vpp
1889 */
Florin Coras458089b2019-08-21 16:20:44 -07001890 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001891 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001892 vcm->cfg.session_timeout);
1893
1894 session->listener_index = parent_session_index;
1895 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001896 if (parent_session)
1897 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001898
1899 session = vcl_session_get (wrk, session_index);
1900 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1901 session->vpp_handle, rv ? "failed" : "succeeded");
1902
1903 return rv;
1904}
1905
Steven58f464e2017-10-25 12:33:12 -07001906static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001907vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001908 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001909{
Florin Coras134a9962018-08-28 11:32:04 -07001910 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001911 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001912 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001913 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001914 session_event_t *e;
1915 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001916 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001917
Florin Coras070453d2018-08-24 17:04:27 -07001918 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001919 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001920
Florin Coras134a9962018-08-28 11:32:04 -07001921 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001922 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001923 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001924
Florin Coras6d0106e2019-01-29 20:11:58 -08001925 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001926 {
Florin Coras5e062572019-03-14 19:07:51 -07001927 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001928 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001929 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08001930 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001931 }
1932
liuyacan55c952e2021-06-13 14:54:55 +08001933 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
1934 {
1935 /* Vpp would ack the incoming data and enqueue it for reading.
1936 * So even if SHUT_RD is set, we can still read() the data if
1937 * the session is ready.
1938 */
1939 if (!vcl_session_read_ready (s))
1940 {
1941 return 0;
1942 }
1943 }
1944
Florin Corasac422d62020-10-19 20:51:36 -07001945 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001946 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001947 mq = wrk->app_event_queue;
1948 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001949 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001950
Sirshak Das28aa5392019-02-05 01:33:33 -06001951 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001952 {
Florin Coras54693d22018-07-17 10:46:29 -07001953 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001954 {
Yu Pingb2955352019-12-04 06:49:04 +08001955 if (vcl_session_is_closing (s))
1956 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001957 if (is_ct)
1958 svm_fifo_unset_event (s->rx_fifo);
1959 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001960 return VPPCOM_EWOULDBLOCK;
1961 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001962 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001963 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001964 if (vcl_session_is_closing (s))
1965 return vcl_session_closing_error (s);
1966
Florin Corasd6894562020-10-28 00:37:15 -07001967 if (is_ct)
1968 svm_fifo_unset_event (s->rx_fifo);
1969 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001970
Florin Coras5398dfb2021-01-25 20:31:27 -08001971 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1972 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001973 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001974 }
Florin Coras54693d22018-07-17 10:46:29 -07001975
Florin Coras4a2c7942020-09-10 12:27:14 -07001976read_again:
1977
Florin Coras460dce62018-07-27 05:45:06 -07001978 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001979 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001980 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001981 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1982
1983 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07001984
1985 if (peek)
1986 return rv;
1987
Florin Coras4a2c7942020-09-10 12:27:14 -07001988 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001989
Sirshak Das28aa5392019-02-05 01:33:33 -06001990 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001991 {
Florin Corasd6894562020-10-28 00:37:15 -07001992 if (is_ct)
1993 svm_fifo_unset_event (s->rx_fifo);
1994 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001995 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001996 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001997 {
Florin Corasc34118b2020-08-12 18:58:25 -07001998 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1999 e->event_type = SESSION_IO_EVT_RX;
2000 e->session_index = s->session_index;
2001 }
2002 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002003 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002004 {
2005 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002006 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002007 buf += rv;
2008 n -= rv;
2009 goto read_again;
2010 }
Florin Coras41c9e042018-09-11 00:10:41 -07002011
Florin Coras17ec5772020-08-12 20:46:29 -07002012 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002013 {
Florin Coras17ec5772020-08-12 20:46:29 -07002014 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002015 app_send_io_evt_to_vpp (s->vpp_evt_q,
2016 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002017 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002018 }
2019
Florin Coras5e062572019-03-14 19:07:51 -07002020 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2021 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002022
Florin Coras54693d22018-07-17 10:46:29 -07002023 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002024}
2025
Steven58f464e2017-10-25 12:33:12 -07002026int
Florin Coras134a9962018-08-28 11:32:04 -07002027vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002028{
Florin Coras134a9962018-08-28 11:32:04 -07002029 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002030}
2031
2032static int
Florin Coras134a9962018-08-28 11:32:04 -07002033vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002034{
Florin Coras134a9962018-08-28 11:32:04 -07002035 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002036}
2037
Florin Coras2cba8532018-09-11 16:33:36 -07002038int
2039vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002040 vppcom_data_segment_t * ds, uint32_t n_segments,
2041 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002042{
2043 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002044 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002045 vcl_session_t *s = 0;
2046 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002047 svm_msg_q_t *mq;
2048 u8 is_ct;
2049
2050 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002051 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002052 return VPPCOM_EBADFD;
2053
Florin Coras6d0106e2019-01-29 20:11:58 -08002054 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2055 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002056
Florin Corasac422d62020-10-19 20:51:36 -07002057 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002058 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002059 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002060 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002061 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002062
Sirshak Das28aa5392019-02-05 01:33:33 -06002063 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002064 {
2065 if (is_nonblocking)
2066 {
Florin Coras62877022020-10-28 21:22:04 -07002067 if (is_ct)
2068 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002069 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002070 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002071 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002072 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002073 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002074 if (vcl_session_is_closing (s))
2075 return vcl_session_closing_error (s);
2076
Florin Coras62877022020-10-28 21:22:04 -07002077 if (is_ct)
2078 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002079 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002080
Florin Coras5398dfb2021-01-25 20:31:27 -08002081 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2082 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002083 }
2084 }
2085
Florin Corasd1cc38d2020-10-11 11:05:04 -07002086 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2087 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2088 if (n_read < 0)
2089 return VPPCOM_EAGAIN;
2090
2091 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2092 {
Florin Coras62877022020-10-28 21:22:04 -07002093 if (is_ct)
2094 svm_fifo_unset_event (s->rx_fifo);
2095 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002096 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002097 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002098 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002099 {
2100 session_event_t *e;
2101 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2102 e->event_type = SESSION_IO_EVT_RX;
2103 e->session_index = s->session_index;
2104 }
2105 }
2106
2107 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002108 return n_read;
2109}
2110
2111void
Florin Corasd68faf82020-09-25 15:18:13 -07002112vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002113{
2114 vcl_worker_t *wrk = vcl_worker_get_current ();
2115 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002116 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002117
2118 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002119 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002120 return;
2121
Florin Coras62877022020-10-28 21:22:04 -07002122 is_ct = vcl_session_is_ct (s);
2123 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002124
Florin Coras8cc93212021-09-10 11:37:12 -07002125 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002126 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002127}
2128
Florin Coras7a2abce2020-04-05 19:25:44 +00002129always_inline u8
2130vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002131{
Florin Coras7a2abce2020-04-05 19:25:44 +00002132 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2133 if (is_dgram)
2134 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2135 else
2136 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002137}
2138
2139always_inline int
2140vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2141 size_t n, u8 is_flush, u8 is_dgram)
2142{
Florin Coras6d0106e2019-01-29 20:11:58 -08002143 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002144 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002145 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002146 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002147 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002148
Florin Coras0b0d28e2021-06-04 17:31:53 -07002149 /* Accept zero length writes but just return */
2150 if (PREDICT_FALSE (!n))
2151 return VPPCOM_OK;
2152
2153 if (PREDICT_FALSE (!buf))
2154 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002155
Florin Coras6c3b2182020-10-19 18:36:48 -07002156 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002157 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002158 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2159 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002160 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002161 }
2162
liuyacan55c952e2021-06-13 14:54:55 +08002163 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002164 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002165 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2166 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002167 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002168 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002169 }
2170
liuyacan55c952e2021-06-13 14:54:55 +08002171 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2172 {
2173 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2174 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002175 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002176 return VPPCOM_EPIPE;
2177 }
2178
Florin Coras0e88e852018-09-17 22:09:02 -07002179 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002180 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002181 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002182
Florin Coras653e43f2019-03-04 10:56:23 -08002183 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002184 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002185 {
Florin Coras54693d22018-07-17 10:46:29 -07002186 if (is_nonblocking)
2187 {
Florin Coras070453d2018-08-24 17:04:27 -07002188 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002189 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002190 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002191 {
Florin Coras2d379d82019-06-28 12:45:12 -07002192 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002193 if (vcl_session_is_closing (s))
2194 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002195
Florin Coras5398dfb2021-01-25 20:31:27 -08002196 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2197 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002198 }
Dave Wallace543852a2017-08-03 02:11:34 -04002199 }
Dave Wallace543852a2017-08-03 02:11:34 -04002200
Florin Coras653e43f2019-03-04 10:56:23 -08002201 et = SESSION_IO_EVT_TX;
2202 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002203 et = SESSION_IO_EVT_TX_FLUSH;
2204
Florin Coras7a2abce2020-04-05 19:25:44 +00002205 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002206 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002207 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002208 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002209 else
2210 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002211 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002212
Florin Coras14ed6df2019-03-06 21:13:42 -08002213 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002214 app_send_io_evt_to_vpp (
2215 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002216
Florin Coras56230092020-09-02 20:52:58 -07002217 /* The underlying fifo segment can run out of memory */
2218 if (PREDICT_FALSE (n_write < 0))
2219 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002220
Florin Coras6d0106e2019-01-29 20:11:58 -08002221 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2222 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002223
Florin Coras54693d22018-07-17 10:46:29 -07002224 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002225}
2226
Florin Coras42ceddb2018-12-12 10:56:01 -08002227int
2228vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2229{
Florin Coras7a2abce2020-04-05 19:25:44 +00002230 vcl_worker_t *wrk = vcl_worker_get_current ();
2231 vcl_session_t *s;
2232
2233 s = vcl_session_get_w_handle (wrk, session_handle);
2234 if (PREDICT_FALSE (!s))
2235 return VPPCOM_EBADFD;
2236
2237 return vppcom_session_write_inline (wrk, s, buf, n,
2238 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002239}
2240
Florin Corasb0f662f2018-12-27 14:51:46 -08002241int
2242vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2243{
Florin Coras7a2abce2020-04-05 19:25:44 +00002244 vcl_worker_t *wrk = vcl_worker_get_current ();
2245 vcl_session_t *s;
2246
2247 s = vcl_session_get_w_handle (wrk, session_handle);
2248 if (PREDICT_FALSE (!s))
2249 return VPPCOM_EBADFD;
2250
2251 return vppcom_session_write_inline (wrk, s, buf, n,
2252 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002253}
2254
Florin Corasc0737e92019-03-04 14:19:39 -08002255#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002256if (PREDICT_FALSE (!_s->rx_fifo)) \
2257 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002258if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2259 { \
2260 if (!vcl_session_is_ct (_s)) \
2261 { \
2262 svm_fifo_unset_event (_s->rx_fifo); \
2263 if (svm_fifo_is_empty (_s->rx_fifo)) \
2264 break; \
2265 } \
2266 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2267 { \
Florin Coras2f630182020-08-13 00:17:51 -07002268 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002269 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2270 break; \
2271 } \
2272 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002273
Florin Coras86f04502018-09-12 16:08:01 -07002274static void
2275vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2276 unsigned long n_bits, unsigned long *read_map,
2277 unsigned long *write_map,
2278 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002279{
2280 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002281 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002282 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002283 u32 sid;
2284
2285 switch (e->event_type)
2286 {
Florin Corasc0737e92019-03-04 14:19:39 -08002287 case SESSION_IO_EVT_RX:
2288 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002289 s = vcl_session_get (wrk, sid);
2290 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002291 break;
Florin Corasb242d312020-10-26 15:35:40 -07002292 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002293 if (sid < n_bits && read_map)
2294 {
David Johnsond9818dd2018-12-14 14:53:41 -05002295 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002296 *bits_set += 1;
2297 }
2298 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002299 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002300 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002301 s = vcl_session_get (wrk, sid);
2302 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002303 break;
2304 if (sid < n_bits && write_map)
2305 {
David Johnsond9818dd2018-12-14 14:53:41 -05002306 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002307 *bits_set += 1;
2308 }
2309 break;
Florin Coras86f04502018-09-12 16:08:01 -07002310 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002311 if (!e->postponed)
2312 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2313 else
2314 s = vcl_session_get (wrk, e->session_index);
2315 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002316 break;
Florin Corasb242d312020-10-26 15:35:40 -07002317 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002318 if (sid < n_bits && read_map)
2319 {
David Johnsond9818dd2018-12-14 14:53:41 -05002320 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002321 *bits_set += 1;
2322 }
2323 break;
2324 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002325 if (!e->postponed)
2326 {
2327 connected_msg = (session_connected_msg_t *) e->data;
2328 sid = vcl_session_connected_handler (wrk, connected_msg);
2329 }
2330 else
2331 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002332 if (sid == VCL_INVALID_SESSION_INDEX)
2333 break;
2334 if (sid < n_bits && write_map)
2335 {
2336 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2337 *bits_set += 1;
2338 }
Florin Coras86f04502018-09-12 16:08:01 -07002339 break;
2340 case SESSION_CTRL_EVT_DISCONNECTED:
2341 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002342 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2343 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002344 break;
Florin Corasb242d312020-10-26 15:35:40 -07002345 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002346 if (sid < n_bits && except_map)
2347 {
David Johnsond9818dd2018-12-14 14:53:41 -05002348 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002349 *bits_set += 1;
2350 }
2351 break;
2352 case SESSION_CTRL_EVT_RESET:
2353 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2354 if (sid < n_bits && except_map)
2355 {
David Johnsond9818dd2018-12-14 14:53:41 -05002356 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002357 *bits_set += 1;
2358 }
2359 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002360 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2361 vcl_session_unlisten_reply_handler (wrk, e->data);
2362 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002363 case SESSION_CTRL_EVT_MIGRATED:
2364 vcl_session_migrated_handler (wrk, e->data);
2365 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002366 case SESSION_CTRL_EVT_CLEANUP:
2367 vcl_session_cleanup_handler (wrk, e->data);
2368 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002369 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2370 vcl_session_worker_update_reply_handler (wrk, e->data);
2371 break;
2372 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2373 vcl_session_req_worker_update_handler (wrk, e->data);
2374 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002375 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2376 vcl_session_app_add_segment_handler (wrk, e->data);
2377 break;
2378 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2379 vcl_session_app_del_segment_handler (wrk, e->data);
2380 break;
hanlina3a48962020-07-13 11:09:15 +08002381 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002382 vcl_worker_rpc_handler (wrk, e->data);
2383 break;
Florin Coras86f04502018-09-12 16:08:01 -07002384 default:
2385 clib_warning ("unhandled: %u", e->event_type);
2386 break;
2387 }
2388}
2389
2390static int
2391vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2392 unsigned long n_bits, unsigned long *read_map,
2393 unsigned long *write_map, unsigned long *except_map,
2394 double time_to_wait, u32 * bits_set)
2395{
Florin Coras99368312018-08-02 10:45:44 -07002396 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002397 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002398 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002399
Florin Coras54693d22018-07-17 10:46:29 -07002400 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002401 {
Florin Coras54693d22018-07-17 10:46:29 -07002402 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002403 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002404
Florin Coras54693d22018-07-17 10:46:29 -07002405 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002406 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002407 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002408 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002409 else
2410 {
2411 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002412 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002413 }
2414 }
Florin Corase003a1b2019-06-05 10:47:16 -07002415 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002416
Florin Coras134a9962018-08-28 11:32:04 -07002417 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002418 {
Florin Coras134a9962018-08-28 11:32:04 -07002419 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002420 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002421 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2422 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002423 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002424 }
Florin Coras134a9962018-08-28 11:32:04 -07002425 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002426 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002427 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002428}
2429
Florin Coras99368312018-08-02 10:45:44 -07002430static int
Florin Coras294afe22019-01-07 17:49:17 -08002431vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2432 vcl_si_set * read_map, vcl_si_set * write_map,
2433 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002434 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002435{
Florin Coras14ed6df2019-03-06 21:13:42 -08002436 double wait = 0, start = 0;
2437
2438 if (!*bits_set)
2439 {
2440 wait = time_to_wait;
2441 start = clib_time_now (&wrk->clib_time);
2442 }
2443
2444 do
2445 {
2446 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2447 write_map, except_map, wait, bits_set);
2448 if (*bits_set)
2449 return *bits_set;
2450 if (wait == -1)
2451 continue;
2452
2453 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2454 }
2455 while (wait > 0);
2456
2457 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002458}
2459
2460static int
Florin Coras294afe22019-01-07 17:49:17 -08002461vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2462 vcl_si_set * read_map, vcl_si_set * write_map,
2463 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002464 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002465{
2466 vcl_mq_evt_conn_t *mqc;
2467 int __clib_unused n_read;
2468 int n_mq_evts, i;
2469 u64 buf;
2470
Florin Coras134a9962018-08-28 11:32:04 -07002471 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2472 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2473 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002474 for (i = 0; i < n_mq_evts; i++)
2475 {
Florin Coras134a9962018-08-28 11:32:04 -07002476 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002477 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002478 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002479 except_map, 0, bits_set);
2480 }
2481
2482 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2483}
2484
Dave Wallace543852a2017-08-03 02:11:34 -04002485int
Florin Coras294afe22019-01-07 17:49:17 -08002486vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2487 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002488{
Florin Coras54693d22018-07-17 10:46:29 -07002489 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002490 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002491 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002492 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002493
Dave Wallace7876d392017-10-19 03:53:57 -04002494 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002495 {
Florin Coras134a9962018-08-28 11:32:04 -07002496 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002497 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002498 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2499 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002500 }
Dave Wallace7876d392017-10-19 03:53:57 -04002501 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002502 {
Florin Coras134a9962018-08-28 11:32:04 -07002503 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002504 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002505 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2506 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002507 }
Dave Wallace7876d392017-10-19 03:53:57 -04002508 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002509 {
Florin Coras134a9962018-08-28 11:32:04 -07002510 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002511 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002512 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2513 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002514 }
2515
Florin Coras54693d22018-07-17 10:46:29 -07002516 if (!n_bits)
2517 return 0;
2518
2519 if (!write_map)
2520 goto check_rd;
2521
Florin Coras096a1d52021-01-27 15:33:51 -08002522 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2523 {
2524 if (!(s = vcl_session_get (wrk, sid)))
2525 {
2526 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2527 bits_set++;
2528 continue;
2529 }
Florin Coras54693d22018-07-17 10:46:29 -07002530
Florin Coras096a1d52021-01-27 15:33:51 -08002531 if (vcl_session_write_ready (s))
2532 {
2533 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2534 bits_set++;
2535 }
2536 else
2537 {
2538 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2539 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2540 }
2541 }
Florin Coras54693d22018-07-17 10:46:29 -07002542
2543check_rd:
2544 if (!read_map)
2545 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002546
Florin Coras096a1d52021-01-27 15:33:51 -08002547 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2548 {
2549 if (!(s = vcl_session_get (wrk, sid)))
2550 {
2551 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2552 bits_set++;
2553 continue;
2554 }
Florin Coras54693d22018-07-17 10:46:29 -07002555
Florin Coras096a1d52021-01-27 15:33:51 -08002556 if (vcl_session_read_ready (s))
2557 {
2558 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2559 bits_set++;
2560 }
2561 }
Florin Coras54693d22018-07-17 10:46:29 -07002562
2563check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002564
Florin Coras86f04502018-09-12 16:08:01 -07002565 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2566 {
2567 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2568 read_map, write_map, except_map, &bits_set);
2569 }
2570 vec_reset_length (wrk->unhandled_evts_vector);
2571
Florin Coras99368312018-08-02 10:45:44 -07002572 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002573 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002574 time_to_wait, &bits_set);
2575 else
Florin Coras134a9962018-08-28 11:32:04 -07002576 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002577 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002578
Dave Wallace543852a2017-08-03 02:11:34 -04002579 return (bits_set);
2580}
2581
Dave Wallacef7f809c2017-10-03 01:48:42 -04002582static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002583vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002584{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002585 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002586 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002587 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002588
Florin Corasc0737e92019-03-04 14:19:39 -08002589 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002590 return;
2591
Florin Coras6c3b2182020-10-19 18:36:48 -07002592 s = vcl_session_get_w_handle (wrk, vep_handle);
2593 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002594 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002595 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002596 goto done;
2597 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002598 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002599 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002600 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002601 goto done;
2602 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002603 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002604 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002605 "{\n"
2606 " is_vep = %u\n"
2607 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002608 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002609 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2610 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002611
Florin Coras7e5e62b2019-07-30 14:08:23 -07002612 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002613 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002614 s = vcl_session_get_w_handle (wrk, sh);
2615 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002617 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002618 goto done;
2619 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002620 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002621 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002622 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002623 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002624 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002625 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002626 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002627 goto done;
2628 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002629 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002630 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2631 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002632 sh, s->vep.vep_sh, vep_handle);
2633 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002634 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002635 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002636 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002637 " next_sh = 0x%x (%u)\n"
2638 " prev_sh = 0x%x (%u)\n"
2639 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002640 " ev.events = 0x%x\n"
2641 " ev.data.u64 = 0x%llx\n"
2642 " et_mask = 0x%x\n"
2643 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002644 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002645 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2646 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002647 }
2648 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002649
2650done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002651 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002652}
2653
2654int
2655vppcom_epoll_create (void)
2656{
Florin Coras134a9962018-08-28 11:32:04 -07002657 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002658 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002659
Florin Coras134a9962018-08-28 11:32:04 -07002660 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002661
Florin Coras6c3b2182020-10-19 18:36:48 -07002662 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002663 vep_session->vep.vep_sh = ~0;
2664 vep_session->vep.next_sh = ~0;
2665 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002666 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002667
Florin Corasa7a1a222018-12-30 17:11:31 -08002668 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2669 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002670
Florin Corasab2f6db2018-08-31 14:31:41 -07002671 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002672}
2673
2674int
Florin Coras134a9962018-08-28 11:32:04 -07002675vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002676 struct epoll_event *event)
2677{
Florin Coras134a9962018-08-28 11:32:04 -07002678 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002679 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002680 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002681 vcl_session_t *s;
2682 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002683
Florin Coras134a9962018-08-28 11:32:04 -07002684 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002685 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002686 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687 return VPPCOM_EINVAL;
2688 }
2689
Florin Coras134a9962018-08-28 11:32:04 -07002690 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002691 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002692 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002693 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002694 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002695 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002696 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002698 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002699 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002700 }
2701
Florin Coras134a9962018-08-28 11:32:04 -07002702 ASSERT (vep_session->vep.vep_sh == ~0);
2703 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002704
Florin Coras17ec5772020-08-12 20:46:29 -07002705 s = vcl_session_get_w_handle (wrk, session_handle);
2706 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002707 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002708 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002709 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002710 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002711 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002712 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002713 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002714 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 }
2716
2717 switch (op)
2718 {
2719 case EPOLL_CTL_ADD:
2720 if (PREDICT_FALSE (!event))
2721 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002722 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002723 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002724 }
Florin Coras2645f682021-06-04 15:59:41 -07002725 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2726 {
2727 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2728 rv = VPPCOM_EEXIST;
2729 goto done;
2730 }
Florin Coras134a9962018-08-28 11:32:04 -07002731 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002732 {
Florin Coras7e12d942018-06-27 14:32:43 -07002733 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002734 next_session = vcl_session_get_w_handle (wrk,
2735 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002736 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002737 {
Florin Coras5e062572019-03-14 19:07:51 -07002738 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002739 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002740 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002741 }
Florin Coras134a9962018-08-28 11:32:04 -07002742 ASSERT (next_session->vep.prev_sh == vep_handle);
2743 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002744 }
Florin Coras17ec5772020-08-12 20:46:29 -07002745 s->vep.next_sh = vep_session->vep.next_sh;
2746 s->vep.prev_sh = vep_handle;
2747 s->vep.vep_sh = vep_handle;
2748 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002749 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002750 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002751 s->flags &= ~VCL_SESSION_F_IS_VEP;
2752 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002753 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002754
Florin Coras17ec5772020-08-12 20:46:29 -07002755 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2756 if (txf && (event->events & EPOLLOUT))
2757 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002758
Florin Coras6e3c1f82020-01-15 01:30:46 +00002759 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002760 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002761 {
2762 session_event_t e = { 0 };
2763 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002764 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002765 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002766 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002767 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002768 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002769 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002770 {
2771 session_event_t e = { 0 };
2772 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002773 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002774 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002775 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2776 add_evt = 1;
2777 }
2778 if (!add_evt && vcl_session_is_closing (s))
2779 {
2780 session_event_t e = { 0 };
2781 if (s->session_state == VCL_STATE_VPP_CLOSING)
2782 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2783 else
2784 e.event_type = SESSION_CTRL_EVT_RESET;
2785 e.session_index = s->session_index;
2786 e.postponed = 1;
2787 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002788 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002789 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2790 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002791 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002792 break;
2793
2794 case EPOLL_CTL_MOD:
2795 if (PREDICT_FALSE (!event))
2796 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002797 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002798 rv = VPPCOM_EINVAL;
2799 goto done;
2800 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002801 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802 {
Florin Coras5e062572019-03-14 19:07:51 -07002803 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002804 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002805 goto done;
2806 }
Florin Coras17ec5772020-08-12 20:46:29 -07002807 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002808 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002809 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002810 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002811 rv = VPPCOM_EINVAL;
2812 goto done;
2813 }
hanlin475c9d72019-12-26 11:44:28 +08002814
Florin Coras2645f682021-06-04 15:59:41 -07002815 /* Generate EPOLLOUT if session write ready nd event was not on */
2816 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2817 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002818 {
2819 session_event_t e = { 0 };
2820 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002821 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002822 vec_add1 (wrk->unhandled_evts_vector, e);
2823 }
Florin Coras2645f682021-06-04 15:59:41 -07002824 /* Generate EPOLLIN if session read ready and event was not on */
2825 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2826 (vcl_session_read_ready (s) > 0))
2827 {
2828 session_event_t e = { 0 };
2829 e.event_type = SESSION_IO_EVT_RX;
2830 e.session_index = s->session_index;
2831 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002832 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002833 }
Florin Coras17ec5772020-08-12 20:46:29 -07002834 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2835 s->vep.ev = *event;
2836 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002837 if (txf)
2838 {
2839 if (event->events & EPOLLOUT)
2840 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2841 else
2842 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2843 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002844 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2845 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002846 break;
2847
2848 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002849 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002850 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002851 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002852 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002853 goto done;
2854 }
Florin Coras17ec5772020-08-12 20:46:29 -07002855 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002856 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002857 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002858 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002859 rv = VPPCOM_EINVAL;
2860 goto done;
2861 }
2862
Florin Coras17ec5772020-08-12 20:46:29 -07002863 if (s->vep.prev_sh == vep_handle)
2864 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002865 else
2866 {
Florin Coras7e12d942018-06-27 14:32:43 -07002867 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002868 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002869 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002870 {
Florin Coras5e062572019-03-14 19:07:51 -07002871 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002872 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002873 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002874 }
Florin Coras134a9962018-08-28 11:32:04 -07002875 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002876 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002877 }
Florin Coras17ec5772020-08-12 20:46:29 -07002878 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002879 {
Florin Coras7e12d942018-06-27 14:32:43 -07002880 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002881 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002882 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002883 {
Florin Coras5e062572019-03-14 19:07:51 -07002884 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002885 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002886 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002887 }
Florin Coras134a9962018-08-28 11:32:04 -07002888 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002889 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002890 }
2891
Florin Corasfa3884f2021-06-22 20:04:46 -07002892 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2893 vcl_epoll_lt_del (wrk, s);
2894
Florin Coras17ec5772020-08-12 20:46:29 -07002895 memset (&s->vep, 0, sizeof (s->vep));
2896 s->vep.next_sh = ~0;
2897 s->vep.prev_sh = ~0;
2898 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002899 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002900 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002901
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002902 if (vcl_session_is_open (s))
2903 {
2904 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2905 if (txf)
2906 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2907 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002908
Florin Coras5e062572019-03-14 19:07:51 -07002909 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002910 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002911 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002912 break;
2913
2914 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002915 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002916 rv = VPPCOM_EINVAL;
2917 }
2918
Florin Coras134a9962018-08-28 11:32:04 -07002919 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002920
2921done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002922 return rv;
2923}
2924
Florin Coras86f04502018-09-12 16:08:01 -07002925static inline void
2926vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2927 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002928{
2929 session_disconnected_msg_t *disconnected_msg;
2930 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002931 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002932 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002933 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002934 u8 add_event = 0;
2935
2936 switch (e->event_type)
2937 {
Florin Coras653e43f2019-03-04 10:56:23 -08002938 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002939 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002940 s = vcl_session_get (wrk, sid);
2941 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002942 break;
Florin Corasb242d312020-10-26 15:35:40 -07002943 vcl_fifo_rx_evt_valid_or_break (s);
2944 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07002945 if (!(EPOLLIN & s->vep.ev.events) ||
2946 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
2947 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07002948 break;
2949 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08002950 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002951 session_evt_data = s->vep.ev.data.u64;
2952 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002953 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002954 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002955 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002956 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08002957 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002958 break;
Florin Corasb242d312020-10-26 15:35:40 -07002959 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002960 if (!(EPOLLOUT & session_events))
2961 break;
2962 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08002963 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002964 session_evt_data = s->vep.ev.data.u64;
2965 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2966 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002967 break;
Florin Coras86f04502018-09-12 16:08:01 -07002968 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002969 if (!e->postponed)
2970 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2971 else
2972 s = vcl_session_get (wrk, e->session_index);
2973 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002974 break;
Florin Corasb242d312020-10-26 15:35:40 -07002975 session_events = s->vep.ev.events;
2976 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08002977 if (!(EPOLLIN & session_events) ||
2978 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07002979 break;
Florin Coras86f04502018-09-12 16:08:01 -07002980 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08002981 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002982 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002983 break;
2984 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002985 if (!e->postponed)
2986 {
2987 connected_msg = (session_connected_msg_t *) e->data;
2988 sid = vcl_session_connected_handler (wrk, connected_msg);
2989 }
2990 else
2991 sid = e->session_index;
2992 s = vcl_session_get (wrk, sid);
2993 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002994 break;
Florin Corasb242d312020-10-26 15:35:40 -07002995 session_events = s->vep.ev.events;
2996 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002997 if (!(EPOLLOUT & session_events))
2998 break;
2999 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003000 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003001 session_evt_data = s->vep.ev.data.u64;
3002 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003003 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003004 break;
3005 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003006 if (!e->postponed)
3007 {
3008 disconnected_msg = (session_disconnected_msg_t *) e->data;
3009 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3010 }
3011 else
3012 {
3013 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003014 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003015 }
3016 if (vcl_session_is_closed (s) ||
3017 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003018 {
Florin Coras74254b52021-12-08 11:03:08 -08003019 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003020 vcl_session_free (wrk, s);
3021 break;
3022 }
Florin Corasb242d312020-10-26 15:35:40 -07003023 sid = s->session_index;
3024 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003025 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003026 if (EPOLLRDHUP & session_events)
3027 {
3028 /* If app can distinguish between RDHUP and HUP,
3029 * we make finer control */
3030 events[*num_ev].events = EPOLLRDHUP;
3031 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3032 {
3033 events[*num_ev].events |= EPOLLHUP;
3034 }
3035 }
3036 else
3037 {
3038 events[*num_ev].events = EPOLLHUP;
3039 }
Florin Corasb242d312020-10-26 15:35:40 -07003040 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003041
Florin Coras86f04502018-09-12 16:08:01 -07003042 break;
3043 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003044 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003045 {
3046 sid =
3047 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3048 s = vcl_session_get (wrk, sid);
3049 }
Florin Coras87f76002021-06-28 19:13:29 -07003050 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003051 {
3052 sid = e->session_index;
3053 s = vcl_session_get (wrk, sid);
3054 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3055 }
Florin Coras87f76002021-06-28 19:13:29 -07003056 if (vcl_session_is_closed (s) ||
3057 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003058 {
Florin Coras74254b52021-12-08 11:03:08 -08003059 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003060 vcl_session_free (wrk, s);
3061 break;
3062 }
Florin Corasb242d312020-10-26 15:35:40 -07003063 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003064 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003065 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003066 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003067 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003068 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3069 vcl_session_unlisten_reply_handler (wrk, e->data);
3070 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003071 case SESSION_CTRL_EVT_MIGRATED:
3072 vcl_session_migrated_handler (wrk, e->data);
3073 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003074 case SESSION_CTRL_EVT_CLEANUP:
3075 vcl_session_cleanup_handler (wrk, e->data);
3076 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003077 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3078 vcl_session_req_worker_update_handler (wrk, e->data);
3079 break;
3080 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3081 vcl_session_worker_update_reply_handler (wrk, e->data);
3082 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003083 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3084 vcl_session_app_add_segment_handler (wrk, e->data);
3085 break;
3086 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3087 vcl_session_app_del_segment_handler (wrk, e->data);
3088 break;
hanlina3a48962020-07-13 11:09:15 +08003089 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003090 vcl_worker_rpc_handler (wrk, e->data);
3091 break;
Florin Coras86f04502018-09-12 16:08:01 -07003092 default:
3093 VDBG (0, "unhandled: %u", e->event_type);
3094 break;
3095 }
3096
3097 if (add_event)
3098 {
3099 events[*num_ev].data.u64 = session_evt_data;
3100 if (EPOLLONESHOT & session_events)
3101 {
Florin Corasb242d312020-10-26 15:35:40 -07003102 s = vcl_session_get (wrk, sid);
3103 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003104 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003105 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003106 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003107 s = vcl_session_get (wrk, sid);
3108 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3109 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003110 }
Florin Coras86f04502018-09-12 16:08:01 -07003111 *num_ev += 1;
3112 }
3113}
3114
3115static int
3116vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3117 struct epoll_event *events, u32 maxevents,
3118 double wait_for_time, u32 * num_ev)
3119{
Florin Coras99368312018-08-02 10:45:44 -07003120 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003121 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003122 int i;
3123
Florin Coras539663c2018-09-28 14:59:37 -07003124 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3125 goto handle_dequeued;
3126
Florin Coras54693d22018-07-17 10:46:29 -07003127 if (svm_msg_q_is_empty (mq))
3128 {
3129 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003130 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003131 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003132 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003133 else
3134 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003135 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003136 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003137 }
3138 }
Florin Corase003a1b2019-06-05 10:47:16 -07003139 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003140 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003141
Florin Coras539663c2018-09-28 14:59:37 -07003142handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003143 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003144 {
Florin Coras134a9962018-08-28 11:32:04 -07003145 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003146 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003147 if (*num_ev < maxevents)
3148 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3149 else
3150 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003151 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003152 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003153 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003154 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003155 return *num_ev;
3156}
3157
Florin Coras99368312018-08-02 10:45:44 -07003158static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003159vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3160 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003161{
Florin Corasccdb8b82021-04-28 13:01:06 -07003162 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003163
3164 if (!n_evts)
3165 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003166 if (timeout_ms > 0)
3167 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003168 }
3169
3170 do
3171 {
3172 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003173 timeout_ms, &n_evts);
3174 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003175 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003176 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003177 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003178
3179 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003180}
3181
3182static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003183vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3184 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003185{
Florin Coras99368312018-08-02 10:45:44 -07003186 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003187 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003188 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003189 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003190 u64 buf;
3191
Florin Coras134a9962018-08-28 11:32:04 -07003192 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003193 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003194 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003195 if (timeout_ms > 0)
3196 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003197 }
3198
Florin Corasb13ba132021-01-27 16:05:24 -08003199 do
3200 {
3201 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003202 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003203 if (n_mq_evts < 0)
3204 {
3205 VDBG (0, "epoll_wait error %u", errno);
3206 return n_evts;
3207 }
3208
3209 for (i = 0; i < n_mq_evts; i++)
3210 {
3211 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3212 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3213 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3214 &n_evts);
3215 }
3216
Florin Corasccdb8b82021-04-28 13:01:06 -07003217 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003218 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003219 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003220 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003221
3222 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003223}
3224
Florin Corasfe286f72021-06-04 10:07:55 -07003225static void
Florin Corasfe286f72021-06-04 10:07:55 -07003226vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3227 int maxevents, u32 *n_evts)
3228{
Florin Coras734268f2021-06-30 07:54:29 -07003229 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003230 vcl_session_t *s;
3231 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003232 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003233
Florin Corasfa3884f2021-06-22 20:04:46 -07003234 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003235 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003236 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003237
Florin Corasfa3884f2021-06-22 20:04:46 -07003238 next = wrk->ep_lt_current;
3239 do
Florin Corasfe286f72021-06-04 10:07:55 -07003240 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003241 s = vcl_session_get (wrk, next);
3242 next = s->vep.lt_next;
3243
3244 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003245 {
3246 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003247 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003248 evt_data = s->vep.ev.data.u64;
3249 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003250 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003251 {
3252 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003253 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3254 evt_data = s->vep.ev.data.u64;
3255 }
3256 if (!add_event && s->session_state > VCL_STATE_READY)
3257 {
3258 add_event = 1;
3259 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003260 evt_data = s->vep.ev.data.u64;
3261 }
3262 if (add_event)
3263 {
3264 events[*n_evts].data.u64 = evt_data;
3265 *n_evts += 1;
3266 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003267 if (EPOLLONESHOT & s->vep.ev.events)
3268 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003269 if (*n_evts == maxevents)
3270 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003271 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003272 break;
3273 }
3274 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003275 else
3276 {
3277 vcl_epoll_lt_del (wrk, s);
3278 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3279 break;
3280 }
Florin Corasfe286f72021-06-04 10:07:55 -07003281 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003282 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003283}
3284
Dave Wallacef7f809c2017-10-03 01:48:42 -04003285int
Florin Coras134a9962018-08-28 11:32:04 -07003286vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003287 int maxevents, double wait_for_time)
3288{
Florin Coras134a9962018-08-28 11:32:04 -07003289 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003290 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003291 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003292 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003293
3294 if (PREDICT_FALSE (maxevents <= 0))
3295 {
Florin Coras5e062572019-03-14 19:07:51 -07003296 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003297 return VPPCOM_EINVAL;
3298 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003299
Florin Coras134a9962018-08-28 11:32:04 -07003300 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003301 if (!vep_session)
3302 return VPPCOM_EBADFD;
3303
Florin Coras6c3b2182020-10-19 18:36:48 -07003304 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003305 {
Florin Coras5e062572019-03-14 19:07:51 -07003306 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003307 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003308 }
Florin Coras54693d22018-07-17 10:46:29 -07003309
Florin Coras86f04502018-09-12 16:08:01 -07003310 if (vec_len (wrk->unhandled_evts_vector))
3311 {
3312 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3313 {
3314 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3315 events, &n_evts);
3316 if (n_evts == maxevents)
3317 {
Florin Corase003a1b2019-06-05 10:47:16 -07003318 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3319 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003320 }
3321 }
Florin Corase003a1b2019-06-05 10:47:16 -07003322 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003323 }
liuyacancba87102021-09-10 15:14:05 +08003324
3325 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3326 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3327
wanghanlin8919fec2021-03-18 20:00:41 +08003328 /* Request to only drain unhandled */
3329 if ((int) wait_for_time == -2)
3330 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003331
Florin Coras86f04502018-09-12 16:08:01 -07003332
Florin Corasfe286f72021-06-04 10:07:55 -07003333 if (vcm->cfg.use_mq_eventfd)
3334 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3335 wait_for_time);
3336 else
3337 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3338 wait_for_time);
3339
Florin Corasfe286f72021-06-04 10:07:55 -07003340 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003341}
3342
Dave Wallace35830af2017-10-09 01:43:42 -04003343int
Florin Coras134a9962018-08-28 11:32:04 -07003344vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003345 void *buffer, uint32_t * buflen)
3346{
Florin Coras134a9962018-08-28 11:32:04 -07003347 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003348 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003349 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003350 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003351 vcl_session_t *session;
3352 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003353
Florin Coras134a9962018-08-28 11:32:04 -07003354 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003355 if (!session)
3356 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003357
Dave Wallace35830af2017-10-09 01:43:42 -04003358 switch (op)
3359 {
3360 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003361 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003362 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3363 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003364 break;
3365
Dave Wallace227867f2017-11-13 21:21:53 -05003366 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003367 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003368 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3369 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003370 break;
3371
3372 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003373 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003374 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003375 *flags =
3376 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003377 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003378 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003379 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003380 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3381 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003382 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003383 }
3384 else
3385 rv = VPPCOM_EINVAL;
3386 break;
3387
3388 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003389 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003390 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003391 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003392 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003393 else
Florin Corasac422d62020-10-19 20:51:36 -07003394 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003395
Florin Coras5e062572019-03-14 19:07:51 -07003396 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3397 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003398 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003399 }
3400 else
3401 rv = VPPCOM_EINVAL;
3402 break;
3403
3404 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003405 if (PREDICT_TRUE (buffer && buflen &&
3406 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003407 {
Florin Coras7e12d942018-06-27 14:32:43 -07003408 ep->is_ip4 = session->transport.is_ip4;
3409 ep->port = session->transport.rmt_port;
3410 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003411 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3412 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003413 else
Dave Barach178cf492018-11-13 16:34:13 -05003414 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3415 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003416 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003417 VDBG (1,
3418 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3419 "addr = %U, port %u",
3420 session_handle, ep->is_ip4, vcl_format_ip46_address,
3421 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003422 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3423 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003424 }
3425 else
3426 rv = VPPCOM_EINVAL;
3427 break;
3428
3429 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003430 if (PREDICT_TRUE (buffer && buflen &&
3431 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003432 {
Florin Coras7e12d942018-06-27 14:32:43 -07003433 ep->is_ip4 = session->transport.is_ip4;
3434 ep->port = session->transport.lcl_port;
3435 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003436 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3437 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003438 else
Dave Barach178cf492018-11-13 16:34:13 -05003439 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3440 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003441 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003442 VDBG (1,
3443 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3444 " port %d",
3445 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003446 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003447 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3448 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003449 }
3450 else
3451 rv = VPPCOM_EINVAL;
3452 break;
Stevenb5a11602017-10-11 09:59:30 -07003453
Florin Corasef7cbf62019-10-17 09:56:27 -07003454 case VPPCOM_ATTR_SET_LCL_ADDR:
3455 if (PREDICT_TRUE (buffer && buflen &&
3456 (*buflen >= sizeof (*ep)) && ep->ip))
3457 {
3458 session->transport.is_ip4 = ep->is_ip4;
3459 session->transport.lcl_port = ep->port;
3460 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3461 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003462 VDBG (1,
3463 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3464 " port %d",
3465 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003466 &session->transport.lcl_ip,
3467 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3468 clib_net_to_host_u16 (ep->port));
3469 }
3470 else
3471 rv = VPPCOM_EINVAL;
3472 break;
3473
Dave Wallace048b1d62018-01-03 22:24:41 -05003474 case VPPCOM_ATTR_GET_LIBC_EPFD:
3475 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003476 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003477 break;
3478
3479 case VPPCOM_ATTR_SET_LIBC_EPFD:
3480 if (PREDICT_TRUE (buffer && buflen &&
3481 (*buflen == sizeof (session->libc_epfd))))
3482 {
3483 session->libc_epfd = *(int *) buffer;
3484 *buflen = sizeof (session->libc_epfd);
3485
Florin Coras5e062572019-03-14 19:07:51 -07003486 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3487 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003488 }
3489 else
3490 rv = VPPCOM_EINVAL;
3491 break;
3492
3493 case VPPCOM_ATTR_GET_PROTOCOL:
3494 if (buffer && buflen && (*buflen >= sizeof (int)))
3495 {
Florin Coras7e12d942018-06-27 14:32:43 -07003496 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003497 *buflen = sizeof (int);
3498
Florin Coras5e062572019-03-14 19:07:51 -07003499 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3500 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003501 }
3502 else
3503 rv = VPPCOM_EINVAL;
3504 break;
3505
3506 case VPPCOM_ATTR_GET_LISTEN:
3507 if (buffer && buflen && (*buflen >= sizeof (int)))
3508 {
Florin Corasac422d62020-10-19 20:51:36 -07003509 *(int *) buffer = vcl_session_has_attr (session,
3510 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003511 *buflen = sizeof (int);
3512
Florin Coras5e062572019-03-14 19:07:51 -07003513 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3514 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003515 }
3516 else
3517 rv = VPPCOM_EINVAL;
3518 break;
3519
3520 case VPPCOM_ATTR_GET_ERROR:
3521 if (buffer && buflen && (*buflen >= sizeof (int)))
3522 {
3523 *(int *) buffer = 0;
3524 *buflen = sizeof (int);
3525
Florin Coras5e062572019-03-14 19:07:51 -07003526 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3527 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 }
3529 else
3530 rv = VPPCOM_EINVAL;
3531 break;
3532
3533 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3534 if (buffer && buflen && (*buflen >= sizeof (u32)))
3535 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003536
3537 /* VPP-TBD */
3538 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003539 session->tx_fifo ?
3540 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003541 vcm->cfg.tx_fifo_size);
3542 *buflen = sizeof (u32);
3543
Florin Coras5e062572019-03-14 19:07:51 -07003544 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3545 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3546 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003547 }
3548 else
3549 rv = VPPCOM_EINVAL;
3550 break;
3551
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003552 case VPPCOM_ATTR_SET_DSCP:
3553 if (buffer && buflen && (*buflen >= sizeof (u8)))
3554 {
3555 session->dscp = *(u8 *) buffer;
3556
3557 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3558 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3559 }
3560 else
3561 rv = VPPCOM_EINVAL;
3562 break;
3563
Dave Wallace048b1d62018-01-03 22:24:41 -05003564 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3565 if (buffer && buflen && (*buflen == sizeof (u32)))
3566 {
3567 /* VPP-TBD */
3568 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003569 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3570 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3571 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003572 }
3573 else
3574 rv = VPPCOM_EINVAL;
3575 break;
3576
3577 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3578 if (buffer && buflen && (*buflen >= sizeof (u32)))
3579 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003580
3581 /* VPP-TBD */
3582 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003583 session->rx_fifo ?
3584 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003585 vcm->cfg.rx_fifo_size);
3586 *buflen = sizeof (u32);
3587
Florin Coras5e062572019-03-14 19:07:51 -07003588 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3589 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003590 }
3591 else
3592 rv = VPPCOM_EINVAL;
3593 break;
3594
3595 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3596 if (buffer && buflen && (*buflen == sizeof (u32)))
3597 {
3598 /* VPP-TBD */
3599 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003600 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3601 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3602 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003603 }
3604 else
3605 rv = VPPCOM_EINVAL;
3606 break;
3607
3608 case VPPCOM_ATTR_GET_REUSEADDR:
3609 if (buffer && buflen && (*buflen >= sizeof (int)))
3610 {
3611 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003612 *(int *) buffer = vcl_session_has_attr (session,
3613 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003614 *buflen = sizeof (int);
3615
Florin Coras5e062572019-03-14 19:07:51 -07003616 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3617 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003618 }
3619 else
3620 rv = VPPCOM_EINVAL;
3621 break;
3622
Stevenb5a11602017-10-11 09:59:30 -07003623 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003624 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003625 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003626 {
3627 /* VPP-TBD */
3628 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003629 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003630 else
Florin Corasac422d62020-10-19 20:51:36 -07003631 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003632
Florin Coras5e062572019-03-14 19:07:51 -07003633 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003634 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003635 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003636 }
3637 else
3638 rv = VPPCOM_EINVAL;
3639 break;
3640
3641 case VPPCOM_ATTR_GET_REUSEPORT:
3642 if (buffer && buflen && (*buflen >= sizeof (int)))
3643 {
3644 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003645 *(int *) buffer = vcl_session_has_attr (session,
3646 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003647 *buflen = sizeof (int);
3648
Florin Coras5e062572019-03-14 19:07:51 -07003649 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3650 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003651 }
3652 else
3653 rv = VPPCOM_EINVAL;
3654 break;
3655
3656 case VPPCOM_ATTR_SET_REUSEPORT:
3657 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003658 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003659 {
3660 /* VPP-TBD */
3661 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003662 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003663 else
Florin Corasac422d62020-10-19 20:51:36 -07003664 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003665
Florin Coras5e062572019-03-14 19:07:51 -07003666 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003667 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003668 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003669 }
3670 else
3671 rv = VPPCOM_EINVAL;
3672 break;
3673
3674 case VPPCOM_ATTR_GET_BROADCAST:
3675 if (buffer && buflen && (*buflen >= sizeof (int)))
3676 {
3677 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003678 *(int *) buffer = vcl_session_has_attr (session,
3679 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003680 *buflen = sizeof (int);
3681
Florin Coras5e062572019-03-14 19:07:51 -07003682 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3683 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003684 }
3685 else
3686 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003687 break;
3688
3689 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003690 if (buffer && buflen && (*buflen == sizeof (int)))
3691 {
3692 /* VPP-TBD */
3693 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003694 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003695 else
Florin Corasac422d62020-10-19 20:51:36 -07003696 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003697
Florin Coras5e062572019-03-14 19:07:51 -07003698 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003699 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003700 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003701 }
3702 else
3703 rv = VPPCOM_EINVAL;
3704 break;
3705
3706 case VPPCOM_ATTR_GET_V6ONLY:
3707 if (buffer && buflen && (*buflen >= sizeof (int)))
3708 {
3709 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003710 *(int *) buffer = vcl_session_has_attr (session,
3711 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003712 *buflen = sizeof (int);
3713
Florin Coras5e062572019-03-14 19:07:51 -07003714 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3715 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003716 }
3717 else
3718 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003719 break;
3720
3721 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003722 if (buffer && buflen && (*buflen == sizeof (int)))
3723 {
3724 /* VPP-TBD */
3725 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003726 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003727 else
Florin Corasac422d62020-10-19 20:51:36 -07003728 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003729
Florin Coras5e062572019-03-14 19:07:51 -07003730 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003731 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003732 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003733 }
3734 else
3735 rv = VPPCOM_EINVAL;
3736 break;
3737
3738 case VPPCOM_ATTR_GET_KEEPALIVE:
3739 if (buffer && buflen && (*buflen >= sizeof (int)))
3740 {
3741 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003742 *(int *) buffer = vcl_session_has_attr (session,
3743 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003744 *buflen = sizeof (int);
3745
Florin Coras5e062572019-03-14 19:07:51 -07003746 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3747 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003748 }
3749 else
3750 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003751 break;
Stevenbccd3392017-10-12 20:42:21 -07003752
3753 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003754 if (buffer && buflen && (*buflen == sizeof (int)))
3755 {
3756 /* VPP-TBD */
3757 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003758 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003759 else
Florin Corasac422d62020-10-19 20:51:36 -07003760 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003761
Florin Coras5e062572019-03-14 19:07:51 -07003762 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003763 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003764 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003765 }
3766 else
3767 rv = VPPCOM_EINVAL;
3768 break;
3769
3770 case VPPCOM_ATTR_GET_TCP_NODELAY:
3771 if (buffer && buflen && (*buflen >= sizeof (int)))
3772 {
3773 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003774 *(int *) buffer = vcl_session_has_attr (session,
3775 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003776 *buflen = sizeof (int);
3777
Florin Coras5e062572019-03-14 19:07:51 -07003778 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3779 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003780 }
3781 else
3782 rv = VPPCOM_EINVAL;
3783 break;
3784
3785 case VPPCOM_ATTR_SET_TCP_NODELAY:
3786 if (buffer && buflen && (*buflen == sizeof (int)))
3787 {
3788 /* VPP-TBD */
3789 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003790 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003791 else
Florin Corasac422d62020-10-19 20:51:36 -07003792 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003793
Florin Coras5e062572019-03-14 19:07:51 -07003794 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003795 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003796 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003797 }
3798 else
3799 rv = VPPCOM_EINVAL;
3800 break;
3801
3802 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3803 if (buffer && buflen && (*buflen >= sizeof (int)))
3804 {
3805 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003806 *(int *) buffer = vcl_session_has_attr (session,
3807 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003808 *buflen = sizeof (int);
3809
Florin Coras5e062572019-03-14 19:07:51 -07003810 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3811 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003812 }
3813 else
3814 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003815 break;
3816
3817 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003818 if (buffer && buflen && (*buflen == sizeof (int)))
3819 {
3820 /* VPP-TBD */
3821 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003822 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003823 else
Florin Corasac422d62020-10-19 20:51:36 -07003824 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003825
Florin Coras5e062572019-03-14 19:07:51 -07003826 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003827 vcl_session_has_attr (session,
3828 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003829 }
3830 else
3831 rv = VPPCOM_EINVAL;
3832 break;
3833
3834 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3835 if (buffer && buflen && (*buflen >= sizeof (int)))
3836 {
3837 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003838 *(int *) buffer = vcl_session_has_attr (session,
3839 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003840 *buflen = sizeof (int);
3841
Florin Coras5e062572019-03-14 19:07:51 -07003842 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3843 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003844 }
3845 else
3846 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003847 break;
3848
3849 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003850 if (buffer && buflen && (*buflen == sizeof (int)))
3851 {
3852 /* VPP-TBD */
3853 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003854 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003855 else
Florin Corasac422d62020-10-19 20:51:36 -07003856 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003857
Florin Coras5e062572019-03-14 19:07:51 -07003858 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003859 vcl_session_has_attr (session,
3860 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003861 }
3862 else
3863 rv = VPPCOM_EINVAL;
3864 break;
3865
3866 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003867 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003868 {
Florin Coras04ae8272021-04-12 19:55:37 -07003869 rv = VPPCOM_EINVAL;
3870 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003871 }
Florin Coras04ae8272021-04-12 19:55:37 -07003872
3873 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3874 tea.mss = *(u32 *) buffer;
3875 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3876 rv = VPPCOM_ENOPROTOOPT;
3877
3878 if (!rv)
3879 {
3880 *(u32 *) buffer = tea.mss;
3881 *buflen = sizeof (int);
3882 }
3883
3884 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3885 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003886 break;
3887
3888 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003889 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003890 {
Florin Coras04ae8272021-04-12 19:55:37 -07003891 rv = VPPCOM_EINVAL;
3892 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003893 }
Florin Coras04ae8272021-04-12 19:55:37 -07003894
3895 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3896 tea.mss = *(u32 *) buffer;
3897 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3898 rv = VPPCOM_ENOPROTOOPT;
3899
3900 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3901 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003902 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003903
Florin Coras1e966172020-05-16 18:18:14 +00003904 case VPPCOM_ATTR_SET_CONNECTED:
3905 session->flags |= VCL_SESSION_F_CONNECTED;
3906 break;
3907
Florin Corasa5a9efd2021-01-05 17:03:29 -08003908 case VPPCOM_ATTR_SET_CKPAIR:
3909 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3910 !vcl_session_has_crypto (session))
3911 {
3912 rv = VPPCOM_EINVAL;
3913 break;
3914 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003915 if (!session->ext_config)
3916 {
Florin Coras87f63892021-05-01 16:01:40 -07003917 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3918 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003919 }
3920 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3921 {
3922 rv = VPPCOM_EINVAL;
3923 break;
3924 }
3925
3926 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003927 break;
3928
Florin Coras6a6555a2021-01-28 11:39:27 -08003929 case VPPCOM_ATTR_SET_VRF:
3930 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3931 {
3932 rv = VPPCOM_EINVAL;
3933 break;
3934 }
3935 session->vrf = *(u32 *) buffer;
3936 break;
3937
3938 case VPPCOM_ATTR_GET_VRF:
3939 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3940 {
3941 rv = VPPCOM_EINVAL;
3942 break;
3943 }
3944 *(u32 *) buffer = session->vrf;
3945 *buflen = sizeof (u32);
3946 break;
3947
wanghanlin0674f852021-02-22 10:38:36 +08003948 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003949 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003950 {
Florin Corasd77325c2021-02-23 12:03:03 -08003951 rv = VPPCOM_EINVAL;
3952 break;
wanghanlin0674f852021-02-22 10:38:36 +08003953 }
Florin Corasd77325c2021-02-23 12:03:03 -08003954
3955 if (session->transport.is_ip4)
3956 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003957 else
Florin Corasd77325c2021-02-23 12:03:03 -08003958 *(int *) buffer = AF_INET6;
3959 *buflen = sizeof (int);
3960
wanghanlin0674f852021-02-22 10:38:36 +08003961 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3962 *buflen);
3963 break;
3964
Florin Coras87f63892021-05-01 16:01:40 -07003965 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3966 if (!(buffer && buflen && (*buflen > 0)))
3967 {
3968 rv = VPPCOM_EINVAL;
3969 break;
3970 }
3971 if (session->ext_config)
3972 {
3973 rv = VPPCOM_EINVAL;
3974 break;
3975 }
3976 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3977 *buflen + sizeof (u32));
3978 clib_memcpy (session->ext_config->data, buffer, *buflen);
3979 session->ext_config->len = *buflen;
3980 break;
3981
Dave Wallacee22aa742017-10-20 12:30:38 -04003982 default:
3983 rv = VPPCOM_EINVAL;
3984 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003985 }
3986
Dave Wallace35830af2017-10-09 01:43:42 -04003987 return rv;
3988}
3989
Stevenac1f96d2017-10-24 16:03:58 -07003990int
Florin Coras134a9962018-08-28 11:32:04 -07003991vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003992 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3993{
Florin Coras134a9962018-08-28 11:32:04 -07003994 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003995 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003996 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003997
3998 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003999 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004000 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004001 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004002 else
4003 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004004 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004005 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004006 }
4007
Florin Coras0a1e1832020-03-29 18:54:04 +00004008 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004009 {
Florin Coras5da10c42020-04-01 04:31:21 +00004010 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004011 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004012 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4013 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004014 else
Dave Barach178cf492018-11-13 16:34:13 -05004015 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4016 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004017 ep->is_ip4 = session->transport.is_ip4;
4018 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004019 }
Florin Coras460dce62018-07-27 05:45:06 -07004020
Stevenac1f96d2017-10-24 16:03:58 -07004021 return rv;
4022}
4023
4024int
Florin Coras134a9962018-08-28 11:32:04 -07004025vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004026 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4027{
Florin Coras7a2abce2020-04-05 19:25:44 +00004028 vcl_worker_t *wrk = vcl_worker_get_current ();
4029 vcl_session_t *s;
4030
4031 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004032 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004033 return VPPCOM_EBADFD;
4034
Dave Wallace617dffa2017-10-26 14:47:06 -04004035 if (ep)
4036 {
Florin Coras5824cc52020-10-20 18:44:41 -07004037 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004038 return VPPCOM_EINVAL;
4039
liuyacanbc0c7542021-08-02 20:15:05 +08004040 s->transport.is_ip4 = ep->is_ip4;
4041 s->transport.rmt_port = ep->port;
4042 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4043
Florin Coras5da10c42020-04-01 04:31:21 +00004044 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004045 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004046 {
Florin Coras5824cc52020-10-20 18:44:41 -07004047 u32 session_index = s->session_index;
4048 f64 timeout = vcm->cfg.session_timeout;
4049 int rv;
4050
Florin Coras5da10c42020-04-01 04:31:21 +00004051 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004052 rv = vppcom_wait_for_session_state_change (session_index,
4053 VCL_STATE_READY,
4054 timeout);
4055 if (rv < 0)
4056 return rv;
4057 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004058 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004059 }
4060
4061 if (flags)
4062 {
4063 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004064 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004065 }
4066
Florin Coras7a2abce2020-04-05 19:25:44 +00004067 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4068 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004069}
4070
Dave Wallace048b1d62018-01-03 22:24:41 -05004071int
4072vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4073{
Florin Coras134a9962018-08-28 11:32:04 -07004074 vcl_worker_t *wrk = vcl_worker_get_current ();
4075 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004076 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004077 svm_msg_q_msg_t msg;
4078 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004079 int rv, num_ev = 0;
4080
Florin Coras5e062572019-03-14 19:07:51 -07004081 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004082
4083 if (!vp)
4084 return VPPCOM_EFAULT;
4085
4086 do
4087 {
Florin Coras7e12d942018-06-27 14:32:43 -07004088 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004089
Florin Coras6917b942018-11-13 22:44:54 -08004090 /* Dequeue all events and drop all unhandled io events */
4091 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4092 {
4093 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4094 vcl_handle_mq_event (wrk, e);
4095 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4096 }
4097 vec_reset_length (wrk->unhandled_evts_vector);
4098
Dave Wallace048b1d62018-01-03 22:24:41 -05004099 for (i = 0; i < n_sids; i++)
4100 {
Florin Coras7baeb712019-01-04 17:05:43 -08004101 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004102 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004103 {
4104 vp[i].revents = POLLHUP;
4105 num_ev++;
4106 continue;
4107 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004108
Florin Coras6917b942018-11-13 22:44:54 -08004109 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004110
4111 if (POLLIN & vp[i].events)
4112 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004113 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004114 if (rv > 0)
4115 {
Florin Coras6917b942018-11-13 22:44:54 -08004116 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004117 num_ev++;
4118 }
4119 else if (rv < 0)
4120 {
4121 switch (rv)
4122 {
4123 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004124 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004125 break;
4126
4127 default:
Florin Coras6917b942018-11-13 22:44:54 -08004128 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004129 break;
4130 }
4131 num_ev++;
4132 }
4133 }
4134
4135 if (POLLOUT & vp[i].events)
4136 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004137 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004138 if (rv > 0)
4139 {
Florin Coras6917b942018-11-13 22:44:54 -08004140 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004141 num_ev++;
4142 }
4143 else if (rv < 0)
4144 {
4145 switch (rv)
4146 {
4147 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004148 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004149 break;
4150
4151 default:
Florin Coras6917b942018-11-13 22:44:54 -08004152 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004153 break;
4154 }
4155 num_ev++;
4156 }
4157 }
4158
Dave Wallace7e607a72018-06-18 18:41:32 -04004159 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004160 {
Florin Coras6917b942018-11-13 22:44:54 -08004161 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004162 num_ev++;
4163 }
4164 }
4165 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004166 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004167 }
4168 while ((num_ev == 0) && keep_trying);
4169
Dave Wallace048b1d62018-01-03 22:24:41 -05004170 return num_ev;
4171}
4172
Florin Coras99368312018-08-02 10:45:44 -07004173int
4174vppcom_mq_epoll_fd (void)
4175{
Florin Coras134a9962018-08-28 11:32:04 -07004176 vcl_worker_t *wrk = vcl_worker_get_current ();
4177 return wrk->mqs_epfd;
4178}
4179
4180int
Florin Coras30e79c22019-01-02 19:31:22 -08004181vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004182{
4183 return session_handle & 0xFFFFFF;
4184}
4185
4186int
Florin Coras30e79c22019-01-02 19:31:22 -08004187vppcom_session_worker (vcl_session_handle_t session_handle)
4188{
4189 return session_handle >> 24;
4190}
4191
4192int
Florin Coras134a9962018-08-28 11:32:04 -07004193vppcom_worker_register (void)
4194{
Florin Coras47c40e22018-11-26 17:01:36 -08004195 if (!vcl_worker_alloc_and_init ())
4196 return VPPCOM_EEXIST;
4197
Florin Coras47c40e22018-11-26 17:01:36 -08004198 if (vcl_worker_register_with_vpp ())
4199 return VPPCOM_EEXIST;
4200
4201 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004202}
4203
Florin Coras369db832019-07-08 12:34:45 -07004204void
4205vppcom_worker_unregister (void)
4206{
4207 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4208 vcl_set_worker_index (~0);
4209}
4210
Pivo6017ff02020-05-04 17:57:33 +02004211void
4212vppcom_worker_index_set (int index)
4213{
4214 vcl_set_worker_index (index);
4215}
4216
Florin Corasdfe4cf42018-11-28 22:13:45 -08004217int
4218vppcom_worker_index (void)
4219{
4220 return vcl_get_worker_index ();
4221}
4222
Florin Corase0982e52019-01-25 13:19:56 -08004223int
4224vppcom_worker_mqs_epfd (void)
4225{
4226 vcl_worker_t *wrk = vcl_worker_get_current ();
4227 if (!vcm->cfg.use_mq_eventfd)
4228 return -1;
4229 return wrk->mqs_epfd;
4230}
4231
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004232int
4233vppcom_session_is_connectable_listener (uint32_t session_handle)
4234{
4235 vcl_session_t *session;
4236 vcl_worker_t *wrk = vcl_worker_get_current ();
4237 session = vcl_session_get_w_handle (wrk, session_handle);
4238 if (!session)
4239 return VPPCOM_EBADFD;
4240 return vcl_session_is_connectable_listener (wrk, session);
4241}
4242
4243int
4244vppcom_session_listener (uint32_t session_handle)
4245{
4246 vcl_worker_t *wrk = vcl_worker_get_current ();
4247 vcl_session_t *listen_session, *session;
4248 session = vcl_session_get_w_handle (wrk, session_handle);
4249 if (!session)
4250 return VPPCOM_EBADFD;
4251 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4252 return VPPCOM_EBADFD;
4253 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4254 if (!listen_session)
4255 return VPPCOM_EBADFD;
4256 return vcl_session_handle (listen_session);
4257}
4258
4259int
4260vppcom_session_n_accepted (uint32_t session_handle)
4261{
4262 vcl_worker_t *wrk = vcl_worker_get_current ();
4263 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4264 if (!session)
4265 return VPPCOM_EBADFD;
4266 return session->n_accepted_sessions;
4267}
4268
Florin Coras66ec4672020-06-15 07:59:40 -07004269const char *
4270vppcom_proto_str (vppcom_proto_t proto)
4271{
4272 char const *proto_str;
4273
4274 switch (proto)
4275 {
4276 case VPPCOM_PROTO_TCP:
4277 proto_str = "TCP";
4278 break;
4279 case VPPCOM_PROTO_UDP:
4280 proto_str = "UDP";
4281 break;
4282 case VPPCOM_PROTO_TLS:
4283 proto_str = "TLS";
4284 break;
4285 case VPPCOM_PROTO_QUIC:
4286 proto_str = "QUIC";
4287 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004288 case VPPCOM_PROTO_DTLS:
4289 proto_str = "DTLS";
4290 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004291 case VPPCOM_PROTO_SRTP:
4292 proto_str = "SRTP";
4293 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004294 default:
4295 proto_str = "UNKNOWN";
4296 break;
4297 }
4298 return proto_str;
4299}
4300
4301const char *
4302vppcom_retval_str (int retval)
4303{
4304 char const *st;
4305
4306 switch (retval)
4307 {
4308 case VPPCOM_OK:
4309 st = "VPPCOM_OK";
4310 break;
4311
4312 case VPPCOM_EAGAIN:
4313 st = "VPPCOM_EAGAIN";
4314 break;
4315
4316 case VPPCOM_EFAULT:
4317 st = "VPPCOM_EFAULT";
4318 break;
4319
4320 case VPPCOM_ENOMEM:
4321 st = "VPPCOM_ENOMEM";
4322 break;
4323
4324 case VPPCOM_EINVAL:
4325 st = "VPPCOM_EINVAL";
4326 break;
4327
4328 case VPPCOM_EBADFD:
4329 st = "VPPCOM_EBADFD";
4330 break;
4331
4332 case VPPCOM_EAFNOSUPPORT:
4333 st = "VPPCOM_EAFNOSUPPORT";
4334 break;
4335
4336 case VPPCOM_ECONNABORTED:
4337 st = "VPPCOM_ECONNABORTED";
4338 break;
4339
4340 case VPPCOM_ECONNRESET:
4341 st = "VPPCOM_ECONNRESET";
4342 break;
4343
4344 case VPPCOM_ENOTCONN:
4345 st = "VPPCOM_ENOTCONN";
4346 break;
4347
4348 case VPPCOM_ECONNREFUSED:
4349 st = "VPPCOM_ECONNREFUSED";
4350 break;
4351
4352 case VPPCOM_ETIMEDOUT:
4353 st = "VPPCOM_ETIMEDOUT";
4354 break;
4355
4356 default:
4357 st = "UNKNOWN_STATE";
4358 break;
4359 }
4360
4361 return st;
4362}
4363
Florin Corasa5a9efd2021-01-05 17:03:29 -08004364int
4365vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4366{
4367 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004368 return vcl_sapi_add_cert_key_pair (ckpair);
4369 else
4370 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004371}
4372
4373int
4374vppcom_del_cert_key_pair (uint32_t ckpair_index)
4375{
4376 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004377 return vcl_sapi_del_cert_key_pair (ckpair_index);
4378 else
4379 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004380}
4381
Dave Wallacee22aa742017-10-20 12:30:38 -04004382/*
4383 * fd.io coding-style-patch-verification: ON
4384 *
4385 * Local Variables:
4386 * eval: (c-set-style "gnu")
4387 * End:
4388 */