blob: 06a345d57922b744fa485fd42107a4fb6f15168f [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 */
Florin Corasc127d5a2020-10-14 16:35:58 -0700516 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800517 {
518
519 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 */
Florin Corasc127d5a2020-10-14 16:35:58 -0700709 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800710 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800711 if (!vcl_flag_accepted_session (session, msg->handle,
712 VCL_ACCEPTED_F_CLOSED))
713 VDBG (0, "session was not accepted!");
714 return 0;
715 }
716
Florin Corasadcfb152020-02-12 08:50:29 +0000717 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700718 if (session->session_state != VCL_STATE_DISCONNECT)
719 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000720
Florin Coras3c7d4f92018-12-14 11:28:43 -0800721 return session;
722}
723
liuyacan534468e2021-05-09 03:50:40 +0000724int
liuyacan55c952e2021-06-13 14:54:55 +0800725vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000726{
727 vcl_worker_t *wrk = vcl_worker_get_current ();
728 vcl_session_t *session;
729 vcl_session_state_t state;
730 u64 vpp_handle;
731
732 session = vcl_session_get_w_handle (wrk, session_handle);
733 if (PREDICT_FALSE (!session))
734 return VPPCOM_EBADFD;
735
736 vpp_handle = session->vpp_handle;
737 state = session->session_state;
738
739 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800740 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000741
742 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
743 {
744 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
745 return VPPCOM_EBADFD;
746 }
747
liuyacan55c952e2021-06-13 14:54:55 +0800748 if (how == SHUT_RD || how == SHUT_RDWR)
749 {
750 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
751 if (how == SHUT_RD)
752 return VPPCOM_OK;
753 }
754 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
755
liuyacan534468e2021-05-09 03:50:40 +0000756 if (PREDICT_TRUE (state == VCL_STATE_READY))
757 {
758 VDBG (1, "session %u [0x%llx]: sending shutdown...",
759 session->session_index, vpp_handle);
760
761 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000762 }
763
764 return VPPCOM_OK;
765}
766
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700767static int
768vppcom_session_disconnect (u32 session_handle)
769{
770 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700771 vcl_session_t *session, *listen_session;
772 vcl_session_state_t state;
773 u64 vpp_handle;
774
775 session = vcl_session_get_w_handle (wrk, session_handle);
776 if (!session)
777 return VPPCOM_EBADFD;
778
779 vpp_handle = session->vpp_handle;
780 state = session->session_state;
781
Florin Coras5ffb9642021-12-03 17:24:13 -0800782 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
783 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700784
785 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
786 {
787 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
788 return VPPCOM_EBADFD;
789 }
790
791 if (state == VCL_STATE_VPP_CLOSING)
792 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800793 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700794 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
795 session->session_index, vpp_handle);
796 }
797 else
798 {
799 /* Session doesn't have an event queue yet. Probably a non-blocking
800 * connect. Wait for the reply */
801 if (PREDICT_FALSE (!session->vpp_evt_q))
802 return VPPCOM_OK;
803
Florin Coras5ffb9642021-12-03 17:24:13 -0800804 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700805 session->session_index, vpp_handle);
806 vcl_send_session_disconnect (wrk, session);
807 }
808
809 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
810 {
811 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700812 if (listen_session)
813 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700814 }
815
816 return VPPCOM_OK;
817}
818
Florin Coras30e79c22019-01-02 19:31:22 -0800819static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700820vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
821{
822 session_cleanup_msg_t *msg;
823 vcl_session_t *session;
824
825 msg = (session_cleanup_msg_t *) data;
826 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
827 if (!session)
828 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800829 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700830 return;
831 }
832
Florin Coras36d49392020-04-24 23:00:11 +0000833 if (msg->type == SESSION_CLEANUP_TRANSPORT)
834 {
835 /* Transport was cleaned up before we confirmed close. Probably the
836 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700837 * Confirm close to make sure everything is cleaned up.
838 * Move to undetermined state to ensure that the session is not
839 * removed before both vpp and the app cleanup.
840 * - If the app closes first, the session is moved to CLOSED state
841 * and the session cleanup notification from vpp removes the
842 * session.
843 * - If vpp cleans up the session first, the session is moved to
844 * DETACHED state lower and subsequently the close from the app
845 * frees the session
846 */
847 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700848 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700849 vppcom_session_disconnect (vcl_session_handle (session));
850 session->session_state = VCL_STATE_UPDATED;
851 }
852 else if (session->session_state == VCL_STATE_DISCONNECT)
853 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800854 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700855 session->session_state = VCL_STATE_UPDATED;
856 }
Florin Coras36d49392020-04-24 23:00:11 +0000857 return;
858 }
859
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800860 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700861 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800862
863 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700864 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000865 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800866 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700867 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000868 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
869 return;
870 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800871
872 /* Session probably tracked with epoll, disconnect not yet handled and
873 * 1) both transport and session cleanup completed 2) app closed. Wait
874 * until message is drained to free the session.
875 * See @ref vcl_handle_mq_event */
876 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
877 {
878 session->flags |= VCL_SESSION_F_PENDING_FREE;
879 return;
880 }
881
Florin Coras9ace36d2019-10-28 13:14:17 -0700882 vcl_session_free (wrk, session);
883}
884
885static void
Florin Coras30e79c22019-01-02 19:31:22 -0800886vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
887{
888 session_req_worker_update_msg_t *msg;
889 vcl_session_t *s;
890
891 msg = (session_req_worker_update_msg_t *) data;
892 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
893 if (!s)
894 return;
895
896 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
897}
898
899static void
900vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
901{
902 session_worker_update_reply_msg_t *msg;
903 vcl_session_t *s;
904
905 msg = (session_worker_update_reply_msg_t *) data;
906 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
907 if (!s)
908 {
909 VDBG (0, "unknown handle 0x%llx", msg->handle);
910 return;
911 }
Florin Coras30e79c22019-01-02 19:31:22 -0800912
Florin Coras5f45e012019-01-23 09:21:30 -0800913 if (s->rx_fifo)
914 {
Florin Corasc547e912020-12-08 17:50:45 -0800915 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700916 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800917 {
918 VDBG (0, "failed to attach fifos for %u", s->session_index);
919 return;
920 }
Florin Coras5f45e012019-01-23 09:21:30 -0800921 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700922 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800923
Florin Coras30e79c22019-01-02 19:31:22 -0800924 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
925 s->vpp_handle, wrk->wrk_index);
926}
927
Florin Coras935ce752020-09-08 22:43:47 -0700928static int
929vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
930{
931
932 if (vcm->cfg.vpp_app_socket_api)
933 return vcl_sapi_recv_fds (wrk, fds, n_fds);
934
935 return vcl_bapi_recv_fds (wrk, fds, n_fds);
936}
937
Florin Corasc4c4cf52019-08-24 18:17:34 -0700938static void
939vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
940{
941 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
942 session_app_add_segment_msg_t *msg;
943 u64 segment_handle;
944 int fd = -1;
945
946 msg = (session_app_add_segment_msg_t *) data;
947
948 if (msg->fd_flags)
949 {
Florin Coras935ce752020-09-08 22:43:47 -0700950 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700951 seg_type = SSVM_SEGMENT_MEMFD;
952 }
953
954 segment_handle = msg->segment_handle;
955 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
956 {
957 clib_warning ("invalid segment handle");
958 return;
959 }
960
961 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
962 seg_type, fd))
963 {
964 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
965 return;
966 }
967
968 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
969 msg->segment_size);
970}
971
972static void
973vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
974{
975 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
976 vcl_segment_detach (msg->segment_handle);
Florin Coras1f40ab42023-06-13 17:18:56 -0700977 VDBG (1, "Unmapped segment: %lx", msg->segment_handle);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700978}
979
Florin Coras40c07ce2020-07-16 20:46:17 -0700980static void
981vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
982{
983 if (!vcm->wrk_rpc_fn)
984 return;
985
hanlina3a48962020-07-13 11:09:15 +0800986 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700987}
988
Florin Coras04ae8272021-04-12 19:55:37 -0700989static void
990vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
991{
992 session_transport_attr_reply_msg_t *mp;
993
994 if (!wrk->session_attr_op)
995 return;
996
997 mp = (session_transport_attr_reply_msg_t *) data;
998
999 wrk->session_attr_op_rv = mp->retval;
1000 wrk->session_attr_op = 0;
1001 wrk->session_attr_rv = mp->attr;
1002}
1003
Florin Coras86f04502018-09-12 16:08:01 -07001004static int
1005vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001006{
Florin Coras54693d22018-07-17 10:46:29 -07001007 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001008 session_connected_msg_t *connected_msg;
1009 session_reset_msg_t *reset_msg;
1010 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001011 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001012 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001013
1014 switch (e->event_type)
1015 {
Florin Coras653e43f2019-03-04 10:56:23 -08001016 case SESSION_IO_EVT_RX:
1017 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001018 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001019 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001020 break;
Florin Coras86f04502018-09-12 16:08:01 -07001021 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001022 break;
Florin Corasb242d312020-10-26 15:35:40 -07001023 case SESSION_CTRL_EVT_BOUND:
1024 /* We can only wait for only one listen so not postponed */
1025 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1026 break;
Florin Coras54693d22018-07-17 10:46:29 -07001027 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001028 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1029 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1030 {
1031 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1032 *ecpy = *e;
1033 ecpy->postponed = 1;
1034 ecpy->session_index = s->session_index;
1035 }
Florin Coras54693d22018-07-17 10:46:29 -07001036 break;
1037 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001038 connected_msg = (session_connected_msg_t *) e->data;
1039 sid = vcl_session_connected_handler (wrk, connected_msg);
1040 if (!(s = vcl_session_get (wrk, sid)))
1041 break;
1042 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1043 {
1044 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1045 *ecpy = *e;
1046 ecpy->postponed = 1;
1047 ecpy->session_index = s->session_index;
1048 }
Florin Coras54693d22018-07-17 10:46:29 -07001049 break;
1050 case SESSION_CTRL_EVT_DISCONNECTED:
1051 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001052 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1053 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001054 if (s->session_state == VCL_STATE_CLOSED)
1055 break;
Florin Corasb242d312020-10-26 15:35:40 -07001056 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1057 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001058 s->session_state = VCL_STATE_VPP_CLOSING;
1059 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1060 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1061 *ecpy = *e;
1062 ecpy->postponed = 1;
1063 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001064 break;
1065 }
1066 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001067 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001068 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1069 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001070 break;
1071 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001072 reset_msg = (session_reset_msg_t *) e->data;
1073 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1074 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001075 if (s->session_state == VCL_STATE_CLOSED)
1076 break;
Florin Corasb242d312020-10-26 15:35:40 -07001077 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1078 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001079 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1080 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001081 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001082 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1083 *ecpy = *e;
1084 ecpy->postponed = 1;
1085 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001086 break;
1087 }
Florin Coras134a9962018-08-28 11:32:04 -07001088 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001089 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001090 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1091 vcl_session_unlisten_reply_handler (wrk, e->data);
1092 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001093 case SESSION_CTRL_EVT_MIGRATED:
1094 vcl_session_migrated_handler (wrk, e->data);
1095 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001096 case SESSION_CTRL_EVT_CLEANUP:
1097 vcl_session_cleanup_handler (wrk, e->data);
1098 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001099 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1100 vcl_session_req_worker_update_handler (wrk, e->data);
1101 break;
1102 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1103 vcl_session_worker_update_reply_handler (wrk, e->data);
1104 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001105 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1106 vcl_session_app_add_segment_handler (wrk, e->data);
1107 break;
1108 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1109 vcl_session_app_del_segment_handler (wrk, e->data);
1110 break;
hanlina3a48962020-07-13 11:09:15 +08001111 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001112 vcl_worker_rpc_handler (wrk, e->data);
1113 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001114 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1115 vcl_session_transport_attr_reply_handler (wrk, e->data);
1116 break;
Florin Coras54693d22018-07-17 10:46:29 -07001117 default:
1118 clib_warning ("unhandled %u", e->event_type);
1119 }
1120 return VPPCOM_OK;
1121}
1122
Florin Coras30e79c22019-01-02 19:31:22 -08001123static int
Florin Coras697faea2018-06-27 17:10:49 -07001124vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001125 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001126 f64 wait_for_time)
1127{
Florin Coras134a9962018-08-28 11:32:04 -07001128 vcl_worker_t *wrk = vcl_worker_get_current ();
1129 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001130 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001131 svm_msg_q_msg_t msg;
1132 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001133
Florin Coras697faea2018-06-27 17:10:49 -07001134 do
Dave Wallace543852a2017-08-03 02:11:34 -04001135 {
Florin Coras134a9962018-08-28 11:32:04 -07001136 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001137 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001138 {
Florin Coras070453d2018-08-24 17:04:27 -07001139 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001140 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001141 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001142 {
Florin Coras697faea2018-06-27 17:10:49 -07001143 return VPPCOM_OK;
1144 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001145 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001146 {
Florin Coras697faea2018-06-27 17:10:49 -07001147 return VPPCOM_ECONNREFUSED;
1148 }
Florin Coras54693d22018-07-17 10:46:29 -07001149
Florin Coras134a9962018-08-28 11:32:04 -07001150 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001151 {
1152 usleep (100);
1153 continue;
1154 }
Florin Coras134a9962018-08-28 11:32:04 -07001155 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001156 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001157 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001158 }
Florin Coras134a9962018-08-28 11:32:04 -07001159 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001160
Florin Coras05ecfcc2018-12-12 18:19:39 -08001161 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001162 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001163 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001164
Florin Coras697faea2018-06-27 17:10:49 -07001165 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001166}
1167
Florin Coras30e79c22019-01-02 19:31:22 -08001168static void
1169vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1170{
Florin Coras288eaab2019-02-03 15:26:14 -08001171 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001172 vcl_session_t *s;
1173 u32 *sip;
1174
1175 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1176 return;
1177
1178 vec_foreach (sip, wrk->pending_session_wrk_updates)
1179 {
1180 s = vcl_session_get (wrk, *sip);
1181 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1182 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001183 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1184 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001185 s->session_state = state;
1186 }
1187 vec_reset_length (wrk->pending_session_wrk_updates);
1188}
1189
Florin Corasf9240dc2019-01-15 08:03:17 -08001190void
Florin Coras5398dfb2021-01-25 20:31:27 -08001191vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001192{
Florin Coras30e79c22019-01-02 19:31:22 -08001193 svm_msg_q_msg_t *msg;
1194 session_event_t *e;
1195 svm_msg_q_t *mq;
1196 int i;
1197
1198 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001199 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001200
1201 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1202 {
1203 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1204 e = svm_msg_q_msg_data (mq, msg);
1205 vcl_handle_mq_event (wrk, e);
1206 svm_msg_q_free_msg (mq, msg);
1207 }
1208 vec_reset_length (wrk->mq_msg_vector);
1209 vcl_handle_pending_wrk_updates (wrk);
1210}
1211
Florin Coras5398dfb2021-01-25 20:31:27 -08001212void
1213vcl_flush_mq_events (void)
1214{
1215 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1216}
1217
Florin Coras697faea2018-06-27 17:10:49 -07001218static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001219vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001220{
Florin Coras134a9962018-08-28 11:32:04 -07001221 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001222 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001223 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001224 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001225
Florin Corasab2f6db2018-08-31 14:31:41 -07001226 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001227 if (!session)
1228 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001229
Florin Corasaaff5ee2019-07-08 13:00:00 -07001230 /* Flush pending accept events, if any */
1231 while (clib_fifo_elts (session->accept_evts_fifo))
1232 {
1233 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1234 accepted_msg = &evt->accepted_msg;
1235 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1236 vcl_send_session_accepted_reply (session->vpp_evt_q,
1237 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001238 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001239 }
1240 clib_fifo_free (session->accept_evts_fifo);
1241
Florin Coras458089b2019-08-21 16:20:44 -07001242 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001243
Florin Coras32881932023-02-06 13:30:13 -08001244 VDBG (0, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001245 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001246 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001247
1248 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001249 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001250
Florin Coras070453d2018-08-24 17:04:27 -07001251 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001252}
1253
Florin Coras940f78f2018-11-30 12:11:20 -08001254/**
1255 * Handle app exit
1256 *
1257 * Notify vpp of the disconnect and mark the worker as free. If we're the
1258 * last worker, do a full cleanup otherwise, since we're probably a forked
1259 * child, avoid syscalls as much as possible. We might've lost privileges.
1260 */
1261void
1262vppcom_app_exit (void)
1263{
1264 if (!pool_elts (vcm->workers))
1265 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001266 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1267 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001268 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001269}
1270
Florin Coras935ce752020-09-08 22:43:47 -07001271static int
1272vcl_api_attach (void)
1273{
1274 if (vcm->cfg.vpp_app_socket_api)
1275 return vcl_sapi_attach ();
1276
1277 return vcl_bapi_attach ();
1278}
1279
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001280int
1281vcl_is_first_reattach_to_execute ()
1282{
1283 if (vcm->reattach_count == 0)
1284 return 1;
1285
1286 return 0;
1287}
1288
1289void
1290vcl_set_reattach_counter ()
1291{
1292 ++vcm->reattach_count;
1293
1294 if (vcm->reattach_count == vec_len (vcm->workers))
1295 vcm->reattach_count = 0;
1296}
1297
1298/**
1299 * Reattach vcl to vpp after it has previously been disconnected.
1300 *
1301 * The logic should be:
1302 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1303 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1304 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1305 * from `vppcom_worker_register` since they were already allocated.
1306 */
1307
Florin Coras935ce752020-09-08 22:43:47 -07001308static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001309vcl_api_retry_attach (vcl_worker_t *wrk)
1310{
1311 vcl_session_t *s;
1312
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001313 clib_spinlock_lock (&vcm->workers_lock);
1314 if (vcl_is_first_reattach_to_execute ())
1315 {
1316 if (vcl_api_attach ())
1317 {
1318 clib_spinlock_unlock (&vcm->workers_lock);
1319 return;
1320 }
1321 vcl_set_reattach_counter ();
1322 clib_spinlock_unlock (&vcm->workers_lock);
1323 }
1324 else
1325 {
1326 vcl_set_reattach_counter ();
1327 clib_spinlock_unlock (&vcm->workers_lock);
1328 vcl_worker_register_with_vpp ();
1329 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001330
1331 /* Treat listeners as configuration that needs to be re-added to vpp */
1332 pool_foreach (s, wrk->sessions)
1333 {
1334 if (s->flags & VCL_SESSION_F_IS_VEP)
1335 continue;
1336 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1337 vppcom_session_listen (vcl_session_handle (s), 10);
1338 else
1339 VDBG (0, "internal error: unexpected state %d", s->session_state);
1340 }
1341}
1342
1343static void
1344vcl_api_handle_disconnect (vcl_worker_t *wrk)
1345{
1346 wrk->api_client_handle = ~0;
1347 vcl_worker_detach_sessions (wrk);
1348}
1349
1350static void
Florin Coras935ce752020-09-08 22:43:47 -07001351vcl_api_detach (vcl_worker_t * wrk)
1352{
Florin Corasd04ea442022-03-30 16:08:25 -07001353 if (wrk->api_client_handle == ~0)
1354 return;
1355
Florin Coras935ce752020-09-08 22:43:47 -07001356 vcl_send_app_detach (wrk);
1357
1358 if (vcm->cfg.vpp_app_socket_api)
1359 return vcl_sapi_detach (wrk);
1360
1361 return vcl_bapi_disconnect_from_vpp ();
1362}
1363
Dave Wallace543852a2017-08-03 02:11:34 -04001364/*
1365 * VPPCOM Public API functions
1366 */
1367int
Florin Coras66ec4672020-06-15 07:59:40 -07001368vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001369{
Dave Wallace543852a2017-08-03 02:11:34 -04001370 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001371 int rv;
1372
Florin Coras47c40e22018-11-26 17:01:36 -08001373 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001374 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001375 VDBG (1, "already initialized");
1376 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001377 }
1378
Florin Coras47c40e22018-11-26 17:01:36 -08001379 vcm->is_init = 1;
1380 vppcom_cfg (&vcm->cfg);
1381 vcl_cfg = &vcm->cfg;
1382
1383 vcm->main_cpu = pthread_self ();
1384 vcm->main_pid = getpid ();
1385 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001386 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1387 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001388 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1389 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001390 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001391 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001392 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001393
1394 /* Allocate default worker */
1395 vcl_worker_alloc_and_init ();
1396
Florin Coras935ce752020-09-08 22:43:47 -07001397 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001398 {
1399 vppcom_app_destroy ();
1400 return rv;
1401 }
Florin Coras47c40e22018-11-26 17:01:36 -08001402
1403 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001404 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001405
1406 return VPPCOM_OK;
1407}
1408
1409void
1410vppcom_app_destroy (void)
1411{
Florin Corasdc0ded72020-04-30 02:59:55 +00001412 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001413 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001414
Florin Coras940f78f2018-11-30 12:11:20 -08001415 if (!pool_elts (vcm->workers))
1416 return;
1417
Florin Coras0d427d82018-06-27 03:24:07 -07001418 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001419
Florin Corasdc0ded72020-04-30 02:59:55 +00001420 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001421
Florin Corasce815de2020-04-16 18:47:27 +00001422 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001423 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001424 if (current_wrk != wrk)
1425 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001426 }
Florin Corasce815de2020-04-16 18:47:27 +00001427 /* *INDENT-ON* */
1428
Florin Coras935ce752020-09-08 22:43:47 -07001429 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001430 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001431 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001432
Florin Coras0d427d82018-06-27 03:24:07 -07001433 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001434
1435 /*
1436 * Free the heap and fix vcm
1437 */
1438 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001439 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001440
1441 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001442 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001443}
1444
1445int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001446vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001447{
Florin Coras134a9962018-08-28 11:32:04 -07001448 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001449 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001450
Florin Coras134a9962018-08-28 11:32:04 -07001451 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001452
Florin Coras7e12d942018-06-27 14:32:43 -07001453 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001454 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001455 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001456 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301457 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001458
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001459 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001460 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001461
Florin Coras7e12d942018-06-27 14:32:43 -07001462 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1463 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001464
Florin Coras5e062572019-03-14 19:07:51 -07001465 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001466
Florin Coras134a9962018-08-28 11:32:04 -07001467 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001468}
1469
Florin Corasfe286f72021-06-04 10:07:55 -07001470static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001471vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001472{
Florin Corasfa3884f2021-06-22 20:04:46 -07001473 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001474
Florin Corasc2a14172023-02-28 21:13:50 -08001475 ASSERT (s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
1476
Florin Corasfa3884f2021-06-22 20:04:46 -07001477 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001478 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001479 wrk->ep_lt_current = s->session_index;
1480 s->vep.lt_next = s->session_index;
1481 s->vep.lt_prev = s->session_index;
1482 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001483 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001484
1485 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1486 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1487
1488 prev->vep.lt_next = s->session_index;
1489 s->vep.lt_prev = prev->session_index;
1490
1491 s->vep.lt_next = cur->session_index;
1492 cur->vep.lt_prev = s->session_index;
1493}
1494
1495static void
1496vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1497{
1498 vcl_session_t *prev, *next;
1499
Florin Corasc2a14172023-02-28 21:13:50 -08001500 ASSERT (s->vep.lt_next != VCL_INVALID_SESSION_INDEX);
1501
Florin Corasfa3884f2021-06-22 20:04:46 -07001502 if (s->vep.lt_next == s->session_index)
1503 {
1504 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1505 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001506 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfa3884f2021-06-22 20:04:46 -07001507 return;
1508 }
1509
1510 prev = vcl_session_get (wrk, s->vep.lt_prev);
1511 next = vcl_session_get (wrk, s->vep.lt_next);
1512
1513 prev->vep.lt_next = next->session_index;
1514 next->vep.lt_prev = prev->session_index;
1515
1516 if (s->session_index == wrk->ep_lt_current)
1517 wrk->ep_lt_current = s->vep.lt_next;
1518
1519 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001520 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001521}
1522
Dave Wallace543852a2017-08-03 02:11:34 -04001523int
Florin Corasc127d5a2020-10-14 16:35:58 -07001524vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001525 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001526{
Florin Coras134a9962018-08-28 11:32:04 -07001527 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001528
Florin Coras6c3b2182020-10-19 18:36:48 -07001529 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001530
Florin Coras6c3b2182020-10-19 18:36:48 -07001531 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001532 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001533 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001534 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001535 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001536 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001537 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001538 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001539 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1540 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001541 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001542 }
Florin Corase4306b62020-10-28 12:51:10 -07001543 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001544 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001545
Florin Coras6c3b2182020-10-19 18:36:48 -07001546 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001547 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001548 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001549 if (rv < 0)
1550 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001551 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1552 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001553 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001554
Florin Coras9ace36d2019-10-28 13:14:17 -07001555 if (!do_disconnect)
1556 {
1557 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001558 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001559 goto cleanup;
1560 }
1561
Florin Coras6c3b2182020-10-19 18:36:48 -07001562 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001563 {
1564 rv = vppcom_session_unbind (sh);
1565 if (PREDICT_FALSE (rv < 0))
1566 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001567 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001568 vppcom_retval_str (rv));
1569 return rv;
1570 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001571 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001572 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001573 {
1574 rv = vppcom_session_disconnect (sh);
1575 if (PREDICT_FALSE (rv < 0))
1576 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001577 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001578 rv, vppcom_retval_str (rv));
1579 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001580 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001581 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001582 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001583 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001584 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001585 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001586 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001587
1588 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1589 goto free_session;
1590
1591 /* Disconnect/reset messages pending but vpp transport and session
1592 * cleanups already done. Free only after messages drained. */
1593 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001594 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001595
Florin Corasc127d5a2020-10-14 16:35:58 -07001596 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001597
Florin Coras9ace36d2019-10-28 13:14:17 -07001598 /* Session is removed only after vpp confirms the disconnect */
1599 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001600
Florin Corasf9240dc2019-01-15 08:03:17 -08001601cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001602 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001603free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001604 vcl_session_free (wrk, s);
1605 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001606
Dave Wallace543852a2017-08-03 02:11:34 -04001607 return rv;
1608}
1609
1610int
Florin Corasf9240dc2019-01-15 08:03:17 -08001611vppcom_session_close (uint32_t session_handle)
1612{
1613 vcl_worker_t *wrk = vcl_worker_get_current ();
1614 vcl_session_t *session;
1615
1616 session = vcl_session_get_w_handle (wrk, session_handle);
1617 if (!session)
1618 return VPPCOM_EBADFD;
1619 return vcl_session_cleanup (wrk, session, session_handle,
1620 1 /* do_disconnect */ );
1621}
1622
1623int
Florin Coras134a9962018-08-28 11:32:04 -07001624vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001625{
Florin Coras134a9962018-08-28 11:32:04 -07001626 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001627 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001628
1629 if (!ep || !ep->ip)
1630 return VPPCOM_EINVAL;
1631
Florin Coras134a9962018-08-28 11:32:04 -07001632 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001633 if (!session)
1634 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001635
Florin Coras6c3b2182020-10-19 18:36:48 -07001636 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001637 {
Florin Coras5e062572019-03-14 19:07:51 -07001638 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1639 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001640 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001641 }
1642
Florin Coras7e12d942018-06-27 14:32:43 -07001643 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001644 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001645 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1646 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001647 else
Dave Barach178cf492018-11-13 16:34:13 -05001648 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1649 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001650 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001651
Florin Coras60687192021-11-29 21:00:47 -08001652 VDBG (0,
1653 "session %u handle %u: binding to local %s address %U port %u, "
1654 "proto %s",
1655 session->session_index, session_handle,
1656 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1657 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001658 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1659 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001660 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001661 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001662
1663 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001664 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001665
Florin Coras070453d2018-08-24 17:04:27 -07001666 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001667}
1668
1669int
Florin Coras134a9962018-08-28 11:32:04 -07001670vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001671{
Florin Coras134a9962018-08-28 11:32:04 -07001672 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001673 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001674 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001675 int rv;
1676
Florin Coras134a9962018-08-28 11:32:04 -07001677 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001678 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001679 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001680
Dave Wallaceee45d412017-11-24 21:44:06 -05001681 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001682 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001683 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001684 VDBG (0, "session %u [0x%llx]: already in listen state!",
1685 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001686 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001687 }
1688
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001689 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001690
Florin Coras070453d2018-08-24 17:04:27 -07001691 /*
1692 * Send listen request to vpp and wait for reply
1693 */
Florin Coras458089b2019-08-21 16:20:44 -07001694 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001695 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001696 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001697 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001698
Florin Coras070453d2018-08-24 17:04:27 -07001699 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001700 {
Florin Coras134a9962018-08-28 11:32:04 -07001701 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001702 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1703 listen_sh, listen_session->vpp_handle, rv,
1704 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001705 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001706 }
1707
Florin Coras070453d2018-08-24 17:04:27 -07001708 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001709}
1710
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001711int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001712vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1713{
1714 if (!strcmp (proto_str, "TCP"))
1715 *proto = VPPCOM_PROTO_TCP;
1716 else if (!strcmp (proto_str, "tcp"))
1717 *proto = VPPCOM_PROTO_TCP;
1718 else if (!strcmp (proto_str, "UDP"))
1719 *proto = VPPCOM_PROTO_UDP;
1720 else if (!strcmp (proto_str, "udp"))
1721 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001722 else if (!strcmp (proto_str, "TLS"))
1723 *proto = VPPCOM_PROTO_TLS;
1724 else if (!strcmp (proto_str, "tls"))
1725 *proto = VPPCOM_PROTO_TLS;
1726 else if (!strcmp (proto_str, "QUIC"))
1727 *proto = VPPCOM_PROTO_QUIC;
1728 else if (!strcmp (proto_str, "quic"))
1729 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001730 else if (!strcmp (proto_str, "DTLS"))
1731 *proto = VPPCOM_PROTO_DTLS;
1732 else if (!strcmp (proto_str, "dtls"))
1733 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001734 else if (!strcmp (proto_str, "SRTP"))
1735 *proto = VPPCOM_PROTO_SRTP;
1736 else if (!strcmp (proto_str, "srtp"))
1737 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001738 else
1739 return 1;
1740 return 0;
1741}
1742
1743int
Florin Coras32881932023-02-06 13:30:13 -08001744vppcom_session_accept (uint32_t ls_handle, vppcom_endpt_t *ep, uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001745{
Florin Coras32881932023-02-06 13:30:13 -08001746 u32 client_session_index = ~0, ls_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001747 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001748 session_accepted_msg_t accepted_msg;
Florin Coras32881932023-02-06 13:30:13 -08001749 vcl_session_t *ls, *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001750 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001751 u8 is_nonblocking;
Dave Wallace543852a2017-08-03 02:11:34 -04001752
Florin Coras5398dfb2021-01-25 20:31:27 -08001753again:
1754
Florin Coras32881932023-02-06 13:30:13 -08001755 ls = vcl_session_get_w_handle (wrk, ls_handle);
1756 if (!ls)
Florin Coras070453d2018-08-24 17:04:27 -07001757 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001758
Florin Coras32881932023-02-06 13:30:13 -08001759 if ((ls->session_state != VCL_STATE_LISTEN) &&
1760 (ls->session_state != VCL_STATE_LISTEN_NO_MQ) &&
1761 (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001762 {
Florin Coras32881932023-02-06 13:30:13 -08001763 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state (%s)",
1764 ls->vpp_handle, vcl_session_state_str (ls->session_state));
1765 return VPPCOM_EBADFD;
1766 }
1767
1768 ls_index = ls->session_index;
1769
1770 if (clib_fifo_elts (ls->accept_evts_fifo))
1771 {
1772 clib_fifo_sub2 (ls->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001773 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001774 accepted_msg = evt->accepted_msg;
1775 goto handle;
1776 }
1777
Florin Coras32881932023-02-06 13:30:13 -08001778 is_nonblocking = vcl_session_has_attr (ls, VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001779 while (1)
1780 {
Carl Smith592a9092019-11-12 14:57:37 +13001781 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1782 return VPPCOM_EAGAIN;
1783
Florin Coras5398dfb2021-01-25 20:31:27 -08001784 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1785 vcl_worker_flush_mq_events (wrk);
1786 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001787 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001788
Florin Coras54693d22018-07-17 10:46:29 -07001789handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001790
Florin Coras32881932023-02-06 13:30:13 -08001791 client_session_index =
1792 vcl_session_accepted_handler (wrk, &accepted_msg, ls_index);
Florin Coras00cca802019-06-06 09:38:44 -07001793 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1794 return VPPCOM_ECONNABORTED;
1795
Florin Coras32881932023-02-06 13:30:13 -08001796 ls = vcl_session_get (wrk, ls_index);
Florin Coras134a9962018-08-28 11:32:04 -07001797 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001798
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001799 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001800 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001801
Florin Coras32881932023-02-06 13:30:13 -08001802 VDBG (1,
1803 "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1804 " flags %d, is_nonblocking %u",
1805 ls->session_index, ls->vpp_handle, client_session_index,
Florin Coras5e062572019-03-14 19:07:51 -07001806 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001807 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001808
Dave Wallace048b1d62018-01-03 22:24:41 -05001809 if (ep)
1810 {
Florin Coras7e12d942018-06-27 14:32:43 -07001811 ep->is_ip4 = client_session->transport.is_ip4;
1812 ep->port = client_session->transport.rmt_port;
1813 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001814 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1815 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001816 else
Dave Barach178cf492018-11-13 16:34:13 -05001817 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1818 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001819 }
Dave Wallace60caa062017-11-10 17:07:13 -05001820
Florin Coras60687192021-11-29 21:00:47 -08001821 VDBG (0,
1822 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1823 "local: %U:%u",
Florin Coras32881932023-02-06 13:30:13 -08001824 ls_handle, ls->vpp_handle, client_session_index,
1825 client_session->vpp_handle, vcl_format_ip46_address,
1826 &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001827 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001828 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001829 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001830 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001831 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras32881932023-02-06 13:30:13 -08001832 vcl_evt (VCL_EVT_ACCEPT, client_session, ls, client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001833
Florin Coras3c7d4f92018-12-14 11:28:43 -08001834 /*
1835 * Session might have been closed already
1836 */
1837 if (accept_flags)
1838 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001839 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001840 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001841 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001842 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001843 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001844 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001845}
1846
1847int
Florin Coras134a9962018-08-28 11:32:04 -07001848vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001849{
Florin Coras134a9962018-08-28 11:32:04 -07001850 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001851 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001852 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001853 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001854
Florin Coras134a9962018-08-28 11:32:04 -07001855 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001856 if (!session)
1857 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001858 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001859
Florin Coras6c3b2182020-10-19 18:36:48 -07001860 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001861 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001862 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001863 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001864 }
1865
Florin Corasc127d5a2020-10-14 16:35:58 -07001866 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001867 {
Florin Coras60687192021-11-29 21:00:47 -08001868 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001869 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1870 " state (%s)",
1871 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001872 vcl_format_ip46_address, &session->transport.rmt_ip,
1873 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001874 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001875 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001876 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001877 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001878 }
1879
Florin Coras0a1e1832020-03-29 18:54:04 +00001880 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001881 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001882 {
1883 if (session->session_type != VPPCOM_PROTO_UDP)
1884 return VPPCOM_EINVAL;
1885 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001886 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001887 }
1888
Florin Coras7e12d942018-06-27 14:32:43 -07001889 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001890 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001891 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001892 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001893 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001894
Florin Coras5ffb9642021-12-03 17:24:13 -08001895 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1896 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001897 &session->transport.rmt_ip,
1898 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001899 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001900 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001901
Florin Coras458089b2019-08-21 16:20:44 -07001902 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001903
Florin Corasac422d62020-10-19 20:51:36 -07001904 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001905 {
fanyfa49d59d2020-10-13 17:07:16 +08001906 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001907 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001908 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001909 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001910 return VPPCOM_EINPROGRESS;
1911 }
Florin Coras57c88932019-08-29 12:03:17 -07001912
1913 /*
1914 * Wait for reply from vpp if blocking
1915 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001916 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001917 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001918
Florin Coras134a9962018-08-28 11:32:04 -07001919 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001920 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1921 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001922
Dave Wallace4878cbe2017-11-21 03:45:09 -05001923 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001924}
1925
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001926int
1927vppcom_session_stream_connect (uint32_t session_handle,
1928 uint32_t parent_session_handle)
1929{
1930 vcl_worker_t *wrk = vcl_worker_get_current ();
1931 vcl_session_t *session, *parent_session;
1932 u32 session_index, parent_session_index;
1933 int rv;
1934
1935 session = vcl_session_get_w_handle (wrk, session_handle);
1936 if (!session)
1937 return VPPCOM_EBADFD;
1938 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1939 if (!parent_session)
1940 return VPPCOM_EBADFD;
1941
1942 session_index = session->session_index;
1943 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001944 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001945 {
1946 VDBG (0, "ERROR: cannot connect epoll session %u!",
1947 session->session_index);
1948 return VPPCOM_EBADFD;
1949 }
1950
Florin Corasc127d5a2020-10-14 16:35:58 -07001951 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001952 {
Florin Coras60687192021-11-29 21:00:47 -08001953 VDBG (0,
1954 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001955 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001956 session_handle, session->vpp_handle, parent_session_handle,
1957 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001958 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001959 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001960 return VPPCOM_OK;
1961 }
1962
1963 /* Connect to quic session specifics */
1964 session->transport.is_ip4 = parent_session->transport.is_ip4;
1965 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1966 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001967 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001968
1969 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1970 session_handle, parent_session_handle, parent_session->vpp_handle);
1971
1972 /*
1973 * Send connect request and wait for reply from vpp
1974 */
Florin Coras458089b2019-08-21 16:20:44 -07001975 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001976 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001977 vcm->cfg.session_timeout);
1978
1979 session->listener_index = parent_session_index;
1980 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001981 if (parent_session)
1982 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001983
1984 session = vcl_session_get (wrk, session_index);
1985 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1986 session->vpp_handle, rv ? "failed" : "succeeded");
1987
1988 return rv;
1989}
1990
Steven58f464e2017-10-25 12:33:12 -07001991static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001992vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001993 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001994{
Florin Coras134a9962018-08-28 11:32:04 -07001995 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001996 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001997 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001998 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001999 session_event_t *e;
2000 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07002001 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002002
Florin Coras070453d2018-08-24 17:04:27 -07002003 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08002004 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002005
Florin Coras134a9962018-08-28 11:32:04 -07002006 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002007 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07002008 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002009
Florin Coras6d0106e2019-01-29 20:11:58 -08002010 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002011 {
Florin Coras5e062572019-03-14 19:07:51 -07002012 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002013 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002014 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002015 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002016 }
2017
liuyacan55c952e2021-06-13 14:54:55 +08002018 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2019 {
2020 /* Vpp would ack the incoming data and enqueue it for reading.
2021 * So even if SHUT_RD is set, we can still read() the data if
2022 * the session is ready.
2023 */
2024 if (!vcl_session_read_ready (s))
2025 {
2026 return 0;
2027 }
2028 }
2029
Florin Corasac422d62020-10-19 20:51:36 -07002030 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002031 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002032 mq = wrk->app_event_queue;
2033 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002034 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002035
Sirshak Das28aa5392019-02-05 01:33:33 -06002036 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002037 {
Florin Coras54693d22018-07-17 10:46:29 -07002038 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002039 {
Yu Pingb2955352019-12-04 06:49:04 +08002040 if (vcl_session_is_closing (s))
2041 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002042 if (is_ct)
2043 svm_fifo_unset_event (s->rx_fifo);
2044 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002045 return VPPCOM_EWOULDBLOCK;
2046 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002047 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002048 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002049 if (vcl_session_is_closing (s))
2050 return vcl_session_closing_error (s);
2051
Florin Corasd6894562020-10-28 00:37:15 -07002052 if (is_ct)
2053 svm_fifo_unset_event (s->rx_fifo);
2054 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002055
Florin Coras5398dfb2021-01-25 20:31:27 -08002056 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2057 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002058 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002059 }
Florin Coras54693d22018-07-17 10:46:29 -07002060
Florin Coras4a2c7942020-09-10 12:27:14 -07002061read_again:
2062
Florin Coras460dce62018-07-27 05:45:06 -07002063 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002064 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002065 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002066 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2067
2068 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002069
2070 if (peek)
2071 return rv;
2072
Florin Coras4a2c7942020-09-10 12:27:14 -07002073 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002074
Sirshak Das28aa5392019-02-05 01:33:33 -06002075 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002076 {
Florin Corasd6894562020-10-28 00:37:15 -07002077 if (is_ct)
2078 svm_fifo_unset_event (s->rx_fifo);
2079 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002080 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002081 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002082 {
Florin Corasc34118b2020-08-12 18:58:25 -07002083 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2084 e->event_type = SESSION_IO_EVT_RX;
2085 e->session_index = s->session_index;
2086 }
2087 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002088 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002089 {
2090 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002091 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002092 buf += rv;
2093 n -= rv;
2094 goto read_again;
2095 }
Florin Coras41c9e042018-09-11 00:10:41 -07002096
Florin Coras17ec5772020-08-12 20:46:29 -07002097 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002098 {
Florin Coras17ec5772020-08-12 20:46:29 -07002099 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002100 app_send_io_evt_to_vpp (s->vpp_evt_q,
2101 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002102 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002103 }
2104
Florin Coras5e062572019-03-14 19:07:51 -07002105 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2106 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002107
Florin Coras54693d22018-07-17 10:46:29 -07002108 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002109}
2110
Steven58f464e2017-10-25 12:33:12 -07002111int
Florin Coras134a9962018-08-28 11:32:04 -07002112vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002113{
Florin Coras134a9962018-08-28 11:32:04 -07002114 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002115}
2116
2117static int
Florin Coras134a9962018-08-28 11:32:04 -07002118vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002119{
Florin Coras134a9962018-08-28 11:32:04 -07002120 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002121}
2122
Florin Coras2cba8532018-09-11 16:33:36 -07002123int
2124vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002125 vppcom_data_segment_t * ds, uint32_t n_segments,
2126 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002127{
2128 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002129 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002130 vcl_session_t *s = 0;
2131 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002132 svm_msg_q_t *mq;
2133 u8 is_ct;
2134
2135 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002136 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002137 return VPPCOM_EBADFD;
2138
Florin Coras6d0106e2019-01-29 20:11:58 -08002139 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2140 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002141
Florin Corasac422d62020-10-19 20:51:36 -07002142 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002143 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002144 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002145 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002146 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002147
Sirshak Das28aa5392019-02-05 01:33:33 -06002148 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002149 {
2150 if (is_nonblocking)
2151 {
Florin Coras62877022020-10-28 21:22:04 -07002152 if (is_ct)
2153 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002154 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002155 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002156 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002157 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002158 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002159 if (vcl_session_is_closing (s))
2160 return vcl_session_closing_error (s);
2161
Florin Coras62877022020-10-28 21:22:04 -07002162 if (is_ct)
2163 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002164 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002165
Florin Coras5398dfb2021-01-25 20:31:27 -08002166 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2167 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002168 }
2169 }
2170
Florin Corasd1cc38d2020-10-11 11:05:04 -07002171 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002172 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002173 if (n_read < 0)
2174 return VPPCOM_EAGAIN;
2175
2176 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2177 {
Florin Coras62877022020-10-28 21:22:04 -07002178 if (is_ct)
2179 svm_fifo_unset_event (s->rx_fifo);
2180 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002181 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002182 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002183 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002184 {
2185 session_event_t *e;
2186 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2187 e->event_type = SESSION_IO_EVT_RX;
2188 e->session_index = s->session_index;
2189 }
2190 }
2191
2192 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002193 return n_read;
2194}
2195
2196void
Florin Corasd68faf82020-09-25 15:18:13 -07002197vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002198{
2199 vcl_worker_t *wrk = vcl_worker_get_current ();
2200 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002201 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002202
2203 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002204 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002205 return;
2206
Florin Coras62877022020-10-28 21:22:04 -07002207 is_ct = vcl_session_is_ct (s);
2208 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002209
Florin Coras8cc93212021-09-10 11:37:12 -07002210 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002211 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002212}
2213
Florin Coras7a2abce2020-04-05 19:25:44 +00002214always_inline u8
2215vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002216{
Florin Coras7a2abce2020-04-05 19:25:44 +00002217 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2218 if (is_dgram)
2219 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2220 else
2221 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002222}
2223
2224always_inline int
Dou Chao243a0432022-11-29 19:41:34 +08002225vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002226 size_t n, u8 is_flush, u8 is_dgram)
Florin Coras7a2abce2020-04-05 19:25:44 +00002227{
Florin Coras6d0106e2019-01-29 20:11:58 -08002228 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002229 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002230 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002231 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002232 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002233
Florin Coras0b0d28e2021-06-04 17:31:53 -07002234 /* Accept zero length writes but just return */
2235 if (PREDICT_FALSE (!n))
2236 return VPPCOM_OK;
2237
2238 if (PREDICT_FALSE (!buf))
2239 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002240
Florin Coras6c3b2182020-10-19 18:36:48 -07002241 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002242 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002243 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2244 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002245 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002246 }
2247
liuyacan55c952e2021-06-13 14:54:55 +08002248 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002249 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002250 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2251 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002252 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002253 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002254 }
2255
liuyacan55c952e2021-06-13 14:54:55 +08002256 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2257 {
2258 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2259 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002260 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002261 return VPPCOM_EPIPE;
2262 }
2263
Florin Coras0e88e852018-09-17 22:09:02 -07002264 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002265 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002266 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002267
Florin Coras653e43f2019-03-04 10:56:23 -08002268 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002269 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002270 {
Florin Coras54693d22018-07-17 10:46:29 -07002271 if (is_nonblocking)
2272 {
Florin Coras070453d2018-08-24 17:04:27 -07002273 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002274 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002275 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002276 {
Florin Coras2d379d82019-06-28 12:45:12 -07002277 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002278 if (vcl_session_is_closing (s))
2279 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002280
Florin Coras5398dfb2021-01-25 20:31:27 -08002281 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2282 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002283 }
Dave Wallace543852a2017-08-03 02:11:34 -04002284 }
Dave Wallace543852a2017-08-03 02:11:34 -04002285
Florin Coras653e43f2019-03-04 10:56:23 -08002286 et = SESSION_IO_EVT_TX;
2287 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002288 et = SESSION_IO_EVT_TX_FLUSH;
2289
Florin Coras7a2abce2020-04-05 19:25:44 +00002290 if (is_dgram)
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002291 {
2292 et = vcl_session_dgram_tx_evt (s, et);
2293 n_write =
2294 app_send_dgram_raw_gso (tx_fifo, &s->transport, s->vpp_evt_q, buf, n,
2295 s->gso_size, et, 0 /* do_evt */, SVM_Q_WAIT);
2296 }
Florin Coras460dce62018-07-27 05:45:06 -07002297 else
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002298 {
2299 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
2300 0 /* do_evt */, SVM_Q_WAIT);
2301 }
Florin Coras653e43f2019-03-04 10:56:23 -08002302
Florin Coras14ed6df2019-03-06 21:13:42 -08002303 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002304 app_send_io_evt_to_vpp (
2305 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002306
Florin Coras56230092020-09-02 20:52:58 -07002307 /* The underlying fifo segment can run out of memory */
2308 if (PREDICT_FALSE (n_write < 0))
2309 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002310
Florin Coras6d0106e2019-01-29 20:11:58 -08002311 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2312 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002313
Florin Coras54693d22018-07-17 10:46:29 -07002314 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002315}
2316
Florin Coras42ceddb2018-12-12 10:56:01 -08002317int
2318vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2319{
Florin Coras7a2abce2020-04-05 19:25:44 +00002320 vcl_worker_t *wrk = vcl_worker_get_current ();
2321 vcl_session_t *s;
2322
2323 s = vcl_session_get_w_handle (wrk, session_handle);
2324 if (PREDICT_FALSE (!s))
2325 return VPPCOM_EBADFD;
2326
Florin Coraseff5f7a2023-02-07 17:36:17 -08002327 return vppcom_session_write_inline (wrk, s, buf, n, 0 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002328 s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002329}
2330
Florin Corasb0f662f2018-12-27 14:51:46 -08002331int
2332vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2333{
Florin Coras7a2abce2020-04-05 19:25:44 +00002334 vcl_worker_t *wrk = vcl_worker_get_current ();
2335 vcl_session_t *s;
2336
2337 s = vcl_session_get_w_handle (wrk, session_handle);
2338 if (PREDICT_FALSE (!s))
2339 return VPPCOM_EBADFD;
2340
Florin Coraseff5f7a2023-02-07 17:36:17 -08002341 return vppcom_session_write_inline (wrk, s, buf, n, 1 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002342 s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002343}
2344
Florin Corasc0737e92019-03-04 14:19:39 -08002345#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002346if (PREDICT_FALSE (!_s->rx_fifo)) \
2347 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002348if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2349 { \
2350 if (!vcl_session_is_ct (_s)) \
2351 { \
2352 svm_fifo_unset_event (_s->rx_fifo); \
2353 if (svm_fifo_is_empty (_s->rx_fifo)) \
2354 break; \
2355 } \
2356 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2357 { \
Florin Coras2f630182020-08-13 00:17:51 -07002358 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002359 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2360 break; \
2361 } \
2362 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002363
Florin Coras86f04502018-09-12 16:08:01 -07002364static void
2365vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2366 unsigned long n_bits, unsigned long *read_map,
2367 unsigned long *write_map,
2368 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002369{
2370 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002371 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002372 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002373 u32 sid;
2374
2375 switch (e->event_type)
2376 {
Florin Corasc0737e92019-03-04 14:19:39 -08002377 case SESSION_IO_EVT_RX:
2378 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002379 s = vcl_session_get (wrk, sid);
2380 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002381 break;
Florin Corasb242d312020-10-26 15:35:40 -07002382 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002383 if (sid < n_bits && read_map)
2384 {
David Johnsond9818dd2018-12-14 14:53:41 -05002385 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002386 *bits_set += 1;
2387 }
2388 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002389 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002390 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002391 s = vcl_session_get (wrk, sid);
2392 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002393 break;
2394 if (sid < n_bits && write_map)
2395 {
David Johnsond9818dd2018-12-14 14:53:41 -05002396 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002397 *bits_set += 1;
2398 }
2399 break;
Florin Coras86f04502018-09-12 16:08:01 -07002400 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002401 if (!e->postponed)
2402 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2403 else
2404 s = vcl_session_get (wrk, e->session_index);
2405 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002406 break;
Florin Corasb242d312020-10-26 15:35:40 -07002407 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002408 if (sid < n_bits && read_map)
2409 {
David Johnsond9818dd2018-12-14 14:53:41 -05002410 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002411 *bits_set += 1;
2412 }
2413 break;
2414 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002415 if (!e->postponed)
2416 {
2417 connected_msg = (session_connected_msg_t *) e->data;
2418 sid = vcl_session_connected_handler (wrk, connected_msg);
2419 }
2420 else
2421 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002422 if (sid == VCL_INVALID_SESSION_INDEX)
2423 break;
Florin Coras66c675e2023-03-09 16:43:02 -08002424 if (!(sid < n_bits && write_map))
2425 break;
2426 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2427 *bits_set += 1;
2428 s = vcl_session_get (wrk, sid);
Florin Coras66c675e2023-03-09 16:43:02 -08002429 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07002430 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras86f04502018-09-12 16:08:01 -07002431 break;
2432 case SESSION_CTRL_EVT_DISCONNECTED:
2433 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002434 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2435 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002436 break;
Florin Corasb242d312020-10-26 15:35:40 -07002437 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002438 if (sid < n_bits && except_map)
2439 {
David Johnsond9818dd2018-12-14 14:53:41 -05002440 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002441 *bits_set += 1;
2442 }
2443 break;
2444 case SESSION_CTRL_EVT_RESET:
2445 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2446 if (sid < n_bits && except_map)
2447 {
David Johnsond9818dd2018-12-14 14:53:41 -05002448 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002449 *bits_set += 1;
2450 }
2451 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002452 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2453 vcl_session_unlisten_reply_handler (wrk, e->data);
2454 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002455 case SESSION_CTRL_EVT_MIGRATED:
2456 vcl_session_migrated_handler (wrk, e->data);
2457 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002458 case SESSION_CTRL_EVT_CLEANUP:
2459 vcl_session_cleanup_handler (wrk, e->data);
2460 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002461 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2462 vcl_session_worker_update_reply_handler (wrk, e->data);
2463 break;
2464 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2465 vcl_session_req_worker_update_handler (wrk, e->data);
2466 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002467 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2468 vcl_session_app_add_segment_handler (wrk, e->data);
2469 break;
2470 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2471 vcl_session_app_del_segment_handler (wrk, e->data);
2472 break;
hanlina3a48962020-07-13 11:09:15 +08002473 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002474 vcl_worker_rpc_handler (wrk, e->data);
2475 break;
Florin Coras86f04502018-09-12 16:08:01 -07002476 default:
2477 clib_warning ("unhandled: %u", e->event_type);
2478 break;
2479 }
2480}
2481
2482static int
2483vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2484 unsigned long n_bits, unsigned long *read_map,
2485 unsigned long *write_map, unsigned long *except_map,
2486 double time_to_wait, u32 * bits_set)
2487{
Florin Coras99368312018-08-02 10:45:44 -07002488 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002489 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002490 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002491
Florin Coras54693d22018-07-17 10:46:29 -07002492 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002493 {
Florin Coras54693d22018-07-17 10:46:29 -07002494 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002495 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002496
Florin Coras54693d22018-07-17 10:46:29 -07002497 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002498 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002499 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002500 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002501 else
2502 {
2503 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002504 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002505 }
2506 }
Florin Corase003a1b2019-06-05 10:47:16 -07002507 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002508
Florin Coras134a9962018-08-28 11:32:04 -07002509 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002510 {
Florin Coras134a9962018-08-28 11:32:04 -07002511 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002512 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002513 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2514 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002515 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002516 }
Florin Coras134a9962018-08-28 11:32:04 -07002517 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002518 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002519 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002520}
2521
Florin Coras99368312018-08-02 10:45:44 -07002522static int
Florin Coras294afe22019-01-07 17:49:17 -08002523vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2524 vcl_si_set * read_map, vcl_si_set * write_map,
2525 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002526 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002527{
Florin Coras14ed6df2019-03-06 21:13:42 -08002528 double wait = 0, start = 0;
2529
2530 if (!*bits_set)
2531 {
2532 wait = time_to_wait;
2533 start = clib_time_now (&wrk->clib_time);
2534 }
2535
2536 do
2537 {
2538 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2539 write_map, except_map, wait, bits_set);
2540 if (*bits_set)
2541 return *bits_set;
2542 if (wait == -1)
2543 continue;
2544
2545 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2546 }
2547 while (wait > 0);
2548
2549 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002550}
2551
2552static int
Florin Coras294afe22019-01-07 17:49:17 -08002553vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2554 vcl_si_set * read_map, vcl_si_set * write_map,
2555 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002556 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002557{
2558 vcl_mq_evt_conn_t *mqc;
2559 int __clib_unused n_read;
2560 int n_mq_evts, i;
2561 u64 buf;
2562
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002563 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2564 {
2565 vcl_api_retry_attach (wrk);
2566 return 0;
2567 }
2568
Florin Coras134a9962018-08-28 11:32:04 -07002569 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2570 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2571 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002572 for (i = 0; i < n_mq_evts; i++)
2573 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002574 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2575 {
2576 vcl_api_handle_disconnect (wrk);
2577 continue;
2578 }
2579
Florin Coras134a9962018-08-28 11:32:04 -07002580 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002581 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002582 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002583 except_map, 0, bits_set);
2584 }
2585
2586 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2587}
2588
Dave Wallace543852a2017-08-03 02:11:34 -04002589int
Florin Coras294afe22019-01-07 17:49:17 -08002590vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2591 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002592{
Florin Coras54693d22018-07-17 10:46:29 -07002593 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002594 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002595 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002596 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002597
Dave Wallace7876d392017-10-19 03:53:57 -04002598 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002599 {
Florin Coras134a9962018-08-28 11:32:04 -07002600 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002601 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002602 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2603 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002604 }
Dave Wallace7876d392017-10-19 03:53:57 -04002605 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002606 {
Florin Coras134a9962018-08-28 11:32:04 -07002607 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002608 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002609 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2610 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002611 }
Dave Wallace7876d392017-10-19 03:53:57 -04002612 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002613 {
Florin Coras134a9962018-08-28 11:32:04 -07002614 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002615 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002616 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2617 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002618 }
2619
Florin Coras54693d22018-07-17 10:46:29 -07002620 if (!n_bits)
2621 return 0;
2622
2623 if (!write_map)
2624 goto check_rd;
2625
Florin Coras096a1d52021-01-27 15:33:51 -08002626 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2627 {
2628 if (!(s = vcl_session_get (wrk, sid)))
2629 {
2630 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2631 bits_set++;
2632 continue;
2633 }
Florin Coras54693d22018-07-17 10:46:29 -07002634
Florin Coras096a1d52021-01-27 15:33:51 -08002635 if (vcl_session_write_ready (s))
2636 {
2637 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2638 bits_set++;
2639 }
Florin Coras607eb202023-05-29 16:19:38 -07002640 else
Florin Coras096a1d52021-01-27 15:33:51 -08002641 {
Florin Coras607eb202023-05-29 16:19:38 -07002642 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras096a1d52021-01-27 15:33:51 -08002643 }
2644 }
Florin Coras54693d22018-07-17 10:46:29 -07002645
2646check_rd:
2647 if (!read_map)
2648 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002649
Florin Coras096a1d52021-01-27 15:33:51 -08002650 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2651 {
2652 if (!(s = vcl_session_get (wrk, sid)))
2653 {
2654 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2655 bits_set++;
2656 continue;
2657 }
Florin Coras54693d22018-07-17 10:46:29 -07002658
Florin Coras096a1d52021-01-27 15:33:51 -08002659 if (vcl_session_read_ready (s))
2660 {
2661 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2662 bits_set++;
2663 }
2664 }
Florin Coras54693d22018-07-17 10:46:29 -07002665
2666check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002667
Florin Coras86f04502018-09-12 16:08:01 -07002668 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2669 {
2670 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2671 read_map, write_map, except_map, &bits_set);
2672 }
2673 vec_reset_length (wrk->unhandled_evts_vector);
2674
Florin Coras99368312018-08-02 10:45:44 -07002675 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002676 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002677 time_to_wait, &bits_set);
2678 else
Florin Coras134a9962018-08-28 11:32:04 -07002679 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002680 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002681
Dave Wallace543852a2017-08-03 02:11:34 -04002682 return (bits_set);
2683}
2684
Dave Wallacef7f809c2017-10-03 01:48:42 -04002685static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002686vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002689 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002690 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002692 if (VPPCOM_DEBUG <= 3)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002693 return;
2694
Florin Coras6c3b2182020-10-19 18:36:48 -07002695 s = vcl_session_get_w_handle (wrk, vep_handle);
2696 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002698 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002699 goto done;
2700 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002701 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002702 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002703 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002704 goto done;
2705 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002706 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002707 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002708 "{\n"
2709 " is_vep = %u\n"
2710 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002711 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002712 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2713 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002714
Florin Coras7e5e62b2019-07-30 14:08:23 -07002715 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002716 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002717 s = vcl_session_get_w_handle (wrk, sh);
2718 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002719 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002720 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002721 goto done;
2722 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002723 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002724 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002725 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002726 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002727 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002728 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002729 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002730 goto done;
2731 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002732 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002733 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2734 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002735 sh, s->vep.vep_sh, vep_handle);
2736 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002737 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002738 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002739 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002740 " next_sh = 0x%x (%u)\n"
2741 " prev_sh = 0x%x (%u)\n"
2742 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002743 " ev.events = 0x%x\n"
2744 " ev.data.u64 = 0x%llx\n"
2745 " et_mask = 0x%x\n"
2746 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002747 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002748 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2749 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002750 }
2751 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002752
2753done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002754 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002755}
2756
2757int
2758vppcom_epoll_create (void)
2759{
Florin Coras134a9962018-08-28 11:32:04 -07002760 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002761 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002762
Florin Coras134a9962018-08-28 11:32:04 -07002763 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002764
Florin Coras6c3b2182020-10-19 18:36:48 -07002765 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002766 vep_session->vep.vep_sh = ~0;
2767 vep_session->vep.next_sh = ~0;
2768 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002769 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002770
Florin Corasa7a1a222018-12-30 17:11:31 -08002771 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2772 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002773
Florin Corasab2f6db2018-08-31 14:31:41 -07002774 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002775}
2776
Florin Corasb0116a12023-02-07 09:11:47 -08002777static void
2778vcl_epoll_ctl_add_unhandled_event (vcl_worker_t *wrk, vcl_session_t *s,
2779 u8 is_epollet, session_evt_type_t evt)
2780{
2781 if (!is_epollet)
2782 {
Florin Corasc2a14172023-02-28 21:13:50 -08002783 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
2784 vcl_epoll_lt_add (wrk, s);
Florin Corasb0116a12023-02-07 09:11:47 -08002785 return;
2786 }
2787
2788 session_event_t e = { 0 };
2789 e.session_index = s->session_index;
2790 e.event_type = evt;
2791 if (evt == SESSION_IO_EVT_RX)
2792 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2793 vec_add1 (wrk->unhandled_evts_vector, e);
2794}
2795
Dave Wallacef7f809c2017-10-03 01:48:42 -04002796int
Florin Coras134a9962018-08-28 11:32:04 -07002797vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002798 struct epoll_event *event)
2799{
Florin Coras134a9962018-08-28 11:32:04 -07002800 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002801 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002802 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002803 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002804
Florin Coras134a9962018-08-28 11:32:04 -07002805 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002806 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002807 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002808 return VPPCOM_EINVAL;
2809 }
2810
Florin Coras134a9962018-08-28 11:32:04 -07002811 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002812 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002813 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002814 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002815 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002816 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002817 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002818 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002819 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002820 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002821 }
2822
Florin Coras134a9962018-08-28 11:32:04 -07002823 ASSERT (vep_session->vep.vep_sh == ~0);
2824 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002825
Florin Coras17ec5772020-08-12 20:46:29 -07002826 s = vcl_session_get_w_handle (wrk, session_handle);
2827 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002828 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002829 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002830 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002831 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002832 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002833 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002834 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002835 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002836 }
2837
2838 switch (op)
2839 {
2840 case EPOLL_CTL_ADD:
2841 if (PREDICT_FALSE (!event))
2842 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002843 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002844 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002845 }
Florin Coras2645f682021-06-04 15:59:41 -07002846 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2847 {
2848 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2849 rv = VPPCOM_EEXIST;
2850 goto done;
2851 }
Florin Coras134a9962018-08-28 11:32:04 -07002852 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002853 {
Florin Coras7e12d942018-06-27 14:32:43 -07002854 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002855 next_session = vcl_session_get_w_handle (wrk,
2856 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002857 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002858 {
Florin Coras5e062572019-03-14 19:07:51 -07002859 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002860 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002861 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002862 }
Florin Coras134a9962018-08-28 11:32:04 -07002863 ASSERT (next_session->vep.prev_sh == vep_handle);
2864 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002865 }
Florin Coras17ec5772020-08-12 20:46:29 -07002866 s->vep.next_sh = vep_session->vep.next_sh;
2867 s->vep.prev_sh = vep_handle;
2868 s->vep.vep_sh = vep_handle;
2869 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002870 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002871 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07002872 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Coras6c3b2182020-10-19 18:36:48 -07002873 s->flags &= ~VCL_SESSION_F_IS_VEP;
2874 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002875 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002876
Florin Coras470d72f2023-06-01 21:50:03 -07002877 if ((event->events & EPOLLOUT))
hanlin475c9d72019-12-26 11:44:28 +08002878 {
Florin Coras470d72f2023-06-01 21:50:03 -07002879 int write_ready = vcl_session_write_ready (s);
2880
2881 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2882 if (write_ready > 0)
2883 {
2884 /* Generate EPOLLOUT if tx fifo not full */
2885 vcl_epoll_ctl_add_unhandled_event (
2886 wrk, s, event->events & EPOLLET, SESSION_IO_EVT_TX);
2887 add_evt = 1;
2888 }
2889 else
2890 {
2891 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
2892 }
hanlin475c9d72019-12-26 11:44:28 +08002893 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002894 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002895 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002896 {
Florin Corasb0116a12023-02-07 09:11:47 -08002897 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2898 SESSION_IO_EVT_RX);
Florin Coras87f76002021-06-28 19:13:29 -07002899 add_evt = 1;
2900 }
2901 if (!add_evt && vcl_session_is_closing (s))
2902 {
2903 session_event_t e = { 0 };
2904 if (s->session_state == VCL_STATE_VPP_CLOSING)
2905 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2906 else
2907 e.event_type = SESSION_CTRL_EVT_RESET;
2908 e.session_index = s->session_index;
2909 e.postponed = 1;
2910 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002911 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002912 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2913 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002914 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002915 break;
2916
2917 case EPOLL_CTL_MOD:
2918 if (PREDICT_FALSE (!event))
2919 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002920 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002921 rv = VPPCOM_EINVAL;
2922 goto done;
2923 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002924 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002925 {
Florin Coras5e062572019-03-14 19:07:51 -07002926 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002927 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002928 goto done;
2929 }
Florin Coras17ec5772020-08-12 20:46:29 -07002930 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002931 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002932 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002933 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002934 rv = VPPCOM_EINVAL;
2935 goto done;
2936 }
hanlin475c9d72019-12-26 11:44:28 +08002937
Florin Coras470d72f2023-06-01 21:50:03 -07002938 /* Generate EPOLLOUT if session write ready and event was not on */
2939 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT))
2940 {
2941 /* Fifo size load acq synchronized with update store rel */
2942 int write_ready = vcl_session_write_ready (s);
2943
2944 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2945 if (write_ready > 0)
2946 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2947 SESSION_IO_EVT_TX);
2948 else
2949 /* Request deq ntf in case dequeue happened while updating flag */
2950 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
2951 }
2952 else if (!(event->events & EPOLLOUT))
Florin Coras607eb202023-05-29 16:19:38 -07002953 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasbc4d5b02023-05-17 23:20:56 -07002954
Florin Coras2645f682021-06-04 15:59:41 -07002955 /* Generate EPOLLIN if session read ready and event was not on */
2956 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2957 (vcl_session_read_ready (s) > 0))
2958 {
Florin Corasb0116a12023-02-07 09:11:47 -08002959 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2960 SESSION_IO_EVT_RX);
Florin Coras2645f682021-06-04 15:59:41 -07002961 }
Florin Coras17ec5772020-08-12 20:46:29 -07002962 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2963 s->vep.ev = *event;
Florin Coras30ecfa82023-06-14 11:15:36 -07002964 s->vep.ev.events |= EPOLLHUP | EPOLLERR;
Florin Corasbc4d5b02023-05-17 23:20:56 -07002965
Florin Corasa7a1a222018-12-30 17:11:31 -08002966 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2967 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002968 break;
2969
2970 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002971 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002972 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002973 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002974 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002975 goto done;
2976 }
Florin Coras17ec5772020-08-12 20:46:29 -07002977 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002978 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002979 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002980 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002981 rv = VPPCOM_EINVAL;
2982 goto done;
2983 }
2984
Florin Coras17ec5772020-08-12 20:46:29 -07002985 if (s->vep.prev_sh == vep_handle)
2986 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002987 else
2988 {
Florin Coras7e12d942018-06-27 14:32:43 -07002989 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002990 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002991 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002992 {
Florin Coras5e062572019-03-14 19:07:51 -07002993 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002994 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002995 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002996 }
Florin Coras134a9962018-08-28 11:32:04 -07002997 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002998 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002999 }
Florin Coras17ec5772020-08-12 20:46:29 -07003000 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003001 {
Florin Coras7e12d942018-06-27 14:32:43 -07003002 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07003003 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07003004 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003005 {
Florin Coras5e062572019-03-14 19:07:51 -07003006 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07003007 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003008 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003009 }
Florin Coras134a9962018-08-28 11:32:04 -07003010 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003011 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003012 }
3013
Florin Corasfa3884f2021-06-22 20:04:46 -07003014 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
3015 vcl_epoll_lt_del (wrk, s);
3016
Florin Coras17ec5772020-08-12 20:46:29 -07003017 memset (&s->vep, 0, sizeof (s->vep));
3018 s->vep.next_sh = ~0;
3019 s->vep.prev_sh = ~0;
3020 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003021 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07003022 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08003023
Florin Corasf1ddeeb2021-06-10 08:08:53 -07003024 if (vcl_session_is_open (s))
Florin Coras607eb202023-05-29 16:19:38 -07003025 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08003026
Florin Coras5e062572019-03-14 19:07:51 -07003027 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003028 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003029 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003030 break;
3031
3032 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003033 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003034 rv = VPPCOM_EINVAL;
3035 }
3036
Florin Coras134a9962018-08-28 11:32:04 -07003037 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003038
3039done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003040 return rv;
3041}
3042
Florin Coras30ecfa82023-06-14 11:15:36 -07003043always_inline u8
3044vcl_ep_session_needs_evt (vcl_session_t *s, u32 evt)
3045{
3046 /* No event if not epolled / events reset on hup or level-trigger on */
3047 return ((s->vep.ev.events & evt) &&
3048 s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
3049}
3050
Florin Coras86f04502018-09-12 16:08:01 -07003051static inline void
3052vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3053 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003054{
3055 session_disconnected_msg_t *disconnected_msg;
3056 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003057 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003058 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003059 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003060 u8 add_event = 0;
3061
3062 switch (e->event_type)
3063 {
Florin Coras653e43f2019-03-04 10:56:23 -08003064 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003065 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003066 s = vcl_session_get (wrk, sid);
3067 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003068 break;
Florin Corasb242d312020-10-26 15:35:40 -07003069 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras30ecfa82023-06-14 11:15:36 -07003070 if (!vcl_ep_session_needs_evt (s, EPOLLIN) ||
3071 (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07003072 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003073 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003074 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003075 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003076 session_evt_data = s->vep.ev.data.u64;
3077 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003078 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003079 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003080 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003081 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003082 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003083 break;
Florin Coras48178552023-05-17 22:59:40 -07003084 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ? s->ct_tx_fifo :
3085 s->tx_fifo);
Florin Coras30ecfa82023-06-14 11:15:36 -07003086 if (!vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras86f04502018-09-12 16:08:01 -07003087 break;
Florin Coras30ecfa82023-06-14 11:15:36 -07003088 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003089 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003090 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003091 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003092 break;
Florin Coras86f04502018-09-12 16:08:01 -07003093 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003094 if (!e->postponed)
3095 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3096 else
3097 s = vcl_session_get (wrk, e->session_index);
Florin Coras30ecfa82023-06-14 11:15:36 -07003098 if (!s || !vcl_ep_session_needs_evt (s, EPOLLIN))
Florin Coras3c7d4f92018-12-14 11:28:43 -08003099 break;
Florin Corasb242d312020-10-26 15:35:40 -07003100 sid = s->session_index;
Florin Coras30ecfa82023-06-14 11:15:36 -07003101 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003102 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003103 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003104 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003105 break;
3106 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003107 if (!e->postponed)
3108 {
3109 connected_msg = (session_connected_msg_t *) e->data;
3110 sid = vcl_session_connected_handler (wrk, connected_msg);
3111 }
3112 else
3113 sid = e->session_index;
3114 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003115 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLOUT))
Florin Coras72f77822019-01-22 19:05:52 -08003116 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003117 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07003118 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003119 add_event = 1;
Florin Coras30ecfa82023-06-14 11:15:36 -07003120 session_events = s->vep.ev.events;
3121 /* Generate EPOLLOUT because there's no connected event */
wanghanlin9e42cc22021-06-25 17:40:13 +08003122 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003123 session_evt_data = s->vep.ev.data.u64;
3124 if (s->session_state == VCL_STATE_DETACHED)
Florin Coras30ecfa82023-06-14 11:15:36 -07003125 {
3126 events[*num_ev].events |= EPOLLHUP;
3127 s->vep.ev.events = 0;
3128 }
Florin Coras86f04502018-09-12 16:08:01 -07003129 break;
3130 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003131 if (!e->postponed)
3132 {
3133 disconnected_msg = (session_disconnected_msg_t *) e->data;
3134 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3135 }
3136 else
3137 {
3138 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003139 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003140 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003141 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003142 {
Florin Coras74254b52021-12-08 11:03:08 -08003143 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003144 vcl_session_free (wrk, s);
3145 break;
3146 }
Florin Corasb242d312020-10-26 15:35:40 -07003147 sid = s->session_index;
3148 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003149 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003150 if (EPOLLRDHUP & session_events)
3151 {
3152 /* If app can distinguish between RDHUP and HUP,
3153 * we make finer control */
3154 events[*num_ev].events = EPOLLRDHUP;
3155 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3156 {
3157 events[*num_ev].events |= EPOLLHUP;
3158 }
3159 }
3160 else
3161 {
3162 events[*num_ev].events = EPOLLHUP;
3163 }
Florin Corasb242d312020-10-26 15:35:40 -07003164 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003165 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003166 break;
Florin Coras01ee7a72023-03-01 00:49:25 -08003167 case SESSION_CTRL_EVT_BOUND:
3168 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
3169 break;
Florin Coras86f04502018-09-12 16:08:01 -07003170 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003171 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003172 {
3173 sid =
3174 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3175 s = vcl_session_get (wrk, sid);
3176 }
Florin Coras87f76002021-06-28 19:13:29 -07003177 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003178 {
3179 sid = e->session_index;
3180 s = vcl_session_get (wrk, sid);
3181 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3182 }
Florin Coras30ecfa82023-06-14 11:15:36 -07003183 if (vcl_session_is_closed (s) || !vcl_ep_session_needs_evt (s, EPOLLHUP))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003184 {
Florin Coras74254b52021-12-08 11:03:08 -08003185 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003186 vcl_session_free (wrk, s);
3187 break;
3188 }
Florin Corasb242d312020-10-26 15:35:40 -07003189 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003190 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003191 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3192 if ((EPOLLRDHUP & session_events) &&
3193 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3194 {
Yacan Liuab157702022-09-26 16:41:32 +08003195 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003196 }
3197 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3198 {
3199 events[*num_ev].events |= EPOLLIN;
3200 }
Florin Corasb242d312020-10-26 15:35:40 -07003201 session_evt_data = s->vep.ev.data.u64;
Florin Coras30ecfa82023-06-14 11:15:36 -07003202 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003203 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003204 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3205 vcl_session_unlisten_reply_handler (wrk, e->data);
3206 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003207 case SESSION_CTRL_EVT_MIGRATED:
3208 vcl_session_migrated_handler (wrk, e->data);
3209 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003210 case SESSION_CTRL_EVT_CLEANUP:
3211 vcl_session_cleanup_handler (wrk, e->data);
3212 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003213 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3214 vcl_session_req_worker_update_handler (wrk, e->data);
3215 break;
3216 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3217 vcl_session_worker_update_reply_handler (wrk, e->data);
3218 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003219 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3220 vcl_session_app_add_segment_handler (wrk, e->data);
3221 break;
3222 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3223 vcl_session_app_del_segment_handler (wrk, e->data);
3224 break;
hanlina3a48962020-07-13 11:09:15 +08003225 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003226 vcl_worker_rpc_handler (wrk, e->data);
3227 break;
Florin Coras86f04502018-09-12 16:08:01 -07003228 default:
3229 VDBG (0, "unhandled: %u", e->event_type);
3230 break;
3231 }
3232
3233 if (add_event)
3234 {
Florin Coras30ecfa82023-06-14 11:15:36 -07003235 ASSERT (s->flags & VCL_SESSION_F_IS_VEP_SESSION);
Florin Coras86f04502018-09-12 16:08:01 -07003236 events[*num_ev].data.u64 = session_evt_data;
3237 if (EPOLLONESHOT & session_events)
3238 {
Florin Corasb242d312020-10-26 15:35:40 -07003239 s = vcl_session_get (wrk, sid);
Florin Coras30ecfa82023-06-14 11:15:36 -07003240 if (!(events[*num_ev].events & EPOLLHUP))
3241 s->vep.ev.events = EPOLLHUP | EPOLLERR;
Florin Coras86f04502018-09-12 16:08:01 -07003242 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003243 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003244 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003245 s = vcl_session_get (wrk, sid);
3246 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3247 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003248 }
Florin Coras86f04502018-09-12 16:08:01 -07003249 *num_ev += 1;
3250 }
3251}
3252
3253static int
3254vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3255 struct epoll_event *events, u32 maxevents,
3256 double wait_for_time, u32 * num_ev)
3257{
Florin Coras99368312018-08-02 10:45:44 -07003258 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003259 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003260 int i;
3261
Florin Coras539663c2018-09-28 14:59:37 -07003262 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3263 goto handle_dequeued;
3264
Florin Coras54693d22018-07-17 10:46:29 -07003265 if (svm_msg_q_is_empty (mq))
3266 {
3267 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003268 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003269 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003270 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003271 else
3272 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003273 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003274 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003275 }
3276 }
Florin Corase003a1b2019-06-05 10:47:16 -07003277 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003278 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003279
Florin Coras539663c2018-09-28 14:59:37 -07003280handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003281 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003282 {
Florin Coras134a9962018-08-28 11:32:04 -07003283 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003284 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003285 if (*num_ev < maxevents)
3286 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3287 else
3288 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003289 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003290 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003291 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003292 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003293 return *num_ev;
3294}
3295
Florin Coras99368312018-08-02 10:45:44 -07003296static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003297vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3298 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003299{
Florin Corasccdb8b82021-04-28 13:01:06 -07003300 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003301
3302 if (!n_evts)
3303 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003304 if (timeout_ms > 0)
3305 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003306 }
3307
3308 do
3309 {
3310 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003311 timeout_ms, &n_evts);
3312 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003313 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003314 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003315 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003316
3317 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003318}
3319
3320static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003321vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3322 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003323{
Florin Coras99368312018-08-02 10:45:44 -07003324 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003325 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003326 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003327 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003328 u64 buf;
3329
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003330 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3331 {
3332 vcl_api_retry_attach (wrk);
3333 return n_evts;
3334 }
3335
Florin Coras134a9962018-08-28 11:32:04 -07003336 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003337 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003338 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003339 if (timeout_ms > 0)
3340 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003341 }
3342
Florin Corasb13ba132021-01-27 16:05:24 -08003343 do
3344 {
3345 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003346 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003347 if (n_mq_evts < 0)
3348 {
3349 VDBG (0, "epoll_wait error %u", errno);
3350 return n_evts;
3351 }
3352
3353 for (i = 0; i < n_mq_evts; i++)
3354 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003355 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3356 {
3357 /* api socket was closed */
3358 vcl_api_handle_disconnect (wrk);
3359 continue;
3360 }
3361
Florin Corasb13ba132021-01-27 16:05:24 -08003362 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3363 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3364 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3365 &n_evts);
3366 }
3367
Florin Corasccdb8b82021-04-28 13:01:06 -07003368 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003369 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003370 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003371 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003372
3373 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003374}
3375
Florin Corasfe286f72021-06-04 10:07:55 -07003376static void
Florin Corasfe286f72021-06-04 10:07:55 -07003377vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3378 int maxevents, u32 *n_evts)
3379{
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003380 u32 add_event = 0, evt_flags = 0, next, *to_remove = 0, *si;
Florin Corasfe286f72021-06-04 10:07:55 -07003381 vcl_session_t *s;
3382 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003383 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003384
Florin Corasfa3884f2021-06-22 20:04:46 -07003385 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003386 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003387 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003388
Florin Corasfa3884f2021-06-22 20:04:46 -07003389 next = wrk->ep_lt_current;
3390 do
Florin Corasfe286f72021-06-04 10:07:55 -07003391 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003392 s = vcl_session_get (wrk, next);
3393 next = s->vep.lt_next;
3394
Florin Coras30ecfa82023-06-14 11:15:36 -07003395 if (s->vep.ev.events == 0)
3396 {
3397 vec_add1 (to_remove, s->session_index);
3398 continue;
3399 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003400 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003401 {
3402 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003403 evt_flags |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003404 evt_data = s->vep.ev.data.u64;
3405 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003406 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003407 {
3408 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003409 evt_flags |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
Florin Corasfa3884f2021-06-22 20:04:46 -07003410 evt_data = s->vep.ev.data.u64;
3411 }
3412 if (!add_event && s->session_state > VCL_STATE_READY)
3413 {
3414 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003415 evt_flags |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003416 evt_data = s->vep.ev.data.u64;
3417 }
3418 if (add_event)
3419 {
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003420 events[*n_evts].events = evt_flags;
Florin Corasfe286f72021-06-04 10:07:55 -07003421 events[*n_evts].data.u64 = evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003422 if (EPOLLONESHOT & s->vep.ev.events)
Florin Coras30ecfa82023-06-14 11:15:36 -07003423 s->vep.ev.events = EPOLLHUP | EPOLLERR;
3424 if (evt_flags & EPOLLHUP)
Florin Corasfa3884f2021-06-22 20:04:46 -07003425 s->vep.ev.events = 0;
Florin Coras5cca6692023-06-20 08:47:37 -07003426 *n_evts += 1;
3427 add_event = 0;
3428 evt_flags = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003429 if (*n_evts == maxevents)
3430 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003431 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003432 break;
3433 }
3434 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003435 else
3436 {
Florin Coras7ff72742023-05-16 13:05:28 -07003437 vec_add1 (to_remove, s->session_index);
Florin Corasfa3884f2021-06-22 20:04:46 -07003438 }
Florin Corasfe286f72021-06-04 10:07:55 -07003439 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003440 while (next != wrk->ep_lt_current);
Florin Coras7ff72742023-05-16 13:05:28 -07003441
3442 vec_foreach (si, to_remove)
3443 {
3444 s = vcl_session_get (wrk, *si);
3445 vcl_epoll_lt_del (wrk, s);
3446 }
3447 vec_free (to_remove);
Florin Corasfe286f72021-06-04 10:07:55 -07003448}
3449
Dave Wallacef7f809c2017-10-03 01:48:42 -04003450int
Florin Coras134a9962018-08-28 11:32:04 -07003451vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003452 int maxevents, double wait_for_time)
3453{
Florin Coras134a9962018-08-28 11:32:04 -07003454 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003455 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003456 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003457 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003458
3459 if (PREDICT_FALSE (maxevents <= 0))
3460 {
Florin Coras5e062572019-03-14 19:07:51 -07003461 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003462 return VPPCOM_EINVAL;
3463 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003464
Florin Coras134a9962018-08-28 11:32:04 -07003465 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003466 if (!vep_session)
3467 return VPPCOM_EBADFD;
3468
Florin Coras6c3b2182020-10-19 18:36:48 -07003469 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003470 {
Florin Coras5e062572019-03-14 19:07:51 -07003471 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003472 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003473 }
Florin Coras54693d22018-07-17 10:46:29 -07003474
Florin Coras86f04502018-09-12 16:08:01 -07003475 if (vec_len (wrk->unhandled_evts_vector))
3476 {
3477 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3478 {
3479 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3480 events, &n_evts);
3481 if (n_evts == maxevents)
3482 {
Florin Corase003a1b2019-06-05 10:47:16 -07003483 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3484 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003485 }
3486 }
Florin Corase003a1b2019-06-05 10:47:16 -07003487 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003488 }
liuyacancba87102021-09-10 15:14:05 +08003489
3490 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3491 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3492
wanghanlin8919fec2021-03-18 20:00:41 +08003493 /* Request to only drain unhandled */
3494 if ((int) wait_for_time == -2)
3495 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003496
Florin Coras86f04502018-09-12 16:08:01 -07003497
Florin Corasfe286f72021-06-04 10:07:55 -07003498 if (vcm->cfg.use_mq_eventfd)
3499 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3500 wait_for_time);
3501 else
3502 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3503 wait_for_time);
3504
Florin Corasfe286f72021-06-04 10:07:55 -07003505 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003506}
3507
Dave Wallace35830af2017-10-09 01:43:42 -04003508int
Florin Coras134a9962018-08-28 11:32:04 -07003509vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003510 void *buffer, uint32_t * buflen)
3511{
Florin Coras134a9962018-08-28 11:32:04 -07003512 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003513 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003514 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003515 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003516 vcl_session_t *session;
3517 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003518
Florin Coras134a9962018-08-28 11:32:04 -07003519 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003520 if (!session)
3521 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003522
Dave Wallace35830af2017-10-09 01:43:42 -04003523 switch (op)
3524 {
3525 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003526 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003527 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3528 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003529 break;
3530
Dave Wallace227867f2017-11-13 21:21:53 -05003531 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003532 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003533 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3534 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003535 break;
3536
3537 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003538 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003539 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003540 *flags =
3541 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003542 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003543 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003544 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003545 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3546 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003547 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003548 }
3549 else
3550 rv = VPPCOM_EINVAL;
3551 break;
3552
3553 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003554 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003555 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003556 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003557 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003558 else
Florin Corasac422d62020-10-19 20:51:36 -07003559 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003560
Florin Coras5e062572019-03-14 19:07:51 -07003561 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3562 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003563 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003564 }
3565 else
3566 rv = VPPCOM_EINVAL;
3567 break;
3568
3569 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003570 if (PREDICT_TRUE (buffer && buflen &&
3571 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003572 {
Florin Coras7e12d942018-06-27 14:32:43 -07003573 ep->is_ip4 = session->transport.is_ip4;
3574 ep->port = session->transport.rmt_port;
3575 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003576 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3577 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003578 else
Dave Barach178cf492018-11-13 16:34:13 -05003579 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3580 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003581 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003582 VDBG (1,
3583 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3584 "addr = %U, port %u",
3585 session_handle, ep->is_ip4, vcl_format_ip46_address,
3586 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003587 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3588 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003589 }
3590 else
3591 rv = VPPCOM_EINVAL;
3592 break;
3593
3594 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003595 if (PREDICT_TRUE (buffer && buflen &&
3596 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003597 {
Florin Coras7e12d942018-06-27 14:32:43 -07003598 ep->is_ip4 = session->transport.is_ip4;
3599 ep->port = session->transport.lcl_port;
3600 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003601 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3602 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003603 else
Dave Barach178cf492018-11-13 16:34:13 -05003604 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3605 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003606 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003607 VDBG (1,
3608 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3609 " port %d",
3610 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003611 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003612 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3613 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003614 }
3615 else
3616 rv = VPPCOM_EINVAL;
3617 break;
Stevenb5a11602017-10-11 09:59:30 -07003618
qinyangaf9b7152023-06-27 01:11:53 -07003619 case VPPCOM_ATTR_GET_ORIGINAL_DST:
3620 if (!session->transport.is_ip4)
3621 {
3622 /* now original dst only support ipv4*/
3623 rv = VPPCOM_EAFNOSUPPORT;
3624 break;
3625 }
3626 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*ep)) &&
3627 ep->ip))
3628 {
3629 ep->is_ip4 = session->transport.is_ip4;
3630 ep->port = session->original_dst_port;
3631 clib_memcpy_fast (ep->ip, &session->original_dst_ip4,
3632 sizeof (ip4_address_t));
3633 *buflen = sizeof (*ep);
3634 VDBG (1,
3635 "VPPCOM_ATTR_GET_ORIGINAL_DST: sh %u, is_ip4 = %u, addr = %U"
3636 " port %d",
3637 session_handle, ep->is_ip4, vcl_format_ip4_address,
3638 (ip4_address_t *) (&session->original_dst_ip4),
3639 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3640 clib_net_to_host_u16 (ep->port));
3641 }
3642 else
3643 rv = VPPCOM_EINVAL;
3644 break;
3645
Florin Corasef7cbf62019-10-17 09:56:27 -07003646 case VPPCOM_ATTR_SET_LCL_ADDR:
3647 if (PREDICT_TRUE (buffer && buflen &&
3648 (*buflen >= sizeof (*ep)) && ep->ip))
3649 {
3650 session->transport.is_ip4 = ep->is_ip4;
3651 session->transport.lcl_port = ep->port;
3652 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3653 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003654 VDBG (1,
3655 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3656 " port %d",
3657 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003658 &session->transport.lcl_ip,
3659 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3660 clib_net_to_host_u16 (ep->port));
3661 }
3662 else
3663 rv = VPPCOM_EINVAL;
3664 break;
3665
Dave Wallace048b1d62018-01-03 22:24:41 -05003666 case VPPCOM_ATTR_GET_LIBC_EPFD:
3667 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003668 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003669 break;
3670
3671 case VPPCOM_ATTR_SET_LIBC_EPFD:
3672 if (PREDICT_TRUE (buffer && buflen &&
3673 (*buflen == sizeof (session->libc_epfd))))
3674 {
3675 session->libc_epfd = *(int *) buffer;
3676 *buflen = sizeof (session->libc_epfd);
3677
Florin Coras5e062572019-03-14 19:07:51 -07003678 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3679 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003680 }
3681 else
3682 rv = VPPCOM_EINVAL;
3683 break;
3684
3685 case VPPCOM_ATTR_GET_PROTOCOL:
3686 if (buffer && buflen && (*buflen >= sizeof (int)))
3687 {
Florin Coras7e12d942018-06-27 14:32:43 -07003688 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003689 *buflen = sizeof (int);
3690
Florin Coras5e062572019-03-14 19:07:51 -07003691 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3692 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003693 }
3694 else
3695 rv = VPPCOM_EINVAL;
3696 break;
3697
3698 case VPPCOM_ATTR_GET_LISTEN:
3699 if (buffer && buflen && (*buflen >= sizeof (int)))
3700 {
Florin Corasac422d62020-10-19 20:51:36 -07003701 *(int *) buffer = vcl_session_has_attr (session,
3702 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003703 *buflen = sizeof (int);
3704
Florin Coras5e062572019-03-14 19:07:51 -07003705 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3706 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003707 }
3708 else
3709 rv = VPPCOM_EINVAL;
3710 break;
3711
3712 case VPPCOM_ATTR_GET_ERROR:
3713 if (buffer && buflen && (*buflen >= sizeof (int)))
3714 {
3715 *(int *) buffer = 0;
3716 *buflen = sizeof (int);
3717
Florin Coras5e062572019-03-14 19:07:51 -07003718 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3719 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003720 }
3721 else
3722 rv = VPPCOM_EINVAL;
3723 break;
3724
3725 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3726 if (buffer && buflen && (*buflen >= sizeof (u32)))
3727 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003728
3729 /* VPP-TBD */
3730 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003731 session->tx_fifo ?
3732 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003733 vcm->cfg.tx_fifo_size);
3734 *buflen = sizeof (u32);
3735
Florin Coras5e062572019-03-14 19:07:51 -07003736 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3737 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3738 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003739 }
3740 else
3741 rv = VPPCOM_EINVAL;
3742 break;
3743
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003744 case VPPCOM_ATTR_SET_DSCP:
3745 if (buffer && buflen && (*buflen >= sizeof (u8)))
3746 {
3747 session->dscp = *(u8 *) buffer;
3748
3749 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3750 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3751 }
3752 else
3753 rv = VPPCOM_EINVAL;
3754 break;
3755
Dave Wallace048b1d62018-01-03 22:24:41 -05003756 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3757 if (buffer && buflen && (*buflen == sizeof (u32)))
3758 {
3759 /* VPP-TBD */
3760 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003761 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3762 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3763 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003764 }
3765 else
3766 rv = VPPCOM_EINVAL;
3767 break;
3768
3769 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3770 if (buffer && buflen && (*buflen >= sizeof (u32)))
3771 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003772
3773 /* VPP-TBD */
3774 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003775 session->rx_fifo ?
3776 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003777 vcm->cfg.rx_fifo_size);
3778 *buflen = sizeof (u32);
3779
Florin Coras5e062572019-03-14 19:07:51 -07003780 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3781 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003782 }
3783 else
3784 rv = VPPCOM_EINVAL;
3785 break;
3786
3787 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3788 if (buffer && buflen && (*buflen == sizeof (u32)))
3789 {
3790 /* VPP-TBD */
3791 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003792 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3793 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3794 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003795 }
3796 else
3797 rv = VPPCOM_EINVAL;
3798 break;
3799
3800 case VPPCOM_ATTR_GET_REUSEADDR:
3801 if (buffer && buflen && (*buflen >= sizeof (int)))
3802 {
3803 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003804 *(int *) buffer = vcl_session_has_attr (session,
3805 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003806 *buflen = sizeof (int);
3807
Florin Coras5e062572019-03-14 19:07:51 -07003808 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3809 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003810 }
3811 else
3812 rv = VPPCOM_EINVAL;
3813 break;
3814
Stevenb5a11602017-10-11 09:59:30 -07003815 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003816 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003817 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003818 {
3819 /* VPP-TBD */
3820 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003821 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003822 else
Florin Corasac422d62020-10-19 20:51:36 -07003823 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003824
Florin Coras5e062572019-03-14 19:07:51 -07003825 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003826 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003827 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 }
3829 else
3830 rv = VPPCOM_EINVAL;
3831 break;
3832
3833 case VPPCOM_ATTR_GET_REUSEPORT:
3834 if (buffer && buflen && (*buflen >= sizeof (int)))
3835 {
3836 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003837 *(int *) buffer = vcl_session_has_attr (session,
3838 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003839 *buflen = sizeof (int);
3840
Florin Coras5e062572019-03-14 19:07:51 -07003841 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3842 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 }
3844 else
3845 rv = VPPCOM_EINVAL;
3846 break;
3847
3848 case VPPCOM_ATTR_SET_REUSEPORT:
3849 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003850 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003851 {
3852 /* VPP-TBD */
3853 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003854 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003855 else
Florin Corasac422d62020-10-19 20:51:36 -07003856 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003857
Florin Coras5e062572019-03-14 19:07:51 -07003858 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003859 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003860 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003861 }
3862 else
3863 rv = VPPCOM_EINVAL;
3864 break;
3865
3866 case VPPCOM_ATTR_GET_BROADCAST:
3867 if (buffer && buflen && (*buflen >= sizeof (int)))
3868 {
3869 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003870 *(int *) buffer = vcl_session_has_attr (session,
3871 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003872 *buflen = sizeof (int);
3873
Florin Coras5e062572019-03-14 19:07:51 -07003874 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3875 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003876 }
3877 else
3878 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003879 break;
3880
3881 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003882 if (buffer && buflen && (*buflen == sizeof (int)))
3883 {
3884 /* VPP-TBD */
3885 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003886 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003887 else
Florin Corasac422d62020-10-19 20:51:36 -07003888 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003889
Florin Coras5e062572019-03-14 19:07:51 -07003890 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003891 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003892 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003893 }
3894 else
3895 rv = VPPCOM_EINVAL;
3896 break;
3897
3898 case VPPCOM_ATTR_GET_V6ONLY:
3899 if (buffer && buflen && (*buflen >= sizeof (int)))
3900 {
3901 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003902 *(int *) buffer = vcl_session_has_attr (session,
3903 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003904 *buflen = sizeof (int);
3905
Florin Coras5e062572019-03-14 19:07:51 -07003906 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3907 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003908 }
3909 else
3910 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003911 break;
3912
3913 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003914 if (buffer && buflen && (*buflen == sizeof (int)))
3915 {
3916 /* VPP-TBD */
3917 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003918 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003919 else
Florin Corasac422d62020-10-19 20:51:36 -07003920 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003921
Florin Coras5e062572019-03-14 19:07:51 -07003922 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003923 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003924 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003925 }
3926 else
3927 rv = VPPCOM_EINVAL;
3928 break;
3929
3930 case VPPCOM_ATTR_GET_KEEPALIVE:
3931 if (buffer && buflen && (*buflen >= sizeof (int)))
3932 {
3933 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003934 *(int *) buffer = vcl_session_has_attr (session,
3935 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003936 *buflen = sizeof (int);
3937
Florin Coras5e062572019-03-14 19:07:51 -07003938 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3939 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003940 }
3941 else
3942 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003943 break;
Stevenbccd3392017-10-12 20:42:21 -07003944
3945 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003946 if (buffer && buflen && (*buflen == sizeof (int)))
3947 {
3948 /* VPP-TBD */
3949 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003950 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003951 else
Florin Corasac422d62020-10-19 20:51:36 -07003952 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003953
Florin Coras5e062572019-03-14 19:07:51 -07003954 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003955 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003956 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003957 }
3958 else
3959 rv = VPPCOM_EINVAL;
3960 break;
3961
3962 case VPPCOM_ATTR_GET_TCP_NODELAY:
3963 if (buffer && buflen && (*buflen >= sizeof (int)))
3964 {
3965 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003966 *(int *) buffer = vcl_session_has_attr (session,
3967 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003968 *buflen = sizeof (int);
3969
Florin Coras5e062572019-03-14 19:07:51 -07003970 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3971 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003972 }
3973 else
3974 rv = VPPCOM_EINVAL;
3975 break;
3976
3977 case VPPCOM_ATTR_SET_TCP_NODELAY:
3978 if (buffer && buflen && (*buflen == sizeof (int)))
3979 {
3980 /* VPP-TBD */
3981 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003982 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003983 else
Florin Corasac422d62020-10-19 20:51:36 -07003984 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003985
Florin Coras5e062572019-03-14 19:07:51 -07003986 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003987 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003988 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003989 }
3990 else
3991 rv = VPPCOM_EINVAL;
3992 break;
3993
3994 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3995 if (buffer && buflen && (*buflen >= sizeof (int)))
3996 {
3997 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003998 *(int *) buffer = vcl_session_has_attr (session,
3999 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004000 *buflen = sizeof (int);
4001
Florin Coras5e062572019-03-14 19:07:51 -07004002 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
4003 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004004 }
4005 else
4006 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004007 break;
4008
4009 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05004010 if (buffer && buflen && (*buflen == sizeof (int)))
4011 {
4012 /* VPP-TBD */
4013 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004014 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004015 else
Florin Corasac422d62020-10-19 20:51:36 -07004016 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05004017
Florin Coras5e062572019-03-14 19:07:51 -07004018 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004019 vcl_session_has_attr (session,
4020 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004021 }
4022 else
4023 rv = VPPCOM_EINVAL;
4024 break;
4025
4026 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
4027 if (buffer && buflen && (*buflen >= sizeof (int)))
4028 {
4029 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07004030 *(int *) buffer = vcl_session_has_attr (session,
4031 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004032 *buflen = sizeof (int);
4033
Florin Coras5e062572019-03-14 19:07:51 -07004034 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
4035 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004036 }
4037 else
4038 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004039 break;
4040
4041 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05004042 if (buffer && buflen && (*buflen == sizeof (int)))
4043 {
4044 /* VPP-TBD */
4045 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07004046 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004047 else
Florin Corasac422d62020-10-19 20:51:36 -07004048 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05004049
Florin Coras5e062572019-03-14 19:07:51 -07004050 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07004051 vcl_session_has_attr (session,
4052 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004053 }
4054 else
4055 rv = VPPCOM_EINVAL;
4056 break;
4057
4058 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004059 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004060 {
Florin Coras04ae8272021-04-12 19:55:37 -07004061 rv = VPPCOM_EINVAL;
4062 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004063 }
Florin Coras04ae8272021-04-12 19:55:37 -07004064
4065 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4066 tea.mss = *(u32 *) buffer;
4067 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
4068 rv = VPPCOM_ENOPROTOOPT;
4069
4070 if (!rv)
4071 {
4072 *(u32 *) buffer = tea.mss;
4073 *buflen = sizeof (int);
4074 }
4075
4076 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4077 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004078 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004079 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004080 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004081 {
Florin Coras04ae8272021-04-12 19:55:37 -07004082 rv = VPPCOM_EINVAL;
4083 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004084 }
Florin Coras04ae8272021-04-12 19:55:37 -07004085
4086 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4087 tea.mss = *(u32 *) buffer;
4088 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4089 rv = VPPCOM_ENOPROTOOPT;
4090
4091 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4092 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004093 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004094
Florin Coras1e966172020-05-16 18:18:14 +00004095 case VPPCOM_ATTR_SET_CONNECTED:
4096 session->flags |= VCL_SESSION_F_CONNECTED;
4097 break;
4098
Florin Corasa5a9efd2021-01-05 17:03:29 -08004099 case VPPCOM_ATTR_SET_CKPAIR:
4100 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4101 !vcl_session_has_crypto (session))
4102 {
4103 rv = VPPCOM_EINVAL;
4104 break;
4105 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004106 if (!session->ext_config)
4107 {
Florin Coras87f63892021-05-01 16:01:40 -07004108 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4109 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004110 }
4111 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4112 {
4113 rv = VPPCOM_EINVAL;
4114 break;
4115 }
4116
4117 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004118 break;
4119
Florin Coras6a6555a2021-01-28 11:39:27 -08004120 case VPPCOM_ATTR_SET_VRF:
4121 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4122 {
4123 rv = VPPCOM_EINVAL;
4124 break;
4125 }
4126 session->vrf = *(u32 *) buffer;
4127 break;
4128
4129 case VPPCOM_ATTR_GET_VRF:
4130 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4131 {
4132 rv = VPPCOM_EINVAL;
4133 break;
4134 }
4135 *(u32 *) buffer = session->vrf;
4136 *buflen = sizeof (u32);
4137 break;
4138
wanghanlin0674f852021-02-22 10:38:36 +08004139 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004140 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004141 {
Florin Corasd77325c2021-02-23 12:03:03 -08004142 rv = VPPCOM_EINVAL;
4143 break;
wanghanlin0674f852021-02-22 10:38:36 +08004144 }
Florin Corasd77325c2021-02-23 12:03:03 -08004145
4146 if (session->transport.is_ip4)
4147 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004148 else
Florin Corasd77325c2021-02-23 12:03:03 -08004149 *(int *) buffer = AF_INET6;
4150 *buflen = sizeof (int);
4151
wanghanlin0674f852021-02-22 10:38:36 +08004152 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4153 *buflen);
4154 break;
4155
Florin Coras87f63892021-05-01 16:01:40 -07004156 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4157 if (!(buffer && buflen && (*buflen > 0)))
4158 {
4159 rv = VPPCOM_EINVAL;
4160 break;
4161 }
4162 if (session->ext_config)
4163 {
4164 rv = VPPCOM_EINVAL;
4165 break;
4166 }
4167 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4168 *buflen + sizeof (u32));
4169 clib_memcpy (session->ext_config->data, buffer, *buflen);
4170 session->ext_config->len = *buflen;
4171 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004172 case VPPCOM_ATTR_SET_IP_PKTINFO:
4173 if (buffer && buflen && (*buflen == sizeof (int)) &&
4174 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4175 {
4176 if (*(int *) buffer)
4177 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4178 else
4179 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4180
4181 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4182 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4183 *buflen);
4184 }
4185 else
4186 rv = VPPCOM_EINVAL;
4187 break;
4188
4189 case VPPCOM_ATTR_GET_IP_PKTINFO:
4190 if (buffer && buflen && (*buflen >= sizeof (int)))
4191 {
4192 *(int *) buffer =
4193 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4194 *buflen = sizeof (int);
4195
4196 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4197 *buflen);
4198 }
4199 else
4200 rv = VPPCOM_EINVAL;
4201 break;
Florin Coras87f63892021-05-01 16:01:40 -07004202
Dave Wallacee22aa742017-10-20 12:30:38 -04004203 default:
4204 rv = VPPCOM_EINVAL;
4205 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004206 }
4207
Dave Wallace35830af2017-10-09 01:43:42 -04004208 return rv;
4209}
4210
Stevenac1f96d2017-10-24 16:03:58 -07004211int
Florin Coras134a9962018-08-28 11:32:04 -07004212vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004213 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4214{
Florin Coras134a9962018-08-28 11:32:04 -07004215 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004216 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004217 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004218
4219 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004220 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004221 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004222 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004223 else
4224 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004225 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004226 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004227 }
4228
Florin Coras0a1e1832020-03-29 18:54:04 +00004229 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004230 {
Florin Coras5da10c42020-04-01 04:31:21 +00004231 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004232 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004233 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4234 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004235 else
Dave Barach178cf492018-11-13 16:34:13 -05004236 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4237 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004238 ep->is_ip4 = session->transport.is_ip4;
4239 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004240 }
Florin Coras460dce62018-07-27 05:45:06 -07004241
Stevenac1f96d2017-10-24 16:03:58 -07004242 return rv;
4243}
4244
Florin Coraseff5f7a2023-02-07 17:36:17 -08004245static void
4246vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4247{
4248 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4249
4250 do
4251 {
4252 switch (tlv->data_type)
4253 {
4254 case VCL_UDP_SEGMENT:
4255 s->gso_size = *(u16 *) tlv->data;
4256 break;
4257 case VCL_IP_PKTINFO:
4258 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4259 sizeof (ip4_address_t));
4260 break;
4261 default:
4262 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4263 break;
4264 }
4265 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4266 }
4267 while (tlv);
4268}
4269
Stevenac1f96d2017-10-24 16:03:58 -07004270int
Florin Coras134a9962018-08-28 11:32:04 -07004271vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004272 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4273{
Florin Coras7a2abce2020-04-05 19:25:44 +00004274 vcl_worker_t *wrk = vcl_worker_get_current ();
4275 vcl_session_t *s;
4276
4277 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004278 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004279 return VPPCOM_EBADFD;
4280
Dave Wallace617dffa2017-10-26 14:47:06 -04004281 if (ep)
4282 {
Florin Coras5824cc52020-10-20 18:44:41 -07004283 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004284 return VPPCOM_EINVAL;
4285
liuyacanbc0c7542021-08-02 20:15:05 +08004286 s->transport.is_ip4 = ep->is_ip4;
4287 s->transport.rmt_port = ep->port;
4288 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4289
Florin Coraseff5f7a2023-02-07 17:36:17 -08004290 if (ep->app_tlvs)
4291 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004292
Florin Coras5da10c42020-04-01 04:31:21 +00004293 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004294 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004295 {
Florin Coras5824cc52020-10-20 18:44:41 -07004296 u32 session_index = s->session_index;
4297 f64 timeout = vcm->cfg.session_timeout;
4298 int rv;
4299
Florin Coras5da10c42020-04-01 04:31:21 +00004300 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004301 rv = vppcom_wait_for_session_state_change (session_index,
4302 VCL_STATE_READY,
4303 timeout);
4304 if (rv < 0)
4305 return rv;
4306 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004307 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004308 }
4309
4310 if (flags)
4311 {
4312 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004313 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004314 }
4315
Florin Coraseff5f7a2023-02-07 17:36:17 -08004316 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004317 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004318}
4319
Dave Wallace048b1d62018-01-03 22:24:41 -05004320int
4321vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4322{
Florin Coras134a9962018-08-28 11:32:04 -07004323 vcl_worker_t *wrk = vcl_worker_get_current ();
4324 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004325 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004326 svm_msg_q_msg_t msg;
4327 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004328 int rv, num_ev = 0;
4329
Florin Coras5e062572019-03-14 19:07:51 -07004330 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004331
4332 if (!vp)
4333 return VPPCOM_EFAULT;
4334
4335 do
4336 {
Florin Coras7e12d942018-06-27 14:32:43 -07004337 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004338
Florin Coras6917b942018-11-13 22:44:54 -08004339 /* Dequeue all events and drop all unhandled io events */
4340 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4341 {
4342 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4343 vcl_handle_mq_event (wrk, e);
4344 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4345 }
4346 vec_reset_length (wrk->unhandled_evts_vector);
4347
Dave Wallace048b1d62018-01-03 22:24:41 -05004348 for (i = 0; i < n_sids; i++)
4349 {
Florin Coras7baeb712019-01-04 17:05:43 -08004350 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004351 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004352 {
4353 vp[i].revents = POLLHUP;
4354 num_ev++;
4355 continue;
4356 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004357
Florin Coras6917b942018-11-13 22:44:54 -08004358 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004359
4360 if (POLLIN & vp[i].events)
4361 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004362 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004363 if (rv > 0)
4364 {
Florin Coras6917b942018-11-13 22:44:54 -08004365 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004366 num_ev++;
4367 }
4368 else if (rv < 0)
4369 {
4370 switch (rv)
4371 {
4372 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004373 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004374 break;
4375
4376 default:
Florin Coras6917b942018-11-13 22:44:54 -08004377 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004378 break;
4379 }
4380 num_ev++;
4381 }
4382 }
4383
4384 if (POLLOUT & vp[i].events)
4385 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004386 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004387 if (rv > 0)
4388 {
Florin Coras6917b942018-11-13 22:44:54 -08004389 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004390 num_ev++;
4391 }
4392 else if (rv < 0)
4393 {
4394 switch (rv)
4395 {
4396 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004397 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004398 break;
4399
4400 default:
Florin Coras6917b942018-11-13 22:44:54 -08004401 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004402 break;
4403 }
4404 num_ev++;
4405 }
4406 }
4407
Dave Wallace7e607a72018-06-18 18:41:32 -04004408 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004409 {
Florin Coras6917b942018-11-13 22:44:54 -08004410 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004411 num_ev++;
4412 }
4413 }
4414 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004415 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004416 }
4417 while ((num_ev == 0) && keep_trying);
4418
Dave Wallace048b1d62018-01-03 22:24:41 -05004419 return num_ev;
4420}
4421
Florin Coras99368312018-08-02 10:45:44 -07004422int
4423vppcom_mq_epoll_fd (void)
4424{
Florin Coras134a9962018-08-28 11:32:04 -07004425 vcl_worker_t *wrk = vcl_worker_get_current ();
4426 return wrk->mqs_epfd;
4427}
4428
4429int
Florin Coras30e79c22019-01-02 19:31:22 -08004430vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004431{
4432 return session_handle & 0xFFFFFF;
4433}
4434
4435int
Florin Coras30e79c22019-01-02 19:31:22 -08004436vppcom_session_worker (vcl_session_handle_t session_handle)
4437{
4438 return session_handle >> 24;
4439}
4440
4441int
Florin Coras134a9962018-08-28 11:32:04 -07004442vppcom_worker_register (void)
4443{
Florin Coras47c40e22018-11-26 17:01:36 -08004444 if (!vcl_worker_alloc_and_init ())
4445 return VPPCOM_EEXIST;
4446
Florin Coras47c40e22018-11-26 17:01:36 -08004447 if (vcl_worker_register_with_vpp ())
4448 return VPPCOM_EEXIST;
4449
4450 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004451}
4452
Florin Coras369db832019-07-08 12:34:45 -07004453void
4454vppcom_worker_unregister (void)
4455{
4456 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4457 vcl_set_worker_index (~0);
4458}
4459
Pivo6017ff02020-05-04 17:57:33 +02004460void
4461vppcom_worker_index_set (int index)
4462{
4463 vcl_set_worker_index (index);
4464}
4465
Florin Corasdfe4cf42018-11-28 22:13:45 -08004466int
4467vppcom_worker_index (void)
4468{
4469 return vcl_get_worker_index ();
4470}
4471
Florin Corase0982e52019-01-25 13:19:56 -08004472int
4473vppcom_worker_mqs_epfd (void)
4474{
4475 vcl_worker_t *wrk = vcl_worker_get_current ();
4476 if (!vcm->cfg.use_mq_eventfd)
4477 return -1;
4478 return wrk->mqs_epfd;
4479}
4480
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004481int
4482vppcom_session_is_connectable_listener (uint32_t session_handle)
4483{
4484 vcl_session_t *session;
4485 vcl_worker_t *wrk = vcl_worker_get_current ();
4486 session = vcl_session_get_w_handle (wrk, session_handle);
4487 if (!session)
4488 return VPPCOM_EBADFD;
4489 return vcl_session_is_connectable_listener (wrk, session);
4490}
4491
4492int
4493vppcom_session_listener (uint32_t session_handle)
4494{
4495 vcl_worker_t *wrk = vcl_worker_get_current ();
4496 vcl_session_t *listen_session, *session;
4497 session = vcl_session_get_w_handle (wrk, session_handle);
4498 if (!session)
4499 return VPPCOM_EBADFD;
4500 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4501 return VPPCOM_EBADFD;
4502 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4503 if (!listen_session)
4504 return VPPCOM_EBADFD;
4505 return vcl_session_handle (listen_session);
4506}
4507
4508int
4509vppcom_session_n_accepted (uint32_t session_handle)
4510{
4511 vcl_worker_t *wrk = vcl_worker_get_current ();
4512 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4513 if (!session)
4514 return VPPCOM_EBADFD;
4515 return session->n_accepted_sessions;
4516}
4517
Florin Coras66ec4672020-06-15 07:59:40 -07004518const char *
4519vppcom_proto_str (vppcom_proto_t proto)
4520{
4521 char const *proto_str;
4522
4523 switch (proto)
4524 {
4525 case VPPCOM_PROTO_TCP:
4526 proto_str = "TCP";
4527 break;
4528 case VPPCOM_PROTO_UDP:
4529 proto_str = "UDP";
4530 break;
4531 case VPPCOM_PROTO_TLS:
4532 proto_str = "TLS";
4533 break;
4534 case VPPCOM_PROTO_QUIC:
4535 proto_str = "QUIC";
4536 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004537 case VPPCOM_PROTO_DTLS:
4538 proto_str = "DTLS";
4539 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004540 case VPPCOM_PROTO_SRTP:
4541 proto_str = "SRTP";
4542 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004543 default:
4544 proto_str = "UNKNOWN";
4545 break;
4546 }
4547 return proto_str;
4548}
4549
4550const char *
4551vppcom_retval_str (int retval)
4552{
4553 char const *st;
4554
4555 switch (retval)
4556 {
4557 case VPPCOM_OK:
4558 st = "VPPCOM_OK";
4559 break;
4560
4561 case VPPCOM_EAGAIN:
4562 st = "VPPCOM_EAGAIN";
4563 break;
4564
4565 case VPPCOM_EFAULT:
4566 st = "VPPCOM_EFAULT";
4567 break;
4568
4569 case VPPCOM_ENOMEM:
4570 st = "VPPCOM_ENOMEM";
4571 break;
4572
4573 case VPPCOM_EINVAL:
4574 st = "VPPCOM_EINVAL";
4575 break;
4576
4577 case VPPCOM_EBADFD:
4578 st = "VPPCOM_EBADFD";
4579 break;
4580
4581 case VPPCOM_EAFNOSUPPORT:
4582 st = "VPPCOM_EAFNOSUPPORT";
4583 break;
4584
4585 case VPPCOM_ECONNABORTED:
4586 st = "VPPCOM_ECONNABORTED";
4587 break;
4588
4589 case VPPCOM_ECONNRESET:
4590 st = "VPPCOM_ECONNRESET";
4591 break;
4592
4593 case VPPCOM_ENOTCONN:
4594 st = "VPPCOM_ENOTCONN";
4595 break;
4596
4597 case VPPCOM_ECONNREFUSED:
4598 st = "VPPCOM_ECONNREFUSED";
4599 break;
4600
4601 case VPPCOM_ETIMEDOUT:
4602 st = "VPPCOM_ETIMEDOUT";
4603 break;
4604
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304605 case VPPCOM_EADDRINUSE:
4606 st = "VPPCOM_EADDRINUSE";
4607 break;
4608
Florin Coras66ec4672020-06-15 07:59:40 -07004609 default:
4610 st = "UNKNOWN_STATE";
4611 break;
4612 }
4613
4614 return st;
4615}
4616
Florin Corasa5a9efd2021-01-05 17:03:29 -08004617int
4618vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4619{
4620 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004621 return vcl_sapi_add_cert_key_pair (ckpair);
4622 else
4623 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004624}
4625
4626int
4627vppcom_del_cert_key_pair (uint32_t ckpair_index)
4628{
4629 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004630 return vcl_sapi_del_cert_key_pair (ckpair_index);
4631 else
4632 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004633}
4634
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304635int
4636vppcom_session_get_error (uint32_t session_handle)
4637{
4638 vcl_worker_t *wrk = vcl_worker_get_current ();
4639 vcl_session_t *session = 0;
4640
4641 session = vcl_session_get_w_handle (wrk, session_handle);
4642 if (!session)
4643 return VPPCOM_EBADFD;
4644
4645 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4646 {
4647 VWRN ("epoll session %u! will not have connect", session->session_index);
4648 return VPPCOM_EBADFD;
4649 }
4650
4651 if (session->vpp_error == SESSION_E_PORTINUSE)
4652 return VPPCOM_EADDRINUSE;
4653 else if (session->vpp_error == SESSION_E_REFUSED)
4654 return VPPCOM_ECONNREFUSED;
4655 else if (session->vpp_error != SESSION_E_NONE)
4656 return VPPCOM_EFAULT;
4657 else
4658 return VPPCOM_OK;
4659}
4660
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004661int
4662vppcom_worker_is_detached (void)
4663{
4664 vcl_worker_t *wrk = vcl_worker_get_current ();
4665
4666 if (!vcm->cfg.use_mq_eventfd)
4667 return VPPCOM_ENOTSUP;
4668
4669 return wrk->api_client_handle == ~0;
4670}
4671
Dave Wallacee22aa742017-10-20 12:30:38 -04004672/*
4673 * fd.io coding-style-patch-verification: ON
4674 *
4675 * Local Variables:
4676 * eval: (c-set-style "gnu")
4677 * End:
4678 */