blob: e929fa1b6e30c26abc5e339c96ebe14c4f9bbf46 [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;
548 return sid;
549 }
550 else
551 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800552 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
553 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700554 return VCL_INVALID_SESSION_INDEX;
555 }
556 }
557
558 session->vpp_handle = mp->handle;
559 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500560 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
561 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700562 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700563 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700564 session->session_state = VCL_STATE_LISTEN;
Florin Coras32881932023-02-06 13:30:13 -0800565 session->flags &= ~VCL_SESSION_F_PENDING_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800566
Florin Coras6e3c1f82020-01-15 01:30:46 +0000567 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700568 {
Florin Corasc547e912020-12-08 17:50:45 -0800569 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700570 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
571 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800572 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800573 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
574 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800575 session->session_state = VCL_STATE_DETACHED;
576 return VCL_INVALID_SESSION_INDEX;
577 }
Florin Coras60116992018-08-27 09:52:18 -0700578 }
579
Florin Coras05ecfcc2018-12-12 18:19:39 -0800580 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700581 return sid;
582}
583
Florin Corasdfae9f92019-02-20 19:48:31 -0800584static void
585vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
586{
587 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
588 vcl_session_t *s;
589
590 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000591 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800592 {
593 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
594 return;
595 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700596 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000597 {
598 /* Connected udp listener */
599 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700600 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000601 return;
602
603 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
604 return;
605 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800606
607 if (mp->retval)
608 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700609 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800610
611 if (mp->context != wrk->wrk_index)
612 VDBG (0, "wrong context");
613
614 vcl_session_table_del_vpp_handle (wrk, mp->handle);
615 vcl_session_free (wrk, s);
616}
617
Florin Coras68b7e582020-01-21 18:33:23 -0800618static void
619vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
620{
621 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
622 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800623 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800624
625 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
626 if (!s)
627 {
628 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
629 return;
630 }
631
Florin Coras1bb74942021-02-10 15:26:37 -0800632 /* Only validate if a value is provided */
633 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800634 {
Florin Coras1bb74942021-02-10 15:26:37 -0800635 fs_index = vcl_segment_table_lookup (mp->segment_handle);
636 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
637 {
638 VDBG (0, "segment %lx for session %u is not mounted!",
639 mp->segment_handle, s->session_index);
640 s->session_state = VCL_STATE_DETACHED;
641 return;
642 }
Florin Corasc547e912020-12-08 17:50:45 -0800643 }
644
Florin Coras57660d92020-04-04 22:45:34 +0000645 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800646
647 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
648 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800649
Florin Coras68b7e582020-01-21 18:33:23 -0800650 vcl_session_table_del_vpp_handle (wrk, mp->handle);
651 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
652
653 /* Generate new tx event if we have outstanding data */
654 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800655 app_send_io_evt_to_vpp (s->vpp_evt_q,
656 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800657 SESSION_IO_EVT_TX, SVM_Q_WAIT);
658
Florin Coras57660d92020-04-04 22:45:34 +0000659 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800660 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800661}
662
Florin Coras3c7d4f92018-12-14 11:28:43 -0800663static vcl_session_t *
664vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
665{
666 vcl_session_msg_t *vcl_msg;
667 vcl_session_t *session;
668
669 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
670 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800671 VWRN ("session overlap handle %lu state %u!", msg->handle,
672 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800673
674 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
675 if (!session)
676 {
677 VERR ("couldn't find listen session: listener handle %llx",
678 msg->listener_handle);
679 return 0;
680 }
681
682 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000683 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800684 vcl_msg->accepted_msg = *msg;
685 /* Session handle points to listener until fully accepted by app */
686 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
687
688 return session;
689}
690
691static vcl_session_t *
692vcl_session_disconnected_handler (vcl_worker_t * wrk,
693 session_disconnected_msg_t * msg)
694{
695 vcl_session_t *session;
696
697 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
698 if (!session)
699 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800700 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800701 return 0;
702 }
703
Florin Corasadcfb152020-02-12 08:50:29 +0000704 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700705 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000706 return 0;
707
Florin Coras3c7d4f92018-12-14 11:28:43 -0800708 /* Caught a disconnect before actually accepting the session */
wanghanlin696db202023-08-07 17:23:53 +0800709 if (session->session_state == VCL_STATE_LISTEN ||
710 session->session_state == VCL_STATE_LISTEN_NO_MQ)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800711 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800712 if (!vcl_flag_accepted_session (session, msg->handle,
713 VCL_ACCEPTED_F_CLOSED))
714 VDBG (0, "session was not accepted!");
715 return 0;
716 }
717
Florin Corasadcfb152020-02-12 08:50:29 +0000718 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700719 if (session->session_state != VCL_STATE_DISCONNECT)
720 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000721
Florin Coras3c7d4f92018-12-14 11:28:43 -0800722 return session;
723}
724
liuyacan534468e2021-05-09 03:50:40 +0000725int
liuyacan55c952e2021-06-13 14:54:55 +0800726vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000727{
728 vcl_worker_t *wrk = vcl_worker_get_current ();
729 vcl_session_t *session;
730 vcl_session_state_t state;
731 u64 vpp_handle;
732
733 session = vcl_session_get_w_handle (wrk, session_handle);
734 if (PREDICT_FALSE (!session))
735 return VPPCOM_EBADFD;
736
737 vpp_handle = session->vpp_handle;
738 state = session->session_state;
739
740 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800741 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000742
743 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
744 {
745 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
746 return VPPCOM_EBADFD;
747 }
748
liuyacan55c952e2021-06-13 14:54:55 +0800749 if (how == SHUT_RD || how == SHUT_RDWR)
750 {
751 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
752 if (how == SHUT_RD)
753 return VPPCOM_OK;
754 }
755 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
756
liuyacan534468e2021-05-09 03:50:40 +0000757 if (PREDICT_TRUE (state == VCL_STATE_READY))
758 {
759 VDBG (1, "session %u [0x%llx]: sending shutdown...",
760 session->session_index, vpp_handle);
761
762 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000763 }
764
765 return VPPCOM_OK;
766}
767
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700768static int
769vppcom_session_disconnect (u32 session_handle)
770{
771 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700772 vcl_session_t *session, *listen_session;
773 vcl_session_state_t state;
774 u64 vpp_handle;
775
776 session = vcl_session_get_w_handle (wrk, session_handle);
777 if (!session)
778 return VPPCOM_EBADFD;
779
780 vpp_handle = session->vpp_handle;
781 state = session->session_state;
782
Florin Coras5ffb9642021-12-03 17:24:13 -0800783 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
784 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700785
786 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
787 {
788 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
789 return VPPCOM_EBADFD;
790 }
791
792 if (state == VCL_STATE_VPP_CLOSING)
793 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800794 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700795 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
796 session->session_index, vpp_handle);
797 }
798 else
799 {
800 /* Session doesn't have an event queue yet. Probably a non-blocking
801 * connect. Wait for the reply */
802 if (PREDICT_FALSE (!session->vpp_evt_q))
803 return VPPCOM_OK;
804
Florin Coras5ffb9642021-12-03 17:24:13 -0800805 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700806 session->session_index, vpp_handle);
807 vcl_send_session_disconnect (wrk, session);
808 }
809
810 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
811 {
812 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700813 if (listen_session)
814 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700815 }
816
817 return VPPCOM_OK;
818}
819
Florin Coras30e79c22019-01-02 19:31:22 -0800820static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700821vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
822{
823 session_cleanup_msg_t *msg;
824 vcl_session_t *session;
825
826 msg = (session_cleanup_msg_t *) data;
827 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
828 if (!session)
829 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800830 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700831 return;
832 }
833
Florin Coras36d49392020-04-24 23:00:11 +0000834 if (msg->type == SESSION_CLEANUP_TRANSPORT)
835 {
836 /* Transport was cleaned up before we confirmed close. Probably the
837 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700838 * Confirm close to make sure everything is cleaned up.
839 * Move to undetermined state to ensure that the session is not
840 * removed before both vpp and the app cleanup.
841 * - If the app closes first, the session is moved to CLOSED state
842 * and the session cleanup notification from vpp removes the
843 * session.
844 * - If vpp cleans up the session first, the session is moved to
845 * DETACHED state lower and subsequently the close from the app
846 * frees the session
847 */
848 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700849 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700850 vppcom_session_disconnect (vcl_session_handle (session));
851 session->session_state = VCL_STATE_UPDATED;
852 }
853 else if (session->session_state == VCL_STATE_DISCONNECT)
854 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800855 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700856 session->session_state = VCL_STATE_UPDATED;
857 }
Florin Coras36d49392020-04-24 23:00:11 +0000858 return;
859 }
860
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800861 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700862 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800863
864 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700865 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000866 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800867 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700868 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000869 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
870 return;
871 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800872
873 /* Session probably tracked with epoll, disconnect not yet handled and
874 * 1) both transport and session cleanup completed 2) app closed. Wait
875 * until message is drained to free the session.
876 * See @ref vcl_handle_mq_event */
877 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
878 {
879 session->flags |= VCL_SESSION_F_PENDING_FREE;
880 return;
881 }
882
Florin Coras9ace36d2019-10-28 13:14:17 -0700883 vcl_session_free (wrk, session);
884}
885
886static void
Florin Coras30e79c22019-01-02 19:31:22 -0800887vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
888{
889 session_req_worker_update_msg_t *msg;
890 vcl_session_t *s;
891
892 msg = (session_req_worker_update_msg_t *) data;
893 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
894 if (!s)
895 return;
896
897 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
898}
899
900static void
901vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
902{
903 session_worker_update_reply_msg_t *msg;
904 vcl_session_t *s;
905
906 msg = (session_worker_update_reply_msg_t *) data;
907 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
908 if (!s)
909 {
910 VDBG (0, "unknown handle 0x%llx", msg->handle);
911 return;
912 }
Florin Coras30e79c22019-01-02 19:31:22 -0800913
Florin Coras5f45e012019-01-23 09:21:30 -0800914 if (s->rx_fifo)
915 {
Florin Corasc547e912020-12-08 17:50:45 -0800916 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700917 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800918 {
919 VDBG (0, "failed to attach fifos for %u", s->session_index);
920 return;
921 }
Florin Coras5f45e012019-01-23 09:21:30 -0800922 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700923 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800924
Florin Coras30e79c22019-01-02 19:31:22 -0800925 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
926 s->vpp_handle, wrk->wrk_index);
927}
928
Florin Coras935ce752020-09-08 22:43:47 -0700929static int
930vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
931{
932
933 if (vcm->cfg.vpp_app_socket_api)
934 return vcl_sapi_recv_fds (wrk, fds, n_fds);
935
936 return vcl_bapi_recv_fds (wrk, fds, n_fds);
937}
938
Florin Corasc4c4cf52019-08-24 18:17:34 -0700939static void
940vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
941{
942 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
943 session_app_add_segment_msg_t *msg;
944 u64 segment_handle;
945 int fd = -1;
946
947 msg = (session_app_add_segment_msg_t *) data;
948
949 if (msg->fd_flags)
950 {
Florin Coras935ce752020-09-08 22:43:47 -0700951 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700952 seg_type = SSVM_SEGMENT_MEMFD;
953 }
954
955 segment_handle = msg->segment_handle;
956 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
957 {
958 clib_warning ("invalid segment handle");
959 return;
960 }
961
962 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
963 seg_type, fd))
964 {
965 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
966 return;
967 }
968
969 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
970 msg->segment_size);
971}
972
973static void
974vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
975{
976 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
977 vcl_segment_detach (msg->segment_handle);
Florin Coras1f40ab42023-06-13 17:18:56 -0700978 VDBG (1, "Unmapped segment: %lx", msg->segment_handle);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700979}
980
Florin Coras40c07ce2020-07-16 20:46:17 -0700981static void
982vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
983{
984 if (!vcm->wrk_rpc_fn)
985 return;
986
hanlina3a48962020-07-13 11:09:15 +0800987 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700988}
989
Florin Coras04ae8272021-04-12 19:55:37 -0700990static void
991vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
992{
993 session_transport_attr_reply_msg_t *mp;
994
995 if (!wrk->session_attr_op)
996 return;
997
998 mp = (session_transport_attr_reply_msg_t *) data;
999
1000 wrk->session_attr_op_rv = mp->retval;
1001 wrk->session_attr_op = 0;
1002 wrk->session_attr_rv = mp->attr;
1003}
1004
Florin Coras86f04502018-09-12 16:08:01 -07001005static int
1006vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001007{
Florin Coras54693d22018-07-17 10:46:29 -07001008 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001009 session_connected_msg_t *connected_msg;
1010 session_reset_msg_t *reset_msg;
1011 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001012 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001013 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001014
1015 switch (e->event_type)
1016 {
Florin Coras653e43f2019-03-04 10:56:23 -08001017 case SESSION_IO_EVT_RX:
1018 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001019 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001020 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001021 break;
Florin Coras86f04502018-09-12 16:08:01 -07001022 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001023 break;
Florin Corasb242d312020-10-26 15:35:40 -07001024 case SESSION_CTRL_EVT_BOUND:
1025 /* We can only wait for only one listen so not postponed */
1026 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1027 break;
Florin Coras54693d22018-07-17 10:46:29 -07001028 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001029 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1030 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1031 {
1032 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1033 *ecpy = *e;
1034 ecpy->postponed = 1;
1035 ecpy->session_index = s->session_index;
1036 }
Florin Coras54693d22018-07-17 10:46:29 -07001037 break;
1038 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001039 connected_msg = (session_connected_msg_t *) e->data;
1040 sid = vcl_session_connected_handler (wrk, connected_msg);
1041 if (!(s = vcl_session_get (wrk, sid)))
1042 break;
1043 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1044 {
1045 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1046 *ecpy = *e;
1047 ecpy->postponed = 1;
1048 ecpy->session_index = s->session_index;
1049 }
Florin Coras54693d22018-07-17 10:46:29 -07001050 break;
1051 case SESSION_CTRL_EVT_DISCONNECTED:
1052 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001053 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1054 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001055 if (s->session_state == VCL_STATE_CLOSED)
1056 break;
wanghanlin4747b342023-08-08 10:40:04 +08001057 /* We do not postpone for blocking sessions or listen sessions because:
1058 * 1. Blocking sessions are not part of epoll instead they're used in a
1059 * synchronous manner, such as read/write and etc.
1060 * 2. Listen sessions that have not yet been accepted can't change to
1061 * VPP_CLOSING state instead can been marked as ACCEPTED_F_CLOSED.
1062 */
1063 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK) &&
1064 !(s->session_state == VCL_STATE_LISTEN ||
1065 s->session_state == VCL_STATE_LISTEN_NO_MQ))
Florin Corasb242d312020-10-26 15:35:40 -07001066 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001067 s->session_state = VCL_STATE_VPP_CLOSING;
1068 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1069 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1070 *ecpy = *e;
1071 ecpy->postponed = 1;
1072 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001073 break;
1074 }
1075 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001076 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001077 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1078 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001079 break;
1080 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001081 reset_msg = (session_reset_msg_t *) e->data;
1082 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1083 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001084 if (s->session_state == VCL_STATE_CLOSED)
1085 break;
wanghanlin4747b342023-08-08 10:40:04 +08001086 /* We do not postpone for blocking sessions or listen sessions because:
1087 * 1. Blocking sessions are not part of epoll instead they're used in a
1088 * synchronous manner, such as read/write and etc.
1089 * 2. Listen sessions that have not yet been accepted can't change to
1090 * DISCONNECT state instead can been marked as ACCEPTED_F_RESET.
1091 */
1092 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK) &&
1093 !(s->session_state == VCL_STATE_LISTEN ||
1094 s->session_state == VCL_STATE_LISTEN_NO_MQ))
Florin Corasb242d312020-10-26 15:35:40 -07001095 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001096 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1097 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001098 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001099 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1100 *ecpy = *e;
1101 ecpy->postponed = 1;
1102 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001103 break;
1104 }
Florin Coras134a9962018-08-28 11:32:04 -07001105 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001106 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001107 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1108 vcl_session_unlisten_reply_handler (wrk, e->data);
1109 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001110 case SESSION_CTRL_EVT_MIGRATED:
1111 vcl_session_migrated_handler (wrk, e->data);
1112 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001113 case SESSION_CTRL_EVT_CLEANUP:
1114 vcl_session_cleanup_handler (wrk, e->data);
1115 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001116 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1117 vcl_session_req_worker_update_handler (wrk, e->data);
1118 break;
1119 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1120 vcl_session_worker_update_reply_handler (wrk, e->data);
1121 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001122 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1123 vcl_session_app_add_segment_handler (wrk, e->data);
1124 break;
1125 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1126 vcl_session_app_del_segment_handler (wrk, e->data);
1127 break;
hanlina3a48962020-07-13 11:09:15 +08001128 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001129 vcl_worker_rpc_handler (wrk, e->data);
1130 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001131 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1132 vcl_session_transport_attr_reply_handler (wrk, e->data);
1133 break;
Florin Coras54693d22018-07-17 10:46:29 -07001134 default:
1135 clib_warning ("unhandled %u", e->event_type);
1136 }
1137 return VPPCOM_OK;
1138}
1139
Florin Coras30e79c22019-01-02 19:31:22 -08001140static int
Florin Coras697faea2018-06-27 17:10:49 -07001141vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001142 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001143 f64 wait_for_time)
1144{
Florin Coras134a9962018-08-28 11:32:04 -07001145 vcl_worker_t *wrk = vcl_worker_get_current ();
1146 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001147 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001148 svm_msg_q_msg_t msg;
1149 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001150
Florin Coras697faea2018-06-27 17:10:49 -07001151 do
Dave Wallace543852a2017-08-03 02:11:34 -04001152 {
Florin Coras134a9962018-08-28 11:32:04 -07001153 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001154 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001155 {
Florin Coras070453d2018-08-24 17:04:27 -07001156 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001157 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001158 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001159 {
Florin Coras697faea2018-06-27 17:10:49 -07001160 return VPPCOM_OK;
1161 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001162 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001163 {
Florin Coras697faea2018-06-27 17:10:49 -07001164 return VPPCOM_ECONNREFUSED;
1165 }
Florin Coras54693d22018-07-17 10:46:29 -07001166
Florin Coras134a9962018-08-28 11:32:04 -07001167 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001168 {
1169 usleep (100);
1170 continue;
1171 }
Florin Coras134a9962018-08-28 11:32:04 -07001172 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001173 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001174 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001175 }
Florin Coras134a9962018-08-28 11:32:04 -07001176 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001177
Florin Coras05ecfcc2018-12-12 18:19:39 -08001178 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001179 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001180 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001181
Florin Coras697faea2018-06-27 17:10:49 -07001182 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001183}
1184
Florin Coras30e79c22019-01-02 19:31:22 -08001185static void
1186vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1187{
Florin Coras288eaab2019-02-03 15:26:14 -08001188 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001189 vcl_session_t *s;
1190 u32 *sip;
1191
1192 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1193 return;
1194
1195 vec_foreach (sip, wrk->pending_session_wrk_updates)
1196 {
1197 s = vcl_session_get (wrk, *sip);
1198 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1199 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001200 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1201 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001202 s->session_state = state;
1203 }
1204 vec_reset_length (wrk->pending_session_wrk_updates);
1205}
1206
Florin Corasf9240dc2019-01-15 08:03:17 -08001207void
Florin Coras5398dfb2021-01-25 20:31:27 -08001208vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001209{
Florin Coras30e79c22019-01-02 19:31:22 -08001210 svm_msg_q_msg_t *msg;
1211 session_event_t *e;
1212 svm_msg_q_t *mq;
1213 int i;
1214
1215 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001216 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001217
1218 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1219 {
1220 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1221 e = svm_msg_q_msg_data (mq, msg);
1222 vcl_handle_mq_event (wrk, e);
1223 svm_msg_q_free_msg (mq, msg);
1224 }
1225 vec_reset_length (wrk->mq_msg_vector);
1226 vcl_handle_pending_wrk_updates (wrk);
1227}
1228
Florin Coras5398dfb2021-01-25 20:31:27 -08001229void
1230vcl_flush_mq_events (void)
1231{
1232 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1233}
1234
Florin Coras697faea2018-06-27 17:10:49 -07001235static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001236vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001237{
Florin Coras134a9962018-08-28 11:32:04 -07001238 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001239 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001240 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001241 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001242
Florin Corasab2f6db2018-08-31 14:31:41 -07001243 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001244 if (!session)
1245 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001246
Florin Corasaaff5ee2019-07-08 13:00:00 -07001247 /* Flush pending accept events, if any */
1248 while (clib_fifo_elts (session->accept_evts_fifo))
1249 {
1250 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1251 accepted_msg = &evt->accepted_msg;
1252 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1253 vcl_send_session_accepted_reply (session->vpp_evt_q,
1254 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001255 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001256 }
1257 clib_fifo_free (session->accept_evts_fifo);
1258
Florin Coras458089b2019-08-21 16:20:44 -07001259 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001260
Florin Coras32881932023-02-06 13:30:13 -08001261 VDBG (0, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001262 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001263 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001264
1265 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001266 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001267
Florin Coras070453d2018-08-24 17:04:27 -07001268 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001269}
1270
Florin Coras940f78f2018-11-30 12:11:20 -08001271/**
1272 * Handle app exit
1273 *
1274 * Notify vpp of the disconnect and mark the worker as free. If we're the
1275 * last worker, do a full cleanup otherwise, since we're probably a forked
1276 * child, avoid syscalls as much as possible. We might've lost privileges.
1277 */
1278void
1279vppcom_app_exit (void)
1280{
1281 if (!pool_elts (vcm->workers))
1282 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001283 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1284 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001285 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001286}
1287
Florin Coras935ce752020-09-08 22:43:47 -07001288static int
1289vcl_api_attach (void)
1290{
1291 if (vcm->cfg.vpp_app_socket_api)
1292 return vcl_sapi_attach ();
1293
1294 return vcl_bapi_attach ();
1295}
1296
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001297int
1298vcl_is_first_reattach_to_execute ()
1299{
1300 if (vcm->reattach_count == 0)
1301 return 1;
1302
1303 return 0;
1304}
1305
1306void
1307vcl_set_reattach_counter ()
1308{
1309 ++vcm->reattach_count;
1310
1311 if (vcm->reattach_count == vec_len (vcm->workers))
1312 vcm->reattach_count = 0;
1313}
1314
1315/**
1316 * Reattach vcl to vpp after it has previously been disconnected.
1317 *
1318 * The logic should be:
1319 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1320 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1321 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1322 * from `vppcom_worker_register` since they were already allocated.
1323 */
1324
Florin Coras935ce752020-09-08 22:43:47 -07001325static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001326vcl_api_retry_attach (vcl_worker_t *wrk)
1327{
1328 vcl_session_t *s;
1329
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001330 clib_spinlock_lock (&vcm->workers_lock);
1331 if (vcl_is_first_reattach_to_execute ())
1332 {
1333 if (vcl_api_attach ())
1334 {
1335 clib_spinlock_unlock (&vcm->workers_lock);
1336 return;
1337 }
1338 vcl_set_reattach_counter ();
1339 clib_spinlock_unlock (&vcm->workers_lock);
1340 }
1341 else
1342 {
1343 vcl_set_reattach_counter ();
1344 clib_spinlock_unlock (&vcm->workers_lock);
1345 vcl_worker_register_with_vpp ();
1346 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001347
1348 /* Treat listeners as configuration that needs to be re-added to vpp */
1349 pool_foreach (s, wrk->sessions)
1350 {
1351 if (s->flags & VCL_SESSION_F_IS_VEP)
1352 continue;
1353 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1354 vppcom_session_listen (vcl_session_handle (s), 10);
1355 else
1356 VDBG (0, "internal error: unexpected state %d", s->session_state);
1357 }
1358}
1359
1360static void
1361vcl_api_handle_disconnect (vcl_worker_t *wrk)
1362{
1363 wrk->api_client_handle = ~0;
1364 vcl_worker_detach_sessions (wrk);
1365}
1366
1367static void
Florin Coras935ce752020-09-08 22:43:47 -07001368vcl_api_detach (vcl_worker_t * wrk)
1369{
Florin Corasd04ea442022-03-30 16:08:25 -07001370 if (wrk->api_client_handle == ~0)
1371 return;
1372
Florin Coras935ce752020-09-08 22:43:47 -07001373 vcl_send_app_detach (wrk);
1374
1375 if (vcm->cfg.vpp_app_socket_api)
1376 return vcl_sapi_detach (wrk);
1377
1378 return vcl_bapi_disconnect_from_vpp ();
1379}
1380
Dave Wallace543852a2017-08-03 02:11:34 -04001381/*
1382 * VPPCOM Public API functions
1383 */
1384int
Florin Coras66ec4672020-06-15 07:59:40 -07001385vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001386{
Dave Wallace543852a2017-08-03 02:11:34 -04001387 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001388 int rv;
1389
Florin Coras47c40e22018-11-26 17:01:36 -08001390 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001391 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001392 VDBG (1, "already initialized");
1393 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001394 }
1395
Florin Coras47c40e22018-11-26 17:01:36 -08001396 vcm->is_init = 1;
1397 vppcom_cfg (&vcm->cfg);
1398 vcl_cfg = &vcm->cfg;
1399
1400 vcm->main_cpu = pthread_self ();
1401 vcm->main_pid = getpid ();
1402 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001403 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1404 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001405 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1406 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001407 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001408 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001409 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001410
1411 /* Allocate default worker */
1412 vcl_worker_alloc_and_init ();
1413
Florin Coras935ce752020-09-08 22:43:47 -07001414 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001415 {
1416 vppcom_app_destroy ();
1417 return rv;
1418 }
Florin Coras47c40e22018-11-26 17:01:36 -08001419
1420 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001421 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001422
1423 return VPPCOM_OK;
1424}
1425
1426void
1427vppcom_app_destroy (void)
1428{
Florin Corasdc0ded72020-04-30 02:59:55 +00001429 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001430 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001431
Florin Coras940f78f2018-11-30 12:11:20 -08001432 if (!pool_elts (vcm->workers))
1433 return;
1434
Florin Coras0d427d82018-06-27 03:24:07 -07001435 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001436
Florin Corasdc0ded72020-04-30 02:59:55 +00001437 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001438
Florin Corasce815de2020-04-16 18:47:27 +00001439 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001440 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001441 if (current_wrk != wrk)
1442 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001443 }
Florin Corasce815de2020-04-16 18:47:27 +00001444 /* *INDENT-ON* */
1445
Florin Coras935ce752020-09-08 22:43:47 -07001446 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001447 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001448 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001449
Florin Coras0d427d82018-06-27 03:24:07 -07001450 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001451
1452 /*
1453 * Free the heap and fix vcm
1454 */
1455 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001456 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001457
1458 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001459 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001460}
1461
1462int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001463vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001464{
Florin Coras134a9962018-08-28 11:32:04 -07001465 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001466 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001467
Florin Coras134a9962018-08-28 11:32:04 -07001468 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001469
Florin Coras7e12d942018-06-27 14:32:43 -07001470 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001471 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001472 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001473 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301474 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001475
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001476 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001477 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001478
Florin Coras7e12d942018-06-27 14:32:43 -07001479 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1480 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001481
Florin Coras5e062572019-03-14 19:07:51 -07001482 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001483
Florin Coras134a9962018-08-28 11:32:04 -07001484 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001485}
1486
Florin Corasfe286f72021-06-04 10:07:55 -07001487static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001488vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001489{
Florin Corasfa3884f2021-06-22 20:04:46 -07001490 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001491
Florin Corasc2a14172023-02-28 21:13:50 -08001492 ASSERT (s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
1493
Florin Corasfa3884f2021-06-22 20:04:46 -07001494 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001495 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001496 wrk->ep_lt_current = s->session_index;
1497 s->vep.lt_next = s->session_index;
1498 s->vep.lt_prev = s->session_index;
1499 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001500 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001501
1502 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1503 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1504
1505 prev->vep.lt_next = s->session_index;
1506 s->vep.lt_prev = prev->session_index;
1507
1508 s->vep.lt_next = cur->session_index;
1509 cur->vep.lt_prev = s->session_index;
1510}
1511
1512static void
1513vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1514{
1515 vcl_session_t *prev, *next;
1516
Florin Corasc2a14172023-02-28 21:13:50 -08001517 ASSERT (s->vep.lt_next != VCL_INVALID_SESSION_INDEX);
1518
Florin Corasfa3884f2021-06-22 20:04:46 -07001519 if (s->vep.lt_next == s->session_index)
1520 {
1521 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1522 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001523 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfa3884f2021-06-22 20:04:46 -07001524 return;
1525 }
1526
1527 prev = vcl_session_get (wrk, s->vep.lt_prev);
1528 next = vcl_session_get (wrk, s->vep.lt_next);
1529
1530 prev->vep.lt_next = next->session_index;
1531 next->vep.lt_prev = prev->session_index;
1532
1533 if (s->session_index == wrk->ep_lt_current)
1534 wrk->ep_lt_current = s->vep.lt_next;
1535
1536 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001537 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001538}
1539
Dave Wallace543852a2017-08-03 02:11:34 -04001540int
Florin Corasc127d5a2020-10-14 16:35:58 -07001541vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001542 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001543{
Florin Coras134a9962018-08-28 11:32:04 -07001544 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001545
Florin Coras6c3b2182020-10-19 18:36:48 -07001546 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001547
Florin Coras6c3b2182020-10-19 18:36:48 -07001548 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001549 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001550 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001551 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001552 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001553 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001554 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001555 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001556 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1557 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001558 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001559 }
Florin Corase4306b62020-10-28 12:51:10 -07001560 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001561 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001562
Florin Coras6c3b2182020-10-19 18:36:48 -07001563 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001564 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001565 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001566 if (rv < 0)
1567 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001568 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1569 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001570 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001571
Florin Coras9ace36d2019-10-28 13:14:17 -07001572 if (!do_disconnect)
1573 {
1574 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001575 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001576 goto cleanup;
1577 }
1578
Florin Coras6c3b2182020-10-19 18:36:48 -07001579 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001580 {
1581 rv = vppcom_session_unbind (sh);
1582 if (PREDICT_FALSE (rv < 0))
1583 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001584 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001585 vppcom_retval_str (rv));
1586 return rv;
1587 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001588 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001589 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001590 {
1591 rv = vppcom_session_disconnect (sh);
1592 if (PREDICT_FALSE (rv < 0))
1593 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001594 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001595 rv, vppcom_retval_str (rv));
1596 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001597 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001598 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001599 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001600 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001601 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001602 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001603 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001604
1605 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1606 goto free_session;
1607
1608 /* Disconnect/reset messages pending but vpp transport and session
1609 * cleanups already done. Free only after messages drained. */
1610 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001611 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001612
Florin Corasc127d5a2020-10-14 16:35:58 -07001613 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001614
Florin Coras9ace36d2019-10-28 13:14:17 -07001615 /* Session is removed only after vpp confirms the disconnect */
1616 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001617
Florin Corasf9240dc2019-01-15 08:03:17 -08001618cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001619 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001620free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001621 vcl_session_free (wrk, s);
1622 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001623
Dave Wallace543852a2017-08-03 02:11:34 -04001624 return rv;
1625}
1626
1627int
Florin Corasf9240dc2019-01-15 08:03:17 -08001628vppcom_session_close (uint32_t session_handle)
1629{
1630 vcl_worker_t *wrk = vcl_worker_get_current ();
1631 vcl_session_t *session;
1632
1633 session = vcl_session_get_w_handle (wrk, session_handle);
1634 if (!session)
1635 return VPPCOM_EBADFD;
1636 return vcl_session_cleanup (wrk, session, session_handle,
1637 1 /* do_disconnect */ );
1638}
1639
1640int
Florin Coras134a9962018-08-28 11:32:04 -07001641vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001642{
Florin Coras134a9962018-08-28 11:32:04 -07001643 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001644 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001645
1646 if (!ep || !ep->ip)
1647 return VPPCOM_EINVAL;
1648
Florin Coras134a9962018-08-28 11:32:04 -07001649 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001650 if (!session)
1651 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001652
Florin Coras6c3b2182020-10-19 18:36:48 -07001653 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001654 {
Florin Coras5e062572019-03-14 19:07:51 -07001655 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1656 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001657 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001658 }
1659
Florin Coras7e12d942018-06-27 14:32:43 -07001660 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001661 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001662 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1663 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001664 else
Dave Barach178cf492018-11-13 16:34:13 -05001665 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1666 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001667 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001668
Florin Coras60687192021-11-29 21:00:47 -08001669 VDBG (0,
1670 "session %u handle %u: binding to local %s address %U port %u, "
1671 "proto %s",
1672 session->session_index, session_handle,
1673 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1674 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001675 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1676 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001677 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001678 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001679
1680 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001681 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001682
Florin Coras070453d2018-08-24 17:04:27 -07001683 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001684}
1685
1686int
Florin Coras134a9962018-08-28 11:32:04 -07001687vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001688{
Florin Coras134a9962018-08-28 11:32:04 -07001689 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001690 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001691 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001692 int rv;
1693
Florin Coras134a9962018-08-28 11:32:04 -07001694 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001695 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001696 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001697
Dave Wallaceee45d412017-11-24 21:44:06 -05001698 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001699 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001700 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001701 VDBG (0, "session %u [0x%llx]: already in listen state!",
1702 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001703 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001704 }
1705
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001706 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001707
Florin Coras070453d2018-08-24 17:04:27 -07001708 /*
1709 * Send listen request to vpp and wait for reply
1710 */
Florin Coras458089b2019-08-21 16:20:44 -07001711 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001712 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001713 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001714 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001715
Florin Coras070453d2018-08-24 17:04:27 -07001716 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001717 {
Florin Coras134a9962018-08-28 11:32:04 -07001718 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001719 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1720 listen_sh, listen_session->vpp_handle, rv,
1721 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001722 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001723 }
1724
Florin Coras070453d2018-08-24 17:04:27 -07001725 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001726}
1727
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001728int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001729vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1730{
1731 if (!strcmp (proto_str, "TCP"))
1732 *proto = VPPCOM_PROTO_TCP;
1733 else if (!strcmp (proto_str, "tcp"))
1734 *proto = VPPCOM_PROTO_TCP;
1735 else if (!strcmp (proto_str, "UDP"))
1736 *proto = VPPCOM_PROTO_UDP;
1737 else if (!strcmp (proto_str, "udp"))
1738 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001739 else if (!strcmp (proto_str, "TLS"))
1740 *proto = VPPCOM_PROTO_TLS;
1741 else if (!strcmp (proto_str, "tls"))
1742 *proto = VPPCOM_PROTO_TLS;
1743 else if (!strcmp (proto_str, "QUIC"))
1744 *proto = VPPCOM_PROTO_QUIC;
1745 else if (!strcmp (proto_str, "quic"))
1746 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001747 else if (!strcmp (proto_str, "DTLS"))
1748 *proto = VPPCOM_PROTO_DTLS;
1749 else if (!strcmp (proto_str, "dtls"))
1750 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001751 else if (!strcmp (proto_str, "SRTP"))
1752 *proto = VPPCOM_PROTO_SRTP;
1753 else if (!strcmp (proto_str, "srtp"))
1754 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001755 else
1756 return 1;
1757 return 0;
1758}
1759
1760int
Florin Coras32881932023-02-06 13:30:13 -08001761vppcom_session_accept (uint32_t ls_handle, vppcom_endpt_t *ep, uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001762{
Florin Coras32881932023-02-06 13:30:13 -08001763 u32 client_session_index = ~0, ls_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001764 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001765 session_accepted_msg_t accepted_msg;
Florin Coras32881932023-02-06 13:30:13 -08001766 vcl_session_t *ls, *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001767 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001768 u8 is_nonblocking;
Dave Wallace543852a2017-08-03 02:11:34 -04001769
Florin Coras5398dfb2021-01-25 20:31:27 -08001770again:
1771
Florin Coras32881932023-02-06 13:30:13 -08001772 ls = vcl_session_get_w_handle (wrk, ls_handle);
1773 if (!ls)
Florin Coras070453d2018-08-24 17:04:27 -07001774 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001775
Florin Coras32881932023-02-06 13:30:13 -08001776 if ((ls->session_state != VCL_STATE_LISTEN) &&
1777 (ls->session_state != VCL_STATE_LISTEN_NO_MQ) &&
1778 (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001779 {
Florin Coras32881932023-02-06 13:30:13 -08001780 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state (%s)",
1781 ls->vpp_handle, vcl_session_state_str (ls->session_state));
1782 return VPPCOM_EBADFD;
1783 }
1784
1785 ls_index = ls->session_index;
1786
1787 if (clib_fifo_elts (ls->accept_evts_fifo))
1788 {
1789 clib_fifo_sub2 (ls->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001790 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001791 accepted_msg = evt->accepted_msg;
1792 goto handle;
1793 }
1794
Florin Coras32881932023-02-06 13:30:13 -08001795 is_nonblocking = vcl_session_has_attr (ls, VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001796 while (1)
1797 {
Carl Smith592a9092019-11-12 14:57:37 +13001798 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1799 return VPPCOM_EAGAIN;
1800
Florin Coras5398dfb2021-01-25 20:31:27 -08001801 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1802 vcl_worker_flush_mq_events (wrk);
1803 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001804 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001805
Florin Coras54693d22018-07-17 10:46:29 -07001806handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001807
Florin Coras32881932023-02-06 13:30:13 -08001808 client_session_index =
1809 vcl_session_accepted_handler (wrk, &accepted_msg, ls_index);
Florin Coras00cca802019-06-06 09:38:44 -07001810 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1811 return VPPCOM_ECONNABORTED;
1812
Florin Coras32881932023-02-06 13:30:13 -08001813 ls = vcl_session_get (wrk, ls_index);
Florin Coras134a9962018-08-28 11:32:04 -07001814 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001815
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001816 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001817 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001818
Florin Coras32881932023-02-06 13:30:13 -08001819 VDBG (1,
1820 "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1821 " flags %d, is_nonblocking %u",
1822 ls->session_index, ls->vpp_handle, client_session_index,
Florin Coras5e062572019-03-14 19:07:51 -07001823 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001824 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001825
Dave Wallace048b1d62018-01-03 22:24:41 -05001826 if (ep)
1827 {
Florin Coras7e12d942018-06-27 14:32:43 -07001828 ep->is_ip4 = client_session->transport.is_ip4;
1829 ep->port = client_session->transport.rmt_port;
1830 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001831 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1832 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001833 else
Dave Barach178cf492018-11-13 16:34:13 -05001834 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1835 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001836 }
Dave Wallace60caa062017-11-10 17:07:13 -05001837
Florin Coras60687192021-11-29 21:00:47 -08001838 VDBG (0,
1839 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1840 "local: %U:%u",
Florin Coras32881932023-02-06 13:30:13 -08001841 ls_handle, ls->vpp_handle, client_session_index,
1842 client_session->vpp_handle, vcl_format_ip46_address,
1843 &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001844 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001845 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001846 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001847 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001848 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras32881932023-02-06 13:30:13 -08001849 vcl_evt (VCL_EVT_ACCEPT, client_session, ls, client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001850
Florin Coras3c7d4f92018-12-14 11:28:43 -08001851 /*
1852 * Session might have been closed already
1853 */
1854 if (accept_flags)
1855 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001856 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001857 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001858 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001859 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001860 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001861 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001862}
1863
1864int
Florin Coras134a9962018-08-28 11:32:04 -07001865vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001866{
Florin Coras134a9962018-08-28 11:32:04 -07001867 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001868 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001869 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001870 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001871
Florin Coras134a9962018-08-28 11:32:04 -07001872 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001873 if (!session)
1874 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001875 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001876
Florin Coras6c3b2182020-10-19 18:36:48 -07001877 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001878 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001879 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001880 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001881 }
1882
Florin Corasc127d5a2020-10-14 16:35:58 -07001883 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001884 {
Florin Coras60687192021-11-29 21:00:47 -08001885 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001886 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1887 " state (%s)",
1888 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001889 vcl_format_ip46_address, &session->transport.rmt_ip,
1890 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001891 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001892 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001893 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001894 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001895 }
1896
Florin Coras0a1e1832020-03-29 18:54:04 +00001897 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001898 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001899 {
1900 if (session->session_type != VPPCOM_PROTO_UDP)
1901 return VPPCOM_EINVAL;
1902 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001903 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001904 }
1905
Florin Coras7e12d942018-06-27 14:32:43 -07001906 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001907 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001908 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001909 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001910 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001911
Florin Coras5ffb9642021-12-03 17:24:13 -08001912 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1913 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001914 &session->transport.rmt_ip,
1915 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001916 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001917 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001918
Florin Coras458089b2019-08-21 16:20:44 -07001919 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001920
Florin Corasac422d62020-10-19 20:51:36 -07001921 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001922 {
fanyfa49d59d2020-10-13 17:07:16 +08001923 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001924 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001925 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001926 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001927 return VPPCOM_EINPROGRESS;
1928 }
Florin Coras57c88932019-08-29 12:03:17 -07001929
1930 /*
1931 * Wait for reply from vpp if blocking
1932 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001933 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001934 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001935
Florin Coras134a9962018-08-28 11:32:04 -07001936 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001937 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1938 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001939
Dave Wallace4878cbe2017-11-21 03:45:09 -05001940 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001941}
1942
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001943int
1944vppcom_session_stream_connect (uint32_t session_handle,
1945 uint32_t parent_session_handle)
1946{
1947 vcl_worker_t *wrk = vcl_worker_get_current ();
1948 vcl_session_t *session, *parent_session;
1949 u32 session_index, parent_session_index;
1950 int rv;
1951
1952 session = vcl_session_get_w_handle (wrk, session_handle);
1953 if (!session)
1954 return VPPCOM_EBADFD;
1955 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1956 if (!parent_session)
1957 return VPPCOM_EBADFD;
1958
1959 session_index = session->session_index;
1960 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001961 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001962 {
1963 VDBG (0, "ERROR: cannot connect epoll session %u!",
1964 session->session_index);
1965 return VPPCOM_EBADFD;
1966 }
1967
Florin Corasc127d5a2020-10-14 16:35:58 -07001968 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001969 {
Florin Coras60687192021-11-29 21:00:47 -08001970 VDBG (0,
1971 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001972 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001973 session_handle, session->vpp_handle, parent_session_handle,
1974 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001975 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001976 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001977 return VPPCOM_OK;
1978 }
1979
1980 /* Connect to quic session specifics */
1981 session->transport.is_ip4 = parent_session->transport.is_ip4;
1982 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1983 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001984 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001985
1986 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1987 session_handle, parent_session_handle, parent_session->vpp_handle);
1988
1989 /*
1990 * Send connect request and wait for reply from vpp
1991 */
Florin Coras458089b2019-08-21 16:20:44 -07001992 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001993 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001994 vcm->cfg.session_timeout);
1995
1996 session->listener_index = parent_session_index;
1997 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001998 if (parent_session)
1999 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02002000
2001 session = vcl_session_get (wrk, session_index);
2002 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
2003 session->vpp_handle, rv ? "failed" : "succeeded");
2004
2005 return rv;
2006}
2007
Steven58f464e2017-10-25 12:33:12 -07002008static inline int
Florin Coras134a9962018-08-28 11:32:04 -07002009vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07002010 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002011{
Florin Coras134a9962018-08-28 11:32:04 -07002012 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07002013 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002014 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002015 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002016 session_event_t *e;
2017 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07002018 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002019
Florin Coras070453d2018-08-24 17:04:27 -07002020 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08002021 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002022
Florin Coras134a9962018-08-28 11:32:04 -07002023 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002024 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07002025 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002026
Florin Coras6d0106e2019-01-29 20:11:58 -08002027 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002028 {
Florin Coras5e062572019-03-14 19:07:51 -07002029 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002030 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002031 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002032 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002033 }
2034
liuyacan55c952e2021-06-13 14:54:55 +08002035 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2036 {
2037 /* Vpp would ack the incoming data and enqueue it for reading.
2038 * So even if SHUT_RD is set, we can still read() the data if
2039 * the session is ready.
2040 */
2041 if (!vcl_session_read_ready (s))
2042 {
2043 return 0;
2044 }
2045 }
2046
Florin Corasac422d62020-10-19 20:51:36 -07002047 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002048 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002049 mq = wrk->app_event_queue;
2050 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002051 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002052
Sirshak Das28aa5392019-02-05 01:33:33 -06002053 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002054 {
Florin Corasae036d32023-09-18 09:56:21 -07002055 if (is_ct)
2056 svm_fifo_unset_event (s->rx_fifo);
2057 svm_fifo_unset_event (rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002058 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002059 {
Yu Pingb2955352019-12-04 06:49:04 +08002060 if (vcl_session_is_closing (s))
2061 return vcl_session_closing_error (s);
Florin Corasc0737e92019-03-04 14:19:39 -08002062 return VPPCOM_EWOULDBLOCK;
2063 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002064 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002065 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002066 if (vcl_session_is_closing (s))
2067 return vcl_session_closing_error (s);
2068
Florin Corasd6894562020-10-28 00:37:15 -07002069 if (is_ct)
2070 svm_fifo_unset_event (s->rx_fifo);
2071 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002072
Florin Coras5398dfb2021-01-25 20:31:27 -08002073 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2074 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002075 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002076 }
Florin Coras54693d22018-07-17 10:46:29 -07002077
Florin Coras4a2c7942020-09-10 12:27:14 -07002078read_again:
2079
Florin Coras460dce62018-07-27 05:45:06 -07002080 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002081 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002082 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002083 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2084
2085 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002086
2087 if (peek)
2088 return rv;
2089
Florin Coras4a2c7942020-09-10 12:27:14 -07002090 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002091
Sirshak Das28aa5392019-02-05 01:33:33 -06002092 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002093 {
Florin Corasd6894562020-10-28 00:37:15 -07002094 if (is_ct)
2095 svm_fifo_unset_event (s->rx_fifo);
2096 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002097 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002098 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002099 {
Florin Corasc34118b2020-08-12 18:58:25 -07002100 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2101 e->event_type = SESSION_IO_EVT_RX;
2102 e->session_index = s->session_index;
2103 }
2104 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002105 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002106 {
2107 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002108 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002109 buf += rv;
2110 n -= rv;
2111 goto read_again;
2112 }
Florin Coras41c9e042018-09-11 00:10:41 -07002113
Florin Coras17ec5772020-08-12 20:46:29 -07002114 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002115 {
Florin Coras17ec5772020-08-12 20:46:29 -07002116 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002117 app_send_io_evt_to_vpp (s->vpp_evt_q,
2118 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002119 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002120 }
2121
Florin Coras5e062572019-03-14 19:07:51 -07002122 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2123 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002124
Florin Coras54693d22018-07-17 10:46:29 -07002125 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002126}
2127
Steven58f464e2017-10-25 12:33:12 -07002128int
Florin Coras134a9962018-08-28 11:32:04 -07002129vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002130{
Florin Coras134a9962018-08-28 11:32:04 -07002131 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002132}
2133
2134static int
Florin Coras134a9962018-08-28 11:32:04 -07002135vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002136{
Florin Coras134a9962018-08-28 11:32:04 -07002137 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002138}
2139
Florin Coras2cba8532018-09-11 16:33:36 -07002140int
2141vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002142 vppcom_data_segment_t * ds, uint32_t n_segments,
2143 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002144{
2145 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002146 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002147 vcl_session_t *s = 0;
2148 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002149 svm_msg_q_t *mq;
2150 u8 is_ct;
2151
2152 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002153 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002154 return VPPCOM_EBADFD;
2155
Florin Coras6d0106e2019-01-29 20:11:58 -08002156 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2157 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002158
Florin Corasac422d62020-10-19 20:51:36 -07002159 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002160 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002161 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002162 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002163 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002164
Sirshak Das28aa5392019-02-05 01:33:33 -06002165 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002166 {
Florin Corasae036d32023-09-18 09:56:21 -07002167 if (is_ct)
2168 svm_fifo_unset_event (s->rx_fifo);
2169 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002170 if (is_nonblocking)
2171 {
Florin Corasae036d32023-09-18 09:56:21 -07002172 if (vcl_session_is_closing (s))
2173 return vcl_session_closing_error (s);
Florin Corasaa27eb92018-10-13 12:20:01 -07002174 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002175 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002176 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002177 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002178 if (vcl_session_is_closing (s))
2179 return vcl_session_closing_error (s);
2180
Florin Coras62877022020-10-28 21:22:04 -07002181 if (is_ct)
2182 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002183 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002184
Florin Coras5398dfb2021-01-25 20:31:27 -08002185 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2186 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002187 }
2188 }
2189
Florin Corasd1cc38d2020-10-11 11:05:04 -07002190 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002191 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002192 if (n_read < 0)
2193 return VPPCOM_EAGAIN;
2194
2195 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2196 {
Florin Coras62877022020-10-28 21:22:04 -07002197 if (is_ct)
2198 svm_fifo_unset_event (s->rx_fifo);
2199 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002200 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002201 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002202 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002203 {
2204 session_event_t *e;
2205 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2206 e->event_type = SESSION_IO_EVT_RX;
2207 e->session_index = s->session_index;
2208 }
2209 }
2210
2211 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002212 return n_read;
2213}
2214
2215void
Florin Corasd68faf82020-09-25 15:18:13 -07002216vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002217{
2218 vcl_worker_t *wrk = vcl_worker_get_current ();
2219 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002220 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002221
2222 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002223 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002224 return;
2225
Florin Coras62877022020-10-28 21:22:04 -07002226 is_ct = vcl_session_is_ct (s);
2227 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002228
Florin Coras8cc93212021-09-10 11:37:12 -07002229 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002230 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002231}
2232
Florin Coras7a2abce2020-04-05 19:25:44 +00002233always_inline u8
2234vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002235{
Florin Coras7a2abce2020-04-05 19:25:44 +00002236 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2237 if (is_dgram)
2238 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2239 else
2240 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002241}
2242
2243always_inline int
Dou Chao243a0432022-11-29 19:41:34 +08002244vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002245 size_t n, u8 is_flush, u8 is_dgram)
Florin Coras7a2abce2020-04-05 19:25:44 +00002246{
Florin Coras6d0106e2019-01-29 20:11:58 -08002247 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002248 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002249 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002250 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002251 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002252
Florin Coras0b0d28e2021-06-04 17:31:53 -07002253 /* Accept zero length writes but just return */
2254 if (PREDICT_FALSE (!n))
2255 return VPPCOM_OK;
2256
2257 if (PREDICT_FALSE (!buf))
2258 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002259
Florin Coras6c3b2182020-10-19 18:36:48 -07002260 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002261 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002262 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2263 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002264 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002265 }
2266
liuyacan55c952e2021-06-13 14:54:55 +08002267 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002268 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002269 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2270 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002271 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002272 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002273 }
2274
liuyacan55c952e2021-06-13 14:54:55 +08002275 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2276 {
2277 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2278 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002279 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002280 return VPPCOM_EPIPE;
2281 }
2282
Florin Coras0e88e852018-09-17 22:09:02 -07002283 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002284 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002285 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002286
Florin Coras653e43f2019-03-04 10:56:23 -08002287 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002288 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002289 {
Florin Coras54693d22018-07-17 10:46:29 -07002290 if (is_nonblocking)
2291 {
Florin Coras070453d2018-08-24 17:04:27 -07002292 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002293 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002294 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002295 {
Florin Coras2d379d82019-06-28 12:45:12 -07002296 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002297 if (vcl_session_is_closing (s))
2298 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002299
Florin Coras5398dfb2021-01-25 20:31:27 -08002300 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2301 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002302 }
Dave Wallace543852a2017-08-03 02:11:34 -04002303 }
Dave Wallace543852a2017-08-03 02:11:34 -04002304
Florin Coras653e43f2019-03-04 10:56:23 -08002305 et = SESSION_IO_EVT_TX;
2306 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002307 et = SESSION_IO_EVT_TX_FLUSH;
2308
Florin Coras7a2abce2020-04-05 19:25:44 +00002309 if (is_dgram)
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002310 {
2311 et = vcl_session_dgram_tx_evt (s, et);
2312 n_write =
2313 app_send_dgram_raw_gso (tx_fifo, &s->transport, s->vpp_evt_q, buf, n,
2314 s->gso_size, et, 0 /* do_evt */, SVM_Q_WAIT);
2315 }
Florin Coras460dce62018-07-27 05:45:06 -07002316 else
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002317 {
2318 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
2319 0 /* do_evt */, SVM_Q_WAIT);
2320 }
Florin Coras653e43f2019-03-04 10:56:23 -08002321
Florin Coras14ed6df2019-03-06 21:13:42 -08002322 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002323 app_send_io_evt_to_vpp (
2324 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002325
Florin Coras56230092020-09-02 20:52:58 -07002326 /* The underlying fifo segment can run out of memory */
2327 if (PREDICT_FALSE (n_write < 0))
2328 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002329
Florin Coras6d0106e2019-01-29 20:11:58 -08002330 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2331 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002332
Florin Coras54693d22018-07-17 10:46:29 -07002333 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002334}
2335
Florin Coras42ceddb2018-12-12 10:56:01 -08002336int
2337vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2338{
Florin Coras7a2abce2020-04-05 19:25:44 +00002339 vcl_worker_t *wrk = vcl_worker_get_current ();
2340 vcl_session_t *s;
2341
2342 s = vcl_session_get_w_handle (wrk, session_handle);
2343 if (PREDICT_FALSE (!s))
2344 return VPPCOM_EBADFD;
2345
Florin Coraseff5f7a2023-02-07 17:36:17 -08002346 return vppcom_session_write_inline (wrk, s, buf, n, 0 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002347 s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002348}
2349
Florin Corasb0f662f2018-12-27 14:51:46 -08002350int
2351vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2352{
Florin Coras7a2abce2020-04-05 19:25:44 +00002353 vcl_worker_t *wrk = vcl_worker_get_current ();
2354 vcl_session_t *s;
2355
2356 s = vcl_session_get_w_handle (wrk, session_handle);
2357 if (PREDICT_FALSE (!s))
2358 return VPPCOM_EBADFD;
2359
Florin Coraseff5f7a2023-02-07 17:36:17 -08002360 return vppcom_session_write_inline (wrk, s, buf, n, 1 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002361 s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002362}
2363
Florin Corasc0737e92019-03-04 14:19:39 -08002364#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002365if (PREDICT_FALSE (!_s->rx_fifo)) \
2366 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002367if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2368 { \
2369 if (!vcl_session_is_ct (_s)) \
2370 { \
2371 svm_fifo_unset_event (_s->rx_fifo); \
2372 if (svm_fifo_is_empty (_s->rx_fifo)) \
2373 break; \
2374 } \
2375 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2376 { \
Florin Coras2f630182020-08-13 00:17:51 -07002377 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002378 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2379 break; \
2380 } \
2381 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002382
Florin Coras86f04502018-09-12 16:08:01 -07002383static void
2384vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2385 unsigned long n_bits, unsigned long *read_map,
2386 unsigned long *write_map,
2387 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002388{
2389 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002390 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002391 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002392 u32 sid;
2393
2394 switch (e->event_type)
2395 {
Florin Corasc0737e92019-03-04 14:19:39 -08002396 case SESSION_IO_EVT_RX:
2397 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002398 s = vcl_session_get (wrk, sid);
2399 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002400 break;
Florin Corasb242d312020-10-26 15:35:40 -07002401 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002402 if (sid < n_bits && read_map)
2403 {
David Johnsond9818dd2018-12-14 14:53:41 -05002404 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002405 *bits_set += 1;
2406 }
2407 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002408 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002409 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002410 s = vcl_session_get (wrk, sid);
2411 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002412 break;
2413 if (sid < n_bits && write_map)
2414 {
David Johnsond9818dd2018-12-14 14:53:41 -05002415 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002416 *bits_set += 1;
2417 }
2418 break;
Florin Coras86f04502018-09-12 16:08:01 -07002419 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002420 if (!e->postponed)
2421 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2422 else
2423 s = vcl_session_get (wrk, e->session_index);
2424 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002425 break;
Florin Corasb242d312020-10-26 15:35:40 -07002426 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002427 if (sid < n_bits && read_map)
2428 {
David Johnsond9818dd2018-12-14 14:53:41 -05002429 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002430 *bits_set += 1;
2431 }
2432 break;
2433 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002434 if (!e->postponed)
2435 {
2436 connected_msg = (session_connected_msg_t *) e->data;
2437 sid = vcl_session_connected_handler (wrk, connected_msg);
2438 }
2439 else
2440 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002441 if (sid == VCL_INVALID_SESSION_INDEX)
2442 break;
Florin Coras66c675e2023-03-09 16:43:02 -08002443 if (!(sid < n_bits && write_map))
2444 break;
2445 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2446 *bits_set += 1;
2447 s = vcl_session_get (wrk, sid);
Florin Coras66c675e2023-03-09 16:43:02 -08002448 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07002449 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras86f04502018-09-12 16:08:01 -07002450 break;
2451 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras5e9ed0d2023-08-15 11:16:34 -07002452 if (!e->postponed)
2453 {
2454 disconnected_msg = (session_disconnected_msg_t *) e->data;
2455 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2456 if (!s)
2457 break;
2458 }
2459 else
2460 {
2461 s = vcl_session_get (wrk, e->session_index);
2462 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
2463 }
2464 if (vcl_session_is_closed (s))
2465 {
2466 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
2467 vcl_session_free (wrk, s);
2468 break;
2469 }
Florin Corasb242d312020-10-26 15:35:40 -07002470 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002471 if (sid < n_bits && except_map)
2472 {
David Johnsond9818dd2018-12-14 14:53:41 -05002473 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002474 *bits_set += 1;
2475 }
2476 break;
2477 case SESSION_CTRL_EVT_RESET:
Florin Coras5e9ed0d2023-08-15 11:16:34 -07002478 if (!e->postponed)
2479 {
2480 sid =
2481 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2482 s = vcl_session_get (wrk, sid);
2483 }
2484 else
2485 {
2486 sid = e->session_index;
2487 s = vcl_session_get (wrk, sid);
2488 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
2489 }
2490 if (vcl_session_is_closed (s))
2491 {
2492 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
2493 vcl_session_free (wrk, s);
2494 break;
2495 }
Florin Coras86f04502018-09-12 16:08:01 -07002496 if (sid < n_bits && except_map)
2497 {
David Johnsond9818dd2018-12-14 14:53:41 -05002498 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002499 *bits_set += 1;
2500 }
2501 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002502 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2503 vcl_session_unlisten_reply_handler (wrk, e->data);
2504 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002505 case SESSION_CTRL_EVT_MIGRATED:
2506 vcl_session_migrated_handler (wrk, e->data);
2507 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002508 case SESSION_CTRL_EVT_CLEANUP:
2509 vcl_session_cleanup_handler (wrk, e->data);
2510 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002511 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2512 vcl_session_worker_update_reply_handler (wrk, e->data);
2513 break;
2514 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2515 vcl_session_req_worker_update_handler (wrk, e->data);
2516 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002517 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2518 vcl_session_app_add_segment_handler (wrk, e->data);
2519 break;
2520 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2521 vcl_session_app_del_segment_handler (wrk, e->data);
2522 break;
hanlina3a48962020-07-13 11:09:15 +08002523 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002524 vcl_worker_rpc_handler (wrk, e->data);
2525 break;
Florin Coras86f04502018-09-12 16:08:01 -07002526 default:
2527 clib_warning ("unhandled: %u", e->event_type);
2528 break;
2529 }
2530}
2531
2532static int
2533vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2534 unsigned long n_bits, unsigned long *read_map,
2535 unsigned long *write_map, unsigned long *except_map,
2536 double time_to_wait, u32 * bits_set)
2537{
Florin Coras99368312018-08-02 10:45:44 -07002538 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002539 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002540 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002541
Florin Coras54693d22018-07-17 10:46:29 -07002542 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002543 {
Florin Coras54693d22018-07-17 10:46:29 -07002544 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002545 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002546
Florin Coras54693d22018-07-17 10:46:29 -07002547 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002548 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002549 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002550 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002551 else
2552 {
2553 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002554 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002555 }
2556 }
Florin Corase003a1b2019-06-05 10:47:16 -07002557 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002558
Florin Coras134a9962018-08-28 11:32:04 -07002559 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002560 {
Florin Coras134a9962018-08-28 11:32:04 -07002561 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002562 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002563 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2564 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002565 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002566 }
Florin Coras134a9962018-08-28 11:32:04 -07002567 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002568 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002569 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002570}
2571
Florin Coras99368312018-08-02 10:45:44 -07002572static int
Florin Coras294afe22019-01-07 17:49:17 -08002573vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2574 vcl_si_set * read_map, vcl_si_set * write_map,
2575 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002576 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002577{
Florin Coras14ed6df2019-03-06 21:13:42 -08002578 double wait = 0, start = 0;
2579
2580 if (!*bits_set)
2581 {
2582 wait = time_to_wait;
2583 start = clib_time_now (&wrk->clib_time);
2584 }
2585
2586 do
2587 {
2588 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2589 write_map, except_map, wait, bits_set);
2590 if (*bits_set)
2591 return *bits_set;
2592 if (wait == -1)
2593 continue;
2594
2595 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2596 }
2597 while (wait > 0);
2598
2599 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002600}
2601
2602static int
Florin Coras294afe22019-01-07 17:49:17 -08002603vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2604 vcl_si_set * read_map, vcl_si_set * write_map,
2605 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002606 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002607{
2608 vcl_mq_evt_conn_t *mqc;
2609 int __clib_unused n_read;
2610 int n_mq_evts, i;
2611 u64 buf;
2612
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002613 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2614 {
2615 vcl_api_retry_attach (wrk);
2616 return 0;
2617 }
2618
Florin Coras134a9962018-08-28 11:32:04 -07002619 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2620 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2621 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002622 for (i = 0; i < n_mq_evts; i++)
2623 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002624 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2625 {
2626 vcl_api_handle_disconnect (wrk);
2627 continue;
2628 }
2629
Florin Coras134a9962018-08-28 11:32:04 -07002630 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002631 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002632 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002633 except_map, 0, bits_set);
2634 }
2635
2636 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2637}
2638
Dave Wallace543852a2017-08-03 02:11:34 -04002639int
Florin Coras294afe22019-01-07 17:49:17 -08002640vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2641 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002642{
Florin Coras54693d22018-07-17 10:46:29 -07002643 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002644 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002645 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002646 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002647
Dave Wallace7876d392017-10-19 03:53:57 -04002648 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002649 {
Florin Coras134a9962018-08-28 11:32:04 -07002650 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002651 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002652 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2653 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002654 }
Dave Wallace7876d392017-10-19 03:53:57 -04002655 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002656 {
Florin Coras134a9962018-08-28 11:32:04 -07002657 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002658 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002659 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2660 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002661 }
Dave Wallace7876d392017-10-19 03:53:57 -04002662 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002663 {
Florin Coras134a9962018-08-28 11:32:04 -07002664 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002665 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002666 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2667 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002668 }
2669
Florin Coras54693d22018-07-17 10:46:29 -07002670 if (!n_bits)
2671 return 0;
2672
2673 if (!write_map)
2674 goto check_rd;
2675
Florin Coras096a1d52021-01-27 15:33:51 -08002676 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2677 {
2678 if (!(s = vcl_session_get (wrk, sid)))
2679 {
2680 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2681 bits_set++;
2682 continue;
2683 }
Florin Coras54693d22018-07-17 10:46:29 -07002684
Florin Coras096a1d52021-01-27 15:33:51 -08002685 if (vcl_session_write_ready (s))
2686 {
2687 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2688 bits_set++;
2689 }
Florin Coras607eb202023-05-29 16:19:38 -07002690 else
Florin Coras096a1d52021-01-27 15:33:51 -08002691 {
Florin Coras607eb202023-05-29 16:19:38 -07002692 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras096a1d52021-01-27 15:33:51 -08002693 }
2694 }
Florin Coras54693d22018-07-17 10:46:29 -07002695
2696check_rd:
2697 if (!read_map)
2698 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002699
Florin Coras096a1d52021-01-27 15:33:51 -08002700 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2701 {
2702 if (!(s = vcl_session_get (wrk, sid)))
2703 {
2704 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2705 bits_set++;
2706 continue;
2707 }
Florin Coras54693d22018-07-17 10:46:29 -07002708
Florin Coras096a1d52021-01-27 15:33:51 -08002709 if (vcl_session_read_ready (s))
2710 {
2711 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2712 bits_set++;
2713 }
2714 }
Florin Coras54693d22018-07-17 10:46:29 -07002715
2716check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002717
Florin Coras86f04502018-09-12 16:08:01 -07002718 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2719 {
2720 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2721 read_map, write_map, except_map, &bits_set);
2722 }
2723 vec_reset_length (wrk->unhandled_evts_vector);
2724
Florin Coras99368312018-08-02 10:45:44 -07002725 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002726 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002727 time_to_wait, &bits_set);
2728 else
Florin Coras134a9962018-08-28 11:32:04 -07002729 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002730 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002731
Dave Wallace543852a2017-08-03 02:11:34 -04002732 return (bits_set);
2733}
2734
Dave Wallacef7f809c2017-10-03 01:48:42 -04002735static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002736vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002737{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002738 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002739 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002740 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002741
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002742 if (VPPCOM_DEBUG <= 3)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743 return;
2744
Florin Coras6c3b2182020-10-19 18:36:48 -07002745 s = vcl_session_get_w_handle (wrk, vep_handle);
2746 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002747 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002748 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002749 goto done;
2750 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002751 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002752 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002753 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002754 goto done;
2755 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002756 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002757 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002758 "{\n"
2759 " is_vep = %u\n"
2760 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002761 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002762 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2763 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002764
Florin Coras7e5e62b2019-07-30 14:08:23 -07002765 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002766 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002767 s = vcl_session_get_w_handle (wrk, sh);
2768 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002770 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002771 goto done;
2772 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002773 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002774 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002775 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002776 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002777 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002778 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002779 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002780 goto done;
2781 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002782 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002783 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2784 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002785 sh, s->vep.vep_sh, vep_handle);
2786 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002787 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002788 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002789 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002790 " next_sh = 0x%x (%u)\n"
2791 " prev_sh = 0x%x (%u)\n"
2792 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002793 " ev.events = 0x%x\n"
2794 " ev.data.u64 = 0x%llx\n"
2795 " et_mask = 0x%x\n"
2796 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002797 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002798 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2799 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002800 }
2801 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802
2803done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002804 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002805}
2806
2807int
2808vppcom_epoll_create (void)
2809{
Florin Coras134a9962018-08-28 11:32:04 -07002810 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002811 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002812
Florin Coras134a9962018-08-28 11:32:04 -07002813 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002814
Florin Coras6c3b2182020-10-19 18:36:48 -07002815 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002816 vep_session->vep.vep_sh = ~0;
2817 vep_session->vep.next_sh = ~0;
2818 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002819 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002820
Florin Corasa7a1a222018-12-30 17:11:31 -08002821 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2822 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002823
Florin Corasab2f6db2018-08-31 14:31:41 -07002824 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002825}
2826
Florin Corasb0116a12023-02-07 09:11:47 -08002827static void
2828vcl_epoll_ctl_add_unhandled_event (vcl_worker_t *wrk, vcl_session_t *s,
2829 u8 is_epollet, session_evt_type_t evt)
2830{
2831 if (!is_epollet)
2832 {
Florin Corasc2a14172023-02-28 21:13:50 -08002833 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
2834 vcl_epoll_lt_add (wrk, s);
Florin Corasb0116a12023-02-07 09:11:47 -08002835 return;
2836 }
2837
2838 session_event_t e = { 0 };
2839 e.session_index = s->session_index;
2840 e.event_type = evt;
2841 if (evt == SESSION_IO_EVT_RX)
2842 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2843 vec_add1 (wrk->unhandled_evts_vector, e);
2844}
2845
Dave Wallacef7f809c2017-10-03 01:48:42 -04002846int
Florin Coras134a9962018-08-28 11:32:04 -07002847vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002848 struct epoll_event *event)
2849{
Florin Coras134a9962018-08-28 11:32:04 -07002850 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002851 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002852 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002853 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002854
Florin Coras134a9962018-08-28 11:32:04 -07002855 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002856 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002857 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002858 return VPPCOM_EINVAL;
2859 }
2860
Florin Coras134a9962018-08-28 11:32:04 -07002861 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002862 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002863 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002864 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002865 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002866 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002867 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002868 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002869 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002870 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002871 }
2872
Florin Coras134a9962018-08-28 11:32:04 -07002873 ASSERT (vep_session->vep.vep_sh == ~0);
2874 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002875
Florin Coras17ec5772020-08-12 20:46:29 -07002876 s = vcl_session_get_w_handle (wrk, session_handle);
2877 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002878 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002879 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002880 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002881 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002882 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002883 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002884 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002885 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002886 }
2887
2888 switch (op)
2889 {
2890 case EPOLL_CTL_ADD:
2891 if (PREDICT_FALSE (!event))
2892 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002893 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002894 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002895 }
Florin Coras2645f682021-06-04 15:59:41 -07002896 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2897 {
2898 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2899 rv = VPPCOM_EEXIST;
2900 goto done;
2901 }
Florin Coras134a9962018-08-28 11:32:04 -07002902 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002903 {
Florin Coras7e12d942018-06-27 14:32:43 -07002904 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002905 next_session = vcl_session_get_w_handle (wrk,
2906 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002907 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002908 {
Florin Coras5e062572019-03-14 19:07:51 -07002909 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002910 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002911 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002912 }
Florin Coras134a9962018-08-28 11:32:04 -07002913 ASSERT (next_session->vep.prev_sh == vep_handle);
2914 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002915 }
Florin Coras17ec5772020-08-12 20:46:29 -07002916 s->vep.next_sh = vep_session->vep.next_sh;
2917 s->vep.prev_sh = vep_handle;
2918 s->vep.vep_sh = vep_handle;
2919 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002920 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002921 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07002922 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Coras6c3b2182020-10-19 18:36:48 -07002923 s->flags &= ~VCL_SESSION_F_IS_VEP;
2924 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002925 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002926
Florin Coras470d72f2023-06-01 21:50:03 -07002927 if ((event->events & EPOLLOUT))
hanlin475c9d72019-12-26 11:44:28 +08002928 {
Florin Coras470d72f2023-06-01 21:50:03 -07002929 int write_ready = vcl_session_write_ready (s);
2930
2931 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2932 if (write_ready > 0)
2933 {
2934 /* Generate EPOLLOUT if tx fifo not full */
2935 vcl_epoll_ctl_add_unhandled_event (
2936 wrk, s, event->events & EPOLLET, SESSION_IO_EVT_TX);
2937 add_evt = 1;
2938 }
2939 else
2940 {
2941 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
2942 }
hanlin475c9d72019-12-26 11:44:28 +08002943 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002944 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002945 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002946 {
Florin Corasb0116a12023-02-07 09:11:47 -08002947 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2948 SESSION_IO_EVT_RX);
Florin Coras87f76002021-06-28 19:13:29 -07002949 add_evt = 1;
2950 }
2951 if (!add_evt && vcl_session_is_closing (s))
2952 {
2953 session_event_t e = { 0 };
2954 if (s->session_state == VCL_STATE_VPP_CLOSING)
2955 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2956 else
2957 e.event_type = SESSION_CTRL_EVT_RESET;
2958 e.session_index = s->session_index;
2959 e.postponed = 1;
2960 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002961 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002962 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2963 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002964 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002965 break;
2966
2967 case EPOLL_CTL_MOD:
2968 if (PREDICT_FALSE (!event))
2969 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002970 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002971 rv = VPPCOM_EINVAL;
2972 goto done;
2973 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002974 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002975 {
Florin Coras5e062572019-03-14 19:07:51 -07002976 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002977 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002978 goto done;
2979 }
Florin Coras17ec5772020-08-12 20:46:29 -07002980 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002981 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002982 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002983 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002984 rv = VPPCOM_EINVAL;
2985 goto done;
2986 }
hanlin475c9d72019-12-26 11:44:28 +08002987
Florin Coras470d72f2023-06-01 21:50:03 -07002988 /* Generate EPOLLOUT if session write ready and event was not on */
2989 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT))
2990 {
2991 /* Fifo size load acq synchronized with update store rel */
2992 int write_ready = vcl_session_write_ready (s);
2993
2994 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2995 if (write_ready > 0)
2996 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2997 SESSION_IO_EVT_TX);
2998 else
2999 /* Request deq ntf in case dequeue happened while updating flag */
3000 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
3001 }
3002 else if (!(event->events & EPOLLOUT))
Florin Coras607eb202023-05-29 16:19:38 -07003003 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasbc4d5b02023-05-17 23:20:56 -07003004
Florin Coras2645f682021-06-04 15:59:41 -07003005 /* Generate EPOLLIN if session read ready and event was not on */
3006 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
3007 (vcl_session_read_ready (s) > 0))
3008 {
Florin Corasb0116a12023-02-07 09:11:47 -08003009 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
3010 SESSION_IO_EVT_RX);
Florin Coras2645f682021-06-04 15:59:41 -07003011 }
Florin Coras17ec5772020-08-12 20:46:29 -07003012 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
3013 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07003014 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Corasbc4d5b02023-05-17 23:20:56 -07003015
Florin Corasa7a1a222018-12-30 17:11:31 -08003016 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
3017 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003018 break;
3019
3020 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07003021 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003022 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003023 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07003024 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003025 goto done;
3026 }
Florin Coras17ec5772020-08-12 20:46:29 -07003027 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05003028 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003029 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003030 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003031 rv = VPPCOM_EINVAL;
3032 goto done;
3033 }
3034
Florin Coras17ec5772020-08-12 20:46:29 -07003035 if (s->vep.prev_sh == vep_handle)
3036 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003037 else
3038 {
Florin Coras7e12d942018-06-27 14:32:43 -07003039 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07003040 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07003041 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003042 {
Florin Coras5e062572019-03-14 19:07:51 -07003043 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003044 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003045 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003046 }
Florin Coras134a9962018-08-28 11:32:04 -07003047 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003048 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003049 }
Florin Coras17ec5772020-08-12 20:46:29 -07003050 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003051 {
Florin Coras7e12d942018-06-27 14:32:43 -07003052 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07003053 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07003054 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003055 {
Florin Coras5e062572019-03-14 19:07:51 -07003056 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003057 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003058 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003059 }
Florin Coras134a9962018-08-28 11:32:04 -07003060 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003061 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003062 }
3063
Florin Corasfa3884f2021-06-22 20:04:46 -07003064 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
3065 vcl_epoll_lt_del (wrk, s);
3066
Florin Coras17ec5772020-08-12 20:46:29 -07003067 memset (&s->vep, 0, sizeof (s->vep));
3068 s->vep.next_sh = ~0;
3069 s->vep.prev_sh = ~0;
3070 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003071 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07003072 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08003073
Florin Corasf1ddeeb2021-06-10 08:08:53 -07003074 if (vcl_session_is_open (s))
Florin Coras607eb202023-05-29 16:19:38 -07003075 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08003076
Florin Coras5e062572019-03-14 19:07:51 -07003077 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003078 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003079 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003080 break;
3081
3082 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003083 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003084 rv = VPPCOM_EINVAL;
3085 }
3086
Florin Coras134a9962018-08-28 11:32:04 -07003087 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003088
3089done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003090 return rv;
3091}
3092
Florin Coras30ecfa82023-06-14 11:15:36 -07003093always_inline u8
3094vcl_ep_session_needs_evt (vcl_session_t *s, u32 evt)
3095{
3096 /* No event if not epolled / events reset on hup or level-trigger on */
3097 return ((s->vep.ev.events & evt) &&
3098 s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
3099}
3100
Florin Coras86f04502018-09-12 16:08:01 -07003101static inline void
3102vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3103 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003104{
3105 session_disconnected_msg_t *disconnected_msg;
3106 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003107 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003108 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003109 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003110 u8 add_event = 0;
3111
3112 switch (e->event_type)
3113 {
Florin Coras653e43f2019-03-04 10:56:23 -08003114 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003115 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003116 s = vcl_session_get (wrk, sid);
3117 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003118 break;
Florin Corasb242d312020-10-26 15:35:40 -07003119 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras30ecfa82023-06-14 11:15:36 -07003120 if (!vcl_ep_session_needs_evt (s, EPOLLIN) ||
3121 (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07003122 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003123 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003124 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003125 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003126 session_evt_data = s->vep.ev.data.u64;
3127 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003128 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003129 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003130 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003131 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003132 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003133 break;
Florin Coras48178552023-05-17 22:59:40 -07003134 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ? s->ct_tx_fifo :
3135 s->tx_fifo);
Florin Coras30ecfa82023-06-14 11:15:36 -07003136 if (!vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras86f04502018-09-12 16:08:01 -07003137 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003138 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003139 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003140 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003141 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003142 break;
Florin Coras86f04502018-09-12 16:08:01 -07003143 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003144 if (!e->postponed)
3145 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3146 else
3147 s = vcl_session_get (wrk, e->session_index);
Florin Coras30ecfa82023-06-14 11:15:36 -07003148 if (!s || !vcl_ep_session_needs_evt (s, EPOLLIN))
Florin Coras3c7d4f92018-12-14 11:28:43 -08003149 break;
Florin Corasb242d312020-10-26 15:35:40 -07003150 sid = s->session_index;
Florin Coras30ecfa82023-06-14 11:15:36 -07003151 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003152 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003153 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003154 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003155 break;
3156 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003157 if (!e->postponed)
3158 {
3159 connected_msg = (session_connected_msg_t *) e->data;
3160 sid = vcl_session_connected_handler (wrk, connected_msg);
3161 }
3162 else
3163 sid = e->session_index;
3164 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003165 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras72f77822019-01-22 19:05:52 -08003166 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003167 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07003168 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003169 add_event = 1;
Florin Coras30ecfa82023-06-14 11:15:36 -07003170 session_events = s->vep.ev.events;
3171 /* Generate EPOLLOUT because there's no connected event */
wanghanlin9e42cc22021-06-25 17:40:13 +08003172 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003173 session_evt_data = s->vep.ev.data.u64;
3174 if (s->session_state == VCL_STATE_DETACHED)
Florin Coras30ecfa82023-06-14 11:15:36 -07003175 {
3176 events[*num_ev].events |= EPOLLHUP;
3177 s->vep.ev.events = 0;
3178 }
Florin Coras86f04502018-09-12 16:08:01 -07003179 break;
3180 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003181 if (!e->postponed)
3182 {
3183 disconnected_msg = (session_disconnected_msg_t *) e->data;
3184 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3185 }
3186 else
3187 {
3188 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003189 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003190 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003191 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003192 {
Florin Coras74254b52021-12-08 11:03:08 -08003193 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003194 vcl_session_free (wrk, s);
3195 break;
3196 }
Florin Corasb242d312020-10-26 15:35:40 -07003197 sid = s->session_index;
3198 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003199 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003200 if (EPOLLRDHUP & session_events)
3201 {
3202 /* If app can distinguish between RDHUP and HUP,
3203 * we make finer control */
3204 events[*num_ev].events = EPOLLRDHUP;
3205 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3206 {
3207 events[*num_ev].events |= EPOLLHUP;
3208 }
3209 }
3210 else
3211 {
3212 events[*num_ev].events = EPOLLHUP;
3213 }
Florin Corasb242d312020-10-26 15:35:40 -07003214 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003215 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003216 break;
Florin Coras01ee7a72023-03-01 00:49:25 -08003217 case SESSION_CTRL_EVT_BOUND:
3218 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
3219 break;
Florin Coras86f04502018-09-12 16:08:01 -07003220 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003221 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003222 {
3223 sid =
3224 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3225 s = vcl_session_get (wrk, sid);
3226 }
Florin Coras87f76002021-06-28 19:13:29 -07003227 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003228 {
3229 sid = e->session_index;
3230 s = vcl_session_get (wrk, sid);
3231 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3232 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003233 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003234 {
Florin Coras74254b52021-12-08 11:03:08 -08003235 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003236 vcl_session_free (wrk, s);
3237 break;
3238 }
Florin Corasb242d312020-10-26 15:35:40 -07003239 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003240 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003241 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3242 if ((EPOLLRDHUP & session_events) &&
3243 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3244 {
Yacan Liuab157702022-09-26 16:41:32 +08003245 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003246 }
3247 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3248 {
3249 events[*num_ev].events |= EPOLLIN;
3250 }
Florin Corasb242d312020-10-26 15:35:40 -07003251 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003252 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003253 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003254 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3255 vcl_session_unlisten_reply_handler (wrk, e->data);
3256 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003257 case SESSION_CTRL_EVT_MIGRATED:
3258 vcl_session_migrated_handler (wrk, e->data);
3259 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003260 case SESSION_CTRL_EVT_CLEANUP:
3261 vcl_session_cleanup_handler (wrk, e->data);
3262 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003263 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3264 vcl_session_req_worker_update_handler (wrk, e->data);
3265 break;
3266 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3267 vcl_session_worker_update_reply_handler (wrk, e->data);
3268 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003269 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3270 vcl_session_app_add_segment_handler (wrk, e->data);
3271 break;
3272 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3273 vcl_session_app_del_segment_handler (wrk, e->data);
3274 break;
hanlina3a48962020-07-13 11:09:15 +08003275 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003276 vcl_worker_rpc_handler (wrk, e->data);
3277 break;
Florin Coras86f04502018-09-12 16:08:01 -07003278 default:
3279 VDBG (0, "unhandled: %u", e->event_type);
3280 break;
3281 }
3282
3283 if (add_event)
3284 {
Florin Coras30ecfa82023-06-14 11:15:36 -07003285 ASSERT (s->flags & VCL_SESSION_F_IS_VEP_SESSION);
Florin Coras86f04502018-09-12 16:08:01 -07003286 events[*num_ev].data.u64 = session_evt_data;
3287 if (EPOLLONESHOT & session_events)
3288 {
Florin Corasb242d312020-10-26 15:35:40 -07003289 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003290 if (!(events[*num_ev].events & EPOLLHUP))
3291 s->vep.ev.events = EPOLLHUP | EPOLLERR;
Florin Coras86f04502018-09-12 16:08:01 -07003292 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003293 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003294 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003295 s = vcl_session_get (wrk, sid);
3296 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3297 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003298 }
Florin Coras86f04502018-09-12 16:08:01 -07003299 *num_ev += 1;
3300 }
3301}
3302
3303static int
3304vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3305 struct epoll_event *events, u32 maxevents,
3306 double wait_for_time, u32 * num_ev)
3307{
Florin Coras99368312018-08-02 10:45:44 -07003308 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003309 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003310 int i;
3311
Florin Coras539663c2018-09-28 14:59:37 -07003312 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3313 goto handle_dequeued;
3314
Florin Coras54693d22018-07-17 10:46:29 -07003315 if (svm_msg_q_is_empty (mq))
3316 {
3317 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003318 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003319 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003320 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003321 else
3322 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003323 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003324 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003325 }
3326 }
Florin Corase003a1b2019-06-05 10:47:16 -07003327 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003328 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003329
Florin Coras539663c2018-09-28 14:59:37 -07003330handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003331 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003332 {
Florin Coras134a9962018-08-28 11:32:04 -07003333 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003334 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003335 if (*num_ev < maxevents)
3336 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3337 else
3338 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003339 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003340 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003341 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003342 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003343 return *num_ev;
3344}
3345
Florin Coras99368312018-08-02 10:45:44 -07003346static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003347vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3348 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003349{
Florin Corasccdb8b82021-04-28 13:01:06 -07003350 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003351
3352 if (!n_evts)
3353 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003354 if (timeout_ms > 0)
3355 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003356 }
3357
3358 do
3359 {
3360 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003361 timeout_ms, &n_evts);
3362 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003363 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003364 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003365 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003366
3367 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003368}
3369
3370static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003371vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3372 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003373{
Florin Coras99368312018-08-02 10:45:44 -07003374 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003375 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003376 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003377 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003378 u64 buf;
3379
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003380 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3381 {
3382 vcl_api_retry_attach (wrk);
3383 return n_evts;
3384 }
3385
Florin Coras134a9962018-08-28 11:32:04 -07003386 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003387 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003388 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003389 if (timeout_ms > 0)
3390 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003391 }
3392
Florin Corasb13ba132021-01-27 16:05:24 -08003393 do
3394 {
3395 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003396 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003397 if (n_mq_evts < 0)
3398 {
3399 VDBG (0, "epoll_wait error %u", errno);
3400 return n_evts;
3401 }
3402
3403 for (i = 0; i < n_mq_evts; i++)
3404 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003405 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3406 {
3407 /* api socket was closed */
3408 vcl_api_handle_disconnect (wrk);
3409 continue;
3410 }
3411
Florin Corasb13ba132021-01-27 16:05:24 -08003412 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3413 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3414 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3415 &n_evts);
3416 }
3417
Florin Corasccdb8b82021-04-28 13:01:06 -07003418 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003419 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003420 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003421 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003422
3423 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003424}
3425
Florin Corasfe286f72021-06-04 10:07:55 -07003426static void
Florin Corasfe286f72021-06-04 10:07:55 -07003427vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3428 int maxevents, u32 *n_evts)
3429{
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003430 u32 add_event = 0, evt_flags = 0, next, *to_remove = 0, *si;
Florin Corasfe286f72021-06-04 10:07:55 -07003431 vcl_session_t *s;
3432 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003433 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003434
Florin Corasfa3884f2021-06-22 20:04:46 -07003435 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003436 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003437 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003438
Florin Corasfa3884f2021-06-22 20:04:46 -07003439 next = wrk->ep_lt_current;
3440 do
Florin Corasfe286f72021-06-04 10:07:55 -07003441 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003442 s = vcl_session_get (wrk, next);
3443 next = s->vep.lt_next;
3444
Florin Coras30ecfa82023-06-14 11:15:36 -07003445 if (s->vep.ev.events == 0)
3446 {
3447 vec_add1 (to_remove, s->session_index);
3448 continue;
3449 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003450 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003451 {
3452 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003453 evt_flags |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003454 evt_data = s->vep.ev.data.u64;
3455 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003456 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003457 {
3458 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003459 evt_flags |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
Florin Corasfa3884f2021-06-22 20:04:46 -07003460 evt_data = s->vep.ev.data.u64;
3461 }
3462 if (!add_event && s->session_state > VCL_STATE_READY)
3463 {
3464 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003465 evt_flags |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003466 evt_data = s->vep.ev.data.u64;
3467 }
3468 if (add_event)
3469 {
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003470 events[*n_evts].events = evt_flags;
Florin Corasfe286f72021-06-04 10:07:55 -07003471 events[*n_evts].data.u64 = evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003472 if (EPOLLONESHOT & s->vep.ev.events)
Florin Coras30ecfa82023-06-14 11:15:36 -07003473 s->vep.ev.events = EPOLLHUP | EPOLLERR;
3474 if (evt_flags & EPOLLHUP)
Florin Corasfa3884f2021-06-22 20:04:46 -07003475 s->vep.ev.events = 0;
Florin Coras5cca6692023-06-20 08:47:37 -07003476 *n_evts += 1;
3477 add_event = 0;
3478 evt_flags = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003479 if (*n_evts == maxevents)
3480 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003481 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003482 break;
3483 }
3484 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003485 else
3486 {
Florin Coras7ff72742023-05-16 13:05:28 -07003487 vec_add1 (to_remove, s->session_index);
Florin Corasfa3884f2021-06-22 20:04:46 -07003488 }
Florin Corasfe286f72021-06-04 10:07:55 -07003489 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003490 while (next != wrk->ep_lt_current);
Florin Coras7ff72742023-05-16 13:05:28 -07003491
3492 vec_foreach (si, to_remove)
3493 {
3494 s = vcl_session_get (wrk, *si);
3495 vcl_epoll_lt_del (wrk, s);
3496 }
3497 vec_free (to_remove);
Florin Corasfe286f72021-06-04 10:07:55 -07003498}
3499
Dave Wallacef7f809c2017-10-03 01:48:42 -04003500int
Florin Coras134a9962018-08-28 11:32:04 -07003501vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003502 int maxevents, double wait_for_time)
3503{
Florin Coras134a9962018-08-28 11:32:04 -07003504 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003505 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003506 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003507 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003508
3509 if (PREDICT_FALSE (maxevents <= 0))
3510 {
Florin Coras5e062572019-03-14 19:07:51 -07003511 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003512 return VPPCOM_EINVAL;
3513 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003514
Florin Coras134a9962018-08-28 11:32:04 -07003515 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003516 if (!vep_session)
3517 return VPPCOM_EBADFD;
3518
Florin Coras6c3b2182020-10-19 18:36:48 -07003519 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003520 {
Florin Coras5e062572019-03-14 19:07:51 -07003521 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003522 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003523 }
Florin Coras54693d22018-07-17 10:46:29 -07003524
Florin Coras86f04502018-09-12 16:08:01 -07003525 if (vec_len (wrk->unhandled_evts_vector))
3526 {
3527 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3528 {
3529 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3530 events, &n_evts);
3531 if (n_evts == maxevents)
3532 {
Florin Corase003a1b2019-06-05 10:47:16 -07003533 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3534 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003535 }
3536 }
Florin Corase003a1b2019-06-05 10:47:16 -07003537 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003538 }
liuyacancba87102021-09-10 15:14:05 +08003539
3540 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3541 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3542
wanghanlin8919fec2021-03-18 20:00:41 +08003543 /* Request to only drain unhandled */
3544 if ((int) wait_for_time == -2)
3545 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003546
Florin Coras86f04502018-09-12 16:08:01 -07003547
Florin Corasfe286f72021-06-04 10:07:55 -07003548 if (vcm->cfg.use_mq_eventfd)
3549 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3550 wait_for_time);
3551 else
3552 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3553 wait_for_time);
3554
Florin Corasfe286f72021-06-04 10:07:55 -07003555 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003556}
3557
Dave Wallace35830af2017-10-09 01:43:42 -04003558int
Florin Coras134a9962018-08-28 11:32:04 -07003559vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003560 void *buffer, uint32_t * buflen)
3561{
Florin Coras134a9962018-08-28 11:32:04 -07003562 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003563 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003564 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003565 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003566 vcl_session_t *session;
3567 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003568
Florin Coras134a9962018-08-28 11:32:04 -07003569 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003570 if (!session)
3571 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003572
Dave Wallace35830af2017-10-09 01:43:42 -04003573 switch (op)
3574 {
3575 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003576 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003577 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3578 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003579 break;
3580
Dave Wallace227867f2017-11-13 21:21:53 -05003581 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003582 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003583 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3584 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003585 break;
3586
3587 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003588 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003589 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003590 *flags =
3591 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003592 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003593 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003594 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003595 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3596 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003597 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003598 }
3599 else
3600 rv = VPPCOM_EINVAL;
3601 break;
3602
3603 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003604 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003605 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003606 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003607 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003608 else
Florin Corasac422d62020-10-19 20:51:36 -07003609 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003610
Florin Coras5e062572019-03-14 19:07:51 -07003611 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3612 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003613 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003614 }
3615 else
3616 rv = VPPCOM_EINVAL;
3617 break;
3618
3619 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003620 if (PREDICT_TRUE (buffer && buflen &&
3621 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003622 {
Florin Coras7e12d942018-06-27 14:32:43 -07003623 ep->is_ip4 = session->transport.is_ip4;
3624 ep->port = session->transport.rmt_port;
3625 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003626 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3627 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003628 else
Dave Barach178cf492018-11-13 16:34:13 -05003629 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3630 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003631 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003632 VDBG (1,
3633 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3634 "addr = %U, port %u",
3635 session_handle, ep->is_ip4, vcl_format_ip46_address,
3636 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003637 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3638 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003639 }
3640 else
3641 rv = VPPCOM_EINVAL;
3642 break;
3643
3644 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003645 if (PREDICT_TRUE (buffer && buflen &&
3646 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003647 {
Florin Coras7e12d942018-06-27 14:32:43 -07003648 ep->is_ip4 = session->transport.is_ip4;
3649 ep->port = session->transport.lcl_port;
3650 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003651 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3652 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003653 else
Dave Barach178cf492018-11-13 16:34:13 -05003654 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3655 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003656 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003657 VDBG (1,
3658 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3659 " port %d",
3660 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003661 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003662 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3663 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003664 }
3665 else
3666 rv = VPPCOM_EINVAL;
3667 break;
Stevenb5a11602017-10-11 09:59:30 -07003668
qinyangaf9b7152023-06-27 01:11:53 -07003669 case VPPCOM_ATTR_GET_ORIGINAL_DST:
3670 if (!session->transport.is_ip4)
3671 {
3672 /* now original dst only support ipv4*/
3673 rv = VPPCOM_EAFNOSUPPORT;
3674 break;
3675 }
3676 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*ep)) &&
3677 ep->ip))
3678 {
3679 ep->is_ip4 = session->transport.is_ip4;
3680 ep->port = session->original_dst_port;
3681 clib_memcpy_fast (ep->ip, &session->original_dst_ip4,
3682 sizeof (ip4_address_t));
3683 *buflen = sizeof (*ep);
3684 VDBG (1,
3685 "VPPCOM_ATTR_GET_ORIGINAL_DST: sh %u, is_ip4 = %u, addr = %U"
3686 " port %d",
3687 session_handle, ep->is_ip4, vcl_format_ip4_address,
3688 (ip4_address_t *) (&session->original_dst_ip4),
3689 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3690 clib_net_to_host_u16 (ep->port));
3691 }
3692 else
3693 rv = VPPCOM_EINVAL;
3694 break;
3695
Florin Corasef7cbf62019-10-17 09:56:27 -07003696 case VPPCOM_ATTR_SET_LCL_ADDR:
3697 if (PREDICT_TRUE (buffer && buflen &&
3698 (*buflen >= sizeof (*ep)) && ep->ip))
3699 {
3700 session->transport.is_ip4 = ep->is_ip4;
3701 session->transport.lcl_port = ep->port;
3702 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3703 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003704 VDBG (1,
3705 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3706 " port %d",
3707 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003708 &session->transport.lcl_ip,
3709 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3710 clib_net_to_host_u16 (ep->port));
3711 }
3712 else
3713 rv = VPPCOM_EINVAL;
3714 break;
3715
Dave Wallace048b1d62018-01-03 22:24:41 -05003716 case VPPCOM_ATTR_GET_LIBC_EPFD:
3717 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003718 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003719 break;
3720
3721 case VPPCOM_ATTR_SET_LIBC_EPFD:
3722 if (PREDICT_TRUE (buffer && buflen &&
3723 (*buflen == sizeof (session->libc_epfd))))
3724 {
3725 session->libc_epfd = *(int *) buffer;
3726 *buflen = sizeof (session->libc_epfd);
3727
Florin Coras5e062572019-03-14 19:07:51 -07003728 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3729 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003730 }
3731 else
3732 rv = VPPCOM_EINVAL;
3733 break;
3734
3735 case VPPCOM_ATTR_GET_PROTOCOL:
3736 if (buffer && buflen && (*buflen >= sizeof (int)))
3737 {
Florin Coras7e12d942018-06-27 14:32:43 -07003738 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003739 *buflen = sizeof (int);
3740
Florin Coras5e062572019-03-14 19:07:51 -07003741 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3742 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003743 }
3744 else
3745 rv = VPPCOM_EINVAL;
3746 break;
3747
3748 case VPPCOM_ATTR_GET_LISTEN:
3749 if (buffer && buflen && (*buflen >= sizeof (int)))
3750 {
Florin Corasac422d62020-10-19 20:51:36 -07003751 *(int *) buffer = vcl_session_has_attr (session,
3752 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003753 *buflen = sizeof (int);
3754
Florin Coras5e062572019-03-14 19:07:51 -07003755 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3756 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003757 }
3758 else
3759 rv = VPPCOM_EINVAL;
3760 break;
3761
3762 case VPPCOM_ATTR_GET_ERROR:
3763 if (buffer && buflen && (*buflen >= sizeof (int)))
3764 {
3765 *(int *) buffer = 0;
3766 *buflen = sizeof (int);
3767
Florin Coras5e062572019-03-14 19:07:51 -07003768 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3769 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003770 }
3771 else
3772 rv = VPPCOM_EINVAL;
3773 break;
3774
3775 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3776 if (buffer && buflen && (*buflen >= sizeof (u32)))
3777 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003778
3779 /* VPP-TBD */
3780 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003781 session->tx_fifo ?
3782 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003783 vcm->cfg.tx_fifo_size);
3784 *buflen = sizeof (u32);
3785
Florin Coras5e062572019-03-14 19:07:51 -07003786 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3787 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3788 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003789 }
3790 else
3791 rv = VPPCOM_EINVAL;
3792 break;
3793
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003794 case VPPCOM_ATTR_SET_DSCP:
3795 if (buffer && buflen && (*buflen >= sizeof (u8)))
3796 {
3797 session->dscp = *(u8 *) buffer;
3798
3799 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3800 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3801 }
3802 else
3803 rv = VPPCOM_EINVAL;
3804 break;
3805
Dave Wallace048b1d62018-01-03 22:24:41 -05003806 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3807 if (buffer && buflen && (*buflen == sizeof (u32)))
3808 {
3809 /* VPP-TBD */
3810 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003811 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3812 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3813 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003814 }
3815 else
3816 rv = VPPCOM_EINVAL;
3817 break;
3818
3819 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3820 if (buffer && buflen && (*buflen >= sizeof (u32)))
3821 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003822
3823 /* VPP-TBD */
3824 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003825 session->rx_fifo ?
3826 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003827 vcm->cfg.rx_fifo_size);
3828 *buflen = sizeof (u32);
3829
Florin Coras5e062572019-03-14 19:07:51 -07003830 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3831 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 }
3833 else
3834 rv = VPPCOM_EINVAL;
3835 break;
3836
3837 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3838 if (buffer && buflen && (*buflen == sizeof (u32)))
3839 {
3840 /* VPP-TBD */
3841 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003842 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3843 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3844 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003845 }
3846 else
3847 rv = VPPCOM_EINVAL;
3848 break;
3849
3850 case VPPCOM_ATTR_GET_REUSEADDR:
3851 if (buffer && buflen && (*buflen >= sizeof (int)))
3852 {
3853 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003854 *(int *) buffer = vcl_session_has_attr (session,
3855 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003856 *buflen = sizeof (int);
3857
Florin Coras5e062572019-03-14 19:07:51 -07003858 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3859 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003860 }
3861 else
3862 rv = VPPCOM_EINVAL;
3863 break;
3864
Stevenb5a11602017-10-11 09:59:30 -07003865 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003866 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003867 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003868 {
3869 /* VPP-TBD */
3870 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003871 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003872 else
Florin Corasac422d62020-10-19 20:51:36 -07003873 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003874
Florin Coras5e062572019-03-14 19:07:51 -07003875 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003876 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003877 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003878 }
3879 else
3880 rv = VPPCOM_EINVAL;
3881 break;
3882
3883 case VPPCOM_ATTR_GET_REUSEPORT:
3884 if (buffer && buflen && (*buflen >= sizeof (int)))
3885 {
3886 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003887 *(int *) buffer = vcl_session_has_attr (session,
3888 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003889 *buflen = sizeof (int);
3890
Florin Coras5e062572019-03-14 19:07:51 -07003891 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3892 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003893 }
3894 else
3895 rv = VPPCOM_EINVAL;
3896 break;
3897
3898 case VPPCOM_ATTR_SET_REUSEPORT:
3899 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003900 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003901 {
3902 /* VPP-TBD */
3903 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003904 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003905 else
Florin Corasac422d62020-10-19 20:51:36 -07003906 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003907
Florin Coras5e062572019-03-14 19:07:51 -07003908 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003909 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003910 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003911 }
3912 else
3913 rv = VPPCOM_EINVAL;
3914 break;
3915
3916 case VPPCOM_ATTR_GET_BROADCAST:
3917 if (buffer && buflen && (*buflen >= sizeof (int)))
3918 {
3919 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003920 *(int *) buffer = vcl_session_has_attr (session,
3921 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003922 *buflen = sizeof (int);
3923
Florin Coras5e062572019-03-14 19:07:51 -07003924 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3925 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003926 }
3927 else
3928 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003929 break;
3930
3931 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003932 if (buffer && buflen && (*buflen == sizeof (int)))
3933 {
3934 /* VPP-TBD */
3935 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003936 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003937 else
Florin Corasac422d62020-10-19 20:51:36 -07003938 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003939
Florin Coras5e062572019-03-14 19:07:51 -07003940 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003941 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003942 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003943 }
3944 else
3945 rv = VPPCOM_EINVAL;
3946 break;
3947
3948 case VPPCOM_ATTR_GET_V6ONLY:
3949 if (buffer && buflen && (*buflen >= sizeof (int)))
3950 {
3951 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003952 *(int *) buffer = vcl_session_has_attr (session,
3953 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003954 *buflen = sizeof (int);
3955
Florin Coras5e062572019-03-14 19:07:51 -07003956 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3957 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003958 }
3959 else
3960 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003961 break;
3962
3963 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003964 if (buffer && buflen && (*buflen == sizeof (int)))
3965 {
3966 /* VPP-TBD */
3967 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003968 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003969 else
Florin Corasac422d62020-10-19 20:51:36 -07003970 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003971
Florin Coras5e062572019-03-14 19:07:51 -07003972 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003973 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003974 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003975 }
3976 else
3977 rv = VPPCOM_EINVAL;
3978 break;
3979
3980 case VPPCOM_ATTR_GET_KEEPALIVE:
3981 if (buffer && buflen && (*buflen >= sizeof (int)))
3982 {
3983 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003984 *(int *) buffer = vcl_session_has_attr (session,
3985 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003986 *buflen = sizeof (int);
3987
Florin Coras5e062572019-03-14 19:07:51 -07003988 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3989 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003990 }
3991 else
3992 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003993 break;
Stevenbccd3392017-10-12 20:42:21 -07003994
3995 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003996 if (buffer && buflen && (*buflen == sizeof (int)))
3997 {
3998 /* VPP-TBD */
3999 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004000 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004001 else
Florin Corasac422d62020-10-19 20:51:36 -07004002 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004003
Florin Coras5e062572019-03-14 19:07:51 -07004004 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004005 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07004006 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004007 }
4008 else
4009 rv = VPPCOM_EINVAL;
4010 break;
4011
4012 case VPPCOM_ATTR_GET_TCP_NODELAY:
4013 if (buffer && buflen && (*buflen >= sizeof (int)))
4014 {
4015 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004016 *(int *) buffer = vcl_session_has_attr (session,
4017 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004018 *buflen = sizeof (int);
4019
Florin Coras5e062572019-03-14 19:07:51 -07004020 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
4021 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004022 }
4023 else
4024 rv = VPPCOM_EINVAL;
4025 break;
4026
4027 case VPPCOM_ATTR_SET_TCP_NODELAY:
4028 if (buffer && buflen && (*buflen == sizeof (int)))
4029 {
4030 /* VPP-TBD */
4031 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004032 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004033 else
Florin Corasac422d62020-10-19 20:51:36 -07004034 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05004035
Florin Coras5e062572019-03-14 19:07:51 -07004036 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004037 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07004038 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004039 }
4040 else
4041 rv = VPPCOM_EINVAL;
4042 break;
4043
4044 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
4045 if (buffer && buflen && (*buflen >= sizeof (int)))
4046 {
4047 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004048 *(int *) buffer = vcl_session_has_attr (session,
4049 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004050 *buflen = sizeof (int);
4051
Florin Coras5e062572019-03-14 19:07:51 -07004052 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
4053 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004054 }
4055 else
4056 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004057 break;
4058
4059 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05004060 if (buffer && buflen && (*buflen == sizeof (int)))
4061 {
4062 /* VPP-TBD */
4063 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004064 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004065 else
Florin Corasac422d62020-10-19 20:51:36 -07004066 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004067
Florin Coras5e062572019-03-14 19:07:51 -07004068 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004069 vcl_session_has_attr (session,
4070 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004071 }
4072 else
4073 rv = VPPCOM_EINVAL;
4074 break;
4075
4076 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
4077 if (buffer && buflen && (*buflen >= sizeof (int)))
4078 {
4079 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004080 *(int *) buffer = vcl_session_has_attr (session,
4081 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004082 *buflen = sizeof (int);
4083
Florin Coras5e062572019-03-14 19:07:51 -07004084 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
4085 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004086 }
4087 else
4088 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004089 break;
4090
4091 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05004092 if (buffer && buflen && (*buflen == sizeof (int)))
4093 {
4094 /* VPP-TBD */
4095 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004096 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004097 else
Florin Corasac422d62020-10-19 20:51:36 -07004098 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004099
Florin Coras5e062572019-03-14 19:07:51 -07004100 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004101 vcl_session_has_attr (session,
4102 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004103 }
4104 else
4105 rv = VPPCOM_EINVAL;
4106 break;
4107
4108 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004109 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004110 {
Florin Coras04ae8272021-04-12 19:55:37 -07004111 rv = VPPCOM_EINVAL;
4112 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004113 }
Florin Coras04ae8272021-04-12 19:55:37 -07004114
4115 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4116 tea.mss = *(u32 *) buffer;
4117 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
4118 rv = VPPCOM_ENOPROTOOPT;
4119
4120 if (!rv)
4121 {
4122 *(u32 *) buffer = tea.mss;
4123 *buflen = sizeof (int);
4124 }
4125
4126 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4127 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004128 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004129 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004130 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004131 {
Florin Coras04ae8272021-04-12 19:55:37 -07004132 rv = VPPCOM_EINVAL;
4133 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004134 }
Florin Coras04ae8272021-04-12 19:55:37 -07004135
4136 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4137 tea.mss = *(u32 *) buffer;
4138 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4139 rv = VPPCOM_ENOPROTOOPT;
4140
4141 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4142 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004143 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004144
Florin Coras1e966172020-05-16 18:18:14 +00004145 case VPPCOM_ATTR_SET_CONNECTED:
4146 session->flags |= VCL_SESSION_F_CONNECTED;
4147 break;
4148
Florin Corasa5a9efd2021-01-05 17:03:29 -08004149 case VPPCOM_ATTR_SET_CKPAIR:
4150 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4151 !vcl_session_has_crypto (session))
4152 {
4153 rv = VPPCOM_EINVAL;
4154 break;
4155 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004156 if (!session->ext_config)
4157 {
Florin Coras87f63892021-05-01 16:01:40 -07004158 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4159 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004160 }
4161 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4162 {
4163 rv = VPPCOM_EINVAL;
4164 break;
4165 }
4166
4167 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004168 break;
4169
Florin Coras6a6555a2021-01-28 11:39:27 -08004170 case VPPCOM_ATTR_SET_VRF:
4171 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4172 {
4173 rv = VPPCOM_EINVAL;
4174 break;
4175 }
4176 session->vrf = *(u32 *) buffer;
4177 break;
4178
4179 case VPPCOM_ATTR_GET_VRF:
4180 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4181 {
4182 rv = VPPCOM_EINVAL;
4183 break;
4184 }
4185 *(u32 *) buffer = session->vrf;
4186 *buflen = sizeof (u32);
4187 break;
4188
wanghanlin0674f852021-02-22 10:38:36 +08004189 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004190 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004191 {
Florin Corasd77325c2021-02-23 12:03:03 -08004192 rv = VPPCOM_EINVAL;
4193 break;
wanghanlin0674f852021-02-22 10:38:36 +08004194 }
Florin Corasd77325c2021-02-23 12:03:03 -08004195
4196 if (session->transport.is_ip4)
4197 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004198 else
Florin Corasd77325c2021-02-23 12:03:03 -08004199 *(int *) buffer = AF_INET6;
4200 *buflen = sizeof (int);
4201
wanghanlin0674f852021-02-22 10:38:36 +08004202 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4203 *buflen);
4204 break;
4205
Florin Coras87f63892021-05-01 16:01:40 -07004206 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4207 if (!(buffer && buflen && (*buflen > 0)))
4208 {
4209 rv = VPPCOM_EINVAL;
4210 break;
4211 }
4212 if (session->ext_config)
4213 {
4214 rv = VPPCOM_EINVAL;
4215 break;
4216 }
4217 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4218 *buflen + sizeof (u32));
4219 clib_memcpy (session->ext_config->data, buffer, *buflen);
4220 session->ext_config->len = *buflen;
4221 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004222 case VPPCOM_ATTR_SET_IP_PKTINFO:
4223 if (buffer && buflen && (*buflen == sizeof (int)) &&
4224 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4225 {
4226 if (*(int *) buffer)
4227 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4228 else
4229 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4230
4231 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4232 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4233 *buflen);
4234 }
4235 else
4236 rv = VPPCOM_EINVAL;
4237 break;
4238
4239 case VPPCOM_ATTR_GET_IP_PKTINFO:
4240 if (buffer && buflen && (*buflen >= sizeof (int)))
4241 {
4242 *(int *) buffer =
4243 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4244 *buflen = sizeof (int);
4245
4246 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4247 *buflen);
4248 }
4249 else
4250 rv = VPPCOM_EINVAL;
4251 break;
Florin Coras87f63892021-05-01 16:01:40 -07004252
Dave Wallacee22aa742017-10-20 12:30:38 -04004253 default:
4254 rv = VPPCOM_EINVAL;
4255 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004256 }
4257
Dave Wallace35830af2017-10-09 01:43:42 -04004258 return rv;
4259}
4260
Stevenac1f96d2017-10-24 16:03:58 -07004261int
Florin Coras134a9962018-08-28 11:32:04 -07004262vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004263 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4264{
Florin Coras134a9962018-08-28 11:32:04 -07004265 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004266 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004267 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004268
4269 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004270 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004271 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004272 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004273 else
4274 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004275 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004276 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004277 }
4278
Florin Coras0a1e1832020-03-29 18:54:04 +00004279 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004280 {
Florin Coras5da10c42020-04-01 04:31:21 +00004281 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004282 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004283 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4284 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004285 else
Dave Barach178cf492018-11-13 16:34:13 -05004286 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4287 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004288 ep->is_ip4 = session->transport.is_ip4;
4289 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004290 }
Florin Coras460dce62018-07-27 05:45:06 -07004291
Stevenac1f96d2017-10-24 16:03:58 -07004292 return rv;
4293}
4294
Florin Coraseff5f7a2023-02-07 17:36:17 -08004295static void
4296vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4297{
4298 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4299
4300 do
4301 {
4302 switch (tlv->data_type)
4303 {
4304 case VCL_UDP_SEGMENT:
4305 s->gso_size = *(u16 *) tlv->data;
4306 break;
4307 case VCL_IP_PKTINFO:
4308 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4309 sizeof (ip4_address_t));
4310 break;
4311 default:
4312 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4313 break;
4314 }
4315 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4316 }
4317 while (tlv);
4318}
4319
Stevenac1f96d2017-10-24 16:03:58 -07004320int
Florin Coras134a9962018-08-28 11:32:04 -07004321vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004322 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4323{
Florin Coras7a2abce2020-04-05 19:25:44 +00004324 vcl_worker_t *wrk = vcl_worker_get_current ();
4325 vcl_session_t *s;
4326
4327 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004328 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004329 return VPPCOM_EBADFD;
4330
Dave Wallace617dffa2017-10-26 14:47:06 -04004331 if (ep)
4332 {
Florin Coras5824cc52020-10-20 18:44:41 -07004333 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004334 return VPPCOM_EINVAL;
4335
liuyacanbc0c7542021-08-02 20:15:05 +08004336 s->transport.is_ip4 = ep->is_ip4;
4337 s->transport.rmt_port = ep->port;
4338 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4339
Florin Coraseff5f7a2023-02-07 17:36:17 -08004340 if (ep->app_tlvs)
4341 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004342
Florin Coras5da10c42020-04-01 04:31:21 +00004343 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004344 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004345 {
Florin Coras5824cc52020-10-20 18:44:41 -07004346 u32 session_index = s->session_index;
4347 f64 timeout = vcm->cfg.session_timeout;
4348 int rv;
4349
Florin Coras5da10c42020-04-01 04:31:21 +00004350 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004351 rv = vppcom_wait_for_session_state_change (session_index,
4352 VCL_STATE_READY,
4353 timeout);
4354 if (rv < 0)
4355 return rv;
4356 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004357 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004358 }
4359
4360 if (flags)
4361 {
4362 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004363 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004364 }
4365
Florin Coraseff5f7a2023-02-07 17:36:17 -08004366 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004367 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004368}
4369
Dave Wallace048b1d62018-01-03 22:24:41 -05004370int
4371vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4372{
Florin Coras134a9962018-08-28 11:32:04 -07004373 vcl_worker_t *wrk = vcl_worker_get_current ();
4374 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004375 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004376 svm_msg_q_msg_t msg;
4377 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004378 int rv, num_ev = 0;
4379
Florin Coras5e062572019-03-14 19:07:51 -07004380 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004381
4382 if (!vp)
4383 return VPPCOM_EFAULT;
4384
4385 do
4386 {
Florin Coras7e12d942018-06-27 14:32:43 -07004387 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004388
Florin Coras6917b942018-11-13 22:44:54 -08004389 /* Dequeue all events and drop all unhandled io events */
4390 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4391 {
4392 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4393 vcl_handle_mq_event (wrk, e);
4394 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4395 }
4396 vec_reset_length (wrk->unhandled_evts_vector);
4397
Dave Wallace048b1d62018-01-03 22:24:41 -05004398 for (i = 0; i < n_sids; i++)
4399 {
Florin Coras7baeb712019-01-04 17:05:43 -08004400 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004401 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004402 {
4403 vp[i].revents = POLLHUP;
4404 num_ev++;
4405 continue;
4406 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004407
Florin Coras6917b942018-11-13 22:44:54 -08004408 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004409
4410 if (POLLIN & vp[i].events)
4411 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004412 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004413 if (rv > 0)
4414 {
Florin Coras6917b942018-11-13 22:44:54 -08004415 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004416 num_ev++;
4417 }
4418 else if (rv < 0)
4419 {
4420 switch (rv)
4421 {
4422 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004423 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004424 break;
4425
4426 default:
Florin Coras6917b942018-11-13 22:44:54 -08004427 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004428 break;
4429 }
4430 num_ev++;
4431 }
4432 }
4433
4434 if (POLLOUT & vp[i].events)
4435 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004436 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004437 if (rv > 0)
4438 {
Florin Coras6917b942018-11-13 22:44:54 -08004439 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004440 num_ev++;
4441 }
4442 else if (rv < 0)
4443 {
4444 switch (rv)
4445 {
4446 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004447 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004448 break;
4449
4450 default:
Florin Coras6917b942018-11-13 22:44:54 -08004451 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004452 break;
4453 }
4454 num_ev++;
4455 }
4456 }
4457
Dave Wallace7e607a72018-06-18 18:41:32 -04004458 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004459 {
Florin Coras6917b942018-11-13 22:44:54 -08004460 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004461 num_ev++;
4462 }
4463 }
4464 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004465 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004466 }
4467 while ((num_ev == 0) && keep_trying);
4468
Dave Wallace048b1d62018-01-03 22:24:41 -05004469 return num_ev;
4470}
4471
Florin Coras99368312018-08-02 10:45:44 -07004472int
4473vppcom_mq_epoll_fd (void)
4474{
Florin Coras134a9962018-08-28 11:32:04 -07004475 vcl_worker_t *wrk = vcl_worker_get_current ();
4476 return wrk->mqs_epfd;
4477}
4478
4479int
Florin Coras30e79c22019-01-02 19:31:22 -08004480vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004481{
4482 return session_handle & 0xFFFFFF;
4483}
4484
4485int
Florin Coras30e79c22019-01-02 19:31:22 -08004486vppcom_session_worker (vcl_session_handle_t session_handle)
4487{
4488 return session_handle >> 24;
4489}
4490
4491int
Florin Coras134a9962018-08-28 11:32:04 -07004492vppcom_worker_register (void)
4493{
Florin Coras47c40e22018-11-26 17:01:36 -08004494 if (!vcl_worker_alloc_and_init ())
4495 return VPPCOM_EEXIST;
4496
Florin Coras47c40e22018-11-26 17:01:36 -08004497 if (vcl_worker_register_with_vpp ())
4498 return VPPCOM_EEXIST;
4499
4500 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004501}
4502
Florin Coras369db832019-07-08 12:34:45 -07004503void
4504vppcom_worker_unregister (void)
4505{
4506 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4507 vcl_set_worker_index (~0);
4508}
4509
Pivo6017ff02020-05-04 17:57:33 +02004510void
4511vppcom_worker_index_set (int index)
4512{
4513 vcl_set_worker_index (index);
4514}
4515
Florin Corasdfe4cf42018-11-28 22:13:45 -08004516int
4517vppcom_worker_index (void)
4518{
4519 return vcl_get_worker_index ();
4520}
4521
Florin Corase0982e52019-01-25 13:19:56 -08004522int
4523vppcom_worker_mqs_epfd (void)
4524{
4525 vcl_worker_t *wrk = vcl_worker_get_current ();
4526 if (!vcm->cfg.use_mq_eventfd)
4527 return -1;
4528 return wrk->mqs_epfd;
4529}
4530
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004531int
4532vppcom_session_is_connectable_listener (uint32_t session_handle)
4533{
4534 vcl_session_t *session;
4535 vcl_worker_t *wrk = vcl_worker_get_current ();
4536 session = vcl_session_get_w_handle (wrk, session_handle);
4537 if (!session)
4538 return VPPCOM_EBADFD;
4539 return vcl_session_is_connectable_listener (wrk, session);
4540}
4541
4542int
4543vppcom_session_listener (uint32_t session_handle)
4544{
4545 vcl_worker_t *wrk = vcl_worker_get_current ();
4546 vcl_session_t *listen_session, *session;
4547 session = vcl_session_get_w_handle (wrk, session_handle);
4548 if (!session)
4549 return VPPCOM_EBADFD;
4550 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4551 return VPPCOM_EBADFD;
4552 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4553 if (!listen_session)
4554 return VPPCOM_EBADFD;
4555 return vcl_session_handle (listen_session);
4556}
4557
4558int
4559vppcom_session_n_accepted (uint32_t session_handle)
4560{
4561 vcl_worker_t *wrk = vcl_worker_get_current ();
4562 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4563 if (!session)
4564 return VPPCOM_EBADFD;
4565 return session->n_accepted_sessions;
4566}
4567
Florin Coras66ec4672020-06-15 07:59:40 -07004568const char *
4569vppcom_proto_str (vppcom_proto_t proto)
4570{
4571 char const *proto_str;
4572
4573 switch (proto)
4574 {
4575 case VPPCOM_PROTO_TCP:
4576 proto_str = "TCP";
4577 break;
4578 case VPPCOM_PROTO_UDP:
4579 proto_str = "UDP";
4580 break;
4581 case VPPCOM_PROTO_TLS:
4582 proto_str = "TLS";
4583 break;
4584 case VPPCOM_PROTO_QUIC:
4585 proto_str = "QUIC";
4586 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004587 case VPPCOM_PROTO_DTLS:
4588 proto_str = "DTLS";
4589 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004590 case VPPCOM_PROTO_SRTP:
4591 proto_str = "SRTP";
4592 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004593 default:
4594 proto_str = "UNKNOWN";
4595 break;
4596 }
4597 return proto_str;
4598}
4599
4600const char *
4601vppcom_retval_str (int retval)
4602{
4603 char const *st;
4604
4605 switch (retval)
4606 {
4607 case VPPCOM_OK:
4608 st = "VPPCOM_OK";
4609 break;
4610
4611 case VPPCOM_EAGAIN:
4612 st = "VPPCOM_EAGAIN";
4613 break;
4614
4615 case VPPCOM_EFAULT:
4616 st = "VPPCOM_EFAULT";
4617 break;
4618
4619 case VPPCOM_ENOMEM:
4620 st = "VPPCOM_ENOMEM";
4621 break;
4622
4623 case VPPCOM_EINVAL:
4624 st = "VPPCOM_EINVAL";
4625 break;
4626
4627 case VPPCOM_EBADFD:
4628 st = "VPPCOM_EBADFD";
4629 break;
4630
4631 case VPPCOM_EAFNOSUPPORT:
4632 st = "VPPCOM_EAFNOSUPPORT";
4633 break;
4634
4635 case VPPCOM_ECONNABORTED:
4636 st = "VPPCOM_ECONNABORTED";
4637 break;
4638
4639 case VPPCOM_ECONNRESET:
4640 st = "VPPCOM_ECONNRESET";
4641 break;
4642
4643 case VPPCOM_ENOTCONN:
4644 st = "VPPCOM_ENOTCONN";
4645 break;
4646
4647 case VPPCOM_ECONNREFUSED:
4648 st = "VPPCOM_ECONNREFUSED";
4649 break;
4650
4651 case VPPCOM_ETIMEDOUT:
4652 st = "VPPCOM_ETIMEDOUT";
4653 break;
4654
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304655 case VPPCOM_EADDRINUSE:
4656 st = "VPPCOM_EADDRINUSE";
4657 break;
4658
Florin Coras66ec4672020-06-15 07:59:40 -07004659 default:
4660 st = "UNKNOWN_STATE";
4661 break;
4662 }
4663
4664 return st;
4665}
4666
Florin Corasa5a9efd2021-01-05 17:03:29 -08004667int
4668vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4669{
4670 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004671 return vcl_sapi_add_cert_key_pair (ckpair);
4672 else
4673 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004674}
4675
4676int
4677vppcom_del_cert_key_pair (uint32_t ckpair_index)
4678{
4679 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004680 return vcl_sapi_del_cert_key_pair (ckpair_index);
4681 else
4682 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004683}
4684
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304685int
4686vppcom_session_get_error (uint32_t session_handle)
4687{
4688 vcl_worker_t *wrk = vcl_worker_get_current ();
4689 vcl_session_t *session = 0;
4690
4691 session = vcl_session_get_w_handle (wrk, session_handle);
4692 if (!session)
4693 return VPPCOM_EBADFD;
4694
4695 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4696 {
4697 VWRN ("epoll session %u! will not have connect", session->session_index);
4698 return VPPCOM_EBADFD;
4699 }
4700
4701 if (session->vpp_error == SESSION_E_PORTINUSE)
4702 return VPPCOM_EADDRINUSE;
4703 else if (session->vpp_error == SESSION_E_REFUSED)
4704 return VPPCOM_ECONNREFUSED;
4705 else if (session->vpp_error != SESSION_E_NONE)
4706 return VPPCOM_EFAULT;
4707 else
4708 return VPPCOM_OK;
4709}
4710
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004711int
4712vppcom_worker_is_detached (void)
4713{
4714 vcl_worker_t *wrk = vcl_worker_get_current ();
4715
4716 if (!vcm->cfg.use_mq_eventfd)
4717 return VPPCOM_ENOTSUP;
4718
4719 return wrk->api_client_handle == ~0;
4720}
4721
Dave Wallacee22aa742017-10-20 12:30:38 -04004722/*
4723 * fd.io coding-style-patch-verification: ON
4724 *
4725 * Local Variables:
4726 * eval: (c-set-style "gnu")
4727 * End:
4728 */