blob: 5408fb79186781db7906246d500ad117d5ad50ea [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
Florin Coras32881932023-02-06 13:30:13 -080054void
55vcl_send_session_listen (vcl_worker_t *wrk, vcl_session_t *s)
Florin Coras458089b2019-08-21 16:20:44 -070056{
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 Coras32881932023-02-06 13:30:13 -080083 s->flags |= VCL_SESSION_F_PENDING_LISTEN;
Florin Coras458089b2019-08-21 16:20:44 -070084}
85
86static void
87vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
88{
89 app_session_evt_t _app_evt, *app_evt = &_app_evt;
90 session_connect_msg_t *mp;
91 svm_msg_q_t *mq;
92
93 mq = vcl_worker_ctrl_mq (wrk);
94 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
95 mp = (session_connect_msg_t *) app_evt->evt->data;
96 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070097 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070098 mp->context = s->session_index;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +000099 mp->dscp = s->dscp;
Florin Coras458089b2019-08-21 16:20:44 -0700100 mp->wrk_index = wrk->vpp_wrk_index;
101 mp->is_ip4 = s->transport.is_ip4;
102 mp->parent_handle = s->parent_handle;
103 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700104 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700105 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000106 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700107 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800108 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000109 if (s->flags & VCL_SESSION_F_CONNECTED)
110 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700111 if (s->ext_config)
112 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700113 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700114
115 if (s->ext_config)
116 {
117 clib_mem_free (s->ext_config);
118 s->ext_config = 0;
119 }
Florin Coras458089b2019-08-21 16:20:44 -0700120}
121
122void
123vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
124{
125 app_session_evt_t _app_evt, *app_evt = &_app_evt;
126 session_unlisten_msg_t *mp;
127 svm_msg_q_t *mq;
128
129 mq = vcl_worker_ctrl_mq (wrk);
130 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
131 mp = (session_unlisten_msg_t *) app_evt->evt->data;
132 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700133 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700134 mp->wrk_index = wrk->vpp_wrk_index;
135 mp->handle = s->vpp_handle;
136 mp->context = wrk->wrk_index;
137 app_send_ctrl_evt_to_vpp (mq, app_evt);
138}
139
140static void
liuyacan534468e2021-05-09 03:50:40 +0000141vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
142{
143 app_session_evt_t _app_evt, *app_evt = &_app_evt;
144 session_shutdown_msg_t *mp;
145 svm_msg_q_t *mq;
146
147 /* Send to thread that owns the session */
148 mq = s->vpp_evt_q;
149 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
150 mp = (session_shutdown_msg_t *) app_evt->evt->data;
151 memset (mp, 0, sizeof (*mp));
152 mp->client_index = wrk->api_client_handle;
153 mp->handle = s->vpp_handle;
154 app_send_ctrl_evt_to_vpp (mq, app_evt);
155}
156
157static void
Florin Coras458089b2019-08-21 16:20:44 -0700158vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
159{
160 app_session_evt_t _app_evt, *app_evt = &_app_evt;
161 session_disconnect_msg_t *mp;
162 svm_msg_q_t *mq;
163
164 /* Send to thread that owns the session */
165 mq = s->vpp_evt_q;
166 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
167 mp = (session_disconnect_msg_t *) app_evt->evt->data;
168 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700169 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700170 mp->handle = s->vpp_handle;
171 app_send_ctrl_evt_to_vpp (mq, app_evt);
172}
173
174static void
175vcl_send_app_detach (vcl_worker_t * wrk)
176{
177 app_session_evt_t _app_evt, *app_evt = &_app_evt;
178 session_app_detach_msg_t *mp;
179 svm_msg_q_t *mq;
180
181 mq = vcl_worker_ctrl_mq (wrk);
182 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
183 mp = (session_app_detach_msg_t *) app_evt->evt->data;
184 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700185 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700186 app_send_ctrl_evt_to_vpp (mq, app_evt);
187}
Florin Coras697faea2018-06-27 17:10:49 -0700188
Florin Coras54693d22018-07-17 10:46:29 -0700189static void
190vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
191 session_handle_t handle, int retval)
192{
193 app_session_evt_t _app_evt, *app_evt = &_app_evt;
194 session_accepted_reply_msg_t *rmp;
195 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
196 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
197 rmp->handle = handle;
198 rmp->context = context;
199 rmp->retval = retval;
200 app_send_ctrl_evt_to_vpp (mq, app_evt);
201}
202
Florin Coras99368312018-08-02 10:45:44 -0700203static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800204vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
205 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700206{
207 app_session_evt_t _app_evt, *app_evt = &_app_evt;
208 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800209 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700210 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
211 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800212 rmp->handle = s->vpp_handle;
213 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700214 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800215 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700216}
217
Florin Corasc9fbd662018-08-24 12:59:56 -0700218static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800219vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
220 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700221{
222 app_session_evt_t _app_evt, *app_evt = &_app_evt;
223 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800224 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
225 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700226 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800227 rmp->handle = s->vpp_handle;
228 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700229 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800230 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700231}
232
Florin Coras30e79c22019-01-02 19:31:22 -0800233void
234vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
235 u32 wrk_index)
236{
237 app_session_evt_t _app_evt, *app_evt = &_app_evt;
238 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800239
Florin Coras52dd29f2020-11-18 19:02:17 -0800240 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
241 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800242 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700243 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800244 mp->handle = s->vpp_handle;
245 mp->req_wrk_index = wrk->vpp_wrk_index;
246 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800247 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800248}
249
hanlina3a48962020-07-13 11:09:15 +0800250int
Florin Coras40c07ce2020-07-16 20:46:17 -0700251vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
252{
253 app_session_evt_t _app_evt, *app_evt = &_app_evt;
254 session_app_wrk_rpc_msg_t *mp;
255 vcl_worker_t *dst_wrk, *wrk;
256 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800257 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700258
259 if (data_len > sizeof (mp->data))
260 goto done;
261
262 clib_spinlock_lock (&vcm->workers_lock);
263
264 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
265 if (!dst_wrk)
266 goto done;
267
268 wrk = vcl_worker_get_current ();
269 mq = vcl_worker_ctrl_mq (wrk);
270 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
271 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700272 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700273 mp->wrk_index = dst_wrk->vpp_wrk_index;
274 clib_memcpy (mp->data, data, data_len);
275 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800276 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700277
278done:
279 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800280 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700281}
282
Florin Coras04ae8272021-04-12 19:55:37 -0700283int
284vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
285 transport_endpt_attr_t *attr)
286{
287 app_session_evt_t _app_evt, *app_evt = &_app_evt;
288 session_transport_attr_msg_t *mp;
289 svm_msg_q_t *mq;
290 f64 timeout;
291
292 ASSERT (!wrk->session_attr_op);
Liangxing Wang22112772022-05-13 04:24:19 +0000293 mq = s->vpp_evt_q;
294 if (PREDICT_FALSE (!mq))
295 {
296 /* FIXME: attribute should be stored and sent once session is
297 * bound/connected to vpp */
298 return 0;
299 }
300
Florin Coras04ae8272021-04-12 19:55:37 -0700301 wrk->session_attr_op = 1;
302 wrk->session_attr_op_rv = -1;
303
Florin Coras04ae8272021-04-12 19:55:37 -0700304 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
305 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
306 memset (mp, 0, sizeof (*mp));
307 mp->client_index = wrk->api_client_handle;
308 mp->handle = s->vpp_handle;
309 mp->is_get = is_get;
310 mp->attr = *attr;
311 app_send_ctrl_evt_to_vpp (mq, app_evt);
312
313 timeout = clib_time_now (&wrk->clib_time) + 1;
314
315 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
316 vcl_flush_mq_events ();
317
318 if (!wrk->session_attr_op_rv && is_get)
319 *attr = wrk->session_attr_rv;
320
321 wrk->session_attr_op = 0;
322
323 return wrk->session_attr_op_rv;
324}
325
Florin Coras54693d22018-07-17 10:46:29 -0700326static u32
Florin Coras00cca802019-06-06 09:38:44 -0700327vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
328 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700329{
330 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700331 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700332
Florin Coras134a9962018-08-28 11:32:04 -0700333 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700334
Florin Coras00cca802019-06-06 09:38:44 -0700335 listen_session = vcl_session_get (wrk, ls_index);
336 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700337 {
Florin Coras00cca802019-06-06 09:38:44 -0700338 VDBG (0, "ERROR: listener handle %lu does not match session %u",
339 mp->listener_handle, ls_index);
340 goto error;
341 }
342
Florin Corasf6e284b2021-07-21 18:17:20 -0700343 if (vcl_segment_attach_session (
344 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
345 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700346 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800347 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
348 session->session_index, mp->handle);
Florin Coras00cca802019-06-06 09:38:44 -0700349 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700350 }
351
Florin Coras54693d22018-07-17 10:46:29 -0700352 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700353 session->session_state = VCL_STATE_READY;
qinyangaf9b7152023-06-27 01:11:53 -0700354 if (mp->rmt.is_ip4)
355 {
356 session->original_dst_ip4 = mp->original_dst_ip4;
357 session->original_dst_port = mp->original_dst_port;
358 }
Florin Coras09d18c22019-04-24 11:10:02 -0700359 session->transport.rmt_port = mp->rmt.port;
360 session->transport.is_ip4 = mp->rmt.is_ip4;
361 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500362 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700363
Florin Coras134a9962018-08-28 11:32:04 -0700364 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800365 session->transport.lcl_port = mp->lcl.port;
366 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700367 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200368 session->is_dgram = vcl_proto_is_dgram (session->session_type);
Florin Coras89627e82023-04-27 13:38:35 -0700369 if (session->is_dgram)
370 session->flags |= (listen_session->flags & VCL_SESSION_F_CONNECTED);
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200371 session->listener_index = listen_session->session_index;
372 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700373
Florin Coras54693d22018-07-17 10:46:29 -0700374 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
375
Florin Coras00cca802019-06-06 09:38:44 -0700376 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
377 session->vpp_handle, 0);
378
Florin Coras134a9962018-08-28 11:32:04 -0700379 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700380
381error:
Florin Corasb4624182020-12-11 13:58:12 -0800382 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
383 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700384 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
385 VNET_API_ERROR_INVALID_ARGUMENT);
386 vcl_session_free (wrk, session);
387 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700388}
389
390static u32
Florin Coras134a9962018-08-28 11:32:04 -0700391vcl_session_connected_handler (vcl_worker_t * wrk,
392 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700393{
Florin Coras99368312018-08-02 10:45:44 -0700394 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800395 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700396
397 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700398 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800399 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700400 {
Florin Coras8d42c752021-11-29 23:26:19 -0800401 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
402 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800403 /* Should not happen but if it does, force vpp session cleanup */
404 vcl_session_t tmp_session = {
405 .vpp_handle = mp->handle,
406 .vpp_evt_q = 0,
407 };
408 vcl_segment_attach_session (
409 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
410 mp->vpp_event_queue_address, mp->mq_index, 0, session);
411 if (tmp_session.vpp_evt_q)
412 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700413 return VCL_INVALID_SESSION_INDEX;
414 }
Florin Coras8d42c752021-11-29 23:26:19 -0800415
Florin Coras54693d22018-07-17 10:46:29 -0700416 if (mp->retval)
417 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800418 VDBG (0, "session %u: connect failed! %U", session_index,
Florin Coras8d42c752021-11-29 23:26:19 -0800419 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700420 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800421 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +0530422 session->vpp_error = mp->retval;
Florin Coras070453d2018-08-24 17:04:27 -0700423 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700424 }
425
Florin Corasdbc9c592019-09-25 16:37:43 -0700426 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800427
Florin Coras8d42c752021-11-29 23:26:19 -0800428 /* Add to lookup table. Even if something fails, session cannot be
429 * cleaned up prior to notifying vpp and going through the cleanup
430 * "procedure" see @ref vcl_session_cleanup_handler */
431 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
432
Florin Corasf6e284b2021-07-21 18:17:20 -0700433 if (vcl_segment_attach_session (
434 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
435 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800436 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800437 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
438 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800439 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700440 vcl_send_session_disconnect (wrk, session);
441 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800442 }
443
Florin Coras653e43f2019-03-04 10:56:23 -0800444 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700445 {
Florin Corasc547e912020-12-08 17:50:45 -0800446 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700447 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
448 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800449 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800450 VDBG (0, "session %u [0x%llx]: failed to attach ct fifos",
451 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800452 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700453 vcl_send_session_disconnect (wrk, session);
454 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800455 }
Florin Coras99368312018-08-02 10:45:44 -0700456 }
Florin Coras54693d22018-07-17 10:46:29 -0700457
Florin Coras09d18c22019-04-24 11:10:02 -0700458 session->transport.is_ip4 = mp->lcl.is_ip4;
459 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500460 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700461 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700462
463 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700464 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700465 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700466 vcl_send_session_disconnect (wrk, session);
467 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700468 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700469
Florin Coras5ffb9642021-12-03 17:24:13 -0800470 VDBG (0, "session %u [0x%llx] connected local: %U:%u remote %U:%u",
471 session->session_index, session->vpp_handle, vcl_format_ip46_address,
472 &session->transport.lcl_ip,
473 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
474 clib_net_to_host_u16 (session->transport.lcl_port),
475 vcl_format_ip46_address, &session->transport.rmt_ip,
476 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
477 clib_net_to_host_u16 (session->transport.rmt_port));
Florin Coras070453d2018-08-24 17:04:27 -0700478
Florin Coras54693d22018-07-17 10:46:29 -0700479 return session_index;
480}
481
Florin Coras3c7d4f92018-12-14 11:28:43 -0800482static int
483vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
484{
485 vcl_session_msg_t *accepted_msg;
486 int i;
487
488 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
489 {
490 accepted_msg = &session->accept_evts_fifo[i];
491 if (accepted_msg->accepted_msg.handle == handle)
492 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800493 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800494 return 1;
495 }
496 }
497 return 0;
498}
499
Florin Corasc9fbd662018-08-24 12:59:56 -0700500static u32
Florin Coras134a9962018-08-28 11:32:04 -0700501vcl_session_reset_handler (vcl_worker_t * wrk,
502 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700503{
504 vcl_session_t *session;
505 u32 sid;
506
Florin Coras134a9962018-08-28 11:32:04 -0700507 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
508 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700509 if (!session)
510 {
511 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
512 return VCL_INVALID_SESSION_INDEX;
513 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800514
515 /* Caught a reset before actually accepting the session */
wanghanlin696db202023-08-07 17:23:53 +0800516 if (session->session_state == VCL_STATE_LISTEN ||
517 session->session_state == VCL_STATE_LISTEN_NO_MQ)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800518 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800519 if (!vcl_flag_accepted_session (session, reset_msg->handle,
520 VCL_ACCEPTED_F_RESET))
521 VDBG (0, "session was not accepted!");
522 return VCL_INVALID_SESSION_INDEX;
523 }
524
Florin Corasc127d5a2020-10-14 16:35:58 -0700525 if (session->session_state != VCL_STATE_CLOSED)
526 session->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +0800527
528 session->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Coras5ffb9642021-12-03 17:24:13 -0800529 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700530 return sid;
531}
532
Florin Coras60116992018-08-27 09:52:18 -0700533static u32
Florin Coras134a9962018-08-28 11:32:04 -0700534vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700535{
536 vcl_session_t *session;
537 u32 sid = mp->context;
538
Florin Coras134a9962018-08-28 11:32:04 -0700539 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700540 if (mp->retval)
541 {
Florin Coras5e062572019-03-14 19:07:51 -0700542 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700543 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700544 if (session)
545 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700546 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700547 session->vpp_handle = mp->handle;
Mohammed Hawarif4e3ee12023-10-30 10:52:19 +0100548 session->vpp_error = mp->retval;
Florin Coras60116992018-08-27 09:52:18 -0700549 return sid;
550 }
551 else
552 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800553 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
554 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700555 return VCL_INVALID_SESSION_INDEX;
556 }
557 }
558
559 session->vpp_handle = mp->handle;
560 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500561 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
562 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700563 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700564 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700565 session->session_state = VCL_STATE_LISTEN;
Florin Coras32881932023-02-06 13:30:13 -0800566 session->flags &= ~VCL_SESSION_F_PENDING_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800567
Florin Coras6e3c1f82020-01-15 01:30:46 +0000568 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700569 {
Florin Corasc547e912020-12-08 17:50:45 -0800570 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700571 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
572 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800573 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800574 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
575 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800576 session->session_state = VCL_STATE_DETACHED;
577 return VCL_INVALID_SESSION_INDEX;
578 }
Florin Coras60116992018-08-27 09:52:18 -0700579 }
580
Florin Coras05ecfcc2018-12-12 18:19:39 -0800581 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700582 return sid;
583}
584
Florin Corasdfae9f92019-02-20 19:48:31 -0800585static void
586vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
587{
588 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
589 vcl_session_t *s;
590
591 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000592 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800593 {
594 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
595 return;
596 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700597 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000598 {
599 /* Connected udp listener */
600 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700601 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000602 return;
603
604 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
605 return;
606 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800607
608 if (mp->retval)
609 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700610 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800611
612 if (mp->context != wrk->wrk_index)
613 VDBG (0, "wrong context");
614
615 vcl_session_table_del_vpp_handle (wrk, mp->handle);
616 vcl_session_free (wrk, s);
617}
618
Florin Coras68b7e582020-01-21 18:33:23 -0800619static void
620vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
621{
622 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
623 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800624 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800625
626 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
627 if (!s)
628 {
629 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
630 return;
631 }
632
Florin Coras1bb74942021-02-10 15:26:37 -0800633 /* Only validate if a value is provided */
634 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800635 {
Florin Coras1bb74942021-02-10 15:26:37 -0800636 fs_index = vcl_segment_table_lookup (mp->segment_handle);
637 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
638 {
639 VDBG (0, "segment %lx for session %u is not mounted!",
640 mp->segment_handle, s->session_index);
641 s->session_state = VCL_STATE_DETACHED;
642 return;
643 }
Florin Corasc547e912020-12-08 17:50:45 -0800644 }
645
Florin Coras57660d92020-04-04 22:45:34 +0000646 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800647
648 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
649 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800650
Florin Coras68b7e582020-01-21 18:33:23 -0800651 vcl_session_table_del_vpp_handle (wrk, mp->handle);
652 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
653
654 /* Generate new tx event if we have outstanding data */
655 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800656 app_send_io_evt_to_vpp (s->vpp_evt_q,
657 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800658 SESSION_IO_EVT_TX, SVM_Q_WAIT);
659
Florin Coras57660d92020-04-04 22:45:34 +0000660 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800661 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800662}
663
Florin Coras3c7d4f92018-12-14 11:28:43 -0800664static vcl_session_t *
665vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
666{
667 vcl_session_msg_t *vcl_msg;
668 vcl_session_t *session;
669
670 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
671 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800672 VWRN ("session overlap handle %lu state %u!", msg->handle,
673 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800674
675 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
676 if (!session)
677 {
678 VERR ("couldn't find listen session: listener handle %llx",
679 msg->listener_handle);
680 return 0;
681 }
682
683 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000684 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800685 vcl_msg->accepted_msg = *msg;
686 /* Session handle points to listener until fully accepted by app */
687 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
688
689 return session;
690}
691
692static vcl_session_t *
693vcl_session_disconnected_handler (vcl_worker_t * wrk,
694 session_disconnected_msg_t * msg)
695{
696 vcl_session_t *session;
697
698 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
699 if (!session)
700 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800701 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800702 return 0;
703 }
704
Florin Corasadcfb152020-02-12 08:50:29 +0000705 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700706 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000707 return 0;
708
Florin Coras3c7d4f92018-12-14 11:28:43 -0800709 /* Caught a disconnect before actually accepting the session */
wanghanlin696db202023-08-07 17:23:53 +0800710 if (session->session_state == VCL_STATE_LISTEN ||
711 session->session_state == VCL_STATE_LISTEN_NO_MQ)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800712 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800713 if (!vcl_flag_accepted_session (session, msg->handle,
714 VCL_ACCEPTED_F_CLOSED))
715 VDBG (0, "session was not accepted!");
716 return 0;
717 }
718
Florin Corasadcfb152020-02-12 08:50:29 +0000719 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700720 if (session->session_state != VCL_STATE_DISCONNECT)
721 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000722
Florin Coras3c7d4f92018-12-14 11:28:43 -0800723 return session;
724}
725
liuyacan534468e2021-05-09 03:50:40 +0000726int
liuyacan55c952e2021-06-13 14:54:55 +0800727vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000728{
729 vcl_worker_t *wrk = vcl_worker_get_current ();
730 vcl_session_t *session;
731 vcl_session_state_t state;
732 u64 vpp_handle;
733
734 session = vcl_session_get_w_handle (wrk, session_handle);
735 if (PREDICT_FALSE (!session))
736 return VPPCOM_EBADFD;
737
738 vpp_handle = session->vpp_handle;
739 state = session->session_state;
740
741 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800742 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000743
744 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
745 {
746 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
747 return VPPCOM_EBADFD;
748 }
749
liuyacan55c952e2021-06-13 14:54:55 +0800750 if (how == SHUT_RD || how == SHUT_RDWR)
751 {
752 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
753 if (how == SHUT_RD)
754 return VPPCOM_OK;
755 }
756 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
757
liuyacan534468e2021-05-09 03:50:40 +0000758 if (PREDICT_TRUE (state == VCL_STATE_READY))
759 {
760 VDBG (1, "session %u [0x%llx]: sending shutdown...",
761 session->session_index, vpp_handle);
762
763 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000764 }
765
766 return VPPCOM_OK;
767}
768
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700769static int
770vppcom_session_disconnect (u32 session_handle)
771{
772 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700773 vcl_session_t *session, *listen_session;
774 vcl_session_state_t state;
775 u64 vpp_handle;
776
777 session = vcl_session_get_w_handle (wrk, session_handle);
778 if (!session)
779 return VPPCOM_EBADFD;
780
781 vpp_handle = session->vpp_handle;
782 state = session->session_state;
783
Florin Coras5ffb9642021-12-03 17:24:13 -0800784 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
785 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700786
787 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
788 {
789 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
790 return VPPCOM_EBADFD;
791 }
792
793 if (state == VCL_STATE_VPP_CLOSING)
794 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800795 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700796 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
797 session->session_index, vpp_handle);
798 }
799 else
800 {
801 /* Session doesn't have an event queue yet. Probably a non-blocking
802 * connect. Wait for the reply */
803 if (PREDICT_FALSE (!session->vpp_evt_q))
804 return VPPCOM_OK;
805
Florin Coras5ffb9642021-12-03 17:24:13 -0800806 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700807 session->session_index, vpp_handle);
808 vcl_send_session_disconnect (wrk, session);
809 }
810
811 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
812 {
813 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700814 if (listen_session)
815 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700816 }
817
818 return VPPCOM_OK;
819}
820
Florin Coras30e79c22019-01-02 19:31:22 -0800821static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700822vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
823{
824 session_cleanup_msg_t *msg;
825 vcl_session_t *session;
826
827 msg = (session_cleanup_msg_t *) data;
828 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
829 if (!session)
830 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800831 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700832 return;
833 }
834
Florin Coras36d49392020-04-24 23:00:11 +0000835 if (msg->type == SESSION_CLEANUP_TRANSPORT)
836 {
837 /* Transport was cleaned up before we confirmed close. Probably the
838 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700839 * Confirm close to make sure everything is cleaned up.
840 * Move to undetermined state to ensure that the session is not
841 * removed before both vpp and the app cleanup.
842 * - If the app closes first, the session is moved to CLOSED state
843 * and the session cleanup notification from vpp removes the
844 * session.
845 * - If vpp cleans up the session first, the session is moved to
846 * DETACHED state lower and subsequently the close from the app
847 * frees the session
848 */
849 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700850 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700851 vppcom_session_disconnect (vcl_session_handle (session));
852 session->session_state = VCL_STATE_UPDATED;
853 }
854 else if (session->session_state == VCL_STATE_DISCONNECT)
855 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800856 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700857 session->session_state = VCL_STATE_UPDATED;
858 }
Florin Coras36d49392020-04-24 23:00:11 +0000859 return;
860 }
861
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800862 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700863 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800864
865 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700866 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000867 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800868 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700869 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000870 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
871 return;
872 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800873
874 /* Session probably tracked with epoll, disconnect not yet handled and
875 * 1) both transport and session cleanup completed 2) app closed. Wait
876 * until message is drained to free the session.
877 * See @ref vcl_handle_mq_event */
878 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
879 {
880 session->flags |= VCL_SESSION_F_PENDING_FREE;
881 return;
882 }
883
Florin Coras9ace36d2019-10-28 13:14:17 -0700884 vcl_session_free (wrk, session);
885}
886
887static void
Florin Coras30e79c22019-01-02 19:31:22 -0800888vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
889{
890 session_req_worker_update_msg_t *msg;
891 vcl_session_t *s;
892
893 msg = (session_req_worker_update_msg_t *) data;
894 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
895 if (!s)
896 return;
897
898 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
899}
900
901static void
902vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
903{
904 session_worker_update_reply_msg_t *msg;
905 vcl_session_t *s;
906
907 msg = (session_worker_update_reply_msg_t *) data;
908 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
909 if (!s)
910 {
911 VDBG (0, "unknown handle 0x%llx", msg->handle);
912 return;
913 }
Florin Coras30e79c22019-01-02 19:31:22 -0800914
Florin Coras5f45e012019-01-23 09:21:30 -0800915 if (s->rx_fifo)
916 {
Florin Corasc547e912020-12-08 17:50:45 -0800917 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700918 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800919 {
920 VDBG (0, "failed to attach fifos for %u", s->session_index);
921 return;
922 }
Florin Coras5f45e012019-01-23 09:21:30 -0800923 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700924 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800925
Florin Coras30e79c22019-01-02 19:31:22 -0800926 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
927 s->vpp_handle, wrk->wrk_index);
928}
929
Florin Coras935ce752020-09-08 22:43:47 -0700930static int
931vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
932{
933
934 if (vcm->cfg.vpp_app_socket_api)
935 return vcl_sapi_recv_fds (wrk, fds, n_fds);
936
937 return vcl_bapi_recv_fds (wrk, fds, n_fds);
938}
939
Florin Corasc4c4cf52019-08-24 18:17:34 -0700940static void
941vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
942{
943 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
944 session_app_add_segment_msg_t *msg;
945 u64 segment_handle;
946 int fd = -1;
947
948 msg = (session_app_add_segment_msg_t *) data;
949
950 if (msg->fd_flags)
951 {
Florin Coras935ce752020-09-08 22:43:47 -0700952 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700953 seg_type = SSVM_SEGMENT_MEMFD;
954 }
955
956 segment_handle = msg->segment_handle;
957 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
958 {
959 clib_warning ("invalid segment handle");
960 return;
961 }
962
963 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
964 seg_type, fd))
965 {
966 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
967 return;
968 }
969
970 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
971 msg->segment_size);
972}
973
974static void
975vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
976{
977 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
978 vcl_segment_detach (msg->segment_handle);
Florin Coras1f40ab42023-06-13 17:18:56 -0700979 VDBG (1, "Unmapped segment: %lx", msg->segment_handle);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700980}
981
Florin Coras40c07ce2020-07-16 20:46:17 -0700982static void
983vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
984{
985 if (!vcm->wrk_rpc_fn)
986 return;
987
hanlina3a48962020-07-13 11:09:15 +0800988 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700989}
990
Florin Coras04ae8272021-04-12 19:55:37 -0700991static void
992vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
993{
994 session_transport_attr_reply_msg_t *mp;
995
996 if (!wrk->session_attr_op)
997 return;
998
999 mp = (session_transport_attr_reply_msg_t *) data;
1000
1001 wrk->session_attr_op_rv = mp->retval;
1002 wrk->session_attr_op = 0;
1003 wrk->session_attr_rv = mp->attr;
1004}
1005
Florin Coras86f04502018-09-12 16:08:01 -07001006static int
1007vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001008{
Florin Coras54693d22018-07-17 10:46:29 -07001009 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001010 session_connected_msg_t *connected_msg;
1011 session_reset_msg_t *reset_msg;
1012 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001013 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001014 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001015
1016 switch (e->event_type)
1017 {
Florin Coras653e43f2019-03-04 10:56:23 -08001018 case SESSION_IO_EVT_RX:
1019 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001020 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001021 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001022 break;
Florin Coras86f04502018-09-12 16:08:01 -07001023 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001024 break;
Florin Corasb242d312020-10-26 15:35:40 -07001025 case SESSION_CTRL_EVT_BOUND:
1026 /* We can only wait for only one listen so not postponed */
1027 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1028 break;
Florin Coras54693d22018-07-17 10:46:29 -07001029 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001030 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1031 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1032 {
1033 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1034 *ecpy = *e;
1035 ecpy->postponed = 1;
1036 ecpy->session_index = s->session_index;
1037 }
Florin Coras54693d22018-07-17 10:46:29 -07001038 break;
1039 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001040 connected_msg = (session_connected_msg_t *) e->data;
1041 sid = vcl_session_connected_handler (wrk, connected_msg);
1042 if (!(s = vcl_session_get (wrk, sid)))
1043 break;
1044 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1045 {
1046 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1047 *ecpy = *e;
1048 ecpy->postponed = 1;
1049 ecpy->session_index = s->session_index;
1050 }
Florin Coras54693d22018-07-17 10:46:29 -07001051 break;
1052 case SESSION_CTRL_EVT_DISCONNECTED:
1053 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001054 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1055 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001056 if (s->session_state == VCL_STATE_CLOSED)
1057 break;
wanghanlin4747b342023-08-08 10:40:04 +08001058 /* We do not postpone for blocking sessions or listen sessions because:
1059 * 1. Blocking sessions are not part of epoll instead they're used in a
1060 * synchronous manner, such as read/write and etc.
1061 * 2. Listen sessions that have not yet been accepted can't change to
1062 * VPP_CLOSING state instead can been marked as ACCEPTED_F_CLOSED.
1063 */
1064 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK) &&
1065 !(s->session_state == VCL_STATE_LISTEN ||
1066 s->session_state == VCL_STATE_LISTEN_NO_MQ))
Florin Corasb242d312020-10-26 15:35:40 -07001067 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001068 s->session_state = VCL_STATE_VPP_CLOSING;
1069 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1070 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1071 *ecpy = *e;
1072 ecpy->postponed = 1;
1073 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001074 break;
1075 }
1076 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001077 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001078 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1079 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001080 break;
1081 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001082 reset_msg = (session_reset_msg_t *) e->data;
1083 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1084 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001085 if (s->session_state == VCL_STATE_CLOSED)
1086 break;
wanghanlin4747b342023-08-08 10:40:04 +08001087 /* We do not postpone for blocking sessions or listen sessions because:
1088 * 1. Blocking sessions are not part of epoll instead they're used in a
1089 * synchronous manner, such as read/write and etc.
1090 * 2. Listen sessions that have not yet been accepted can't change to
1091 * DISCONNECT state instead can been marked as ACCEPTED_F_RESET.
1092 */
1093 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK) &&
1094 !(s->session_state == VCL_STATE_LISTEN ||
1095 s->session_state == VCL_STATE_LISTEN_NO_MQ))
Florin Corasb242d312020-10-26 15:35:40 -07001096 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001097 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1098 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001099 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001100 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1101 *ecpy = *e;
1102 ecpy->postponed = 1;
1103 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001104 break;
1105 }
Florin Coras134a9962018-08-28 11:32:04 -07001106 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001107 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001108 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1109 vcl_session_unlisten_reply_handler (wrk, e->data);
1110 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001111 case SESSION_CTRL_EVT_MIGRATED:
1112 vcl_session_migrated_handler (wrk, e->data);
1113 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001114 case SESSION_CTRL_EVT_CLEANUP:
1115 vcl_session_cleanup_handler (wrk, e->data);
1116 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001117 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1118 vcl_session_req_worker_update_handler (wrk, e->data);
1119 break;
1120 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1121 vcl_session_worker_update_reply_handler (wrk, e->data);
1122 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001123 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1124 vcl_session_app_add_segment_handler (wrk, e->data);
1125 break;
1126 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1127 vcl_session_app_del_segment_handler (wrk, e->data);
1128 break;
hanlina3a48962020-07-13 11:09:15 +08001129 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001130 vcl_worker_rpc_handler (wrk, e->data);
1131 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001132 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1133 vcl_session_transport_attr_reply_handler (wrk, e->data);
1134 break;
Florin Coras54693d22018-07-17 10:46:29 -07001135 default:
1136 clib_warning ("unhandled %u", e->event_type);
1137 }
1138 return VPPCOM_OK;
1139}
1140
Florin Coras30e79c22019-01-02 19:31:22 -08001141static int
Florin Coras697faea2018-06-27 17:10:49 -07001142vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001143 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001144 f64 wait_for_time)
1145{
Florin Coras134a9962018-08-28 11:32:04 -07001146 vcl_worker_t *wrk = vcl_worker_get_current ();
1147 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001148 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001149 svm_msg_q_msg_t msg;
1150 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001151
Florin Coras697faea2018-06-27 17:10:49 -07001152 do
Dave Wallace543852a2017-08-03 02:11:34 -04001153 {
Florin Coras134a9962018-08-28 11:32:04 -07001154 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001155 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001156 {
Florin Coras070453d2018-08-24 17:04:27 -07001157 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001158 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001159 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001160 {
Florin Coras697faea2018-06-27 17:10:49 -07001161 return VPPCOM_OK;
1162 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001163 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001164 {
Mohammed Hawarif4e3ee12023-10-30 10:52:19 +01001165 if (session->vpp_error == SESSION_E_ALREADY_LISTENING)
1166 return VPPCOM_EADDRINUSE;
1167 else
1168 return VPPCOM_ECONNREFUSED;
Florin Coras697faea2018-06-27 17:10:49 -07001169 }
Florin Coras54693d22018-07-17 10:46:29 -07001170
Florin Coras134a9962018-08-28 11:32:04 -07001171 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001172 {
1173 usleep (100);
1174 continue;
1175 }
Florin Coras134a9962018-08-28 11:32:04 -07001176 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001177 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001178 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001179 }
Florin Coras134a9962018-08-28 11:32:04 -07001180 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001181
Florin Coras05ecfcc2018-12-12 18:19:39 -08001182 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001183 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001184 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001185
Florin Coras697faea2018-06-27 17:10:49 -07001186 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001187}
1188
Florin Coras30e79c22019-01-02 19:31:22 -08001189static void
1190vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1191{
Florin Coras288eaab2019-02-03 15:26:14 -08001192 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001193 vcl_session_t *s;
1194 u32 *sip;
1195
1196 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1197 return;
1198
1199 vec_foreach (sip, wrk->pending_session_wrk_updates)
1200 {
1201 s = vcl_session_get (wrk, *sip);
1202 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1203 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001204 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1205 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001206 s->session_state = state;
1207 }
1208 vec_reset_length (wrk->pending_session_wrk_updates);
1209}
1210
Florin Corasf9240dc2019-01-15 08:03:17 -08001211void
Florin Coras5398dfb2021-01-25 20:31:27 -08001212vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001213{
Florin Coras30e79c22019-01-02 19:31:22 -08001214 svm_msg_q_msg_t *msg;
1215 session_event_t *e;
1216 svm_msg_q_t *mq;
1217 int i;
1218
1219 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001220 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001221
1222 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1223 {
1224 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1225 e = svm_msg_q_msg_data (mq, msg);
1226 vcl_handle_mq_event (wrk, e);
1227 svm_msg_q_free_msg (mq, msg);
1228 }
1229 vec_reset_length (wrk->mq_msg_vector);
1230 vcl_handle_pending_wrk_updates (wrk);
1231}
1232
Florin Coras5398dfb2021-01-25 20:31:27 -08001233void
1234vcl_flush_mq_events (void)
1235{
1236 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1237}
1238
Florin Coras697faea2018-06-27 17:10:49 -07001239static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001240vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001241{
Florin Coras134a9962018-08-28 11:32:04 -07001242 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001243 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001244 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001245 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001246
Florin Corasab2f6db2018-08-31 14:31:41 -07001247 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001248 if (!session)
1249 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001250
Florin Corasaaff5ee2019-07-08 13:00:00 -07001251 /* Flush pending accept events, if any */
1252 while (clib_fifo_elts (session->accept_evts_fifo))
1253 {
1254 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1255 accepted_msg = &evt->accepted_msg;
1256 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1257 vcl_send_session_accepted_reply (session->vpp_evt_q,
1258 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001259 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001260 }
1261 clib_fifo_free (session->accept_evts_fifo);
1262
Florin Coras458089b2019-08-21 16:20:44 -07001263 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001264
Florin Coras32881932023-02-06 13:30:13 -08001265 VDBG (0, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001266 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001267 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001268
Florin Coras980ee742023-08-04 16:14:01 -07001269 session->vpp_handle = SESSION_INVALID_HANDLE;
Florin Corasc127d5a2020-10-14 16:35:58 -07001270 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001271
Florin Coras070453d2018-08-24 17:04:27 -07001272 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001273}
1274
Florin Coras940f78f2018-11-30 12:11:20 -08001275/**
1276 * Handle app exit
1277 *
1278 * Notify vpp of the disconnect and mark the worker as free. If we're the
1279 * last worker, do a full cleanup otherwise, since we're probably a forked
1280 * child, avoid syscalls as much as possible. We might've lost privileges.
1281 */
1282void
1283vppcom_app_exit (void)
1284{
1285 if (!pool_elts (vcm->workers))
1286 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001287 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1288 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001289 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001290}
1291
Florin Coras935ce752020-09-08 22:43:47 -07001292static int
1293vcl_api_attach (void)
1294{
1295 if (vcm->cfg.vpp_app_socket_api)
1296 return vcl_sapi_attach ();
1297
1298 return vcl_bapi_attach ();
1299}
1300
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001301int
1302vcl_is_first_reattach_to_execute ()
1303{
1304 if (vcm->reattach_count == 0)
1305 return 1;
1306
1307 return 0;
1308}
1309
1310void
1311vcl_set_reattach_counter ()
1312{
1313 ++vcm->reattach_count;
1314
1315 if (vcm->reattach_count == vec_len (vcm->workers))
1316 vcm->reattach_count = 0;
1317}
1318
1319/**
1320 * Reattach vcl to vpp after it has previously been disconnected.
1321 *
1322 * The logic should be:
1323 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1324 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1325 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1326 * from `vppcom_worker_register` since they were already allocated.
1327 */
1328
Florin Coras935ce752020-09-08 22:43:47 -07001329static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001330vcl_api_retry_attach (vcl_worker_t *wrk)
1331{
1332 vcl_session_t *s;
1333
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001334 clib_spinlock_lock (&vcm->workers_lock);
1335 if (vcl_is_first_reattach_to_execute ())
1336 {
1337 if (vcl_api_attach ())
1338 {
1339 clib_spinlock_unlock (&vcm->workers_lock);
1340 return;
1341 }
1342 vcl_set_reattach_counter ();
1343 clib_spinlock_unlock (&vcm->workers_lock);
1344 }
1345 else
1346 {
1347 vcl_set_reattach_counter ();
1348 clib_spinlock_unlock (&vcm->workers_lock);
1349 vcl_worker_register_with_vpp ();
1350 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001351
1352 /* Treat listeners as configuration that needs to be re-added to vpp */
1353 pool_foreach (s, wrk->sessions)
1354 {
1355 if (s->flags & VCL_SESSION_F_IS_VEP)
1356 continue;
1357 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1358 vppcom_session_listen (vcl_session_handle (s), 10);
1359 else
1360 VDBG (0, "internal error: unexpected state %d", s->session_state);
1361 }
1362}
1363
1364static void
1365vcl_api_handle_disconnect (vcl_worker_t *wrk)
1366{
1367 wrk->api_client_handle = ~0;
1368 vcl_worker_detach_sessions (wrk);
1369}
1370
1371static void
Florin Coras935ce752020-09-08 22:43:47 -07001372vcl_api_detach (vcl_worker_t * wrk)
1373{
Florin Corasd04ea442022-03-30 16:08:25 -07001374 if (wrk->api_client_handle == ~0)
1375 return;
1376
Florin Coras935ce752020-09-08 22:43:47 -07001377 vcl_send_app_detach (wrk);
1378
1379 if (vcm->cfg.vpp_app_socket_api)
1380 return vcl_sapi_detach (wrk);
1381
1382 return vcl_bapi_disconnect_from_vpp ();
1383}
1384
Dave Wallace543852a2017-08-03 02:11:34 -04001385/*
1386 * VPPCOM Public API functions
1387 */
1388int
Florin Coras66ec4672020-06-15 07:59:40 -07001389vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001390{
Dave Wallace543852a2017-08-03 02:11:34 -04001391 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001392 int rv;
1393
Florin Coras47c40e22018-11-26 17:01:36 -08001394 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001395 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001396 VDBG (1, "already initialized");
1397 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001398 }
1399
Florin Coras47c40e22018-11-26 17:01:36 -08001400 vcm->is_init = 1;
1401 vppcom_cfg (&vcm->cfg);
1402 vcl_cfg = &vcm->cfg;
1403
1404 vcm->main_cpu = pthread_self ();
1405 vcm->main_pid = getpid ();
1406 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001407 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1408 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001409 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1410 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001411 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001412 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001413 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001414
1415 /* Allocate default worker */
1416 vcl_worker_alloc_and_init ();
1417
Florin Coras935ce752020-09-08 22:43:47 -07001418 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001419 {
1420 vppcom_app_destroy ();
1421 return rv;
1422 }
Florin Coras47c40e22018-11-26 17:01:36 -08001423
1424 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001425 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001426
1427 return VPPCOM_OK;
1428}
1429
1430void
1431vppcom_app_destroy (void)
1432{
Florin Corasdc0ded72020-04-30 02:59:55 +00001433 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001434 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001435
Florin Coras940f78f2018-11-30 12:11:20 -08001436 if (!pool_elts (vcm->workers))
1437 return;
1438
Florin Coras0d427d82018-06-27 03:24:07 -07001439 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001440
Florin Corasdc0ded72020-04-30 02:59:55 +00001441 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001442
Damjan Marionb2c31b62020-12-13 21:47:40 +01001443 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001444 if (current_wrk != wrk)
1445 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001446 }
Florin Corasce815de2020-04-16 18:47:27 +00001447
Florin Coras935ce752020-09-08 22:43:47 -07001448 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001449 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001450 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001451
Florin Coras0d427d82018-06-27 03:24:07 -07001452 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001453
1454 /*
1455 * Free the heap and fix vcm
1456 */
1457 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001458 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001459
1460 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001461 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001462}
1463
1464int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001465vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001466{
Florin Coras134a9962018-08-28 11:32:04 -07001467 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001468 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001469
Florin Coras134a9962018-08-28 11:32:04 -07001470 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001471
Florin Coras7e12d942018-06-27 14:32:43 -07001472 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001473 session->session_state = VCL_STATE_CLOSED;
Florin Coras980ee742023-08-04 16:14:01 -07001474 session->vpp_handle = SESSION_INVALID_HANDLE;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001475 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301476 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001477
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001478 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001479 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001480
Florin Coras7e12d942018-06-27 14:32:43 -07001481 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1482 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001483
Florin Coras5e062572019-03-14 19:07:51 -07001484 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001485
Florin Coras134a9962018-08-28 11:32:04 -07001486 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001487}
1488
Florin Corasfe286f72021-06-04 10:07:55 -07001489static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001490vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001491{
Florin Corasfa3884f2021-06-22 20:04:46 -07001492 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001493
Florin Corasc2a14172023-02-28 21:13:50 -08001494 ASSERT (s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
1495
Florin Corasfa3884f2021-06-22 20:04:46 -07001496 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001497 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001498 wrk->ep_lt_current = s->session_index;
1499 s->vep.lt_next = s->session_index;
1500 s->vep.lt_prev = s->session_index;
1501 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001502 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001503
1504 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1505 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1506
1507 prev->vep.lt_next = s->session_index;
1508 s->vep.lt_prev = prev->session_index;
1509
1510 s->vep.lt_next = cur->session_index;
1511 cur->vep.lt_prev = s->session_index;
1512}
1513
1514static void
1515vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1516{
1517 vcl_session_t *prev, *next;
1518
Florin Corasc2a14172023-02-28 21:13:50 -08001519 ASSERT (s->vep.lt_next != VCL_INVALID_SESSION_INDEX);
1520
Florin Corasfa3884f2021-06-22 20:04:46 -07001521 if (s->vep.lt_next == s->session_index)
1522 {
1523 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1524 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001525 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfa3884f2021-06-22 20:04:46 -07001526 return;
1527 }
1528
1529 prev = vcl_session_get (wrk, s->vep.lt_prev);
1530 next = vcl_session_get (wrk, s->vep.lt_next);
1531
1532 prev->vep.lt_next = next->session_index;
1533 next->vep.lt_prev = prev->session_index;
1534
1535 if (s->session_index == wrk->ep_lt_current)
1536 wrk->ep_lt_current = s->vep.lt_next;
1537
1538 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001539 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001540}
1541
Dave Wallace543852a2017-08-03 02:11:34 -04001542int
Florin Corasc127d5a2020-10-14 16:35:58 -07001543vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001544 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001545{
Florin Coras134a9962018-08-28 11:32:04 -07001546 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001547
Florin Coras6c3b2182020-10-19 18:36:48 -07001548 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001549
Florin Coras6c3b2182020-10-19 18:36:48 -07001550 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001551 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001552 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001553 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001554 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001555 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001556 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001557 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001558 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1559 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001560 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001561 }
Florin Corase4306b62020-10-28 12:51:10 -07001562 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001563 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001564
Florin Coras6c3b2182020-10-19 18:36:48 -07001565 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001566 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001567 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001568 if (rv < 0)
1569 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001570 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1571 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001572 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001573
Florin Coras9ace36d2019-10-28 13:14:17 -07001574 if (!do_disconnect)
1575 {
1576 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001577 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001578 goto cleanup;
1579 }
1580
Florin Coras6c3b2182020-10-19 18:36:48 -07001581 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001582 {
1583 rv = vppcom_session_unbind (sh);
1584 if (PREDICT_FALSE (rv < 0))
1585 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001586 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001587 vppcom_retval_str (rv));
1588 return rv;
1589 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001590 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001591 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001592 {
1593 rv = vppcom_session_disconnect (sh);
1594 if (PREDICT_FALSE (rv < 0))
1595 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001596 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001597 rv, vppcom_retval_str (rv));
1598 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001599 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001600 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001601 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001602 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001603 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001604 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001605 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001606
1607 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1608 goto free_session;
1609
1610 /* Disconnect/reset messages pending but vpp transport and session
1611 * cleanups already done. Free only after messages drained. */
1612 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001613 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001614
Florin Corasc127d5a2020-10-14 16:35:58 -07001615 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001616
Florin Coras9ace36d2019-10-28 13:14:17 -07001617 /* Session is removed only after vpp confirms the disconnect */
1618 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001619
Florin Corasf9240dc2019-01-15 08:03:17 -08001620cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001621 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001622free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001623 vcl_session_free (wrk, s);
1624 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001625
Dave Wallace543852a2017-08-03 02:11:34 -04001626 return rv;
1627}
1628
1629int
Florin Corasf9240dc2019-01-15 08:03:17 -08001630vppcom_session_close (uint32_t session_handle)
1631{
1632 vcl_worker_t *wrk = vcl_worker_get_current ();
1633 vcl_session_t *session;
1634
1635 session = vcl_session_get_w_handle (wrk, session_handle);
1636 if (!session)
1637 return VPPCOM_EBADFD;
1638 return vcl_session_cleanup (wrk, session, session_handle,
1639 1 /* do_disconnect */ );
1640}
1641
1642int
Florin Coras134a9962018-08-28 11:32:04 -07001643vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001644{
Florin Coras134a9962018-08-28 11:32:04 -07001645 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001646 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001647
1648 if (!ep || !ep->ip)
1649 return VPPCOM_EINVAL;
1650
Florin Coras134a9962018-08-28 11:32:04 -07001651 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001652 if (!session)
1653 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001654
Florin Coras6c3b2182020-10-19 18:36:48 -07001655 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001656 {
Florin Coras5e062572019-03-14 19:07:51 -07001657 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1658 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001659 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001660 }
1661
Florin Coras7e12d942018-06-27 14:32:43 -07001662 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001663 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001664 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1665 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001666 else
Dave Barach178cf492018-11-13 16:34:13 -05001667 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1668 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001669 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001670
Florin Coras60687192021-11-29 21:00:47 -08001671 VDBG (0,
1672 "session %u handle %u: binding to local %s address %U port %u, "
1673 "proto %s",
1674 session->session_index, session_handle,
1675 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1676 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001677 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1678 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001679 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001680 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001681
1682 if (session->session_type == VPPCOM_PROTO_UDP)
Mohammed Hawarif4e3ee12023-10-30 10:52:19 +01001683 return vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001684
Florin Coras070453d2018-08-24 17:04:27 -07001685 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001686}
1687
1688int
Florin Coras134a9962018-08-28 11:32:04 -07001689vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001690{
Florin Coras134a9962018-08-28 11:32:04 -07001691 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001692 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001693 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001694 int rv;
1695
Florin Coras134a9962018-08-28 11:32:04 -07001696 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001697 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001698 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001699
Dave Wallaceee45d412017-11-24 21:44:06 -05001700 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001701 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001702 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001703 VDBG (0, "session %u [0x%llx]: already in listen state!",
1704 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001705 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001706 }
1707
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001708 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001709
Florin Coras070453d2018-08-24 17:04:27 -07001710 /*
1711 * Send listen request to vpp and wait for reply
1712 */
Florin Coras458089b2019-08-21 16:20:44 -07001713 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001714 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001715 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001716 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001717
Florin Coras070453d2018-08-24 17:04:27 -07001718 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001719 {
Florin Coras134a9962018-08-28 11:32:04 -07001720 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001721 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1722 listen_sh, listen_session->vpp_handle, rv,
1723 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001724 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001725 }
1726
Florin Coras070453d2018-08-24 17:04:27 -07001727 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001728}
1729
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001730int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001731vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1732{
1733 if (!strcmp (proto_str, "TCP"))
1734 *proto = VPPCOM_PROTO_TCP;
1735 else if (!strcmp (proto_str, "tcp"))
1736 *proto = VPPCOM_PROTO_TCP;
1737 else if (!strcmp (proto_str, "UDP"))
1738 *proto = VPPCOM_PROTO_UDP;
1739 else if (!strcmp (proto_str, "udp"))
1740 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001741 else if (!strcmp (proto_str, "TLS"))
1742 *proto = VPPCOM_PROTO_TLS;
1743 else if (!strcmp (proto_str, "tls"))
1744 *proto = VPPCOM_PROTO_TLS;
1745 else if (!strcmp (proto_str, "QUIC"))
1746 *proto = VPPCOM_PROTO_QUIC;
1747 else if (!strcmp (proto_str, "quic"))
1748 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001749 else if (!strcmp (proto_str, "DTLS"))
1750 *proto = VPPCOM_PROTO_DTLS;
1751 else if (!strcmp (proto_str, "dtls"))
1752 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001753 else if (!strcmp (proto_str, "SRTP"))
1754 *proto = VPPCOM_PROTO_SRTP;
1755 else if (!strcmp (proto_str, "srtp"))
1756 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001757 else
1758 return 1;
1759 return 0;
1760}
1761
1762int
Florin Coras32881932023-02-06 13:30:13 -08001763vppcom_session_accept (uint32_t ls_handle, vppcom_endpt_t *ep, uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001764{
Florin Coras32881932023-02-06 13:30:13 -08001765 u32 client_session_index = ~0, ls_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001766 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001767 session_accepted_msg_t accepted_msg;
Florin Coras32881932023-02-06 13:30:13 -08001768 vcl_session_t *ls, *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001769 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001770 u8 is_nonblocking;
Dave Wallace543852a2017-08-03 02:11:34 -04001771
Florin Coras5398dfb2021-01-25 20:31:27 -08001772again:
1773
Florin Coras32881932023-02-06 13:30:13 -08001774 ls = vcl_session_get_w_handle (wrk, ls_handle);
1775 if (!ls)
Florin Coras070453d2018-08-24 17:04:27 -07001776 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001777
Florin Coras32881932023-02-06 13:30:13 -08001778 if ((ls->session_state != VCL_STATE_LISTEN) &&
1779 (ls->session_state != VCL_STATE_LISTEN_NO_MQ) &&
1780 (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001781 {
Florin Coras32881932023-02-06 13:30:13 -08001782 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state (%s)",
1783 ls->vpp_handle, vcl_session_state_str (ls->session_state));
1784 return VPPCOM_EBADFD;
1785 }
1786
1787 ls_index = ls->session_index;
1788
1789 if (clib_fifo_elts (ls->accept_evts_fifo))
1790 {
1791 clib_fifo_sub2 (ls->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001792 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001793 accepted_msg = evt->accepted_msg;
1794 goto handle;
1795 }
1796
Florin Coras32881932023-02-06 13:30:13 -08001797 is_nonblocking = vcl_session_has_attr (ls, VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001798 while (1)
1799 {
Carl Smith592a9092019-11-12 14:57:37 +13001800 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1801 return VPPCOM_EAGAIN;
1802
Florin Coras5398dfb2021-01-25 20:31:27 -08001803 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1804 vcl_worker_flush_mq_events (wrk);
1805 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001806 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001807
Florin Coras54693d22018-07-17 10:46:29 -07001808handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001809
Florin Coras32881932023-02-06 13:30:13 -08001810 client_session_index =
1811 vcl_session_accepted_handler (wrk, &accepted_msg, ls_index);
Florin Coras00cca802019-06-06 09:38:44 -07001812 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1813 return VPPCOM_ECONNABORTED;
1814
Florin Coras32881932023-02-06 13:30:13 -08001815 ls = vcl_session_get (wrk, ls_index);
Florin Coras134a9962018-08-28 11:32:04 -07001816 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001817
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001818 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001819 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001820
Florin Coras32881932023-02-06 13:30:13 -08001821 VDBG (1,
1822 "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1823 " flags %d, is_nonblocking %u",
1824 ls->session_index, ls->vpp_handle, client_session_index,
Florin Coras5e062572019-03-14 19:07:51 -07001825 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001826 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001827
Dave Wallace048b1d62018-01-03 22:24:41 -05001828 if (ep)
1829 {
Florin Coras7e12d942018-06-27 14:32:43 -07001830 ep->is_ip4 = client_session->transport.is_ip4;
1831 ep->port = client_session->transport.rmt_port;
1832 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001833 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1834 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001835 else
Dave Barach178cf492018-11-13 16:34:13 -05001836 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1837 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001838 }
Dave Wallace60caa062017-11-10 17:07:13 -05001839
Florin Coras60687192021-11-29 21:00:47 -08001840 VDBG (0,
1841 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1842 "local: %U:%u",
Florin Coras32881932023-02-06 13:30:13 -08001843 ls_handle, ls->vpp_handle, client_session_index,
1844 client_session->vpp_handle, vcl_format_ip46_address,
1845 &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001846 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001847 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001848 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001849 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001850 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras32881932023-02-06 13:30:13 -08001851 vcl_evt (VCL_EVT_ACCEPT, client_session, ls, client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001852
Florin Coras3c7d4f92018-12-14 11:28:43 -08001853 /*
1854 * Session might have been closed already
1855 */
1856 if (accept_flags)
1857 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001858 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001859 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001860 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001861 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001862 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001863 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001864}
1865
1866int
Florin Coras134a9962018-08-28 11:32:04 -07001867vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001868{
Florin Coras134a9962018-08-28 11:32:04 -07001869 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001870 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001871 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001872 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001873
Florin Coras134a9962018-08-28 11:32:04 -07001874 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001875 if (!session)
1876 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001877 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001878
Florin Coras6c3b2182020-10-19 18:36:48 -07001879 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001880 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001881 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001882 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001883 }
1884
Florin Corasc127d5a2020-10-14 16:35:58 -07001885 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001886 {
Florin Coras60687192021-11-29 21:00:47 -08001887 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001888 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1889 " state (%s)",
1890 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001891 vcl_format_ip46_address, &session->transport.rmt_ip,
1892 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001893 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001894 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001895 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001896 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001897 }
1898
Florin Coras0a1e1832020-03-29 18:54:04 +00001899 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001900 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001901 {
1902 if (session->session_type != VPPCOM_PROTO_UDP)
1903 return VPPCOM_EINVAL;
1904 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001905 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001906 }
1907
Florin Coras7e12d942018-06-27 14:32:43 -07001908 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001909 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001910 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001911 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001912 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001913
Florin Coras5ffb9642021-12-03 17:24:13 -08001914 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1915 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001916 &session->transport.rmt_ip,
1917 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001918 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001919 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001920
Florin Coras458089b2019-08-21 16:20:44 -07001921 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001922
Florin Corasac422d62020-10-19 20:51:36 -07001923 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001924 {
fanyfa49d59d2020-10-13 17:07:16 +08001925 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001926 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001927 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001928 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001929 return VPPCOM_EINPROGRESS;
1930 }
Florin Coras57c88932019-08-29 12:03:17 -07001931
1932 /*
1933 * Wait for reply from vpp if blocking
1934 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001935 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001936 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001937
Florin Coras134a9962018-08-28 11:32:04 -07001938 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001939 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1940 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001941
Dave Wallace4878cbe2017-11-21 03:45:09 -05001942 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001943}
1944
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001945int
1946vppcom_session_stream_connect (uint32_t session_handle,
1947 uint32_t parent_session_handle)
1948{
1949 vcl_worker_t *wrk = vcl_worker_get_current ();
1950 vcl_session_t *session, *parent_session;
1951 u32 session_index, parent_session_index;
1952 int rv;
1953
1954 session = vcl_session_get_w_handle (wrk, session_handle);
1955 if (!session)
1956 return VPPCOM_EBADFD;
1957 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1958 if (!parent_session)
1959 return VPPCOM_EBADFD;
1960
1961 session_index = session->session_index;
1962 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001963 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001964 {
1965 VDBG (0, "ERROR: cannot connect epoll session %u!",
1966 session->session_index);
1967 return VPPCOM_EBADFD;
1968 }
1969
Florin Corasc127d5a2020-10-14 16:35:58 -07001970 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001971 {
Florin Coras60687192021-11-29 21:00:47 -08001972 VDBG (0,
1973 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001974 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001975 session_handle, session->vpp_handle, parent_session_handle,
1976 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001977 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001978 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001979 return VPPCOM_OK;
1980 }
1981
1982 /* Connect to quic session specifics */
1983 session->transport.is_ip4 = parent_session->transport.is_ip4;
1984 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1985 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001986 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001987
1988 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1989 session_handle, parent_session_handle, parent_session->vpp_handle);
1990
1991 /*
1992 * Send connect request and wait for reply from vpp
1993 */
Florin Coras458089b2019-08-21 16:20:44 -07001994 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001995 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001996 vcm->cfg.session_timeout);
1997
1998 session->listener_index = parent_session_index;
1999 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07002000 if (parent_session)
2001 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02002002
2003 session = vcl_session_get (wrk, session_index);
2004 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
2005 session->vpp_handle, rv ? "failed" : "succeeded");
2006
2007 return rv;
2008}
2009
Steven58f464e2017-10-25 12:33:12 -07002010static inline int
Florin Coras134a9962018-08-28 11:32:04 -07002011vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07002012 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002013{
Florin Coras134a9962018-08-28 11:32:04 -07002014 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07002015 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002016 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002017 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002018 session_event_t *e;
2019 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07002020 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002021
Florin Coras070453d2018-08-24 17:04:27 -07002022 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08002023 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002024
Florin Coras134a9962018-08-28 11:32:04 -07002025 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002026 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07002027 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002028
Florin Coras6d0106e2019-01-29 20:11:58 -08002029 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002030 {
Florin Coras5e062572019-03-14 19:07:51 -07002031 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002032 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002033 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002034 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002035 }
2036
liuyacan55c952e2021-06-13 14:54:55 +08002037 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2038 {
2039 /* Vpp would ack the incoming data and enqueue it for reading.
2040 * So even if SHUT_RD is set, we can still read() the data if
2041 * the session is ready.
2042 */
2043 if (!vcl_session_read_ready (s))
2044 {
2045 return 0;
2046 }
2047 }
2048
Florin Corasac422d62020-10-19 20:51:36 -07002049 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002050 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002051 mq = wrk->app_event_queue;
2052 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002053 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002054
Sirshak Das28aa5392019-02-05 01:33:33 -06002055 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002056 {
Florin Corasae036d32023-09-18 09:56:21 -07002057 if (is_ct)
2058 svm_fifo_unset_event (s->rx_fifo);
2059 svm_fifo_unset_event (rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002060 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002061 {
Yu Pingb2955352019-12-04 06:49:04 +08002062 if (vcl_session_is_closing (s))
2063 return vcl_session_closing_error (s);
Florin Corasc0737e92019-03-04 14:19:39 -08002064 return VPPCOM_EWOULDBLOCK;
2065 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002066 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002067 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002068 if (vcl_session_is_closing (s))
2069 return vcl_session_closing_error (s);
2070
Florin Corasd6894562020-10-28 00:37:15 -07002071 if (is_ct)
2072 svm_fifo_unset_event (s->rx_fifo);
2073 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002074
Florin Coras5398dfb2021-01-25 20:31:27 -08002075 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2076 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002077 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002078 }
Florin Coras54693d22018-07-17 10:46:29 -07002079
Florin Coras4a2c7942020-09-10 12:27:14 -07002080read_again:
2081
Florin Coras460dce62018-07-27 05:45:06 -07002082 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002083 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002084 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002085 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2086
2087 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002088
2089 if (peek)
Florin Coras916ca8d2024-05-03 13:23:00 -07002090 {
2091 /* Request new notifications if more data enqueued */
2092 if (rv < n || rv == svm_fifo_max_dequeue_cons (rx_fifo))
2093 {
2094 if (is_ct)
2095 svm_fifo_unset_event (s->rx_fifo);
2096 svm_fifo_unset_event (rx_fifo);
2097 }
2098 return rv;
2099 }
Florin Coras23368312021-06-04 16:28:18 -07002100
Florin Coras4a2c7942020-09-10 12:27:14 -07002101 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002102
Sirshak Das28aa5392019-02-05 01:33:33 -06002103 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002104 {
Florin Corasd6894562020-10-28 00:37:15 -07002105 if (is_ct)
2106 svm_fifo_unset_event (s->rx_fifo);
2107 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002108 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002109 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002110 {
Florin Corasc34118b2020-08-12 18:58:25 -07002111 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2112 e->event_type = SESSION_IO_EVT_RX;
2113 e->session_index = s->session_index;
2114 }
2115 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002116 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002117 {
2118 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002119 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002120 buf += rv;
2121 n -= rv;
2122 goto read_again;
2123 }
Florin Coras41c9e042018-09-11 00:10:41 -07002124
Florin Coras17ec5772020-08-12 20:46:29 -07002125 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002126 {
Florin Coras17ec5772020-08-12 20:46:29 -07002127 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002128 app_send_io_evt_to_vpp (s->vpp_evt_q,
2129 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002130 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002131 }
2132
Florin Coras5e062572019-03-14 19:07:51 -07002133 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2134 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002135
Florin Coras54693d22018-07-17 10:46:29 -07002136 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002137}
2138
Steven58f464e2017-10-25 12:33:12 -07002139int
Florin Coras134a9962018-08-28 11:32:04 -07002140vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002141{
Florin Coras134a9962018-08-28 11:32:04 -07002142 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002143}
2144
2145static int
Florin Coras134a9962018-08-28 11:32:04 -07002146vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002147{
Florin Coras134a9962018-08-28 11:32:04 -07002148 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002149}
2150
Florin Coras2cba8532018-09-11 16:33:36 -07002151int
2152vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002153 vppcom_data_segment_t * ds, uint32_t n_segments,
2154 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002155{
2156 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002157 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002158 vcl_session_t *s = 0;
2159 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002160 svm_msg_q_t *mq;
2161 u8 is_ct;
2162
2163 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002164 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002165 return VPPCOM_EBADFD;
2166
Florin Coras6d0106e2019-01-29 20:11:58 -08002167 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2168 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002169
Florin Corasac422d62020-10-19 20:51:36 -07002170 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002171 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002172 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002173 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002174 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002175
Sirshak Das28aa5392019-02-05 01:33:33 -06002176 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002177 {
Florin Corasae036d32023-09-18 09:56:21 -07002178 if (is_ct)
2179 svm_fifo_unset_event (s->rx_fifo);
2180 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002181 if (is_nonblocking)
2182 {
Florin Corasae036d32023-09-18 09:56:21 -07002183 if (vcl_session_is_closing (s))
2184 return vcl_session_closing_error (s);
Florin Corasaa27eb92018-10-13 12:20:01 -07002185 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002186 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002187 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002188 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002189 if (vcl_session_is_closing (s))
2190 return vcl_session_closing_error (s);
2191
Florin Coras62877022020-10-28 21:22:04 -07002192 if (is_ct)
2193 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002194 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002195
Florin Coras5398dfb2021-01-25 20:31:27 -08002196 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2197 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002198 }
2199 }
2200
Florin Corasd1cc38d2020-10-11 11:05:04 -07002201 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002202 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002203 if (n_read < 0)
2204 return VPPCOM_EAGAIN;
2205
2206 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2207 {
Florin Coras62877022020-10-28 21:22:04 -07002208 if (is_ct)
2209 svm_fifo_unset_event (s->rx_fifo);
2210 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002211 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002212 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002213 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002214 {
2215 session_event_t *e;
2216 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2217 e->event_type = SESSION_IO_EVT_RX;
2218 e->session_index = s->session_index;
2219 }
2220 }
2221
2222 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002223 return n_read;
2224}
2225
2226void
Florin Corasd68faf82020-09-25 15:18:13 -07002227vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002228{
2229 vcl_worker_t *wrk = vcl_worker_get_current ();
2230 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002231 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002232
2233 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002234 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002235 return;
2236
Florin Coras62877022020-10-28 21:22:04 -07002237 is_ct = vcl_session_is_ct (s);
2238 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002239
Florin Coras8cc93212021-09-10 11:37:12 -07002240 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002241 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002242}
2243
Florin Coras7a2abce2020-04-05 19:25:44 +00002244always_inline u8
2245vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002246{
Florin Coras7a2abce2020-04-05 19:25:44 +00002247 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2248 if (is_dgram)
2249 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2250 else
2251 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002252}
2253
2254always_inline int
Dou Chao243a0432022-11-29 19:41:34 +08002255vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002256 size_t n, u8 is_flush, u8 is_dgram)
Florin Coras7a2abce2020-04-05 19:25:44 +00002257{
Florin Coras6d0106e2019-01-29 20:11:58 -08002258 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002259 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002260 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002261 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002262 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002263
Florin Coras0b0d28e2021-06-04 17:31:53 -07002264 /* Accept zero length writes but just return */
2265 if (PREDICT_FALSE (!n))
2266 return VPPCOM_OK;
2267
2268 if (PREDICT_FALSE (!buf))
2269 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002270
Florin Coras6c3b2182020-10-19 18:36:48 -07002271 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002272 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002273 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2274 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002275 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002276 }
2277
liuyacan55c952e2021-06-13 14:54:55 +08002278 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002279 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002280 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2281 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002282 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002283 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002284 }
2285
liuyacan55c952e2021-06-13 14:54:55 +08002286 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2287 {
2288 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2289 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002290 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002291 return VPPCOM_EPIPE;
2292 }
2293
Florin Coras0e88e852018-09-17 22:09:02 -07002294 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002295 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002296 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002297
Florin Coras653e43f2019-03-04 10:56:23 -08002298 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002299 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002300 {
Florin Coras54693d22018-07-17 10:46:29 -07002301 if (is_nonblocking)
2302 {
Florin Coras070453d2018-08-24 17:04:27 -07002303 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002304 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002305 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002306 {
Florin Coras2d379d82019-06-28 12:45:12 -07002307 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002308 if (vcl_session_is_closing (s))
2309 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002310
Florin Coras5398dfb2021-01-25 20:31:27 -08002311 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2312 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002313 }
Dave Wallace543852a2017-08-03 02:11:34 -04002314 }
Dave Wallace543852a2017-08-03 02:11:34 -04002315
Florin Coras653e43f2019-03-04 10:56:23 -08002316 et = SESSION_IO_EVT_TX;
2317 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002318 et = SESSION_IO_EVT_TX_FLUSH;
2319
Florin Coras7a2abce2020-04-05 19:25:44 +00002320 if (is_dgram)
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002321 {
2322 et = vcl_session_dgram_tx_evt (s, et);
2323 n_write =
2324 app_send_dgram_raw_gso (tx_fifo, &s->transport, s->vpp_evt_q, buf, n,
2325 s->gso_size, et, 0 /* do_evt */, SVM_Q_WAIT);
2326 }
Florin Coras460dce62018-07-27 05:45:06 -07002327 else
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002328 {
2329 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
2330 0 /* do_evt */, SVM_Q_WAIT);
2331 }
Florin Coras653e43f2019-03-04 10:56:23 -08002332
Florin Coras14ed6df2019-03-06 21:13:42 -08002333 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002334 app_send_io_evt_to_vpp (
2335 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002336
Florin Coras56230092020-09-02 20:52:58 -07002337 /* The underlying fifo segment can run out of memory */
2338 if (PREDICT_FALSE (n_write < 0))
2339 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002340
Florin Coras6d0106e2019-01-29 20:11:58 -08002341 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2342 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002343
Florin Coras54693d22018-07-17 10:46:29 -07002344 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002345}
2346
Florin Coras42ceddb2018-12-12 10:56:01 -08002347int
Aritra Basuf0e81d22024-08-27 18:05:17 -07002348vppcom_session_write_segments (uint32_t session_handle,
2349 vppcom_data_segment_t *ds, uint32_t n_segments)
2350{
2351 vcl_worker_t *wrk = vcl_worker_get_current ();
2352 int n_write = 0, n_bytes = 0, is_nonblocking;
2353 vcl_session_t *s = 0;
2354 svm_fifo_t *tx_fifo;
2355 svm_msg_q_t *mq;
2356 u8 is_ct;
2357 u32 i;
2358
2359 if (PREDICT_FALSE (!ds))
2360 return VPPCOM_EFAULT;
2361
2362 /* Accept zero length writes but just return */
2363 if (PREDICT_FALSE (ds[0].len == 0))
2364 return VPPCOM_OK;
2365
2366 s = vcl_session_get_w_handle (wrk, session_handle);
2367 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
2368 return VPPCOM_EBADFD;
2369
2370 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2371 return vcl_session_closed_error (s);
2372
2373 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2374 return VPPCOM_EPIPE;
2375
2376 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
2377 is_ct = vcl_session_is_ct (s);
2378 mq = wrk->app_event_queue;
2379 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
2380
2381 for (i = 0; i < n_segments; i++)
2382 n_bytes += ds[i].len;
2383
2384 if (svm_fifo_max_enqueue_prod (tx_fifo) < n_bytes)
2385 {
2386 if (is_nonblocking)
2387 {
2388 return VPPCOM_EWOULDBLOCK;
2389 }
2390 while (svm_fifo_max_enqueue_prod (tx_fifo) < n_bytes)
2391 {
2392 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
2393 if (vcl_session_is_closing (s))
2394 return vcl_session_closing_error (s);
2395
2396 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2397 vcl_worker_flush_mq_events (wrk);
2398 }
2399 }
2400
2401 n_write = svm_fifo_enqueue_segments (tx_fifo, (svm_fifo_seg_t *) ds,
2402 n_segments, 0 /* allow_partial */);
2403
2404 /* The underlying fifo segment can run out of memory */
2405 if (PREDICT_FALSE (n_write < 0))
2406 return VPPCOM_EAGAIN;
2407
2408 if (svm_fifo_set_event (s->tx_fifo))
2409 app_send_io_evt_to_vpp (s->vpp_evt_q,
2410 s->tx_fifo->shr->master_session_index,
2411 SESSION_IO_EVT_TX, SVM_Q_WAIT);
2412
2413 return n_write;
2414}
2415
2416int
Florin Coras42ceddb2018-12-12 10:56:01 -08002417vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2418{
Florin Coras7a2abce2020-04-05 19:25:44 +00002419 vcl_worker_t *wrk = vcl_worker_get_current ();
2420 vcl_session_t *s;
2421
2422 s = vcl_session_get_w_handle (wrk, session_handle);
2423 if (PREDICT_FALSE (!s))
2424 return VPPCOM_EBADFD;
2425
Florin Coraseff5f7a2023-02-07 17:36:17 -08002426 return vppcom_session_write_inline (wrk, s, buf, n, 0 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002427 s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002428}
2429
Florin Corasb0f662f2018-12-27 14:51:46 -08002430int
2431vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2432{
Florin Coras7a2abce2020-04-05 19:25:44 +00002433 vcl_worker_t *wrk = vcl_worker_get_current ();
2434 vcl_session_t *s;
2435
2436 s = vcl_session_get_w_handle (wrk, session_handle);
2437 if (PREDICT_FALSE (!s))
2438 return VPPCOM_EBADFD;
2439
Florin Coraseff5f7a2023-02-07 17:36:17 -08002440 return vppcom_session_write_inline (wrk, s, buf, n, 1 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002441 s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002442}
2443
Florin Corasc0737e92019-03-04 14:19:39 -08002444#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002445if (PREDICT_FALSE (!_s->rx_fifo)) \
2446 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002447if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2448 { \
2449 if (!vcl_session_is_ct (_s)) \
2450 { \
2451 svm_fifo_unset_event (_s->rx_fifo); \
2452 if (svm_fifo_is_empty (_s->rx_fifo)) \
2453 break; \
2454 } \
2455 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2456 { \
Florin Coras2f630182020-08-13 00:17:51 -07002457 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002458 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2459 break; \
2460 } \
2461 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002462
Florin Coras86f04502018-09-12 16:08:01 -07002463static void
2464vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2465 unsigned long n_bits, unsigned long *read_map,
2466 unsigned long *write_map,
2467 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002468{
2469 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002470 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002471 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002472 u32 sid;
2473
2474 switch (e->event_type)
2475 {
Florin Corasc0737e92019-03-04 14:19:39 -08002476 case SESSION_IO_EVT_RX:
2477 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002478 s = vcl_session_get (wrk, sid);
2479 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002480 break;
Florin Corasb242d312020-10-26 15:35:40 -07002481 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002482 if (sid < n_bits && read_map)
2483 {
David Johnsond9818dd2018-12-14 14:53:41 -05002484 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002485 *bits_set += 1;
2486 }
2487 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002488 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002489 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002490 s = vcl_session_get (wrk, sid);
2491 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002492 break;
2493 if (sid < n_bits && write_map)
2494 {
David Johnsond9818dd2018-12-14 14:53:41 -05002495 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002496 *bits_set += 1;
2497 }
2498 break;
Florin Coras86f04502018-09-12 16:08:01 -07002499 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002500 if (!e->postponed)
2501 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2502 else
2503 s = vcl_session_get (wrk, e->session_index);
2504 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002505 break;
Florin Corasb242d312020-10-26 15:35:40 -07002506 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002507 if (sid < n_bits && read_map)
2508 {
David Johnsond9818dd2018-12-14 14:53:41 -05002509 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002510 *bits_set += 1;
2511 }
2512 break;
2513 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002514 if (!e->postponed)
2515 {
2516 connected_msg = (session_connected_msg_t *) e->data;
2517 sid = vcl_session_connected_handler (wrk, connected_msg);
2518 }
2519 else
2520 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002521 if (sid == VCL_INVALID_SESSION_INDEX)
2522 break;
Florin Coras66c675e2023-03-09 16:43:02 -08002523 if (!(sid < n_bits && write_map))
2524 break;
2525 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2526 *bits_set += 1;
2527 s = vcl_session_get (wrk, sid);
Florin Coras66c675e2023-03-09 16:43:02 -08002528 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07002529 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras86f04502018-09-12 16:08:01 -07002530 break;
2531 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras5e9ed0d2023-08-15 11:16:34 -07002532 if (!e->postponed)
2533 {
2534 disconnected_msg = (session_disconnected_msg_t *) e->data;
2535 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2536 if (!s)
2537 break;
2538 }
2539 else
2540 {
2541 s = vcl_session_get (wrk, e->session_index);
2542 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
2543 }
2544 if (vcl_session_is_closed (s))
2545 {
2546 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
2547 vcl_session_free (wrk, s);
2548 break;
2549 }
Florin Corasb242d312020-10-26 15:35:40 -07002550 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002551 if (sid < n_bits && except_map)
2552 {
David Johnsond9818dd2018-12-14 14:53:41 -05002553 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002554 *bits_set += 1;
2555 }
2556 break;
2557 case SESSION_CTRL_EVT_RESET:
Florin Coras5e9ed0d2023-08-15 11:16:34 -07002558 if (!e->postponed)
2559 {
2560 sid =
2561 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2562 s = vcl_session_get (wrk, sid);
2563 }
2564 else
2565 {
2566 sid = e->session_index;
2567 s = vcl_session_get (wrk, sid);
2568 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
2569 }
2570 if (vcl_session_is_closed (s))
2571 {
2572 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
2573 vcl_session_free (wrk, s);
2574 break;
2575 }
Florin Coras86f04502018-09-12 16:08:01 -07002576 if (sid < n_bits && except_map)
2577 {
David Johnsond9818dd2018-12-14 14:53:41 -05002578 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002579 *bits_set += 1;
2580 }
2581 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002582 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2583 vcl_session_unlisten_reply_handler (wrk, e->data);
2584 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002585 case SESSION_CTRL_EVT_MIGRATED:
2586 vcl_session_migrated_handler (wrk, e->data);
2587 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002588 case SESSION_CTRL_EVT_CLEANUP:
2589 vcl_session_cleanup_handler (wrk, e->data);
2590 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002591 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2592 vcl_session_worker_update_reply_handler (wrk, e->data);
2593 break;
2594 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2595 vcl_session_req_worker_update_handler (wrk, e->data);
2596 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002597 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2598 vcl_session_app_add_segment_handler (wrk, e->data);
2599 break;
2600 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2601 vcl_session_app_del_segment_handler (wrk, e->data);
2602 break;
hanlina3a48962020-07-13 11:09:15 +08002603 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002604 vcl_worker_rpc_handler (wrk, e->data);
2605 break;
Florin Coras86f04502018-09-12 16:08:01 -07002606 default:
2607 clib_warning ("unhandled: %u", e->event_type);
2608 break;
2609 }
2610}
2611
2612static int
2613vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2614 unsigned long n_bits, unsigned long *read_map,
2615 unsigned long *write_map, unsigned long *except_map,
2616 double time_to_wait, u32 * bits_set)
2617{
Florin Coras99368312018-08-02 10:45:44 -07002618 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002619 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002620 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002621
Florin Coras54693d22018-07-17 10:46:29 -07002622 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002623 {
Florin Coras54693d22018-07-17 10:46:29 -07002624 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002625 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002626
Florin Coras54693d22018-07-17 10:46:29 -07002627 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002628 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002629 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002630 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002631 else
2632 {
2633 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002634 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002635 }
2636 }
Florin Corase003a1b2019-06-05 10:47:16 -07002637 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002638
Florin Coras134a9962018-08-28 11:32:04 -07002639 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002640 {
Florin Coras134a9962018-08-28 11:32:04 -07002641 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002642 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002643 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2644 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002645 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002646 }
Florin Coras134a9962018-08-28 11:32:04 -07002647 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002648 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002649 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002650}
2651
Florin Coras99368312018-08-02 10:45:44 -07002652static int
Florin Coras294afe22019-01-07 17:49:17 -08002653vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2654 vcl_si_set * read_map, vcl_si_set * write_map,
2655 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002656 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002657{
Florin Coras14ed6df2019-03-06 21:13:42 -08002658 double wait = 0, start = 0;
2659
2660 if (!*bits_set)
2661 {
2662 wait = time_to_wait;
2663 start = clib_time_now (&wrk->clib_time);
2664 }
2665
2666 do
2667 {
2668 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2669 write_map, except_map, wait, bits_set);
2670 if (*bits_set)
2671 return *bits_set;
2672 if (wait == -1)
2673 continue;
2674
2675 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2676 }
2677 while (wait > 0);
2678
2679 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002680}
2681
2682static int
Florin Coras294afe22019-01-07 17:49:17 -08002683vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2684 vcl_si_set * read_map, vcl_si_set * write_map,
2685 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002686 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002687{
2688 vcl_mq_evt_conn_t *mqc;
2689 int __clib_unused n_read;
2690 int n_mq_evts, i;
Aritra Basu119cbf22024-07-30 16:03:59 -07002691 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07002692 u64 buf;
2693
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002694 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2695 {
2696 vcl_api_retry_attach (wrk);
2697 return 0;
2698 }
2699
Florin Coras134a9962018-08-28 11:32:04 -07002700 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Aritra Basu119cbf22024-07-30 16:03:59 -07002701 if (time_to_wait > 0)
2702 end = clib_time_now (&wrk->clib_time) + (time_to_wait / 1e3);
2703
2704 do
Florin Coras99368312018-08-02 10:45:44 -07002705 {
Aritra Basu119cbf22024-07-30 16:03:59 -07002706 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2707 vec_len (wrk->mq_events), time_to_wait);
2708 if (n_mq_evts < 0)
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002709 {
Aritra Basu119cbf22024-07-30 16:03:59 -07002710 if (errno == EINTR)
2711 continue;
2712
2713 VDBG (0, "epoll_wait error %u", errno);
2714 return 0;
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002715 }
2716
Aritra Basu119cbf22024-07-30 16:03:59 -07002717 if (n_mq_evts == 0)
2718 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002719
Aritra Basu119cbf22024-07-30 16:03:59 -07002720 for (i = 0; i < n_mq_evts; i++)
2721 {
2722 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2723 {
2724 vcl_api_handle_disconnect (wrk);
2725 continue;
2726 }
2727
2728 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2729 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2730 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
2731 except_map, 0, bits_set);
2732 }
2733
2734 if (*bits_set || !time_to_wait)
2735 return (int) *bits_set;
2736 }
2737 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
2738
2739 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002740}
2741
Dave Wallace543852a2017-08-03 02:11:34 -04002742int
Florin Coras294afe22019-01-07 17:49:17 -08002743vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2744 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002745{
Florin Coras54693d22018-07-17 10:46:29 -07002746 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002747 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002748 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002749 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002750
Dave Wallace7876d392017-10-19 03:53:57 -04002751 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002752 {
Florin Coras134a9962018-08-28 11:32:04 -07002753 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002754 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002755 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2756 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002757 }
Dave Wallace7876d392017-10-19 03:53:57 -04002758 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002759 {
Florin Coras134a9962018-08-28 11:32:04 -07002760 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002761 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002762 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2763 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002764 }
Dave Wallace7876d392017-10-19 03:53:57 -04002765 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002766 {
Florin Coras134a9962018-08-28 11:32:04 -07002767 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002768 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002769 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2770 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002771 }
2772
Florin Coras54693d22018-07-17 10:46:29 -07002773 if (!n_bits)
2774 return 0;
2775
2776 if (!write_map)
2777 goto check_rd;
2778
Florin Coras096a1d52021-01-27 15:33:51 -08002779 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2780 {
2781 if (!(s = vcl_session_get (wrk, sid)))
2782 {
2783 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2784 bits_set++;
2785 continue;
2786 }
Florin Coras54693d22018-07-17 10:46:29 -07002787
Florin Coras096a1d52021-01-27 15:33:51 -08002788 if (vcl_session_write_ready (s))
2789 {
2790 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2791 bits_set++;
2792 }
Florin Coras607eb202023-05-29 16:19:38 -07002793 else
Florin Coras096a1d52021-01-27 15:33:51 -08002794 {
Florin Coras607eb202023-05-29 16:19:38 -07002795 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras096a1d52021-01-27 15:33:51 -08002796 }
2797 }
Florin Coras54693d22018-07-17 10:46:29 -07002798
2799check_rd:
2800 if (!read_map)
2801 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002802
Florin Coras096a1d52021-01-27 15:33:51 -08002803 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2804 {
2805 if (!(s = vcl_session_get (wrk, sid)))
2806 {
2807 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2808 bits_set++;
2809 continue;
2810 }
Florin Coras54693d22018-07-17 10:46:29 -07002811
Florin Coras096a1d52021-01-27 15:33:51 -08002812 if (vcl_session_read_ready (s))
2813 {
2814 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2815 bits_set++;
2816 }
2817 }
Florin Coras54693d22018-07-17 10:46:29 -07002818
2819check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002820
Florin Coras86f04502018-09-12 16:08:01 -07002821 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2822 {
2823 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2824 read_map, write_map, except_map, &bits_set);
2825 }
2826 vec_reset_length (wrk->unhandled_evts_vector);
2827
Florin Coras99368312018-08-02 10:45:44 -07002828 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002829 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002830 time_to_wait, &bits_set);
2831 else
Florin Coras134a9962018-08-28 11:32:04 -07002832 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002833 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002834
Dave Wallace543852a2017-08-03 02:11:34 -04002835 return (bits_set);
2836}
2837
Dave Wallacef7f809c2017-10-03 01:48:42 -04002838static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002839vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002840{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002841 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002842 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002843 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002844
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002845 if (VPPCOM_DEBUG <= 3)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002846 return;
2847
Florin Coras6c3b2182020-10-19 18:36:48 -07002848 s = vcl_session_get_w_handle (wrk, vep_handle);
2849 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002850 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002851 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002852 goto done;
2853 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002854 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002855 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002856 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002857 goto done;
2858 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002859 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002860 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002861 "{\n"
2862 " is_vep = %u\n"
2863 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002864 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002865 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2866 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002867
Florin Coras7e5e62b2019-07-30 14:08:23 -07002868 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002869 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002870 s = vcl_session_get_w_handle (wrk, sh);
2871 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002872 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002873 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002874 goto done;
2875 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002876 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002877 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002878 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002879 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002880 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002881 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002882 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002883 goto done;
2884 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002885 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002886 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2887 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002888 sh, s->vep.vep_sh, vep_handle);
2889 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002890 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002891 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002892 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002893 " next_sh = 0x%x (%u)\n"
2894 " prev_sh = 0x%x (%u)\n"
2895 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002896 " ev.events = 0x%x\n"
2897 " ev.data.u64 = 0x%llx\n"
2898 " et_mask = 0x%x\n"
2899 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002900 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002901 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2902 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002903 }
2904 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002905
2906done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002907 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002908}
2909
2910int
2911vppcom_epoll_create (void)
2912{
Florin Coras134a9962018-08-28 11:32:04 -07002913 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002914 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002915
Florin Coras134a9962018-08-28 11:32:04 -07002916 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002917
Florin Coras6c3b2182020-10-19 18:36:48 -07002918 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002919 vep_session->vep.vep_sh = ~0;
2920 vep_session->vep.next_sh = ~0;
2921 vep_session->vep.prev_sh = ~0;
Florin Coras980ee742023-08-04 16:14:01 -07002922 vep_session->vpp_handle = SESSION_INVALID_HANDLE;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002923
Florin Corasa7a1a222018-12-30 17:11:31 -08002924 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2925 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002926
Florin Corasab2f6db2018-08-31 14:31:41 -07002927 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002928}
2929
Florin Corasb0116a12023-02-07 09:11:47 -08002930static void
2931vcl_epoll_ctl_add_unhandled_event (vcl_worker_t *wrk, vcl_session_t *s,
Florin Corase81f27f2024-02-12 22:33:41 -05002932 u32 is_epollet, session_evt_type_t evt)
Florin Corasb0116a12023-02-07 09:11:47 -08002933{
2934 if (!is_epollet)
2935 {
Florin Corasc2a14172023-02-28 21:13:50 -08002936 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
2937 vcl_epoll_lt_add (wrk, s);
Florin Corasb0116a12023-02-07 09:11:47 -08002938 return;
2939 }
2940
2941 session_event_t e = { 0 };
2942 e.session_index = s->session_index;
2943 e.event_type = evt;
2944 if (evt == SESSION_IO_EVT_RX)
2945 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2946 vec_add1 (wrk->unhandled_evts_vector, e);
2947}
2948
Dave Wallacef7f809c2017-10-03 01:48:42 -04002949int
Florin Coras134a9962018-08-28 11:32:04 -07002950vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002951 struct epoll_event *event)
2952{
Florin Coras134a9962018-08-28 11:32:04 -07002953 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002954 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002955 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002956 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002957
Florin Coras134a9962018-08-28 11:32:04 -07002958 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002959 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002960 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002961 return VPPCOM_EINVAL;
2962 }
2963
Florin Coras134a9962018-08-28 11:32:04 -07002964 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002965 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002966 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002967 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002968 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002969 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002970 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002971 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002972 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002973 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002974 }
2975
Florin Coras134a9962018-08-28 11:32:04 -07002976 ASSERT (vep_session->vep.vep_sh == ~0);
2977 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002978
Florin Coras17ec5772020-08-12 20:46:29 -07002979 s = vcl_session_get_w_handle (wrk, session_handle);
2980 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002981 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002982 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002983 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002984 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002985 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002986 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002987 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002988 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002989 }
2990
2991 switch (op)
2992 {
2993 case EPOLL_CTL_ADD:
2994 if (PREDICT_FALSE (!event))
2995 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002996 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002997 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002998 }
Florin Coras2645f682021-06-04 15:59:41 -07002999 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
3000 {
3001 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
3002 rv = VPPCOM_EEXIST;
3003 goto done;
3004 }
Florin Coras134a9962018-08-28 11:32:04 -07003005 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003006 {
Florin Coras7e12d942018-06-27 14:32:43 -07003007 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07003008 next_session = vcl_session_get_w_handle (wrk,
3009 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07003010 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003011 {
Florin Coras5e062572019-03-14 19:07:51 -07003012 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08003013 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003014 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003015 }
Florin Coras134a9962018-08-28 11:32:04 -07003016 ASSERT (next_session->vep.prev_sh == vep_handle);
3017 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003018 }
Florin Coras17ec5772020-08-12 20:46:29 -07003019 s->vep.next_sh = vep_session->vep.next_sh;
3020 s->vep.prev_sh = vep_handle;
3021 s->vep.vep_sh = vep_handle;
3022 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07003023 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07003024 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07003025 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Coras6c3b2182020-10-19 18:36:48 -07003026 s->flags &= ~VCL_SESSION_F_IS_VEP;
3027 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07003028 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08003029
Florin Coras470d72f2023-06-01 21:50:03 -07003030 if ((event->events & EPOLLOUT))
hanlin475c9d72019-12-26 11:44:28 +08003031 {
Florin Coras470d72f2023-06-01 21:50:03 -07003032 int write_ready = vcl_session_write_ready (s);
3033
3034 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
3035 if (write_ready > 0)
3036 {
3037 /* Generate EPOLLOUT if tx fifo not full */
3038 vcl_epoll_ctl_add_unhandled_event (
3039 wrk, s, event->events & EPOLLET, SESSION_IO_EVT_TX);
3040 add_evt = 1;
3041 }
3042 else
3043 {
3044 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
3045 }
hanlin475c9d72019-12-26 11:44:28 +08003046 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00003047 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07003048 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00003049 {
Florin Corasb0116a12023-02-07 09:11:47 -08003050 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
3051 SESSION_IO_EVT_RX);
Florin Coras87f76002021-06-28 19:13:29 -07003052 add_evt = 1;
3053 }
3054 if (!add_evt && vcl_session_is_closing (s))
3055 {
3056 session_event_t e = { 0 };
3057 if (s->session_state == VCL_STATE_VPP_CLOSING)
3058 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
3059 else
3060 e.event_type = SESSION_CTRL_EVT_RESET;
3061 e.session_index = s->session_index;
3062 e.postponed = 1;
3063 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00003064 }
Florin Corasa7a1a222018-12-30 17:11:31 -08003065 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
3066 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07003067 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003068 break;
3069
3070 case EPOLL_CTL_MOD:
3071 if (PREDICT_FALSE (!event))
3072 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003073 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04003074 rv = VPPCOM_EINVAL;
3075 goto done;
3076 }
Florin Coras6c3b2182020-10-19 18:36:48 -07003077 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003078 {
Florin Coras5e062572019-03-14 19:07:51 -07003079 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07003080 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003081 goto done;
3082 }
Florin Coras17ec5772020-08-12 20:46:29 -07003083 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05003084 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003085 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003086 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003087 rv = VPPCOM_EINVAL;
3088 goto done;
3089 }
hanlin475c9d72019-12-26 11:44:28 +08003090
Florin Coras470d72f2023-06-01 21:50:03 -07003091 /* Generate EPOLLOUT if session write ready and event was not on */
3092 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT))
3093 {
3094 /* Fifo size load acq synchronized with update store rel */
3095 int write_ready = vcl_session_write_ready (s);
3096
3097 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
3098 if (write_ready > 0)
3099 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
3100 SESSION_IO_EVT_TX);
3101 else
3102 /* Request deq ntf in case dequeue happened while updating flag */
3103 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
3104 }
3105 else if (!(event->events & EPOLLOUT))
Florin Coras607eb202023-05-29 16:19:38 -07003106 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasbc4d5b02023-05-17 23:20:56 -07003107
Florin Coras2645f682021-06-04 15:59:41 -07003108 /* Generate EPOLLIN if session read ready and event was not on */
3109 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
3110 (vcl_session_read_ready (s) > 0))
3111 {
Florin Corasb0116a12023-02-07 09:11:47 -08003112 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
3113 SESSION_IO_EVT_RX);
Florin Coras2645f682021-06-04 15:59:41 -07003114 }
Florin Coras17ec5772020-08-12 20:46:29 -07003115 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
3116 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07003117 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Corasbc4d5b02023-05-17 23:20:56 -07003118
Florin Corasa7a1a222018-12-30 17:11:31 -08003119 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
3120 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003121 break;
3122
3123 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07003124 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003125 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003126 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07003127 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003128 goto done;
3129 }
Florin Coras17ec5772020-08-12 20:46:29 -07003130 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05003131 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003132 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003133 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003134 rv = VPPCOM_EINVAL;
3135 goto done;
3136 }
3137
Florin Coras17ec5772020-08-12 20:46:29 -07003138 if (s->vep.prev_sh == vep_handle)
3139 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003140 else
3141 {
Florin Coras7e12d942018-06-27 14:32:43 -07003142 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07003143 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07003144 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003145 {
Florin Coras5e062572019-03-14 19:07:51 -07003146 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003147 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003148 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003149 }
Florin Coras134a9962018-08-28 11:32:04 -07003150 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003151 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003152 }
Florin Coras17ec5772020-08-12 20:46:29 -07003153 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003154 {
Florin Coras7e12d942018-06-27 14:32:43 -07003155 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07003156 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07003157 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003158 {
Florin Coras5e062572019-03-14 19:07:51 -07003159 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003160 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003161 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003162 }
Florin Coras134a9962018-08-28 11:32:04 -07003163 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003164 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003165 }
3166
Florin Corasfa3884f2021-06-22 20:04:46 -07003167 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
3168 vcl_epoll_lt_del (wrk, s);
3169
Florin Coras17ec5772020-08-12 20:46:29 -07003170 memset (&s->vep, 0, sizeof (s->vep));
3171 s->vep.next_sh = ~0;
3172 s->vep.prev_sh = ~0;
3173 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003174 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07003175 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08003176
Florin Corasf1ddeeb2021-06-10 08:08:53 -07003177 if (vcl_session_is_open (s))
Florin Coras607eb202023-05-29 16:19:38 -07003178 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08003179
Florin Coras5e062572019-03-14 19:07:51 -07003180 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003181 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003182 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003183 break;
3184
3185 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003186 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003187 rv = VPPCOM_EINVAL;
3188 }
3189
Florin Coras134a9962018-08-28 11:32:04 -07003190 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003191
3192done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003193 return rv;
3194}
3195
Florin Coras30ecfa82023-06-14 11:15:36 -07003196always_inline u8
3197vcl_ep_session_needs_evt (vcl_session_t *s, u32 evt)
3198{
3199 /* No event if not epolled / events reset on hup or level-trigger on */
3200 return ((s->vep.ev.events & evt) &&
3201 s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
3202}
3203
Florin Coras86f04502018-09-12 16:08:01 -07003204static inline void
3205vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3206 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003207{
3208 session_disconnected_msg_t *disconnected_msg;
3209 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003210 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003211 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003212 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003213 u8 add_event = 0;
3214
3215 switch (e->event_type)
3216 {
Florin Coras653e43f2019-03-04 10:56:23 -08003217 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003218 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003219 s = vcl_session_get (wrk, sid);
3220 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003221 break;
Florin Corasb242d312020-10-26 15:35:40 -07003222 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras30ecfa82023-06-14 11:15:36 -07003223 if (!vcl_ep_session_needs_evt (s, EPOLLIN) ||
3224 (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07003225 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003226 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003227 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003228 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003229 session_evt_data = s->vep.ev.data.u64;
3230 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003231 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003232 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003233 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003234 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003235 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003236 break;
Florin Coras48178552023-05-17 22:59:40 -07003237 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ? s->ct_tx_fifo :
3238 s->tx_fifo);
Florin Coras30ecfa82023-06-14 11:15:36 -07003239 if (!vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras86f04502018-09-12 16:08:01 -07003240 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003241 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003242 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003243 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003244 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003245 break;
Florin Coras86f04502018-09-12 16:08:01 -07003246 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003247 if (!e->postponed)
3248 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3249 else
3250 s = vcl_session_get (wrk, e->session_index);
Florin Coras30ecfa82023-06-14 11:15:36 -07003251 if (!s || !vcl_ep_session_needs_evt (s, EPOLLIN))
Florin Coras3c7d4f92018-12-14 11:28:43 -08003252 break;
Florin Corasb242d312020-10-26 15:35:40 -07003253 sid = s->session_index;
Florin Coras30ecfa82023-06-14 11:15:36 -07003254 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003255 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003256 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003257 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003258 break;
3259 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003260 if (!e->postponed)
3261 {
3262 connected_msg = (session_connected_msg_t *) e->data;
3263 sid = vcl_session_connected_handler (wrk, connected_msg);
3264 }
3265 else
3266 sid = e->session_index;
3267 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003268 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras72f77822019-01-22 19:05:52 -08003269 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003270 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07003271 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003272 add_event = 1;
Florin Coras30ecfa82023-06-14 11:15:36 -07003273 session_events = s->vep.ev.events;
3274 /* Generate EPOLLOUT because there's no connected event */
wanghanlin9e42cc22021-06-25 17:40:13 +08003275 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003276 session_evt_data = s->vep.ev.data.u64;
3277 if (s->session_state == VCL_STATE_DETACHED)
Florin Coras30ecfa82023-06-14 11:15:36 -07003278 {
3279 events[*num_ev].events |= EPOLLHUP;
3280 s->vep.ev.events = 0;
3281 }
Florin Coras86f04502018-09-12 16:08:01 -07003282 break;
3283 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003284 if (!e->postponed)
3285 {
3286 disconnected_msg = (session_disconnected_msg_t *) e->data;
3287 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3288 }
3289 else
3290 {
3291 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003292 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003293 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003294 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003295 {
Florin Coras74254b52021-12-08 11:03:08 -08003296 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003297 vcl_session_free (wrk, s);
3298 break;
3299 }
Florin Corasb242d312020-10-26 15:35:40 -07003300 sid = s->session_index;
3301 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003302 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003303 if (EPOLLRDHUP & session_events)
3304 {
3305 /* If app can distinguish between RDHUP and HUP,
3306 * we make finer control */
3307 events[*num_ev].events = EPOLLRDHUP;
3308 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3309 {
3310 events[*num_ev].events |= EPOLLHUP;
3311 }
3312 }
3313 else
3314 {
3315 events[*num_ev].events = EPOLLHUP;
3316 }
Florin Corasb242d312020-10-26 15:35:40 -07003317 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003318 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003319 break;
Florin Coras01ee7a72023-03-01 00:49:25 -08003320 case SESSION_CTRL_EVT_BOUND:
3321 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
3322 break;
Florin Coras86f04502018-09-12 16:08:01 -07003323 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003324 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003325 {
3326 sid =
3327 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3328 s = vcl_session_get (wrk, sid);
3329 }
Florin Coras87f76002021-06-28 19:13:29 -07003330 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003331 {
3332 sid = e->session_index;
3333 s = vcl_session_get (wrk, sid);
3334 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3335 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003336 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003337 {
Florin Coras74254b52021-12-08 11:03:08 -08003338 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003339 vcl_session_free (wrk, s);
3340 break;
3341 }
Florin Corasb242d312020-10-26 15:35:40 -07003342 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003343 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003344 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3345 if ((EPOLLRDHUP & session_events) &&
3346 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3347 {
Yacan Liuab157702022-09-26 16:41:32 +08003348 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003349 }
3350 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3351 {
3352 events[*num_ev].events |= EPOLLIN;
3353 }
Florin Corasb242d312020-10-26 15:35:40 -07003354 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003355 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003356 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003357 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3358 vcl_session_unlisten_reply_handler (wrk, e->data);
3359 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003360 case SESSION_CTRL_EVT_MIGRATED:
3361 vcl_session_migrated_handler (wrk, e->data);
3362 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003363 case SESSION_CTRL_EVT_CLEANUP:
3364 vcl_session_cleanup_handler (wrk, e->data);
3365 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003366 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3367 vcl_session_req_worker_update_handler (wrk, e->data);
3368 break;
3369 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3370 vcl_session_worker_update_reply_handler (wrk, e->data);
3371 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003372 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3373 vcl_session_app_add_segment_handler (wrk, e->data);
3374 break;
3375 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3376 vcl_session_app_del_segment_handler (wrk, e->data);
3377 break;
hanlina3a48962020-07-13 11:09:15 +08003378 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003379 vcl_worker_rpc_handler (wrk, e->data);
3380 break;
Florin Coras86f04502018-09-12 16:08:01 -07003381 default:
3382 VDBG (0, "unhandled: %u", e->event_type);
3383 break;
3384 }
3385
3386 if (add_event)
3387 {
Florin Coras30ecfa82023-06-14 11:15:36 -07003388 ASSERT (s->flags & VCL_SESSION_F_IS_VEP_SESSION);
Florin Coras86f04502018-09-12 16:08:01 -07003389 events[*num_ev].data.u64 = session_evt_data;
3390 if (EPOLLONESHOT & session_events)
3391 {
Florin Corasb242d312020-10-26 15:35:40 -07003392 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003393 if (!(events[*num_ev].events & EPOLLHUP))
3394 s->vep.ev.events = EPOLLHUP | EPOLLERR;
Florin Coras86f04502018-09-12 16:08:01 -07003395 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003396 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003397 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003398 s = vcl_session_get (wrk, sid);
3399 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3400 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003401 }
Florin Coras86f04502018-09-12 16:08:01 -07003402 *num_ev += 1;
3403 }
3404}
3405
3406static int
3407vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3408 struct epoll_event *events, u32 maxevents,
3409 double wait_for_time, u32 * num_ev)
3410{
Florin Coras99368312018-08-02 10:45:44 -07003411 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003412 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003413 int i;
3414
Florin Coras539663c2018-09-28 14:59:37 -07003415 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3416 goto handle_dequeued;
3417
Florin Coras54693d22018-07-17 10:46:29 -07003418 if (svm_msg_q_is_empty (mq))
3419 {
3420 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003421 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003422 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003423 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003424 else
3425 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003426 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003427 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003428 }
3429 }
Florin Corase003a1b2019-06-05 10:47:16 -07003430 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003431 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003432
Florin Coras539663c2018-09-28 14:59:37 -07003433handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003434 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003435 {
Florin Coras134a9962018-08-28 11:32:04 -07003436 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003437 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003438 if (*num_ev < maxevents)
3439 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3440 else
3441 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003442 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003443 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003444 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003445 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003446 return *num_ev;
3447}
3448
Florin Coras99368312018-08-02 10:45:44 -07003449static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003450vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3451 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003452{
Florin Corasccdb8b82021-04-28 13:01:06 -07003453 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003454
3455 if (!n_evts)
3456 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003457 if (timeout_ms > 0)
3458 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003459 }
3460
3461 do
3462 {
3463 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003464 timeout_ms, &n_evts);
3465 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003466 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003467 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003468 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003469
3470 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003471}
3472
3473static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003474vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3475 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003476{
Florin Coras99368312018-08-02 10:45:44 -07003477 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003478 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003479 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003480 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003481 u64 buf;
3482
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003483 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3484 {
3485 vcl_api_retry_attach (wrk);
3486 return n_evts;
3487 }
3488
Florin Coras134a9962018-08-28 11:32:04 -07003489 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003490 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003491 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003492 if (timeout_ms > 0)
3493 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003494 }
3495
Florin Corasb13ba132021-01-27 16:05:24 -08003496 do
3497 {
3498 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003499 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003500 if (n_mq_evts < 0)
3501 {
Aritra Basu119cbf22024-07-30 16:03:59 -07003502 if (errno == EINTR)
3503 continue;
3504
Florin Corasb13ba132021-01-27 16:05:24 -08003505 VDBG (0, "epoll_wait error %u", errno);
3506 return n_evts;
3507 }
3508
3509 for (i = 0; i < n_mq_evts; i++)
3510 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003511 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3512 {
3513 /* api socket was closed */
3514 vcl_api_handle_disconnect (wrk);
3515 continue;
3516 }
3517
Florin Corasb13ba132021-01-27 16:05:24 -08003518 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3519 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3520 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3521 &n_evts);
3522 }
3523
Florin Corasccdb8b82021-04-28 13:01:06 -07003524 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003525 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003526 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003527 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003528
3529 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003530}
3531
Florin Corasfe286f72021-06-04 10:07:55 -07003532static void
Florin Corasfe286f72021-06-04 10:07:55 -07003533vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3534 int maxevents, u32 *n_evts)
3535{
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003536 u32 add_event = 0, evt_flags = 0, next, *to_remove = 0, *si;
Florin Corasfe286f72021-06-04 10:07:55 -07003537 vcl_session_t *s;
3538 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003539 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003540
Florin Corasfa3884f2021-06-22 20:04:46 -07003541 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003542 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003543 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003544
Florin Corasfa3884f2021-06-22 20:04:46 -07003545 next = wrk->ep_lt_current;
3546 do
Florin Corasfe286f72021-06-04 10:07:55 -07003547 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003548 s = vcl_session_get (wrk, next);
3549 next = s->vep.lt_next;
3550
Florin Coras30ecfa82023-06-14 11:15:36 -07003551 if (s->vep.ev.events == 0)
3552 {
3553 vec_add1 (to_remove, s->session_index);
3554 continue;
3555 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003556 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003557 {
3558 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003559 evt_flags |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003560 evt_data = s->vep.ev.data.u64;
3561 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003562 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003563 {
3564 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003565 evt_flags |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
Florin Corasfa3884f2021-06-22 20:04:46 -07003566 evt_data = s->vep.ev.data.u64;
3567 }
3568 if (!add_event && s->session_state > VCL_STATE_READY)
3569 {
3570 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003571 evt_flags |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003572 evt_data = s->vep.ev.data.u64;
3573 }
3574 if (add_event)
3575 {
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003576 events[*n_evts].events = evt_flags;
Florin Corasfe286f72021-06-04 10:07:55 -07003577 events[*n_evts].data.u64 = evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003578 if (EPOLLONESHOT & s->vep.ev.events)
Florin Coras30ecfa82023-06-14 11:15:36 -07003579 s->vep.ev.events = EPOLLHUP | EPOLLERR;
3580 if (evt_flags & EPOLLHUP)
Florin Corasfa3884f2021-06-22 20:04:46 -07003581 s->vep.ev.events = 0;
Florin Coras5cca6692023-06-20 08:47:37 -07003582 *n_evts += 1;
3583 add_event = 0;
3584 evt_flags = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003585 if (*n_evts == maxevents)
3586 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003587 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003588 break;
3589 }
3590 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003591 else
3592 {
Florin Coras7ff72742023-05-16 13:05:28 -07003593 vec_add1 (to_remove, s->session_index);
Florin Corasfa3884f2021-06-22 20:04:46 -07003594 }
Florin Corasfe286f72021-06-04 10:07:55 -07003595 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003596 while (next != wrk->ep_lt_current);
Florin Coras7ff72742023-05-16 13:05:28 -07003597
3598 vec_foreach (si, to_remove)
3599 {
3600 s = vcl_session_get (wrk, *si);
3601 vcl_epoll_lt_del (wrk, s);
3602 }
3603 vec_free (to_remove);
Florin Corasfe286f72021-06-04 10:07:55 -07003604}
3605
Dave Wallacef7f809c2017-10-03 01:48:42 -04003606int
Florin Coras134a9962018-08-28 11:32:04 -07003607vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003608 int maxevents, double wait_for_time)
3609{
Florin Coras134a9962018-08-28 11:32:04 -07003610 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003611 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003612 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003613 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003614
3615 if (PREDICT_FALSE (maxevents <= 0))
3616 {
Florin Coras5e062572019-03-14 19:07:51 -07003617 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003618 return VPPCOM_EINVAL;
3619 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003620
Florin Coras134a9962018-08-28 11:32:04 -07003621 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003622 if (!vep_session)
3623 return VPPCOM_EBADFD;
3624
Florin Coras6c3b2182020-10-19 18:36:48 -07003625 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003626 {
Florin Coras5e062572019-03-14 19:07:51 -07003627 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003628 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003629 }
Florin Coras54693d22018-07-17 10:46:29 -07003630
Florin Coras86f04502018-09-12 16:08:01 -07003631 if (vec_len (wrk->unhandled_evts_vector))
3632 {
3633 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3634 {
3635 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3636 events, &n_evts);
3637 if (n_evts == maxevents)
3638 {
Florin Corase003a1b2019-06-05 10:47:16 -07003639 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3640 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003641 }
3642 }
Florin Corase003a1b2019-06-05 10:47:16 -07003643 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003644 }
liuyacancba87102021-09-10 15:14:05 +08003645
3646 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3647 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3648
wanghanlin8919fec2021-03-18 20:00:41 +08003649 /* Request to only drain unhandled */
3650 if ((int) wait_for_time == -2)
3651 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003652
Florin Coras86f04502018-09-12 16:08:01 -07003653
Florin Corasfe286f72021-06-04 10:07:55 -07003654 if (vcm->cfg.use_mq_eventfd)
3655 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3656 wait_for_time);
3657 else
3658 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3659 wait_for_time);
3660
Florin Corasfe286f72021-06-04 10:07:55 -07003661 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003662}
3663
Dave Wallace35830af2017-10-09 01:43:42 -04003664int
Florin Coras134a9962018-08-28 11:32:04 -07003665vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003666 void *buffer, uint32_t * buflen)
3667{
Florin Coras134a9962018-08-28 11:32:04 -07003668 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003669 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003670 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003671 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003672 vcl_session_t *session;
3673 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003674
Florin Coras134a9962018-08-28 11:32:04 -07003675 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003676 if (!session)
3677 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003678
Dave Wallace35830af2017-10-09 01:43:42 -04003679 switch (op)
3680 {
3681 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003682 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003683 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3684 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003685 break;
3686
Dave Wallace227867f2017-11-13 21:21:53 -05003687 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003688 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003689 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3690 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003691 break;
Florin Coras8ae63db2024-03-18 12:25:38 -07003692 case VPPCOM_ATTR_GET_NWRITEQ:
3693 if (PREDICT_FALSE (!buffer || !buflen || *buflen != sizeof (int)))
3694 {
3695 rv = VPPCOM_EINVAL;
3696 break;
3697 }
3698 if (!session->tx_fifo || session->session_state == VCL_STATE_DETACHED)
3699 {
3700 rv = VPPCOM_EINVAL;
3701 break;
3702 }
3703 *(int *) buffer = svm_fifo_max_dequeue (session->tx_fifo);
3704 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003705 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003706 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003707 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003708 *flags =
3709 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003710 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003711 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003712 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003713 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3714 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003715 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003716 }
3717 else
3718 rv = VPPCOM_EINVAL;
3719 break;
3720
3721 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003722 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003723 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003724 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003725 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003726 else
Florin Corasac422d62020-10-19 20:51:36 -07003727 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003728
Florin Coras5e062572019-03-14 19:07:51 -07003729 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3730 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003731 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003732 }
3733 else
3734 rv = VPPCOM_EINVAL;
3735 break;
3736
3737 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003738 if (PREDICT_TRUE (buffer && buflen &&
3739 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003740 {
Florin Coras7e12d942018-06-27 14:32:43 -07003741 ep->is_ip4 = session->transport.is_ip4;
3742 ep->port = session->transport.rmt_port;
3743 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003744 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3745 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003746 else
Dave Barach178cf492018-11-13 16:34:13 -05003747 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3748 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003749 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003750 VDBG (1,
3751 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3752 "addr = %U, port %u",
3753 session_handle, ep->is_ip4, vcl_format_ip46_address,
3754 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003755 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3756 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003757 }
3758 else
3759 rv = VPPCOM_EINVAL;
3760 break;
3761
3762 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003763 if (PREDICT_TRUE (buffer && buflen &&
3764 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003765 {
Florin Coras7e12d942018-06-27 14:32:43 -07003766 ep->is_ip4 = session->transport.is_ip4;
3767 ep->port = session->transport.lcl_port;
3768 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003769 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3770 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003771 else
Dave Barach178cf492018-11-13 16:34:13 -05003772 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3773 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003774 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003775 VDBG (1,
3776 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3777 " port %d",
3778 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003779 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003780 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3781 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003782 }
3783 else
3784 rv = VPPCOM_EINVAL;
3785 break;
Stevenb5a11602017-10-11 09:59:30 -07003786
qinyangaf9b7152023-06-27 01:11:53 -07003787 case VPPCOM_ATTR_GET_ORIGINAL_DST:
3788 if (!session->transport.is_ip4)
3789 {
3790 /* now original dst only support ipv4*/
3791 rv = VPPCOM_EAFNOSUPPORT;
3792 break;
3793 }
3794 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*ep)) &&
3795 ep->ip))
3796 {
3797 ep->is_ip4 = session->transport.is_ip4;
3798 ep->port = session->original_dst_port;
3799 clib_memcpy_fast (ep->ip, &session->original_dst_ip4,
3800 sizeof (ip4_address_t));
3801 *buflen = sizeof (*ep);
3802 VDBG (1,
3803 "VPPCOM_ATTR_GET_ORIGINAL_DST: sh %u, is_ip4 = %u, addr = %U"
3804 " port %d",
3805 session_handle, ep->is_ip4, vcl_format_ip4_address,
3806 (ip4_address_t *) (&session->original_dst_ip4),
3807 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3808 clib_net_to_host_u16 (ep->port));
3809 }
3810 else
3811 rv = VPPCOM_EINVAL;
3812 break;
3813
Florin Corasef7cbf62019-10-17 09:56:27 -07003814 case VPPCOM_ATTR_SET_LCL_ADDR:
3815 if (PREDICT_TRUE (buffer && buflen &&
3816 (*buflen >= sizeof (*ep)) && ep->ip))
3817 {
3818 session->transport.is_ip4 = ep->is_ip4;
3819 session->transport.lcl_port = ep->port;
3820 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3821 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003822 VDBG (1,
3823 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3824 " port %d",
3825 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003826 &session->transport.lcl_ip,
3827 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3828 clib_net_to_host_u16 (ep->port));
3829 }
3830 else
3831 rv = VPPCOM_EINVAL;
3832 break;
3833
Dave Wallace048b1d62018-01-03 22:24:41 -05003834 case VPPCOM_ATTR_GET_LIBC_EPFD:
3835 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003836 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003837 break;
3838
3839 case VPPCOM_ATTR_SET_LIBC_EPFD:
3840 if (PREDICT_TRUE (buffer && buflen &&
3841 (*buflen == sizeof (session->libc_epfd))))
3842 {
3843 session->libc_epfd = *(int *) buffer;
3844 *buflen = sizeof (session->libc_epfd);
3845
Florin Coras5e062572019-03-14 19:07:51 -07003846 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3847 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003848 }
3849 else
3850 rv = VPPCOM_EINVAL;
3851 break;
3852
3853 case VPPCOM_ATTR_GET_PROTOCOL:
3854 if (buffer && buflen && (*buflen >= sizeof (int)))
3855 {
Florin Coras7e12d942018-06-27 14:32:43 -07003856 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003857 *buflen = sizeof (int);
3858
Florin Coras5e062572019-03-14 19:07:51 -07003859 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3860 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003861 }
3862 else
3863 rv = VPPCOM_EINVAL;
3864 break;
3865
3866 case VPPCOM_ATTR_GET_LISTEN:
3867 if (buffer && buflen && (*buflen >= sizeof (int)))
3868 {
Florin Corasac422d62020-10-19 20:51:36 -07003869 *(int *) buffer = vcl_session_has_attr (session,
3870 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003871 *buflen = sizeof (int);
3872
Florin Coras5e062572019-03-14 19:07:51 -07003873 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3874 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003875 }
3876 else
3877 rv = VPPCOM_EINVAL;
3878 break;
3879
3880 case VPPCOM_ATTR_GET_ERROR:
3881 if (buffer && buflen && (*buflen >= sizeof (int)))
3882 {
3883 *(int *) buffer = 0;
3884 *buflen = sizeof (int);
3885
Florin Coras5e062572019-03-14 19:07:51 -07003886 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3887 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003888 }
3889 else
3890 rv = VPPCOM_EINVAL;
3891 break;
3892
3893 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3894 if (buffer && buflen && (*buflen >= sizeof (u32)))
3895 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003896
3897 /* VPP-TBD */
3898 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003899 session->tx_fifo ?
3900 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003901 vcm->cfg.tx_fifo_size);
3902 *buflen = sizeof (u32);
3903
Florin Coras5e062572019-03-14 19:07:51 -07003904 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3905 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3906 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003907 }
3908 else
3909 rv = VPPCOM_EINVAL;
3910 break;
3911
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003912 case VPPCOM_ATTR_SET_DSCP:
3913 if (buffer && buflen && (*buflen >= sizeof (u8)))
3914 {
3915 session->dscp = *(u8 *) buffer;
3916
3917 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3918 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3919 }
3920 else
3921 rv = VPPCOM_EINVAL;
3922 break;
3923
Dave Wallace048b1d62018-01-03 22:24:41 -05003924 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3925 if (buffer && buflen && (*buflen == sizeof (u32)))
3926 {
3927 /* VPP-TBD */
3928 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003929 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3930 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3931 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003932 }
3933 else
3934 rv = VPPCOM_EINVAL;
3935 break;
3936
3937 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3938 if (buffer && buflen && (*buflen >= sizeof (u32)))
3939 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003940
3941 /* VPP-TBD */
3942 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003943 session->rx_fifo ?
3944 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003945 vcm->cfg.rx_fifo_size);
3946 *buflen = sizeof (u32);
3947
Florin Coras5e062572019-03-14 19:07:51 -07003948 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3949 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003950 }
3951 else
3952 rv = VPPCOM_EINVAL;
3953 break;
3954
3955 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3956 if (buffer && buflen && (*buflen == sizeof (u32)))
3957 {
3958 /* VPP-TBD */
3959 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003960 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3961 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3962 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003963 }
3964 else
3965 rv = VPPCOM_EINVAL;
3966 break;
3967
3968 case VPPCOM_ATTR_GET_REUSEADDR:
3969 if (buffer && buflen && (*buflen >= sizeof (int)))
3970 {
3971 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003972 *(int *) buffer = vcl_session_has_attr (session,
3973 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003974 *buflen = sizeof (int);
3975
Florin Coras5e062572019-03-14 19:07:51 -07003976 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3977 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003978 }
3979 else
3980 rv = VPPCOM_EINVAL;
3981 break;
3982
Stevenb5a11602017-10-11 09:59:30 -07003983 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003984 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003985 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003986 {
3987 /* VPP-TBD */
3988 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003989 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003990 else
Florin Corasac422d62020-10-19 20:51:36 -07003991 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003992
Florin Coras5e062572019-03-14 19:07:51 -07003993 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003994 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003995 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003996 }
3997 else
3998 rv = VPPCOM_EINVAL;
3999 break;
4000
4001 case VPPCOM_ATTR_GET_REUSEPORT:
4002 if (buffer && buflen && (*buflen >= sizeof (int)))
4003 {
4004 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004005 *(int *) buffer = vcl_session_has_attr (session,
4006 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05004007 *buflen = sizeof (int);
4008
Florin Coras5e062572019-03-14 19:07:51 -07004009 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
4010 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004011 }
4012 else
4013 rv = VPPCOM_EINVAL;
4014 break;
4015
4016 case VPPCOM_ATTR_SET_REUSEPORT:
4017 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07004018 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05004019 {
4020 /* VPP-TBD */
4021 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004022 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05004023 else
Florin Corasac422d62020-10-19 20:51:36 -07004024 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05004025
Florin Coras5e062572019-03-14 19:07:51 -07004026 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004027 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07004028 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004029 }
4030 else
4031 rv = VPPCOM_EINVAL;
4032 break;
4033
4034 case VPPCOM_ATTR_GET_BROADCAST:
4035 if (buffer && buflen && (*buflen >= sizeof (int)))
4036 {
4037 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004038 *(int *) buffer = vcl_session_has_attr (session,
4039 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05004040 *buflen = sizeof (int);
4041
Florin Coras5e062572019-03-14 19:07:51 -07004042 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
4043 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004044 }
4045 else
4046 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07004047 break;
4048
4049 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05004050 if (buffer && buflen && (*buflen == sizeof (int)))
4051 {
4052 /* VPP-TBD */
4053 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004054 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05004055 else
Florin Corasac422d62020-10-19 20:51:36 -07004056 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05004057
Florin Coras5e062572019-03-14 19:07:51 -07004058 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004059 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07004060 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004061 }
4062 else
4063 rv = VPPCOM_EINVAL;
4064 break;
4065
4066 case VPPCOM_ATTR_GET_V6ONLY:
4067 if (buffer && buflen && (*buflen >= sizeof (int)))
4068 {
4069 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004070 *(int *) buffer = vcl_session_has_attr (session,
4071 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004072 *buflen = sizeof (int);
4073
Florin Coras5e062572019-03-14 19:07:51 -07004074 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
4075 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004076 }
4077 else
4078 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07004079 break;
4080
4081 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05004082 if (buffer && buflen && (*buflen == sizeof (int)))
4083 {
4084 /* VPP-TBD */
4085 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004086 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004087 else
Florin Corasac422d62020-10-19 20:51:36 -07004088 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004089
Florin Coras5e062572019-03-14 19:07:51 -07004090 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004091 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07004092 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004093 }
4094 else
4095 rv = VPPCOM_EINVAL;
4096 break;
4097
4098 case VPPCOM_ATTR_GET_KEEPALIVE:
4099 if (buffer && buflen && (*buflen >= sizeof (int)))
4100 {
4101 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004102 *(int *) buffer = vcl_session_has_attr (session,
4103 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004104 *buflen = sizeof (int);
4105
Florin Coras5e062572019-03-14 19:07:51 -07004106 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
4107 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004108 }
4109 else
4110 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07004111 break;
Stevenbccd3392017-10-12 20:42:21 -07004112
4113 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05004114 if (buffer && buflen && (*buflen == sizeof (int)))
4115 {
4116 /* VPP-TBD */
4117 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004118 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004119 else
Florin Corasac422d62020-10-19 20:51:36 -07004120 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004121
Florin Coras5e062572019-03-14 19:07:51 -07004122 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004123 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07004124 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004125 }
4126 else
4127 rv = VPPCOM_EINVAL;
4128 break;
4129
4130 case VPPCOM_ATTR_GET_TCP_NODELAY:
4131 if (buffer && buflen && (*buflen >= sizeof (int)))
4132 {
4133 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004134 *(int *) buffer = vcl_session_has_attr (session,
4135 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004136 *buflen = sizeof (int);
4137
Florin Coras5e062572019-03-14 19:07:51 -07004138 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
4139 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004140 }
4141 else
4142 rv = VPPCOM_EINVAL;
4143 break;
4144
4145 case VPPCOM_ATTR_SET_TCP_NODELAY:
4146 if (buffer && buflen && (*buflen == sizeof (int)))
4147 {
4148 /* VPP-TBD */
4149 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004150 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004151 else
Florin Corasac422d62020-10-19 20:51:36 -07004152 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004153
Florin Coras5e062572019-03-14 19:07:51 -07004154 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004155 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07004156 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004157 }
4158 else
4159 rv = VPPCOM_EINVAL;
4160 break;
4161
4162 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
4163 if (buffer && buflen && (*buflen >= sizeof (int)))
4164 {
4165 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004166 *(int *) buffer = vcl_session_has_attr (session,
4167 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004168 *buflen = sizeof (int);
4169
Florin Coras5e062572019-03-14 19:07:51 -07004170 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
4171 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004172 }
4173 else
4174 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004175 break;
4176
4177 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05004178 if (buffer && buflen && (*buflen == sizeof (int)))
4179 {
4180 /* VPP-TBD */
4181 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004182 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004183 else
Florin Corasac422d62020-10-19 20:51:36 -07004184 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004185
Florin Coras5e062572019-03-14 19:07:51 -07004186 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004187 vcl_session_has_attr (session,
4188 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004189 }
4190 else
4191 rv = VPPCOM_EINVAL;
4192 break;
4193
4194 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
4195 if (buffer && buflen && (*buflen >= sizeof (int)))
4196 {
4197 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004198 *(int *) buffer = vcl_session_has_attr (session,
4199 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004200 *buflen = sizeof (int);
4201
Florin Coras5e062572019-03-14 19:07:51 -07004202 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
4203 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004204 }
4205 else
4206 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004207 break;
4208
4209 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05004210 if (buffer && buflen && (*buflen == sizeof (int)))
4211 {
4212 /* VPP-TBD */
4213 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004214 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004215 else
Florin Corasac422d62020-10-19 20:51:36 -07004216 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004217
Florin Coras5e062572019-03-14 19:07:51 -07004218 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004219 vcl_session_has_attr (session,
4220 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004221 }
4222 else
4223 rv = VPPCOM_EINVAL;
4224 break;
4225
4226 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004227 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004228 {
Florin Coras04ae8272021-04-12 19:55:37 -07004229 rv = VPPCOM_EINVAL;
4230 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004231 }
Florin Coras04ae8272021-04-12 19:55:37 -07004232
4233 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4234 tea.mss = *(u32 *) buffer;
4235 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
4236 rv = VPPCOM_ENOPROTOOPT;
4237
4238 if (!rv)
4239 {
4240 *(u32 *) buffer = tea.mss;
4241 *buflen = sizeof (int);
4242 }
4243
4244 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4245 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004246 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004247 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004248 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004249 {
Florin Coras04ae8272021-04-12 19:55:37 -07004250 rv = VPPCOM_EINVAL;
4251 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004252 }
Florin Coras04ae8272021-04-12 19:55:37 -07004253
4254 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4255 tea.mss = *(u32 *) buffer;
4256 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4257 rv = VPPCOM_ENOPROTOOPT;
4258
4259 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4260 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004261 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004262
Florin Coras1e966172020-05-16 18:18:14 +00004263 case VPPCOM_ATTR_SET_CONNECTED:
4264 session->flags |= VCL_SESSION_F_CONNECTED;
4265 break;
4266
Florin Corasa5a9efd2021-01-05 17:03:29 -08004267 case VPPCOM_ATTR_SET_CKPAIR:
4268 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4269 !vcl_session_has_crypto (session))
4270 {
4271 rv = VPPCOM_EINVAL;
4272 break;
4273 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004274 if (!session->ext_config)
4275 {
Florin Coras87f63892021-05-01 16:01:40 -07004276 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4277 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004278 }
4279 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4280 {
4281 rv = VPPCOM_EINVAL;
4282 break;
4283 }
4284
4285 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004286 break;
4287
Florin Coras6a6555a2021-01-28 11:39:27 -08004288 case VPPCOM_ATTR_SET_VRF:
4289 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4290 {
4291 rv = VPPCOM_EINVAL;
4292 break;
4293 }
4294 session->vrf = *(u32 *) buffer;
4295 break;
4296
4297 case VPPCOM_ATTR_GET_VRF:
4298 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4299 {
4300 rv = VPPCOM_EINVAL;
4301 break;
4302 }
4303 *(u32 *) buffer = session->vrf;
4304 *buflen = sizeof (u32);
4305 break;
4306
wanghanlin0674f852021-02-22 10:38:36 +08004307 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004308 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004309 {
Florin Corasd77325c2021-02-23 12:03:03 -08004310 rv = VPPCOM_EINVAL;
4311 break;
wanghanlin0674f852021-02-22 10:38:36 +08004312 }
Florin Corasd77325c2021-02-23 12:03:03 -08004313
4314 if (session->transport.is_ip4)
4315 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004316 else
Florin Corasd77325c2021-02-23 12:03:03 -08004317 *(int *) buffer = AF_INET6;
4318 *buflen = sizeof (int);
4319
wanghanlin0674f852021-02-22 10:38:36 +08004320 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4321 *buflen);
4322 break;
4323
Florin Coras87f63892021-05-01 16:01:40 -07004324 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4325 if (!(buffer && buflen && (*buflen > 0)))
4326 {
4327 rv = VPPCOM_EINVAL;
4328 break;
4329 }
4330 if (session->ext_config)
4331 {
4332 rv = VPPCOM_EINVAL;
4333 break;
4334 }
4335 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4336 *buflen + sizeof (u32));
4337 clib_memcpy (session->ext_config->data, buffer, *buflen);
4338 session->ext_config->len = *buflen;
4339 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004340 case VPPCOM_ATTR_SET_IP_PKTINFO:
4341 if (buffer && buflen && (*buflen == sizeof (int)) &&
4342 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4343 {
4344 if (*(int *) buffer)
4345 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4346 else
4347 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4348
4349 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4350 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4351 *buflen);
4352 }
4353 else
4354 rv = VPPCOM_EINVAL;
4355 break;
4356
4357 case VPPCOM_ATTR_GET_IP_PKTINFO:
4358 if (buffer && buflen && (*buflen >= sizeof (int)))
4359 {
4360 *(int *) buffer =
4361 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4362 *buflen = sizeof (int);
4363
4364 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4365 *buflen);
4366 }
4367 else
4368 rv = VPPCOM_EINVAL;
4369 break;
Florin Coras87f63892021-05-01 16:01:40 -07004370
Dave Wallacee22aa742017-10-20 12:30:38 -04004371 default:
4372 rv = VPPCOM_EINVAL;
4373 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004374 }
4375
Dave Wallace35830af2017-10-09 01:43:42 -04004376 return rv;
4377}
4378
Stevenac1f96d2017-10-24 16:03:58 -07004379int
Florin Coras134a9962018-08-28 11:32:04 -07004380vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004381 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4382{
Florin Coras134a9962018-08-28 11:32:04 -07004383 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004384 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004385 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004386
4387 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004388 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004389 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004390 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004391 else
4392 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004393 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004394 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004395 }
4396
Florin Coras0a1e1832020-03-29 18:54:04 +00004397 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004398 {
Florin Coras5da10c42020-04-01 04:31:21 +00004399 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004400 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004401 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4402 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004403 else
Dave Barach178cf492018-11-13 16:34:13 -05004404 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4405 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004406 ep->is_ip4 = session->transport.is_ip4;
4407 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004408 }
Florin Coras460dce62018-07-27 05:45:06 -07004409
Stevenac1f96d2017-10-24 16:03:58 -07004410 return rv;
4411}
4412
Florin Coraseff5f7a2023-02-07 17:36:17 -08004413static void
4414vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4415{
4416 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4417
4418 do
4419 {
4420 switch (tlv->data_type)
4421 {
4422 case VCL_UDP_SEGMENT:
4423 s->gso_size = *(u16 *) tlv->data;
4424 break;
4425 case VCL_IP_PKTINFO:
4426 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4427 sizeof (ip4_address_t));
4428 break;
4429 default:
4430 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4431 break;
4432 }
4433 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4434 }
4435 while (tlv);
4436}
4437
Stevenac1f96d2017-10-24 16:03:58 -07004438int
Florin Coras134a9962018-08-28 11:32:04 -07004439vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004440 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4441{
Florin Coras7a2abce2020-04-05 19:25:44 +00004442 vcl_worker_t *wrk = vcl_worker_get_current ();
4443 vcl_session_t *s;
4444
4445 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004446 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004447 return VPPCOM_EBADFD;
4448
Dave Wallace617dffa2017-10-26 14:47:06 -04004449 if (ep)
4450 {
Florin Coras5824cc52020-10-20 18:44:41 -07004451 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004452 return VPPCOM_EINVAL;
4453
liuyacanbc0c7542021-08-02 20:15:05 +08004454 s->transport.is_ip4 = ep->is_ip4;
4455 s->transport.rmt_port = ep->port;
4456 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4457
Florin Coraseff5f7a2023-02-07 17:36:17 -08004458 if (ep->app_tlvs)
4459 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004460
Florin Coras5da10c42020-04-01 04:31:21 +00004461 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004462 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004463 {
Florin Coras5824cc52020-10-20 18:44:41 -07004464 u32 session_index = s->session_index;
4465 f64 timeout = vcm->cfg.session_timeout;
4466 int rv;
4467
Florin Coras5da10c42020-04-01 04:31:21 +00004468 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004469 rv = vppcom_wait_for_session_state_change (session_index,
4470 VCL_STATE_READY,
4471 timeout);
4472 if (rv < 0)
4473 return rv;
4474 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004475 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004476 }
4477
4478 if (flags)
4479 {
4480 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004481 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004482 }
4483
Florin Coraseff5f7a2023-02-07 17:36:17 -08004484 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004485 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004486}
4487
Dave Wallace048b1d62018-01-03 22:24:41 -05004488int
4489vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4490{
Florin Coras134a9962018-08-28 11:32:04 -07004491 vcl_worker_t *wrk = vcl_worker_get_current ();
4492 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004493 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004494 svm_msg_q_msg_t msg;
4495 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004496 int rv, num_ev = 0;
4497
Florin Coras5e062572019-03-14 19:07:51 -07004498 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004499
4500 if (!vp)
4501 return VPPCOM_EFAULT;
4502
4503 do
4504 {
Florin Coras7e12d942018-06-27 14:32:43 -07004505 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004506
Florin Coras6917b942018-11-13 22:44:54 -08004507 /* Dequeue all events and drop all unhandled io events */
4508 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4509 {
4510 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4511 vcl_handle_mq_event (wrk, e);
4512 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4513 }
4514 vec_reset_length (wrk->unhandled_evts_vector);
4515
Dave Wallace048b1d62018-01-03 22:24:41 -05004516 for (i = 0; i < n_sids; i++)
4517 {
Florin Coras7baeb712019-01-04 17:05:43 -08004518 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004519 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004520 {
4521 vp[i].revents = POLLHUP;
4522 num_ev++;
4523 continue;
4524 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004525
Florin Coras6917b942018-11-13 22:44:54 -08004526 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004527
4528 if (POLLIN & vp[i].events)
4529 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004530 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004531 if (rv > 0)
4532 {
Florin Coras6917b942018-11-13 22:44:54 -08004533 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004534 num_ev++;
4535 }
4536 else if (rv < 0)
4537 {
4538 switch (rv)
4539 {
4540 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004541 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004542 break;
4543
4544 default:
Florin Coras6917b942018-11-13 22:44:54 -08004545 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004546 break;
4547 }
4548 num_ev++;
4549 }
4550 }
4551
4552 if (POLLOUT & vp[i].events)
4553 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004554 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004555 if (rv > 0)
4556 {
Florin Coras6917b942018-11-13 22:44:54 -08004557 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004558 num_ev++;
4559 }
4560 else if (rv < 0)
4561 {
4562 switch (rv)
4563 {
4564 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004565 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004566 break;
4567
4568 default:
Florin Coras6917b942018-11-13 22:44:54 -08004569 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004570 break;
4571 }
4572 num_ev++;
4573 }
4574 }
4575
Dave Wallace7e607a72018-06-18 18:41:32 -04004576 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004577 {
Florin Coras6917b942018-11-13 22:44:54 -08004578 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004579 num_ev++;
4580 }
4581 }
4582 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004583 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004584 }
4585 while ((num_ev == 0) && keep_trying);
4586
Dave Wallace048b1d62018-01-03 22:24:41 -05004587 return num_ev;
4588}
4589
Florin Coras99368312018-08-02 10:45:44 -07004590int
4591vppcom_mq_epoll_fd (void)
4592{
Florin Coras134a9962018-08-28 11:32:04 -07004593 vcl_worker_t *wrk = vcl_worker_get_current ();
4594 return wrk->mqs_epfd;
4595}
4596
4597int
Florin Coras30e79c22019-01-02 19:31:22 -08004598vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004599{
4600 return session_handle & 0xFFFFFF;
4601}
4602
4603int
Florin Coras30e79c22019-01-02 19:31:22 -08004604vppcom_session_worker (vcl_session_handle_t session_handle)
4605{
4606 return session_handle >> 24;
4607}
4608
4609int
Florin Coras134a9962018-08-28 11:32:04 -07004610vppcom_worker_register (void)
4611{
Florin Coras47c40e22018-11-26 17:01:36 -08004612 if (!vcl_worker_alloc_and_init ())
4613 return VPPCOM_EEXIST;
4614
Florin Coras47c40e22018-11-26 17:01:36 -08004615 if (vcl_worker_register_with_vpp ())
4616 return VPPCOM_EEXIST;
4617
4618 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004619}
4620
Florin Coras369db832019-07-08 12:34:45 -07004621void
4622vppcom_worker_unregister (void)
4623{
4624 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4625 vcl_set_worker_index (~0);
4626}
4627
Pivo6017ff02020-05-04 17:57:33 +02004628void
4629vppcom_worker_index_set (int index)
4630{
4631 vcl_set_worker_index (index);
4632}
4633
Florin Corasdfe4cf42018-11-28 22:13:45 -08004634int
4635vppcom_worker_index (void)
4636{
4637 return vcl_get_worker_index ();
4638}
4639
Florin Corase0982e52019-01-25 13:19:56 -08004640int
4641vppcom_worker_mqs_epfd (void)
4642{
4643 vcl_worker_t *wrk = vcl_worker_get_current ();
4644 if (!vcm->cfg.use_mq_eventfd)
4645 return -1;
4646 return wrk->mqs_epfd;
4647}
4648
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004649int
4650vppcom_session_is_connectable_listener (uint32_t session_handle)
4651{
4652 vcl_session_t *session;
4653 vcl_worker_t *wrk = vcl_worker_get_current ();
4654 session = vcl_session_get_w_handle (wrk, session_handle);
4655 if (!session)
4656 return VPPCOM_EBADFD;
4657 return vcl_session_is_connectable_listener (wrk, session);
4658}
4659
4660int
4661vppcom_session_listener (uint32_t session_handle)
4662{
4663 vcl_worker_t *wrk = vcl_worker_get_current ();
4664 vcl_session_t *listen_session, *session;
4665 session = vcl_session_get_w_handle (wrk, session_handle);
4666 if (!session)
4667 return VPPCOM_EBADFD;
4668 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4669 return VPPCOM_EBADFD;
4670 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4671 if (!listen_session)
4672 return VPPCOM_EBADFD;
4673 return vcl_session_handle (listen_session);
4674}
4675
4676int
4677vppcom_session_n_accepted (uint32_t session_handle)
4678{
4679 vcl_worker_t *wrk = vcl_worker_get_current ();
4680 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4681 if (!session)
4682 return VPPCOM_EBADFD;
4683 return session->n_accepted_sessions;
4684}
4685
Florin Coras66ec4672020-06-15 07:59:40 -07004686const char *
4687vppcom_proto_str (vppcom_proto_t proto)
4688{
4689 char const *proto_str;
4690
4691 switch (proto)
4692 {
4693 case VPPCOM_PROTO_TCP:
4694 proto_str = "TCP";
4695 break;
4696 case VPPCOM_PROTO_UDP:
4697 proto_str = "UDP";
4698 break;
4699 case VPPCOM_PROTO_TLS:
4700 proto_str = "TLS";
4701 break;
4702 case VPPCOM_PROTO_QUIC:
4703 proto_str = "QUIC";
4704 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004705 case VPPCOM_PROTO_DTLS:
4706 proto_str = "DTLS";
4707 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004708 case VPPCOM_PROTO_SRTP:
4709 proto_str = "SRTP";
4710 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004711 default:
4712 proto_str = "UNKNOWN";
4713 break;
4714 }
4715 return proto_str;
4716}
4717
4718const char *
4719vppcom_retval_str (int retval)
4720{
4721 char const *st;
4722
4723 switch (retval)
4724 {
4725 case VPPCOM_OK:
4726 st = "VPPCOM_OK";
4727 break;
4728
4729 case VPPCOM_EAGAIN:
4730 st = "VPPCOM_EAGAIN";
4731 break;
4732
4733 case VPPCOM_EFAULT:
4734 st = "VPPCOM_EFAULT";
4735 break;
4736
4737 case VPPCOM_ENOMEM:
4738 st = "VPPCOM_ENOMEM";
4739 break;
4740
4741 case VPPCOM_EINVAL:
4742 st = "VPPCOM_EINVAL";
4743 break;
4744
4745 case VPPCOM_EBADFD:
4746 st = "VPPCOM_EBADFD";
4747 break;
4748
4749 case VPPCOM_EAFNOSUPPORT:
4750 st = "VPPCOM_EAFNOSUPPORT";
4751 break;
4752
4753 case VPPCOM_ECONNABORTED:
4754 st = "VPPCOM_ECONNABORTED";
4755 break;
4756
4757 case VPPCOM_ECONNRESET:
4758 st = "VPPCOM_ECONNRESET";
4759 break;
4760
4761 case VPPCOM_ENOTCONN:
4762 st = "VPPCOM_ENOTCONN";
4763 break;
4764
4765 case VPPCOM_ECONNREFUSED:
4766 st = "VPPCOM_ECONNREFUSED";
4767 break;
4768
4769 case VPPCOM_ETIMEDOUT:
4770 st = "VPPCOM_ETIMEDOUT";
4771 break;
4772
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304773 case VPPCOM_EADDRINUSE:
4774 st = "VPPCOM_EADDRINUSE";
4775 break;
4776
Florin Coras66ec4672020-06-15 07:59:40 -07004777 default:
4778 st = "UNKNOWN_STATE";
4779 break;
4780 }
4781
4782 return st;
4783}
4784
Florin Corasa5a9efd2021-01-05 17:03:29 -08004785int
4786vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4787{
4788 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004789 return vcl_sapi_add_cert_key_pair (ckpair);
4790 else
4791 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004792}
4793
4794int
4795vppcom_del_cert_key_pair (uint32_t ckpair_index)
4796{
4797 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004798 return vcl_sapi_del_cert_key_pair (ckpair_index);
4799 else
4800 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004801}
4802
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304803int
4804vppcom_session_get_error (uint32_t session_handle)
4805{
4806 vcl_worker_t *wrk = vcl_worker_get_current ();
4807 vcl_session_t *session = 0;
4808
4809 session = vcl_session_get_w_handle (wrk, session_handle);
4810 if (!session)
4811 return VPPCOM_EBADFD;
4812
4813 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4814 {
4815 VWRN ("epoll session %u! will not have connect", session->session_index);
4816 return VPPCOM_EBADFD;
4817 }
4818
4819 if (session->vpp_error == SESSION_E_PORTINUSE)
4820 return VPPCOM_EADDRINUSE;
4821 else if (session->vpp_error == SESSION_E_REFUSED)
4822 return VPPCOM_ECONNREFUSED;
4823 else if (session->vpp_error != SESSION_E_NONE)
4824 return VPPCOM_EFAULT;
4825 else
4826 return VPPCOM_OK;
4827}
4828
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004829int
4830vppcom_worker_is_detached (void)
4831{
4832 vcl_worker_t *wrk = vcl_worker_get_current ();
4833
4834 if (!vcm->cfg.use_mq_eventfd)
4835 return VPPCOM_ENOTSUP;
4836
4837 return wrk->api_client_handle == ~0;
4838}
4839
Dave Wallacee22aa742017-10-20 12:30:38 -04004840/*
4841 * fd.io coding-style-patch-verification: ON
4842 *
4843 * Local Variables:
4844 * eval: (c-set-style "gnu")
4845 * End:
4846 */