blob: 3538a36f5088037a0f12ab3f9cae001ee7c97696 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
Florin Coras5398dfb2021-01-25 20:31:27 -080028 u32 n_msgs = 0, sz, len;
Florin Coras30e79c22019-01-02 19:31:22 -080029
Florin Coras5398dfb2021-01-25 20:31:27 -080030 while ((sz = svm_msg_q_size (mq)))
Florin Coras30e79c22019-01-02 19:31:22 -080031 {
Florin Coras5398dfb2021-01-25 20:31:27 -080032 len = vec_len (wrk->mq_msg_vector);
33 vec_validate (wrk->mq_msg_vector, len + sz - 1);
34 svm_msg_q_sub_raw_batch (mq, wrk->mq_msg_vector + len, sz);
35 n_msgs += sz;
Florin Coras30e79c22019-01-02 19:31:22 -080036 }
37 return n_msgs;
38}
39
Dave Wallace543852a2017-08-03 02:11:34 -040040
Florin Coras697faea2018-06-27 17:10:49 -070041
Florin Coras458089b2019-08-21 16:20:44 -070042static void
Florin Coras4ac25842021-04-19 17:34:54 -070043vcl_msg_add_ext_config (vcl_session_t *s, uword *offset)
44{
45 svm_fifo_chunk_t *c;
46
47 c = vcl_segment_alloc_chunk (vcl_vpp_worker_segment_handle (0),
48 0 /* one slice only */, s->ext_config->len,
49 offset);
50 if (c)
51 clib_memcpy_fast (c->data, s->ext_config, s->ext_config->len);
52}
53
54static void
Florin Coras458089b2019-08-21 16:20:44 -070055vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
56{
57 app_session_evt_t _app_evt, *app_evt = &_app_evt;
58 session_listen_msg_t *mp;
59 svm_msg_q_t *mq;
60
61 mq = vcl_worker_ctrl_mq (wrk);
62 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
63 mp = (session_listen_msg_t *) app_evt->evt->data;
64 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070065 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070066 mp->context = s->session_index;
67 mp->wrk_index = wrk->vpp_wrk_index;
68 mp->is_ip4 = s->transport.is_ip4;
69 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
70 mp->port = s->transport.lcl_port;
71 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -080072 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +000073 if (s->flags & VCL_SESSION_F_CONNECTED)
74 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -070075 if (s->ext_config)
76 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -070077 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -070078 if (s->ext_config)
79 {
80 clib_mem_free (s->ext_config);
81 s->ext_config = 0;
82 }
Florin Coras458089b2019-08-21 16:20:44 -070083}
84
85static void
86vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
87{
88 app_session_evt_t _app_evt, *app_evt = &_app_evt;
89 session_connect_msg_t *mp;
90 svm_msg_q_t *mq;
91
92 mq = vcl_worker_ctrl_mq (wrk);
93 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
94 mp = (session_connect_msg_t *) app_evt->evt->data;
95 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070096 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070097 mp->context = s->session_index;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +000098 mp->dscp = s->dscp;
Florin Coras458089b2019-08-21 16:20:44 -070099 mp->wrk_index = wrk->vpp_wrk_index;
100 mp->is_ip4 = s->transport.is_ip4;
101 mp->parent_handle = s->parent_handle;
102 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700103 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700104 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000105 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700106 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800107 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000108 if (s->flags & VCL_SESSION_F_CONNECTED)
109 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700110 if (s->ext_config)
111 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700112 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700113
114 if (s->ext_config)
115 {
116 clib_mem_free (s->ext_config);
117 s->ext_config = 0;
118 }
Florin Coras458089b2019-08-21 16:20:44 -0700119}
120
121void
122vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
123{
124 app_session_evt_t _app_evt, *app_evt = &_app_evt;
125 session_unlisten_msg_t *mp;
126 svm_msg_q_t *mq;
127
128 mq = vcl_worker_ctrl_mq (wrk);
129 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
130 mp = (session_unlisten_msg_t *) app_evt->evt->data;
131 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700132 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700133 mp->wrk_index = wrk->vpp_wrk_index;
134 mp->handle = s->vpp_handle;
135 mp->context = wrk->wrk_index;
136 app_send_ctrl_evt_to_vpp (mq, app_evt);
137}
138
139static void
liuyacan534468e2021-05-09 03:50:40 +0000140vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
141{
142 app_session_evt_t _app_evt, *app_evt = &_app_evt;
143 session_shutdown_msg_t *mp;
144 svm_msg_q_t *mq;
145
146 /* Send to thread that owns the session */
147 mq = s->vpp_evt_q;
148 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
149 mp = (session_shutdown_msg_t *) app_evt->evt->data;
150 memset (mp, 0, sizeof (*mp));
151 mp->client_index = wrk->api_client_handle;
152 mp->handle = s->vpp_handle;
153 app_send_ctrl_evt_to_vpp (mq, app_evt);
154}
155
156static void
Florin Coras458089b2019-08-21 16:20:44 -0700157vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
158{
159 app_session_evt_t _app_evt, *app_evt = &_app_evt;
160 session_disconnect_msg_t *mp;
161 svm_msg_q_t *mq;
162
163 /* Send to thread that owns the session */
164 mq = s->vpp_evt_q;
165 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
166 mp = (session_disconnect_msg_t *) app_evt->evt->data;
167 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700168 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700169 mp->handle = s->vpp_handle;
170 app_send_ctrl_evt_to_vpp (mq, app_evt);
171}
172
173static void
174vcl_send_app_detach (vcl_worker_t * wrk)
175{
176 app_session_evt_t _app_evt, *app_evt = &_app_evt;
177 session_app_detach_msg_t *mp;
178 svm_msg_q_t *mq;
179
180 mq = vcl_worker_ctrl_mq (wrk);
181 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
182 mp = (session_app_detach_msg_t *) app_evt->evt->data;
183 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700184 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700185 app_send_ctrl_evt_to_vpp (mq, app_evt);
186}
Florin Coras697faea2018-06-27 17:10:49 -0700187
Florin Coras54693d22018-07-17 10:46:29 -0700188static void
189vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
190 session_handle_t handle, int retval)
191{
192 app_session_evt_t _app_evt, *app_evt = &_app_evt;
193 session_accepted_reply_msg_t *rmp;
194 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
195 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
196 rmp->handle = handle;
197 rmp->context = context;
198 rmp->retval = retval;
199 app_send_ctrl_evt_to_vpp (mq, app_evt);
200}
201
Florin Coras99368312018-08-02 10:45:44 -0700202static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800203vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
204 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700205{
206 app_session_evt_t _app_evt, *app_evt = &_app_evt;
207 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800208 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700209 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
210 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800211 rmp->handle = s->vpp_handle;
212 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700213 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800214 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700215}
216
Florin Corasc9fbd662018-08-24 12:59:56 -0700217static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800218vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
219 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700220{
221 app_session_evt_t _app_evt, *app_evt = &_app_evt;
222 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800223 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
224 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700225 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800226 rmp->handle = s->vpp_handle;
227 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700228 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800229 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700230}
231
Florin Coras30e79c22019-01-02 19:31:22 -0800232void
233vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
234 u32 wrk_index)
235{
236 app_session_evt_t _app_evt, *app_evt = &_app_evt;
237 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800238
Florin Coras52dd29f2020-11-18 19:02:17 -0800239 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
240 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800241 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700242 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800243 mp->handle = s->vpp_handle;
244 mp->req_wrk_index = wrk->vpp_wrk_index;
245 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800246 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800247}
248
hanlina3a48962020-07-13 11:09:15 +0800249int
Florin Coras40c07ce2020-07-16 20:46:17 -0700250vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
251{
252 app_session_evt_t _app_evt, *app_evt = &_app_evt;
253 session_app_wrk_rpc_msg_t *mp;
254 vcl_worker_t *dst_wrk, *wrk;
255 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800256 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700257
258 if (data_len > sizeof (mp->data))
259 goto done;
260
261 clib_spinlock_lock (&vcm->workers_lock);
262
263 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
264 if (!dst_wrk)
265 goto done;
266
267 wrk = vcl_worker_get_current ();
268 mq = vcl_worker_ctrl_mq (wrk);
269 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
270 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700271 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700272 mp->wrk_index = dst_wrk->vpp_wrk_index;
273 clib_memcpy (mp->data, data, data_len);
274 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800275 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700276
277done:
278 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800279 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700280}
281
Florin Coras04ae8272021-04-12 19:55:37 -0700282int
283vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
284 transport_endpt_attr_t *attr)
285{
286 app_session_evt_t _app_evt, *app_evt = &_app_evt;
287 session_transport_attr_msg_t *mp;
288 svm_msg_q_t *mq;
289 f64 timeout;
290
291 ASSERT (!wrk->session_attr_op);
Liangxing Wang22112772022-05-13 04:24:19 +0000292 mq = s->vpp_evt_q;
293 if (PREDICT_FALSE (!mq))
294 {
295 /* FIXME: attribute should be stored and sent once session is
296 * bound/connected to vpp */
297 return 0;
298 }
299
Florin Coras04ae8272021-04-12 19:55:37 -0700300 wrk->session_attr_op = 1;
301 wrk->session_attr_op_rv = -1;
302
Florin Coras04ae8272021-04-12 19:55:37 -0700303 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
304 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
305 memset (mp, 0, sizeof (*mp));
306 mp->client_index = wrk->api_client_handle;
307 mp->handle = s->vpp_handle;
308 mp->is_get = is_get;
309 mp->attr = *attr;
310 app_send_ctrl_evt_to_vpp (mq, app_evt);
311
312 timeout = clib_time_now (&wrk->clib_time) + 1;
313
314 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
315 vcl_flush_mq_events ();
316
317 if (!wrk->session_attr_op_rv && is_get)
318 *attr = wrk->session_attr_rv;
319
320 wrk->session_attr_op = 0;
321
322 return wrk->session_attr_op_rv;
323}
324
Florin Coras54693d22018-07-17 10:46:29 -0700325static u32
Florin Coras00cca802019-06-06 09:38:44 -0700326vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
327 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700328{
329 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700330 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700331
Florin Coras134a9962018-08-28 11:32:04 -0700332 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700333
Florin Coras00cca802019-06-06 09:38:44 -0700334 listen_session = vcl_session_get (wrk, ls_index);
335 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700336 {
Florin Coras00cca802019-06-06 09:38:44 -0700337 VDBG (0, "ERROR: listener handle %lu does not match session %u",
338 mp->listener_handle, ls_index);
339 goto error;
340 }
341
Florin Corasf6e284b2021-07-21 18:17:20 -0700342 if (vcl_segment_attach_session (
343 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
344 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700345 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800346 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
347 session->session_index, mp->handle);
Florin Coras00cca802019-06-06 09:38:44 -0700348 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700349 }
350
Florin Coras54693d22018-07-17 10:46:29 -0700351 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700352 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700353 session->transport.rmt_port = mp->rmt.port;
354 session->transport.is_ip4 = mp->rmt.is_ip4;
355 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500356 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700357
Florin Coras134a9962018-08-28 11:32:04 -0700358 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800359 session->transport.lcl_port = mp->lcl.port;
360 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700361 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200362 session->is_dgram = vcl_proto_is_dgram (session->session_type);
363 session->listener_index = listen_session->session_index;
364 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700365
Florin Coras54693d22018-07-17 10:46:29 -0700366 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
367
Florin Coras00cca802019-06-06 09:38:44 -0700368 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
369 session->vpp_handle, 0);
370
Florin Coras134a9962018-08-28 11:32:04 -0700371 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700372
373error:
Florin Corasb4624182020-12-11 13:58:12 -0800374 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
375 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700376 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
377 VNET_API_ERROR_INVALID_ARGUMENT);
378 vcl_session_free (wrk, session);
379 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700380}
381
382static u32
Florin Coras134a9962018-08-28 11:32:04 -0700383vcl_session_connected_handler (vcl_worker_t * wrk,
384 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700385{
Florin Coras99368312018-08-02 10:45:44 -0700386 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800387 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700388
389 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700390 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800391 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700392 {
Florin Coras8d42c752021-11-29 23:26:19 -0800393 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
394 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800395 /* Should not happen but if it does, force vpp session cleanup */
396 vcl_session_t tmp_session = {
397 .vpp_handle = mp->handle,
398 .vpp_evt_q = 0,
399 };
400 vcl_segment_attach_session (
401 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
402 mp->vpp_event_queue_address, mp->mq_index, 0, session);
403 if (tmp_session.vpp_evt_q)
404 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700405 return VCL_INVALID_SESSION_INDEX;
406 }
Florin Coras8d42c752021-11-29 23:26:19 -0800407
Florin Coras54693d22018-07-17 10:46:29 -0700408 if (mp->retval)
409 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800410 VDBG (0, "session %u: connect failed! %U", session_index,
Florin Coras8d42c752021-11-29 23:26:19 -0800411 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700412 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800413 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +0530414 session->vpp_error = mp->retval;
Florin Coras070453d2018-08-24 17:04:27 -0700415 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700416 }
417
Florin Corasdbc9c592019-09-25 16:37:43 -0700418 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800419
Florin Coras8d42c752021-11-29 23:26:19 -0800420 /* Add to lookup table. Even if something fails, session cannot be
421 * cleaned up prior to notifying vpp and going through the cleanup
422 * "procedure" see @ref vcl_session_cleanup_handler */
423 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
424
Florin Corasf6e284b2021-07-21 18:17:20 -0700425 if (vcl_segment_attach_session (
426 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
427 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800428 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800429 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
430 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800431 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700432 vcl_send_session_disconnect (wrk, session);
433 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800434 }
435
Florin Coras653e43f2019-03-04 10:56:23 -0800436 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700437 {
Florin Corasc547e912020-12-08 17:50:45 -0800438 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700439 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
440 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800441 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800442 VDBG (0, "session %u [0x%llx]: failed to attach ct fifos",
443 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800444 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700445 vcl_send_session_disconnect (wrk, session);
446 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800447 }
Florin Coras99368312018-08-02 10:45:44 -0700448 }
Florin Coras54693d22018-07-17 10:46:29 -0700449
Florin Coras09d18c22019-04-24 11:10:02 -0700450 session->transport.is_ip4 = mp->lcl.is_ip4;
451 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500452 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700453 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700454
455 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700456 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700457 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700458 vcl_send_session_disconnect (wrk, session);
459 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700460 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700461
Florin Coras5ffb9642021-12-03 17:24:13 -0800462 VDBG (0, "session %u [0x%llx] connected local: %U:%u remote %U:%u",
463 session->session_index, session->vpp_handle, vcl_format_ip46_address,
464 &session->transport.lcl_ip,
465 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
466 clib_net_to_host_u16 (session->transport.lcl_port),
467 vcl_format_ip46_address, &session->transport.rmt_ip,
468 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
469 clib_net_to_host_u16 (session->transport.rmt_port));
Florin Coras070453d2018-08-24 17:04:27 -0700470
Florin Coras54693d22018-07-17 10:46:29 -0700471 return session_index;
472}
473
Florin Coras3c7d4f92018-12-14 11:28:43 -0800474static int
475vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
476{
477 vcl_session_msg_t *accepted_msg;
478 int i;
479
480 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
481 {
482 accepted_msg = &session->accept_evts_fifo[i];
483 if (accepted_msg->accepted_msg.handle == handle)
484 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800485 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800486 return 1;
487 }
488 }
489 return 0;
490}
491
Florin Corasc9fbd662018-08-24 12:59:56 -0700492static u32
Florin Coras134a9962018-08-28 11:32:04 -0700493vcl_session_reset_handler (vcl_worker_t * wrk,
494 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700495{
496 vcl_session_t *session;
497 u32 sid;
498
Florin Coras134a9962018-08-28 11:32:04 -0700499 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
500 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700501 if (!session)
502 {
503 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
504 return VCL_INVALID_SESSION_INDEX;
505 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800506
507 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700508 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800509 {
510
511 if (!vcl_flag_accepted_session (session, reset_msg->handle,
512 VCL_ACCEPTED_F_RESET))
513 VDBG (0, "session was not accepted!");
514 return VCL_INVALID_SESSION_INDEX;
515 }
516
Florin Corasc127d5a2020-10-14 16:35:58 -0700517 if (session->session_state != VCL_STATE_CLOSED)
518 session->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +0800519
520 session->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Coras5ffb9642021-12-03 17:24:13 -0800521 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700522 return sid;
523}
524
Florin Coras60116992018-08-27 09:52:18 -0700525static u32
Florin Coras134a9962018-08-28 11:32:04 -0700526vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700527{
528 vcl_session_t *session;
529 u32 sid = mp->context;
530
Florin Coras134a9962018-08-28 11:32:04 -0700531 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700532 if (mp->retval)
533 {
Florin Coras5e062572019-03-14 19:07:51 -0700534 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700535 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700536 if (session)
537 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700538 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700539 session->vpp_handle = mp->handle;
540 return sid;
541 }
542 else
543 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800544 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
545 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700546 return VCL_INVALID_SESSION_INDEX;
547 }
548 }
549
550 session->vpp_handle = mp->handle;
551 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500552 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
553 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700554 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700555 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700556 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800557
Florin Coras6e3c1f82020-01-15 01:30:46 +0000558 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700559 {
Florin Corasc547e912020-12-08 17:50:45 -0800560 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700561 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
562 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800563 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800564 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
565 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800566 session->session_state = VCL_STATE_DETACHED;
567 return VCL_INVALID_SESSION_INDEX;
568 }
Florin Coras60116992018-08-27 09:52:18 -0700569 }
570
Florin Coras05ecfcc2018-12-12 18:19:39 -0800571 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700572 return sid;
573}
574
Florin Corasdfae9f92019-02-20 19:48:31 -0800575static void
576vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
577{
578 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
579 vcl_session_t *s;
580
581 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000582 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800583 {
584 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
585 return;
586 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700587 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000588 {
589 /* Connected udp listener */
590 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700591 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000592 return;
593
594 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
595 return;
596 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800597
598 if (mp->retval)
599 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700600 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800601
602 if (mp->context != wrk->wrk_index)
603 VDBG (0, "wrong context");
604
605 vcl_session_table_del_vpp_handle (wrk, mp->handle);
606 vcl_session_free (wrk, s);
607}
608
Florin Coras68b7e582020-01-21 18:33:23 -0800609static void
610vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
611{
612 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
613 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800614 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800615
616 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
617 if (!s)
618 {
619 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
620 return;
621 }
622
Florin Coras1bb74942021-02-10 15:26:37 -0800623 /* Only validate if a value is provided */
624 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800625 {
Florin Coras1bb74942021-02-10 15:26:37 -0800626 fs_index = vcl_segment_table_lookup (mp->segment_handle);
627 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
628 {
629 VDBG (0, "segment %lx for session %u is not mounted!",
630 mp->segment_handle, s->session_index);
631 s->session_state = VCL_STATE_DETACHED;
632 return;
633 }
Florin Corasc547e912020-12-08 17:50:45 -0800634 }
635
Florin Coras57660d92020-04-04 22:45:34 +0000636 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800637
638 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
639 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800640
Florin Coras68b7e582020-01-21 18:33:23 -0800641 vcl_session_table_del_vpp_handle (wrk, mp->handle);
642 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
643
644 /* Generate new tx event if we have outstanding data */
645 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800646 app_send_io_evt_to_vpp (s->vpp_evt_q,
647 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800648 SESSION_IO_EVT_TX, SVM_Q_WAIT);
649
Florin Coras57660d92020-04-04 22:45:34 +0000650 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800651 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800652}
653
Florin Coras3c7d4f92018-12-14 11:28:43 -0800654static vcl_session_t *
655vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
656{
657 vcl_session_msg_t *vcl_msg;
658 vcl_session_t *session;
659
660 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
661 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800662 VWRN ("session overlap handle %lu state %u!", msg->handle,
663 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800664
665 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
666 if (!session)
667 {
668 VERR ("couldn't find listen session: listener handle %llx",
669 msg->listener_handle);
670 return 0;
671 }
672
673 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000674 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800675 vcl_msg->accepted_msg = *msg;
676 /* Session handle points to listener until fully accepted by app */
677 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
678
679 return session;
680}
681
682static vcl_session_t *
683vcl_session_disconnected_handler (vcl_worker_t * wrk,
684 session_disconnected_msg_t * msg)
685{
686 vcl_session_t *session;
687
688 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
689 if (!session)
690 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800691 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800692 return 0;
693 }
694
Florin Corasadcfb152020-02-12 08:50:29 +0000695 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700696 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000697 return 0;
698
Florin Coras3c7d4f92018-12-14 11:28:43 -0800699 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700700 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800701 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800702 if (!vcl_flag_accepted_session (session, msg->handle,
703 VCL_ACCEPTED_F_CLOSED))
704 VDBG (0, "session was not accepted!");
705 return 0;
706 }
707
Florin Corasadcfb152020-02-12 08:50:29 +0000708 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700709 if (session->session_state != VCL_STATE_DISCONNECT)
710 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000711
Florin Coras3c7d4f92018-12-14 11:28:43 -0800712 return session;
713}
714
liuyacan534468e2021-05-09 03:50:40 +0000715int
liuyacan55c952e2021-06-13 14:54:55 +0800716vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000717{
718 vcl_worker_t *wrk = vcl_worker_get_current ();
719 vcl_session_t *session;
720 vcl_session_state_t state;
721 u64 vpp_handle;
722
723 session = vcl_session_get_w_handle (wrk, session_handle);
724 if (PREDICT_FALSE (!session))
725 return VPPCOM_EBADFD;
726
727 vpp_handle = session->vpp_handle;
728 state = session->session_state;
729
730 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800731 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000732
733 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
734 {
735 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
736 return VPPCOM_EBADFD;
737 }
738
liuyacan55c952e2021-06-13 14:54:55 +0800739 if (how == SHUT_RD || how == SHUT_RDWR)
740 {
741 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
742 if (how == SHUT_RD)
743 return VPPCOM_OK;
744 }
745 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
746
liuyacan534468e2021-05-09 03:50:40 +0000747 if (PREDICT_TRUE (state == VCL_STATE_READY))
748 {
749 VDBG (1, "session %u [0x%llx]: sending shutdown...",
750 session->session_index, vpp_handle);
751
752 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000753 }
754
755 return VPPCOM_OK;
756}
757
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700758static int
759vppcom_session_disconnect (u32 session_handle)
760{
761 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700762 vcl_session_t *session, *listen_session;
763 vcl_session_state_t state;
764 u64 vpp_handle;
765
766 session = vcl_session_get_w_handle (wrk, session_handle);
767 if (!session)
768 return VPPCOM_EBADFD;
769
770 vpp_handle = session->vpp_handle;
771 state = session->session_state;
772
Florin Coras5ffb9642021-12-03 17:24:13 -0800773 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
774 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700775
776 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
777 {
778 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
779 return VPPCOM_EBADFD;
780 }
781
782 if (state == VCL_STATE_VPP_CLOSING)
783 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800784 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700785 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
786 session->session_index, vpp_handle);
787 }
788 else
789 {
790 /* Session doesn't have an event queue yet. Probably a non-blocking
791 * connect. Wait for the reply */
792 if (PREDICT_FALSE (!session->vpp_evt_q))
793 return VPPCOM_OK;
794
Florin Coras5ffb9642021-12-03 17:24:13 -0800795 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700796 session->session_index, vpp_handle);
797 vcl_send_session_disconnect (wrk, session);
798 }
799
800 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
801 {
802 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700803 if (listen_session)
804 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700805 }
806
807 return VPPCOM_OK;
808}
809
Florin Coras30e79c22019-01-02 19:31:22 -0800810static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700811vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
812{
813 session_cleanup_msg_t *msg;
814 vcl_session_t *session;
815
816 msg = (session_cleanup_msg_t *) data;
817 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
818 if (!session)
819 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800820 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700821 return;
822 }
823
Florin Coras36d49392020-04-24 23:00:11 +0000824 if (msg->type == SESSION_CLEANUP_TRANSPORT)
825 {
826 /* Transport was cleaned up before we confirmed close. Probably the
827 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700828 * Confirm close to make sure everything is cleaned up.
829 * Move to undetermined state to ensure that the session is not
830 * removed before both vpp and the app cleanup.
831 * - If the app closes first, the session is moved to CLOSED state
832 * and the session cleanup notification from vpp removes the
833 * session.
834 * - If vpp cleans up the session first, the session is moved to
835 * DETACHED state lower and subsequently the close from the app
836 * frees the session
837 */
838 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700839 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700840 vppcom_session_disconnect (vcl_session_handle (session));
841 session->session_state = VCL_STATE_UPDATED;
842 }
843 else if (session->session_state == VCL_STATE_DISCONNECT)
844 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800845 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700846 session->session_state = VCL_STATE_UPDATED;
847 }
Florin Coras36d49392020-04-24 23:00:11 +0000848 return;
849 }
850
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800851 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700852 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800853
854 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700855 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000856 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800857 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700858 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000859 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
860 return;
861 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800862
863 /* Session probably tracked with epoll, disconnect not yet handled and
864 * 1) both transport and session cleanup completed 2) app closed. Wait
865 * until message is drained to free the session.
866 * See @ref vcl_handle_mq_event */
867 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
868 {
869 session->flags |= VCL_SESSION_F_PENDING_FREE;
870 return;
871 }
872
Florin Coras9ace36d2019-10-28 13:14:17 -0700873 vcl_session_free (wrk, session);
874}
875
876static void
Florin Coras30e79c22019-01-02 19:31:22 -0800877vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
878{
879 session_req_worker_update_msg_t *msg;
880 vcl_session_t *s;
881
882 msg = (session_req_worker_update_msg_t *) data;
883 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
884 if (!s)
885 return;
886
887 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
888}
889
890static void
891vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
892{
893 session_worker_update_reply_msg_t *msg;
894 vcl_session_t *s;
895
896 msg = (session_worker_update_reply_msg_t *) data;
897 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
898 if (!s)
899 {
900 VDBG (0, "unknown handle 0x%llx", msg->handle);
901 return;
902 }
Florin Coras30e79c22019-01-02 19:31:22 -0800903
Florin Coras5f45e012019-01-23 09:21:30 -0800904 if (s->rx_fifo)
905 {
Florin Corasc547e912020-12-08 17:50:45 -0800906 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700907 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800908 {
909 VDBG (0, "failed to attach fifos for %u", s->session_index);
910 return;
911 }
Florin Coras5f45e012019-01-23 09:21:30 -0800912 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700913 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800914
Florin Coras30e79c22019-01-02 19:31:22 -0800915 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
916 s->vpp_handle, wrk->wrk_index);
917}
918
Florin Coras935ce752020-09-08 22:43:47 -0700919static int
920vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
921{
922
923 if (vcm->cfg.vpp_app_socket_api)
924 return vcl_sapi_recv_fds (wrk, fds, n_fds);
925
926 return vcl_bapi_recv_fds (wrk, fds, n_fds);
927}
928
Florin Corasc4c4cf52019-08-24 18:17:34 -0700929static void
930vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
931{
932 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
933 session_app_add_segment_msg_t *msg;
934 u64 segment_handle;
935 int fd = -1;
936
937 msg = (session_app_add_segment_msg_t *) data;
938
939 if (msg->fd_flags)
940 {
Florin Coras935ce752020-09-08 22:43:47 -0700941 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700942 seg_type = SSVM_SEGMENT_MEMFD;
943 }
944
945 segment_handle = msg->segment_handle;
946 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
947 {
948 clib_warning ("invalid segment handle");
949 return;
950 }
951
952 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
953 seg_type, fd))
954 {
955 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
956 return;
957 }
958
959 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
960 msg->segment_size);
961}
962
963static void
964vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
965{
966 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
967 vcl_segment_detach (msg->segment_handle);
968 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
969}
970
Florin Coras40c07ce2020-07-16 20:46:17 -0700971static void
972vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
973{
974 if (!vcm->wrk_rpc_fn)
975 return;
976
hanlina3a48962020-07-13 11:09:15 +0800977 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700978}
979
Florin Coras04ae8272021-04-12 19:55:37 -0700980static void
981vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
982{
983 session_transport_attr_reply_msg_t *mp;
984
985 if (!wrk->session_attr_op)
986 return;
987
988 mp = (session_transport_attr_reply_msg_t *) data;
989
990 wrk->session_attr_op_rv = mp->retval;
991 wrk->session_attr_op = 0;
992 wrk->session_attr_rv = mp->attr;
993}
994
Florin Coras86f04502018-09-12 16:08:01 -0700995static int
996vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700997{
Florin Coras54693d22018-07-17 10:46:29 -0700998 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700999 session_connected_msg_t *connected_msg;
1000 session_reset_msg_t *reset_msg;
1001 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001002 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001003 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001004
1005 switch (e->event_type)
1006 {
Florin Coras653e43f2019-03-04 10:56:23 -08001007 case SESSION_IO_EVT_RX:
1008 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001009 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001010 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001011 break;
Florin Coras86f04502018-09-12 16:08:01 -07001012 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001013 break;
Florin Corasb242d312020-10-26 15:35:40 -07001014 case SESSION_CTRL_EVT_BOUND:
1015 /* We can only wait for only one listen so not postponed */
1016 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1017 break;
Florin Coras54693d22018-07-17 10:46:29 -07001018 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001019 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1020 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1021 {
1022 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1023 *ecpy = *e;
1024 ecpy->postponed = 1;
1025 ecpy->session_index = s->session_index;
1026 }
Florin Coras54693d22018-07-17 10:46:29 -07001027 break;
1028 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001029 connected_msg = (session_connected_msg_t *) e->data;
1030 sid = vcl_session_connected_handler (wrk, connected_msg);
1031 if (!(s = vcl_session_get (wrk, sid)))
1032 break;
1033 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1034 {
1035 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1036 *ecpy = *e;
1037 ecpy->postponed = 1;
1038 ecpy->session_index = s->session_index;
1039 }
Florin Coras54693d22018-07-17 10:46:29 -07001040 break;
1041 case SESSION_CTRL_EVT_DISCONNECTED:
1042 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001043 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1044 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001045 if (s->session_state == VCL_STATE_CLOSED)
1046 break;
Florin Corasb242d312020-10-26 15:35:40 -07001047 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1048 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001049 s->session_state = VCL_STATE_VPP_CLOSING;
1050 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1051 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1052 *ecpy = *e;
1053 ecpy->postponed = 1;
1054 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001055 break;
1056 }
1057 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001058 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001059 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1060 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001061 break;
1062 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001063 reset_msg = (session_reset_msg_t *) e->data;
1064 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1065 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001066 if (s->session_state == VCL_STATE_CLOSED)
1067 break;
Florin Corasb242d312020-10-26 15:35:40 -07001068 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1069 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001070 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1071 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001072 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001073 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1074 *ecpy = *e;
1075 ecpy->postponed = 1;
1076 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001077 break;
1078 }
Florin Coras134a9962018-08-28 11:32:04 -07001079 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001080 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001081 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1082 vcl_session_unlisten_reply_handler (wrk, e->data);
1083 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001084 case SESSION_CTRL_EVT_MIGRATED:
1085 vcl_session_migrated_handler (wrk, e->data);
1086 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001087 case SESSION_CTRL_EVT_CLEANUP:
1088 vcl_session_cleanup_handler (wrk, e->data);
1089 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001090 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1091 vcl_session_req_worker_update_handler (wrk, e->data);
1092 break;
1093 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1094 vcl_session_worker_update_reply_handler (wrk, e->data);
1095 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001096 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1097 vcl_session_app_add_segment_handler (wrk, e->data);
1098 break;
1099 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1100 vcl_session_app_del_segment_handler (wrk, e->data);
1101 break;
hanlina3a48962020-07-13 11:09:15 +08001102 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001103 vcl_worker_rpc_handler (wrk, e->data);
1104 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001105 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1106 vcl_session_transport_attr_reply_handler (wrk, e->data);
1107 break;
Florin Coras54693d22018-07-17 10:46:29 -07001108 default:
1109 clib_warning ("unhandled %u", e->event_type);
1110 }
1111 return VPPCOM_OK;
1112}
1113
Florin Coras30e79c22019-01-02 19:31:22 -08001114static int
Florin Coras697faea2018-06-27 17:10:49 -07001115vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001116 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001117 f64 wait_for_time)
1118{
Florin Coras134a9962018-08-28 11:32:04 -07001119 vcl_worker_t *wrk = vcl_worker_get_current ();
1120 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001121 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001122 svm_msg_q_msg_t msg;
1123 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001124
Florin Coras697faea2018-06-27 17:10:49 -07001125 do
Dave Wallace543852a2017-08-03 02:11:34 -04001126 {
Florin Coras134a9962018-08-28 11:32:04 -07001127 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001128 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001129 {
Florin Coras070453d2018-08-24 17:04:27 -07001130 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001131 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001132 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001133 {
Florin Coras697faea2018-06-27 17:10:49 -07001134 return VPPCOM_OK;
1135 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001136 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001137 {
Florin Coras697faea2018-06-27 17:10:49 -07001138 return VPPCOM_ECONNREFUSED;
1139 }
Florin Coras54693d22018-07-17 10:46:29 -07001140
Florin Coras134a9962018-08-28 11:32:04 -07001141 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001142 {
1143 usleep (100);
1144 continue;
1145 }
Florin Coras134a9962018-08-28 11:32:04 -07001146 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001147 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001148 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001149 }
Florin Coras134a9962018-08-28 11:32:04 -07001150 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001151
Florin Coras05ecfcc2018-12-12 18:19:39 -08001152 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001153 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001154 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001155
Florin Coras697faea2018-06-27 17:10:49 -07001156 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001157}
1158
Florin Coras30e79c22019-01-02 19:31:22 -08001159static void
1160vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1161{
Florin Coras288eaab2019-02-03 15:26:14 -08001162 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001163 vcl_session_t *s;
1164 u32 *sip;
1165
1166 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1167 return;
1168
1169 vec_foreach (sip, wrk->pending_session_wrk_updates)
1170 {
1171 s = vcl_session_get (wrk, *sip);
1172 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1173 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001174 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1175 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001176 s->session_state = state;
1177 }
1178 vec_reset_length (wrk->pending_session_wrk_updates);
1179}
1180
Florin Corasf9240dc2019-01-15 08:03:17 -08001181void
Florin Coras5398dfb2021-01-25 20:31:27 -08001182vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001183{
Florin Coras30e79c22019-01-02 19:31:22 -08001184 svm_msg_q_msg_t *msg;
1185 session_event_t *e;
1186 svm_msg_q_t *mq;
1187 int i;
1188
1189 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001190 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001191
1192 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1193 {
1194 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1195 e = svm_msg_q_msg_data (mq, msg);
1196 vcl_handle_mq_event (wrk, e);
1197 svm_msg_q_free_msg (mq, msg);
1198 }
1199 vec_reset_length (wrk->mq_msg_vector);
1200 vcl_handle_pending_wrk_updates (wrk);
1201}
1202
Florin Coras5398dfb2021-01-25 20:31:27 -08001203void
1204vcl_flush_mq_events (void)
1205{
1206 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1207}
1208
Florin Coras697faea2018-06-27 17:10:49 -07001209static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001210vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001211{
Florin Coras134a9962018-08-28 11:32:04 -07001212 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001213 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001214 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001215 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001216
Florin Corasab2f6db2018-08-31 14:31:41 -07001217 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001218 if (!session)
1219 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001220
Florin Corasaaff5ee2019-07-08 13:00:00 -07001221 /* Flush pending accept events, if any */
1222 while (clib_fifo_elts (session->accept_evts_fifo))
1223 {
1224 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1225 accepted_msg = &evt->accepted_msg;
1226 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1227 vcl_send_session_accepted_reply (session->vpp_evt_q,
1228 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001229 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001230 }
1231 clib_fifo_free (session->accept_evts_fifo);
1232
Florin Coras458089b2019-08-21 16:20:44 -07001233 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001234
Florin Coras5e062572019-03-14 19:07:51 -07001235 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001236 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001237 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001238
1239 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001240 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001241
Florin Coras070453d2018-08-24 17:04:27 -07001242 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001243}
1244
Florin Coras940f78f2018-11-30 12:11:20 -08001245/**
1246 * Handle app exit
1247 *
1248 * Notify vpp of the disconnect and mark the worker as free. If we're the
1249 * last worker, do a full cleanup otherwise, since we're probably a forked
1250 * child, avoid syscalls as much as possible. We might've lost privileges.
1251 */
1252void
1253vppcom_app_exit (void)
1254{
1255 if (!pool_elts (vcm->workers))
1256 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001257 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1258 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001259 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001260}
1261
Florin Coras935ce752020-09-08 22:43:47 -07001262static int
1263vcl_api_attach (void)
1264{
1265 if (vcm->cfg.vpp_app_socket_api)
1266 return vcl_sapi_attach ();
1267
1268 return vcl_bapi_attach ();
1269}
1270
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001271int
1272vcl_is_first_reattach_to_execute ()
1273{
1274 if (vcm->reattach_count == 0)
1275 return 1;
1276
1277 return 0;
1278}
1279
1280void
1281vcl_set_reattach_counter ()
1282{
1283 ++vcm->reattach_count;
1284
1285 if (vcm->reattach_count == vec_len (vcm->workers))
1286 vcm->reattach_count = 0;
1287}
1288
1289/**
1290 * Reattach vcl to vpp after it has previously been disconnected.
1291 *
1292 * The logic should be:
1293 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1294 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1295 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1296 * from `vppcom_worker_register` since they were already allocated.
1297 */
1298
Florin Coras935ce752020-09-08 22:43:47 -07001299static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001300vcl_api_retry_attach (vcl_worker_t *wrk)
1301{
1302 vcl_session_t *s;
1303
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001304 clib_spinlock_lock (&vcm->workers_lock);
1305 if (vcl_is_first_reattach_to_execute ())
1306 {
1307 if (vcl_api_attach ())
1308 {
1309 clib_spinlock_unlock (&vcm->workers_lock);
1310 return;
1311 }
1312 vcl_set_reattach_counter ();
1313 clib_spinlock_unlock (&vcm->workers_lock);
1314 }
1315 else
1316 {
1317 vcl_set_reattach_counter ();
1318 clib_spinlock_unlock (&vcm->workers_lock);
1319 vcl_worker_register_with_vpp ();
1320 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001321
1322 /* Treat listeners as configuration that needs to be re-added to vpp */
1323 pool_foreach (s, wrk->sessions)
1324 {
1325 if (s->flags & VCL_SESSION_F_IS_VEP)
1326 continue;
1327 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1328 vppcom_session_listen (vcl_session_handle (s), 10);
1329 else
1330 VDBG (0, "internal error: unexpected state %d", s->session_state);
1331 }
1332}
1333
1334static void
1335vcl_api_handle_disconnect (vcl_worker_t *wrk)
1336{
1337 wrk->api_client_handle = ~0;
1338 vcl_worker_detach_sessions (wrk);
1339}
1340
1341static void
Florin Coras935ce752020-09-08 22:43:47 -07001342vcl_api_detach (vcl_worker_t * wrk)
1343{
Florin Corasd04ea442022-03-30 16:08:25 -07001344 if (wrk->api_client_handle == ~0)
1345 return;
1346
Florin Coras935ce752020-09-08 22:43:47 -07001347 vcl_send_app_detach (wrk);
1348
1349 if (vcm->cfg.vpp_app_socket_api)
1350 return vcl_sapi_detach (wrk);
1351
1352 return vcl_bapi_disconnect_from_vpp ();
1353}
1354
Dave Wallace543852a2017-08-03 02:11:34 -04001355/*
1356 * VPPCOM Public API functions
1357 */
1358int
Florin Coras66ec4672020-06-15 07:59:40 -07001359vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001360{
Dave Wallace543852a2017-08-03 02:11:34 -04001361 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001362 int rv;
1363
Florin Coras47c40e22018-11-26 17:01:36 -08001364 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001365 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001366 VDBG (1, "already initialized");
1367 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001368 }
1369
Florin Coras47c40e22018-11-26 17:01:36 -08001370 vcm->is_init = 1;
1371 vppcom_cfg (&vcm->cfg);
1372 vcl_cfg = &vcm->cfg;
1373
1374 vcm->main_cpu = pthread_self ();
1375 vcm->main_pid = getpid ();
1376 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001377 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1378 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001379 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1380 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001381 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001382 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001383 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001384
1385 /* Allocate default worker */
1386 vcl_worker_alloc_and_init ();
1387
Florin Coras935ce752020-09-08 22:43:47 -07001388 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001389 {
1390 vppcom_app_destroy ();
1391 return rv;
1392 }
Florin Coras47c40e22018-11-26 17:01:36 -08001393
1394 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001395 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001396
1397 return VPPCOM_OK;
1398}
1399
1400void
1401vppcom_app_destroy (void)
1402{
Florin Corasdc0ded72020-04-30 02:59:55 +00001403 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001404 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001405
Florin Coras940f78f2018-11-30 12:11:20 -08001406 if (!pool_elts (vcm->workers))
1407 return;
1408
Florin Coras0d427d82018-06-27 03:24:07 -07001409 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001410
Florin Corasdc0ded72020-04-30 02:59:55 +00001411 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001412
Florin Corasce815de2020-04-16 18:47:27 +00001413 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001414 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001415 if (current_wrk != wrk)
1416 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001417 }
Florin Corasce815de2020-04-16 18:47:27 +00001418 /* *INDENT-ON* */
1419
Florin Coras935ce752020-09-08 22:43:47 -07001420 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001421 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001422 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001423
Florin Coras0d427d82018-06-27 03:24:07 -07001424 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001425
1426 /*
1427 * Free the heap and fix vcm
1428 */
1429 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001430 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001431
1432 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001433 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001434}
1435
1436int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001437vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001438{
Florin Coras134a9962018-08-28 11:32:04 -07001439 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001440 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001441
Florin Coras134a9962018-08-28 11:32:04 -07001442 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001443
Florin Coras7e12d942018-06-27 14:32:43 -07001444 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001445 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001446 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001447 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301448 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001449
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001450 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001451 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001452
Florin Coras7e12d942018-06-27 14:32:43 -07001453 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1454 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001455
Florin Coras5e062572019-03-14 19:07:51 -07001456 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001457
Florin Coras134a9962018-08-28 11:32:04 -07001458 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001459}
1460
Florin Corasfe286f72021-06-04 10:07:55 -07001461static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001462vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001463{
Florin Corasfa3884f2021-06-22 20:04:46 -07001464 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001465
Florin Corasfa3884f2021-06-22 20:04:46 -07001466 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001467 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001468 wrk->ep_lt_current = s->session_index;
1469 s->vep.lt_next = s->session_index;
1470 s->vep.lt_prev = s->session_index;
1471 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001472 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001473
1474 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1475 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1476
1477 prev->vep.lt_next = s->session_index;
1478 s->vep.lt_prev = prev->session_index;
1479
1480 s->vep.lt_next = cur->session_index;
1481 cur->vep.lt_prev = s->session_index;
1482}
1483
1484static void
1485vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1486{
1487 vcl_session_t *prev, *next;
1488
1489 if (s->vep.lt_next == s->session_index)
1490 {
1491 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1492 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1493 return;
1494 }
1495
1496 prev = vcl_session_get (wrk, s->vep.lt_prev);
1497 next = vcl_session_get (wrk, s->vep.lt_next);
1498
1499 prev->vep.lt_next = next->session_index;
1500 next->vep.lt_prev = prev->session_index;
1501
1502 if (s->session_index == wrk->ep_lt_current)
1503 wrk->ep_lt_current = s->vep.lt_next;
1504
1505 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001506}
1507
Dave Wallace543852a2017-08-03 02:11:34 -04001508int
Florin Corasc127d5a2020-10-14 16:35:58 -07001509vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001510 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001511{
Florin Coras134a9962018-08-28 11:32:04 -07001512 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001513
Florin Coras6c3b2182020-10-19 18:36:48 -07001514 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001515
Florin Coras6c3b2182020-10-19 18:36:48 -07001516 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001517 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001518 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001519 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001520 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001521 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001522 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001523 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001524 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1525 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001526 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001527 }
Florin Corase4306b62020-10-28 12:51:10 -07001528 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001529 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001530
Florin Coras6c3b2182020-10-19 18:36:48 -07001531 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001532 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001533 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001534 if (rv < 0)
1535 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001536 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1537 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001538 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001539
Florin Coras9ace36d2019-10-28 13:14:17 -07001540 if (!do_disconnect)
1541 {
1542 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001543 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001544 goto cleanup;
1545 }
1546
Florin Coras6c3b2182020-10-19 18:36:48 -07001547 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001548 {
1549 rv = vppcom_session_unbind (sh);
1550 if (PREDICT_FALSE (rv < 0))
1551 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001552 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001553 vppcom_retval_str (rv));
1554 return rv;
1555 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001556 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001557 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001558 {
1559 rv = vppcom_session_disconnect (sh);
1560 if (PREDICT_FALSE (rv < 0))
1561 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001562 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001563 rv, vppcom_retval_str (rv));
1564 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001565 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001566 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001567 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001568 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001569 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001570 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001571 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001572
1573 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1574 goto free_session;
1575
1576 /* Disconnect/reset messages pending but vpp transport and session
1577 * cleanups already done. Free only after messages drained. */
1578 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001579 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001580
Florin Corasc127d5a2020-10-14 16:35:58 -07001581 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001582
Florin Coras9ace36d2019-10-28 13:14:17 -07001583 /* Session is removed only after vpp confirms the disconnect */
1584 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001585
Florin Corasf9240dc2019-01-15 08:03:17 -08001586cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001587 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001588free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001589 vcl_session_free (wrk, s);
1590 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001591
Dave Wallace543852a2017-08-03 02:11:34 -04001592 return rv;
1593}
1594
1595int
Florin Corasf9240dc2019-01-15 08:03:17 -08001596vppcom_session_close (uint32_t session_handle)
1597{
1598 vcl_worker_t *wrk = vcl_worker_get_current ();
1599 vcl_session_t *session;
1600
1601 session = vcl_session_get_w_handle (wrk, session_handle);
1602 if (!session)
1603 return VPPCOM_EBADFD;
1604 return vcl_session_cleanup (wrk, session, session_handle,
1605 1 /* do_disconnect */ );
1606}
1607
1608int
Florin Coras134a9962018-08-28 11:32:04 -07001609vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001610{
Florin Coras134a9962018-08-28 11:32:04 -07001611 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001612 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001613
1614 if (!ep || !ep->ip)
1615 return VPPCOM_EINVAL;
1616
Florin Coras134a9962018-08-28 11:32:04 -07001617 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001618 if (!session)
1619 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001620
Florin Coras6c3b2182020-10-19 18:36:48 -07001621 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001622 {
Florin Coras5e062572019-03-14 19:07:51 -07001623 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1624 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001625 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001626 }
1627
Florin Coras7e12d942018-06-27 14:32:43 -07001628 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001629 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001630 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1631 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001632 else
Dave Barach178cf492018-11-13 16:34:13 -05001633 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1634 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001635 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001636
Florin Coras60687192021-11-29 21:00:47 -08001637 VDBG (0,
1638 "session %u handle %u: binding to local %s address %U port %u, "
1639 "proto %s",
1640 session->session_index, session_handle,
1641 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1642 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001643 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1644 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001645 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001646 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001647
1648 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001649 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001650
Florin Coras070453d2018-08-24 17:04:27 -07001651 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001652}
1653
1654int
Florin Coras134a9962018-08-28 11:32:04 -07001655vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001656{
Florin Coras134a9962018-08-28 11:32:04 -07001657 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001658 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001659 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001660 int rv;
1661
Florin Coras134a9962018-08-28 11:32:04 -07001662 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001663 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001664 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001665
Dave Wallaceee45d412017-11-24 21:44:06 -05001666 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001667 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001668 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001669 VDBG (0, "session %u [0x%llx]: already in listen state!",
1670 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001671 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001672 }
1673
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001674 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001675
Florin Coras070453d2018-08-24 17:04:27 -07001676 /*
1677 * Send listen request to vpp and wait for reply
1678 */
Florin Coras458089b2019-08-21 16:20:44 -07001679 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001680 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001681 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001682 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001683
Florin Coras070453d2018-08-24 17:04:27 -07001684 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001685 {
Florin Coras134a9962018-08-28 11:32:04 -07001686 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001687 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1688 listen_sh, listen_session->vpp_handle, rv,
1689 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001690 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001691 }
1692
Florin Coras070453d2018-08-24 17:04:27 -07001693 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001694}
1695
Florin Coras134a9962018-08-28 11:32:04 -07001696static int
Florin Coras5e062572019-03-14 19:07:51 -07001697validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001698{
Florin Coras6c3b2182020-10-19 18:36:48 -07001699 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001700 {
Florin Coras5e062572019-03-14 19:07:51 -07001701 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1702 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001703 return VPPCOM_EBADFD;
1704 }
1705
Florin Corasc127d5a2020-10-14 16:35:58 -07001706 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001707 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001708 {
Florin Coras60687192021-11-29 21:00:47 -08001709 VDBG (0,
1710 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1711 " (%s)",
1712 ls->vpp_handle, ls->session_state,
1713 vcl_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001714 return VPPCOM_EBADFD;
1715 }
1716 return VPPCOM_OK;
1717}
1718
1719int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001720vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1721{
1722 if (!strcmp (proto_str, "TCP"))
1723 *proto = VPPCOM_PROTO_TCP;
1724 else if (!strcmp (proto_str, "tcp"))
1725 *proto = VPPCOM_PROTO_TCP;
1726 else if (!strcmp (proto_str, "UDP"))
1727 *proto = VPPCOM_PROTO_UDP;
1728 else if (!strcmp (proto_str, "udp"))
1729 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001730 else if (!strcmp (proto_str, "TLS"))
1731 *proto = VPPCOM_PROTO_TLS;
1732 else if (!strcmp (proto_str, "tls"))
1733 *proto = VPPCOM_PROTO_TLS;
1734 else if (!strcmp (proto_str, "QUIC"))
1735 *proto = VPPCOM_PROTO_QUIC;
1736 else if (!strcmp (proto_str, "quic"))
1737 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001738 else if (!strcmp (proto_str, "DTLS"))
1739 *proto = VPPCOM_PROTO_DTLS;
1740 else if (!strcmp (proto_str, "dtls"))
1741 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001742 else if (!strcmp (proto_str, "SRTP"))
1743 *proto = VPPCOM_PROTO_SRTP;
1744 else if (!strcmp (proto_str, "srtp"))
1745 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001746 else
1747 return 1;
1748 return 0;
1749}
1750
1751int
Florin Coras134a9962018-08-28 11:32:04 -07001752vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001753 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001754{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001755 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001756 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001757 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001758 vcl_session_t *listen_session = 0;
1759 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001760 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001761 u8 is_nonblocking;
1762 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001763
Florin Coras5398dfb2021-01-25 20:31:27 -08001764again:
1765
Florin Coras134a9962018-08-28 11:32:04 -07001766 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001767 if (!listen_session)
1768 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001769
Florin Coras134a9962018-08-28 11:32:04 -07001770 listen_session_index = listen_session->session_index;
1771 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001772 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001773
Florin Coras54693d22018-07-17 10:46:29 -07001774 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001775 {
Florin Coras54693d22018-07-17 10:46:29 -07001776 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001777 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001778 accepted_msg = evt->accepted_msg;
1779 goto handle;
1780 }
1781
Florin Corasac422d62020-10-19 20:51:36 -07001782 is_nonblocking = vcl_session_has_attr (listen_session,
1783 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001784 while (1)
1785 {
Carl Smith592a9092019-11-12 14:57:37 +13001786 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1787 return VPPCOM_EAGAIN;
1788
Florin Coras5398dfb2021-01-25 20:31:27 -08001789 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1790 vcl_worker_flush_mq_events (wrk);
1791 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001792 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001793
Florin Coras54693d22018-07-17 10:46:29 -07001794handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001795
Florin Coras00cca802019-06-06 09:38:44 -07001796 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1797 listen_session_index);
1798 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1799 return VPPCOM_ECONNABORTED;
1800
Florin Coras134a9962018-08-28 11:32:04 -07001801 listen_session = vcl_session_get (wrk, listen_session_index);
1802 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001803
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001804 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001805 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001806
Florin Coras5e062572019-03-14 19:07:51 -07001807 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1808 " flags %d, is_nonblocking %u", listen_session->session_index,
1809 listen_session->vpp_handle, client_session_index,
1810 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001811 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001812
Dave Wallace048b1d62018-01-03 22:24:41 -05001813 if (ep)
1814 {
Florin Coras7e12d942018-06-27 14:32:43 -07001815 ep->is_ip4 = client_session->transport.is_ip4;
1816 ep->port = client_session->transport.rmt_port;
1817 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001818 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1819 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001820 else
Dave Barach178cf492018-11-13 16:34:13 -05001821 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1822 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001823 }
Dave Wallace60caa062017-11-10 17:07:13 -05001824
Florin Coras60687192021-11-29 21:00:47 -08001825 VDBG (0,
1826 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1827 "local: %U:%u",
1828 listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001829 client_session_index, client_session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001830 vcl_format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001831 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001832 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001833 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001834 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001835 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001836 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1837 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001838
Florin Coras3c7d4f92018-12-14 11:28:43 -08001839 /*
1840 * Session might have been closed already
1841 */
1842 if (accept_flags)
1843 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001844 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001845 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001846 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001847 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001848 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001849 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001850}
1851
1852int
Florin Coras134a9962018-08-28 11:32:04 -07001853vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001854{
Florin Coras134a9962018-08-28 11:32:04 -07001855 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001856 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001857 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001858 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001859
Florin Coras134a9962018-08-28 11:32:04 -07001860 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001861 if (!session)
1862 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001863 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001864
Florin Coras6c3b2182020-10-19 18:36:48 -07001865 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001866 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001867 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001868 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001869 }
1870
Florin Corasc127d5a2020-10-14 16:35:58 -07001871 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001872 {
Florin Coras60687192021-11-29 21:00:47 -08001873 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001874 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1875 " state (%s)",
1876 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001877 vcl_format_ip46_address, &session->transport.rmt_ip,
1878 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001879 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001880 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001881 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001882 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001883 }
1884
Florin Coras0a1e1832020-03-29 18:54:04 +00001885 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001886 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001887 {
1888 if (session->session_type != VPPCOM_PROTO_UDP)
1889 return VPPCOM_EINVAL;
1890 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001891 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001892 }
1893
Florin Coras7e12d942018-06-27 14:32:43 -07001894 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001895 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001896 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001897 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001898 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001899
Florin Coras5ffb9642021-12-03 17:24:13 -08001900 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1901 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001902 &session->transport.rmt_ip,
1903 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001904 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001905 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001906
Florin Coras458089b2019-08-21 16:20:44 -07001907 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001908
Florin Corasac422d62020-10-19 20:51:36 -07001909 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001910 {
fanyfa49d59d2020-10-13 17:07:16 +08001911 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001912 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001913 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001914 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001915 return VPPCOM_EINPROGRESS;
1916 }
Florin Coras57c88932019-08-29 12:03:17 -07001917
1918 /*
1919 * Wait for reply from vpp if blocking
1920 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001921 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001922 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001923
Florin Coras134a9962018-08-28 11:32:04 -07001924 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001925 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1926 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001927
Dave Wallace4878cbe2017-11-21 03:45:09 -05001928 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001929}
1930
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001931int
1932vppcom_session_stream_connect (uint32_t session_handle,
1933 uint32_t parent_session_handle)
1934{
1935 vcl_worker_t *wrk = vcl_worker_get_current ();
1936 vcl_session_t *session, *parent_session;
1937 u32 session_index, parent_session_index;
1938 int rv;
1939
1940 session = vcl_session_get_w_handle (wrk, session_handle);
1941 if (!session)
1942 return VPPCOM_EBADFD;
1943 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1944 if (!parent_session)
1945 return VPPCOM_EBADFD;
1946
1947 session_index = session->session_index;
1948 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001949 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001950 {
1951 VDBG (0, "ERROR: cannot connect epoll session %u!",
1952 session->session_index);
1953 return VPPCOM_EBADFD;
1954 }
1955
Florin Corasc127d5a2020-10-14 16:35:58 -07001956 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001957 {
Florin Coras60687192021-11-29 21:00:47 -08001958 VDBG (0,
1959 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001960 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001961 session_handle, session->vpp_handle, parent_session_handle,
1962 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001963 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001964 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001965 return VPPCOM_OK;
1966 }
1967
1968 /* Connect to quic session specifics */
1969 session->transport.is_ip4 = parent_session->transport.is_ip4;
1970 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1971 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001972 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001973
1974 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1975 session_handle, parent_session_handle, parent_session->vpp_handle);
1976
1977 /*
1978 * Send connect request and wait for reply from vpp
1979 */
Florin Coras458089b2019-08-21 16:20:44 -07001980 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001981 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001982 vcm->cfg.session_timeout);
1983
1984 session->listener_index = parent_session_index;
1985 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001986 if (parent_session)
1987 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001988
1989 session = vcl_session_get (wrk, session_index);
1990 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1991 session->vpp_handle, rv ? "failed" : "succeeded");
1992
1993 return rv;
1994}
1995
Steven58f464e2017-10-25 12:33:12 -07001996static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001997vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001998 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001999{
Florin Coras134a9962018-08-28 11:32:04 -07002000 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07002001 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002002 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002003 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002004 session_event_t *e;
2005 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07002006 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002007
Florin Coras070453d2018-08-24 17:04:27 -07002008 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08002009 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002010
Florin Coras134a9962018-08-28 11:32:04 -07002011 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002012 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07002013 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002014
Florin Coras6d0106e2019-01-29 20:11:58 -08002015 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002016 {
Florin Coras5e062572019-03-14 19:07:51 -07002017 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002018 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002019 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002020 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002021 }
2022
liuyacan55c952e2021-06-13 14:54:55 +08002023 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2024 {
2025 /* Vpp would ack the incoming data and enqueue it for reading.
2026 * So even if SHUT_RD is set, we can still read() the data if
2027 * the session is ready.
2028 */
2029 if (!vcl_session_read_ready (s))
2030 {
2031 return 0;
2032 }
2033 }
2034
Florin Corasac422d62020-10-19 20:51:36 -07002035 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002036 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002037 mq = wrk->app_event_queue;
2038 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002039 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002040
Sirshak Das28aa5392019-02-05 01:33:33 -06002041 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002042 {
Florin Coras54693d22018-07-17 10:46:29 -07002043 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002044 {
Yu Pingb2955352019-12-04 06:49:04 +08002045 if (vcl_session_is_closing (s))
2046 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002047 if (is_ct)
2048 svm_fifo_unset_event (s->rx_fifo);
2049 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002050 return VPPCOM_EWOULDBLOCK;
2051 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002052 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002053 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002054 if (vcl_session_is_closing (s))
2055 return vcl_session_closing_error (s);
2056
Florin Corasd6894562020-10-28 00:37:15 -07002057 if (is_ct)
2058 svm_fifo_unset_event (s->rx_fifo);
2059 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002060
Florin Coras5398dfb2021-01-25 20:31:27 -08002061 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2062 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002063 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002064 }
Florin Coras54693d22018-07-17 10:46:29 -07002065
Florin Coras4a2c7942020-09-10 12:27:14 -07002066read_again:
2067
Florin Coras460dce62018-07-27 05:45:06 -07002068 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002069 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002070 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002071 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2072
2073 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002074
2075 if (peek)
2076 return rv;
2077
Florin Coras4a2c7942020-09-10 12:27:14 -07002078 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002079
Sirshak Das28aa5392019-02-05 01:33:33 -06002080 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002081 {
Florin Corasd6894562020-10-28 00:37:15 -07002082 if (is_ct)
2083 svm_fifo_unset_event (s->rx_fifo);
2084 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002085 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002086 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002087 {
Florin Corasc34118b2020-08-12 18:58:25 -07002088 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2089 e->event_type = SESSION_IO_EVT_RX;
2090 e->session_index = s->session_index;
2091 }
2092 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002093 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002094 {
2095 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002096 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002097 buf += rv;
2098 n -= rv;
2099 goto read_again;
2100 }
Florin Coras41c9e042018-09-11 00:10:41 -07002101
Florin Coras17ec5772020-08-12 20:46:29 -07002102 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002103 {
Florin Coras17ec5772020-08-12 20:46:29 -07002104 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002105 app_send_io_evt_to_vpp (s->vpp_evt_q,
2106 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002107 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002108 }
2109
Florin Coras5e062572019-03-14 19:07:51 -07002110 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2111 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002112
Florin Coras54693d22018-07-17 10:46:29 -07002113 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002114}
2115
Steven58f464e2017-10-25 12:33:12 -07002116int
Florin Coras134a9962018-08-28 11:32:04 -07002117vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002118{
Florin Coras134a9962018-08-28 11:32:04 -07002119 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002120}
2121
2122static int
Florin Coras134a9962018-08-28 11:32:04 -07002123vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002124{
Florin Coras134a9962018-08-28 11:32:04 -07002125 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002126}
2127
Florin Coras2cba8532018-09-11 16:33:36 -07002128int
2129vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002130 vppcom_data_segment_t * ds, uint32_t n_segments,
2131 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002132{
2133 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002134 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002135 vcl_session_t *s = 0;
2136 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002137 svm_msg_q_t *mq;
2138 u8 is_ct;
2139
2140 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002141 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002142 return VPPCOM_EBADFD;
2143
Florin Coras6d0106e2019-01-29 20:11:58 -08002144 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2145 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002146
Florin Corasac422d62020-10-19 20:51:36 -07002147 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002148 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002149 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002150 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002151 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002152
Sirshak Das28aa5392019-02-05 01:33:33 -06002153 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002154 {
2155 if (is_nonblocking)
2156 {
Florin Coras62877022020-10-28 21:22:04 -07002157 if (is_ct)
2158 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002159 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002160 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002161 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002162 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002163 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002164 if (vcl_session_is_closing (s))
2165 return vcl_session_closing_error (s);
2166
Florin Coras62877022020-10-28 21:22:04 -07002167 if (is_ct)
2168 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002169 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002170
Florin Coras5398dfb2021-01-25 20:31:27 -08002171 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2172 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002173 }
2174 }
2175
Florin Corasd1cc38d2020-10-11 11:05:04 -07002176 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002177 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002178 if (n_read < 0)
2179 return VPPCOM_EAGAIN;
2180
2181 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2182 {
Florin Coras62877022020-10-28 21:22:04 -07002183 if (is_ct)
2184 svm_fifo_unset_event (s->rx_fifo);
2185 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002186 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002187 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002188 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002189 {
2190 session_event_t *e;
2191 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2192 e->event_type = SESSION_IO_EVT_RX;
2193 e->session_index = s->session_index;
2194 }
2195 }
2196
2197 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002198 return n_read;
2199}
2200
2201void
Florin Corasd68faf82020-09-25 15:18:13 -07002202vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002203{
2204 vcl_worker_t *wrk = vcl_worker_get_current ();
2205 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002206 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002207
2208 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002209 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002210 return;
2211
Florin Coras62877022020-10-28 21:22:04 -07002212 is_ct = vcl_session_is_ct (s);
2213 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002214
Florin Coras8cc93212021-09-10 11:37:12 -07002215 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002216 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002217}
2218
Florin Coras7a2abce2020-04-05 19:25:44 +00002219always_inline u8
2220vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002221{
Florin Coras7a2abce2020-04-05 19:25:44 +00002222 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2223 if (is_dgram)
2224 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2225 else
2226 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002227}
2228
2229always_inline int
2230vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2231 size_t n, u8 is_flush, u8 is_dgram)
2232{
Florin Coras6d0106e2019-01-29 20:11:58 -08002233 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002234 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002235 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002236 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002237 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002238
Florin Coras0b0d28e2021-06-04 17:31:53 -07002239 /* Accept zero length writes but just return */
2240 if (PREDICT_FALSE (!n))
2241 return VPPCOM_OK;
2242
2243 if (PREDICT_FALSE (!buf))
2244 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002245
Florin Coras6c3b2182020-10-19 18:36:48 -07002246 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002247 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002248 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2249 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002250 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002251 }
2252
liuyacan55c952e2021-06-13 14:54:55 +08002253 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002254 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002255 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2256 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002257 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002258 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002259 }
2260
liuyacan55c952e2021-06-13 14:54:55 +08002261 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2262 {
2263 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2264 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002265 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002266 return VPPCOM_EPIPE;
2267 }
2268
Florin Coras0e88e852018-09-17 22:09:02 -07002269 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002270 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002271 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002272
Florin Coras653e43f2019-03-04 10:56:23 -08002273 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002274 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002275 {
Florin Coras54693d22018-07-17 10:46:29 -07002276 if (is_nonblocking)
2277 {
Florin Coras070453d2018-08-24 17:04:27 -07002278 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002279 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002280 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002281 {
Florin Coras2d379d82019-06-28 12:45:12 -07002282 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002283 if (vcl_session_is_closing (s))
2284 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002285
Florin Coras5398dfb2021-01-25 20:31:27 -08002286 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2287 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002288 }
Dave Wallace543852a2017-08-03 02:11:34 -04002289 }
Dave Wallace543852a2017-08-03 02:11:34 -04002290
Florin Coras653e43f2019-03-04 10:56:23 -08002291 et = SESSION_IO_EVT_TX;
2292 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002293 et = SESSION_IO_EVT_TX_FLUSH;
2294
Florin Coras7a2abce2020-04-05 19:25:44 +00002295 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002296 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002297 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002298 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002299 else
2300 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002301 0 /* do_evt */ , SVM_Q_WAIT);
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
2327 return vppcom_session_write_inline (wrk, s, buf, n,
2328 0 /* is_flush */ , 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
2341 return vppcom_session_write_inline (wrk, s, buf, n,
2342 1 /* is_flush */ , 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;
2424 if (sid < n_bits && write_map)
2425 {
2426 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2427 *bits_set += 1;
2428 }
Florin Coras86f04502018-09-12 16:08:01 -07002429 break;
2430 case SESSION_CTRL_EVT_DISCONNECTED:
2431 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002432 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2433 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002434 break;
Florin Corasb242d312020-10-26 15:35:40 -07002435 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002436 if (sid < n_bits && except_map)
2437 {
David Johnsond9818dd2018-12-14 14:53:41 -05002438 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002439 *bits_set += 1;
2440 }
2441 break;
2442 case SESSION_CTRL_EVT_RESET:
2443 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2444 if (sid < n_bits && except_map)
2445 {
David Johnsond9818dd2018-12-14 14:53:41 -05002446 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002447 *bits_set += 1;
2448 }
2449 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002450 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2451 vcl_session_unlisten_reply_handler (wrk, e->data);
2452 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002453 case SESSION_CTRL_EVT_MIGRATED:
2454 vcl_session_migrated_handler (wrk, e->data);
2455 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002456 case SESSION_CTRL_EVT_CLEANUP:
2457 vcl_session_cleanup_handler (wrk, e->data);
2458 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002459 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2460 vcl_session_worker_update_reply_handler (wrk, e->data);
2461 break;
2462 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2463 vcl_session_req_worker_update_handler (wrk, e->data);
2464 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002465 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2466 vcl_session_app_add_segment_handler (wrk, e->data);
2467 break;
2468 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2469 vcl_session_app_del_segment_handler (wrk, e->data);
2470 break;
hanlina3a48962020-07-13 11:09:15 +08002471 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002472 vcl_worker_rpc_handler (wrk, e->data);
2473 break;
Florin Coras86f04502018-09-12 16:08:01 -07002474 default:
2475 clib_warning ("unhandled: %u", e->event_type);
2476 break;
2477 }
2478}
2479
2480static int
2481vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2482 unsigned long n_bits, unsigned long *read_map,
2483 unsigned long *write_map, unsigned long *except_map,
2484 double time_to_wait, u32 * bits_set)
2485{
Florin Coras99368312018-08-02 10:45:44 -07002486 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002487 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002488 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002489
Florin Coras54693d22018-07-17 10:46:29 -07002490 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002491 {
Florin Coras54693d22018-07-17 10:46:29 -07002492 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002493 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002494
Florin Coras54693d22018-07-17 10:46:29 -07002495 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002496 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002497 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002498 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002499 else
2500 {
2501 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002502 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002503 }
2504 }
Florin Corase003a1b2019-06-05 10:47:16 -07002505 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002506
Florin Coras134a9962018-08-28 11:32:04 -07002507 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002508 {
Florin Coras134a9962018-08-28 11:32:04 -07002509 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002510 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002511 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2512 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002513 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002514 }
Florin Coras134a9962018-08-28 11:32:04 -07002515 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002516 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002517 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002518}
2519
Florin Coras99368312018-08-02 10:45:44 -07002520static int
Florin Coras294afe22019-01-07 17:49:17 -08002521vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2522 vcl_si_set * read_map, vcl_si_set * write_map,
2523 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002524 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002525{
Florin Coras14ed6df2019-03-06 21:13:42 -08002526 double wait = 0, start = 0;
2527
2528 if (!*bits_set)
2529 {
2530 wait = time_to_wait;
2531 start = clib_time_now (&wrk->clib_time);
2532 }
2533
2534 do
2535 {
2536 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2537 write_map, except_map, wait, bits_set);
2538 if (*bits_set)
2539 return *bits_set;
2540 if (wait == -1)
2541 continue;
2542
2543 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2544 }
2545 while (wait > 0);
2546
2547 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002548}
2549
2550static int
Florin Coras294afe22019-01-07 17:49:17 -08002551vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2552 vcl_si_set * read_map, vcl_si_set * write_map,
2553 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002554 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002555{
2556 vcl_mq_evt_conn_t *mqc;
2557 int __clib_unused n_read;
2558 int n_mq_evts, i;
2559 u64 buf;
2560
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002561 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2562 {
2563 vcl_api_retry_attach (wrk);
2564 return 0;
2565 }
2566
Florin Coras134a9962018-08-28 11:32:04 -07002567 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2568 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2569 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002570 for (i = 0; i < n_mq_evts; i++)
2571 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002572 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2573 {
2574 vcl_api_handle_disconnect (wrk);
2575 continue;
2576 }
2577
Florin Coras134a9962018-08-28 11:32:04 -07002578 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002579 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002580 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002581 except_map, 0, bits_set);
2582 }
2583
2584 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2585}
2586
Dave Wallace543852a2017-08-03 02:11:34 -04002587int
Florin Coras294afe22019-01-07 17:49:17 -08002588vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2589 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002590{
Florin Coras54693d22018-07-17 10:46:29 -07002591 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002592 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002593 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002594 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002595
Dave Wallace7876d392017-10-19 03:53:57 -04002596 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002597 {
Florin Coras134a9962018-08-28 11:32:04 -07002598 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002599 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002600 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2601 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002602 }
Dave Wallace7876d392017-10-19 03:53:57 -04002603 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002604 {
Florin Coras134a9962018-08-28 11:32:04 -07002605 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002606 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002607 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2608 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002609 }
Dave Wallace7876d392017-10-19 03:53:57 -04002610 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002611 {
Florin Coras134a9962018-08-28 11:32:04 -07002612 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002613 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002614 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2615 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002616 }
2617
Florin Coras54693d22018-07-17 10:46:29 -07002618 if (!n_bits)
2619 return 0;
2620
2621 if (!write_map)
2622 goto check_rd;
2623
Florin Coras096a1d52021-01-27 15:33:51 -08002624 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2625 {
2626 if (!(s = vcl_session_get (wrk, sid)))
2627 {
2628 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2629 bits_set++;
2630 continue;
2631 }
Florin Coras54693d22018-07-17 10:46:29 -07002632
Florin Coras096a1d52021-01-27 15:33:51 -08002633 if (vcl_session_write_ready (s))
2634 {
2635 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2636 bits_set++;
2637 }
2638 else
2639 {
2640 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2641 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2642 }
2643 }
Florin Coras54693d22018-07-17 10:46:29 -07002644
2645check_rd:
2646 if (!read_map)
2647 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002648
Florin Coras096a1d52021-01-27 15:33:51 -08002649 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2650 {
2651 if (!(s = vcl_session_get (wrk, sid)))
2652 {
2653 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2654 bits_set++;
2655 continue;
2656 }
Florin Coras54693d22018-07-17 10:46:29 -07002657
Florin Coras096a1d52021-01-27 15:33:51 -08002658 if (vcl_session_read_ready (s))
2659 {
2660 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2661 bits_set++;
2662 }
2663 }
Florin Coras54693d22018-07-17 10:46:29 -07002664
2665check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002666
Florin Coras86f04502018-09-12 16:08:01 -07002667 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2668 {
2669 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2670 read_map, write_map, except_map, &bits_set);
2671 }
2672 vec_reset_length (wrk->unhandled_evts_vector);
2673
Florin Coras99368312018-08-02 10:45:44 -07002674 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002675 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002676 time_to_wait, &bits_set);
2677 else
Florin Coras134a9962018-08-28 11:32:04 -07002678 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002679 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002680
Dave Wallace543852a2017-08-03 02:11:34 -04002681 return (bits_set);
2682}
2683
Dave Wallacef7f809c2017-10-03 01:48:42 -04002684static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002685vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002688 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002689 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690
Florin Corasc0737e92019-03-04 14:19:39 -08002691 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002692 return;
2693
Florin Coras6c3b2182020-10-19 18:36:48 -07002694 s = vcl_session_get_w_handle (wrk, vep_handle);
2695 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002696 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002697 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002698 goto done;
2699 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002700 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002701 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002702 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002703 goto done;
2704 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002705 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002706 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002707 "{\n"
2708 " is_vep = %u\n"
2709 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002710 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002711 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2712 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002713
Florin Coras7e5e62b2019-07-30 14:08:23 -07002714 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002716 s = vcl_session_get_w_handle (wrk, sh);
2717 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002718 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002719 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002720 goto done;
2721 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002722 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002723 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002724 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002725 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002726 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002727 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002728 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002729 goto done;
2730 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002731 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002732 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2733 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002734 sh, s->vep.vep_sh, vep_handle);
2735 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002736 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002737 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002738 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002739 " next_sh = 0x%x (%u)\n"
2740 " prev_sh = 0x%x (%u)\n"
2741 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002742 " ev.events = 0x%x\n"
2743 " ev.data.u64 = 0x%llx\n"
2744 " et_mask = 0x%x\n"
2745 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002746 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002747 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2748 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002749 }
2750 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751
2752done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002753 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002754}
2755
2756int
2757vppcom_epoll_create (void)
2758{
Florin Coras134a9962018-08-28 11:32:04 -07002759 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002760 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002761
Florin Coras134a9962018-08-28 11:32:04 -07002762 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002763
Florin Coras6c3b2182020-10-19 18:36:48 -07002764 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002765 vep_session->vep.vep_sh = ~0;
2766 vep_session->vep.next_sh = ~0;
2767 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002768 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002769
Florin Corasa7a1a222018-12-30 17:11:31 -08002770 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2771 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002772
Florin Corasab2f6db2018-08-31 14:31:41 -07002773 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002774}
2775
2776int
Florin Coras134a9962018-08-28 11:32:04 -07002777vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002778 struct epoll_event *event)
2779{
Florin Coras134a9962018-08-28 11:32:04 -07002780 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002781 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002782 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002783 vcl_session_t *s;
2784 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002785
Florin Coras134a9962018-08-28 11:32:04 -07002786 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002787 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002788 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789 return VPPCOM_EINVAL;
2790 }
2791
Florin Coras134a9962018-08-28 11:32:04 -07002792 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002793 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002794 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002795 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002796 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002797 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002798 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002799 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002800 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002801 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802 }
2803
Florin Coras134a9962018-08-28 11:32:04 -07002804 ASSERT (vep_session->vep.vep_sh == ~0);
2805 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002806
Florin Coras17ec5772020-08-12 20:46:29 -07002807 s = vcl_session_get_w_handle (wrk, session_handle);
2808 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002809 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002810 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002811 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002812 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002813 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002814 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002815 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002816 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002817 }
2818
2819 switch (op)
2820 {
2821 case EPOLL_CTL_ADD:
2822 if (PREDICT_FALSE (!event))
2823 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002824 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002825 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002826 }
Florin Coras2645f682021-06-04 15:59:41 -07002827 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2828 {
2829 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2830 rv = VPPCOM_EEXIST;
2831 goto done;
2832 }
Florin Coras134a9962018-08-28 11:32:04 -07002833 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002834 {
Florin Coras7e12d942018-06-27 14:32:43 -07002835 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002836 next_session = vcl_session_get_w_handle (wrk,
2837 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002838 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002839 {
Florin Coras5e062572019-03-14 19:07:51 -07002840 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002841 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002842 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002843 }
Florin Coras134a9962018-08-28 11:32:04 -07002844 ASSERT (next_session->vep.prev_sh == vep_handle);
2845 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002846 }
Florin Coras17ec5772020-08-12 20:46:29 -07002847 s->vep.next_sh = vep_session->vep.next_sh;
2848 s->vep.prev_sh = vep_handle;
2849 s->vep.vep_sh = vep_handle;
2850 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002851 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002852 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002853 s->flags &= ~VCL_SESSION_F_IS_VEP;
2854 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002855 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002856
Florin Coras17ec5772020-08-12 20:46:29 -07002857 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2858 if (txf && (event->events & EPOLLOUT))
2859 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002860
Florin Coras6e3c1f82020-01-15 01:30:46 +00002861 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002862 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002863 {
2864 session_event_t e = { 0 };
2865 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002866 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002867 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002868 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002869 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002870 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002871 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002872 {
2873 session_event_t e = { 0 };
2874 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002875 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002876 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002877 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2878 add_evt = 1;
2879 }
2880 if (!add_evt && vcl_session_is_closing (s))
2881 {
2882 session_event_t e = { 0 };
2883 if (s->session_state == VCL_STATE_VPP_CLOSING)
2884 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2885 else
2886 e.event_type = SESSION_CTRL_EVT_RESET;
2887 e.session_index = s->session_index;
2888 e.postponed = 1;
2889 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002890 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002891 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2892 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002893 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002894 break;
2895
2896 case EPOLL_CTL_MOD:
2897 if (PREDICT_FALSE (!event))
2898 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002899 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002900 rv = VPPCOM_EINVAL;
2901 goto done;
2902 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002903 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002904 {
Florin Coras5e062572019-03-14 19:07:51 -07002905 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002906 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002907 goto done;
2908 }
Florin Coras17ec5772020-08-12 20:46:29 -07002909 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002910 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002911 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002912 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002913 rv = VPPCOM_EINVAL;
2914 goto done;
2915 }
hanlin475c9d72019-12-26 11:44:28 +08002916
Florin Coras2645f682021-06-04 15:59:41 -07002917 /* Generate EPOLLOUT if session write ready nd event was not on */
2918 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2919 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002920 {
2921 session_event_t e = { 0 };
2922 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002923 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002924 vec_add1 (wrk->unhandled_evts_vector, e);
2925 }
Florin Coras2645f682021-06-04 15:59:41 -07002926 /* Generate EPOLLIN if session read ready and event was not on */
2927 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2928 (vcl_session_read_ready (s) > 0))
2929 {
2930 session_event_t e = { 0 };
2931 e.event_type = SESSION_IO_EVT_RX;
2932 e.session_index = s->session_index;
2933 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002934 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002935 }
Florin Coras17ec5772020-08-12 20:46:29 -07002936 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2937 s->vep.ev = *event;
2938 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002939 if (txf)
2940 {
2941 if (event->events & EPOLLOUT)
2942 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2943 else
2944 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2945 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002946 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2947 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002948 break;
2949
2950 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002951 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002952 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002953 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002954 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002955 goto done;
2956 }
Florin Coras17ec5772020-08-12 20:46:29 -07002957 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002958 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002959 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002960 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002961 rv = VPPCOM_EINVAL;
2962 goto done;
2963 }
2964
Florin Coras17ec5772020-08-12 20:46:29 -07002965 if (s->vep.prev_sh == vep_handle)
2966 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002967 else
2968 {
Florin Coras7e12d942018-06-27 14:32:43 -07002969 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002970 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002971 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002972 {
Florin Coras5e062572019-03-14 19:07:51 -07002973 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002974 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002975 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002976 }
Florin Coras134a9962018-08-28 11:32:04 -07002977 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002978 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002979 }
Florin Coras17ec5772020-08-12 20:46:29 -07002980 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002981 {
Florin Coras7e12d942018-06-27 14:32:43 -07002982 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002983 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002984 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002985 {
Florin Coras5e062572019-03-14 19:07:51 -07002986 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002987 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002988 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002989 }
Florin Coras134a9962018-08-28 11:32:04 -07002990 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002991 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002992 }
2993
Florin Corasfa3884f2021-06-22 20:04:46 -07002994 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2995 vcl_epoll_lt_del (wrk, s);
2996
Florin Coras17ec5772020-08-12 20:46:29 -07002997 memset (&s->vep, 0, sizeof (s->vep));
2998 s->vep.next_sh = ~0;
2999 s->vep.prev_sh = ~0;
3000 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003001 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07003002 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08003003
Florin Corasf1ddeeb2021-06-10 08:08:53 -07003004 if (vcl_session_is_open (s))
3005 {
3006 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
3007 if (txf)
3008 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
3009 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08003010
Florin Coras5e062572019-03-14 19:07:51 -07003011 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003012 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003013 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003014 break;
3015
3016 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003017 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003018 rv = VPPCOM_EINVAL;
3019 }
3020
Florin Coras134a9962018-08-28 11:32:04 -07003021 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003022
3023done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003024 return rv;
3025}
3026
Florin Coras86f04502018-09-12 16:08:01 -07003027static inline void
3028vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3029 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003030{
3031 session_disconnected_msg_t *disconnected_msg;
3032 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003033 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003034 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003035 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003036 u8 add_event = 0;
3037
3038 switch (e->event_type)
3039 {
Florin Coras653e43f2019-03-04 10:56:23 -08003040 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003041 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003042 s = vcl_session_get (wrk, sid);
3043 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003044 break;
Florin Corasb242d312020-10-26 15:35:40 -07003045 vcl_fifo_rx_evt_valid_or_break (s);
3046 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003047 if (!(EPOLLIN & s->vep.ev.events) ||
3048 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3049 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003050 break;
3051 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003052 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003053 session_evt_data = s->vep.ev.data.u64;
3054 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003055 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003056 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003057 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003058 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003059 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003060 break;
Florin Corasb242d312020-10-26 15:35:40 -07003061 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003062 if (!(EPOLLOUT & session_events))
3063 break;
3064 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003065 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003066 session_evt_data = s->vep.ev.data.u64;
3067 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3068 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003069 break;
Florin Coras86f04502018-09-12 16:08:01 -07003070 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003071 if (!e->postponed)
3072 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3073 else
3074 s = vcl_session_get (wrk, e->session_index);
3075 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003076 break;
Florin Corasb242d312020-10-26 15:35:40 -07003077 session_events = s->vep.ev.events;
3078 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003079 if (!(EPOLLIN & session_events) ||
3080 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003081 break;
Florin Coras86f04502018-09-12 16:08:01 -07003082 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003083 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003084 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003085 break;
3086 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003087 if (!e->postponed)
3088 {
3089 connected_msg = (session_connected_msg_t *) e->data;
3090 sid = vcl_session_connected_handler (wrk, connected_msg);
3091 }
3092 else
3093 sid = e->session_index;
3094 s = vcl_session_get (wrk, sid);
3095 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003096 break;
Florin Corasb242d312020-10-26 15:35:40 -07003097 session_events = s->vep.ev.events;
3098 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003099 if (!(EPOLLOUT & session_events))
3100 break;
3101 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003102 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003103 session_evt_data = s->vep.ev.data.u64;
3104 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003105 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003106 break;
3107 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003108 if (!e->postponed)
3109 {
3110 disconnected_msg = (session_disconnected_msg_t *) e->data;
3111 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3112 }
3113 else
3114 {
3115 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003116 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003117 }
3118 if (vcl_session_is_closed (s) ||
3119 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003120 {
Florin Coras74254b52021-12-08 11:03:08 -08003121 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003122 vcl_session_free (wrk, s);
3123 break;
3124 }
Florin Corasb242d312020-10-26 15:35:40 -07003125 sid = s->session_index;
3126 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003127 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003128 if (EPOLLRDHUP & session_events)
3129 {
3130 /* If app can distinguish between RDHUP and HUP,
3131 * we make finer control */
3132 events[*num_ev].events = EPOLLRDHUP;
3133 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3134 {
3135 events[*num_ev].events |= EPOLLHUP;
3136 }
3137 }
3138 else
3139 {
3140 events[*num_ev].events = EPOLLHUP;
3141 }
Florin Corasb242d312020-10-26 15:35:40 -07003142 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003143
Florin Coras86f04502018-09-12 16:08:01 -07003144 break;
3145 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003146 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003147 {
3148 sid =
3149 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3150 s = vcl_session_get (wrk, sid);
3151 }
Florin Coras87f76002021-06-28 19:13:29 -07003152 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003153 {
3154 sid = e->session_index;
3155 s = vcl_session_get (wrk, sid);
3156 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3157 }
Florin Coras87f76002021-06-28 19:13:29 -07003158 if (vcl_session_is_closed (s) ||
3159 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003160 {
Florin Coras74254b52021-12-08 11:03:08 -08003161 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003162 vcl_session_free (wrk, s);
3163 break;
3164 }
Florin Corasb242d312020-10-26 15:35:40 -07003165 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003166 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003167 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3168 if ((EPOLLRDHUP & session_events) &&
3169 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3170 {
Yacan Liuab157702022-09-26 16:41:32 +08003171 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003172 }
3173 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3174 {
3175 events[*num_ev].events |= EPOLLIN;
3176 }
Florin Corasb242d312020-10-26 15:35:40 -07003177 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003178 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003179 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3180 vcl_session_unlisten_reply_handler (wrk, e->data);
3181 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003182 case SESSION_CTRL_EVT_MIGRATED:
3183 vcl_session_migrated_handler (wrk, e->data);
3184 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003185 case SESSION_CTRL_EVT_CLEANUP:
3186 vcl_session_cleanup_handler (wrk, e->data);
3187 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003188 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3189 vcl_session_req_worker_update_handler (wrk, e->data);
3190 break;
3191 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3192 vcl_session_worker_update_reply_handler (wrk, e->data);
3193 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003194 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3195 vcl_session_app_add_segment_handler (wrk, e->data);
3196 break;
3197 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3198 vcl_session_app_del_segment_handler (wrk, e->data);
3199 break;
hanlina3a48962020-07-13 11:09:15 +08003200 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003201 vcl_worker_rpc_handler (wrk, e->data);
3202 break;
Florin Coras86f04502018-09-12 16:08:01 -07003203 default:
3204 VDBG (0, "unhandled: %u", e->event_type);
3205 break;
3206 }
3207
3208 if (add_event)
3209 {
3210 events[*num_ev].data.u64 = session_evt_data;
3211 if (EPOLLONESHOT & session_events)
3212 {
Florin Corasb242d312020-10-26 15:35:40 -07003213 s = vcl_session_get (wrk, sid);
3214 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003215 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003216 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003217 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003218 s = vcl_session_get (wrk, sid);
3219 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3220 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003221 }
Florin Coras86f04502018-09-12 16:08:01 -07003222 *num_ev += 1;
3223 }
3224}
3225
3226static int
3227vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3228 struct epoll_event *events, u32 maxevents,
3229 double wait_for_time, u32 * num_ev)
3230{
Florin Coras99368312018-08-02 10:45:44 -07003231 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003232 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003233 int i;
3234
Florin Coras539663c2018-09-28 14:59:37 -07003235 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3236 goto handle_dequeued;
3237
Florin Coras54693d22018-07-17 10:46:29 -07003238 if (svm_msg_q_is_empty (mq))
3239 {
3240 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003241 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003242 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003243 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003244 else
3245 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003246 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003247 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003248 }
3249 }
Florin Corase003a1b2019-06-05 10:47:16 -07003250 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003251 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003252
Florin Coras539663c2018-09-28 14:59:37 -07003253handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003254 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003255 {
Florin Coras134a9962018-08-28 11:32:04 -07003256 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003257 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003258 if (*num_ev < maxevents)
3259 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3260 else
3261 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003262 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003263 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003264 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003265 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003266 return *num_ev;
3267}
3268
Florin Coras99368312018-08-02 10:45:44 -07003269static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003270vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3271 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003272{
Florin Corasccdb8b82021-04-28 13:01:06 -07003273 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003274
3275 if (!n_evts)
3276 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003277 if (timeout_ms > 0)
3278 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003279 }
3280
3281 do
3282 {
3283 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003284 timeout_ms, &n_evts);
3285 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003286 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003287 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003288 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003289
3290 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003291}
3292
3293static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003294vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3295 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003296{
Florin Coras99368312018-08-02 10:45:44 -07003297 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003298 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003299 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003300 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003301 u64 buf;
3302
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003303 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3304 {
3305 vcl_api_retry_attach (wrk);
3306 return n_evts;
3307 }
3308
Florin Coras134a9962018-08-28 11:32:04 -07003309 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003310 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003311 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003312 if (timeout_ms > 0)
3313 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003314 }
3315
Florin Corasb13ba132021-01-27 16:05:24 -08003316 do
3317 {
3318 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003319 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003320 if (n_mq_evts < 0)
3321 {
3322 VDBG (0, "epoll_wait error %u", errno);
3323 return n_evts;
3324 }
3325
3326 for (i = 0; i < n_mq_evts; i++)
3327 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003328 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3329 {
3330 /* api socket was closed */
3331 vcl_api_handle_disconnect (wrk);
3332 continue;
3333 }
3334
Florin Corasb13ba132021-01-27 16:05:24 -08003335 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3336 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3337 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3338 &n_evts);
3339 }
3340
Florin Corasccdb8b82021-04-28 13:01:06 -07003341 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003342 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003343 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003344 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003345
3346 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003347}
3348
Florin Corasfe286f72021-06-04 10:07:55 -07003349static void
Florin Corasfe286f72021-06-04 10:07:55 -07003350vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3351 int maxevents, u32 *n_evts)
3352{
Florin Coras734268f2021-06-30 07:54:29 -07003353 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003354 vcl_session_t *s;
3355 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003356 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003357
Florin Corasfa3884f2021-06-22 20:04:46 -07003358 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003359 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003360 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003361
Florin Corasfa3884f2021-06-22 20:04:46 -07003362 next = wrk->ep_lt_current;
3363 do
Florin Corasfe286f72021-06-04 10:07:55 -07003364 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003365 s = vcl_session_get (wrk, next);
3366 next = s->vep.lt_next;
3367
3368 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003369 {
3370 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003371 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003372 evt_data = s->vep.ev.data.u64;
3373 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003374 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003375 {
3376 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003377 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3378 evt_data = s->vep.ev.data.u64;
3379 }
3380 if (!add_event && s->session_state > VCL_STATE_READY)
3381 {
3382 add_event = 1;
3383 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003384 evt_data = s->vep.ev.data.u64;
3385 }
3386 if (add_event)
3387 {
3388 events[*n_evts].data.u64 = evt_data;
3389 *n_evts += 1;
3390 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003391 if (EPOLLONESHOT & s->vep.ev.events)
3392 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003393 if (*n_evts == maxevents)
3394 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003395 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003396 break;
3397 }
3398 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003399 else
3400 {
3401 vcl_epoll_lt_del (wrk, s);
3402 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3403 break;
3404 }
Florin Corasfe286f72021-06-04 10:07:55 -07003405 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003406 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003407}
3408
Dave Wallacef7f809c2017-10-03 01:48:42 -04003409int
Florin Coras134a9962018-08-28 11:32:04 -07003410vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003411 int maxevents, double wait_for_time)
3412{
Florin Coras134a9962018-08-28 11:32:04 -07003413 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003414 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003415 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003416 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003417
3418 if (PREDICT_FALSE (maxevents <= 0))
3419 {
Florin Coras5e062572019-03-14 19:07:51 -07003420 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003421 return VPPCOM_EINVAL;
3422 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003423
Florin Coras134a9962018-08-28 11:32:04 -07003424 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003425 if (!vep_session)
3426 return VPPCOM_EBADFD;
3427
Florin Coras6c3b2182020-10-19 18:36:48 -07003428 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003429 {
Florin Coras5e062572019-03-14 19:07:51 -07003430 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003431 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003432 }
Florin Coras54693d22018-07-17 10:46:29 -07003433
Florin Coras86f04502018-09-12 16:08:01 -07003434 if (vec_len (wrk->unhandled_evts_vector))
3435 {
3436 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3437 {
3438 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3439 events, &n_evts);
3440 if (n_evts == maxevents)
3441 {
Florin Corase003a1b2019-06-05 10:47:16 -07003442 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3443 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003444 }
3445 }
Florin Corase003a1b2019-06-05 10:47:16 -07003446 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003447 }
liuyacancba87102021-09-10 15:14:05 +08003448
3449 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3450 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3451
wanghanlin8919fec2021-03-18 20:00:41 +08003452 /* Request to only drain unhandled */
3453 if ((int) wait_for_time == -2)
3454 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003455
Florin Coras86f04502018-09-12 16:08:01 -07003456
Florin Corasfe286f72021-06-04 10:07:55 -07003457 if (vcm->cfg.use_mq_eventfd)
3458 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3459 wait_for_time);
3460 else
3461 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3462 wait_for_time);
3463
Florin Corasfe286f72021-06-04 10:07:55 -07003464 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003465}
3466
Dave Wallace35830af2017-10-09 01:43:42 -04003467int
Florin Coras134a9962018-08-28 11:32:04 -07003468vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003469 void *buffer, uint32_t * buflen)
3470{
Florin Coras134a9962018-08-28 11:32:04 -07003471 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003472 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003473 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003474 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003475 vcl_session_t *session;
3476 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003477
Florin Coras134a9962018-08-28 11:32:04 -07003478 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003479 if (!session)
3480 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003481
Dave Wallace35830af2017-10-09 01:43:42 -04003482 switch (op)
3483 {
3484 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003485 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003486 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3487 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003488 break;
3489
Dave Wallace227867f2017-11-13 21:21:53 -05003490 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003491 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003492 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3493 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003494 break;
3495
3496 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003497 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003498 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003499 *flags =
3500 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003501 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003502 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003503 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003504 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3505 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003506 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003507 }
3508 else
3509 rv = VPPCOM_EINVAL;
3510 break;
3511
3512 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003513 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003514 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003515 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003516 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003517 else
Florin Corasac422d62020-10-19 20:51:36 -07003518 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003519
Florin Coras5e062572019-03-14 19:07:51 -07003520 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3521 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003522 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003523 }
3524 else
3525 rv = VPPCOM_EINVAL;
3526 break;
3527
3528 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003529 if (PREDICT_TRUE (buffer && buflen &&
3530 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003531 {
Florin Coras7e12d942018-06-27 14:32:43 -07003532 ep->is_ip4 = session->transport.is_ip4;
3533 ep->port = session->transport.rmt_port;
3534 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003535 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3536 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003537 else
Dave Barach178cf492018-11-13 16:34:13 -05003538 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3539 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003540 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003541 VDBG (1,
3542 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3543 "addr = %U, port %u",
3544 session_handle, ep->is_ip4, vcl_format_ip46_address,
3545 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003546 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3547 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003548 }
3549 else
3550 rv = VPPCOM_EINVAL;
3551 break;
3552
3553 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003554 if (PREDICT_TRUE (buffer && buflen &&
3555 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003556 {
Florin Coras7e12d942018-06-27 14:32:43 -07003557 ep->is_ip4 = session->transport.is_ip4;
3558 ep->port = session->transport.lcl_port;
3559 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003560 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3561 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003562 else
Dave Barach178cf492018-11-13 16:34:13 -05003563 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3564 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003565 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003566 VDBG (1,
3567 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3568 " port %d",
3569 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003570 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003571 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3572 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003573 }
3574 else
3575 rv = VPPCOM_EINVAL;
3576 break;
Stevenb5a11602017-10-11 09:59:30 -07003577
Florin Corasef7cbf62019-10-17 09:56:27 -07003578 case VPPCOM_ATTR_SET_LCL_ADDR:
3579 if (PREDICT_TRUE (buffer && buflen &&
3580 (*buflen >= sizeof (*ep)) && ep->ip))
3581 {
3582 session->transport.is_ip4 = ep->is_ip4;
3583 session->transport.lcl_port = ep->port;
3584 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3585 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003586 VDBG (1,
3587 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3588 " port %d",
3589 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003590 &session->transport.lcl_ip,
3591 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3592 clib_net_to_host_u16 (ep->port));
3593 }
3594 else
3595 rv = VPPCOM_EINVAL;
3596 break;
3597
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 case VPPCOM_ATTR_GET_LIBC_EPFD:
3599 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003600 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003601 break;
3602
3603 case VPPCOM_ATTR_SET_LIBC_EPFD:
3604 if (PREDICT_TRUE (buffer && buflen &&
3605 (*buflen == sizeof (session->libc_epfd))))
3606 {
3607 session->libc_epfd = *(int *) buffer;
3608 *buflen = sizeof (session->libc_epfd);
3609
Florin Coras5e062572019-03-14 19:07:51 -07003610 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3611 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003612 }
3613 else
3614 rv = VPPCOM_EINVAL;
3615 break;
3616
3617 case VPPCOM_ATTR_GET_PROTOCOL:
3618 if (buffer && buflen && (*buflen >= sizeof (int)))
3619 {
Florin Coras7e12d942018-06-27 14:32:43 -07003620 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003621 *buflen = sizeof (int);
3622
Florin Coras5e062572019-03-14 19:07:51 -07003623 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3624 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003625 }
3626 else
3627 rv = VPPCOM_EINVAL;
3628 break;
3629
3630 case VPPCOM_ATTR_GET_LISTEN:
3631 if (buffer && buflen && (*buflen >= sizeof (int)))
3632 {
Florin Corasac422d62020-10-19 20:51:36 -07003633 *(int *) buffer = vcl_session_has_attr (session,
3634 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003635 *buflen = sizeof (int);
3636
Florin Coras5e062572019-03-14 19:07:51 -07003637 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3638 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003639 }
3640 else
3641 rv = VPPCOM_EINVAL;
3642 break;
3643
3644 case VPPCOM_ATTR_GET_ERROR:
3645 if (buffer && buflen && (*buflen >= sizeof (int)))
3646 {
3647 *(int *) buffer = 0;
3648 *buflen = sizeof (int);
3649
Florin Coras5e062572019-03-14 19:07:51 -07003650 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3651 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003652 }
3653 else
3654 rv = VPPCOM_EINVAL;
3655 break;
3656
3657 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3658 if (buffer && buflen && (*buflen >= sizeof (u32)))
3659 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003660
3661 /* VPP-TBD */
3662 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003663 session->tx_fifo ?
3664 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003665 vcm->cfg.tx_fifo_size);
3666 *buflen = sizeof (u32);
3667
Florin Coras5e062572019-03-14 19:07:51 -07003668 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3669 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3670 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003671 }
3672 else
3673 rv = VPPCOM_EINVAL;
3674 break;
3675
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003676 case VPPCOM_ATTR_SET_DSCP:
3677 if (buffer && buflen && (*buflen >= sizeof (u8)))
3678 {
3679 session->dscp = *(u8 *) buffer;
3680
3681 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3682 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3683 }
3684 else
3685 rv = VPPCOM_EINVAL;
3686 break;
3687
Dave Wallace048b1d62018-01-03 22:24:41 -05003688 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3689 if (buffer && buflen && (*buflen == sizeof (u32)))
3690 {
3691 /* VPP-TBD */
3692 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003693 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3694 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3695 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003696 }
3697 else
3698 rv = VPPCOM_EINVAL;
3699 break;
3700
3701 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3702 if (buffer && buflen && (*buflen >= sizeof (u32)))
3703 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003704
3705 /* VPP-TBD */
3706 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003707 session->rx_fifo ?
3708 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003709 vcm->cfg.rx_fifo_size);
3710 *buflen = sizeof (u32);
3711
Florin Coras5e062572019-03-14 19:07:51 -07003712 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3713 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003714 }
3715 else
3716 rv = VPPCOM_EINVAL;
3717 break;
3718
3719 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3720 if (buffer && buflen && (*buflen == sizeof (u32)))
3721 {
3722 /* VPP-TBD */
3723 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003724 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3725 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3726 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003727 }
3728 else
3729 rv = VPPCOM_EINVAL;
3730 break;
3731
3732 case VPPCOM_ATTR_GET_REUSEADDR:
3733 if (buffer && buflen && (*buflen >= sizeof (int)))
3734 {
3735 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003736 *(int *) buffer = vcl_session_has_attr (session,
3737 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003738 *buflen = sizeof (int);
3739
Florin Coras5e062572019-03-14 19:07:51 -07003740 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3741 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003742 }
3743 else
3744 rv = VPPCOM_EINVAL;
3745 break;
3746
Stevenb5a11602017-10-11 09:59:30 -07003747 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003748 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003749 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003750 {
3751 /* VPP-TBD */
3752 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003753 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003754 else
Florin Corasac422d62020-10-19 20:51:36 -07003755 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003756
Florin Coras5e062572019-03-14 19:07:51 -07003757 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003758 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003759 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003760 }
3761 else
3762 rv = VPPCOM_EINVAL;
3763 break;
3764
3765 case VPPCOM_ATTR_GET_REUSEPORT:
3766 if (buffer && buflen && (*buflen >= sizeof (int)))
3767 {
3768 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003769 *(int *) buffer = vcl_session_has_attr (session,
3770 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003771 *buflen = sizeof (int);
3772
Florin Coras5e062572019-03-14 19:07:51 -07003773 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3774 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003775 }
3776 else
3777 rv = VPPCOM_EINVAL;
3778 break;
3779
3780 case VPPCOM_ATTR_SET_REUSEPORT:
3781 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003782 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003783 {
3784 /* VPP-TBD */
3785 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003786 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003787 else
Florin Corasac422d62020-10-19 20:51:36 -07003788 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003789
Florin Coras5e062572019-03-14 19:07:51 -07003790 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003791 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003792 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003793 }
3794 else
3795 rv = VPPCOM_EINVAL;
3796 break;
3797
3798 case VPPCOM_ATTR_GET_BROADCAST:
3799 if (buffer && buflen && (*buflen >= sizeof (int)))
3800 {
3801 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003802 *(int *) buffer = vcl_session_has_attr (session,
3803 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003804 *buflen = sizeof (int);
3805
Florin Coras5e062572019-03-14 19:07:51 -07003806 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3807 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003808 }
3809 else
3810 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003811 break;
3812
3813 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003814 if (buffer && buflen && (*buflen == sizeof (int)))
3815 {
3816 /* VPP-TBD */
3817 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003818 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 else
Florin Corasac422d62020-10-19 20:51:36 -07003820 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003821
Florin Coras5e062572019-03-14 19:07:51 -07003822 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003823 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003824 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003825 }
3826 else
3827 rv = VPPCOM_EINVAL;
3828 break;
3829
3830 case VPPCOM_ATTR_GET_V6ONLY:
3831 if (buffer && buflen && (*buflen >= sizeof (int)))
3832 {
3833 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003834 *(int *) buffer = vcl_session_has_attr (session,
3835 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003836 *buflen = sizeof (int);
3837
Florin Coras5e062572019-03-14 19:07:51 -07003838 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3839 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003840 }
3841 else
3842 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003843 break;
3844
3845 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003846 if (buffer && buflen && (*buflen == sizeof (int)))
3847 {
3848 /* VPP-TBD */
3849 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003850 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003851 else
Florin Corasac422d62020-10-19 20:51:36 -07003852 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003853
Florin Coras5e062572019-03-14 19:07:51 -07003854 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003855 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003856 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003857 }
3858 else
3859 rv = VPPCOM_EINVAL;
3860 break;
3861
3862 case VPPCOM_ATTR_GET_KEEPALIVE:
3863 if (buffer && buflen && (*buflen >= sizeof (int)))
3864 {
3865 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003866 *(int *) buffer = vcl_session_has_attr (session,
3867 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003868 *buflen = sizeof (int);
3869
Florin Coras5e062572019-03-14 19:07:51 -07003870 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3871 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003872 }
3873 else
3874 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003875 break;
Stevenbccd3392017-10-12 20:42:21 -07003876
3877 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003878 if (buffer && buflen && (*buflen == sizeof (int)))
3879 {
3880 /* VPP-TBD */
3881 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003882 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003883 else
Florin Corasac422d62020-10-19 20:51:36 -07003884 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003885
Florin Coras5e062572019-03-14 19:07:51 -07003886 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003887 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003888 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003889 }
3890 else
3891 rv = VPPCOM_EINVAL;
3892 break;
3893
3894 case VPPCOM_ATTR_GET_TCP_NODELAY:
3895 if (buffer && buflen && (*buflen >= sizeof (int)))
3896 {
3897 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003898 *(int *) buffer = vcl_session_has_attr (session,
3899 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003900 *buflen = sizeof (int);
3901
Florin Coras5e062572019-03-14 19:07:51 -07003902 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3903 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003904 }
3905 else
3906 rv = VPPCOM_EINVAL;
3907 break;
3908
3909 case VPPCOM_ATTR_SET_TCP_NODELAY:
3910 if (buffer && buflen && (*buflen == sizeof (int)))
3911 {
3912 /* VPP-TBD */
3913 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003914 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003915 else
Florin Corasac422d62020-10-19 20:51:36 -07003916 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003917
Florin Coras5e062572019-03-14 19:07:51 -07003918 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003919 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003920 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003921 }
3922 else
3923 rv = VPPCOM_EINVAL;
3924 break;
3925
3926 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3927 if (buffer && buflen && (*buflen >= sizeof (int)))
3928 {
3929 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003930 *(int *) buffer = vcl_session_has_attr (session,
3931 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003932 *buflen = sizeof (int);
3933
Florin Coras5e062572019-03-14 19:07:51 -07003934 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3935 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003936 }
3937 else
3938 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003939 break;
3940
3941 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003942 if (buffer && buflen && (*buflen == sizeof (int)))
3943 {
3944 /* VPP-TBD */
3945 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003946 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003947 else
Florin Corasac422d62020-10-19 20:51:36 -07003948 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003949
Florin Coras5e062572019-03-14 19:07:51 -07003950 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003951 vcl_session_has_attr (session,
3952 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003953 }
3954 else
3955 rv = VPPCOM_EINVAL;
3956 break;
3957
3958 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3959 if (buffer && buflen && (*buflen >= sizeof (int)))
3960 {
3961 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003962 *(int *) buffer = vcl_session_has_attr (session,
3963 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003964 *buflen = sizeof (int);
3965
Florin Coras5e062572019-03-14 19:07:51 -07003966 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3967 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003968 }
3969 else
3970 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003971 break;
3972
3973 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003974 if (buffer && buflen && (*buflen == sizeof (int)))
3975 {
3976 /* VPP-TBD */
3977 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003978 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003979 else
Florin Corasac422d62020-10-19 20:51:36 -07003980 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003981
Florin Coras5e062572019-03-14 19:07:51 -07003982 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003983 vcl_session_has_attr (session,
3984 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003985 }
3986 else
3987 rv = VPPCOM_EINVAL;
3988 break;
3989
3990 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003991 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003992 {
Florin Coras04ae8272021-04-12 19:55:37 -07003993 rv = VPPCOM_EINVAL;
3994 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003995 }
Florin Coras04ae8272021-04-12 19:55:37 -07003996
3997 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3998 tea.mss = *(u32 *) buffer;
3999 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
4000 rv = VPPCOM_ENOPROTOOPT;
4001
4002 if (!rv)
4003 {
4004 *(u32 *) buffer = tea.mss;
4005 *buflen = sizeof (int);
4006 }
4007
4008 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4009 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004010 break;
4011
4012 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004013 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004014 {
Florin Coras04ae8272021-04-12 19:55:37 -07004015 rv = VPPCOM_EINVAL;
4016 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004017 }
Florin Coras04ae8272021-04-12 19:55:37 -07004018
4019 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4020 tea.mss = *(u32 *) buffer;
4021 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4022 rv = VPPCOM_ENOPROTOOPT;
4023
4024 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4025 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004026 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004027
Florin Coras1e966172020-05-16 18:18:14 +00004028 case VPPCOM_ATTR_SET_CONNECTED:
4029 session->flags |= VCL_SESSION_F_CONNECTED;
4030 break;
4031
Florin Corasa5a9efd2021-01-05 17:03:29 -08004032 case VPPCOM_ATTR_SET_CKPAIR:
4033 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4034 !vcl_session_has_crypto (session))
4035 {
4036 rv = VPPCOM_EINVAL;
4037 break;
4038 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004039 if (!session->ext_config)
4040 {
Florin Coras87f63892021-05-01 16:01:40 -07004041 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4042 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004043 }
4044 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4045 {
4046 rv = VPPCOM_EINVAL;
4047 break;
4048 }
4049
4050 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004051 break;
4052
Florin Coras6a6555a2021-01-28 11:39:27 -08004053 case VPPCOM_ATTR_SET_VRF:
4054 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4055 {
4056 rv = VPPCOM_EINVAL;
4057 break;
4058 }
4059 session->vrf = *(u32 *) buffer;
4060 break;
4061
4062 case VPPCOM_ATTR_GET_VRF:
4063 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4064 {
4065 rv = VPPCOM_EINVAL;
4066 break;
4067 }
4068 *(u32 *) buffer = session->vrf;
4069 *buflen = sizeof (u32);
4070 break;
4071
wanghanlin0674f852021-02-22 10:38:36 +08004072 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004073 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004074 {
Florin Corasd77325c2021-02-23 12:03:03 -08004075 rv = VPPCOM_EINVAL;
4076 break;
wanghanlin0674f852021-02-22 10:38:36 +08004077 }
Florin Corasd77325c2021-02-23 12:03:03 -08004078
4079 if (session->transport.is_ip4)
4080 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004081 else
Florin Corasd77325c2021-02-23 12:03:03 -08004082 *(int *) buffer = AF_INET6;
4083 *buflen = sizeof (int);
4084
wanghanlin0674f852021-02-22 10:38:36 +08004085 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4086 *buflen);
4087 break;
4088
Florin Coras87f63892021-05-01 16:01:40 -07004089 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4090 if (!(buffer && buflen && (*buflen > 0)))
4091 {
4092 rv = VPPCOM_EINVAL;
4093 break;
4094 }
4095 if (session->ext_config)
4096 {
4097 rv = VPPCOM_EINVAL;
4098 break;
4099 }
4100 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4101 *buflen + sizeof (u32));
4102 clib_memcpy (session->ext_config->data, buffer, *buflen);
4103 session->ext_config->len = *buflen;
4104 break;
4105
Dave Wallacee22aa742017-10-20 12:30:38 -04004106 default:
4107 rv = VPPCOM_EINVAL;
4108 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004109 }
4110
Dave Wallace35830af2017-10-09 01:43:42 -04004111 return rv;
4112}
4113
Stevenac1f96d2017-10-24 16:03:58 -07004114int
Florin Coras134a9962018-08-28 11:32:04 -07004115vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004116 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4117{
Florin Coras134a9962018-08-28 11:32:04 -07004118 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004119 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004120 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004121
4122 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004123 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004124 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004125 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004126 else
4127 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004128 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004129 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004130 }
4131
Florin Coras0a1e1832020-03-29 18:54:04 +00004132 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004133 {
Florin Coras5da10c42020-04-01 04:31:21 +00004134 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004135 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004136 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4137 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004138 else
Dave Barach178cf492018-11-13 16:34:13 -05004139 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4140 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004141 ep->is_ip4 = session->transport.is_ip4;
4142 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004143 }
Florin Coras460dce62018-07-27 05:45:06 -07004144
Stevenac1f96d2017-10-24 16:03:58 -07004145 return rv;
4146}
4147
4148int
Florin Coras134a9962018-08-28 11:32:04 -07004149vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004150 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4151{
Florin Coras7a2abce2020-04-05 19:25:44 +00004152 vcl_worker_t *wrk = vcl_worker_get_current ();
4153 vcl_session_t *s;
4154
4155 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004156 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004157 return VPPCOM_EBADFD;
4158
Dave Wallace617dffa2017-10-26 14:47:06 -04004159 if (ep)
4160 {
Florin Coras5824cc52020-10-20 18:44:41 -07004161 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004162 return VPPCOM_EINVAL;
4163
liuyacanbc0c7542021-08-02 20:15:05 +08004164 s->transport.is_ip4 = ep->is_ip4;
4165 s->transport.rmt_port = ep->port;
4166 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4167
Florin Coras5da10c42020-04-01 04:31:21 +00004168 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004169 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004170 {
Florin Coras5824cc52020-10-20 18:44:41 -07004171 u32 session_index = s->session_index;
4172 f64 timeout = vcm->cfg.session_timeout;
4173 int rv;
4174
Florin Coras5da10c42020-04-01 04:31:21 +00004175 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004176 rv = vppcom_wait_for_session_state_change (session_index,
4177 VCL_STATE_READY,
4178 timeout);
4179 if (rv < 0)
4180 return rv;
4181 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004182 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004183 }
4184
4185 if (flags)
4186 {
4187 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004188 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004189 }
4190
Florin Coras7a2abce2020-04-05 19:25:44 +00004191 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4192 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004193}
4194
Dave Wallace048b1d62018-01-03 22:24:41 -05004195int
4196vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4197{
Florin Coras134a9962018-08-28 11:32:04 -07004198 vcl_worker_t *wrk = vcl_worker_get_current ();
4199 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004200 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004201 svm_msg_q_msg_t msg;
4202 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004203 int rv, num_ev = 0;
4204
Florin Coras5e062572019-03-14 19:07:51 -07004205 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004206
4207 if (!vp)
4208 return VPPCOM_EFAULT;
4209
4210 do
4211 {
Florin Coras7e12d942018-06-27 14:32:43 -07004212 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004213
Florin Coras6917b942018-11-13 22:44:54 -08004214 /* Dequeue all events and drop all unhandled io events */
4215 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4216 {
4217 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4218 vcl_handle_mq_event (wrk, e);
4219 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4220 }
4221 vec_reset_length (wrk->unhandled_evts_vector);
4222
Dave Wallace048b1d62018-01-03 22:24:41 -05004223 for (i = 0; i < n_sids; i++)
4224 {
Florin Coras7baeb712019-01-04 17:05:43 -08004225 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004226 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004227 {
4228 vp[i].revents = POLLHUP;
4229 num_ev++;
4230 continue;
4231 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004232
Florin Coras6917b942018-11-13 22:44:54 -08004233 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004234
4235 if (POLLIN & vp[i].events)
4236 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004237 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004238 if (rv > 0)
4239 {
Florin Coras6917b942018-11-13 22:44:54 -08004240 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004241 num_ev++;
4242 }
4243 else if (rv < 0)
4244 {
4245 switch (rv)
4246 {
4247 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004248 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004249 break;
4250
4251 default:
Florin Coras6917b942018-11-13 22:44:54 -08004252 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004253 break;
4254 }
4255 num_ev++;
4256 }
4257 }
4258
4259 if (POLLOUT & vp[i].events)
4260 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004261 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004262 if (rv > 0)
4263 {
Florin Coras6917b942018-11-13 22:44:54 -08004264 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004265 num_ev++;
4266 }
4267 else if (rv < 0)
4268 {
4269 switch (rv)
4270 {
4271 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004272 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004273 break;
4274
4275 default:
Florin Coras6917b942018-11-13 22:44:54 -08004276 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004277 break;
4278 }
4279 num_ev++;
4280 }
4281 }
4282
Dave Wallace7e607a72018-06-18 18:41:32 -04004283 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004284 {
Florin Coras6917b942018-11-13 22:44:54 -08004285 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004286 num_ev++;
4287 }
4288 }
4289 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004290 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004291 }
4292 while ((num_ev == 0) && keep_trying);
4293
Dave Wallace048b1d62018-01-03 22:24:41 -05004294 return num_ev;
4295}
4296
Florin Coras99368312018-08-02 10:45:44 -07004297int
4298vppcom_mq_epoll_fd (void)
4299{
Florin Coras134a9962018-08-28 11:32:04 -07004300 vcl_worker_t *wrk = vcl_worker_get_current ();
4301 return wrk->mqs_epfd;
4302}
4303
4304int
Florin Coras30e79c22019-01-02 19:31:22 -08004305vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004306{
4307 return session_handle & 0xFFFFFF;
4308}
4309
4310int
Florin Coras30e79c22019-01-02 19:31:22 -08004311vppcom_session_worker (vcl_session_handle_t session_handle)
4312{
4313 return session_handle >> 24;
4314}
4315
4316int
Florin Coras134a9962018-08-28 11:32:04 -07004317vppcom_worker_register (void)
4318{
Florin Coras47c40e22018-11-26 17:01:36 -08004319 if (!vcl_worker_alloc_and_init ())
4320 return VPPCOM_EEXIST;
4321
Florin Coras47c40e22018-11-26 17:01:36 -08004322 if (vcl_worker_register_with_vpp ())
4323 return VPPCOM_EEXIST;
4324
4325 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004326}
4327
Florin Coras369db832019-07-08 12:34:45 -07004328void
4329vppcom_worker_unregister (void)
4330{
4331 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4332 vcl_set_worker_index (~0);
4333}
4334
Pivo6017ff02020-05-04 17:57:33 +02004335void
4336vppcom_worker_index_set (int index)
4337{
4338 vcl_set_worker_index (index);
4339}
4340
Florin Corasdfe4cf42018-11-28 22:13:45 -08004341int
4342vppcom_worker_index (void)
4343{
4344 return vcl_get_worker_index ();
4345}
4346
Florin Corase0982e52019-01-25 13:19:56 -08004347int
4348vppcom_worker_mqs_epfd (void)
4349{
4350 vcl_worker_t *wrk = vcl_worker_get_current ();
4351 if (!vcm->cfg.use_mq_eventfd)
4352 return -1;
4353 return wrk->mqs_epfd;
4354}
4355
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004356int
4357vppcom_session_is_connectable_listener (uint32_t session_handle)
4358{
4359 vcl_session_t *session;
4360 vcl_worker_t *wrk = vcl_worker_get_current ();
4361 session = vcl_session_get_w_handle (wrk, session_handle);
4362 if (!session)
4363 return VPPCOM_EBADFD;
4364 return vcl_session_is_connectable_listener (wrk, session);
4365}
4366
4367int
4368vppcom_session_listener (uint32_t session_handle)
4369{
4370 vcl_worker_t *wrk = vcl_worker_get_current ();
4371 vcl_session_t *listen_session, *session;
4372 session = vcl_session_get_w_handle (wrk, session_handle);
4373 if (!session)
4374 return VPPCOM_EBADFD;
4375 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4376 return VPPCOM_EBADFD;
4377 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4378 if (!listen_session)
4379 return VPPCOM_EBADFD;
4380 return vcl_session_handle (listen_session);
4381}
4382
4383int
4384vppcom_session_n_accepted (uint32_t session_handle)
4385{
4386 vcl_worker_t *wrk = vcl_worker_get_current ();
4387 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4388 if (!session)
4389 return VPPCOM_EBADFD;
4390 return session->n_accepted_sessions;
4391}
4392
Florin Coras66ec4672020-06-15 07:59:40 -07004393const char *
4394vppcom_proto_str (vppcom_proto_t proto)
4395{
4396 char const *proto_str;
4397
4398 switch (proto)
4399 {
4400 case VPPCOM_PROTO_TCP:
4401 proto_str = "TCP";
4402 break;
4403 case VPPCOM_PROTO_UDP:
4404 proto_str = "UDP";
4405 break;
4406 case VPPCOM_PROTO_TLS:
4407 proto_str = "TLS";
4408 break;
4409 case VPPCOM_PROTO_QUIC:
4410 proto_str = "QUIC";
4411 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004412 case VPPCOM_PROTO_DTLS:
4413 proto_str = "DTLS";
4414 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004415 case VPPCOM_PROTO_SRTP:
4416 proto_str = "SRTP";
4417 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004418 default:
4419 proto_str = "UNKNOWN";
4420 break;
4421 }
4422 return proto_str;
4423}
4424
4425const char *
4426vppcom_retval_str (int retval)
4427{
4428 char const *st;
4429
4430 switch (retval)
4431 {
4432 case VPPCOM_OK:
4433 st = "VPPCOM_OK";
4434 break;
4435
4436 case VPPCOM_EAGAIN:
4437 st = "VPPCOM_EAGAIN";
4438 break;
4439
4440 case VPPCOM_EFAULT:
4441 st = "VPPCOM_EFAULT";
4442 break;
4443
4444 case VPPCOM_ENOMEM:
4445 st = "VPPCOM_ENOMEM";
4446 break;
4447
4448 case VPPCOM_EINVAL:
4449 st = "VPPCOM_EINVAL";
4450 break;
4451
4452 case VPPCOM_EBADFD:
4453 st = "VPPCOM_EBADFD";
4454 break;
4455
4456 case VPPCOM_EAFNOSUPPORT:
4457 st = "VPPCOM_EAFNOSUPPORT";
4458 break;
4459
4460 case VPPCOM_ECONNABORTED:
4461 st = "VPPCOM_ECONNABORTED";
4462 break;
4463
4464 case VPPCOM_ECONNRESET:
4465 st = "VPPCOM_ECONNRESET";
4466 break;
4467
4468 case VPPCOM_ENOTCONN:
4469 st = "VPPCOM_ENOTCONN";
4470 break;
4471
4472 case VPPCOM_ECONNREFUSED:
4473 st = "VPPCOM_ECONNREFUSED";
4474 break;
4475
4476 case VPPCOM_ETIMEDOUT:
4477 st = "VPPCOM_ETIMEDOUT";
4478 break;
4479
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304480 case VPPCOM_EADDRINUSE:
4481 st = "VPPCOM_EADDRINUSE";
4482 break;
4483
Florin Coras66ec4672020-06-15 07:59:40 -07004484 default:
4485 st = "UNKNOWN_STATE";
4486 break;
4487 }
4488
4489 return st;
4490}
4491
Florin Corasa5a9efd2021-01-05 17:03:29 -08004492int
4493vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4494{
4495 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004496 return vcl_sapi_add_cert_key_pair (ckpair);
4497 else
4498 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004499}
4500
4501int
4502vppcom_del_cert_key_pair (uint32_t ckpair_index)
4503{
4504 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004505 return vcl_sapi_del_cert_key_pair (ckpair_index);
4506 else
4507 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004508}
4509
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304510int
4511vppcom_session_get_error (uint32_t session_handle)
4512{
4513 vcl_worker_t *wrk = vcl_worker_get_current ();
4514 vcl_session_t *session = 0;
4515
4516 session = vcl_session_get_w_handle (wrk, session_handle);
4517 if (!session)
4518 return VPPCOM_EBADFD;
4519
4520 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4521 {
4522 VWRN ("epoll session %u! will not have connect", session->session_index);
4523 return VPPCOM_EBADFD;
4524 }
4525
4526 if (session->vpp_error == SESSION_E_PORTINUSE)
4527 return VPPCOM_EADDRINUSE;
4528 else if (session->vpp_error == SESSION_E_REFUSED)
4529 return VPPCOM_ECONNREFUSED;
4530 else if (session->vpp_error != SESSION_E_NONE)
4531 return VPPCOM_EFAULT;
4532 else
4533 return VPPCOM_OK;
4534}
4535
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004536int
4537vppcom_worker_is_detached (void)
4538{
4539 vcl_worker_t *wrk = vcl_worker_get_current ();
4540
4541 if (!vcm->cfg.use_mq_eventfd)
4542 return VPPCOM_ENOTSUP;
4543
4544 return wrk->api_client_handle == ~0;
4545}
4546
Dave Wallacee22aa742017-10-20 12:30:38 -04004547/*
4548 * fd.io coding-style-patch-verification: ON
4549 *
4550 * Local Variables:
4551 * eval: (c-set-style "gnu")
4552 * End:
4553 */