blob: c8f1172deffead83e11f5278501eeecfc94fb54b [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 Corasc547e912020-12-08 17:50:45 -0800339 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700340 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700341 }
342
Florin Coras54693d22018-07-17 10:46:29 -0700343 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700344 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700345 session->transport.rmt_port = mp->rmt.port;
346 session->transport.is_ip4 = mp->rmt.is_ip4;
347 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500348 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700349
Florin Coras134a9962018-08-28 11:32:04 -0700350 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800351 session->transport.lcl_port = mp->lcl.port;
352 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700353 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200354 session->is_dgram = vcl_proto_is_dgram (session->session_type);
355 session->listener_index = listen_session->session_index;
356 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700357
Florin Coras60687192021-11-29 21:00:47 -0800358 VDBG (1,
359 "session %u [0x%llx]: client accept request from %s address %U"
360 " port %d queue %p!",
361 session->session_index, mp->handle, mp->rmt.is_ip4 ? "IPv4" : "IPv6",
362 vcl_format_ip46_address, &mp->rmt.ip,
Florin Coras09d18c22019-04-24 11:10:02 -0700363 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
364 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700365 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
366
Florin Coras00cca802019-06-06 09:38:44 -0700367 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
368 session->vpp_handle, 0);
369
Florin Coras134a9962018-08-28 11:32:04 -0700370 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700371
372error:
Florin Corasb4624182020-12-11 13:58:12 -0800373 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
374 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700375 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
376 VNET_API_ERROR_INVALID_ARGUMENT);
377 vcl_session_free (wrk, session);
378 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700379}
380
381static u32
Florin Coras134a9962018-08-28 11:32:04 -0700382vcl_session_connected_handler (vcl_worker_t * wrk,
383 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700384{
Florin Coras99368312018-08-02 10:45:44 -0700385 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800386 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700387
388 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700389 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800390 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700391 {
Florin Coras8d42c752021-11-29 23:26:19 -0800392 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
393 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800394 /* Should not happen but if it does, force vpp session cleanup */
395 vcl_session_t tmp_session = {
396 .vpp_handle = mp->handle,
397 .vpp_evt_q = 0,
398 };
399 vcl_segment_attach_session (
400 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
401 mp->vpp_event_queue_address, mp->mq_index, 0, session);
402 if (tmp_session.vpp_evt_q)
403 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700404 return VCL_INVALID_SESSION_INDEX;
405 }
Florin Coras8d42c752021-11-29 23:26:19 -0800406
Florin Coras54693d22018-07-17 10:46:29 -0700407 if (mp->retval)
408 {
Florin Coras8d42c752021-11-29 23:26:19 -0800409 VDBG (0, "session index %u: connect failed! %U", session_index,
410 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700411 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800412 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras070453d2018-08-24 17:04:27 -0700413 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700414 }
415
Florin Corasdbc9c592019-09-25 16:37:43 -0700416 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800417
Florin Coras8d42c752021-11-29 23:26:19 -0800418 /* Add to lookup table. Even if something fails, session cannot be
419 * cleaned up prior to notifying vpp and going through the cleanup
420 * "procedure" see @ref vcl_session_cleanup_handler */
421 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
422
Florin Corasf6e284b2021-07-21 18:17:20 -0700423 if (vcl_segment_attach_session (
424 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
425 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800426 {
Florin Corasc547e912020-12-08 17:50:45 -0800427 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras8d42c752021-11-29 23:26:19 -0800428 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700429 vcl_send_session_disconnect (wrk, session);
430 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800431 }
432
Florin Coras653e43f2019-03-04 10:56:23 -0800433 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700434 {
Florin Corasc547e912020-12-08 17:50:45 -0800435 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700436 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
437 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800438 {
Florin Corasc547e912020-12-08 17:50:45 -0800439 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Coras8d42c752021-11-29 23:26:19 -0800440 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700441 vcl_send_session_disconnect (wrk, session);
442 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800443 }
Florin Coras99368312018-08-02 10:45:44 -0700444 }
Florin Coras54693d22018-07-17 10:46:29 -0700445
Florin Coras09d18c22019-04-24 11:10:02 -0700446 session->transport.is_ip4 = mp->lcl.is_ip4;
447 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500448 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700449 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700450
451 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700452 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700453 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700454 vcl_send_session_disconnect (wrk, session);
455 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700456 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700457
Florin Coras5e062572019-03-14 19:07:51 -0700458 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
459 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700460 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700461
Florin Coras54693d22018-07-17 10:46:29 -0700462 return session_index;
463}
464
Florin Coras3c7d4f92018-12-14 11:28:43 -0800465static int
466vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
467{
468 vcl_session_msg_t *accepted_msg;
469 int i;
470
471 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
472 {
473 accepted_msg = &session->accept_evts_fifo[i];
474 if (accepted_msg->accepted_msg.handle == handle)
475 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800476 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800477 return 1;
478 }
479 }
480 return 0;
481}
482
Florin Corasc9fbd662018-08-24 12:59:56 -0700483static u32
Florin Coras134a9962018-08-28 11:32:04 -0700484vcl_session_reset_handler (vcl_worker_t * wrk,
485 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700486{
487 vcl_session_t *session;
488 u32 sid;
489
Florin Coras134a9962018-08-28 11:32:04 -0700490 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
491 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700492 if (!session)
493 {
494 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
495 return VCL_INVALID_SESSION_INDEX;
496 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800497
498 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700499 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800500 {
501
502 if (!vcl_flag_accepted_session (session, reset_msg->handle,
503 VCL_ACCEPTED_F_RESET))
504 VDBG (0, "session was not accepted!");
505 return VCL_INVALID_SESSION_INDEX;
506 }
507
Florin Corasc127d5a2020-10-14 16:35:58 -0700508 if (session->session_state != VCL_STATE_CLOSED)
509 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800510 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700511 return sid;
512}
513
Florin Coras60116992018-08-27 09:52:18 -0700514static u32
Florin Coras134a9962018-08-28 11:32:04 -0700515vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700516{
517 vcl_session_t *session;
518 u32 sid = mp->context;
519
Florin Coras134a9962018-08-28 11:32:04 -0700520 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700521 if (mp->retval)
522 {
Florin Coras5e062572019-03-14 19:07:51 -0700523 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700524 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700525 if (session)
526 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700527 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700528 session->vpp_handle = mp->handle;
529 return sid;
530 }
531 else
532 {
Florin Coras5e062572019-03-14 19:07:51 -0700533 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
534 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700535 return VCL_INVALID_SESSION_INDEX;
536 }
537 }
538
539 session->vpp_handle = mp->handle;
540 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500541 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
542 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700543 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700544 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700545 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800546
Florin Coras6e3c1f82020-01-15 01:30:46 +0000547 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700548 {
Florin Corasc547e912020-12-08 17:50:45 -0800549 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700550 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
551 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800552 {
553 VDBG (0, "failed to attach fifos for %u", session->session_index);
554 session->session_state = VCL_STATE_DETACHED;
555 return VCL_INVALID_SESSION_INDEX;
556 }
Florin Coras60116992018-08-27 09:52:18 -0700557 }
558
Florin Coras05ecfcc2018-12-12 18:19:39 -0800559 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700560 return sid;
561}
562
Florin Corasdfae9f92019-02-20 19:48:31 -0800563static void
564vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
565{
566 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
567 vcl_session_t *s;
568
569 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000570 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800571 {
572 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
573 return;
574 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700575 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000576 {
577 /* Connected udp listener */
578 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700579 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000580 return;
581
582 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
583 return;
584 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800585
586 if (mp->retval)
587 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700588 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800589
590 if (mp->context != wrk->wrk_index)
591 VDBG (0, "wrong context");
592
593 vcl_session_table_del_vpp_handle (wrk, mp->handle);
594 vcl_session_free (wrk, s);
595}
596
Florin Coras68b7e582020-01-21 18:33:23 -0800597static void
598vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
599{
600 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
601 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800602 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800603
604 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
605 if (!s)
606 {
607 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
608 return;
609 }
610
Florin Coras1bb74942021-02-10 15:26:37 -0800611 /* Only validate if a value is provided */
612 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800613 {
Florin Coras1bb74942021-02-10 15:26:37 -0800614 fs_index = vcl_segment_table_lookup (mp->segment_handle);
615 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
616 {
617 VDBG (0, "segment %lx for session %u is not mounted!",
618 mp->segment_handle, s->session_index);
619 s->session_state = VCL_STATE_DETACHED;
620 return;
621 }
Florin Corasc547e912020-12-08 17:50:45 -0800622 }
623
Florin Coras57660d92020-04-04 22:45:34 +0000624 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800625
626 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
627 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800628
Florin Coras68b7e582020-01-21 18:33:23 -0800629 vcl_session_table_del_vpp_handle (wrk, mp->handle);
630 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
631
632 /* Generate new tx event if we have outstanding data */
633 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800634 app_send_io_evt_to_vpp (s->vpp_evt_q,
635 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800636 SESSION_IO_EVT_TX, SVM_Q_WAIT);
637
Florin Coras57660d92020-04-04 22:45:34 +0000638 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800639 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800640}
641
Florin Coras3c7d4f92018-12-14 11:28:43 -0800642static vcl_session_t *
643vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
644{
645 vcl_session_msg_t *vcl_msg;
646 vcl_session_t *session;
647
648 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
649 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800650 VWRN ("session overlap handle %lu state %u!", msg->handle,
651 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800652
653 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
654 if (!session)
655 {
656 VERR ("couldn't find listen session: listener handle %llx",
657 msg->listener_handle);
658 return 0;
659 }
660
661 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000662 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800663 vcl_msg->accepted_msg = *msg;
664 /* Session handle points to listener until fully accepted by app */
665 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
666
667 return session;
668}
669
670static vcl_session_t *
671vcl_session_disconnected_handler (vcl_worker_t * wrk,
672 session_disconnected_msg_t * msg)
673{
674 vcl_session_t *session;
675
676 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
677 if (!session)
678 {
679 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
680 return 0;
681 }
682
Florin Corasadcfb152020-02-12 08:50:29 +0000683 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700684 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000685 return 0;
686
Florin Coras3c7d4f92018-12-14 11:28:43 -0800687 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700688 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800689 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800690 if (!vcl_flag_accepted_session (session, msg->handle,
691 VCL_ACCEPTED_F_CLOSED))
692 VDBG (0, "session was not accepted!");
693 return 0;
694 }
695
Florin Corasadcfb152020-02-12 08:50:29 +0000696 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700697 if (session->session_state != VCL_STATE_DISCONNECT)
698 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000699
Florin Coras3c7d4f92018-12-14 11:28:43 -0800700 return session;
701}
702
liuyacan534468e2021-05-09 03:50:40 +0000703int
liuyacan55c952e2021-06-13 14:54:55 +0800704vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000705{
706 vcl_worker_t *wrk = vcl_worker_get_current ();
707 vcl_session_t *session;
708 vcl_session_state_t state;
709 u64 vpp_handle;
710
711 session = vcl_session_get_w_handle (wrk, session_handle);
712 if (PREDICT_FALSE (!session))
713 return VPPCOM_EBADFD;
714
715 vpp_handle = session->vpp_handle;
716 state = session->session_state;
717
718 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800719 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000720
721 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
722 {
723 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
724 return VPPCOM_EBADFD;
725 }
726
liuyacan55c952e2021-06-13 14:54:55 +0800727 if (how == SHUT_RD || how == SHUT_RDWR)
728 {
729 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
730 if (how == SHUT_RD)
731 return VPPCOM_OK;
732 }
733 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
734
liuyacan534468e2021-05-09 03:50:40 +0000735 if (PREDICT_TRUE (state == VCL_STATE_READY))
736 {
737 VDBG (1, "session %u [0x%llx]: sending shutdown...",
738 session->session_index, vpp_handle);
739
740 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000741 }
742
743 return VPPCOM_OK;
744}
745
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700746static int
747vppcom_session_disconnect (u32 session_handle)
748{
749 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700750 vcl_session_t *session, *listen_session;
751 vcl_session_state_t state;
752 u64 vpp_handle;
753
754 session = vcl_session_get_w_handle (wrk, session_handle);
755 if (!session)
756 return VPPCOM_EBADFD;
757
758 vpp_handle = session->vpp_handle;
759 state = session->session_state;
760
761 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800762 vpp_handle, state, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700763
764 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
765 {
766 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
767 return VPPCOM_EBADFD;
768 }
769
770 if (state == VCL_STATE_VPP_CLOSING)
771 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800772 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700773 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
774 session->session_index, vpp_handle);
775 }
776 else
777 {
778 /* Session doesn't have an event queue yet. Probably a non-blocking
779 * connect. Wait for the reply */
780 if (PREDICT_FALSE (!session->vpp_evt_q))
781 return VPPCOM_OK;
782
783 VDBG (1, "session %u [0x%llx]: sending disconnect...",
784 session->session_index, vpp_handle);
785 vcl_send_session_disconnect (wrk, session);
786 }
787
788 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
789 {
790 listen_session = vcl_session_get (wrk, session->listener_index);
791 listen_session->n_accepted_sessions--;
792 }
793
794 return VPPCOM_OK;
795}
796
Florin Coras30e79c22019-01-02 19:31:22 -0800797static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700798vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
799{
800 session_cleanup_msg_t *msg;
801 vcl_session_t *session;
802
803 msg = (session_cleanup_msg_t *) data;
804 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
805 if (!session)
806 {
807 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
808 return;
809 }
810
Florin Coras36d49392020-04-24 23:00:11 +0000811 if (msg->type == SESSION_CLEANUP_TRANSPORT)
812 {
813 /* Transport was cleaned up before we confirmed close. Probably the
814 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700815 * Confirm close to make sure everything is cleaned up.
816 * Move to undetermined state to ensure that the session is not
817 * removed before both vpp and the app cleanup.
818 * - If the app closes first, the session is moved to CLOSED state
819 * and the session cleanup notification from vpp removes the
820 * session.
821 * - If vpp cleans up the session first, the session is moved to
822 * DETACHED state lower and subsequently the close from the app
823 * frees the session
824 */
825 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700826 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700827 vppcom_session_disconnect (vcl_session_handle (session));
828 session->session_state = VCL_STATE_UPDATED;
829 }
830 else if (session->session_state == VCL_STATE_DISCONNECT)
831 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800832 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700833 session->session_state = VCL_STATE_UPDATED;
834 }
Florin Coras36d49392020-04-24 23:00:11 +0000835 return;
836 }
837
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800838 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700839 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800840
841 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700842 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000843 {
844 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700845 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000846 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
847 return;
848 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800849
850 /* Session probably tracked with epoll, disconnect not yet handled and
851 * 1) both transport and session cleanup completed 2) app closed. Wait
852 * until message is drained to free the session.
853 * See @ref vcl_handle_mq_event */
854 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
855 {
856 session->flags |= VCL_SESSION_F_PENDING_FREE;
857 return;
858 }
859
Florin Coras9ace36d2019-10-28 13:14:17 -0700860 vcl_session_free (wrk, session);
861}
862
863static void
Florin Coras30e79c22019-01-02 19:31:22 -0800864vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
865{
866 session_req_worker_update_msg_t *msg;
867 vcl_session_t *s;
868
869 msg = (session_req_worker_update_msg_t *) data;
870 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
871 if (!s)
872 return;
873
874 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
875}
876
877static void
878vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
879{
880 session_worker_update_reply_msg_t *msg;
881 vcl_session_t *s;
882
883 msg = (session_worker_update_reply_msg_t *) data;
884 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
885 if (!s)
886 {
887 VDBG (0, "unknown handle 0x%llx", msg->handle);
888 return;
889 }
Florin Coras30e79c22019-01-02 19:31:22 -0800890
Florin Coras5f45e012019-01-23 09:21:30 -0800891 if (s->rx_fifo)
892 {
Florin Corasc547e912020-12-08 17:50:45 -0800893 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700894 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800895 {
896 VDBG (0, "failed to attach fifos for %u", s->session_index);
897 return;
898 }
Florin Coras5f45e012019-01-23 09:21:30 -0800899 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700900 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800901
Florin Coras30e79c22019-01-02 19:31:22 -0800902 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
903 s->vpp_handle, wrk->wrk_index);
904}
905
Florin Coras935ce752020-09-08 22:43:47 -0700906static int
907vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
908{
909
910 if (vcm->cfg.vpp_app_socket_api)
911 return vcl_sapi_recv_fds (wrk, fds, n_fds);
912
913 return vcl_bapi_recv_fds (wrk, fds, n_fds);
914}
915
Florin Corasc4c4cf52019-08-24 18:17:34 -0700916static void
917vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
918{
919 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
920 session_app_add_segment_msg_t *msg;
921 u64 segment_handle;
922 int fd = -1;
923
924 msg = (session_app_add_segment_msg_t *) data;
925
926 if (msg->fd_flags)
927 {
Florin Coras935ce752020-09-08 22:43:47 -0700928 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700929 seg_type = SSVM_SEGMENT_MEMFD;
930 }
931
932 segment_handle = msg->segment_handle;
933 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
934 {
935 clib_warning ("invalid segment handle");
936 return;
937 }
938
939 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
940 seg_type, fd))
941 {
942 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
943 return;
944 }
945
946 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
947 msg->segment_size);
948}
949
950static void
951vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
952{
953 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
954 vcl_segment_detach (msg->segment_handle);
955 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
956}
957
Florin Coras40c07ce2020-07-16 20:46:17 -0700958static void
959vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
960{
961 if (!vcm->wrk_rpc_fn)
962 return;
963
hanlina3a48962020-07-13 11:09:15 +0800964 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700965}
966
Florin Coras04ae8272021-04-12 19:55:37 -0700967static void
968vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
969{
970 session_transport_attr_reply_msg_t *mp;
971
972 if (!wrk->session_attr_op)
973 return;
974
975 mp = (session_transport_attr_reply_msg_t *) data;
976
977 wrk->session_attr_op_rv = mp->retval;
978 wrk->session_attr_op = 0;
979 wrk->session_attr_rv = mp->attr;
980}
981
Florin Coras86f04502018-09-12 16:08:01 -0700982static int
983vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700984{
Florin Coras54693d22018-07-17 10:46:29 -0700985 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700986 session_connected_msg_t *connected_msg;
987 session_reset_msg_t *reset_msg;
988 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700989 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700990 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700991
992 switch (e->event_type)
993 {
Florin Coras653e43f2019-03-04 10:56:23 -0800994 case SESSION_IO_EVT_RX:
995 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700996 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -0700997 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -0800998 break;
Florin Coras86f04502018-09-12 16:08:01 -0700999 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001000 break;
Florin Corasb242d312020-10-26 15:35:40 -07001001 case SESSION_CTRL_EVT_BOUND:
1002 /* We can only wait for only one listen so not postponed */
1003 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1004 break;
Florin Coras54693d22018-07-17 10:46:29 -07001005 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001006 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1007 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1008 {
1009 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1010 *ecpy = *e;
1011 ecpy->postponed = 1;
1012 ecpy->session_index = s->session_index;
1013 }
Florin Coras54693d22018-07-17 10:46:29 -07001014 break;
1015 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001016 connected_msg = (session_connected_msg_t *) e->data;
1017 sid = vcl_session_connected_handler (wrk, connected_msg);
1018 if (!(s = vcl_session_get (wrk, sid)))
1019 break;
1020 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1021 {
1022 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1023 *ecpy = *e;
1024 ecpy->postponed = 1;
1025 ecpy->session_index = s->session_index;
1026 }
Florin Coras54693d22018-07-17 10:46:29 -07001027 break;
1028 case SESSION_CTRL_EVT_DISCONNECTED:
1029 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001030 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1031 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001032 if (s->session_state == VCL_STATE_CLOSED)
1033 break;
Florin Corasb242d312020-10-26 15:35:40 -07001034 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1035 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001036 s->session_state = VCL_STATE_VPP_CLOSING;
1037 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1038 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1039 *ecpy = *e;
1040 ecpy->postponed = 1;
1041 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001042 break;
1043 }
1044 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001045 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001046 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1047 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001048 break;
1049 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001050 reset_msg = (session_reset_msg_t *) e->data;
1051 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1052 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001053 if (s->session_state == VCL_STATE_CLOSED)
1054 break;
Florin Corasb242d312020-10-26 15:35:40 -07001055 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1056 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001057 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1058 s->session_state = VCL_STATE_DISCONNECT;
1059 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1060 *ecpy = *e;
1061 ecpy->postponed = 1;
1062 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001063 break;
1064 }
Florin Coras134a9962018-08-28 11:32:04 -07001065 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001066 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001067 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1068 vcl_session_unlisten_reply_handler (wrk, e->data);
1069 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001070 case SESSION_CTRL_EVT_MIGRATED:
1071 vcl_session_migrated_handler (wrk, e->data);
1072 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001073 case SESSION_CTRL_EVT_CLEANUP:
1074 vcl_session_cleanup_handler (wrk, e->data);
1075 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001076 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1077 vcl_session_req_worker_update_handler (wrk, e->data);
1078 break;
1079 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1080 vcl_session_worker_update_reply_handler (wrk, e->data);
1081 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001082 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1083 vcl_session_app_add_segment_handler (wrk, e->data);
1084 break;
1085 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1086 vcl_session_app_del_segment_handler (wrk, e->data);
1087 break;
hanlina3a48962020-07-13 11:09:15 +08001088 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001089 vcl_worker_rpc_handler (wrk, e->data);
1090 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001091 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1092 vcl_session_transport_attr_reply_handler (wrk, e->data);
1093 break;
Florin Coras54693d22018-07-17 10:46:29 -07001094 default:
1095 clib_warning ("unhandled %u", e->event_type);
1096 }
1097 return VPPCOM_OK;
1098}
1099
Florin Coras30e79c22019-01-02 19:31:22 -08001100static int
Florin Coras697faea2018-06-27 17:10:49 -07001101vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001102 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001103 f64 wait_for_time)
1104{
Florin Coras134a9962018-08-28 11:32:04 -07001105 vcl_worker_t *wrk = vcl_worker_get_current ();
1106 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001107 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001108 svm_msg_q_msg_t msg;
1109 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001110
Florin Coras697faea2018-06-27 17:10:49 -07001111 do
Dave Wallace543852a2017-08-03 02:11:34 -04001112 {
Florin Coras134a9962018-08-28 11:32:04 -07001113 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001114 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001115 {
Florin Coras070453d2018-08-24 17:04:27 -07001116 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001117 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001118 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001119 {
Florin Coras697faea2018-06-27 17:10:49 -07001120 return VPPCOM_OK;
1121 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001122 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001123 {
Florin Coras697faea2018-06-27 17:10:49 -07001124 return VPPCOM_ECONNREFUSED;
1125 }
Florin Coras54693d22018-07-17 10:46:29 -07001126
Florin Coras134a9962018-08-28 11:32:04 -07001127 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001128 {
1129 usleep (100);
1130 continue;
1131 }
Florin Coras134a9962018-08-28 11:32:04 -07001132 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001133 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001134 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001135 }
Florin Coras134a9962018-08-28 11:32:04 -07001136 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001137
Florin Coras05ecfcc2018-12-12 18:19:39 -08001138 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001139 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001140 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001141
Florin Coras697faea2018-06-27 17:10:49 -07001142 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001143}
1144
Florin Coras30e79c22019-01-02 19:31:22 -08001145static void
1146vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1147{
Florin Coras288eaab2019-02-03 15:26:14 -08001148 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001149 vcl_session_t *s;
1150 u32 *sip;
1151
1152 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1153 return;
1154
1155 vec_foreach (sip, wrk->pending_session_wrk_updates)
1156 {
1157 s = vcl_session_get (wrk, *sip);
1158 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1159 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001160 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1161 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001162 s->session_state = state;
1163 }
1164 vec_reset_length (wrk->pending_session_wrk_updates);
1165}
1166
Florin Corasf9240dc2019-01-15 08:03:17 -08001167void
Florin Coras5398dfb2021-01-25 20:31:27 -08001168vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001169{
Florin Coras30e79c22019-01-02 19:31:22 -08001170 svm_msg_q_msg_t *msg;
1171 session_event_t *e;
1172 svm_msg_q_t *mq;
1173 int i;
1174
1175 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001176 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001177
1178 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1179 {
1180 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1181 e = svm_msg_q_msg_data (mq, msg);
1182 vcl_handle_mq_event (wrk, e);
1183 svm_msg_q_free_msg (mq, msg);
1184 }
1185 vec_reset_length (wrk->mq_msg_vector);
1186 vcl_handle_pending_wrk_updates (wrk);
1187}
1188
Florin Coras5398dfb2021-01-25 20:31:27 -08001189void
1190vcl_flush_mq_events (void)
1191{
1192 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1193}
1194
Florin Coras697faea2018-06-27 17:10:49 -07001195static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001196vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001197{
Florin Coras134a9962018-08-28 11:32:04 -07001198 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001199 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001200 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001201 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001202
Florin Corasab2f6db2018-08-31 14:31:41 -07001203 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001204 if (!session)
1205 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001206
Florin Corasaaff5ee2019-07-08 13:00:00 -07001207 /* Flush pending accept events, if any */
1208 while (clib_fifo_elts (session->accept_evts_fifo))
1209 {
1210 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1211 accepted_msg = &evt->accepted_msg;
1212 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1213 vcl_send_session_accepted_reply (session->vpp_evt_q,
1214 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001215 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001216 }
1217 clib_fifo_free (session->accept_evts_fifo);
1218
Florin Coras458089b2019-08-21 16:20:44 -07001219 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001220
Florin Coras5e062572019-03-14 19:07:51 -07001221 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001222 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001223 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001224
1225 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001226 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001227
Florin Coras070453d2018-08-24 17:04:27 -07001228 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001229}
1230
Florin Coras940f78f2018-11-30 12:11:20 -08001231/**
1232 * Handle app exit
1233 *
1234 * Notify vpp of the disconnect and mark the worker as free. If we're the
1235 * last worker, do a full cleanup otherwise, since we're probably a forked
1236 * child, avoid syscalls as much as possible. We might've lost privileges.
1237 */
1238void
1239vppcom_app_exit (void)
1240{
1241 if (!pool_elts (vcm->workers))
1242 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001243 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1244 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001245 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001246}
1247
Florin Coras935ce752020-09-08 22:43:47 -07001248static int
1249vcl_api_attach (void)
1250{
1251 if (vcm->cfg.vpp_app_socket_api)
1252 return vcl_sapi_attach ();
1253
1254 return vcl_bapi_attach ();
1255}
1256
1257static void
1258vcl_api_detach (vcl_worker_t * wrk)
1259{
1260 vcl_send_app_detach (wrk);
1261
1262 if (vcm->cfg.vpp_app_socket_api)
1263 return vcl_sapi_detach (wrk);
1264
1265 return vcl_bapi_disconnect_from_vpp ();
1266}
1267
Dave Wallace543852a2017-08-03 02:11:34 -04001268/*
1269 * VPPCOM Public API functions
1270 */
1271int
Florin Coras66ec4672020-06-15 07:59:40 -07001272vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001273{
Dave Wallace543852a2017-08-03 02:11:34 -04001274 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001275 int rv;
1276
Florin Coras47c40e22018-11-26 17:01:36 -08001277 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001278 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001279 VDBG (1, "already initialized");
1280 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001281 }
1282
Florin Coras47c40e22018-11-26 17:01:36 -08001283 vcm->is_init = 1;
1284 vppcom_cfg (&vcm->cfg);
1285 vcl_cfg = &vcm->cfg;
1286
1287 vcm->main_cpu = pthread_self ();
1288 vcm->main_pid = getpid ();
1289 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001290 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1291 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001292 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1293 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001294 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001295 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001296 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001297
1298 /* Allocate default worker */
1299 vcl_worker_alloc_and_init ();
1300
Florin Coras935ce752020-09-08 22:43:47 -07001301 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001302 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001303
1304 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001305 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001306
1307 return VPPCOM_OK;
1308}
1309
1310void
1311vppcom_app_destroy (void)
1312{
Florin Corasdc0ded72020-04-30 02:59:55 +00001313 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001314 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001315
Florin Coras940f78f2018-11-30 12:11:20 -08001316 if (!pool_elts (vcm->workers))
1317 return;
1318
Florin Coras0d427d82018-06-27 03:24:07 -07001319 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001320
Florin Corasdc0ded72020-04-30 02:59:55 +00001321 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001322
Florin Corasce815de2020-04-16 18:47:27 +00001323 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001324 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001325 if (current_wrk != wrk)
1326 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001327 }
Florin Corasce815de2020-04-16 18:47:27 +00001328 /* *INDENT-ON* */
1329
Florin Coras935ce752020-09-08 22:43:47 -07001330 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001331 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1332
Florin Coras0d427d82018-06-27 03:24:07 -07001333 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001334
1335 /*
1336 * Free the heap and fix vcm
1337 */
1338 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001339 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001340
1341 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001342 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001343}
1344
1345int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001346vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001347{
Florin Coras134a9962018-08-28 11:32:04 -07001348 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001349 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001350
Florin Coras134a9962018-08-28 11:32:04 -07001351 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001352
Florin Coras7e12d942018-06-27 14:32:43 -07001353 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001354 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001355 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001356 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001357
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001358 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001359 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001360
Florin Coras7e12d942018-06-27 14:32:43 -07001361 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1362 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001363
Florin Coras5e062572019-03-14 19:07:51 -07001364 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001365
Florin Coras134a9962018-08-28 11:32:04 -07001366 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001367}
1368
Florin Corasfe286f72021-06-04 10:07:55 -07001369static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001370vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001371{
Florin Corasfa3884f2021-06-22 20:04:46 -07001372 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001373
Florin Corasfa3884f2021-06-22 20:04:46 -07001374 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001375 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001376 wrk->ep_lt_current = s->session_index;
1377 s->vep.lt_next = s->session_index;
1378 s->vep.lt_prev = s->session_index;
1379 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001380 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001381
1382 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1383 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1384
1385 prev->vep.lt_next = s->session_index;
1386 s->vep.lt_prev = prev->session_index;
1387
1388 s->vep.lt_next = cur->session_index;
1389 cur->vep.lt_prev = s->session_index;
1390}
1391
1392static void
1393vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1394{
1395 vcl_session_t *prev, *next;
1396
1397 if (s->vep.lt_next == s->session_index)
1398 {
1399 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1400 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1401 return;
1402 }
1403
1404 prev = vcl_session_get (wrk, s->vep.lt_prev);
1405 next = vcl_session_get (wrk, s->vep.lt_next);
1406
1407 prev->vep.lt_next = next->session_index;
1408 next->vep.lt_prev = prev->session_index;
1409
1410 if (s->session_index == wrk->ep_lt_current)
1411 wrk->ep_lt_current = s->vep.lt_next;
1412
1413 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001414}
1415
Dave Wallace543852a2017-08-03 02:11:34 -04001416int
Florin Corasc127d5a2020-10-14 16:35:58 -07001417vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001418 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001419{
Florin Coras134a9962018-08-28 11:32:04 -07001420 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001421
Florin Coras6c3b2182020-10-19 18:36:48 -07001422 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001423
Florin Coras6c3b2182020-10-19 18:36:48 -07001424 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001425 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001426 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001427 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001428 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001429 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001430 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001431 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001432 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1433 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001434 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001435 }
Florin Corase4306b62020-10-28 12:51:10 -07001436 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001437 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001438
Florin Coras6c3b2182020-10-19 18:36:48 -07001439 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001440 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001441 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001442 if (rv < 0)
1443 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001444 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1445 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001446 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001447
Florin Coras9ace36d2019-10-28 13:14:17 -07001448 if (!do_disconnect)
1449 {
1450 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001451 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001452 goto cleanup;
1453 }
1454
Florin Coras6c3b2182020-10-19 18:36:48 -07001455 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001456 {
1457 rv = vppcom_session_unbind (sh);
1458 if (PREDICT_FALSE (rv < 0))
1459 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001460 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001461 vppcom_retval_str (rv));
1462 return rv;
1463 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001464 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001465 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001466 {
1467 rv = vppcom_session_disconnect (sh);
1468 if (PREDICT_FALSE (rv < 0))
1469 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001470 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001471 rv, vppcom_retval_str (rv));
1472 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001473 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001474 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001475 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001476 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001477 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001478 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001479 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001480
1481 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1482 goto free_session;
1483
1484 /* Disconnect/reset messages pending but vpp transport and session
1485 * cleanups already done. Free only after messages drained. */
1486 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001487 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001488
Florin Corasc127d5a2020-10-14 16:35:58 -07001489 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001490
Florin Coras9ace36d2019-10-28 13:14:17 -07001491 /* Session is removed only after vpp confirms the disconnect */
1492 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001493
Florin Corasf9240dc2019-01-15 08:03:17 -08001494cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001495 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001496free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001497 vcl_session_free (wrk, s);
1498 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001499
Dave Wallace543852a2017-08-03 02:11:34 -04001500 return rv;
1501}
1502
1503int
Florin Corasf9240dc2019-01-15 08:03:17 -08001504vppcom_session_close (uint32_t session_handle)
1505{
1506 vcl_worker_t *wrk = vcl_worker_get_current ();
1507 vcl_session_t *session;
1508
1509 session = vcl_session_get_w_handle (wrk, session_handle);
1510 if (!session)
1511 return VPPCOM_EBADFD;
1512 return vcl_session_cleanup (wrk, session, session_handle,
1513 1 /* do_disconnect */ );
1514}
1515
1516int
Florin Coras134a9962018-08-28 11:32:04 -07001517vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001518{
Florin Coras134a9962018-08-28 11:32:04 -07001519 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001520 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001521
1522 if (!ep || !ep->ip)
1523 return VPPCOM_EINVAL;
1524
Florin Coras134a9962018-08-28 11:32:04 -07001525 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001526 if (!session)
1527 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001528
Florin Coras6c3b2182020-10-19 18:36:48 -07001529 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001530 {
Florin Coras5e062572019-03-14 19:07:51 -07001531 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1532 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001533 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001534 }
1535
Florin Coras7e12d942018-06-27 14:32:43 -07001536 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001537 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001538 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1539 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001540 else
Dave Barach178cf492018-11-13 16:34:13 -05001541 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1542 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001543 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001544
Florin Coras60687192021-11-29 21:00:47 -08001545 VDBG (0,
1546 "session %u handle %u: binding to local %s address %U port %u, "
1547 "proto %s",
1548 session->session_index, session_handle,
1549 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1550 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001551 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1552 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001553 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001554 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001555
1556 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001557 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001558
Florin Coras070453d2018-08-24 17:04:27 -07001559 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001560}
1561
1562int
Florin Coras134a9962018-08-28 11:32:04 -07001563vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001564{
Florin Coras134a9962018-08-28 11:32:04 -07001565 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001566 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001567 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001568 int rv;
1569
Florin Coras134a9962018-08-28 11:32:04 -07001570 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001571 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001572 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001573
Dave Wallaceee45d412017-11-24 21:44:06 -05001574 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001575 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001576 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001577 VDBG (0, "session %u [0x%llx]: already in listen state!",
1578 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001579 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001580 }
1581
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001582 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001583
Florin Coras070453d2018-08-24 17:04:27 -07001584 /*
1585 * Send listen request to vpp and wait for reply
1586 */
Florin Coras458089b2019-08-21 16:20:44 -07001587 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001588 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001589 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001590 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001591
Florin Coras070453d2018-08-24 17:04:27 -07001592 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001593 {
Florin Coras134a9962018-08-28 11:32:04 -07001594 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001595 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1596 listen_sh, listen_session->vpp_handle, rv,
1597 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001598 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001599 }
1600
Florin Coras070453d2018-08-24 17:04:27 -07001601 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001602}
1603
Florin Coras134a9962018-08-28 11:32:04 -07001604static int
Florin Coras5e062572019-03-14 19:07:51 -07001605validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001606{
Florin Coras6c3b2182020-10-19 18:36:48 -07001607 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001608 {
Florin Coras5e062572019-03-14 19:07:51 -07001609 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1610 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001611 return VPPCOM_EBADFD;
1612 }
1613
Florin Corasc127d5a2020-10-14 16:35:58 -07001614 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001615 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001616 {
Florin Coras60687192021-11-29 21:00:47 -08001617 VDBG (0,
1618 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1619 " (%s)",
1620 ls->vpp_handle, ls->session_state,
1621 vcl_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001622 return VPPCOM_EBADFD;
1623 }
1624 return VPPCOM_OK;
1625}
1626
1627int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001628vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1629{
1630 if (!strcmp (proto_str, "TCP"))
1631 *proto = VPPCOM_PROTO_TCP;
1632 else if (!strcmp (proto_str, "tcp"))
1633 *proto = VPPCOM_PROTO_TCP;
1634 else if (!strcmp (proto_str, "UDP"))
1635 *proto = VPPCOM_PROTO_UDP;
1636 else if (!strcmp (proto_str, "udp"))
1637 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001638 else if (!strcmp (proto_str, "TLS"))
1639 *proto = VPPCOM_PROTO_TLS;
1640 else if (!strcmp (proto_str, "tls"))
1641 *proto = VPPCOM_PROTO_TLS;
1642 else if (!strcmp (proto_str, "QUIC"))
1643 *proto = VPPCOM_PROTO_QUIC;
1644 else if (!strcmp (proto_str, "quic"))
1645 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001646 else if (!strcmp (proto_str, "DTLS"))
1647 *proto = VPPCOM_PROTO_DTLS;
1648 else if (!strcmp (proto_str, "dtls"))
1649 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001650 else if (!strcmp (proto_str, "SRTP"))
1651 *proto = VPPCOM_PROTO_SRTP;
1652 else if (!strcmp (proto_str, "srtp"))
1653 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001654 else
1655 return 1;
1656 return 0;
1657}
1658
1659int
Florin Coras134a9962018-08-28 11:32:04 -07001660vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001661 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001662{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001663 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001664 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001665 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001666 vcl_session_t *listen_session = 0;
1667 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001668 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001669 u8 is_nonblocking;
1670 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001671
Florin Coras5398dfb2021-01-25 20:31:27 -08001672again:
1673
Florin Coras134a9962018-08-28 11:32:04 -07001674 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001675 if (!listen_session)
1676 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001677
Florin Coras134a9962018-08-28 11:32:04 -07001678 listen_session_index = listen_session->session_index;
1679 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001680 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001681
Florin Coras54693d22018-07-17 10:46:29 -07001682 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001683 {
Florin Coras54693d22018-07-17 10:46:29 -07001684 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001685 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001686 accepted_msg = evt->accepted_msg;
1687 goto handle;
1688 }
1689
Florin Corasac422d62020-10-19 20:51:36 -07001690 is_nonblocking = vcl_session_has_attr (listen_session,
1691 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001692 while (1)
1693 {
Carl Smith592a9092019-11-12 14:57:37 +13001694 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1695 return VPPCOM_EAGAIN;
1696
Florin Coras5398dfb2021-01-25 20:31:27 -08001697 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1698 vcl_worker_flush_mq_events (wrk);
1699 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001700 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001701
Florin Coras54693d22018-07-17 10:46:29 -07001702handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001703
Florin Coras00cca802019-06-06 09:38:44 -07001704 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1705 listen_session_index);
1706 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1707 return VPPCOM_ECONNABORTED;
1708
Florin Coras134a9962018-08-28 11:32:04 -07001709 listen_session = vcl_session_get (wrk, listen_session_index);
1710 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001711
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001712 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001713 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001714
Florin Coras5e062572019-03-14 19:07:51 -07001715 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1716 " flags %d, is_nonblocking %u", listen_session->session_index,
1717 listen_session->vpp_handle, client_session_index,
1718 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001719 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001720
Dave Wallace048b1d62018-01-03 22:24:41 -05001721 if (ep)
1722 {
Florin Coras7e12d942018-06-27 14:32:43 -07001723 ep->is_ip4 = client_session->transport.is_ip4;
1724 ep->port = client_session->transport.rmt_port;
1725 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001726 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1727 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001728 else
Dave Barach178cf492018-11-13 16:34:13 -05001729 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1730 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001731 }
Dave Wallace60caa062017-11-10 17:07:13 -05001732
Florin Coras60687192021-11-29 21:00:47 -08001733 VDBG (0,
1734 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1735 "local: %U:%u",
1736 listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001737 client_session_index, client_session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001738 vcl_format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001739 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001740 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001741 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001742 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001743 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001744 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1745 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001746
Florin Coras3c7d4f92018-12-14 11:28:43 -08001747 /*
1748 * Session might have been closed already
1749 */
1750 if (accept_flags)
1751 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001752 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001753 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001754 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001755 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001756 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001757 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001758}
1759
1760int
Florin Coras134a9962018-08-28 11:32:04 -07001761vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001762{
Florin Coras134a9962018-08-28 11:32:04 -07001763 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001764 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001765 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001766 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001767
Florin Coras134a9962018-08-28 11:32:04 -07001768 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001769 if (!session)
1770 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001771 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001772
Florin Coras6c3b2182020-10-19 18:36:48 -07001773 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001774 {
Florin Coras5e062572019-03-14 19:07:51 -07001775 VDBG (0, "ERROR: cannot connect epoll session %u!",
1776 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001777 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001778 }
1779
Florin Corasc127d5a2020-10-14 16:35:58 -07001780 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001781 {
Florin Coras60687192021-11-29 21:00:47 -08001782 VDBG (0,
1783 "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001784 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001785 session_handle, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001786 session->transport.is_ip4 ? "IPv4" : "IPv6",
1787 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),
Ping Yu34a3a082018-11-30 19:16:17 -05001790 vppcom_proto_str (session->session_type), session->session_state,
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 Coras60687192021-11-29 21:00:47 -08001810 VDBG (0,
1811 "session handle %u (%s): connecting to peer %s %U "
1812 "port %d proto %s",
1813 session_handle, vcl_session_state_str (session->session_state),
1814 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1815 &session->transport.rmt_ip,
1816 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001817 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001818 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001819
Florin Coras458089b2019-08-21 16:20:44 -07001820 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001821
Florin Corasac422d62020-10-19 20:51:36 -07001822 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001823 {
fanyfa49d59d2020-10-13 17:07:16 +08001824 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001825 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001826 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001827 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001828 return VPPCOM_EINPROGRESS;
1829 }
Florin Coras57c88932019-08-29 12:03:17 -07001830
1831 /*
1832 * Wait for reply from vpp if blocking
1833 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001834 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001835 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001836
Florin Coras134a9962018-08-28 11:32:04 -07001837 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001838 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1839 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001840
Dave Wallace4878cbe2017-11-21 03:45:09 -05001841 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001842}
1843
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001844int
1845vppcom_session_stream_connect (uint32_t session_handle,
1846 uint32_t parent_session_handle)
1847{
1848 vcl_worker_t *wrk = vcl_worker_get_current ();
1849 vcl_session_t *session, *parent_session;
1850 u32 session_index, parent_session_index;
1851 int rv;
1852
1853 session = vcl_session_get_w_handle (wrk, session_handle);
1854 if (!session)
1855 return VPPCOM_EBADFD;
1856 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1857 if (!parent_session)
1858 return VPPCOM_EBADFD;
1859
1860 session_index = session->session_index;
1861 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001862 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001863 {
1864 VDBG (0, "ERROR: cannot connect epoll session %u!",
1865 session->session_index);
1866 return VPPCOM_EBADFD;
1867 }
1868
Florin Corasc127d5a2020-10-14 16:35:58 -07001869 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001870 {
Florin Coras60687192021-11-29 21:00:47 -08001871 VDBG (0,
1872 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001873 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001874 session_handle, session->vpp_handle, parent_session_handle,
1875 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001876 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001877 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001878 return VPPCOM_OK;
1879 }
1880
1881 /* Connect to quic session specifics */
1882 session->transport.is_ip4 = parent_session->transport.is_ip4;
1883 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1884 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001885 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001886
1887 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1888 session_handle, parent_session_handle, parent_session->vpp_handle);
1889
1890 /*
1891 * Send connect request and wait for reply from vpp
1892 */
Florin Coras458089b2019-08-21 16:20:44 -07001893 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001894 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001895 vcm->cfg.session_timeout);
1896
1897 session->listener_index = parent_session_index;
1898 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001899 if (parent_session)
1900 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001901
1902 session = vcl_session_get (wrk, session_index);
1903 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1904 session->vpp_handle, rv ? "failed" : "succeeded");
1905
1906 return rv;
1907}
1908
Steven58f464e2017-10-25 12:33:12 -07001909static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001910vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001911 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001912{
Florin Coras134a9962018-08-28 11:32:04 -07001913 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001914 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001915 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001916 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001917 session_event_t *e;
1918 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001919 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001920
Florin Coras070453d2018-08-24 17:04:27 -07001921 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001922 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001923
Florin Coras134a9962018-08-28 11:32:04 -07001924 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001925 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001926 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001927
Florin Coras6d0106e2019-01-29 20:11:58 -08001928 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001929 {
Florin Coras5e062572019-03-14 19:07:51 -07001930 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001931 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001932 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08001933 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001934 }
1935
liuyacan55c952e2021-06-13 14:54:55 +08001936 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
1937 {
1938 /* Vpp would ack the incoming data and enqueue it for reading.
1939 * So even if SHUT_RD is set, we can still read() the data if
1940 * the session is ready.
1941 */
1942 if (!vcl_session_read_ready (s))
1943 {
1944 return 0;
1945 }
1946 }
1947
Florin Corasac422d62020-10-19 20:51:36 -07001948 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001949 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001950 mq = wrk->app_event_queue;
1951 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001952 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001953
Sirshak Das28aa5392019-02-05 01:33:33 -06001954 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001955 {
Florin Coras54693d22018-07-17 10:46:29 -07001956 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001957 {
Yu Pingb2955352019-12-04 06:49:04 +08001958 if (vcl_session_is_closing (s))
1959 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001960 if (is_ct)
1961 svm_fifo_unset_event (s->rx_fifo);
1962 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001963 return VPPCOM_EWOULDBLOCK;
1964 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001965 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001966 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001967 if (vcl_session_is_closing (s))
1968 return vcl_session_closing_error (s);
1969
Florin Corasd6894562020-10-28 00:37:15 -07001970 if (is_ct)
1971 svm_fifo_unset_event (s->rx_fifo);
1972 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001973
Florin Coras5398dfb2021-01-25 20:31:27 -08001974 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1975 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001976 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001977 }
Florin Coras54693d22018-07-17 10:46:29 -07001978
Florin Coras4a2c7942020-09-10 12:27:14 -07001979read_again:
1980
Florin Coras460dce62018-07-27 05:45:06 -07001981 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001982 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001983 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001984 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1985
1986 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07001987
1988 if (peek)
1989 return rv;
1990
Florin Coras4a2c7942020-09-10 12:27:14 -07001991 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001992
Sirshak Das28aa5392019-02-05 01:33:33 -06001993 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001994 {
Florin Corasd6894562020-10-28 00:37:15 -07001995 if (is_ct)
1996 svm_fifo_unset_event (s->rx_fifo);
1997 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001998 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001999 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002000 {
Florin Corasc34118b2020-08-12 18:58:25 -07002001 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2002 e->event_type = SESSION_IO_EVT_RX;
2003 e->session_index = s->session_index;
2004 }
2005 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002006 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002007 {
2008 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002009 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002010 buf += rv;
2011 n -= rv;
2012 goto read_again;
2013 }
Florin Coras41c9e042018-09-11 00:10:41 -07002014
Florin Coras17ec5772020-08-12 20:46:29 -07002015 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002016 {
Florin Coras17ec5772020-08-12 20:46:29 -07002017 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002018 app_send_io_evt_to_vpp (s->vpp_evt_q,
2019 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002020 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002021 }
2022
Florin Coras5e062572019-03-14 19:07:51 -07002023 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2024 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002025
Florin Coras54693d22018-07-17 10:46:29 -07002026 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002027}
2028
Steven58f464e2017-10-25 12:33:12 -07002029int
Florin Coras134a9962018-08-28 11:32:04 -07002030vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002031{
Florin Coras134a9962018-08-28 11:32:04 -07002032 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002033}
2034
2035static int
Florin Coras134a9962018-08-28 11:32:04 -07002036vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002037{
Florin Coras134a9962018-08-28 11:32:04 -07002038 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002039}
2040
Florin Coras2cba8532018-09-11 16:33:36 -07002041int
2042vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002043 vppcom_data_segment_t * ds, uint32_t n_segments,
2044 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002045{
2046 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002047 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002048 vcl_session_t *s = 0;
2049 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002050 svm_msg_q_t *mq;
2051 u8 is_ct;
2052
2053 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002054 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002055 return VPPCOM_EBADFD;
2056
Florin Coras6d0106e2019-01-29 20:11:58 -08002057 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2058 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002059
Florin Corasac422d62020-10-19 20:51:36 -07002060 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002061 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002062 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002063 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002064 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002065
Sirshak Das28aa5392019-02-05 01:33:33 -06002066 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002067 {
2068 if (is_nonblocking)
2069 {
Florin Coras62877022020-10-28 21:22:04 -07002070 if (is_ct)
2071 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002072 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002073 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002074 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002075 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002076 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002077 if (vcl_session_is_closing (s))
2078 return vcl_session_closing_error (s);
2079
Florin Coras62877022020-10-28 21:22:04 -07002080 if (is_ct)
2081 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002082 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002083
Florin Coras5398dfb2021-01-25 20:31:27 -08002084 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2085 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002086 }
2087 }
2088
Florin Corasd1cc38d2020-10-11 11:05:04 -07002089 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2090 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2091 if (n_read < 0)
2092 return VPPCOM_EAGAIN;
2093
2094 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2095 {
Florin Coras62877022020-10-28 21:22:04 -07002096 if (is_ct)
2097 svm_fifo_unset_event (s->rx_fifo);
2098 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002099 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002100 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002101 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002102 {
2103 session_event_t *e;
2104 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2105 e->event_type = SESSION_IO_EVT_RX;
2106 e->session_index = s->session_index;
2107 }
2108 }
2109
2110 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002111 return n_read;
2112}
2113
2114void
Florin Corasd68faf82020-09-25 15:18:13 -07002115vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002116{
2117 vcl_worker_t *wrk = vcl_worker_get_current ();
2118 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002119 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002120
2121 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002122 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002123 return;
2124
Florin Coras62877022020-10-28 21:22:04 -07002125 is_ct = vcl_session_is_ct (s);
2126 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002127
Florin Coras8cc93212021-09-10 11:37:12 -07002128 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002129 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002130}
2131
Florin Coras7a2abce2020-04-05 19:25:44 +00002132always_inline u8
2133vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002134{
Florin Coras7a2abce2020-04-05 19:25:44 +00002135 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2136 if (is_dgram)
2137 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2138 else
2139 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002140}
2141
2142always_inline int
2143vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2144 size_t n, u8 is_flush, u8 is_dgram)
2145{
Florin Coras6d0106e2019-01-29 20:11:58 -08002146 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002147 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002148 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002149 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002150 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002151
Florin Coras0b0d28e2021-06-04 17:31:53 -07002152 /* Accept zero length writes but just return */
2153 if (PREDICT_FALSE (!n))
2154 return VPPCOM_OK;
2155
2156 if (PREDICT_FALSE (!buf))
2157 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002158
Florin Coras6c3b2182020-10-19 18:36:48 -07002159 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002160 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002161 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2162 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002163 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002164 }
2165
liuyacan55c952e2021-06-13 14:54:55 +08002166 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002167 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002168 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2169 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002170 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002171 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002172 }
2173
liuyacan55c952e2021-06-13 14:54:55 +08002174 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2175 {
2176 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2177 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002178 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002179 return VPPCOM_EPIPE;
2180 }
2181
Florin Coras0e88e852018-09-17 22:09:02 -07002182 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002183 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002184 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002185
Florin Coras653e43f2019-03-04 10:56:23 -08002186 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002187 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002188 {
Florin Coras54693d22018-07-17 10:46:29 -07002189 if (is_nonblocking)
2190 {
Florin Coras070453d2018-08-24 17:04:27 -07002191 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002192 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002193 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002194 {
Florin Coras2d379d82019-06-28 12:45:12 -07002195 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002196 if (vcl_session_is_closing (s))
2197 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002198
Florin Coras5398dfb2021-01-25 20:31:27 -08002199 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2200 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002201 }
Dave Wallace543852a2017-08-03 02:11:34 -04002202 }
Dave Wallace543852a2017-08-03 02:11:34 -04002203
Florin Coras653e43f2019-03-04 10:56:23 -08002204 et = SESSION_IO_EVT_TX;
2205 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002206 et = SESSION_IO_EVT_TX_FLUSH;
2207
Florin Coras7a2abce2020-04-05 19:25:44 +00002208 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002209 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002210 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002211 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002212 else
2213 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002214 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002215
Florin Coras14ed6df2019-03-06 21:13:42 -08002216 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002217 app_send_io_evt_to_vpp (
2218 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002219
Florin Coras56230092020-09-02 20:52:58 -07002220 /* The underlying fifo segment can run out of memory */
2221 if (PREDICT_FALSE (n_write < 0))
2222 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002223
Florin Coras6d0106e2019-01-29 20:11:58 -08002224 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2225 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002226
Florin Coras54693d22018-07-17 10:46:29 -07002227 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002228}
2229
Florin Coras42ceddb2018-12-12 10:56:01 -08002230int
2231vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2232{
Florin Coras7a2abce2020-04-05 19:25:44 +00002233 vcl_worker_t *wrk = vcl_worker_get_current ();
2234 vcl_session_t *s;
2235
2236 s = vcl_session_get_w_handle (wrk, session_handle);
2237 if (PREDICT_FALSE (!s))
2238 return VPPCOM_EBADFD;
2239
2240 return vppcom_session_write_inline (wrk, s, buf, n,
2241 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002242}
2243
Florin Corasb0f662f2018-12-27 14:51:46 -08002244int
2245vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2246{
Florin Coras7a2abce2020-04-05 19:25:44 +00002247 vcl_worker_t *wrk = vcl_worker_get_current ();
2248 vcl_session_t *s;
2249
2250 s = vcl_session_get_w_handle (wrk, session_handle);
2251 if (PREDICT_FALSE (!s))
2252 return VPPCOM_EBADFD;
2253
2254 return vppcom_session_write_inline (wrk, s, buf, n,
2255 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002256}
2257
Florin Corasc0737e92019-03-04 14:19:39 -08002258#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002259if (PREDICT_FALSE (!_s->rx_fifo)) \
2260 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002261if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2262 { \
2263 if (!vcl_session_is_ct (_s)) \
2264 { \
2265 svm_fifo_unset_event (_s->rx_fifo); \
2266 if (svm_fifo_is_empty (_s->rx_fifo)) \
2267 break; \
2268 } \
2269 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2270 { \
Florin Coras2f630182020-08-13 00:17:51 -07002271 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002272 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2273 break; \
2274 } \
2275 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002276
Florin Coras86f04502018-09-12 16:08:01 -07002277static void
2278vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2279 unsigned long n_bits, unsigned long *read_map,
2280 unsigned long *write_map,
2281 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002282{
2283 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002284 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002285 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002286 u32 sid;
2287
2288 switch (e->event_type)
2289 {
Florin Corasc0737e92019-03-04 14:19:39 -08002290 case SESSION_IO_EVT_RX:
2291 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002292 s = vcl_session_get (wrk, sid);
2293 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002294 break;
Florin Corasb242d312020-10-26 15:35:40 -07002295 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002296 if (sid < n_bits && read_map)
2297 {
David Johnsond9818dd2018-12-14 14:53:41 -05002298 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002299 *bits_set += 1;
2300 }
2301 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002302 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002303 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002304 s = vcl_session_get (wrk, sid);
2305 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002306 break;
2307 if (sid < n_bits && write_map)
2308 {
David Johnsond9818dd2018-12-14 14:53:41 -05002309 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002310 *bits_set += 1;
2311 }
2312 break;
Florin Coras86f04502018-09-12 16:08:01 -07002313 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002314 if (!e->postponed)
2315 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2316 else
2317 s = vcl_session_get (wrk, e->session_index);
2318 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002319 break;
Florin Corasb242d312020-10-26 15:35:40 -07002320 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002321 if (sid < n_bits && read_map)
2322 {
David Johnsond9818dd2018-12-14 14:53:41 -05002323 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002324 *bits_set += 1;
2325 }
2326 break;
2327 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002328 if (!e->postponed)
2329 {
2330 connected_msg = (session_connected_msg_t *) e->data;
2331 sid = vcl_session_connected_handler (wrk, connected_msg);
2332 }
2333 else
2334 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002335 if (sid == VCL_INVALID_SESSION_INDEX)
2336 break;
2337 if (sid < n_bits && write_map)
2338 {
2339 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2340 *bits_set += 1;
2341 }
Florin Coras86f04502018-09-12 16:08:01 -07002342 break;
2343 case SESSION_CTRL_EVT_DISCONNECTED:
2344 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002345 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2346 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002347 break;
Florin Corasb242d312020-10-26 15:35:40 -07002348 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002349 if (sid < n_bits && except_map)
2350 {
David Johnsond9818dd2018-12-14 14:53:41 -05002351 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002352 *bits_set += 1;
2353 }
2354 break;
2355 case SESSION_CTRL_EVT_RESET:
2356 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2357 if (sid < n_bits && except_map)
2358 {
David Johnsond9818dd2018-12-14 14:53:41 -05002359 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002360 *bits_set += 1;
2361 }
2362 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002363 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2364 vcl_session_unlisten_reply_handler (wrk, e->data);
2365 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002366 case SESSION_CTRL_EVT_MIGRATED:
2367 vcl_session_migrated_handler (wrk, e->data);
2368 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002369 case SESSION_CTRL_EVT_CLEANUP:
2370 vcl_session_cleanup_handler (wrk, e->data);
2371 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002372 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2373 vcl_session_worker_update_reply_handler (wrk, e->data);
2374 break;
2375 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2376 vcl_session_req_worker_update_handler (wrk, e->data);
2377 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002378 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2379 vcl_session_app_add_segment_handler (wrk, e->data);
2380 break;
2381 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2382 vcl_session_app_del_segment_handler (wrk, e->data);
2383 break;
hanlina3a48962020-07-13 11:09:15 +08002384 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002385 vcl_worker_rpc_handler (wrk, e->data);
2386 break;
Florin Coras86f04502018-09-12 16:08:01 -07002387 default:
2388 clib_warning ("unhandled: %u", e->event_type);
2389 break;
2390 }
2391}
2392
2393static int
2394vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2395 unsigned long n_bits, unsigned long *read_map,
2396 unsigned long *write_map, unsigned long *except_map,
2397 double time_to_wait, u32 * bits_set)
2398{
Florin Coras99368312018-08-02 10:45:44 -07002399 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002400 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002401 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002402
Florin Coras54693d22018-07-17 10:46:29 -07002403 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002404 {
Florin Coras54693d22018-07-17 10:46:29 -07002405 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002406 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002407
Florin Coras54693d22018-07-17 10:46:29 -07002408 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002409 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002410 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002411 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002412 else
2413 {
2414 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002415 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002416 }
2417 }
Florin Corase003a1b2019-06-05 10:47:16 -07002418 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002419
Florin Coras134a9962018-08-28 11:32:04 -07002420 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002421 {
Florin Coras134a9962018-08-28 11:32:04 -07002422 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002423 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002424 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2425 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002426 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002427 }
Florin Coras134a9962018-08-28 11:32:04 -07002428 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002429 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002430 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002431}
2432
Florin Coras99368312018-08-02 10:45:44 -07002433static int
Florin Coras294afe22019-01-07 17:49:17 -08002434vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2435 vcl_si_set * read_map, vcl_si_set * write_map,
2436 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002437 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002438{
Florin Coras14ed6df2019-03-06 21:13:42 -08002439 double wait = 0, start = 0;
2440
2441 if (!*bits_set)
2442 {
2443 wait = time_to_wait;
2444 start = clib_time_now (&wrk->clib_time);
2445 }
2446
2447 do
2448 {
2449 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2450 write_map, except_map, wait, bits_set);
2451 if (*bits_set)
2452 return *bits_set;
2453 if (wait == -1)
2454 continue;
2455
2456 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2457 }
2458 while (wait > 0);
2459
2460 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002461}
2462
2463static int
Florin Coras294afe22019-01-07 17:49:17 -08002464vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2465 vcl_si_set * read_map, vcl_si_set * write_map,
2466 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002467 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002468{
2469 vcl_mq_evt_conn_t *mqc;
2470 int __clib_unused n_read;
2471 int n_mq_evts, i;
2472 u64 buf;
2473
Florin Coras134a9962018-08-28 11:32:04 -07002474 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2475 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2476 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002477 for (i = 0; i < n_mq_evts; i++)
2478 {
Florin Coras134a9962018-08-28 11:32:04 -07002479 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002480 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002481 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002482 except_map, 0, bits_set);
2483 }
2484
2485 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2486}
2487
Dave Wallace543852a2017-08-03 02:11:34 -04002488int
Florin Coras294afe22019-01-07 17:49:17 -08002489vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2490 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002491{
Florin Coras54693d22018-07-17 10:46:29 -07002492 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002493 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002494 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002495 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002496
Dave Wallace7876d392017-10-19 03:53:57 -04002497 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002498 {
Florin Coras134a9962018-08-28 11:32:04 -07002499 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002500 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002501 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2502 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002503 }
Dave Wallace7876d392017-10-19 03:53:57 -04002504 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002505 {
Florin Coras134a9962018-08-28 11:32:04 -07002506 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002507 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002508 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2509 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002510 }
Dave Wallace7876d392017-10-19 03:53:57 -04002511 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002512 {
Florin Coras134a9962018-08-28 11:32:04 -07002513 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002514 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002515 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2516 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002517 }
2518
Florin Coras54693d22018-07-17 10:46:29 -07002519 if (!n_bits)
2520 return 0;
2521
2522 if (!write_map)
2523 goto check_rd;
2524
Florin Coras096a1d52021-01-27 15:33:51 -08002525 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2526 {
2527 if (!(s = vcl_session_get (wrk, sid)))
2528 {
2529 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2530 bits_set++;
2531 continue;
2532 }
Florin Coras54693d22018-07-17 10:46:29 -07002533
Florin Coras096a1d52021-01-27 15:33:51 -08002534 if (vcl_session_write_ready (s))
2535 {
2536 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2537 bits_set++;
2538 }
2539 else
2540 {
2541 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2542 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2543 }
2544 }
Florin Coras54693d22018-07-17 10:46:29 -07002545
2546check_rd:
2547 if (!read_map)
2548 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002549
Florin Coras096a1d52021-01-27 15:33:51 -08002550 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2551 {
2552 if (!(s = vcl_session_get (wrk, sid)))
2553 {
2554 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2555 bits_set++;
2556 continue;
2557 }
Florin Coras54693d22018-07-17 10:46:29 -07002558
Florin Coras096a1d52021-01-27 15:33:51 -08002559 if (vcl_session_read_ready (s))
2560 {
2561 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2562 bits_set++;
2563 }
2564 }
Florin Coras54693d22018-07-17 10:46:29 -07002565
2566check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002567
Florin Coras86f04502018-09-12 16:08:01 -07002568 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2569 {
2570 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2571 read_map, write_map, except_map, &bits_set);
2572 }
2573 vec_reset_length (wrk->unhandled_evts_vector);
2574
Florin Coras99368312018-08-02 10:45:44 -07002575 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002576 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002577 time_to_wait, &bits_set);
2578 else
Florin Coras134a9962018-08-28 11:32:04 -07002579 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002580 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002581
Dave Wallace543852a2017-08-03 02:11:34 -04002582 return (bits_set);
2583}
2584
Dave Wallacef7f809c2017-10-03 01:48:42 -04002585static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002586vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002587{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002588 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002589 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002590 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002591
Florin Corasc0737e92019-03-04 14:19:39 -08002592 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002593 return;
2594
Florin Coras6c3b2182020-10-19 18:36:48 -07002595 s = vcl_session_get_w_handle (wrk, vep_handle);
2596 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002597 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002598 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002599 goto done;
2600 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002601 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002602 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002603 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002604 goto done;
2605 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002606 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002607 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002608 "{\n"
2609 " is_vep = %u\n"
2610 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002611 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002612 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2613 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002614
Florin Coras7e5e62b2019-07-30 14:08:23 -07002615 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002617 s = vcl_session_get_w_handle (wrk, sh);
2618 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002619 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002620 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002621 goto done;
2622 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002623 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002624 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002625 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002626 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002627 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002628 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002629 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002630 goto done;
2631 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002632 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002633 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2634 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002635 sh, s->vep.vep_sh, vep_handle);
2636 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002637 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002638 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002639 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002640 " next_sh = 0x%x (%u)\n"
2641 " prev_sh = 0x%x (%u)\n"
2642 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002643 " ev.events = 0x%x\n"
2644 " ev.data.u64 = 0x%llx\n"
2645 " et_mask = 0x%x\n"
2646 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002647 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002648 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2649 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002650 }
2651 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002652
2653done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002654 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655}
2656
2657int
2658vppcom_epoll_create (void)
2659{
Florin Coras134a9962018-08-28 11:32:04 -07002660 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002661 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002662
Florin Coras134a9962018-08-28 11:32:04 -07002663 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002664
Florin Coras6c3b2182020-10-19 18:36:48 -07002665 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002666 vep_session->vep.vep_sh = ~0;
2667 vep_session->vep.next_sh = ~0;
2668 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002669 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002670
Florin Corasa7a1a222018-12-30 17:11:31 -08002671 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2672 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002673
Florin Corasab2f6db2018-08-31 14:31:41 -07002674 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675}
2676
2677int
Florin Coras134a9962018-08-28 11:32:04 -07002678vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002679 struct epoll_event *event)
2680{
Florin Coras134a9962018-08-28 11:32:04 -07002681 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002682 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002683 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002684 vcl_session_t *s;
2685 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686
Florin Coras134a9962018-08-28 11:32:04 -07002687 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002689 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690 return VPPCOM_EINVAL;
2691 }
2692
Florin Coras134a9962018-08-28 11:32:04 -07002693 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002694 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002695 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002696 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002697 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002698 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002699 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002700 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002701 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002702 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002703 }
2704
Florin Coras134a9962018-08-28 11:32:04 -07002705 ASSERT (vep_session->vep.vep_sh == ~0);
2706 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002707
Florin Coras17ec5772020-08-12 20:46:29 -07002708 s = vcl_session_get_w_handle (wrk, session_handle);
2709 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002710 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002711 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002712 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002713 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002714 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002716 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002717 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002718 }
2719
2720 switch (op)
2721 {
2722 case EPOLL_CTL_ADD:
2723 if (PREDICT_FALSE (!event))
2724 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002725 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002726 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002727 }
Florin Coras2645f682021-06-04 15:59:41 -07002728 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2729 {
2730 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2731 rv = VPPCOM_EEXIST;
2732 goto done;
2733 }
Florin Coras134a9962018-08-28 11:32:04 -07002734 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002735 {
Florin Coras7e12d942018-06-27 14:32:43 -07002736 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002737 next_session = vcl_session_get_w_handle (wrk,
2738 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002739 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002740 {
Florin Coras5e062572019-03-14 19:07:51 -07002741 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002742 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002743 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002744 }
Florin Coras134a9962018-08-28 11:32:04 -07002745 ASSERT (next_session->vep.prev_sh == vep_handle);
2746 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002747 }
Florin Coras17ec5772020-08-12 20:46:29 -07002748 s->vep.next_sh = vep_session->vep.next_sh;
2749 s->vep.prev_sh = vep_handle;
2750 s->vep.vep_sh = vep_handle;
2751 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002752 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002753 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002754 s->flags &= ~VCL_SESSION_F_IS_VEP;
2755 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002756 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002757
Florin Coras17ec5772020-08-12 20:46:29 -07002758 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2759 if (txf && (event->events & EPOLLOUT))
2760 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002761
Florin Coras6e3c1f82020-01-15 01:30:46 +00002762 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002763 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002764 {
2765 session_event_t e = { 0 };
2766 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002767 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002768 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002769 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002770 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002771 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002772 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002773 {
2774 session_event_t e = { 0 };
2775 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002776 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002777 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002778 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2779 add_evt = 1;
2780 }
2781 if (!add_evt && vcl_session_is_closing (s))
2782 {
2783 session_event_t e = { 0 };
2784 if (s->session_state == VCL_STATE_VPP_CLOSING)
2785 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2786 else
2787 e.event_type = SESSION_CTRL_EVT_RESET;
2788 e.session_index = s->session_index;
2789 e.postponed = 1;
2790 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002791 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002792 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2793 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002794 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002795 break;
2796
2797 case EPOLL_CTL_MOD:
2798 if (PREDICT_FALSE (!event))
2799 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002800 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002801 rv = VPPCOM_EINVAL;
2802 goto done;
2803 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002804 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002805 {
Florin Coras5e062572019-03-14 19:07:51 -07002806 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002807 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002808 goto done;
2809 }
Florin Coras17ec5772020-08-12 20:46:29 -07002810 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002811 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002812 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002813 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002814 rv = VPPCOM_EINVAL;
2815 goto done;
2816 }
hanlin475c9d72019-12-26 11:44:28 +08002817
Florin Coras2645f682021-06-04 15:59:41 -07002818 /* Generate EPOLLOUT if session write ready nd event was not on */
2819 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2820 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002821 {
2822 session_event_t e = { 0 };
2823 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002824 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002825 vec_add1 (wrk->unhandled_evts_vector, e);
2826 }
Florin Coras2645f682021-06-04 15:59:41 -07002827 /* Generate EPOLLIN if session read ready and event was not on */
2828 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2829 (vcl_session_read_ready (s) > 0))
2830 {
2831 session_event_t e = { 0 };
2832 e.event_type = SESSION_IO_EVT_RX;
2833 e.session_index = s->session_index;
2834 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002835 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002836 }
Florin Coras17ec5772020-08-12 20:46:29 -07002837 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2838 s->vep.ev = *event;
2839 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002840 if (txf)
2841 {
2842 if (event->events & EPOLLOUT)
2843 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2844 else
2845 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2846 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002847 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2848 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002849 break;
2850
2851 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002852 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002853 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002854 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002855 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002856 goto done;
2857 }
Florin Coras17ec5772020-08-12 20:46:29 -07002858 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002859 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002860 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002861 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002862 rv = VPPCOM_EINVAL;
2863 goto done;
2864 }
2865
Florin Coras17ec5772020-08-12 20:46:29 -07002866 if (s->vep.prev_sh == vep_handle)
2867 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002868 else
2869 {
Florin Coras7e12d942018-06-27 14:32:43 -07002870 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002871 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002872 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002873 {
Florin Coras5e062572019-03-14 19:07:51 -07002874 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002875 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002876 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002877 }
Florin Coras134a9962018-08-28 11:32:04 -07002878 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002879 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002880 }
Florin Coras17ec5772020-08-12 20:46:29 -07002881 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002882 {
Florin Coras7e12d942018-06-27 14:32:43 -07002883 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002884 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002885 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002886 {
Florin Coras5e062572019-03-14 19:07:51 -07002887 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002888 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002889 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002890 }
Florin Coras134a9962018-08-28 11:32:04 -07002891 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002892 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002893 }
2894
Florin Corasfa3884f2021-06-22 20:04:46 -07002895 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2896 vcl_epoll_lt_del (wrk, s);
2897
Florin Coras17ec5772020-08-12 20:46:29 -07002898 memset (&s->vep, 0, sizeof (s->vep));
2899 s->vep.next_sh = ~0;
2900 s->vep.prev_sh = ~0;
2901 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002902 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002903 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002904
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002905 if (vcl_session_is_open (s))
2906 {
2907 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2908 if (txf)
2909 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2910 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002911
Florin Coras5e062572019-03-14 19:07:51 -07002912 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002913 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002914 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002915 break;
2916
2917 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002918 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002919 rv = VPPCOM_EINVAL;
2920 }
2921
Florin Coras134a9962018-08-28 11:32:04 -07002922 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002923
2924done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002925 return rv;
2926}
2927
Florin Coras86f04502018-09-12 16:08:01 -07002928static inline void
2929vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2930 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002931{
2932 session_disconnected_msg_t *disconnected_msg;
2933 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002934 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002935 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002936 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002937 u8 add_event = 0;
2938
2939 switch (e->event_type)
2940 {
Florin Coras653e43f2019-03-04 10:56:23 -08002941 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002942 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002943 s = vcl_session_get (wrk, sid);
2944 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002945 break;
Florin Corasb242d312020-10-26 15:35:40 -07002946 vcl_fifo_rx_evt_valid_or_break (s);
2947 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07002948 if (!(EPOLLIN & s->vep.ev.events) ||
2949 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
2950 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07002951 break;
2952 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08002953 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002954 session_evt_data = s->vep.ev.data.u64;
2955 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002956 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002957 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002958 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002959 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08002960 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002961 break;
Florin Corasb242d312020-10-26 15:35:40 -07002962 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002963 if (!(EPOLLOUT & session_events))
2964 break;
2965 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08002966 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002967 session_evt_data = s->vep.ev.data.u64;
2968 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2969 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002970 break;
Florin Coras86f04502018-09-12 16:08:01 -07002971 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002972 if (!e->postponed)
2973 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2974 else
2975 s = vcl_session_get (wrk, e->session_index);
2976 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002977 break;
Florin Corasb242d312020-10-26 15:35:40 -07002978 session_events = s->vep.ev.events;
2979 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08002980 if (!(EPOLLIN & session_events) ||
2981 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07002982 break;
Florin Coras86f04502018-09-12 16:08:01 -07002983 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08002984 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002985 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002986 break;
2987 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002988 if (!e->postponed)
2989 {
2990 connected_msg = (session_connected_msg_t *) e->data;
2991 sid = vcl_session_connected_handler (wrk, connected_msg);
2992 }
2993 else
2994 sid = e->session_index;
2995 s = vcl_session_get (wrk, sid);
2996 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002997 break;
Florin Corasb242d312020-10-26 15:35:40 -07002998 session_events = s->vep.ev.events;
2999 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003000 if (!(EPOLLOUT & session_events))
3001 break;
3002 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003003 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003004 session_evt_data = s->vep.ev.data.u64;
3005 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003006 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003007 break;
3008 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003009 if (!e->postponed)
3010 {
3011 disconnected_msg = (session_disconnected_msg_t *) e->data;
3012 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3013 }
3014 else
3015 {
3016 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003017 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003018 }
3019 if (vcl_session_is_closed (s) ||
3020 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003021 {
3022 if (s->flags & VCL_SESSION_F_PENDING_FREE)
3023 vcl_session_free (wrk, s);
3024 break;
3025 }
Florin Corasb242d312020-10-26 15:35:40 -07003026 sid = s->session_index;
3027 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003028 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003029 if (EPOLLRDHUP & session_events)
3030 {
3031 /* If app can distinguish between RDHUP and HUP,
3032 * we make finer control */
3033 events[*num_ev].events = EPOLLRDHUP;
3034 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3035 {
3036 events[*num_ev].events |= EPOLLHUP;
3037 }
3038 }
3039 else
3040 {
3041 events[*num_ev].events = EPOLLHUP;
3042 }
Florin Corasb242d312020-10-26 15:35:40 -07003043 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003044
Florin Coras86f04502018-09-12 16:08:01 -07003045 break;
3046 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003047 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003048 {
3049 sid =
3050 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3051 s = vcl_session_get (wrk, sid);
3052 }
Florin Coras87f76002021-06-28 19:13:29 -07003053 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003054 {
3055 sid = e->session_index;
3056 s = vcl_session_get (wrk, sid);
3057 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3058 }
Florin Coras87f76002021-06-28 19:13:29 -07003059 if (vcl_session_is_closed (s) ||
3060 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003061 {
3062 if (s->flags & VCL_SESSION_F_PENDING_FREE)
3063 vcl_session_free (wrk, s);
3064 break;
3065 }
Florin Corasb242d312020-10-26 15:35:40 -07003066 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003067 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003068 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003069 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003070 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003071 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3072 vcl_session_unlisten_reply_handler (wrk, e->data);
3073 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003074 case SESSION_CTRL_EVT_MIGRATED:
3075 vcl_session_migrated_handler (wrk, e->data);
3076 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003077 case SESSION_CTRL_EVT_CLEANUP:
3078 vcl_session_cleanup_handler (wrk, e->data);
3079 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003080 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3081 vcl_session_req_worker_update_handler (wrk, e->data);
3082 break;
3083 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3084 vcl_session_worker_update_reply_handler (wrk, e->data);
3085 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003086 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3087 vcl_session_app_add_segment_handler (wrk, e->data);
3088 break;
3089 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3090 vcl_session_app_del_segment_handler (wrk, e->data);
3091 break;
hanlina3a48962020-07-13 11:09:15 +08003092 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003093 vcl_worker_rpc_handler (wrk, e->data);
3094 break;
Florin Coras86f04502018-09-12 16:08:01 -07003095 default:
3096 VDBG (0, "unhandled: %u", e->event_type);
3097 break;
3098 }
3099
3100 if (add_event)
3101 {
3102 events[*num_ev].data.u64 = session_evt_data;
3103 if (EPOLLONESHOT & session_events)
3104 {
Florin Corasb242d312020-10-26 15:35:40 -07003105 s = vcl_session_get (wrk, sid);
3106 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003107 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003108 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003109 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003110 s = vcl_session_get (wrk, sid);
3111 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3112 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003113 }
Florin Coras86f04502018-09-12 16:08:01 -07003114 *num_ev += 1;
3115 }
3116}
3117
3118static int
3119vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3120 struct epoll_event *events, u32 maxevents,
3121 double wait_for_time, u32 * num_ev)
3122{
Florin Coras99368312018-08-02 10:45:44 -07003123 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003124 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003125 int i;
3126
Florin Coras539663c2018-09-28 14:59:37 -07003127 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3128 goto handle_dequeued;
3129
Florin Coras54693d22018-07-17 10:46:29 -07003130 if (svm_msg_q_is_empty (mq))
3131 {
3132 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003133 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003134 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003135 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003136 else
3137 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003138 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003139 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003140 }
3141 }
Florin Corase003a1b2019-06-05 10:47:16 -07003142 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003143 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003144
Florin Coras539663c2018-09-28 14:59:37 -07003145handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003146 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003147 {
Florin Coras134a9962018-08-28 11:32:04 -07003148 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003149 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003150 if (*num_ev < maxevents)
3151 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3152 else
3153 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003154 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003155 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003156 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003157 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003158 return *num_ev;
3159}
3160
Florin Coras99368312018-08-02 10:45:44 -07003161static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003162vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3163 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003164{
Florin Corasccdb8b82021-04-28 13:01:06 -07003165 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003166
3167 if (!n_evts)
3168 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003169 if (timeout_ms > 0)
3170 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003171 }
3172
3173 do
3174 {
3175 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003176 timeout_ms, &n_evts);
3177 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003178 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003179 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003180 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003181
3182 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003183}
3184
3185static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003186vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3187 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003188{
Florin Coras99368312018-08-02 10:45:44 -07003189 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003190 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003191 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003192 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003193 u64 buf;
3194
Florin Coras134a9962018-08-28 11:32:04 -07003195 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003196 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003197 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003198 if (timeout_ms > 0)
3199 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003200 }
3201
Florin Corasb13ba132021-01-27 16:05:24 -08003202 do
3203 {
3204 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003205 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003206 if (n_mq_evts < 0)
3207 {
3208 VDBG (0, "epoll_wait error %u", errno);
3209 return n_evts;
3210 }
3211
3212 for (i = 0; i < n_mq_evts; i++)
3213 {
3214 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3215 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3216 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3217 &n_evts);
3218 }
3219
Florin Corasccdb8b82021-04-28 13:01:06 -07003220 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003221 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003222 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003223 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003224
3225 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003226}
3227
Florin Corasfe286f72021-06-04 10:07:55 -07003228static void
Florin Corasfe286f72021-06-04 10:07:55 -07003229vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3230 int maxevents, u32 *n_evts)
3231{
Florin Coras734268f2021-06-30 07:54:29 -07003232 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003233 vcl_session_t *s;
3234 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003235 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003236
Florin Corasfa3884f2021-06-22 20:04:46 -07003237 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003238 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003239 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003240
Florin Corasfa3884f2021-06-22 20:04:46 -07003241 next = wrk->ep_lt_current;
3242 do
Florin Corasfe286f72021-06-04 10:07:55 -07003243 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003244 s = vcl_session_get (wrk, next);
3245 next = s->vep.lt_next;
3246
3247 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003248 {
3249 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003250 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003251 evt_data = s->vep.ev.data.u64;
3252 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003253 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003254 {
3255 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003256 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3257 evt_data = s->vep.ev.data.u64;
3258 }
3259 if (!add_event && s->session_state > VCL_STATE_READY)
3260 {
3261 add_event = 1;
3262 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003263 evt_data = s->vep.ev.data.u64;
3264 }
3265 if (add_event)
3266 {
3267 events[*n_evts].data.u64 = evt_data;
3268 *n_evts += 1;
3269 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003270 if (EPOLLONESHOT & s->vep.ev.events)
3271 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003272 if (*n_evts == maxevents)
3273 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003274 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003275 break;
3276 }
3277 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003278 else
3279 {
3280 vcl_epoll_lt_del (wrk, s);
3281 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3282 break;
3283 }
Florin Corasfe286f72021-06-04 10:07:55 -07003284 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003285 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003286}
3287
Dave Wallacef7f809c2017-10-03 01:48:42 -04003288int
Florin Coras134a9962018-08-28 11:32:04 -07003289vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003290 int maxevents, double wait_for_time)
3291{
Florin Coras134a9962018-08-28 11:32:04 -07003292 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003293 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003294 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003295 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003296
3297 if (PREDICT_FALSE (maxevents <= 0))
3298 {
Florin Coras5e062572019-03-14 19:07:51 -07003299 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003300 return VPPCOM_EINVAL;
3301 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003302
Florin Coras134a9962018-08-28 11:32:04 -07003303 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003304 if (!vep_session)
3305 return VPPCOM_EBADFD;
3306
Florin Coras6c3b2182020-10-19 18:36:48 -07003307 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003308 {
Florin Coras5e062572019-03-14 19:07:51 -07003309 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003310 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003311 }
Florin Coras54693d22018-07-17 10:46:29 -07003312
Florin Coras86f04502018-09-12 16:08:01 -07003313 if (vec_len (wrk->unhandled_evts_vector))
3314 {
3315 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3316 {
3317 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3318 events, &n_evts);
3319 if (n_evts == maxevents)
3320 {
Florin Corase003a1b2019-06-05 10:47:16 -07003321 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3322 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003323 }
3324 }
Florin Corase003a1b2019-06-05 10:47:16 -07003325 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003326 }
liuyacancba87102021-09-10 15:14:05 +08003327
3328 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3329 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3330
wanghanlin8919fec2021-03-18 20:00:41 +08003331 /* Request to only drain unhandled */
3332 if ((int) wait_for_time == -2)
3333 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003334
Florin Coras86f04502018-09-12 16:08:01 -07003335
Florin Corasfe286f72021-06-04 10:07:55 -07003336 if (vcm->cfg.use_mq_eventfd)
3337 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3338 wait_for_time);
3339 else
3340 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3341 wait_for_time);
3342
Florin Corasfe286f72021-06-04 10:07:55 -07003343 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003344}
3345
Dave Wallace35830af2017-10-09 01:43:42 -04003346int
Florin Coras134a9962018-08-28 11:32:04 -07003347vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003348 void *buffer, uint32_t * buflen)
3349{
Florin Coras134a9962018-08-28 11:32:04 -07003350 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003351 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003352 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003353 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003354 vcl_session_t *session;
3355 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003356
Florin Coras134a9962018-08-28 11:32:04 -07003357 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003358 if (!session)
3359 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003360
Dave Wallace35830af2017-10-09 01:43:42 -04003361 switch (op)
3362 {
3363 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003364 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003365 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3366 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003367 break;
3368
Dave Wallace227867f2017-11-13 21:21:53 -05003369 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003370 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003371 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3372 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003373 break;
3374
3375 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003376 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003377 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003378 *flags =
3379 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003380 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003381 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003382 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003383 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3384 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003385 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003386 }
3387 else
3388 rv = VPPCOM_EINVAL;
3389 break;
3390
3391 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003392 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003393 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003394 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003395 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003396 else
Florin Corasac422d62020-10-19 20:51:36 -07003397 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003398
Florin Coras5e062572019-03-14 19:07:51 -07003399 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3400 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003401 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003402 }
3403 else
3404 rv = VPPCOM_EINVAL;
3405 break;
3406
3407 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003408 if (PREDICT_TRUE (buffer && buflen &&
3409 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003410 {
Florin Coras7e12d942018-06-27 14:32:43 -07003411 ep->is_ip4 = session->transport.is_ip4;
3412 ep->port = session->transport.rmt_port;
3413 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003414 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3415 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003416 else
Dave Barach178cf492018-11-13 16:34:13 -05003417 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3418 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003419 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003420 VDBG (1,
3421 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3422 "addr = %U, port %u",
3423 session_handle, ep->is_ip4, vcl_format_ip46_address,
3424 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003425 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3426 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003427 }
3428 else
3429 rv = VPPCOM_EINVAL;
3430 break;
3431
3432 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003433 if (PREDICT_TRUE (buffer && buflen &&
3434 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003435 {
Florin Coras7e12d942018-06-27 14:32:43 -07003436 ep->is_ip4 = session->transport.is_ip4;
3437 ep->port = session->transport.lcl_port;
3438 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003439 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3440 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003441 else
Dave Barach178cf492018-11-13 16:34:13 -05003442 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3443 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003444 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003445 VDBG (1,
3446 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3447 " port %d",
3448 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003449 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003450 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3451 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003452 }
3453 else
3454 rv = VPPCOM_EINVAL;
3455 break;
Stevenb5a11602017-10-11 09:59:30 -07003456
Florin Corasef7cbf62019-10-17 09:56:27 -07003457 case VPPCOM_ATTR_SET_LCL_ADDR:
3458 if (PREDICT_TRUE (buffer && buflen &&
3459 (*buflen >= sizeof (*ep)) && ep->ip))
3460 {
3461 session->transport.is_ip4 = ep->is_ip4;
3462 session->transport.lcl_port = ep->port;
3463 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3464 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003465 VDBG (1,
3466 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3467 " port %d",
3468 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003469 &session->transport.lcl_ip,
3470 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3471 clib_net_to_host_u16 (ep->port));
3472 }
3473 else
3474 rv = VPPCOM_EINVAL;
3475 break;
3476
Dave Wallace048b1d62018-01-03 22:24:41 -05003477 case VPPCOM_ATTR_GET_LIBC_EPFD:
3478 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003479 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003480 break;
3481
3482 case VPPCOM_ATTR_SET_LIBC_EPFD:
3483 if (PREDICT_TRUE (buffer && buflen &&
3484 (*buflen == sizeof (session->libc_epfd))))
3485 {
3486 session->libc_epfd = *(int *) buffer;
3487 *buflen = sizeof (session->libc_epfd);
3488
Florin Coras5e062572019-03-14 19:07:51 -07003489 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3490 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003491 }
3492 else
3493 rv = VPPCOM_EINVAL;
3494 break;
3495
3496 case VPPCOM_ATTR_GET_PROTOCOL:
3497 if (buffer && buflen && (*buflen >= sizeof (int)))
3498 {
Florin Coras7e12d942018-06-27 14:32:43 -07003499 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003500 *buflen = sizeof (int);
3501
Florin Coras5e062572019-03-14 19:07:51 -07003502 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3503 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003504 }
3505 else
3506 rv = VPPCOM_EINVAL;
3507 break;
3508
3509 case VPPCOM_ATTR_GET_LISTEN:
3510 if (buffer && buflen && (*buflen >= sizeof (int)))
3511 {
Florin Corasac422d62020-10-19 20:51:36 -07003512 *(int *) buffer = vcl_session_has_attr (session,
3513 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003514 *buflen = sizeof (int);
3515
Florin Coras5e062572019-03-14 19:07:51 -07003516 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3517 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003518 }
3519 else
3520 rv = VPPCOM_EINVAL;
3521 break;
3522
3523 case VPPCOM_ATTR_GET_ERROR:
3524 if (buffer && buflen && (*buflen >= sizeof (int)))
3525 {
3526 *(int *) buffer = 0;
3527 *buflen = sizeof (int);
3528
Florin Coras5e062572019-03-14 19:07:51 -07003529 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3530 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003531 }
3532 else
3533 rv = VPPCOM_EINVAL;
3534 break;
3535
3536 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3537 if (buffer && buflen && (*buflen >= sizeof (u32)))
3538 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003539
3540 /* VPP-TBD */
3541 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003542 session->tx_fifo ?
3543 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003544 vcm->cfg.tx_fifo_size);
3545 *buflen = sizeof (u32);
3546
Florin Coras5e062572019-03-14 19:07:51 -07003547 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3548 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3549 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003550 }
3551 else
3552 rv = VPPCOM_EINVAL;
3553 break;
3554
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003555 case VPPCOM_ATTR_SET_DSCP:
3556 if (buffer && buflen && (*buflen >= sizeof (u8)))
3557 {
3558 session->dscp = *(u8 *) buffer;
3559
3560 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3561 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3562 }
3563 else
3564 rv = VPPCOM_EINVAL;
3565 break;
3566
Dave Wallace048b1d62018-01-03 22:24:41 -05003567 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3568 if (buffer && buflen && (*buflen == sizeof (u32)))
3569 {
3570 /* VPP-TBD */
3571 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003572 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3573 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3574 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003575 }
3576 else
3577 rv = VPPCOM_EINVAL;
3578 break;
3579
3580 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3581 if (buffer && buflen && (*buflen >= sizeof (u32)))
3582 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003583
3584 /* VPP-TBD */
3585 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003586 session->rx_fifo ?
3587 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003588 vcm->cfg.rx_fifo_size);
3589 *buflen = sizeof (u32);
3590
Florin Coras5e062572019-03-14 19:07:51 -07003591 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3592 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003593 }
3594 else
3595 rv = VPPCOM_EINVAL;
3596 break;
3597
3598 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3599 if (buffer && buflen && (*buflen == sizeof (u32)))
3600 {
3601 /* VPP-TBD */
3602 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003603 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3604 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3605 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003606 }
3607 else
3608 rv = VPPCOM_EINVAL;
3609 break;
3610
3611 case VPPCOM_ATTR_GET_REUSEADDR:
3612 if (buffer && buflen && (*buflen >= sizeof (int)))
3613 {
3614 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003615 *(int *) buffer = vcl_session_has_attr (session,
3616 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003617 *buflen = sizeof (int);
3618
Florin Coras5e062572019-03-14 19:07:51 -07003619 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3620 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003621 }
3622 else
3623 rv = VPPCOM_EINVAL;
3624 break;
3625
Stevenb5a11602017-10-11 09:59:30 -07003626 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003627 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003628 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003629 {
3630 /* VPP-TBD */
3631 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003632 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003633 else
Florin Corasac422d62020-10-19 20:51:36 -07003634 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003635
Florin Coras5e062572019-03-14 19:07:51 -07003636 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003637 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003638 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003639 }
3640 else
3641 rv = VPPCOM_EINVAL;
3642 break;
3643
3644 case VPPCOM_ATTR_GET_REUSEPORT:
3645 if (buffer && buflen && (*buflen >= sizeof (int)))
3646 {
3647 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003648 *(int *) buffer = vcl_session_has_attr (session,
3649 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003650 *buflen = sizeof (int);
3651
Florin Coras5e062572019-03-14 19:07:51 -07003652 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3653 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003654 }
3655 else
3656 rv = VPPCOM_EINVAL;
3657 break;
3658
3659 case VPPCOM_ATTR_SET_REUSEPORT:
3660 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003661 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003662 {
3663 /* VPP-TBD */
3664 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003665 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003666 else
Florin Corasac422d62020-10-19 20:51:36 -07003667 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003668
Florin Coras5e062572019-03-14 19:07:51 -07003669 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003670 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003671 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003672 }
3673 else
3674 rv = VPPCOM_EINVAL;
3675 break;
3676
3677 case VPPCOM_ATTR_GET_BROADCAST:
3678 if (buffer && buflen && (*buflen >= sizeof (int)))
3679 {
3680 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003681 *(int *) buffer = vcl_session_has_attr (session,
3682 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003683 *buflen = sizeof (int);
3684
Florin Coras5e062572019-03-14 19:07:51 -07003685 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3686 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003687 }
3688 else
3689 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003690 break;
3691
3692 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003693 if (buffer && buflen && (*buflen == sizeof (int)))
3694 {
3695 /* VPP-TBD */
3696 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003697 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003698 else
Florin Corasac422d62020-10-19 20:51:36 -07003699 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003700
Florin Coras5e062572019-03-14 19:07:51 -07003701 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003702 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003703 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003704 }
3705 else
3706 rv = VPPCOM_EINVAL;
3707 break;
3708
3709 case VPPCOM_ATTR_GET_V6ONLY:
3710 if (buffer && buflen && (*buflen >= sizeof (int)))
3711 {
3712 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003713 *(int *) buffer = vcl_session_has_attr (session,
3714 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003715 *buflen = sizeof (int);
3716
Florin Coras5e062572019-03-14 19:07:51 -07003717 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3718 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003719 }
3720 else
3721 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003722 break;
3723
3724 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003725 if (buffer && buflen && (*buflen == sizeof (int)))
3726 {
3727 /* VPP-TBD */
3728 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003729 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003730 else
Florin Corasac422d62020-10-19 20:51:36 -07003731 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003732
Florin Coras5e062572019-03-14 19:07:51 -07003733 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003734 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003735 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003736 }
3737 else
3738 rv = VPPCOM_EINVAL;
3739 break;
3740
3741 case VPPCOM_ATTR_GET_KEEPALIVE:
3742 if (buffer && buflen && (*buflen >= sizeof (int)))
3743 {
3744 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003745 *(int *) buffer = vcl_session_has_attr (session,
3746 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003747 *buflen = sizeof (int);
3748
Florin Coras5e062572019-03-14 19:07:51 -07003749 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3750 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003751 }
3752 else
3753 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003754 break;
Stevenbccd3392017-10-12 20:42:21 -07003755
3756 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003757 if (buffer && buflen && (*buflen == sizeof (int)))
3758 {
3759 /* VPP-TBD */
3760 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003761 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003762 else
Florin Corasac422d62020-10-19 20:51:36 -07003763 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003764
Florin Coras5e062572019-03-14 19:07:51 -07003765 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003766 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003767 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003768 }
3769 else
3770 rv = VPPCOM_EINVAL;
3771 break;
3772
3773 case VPPCOM_ATTR_GET_TCP_NODELAY:
3774 if (buffer && buflen && (*buflen >= sizeof (int)))
3775 {
3776 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003777 *(int *) buffer = vcl_session_has_attr (session,
3778 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003779 *buflen = sizeof (int);
3780
Florin Coras5e062572019-03-14 19:07:51 -07003781 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3782 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003783 }
3784 else
3785 rv = VPPCOM_EINVAL;
3786 break;
3787
3788 case VPPCOM_ATTR_SET_TCP_NODELAY:
3789 if (buffer && buflen && (*buflen == sizeof (int)))
3790 {
3791 /* VPP-TBD */
3792 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003793 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003794 else
Florin Corasac422d62020-10-19 20:51:36 -07003795 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003796
Florin Coras5e062572019-03-14 19:07:51 -07003797 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003798 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003799 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003800 }
3801 else
3802 rv = VPPCOM_EINVAL;
3803 break;
3804
3805 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3806 if (buffer && buflen && (*buflen >= sizeof (int)))
3807 {
3808 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003809 *(int *) buffer = vcl_session_has_attr (session,
3810 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 *buflen = sizeof (int);
3812
Florin Coras5e062572019-03-14 19:07:51 -07003813 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3814 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003815 }
3816 else
3817 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003818 break;
3819
3820 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003821 if (buffer && buflen && (*buflen == sizeof (int)))
3822 {
3823 /* VPP-TBD */
3824 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003825 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003826 else
Florin Corasac422d62020-10-19 20:51:36 -07003827 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828
Florin Coras5e062572019-03-14 19:07:51 -07003829 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003830 vcl_session_has_attr (session,
3831 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 }
3833 else
3834 rv = VPPCOM_EINVAL;
3835 break;
3836
3837 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3838 if (buffer && buflen && (*buflen >= sizeof (int)))
3839 {
3840 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003841 *(int *) buffer = vcl_session_has_attr (session,
3842 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 *buflen = sizeof (int);
3844
Florin Coras5e062572019-03-14 19:07:51 -07003845 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3846 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003847 }
3848 else
3849 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003850 break;
3851
3852 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003853 if (buffer && buflen && (*buflen == sizeof (int)))
3854 {
3855 /* VPP-TBD */
3856 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003857 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003858 else
Florin Corasac422d62020-10-19 20:51:36 -07003859 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003860
Florin Coras5e062572019-03-14 19:07:51 -07003861 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003862 vcl_session_has_attr (session,
3863 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003864 }
3865 else
3866 rv = VPPCOM_EINVAL;
3867 break;
3868
3869 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003870 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003871 {
Florin Coras04ae8272021-04-12 19:55:37 -07003872 rv = VPPCOM_EINVAL;
3873 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003874 }
Florin Coras04ae8272021-04-12 19:55:37 -07003875
3876 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3877 tea.mss = *(u32 *) buffer;
3878 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3879 rv = VPPCOM_ENOPROTOOPT;
3880
3881 if (!rv)
3882 {
3883 *(u32 *) buffer = tea.mss;
3884 *buflen = sizeof (int);
3885 }
3886
3887 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3888 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003889 break;
3890
3891 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003892 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003893 {
Florin Coras04ae8272021-04-12 19:55:37 -07003894 rv = VPPCOM_EINVAL;
3895 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003896 }
Florin Coras04ae8272021-04-12 19:55:37 -07003897
3898 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3899 tea.mss = *(u32 *) buffer;
3900 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3901 rv = VPPCOM_ENOPROTOOPT;
3902
3903 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3904 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003905 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003906
Florin Coras1e966172020-05-16 18:18:14 +00003907 case VPPCOM_ATTR_SET_CONNECTED:
3908 session->flags |= VCL_SESSION_F_CONNECTED;
3909 break;
3910
Florin Corasa5a9efd2021-01-05 17:03:29 -08003911 case VPPCOM_ATTR_SET_CKPAIR:
3912 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3913 !vcl_session_has_crypto (session))
3914 {
3915 rv = VPPCOM_EINVAL;
3916 break;
3917 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003918 if (!session->ext_config)
3919 {
Florin Coras87f63892021-05-01 16:01:40 -07003920 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3921 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003922 }
3923 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3924 {
3925 rv = VPPCOM_EINVAL;
3926 break;
3927 }
3928
3929 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003930 break;
3931
Florin Coras6a6555a2021-01-28 11:39:27 -08003932 case VPPCOM_ATTR_SET_VRF:
3933 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3934 {
3935 rv = VPPCOM_EINVAL;
3936 break;
3937 }
3938 session->vrf = *(u32 *) buffer;
3939 break;
3940
3941 case VPPCOM_ATTR_GET_VRF:
3942 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3943 {
3944 rv = VPPCOM_EINVAL;
3945 break;
3946 }
3947 *(u32 *) buffer = session->vrf;
3948 *buflen = sizeof (u32);
3949 break;
3950
wanghanlin0674f852021-02-22 10:38:36 +08003951 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003952 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003953 {
Florin Corasd77325c2021-02-23 12:03:03 -08003954 rv = VPPCOM_EINVAL;
3955 break;
wanghanlin0674f852021-02-22 10:38:36 +08003956 }
Florin Corasd77325c2021-02-23 12:03:03 -08003957
3958 if (session->transport.is_ip4)
3959 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003960 else
Florin Corasd77325c2021-02-23 12:03:03 -08003961 *(int *) buffer = AF_INET6;
3962 *buflen = sizeof (int);
3963
wanghanlin0674f852021-02-22 10:38:36 +08003964 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3965 *buflen);
3966 break;
3967
Florin Coras87f63892021-05-01 16:01:40 -07003968 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3969 if (!(buffer && buflen && (*buflen > 0)))
3970 {
3971 rv = VPPCOM_EINVAL;
3972 break;
3973 }
3974 if (session->ext_config)
3975 {
3976 rv = VPPCOM_EINVAL;
3977 break;
3978 }
3979 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3980 *buflen + sizeof (u32));
3981 clib_memcpy (session->ext_config->data, buffer, *buflen);
3982 session->ext_config->len = *buflen;
3983 break;
3984
Dave Wallacee22aa742017-10-20 12:30:38 -04003985 default:
3986 rv = VPPCOM_EINVAL;
3987 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003988 }
3989
Dave Wallace35830af2017-10-09 01:43:42 -04003990 return rv;
3991}
3992
Stevenac1f96d2017-10-24 16:03:58 -07003993int
Florin Coras134a9962018-08-28 11:32:04 -07003994vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003995 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3996{
Florin Coras134a9962018-08-28 11:32:04 -07003997 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003998 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003999 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004000
4001 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004002 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004003 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004004 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004005 else
4006 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004007 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004008 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004009 }
4010
Florin Coras0a1e1832020-03-29 18:54:04 +00004011 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004012 {
Florin Coras5da10c42020-04-01 04:31:21 +00004013 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004014 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004015 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4016 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004017 else
Dave Barach178cf492018-11-13 16:34:13 -05004018 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4019 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004020 ep->is_ip4 = session->transport.is_ip4;
4021 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004022 }
Florin Coras460dce62018-07-27 05:45:06 -07004023
Stevenac1f96d2017-10-24 16:03:58 -07004024 return rv;
4025}
4026
4027int
Florin Coras134a9962018-08-28 11:32:04 -07004028vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004029 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4030{
Florin Coras7a2abce2020-04-05 19:25:44 +00004031 vcl_worker_t *wrk = vcl_worker_get_current ();
4032 vcl_session_t *s;
4033
4034 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004035 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004036 return VPPCOM_EBADFD;
4037
Dave Wallace617dffa2017-10-26 14:47:06 -04004038 if (ep)
4039 {
Florin Coras5824cc52020-10-20 18:44:41 -07004040 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004041 return VPPCOM_EINVAL;
4042
liuyacanbc0c7542021-08-02 20:15:05 +08004043 s->transport.is_ip4 = ep->is_ip4;
4044 s->transport.rmt_port = ep->port;
4045 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4046
Florin Coras5da10c42020-04-01 04:31:21 +00004047 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004048 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004049 {
Florin Coras5824cc52020-10-20 18:44:41 -07004050 u32 session_index = s->session_index;
4051 f64 timeout = vcm->cfg.session_timeout;
4052 int rv;
4053
Florin Coras5da10c42020-04-01 04:31:21 +00004054 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004055 rv = vppcom_wait_for_session_state_change (session_index,
4056 VCL_STATE_READY,
4057 timeout);
4058 if (rv < 0)
4059 return rv;
4060 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004061 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004062 }
4063
4064 if (flags)
4065 {
4066 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004067 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004068 }
4069
Florin Coras7a2abce2020-04-05 19:25:44 +00004070 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4071 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004072}
4073
Dave Wallace048b1d62018-01-03 22:24:41 -05004074int
4075vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4076{
Florin Coras134a9962018-08-28 11:32:04 -07004077 vcl_worker_t *wrk = vcl_worker_get_current ();
4078 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004079 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004080 svm_msg_q_msg_t msg;
4081 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004082 int rv, num_ev = 0;
4083
Florin Coras5e062572019-03-14 19:07:51 -07004084 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004085
4086 if (!vp)
4087 return VPPCOM_EFAULT;
4088
4089 do
4090 {
Florin Coras7e12d942018-06-27 14:32:43 -07004091 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004092
Florin Coras6917b942018-11-13 22:44:54 -08004093 /* Dequeue all events and drop all unhandled io events */
4094 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4095 {
4096 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4097 vcl_handle_mq_event (wrk, e);
4098 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4099 }
4100 vec_reset_length (wrk->unhandled_evts_vector);
4101
Dave Wallace048b1d62018-01-03 22:24:41 -05004102 for (i = 0; i < n_sids; i++)
4103 {
Florin Coras7baeb712019-01-04 17:05:43 -08004104 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004105 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004106 {
4107 vp[i].revents = POLLHUP;
4108 num_ev++;
4109 continue;
4110 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004111
Florin Coras6917b942018-11-13 22:44:54 -08004112 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004113
4114 if (POLLIN & vp[i].events)
4115 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004116 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004117 if (rv > 0)
4118 {
Florin Coras6917b942018-11-13 22:44:54 -08004119 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004120 num_ev++;
4121 }
4122 else if (rv < 0)
4123 {
4124 switch (rv)
4125 {
4126 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004127 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004128 break;
4129
4130 default:
Florin Coras6917b942018-11-13 22:44:54 -08004131 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004132 break;
4133 }
4134 num_ev++;
4135 }
4136 }
4137
4138 if (POLLOUT & vp[i].events)
4139 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004140 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004141 if (rv > 0)
4142 {
Florin Coras6917b942018-11-13 22:44:54 -08004143 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004144 num_ev++;
4145 }
4146 else if (rv < 0)
4147 {
4148 switch (rv)
4149 {
4150 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004151 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004152 break;
4153
4154 default:
Florin Coras6917b942018-11-13 22:44:54 -08004155 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004156 break;
4157 }
4158 num_ev++;
4159 }
4160 }
4161
Dave Wallace7e607a72018-06-18 18:41:32 -04004162 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004163 {
Florin Coras6917b942018-11-13 22:44:54 -08004164 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004165 num_ev++;
4166 }
4167 }
4168 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004169 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004170 }
4171 while ((num_ev == 0) && keep_trying);
4172
Dave Wallace048b1d62018-01-03 22:24:41 -05004173 return num_ev;
4174}
4175
Florin Coras99368312018-08-02 10:45:44 -07004176int
4177vppcom_mq_epoll_fd (void)
4178{
Florin Coras134a9962018-08-28 11:32:04 -07004179 vcl_worker_t *wrk = vcl_worker_get_current ();
4180 return wrk->mqs_epfd;
4181}
4182
4183int
Florin Coras30e79c22019-01-02 19:31:22 -08004184vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004185{
4186 return session_handle & 0xFFFFFF;
4187}
4188
4189int
Florin Coras30e79c22019-01-02 19:31:22 -08004190vppcom_session_worker (vcl_session_handle_t session_handle)
4191{
4192 return session_handle >> 24;
4193}
4194
4195int
Florin Coras134a9962018-08-28 11:32:04 -07004196vppcom_worker_register (void)
4197{
Florin Coras47c40e22018-11-26 17:01:36 -08004198 if (!vcl_worker_alloc_and_init ())
4199 return VPPCOM_EEXIST;
4200
Florin Coras47c40e22018-11-26 17:01:36 -08004201 if (vcl_worker_register_with_vpp ())
4202 return VPPCOM_EEXIST;
4203
4204 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004205}
4206
Florin Coras369db832019-07-08 12:34:45 -07004207void
4208vppcom_worker_unregister (void)
4209{
4210 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4211 vcl_set_worker_index (~0);
4212}
4213
Pivo6017ff02020-05-04 17:57:33 +02004214void
4215vppcom_worker_index_set (int index)
4216{
4217 vcl_set_worker_index (index);
4218}
4219
Florin Corasdfe4cf42018-11-28 22:13:45 -08004220int
4221vppcom_worker_index (void)
4222{
4223 return vcl_get_worker_index ();
4224}
4225
Florin Corase0982e52019-01-25 13:19:56 -08004226int
4227vppcom_worker_mqs_epfd (void)
4228{
4229 vcl_worker_t *wrk = vcl_worker_get_current ();
4230 if (!vcm->cfg.use_mq_eventfd)
4231 return -1;
4232 return wrk->mqs_epfd;
4233}
4234
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004235int
4236vppcom_session_is_connectable_listener (uint32_t session_handle)
4237{
4238 vcl_session_t *session;
4239 vcl_worker_t *wrk = vcl_worker_get_current ();
4240 session = vcl_session_get_w_handle (wrk, session_handle);
4241 if (!session)
4242 return VPPCOM_EBADFD;
4243 return vcl_session_is_connectable_listener (wrk, session);
4244}
4245
4246int
4247vppcom_session_listener (uint32_t session_handle)
4248{
4249 vcl_worker_t *wrk = vcl_worker_get_current ();
4250 vcl_session_t *listen_session, *session;
4251 session = vcl_session_get_w_handle (wrk, session_handle);
4252 if (!session)
4253 return VPPCOM_EBADFD;
4254 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4255 return VPPCOM_EBADFD;
4256 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4257 if (!listen_session)
4258 return VPPCOM_EBADFD;
4259 return vcl_session_handle (listen_session);
4260}
4261
4262int
4263vppcom_session_n_accepted (uint32_t session_handle)
4264{
4265 vcl_worker_t *wrk = vcl_worker_get_current ();
4266 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4267 if (!session)
4268 return VPPCOM_EBADFD;
4269 return session->n_accepted_sessions;
4270}
4271
Florin Coras66ec4672020-06-15 07:59:40 -07004272const char *
4273vppcom_proto_str (vppcom_proto_t proto)
4274{
4275 char const *proto_str;
4276
4277 switch (proto)
4278 {
4279 case VPPCOM_PROTO_TCP:
4280 proto_str = "TCP";
4281 break;
4282 case VPPCOM_PROTO_UDP:
4283 proto_str = "UDP";
4284 break;
4285 case VPPCOM_PROTO_TLS:
4286 proto_str = "TLS";
4287 break;
4288 case VPPCOM_PROTO_QUIC:
4289 proto_str = "QUIC";
4290 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004291 case VPPCOM_PROTO_DTLS:
4292 proto_str = "DTLS";
4293 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004294 case VPPCOM_PROTO_SRTP:
4295 proto_str = "SRTP";
4296 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004297 default:
4298 proto_str = "UNKNOWN";
4299 break;
4300 }
4301 return proto_str;
4302}
4303
4304const char *
4305vppcom_retval_str (int retval)
4306{
4307 char const *st;
4308
4309 switch (retval)
4310 {
4311 case VPPCOM_OK:
4312 st = "VPPCOM_OK";
4313 break;
4314
4315 case VPPCOM_EAGAIN:
4316 st = "VPPCOM_EAGAIN";
4317 break;
4318
4319 case VPPCOM_EFAULT:
4320 st = "VPPCOM_EFAULT";
4321 break;
4322
4323 case VPPCOM_ENOMEM:
4324 st = "VPPCOM_ENOMEM";
4325 break;
4326
4327 case VPPCOM_EINVAL:
4328 st = "VPPCOM_EINVAL";
4329 break;
4330
4331 case VPPCOM_EBADFD:
4332 st = "VPPCOM_EBADFD";
4333 break;
4334
4335 case VPPCOM_EAFNOSUPPORT:
4336 st = "VPPCOM_EAFNOSUPPORT";
4337 break;
4338
4339 case VPPCOM_ECONNABORTED:
4340 st = "VPPCOM_ECONNABORTED";
4341 break;
4342
4343 case VPPCOM_ECONNRESET:
4344 st = "VPPCOM_ECONNRESET";
4345 break;
4346
4347 case VPPCOM_ENOTCONN:
4348 st = "VPPCOM_ENOTCONN";
4349 break;
4350
4351 case VPPCOM_ECONNREFUSED:
4352 st = "VPPCOM_ECONNREFUSED";
4353 break;
4354
4355 case VPPCOM_ETIMEDOUT:
4356 st = "VPPCOM_ETIMEDOUT";
4357 break;
4358
4359 default:
4360 st = "UNKNOWN_STATE";
4361 break;
4362 }
4363
4364 return st;
4365}
4366
Florin Corasa5a9efd2021-01-05 17:03:29 -08004367int
4368vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4369{
4370 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004371 return vcl_sapi_add_cert_key_pair (ckpair);
4372 else
4373 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004374}
4375
4376int
4377vppcom_del_cert_key_pair (uint32_t ckpair_index)
4378{
4379 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004380 return vcl_sapi_del_cert_key_pair (ckpair_index);
4381 else
4382 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004383}
4384
Dave Wallacee22aa742017-10-20 12:30:38 -04004385/*
4386 * fd.io coding-style-patch-verification: ON
4387 *
4388 * Local Variables:
4389 * eval: (c-set-style "gnu")
4390 * End:
4391 */