blob: 1791b1be7f89faeadcb56a9d74f00ed3f704439c [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;
Florin Corasb242d312020-10-26 15:35:40 -07001057 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1058 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001059 s->session_state = VCL_STATE_VPP_CLOSING;
1060 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1061 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1062 *ecpy = *e;
1063 ecpy->postponed = 1;
1064 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001065 break;
1066 }
1067 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001068 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001069 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1070 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001071 break;
1072 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001073 reset_msg = (session_reset_msg_t *) e->data;
1074 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1075 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001076 if (s->session_state == VCL_STATE_CLOSED)
1077 break;
Florin Corasb242d312020-10-26 15:35:40 -07001078 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1079 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001080 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1081 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001082 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001083 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1084 *ecpy = *e;
1085 ecpy->postponed = 1;
1086 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001087 break;
1088 }
Florin Coras134a9962018-08-28 11:32:04 -07001089 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001090 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001091 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1092 vcl_session_unlisten_reply_handler (wrk, e->data);
1093 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001094 case SESSION_CTRL_EVT_MIGRATED:
1095 vcl_session_migrated_handler (wrk, e->data);
1096 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001097 case SESSION_CTRL_EVT_CLEANUP:
1098 vcl_session_cleanup_handler (wrk, e->data);
1099 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001100 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1101 vcl_session_req_worker_update_handler (wrk, e->data);
1102 break;
1103 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1104 vcl_session_worker_update_reply_handler (wrk, e->data);
1105 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001106 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1107 vcl_session_app_add_segment_handler (wrk, e->data);
1108 break;
1109 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1110 vcl_session_app_del_segment_handler (wrk, e->data);
1111 break;
hanlina3a48962020-07-13 11:09:15 +08001112 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001113 vcl_worker_rpc_handler (wrk, e->data);
1114 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001115 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1116 vcl_session_transport_attr_reply_handler (wrk, e->data);
1117 break;
Florin Coras54693d22018-07-17 10:46:29 -07001118 default:
1119 clib_warning ("unhandled %u", e->event_type);
1120 }
1121 return VPPCOM_OK;
1122}
1123
Florin Coras30e79c22019-01-02 19:31:22 -08001124static int
Florin Coras697faea2018-06-27 17:10:49 -07001125vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001126 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001127 f64 wait_for_time)
1128{
Florin Coras134a9962018-08-28 11:32:04 -07001129 vcl_worker_t *wrk = vcl_worker_get_current ();
1130 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001131 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001132 svm_msg_q_msg_t msg;
1133 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001134
Florin Coras697faea2018-06-27 17:10:49 -07001135 do
Dave Wallace543852a2017-08-03 02:11:34 -04001136 {
Florin Coras134a9962018-08-28 11:32:04 -07001137 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001138 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001139 {
Florin Coras070453d2018-08-24 17:04:27 -07001140 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001141 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001142 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001143 {
Florin Coras697faea2018-06-27 17:10:49 -07001144 return VPPCOM_OK;
1145 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001146 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001147 {
Florin Coras697faea2018-06-27 17:10:49 -07001148 return VPPCOM_ECONNREFUSED;
1149 }
Florin Coras54693d22018-07-17 10:46:29 -07001150
Florin Coras134a9962018-08-28 11:32:04 -07001151 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001152 {
1153 usleep (100);
1154 continue;
1155 }
Florin Coras134a9962018-08-28 11:32:04 -07001156 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001157 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001158 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001159 }
Florin Coras134a9962018-08-28 11:32:04 -07001160 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001161
Florin Coras05ecfcc2018-12-12 18:19:39 -08001162 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001163 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001164 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001165
Florin Coras697faea2018-06-27 17:10:49 -07001166 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001167}
1168
Florin Coras30e79c22019-01-02 19:31:22 -08001169static void
1170vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1171{
Florin Coras288eaab2019-02-03 15:26:14 -08001172 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001173 vcl_session_t *s;
1174 u32 *sip;
1175
1176 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1177 return;
1178
1179 vec_foreach (sip, wrk->pending_session_wrk_updates)
1180 {
1181 s = vcl_session_get (wrk, *sip);
1182 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1183 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001184 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1185 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001186 s->session_state = state;
1187 }
1188 vec_reset_length (wrk->pending_session_wrk_updates);
1189}
1190
Florin Corasf9240dc2019-01-15 08:03:17 -08001191void
Florin Coras5398dfb2021-01-25 20:31:27 -08001192vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001193{
Florin Coras30e79c22019-01-02 19:31:22 -08001194 svm_msg_q_msg_t *msg;
1195 session_event_t *e;
1196 svm_msg_q_t *mq;
1197 int i;
1198
1199 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001200 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001201
1202 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1203 {
1204 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1205 e = svm_msg_q_msg_data (mq, msg);
1206 vcl_handle_mq_event (wrk, e);
1207 svm_msg_q_free_msg (mq, msg);
1208 }
1209 vec_reset_length (wrk->mq_msg_vector);
1210 vcl_handle_pending_wrk_updates (wrk);
1211}
1212
Florin Coras5398dfb2021-01-25 20:31:27 -08001213void
1214vcl_flush_mq_events (void)
1215{
1216 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1217}
1218
Florin Coras697faea2018-06-27 17:10:49 -07001219static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001220vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001221{
Florin Coras134a9962018-08-28 11:32:04 -07001222 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001223 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001224 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001225 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001226
Florin Corasab2f6db2018-08-31 14:31:41 -07001227 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001228 if (!session)
1229 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001230
Florin Corasaaff5ee2019-07-08 13:00:00 -07001231 /* Flush pending accept events, if any */
1232 while (clib_fifo_elts (session->accept_evts_fifo))
1233 {
1234 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1235 accepted_msg = &evt->accepted_msg;
1236 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1237 vcl_send_session_accepted_reply (session->vpp_evt_q,
1238 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001239 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001240 }
1241 clib_fifo_free (session->accept_evts_fifo);
1242
Florin Coras458089b2019-08-21 16:20:44 -07001243 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001244
Florin Coras32881932023-02-06 13:30:13 -08001245 VDBG (0, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001246 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001247 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001248
1249 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001250 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001251
Florin Coras070453d2018-08-24 17:04:27 -07001252 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001253}
1254
Florin Coras940f78f2018-11-30 12:11:20 -08001255/**
1256 * Handle app exit
1257 *
1258 * Notify vpp of the disconnect and mark the worker as free. If we're the
1259 * last worker, do a full cleanup otherwise, since we're probably a forked
1260 * child, avoid syscalls as much as possible. We might've lost privileges.
1261 */
1262void
1263vppcom_app_exit (void)
1264{
1265 if (!pool_elts (vcm->workers))
1266 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001267 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1268 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001269 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001270}
1271
Florin Coras935ce752020-09-08 22:43:47 -07001272static int
1273vcl_api_attach (void)
1274{
1275 if (vcm->cfg.vpp_app_socket_api)
1276 return vcl_sapi_attach ();
1277
1278 return vcl_bapi_attach ();
1279}
1280
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001281int
1282vcl_is_first_reattach_to_execute ()
1283{
1284 if (vcm->reattach_count == 0)
1285 return 1;
1286
1287 return 0;
1288}
1289
1290void
1291vcl_set_reattach_counter ()
1292{
1293 ++vcm->reattach_count;
1294
1295 if (vcm->reattach_count == vec_len (vcm->workers))
1296 vcm->reattach_count = 0;
1297}
1298
1299/**
1300 * Reattach vcl to vpp after it has previously been disconnected.
1301 *
1302 * The logic should be:
1303 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1304 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1305 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1306 * from `vppcom_worker_register` since they were already allocated.
1307 */
1308
Florin Coras935ce752020-09-08 22:43:47 -07001309static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001310vcl_api_retry_attach (vcl_worker_t *wrk)
1311{
1312 vcl_session_t *s;
1313
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001314 clib_spinlock_lock (&vcm->workers_lock);
1315 if (vcl_is_first_reattach_to_execute ())
1316 {
1317 if (vcl_api_attach ())
1318 {
1319 clib_spinlock_unlock (&vcm->workers_lock);
1320 return;
1321 }
1322 vcl_set_reattach_counter ();
1323 clib_spinlock_unlock (&vcm->workers_lock);
1324 }
1325 else
1326 {
1327 vcl_set_reattach_counter ();
1328 clib_spinlock_unlock (&vcm->workers_lock);
1329 vcl_worker_register_with_vpp ();
1330 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001331
1332 /* Treat listeners as configuration that needs to be re-added to vpp */
1333 pool_foreach (s, wrk->sessions)
1334 {
1335 if (s->flags & VCL_SESSION_F_IS_VEP)
1336 continue;
1337 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1338 vppcom_session_listen (vcl_session_handle (s), 10);
1339 else
1340 VDBG (0, "internal error: unexpected state %d", s->session_state);
1341 }
1342}
1343
1344static void
1345vcl_api_handle_disconnect (vcl_worker_t *wrk)
1346{
1347 wrk->api_client_handle = ~0;
1348 vcl_worker_detach_sessions (wrk);
1349}
1350
1351static void
Florin Coras935ce752020-09-08 22:43:47 -07001352vcl_api_detach (vcl_worker_t * wrk)
1353{
Florin Corasd04ea442022-03-30 16:08:25 -07001354 if (wrk->api_client_handle == ~0)
1355 return;
1356
Florin Coras935ce752020-09-08 22:43:47 -07001357 vcl_send_app_detach (wrk);
1358
1359 if (vcm->cfg.vpp_app_socket_api)
1360 return vcl_sapi_detach (wrk);
1361
1362 return vcl_bapi_disconnect_from_vpp ();
1363}
1364
Dave Wallace543852a2017-08-03 02:11:34 -04001365/*
1366 * VPPCOM Public API functions
1367 */
1368int
Florin Coras66ec4672020-06-15 07:59:40 -07001369vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001370{
Dave Wallace543852a2017-08-03 02:11:34 -04001371 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001372 int rv;
1373
Florin Coras47c40e22018-11-26 17:01:36 -08001374 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001375 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001376 VDBG (1, "already initialized");
1377 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001378 }
1379
Florin Coras47c40e22018-11-26 17:01:36 -08001380 vcm->is_init = 1;
1381 vppcom_cfg (&vcm->cfg);
1382 vcl_cfg = &vcm->cfg;
1383
1384 vcm->main_cpu = pthread_self ();
1385 vcm->main_pid = getpid ();
1386 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001387 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1388 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001389 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1390 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001391 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001392 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001393 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001394
1395 /* Allocate default worker */
1396 vcl_worker_alloc_and_init ();
1397
Florin Coras935ce752020-09-08 22:43:47 -07001398 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001399 {
1400 vppcom_app_destroy ();
1401 return rv;
1402 }
Florin Coras47c40e22018-11-26 17:01:36 -08001403
1404 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001405 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001406
1407 return VPPCOM_OK;
1408}
1409
1410void
1411vppcom_app_destroy (void)
1412{
Florin Corasdc0ded72020-04-30 02:59:55 +00001413 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001414 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001415
Florin Coras940f78f2018-11-30 12:11:20 -08001416 if (!pool_elts (vcm->workers))
1417 return;
1418
Florin Coras0d427d82018-06-27 03:24:07 -07001419 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001420
Florin Corasdc0ded72020-04-30 02:59:55 +00001421 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001422
Florin Corasce815de2020-04-16 18:47:27 +00001423 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001424 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001425 if (current_wrk != wrk)
1426 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001427 }
Florin Corasce815de2020-04-16 18:47:27 +00001428 /* *INDENT-ON* */
1429
Florin Coras935ce752020-09-08 22:43:47 -07001430 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001431 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001432 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001433
Florin Coras0d427d82018-06-27 03:24:07 -07001434 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001435
1436 /*
1437 * Free the heap and fix vcm
1438 */
1439 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001440 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001441
1442 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001443 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001444}
1445
1446int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001447vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001448{
Florin Coras134a9962018-08-28 11:32:04 -07001449 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001450 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001451
Florin Coras134a9962018-08-28 11:32:04 -07001452 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001453
Florin Coras7e12d942018-06-27 14:32:43 -07001454 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001455 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001456 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001457 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301458 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001459
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001460 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001461 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001462
Florin Coras7e12d942018-06-27 14:32:43 -07001463 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1464 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001465
Florin Coras5e062572019-03-14 19:07:51 -07001466 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001467
Florin Coras134a9962018-08-28 11:32:04 -07001468 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001469}
1470
Florin Corasfe286f72021-06-04 10:07:55 -07001471static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001472vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001473{
Florin Corasfa3884f2021-06-22 20:04:46 -07001474 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001475
Florin Corasc2a14172023-02-28 21:13:50 -08001476 ASSERT (s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
1477
Florin Corasfa3884f2021-06-22 20:04:46 -07001478 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001479 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001480 wrk->ep_lt_current = s->session_index;
1481 s->vep.lt_next = s->session_index;
1482 s->vep.lt_prev = s->session_index;
1483 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001484 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001485
1486 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1487 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1488
1489 prev->vep.lt_next = s->session_index;
1490 s->vep.lt_prev = prev->session_index;
1491
1492 s->vep.lt_next = cur->session_index;
1493 cur->vep.lt_prev = s->session_index;
1494}
1495
1496static void
1497vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1498{
1499 vcl_session_t *prev, *next;
1500
Florin Corasc2a14172023-02-28 21:13:50 -08001501 ASSERT (s->vep.lt_next != VCL_INVALID_SESSION_INDEX);
1502
Florin Corasfa3884f2021-06-22 20:04:46 -07001503 if (s->vep.lt_next == s->session_index)
1504 {
1505 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1506 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001507 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfa3884f2021-06-22 20:04:46 -07001508 return;
1509 }
1510
1511 prev = vcl_session_get (wrk, s->vep.lt_prev);
1512 next = vcl_session_get (wrk, s->vep.lt_next);
1513
1514 prev->vep.lt_next = next->session_index;
1515 next->vep.lt_prev = prev->session_index;
1516
1517 if (s->session_index == wrk->ep_lt_current)
1518 wrk->ep_lt_current = s->vep.lt_next;
1519
1520 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001521 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001522}
1523
Dave Wallace543852a2017-08-03 02:11:34 -04001524int
Florin Corasc127d5a2020-10-14 16:35:58 -07001525vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001526 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001527{
Florin Coras134a9962018-08-28 11:32:04 -07001528 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001529
Florin Coras6c3b2182020-10-19 18:36:48 -07001530 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001531
Florin Coras6c3b2182020-10-19 18:36:48 -07001532 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001533 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001534 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001535 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001536 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001537 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001538 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001539 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001540 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1541 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001542 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001543 }
Florin Corase4306b62020-10-28 12:51:10 -07001544 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001545 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001546
Florin Coras6c3b2182020-10-19 18:36:48 -07001547 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001548 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001549 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001550 if (rv < 0)
1551 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001552 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1553 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001554 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001555
Florin Coras9ace36d2019-10-28 13:14:17 -07001556 if (!do_disconnect)
1557 {
1558 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001559 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001560 goto cleanup;
1561 }
1562
Florin Coras6c3b2182020-10-19 18:36:48 -07001563 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001564 {
1565 rv = vppcom_session_unbind (sh);
1566 if (PREDICT_FALSE (rv < 0))
1567 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001568 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001569 vppcom_retval_str (rv));
1570 return rv;
1571 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001572 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001573 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001574 {
1575 rv = vppcom_session_disconnect (sh);
1576 if (PREDICT_FALSE (rv < 0))
1577 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001578 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001579 rv, vppcom_retval_str (rv));
1580 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001581 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001582 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001583 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001584 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001585 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001586 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001587 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001588
1589 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1590 goto free_session;
1591
1592 /* Disconnect/reset messages pending but vpp transport and session
1593 * cleanups already done. Free only after messages drained. */
1594 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001595 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001596
Florin Corasc127d5a2020-10-14 16:35:58 -07001597 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001598
Florin Coras9ace36d2019-10-28 13:14:17 -07001599 /* Session is removed only after vpp confirms the disconnect */
1600 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001601
Florin Corasf9240dc2019-01-15 08:03:17 -08001602cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001603 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001604free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001605 vcl_session_free (wrk, s);
1606 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001607
Dave Wallace543852a2017-08-03 02:11:34 -04001608 return rv;
1609}
1610
1611int
Florin Corasf9240dc2019-01-15 08:03:17 -08001612vppcom_session_close (uint32_t session_handle)
1613{
1614 vcl_worker_t *wrk = vcl_worker_get_current ();
1615 vcl_session_t *session;
1616
1617 session = vcl_session_get_w_handle (wrk, session_handle);
1618 if (!session)
1619 return VPPCOM_EBADFD;
1620 return vcl_session_cleanup (wrk, session, session_handle,
1621 1 /* do_disconnect */ );
1622}
1623
1624int
Florin Coras134a9962018-08-28 11:32:04 -07001625vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001626{
Florin Coras134a9962018-08-28 11:32:04 -07001627 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001628 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001629
1630 if (!ep || !ep->ip)
1631 return VPPCOM_EINVAL;
1632
Florin Coras134a9962018-08-28 11:32:04 -07001633 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001634 if (!session)
1635 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001636
Florin Coras6c3b2182020-10-19 18:36:48 -07001637 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001638 {
Florin Coras5e062572019-03-14 19:07:51 -07001639 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1640 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001641 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001642 }
1643
Florin Coras7e12d942018-06-27 14:32:43 -07001644 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001645 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001646 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1647 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001648 else
Dave Barach178cf492018-11-13 16:34:13 -05001649 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1650 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001651 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001652
Florin Coras60687192021-11-29 21:00:47 -08001653 VDBG (0,
1654 "session %u handle %u: binding to local %s address %U port %u, "
1655 "proto %s",
1656 session->session_index, session_handle,
1657 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1658 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001659 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1660 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001661 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001662 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001663
1664 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001665 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001666
Florin Coras070453d2018-08-24 17:04:27 -07001667 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001668}
1669
1670int
Florin Coras134a9962018-08-28 11:32:04 -07001671vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001672{
Florin Coras134a9962018-08-28 11:32:04 -07001673 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001674 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001675 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001676 int rv;
1677
Florin Coras134a9962018-08-28 11:32:04 -07001678 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001679 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001680 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001681
Dave Wallaceee45d412017-11-24 21:44:06 -05001682 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001683 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001684 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001685 VDBG (0, "session %u [0x%llx]: already in listen state!",
1686 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001687 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001688 }
1689
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001690 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001691
Florin Coras070453d2018-08-24 17:04:27 -07001692 /*
1693 * Send listen request to vpp and wait for reply
1694 */
Florin Coras458089b2019-08-21 16:20:44 -07001695 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001696 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001697 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001698 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001699
Florin Coras070453d2018-08-24 17:04:27 -07001700 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001701 {
Florin Coras134a9962018-08-28 11:32:04 -07001702 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001703 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1704 listen_sh, listen_session->vpp_handle, rv,
1705 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001706 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001707 }
1708
Florin Coras070453d2018-08-24 17:04:27 -07001709 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001710}
1711
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001712int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001713vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1714{
1715 if (!strcmp (proto_str, "TCP"))
1716 *proto = VPPCOM_PROTO_TCP;
1717 else if (!strcmp (proto_str, "tcp"))
1718 *proto = VPPCOM_PROTO_TCP;
1719 else if (!strcmp (proto_str, "UDP"))
1720 *proto = VPPCOM_PROTO_UDP;
1721 else if (!strcmp (proto_str, "udp"))
1722 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001723 else if (!strcmp (proto_str, "TLS"))
1724 *proto = VPPCOM_PROTO_TLS;
1725 else if (!strcmp (proto_str, "tls"))
1726 *proto = VPPCOM_PROTO_TLS;
1727 else if (!strcmp (proto_str, "QUIC"))
1728 *proto = VPPCOM_PROTO_QUIC;
1729 else if (!strcmp (proto_str, "quic"))
1730 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001731 else if (!strcmp (proto_str, "DTLS"))
1732 *proto = VPPCOM_PROTO_DTLS;
1733 else if (!strcmp (proto_str, "dtls"))
1734 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001735 else if (!strcmp (proto_str, "SRTP"))
1736 *proto = VPPCOM_PROTO_SRTP;
1737 else if (!strcmp (proto_str, "srtp"))
1738 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001739 else
1740 return 1;
1741 return 0;
1742}
1743
1744int
Florin Coras32881932023-02-06 13:30:13 -08001745vppcom_session_accept (uint32_t ls_handle, vppcom_endpt_t *ep, uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001746{
Florin Coras32881932023-02-06 13:30:13 -08001747 u32 client_session_index = ~0, ls_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001748 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001749 session_accepted_msg_t accepted_msg;
Florin Coras32881932023-02-06 13:30:13 -08001750 vcl_session_t *ls, *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001751 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001752 u8 is_nonblocking;
Dave Wallace543852a2017-08-03 02:11:34 -04001753
Florin Coras5398dfb2021-01-25 20:31:27 -08001754again:
1755
Florin Coras32881932023-02-06 13:30:13 -08001756 ls = vcl_session_get_w_handle (wrk, ls_handle);
1757 if (!ls)
Florin Coras070453d2018-08-24 17:04:27 -07001758 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001759
Florin Coras32881932023-02-06 13:30:13 -08001760 if ((ls->session_state != VCL_STATE_LISTEN) &&
1761 (ls->session_state != VCL_STATE_LISTEN_NO_MQ) &&
1762 (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001763 {
Florin Coras32881932023-02-06 13:30:13 -08001764 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state (%s)",
1765 ls->vpp_handle, vcl_session_state_str (ls->session_state));
1766 return VPPCOM_EBADFD;
1767 }
1768
1769 ls_index = ls->session_index;
1770
1771 if (clib_fifo_elts (ls->accept_evts_fifo))
1772 {
1773 clib_fifo_sub2 (ls->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001774 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001775 accepted_msg = evt->accepted_msg;
1776 goto handle;
1777 }
1778
Florin Coras32881932023-02-06 13:30:13 -08001779 is_nonblocking = vcl_session_has_attr (ls, VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001780 while (1)
1781 {
Carl Smith592a9092019-11-12 14:57:37 +13001782 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1783 return VPPCOM_EAGAIN;
1784
Florin Coras5398dfb2021-01-25 20:31:27 -08001785 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1786 vcl_worker_flush_mq_events (wrk);
1787 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001788 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001789
Florin Coras54693d22018-07-17 10:46:29 -07001790handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001791
Florin Coras32881932023-02-06 13:30:13 -08001792 client_session_index =
1793 vcl_session_accepted_handler (wrk, &accepted_msg, ls_index);
Florin Coras00cca802019-06-06 09:38:44 -07001794 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1795 return VPPCOM_ECONNABORTED;
1796
Florin Coras32881932023-02-06 13:30:13 -08001797 ls = vcl_session_get (wrk, ls_index);
Florin Coras134a9962018-08-28 11:32:04 -07001798 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001799
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001800 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001801 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001802
Florin Coras32881932023-02-06 13:30:13 -08001803 VDBG (1,
1804 "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1805 " flags %d, is_nonblocking %u",
1806 ls->session_index, ls->vpp_handle, client_session_index,
Florin Coras5e062572019-03-14 19:07:51 -07001807 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001808 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001809
Dave Wallace048b1d62018-01-03 22:24:41 -05001810 if (ep)
1811 {
Florin Coras7e12d942018-06-27 14:32:43 -07001812 ep->is_ip4 = client_session->transport.is_ip4;
1813 ep->port = client_session->transport.rmt_port;
1814 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001815 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1816 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001817 else
Dave Barach178cf492018-11-13 16:34:13 -05001818 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1819 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001820 }
Dave Wallace60caa062017-11-10 17:07:13 -05001821
Florin Coras60687192021-11-29 21:00:47 -08001822 VDBG (0,
1823 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1824 "local: %U:%u",
Florin Coras32881932023-02-06 13:30:13 -08001825 ls_handle, ls->vpp_handle, client_session_index,
1826 client_session->vpp_handle, vcl_format_ip46_address,
1827 &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001828 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001829 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001830 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001831 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001832 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras32881932023-02-06 13:30:13 -08001833 vcl_evt (VCL_EVT_ACCEPT, client_session, ls, client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001834
Florin Coras3c7d4f92018-12-14 11:28:43 -08001835 /*
1836 * Session might have been closed already
1837 */
1838 if (accept_flags)
1839 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001840 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001841 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001842 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001843 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001844 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001845 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001846}
1847
1848int
Florin Coras134a9962018-08-28 11:32:04 -07001849vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001850{
Florin Coras134a9962018-08-28 11:32:04 -07001851 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001852 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001853 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001854 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001855
Florin Coras134a9962018-08-28 11:32:04 -07001856 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001857 if (!session)
1858 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001859 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001860
Florin Coras6c3b2182020-10-19 18:36:48 -07001861 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001862 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001863 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001864 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001865 }
1866
Florin Corasc127d5a2020-10-14 16:35:58 -07001867 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001868 {
Florin Coras60687192021-11-29 21:00:47 -08001869 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001870 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1871 " state (%s)",
1872 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001873 vcl_format_ip46_address, &session->transport.rmt_ip,
1874 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001875 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001876 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001877 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001878 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001879 }
1880
Florin Coras0a1e1832020-03-29 18:54:04 +00001881 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001882 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001883 {
1884 if (session->session_type != VPPCOM_PROTO_UDP)
1885 return VPPCOM_EINVAL;
1886 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001887 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001888 }
1889
Florin Coras7e12d942018-06-27 14:32:43 -07001890 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001891 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001892 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001893 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001894 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001895
Florin Coras5ffb9642021-12-03 17:24:13 -08001896 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1897 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001898 &session->transport.rmt_ip,
1899 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001900 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001901 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001902
Florin Coras458089b2019-08-21 16:20:44 -07001903 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001904
Florin Corasac422d62020-10-19 20:51:36 -07001905 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001906 {
fanyfa49d59d2020-10-13 17:07:16 +08001907 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001908 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001909 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001910 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001911 return VPPCOM_EINPROGRESS;
1912 }
Florin Coras57c88932019-08-29 12:03:17 -07001913
1914 /*
1915 * Wait for reply from vpp if blocking
1916 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001917 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001918 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001919
Florin Coras134a9962018-08-28 11:32:04 -07001920 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001921 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1922 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001923
Dave Wallace4878cbe2017-11-21 03:45:09 -05001924 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001925}
1926
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001927int
1928vppcom_session_stream_connect (uint32_t session_handle,
1929 uint32_t parent_session_handle)
1930{
1931 vcl_worker_t *wrk = vcl_worker_get_current ();
1932 vcl_session_t *session, *parent_session;
1933 u32 session_index, parent_session_index;
1934 int rv;
1935
1936 session = vcl_session_get_w_handle (wrk, session_handle);
1937 if (!session)
1938 return VPPCOM_EBADFD;
1939 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1940 if (!parent_session)
1941 return VPPCOM_EBADFD;
1942
1943 session_index = session->session_index;
1944 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001945 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001946 {
1947 VDBG (0, "ERROR: cannot connect epoll session %u!",
1948 session->session_index);
1949 return VPPCOM_EBADFD;
1950 }
1951
Florin Corasc127d5a2020-10-14 16:35:58 -07001952 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001953 {
Florin Coras60687192021-11-29 21:00:47 -08001954 VDBG (0,
1955 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001956 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001957 session_handle, session->vpp_handle, parent_session_handle,
1958 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001959 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001960 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001961 return VPPCOM_OK;
1962 }
1963
1964 /* Connect to quic session specifics */
1965 session->transport.is_ip4 = parent_session->transport.is_ip4;
1966 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1967 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001968 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001969
1970 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1971 session_handle, parent_session_handle, parent_session->vpp_handle);
1972
1973 /*
1974 * Send connect request and wait for reply from vpp
1975 */
Florin Coras458089b2019-08-21 16:20:44 -07001976 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001977 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001978 vcm->cfg.session_timeout);
1979
1980 session->listener_index = parent_session_index;
1981 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001982 if (parent_session)
1983 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001984
1985 session = vcl_session_get (wrk, session_index);
1986 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1987 session->vpp_handle, rv ? "failed" : "succeeded");
1988
1989 return rv;
1990}
1991
Steven58f464e2017-10-25 12:33:12 -07001992static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001993vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001994 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001995{
Florin Coras134a9962018-08-28 11:32:04 -07001996 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001997 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001998 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001999 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002000 session_event_t *e;
2001 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07002002 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002003
Florin Coras070453d2018-08-24 17:04:27 -07002004 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08002005 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002006
Florin Coras134a9962018-08-28 11:32:04 -07002007 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002008 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07002009 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002010
Florin Coras6d0106e2019-01-29 20:11:58 -08002011 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002012 {
Florin Coras5e062572019-03-14 19:07:51 -07002013 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002014 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002015 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002016 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002017 }
2018
liuyacan55c952e2021-06-13 14:54:55 +08002019 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2020 {
2021 /* Vpp would ack the incoming data and enqueue it for reading.
2022 * So even if SHUT_RD is set, we can still read() the data if
2023 * the session is ready.
2024 */
2025 if (!vcl_session_read_ready (s))
2026 {
2027 return 0;
2028 }
2029 }
2030
Florin Corasac422d62020-10-19 20:51:36 -07002031 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002032 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002033 mq = wrk->app_event_queue;
2034 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002035 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002036
Sirshak Das28aa5392019-02-05 01:33:33 -06002037 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002038 {
Florin Coras54693d22018-07-17 10:46:29 -07002039 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002040 {
Yu Pingb2955352019-12-04 06:49:04 +08002041 if (vcl_session_is_closing (s))
2042 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002043 if (is_ct)
2044 svm_fifo_unset_event (s->rx_fifo);
2045 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002046 return VPPCOM_EWOULDBLOCK;
2047 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002048 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002049 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002050 if (vcl_session_is_closing (s))
2051 return vcl_session_closing_error (s);
2052
Florin Corasd6894562020-10-28 00:37:15 -07002053 if (is_ct)
2054 svm_fifo_unset_event (s->rx_fifo);
2055 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002056
Florin Coras5398dfb2021-01-25 20:31:27 -08002057 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2058 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002059 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002060 }
Florin Coras54693d22018-07-17 10:46:29 -07002061
Florin Coras4a2c7942020-09-10 12:27:14 -07002062read_again:
2063
Florin Coras460dce62018-07-27 05:45:06 -07002064 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002065 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002066 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002067 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2068
2069 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002070
2071 if (peek)
2072 return rv;
2073
Florin Coras4a2c7942020-09-10 12:27:14 -07002074 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002075
Sirshak Das28aa5392019-02-05 01:33:33 -06002076 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002077 {
Florin Corasd6894562020-10-28 00:37:15 -07002078 if (is_ct)
2079 svm_fifo_unset_event (s->rx_fifo);
2080 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002081 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002082 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002083 {
Florin Corasc34118b2020-08-12 18:58:25 -07002084 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2085 e->event_type = SESSION_IO_EVT_RX;
2086 e->session_index = s->session_index;
2087 }
2088 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002089 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002090 {
2091 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002092 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002093 buf += rv;
2094 n -= rv;
2095 goto read_again;
2096 }
Florin Coras41c9e042018-09-11 00:10:41 -07002097
Florin Coras17ec5772020-08-12 20:46:29 -07002098 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002099 {
Florin Coras17ec5772020-08-12 20:46:29 -07002100 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002101 app_send_io_evt_to_vpp (s->vpp_evt_q,
2102 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002103 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002104 }
2105
Florin Coras5e062572019-03-14 19:07:51 -07002106 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2107 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002108
Florin Coras54693d22018-07-17 10:46:29 -07002109 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002110}
2111
Steven58f464e2017-10-25 12:33:12 -07002112int
Florin Coras134a9962018-08-28 11:32:04 -07002113vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002114{
Florin Coras134a9962018-08-28 11:32:04 -07002115 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002116}
2117
2118static int
Florin Coras134a9962018-08-28 11:32:04 -07002119vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002120{
Florin Coras134a9962018-08-28 11:32:04 -07002121 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002122}
2123
Florin Coras2cba8532018-09-11 16:33:36 -07002124int
2125vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002126 vppcom_data_segment_t * ds, uint32_t n_segments,
2127 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002128{
2129 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002130 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002131 vcl_session_t *s = 0;
2132 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002133 svm_msg_q_t *mq;
2134 u8 is_ct;
2135
2136 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002137 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002138 return VPPCOM_EBADFD;
2139
Florin Coras6d0106e2019-01-29 20:11:58 -08002140 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2141 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002142
Florin Corasac422d62020-10-19 20:51:36 -07002143 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002144 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002145 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002146 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002147 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002148
Sirshak Das28aa5392019-02-05 01:33:33 -06002149 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002150 {
2151 if (is_nonblocking)
2152 {
Florin Coras62877022020-10-28 21:22:04 -07002153 if (is_ct)
2154 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002155 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002156 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002157 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002158 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002159 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002160 if (vcl_session_is_closing (s))
2161 return vcl_session_closing_error (s);
2162
Florin Coras62877022020-10-28 21:22:04 -07002163 if (is_ct)
2164 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002165 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002166
Florin Coras5398dfb2021-01-25 20:31:27 -08002167 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2168 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002169 }
2170 }
2171
Florin Corasd1cc38d2020-10-11 11:05:04 -07002172 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002173 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002174 if (n_read < 0)
2175 return VPPCOM_EAGAIN;
2176
2177 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2178 {
Florin Coras62877022020-10-28 21:22:04 -07002179 if (is_ct)
2180 svm_fifo_unset_event (s->rx_fifo);
2181 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002182 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002183 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002184 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002185 {
2186 session_event_t *e;
2187 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2188 e->event_type = SESSION_IO_EVT_RX;
2189 e->session_index = s->session_index;
2190 }
2191 }
2192
2193 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002194 return n_read;
2195}
2196
2197void
Florin Corasd68faf82020-09-25 15:18:13 -07002198vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002199{
2200 vcl_worker_t *wrk = vcl_worker_get_current ();
2201 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002202 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002203
2204 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002205 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002206 return;
2207
Florin Coras62877022020-10-28 21:22:04 -07002208 is_ct = vcl_session_is_ct (s);
2209 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002210
Florin Coras8cc93212021-09-10 11:37:12 -07002211 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002212 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002213}
2214
Florin Coras7a2abce2020-04-05 19:25:44 +00002215always_inline u8
2216vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002217{
Florin Coras7a2abce2020-04-05 19:25:44 +00002218 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2219 if (is_dgram)
2220 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2221 else
2222 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002223}
2224
2225always_inline int
Dou Chao243a0432022-11-29 19:41:34 +08002226vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002227 size_t n, u8 is_flush, u8 is_dgram)
Florin Coras7a2abce2020-04-05 19:25:44 +00002228{
Florin Coras6d0106e2019-01-29 20:11:58 -08002229 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002230 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002231 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002232 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002233 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002234
Florin Coras0b0d28e2021-06-04 17:31:53 -07002235 /* Accept zero length writes but just return */
2236 if (PREDICT_FALSE (!n))
2237 return VPPCOM_OK;
2238
2239 if (PREDICT_FALSE (!buf))
2240 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002241
Florin Coras6c3b2182020-10-19 18:36:48 -07002242 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002243 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002244 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2245 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002246 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002247 }
2248
liuyacan55c952e2021-06-13 14:54:55 +08002249 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002250 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002251 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2252 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002253 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002254 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002255 }
2256
liuyacan55c952e2021-06-13 14:54:55 +08002257 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2258 {
2259 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2260 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002261 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002262 return VPPCOM_EPIPE;
2263 }
2264
Florin Coras0e88e852018-09-17 22:09:02 -07002265 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002266 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002267 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002268
Florin Coras653e43f2019-03-04 10:56:23 -08002269 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002270 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002271 {
Florin Coras54693d22018-07-17 10:46:29 -07002272 if (is_nonblocking)
2273 {
Florin Coras070453d2018-08-24 17:04:27 -07002274 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002275 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002276 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002277 {
Florin Coras2d379d82019-06-28 12:45:12 -07002278 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002279 if (vcl_session_is_closing (s))
2280 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002281
Florin Coras5398dfb2021-01-25 20:31:27 -08002282 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2283 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002284 }
Dave Wallace543852a2017-08-03 02:11:34 -04002285 }
Dave Wallace543852a2017-08-03 02:11:34 -04002286
Florin Coras653e43f2019-03-04 10:56:23 -08002287 et = SESSION_IO_EVT_TX;
2288 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002289 et = SESSION_IO_EVT_TX_FLUSH;
2290
Florin Coras7a2abce2020-04-05 19:25:44 +00002291 if (is_dgram)
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002292 {
2293 et = vcl_session_dgram_tx_evt (s, et);
2294 n_write =
2295 app_send_dgram_raw_gso (tx_fifo, &s->transport, s->vpp_evt_q, buf, n,
2296 s->gso_size, et, 0 /* do_evt */, SVM_Q_WAIT);
2297 }
Florin Coras460dce62018-07-27 05:45:06 -07002298 else
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002299 {
2300 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
2301 0 /* do_evt */, SVM_Q_WAIT);
2302 }
Florin Coras653e43f2019-03-04 10:56:23 -08002303
Florin Coras14ed6df2019-03-06 21:13:42 -08002304 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002305 app_send_io_evt_to_vpp (
2306 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002307
Florin Coras56230092020-09-02 20:52:58 -07002308 /* The underlying fifo segment can run out of memory */
2309 if (PREDICT_FALSE (n_write < 0))
2310 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002311
Florin Coras6d0106e2019-01-29 20:11:58 -08002312 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2313 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002314
Florin Coras54693d22018-07-17 10:46:29 -07002315 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002316}
2317
Florin Coras42ceddb2018-12-12 10:56:01 -08002318int
2319vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2320{
Florin Coras7a2abce2020-04-05 19:25:44 +00002321 vcl_worker_t *wrk = vcl_worker_get_current ();
2322 vcl_session_t *s;
2323
2324 s = vcl_session_get_w_handle (wrk, session_handle);
2325 if (PREDICT_FALSE (!s))
2326 return VPPCOM_EBADFD;
2327
Florin Coraseff5f7a2023-02-07 17:36:17 -08002328 return vppcom_session_write_inline (wrk, s, buf, n, 0 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002329 s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002330}
2331
Florin Corasb0f662f2018-12-27 14:51:46 -08002332int
2333vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2334{
Florin Coras7a2abce2020-04-05 19:25:44 +00002335 vcl_worker_t *wrk = vcl_worker_get_current ();
2336 vcl_session_t *s;
2337
2338 s = vcl_session_get_w_handle (wrk, session_handle);
2339 if (PREDICT_FALSE (!s))
2340 return VPPCOM_EBADFD;
2341
Florin Coraseff5f7a2023-02-07 17:36:17 -08002342 return vppcom_session_write_inline (wrk, s, buf, n, 1 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002343 s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002344}
2345
Florin Corasc0737e92019-03-04 14:19:39 -08002346#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002347if (PREDICT_FALSE (!_s->rx_fifo)) \
2348 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002349if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2350 { \
2351 if (!vcl_session_is_ct (_s)) \
2352 { \
2353 svm_fifo_unset_event (_s->rx_fifo); \
2354 if (svm_fifo_is_empty (_s->rx_fifo)) \
2355 break; \
2356 } \
2357 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2358 { \
Florin Coras2f630182020-08-13 00:17:51 -07002359 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002360 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2361 break; \
2362 } \
2363 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002364
Florin Coras86f04502018-09-12 16:08:01 -07002365static void
2366vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2367 unsigned long n_bits, unsigned long *read_map,
2368 unsigned long *write_map,
2369 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002370{
2371 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002372 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002373 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002374 u32 sid;
2375
2376 switch (e->event_type)
2377 {
Florin Corasc0737e92019-03-04 14:19:39 -08002378 case SESSION_IO_EVT_RX:
2379 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002380 s = vcl_session_get (wrk, sid);
2381 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002382 break;
Florin Corasb242d312020-10-26 15:35:40 -07002383 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002384 if (sid < n_bits && read_map)
2385 {
David Johnsond9818dd2018-12-14 14:53:41 -05002386 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002387 *bits_set += 1;
2388 }
2389 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002390 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002391 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002392 s = vcl_session_get (wrk, sid);
2393 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002394 break;
2395 if (sid < n_bits && write_map)
2396 {
David Johnsond9818dd2018-12-14 14:53:41 -05002397 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002398 *bits_set += 1;
2399 }
2400 break;
Florin Coras86f04502018-09-12 16:08:01 -07002401 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002402 if (!e->postponed)
2403 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2404 else
2405 s = vcl_session_get (wrk, e->session_index);
2406 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002407 break;
Florin Corasb242d312020-10-26 15:35:40 -07002408 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002409 if (sid < n_bits && read_map)
2410 {
David Johnsond9818dd2018-12-14 14:53:41 -05002411 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002412 *bits_set += 1;
2413 }
2414 break;
2415 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002416 if (!e->postponed)
2417 {
2418 connected_msg = (session_connected_msg_t *) e->data;
2419 sid = vcl_session_connected_handler (wrk, connected_msg);
2420 }
2421 else
2422 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002423 if (sid == VCL_INVALID_SESSION_INDEX)
2424 break;
Florin Coras66c675e2023-03-09 16:43:02 -08002425 if (!(sid < n_bits && write_map))
2426 break;
2427 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2428 *bits_set += 1;
2429 s = vcl_session_get (wrk, sid);
Florin Coras66c675e2023-03-09 16:43:02 -08002430 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07002431 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras86f04502018-09-12 16:08:01 -07002432 break;
2433 case SESSION_CTRL_EVT_DISCONNECTED:
2434 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002435 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2436 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002437 break;
Florin Corasb242d312020-10-26 15:35:40 -07002438 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002439 if (sid < n_bits && except_map)
2440 {
David Johnsond9818dd2018-12-14 14:53:41 -05002441 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002442 *bits_set += 1;
2443 }
2444 break;
2445 case SESSION_CTRL_EVT_RESET:
2446 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2447 if (sid < n_bits && except_map)
2448 {
David Johnsond9818dd2018-12-14 14:53:41 -05002449 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002450 *bits_set += 1;
2451 }
2452 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002453 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2454 vcl_session_unlisten_reply_handler (wrk, e->data);
2455 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002456 case SESSION_CTRL_EVT_MIGRATED:
2457 vcl_session_migrated_handler (wrk, e->data);
2458 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002459 case SESSION_CTRL_EVT_CLEANUP:
2460 vcl_session_cleanup_handler (wrk, e->data);
2461 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002462 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2463 vcl_session_worker_update_reply_handler (wrk, e->data);
2464 break;
2465 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2466 vcl_session_req_worker_update_handler (wrk, e->data);
2467 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002468 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2469 vcl_session_app_add_segment_handler (wrk, e->data);
2470 break;
2471 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2472 vcl_session_app_del_segment_handler (wrk, e->data);
2473 break;
hanlina3a48962020-07-13 11:09:15 +08002474 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002475 vcl_worker_rpc_handler (wrk, e->data);
2476 break;
Florin Coras86f04502018-09-12 16:08:01 -07002477 default:
2478 clib_warning ("unhandled: %u", e->event_type);
2479 break;
2480 }
2481}
2482
2483static int
2484vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2485 unsigned long n_bits, unsigned long *read_map,
2486 unsigned long *write_map, unsigned long *except_map,
2487 double time_to_wait, u32 * bits_set)
2488{
Florin Coras99368312018-08-02 10:45:44 -07002489 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002490 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002491 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002492
Florin Coras54693d22018-07-17 10:46:29 -07002493 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002494 {
Florin Coras54693d22018-07-17 10:46:29 -07002495 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002496 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002497
Florin Coras54693d22018-07-17 10:46:29 -07002498 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002499 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002500 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002501 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002502 else
2503 {
2504 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002505 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002506 }
2507 }
Florin Corase003a1b2019-06-05 10:47:16 -07002508 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002509
Florin Coras134a9962018-08-28 11:32:04 -07002510 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002511 {
Florin Coras134a9962018-08-28 11:32:04 -07002512 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002513 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002514 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2515 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002516 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002517 }
Florin Coras134a9962018-08-28 11:32:04 -07002518 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002519 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002520 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002521}
2522
Florin Coras99368312018-08-02 10:45:44 -07002523static int
Florin Coras294afe22019-01-07 17:49:17 -08002524vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2525 vcl_si_set * read_map, vcl_si_set * write_map,
2526 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002527 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002528{
Florin Coras14ed6df2019-03-06 21:13:42 -08002529 double wait = 0, start = 0;
2530
2531 if (!*bits_set)
2532 {
2533 wait = time_to_wait;
2534 start = clib_time_now (&wrk->clib_time);
2535 }
2536
2537 do
2538 {
2539 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2540 write_map, except_map, wait, bits_set);
2541 if (*bits_set)
2542 return *bits_set;
2543 if (wait == -1)
2544 continue;
2545
2546 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2547 }
2548 while (wait > 0);
2549
2550 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002551}
2552
2553static int
Florin Coras294afe22019-01-07 17:49:17 -08002554vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2555 vcl_si_set * read_map, vcl_si_set * write_map,
2556 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002557 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002558{
2559 vcl_mq_evt_conn_t *mqc;
2560 int __clib_unused n_read;
2561 int n_mq_evts, i;
2562 u64 buf;
2563
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002564 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2565 {
2566 vcl_api_retry_attach (wrk);
2567 return 0;
2568 }
2569
Florin Coras134a9962018-08-28 11:32:04 -07002570 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2571 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2572 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002573 for (i = 0; i < n_mq_evts; i++)
2574 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002575 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2576 {
2577 vcl_api_handle_disconnect (wrk);
2578 continue;
2579 }
2580
Florin Coras134a9962018-08-28 11:32:04 -07002581 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002582 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002583 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002584 except_map, 0, bits_set);
2585 }
2586
2587 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2588}
2589
Dave Wallace543852a2017-08-03 02:11:34 -04002590int
Florin Coras294afe22019-01-07 17:49:17 -08002591vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2592 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002593{
Florin Coras54693d22018-07-17 10:46:29 -07002594 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002595 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002596 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002597 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002598
Dave Wallace7876d392017-10-19 03:53:57 -04002599 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002600 {
Florin Coras134a9962018-08-28 11:32:04 -07002601 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002602 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002603 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2604 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002605 }
Dave Wallace7876d392017-10-19 03:53:57 -04002606 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002607 {
Florin Coras134a9962018-08-28 11:32:04 -07002608 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002609 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002610 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2611 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002612 }
Dave Wallace7876d392017-10-19 03:53:57 -04002613 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002614 {
Florin Coras134a9962018-08-28 11:32:04 -07002615 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002616 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002617 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2618 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002619 }
2620
Florin Coras54693d22018-07-17 10:46:29 -07002621 if (!n_bits)
2622 return 0;
2623
2624 if (!write_map)
2625 goto check_rd;
2626
Florin Coras096a1d52021-01-27 15:33:51 -08002627 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2628 {
2629 if (!(s = vcl_session_get (wrk, sid)))
2630 {
2631 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2632 bits_set++;
2633 continue;
2634 }
Florin Coras54693d22018-07-17 10:46:29 -07002635
Florin Coras096a1d52021-01-27 15:33:51 -08002636 if (vcl_session_write_ready (s))
2637 {
2638 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2639 bits_set++;
2640 }
Florin Coras607eb202023-05-29 16:19:38 -07002641 else
Florin Coras096a1d52021-01-27 15:33:51 -08002642 {
Florin Coras607eb202023-05-29 16:19:38 -07002643 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras096a1d52021-01-27 15:33:51 -08002644 }
2645 }
Florin Coras54693d22018-07-17 10:46:29 -07002646
2647check_rd:
2648 if (!read_map)
2649 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002650
Florin Coras096a1d52021-01-27 15:33:51 -08002651 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2652 {
2653 if (!(s = vcl_session_get (wrk, sid)))
2654 {
2655 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2656 bits_set++;
2657 continue;
2658 }
Florin Coras54693d22018-07-17 10:46:29 -07002659
Florin Coras096a1d52021-01-27 15:33:51 -08002660 if (vcl_session_read_ready (s))
2661 {
2662 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2663 bits_set++;
2664 }
2665 }
Florin Coras54693d22018-07-17 10:46:29 -07002666
2667check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002668
Florin Coras86f04502018-09-12 16:08:01 -07002669 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2670 {
2671 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2672 read_map, write_map, except_map, &bits_set);
2673 }
2674 vec_reset_length (wrk->unhandled_evts_vector);
2675
Florin Coras99368312018-08-02 10:45:44 -07002676 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002677 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002678 time_to_wait, &bits_set);
2679 else
Florin Coras134a9962018-08-28 11:32:04 -07002680 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002681 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002682
Dave Wallace543852a2017-08-03 02:11:34 -04002683 return (bits_set);
2684}
2685
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002687vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002689 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002690 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002691 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002692
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002693 if (VPPCOM_DEBUG <= 3)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002694 return;
2695
Florin Coras6c3b2182020-10-19 18:36:48 -07002696 s = vcl_session_get_w_handle (wrk, vep_handle);
2697 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002698 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002699 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002700 goto done;
2701 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002702 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002703 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002704 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002705 goto done;
2706 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002707 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002708 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002709 "{\n"
2710 " is_vep = %u\n"
2711 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002712 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002713 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2714 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002715
Florin Coras7e5e62b2019-07-30 14:08:23 -07002716 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002717 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002718 s = vcl_session_get_w_handle (wrk, sh);
2719 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002720 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002721 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002722 goto done;
2723 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002724 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002725 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002726 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002727 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002728 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002729 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002730 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002731 goto done;
2732 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002733 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002734 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2735 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002736 sh, s->vep.vep_sh, vep_handle);
2737 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002738 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002739 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002740 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002741 " next_sh = 0x%x (%u)\n"
2742 " prev_sh = 0x%x (%u)\n"
2743 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002744 " ev.events = 0x%x\n"
2745 " ev.data.u64 = 0x%llx\n"
2746 " et_mask = 0x%x\n"
2747 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002748 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002749 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2750 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751 }
2752 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002753
2754done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002755 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002756}
2757
2758int
2759vppcom_epoll_create (void)
2760{
Florin Coras134a9962018-08-28 11:32:04 -07002761 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002762 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002763
Florin Coras134a9962018-08-28 11:32:04 -07002764 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002765
Florin Coras6c3b2182020-10-19 18:36:48 -07002766 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002767 vep_session->vep.vep_sh = ~0;
2768 vep_session->vep.next_sh = ~0;
2769 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002770 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002771
Florin Corasa7a1a222018-12-30 17:11:31 -08002772 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2773 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002774
Florin Corasab2f6db2018-08-31 14:31:41 -07002775 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776}
2777
Florin Corasb0116a12023-02-07 09:11:47 -08002778static void
2779vcl_epoll_ctl_add_unhandled_event (vcl_worker_t *wrk, vcl_session_t *s,
2780 u8 is_epollet, session_evt_type_t evt)
2781{
2782 if (!is_epollet)
2783 {
Florin Corasc2a14172023-02-28 21:13:50 -08002784 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
2785 vcl_epoll_lt_add (wrk, s);
Florin Corasb0116a12023-02-07 09:11:47 -08002786 return;
2787 }
2788
2789 session_event_t e = { 0 };
2790 e.session_index = s->session_index;
2791 e.event_type = evt;
2792 if (evt == SESSION_IO_EVT_RX)
2793 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2794 vec_add1 (wrk->unhandled_evts_vector, e);
2795}
2796
Dave Wallacef7f809c2017-10-03 01:48:42 -04002797int
Florin Coras134a9962018-08-28 11:32:04 -07002798vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002799 struct epoll_event *event)
2800{
Florin Coras134a9962018-08-28 11:32:04 -07002801 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002802 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002803 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002804 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002805
Florin Coras134a9962018-08-28 11:32:04 -07002806 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002807 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002808 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002809 return VPPCOM_EINVAL;
2810 }
2811
Florin Coras134a9962018-08-28 11:32:04 -07002812 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002813 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002814 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002815 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002816 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002817 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002818 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002819 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002820 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002821 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002822 }
2823
Florin Coras134a9962018-08-28 11:32:04 -07002824 ASSERT (vep_session->vep.vep_sh == ~0);
2825 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002826
Florin Coras17ec5772020-08-12 20:46:29 -07002827 s = vcl_session_get_w_handle (wrk, session_handle);
2828 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002829 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002830 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002831 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002832 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002833 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002834 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002835 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002836 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002837 }
2838
2839 switch (op)
2840 {
2841 case EPOLL_CTL_ADD:
2842 if (PREDICT_FALSE (!event))
2843 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002844 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002845 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002846 }
Florin Coras2645f682021-06-04 15:59:41 -07002847 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2848 {
2849 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2850 rv = VPPCOM_EEXIST;
2851 goto done;
2852 }
Florin Coras134a9962018-08-28 11:32:04 -07002853 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002854 {
Florin Coras7e12d942018-06-27 14:32:43 -07002855 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002856 next_session = vcl_session_get_w_handle (wrk,
2857 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002858 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002859 {
Florin Coras5e062572019-03-14 19:07:51 -07002860 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002861 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002862 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002863 }
Florin Coras134a9962018-08-28 11:32:04 -07002864 ASSERT (next_session->vep.prev_sh == vep_handle);
2865 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002866 }
Florin Coras17ec5772020-08-12 20:46:29 -07002867 s->vep.next_sh = vep_session->vep.next_sh;
2868 s->vep.prev_sh = vep_handle;
2869 s->vep.vep_sh = vep_handle;
2870 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002871 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002872 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07002873 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Coras6c3b2182020-10-19 18:36:48 -07002874 s->flags &= ~VCL_SESSION_F_IS_VEP;
2875 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002876 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002877
Florin Coras470d72f2023-06-01 21:50:03 -07002878 if ((event->events & EPOLLOUT))
hanlin475c9d72019-12-26 11:44:28 +08002879 {
Florin Coras470d72f2023-06-01 21:50:03 -07002880 int write_ready = vcl_session_write_ready (s);
2881
2882 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2883 if (write_ready > 0)
2884 {
2885 /* Generate EPOLLOUT if tx fifo not full */
2886 vcl_epoll_ctl_add_unhandled_event (
2887 wrk, s, event->events & EPOLLET, SESSION_IO_EVT_TX);
2888 add_evt = 1;
2889 }
2890 else
2891 {
2892 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
2893 }
hanlin475c9d72019-12-26 11:44:28 +08002894 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002895 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002896 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002897 {
Florin Corasb0116a12023-02-07 09:11:47 -08002898 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2899 SESSION_IO_EVT_RX);
Florin Coras87f76002021-06-28 19:13:29 -07002900 add_evt = 1;
2901 }
2902 if (!add_evt && vcl_session_is_closing (s))
2903 {
2904 session_event_t e = { 0 };
2905 if (s->session_state == VCL_STATE_VPP_CLOSING)
2906 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2907 else
2908 e.event_type = SESSION_CTRL_EVT_RESET;
2909 e.session_index = s->session_index;
2910 e.postponed = 1;
2911 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002912 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002913 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2914 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002915 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002916 break;
2917
2918 case EPOLL_CTL_MOD:
2919 if (PREDICT_FALSE (!event))
2920 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002921 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002922 rv = VPPCOM_EINVAL;
2923 goto done;
2924 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002925 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002926 {
Florin Coras5e062572019-03-14 19:07:51 -07002927 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002928 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002929 goto done;
2930 }
Florin Coras17ec5772020-08-12 20:46:29 -07002931 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002932 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002933 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002934 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002935 rv = VPPCOM_EINVAL;
2936 goto done;
2937 }
hanlin475c9d72019-12-26 11:44:28 +08002938
Florin Coras470d72f2023-06-01 21:50:03 -07002939 /* Generate EPOLLOUT if session write ready and event was not on */
2940 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT))
2941 {
2942 /* Fifo size load acq synchronized with update store rel */
2943 int write_ready = vcl_session_write_ready (s);
2944
2945 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2946 if (write_ready > 0)
2947 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2948 SESSION_IO_EVT_TX);
2949 else
2950 /* Request deq ntf in case dequeue happened while updating flag */
2951 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
2952 }
2953 else if (!(event->events & EPOLLOUT))
Florin Coras607eb202023-05-29 16:19:38 -07002954 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasbc4d5b02023-05-17 23:20:56 -07002955
Florin Coras2645f682021-06-04 15:59:41 -07002956 /* Generate EPOLLIN if session read ready and event was not on */
2957 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2958 (vcl_session_read_ready (s) > 0))
2959 {
Florin Corasb0116a12023-02-07 09:11:47 -08002960 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2961 SESSION_IO_EVT_RX);
Florin Coras2645f682021-06-04 15:59:41 -07002962 }
Florin Coras17ec5772020-08-12 20:46:29 -07002963 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2964 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07002965 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Corasbc4d5b02023-05-17 23:20:56 -07002966
Florin Corasa7a1a222018-12-30 17:11:31 -08002967 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2968 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002969 break;
2970
2971 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002972 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002973 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002974 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002975 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002976 goto done;
2977 }
Florin Coras17ec5772020-08-12 20:46:29 -07002978 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002979 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002980 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002981 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002982 rv = VPPCOM_EINVAL;
2983 goto done;
2984 }
2985
Florin Coras17ec5772020-08-12 20:46:29 -07002986 if (s->vep.prev_sh == vep_handle)
2987 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002988 else
2989 {
Florin Coras7e12d942018-06-27 14:32:43 -07002990 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002991 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002992 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002993 {
Florin Coras5e062572019-03-14 19:07:51 -07002994 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002995 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002996 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002997 }
Florin Coras134a9962018-08-28 11:32:04 -07002998 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002999 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003000 }
Florin Coras17ec5772020-08-12 20:46:29 -07003001 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003002 {
Florin Coras7e12d942018-06-27 14:32:43 -07003003 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07003004 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07003005 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003006 {
Florin Coras5e062572019-03-14 19:07:51 -07003007 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003008 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003009 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003010 }
Florin Coras134a9962018-08-28 11:32:04 -07003011 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003012 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003013 }
3014
Florin Corasfa3884f2021-06-22 20:04:46 -07003015 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
3016 vcl_epoll_lt_del (wrk, s);
3017
Florin Coras17ec5772020-08-12 20:46:29 -07003018 memset (&s->vep, 0, sizeof (s->vep));
3019 s->vep.next_sh = ~0;
3020 s->vep.prev_sh = ~0;
3021 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003022 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07003023 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08003024
Florin Corasf1ddeeb2021-06-10 08:08:53 -07003025 if (vcl_session_is_open (s))
Florin Coras607eb202023-05-29 16:19:38 -07003026 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08003027
Florin Coras5e062572019-03-14 19:07:51 -07003028 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003029 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003030 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003031 break;
3032
3033 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003034 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003035 rv = VPPCOM_EINVAL;
3036 }
3037
Florin Coras134a9962018-08-28 11:32:04 -07003038 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003039
3040done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003041 return rv;
3042}
3043
Florin Coras30ecfa82023-06-14 11:15:36 -07003044always_inline u8
3045vcl_ep_session_needs_evt (vcl_session_t *s, u32 evt)
3046{
3047 /* No event if not epolled / events reset on hup or level-trigger on */
3048 return ((s->vep.ev.events & evt) &&
3049 s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
3050}
3051
Florin Coras86f04502018-09-12 16:08:01 -07003052static inline void
3053vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3054 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003055{
3056 session_disconnected_msg_t *disconnected_msg;
3057 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003058 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003059 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003060 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003061 u8 add_event = 0;
3062
3063 switch (e->event_type)
3064 {
Florin Coras653e43f2019-03-04 10:56:23 -08003065 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003066 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003067 s = vcl_session_get (wrk, sid);
3068 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003069 break;
Florin Corasb242d312020-10-26 15:35:40 -07003070 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras30ecfa82023-06-14 11:15:36 -07003071 if (!vcl_ep_session_needs_evt (s, EPOLLIN) ||
3072 (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07003073 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003074 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003075 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003076 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003077 session_evt_data = s->vep.ev.data.u64;
3078 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003079 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003080 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003081 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003082 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003083 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003084 break;
Florin Coras48178552023-05-17 22:59:40 -07003085 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ? s->ct_tx_fifo :
3086 s->tx_fifo);
Florin Coras30ecfa82023-06-14 11:15:36 -07003087 if (!vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras86f04502018-09-12 16:08:01 -07003088 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003089 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003090 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003091 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003092 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003093 break;
Florin Coras86f04502018-09-12 16:08:01 -07003094 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003095 if (!e->postponed)
3096 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3097 else
3098 s = vcl_session_get (wrk, e->session_index);
Florin Coras30ecfa82023-06-14 11:15:36 -07003099 if (!s || !vcl_ep_session_needs_evt (s, EPOLLIN))
Florin Coras3c7d4f92018-12-14 11:28:43 -08003100 break;
Florin Corasb242d312020-10-26 15:35:40 -07003101 sid = s->session_index;
Florin Coras30ecfa82023-06-14 11:15:36 -07003102 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003103 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003104 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003105 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003106 break;
3107 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003108 if (!e->postponed)
3109 {
3110 connected_msg = (session_connected_msg_t *) e->data;
3111 sid = vcl_session_connected_handler (wrk, connected_msg);
3112 }
3113 else
3114 sid = e->session_index;
3115 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003116 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras72f77822019-01-22 19:05:52 -08003117 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003118 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07003119 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003120 add_event = 1;
Florin Coras30ecfa82023-06-14 11:15:36 -07003121 session_events = s->vep.ev.events;
3122 /* Generate EPOLLOUT because there's no connected event */
wanghanlin9e42cc22021-06-25 17:40:13 +08003123 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003124 session_evt_data = s->vep.ev.data.u64;
3125 if (s->session_state == VCL_STATE_DETACHED)
Florin Coras30ecfa82023-06-14 11:15:36 -07003126 {
3127 events[*num_ev].events |= EPOLLHUP;
3128 s->vep.ev.events = 0;
3129 }
Florin Coras86f04502018-09-12 16:08:01 -07003130 break;
3131 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003132 if (!e->postponed)
3133 {
3134 disconnected_msg = (session_disconnected_msg_t *) e->data;
3135 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3136 }
3137 else
3138 {
3139 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003140 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003141 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003142 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003143 {
Florin Coras74254b52021-12-08 11:03:08 -08003144 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003145 vcl_session_free (wrk, s);
3146 break;
3147 }
Florin Corasb242d312020-10-26 15:35:40 -07003148 sid = s->session_index;
3149 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003150 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003151 if (EPOLLRDHUP & session_events)
3152 {
3153 /* If app can distinguish between RDHUP and HUP,
3154 * we make finer control */
3155 events[*num_ev].events = EPOLLRDHUP;
3156 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3157 {
3158 events[*num_ev].events |= EPOLLHUP;
3159 }
3160 }
3161 else
3162 {
3163 events[*num_ev].events = EPOLLHUP;
3164 }
Florin Corasb242d312020-10-26 15:35:40 -07003165 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003166 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003167 break;
Florin Coras01ee7a72023-03-01 00:49:25 -08003168 case SESSION_CTRL_EVT_BOUND:
3169 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
3170 break;
Florin Coras86f04502018-09-12 16:08:01 -07003171 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003172 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003173 {
3174 sid =
3175 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3176 s = vcl_session_get (wrk, sid);
3177 }
Florin Coras87f76002021-06-28 19:13:29 -07003178 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003179 {
3180 sid = e->session_index;
3181 s = vcl_session_get (wrk, sid);
3182 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3183 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003184 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003185 {
Florin Coras74254b52021-12-08 11:03:08 -08003186 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003187 vcl_session_free (wrk, s);
3188 break;
3189 }
Florin Corasb242d312020-10-26 15:35:40 -07003190 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003191 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003192 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3193 if ((EPOLLRDHUP & session_events) &&
3194 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3195 {
Yacan Liuab157702022-09-26 16:41:32 +08003196 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003197 }
3198 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3199 {
3200 events[*num_ev].events |= EPOLLIN;
3201 }
Florin Corasb242d312020-10-26 15:35:40 -07003202 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003203 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003204 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003205 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3206 vcl_session_unlisten_reply_handler (wrk, e->data);
3207 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003208 case SESSION_CTRL_EVT_MIGRATED:
3209 vcl_session_migrated_handler (wrk, e->data);
3210 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003211 case SESSION_CTRL_EVT_CLEANUP:
3212 vcl_session_cleanup_handler (wrk, e->data);
3213 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003214 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3215 vcl_session_req_worker_update_handler (wrk, e->data);
3216 break;
3217 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3218 vcl_session_worker_update_reply_handler (wrk, e->data);
3219 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003220 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3221 vcl_session_app_add_segment_handler (wrk, e->data);
3222 break;
3223 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3224 vcl_session_app_del_segment_handler (wrk, e->data);
3225 break;
hanlina3a48962020-07-13 11:09:15 +08003226 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003227 vcl_worker_rpc_handler (wrk, e->data);
3228 break;
Florin Coras86f04502018-09-12 16:08:01 -07003229 default:
3230 VDBG (0, "unhandled: %u", e->event_type);
3231 break;
3232 }
3233
3234 if (add_event)
3235 {
Florin Coras30ecfa82023-06-14 11:15:36 -07003236 ASSERT (s->flags & VCL_SESSION_F_IS_VEP_SESSION);
Florin Coras86f04502018-09-12 16:08:01 -07003237 events[*num_ev].data.u64 = session_evt_data;
3238 if (EPOLLONESHOT & session_events)
3239 {
Florin Corasb242d312020-10-26 15:35:40 -07003240 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003241 if (!(events[*num_ev].events & EPOLLHUP))
3242 s->vep.ev.events = EPOLLHUP | EPOLLERR;
Florin Coras86f04502018-09-12 16:08:01 -07003243 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003244 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003245 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003246 s = vcl_session_get (wrk, sid);
3247 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3248 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003249 }
Florin Coras86f04502018-09-12 16:08:01 -07003250 *num_ev += 1;
3251 }
3252}
3253
3254static int
3255vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3256 struct epoll_event *events, u32 maxevents,
3257 double wait_for_time, u32 * num_ev)
3258{
Florin Coras99368312018-08-02 10:45:44 -07003259 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003260 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003261 int i;
3262
Florin Coras539663c2018-09-28 14:59:37 -07003263 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3264 goto handle_dequeued;
3265
Florin Coras54693d22018-07-17 10:46:29 -07003266 if (svm_msg_q_is_empty (mq))
3267 {
3268 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003269 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003270 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003271 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003272 else
3273 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003274 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003275 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003276 }
3277 }
Florin Corase003a1b2019-06-05 10:47:16 -07003278 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003279 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003280
Florin Coras539663c2018-09-28 14:59:37 -07003281handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003282 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003283 {
Florin Coras134a9962018-08-28 11:32:04 -07003284 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003285 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003286 if (*num_ev < maxevents)
3287 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3288 else
3289 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003290 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003291 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003292 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003293 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003294 return *num_ev;
3295}
3296
Florin Coras99368312018-08-02 10:45:44 -07003297static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003298vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3299 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003300{
Florin Corasccdb8b82021-04-28 13:01:06 -07003301 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003302
3303 if (!n_evts)
3304 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003305 if (timeout_ms > 0)
3306 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003307 }
3308
3309 do
3310 {
3311 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003312 timeout_ms, &n_evts);
3313 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003314 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003315 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003316 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003317
3318 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003319}
3320
3321static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003322vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3323 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003324{
Florin Coras99368312018-08-02 10:45:44 -07003325 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003326 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003327 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003328 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003329 u64 buf;
3330
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003331 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3332 {
3333 vcl_api_retry_attach (wrk);
3334 return n_evts;
3335 }
3336
Florin Coras134a9962018-08-28 11:32:04 -07003337 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003338 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003339 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003340 if (timeout_ms > 0)
3341 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003342 }
3343
Florin Corasb13ba132021-01-27 16:05:24 -08003344 do
3345 {
3346 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003347 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003348 if (n_mq_evts < 0)
3349 {
3350 VDBG (0, "epoll_wait error %u", errno);
3351 return n_evts;
3352 }
3353
3354 for (i = 0; i < n_mq_evts; i++)
3355 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003356 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3357 {
3358 /* api socket was closed */
3359 vcl_api_handle_disconnect (wrk);
3360 continue;
3361 }
3362
Florin Corasb13ba132021-01-27 16:05:24 -08003363 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3364 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3365 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3366 &n_evts);
3367 }
3368
Florin Corasccdb8b82021-04-28 13:01:06 -07003369 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003370 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003371 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003372 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003373
3374 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003375}
3376
Florin Corasfe286f72021-06-04 10:07:55 -07003377static void
Florin Corasfe286f72021-06-04 10:07:55 -07003378vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3379 int maxevents, u32 *n_evts)
3380{
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003381 u32 add_event = 0, evt_flags = 0, next, *to_remove = 0, *si;
Florin Corasfe286f72021-06-04 10:07:55 -07003382 vcl_session_t *s;
3383 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003384 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003385
Florin Corasfa3884f2021-06-22 20:04:46 -07003386 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003387 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003388 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003389
Florin Corasfa3884f2021-06-22 20:04:46 -07003390 next = wrk->ep_lt_current;
3391 do
Florin Corasfe286f72021-06-04 10:07:55 -07003392 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003393 s = vcl_session_get (wrk, next);
3394 next = s->vep.lt_next;
3395
Florin Coras30ecfa82023-06-14 11:15:36 -07003396 if (s->vep.ev.events == 0)
3397 {
3398 vec_add1 (to_remove, s->session_index);
3399 continue;
3400 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003401 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003402 {
3403 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003404 evt_flags |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003405 evt_data = s->vep.ev.data.u64;
3406 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003407 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003408 {
3409 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003410 evt_flags |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
Florin Corasfa3884f2021-06-22 20:04:46 -07003411 evt_data = s->vep.ev.data.u64;
3412 }
3413 if (!add_event && s->session_state > VCL_STATE_READY)
3414 {
3415 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003416 evt_flags |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003417 evt_data = s->vep.ev.data.u64;
3418 }
3419 if (add_event)
3420 {
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003421 events[*n_evts].events = evt_flags;
Florin Corasfe286f72021-06-04 10:07:55 -07003422 events[*n_evts].data.u64 = evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003423 if (EPOLLONESHOT & s->vep.ev.events)
Florin Coras30ecfa82023-06-14 11:15:36 -07003424 s->vep.ev.events = EPOLLHUP | EPOLLERR;
3425 if (evt_flags & EPOLLHUP)
Florin Corasfa3884f2021-06-22 20:04:46 -07003426 s->vep.ev.events = 0;
Florin Coras5cca6692023-06-20 08:47:37 -07003427 *n_evts += 1;
3428 add_event = 0;
3429 evt_flags = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003430 if (*n_evts == maxevents)
3431 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003432 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003433 break;
3434 }
3435 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003436 else
3437 {
Florin Coras7ff72742023-05-16 13:05:28 -07003438 vec_add1 (to_remove, s->session_index);
Florin Corasfa3884f2021-06-22 20:04:46 -07003439 }
Florin Corasfe286f72021-06-04 10:07:55 -07003440 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003441 while (next != wrk->ep_lt_current);
Florin Coras7ff72742023-05-16 13:05:28 -07003442
3443 vec_foreach (si, to_remove)
3444 {
3445 s = vcl_session_get (wrk, *si);
3446 vcl_epoll_lt_del (wrk, s);
3447 }
3448 vec_free (to_remove);
Florin Corasfe286f72021-06-04 10:07:55 -07003449}
3450
Dave Wallacef7f809c2017-10-03 01:48:42 -04003451int
Florin Coras134a9962018-08-28 11:32:04 -07003452vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003453 int maxevents, double wait_for_time)
3454{
Florin Coras134a9962018-08-28 11:32:04 -07003455 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003456 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003457 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003458 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003459
3460 if (PREDICT_FALSE (maxevents <= 0))
3461 {
Florin Coras5e062572019-03-14 19:07:51 -07003462 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003463 return VPPCOM_EINVAL;
3464 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003465
Florin Coras134a9962018-08-28 11:32:04 -07003466 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003467 if (!vep_session)
3468 return VPPCOM_EBADFD;
3469
Florin Coras6c3b2182020-10-19 18:36:48 -07003470 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003471 {
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003473 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003474 }
Florin Coras54693d22018-07-17 10:46:29 -07003475
Florin Coras86f04502018-09-12 16:08:01 -07003476 if (vec_len (wrk->unhandled_evts_vector))
3477 {
3478 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3479 {
3480 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3481 events, &n_evts);
3482 if (n_evts == maxevents)
3483 {
Florin Corase003a1b2019-06-05 10:47:16 -07003484 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3485 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003486 }
3487 }
Florin Corase003a1b2019-06-05 10:47:16 -07003488 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003489 }
liuyacancba87102021-09-10 15:14:05 +08003490
3491 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3492 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3493
wanghanlin8919fec2021-03-18 20:00:41 +08003494 /* Request to only drain unhandled */
3495 if ((int) wait_for_time == -2)
3496 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003497
Florin Coras86f04502018-09-12 16:08:01 -07003498
Florin Corasfe286f72021-06-04 10:07:55 -07003499 if (vcm->cfg.use_mq_eventfd)
3500 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3501 wait_for_time);
3502 else
3503 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3504 wait_for_time);
3505
Florin Corasfe286f72021-06-04 10:07:55 -07003506 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003507}
3508
Dave Wallace35830af2017-10-09 01:43:42 -04003509int
Florin Coras134a9962018-08-28 11:32:04 -07003510vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003511 void *buffer, uint32_t * buflen)
3512{
Florin Coras134a9962018-08-28 11:32:04 -07003513 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003514 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003515 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003516 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003517 vcl_session_t *session;
3518 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003519
Florin Coras134a9962018-08-28 11:32:04 -07003520 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003521 if (!session)
3522 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003523
Dave Wallace35830af2017-10-09 01:43:42 -04003524 switch (op)
3525 {
3526 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003527 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003528 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3529 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003530 break;
3531
Dave Wallace227867f2017-11-13 21:21:53 -05003532 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003533 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003534 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3535 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003536 break;
3537
3538 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003540 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003541 *flags =
3542 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003543 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003544 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003545 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003546 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3547 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003548 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003549 }
3550 else
3551 rv = VPPCOM_EINVAL;
3552 break;
3553
3554 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003555 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003556 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003557 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003558 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003559 else
Florin Corasac422d62020-10-19 20:51:36 -07003560 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003561
Florin Coras5e062572019-03-14 19:07:51 -07003562 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3563 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003564 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003565 }
3566 else
3567 rv = VPPCOM_EINVAL;
3568 break;
3569
3570 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003571 if (PREDICT_TRUE (buffer && buflen &&
3572 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003573 {
Florin Coras7e12d942018-06-27 14:32:43 -07003574 ep->is_ip4 = session->transport.is_ip4;
3575 ep->port = session->transport.rmt_port;
3576 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003577 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3578 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003579 else
Dave Barach178cf492018-11-13 16:34:13 -05003580 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3581 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003582 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003583 VDBG (1,
3584 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3585 "addr = %U, port %u",
3586 session_handle, ep->is_ip4, vcl_format_ip46_address,
3587 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003588 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3589 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003590 }
3591 else
3592 rv = VPPCOM_EINVAL;
3593 break;
3594
3595 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003596 if (PREDICT_TRUE (buffer && buflen &&
3597 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003598 {
Florin Coras7e12d942018-06-27 14:32:43 -07003599 ep->is_ip4 = session->transport.is_ip4;
3600 ep->port = session->transport.lcl_port;
3601 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003602 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3603 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003604 else
Dave Barach178cf492018-11-13 16:34:13 -05003605 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3606 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003607 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003608 VDBG (1,
3609 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3610 " port %d",
3611 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003612 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003613 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3614 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003615 }
3616 else
3617 rv = VPPCOM_EINVAL;
3618 break;
Stevenb5a11602017-10-11 09:59:30 -07003619
qinyangaf9b7152023-06-27 01:11:53 -07003620 case VPPCOM_ATTR_GET_ORIGINAL_DST:
3621 if (!session->transport.is_ip4)
3622 {
3623 /* now original dst only support ipv4*/
3624 rv = VPPCOM_EAFNOSUPPORT;
3625 break;
3626 }
3627 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*ep)) &&
3628 ep->ip))
3629 {
3630 ep->is_ip4 = session->transport.is_ip4;
3631 ep->port = session->original_dst_port;
3632 clib_memcpy_fast (ep->ip, &session->original_dst_ip4,
3633 sizeof (ip4_address_t));
3634 *buflen = sizeof (*ep);
3635 VDBG (1,
3636 "VPPCOM_ATTR_GET_ORIGINAL_DST: sh %u, is_ip4 = %u, addr = %U"
3637 " port %d",
3638 session_handle, ep->is_ip4, vcl_format_ip4_address,
3639 (ip4_address_t *) (&session->original_dst_ip4),
3640 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3641 clib_net_to_host_u16 (ep->port));
3642 }
3643 else
3644 rv = VPPCOM_EINVAL;
3645 break;
3646
Florin Corasef7cbf62019-10-17 09:56:27 -07003647 case VPPCOM_ATTR_SET_LCL_ADDR:
3648 if (PREDICT_TRUE (buffer && buflen &&
3649 (*buflen >= sizeof (*ep)) && ep->ip))
3650 {
3651 session->transport.is_ip4 = ep->is_ip4;
3652 session->transport.lcl_port = ep->port;
3653 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3654 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003655 VDBG (1,
3656 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3657 " port %d",
3658 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003659 &session->transport.lcl_ip,
3660 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3661 clib_net_to_host_u16 (ep->port));
3662 }
3663 else
3664 rv = VPPCOM_EINVAL;
3665 break;
3666
Dave Wallace048b1d62018-01-03 22:24:41 -05003667 case VPPCOM_ATTR_GET_LIBC_EPFD:
3668 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003669 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003670 break;
3671
3672 case VPPCOM_ATTR_SET_LIBC_EPFD:
3673 if (PREDICT_TRUE (buffer && buflen &&
3674 (*buflen == sizeof (session->libc_epfd))))
3675 {
3676 session->libc_epfd = *(int *) buffer;
3677 *buflen = sizeof (session->libc_epfd);
3678
Florin Coras5e062572019-03-14 19:07:51 -07003679 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3680 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003681 }
3682 else
3683 rv = VPPCOM_EINVAL;
3684 break;
3685
3686 case VPPCOM_ATTR_GET_PROTOCOL:
3687 if (buffer && buflen && (*buflen >= sizeof (int)))
3688 {
Florin Coras7e12d942018-06-27 14:32:43 -07003689 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003690 *buflen = sizeof (int);
3691
Florin Coras5e062572019-03-14 19:07:51 -07003692 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3693 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003694 }
3695 else
3696 rv = VPPCOM_EINVAL;
3697 break;
3698
3699 case VPPCOM_ATTR_GET_LISTEN:
3700 if (buffer && buflen && (*buflen >= sizeof (int)))
3701 {
Florin Corasac422d62020-10-19 20:51:36 -07003702 *(int *) buffer = vcl_session_has_attr (session,
3703 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003704 *buflen = sizeof (int);
3705
Florin Coras5e062572019-03-14 19:07:51 -07003706 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3707 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003708 }
3709 else
3710 rv = VPPCOM_EINVAL;
3711 break;
3712
3713 case VPPCOM_ATTR_GET_ERROR:
3714 if (buffer && buflen && (*buflen >= sizeof (int)))
3715 {
3716 *(int *) buffer = 0;
3717 *buflen = sizeof (int);
3718
Florin Coras5e062572019-03-14 19:07:51 -07003719 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3720 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003721 }
3722 else
3723 rv = VPPCOM_EINVAL;
3724 break;
3725
3726 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3727 if (buffer && buflen && (*buflen >= sizeof (u32)))
3728 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003729
3730 /* VPP-TBD */
3731 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003732 session->tx_fifo ?
3733 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003734 vcm->cfg.tx_fifo_size);
3735 *buflen = sizeof (u32);
3736
Florin Coras5e062572019-03-14 19:07:51 -07003737 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3738 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3739 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003740 }
3741 else
3742 rv = VPPCOM_EINVAL;
3743 break;
3744
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003745 case VPPCOM_ATTR_SET_DSCP:
3746 if (buffer && buflen && (*buflen >= sizeof (u8)))
3747 {
3748 session->dscp = *(u8 *) buffer;
3749
3750 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3751 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3752 }
3753 else
3754 rv = VPPCOM_EINVAL;
3755 break;
3756
Dave Wallace048b1d62018-01-03 22:24:41 -05003757 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3758 if (buffer && buflen && (*buflen == sizeof (u32)))
3759 {
3760 /* VPP-TBD */
3761 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003762 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3763 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3764 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003765 }
3766 else
3767 rv = VPPCOM_EINVAL;
3768 break;
3769
3770 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3771 if (buffer && buflen && (*buflen >= sizeof (u32)))
3772 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003773
3774 /* VPP-TBD */
3775 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003776 session->rx_fifo ?
3777 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003778 vcm->cfg.rx_fifo_size);
3779 *buflen = sizeof (u32);
3780
Florin Coras5e062572019-03-14 19:07:51 -07003781 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3782 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003783 }
3784 else
3785 rv = VPPCOM_EINVAL;
3786 break;
3787
3788 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3789 if (buffer && buflen && (*buflen == sizeof (u32)))
3790 {
3791 /* VPP-TBD */
3792 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003793 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3794 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3795 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003796 }
3797 else
3798 rv = VPPCOM_EINVAL;
3799 break;
3800
3801 case VPPCOM_ATTR_GET_REUSEADDR:
3802 if (buffer && buflen && (*buflen >= sizeof (int)))
3803 {
3804 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003805 *(int *) buffer = vcl_session_has_attr (session,
3806 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003807 *buflen = sizeof (int);
3808
Florin Coras5e062572019-03-14 19:07:51 -07003809 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3810 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 }
3812 else
3813 rv = VPPCOM_EINVAL;
3814 break;
3815
Stevenb5a11602017-10-11 09:59:30 -07003816 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003818 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 {
3820 /* VPP-TBD */
3821 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003822 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003823 else
Florin Corasac422d62020-10-19 20:51:36 -07003824 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003825
Florin Coras5e062572019-03-14 19:07:51 -07003826 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003827 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003828 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003829 }
3830 else
3831 rv = VPPCOM_EINVAL;
3832 break;
3833
3834 case VPPCOM_ATTR_GET_REUSEPORT:
3835 if (buffer && buflen && (*buflen >= sizeof (int)))
3836 {
3837 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003838 *(int *) buffer = vcl_session_has_attr (session,
3839 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003840 *buflen = sizeof (int);
3841
Florin Coras5e062572019-03-14 19:07:51 -07003842 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3843 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003844 }
3845 else
3846 rv = VPPCOM_EINVAL;
3847 break;
3848
3849 case VPPCOM_ATTR_SET_REUSEPORT:
3850 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003851 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003852 {
3853 /* VPP-TBD */
3854 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003855 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003856 else
Florin Corasac422d62020-10-19 20:51:36 -07003857 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003858
Florin Coras5e062572019-03-14 19:07:51 -07003859 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003860 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003861 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003862 }
3863 else
3864 rv = VPPCOM_EINVAL;
3865 break;
3866
3867 case VPPCOM_ATTR_GET_BROADCAST:
3868 if (buffer && buflen && (*buflen >= sizeof (int)))
3869 {
3870 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003871 *(int *) buffer = vcl_session_has_attr (session,
3872 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003873 *buflen = sizeof (int);
3874
Florin Coras5e062572019-03-14 19:07:51 -07003875 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3876 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003877 }
3878 else
3879 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003880 break;
3881
3882 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003883 if (buffer && buflen && (*buflen == sizeof (int)))
3884 {
3885 /* VPP-TBD */
3886 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003887 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003888 else
Florin Corasac422d62020-10-19 20:51:36 -07003889 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003890
Florin Coras5e062572019-03-14 19:07:51 -07003891 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003892 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003893 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003894 }
3895 else
3896 rv = VPPCOM_EINVAL;
3897 break;
3898
3899 case VPPCOM_ATTR_GET_V6ONLY:
3900 if (buffer && buflen && (*buflen >= sizeof (int)))
3901 {
3902 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003903 *(int *) buffer = vcl_session_has_attr (session,
3904 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003905 *buflen = sizeof (int);
3906
Florin Coras5e062572019-03-14 19:07:51 -07003907 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3908 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003909 }
3910 else
3911 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003912 break;
3913
3914 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003915 if (buffer && buflen && (*buflen == sizeof (int)))
3916 {
3917 /* VPP-TBD */
3918 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003919 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003920 else
Florin Corasac422d62020-10-19 20:51:36 -07003921 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003922
Florin Coras5e062572019-03-14 19:07:51 -07003923 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003924 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003925 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003926 }
3927 else
3928 rv = VPPCOM_EINVAL;
3929 break;
3930
3931 case VPPCOM_ATTR_GET_KEEPALIVE:
3932 if (buffer && buflen && (*buflen >= sizeof (int)))
3933 {
3934 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003935 *(int *) buffer = vcl_session_has_attr (session,
3936 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003937 *buflen = sizeof (int);
3938
Florin Coras5e062572019-03-14 19:07:51 -07003939 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3940 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003941 }
3942 else
3943 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003944 break;
Stevenbccd3392017-10-12 20:42:21 -07003945
3946 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003947 if (buffer && buflen && (*buflen == sizeof (int)))
3948 {
3949 /* VPP-TBD */
3950 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003951 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003952 else
Florin Corasac422d62020-10-19 20:51:36 -07003953 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003954
Florin Coras5e062572019-03-14 19:07:51 -07003955 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003956 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003957 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003958 }
3959 else
3960 rv = VPPCOM_EINVAL;
3961 break;
3962
3963 case VPPCOM_ATTR_GET_TCP_NODELAY:
3964 if (buffer && buflen && (*buflen >= sizeof (int)))
3965 {
3966 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003967 *(int *) buffer = vcl_session_has_attr (session,
3968 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003969 *buflen = sizeof (int);
3970
Florin Coras5e062572019-03-14 19:07:51 -07003971 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3972 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003973 }
3974 else
3975 rv = VPPCOM_EINVAL;
3976 break;
3977
3978 case VPPCOM_ATTR_SET_TCP_NODELAY:
3979 if (buffer && buflen && (*buflen == sizeof (int)))
3980 {
3981 /* VPP-TBD */
3982 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003983 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003984 else
Florin Corasac422d62020-10-19 20:51:36 -07003985 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003986
Florin Coras5e062572019-03-14 19:07:51 -07003987 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003988 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003989 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003990 }
3991 else
3992 rv = VPPCOM_EINVAL;
3993 break;
3994
3995 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3996 if (buffer && buflen && (*buflen >= sizeof (int)))
3997 {
3998 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003999 *(int *) buffer = vcl_session_has_attr (session,
4000 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004001 *buflen = sizeof (int);
4002
Florin Coras5e062572019-03-14 19:07:51 -07004003 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
4004 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004005 }
4006 else
4007 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004008 break;
4009
4010 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05004011 if (buffer && buflen && (*buflen == sizeof (int)))
4012 {
4013 /* VPP-TBD */
4014 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004015 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004016 else
Florin Corasac422d62020-10-19 20:51:36 -07004017 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004018
Florin Coras5e062572019-03-14 19:07:51 -07004019 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004020 vcl_session_has_attr (session,
4021 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004022 }
4023 else
4024 rv = VPPCOM_EINVAL;
4025 break;
4026
4027 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
4028 if (buffer && buflen && (*buflen >= sizeof (int)))
4029 {
4030 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004031 *(int *) buffer = vcl_session_has_attr (session,
4032 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004033 *buflen = sizeof (int);
4034
Florin Coras5e062572019-03-14 19:07:51 -07004035 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
4036 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004037 }
4038 else
4039 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004040 break;
4041
4042 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05004043 if (buffer && buflen && (*buflen == sizeof (int)))
4044 {
4045 /* VPP-TBD */
4046 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004047 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004048 else
Florin Corasac422d62020-10-19 20:51:36 -07004049 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004050
Florin Coras5e062572019-03-14 19:07:51 -07004051 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004052 vcl_session_has_attr (session,
4053 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004054 }
4055 else
4056 rv = VPPCOM_EINVAL;
4057 break;
4058
4059 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004060 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004061 {
Florin Coras04ae8272021-04-12 19:55:37 -07004062 rv = VPPCOM_EINVAL;
4063 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004064 }
Florin Coras04ae8272021-04-12 19:55:37 -07004065
4066 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4067 tea.mss = *(u32 *) buffer;
4068 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
4069 rv = VPPCOM_ENOPROTOOPT;
4070
4071 if (!rv)
4072 {
4073 *(u32 *) buffer = tea.mss;
4074 *buflen = sizeof (int);
4075 }
4076
4077 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4078 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004079 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004080 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004081 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004082 {
Florin Coras04ae8272021-04-12 19:55:37 -07004083 rv = VPPCOM_EINVAL;
4084 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004085 }
Florin Coras04ae8272021-04-12 19:55:37 -07004086
4087 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4088 tea.mss = *(u32 *) buffer;
4089 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4090 rv = VPPCOM_ENOPROTOOPT;
4091
4092 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4093 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004094 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004095
Florin Coras1e966172020-05-16 18:18:14 +00004096 case VPPCOM_ATTR_SET_CONNECTED:
4097 session->flags |= VCL_SESSION_F_CONNECTED;
4098 break;
4099
Florin Corasa5a9efd2021-01-05 17:03:29 -08004100 case VPPCOM_ATTR_SET_CKPAIR:
4101 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4102 !vcl_session_has_crypto (session))
4103 {
4104 rv = VPPCOM_EINVAL;
4105 break;
4106 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004107 if (!session->ext_config)
4108 {
Florin Coras87f63892021-05-01 16:01:40 -07004109 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4110 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004111 }
4112 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4113 {
4114 rv = VPPCOM_EINVAL;
4115 break;
4116 }
4117
4118 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004119 break;
4120
Florin Coras6a6555a2021-01-28 11:39:27 -08004121 case VPPCOM_ATTR_SET_VRF:
4122 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4123 {
4124 rv = VPPCOM_EINVAL;
4125 break;
4126 }
4127 session->vrf = *(u32 *) buffer;
4128 break;
4129
4130 case VPPCOM_ATTR_GET_VRF:
4131 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4132 {
4133 rv = VPPCOM_EINVAL;
4134 break;
4135 }
4136 *(u32 *) buffer = session->vrf;
4137 *buflen = sizeof (u32);
4138 break;
4139
wanghanlin0674f852021-02-22 10:38:36 +08004140 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004141 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004142 {
Florin Corasd77325c2021-02-23 12:03:03 -08004143 rv = VPPCOM_EINVAL;
4144 break;
wanghanlin0674f852021-02-22 10:38:36 +08004145 }
Florin Corasd77325c2021-02-23 12:03:03 -08004146
4147 if (session->transport.is_ip4)
4148 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004149 else
Florin Corasd77325c2021-02-23 12:03:03 -08004150 *(int *) buffer = AF_INET6;
4151 *buflen = sizeof (int);
4152
wanghanlin0674f852021-02-22 10:38:36 +08004153 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4154 *buflen);
4155 break;
4156
Florin Coras87f63892021-05-01 16:01:40 -07004157 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4158 if (!(buffer && buflen && (*buflen > 0)))
4159 {
4160 rv = VPPCOM_EINVAL;
4161 break;
4162 }
4163 if (session->ext_config)
4164 {
4165 rv = VPPCOM_EINVAL;
4166 break;
4167 }
4168 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4169 *buflen + sizeof (u32));
4170 clib_memcpy (session->ext_config->data, buffer, *buflen);
4171 session->ext_config->len = *buflen;
4172 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004173 case VPPCOM_ATTR_SET_IP_PKTINFO:
4174 if (buffer && buflen && (*buflen == sizeof (int)) &&
4175 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4176 {
4177 if (*(int *) buffer)
4178 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4179 else
4180 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4181
4182 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4183 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4184 *buflen);
4185 }
4186 else
4187 rv = VPPCOM_EINVAL;
4188 break;
4189
4190 case VPPCOM_ATTR_GET_IP_PKTINFO:
4191 if (buffer && buflen && (*buflen >= sizeof (int)))
4192 {
4193 *(int *) buffer =
4194 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4195 *buflen = sizeof (int);
4196
4197 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4198 *buflen);
4199 }
4200 else
4201 rv = VPPCOM_EINVAL;
4202 break;
Florin Coras87f63892021-05-01 16:01:40 -07004203
Dave Wallacee22aa742017-10-20 12:30:38 -04004204 default:
4205 rv = VPPCOM_EINVAL;
4206 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004207 }
4208
Dave Wallace35830af2017-10-09 01:43:42 -04004209 return rv;
4210}
4211
Stevenac1f96d2017-10-24 16:03:58 -07004212int
Florin Coras134a9962018-08-28 11:32:04 -07004213vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004214 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4215{
Florin Coras134a9962018-08-28 11:32:04 -07004216 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004217 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004218 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004219
4220 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004221 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004222 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004223 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004224 else
4225 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004226 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004227 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004228 }
4229
Florin Coras0a1e1832020-03-29 18:54:04 +00004230 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004231 {
Florin Coras5da10c42020-04-01 04:31:21 +00004232 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004233 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004234 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4235 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004236 else
Dave Barach178cf492018-11-13 16:34:13 -05004237 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4238 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004239 ep->is_ip4 = session->transport.is_ip4;
4240 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004241 }
Florin Coras460dce62018-07-27 05:45:06 -07004242
Stevenac1f96d2017-10-24 16:03:58 -07004243 return rv;
4244}
4245
Florin Coraseff5f7a2023-02-07 17:36:17 -08004246static void
4247vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4248{
4249 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4250
4251 do
4252 {
4253 switch (tlv->data_type)
4254 {
4255 case VCL_UDP_SEGMENT:
4256 s->gso_size = *(u16 *) tlv->data;
4257 break;
4258 case VCL_IP_PKTINFO:
4259 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4260 sizeof (ip4_address_t));
4261 break;
4262 default:
4263 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4264 break;
4265 }
4266 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4267 }
4268 while (tlv);
4269}
4270
Stevenac1f96d2017-10-24 16:03:58 -07004271int
Florin Coras134a9962018-08-28 11:32:04 -07004272vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004273 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4274{
Florin Coras7a2abce2020-04-05 19:25:44 +00004275 vcl_worker_t *wrk = vcl_worker_get_current ();
4276 vcl_session_t *s;
4277
4278 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004279 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004280 return VPPCOM_EBADFD;
4281
Dave Wallace617dffa2017-10-26 14:47:06 -04004282 if (ep)
4283 {
Florin Coras5824cc52020-10-20 18:44:41 -07004284 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004285 return VPPCOM_EINVAL;
4286
liuyacanbc0c7542021-08-02 20:15:05 +08004287 s->transport.is_ip4 = ep->is_ip4;
4288 s->transport.rmt_port = ep->port;
4289 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4290
Florin Coraseff5f7a2023-02-07 17:36:17 -08004291 if (ep->app_tlvs)
4292 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004293
Florin Coras5da10c42020-04-01 04:31:21 +00004294 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004295 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004296 {
Florin Coras5824cc52020-10-20 18:44:41 -07004297 u32 session_index = s->session_index;
4298 f64 timeout = vcm->cfg.session_timeout;
4299 int rv;
4300
Florin Coras5da10c42020-04-01 04:31:21 +00004301 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004302 rv = vppcom_wait_for_session_state_change (session_index,
4303 VCL_STATE_READY,
4304 timeout);
4305 if (rv < 0)
4306 return rv;
4307 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004308 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004309 }
4310
4311 if (flags)
4312 {
4313 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004314 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004315 }
4316
Florin Coraseff5f7a2023-02-07 17:36:17 -08004317 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004318 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004319}
4320
Dave Wallace048b1d62018-01-03 22:24:41 -05004321int
4322vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4323{
Florin Coras134a9962018-08-28 11:32:04 -07004324 vcl_worker_t *wrk = vcl_worker_get_current ();
4325 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004326 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004327 svm_msg_q_msg_t msg;
4328 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004329 int rv, num_ev = 0;
4330
Florin Coras5e062572019-03-14 19:07:51 -07004331 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004332
4333 if (!vp)
4334 return VPPCOM_EFAULT;
4335
4336 do
4337 {
Florin Coras7e12d942018-06-27 14:32:43 -07004338 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004339
Florin Coras6917b942018-11-13 22:44:54 -08004340 /* Dequeue all events and drop all unhandled io events */
4341 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4342 {
4343 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4344 vcl_handle_mq_event (wrk, e);
4345 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4346 }
4347 vec_reset_length (wrk->unhandled_evts_vector);
4348
Dave Wallace048b1d62018-01-03 22:24:41 -05004349 for (i = 0; i < n_sids; i++)
4350 {
Florin Coras7baeb712019-01-04 17:05:43 -08004351 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004352 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004353 {
4354 vp[i].revents = POLLHUP;
4355 num_ev++;
4356 continue;
4357 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004358
Florin Coras6917b942018-11-13 22:44:54 -08004359 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004360
4361 if (POLLIN & vp[i].events)
4362 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004363 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004364 if (rv > 0)
4365 {
Florin Coras6917b942018-11-13 22:44:54 -08004366 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004367 num_ev++;
4368 }
4369 else if (rv < 0)
4370 {
4371 switch (rv)
4372 {
4373 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004374 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004375 break;
4376
4377 default:
Florin Coras6917b942018-11-13 22:44:54 -08004378 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004379 break;
4380 }
4381 num_ev++;
4382 }
4383 }
4384
4385 if (POLLOUT & vp[i].events)
4386 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004387 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004388 if (rv > 0)
4389 {
Florin Coras6917b942018-11-13 22:44:54 -08004390 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004391 num_ev++;
4392 }
4393 else if (rv < 0)
4394 {
4395 switch (rv)
4396 {
4397 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004398 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004399 break;
4400
4401 default:
Florin Coras6917b942018-11-13 22:44:54 -08004402 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004403 break;
4404 }
4405 num_ev++;
4406 }
4407 }
4408
Dave Wallace7e607a72018-06-18 18:41:32 -04004409 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004410 {
Florin Coras6917b942018-11-13 22:44:54 -08004411 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004412 num_ev++;
4413 }
4414 }
4415 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004416 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004417 }
4418 while ((num_ev == 0) && keep_trying);
4419
Dave Wallace048b1d62018-01-03 22:24:41 -05004420 return num_ev;
4421}
4422
Florin Coras99368312018-08-02 10:45:44 -07004423int
4424vppcom_mq_epoll_fd (void)
4425{
Florin Coras134a9962018-08-28 11:32:04 -07004426 vcl_worker_t *wrk = vcl_worker_get_current ();
4427 return wrk->mqs_epfd;
4428}
4429
4430int
Florin Coras30e79c22019-01-02 19:31:22 -08004431vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004432{
4433 return session_handle & 0xFFFFFF;
4434}
4435
4436int
Florin Coras30e79c22019-01-02 19:31:22 -08004437vppcom_session_worker (vcl_session_handle_t session_handle)
4438{
4439 return session_handle >> 24;
4440}
4441
4442int
Florin Coras134a9962018-08-28 11:32:04 -07004443vppcom_worker_register (void)
4444{
Florin Coras47c40e22018-11-26 17:01:36 -08004445 if (!vcl_worker_alloc_and_init ())
4446 return VPPCOM_EEXIST;
4447
Florin Coras47c40e22018-11-26 17:01:36 -08004448 if (vcl_worker_register_with_vpp ())
4449 return VPPCOM_EEXIST;
4450
4451 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004452}
4453
Florin Coras369db832019-07-08 12:34:45 -07004454void
4455vppcom_worker_unregister (void)
4456{
4457 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4458 vcl_set_worker_index (~0);
4459}
4460
Pivo6017ff02020-05-04 17:57:33 +02004461void
4462vppcom_worker_index_set (int index)
4463{
4464 vcl_set_worker_index (index);
4465}
4466
Florin Corasdfe4cf42018-11-28 22:13:45 -08004467int
4468vppcom_worker_index (void)
4469{
4470 return vcl_get_worker_index ();
4471}
4472
Florin Corase0982e52019-01-25 13:19:56 -08004473int
4474vppcom_worker_mqs_epfd (void)
4475{
4476 vcl_worker_t *wrk = vcl_worker_get_current ();
4477 if (!vcm->cfg.use_mq_eventfd)
4478 return -1;
4479 return wrk->mqs_epfd;
4480}
4481
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004482int
4483vppcom_session_is_connectable_listener (uint32_t session_handle)
4484{
4485 vcl_session_t *session;
4486 vcl_worker_t *wrk = vcl_worker_get_current ();
4487 session = vcl_session_get_w_handle (wrk, session_handle);
4488 if (!session)
4489 return VPPCOM_EBADFD;
4490 return vcl_session_is_connectable_listener (wrk, session);
4491}
4492
4493int
4494vppcom_session_listener (uint32_t session_handle)
4495{
4496 vcl_worker_t *wrk = vcl_worker_get_current ();
4497 vcl_session_t *listen_session, *session;
4498 session = vcl_session_get_w_handle (wrk, session_handle);
4499 if (!session)
4500 return VPPCOM_EBADFD;
4501 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4502 return VPPCOM_EBADFD;
4503 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4504 if (!listen_session)
4505 return VPPCOM_EBADFD;
4506 return vcl_session_handle (listen_session);
4507}
4508
4509int
4510vppcom_session_n_accepted (uint32_t session_handle)
4511{
4512 vcl_worker_t *wrk = vcl_worker_get_current ();
4513 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4514 if (!session)
4515 return VPPCOM_EBADFD;
4516 return session->n_accepted_sessions;
4517}
4518
Florin Coras66ec4672020-06-15 07:59:40 -07004519const char *
4520vppcom_proto_str (vppcom_proto_t proto)
4521{
4522 char const *proto_str;
4523
4524 switch (proto)
4525 {
4526 case VPPCOM_PROTO_TCP:
4527 proto_str = "TCP";
4528 break;
4529 case VPPCOM_PROTO_UDP:
4530 proto_str = "UDP";
4531 break;
4532 case VPPCOM_PROTO_TLS:
4533 proto_str = "TLS";
4534 break;
4535 case VPPCOM_PROTO_QUIC:
4536 proto_str = "QUIC";
4537 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004538 case VPPCOM_PROTO_DTLS:
4539 proto_str = "DTLS";
4540 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004541 case VPPCOM_PROTO_SRTP:
4542 proto_str = "SRTP";
4543 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004544 default:
4545 proto_str = "UNKNOWN";
4546 break;
4547 }
4548 return proto_str;
4549}
4550
4551const char *
4552vppcom_retval_str (int retval)
4553{
4554 char const *st;
4555
4556 switch (retval)
4557 {
4558 case VPPCOM_OK:
4559 st = "VPPCOM_OK";
4560 break;
4561
4562 case VPPCOM_EAGAIN:
4563 st = "VPPCOM_EAGAIN";
4564 break;
4565
4566 case VPPCOM_EFAULT:
4567 st = "VPPCOM_EFAULT";
4568 break;
4569
4570 case VPPCOM_ENOMEM:
4571 st = "VPPCOM_ENOMEM";
4572 break;
4573
4574 case VPPCOM_EINVAL:
4575 st = "VPPCOM_EINVAL";
4576 break;
4577
4578 case VPPCOM_EBADFD:
4579 st = "VPPCOM_EBADFD";
4580 break;
4581
4582 case VPPCOM_EAFNOSUPPORT:
4583 st = "VPPCOM_EAFNOSUPPORT";
4584 break;
4585
4586 case VPPCOM_ECONNABORTED:
4587 st = "VPPCOM_ECONNABORTED";
4588 break;
4589
4590 case VPPCOM_ECONNRESET:
4591 st = "VPPCOM_ECONNRESET";
4592 break;
4593
4594 case VPPCOM_ENOTCONN:
4595 st = "VPPCOM_ENOTCONN";
4596 break;
4597
4598 case VPPCOM_ECONNREFUSED:
4599 st = "VPPCOM_ECONNREFUSED";
4600 break;
4601
4602 case VPPCOM_ETIMEDOUT:
4603 st = "VPPCOM_ETIMEDOUT";
4604 break;
4605
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304606 case VPPCOM_EADDRINUSE:
4607 st = "VPPCOM_EADDRINUSE";
4608 break;
4609
Florin Coras66ec4672020-06-15 07:59:40 -07004610 default:
4611 st = "UNKNOWN_STATE";
4612 break;
4613 }
4614
4615 return st;
4616}
4617
Florin Corasa5a9efd2021-01-05 17:03:29 -08004618int
4619vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4620{
4621 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004622 return vcl_sapi_add_cert_key_pair (ckpair);
4623 else
4624 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004625}
4626
4627int
4628vppcom_del_cert_key_pair (uint32_t ckpair_index)
4629{
4630 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004631 return vcl_sapi_del_cert_key_pair (ckpair_index);
4632 else
4633 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004634}
4635
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304636int
4637vppcom_session_get_error (uint32_t session_handle)
4638{
4639 vcl_worker_t *wrk = vcl_worker_get_current ();
4640 vcl_session_t *session = 0;
4641
4642 session = vcl_session_get_w_handle (wrk, session_handle);
4643 if (!session)
4644 return VPPCOM_EBADFD;
4645
4646 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4647 {
4648 VWRN ("epoll session %u! will not have connect", session->session_index);
4649 return VPPCOM_EBADFD;
4650 }
4651
4652 if (session->vpp_error == SESSION_E_PORTINUSE)
4653 return VPPCOM_EADDRINUSE;
4654 else if (session->vpp_error == SESSION_E_REFUSED)
4655 return VPPCOM_ECONNREFUSED;
4656 else if (session->vpp_error != SESSION_E_NONE)
4657 return VPPCOM_EFAULT;
4658 else
4659 return VPPCOM_OK;
4660}
4661
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004662int
4663vppcom_worker_is_detached (void)
4664{
4665 vcl_worker_t *wrk = vcl_worker_get_current ();
4666
4667 if (!vcm->cfg.use_mq_eventfd)
4668 return VPPCOM_ENOTSUP;
4669
4670 return wrk->api_client_handle == ~0;
4671}
4672
Dave Wallacee22aa742017-10-20 12:30:38 -04004673/*
4674 * fd.io coding-style-patch-verification: ON
4675 *
4676 * Local Variables:
4677 * eval: (c-set-style "gnu")
4678 * End:
4679 */