blob: 0ba9423ecf053b81fd82936b001d7ef31c04e155 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
Florin Coras5398dfb2021-01-25 20:31:27 -080028 u32 n_msgs = 0, sz, len;
Florin Coras30e79c22019-01-02 19:31:22 -080029
Florin Coras5398dfb2021-01-25 20:31:27 -080030 while ((sz = svm_msg_q_size (mq)))
Florin Coras30e79c22019-01-02 19:31:22 -080031 {
Florin Coras5398dfb2021-01-25 20:31:27 -080032 len = vec_len (wrk->mq_msg_vector);
33 vec_validate (wrk->mq_msg_vector, len + sz - 1);
34 svm_msg_q_sub_raw_batch (mq, wrk->mq_msg_vector + len, sz);
35 n_msgs += sz;
Florin Coras30e79c22019-01-02 19:31:22 -080036 }
37 return n_msgs;
38}
39
Dave Wallace543852a2017-08-03 02:11:34 -040040
Florin Coras697faea2018-06-27 17:10:49 -070041
Florin Coras458089b2019-08-21 16:20:44 -070042static void
Florin Coras4ac25842021-04-19 17:34:54 -070043vcl_msg_add_ext_config (vcl_session_t *s, uword *offset)
44{
45 svm_fifo_chunk_t *c;
46
47 c = vcl_segment_alloc_chunk (vcl_vpp_worker_segment_handle (0),
48 0 /* one slice only */, s->ext_config->len,
49 offset);
50 if (c)
51 clib_memcpy_fast (c->data, s->ext_config, s->ext_config->len);
52}
53
Florin Coras32881932023-02-06 13:30:13 -080054void
55vcl_send_session_listen (vcl_worker_t *wrk, vcl_session_t *s)
Florin Coras458089b2019-08-21 16:20:44 -070056{
57 app_session_evt_t _app_evt, *app_evt = &_app_evt;
58 session_listen_msg_t *mp;
59 svm_msg_q_t *mq;
60
61 mq = vcl_worker_ctrl_mq (wrk);
62 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
63 mp = (session_listen_msg_t *) app_evt->evt->data;
64 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070065 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070066 mp->context = s->session_index;
67 mp->wrk_index = wrk->vpp_wrk_index;
68 mp->is_ip4 = s->transport.is_ip4;
69 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
70 mp->port = s->transport.lcl_port;
71 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -080072 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +000073 if (s->flags & VCL_SESSION_F_CONNECTED)
74 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -070075 if (s->ext_config)
76 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -070077 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -070078 if (s->ext_config)
79 {
80 clib_mem_free (s->ext_config);
81 s->ext_config = 0;
82 }
Florin Coras32881932023-02-06 13:30:13 -080083 s->flags |= VCL_SESSION_F_PENDING_LISTEN;
Florin Coras458089b2019-08-21 16:20:44 -070084}
85
86static void
87vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
88{
89 app_session_evt_t _app_evt, *app_evt = &_app_evt;
90 session_connect_msg_t *mp;
91 svm_msg_q_t *mq;
92
93 mq = vcl_worker_ctrl_mq (wrk);
94 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
95 mp = (session_connect_msg_t *) app_evt->evt->data;
96 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070097 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070098 mp->context = s->session_index;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +000099 mp->dscp = s->dscp;
Florin Coras458089b2019-08-21 16:20:44 -0700100 mp->wrk_index = wrk->vpp_wrk_index;
101 mp->is_ip4 = s->transport.is_ip4;
102 mp->parent_handle = s->parent_handle;
103 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700104 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700105 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000106 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700107 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800108 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000109 if (s->flags & VCL_SESSION_F_CONNECTED)
110 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700111 if (s->ext_config)
112 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700113 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700114
115 if (s->ext_config)
116 {
117 clib_mem_free (s->ext_config);
118 s->ext_config = 0;
119 }
Florin Coras458089b2019-08-21 16:20:44 -0700120}
121
122void
123vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
124{
125 app_session_evt_t _app_evt, *app_evt = &_app_evt;
126 session_unlisten_msg_t *mp;
127 svm_msg_q_t *mq;
128
129 mq = vcl_worker_ctrl_mq (wrk);
130 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
131 mp = (session_unlisten_msg_t *) app_evt->evt->data;
132 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700133 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700134 mp->wrk_index = wrk->vpp_wrk_index;
135 mp->handle = s->vpp_handle;
136 mp->context = wrk->wrk_index;
137 app_send_ctrl_evt_to_vpp (mq, app_evt);
138}
139
140static void
liuyacan534468e2021-05-09 03:50:40 +0000141vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
142{
143 app_session_evt_t _app_evt, *app_evt = &_app_evt;
144 session_shutdown_msg_t *mp;
145 svm_msg_q_t *mq;
146
147 /* Send to thread that owns the session */
148 mq = s->vpp_evt_q;
149 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
150 mp = (session_shutdown_msg_t *) app_evt->evt->data;
151 memset (mp, 0, sizeof (*mp));
152 mp->client_index = wrk->api_client_handle;
153 mp->handle = s->vpp_handle;
154 app_send_ctrl_evt_to_vpp (mq, app_evt);
155}
156
157static void
Florin Coras458089b2019-08-21 16:20:44 -0700158vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
159{
160 app_session_evt_t _app_evt, *app_evt = &_app_evt;
161 session_disconnect_msg_t *mp;
162 svm_msg_q_t *mq;
163
164 /* Send to thread that owns the session */
165 mq = s->vpp_evt_q;
166 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
167 mp = (session_disconnect_msg_t *) app_evt->evt->data;
168 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700169 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700170 mp->handle = s->vpp_handle;
171 app_send_ctrl_evt_to_vpp (mq, app_evt);
172}
173
174static void
175vcl_send_app_detach (vcl_worker_t * wrk)
176{
177 app_session_evt_t _app_evt, *app_evt = &_app_evt;
178 session_app_detach_msg_t *mp;
179 svm_msg_q_t *mq;
180
181 mq = vcl_worker_ctrl_mq (wrk);
182 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
183 mp = (session_app_detach_msg_t *) app_evt->evt->data;
184 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700185 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700186 app_send_ctrl_evt_to_vpp (mq, app_evt);
187}
Florin Coras697faea2018-06-27 17:10:49 -0700188
Florin Coras54693d22018-07-17 10:46:29 -0700189static void
190vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
191 session_handle_t handle, int retval)
192{
193 app_session_evt_t _app_evt, *app_evt = &_app_evt;
194 session_accepted_reply_msg_t *rmp;
195 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
196 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
197 rmp->handle = handle;
198 rmp->context = context;
199 rmp->retval = retval;
200 app_send_ctrl_evt_to_vpp (mq, app_evt);
201}
202
Florin Coras99368312018-08-02 10:45:44 -0700203static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800204vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
205 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700206{
207 app_session_evt_t _app_evt, *app_evt = &_app_evt;
208 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800209 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700210 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
211 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800212 rmp->handle = s->vpp_handle;
213 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700214 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800215 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700216}
217
Florin Corasc9fbd662018-08-24 12:59:56 -0700218static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800219vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
220 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700221{
222 app_session_evt_t _app_evt, *app_evt = &_app_evt;
223 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800224 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
225 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700226 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800227 rmp->handle = s->vpp_handle;
228 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700229 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800230 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700231}
232
Florin Coras30e79c22019-01-02 19:31:22 -0800233void
234vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
235 u32 wrk_index)
236{
237 app_session_evt_t _app_evt, *app_evt = &_app_evt;
238 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800239
Florin Coras52dd29f2020-11-18 19:02:17 -0800240 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
241 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800242 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700243 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800244 mp->handle = s->vpp_handle;
245 mp->req_wrk_index = wrk->vpp_wrk_index;
246 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800247 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800248}
249
hanlina3a48962020-07-13 11:09:15 +0800250int
Florin Coras40c07ce2020-07-16 20:46:17 -0700251vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
252{
253 app_session_evt_t _app_evt, *app_evt = &_app_evt;
254 session_app_wrk_rpc_msg_t *mp;
255 vcl_worker_t *dst_wrk, *wrk;
256 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800257 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700258
259 if (data_len > sizeof (mp->data))
260 goto done;
261
262 clib_spinlock_lock (&vcm->workers_lock);
263
264 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
265 if (!dst_wrk)
266 goto done;
267
268 wrk = vcl_worker_get_current ();
269 mq = vcl_worker_ctrl_mq (wrk);
270 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
271 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700272 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700273 mp->wrk_index = dst_wrk->vpp_wrk_index;
274 clib_memcpy (mp->data, data, data_len);
275 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800276 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700277
278done:
279 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800280 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700281}
282
Florin Coras04ae8272021-04-12 19:55:37 -0700283int
284vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
285 transport_endpt_attr_t *attr)
286{
287 app_session_evt_t _app_evt, *app_evt = &_app_evt;
288 session_transport_attr_msg_t *mp;
289 svm_msg_q_t *mq;
290 f64 timeout;
291
292 ASSERT (!wrk->session_attr_op);
Liangxing Wang22112772022-05-13 04:24:19 +0000293 mq = s->vpp_evt_q;
294 if (PREDICT_FALSE (!mq))
295 {
296 /* FIXME: attribute should be stored and sent once session is
297 * bound/connected to vpp */
298 return 0;
299 }
300
Florin Coras04ae8272021-04-12 19:55:37 -0700301 wrk->session_attr_op = 1;
302 wrk->session_attr_op_rv = -1;
303
Florin Coras04ae8272021-04-12 19:55:37 -0700304 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
305 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
306 memset (mp, 0, sizeof (*mp));
307 mp->client_index = wrk->api_client_handle;
308 mp->handle = s->vpp_handle;
309 mp->is_get = is_get;
310 mp->attr = *attr;
311 app_send_ctrl_evt_to_vpp (mq, app_evt);
312
313 timeout = clib_time_now (&wrk->clib_time) + 1;
314
315 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
316 vcl_flush_mq_events ();
317
318 if (!wrk->session_attr_op_rv && is_get)
319 *attr = wrk->session_attr_rv;
320
321 wrk->session_attr_op = 0;
322
323 return wrk->session_attr_op_rv;
324}
325
Florin Coras54693d22018-07-17 10:46:29 -0700326static u32
Florin Coras00cca802019-06-06 09:38:44 -0700327vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
328 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700329{
330 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700331 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700332
Florin Coras134a9962018-08-28 11:32:04 -0700333 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700334
Florin Coras00cca802019-06-06 09:38:44 -0700335 listen_session = vcl_session_get (wrk, ls_index);
336 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700337 {
Florin Coras00cca802019-06-06 09:38:44 -0700338 VDBG (0, "ERROR: listener handle %lu does not match session %u",
339 mp->listener_handle, ls_index);
340 goto error;
341 }
342
Florin Corasf6e284b2021-07-21 18:17:20 -0700343 if (vcl_segment_attach_session (
344 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
345 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700346 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800347 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
348 session->session_index, mp->handle);
Florin Coras00cca802019-06-06 09:38:44 -0700349 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700350 }
351
Florin Coras54693d22018-07-17 10:46:29 -0700352 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700353 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700354 session->transport.rmt_port = mp->rmt.port;
355 session->transport.is_ip4 = mp->rmt.is_ip4;
356 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500357 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700358
Florin Coras134a9962018-08-28 11:32:04 -0700359 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800360 session->transport.lcl_port = mp->lcl.port;
361 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700362 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200363 session->is_dgram = vcl_proto_is_dgram (session->session_type);
364 session->listener_index = listen_session->session_index;
365 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700366
Florin Coras54693d22018-07-17 10:46:29 -0700367 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
368
Florin Coras00cca802019-06-06 09:38:44 -0700369 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
370 session->vpp_handle, 0);
371
Florin Coras134a9962018-08-28 11:32:04 -0700372 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700373
374error:
Florin Corasb4624182020-12-11 13:58:12 -0800375 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
376 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700377 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
378 VNET_API_ERROR_INVALID_ARGUMENT);
379 vcl_session_free (wrk, session);
380 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700381}
382
383static u32
Florin Coras134a9962018-08-28 11:32:04 -0700384vcl_session_connected_handler (vcl_worker_t * wrk,
385 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700386{
Florin Coras99368312018-08-02 10:45:44 -0700387 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800388 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700389
390 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700391 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800392 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700393 {
Florin Coras8d42c752021-11-29 23:26:19 -0800394 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
395 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800396 /* Should not happen but if it does, force vpp session cleanup */
397 vcl_session_t tmp_session = {
398 .vpp_handle = mp->handle,
399 .vpp_evt_q = 0,
400 };
401 vcl_segment_attach_session (
402 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
403 mp->vpp_event_queue_address, mp->mq_index, 0, session);
404 if (tmp_session.vpp_evt_q)
405 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700406 return VCL_INVALID_SESSION_INDEX;
407 }
Florin Coras8d42c752021-11-29 23:26:19 -0800408
Florin Coras54693d22018-07-17 10:46:29 -0700409 if (mp->retval)
410 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800411 VDBG (0, "session %u: connect failed! %U", session_index,
Florin Coras8d42c752021-11-29 23:26:19 -0800412 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700413 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800414 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +0530415 session->vpp_error = mp->retval;
Florin Coras070453d2018-08-24 17:04:27 -0700416 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700417 }
418
Florin Corasdbc9c592019-09-25 16:37:43 -0700419 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800420
Florin Coras8d42c752021-11-29 23:26:19 -0800421 /* Add to lookup table. Even if something fails, session cannot be
422 * cleaned up prior to notifying vpp and going through the cleanup
423 * "procedure" see @ref vcl_session_cleanup_handler */
424 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
425
Florin Corasf6e284b2021-07-21 18:17:20 -0700426 if (vcl_segment_attach_session (
427 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
428 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800429 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800430 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
431 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800432 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700433 vcl_send_session_disconnect (wrk, session);
434 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800435 }
436
Florin Coras653e43f2019-03-04 10:56:23 -0800437 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700438 {
Florin Corasc547e912020-12-08 17:50:45 -0800439 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700440 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
441 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800442 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800443 VDBG (0, "session %u [0x%llx]: failed to attach ct fifos",
444 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800445 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700446 vcl_send_session_disconnect (wrk, session);
447 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800448 }
Florin Coras99368312018-08-02 10:45:44 -0700449 }
Florin Coras54693d22018-07-17 10:46:29 -0700450
Florin Coras09d18c22019-04-24 11:10:02 -0700451 session->transport.is_ip4 = mp->lcl.is_ip4;
452 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500453 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700454 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700455
456 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700457 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700458 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700459 vcl_send_session_disconnect (wrk, session);
460 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700461 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700462
Florin Coras5ffb9642021-12-03 17:24:13 -0800463 VDBG (0, "session %u [0x%llx] connected local: %U:%u remote %U:%u",
464 session->session_index, session->vpp_handle, vcl_format_ip46_address,
465 &session->transport.lcl_ip,
466 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
467 clib_net_to_host_u16 (session->transport.lcl_port),
468 vcl_format_ip46_address, &session->transport.rmt_ip,
469 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
470 clib_net_to_host_u16 (session->transport.rmt_port));
Florin Coras070453d2018-08-24 17:04:27 -0700471
Florin Coras54693d22018-07-17 10:46:29 -0700472 return session_index;
473}
474
Florin Coras3c7d4f92018-12-14 11:28:43 -0800475static int
476vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
477{
478 vcl_session_msg_t *accepted_msg;
479 int i;
480
481 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
482 {
483 accepted_msg = &session->accept_evts_fifo[i];
484 if (accepted_msg->accepted_msg.handle == handle)
485 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800486 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800487 return 1;
488 }
489 }
490 return 0;
491}
492
Florin Corasc9fbd662018-08-24 12:59:56 -0700493static u32
Florin Coras134a9962018-08-28 11:32:04 -0700494vcl_session_reset_handler (vcl_worker_t * wrk,
495 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700496{
497 vcl_session_t *session;
498 u32 sid;
499
Florin Coras134a9962018-08-28 11:32:04 -0700500 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
501 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700502 if (!session)
503 {
504 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
505 return VCL_INVALID_SESSION_INDEX;
506 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800507
508 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700509 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800510 {
511
512 if (!vcl_flag_accepted_session (session, reset_msg->handle,
513 VCL_ACCEPTED_F_RESET))
514 VDBG (0, "session was not accepted!");
515 return VCL_INVALID_SESSION_INDEX;
516 }
517
Florin Corasc127d5a2020-10-14 16:35:58 -0700518 if (session->session_state != VCL_STATE_CLOSED)
519 session->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +0800520
521 session->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Coras5ffb9642021-12-03 17:24:13 -0800522 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700523 return sid;
524}
525
Florin Coras60116992018-08-27 09:52:18 -0700526static u32
Florin Coras134a9962018-08-28 11:32:04 -0700527vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700528{
529 vcl_session_t *session;
530 u32 sid = mp->context;
531
Florin Coras134a9962018-08-28 11:32:04 -0700532 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700533 if (mp->retval)
534 {
Florin Coras5e062572019-03-14 19:07:51 -0700535 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700536 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700537 if (session)
538 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700539 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700540 session->vpp_handle = mp->handle;
541 return sid;
542 }
543 else
544 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800545 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
546 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700547 return VCL_INVALID_SESSION_INDEX;
548 }
549 }
550
551 session->vpp_handle = mp->handle;
552 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500553 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
554 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700555 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700556 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700557 session->session_state = VCL_STATE_LISTEN;
Florin Coras32881932023-02-06 13:30:13 -0800558 session->flags &= ~VCL_SESSION_F_PENDING_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800559
Florin Coras6e3c1f82020-01-15 01:30:46 +0000560 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700561 {
Florin Corasc547e912020-12-08 17:50:45 -0800562 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700563 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
564 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800565 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800566 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
567 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800568 session->session_state = VCL_STATE_DETACHED;
569 return VCL_INVALID_SESSION_INDEX;
570 }
Florin Coras60116992018-08-27 09:52:18 -0700571 }
572
Florin Coras05ecfcc2018-12-12 18:19:39 -0800573 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700574 return sid;
575}
576
Florin Corasdfae9f92019-02-20 19:48:31 -0800577static void
578vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
579{
580 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
581 vcl_session_t *s;
582
583 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000584 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800585 {
586 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
587 return;
588 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700589 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000590 {
591 /* Connected udp listener */
592 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700593 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000594 return;
595
596 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
597 return;
598 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800599
600 if (mp->retval)
601 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700602 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800603
604 if (mp->context != wrk->wrk_index)
605 VDBG (0, "wrong context");
606
607 vcl_session_table_del_vpp_handle (wrk, mp->handle);
608 vcl_session_free (wrk, s);
609}
610
Florin Coras68b7e582020-01-21 18:33:23 -0800611static void
612vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
613{
614 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
615 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800616 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800617
618 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
619 if (!s)
620 {
621 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
622 return;
623 }
624
Florin Coras1bb74942021-02-10 15:26:37 -0800625 /* Only validate if a value is provided */
626 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800627 {
Florin Coras1bb74942021-02-10 15:26:37 -0800628 fs_index = vcl_segment_table_lookup (mp->segment_handle);
629 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
630 {
631 VDBG (0, "segment %lx for session %u is not mounted!",
632 mp->segment_handle, s->session_index);
633 s->session_state = VCL_STATE_DETACHED;
634 return;
635 }
Florin Corasc547e912020-12-08 17:50:45 -0800636 }
637
Florin Coras57660d92020-04-04 22:45:34 +0000638 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800639
640 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
641 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800642
Florin Coras68b7e582020-01-21 18:33:23 -0800643 vcl_session_table_del_vpp_handle (wrk, mp->handle);
644 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
645
646 /* Generate new tx event if we have outstanding data */
647 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800648 app_send_io_evt_to_vpp (s->vpp_evt_q,
649 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800650 SESSION_IO_EVT_TX, SVM_Q_WAIT);
651
Florin Coras57660d92020-04-04 22:45:34 +0000652 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800653 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800654}
655
Florin Coras3c7d4f92018-12-14 11:28:43 -0800656static vcl_session_t *
657vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
658{
659 vcl_session_msg_t *vcl_msg;
660 vcl_session_t *session;
661
662 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
663 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800664 VWRN ("session overlap handle %lu state %u!", msg->handle,
665 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800666
667 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
668 if (!session)
669 {
670 VERR ("couldn't find listen session: listener handle %llx",
671 msg->listener_handle);
672 return 0;
673 }
674
675 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000676 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800677 vcl_msg->accepted_msg = *msg;
678 /* Session handle points to listener until fully accepted by app */
679 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
680
681 return session;
682}
683
684static vcl_session_t *
685vcl_session_disconnected_handler (vcl_worker_t * wrk,
686 session_disconnected_msg_t * msg)
687{
688 vcl_session_t *session;
689
690 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
691 if (!session)
692 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800693 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800694 return 0;
695 }
696
Florin Corasadcfb152020-02-12 08:50:29 +0000697 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700698 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000699 return 0;
700
Florin Coras3c7d4f92018-12-14 11:28:43 -0800701 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700702 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800703 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800704 if (!vcl_flag_accepted_session (session, msg->handle,
705 VCL_ACCEPTED_F_CLOSED))
706 VDBG (0, "session was not accepted!");
707 return 0;
708 }
709
Florin Corasadcfb152020-02-12 08:50:29 +0000710 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700711 if (session->session_state != VCL_STATE_DISCONNECT)
712 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000713
Florin Coras3c7d4f92018-12-14 11:28:43 -0800714 return session;
715}
716
liuyacan534468e2021-05-09 03:50:40 +0000717int
liuyacan55c952e2021-06-13 14:54:55 +0800718vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000719{
720 vcl_worker_t *wrk = vcl_worker_get_current ();
721 vcl_session_t *session;
722 vcl_session_state_t state;
723 u64 vpp_handle;
724
725 session = vcl_session_get_w_handle (wrk, session_handle);
726 if (PREDICT_FALSE (!session))
727 return VPPCOM_EBADFD;
728
729 vpp_handle = session->vpp_handle;
730 state = session->session_state;
731
732 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800733 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000734
735 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
736 {
737 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
738 return VPPCOM_EBADFD;
739 }
740
liuyacan55c952e2021-06-13 14:54:55 +0800741 if (how == SHUT_RD || how == SHUT_RDWR)
742 {
743 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
744 if (how == SHUT_RD)
745 return VPPCOM_OK;
746 }
747 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
748
liuyacan534468e2021-05-09 03:50:40 +0000749 if (PREDICT_TRUE (state == VCL_STATE_READY))
750 {
751 VDBG (1, "session %u [0x%llx]: sending shutdown...",
752 session->session_index, vpp_handle);
753
754 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000755 }
756
757 return VPPCOM_OK;
758}
759
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700760static int
761vppcom_session_disconnect (u32 session_handle)
762{
763 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700764 vcl_session_t *session, *listen_session;
765 vcl_session_state_t state;
766 u64 vpp_handle;
767
768 session = vcl_session_get_w_handle (wrk, session_handle);
769 if (!session)
770 return VPPCOM_EBADFD;
771
772 vpp_handle = session->vpp_handle;
773 state = session->session_state;
774
Florin Coras5ffb9642021-12-03 17:24:13 -0800775 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
776 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700777
778 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
779 {
780 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
781 return VPPCOM_EBADFD;
782 }
783
784 if (state == VCL_STATE_VPP_CLOSING)
785 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800786 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700787 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
788 session->session_index, vpp_handle);
789 }
790 else
791 {
792 /* Session doesn't have an event queue yet. Probably a non-blocking
793 * connect. Wait for the reply */
794 if (PREDICT_FALSE (!session->vpp_evt_q))
795 return VPPCOM_OK;
796
Florin Coras5ffb9642021-12-03 17:24:13 -0800797 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700798 session->session_index, vpp_handle);
799 vcl_send_session_disconnect (wrk, session);
800 }
801
802 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
803 {
804 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700805 if (listen_session)
806 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700807 }
808
809 return VPPCOM_OK;
810}
811
Florin Coras30e79c22019-01-02 19:31:22 -0800812static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700813vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
814{
815 session_cleanup_msg_t *msg;
816 vcl_session_t *session;
817
818 msg = (session_cleanup_msg_t *) data;
819 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
820 if (!session)
821 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800822 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700823 return;
824 }
825
Florin Coras36d49392020-04-24 23:00:11 +0000826 if (msg->type == SESSION_CLEANUP_TRANSPORT)
827 {
828 /* Transport was cleaned up before we confirmed close. Probably the
829 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700830 * Confirm close to make sure everything is cleaned up.
831 * Move to undetermined state to ensure that the session is not
832 * removed before both vpp and the app cleanup.
833 * - If the app closes first, the session is moved to CLOSED state
834 * and the session cleanup notification from vpp removes the
835 * session.
836 * - If vpp cleans up the session first, the session is moved to
837 * DETACHED state lower and subsequently the close from the app
838 * frees the session
839 */
840 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700841 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700842 vppcom_session_disconnect (vcl_session_handle (session));
843 session->session_state = VCL_STATE_UPDATED;
844 }
845 else if (session->session_state == VCL_STATE_DISCONNECT)
846 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800847 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700848 session->session_state = VCL_STATE_UPDATED;
849 }
Florin Coras36d49392020-04-24 23:00:11 +0000850 return;
851 }
852
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800853 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700854 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800855
856 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700857 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000858 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800859 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700860 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000861 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
862 return;
863 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800864
865 /* Session probably tracked with epoll, disconnect not yet handled and
866 * 1) both transport and session cleanup completed 2) app closed. Wait
867 * until message is drained to free the session.
868 * See @ref vcl_handle_mq_event */
869 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
870 {
871 session->flags |= VCL_SESSION_F_PENDING_FREE;
872 return;
873 }
874
Florin Coras9ace36d2019-10-28 13:14:17 -0700875 vcl_session_free (wrk, session);
876}
877
878static void
Florin Coras30e79c22019-01-02 19:31:22 -0800879vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
880{
881 session_req_worker_update_msg_t *msg;
882 vcl_session_t *s;
883
884 msg = (session_req_worker_update_msg_t *) data;
885 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
886 if (!s)
887 return;
888
889 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
890}
891
892static void
893vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
894{
895 session_worker_update_reply_msg_t *msg;
896 vcl_session_t *s;
897
898 msg = (session_worker_update_reply_msg_t *) data;
899 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
900 if (!s)
901 {
902 VDBG (0, "unknown handle 0x%llx", msg->handle);
903 return;
904 }
Florin Coras30e79c22019-01-02 19:31:22 -0800905
Florin Coras5f45e012019-01-23 09:21:30 -0800906 if (s->rx_fifo)
907 {
Florin Corasc547e912020-12-08 17:50:45 -0800908 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700909 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800910 {
911 VDBG (0, "failed to attach fifos for %u", s->session_index);
912 return;
913 }
Florin Coras5f45e012019-01-23 09:21:30 -0800914 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700915 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800916
Florin Coras30e79c22019-01-02 19:31:22 -0800917 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
918 s->vpp_handle, wrk->wrk_index);
919}
920
Florin Coras935ce752020-09-08 22:43:47 -0700921static int
922vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
923{
924
925 if (vcm->cfg.vpp_app_socket_api)
926 return vcl_sapi_recv_fds (wrk, fds, n_fds);
927
928 return vcl_bapi_recv_fds (wrk, fds, n_fds);
929}
930
Florin Corasc4c4cf52019-08-24 18:17:34 -0700931static void
932vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
933{
934 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
935 session_app_add_segment_msg_t *msg;
936 u64 segment_handle;
937 int fd = -1;
938
939 msg = (session_app_add_segment_msg_t *) data;
940
941 if (msg->fd_flags)
942 {
Florin Coras935ce752020-09-08 22:43:47 -0700943 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700944 seg_type = SSVM_SEGMENT_MEMFD;
945 }
946
947 segment_handle = msg->segment_handle;
948 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
949 {
950 clib_warning ("invalid segment handle");
951 return;
952 }
953
954 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
955 seg_type, fd))
956 {
957 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
958 return;
959 }
960
961 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
962 msg->segment_size);
963}
964
965static void
966vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
967{
968 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
969 vcl_segment_detach (msg->segment_handle);
970 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
971}
972
Florin Coras40c07ce2020-07-16 20:46:17 -0700973static void
974vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
975{
976 if (!vcm->wrk_rpc_fn)
977 return;
978
hanlina3a48962020-07-13 11:09:15 +0800979 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700980}
981
Florin Coras04ae8272021-04-12 19:55:37 -0700982static void
983vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
984{
985 session_transport_attr_reply_msg_t *mp;
986
987 if (!wrk->session_attr_op)
988 return;
989
990 mp = (session_transport_attr_reply_msg_t *) data;
991
992 wrk->session_attr_op_rv = mp->retval;
993 wrk->session_attr_op = 0;
994 wrk->session_attr_rv = mp->attr;
995}
996
Florin Coras86f04502018-09-12 16:08:01 -0700997static int
998vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700999{
Florin Coras54693d22018-07-17 10:46:29 -07001000 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001001 session_connected_msg_t *connected_msg;
1002 session_reset_msg_t *reset_msg;
1003 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001004 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001005 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001006
1007 switch (e->event_type)
1008 {
Florin Coras653e43f2019-03-04 10:56:23 -08001009 case SESSION_IO_EVT_RX:
1010 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001011 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001012 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001013 break;
Florin Coras86f04502018-09-12 16:08:01 -07001014 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001015 break;
Florin Corasb242d312020-10-26 15:35:40 -07001016 case SESSION_CTRL_EVT_BOUND:
1017 /* We can only wait for only one listen so not postponed */
1018 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1019 break;
Florin Coras54693d22018-07-17 10:46:29 -07001020 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001021 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1022 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1023 {
1024 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1025 *ecpy = *e;
1026 ecpy->postponed = 1;
1027 ecpy->session_index = s->session_index;
1028 }
Florin Coras54693d22018-07-17 10:46:29 -07001029 break;
1030 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001031 connected_msg = (session_connected_msg_t *) e->data;
1032 sid = vcl_session_connected_handler (wrk, connected_msg);
1033 if (!(s = vcl_session_get (wrk, sid)))
1034 break;
1035 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1036 {
1037 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1038 *ecpy = *e;
1039 ecpy->postponed = 1;
1040 ecpy->session_index = s->session_index;
1041 }
Florin Coras54693d22018-07-17 10:46:29 -07001042 break;
1043 case SESSION_CTRL_EVT_DISCONNECTED:
1044 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001045 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1046 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001047 if (s->session_state == VCL_STATE_CLOSED)
1048 break;
Florin Corasb242d312020-10-26 15:35:40 -07001049 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1050 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001051 s->session_state = VCL_STATE_VPP_CLOSING;
1052 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1053 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1054 *ecpy = *e;
1055 ecpy->postponed = 1;
1056 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001057 break;
1058 }
1059 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001060 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001061 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1062 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001063 break;
1064 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001065 reset_msg = (session_reset_msg_t *) e->data;
1066 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1067 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001068 if (s->session_state == VCL_STATE_CLOSED)
1069 break;
Florin Corasb242d312020-10-26 15:35:40 -07001070 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1071 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001072 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1073 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001074 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001075 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1076 *ecpy = *e;
1077 ecpy->postponed = 1;
1078 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001079 break;
1080 }
Florin Coras134a9962018-08-28 11:32:04 -07001081 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001082 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001083 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1084 vcl_session_unlisten_reply_handler (wrk, e->data);
1085 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001086 case SESSION_CTRL_EVT_MIGRATED:
1087 vcl_session_migrated_handler (wrk, e->data);
1088 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001089 case SESSION_CTRL_EVT_CLEANUP:
1090 vcl_session_cleanup_handler (wrk, e->data);
1091 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001092 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1093 vcl_session_req_worker_update_handler (wrk, e->data);
1094 break;
1095 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1096 vcl_session_worker_update_reply_handler (wrk, e->data);
1097 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001098 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1099 vcl_session_app_add_segment_handler (wrk, e->data);
1100 break;
1101 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1102 vcl_session_app_del_segment_handler (wrk, e->data);
1103 break;
hanlina3a48962020-07-13 11:09:15 +08001104 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001105 vcl_worker_rpc_handler (wrk, e->data);
1106 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001107 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1108 vcl_session_transport_attr_reply_handler (wrk, e->data);
1109 break;
Florin Coras54693d22018-07-17 10:46:29 -07001110 default:
1111 clib_warning ("unhandled %u", e->event_type);
1112 }
1113 return VPPCOM_OK;
1114}
1115
Florin Coras30e79c22019-01-02 19:31:22 -08001116static int
Florin Coras697faea2018-06-27 17:10:49 -07001117vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001118 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001119 f64 wait_for_time)
1120{
Florin Coras134a9962018-08-28 11:32:04 -07001121 vcl_worker_t *wrk = vcl_worker_get_current ();
1122 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001123 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001124 svm_msg_q_msg_t msg;
1125 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001126
Florin Coras697faea2018-06-27 17:10:49 -07001127 do
Dave Wallace543852a2017-08-03 02:11:34 -04001128 {
Florin Coras134a9962018-08-28 11:32:04 -07001129 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001130 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001131 {
Florin Coras070453d2018-08-24 17:04:27 -07001132 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001133 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001134 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001135 {
Florin Coras697faea2018-06-27 17:10:49 -07001136 return VPPCOM_OK;
1137 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001138 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001139 {
Florin Coras697faea2018-06-27 17:10:49 -07001140 return VPPCOM_ECONNREFUSED;
1141 }
Florin Coras54693d22018-07-17 10:46:29 -07001142
Florin Coras134a9962018-08-28 11:32:04 -07001143 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001144 {
1145 usleep (100);
1146 continue;
1147 }
Florin Coras134a9962018-08-28 11:32:04 -07001148 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001149 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001150 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001151 }
Florin Coras134a9962018-08-28 11:32:04 -07001152 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001153
Florin Coras05ecfcc2018-12-12 18:19:39 -08001154 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001155 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001156 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001157
Florin Coras697faea2018-06-27 17:10:49 -07001158 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001159}
1160
Florin Coras30e79c22019-01-02 19:31:22 -08001161static void
1162vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1163{
Florin Coras288eaab2019-02-03 15:26:14 -08001164 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001165 vcl_session_t *s;
1166 u32 *sip;
1167
1168 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1169 return;
1170
1171 vec_foreach (sip, wrk->pending_session_wrk_updates)
1172 {
1173 s = vcl_session_get (wrk, *sip);
1174 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1175 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001176 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1177 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001178 s->session_state = state;
1179 }
1180 vec_reset_length (wrk->pending_session_wrk_updates);
1181}
1182
Florin Corasf9240dc2019-01-15 08:03:17 -08001183void
Florin Coras5398dfb2021-01-25 20:31:27 -08001184vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001185{
Florin Coras30e79c22019-01-02 19:31:22 -08001186 svm_msg_q_msg_t *msg;
1187 session_event_t *e;
1188 svm_msg_q_t *mq;
1189 int i;
1190
1191 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001192 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001193
1194 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1195 {
1196 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1197 e = svm_msg_q_msg_data (mq, msg);
1198 vcl_handle_mq_event (wrk, e);
1199 svm_msg_q_free_msg (mq, msg);
1200 }
1201 vec_reset_length (wrk->mq_msg_vector);
1202 vcl_handle_pending_wrk_updates (wrk);
1203}
1204
Florin Coras5398dfb2021-01-25 20:31:27 -08001205void
1206vcl_flush_mq_events (void)
1207{
1208 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1209}
1210
Florin Coras697faea2018-06-27 17:10:49 -07001211static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001212vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001213{
Florin Coras134a9962018-08-28 11:32:04 -07001214 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001215 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001216 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001217 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001218
Florin Corasab2f6db2018-08-31 14:31:41 -07001219 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001220 if (!session)
1221 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001222
Florin Corasaaff5ee2019-07-08 13:00:00 -07001223 /* Flush pending accept events, if any */
1224 while (clib_fifo_elts (session->accept_evts_fifo))
1225 {
1226 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1227 accepted_msg = &evt->accepted_msg;
1228 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1229 vcl_send_session_accepted_reply (session->vpp_evt_q,
1230 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001231 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001232 }
1233 clib_fifo_free (session->accept_evts_fifo);
1234
Florin Coras458089b2019-08-21 16:20:44 -07001235 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001236
Florin Coras32881932023-02-06 13:30:13 -08001237 VDBG (0, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001238 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001239 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001240
1241 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001242 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001243
Florin Coras070453d2018-08-24 17:04:27 -07001244 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001245}
1246
Florin Coras940f78f2018-11-30 12:11:20 -08001247/**
1248 * Handle app exit
1249 *
1250 * Notify vpp of the disconnect and mark the worker as free. If we're the
1251 * last worker, do a full cleanup otherwise, since we're probably a forked
1252 * child, avoid syscalls as much as possible. We might've lost privileges.
1253 */
1254void
1255vppcom_app_exit (void)
1256{
1257 if (!pool_elts (vcm->workers))
1258 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001259 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1260 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001261 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001262}
1263
Florin Coras935ce752020-09-08 22:43:47 -07001264static int
1265vcl_api_attach (void)
1266{
1267 if (vcm->cfg.vpp_app_socket_api)
1268 return vcl_sapi_attach ();
1269
1270 return vcl_bapi_attach ();
1271}
1272
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001273int
1274vcl_is_first_reattach_to_execute ()
1275{
1276 if (vcm->reattach_count == 0)
1277 return 1;
1278
1279 return 0;
1280}
1281
1282void
1283vcl_set_reattach_counter ()
1284{
1285 ++vcm->reattach_count;
1286
1287 if (vcm->reattach_count == vec_len (vcm->workers))
1288 vcm->reattach_count = 0;
1289}
1290
1291/**
1292 * Reattach vcl to vpp after it has previously been disconnected.
1293 *
1294 * The logic should be:
1295 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1296 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1297 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1298 * from `vppcom_worker_register` since they were already allocated.
1299 */
1300
Florin Coras935ce752020-09-08 22:43:47 -07001301static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001302vcl_api_retry_attach (vcl_worker_t *wrk)
1303{
1304 vcl_session_t *s;
1305
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001306 clib_spinlock_lock (&vcm->workers_lock);
1307 if (vcl_is_first_reattach_to_execute ())
1308 {
1309 if (vcl_api_attach ())
1310 {
1311 clib_spinlock_unlock (&vcm->workers_lock);
1312 return;
1313 }
1314 vcl_set_reattach_counter ();
1315 clib_spinlock_unlock (&vcm->workers_lock);
1316 }
1317 else
1318 {
1319 vcl_set_reattach_counter ();
1320 clib_spinlock_unlock (&vcm->workers_lock);
1321 vcl_worker_register_with_vpp ();
1322 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001323
1324 /* Treat listeners as configuration that needs to be re-added to vpp */
1325 pool_foreach (s, wrk->sessions)
1326 {
1327 if (s->flags & VCL_SESSION_F_IS_VEP)
1328 continue;
1329 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1330 vppcom_session_listen (vcl_session_handle (s), 10);
1331 else
1332 VDBG (0, "internal error: unexpected state %d", s->session_state);
1333 }
1334}
1335
1336static void
1337vcl_api_handle_disconnect (vcl_worker_t *wrk)
1338{
1339 wrk->api_client_handle = ~0;
1340 vcl_worker_detach_sessions (wrk);
1341}
1342
1343static void
Florin Coras935ce752020-09-08 22:43:47 -07001344vcl_api_detach (vcl_worker_t * wrk)
1345{
Florin Corasd04ea442022-03-30 16:08:25 -07001346 if (wrk->api_client_handle == ~0)
1347 return;
1348
Florin Coras935ce752020-09-08 22:43:47 -07001349 vcl_send_app_detach (wrk);
1350
1351 if (vcm->cfg.vpp_app_socket_api)
1352 return vcl_sapi_detach (wrk);
1353
1354 return vcl_bapi_disconnect_from_vpp ();
1355}
1356
Dave Wallace543852a2017-08-03 02:11:34 -04001357/*
1358 * VPPCOM Public API functions
1359 */
1360int
Florin Coras66ec4672020-06-15 07:59:40 -07001361vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001362{
Dave Wallace543852a2017-08-03 02:11:34 -04001363 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001364 int rv;
1365
Florin Coras47c40e22018-11-26 17:01:36 -08001366 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001367 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001368 VDBG (1, "already initialized");
1369 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001370 }
1371
Florin Coras47c40e22018-11-26 17:01:36 -08001372 vcm->is_init = 1;
1373 vppcom_cfg (&vcm->cfg);
1374 vcl_cfg = &vcm->cfg;
1375
1376 vcm->main_cpu = pthread_self ();
1377 vcm->main_pid = getpid ();
1378 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001379 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1380 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001381 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1382 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001383 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001384 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001385 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001386
1387 /* Allocate default worker */
1388 vcl_worker_alloc_and_init ();
1389
Florin Coras935ce752020-09-08 22:43:47 -07001390 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001391 {
1392 vppcom_app_destroy ();
1393 return rv;
1394 }
Florin Coras47c40e22018-11-26 17:01:36 -08001395
1396 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001397 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001398
1399 return VPPCOM_OK;
1400}
1401
1402void
1403vppcom_app_destroy (void)
1404{
Florin Corasdc0ded72020-04-30 02:59:55 +00001405 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001406 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001407
Florin Coras940f78f2018-11-30 12:11:20 -08001408 if (!pool_elts (vcm->workers))
1409 return;
1410
Florin Coras0d427d82018-06-27 03:24:07 -07001411 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001412
Florin Corasdc0ded72020-04-30 02:59:55 +00001413 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001414
Florin Corasce815de2020-04-16 18:47:27 +00001415 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001416 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001417 if (current_wrk != wrk)
1418 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001419 }
Florin Corasce815de2020-04-16 18:47:27 +00001420 /* *INDENT-ON* */
1421
Florin Coras935ce752020-09-08 22:43:47 -07001422 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001423 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001424 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001425
Florin Coras0d427d82018-06-27 03:24:07 -07001426 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001427
1428 /*
1429 * Free the heap and fix vcm
1430 */
1431 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001432 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001433
1434 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001435 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001436}
1437
1438int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001439vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001440{
Florin Coras134a9962018-08-28 11:32:04 -07001441 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001442 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001443
Florin Coras134a9962018-08-28 11:32:04 -07001444 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001445
Florin Coras7e12d942018-06-27 14:32:43 -07001446 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001447 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001448 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001449 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301450 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001451
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001452 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001453 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001454
Florin Coras7e12d942018-06-27 14:32:43 -07001455 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1456 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001457
Florin Coras5e062572019-03-14 19:07:51 -07001458 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001459
Florin Coras134a9962018-08-28 11:32:04 -07001460 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001461}
1462
Florin Corasfe286f72021-06-04 10:07:55 -07001463static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001464vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001465{
Florin Corasfa3884f2021-06-22 20:04:46 -07001466 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001467
Florin Corasc2a14172023-02-28 21:13:50 -08001468 ASSERT (s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
1469
Florin Corasfa3884f2021-06-22 20:04:46 -07001470 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001471 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001472 wrk->ep_lt_current = s->session_index;
1473 s->vep.lt_next = s->session_index;
1474 s->vep.lt_prev = s->session_index;
1475 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001476 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001477
1478 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1479 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1480
1481 prev->vep.lt_next = s->session_index;
1482 s->vep.lt_prev = prev->session_index;
1483
1484 s->vep.lt_next = cur->session_index;
1485 cur->vep.lt_prev = s->session_index;
1486}
1487
1488static void
1489vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1490{
1491 vcl_session_t *prev, *next;
1492
Florin Corasc2a14172023-02-28 21:13:50 -08001493 ASSERT (s->vep.lt_next != VCL_INVALID_SESSION_INDEX);
1494
Florin Corasfa3884f2021-06-22 20:04:46 -07001495 if (s->vep.lt_next == s->session_index)
1496 {
1497 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1498 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001499 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfa3884f2021-06-22 20:04:46 -07001500 return;
1501 }
1502
1503 prev = vcl_session_get (wrk, s->vep.lt_prev);
1504 next = vcl_session_get (wrk, s->vep.lt_next);
1505
1506 prev->vep.lt_next = next->session_index;
1507 next->vep.lt_prev = prev->session_index;
1508
1509 if (s->session_index == wrk->ep_lt_current)
1510 wrk->ep_lt_current = s->vep.lt_next;
1511
1512 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001513 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001514}
1515
Dave Wallace543852a2017-08-03 02:11:34 -04001516int
Florin Corasc127d5a2020-10-14 16:35:58 -07001517vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001518 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001519{
Florin Coras134a9962018-08-28 11:32:04 -07001520 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001521
Florin Coras6c3b2182020-10-19 18:36:48 -07001522 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001523
Florin Coras6c3b2182020-10-19 18:36:48 -07001524 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001525 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001526 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001527 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001528 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001529 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001530 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001531 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001532 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1533 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001534 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001535 }
Florin Corase4306b62020-10-28 12:51:10 -07001536 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001537 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001538
Florin Coras6c3b2182020-10-19 18:36:48 -07001539 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001540 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001541 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001542 if (rv < 0)
1543 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001544 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1545 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001546 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001547
Florin Coras9ace36d2019-10-28 13:14:17 -07001548 if (!do_disconnect)
1549 {
1550 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001551 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001552 goto cleanup;
1553 }
1554
Florin Coras6c3b2182020-10-19 18:36:48 -07001555 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001556 {
1557 rv = vppcom_session_unbind (sh);
1558 if (PREDICT_FALSE (rv < 0))
1559 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001560 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001561 vppcom_retval_str (rv));
1562 return rv;
1563 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001564 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001565 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001566 {
1567 rv = vppcom_session_disconnect (sh);
1568 if (PREDICT_FALSE (rv < 0))
1569 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001570 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001571 rv, vppcom_retval_str (rv));
1572 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001573 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001574 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001575 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001576 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001577 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001578 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001579 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001580
1581 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1582 goto free_session;
1583
1584 /* Disconnect/reset messages pending but vpp transport and session
1585 * cleanups already done. Free only after messages drained. */
1586 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001587 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001588
Florin Corasc127d5a2020-10-14 16:35:58 -07001589 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001590
Florin Coras9ace36d2019-10-28 13:14:17 -07001591 /* Session is removed only after vpp confirms the disconnect */
1592 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001593
Florin Corasf9240dc2019-01-15 08:03:17 -08001594cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001595 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001596free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001597 vcl_session_free (wrk, s);
1598 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001599
Dave Wallace543852a2017-08-03 02:11:34 -04001600 return rv;
1601}
1602
1603int
Florin Corasf9240dc2019-01-15 08:03:17 -08001604vppcom_session_close (uint32_t session_handle)
1605{
1606 vcl_worker_t *wrk = vcl_worker_get_current ();
1607 vcl_session_t *session;
1608
1609 session = vcl_session_get_w_handle (wrk, session_handle);
1610 if (!session)
1611 return VPPCOM_EBADFD;
1612 return vcl_session_cleanup (wrk, session, session_handle,
1613 1 /* do_disconnect */ );
1614}
1615
1616int
Florin Coras134a9962018-08-28 11:32:04 -07001617vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001618{
Florin Coras134a9962018-08-28 11:32:04 -07001619 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001620 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001621
1622 if (!ep || !ep->ip)
1623 return VPPCOM_EINVAL;
1624
Florin Coras134a9962018-08-28 11:32:04 -07001625 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001626 if (!session)
1627 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001628
Florin Coras6c3b2182020-10-19 18:36:48 -07001629 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001630 {
Florin Coras5e062572019-03-14 19:07:51 -07001631 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1632 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001633 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001634 }
1635
Florin Coras7e12d942018-06-27 14:32:43 -07001636 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001637 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001638 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1639 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001640 else
Dave Barach178cf492018-11-13 16:34:13 -05001641 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1642 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001643 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001644
Florin Coras60687192021-11-29 21:00:47 -08001645 VDBG (0,
1646 "session %u handle %u: binding to local %s address %U port %u, "
1647 "proto %s",
1648 session->session_index, session_handle,
1649 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1650 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001651 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1652 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001653 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001654 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001655
1656 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001657 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001658
Florin Coras070453d2018-08-24 17:04:27 -07001659 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001660}
1661
1662int
Florin Coras134a9962018-08-28 11:32:04 -07001663vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001664{
Florin Coras134a9962018-08-28 11:32:04 -07001665 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001666 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001667 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001668 int rv;
1669
Florin Coras134a9962018-08-28 11:32:04 -07001670 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001671 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001672 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001673
Dave Wallaceee45d412017-11-24 21:44:06 -05001674 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001675 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001676 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001677 VDBG (0, "session %u [0x%llx]: already in listen state!",
1678 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001679 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001680 }
1681
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001682 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001683
Florin Coras070453d2018-08-24 17:04:27 -07001684 /*
1685 * Send listen request to vpp and wait for reply
1686 */
Florin Coras458089b2019-08-21 16:20:44 -07001687 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001688 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001689 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001690 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001691
Florin Coras070453d2018-08-24 17:04:27 -07001692 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001693 {
Florin Coras134a9962018-08-28 11:32:04 -07001694 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001695 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1696 listen_sh, listen_session->vpp_handle, rv,
1697 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001698 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001699 }
1700
Florin Coras070453d2018-08-24 17:04:27 -07001701 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001702}
1703
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001704int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001705vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1706{
1707 if (!strcmp (proto_str, "TCP"))
1708 *proto = VPPCOM_PROTO_TCP;
1709 else if (!strcmp (proto_str, "tcp"))
1710 *proto = VPPCOM_PROTO_TCP;
1711 else if (!strcmp (proto_str, "UDP"))
1712 *proto = VPPCOM_PROTO_UDP;
1713 else if (!strcmp (proto_str, "udp"))
1714 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001715 else if (!strcmp (proto_str, "TLS"))
1716 *proto = VPPCOM_PROTO_TLS;
1717 else if (!strcmp (proto_str, "tls"))
1718 *proto = VPPCOM_PROTO_TLS;
1719 else if (!strcmp (proto_str, "QUIC"))
1720 *proto = VPPCOM_PROTO_QUIC;
1721 else if (!strcmp (proto_str, "quic"))
1722 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001723 else if (!strcmp (proto_str, "DTLS"))
1724 *proto = VPPCOM_PROTO_DTLS;
1725 else if (!strcmp (proto_str, "dtls"))
1726 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001727 else if (!strcmp (proto_str, "SRTP"))
1728 *proto = VPPCOM_PROTO_SRTP;
1729 else if (!strcmp (proto_str, "srtp"))
1730 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001731 else
1732 return 1;
1733 return 0;
1734}
1735
1736int
Florin Coras32881932023-02-06 13:30:13 -08001737vppcom_session_accept (uint32_t ls_handle, vppcom_endpt_t *ep, uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001738{
Florin Coras32881932023-02-06 13:30:13 -08001739 u32 client_session_index = ~0, ls_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001740 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001741 session_accepted_msg_t accepted_msg;
Florin Coras32881932023-02-06 13:30:13 -08001742 vcl_session_t *ls, *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001743 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001744 u8 is_nonblocking;
Dave Wallace543852a2017-08-03 02:11:34 -04001745
Florin Coras5398dfb2021-01-25 20:31:27 -08001746again:
1747
Florin Coras32881932023-02-06 13:30:13 -08001748 ls = vcl_session_get_w_handle (wrk, ls_handle);
1749 if (!ls)
Florin Coras070453d2018-08-24 17:04:27 -07001750 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001751
Florin Coras32881932023-02-06 13:30:13 -08001752 if ((ls->session_state != VCL_STATE_LISTEN) &&
1753 (ls->session_state != VCL_STATE_LISTEN_NO_MQ) &&
1754 (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001755 {
Florin Coras32881932023-02-06 13:30:13 -08001756 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state (%s)",
1757 ls->vpp_handle, vcl_session_state_str (ls->session_state));
1758 return VPPCOM_EBADFD;
1759 }
1760
1761 ls_index = ls->session_index;
1762
1763 if (clib_fifo_elts (ls->accept_evts_fifo))
1764 {
1765 clib_fifo_sub2 (ls->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001766 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001767 accepted_msg = evt->accepted_msg;
1768 goto handle;
1769 }
1770
Florin Coras32881932023-02-06 13:30:13 -08001771 is_nonblocking = vcl_session_has_attr (ls, VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001772 while (1)
1773 {
Carl Smith592a9092019-11-12 14:57:37 +13001774 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1775 return VPPCOM_EAGAIN;
1776
Florin Coras5398dfb2021-01-25 20:31:27 -08001777 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1778 vcl_worker_flush_mq_events (wrk);
1779 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001780 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001781
Florin Coras54693d22018-07-17 10:46:29 -07001782handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001783
Florin Coras32881932023-02-06 13:30:13 -08001784 client_session_index =
1785 vcl_session_accepted_handler (wrk, &accepted_msg, ls_index);
Florin Coras00cca802019-06-06 09:38:44 -07001786 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1787 return VPPCOM_ECONNABORTED;
1788
Florin Coras32881932023-02-06 13:30:13 -08001789 ls = vcl_session_get (wrk, ls_index);
Florin Coras134a9962018-08-28 11:32:04 -07001790 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001791
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001792 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001793 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001794
Florin Coras32881932023-02-06 13:30:13 -08001795 VDBG (1,
1796 "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1797 " flags %d, is_nonblocking %u",
1798 ls->session_index, ls->vpp_handle, client_session_index,
Florin Coras5e062572019-03-14 19:07:51 -07001799 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001800 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001801
Dave Wallace048b1d62018-01-03 22:24:41 -05001802 if (ep)
1803 {
Florin Coras7e12d942018-06-27 14:32:43 -07001804 ep->is_ip4 = client_session->transport.is_ip4;
1805 ep->port = client_session->transport.rmt_port;
1806 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001807 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1808 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001809 else
Dave Barach178cf492018-11-13 16:34:13 -05001810 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1811 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001812 }
Dave Wallace60caa062017-11-10 17:07:13 -05001813
Florin Coras60687192021-11-29 21:00:47 -08001814 VDBG (0,
1815 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1816 "local: %U:%u",
Florin Coras32881932023-02-06 13:30:13 -08001817 ls_handle, ls->vpp_handle, client_session_index,
1818 client_session->vpp_handle, vcl_format_ip46_address,
1819 &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001820 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001821 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001822 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001823 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001824 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras32881932023-02-06 13:30:13 -08001825 vcl_evt (VCL_EVT_ACCEPT, client_session, ls, client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001826
Florin Coras3c7d4f92018-12-14 11:28:43 -08001827 /*
1828 * Session might have been closed already
1829 */
1830 if (accept_flags)
1831 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001832 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001833 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001834 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001835 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001836 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001837 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001838}
1839
1840int
Florin Coras134a9962018-08-28 11:32:04 -07001841vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001842{
Florin Coras134a9962018-08-28 11:32:04 -07001843 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001844 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001845 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001846 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001847
Florin Coras134a9962018-08-28 11:32:04 -07001848 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001849 if (!session)
1850 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001851 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001852
Florin Coras6c3b2182020-10-19 18:36:48 -07001853 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001854 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001855 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001856 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001857 }
1858
Florin Corasc127d5a2020-10-14 16:35:58 -07001859 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001860 {
Florin Coras60687192021-11-29 21:00:47 -08001861 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001862 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1863 " state (%s)",
1864 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001865 vcl_format_ip46_address, &session->transport.rmt_ip,
1866 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001867 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001868 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001869 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001870 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001871 }
1872
Florin Coras0a1e1832020-03-29 18:54:04 +00001873 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001874 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001875 {
1876 if (session->session_type != VPPCOM_PROTO_UDP)
1877 return VPPCOM_EINVAL;
1878 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001879 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001880 }
1881
Florin Coras7e12d942018-06-27 14:32:43 -07001882 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001883 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001884 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001885 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001886 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001887
Florin Coras5ffb9642021-12-03 17:24:13 -08001888 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1889 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001890 &session->transport.rmt_ip,
1891 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001892 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001893 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001894
Florin Coras458089b2019-08-21 16:20:44 -07001895 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001896
Florin Corasac422d62020-10-19 20:51:36 -07001897 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001898 {
fanyfa49d59d2020-10-13 17:07:16 +08001899 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001900 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001901 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001902 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001903 return VPPCOM_EINPROGRESS;
1904 }
Florin Coras57c88932019-08-29 12:03:17 -07001905
1906 /*
1907 * Wait for reply from vpp if blocking
1908 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001909 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001910 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001911
Florin Coras134a9962018-08-28 11:32:04 -07001912 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001913 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1914 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001915
Dave Wallace4878cbe2017-11-21 03:45:09 -05001916 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001917}
1918
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001919int
1920vppcom_session_stream_connect (uint32_t session_handle,
1921 uint32_t parent_session_handle)
1922{
1923 vcl_worker_t *wrk = vcl_worker_get_current ();
1924 vcl_session_t *session, *parent_session;
1925 u32 session_index, parent_session_index;
1926 int rv;
1927
1928 session = vcl_session_get_w_handle (wrk, session_handle);
1929 if (!session)
1930 return VPPCOM_EBADFD;
1931 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1932 if (!parent_session)
1933 return VPPCOM_EBADFD;
1934
1935 session_index = session->session_index;
1936 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001937 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001938 {
1939 VDBG (0, "ERROR: cannot connect epoll session %u!",
1940 session->session_index);
1941 return VPPCOM_EBADFD;
1942 }
1943
Florin Corasc127d5a2020-10-14 16:35:58 -07001944 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001945 {
Florin Coras60687192021-11-29 21:00:47 -08001946 VDBG (0,
1947 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001948 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001949 session_handle, session->vpp_handle, parent_session_handle,
1950 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001951 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001952 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001953 return VPPCOM_OK;
1954 }
1955
1956 /* Connect to quic session specifics */
1957 session->transport.is_ip4 = parent_session->transport.is_ip4;
1958 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1959 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001960 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001961
1962 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1963 session_handle, parent_session_handle, parent_session->vpp_handle);
1964
1965 /*
1966 * Send connect request and wait for reply from vpp
1967 */
Florin Coras458089b2019-08-21 16:20:44 -07001968 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001969 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001970 vcm->cfg.session_timeout);
1971
1972 session->listener_index = parent_session_index;
1973 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001974 if (parent_session)
1975 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001976
1977 session = vcl_session_get (wrk, session_index);
1978 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1979 session->vpp_handle, rv ? "failed" : "succeeded");
1980
1981 return rv;
1982}
1983
Steven58f464e2017-10-25 12:33:12 -07001984static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001985vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001986 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001987{
Florin Coras134a9962018-08-28 11:32:04 -07001988 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001989 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001990 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001991 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001992 session_event_t *e;
1993 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001994 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001995
Florin Coras070453d2018-08-24 17:04:27 -07001996 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001997 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001998
Florin Coras134a9962018-08-28 11:32:04 -07001999 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002000 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07002001 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002002
Florin Coras6d0106e2019-01-29 20:11:58 -08002003 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002004 {
Florin Coras5e062572019-03-14 19:07:51 -07002005 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002006 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002007 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002008 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002009 }
2010
liuyacan55c952e2021-06-13 14:54:55 +08002011 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2012 {
2013 /* Vpp would ack the incoming data and enqueue it for reading.
2014 * So even if SHUT_RD is set, we can still read() the data if
2015 * the session is ready.
2016 */
2017 if (!vcl_session_read_ready (s))
2018 {
2019 return 0;
2020 }
2021 }
2022
Florin Corasac422d62020-10-19 20:51:36 -07002023 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002024 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002025 mq = wrk->app_event_queue;
2026 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002027 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002028
Sirshak Das28aa5392019-02-05 01:33:33 -06002029 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002030 {
Florin Coras54693d22018-07-17 10:46:29 -07002031 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002032 {
Yu Pingb2955352019-12-04 06:49:04 +08002033 if (vcl_session_is_closing (s))
2034 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002035 if (is_ct)
2036 svm_fifo_unset_event (s->rx_fifo);
2037 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002038 return VPPCOM_EWOULDBLOCK;
2039 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002040 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002041 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002042 if (vcl_session_is_closing (s))
2043 return vcl_session_closing_error (s);
2044
Florin Corasd6894562020-10-28 00:37:15 -07002045 if (is_ct)
2046 svm_fifo_unset_event (s->rx_fifo);
2047 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002048
Florin Coras5398dfb2021-01-25 20:31:27 -08002049 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2050 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002051 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002052 }
Florin Coras54693d22018-07-17 10:46:29 -07002053
Florin Coras4a2c7942020-09-10 12:27:14 -07002054read_again:
2055
Florin Coras460dce62018-07-27 05:45:06 -07002056 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002057 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002058 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002059 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2060
2061 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002062
2063 if (peek)
2064 return rv;
2065
Florin Coras4a2c7942020-09-10 12:27:14 -07002066 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002067
Sirshak Das28aa5392019-02-05 01:33:33 -06002068 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002069 {
Florin Corasd6894562020-10-28 00:37:15 -07002070 if (is_ct)
2071 svm_fifo_unset_event (s->rx_fifo);
2072 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002073 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002074 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002075 {
Florin Corasc34118b2020-08-12 18:58:25 -07002076 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2077 e->event_type = SESSION_IO_EVT_RX;
2078 e->session_index = s->session_index;
2079 }
2080 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002081 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002082 {
2083 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002084 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002085 buf += rv;
2086 n -= rv;
2087 goto read_again;
2088 }
Florin Coras41c9e042018-09-11 00:10:41 -07002089
Florin Coras17ec5772020-08-12 20:46:29 -07002090 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002091 {
Florin Coras17ec5772020-08-12 20:46:29 -07002092 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002093 app_send_io_evt_to_vpp (s->vpp_evt_q,
2094 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002095 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002096 }
2097
Florin Coras5e062572019-03-14 19:07:51 -07002098 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2099 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002100
Florin Coras54693d22018-07-17 10:46:29 -07002101 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002102}
2103
Steven58f464e2017-10-25 12:33:12 -07002104int
Florin Coras134a9962018-08-28 11:32:04 -07002105vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002106{
Florin Coras134a9962018-08-28 11:32:04 -07002107 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002108}
2109
2110static int
Florin Coras134a9962018-08-28 11:32:04 -07002111vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002112{
Florin Coras134a9962018-08-28 11:32:04 -07002113 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002114}
2115
Florin Coras2cba8532018-09-11 16:33:36 -07002116int
2117vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002118 vppcom_data_segment_t * ds, uint32_t n_segments,
2119 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002120{
2121 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002122 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002123 vcl_session_t *s = 0;
2124 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002125 svm_msg_q_t *mq;
2126 u8 is_ct;
2127
2128 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002129 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002130 return VPPCOM_EBADFD;
2131
Florin Coras6d0106e2019-01-29 20:11:58 -08002132 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2133 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002134
Florin Corasac422d62020-10-19 20:51:36 -07002135 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002136 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002137 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002138 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002139 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002140
Sirshak Das28aa5392019-02-05 01:33:33 -06002141 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002142 {
2143 if (is_nonblocking)
2144 {
Florin Coras62877022020-10-28 21:22:04 -07002145 if (is_ct)
2146 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002147 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002148 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002149 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002150 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002151 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002152 if (vcl_session_is_closing (s))
2153 return vcl_session_closing_error (s);
2154
Florin Coras62877022020-10-28 21:22:04 -07002155 if (is_ct)
2156 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002157 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002158
Florin Coras5398dfb2021-01-25 20:31:27 -08002159 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2160 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002161 }
2162 }
2163
Florin Corasd1cc38d2020-10-11 11:05:04 -07002164 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002165 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002166 if (n_read < 0)
2167 return VPPCOM_EAGAIN;
2168
2169 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2170 {
Florin Coras62877022020-10-28 21:22:04 -07002171 if (is_ct)
2172 svm_fifo_unset_event (s->rx_fifo);
2173 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002174 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002175 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002176 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002177 {
2178 session_event_t *e;
2179 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2180 e->event_type = SESSION_IO_EVT_RX;
2181 e->session_index = s->session_index;
2182 }
2183 }
2184
2185 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002186 return n_read;
2187}
2188
2189void
Florin Corasd68faf82020-09-25 15:18:13 -07002190vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002191{
2192 vcl_worker_t *wrk = vcl_worker_get_current ();
2193 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002194 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002195
2196 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002197 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002198 return;
2199
Florin Coras62877022020-10-28 21:22:04 -07002200 is_ct = vcl_session_is_ct (s);
2201 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002202
Florin Coras8cc93212021-09-10 11:37:12 -07002203 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002204 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002205}
2206
Florin Coras7a2abce2020-04-05 19:25:44 +00002207always_inline u8
2208vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002209{
Florin Coras7a2abce2020-04-05 19:25:44 +00002210 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2211 if (is_dgram)
2212 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2213 else
2214 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002215}
2216
2217always_inline int
Dou Chao243a0432022-11-29 19:41:34 +08002218vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002219 size_t n, u8 is_flush, u8 is_dgram)
Florin Coras7a2abce2020-04-05 19:25:44 +00002220{
Florin Coras6d0106e2019-01-29 20:11:58 -08002221 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002222 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002223 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002224 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002225 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002226
Florin Coras0b0d28e2021-06-04 17:31:53 -07002227 /* Accept zero length writes but just return */
2228 if (PREDICT_FALSE (!n))
2229 return VPPCOM_OK;
2230
2231 if (PREDICT_FALSE (!buf))
2232 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002233
Florin Coras6c3b2182020-10-19 18:36:48 -07002234 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002235 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002236 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2237 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002238 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002239 }
2240
liuyacan55c952e2021-06-13 14:54:55 +08002241 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002242 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002243 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2244 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002245 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002246 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002247 }
2248
liuyacan55c952e2021-06-13 14:54:55 +08002249 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2250 {
2251 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2252 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002253 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002254 return VPPCOM_EPIPE;
2255 }
2256
Florin Coras0e88e852018-09-17 22:09:02 -07002257 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002258 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002259 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002260
Florin Coras653e43f2019-03-04 10:56:23 -08002261 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002262 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002263 {
Florin Coras54693d22018-07-17 10:46:29 -07002264 if (is_nonblocking)
2265 {
Florin Coras070453d2018-08-24 17:04:27 -07002266 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002267 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002268 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002269 {
Florin Coras2d379d82019-06-28 12:45:12 -07002270 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002271 if (vcl_session_is_closing (s))
2272 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002273
Florin Coras5398dfb2021-01-25 20:31:27 -08002274 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2275 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002276 }
Dave Wallace543852a2017-08-03 02:11:34 -04002277 }
Dave Wallace543852a2017-08-03 02:11:34 -04002278
Florin Coras653e43f2019-03-04 10:56:23 -08002279 et = SESSION_IO_EVT_TX;
2280 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002281 et = SESSION_IO_EVT_TX_FLUSH;
2282
Florin Coras7a2abce2020-04-05 19:25:44 +00002283 if (is_dgram)
Dou Chao243a0432022-11-29 19:41:34 +08002284 n_write =
2285 app_send_dgram_raw_gso (tx_fifo, &s->transport, s->vpp_evt_q, buf, n,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002286 s->gso_size, et, 0 /* do_evt */, SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002287 else
2288 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002289 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002290
Florin Coras14ed6df2019-03-06 21:13:42 -08002291 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002292 app_send_io_evt_to_vpp (
2293 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002294
Florin Coras56230092020-09-02 20:52:58 -07002295 /* The underlying fifo segment can run out of memory */
2296 if (PREDICT_FALSE (n_write < 0))
2297 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002298
Florin Coras6d0106e2019-01-29 20:11:58 -08002299 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2300 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002301
Florin Coras54693d22018-07-17 10:46:29 -07002302 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002303}
2304
Florin Coras42ceddb2018-12-12 10:56:01 -08002305int
2306vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2307{
Florin Coras7a2abce2020-04-05 19:25:44 +00002308 vcl_worker_t *wrk = vcl_worker_get_current ();
2309 vcl_session_t *s;
2310
2311 s = vcl_session_get_w_handle (wrk, session_handle);
2312 if (PREDICT_FALSE (!s))
2313 return VPPCOM_EBADFD;
2314
Florin Coraseff5f7a2023-02-07 17:36:17 -08002315 return vppcom_session_write_inline (wrk, s, buf, n, 0 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002316 s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002317}
2318
Florin Corasb0f662f2018-12-27 14:51:46 -08002319int
2320vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2321{
Florin Coras7a2abce2020-04-05 19:25:44 +00002322 vcl_worker_t *wrk = vcl_worker_get_current ();
2323 vcl_session_t *s;
2324
2325 s = vcl_session_get_w_handle (wrk, session_handle);
2326 if (PREDICT_FALSE (!s))
2327 return VPPCOM_EBADFD;
2328
Florin Coraseff5f7a2023-02-07 17:36:17 -08002329 return vppcom_session_write_inline (wrk, s, buf, n, 1 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002330 s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002331}
2332
Florin Corasc0737e92019-03-04 14:19:39 -08002333#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002334if (PREDICT_FALSE (!_s->rx_fifo)) \
2335 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002336if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2337 { \
2338 if (!vcl_session_is_ct (_s)) \
2339 { \
2340 svm_fifo_unset_event (_s->rx_fifo); \
2341 if (svm_fifo_is_empty (_s->rx_fifo)) \
2342 break; \
2343 } \
2344 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2345 { \
Florin Coras2f630182020-08-13 00:17:51 -07002346 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002347 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2348 break; \
2349 } \
2350 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002351
Florin Coras86f04502018-09-12 16:08:01 -07002352static void
2353vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2354 unsigned long n_bits, unsigned long *read_map,
2355 unsigned long *write_map,
2356 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002357{
2358 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002359 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002360 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002361 u32 sid;
2362
2363 switch (e->event_type)
2364 {
Florin Corasc0737e92019-03-04 14:19:39 -08002365 case SESSION_IO_EVT_RX:
2366 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002367 s = vcl_session_get (wrk, sid);
2368 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002369 break;
Florin Corasb242d312020-10-26 15:35:40 -07002370 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002371 if (sid < n_bits && read_map)
2372 {
David Johnsond9818dd2018-12-14 14:53:41 -05002373 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002374 *bits_set += 1;
2375 }
2376 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002377 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002378 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;
2382 if (sid < n_bits && write_map)
2383 {
David Johnsond9818dd2018-12-14 14:53:41 -05002384 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002385 *bits_set += 1;
2386 }
2387 break;
Florin Coras86f04502018-09-12 16:08:01 -07002388 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002389 if (!e->postponed)
2390 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2391 else
2392 s = vcl_session_get (wrk, e->session_index);
2393 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002394 break;
Florin Corasb242d312020-10-26 15:35:40 -07002395 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002396 if (sid < n_bits && read_map)
2397 {
David Johnsond9818dd2018-12-14 14:53:41 -05002398 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002399 *bits_set += 1;
2400 }
2401 break;
2402 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002403 if (!e->postponed)
2404 {
2405 connected_msg = (session_connected_msg_t *) e->data;
2406 sid = vcl_session_connected_handler (wrk, connected_msg);
2407 }
2408 else
2409 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002410 if (sid == VCL_INVALID_SESSION_INDEX)
2411 break;
Florin Coras66c675e2023-03-09 16:43:02 -08002412 if (!(sid < n_bits && write_map))
2413 break;
2414 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2415 *bits_set += 1;
2416 s = vcl_session_get (wrk, sid);
2417 if (!s->tx_fifo)
2418 break;
2419 /* We didn't have a fifo when the event was added */
2420 svm_fifo_add_want_deq_ntf (
2421 (vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo),
2422 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras86f04502018-09-12 16:08:01 -07002423 break;
2424 case SESSION_CTRL_EVT_DISCONNECTED:
2425 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002426 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2427 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002428 break;
Florin Corasb242d312020-10-26 15:35:40 -07002429 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002430 if (sid < n_bits && except_map)
2431 {
David Johnsond9818dd2018-12-14 14:53:41 -05002432 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002433 *bits_set += 1;
2434 }
2435 break;
2436 case SESSION_CTRL_EVT_RESET:
2437 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2438 if (sid < n_bits && except_map)
2439 {
David Johnsond9818dd2018-12-14 14:53:41 -05002440 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002441 *bits_set += 1;
2442 }
2443 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002444 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2445 vcl_session_unlisten_reply_handler (wrk, e->data);
2446 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002447 case SESSION_CTRL_EVT_MIGRATED:
2448 vcl_session_migrated_handler (wrk, e->data);
2449 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002450 case SESSION_CTRL_EVT_CLEANUP:
2451 vcl_session_cleanup_handler (wrk, e->data);
2452 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002453 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2454 vcl_session_worker_update_reply_handler (wrk, e->data);
2455 break;
2456 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2457 vcl_session_req_worker_update_handler (wrk, e->data);
2458 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002459 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2460 vcl_session_app_add_segment_handler (wrk, e->data);
2461 break;
2462 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2463 vcl_session_app_del_segment_handler (wrk, e->data);
2464 break;
hanlina3a48962020-07-13 11:09:15 +08002465 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002466 vcl_worker_rpc_handler (wrk, e->data);
2467 break;
Florin Coras86f04502018-09-12 16:08:01 -07002468 default:
2469 clib_warning ("unhandled: %u", e->event_type);
2470 break;
2471 }
2472}
2473
2474static int
2475vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2476 unsigned long n_bits, unsigned long *read_map,
2477 unsigned long *write_map, unsigned long *except_map,
2478 double time_to_wait, u32 * bits_set)
2479{
Florin Coras99368312018-08-02 10:45:44 -07002480 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002481 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002482 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002483
Florin Coras54693d22018-07-17 10:46:29 -07002484 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002485 {
Florin Coras54693d22018-07-17 10:46:29 -07002486 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002487 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002488
Florin Coras54693d22018-07-17 10:46:29 -07002489 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002490 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002491 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002492 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002493 else
2494 {
2495 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002496 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002497 }
2498 }
Florin Corase003a1b2019-06-05 10:47:16 -07002499 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002500
Florin Coras134a9962018-08-28 11:32:04 -07002501 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002502 {
Florin Coras134a9962018-08-28 11:32:04 -07002503 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002504 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002505 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2506 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002507 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002508 }
Florin Coras134a9962018-08-28 11:32:04 -07002509 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002510 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002511 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002512}
2513
Florin Coras99368312018-08-02 10:45:44 -07002514static int
Florin Coras294afe22019-01-07 17:49:17 -08002515vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2516 vcl_si_set * read_map, vcl_si_set * write_map,
2517 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002518 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002519{
Florin Coras14ed6df2019-03-06 21:13:42 -08002520 double wait = 0, start = 0;
2521
2522 if (!*bits_set)
2523 {
2524 wait = time_to_wait;
2525 start = clib_time_now (&wrk->clib_time);
2526 }
2527
2528 do
2529 {
2530 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2531 write_map, except_map, wait, bits_set);
2532 if (*bits_set)
2533 return *bits_set;
2534 if (wait == -1)
2535 continue;
2536
2537 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2538 }
2539 while (wait > 0);
2540
2541 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002542}
2543
2544static int
Florin Coras294afe22019-01-07 17:49:17 -08002545vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2546 vcl_si_set * read_map, vcl_si_set * write_map,
2547 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002548 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002549{
2550 vcl_mq_evt_conn_t *mqc;
2551 int __clib_unused n_read;
2552 int n_mq_evts, i;
2553 u64 buf;
2554
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002555 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2556 {
2557 vcl_api_retry_attach (wrk);
2558 return 0;
2559 }
2560
Florin Coras134a9962018-08-28 11:32:04 -07002561 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2562 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2563 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002564 for (i = 0; i < n_mq_evts; i++)
2565 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002566 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2567 {
2568 vcl_api_handle_disconnect (wrk);
2569 continue;
2570 }
2571
Florin Coras134a9962018-08-28 11:32:04 -07002572 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002573 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002574 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002575 except_map, 0, bits_set);
2576 }
2577
2578 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2579}
2580
Dave Wallace543852a2017-08-03 02:11:34 -04002581int
Florin Coras294afe22019-01-07 17:49:17 -08002582vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2583 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002584{
Florin Coras54693d22018-07-17 10:46:29 -07002585 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002586 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002587 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002588 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002589
Dave Wallace7876d392017-10-19 03:53:57 -04002590 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002591 {
Florin Coras134a9962018-08-28 11:32:04 -07002592 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002593 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002594 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2595 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002596 }
Dave Wallace7876d392017-10-19 03:53:57 -04002597 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002598 {
Florin Coras134a9962018-08-28 11:32:04 -07002599 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002600 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002601 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2602 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002603 }
Dave Wallace7876d392017-10-19 03:53:57 -04002604 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002605 {
Florin Coras134a9962018-08-28 11:32:04 -07002606 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002607 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002608 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2609 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002610 }
2611
Florin Coras54693d22018-07-17 10:46:29 -07002612 if (!n_bits)
2613 return 0;
2614
2615 if (!write_map)
2616 goto check_rd;
2617
Florin Coras096a1d52021-01-27 15:33:51 -08002618 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2619 {
2620 if (!(s = vcl_session_get (wrk, sid)))
2621 {
2622 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2623 bits_set++;
2624 continue;
2625 }
Florin Coras54693d22018-07-17 10:46:29 -07002626
Florin Coras096a1d52021-01-27 15:33:51 -08002627 if (vcl_session_write_ready (s))
2628 {
2629 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2630 bits_set++;
2631 }
Florin Coras66c675e2023-03-09 16:43:02 -08002632 else if (s->tx_fifo)
Florin Coras096a1d52021-01-27 15:33:51 -08002633 {
2634 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2635 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2636 }
2637 }
Florin Coras54693d22018-07-17 10:46:29 -07002638
2639check_rd:
2640 if (!read_map)
2641 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002642
Florin Coras096a1d52021-01-27 15:33:51 -08002643 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2644 {
2645 if (!(s = vcl_session_get (wrk, sid)))
2646 {
2647 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2648 bits_set++;
2649 continue;
2650 }
Florin Coras54693d22018-07-17 10:46:29 -07002651
Florin Coras096a1d52021-01-27 15:33:51 -08002652 if (vcl_session_read_ready (s))
2653 {
2654 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2655 bits_set++;
2656 }
2657 }
Florin Coras54693d22018-07-17 10:46:29 -07002658
2659check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002660
Florin Coras86f04502018-09-12 16:08:01 -07002661 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2662 {
2663 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2664 read_map, write_map, except_map, &bits_set);
2665 }
2666 vec_reset_length (wrk->unhandled_evts_vector);
2667
Florin Coras99368312018-08-02 10:45:44 -07002668 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002669 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002670 time_to_wait, &bits_set);
2671 else
Florin Coras134a9962018-08-28 11:32:04 -07002672 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002673 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002674
Dave Wallace543852a2017-08-03 02:11:34 -04002675 return (bits_set);
2676}
2677
Dave Wallacef7f809c2017-10-03 01:48:42 -04002678static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002679vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002680{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002681 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002682 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002683 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002684
Florin Corasc0737e92019-03-04 14:19:39 -08002685 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686 return;
2687
Florin Coras6c3b2182020-10-19 18:36:48 -07002688 s = vcl_session_get_w_handle (wrk, vep_handle);
2689 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002691 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002692 goto done;
2693 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002694 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002695 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002696 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697 goto done;
2698 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002699 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002700 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002701 "{\n"
2702 " is_vep = %u\n"
2703 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002704 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002705 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2706 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002707
Florin Coras7e5e62b2019-07-30 14:08:23 -07002708 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002709 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002710 s = vcl_session_get_w_handle (wrk, sh);
2711 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002712 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002713 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002714 goto done;
2715 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002716 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002717 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002718 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002719 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002720 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002721 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002722 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002723 goto done;
2724 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002725 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002726 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2727 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002728 sh, s->vep.vep_sh, vep_handle);
2729 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002730 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002731 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002732 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002733 " next_sh = 0x%x (%u)\n"
2734 " prev_sh = 0x%x (%u)\n"
2735 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002736 " ev.events = 0x%x\n"
2737 " ev.data.u64 = 0x%llx\n"
2738 " et_mask = 0x%x\n"
2739 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002740 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002741 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2742 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743 }
2744 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002745
2746done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002747 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002748}
2749
2750int
2751vppcom_epoll_create (void)
2752{
Florin Coras134a9962018-08-28 11:32:04 -07002753 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002754 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002755
Florin Coras134a9962018-08-28 11:32:04 -07002756 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757
Florin Coras6c3b2182020-10-19 18:36:48 -07002758 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002759 vep_session->vep.vep_sh = ~0;
2760 vep_session->vep.next_sh = ~0;
2761 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002762 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002763
Florin Corasa7a1a222018-12-30 17:11:31 -08002764 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2765 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002766
Florin Corasab2f6db2018-08-31 14:31:41 -07002767 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002768}
2769
Florin Corasb0116a12023-02-07 09:11:47 -08002770static void
2771vcl_epoll_ctl_add_unhandled_event (vcl_worker_t *wrk, vcl_session_t *s,
2772 u8 is_epollet, session_evt_type_t evt)
2773{
2774 if (!is_epollet)
2775 {
Florin Corasc2a14172023-02-28 21:13:50 -08002776 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
2777 vcl_epoll_lt_add (wrk, s);
Florin Corasb0116a12023-02-07 09:11:47 -08002778 return;
2779 }
2780
2781 session_event_t e = { 0 };
2782 e.session_index = s->session_index;
2783 e.event_type = evt;
2784 if (evt == SESSION_IO_EVT_RX)
2785 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2786 vec_add1 (wrk->unhandled_evts_vector, e);
2787}
2788
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789int
Florin Coras134a9962018-08-28 11:32:04 -07002790vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791 struct epoll_event *event)
2792{
Florin Coras134a9962018-08-28 11:32:04 -07002793 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002794 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002795 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002796 vcl_session_t *s;
2797 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002798
Florin Coras134a9962018-08-28 11:32:04 -07002799 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002800 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002801 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802 return VPPCOM_EINVAL;
2803 }
2804
Florin Coras134a9962018-08-28 11:32:04 -07002805 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002806 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002807 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002808 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002809 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002810 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002811 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002812 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002813 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002814 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002815 }
2816
Florin Coras134a9962018-08-28 11:32:04 -07002817 ASSERT (vep_session->vep.vep_sh == ~0);
2818 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002819
Florin Coras17ec5772020-08-12 20:46:29 -07002820 s = vcl_session_get_w_handle (wrk, session_handle);
2821 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002822 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002823 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002824 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002825 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002826 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002827 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002828 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002829 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002830 }
2831
2832 switch (op)
2833 {
2834 case EPOLL_CTL_ADD:
2835 if (PREDICT_FALSE (!event))
2836 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002837 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002838 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002839 }
Florin Coras2645f682021-06-04 15:59:41 -07002840 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2841 {
2842 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2843 rv = VPPCOM_EEXIST;
2844 goto done;
2845 }
Florin Coras134a9962018-08-28 11:32:04 -07002846 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002847 {
Florin Coras7e12d942018-06-27 14:32:43 -07002848 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002849 next_session = vcl_session_get_w_handle (wrk,
2850 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002851 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002852 {
Florin Coras5e062572019-03-14 19:07:51 -07002853 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002854 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002855 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002856 }
Florin Coras134a9962018-08-28 11:32:04 -07002857 ASSERT (next_session->vep.prev_sh == vep_handle);
2858 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002859 }
Florin Coras17ec5772020-08-12 20:46:29 -07002860 s->vep.next_sh = vep_session->vep.next_sh;
2861 s->vep.prev_sh = vep_handle;
2862 s->vep.vep_sh = vep_handle;
2863 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002864 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002865 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002866 s->flags &= ~VCL_SESSION_F_IS_VEP;
2867 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002868 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002869
Florin Coras17ec5772020-08-12 20:46:29 -07002870 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2871 if (txf && (event->events & EPOLLOUT))
2872 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002873
Florin Coras6e3c1f82020-01-15 01:30:46 +00002874 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002875 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002876 {
Florin Corasb0116a12023-02-07 09:11:47 -08002877 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2878 SESSION_IO_EVT_TX);
Florin Coras87f76002021-06-28 19:13:29 -07002879 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002880 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002881 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002882 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002883 {
Florin Corasb0116a12023-02-07 09:11:47 -08002884 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2885 SESSION_IO_EVT_RX);
Florin Coras87f76002021-06-28 19:13:29 -07002886 add_evt = 1;
2887 }
2888 if (!add_evt && vcl_session_is_closing (s))
2889 {
2890 session_event_t e = { 0 };
2891 if (s->session_state == VCL_STATE_VPP_CLOSING)
2892 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2893 else
2894 e.event_type = SESSION_CTRL_EVT_RESET;
2895 e.session_index = s->session_index;
2896 e.postponed = 1;
2897 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002898 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002899 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2900 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002901 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002902 break;
2903
2904 case EPOLL_CTL_MOD:
2905 if (PREDICT_FALSE (!event))
2906 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002907 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002908 rv = VPPCOM_EINVAL;
2909 goto done;
2910 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002911 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002912 {
Florin Coras5e062572019-03-14 19:07:51 -07002913 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002914 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002915 goto done;
2916 }
Florin Coras17ec5772020-08-12 20:46:29 -07002917 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002918 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002919 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002920 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002921 rv = VPPCOM_EINVAL;
2922 goto done;
2923 }
hanlin475c9d72019-12-26 11:44:28 +08002924
Florin Coras2645f682021-06-04 15:59:41 -07002925 /* Generate EPOLLOUT if session write ready nd event was not on */
2926 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2927 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002928 {
Florin Corasb0116a12023-02-07 09:11:47 -08002929 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2930 SESSION_IO_EVT_TX);
hanlin475c9d72019-12-26 11:44:28 +08002931 }
Florin Coras2645f682021-06-04 15:59:41 -07002932 /* Generate EPOLLIN if session read ready and event was not on */
2933 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2934 (vcl_session_read_ready (s) > 0))
2935 {
Florin Corasb0116a12023-02-07 09:11:47 -08002936 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2937 SESSION_IO_EVT_RX);
Florin Coras2645f682021-06-04 15:59:41 -07002938 }
Florin Coras17ec5772020-08-12 20:46:29 -07002939 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2940 s->vep.ev = *event;
2941 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002942 if (txf)
2943 {
2944 if (event->events & EPOLLOUT)
2945 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2946 else
2947 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2948 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002949 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2950 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002951 break;
2952
2953 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002954 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002955 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002956 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002957 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002958 goto done;
2959 }
Florin Coras17ec5772020-08-12 20:46:29 -07002960 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002961 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002962 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002963 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002964 rv = VPPCOM_EINVAL;
2965 goto done;
2966 }
2967
Florin Coras17ec5772020-08-12 20:46:29 -07002968 if (s->vep.prev_sh == vep_handle)
2969 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002970 else
2971 {
Florin Coras7e12d942018-06-27 14:32:43 -07002972 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002973 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002974 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002975 {
Florin Coras5e062572019-03-14 19:07:51 -07002976 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002977 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002978 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002979 }
Florin Coras134a9962018-08-28 11:32:04 -07002980 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002981 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002982 }
Florin Coras17ec5772020-08-12 20:46:29 -07002983 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002984 {
Florin Coras7e12d942018-06-27 14:32:43 -07002985 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002986 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002987 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002988 {
Florin Coras5e062572019-03-14 19:07:51 -07002989 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002990 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002991 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002992 }
Florin Coras134a9962018-08-28 11:32:04 -07002993 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002994 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002995 }
2996
Florin Corasfa3884f2021-06-22 20:04:46 -07002997 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2998 vcl_epoll_lt_del (wrk, s);
2999
Florin Coras17ec5772020-08-12 20:46:29 -07003000 memset (&s->vep, 0, sizeof (s->vep));
3001 s->vep.next_sh = ~0;
3002 s->vep.prev_sh = ~0;
3003 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003004 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07003005 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08003006
Florin Corasf1ddeeb2021-06-10 08:08:53 -07003007 if (vcl_session_is_open (s))
3008 {
3009 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
3010 if (txf)
3011 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
3012 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08003013
Florin Coras5e062572019-03-14 19:07:51 -07003014 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003015 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003016 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003017 break;
3018
3019 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003020 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003021 rv = VPPCOM_EINVAL;
3022 }
3023
Florin Coras134a9962018-08-28 11:32:04 -07003024 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003025
3026done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003027 return rv;
3028}
3029
Florin Coras86f04502018-09-12 16:08:01 -07003030static inline void
3031vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3032 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003033{
3034 session_disconnected_msg_t *disconnected_msg;
3035 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003036 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003037 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003038 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003039 u8 add_event = 0;
3040
3041 switch (e->event_type)
3042 {
Florin Coras653e43f2019-03-04 10:56:23 -08003043 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003044 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003045 s = vcl_session_get (wrk, sid);
3046 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003047 break;
Florin Corasb242d312020-10-26 15:35:40 -07003048 vcl_fifo_rx_evt_valid_or_break (s);
3049 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003050 if (!(EPOLLIN & s->vep.ev.events) ||
3051 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3052 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003053 break;
3054 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003055 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003056 session_evt_data = s->vep.ev.data.u64;
3057 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003058 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003059 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003060 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003061 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003062 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003063 break;
Florin Corasb242d312020-10-26 15:35:40 -07003064 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003065 if (!(EPOLLOUT & session_events))
3066 break;
3067 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003068 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003069 session_evt_data = s->vep.ev.data.u64;
3070 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3071 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003072 break;
Florin Coras86f04502018-09-12 16:08:01 -07003073 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003074 if (!e->postponed)
3075 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3076 else
3077 s = vcl_session_get (wrk, e->session_index);
3078 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003079 break;
Florin Corasb242d312020-10-26 15:35:40 -07003080 session_events = s->vep.ev.events;
3081 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003082 if (!(EPOLLIN & session_events) ||
3083 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003084 break;
Florin Coras86f04502018-09-12 16:08:01 -07003085 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003086 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003087 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003088 break;
3089 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003090 if (!e->postponed)
3091 {
3092 connected_msg = (session_connected_msg_t *) e->data;
3093 sid = vcl_session_connected_handler (wrk, connected_msg);
3094 }
3095 else
3096 sid = e->session_index;
3097 s = vcl_session_get (wrk, sid);
3098 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003099 break;
Florin Corasb242d312020-10-26 15:35:40 -07003100 session_events = s->vep.ev.events;
3101 /* Generate EPOLLOUT because there's no connected event */
Florin Coras436c7ba2023-03-08 14:14:38 -08003102 if (!(EPOLLOUT & session_events) || !s->tx_fifo)
Florin Coras72f77822019-01-22 19:05:52 -08003103 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003104 /* We didn't have a fifo when the event was added */
3105 svm_fifo_add_want_deq_ntf (
3106 (vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo),
3107 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003108 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003109 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003110 session_evt_data = s->vep.ev.data.u64;
3111 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003112 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003113 break;
3114 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003115 if (!e->postponed)
3116 {
3117 disconnected_msg = (session_disconnected_msg_t *) e->data;
3118 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3119 }
3120 else
3121 {
3122 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003123 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003124 }
3125 if (vcl_session_is_closed (s) ||
3126 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003127 {
Florin Coras74254b52021-12-08 11:03:08 -08003128 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003129 vcl_session_free (wrk, s);
3130 break;
3131 }
Florin Corasb242d312020-10-26 15:35:40 -07003132 sid = s->session_index;
3133 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003134 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003135 if (EPOLLRDHUP & session_events)
3136 {
3137 /* If app can distinguish between RDHUP and HUP,
3138 * we make finer control */
3139 events[*num_ev].events = EPOLLRDHUP;
3140 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3141 {
3142 events[*num_ev].events |= EPOLLHUP;
3143 }
3144 }
3145 else
3146 {
3147 events[*num_ev].events = EPOLLHUP;
3148 }
Florin Corasb242d312020-10-26 15:35:40 -07003149 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003150
Florin Coras86f04502018-09-12 16:08:01 -07003151 break;
Florin Coras01ee7a72023-03-01 00:49:25 -08003152 case SESSION_CTRL_EVT_BOUND:
3153 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
3154 break;
Florin Coras86f04502018-09-12 16:08:01 -07003155 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003156 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003157 {
3158 sid =
3159 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3160 s = vcl_session_get (wrk, sid);
3161 }
Florin Coras87f76002021-06-28 19:13:29 -07003162 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003163 {
3164 sid = e->session_index;
3165 s = vcl_session_get (wrk, sid);
3166 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3167 }
Florin Coras87f76002021-06-28 19:13:29 -07003168 if (vcl_session_is_closed (s) ||
3169 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003170 {
Florin Coras74254b52021-12-08 11:03:08 -08003171 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003172 vcl_session_free (wrk, s);
3173 break;
3174 }
Florin Corasb242d312020-10-26 15:35:40 -07003175 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003176 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003177 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3178 if ((EPOLLRDHUP & session_events) &&
3179 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3180 {
Yacan Liuab157702022-09-26 16:41:32 +08003181 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003182 }
3183 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3184 {
3185 events[*num_ev].events |= EPOLLIN;
3186 }
Florin Corasb242d312020-10-26 15:35:40 -07003187 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003188 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003189 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3190 vcl_session_unlisten_reply_handler (wrk, e->data);
3191 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003192 case SESSION_CTRL_EVT_MIGRATED:
3193 vcl_session_migrated_handler (wrk, e->data);
3194 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003195 case SESSION_CTRL_EVT_CLEANUP:
3196 vcl_session_cleanup_handler (wrk, e->data);
3197 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003198 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3199 vcl_session_req_worker_update_handler (wrk, e->data);
3200 break;
3201 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3202 vcl_session_worker_update_reply_handler (wrk, e->data);
3203 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003204 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3205 vcl_session_app_add_segment_handler (wrk, e->data);
3206 break;
3207 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3208 vcl_session_app_del_segment_handler (wrk, e->data);
3209 break;
hanlina3a48962020-07-13 11:09:15 +08003210 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003211 vcl_worker_rpc_handler (wrk, e->data);
3212 break;
Florin Coras86f04502018-09-12 16:08:01 -07003213 default:
3214 VDBG (0, "unhandled: %u", e->event_type);
3215 break;
3216 }
3217
3218 if (add_event)
3219 {
3220 events[*num_ev].data.u64 = session_evt_data;
3221 if (EPOLLONESHOT & session_events)
3222 {
Florin Corasb242d312020-10-26 15:35:40 -07003223 s = vcl_session_get (wrk, sid);
3224 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003225 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003226 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003227 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003228 s = vcl_session_get (wrk, sid);
3229 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3230 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003231 }
Florin Coras86f04502018-09-12 16:08:01 -07003232 *num_ev += 1;
3233 }
3234}
3235
3236static int
3237vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3238 struct epoll_event *events, u32 maxevents,
3239 double wait_for_time, u32 * num_ev)
3240{
Florin Coras99368312018-08-02 10:45:44 -07003241 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003242 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003243 int i;
3244
Florin Coras539663c2018-09-28 14:59:37 -07003245 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3246 goto handle_dequeued;
3247
Florin Coras54693d22018-07-17 10:46:29 -07003248 if (svm_msg_q_is_empty (mq))
3249 {
3250 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003251 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003252 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003253 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003254 else
3255 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003256 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003257 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003258 }
3259 }
Florin Corase003a1b2019-06-05 10:47:16 -07003260 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003261 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003262
Florin Coras539663c2018-09-28 14:59:37 -07003263handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003264 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003265 {
Florin Coras134a9962018-08-28 11:32:04 -07003266 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003267 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003268 if (*num_ev < maxevents)
3269 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3270 else
3271 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003272 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003273 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003274 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003275 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003276 return *num_ev;
3277}
3278
Florin Coras99368312018-08-02 10:45:44 -07003279static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003280vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3281 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003282{
Florin Corasccdb8b82021-04-28 13:01:06 -07003283 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003284
3285 if (!n_evts)
3286 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003287 if (timeout_ms > 0)
3288 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003289 }
3290
3291 do
3292 {
3293 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003294 timeout_ms, &n_evts);
3295 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003296 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003297 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003298 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003299
3300 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003301}
3302
3303static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003304vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3305 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003306{
Florin Coras99368312018-08-02 10:45:44 -07003307 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003308 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003309 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003310 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003311 u64 buf;
3312
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003313 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3314 {
3315 vcl_api_retry_attach (wrk);
3316 return n_evts;
3317 }
3318
Florin Coras134a9962018-08-28 11:32:04 -07003319 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003320 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003321 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003322 if (timeout_ms > 0)
3323 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003324 }
3325
Florin Corasb13ba132021-01-27 16:05:24 -08003326 do
3327 {
3328 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003329 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003330 if (n_mq_evts < 0)
3331 {
3332 VDBG (0, "epoll_wait error %u", errno);
3333 return n_evts;
3334 }
3335
3336 for (i = 0; i < n_mq_evts; i++)
3337 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003338 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3339 {
3340 /* api socket was closed */
3341 vcl_api_handle_disconnect (wrk);
3342 continue;
3343 }
3344
Florin Corasb13ba132021-01-27 16:05:24 -08003345 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3346 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3347 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3348 &n_evts);
3349 }
3350
Florin Corasccdb8b82021-04-28 13:01:06 -07003351 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003352 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003353 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003354 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003355
3356 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003357}
3358
Florin Corasfe286f72021-06-04 10:07:55 -07003359static void
Florin Corasfe286f72021-06-04 10:07:55 -07003360vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3361 int maxevents, u32 *n_evts)
3362{
Florin Coras734268f2021-06-30 07:54:29 -07003363 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003364 vcl_session_t *s;
3365 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003366 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003367
Florin Corasfa3884f2021-06-22 20:04:46 -07003368 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003369 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003370 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003371
Florin Corasfa3884f2021-06-22 20:04:46 -07003372 next = wrk->ep_lt_current;
3373 do
Florin Corasfe286f72021-06-04 10:07:55 -07003374 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003375 s = vcl_session_get (wrk, next);
3376 next = s->vep.lt_next;
3377
3378 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003379 {
3380 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003381 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003382 evt_data = s->vep.ev.data.u64;
3383 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003384 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003385 {
3386 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003387 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3388 evt_data = s->vep.ev.data.u64;
3389 }
3390 if (!add_event && s->session_state > VCL_STATE_READY)
3391 {
3392 add_event = 1;
3393 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003394 evt_data = s->vep.ev.data.u64;
3395 }
3396 if (add_event)
3397 {
3398 events[*n_evts].data.u64 = evt_data;
3399 *n_evts += 1;
3400 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003401 if (EPOLLONESHOT & s->vep.ev.events)
3402 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003403 if (*n_evts == maxevents)
3404 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003405 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003406 break;
3407 }
3408 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003409 else
3410 {
3411 vcl_epoll_lt_del (wrk, s);
3412 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3413 break;
3414 }
Florin Corasfe286f72021-06-04 10:07:55 -07003415 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003416 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003417}
3418
Dave Wallacef7f809c2017-10-03 01:48:42 -04003419int
Florin Coras134a9962018-08-28 11:32:04 -07003420vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003421 int maxevents, double wait_for_time)
3422{
Florin Coras134a9962018-08-28 11:32:04 -07003423 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003424 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003425 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003426 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003427
3428 if (PREDICT_FALSE (maxevents <= 0))
3429 {
Florin Coras5e062572019-03-14 19:07:51 -07003430 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003431 return VPPCOM_EINVAL;
3432 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003433
Florin Coras134a9962018-08-28 11:32:04 -07003434 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003435 if (!vep_session)
3436 return VPPCOM_EBADFD;
3437
Florin Coras6c3b2182020-10-19 18:36:48 -07003438 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003439 {
Florin Coras5e062572019-03-14 19:07:51 -07003440 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003441 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003442 }
Florin Coras54693d22018-07-17 10:46:29 -07003443
Florin Coras86f04502018-09-12 16:08:01 -07003444 if (vec_len (wrk->unhandled_evts_vector))
3445 {
3446 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3447 {
3448 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3449 events, &n_evts);
3450 if (n_evts == maxevents)
3451 {
Florin Corase003a1b2019-06-05 10:47:16 -07003452 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3453 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003454 }
3455 }
Florin Corase003a1b2019-06-05 10:47:16 -07003456 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003457 }
liuyacancba87102021-09-10 15:14:05 +08003458
3459 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3460 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3461
wanghanlin8919fec2021-03-18 20:00:41 +08003462 /* Request to only drain unhandled */
3463 if ((int) wait_for_time == -2)
3464 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003465
Florin Coras86f04502018-09-12 16:08:01 -07003466
Florin Corasfe286f72021-06-04 10:07:55 -07003467 if (vcm->cfg.use_mq_eventfd)
3468 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3469 wait_for_time);
3470 else
3471 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3472 wait_for_time);
3473
Florin Corasfe286f72021-06-04 10:07:55 -07003474 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003475}
3476
Dave Wallace35830af2017-10-09 01:43:42 -04003477int
Florin Coras134a9962018-08-28 11:32:04 -07003478vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003479 void *buffer, uint32_t * buflen)
3480{
Florin Coras134a9962018-08-28 11:32:04 -07003481 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003482 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003483 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003484 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003485 vcl_session_t *session;
3486 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003487
Florin Coras134a9962018-08-28 11:32:04 -07003488 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003489 if (!session)
3490 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003491
Dave Wallace35830af2017-10-09 01:43:42 -04003492 switch (op)
3493 {
3494 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003495 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003496 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3497 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003498 break;
3499
Dave Wallace227867f2017-11-13 21:21:53 -05003500 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003501 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003502 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3503 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003504 break;
3505
3506 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003508 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003509 *flags =
3510 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003511 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003512 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003513 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003514 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3515 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003516 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003517 }
3518 else
3519 rv = VPPCOM_EINVAL;
3520 break;
3521
3522 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003523 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003524 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003525 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003526 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003527 else
Florin Corasac422d62020-10-19 20:51:36 -07003528 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003529
Florin Coras5e062572019-03-14 19:07:51 -07003530 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3531 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003532 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003533 }
3534 else
3535 rv = VPPCOM_EINVAL;
3536 break;
3537
3538 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 if (PREDICT_TRUE (buffer && buflen &&
3540 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003541 {
Florin Coras7e12d942018-06-27 14:32:43 -07003542 ep->is_ip4 = session->transport.is_ip4;
3543 ep->port = session->transport.rmt_port;
3544 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003545 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3546 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003547 else
Dave Barach178cf492018-11-13 16:34:13 -05003548 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3549 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003550 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003551 VDBG (1,
3552 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3553 "addr = %U, port %u",
3554 session_handle, ep->is_ip4, vcl_format_ip46_address,
3555 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003556 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3557 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003558 }
3559 else
3560 rv = VPPCOM_EINVAL;
3561 break;
3562
3563 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003564 if (PREDICT_TRUE (buffer && buflen &&
3565 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003566 {
Florin Coras7e12d942018-06-27 14:32:43 -07003567 ep->is_ip4 = session->transport.is_ip4;
3568 ep->port = session->transport.lcl_port;
3569 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003570 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3571 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003572 else
Dave Barach178cf492018-11-13 16:34:13 -05003573 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3574 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003575 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003576 VDBG (1,
3577 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3578 " port %d",
3579 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003580 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003581 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3582 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003583 }
3584 else
3585 rv = VPPCOM_EINVAL;
3586 break;
Stevenb5a11602017-10-11 09:59:30 -07003587
Florin Corasef7cbf62019-10-17 09:56:27 -07003588 case VPPCOM_ATTR_SET_LCL_ADDR:
3589 if (PREDICT_TRUE (buffer && buflen &&
3590 (*buflen >= sizeof (*ep)) && ep->ip))
3591 {
3592 session->transport.is_ip4 = ep->is_ip4;
3593 session->transport.lcl_port = ep->port;
3594 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3595 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003596 VDBG (1,
3597 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3598 " port %d",
3599 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003600 &session->transport.lcl_ip,
3601 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3602 clib_net_to_host_u16 (ep->port));
3603 }
3604 else
3605 rv = VPPCOM_EINVAL;
3606 break;
3607
Dave Wallace048b1d62018-01-03 22:24:41 -05003608 case VPPCOM_ATTR_GET_LIBC_EPFD:
3609 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003610 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003611 break;
3612
3613 case VPPCOM_ATTR_SET_LIBC_EPFD:
3614 if (PREDICT_TRUE (buffer && buflen &&
3615 (*buflen == sizeof (session->libc_epfd))))
3616 {
3617 session->libc_epfd = *(int *) buffer;
3618 *buflen = sizeof (session->libc_epfd);
3619
Florin Coras5e062572019-03-14 19:07:51 -07003620 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3621 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003622 }
3623 else
3624 rv = VPPCOM_EINVAL;
3625 break;
3626
3627 case VPPCOM_ATTR_GET_PROTOCOL:
3628 if (buffer && buflen && (*buflen >= sizeof (int)))
3629 {
Florin Coras7e12d942018-06-27 14:32:43 -07003630 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003631 *buflen = sizeof (int);
3632
Florin Coras5e062572019-03-14 19:07:51 -07003633 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3634 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003635 }
3636 else
3637 rv = VPPCOM_EINVAL;
3638 break;
3639
3640 case VPPCOM_ATTR_GET_LISTEN:
3641 if (buffer && buflen && (*buflen >= sizeof (int)))
3642 {
Florin Corasac422d62020-10-19 20:51:36 -07003643 *(int *) buffer = vcl_session_has_attr (session,
3644 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003645 *buflen = sizeof (int);
3646
Florin Coras5e062572019-03-14 19:07:51 -07003647 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3648 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003649 }
3650 else
3651 rv = VPPCOM_EINVAL;
3652 break;
3653
3654 case VPPCOM_ATTR_GET_ERROR:
3655 if (buffer && buflen && (*buflen >= sizeof (int)))
3656 {
3657 *(int *) buffer = 0;
3658 *buflen = sizeof (int);
3659
Florin Coras5e062572019-03-14 19:07:51 -07003660 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3661 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003662 }
3663 else
3664 rv = VPPCOM_EINVAL;
3665 break;
3666
3667 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3668 if (buffer && buflen && (*buflen >= sizeof (u32)))
3669 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003670
3671 /* VPP-TBD */
3672 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003673 session->tx_fifo ?
3674 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003675 vcm->cfg.tx_fifo_size);
3676 *buflen = sizeof (u32);
3677
Florin Coras5e062572019-03-14 19:07:51 -07003678 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3679 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3680 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003681 }
3682 else
3683 rv = VPPCOM_EINVAL;
3684 break;
3685
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003686 case VPPCOM_ATTR_SET_DSCP:
3687 if (buffer && buflen && (*buflen >= sizeof (u8)))
3688 {
3689 session->dscp = *(u8 *) buffer;
3690
3691 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3692 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3693 }
3694 else
3695 rv = VPPCOM_EINVAL;
3696 break;
3697
Dave Wallace048b1d62018-01-03 22:24:41 -05003698 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3699 if (buffer && buflen && (*buflen == sizeof (u32)))
3700 {
3701 /* VPP-TBD */
3702 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003703 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3704 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3705 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003706 }
3707 else
3708 rv = VPPCOM_EINVAL;
3709 break;
3710
3711 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3712 if (buffer && buflen && (*buflen >= sizeof (u32)))
3713 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003714
3715 /* VPP-TBD */
3716 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003717 session->rx_fifo ?
3718 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003719 vcm->cfg.rx_fifo_size);
3720 *buflen = sizeof (u32);
3721
Florin Coras5e062572019-03-14 19:07:51 -07003722 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3723 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003724 }
3725 else
3726 rv = VPPCOM_EINVAL;
3727 break;
3728
3729 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3730 if (buffer && buflen && (*buflen == sizeof (u32)))
3731 {
3732 /* VPP-TBD */
3733 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003734 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3735 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3736 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003737 }
3738 else
3739 rv = VPPCOM_EINVAL;
3740 break;
3741
3742 case VPPCOM_ATTR_GET_REUSEADDR:
3743 if (buffer && buflen && (*buflen >= sizeof (int)))
3744 {
3745 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003746 *(int *) buffer = vcl_session_has_attr (session,
3747 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003748 *buflen = sizeof (int);
3749
Florin Coras5e062572019-03-14 19:07:51 -07003750 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3751 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003752 }
3753 else
3754 rv = VPPCOM_EINVAL;
3755 break;
3756
Stevenb5a11602017-10-11 09:59:30 -07003757 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003758 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003759 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003760 {
3761 /* VPP-TBD */
3762 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003763 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003764 else
Florin Corasac422d62020-10-19 20:51:36 -07003765 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003766
Florin Coras5e062572019-03-14 19:07:51 -07003767 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003768 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003769 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003770 }
3771 else
3772 rv = VPPCOM_EINVAL;
3773 break;
3774
3775 case VPPCOM_ATTR_GET_REUSEPORT:
3776 if (buffer && buflen && (*buflen >= sizeof (int)))
3777 {
3778 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003779 *(int *) buffer = vcl_session_has_attr (session,
3780 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003781 *buflen = sizeof (int);
3782
Florin Coras5e062572019-03-14 19:07:51 -07003783 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3784 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003785 }
3786 else
3787 rv = VPPCOM_EINVAL;
3788 break;
3789
3790 case VPPCOM_ATTR_SET_REUSEPORT:
3791 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003792 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003793 {
3794 /* VPP-TBD */
3795 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003796 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003797 else
Florin Corasac422d62020-10-19 20:51:36 -07003798 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003799
Florin Coras5e062572019-03-14 19:07:51 -07003800 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003801 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003802 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003803 }
3804 else
3805 rv = VPPCOM_EINVAL;
3806 break;
3807
3808 case VPPCOM_ATTR_GET_BROADCAST:
3809 if (buffer && buflen && (*buflen >= sizeof (int)))
3810 {
3811 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003812 *(int *) buffer = vcl_session_has_attr (session,
3813 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003814 *buflen = sizeof (int);
3815
Florin Coras5e062572019-03-14 19:07:51 -07003816 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3817 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003818 }
3819 else
3820 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003821 break;
3822
3823 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003824 if (buffer && buflen && (*buflen == sizeof (int)))
3825 {
3826 /* VPP-TBD */
3827 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003828 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003829 else
Florin Corasac422d62020-10-19 20:51:36 -07003830 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003831
Florin Coras5e062572019-03-14 19:07:51 -07003832 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003833 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003834 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003835 }
3836 else
3837 rv = VPPCOM_EINVAL;
3838 break;
3839
3840 case VPPCOM_ATTR_GET_V6ONLY:
3841 if (buffer && buflen && (*buflen >= sizeof (int)))
3842 {
3843 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003844 *(int *) buffer = vcl_session_has_attr (session,
3845 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003846 *buflen = sizeof (int);
3847
Florin Coras5e062572019-03-14 19:07:51 -07003848 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3849 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003850 }
3851 else
3852 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003853 break;
3854
3855 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003856 if (buffer && buflen && (*buflen == sizeof (int)))
3857 {
3858 /* VPP-TBD */
3859 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003860 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003861 else
Florin Corasac422d62020-10-19 20:51:36 -07003862 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003863
Florin Coras5e062572019-03-14 19:07:51 -07003864 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003865 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003866 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003867 }
3868 else
3869 rv = VPPCOM_EINVAL;
3870 break;
3871
3872 case VPPCOM_ATTR_GET_KEEPALIVE:
3873 if (buffer && buflen && (*buflen >= sizeof (int)))
3874 {
3875 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003876 *(int *) buffer = vcl_session_has_attr (session,
3877 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003878 *buflen = sizeof (int);
3879
Florin Coras5e062572019-03-14 19:07:51 -07003880 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3881 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003882 }
3883 else
3884 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003885 break;
Stevenbccd3392017-10-12 20:42:21 -07003886
3887 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003888 if (buffer && buflen && (*buflen == sizeof (int)))
3889 {
3890 /* VPP-TBD */
3891 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003892 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003893 else
Florin Corasac422d62020-10-19 20:51:36 -07003894 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003895
Florin Coras5e062572019-03-14 19:07:51 -07003896 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003897 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003898 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003899 }
3900 else
3901 rv = VPPCOM_EINVAL;
3902 break;
3903
3904 case VPPCOM_ATTR_GET_TCP_NODELAY:
3905 if (buffer && buflen && (*buflen >= sizeof (int)))
3906 {
3907 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003908 *(int *) buffer = vcl_session_has_attr (session,
3909 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003910 *buflen = sizeof (int);
3911
Florin Coras5e062572019-03-14 19:07:51 -07003912 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3913 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003914 }
3915 else
3916 rv = VPPCOM_EINVAL;
3917 break;
3918
3919 case VPPCOM_ATTR_SET_TCP_NODELAY:
3920 if (buffer && buflen && (*buflen == sizeof (int)))
3921 {
3922 /* VPP-TBD */
3923 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003924 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003925 else
Florin Corasac422d62020-10-19 20:51:36 -07003926 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003927
Florin Coras5e062572019-03-14 19:07:51 -07003928 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003929 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003930 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003931 }
3932 else
3933 rv = VPPCOM_EINVAL;
3934 break;
3935
3936 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3937 if (buffer && buflen && (*buflen >= sizeof (int)))
3938 {
3939 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003940 *(int *) buffer = vcl_session_has_attr (session,
3941 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003942 *buflen = sizeof (int);
3943
Florin Coras5e062572019-03-14 19:07:51 -07003944 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3945 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003946 }
3947 else
3948 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003949 break;
3950
3951 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003952 if (buffer && buflen && (*buflen == sizeof (int)))
3953 {
3954 /* VPP-TBD */
3955 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003956 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003957 else
Florin Corasac422d62020-10-19 20:51:36 -07003958 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003959
Florin Coras5e062572019-03-14 19:07:51 -07003960 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003961 vcl_session_has_attr (session,
3962 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003963 }
3964 else
3965 rv = VPPCOM_EINVAL;
3966 break;
3967
3968 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3969 if (buffer && buflen && (*buflen >= sizeof (int)))
3970 {
3971 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003972 *(int *) buffer = vcl_session_has_attr (session,
3973 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003974 *buflen = sizeof (int);
3975
Florin Coras5e062572019-03-14 19:07:51 -07003976 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3977 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003978 }
3979 else
3980 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003981 break;
3982
3983 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003984 if (buffer && buflen && (*buflen == sizeof (int)))
3985 {
3986 /* VPP-TBD */
3987 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003988 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003989 else
Florin Corasac422d62020-10-19 20:51:36 -07003990 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003991
Florin Coras5e062572019-03-14 19:07:51 -07003992 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003993 vcl_session_has_attr (session,
3994 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003995 }
3996 else
3997 rv = VPPCOM_EINVAL;
3998 break;
3999
4000 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004001 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004002 {
Florin Coras04ae8272021-04-12 19:55:37 -07004003 rv = VPPCOM_EINVAL;
4004 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004005 }
Florin Coras04ae8272021-04-12 19:55:37 -07004006
4007 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4008 tea.mss = *(u32 *) buffer;
4009 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
4010 rv = VPPCOM_ENOPROTOOPT;
4011
4012 if (!rv)
4013 {
4014 *(u32 *) buffer = tea.mss;
4015 *buflen = sizeof (int);
4016 }
4017
4018 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4019 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004020 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004021 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004022 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004023 {
Florin Coras04ae8272021-04-12 19:55:37 -07004024 rv = VPPCOM_EINVAL;
4025 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004026 }
Florin Coras04ae8272021-04-12 19:55:37 -07004027
4028 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4029 tea.mss = *(u32 *) buffer;
4030 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4031 rv = VPPCOM_ENOPROTOOPT;
4032
4033 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4034 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004035 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004036
Florin Coras1e966172020-05-16 18:18:14 +00004037 case VPPCOM_ATTR_SET_CONNECTED:
4038 session->flags |= VCL_SESSION_F_CONNECTED;
4039 break;
4040
Florin Corasa5a9efd2021-01-05 17:03:29 -08004041 case VPPCOM_ATTR_SET_CKPAIR:
4042 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4043 !vcl_session_has_crypto (session))
4044 {
4045 rv = VPPCOM_EINVAL;
4046 break;
4047 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004048 if (!session->ext_config)
4049 {
Florin Coras87f63892021-05-01 16:01:40 -07004050 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4051 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004052 }
4053 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4054 {
4055 rv = VPPCOM_EINVAL;
4056 break;
4057 }
4058
4059 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004060 break;
4061
Florin Coras6a6555a2021-01-28 11:39:27 -08004062 case VPPCOM_ATTR_SET_VRF:
4063 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4064 {
4065 rv = VPPCOM_EINVAL;
4066 break;
4067 }
4068 session->vrf = *(u32 *) buffer;
4069 break;
4070
4071 case VPPCOM_ATTR_GET_VRF:
4072 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4073 {
4074 rv = VPPCOM_EINVAL;
4075 break;
4076 }
4077 *(u32 *) buffer = session->vrf;
4078 *buflen = sizeof (u32);
4079 break;
4080
wanghanlin0674f852021-02-22 10:38:36 +08004081 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004082 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004083 {
Florin Corasd77325c2021-02-23 12:03:03 -08004084 rv = VPPCOM_EINVAL;
4085 break;
wanghanlin0674f852021-02-22 10:38:36 +08004086 }
Florin Corasd77325c2021-02-23 12:03:03 -08004087
4088 if (session->transport.is_ip4)
4089 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004090 else
Florin Corasd77325c2021-02-23 12:03:03 -08004091 *(int *) buffer = AF_INET6;
4092 *buflen = sizeof (int);
4093
wanghanlin0674f852021-02-22 10:38:36 +08004094 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4095 *buflen);
4096 break;
4097
Florin Coras87f63892021-05-01 16:01:40 -07004098 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4099 if (!(buffer && buflen && (*buflen > 0)))
4100 {
4101 rv = VPPCOM_EINVAL;
4102 break;
4103 }
4104 if (session->ext_config)
4105 {
4106 rv = VPPCOM_EINVAL;
4107 break;
4108 }
4109 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4110 *buflen + sizeof (u32));
4111 clib_memcpy (session->ext_config->data, buffer, *buflen);
4112 session->ext_config->len = *buflen;
4113 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004114 case VPPCOM_ATTR_SET_IP_PKTINFO:
4115 if (buffer && buflen && (*buflen == sizeof (int)) &&
4116 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4117 {
4118 if (*(int *) buffer)
4119 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4120 else
4121 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4122
4123 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4124 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4125 *buflen);
4126 }
4127 else
4128 rv = VPPCOM_EINVAL;
4129 break;
4130
4131 case VPPCOM_ATTR_GET_IP_PKTINFO:
4132 if (buffer && buflen && (*buflen >= sizeof (int)))
4133 {
4134 *(int *) buffer =
4135 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4136 *buflen = sizeof (int);
4137
4138 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4139 *buflen);
4140 }
4141 else
4142 rv = VPPCOM_EINVAL;
4143 break;
Florin Coras87f63892021-05-01 16:01:40 -07004144
Dave Wallacee22aa742017-10-20 12:30:38 -04004145 default:
4146 rv = VPPCOM_EINVAL;
4147 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004148 }
4149
Dave Wallace35830af2017-10-09 01:43:42 -04004150 return rv;
4151}
4152
Stevenac1f96d2017-10-24 16:03:58 -07004153int
Florin Coras134a9962018-08-28 11:32:04 -07004154vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004155 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4156{
Florin Coras134a9962018-08-28 11:32:04 -07004157 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004158 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004159 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004160
4161 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004162 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004163 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004164 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004165 else
4166 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004167 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004168 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004169 }
4170
Florin Coras0a1e1832020-03-29 18:54:04 +00004171 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004172 {
Florin Coras5da10c42020-04-01 04:31:21 +00004173 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004174 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004175 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4176 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004177 else
Dave Barach178cf492018-11-13 16:34:13 -05004178 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4179 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004180 ep->is_ip4 = session->transport.is_ip4;
4181 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004182 }
Florin Coras460dce62018-07-27 05:45:06 -07004183
Stevenac1f96d2017-10-24 16:03:58 -07004184 return rv;
4185}
4186
Florin Coraseff5f7a2023-02-07 17:36:17 -08004187static void
4188vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4189{
4190 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4191
4192 do
4193 {
4194 switch (tlv->data_type)
4195 {
4196 case VCL_UDP_SEGMENT:
4197 s->gso_size = *(u16 *) tlv->data;
4198 break;
4199 case VCL_IP_PKTINFO:
4200 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4201 sizeof (ip4_address_t));
4202 break;
4203 default:
4204 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4205 break;
4206 }
4207 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4208 }
4209 while (tlv);
4210}
4211
Stevenac1f96d2017-10-24 16:03:58 -07004212int
Florin Coras134a9962018-08-28 11:32:04 -07004213vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004214 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4215{
Florin Coras7a2abce2020-04-05 19:25:44 +00004216 vcl_worker_t *wrk = vcl_worker_get_current ();
4217 vcl_session_t *s;
4218
4219 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004220 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004221 return VPPCOM_EBADFD;
4222
Dave Wallace617dffa2017-10-26 14:47:06 -04004223 if (ep)
4224 {
Florin Coras5824cc52020-10-20 18:44:41 -07004225 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004226 return VPPCOM_EINVAL;
4227
liuyacanbc0c7542021-08-02 20:15:05 +08004228 s->transport.is_ip4 = ep->is_ip4;
4229 s->transport.rmt_port = ep->port;
4230 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4231
Florin Coraseff5f7a2023-02-07 17:36:17 -08004232 if (ep->app_tlvs)
4233 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004234
Florin Coras5da10c42020-04-01 04:31:21 +00004235 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004236 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004237 {
Florin Coras5824cc52020-10-20 18:44:41 -07004238 u32 session_index = s->session_index;
4239 f64 timeout = vcm->cfg.session_timeout;
4240 int rv;
4241
Florin Coras5da10c42020-04-01 04:31:21 +00004242 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004243 rv = vppcom_wait_for_session_state_change (session_index,
4244 VCL_STATE_READY,
4245 timeout);
4246 if (rv < 0)
4247 return rv;
4248 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004249 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004250 }
4251
4252 if (flags)
4253 {
4254 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004255 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004256 }
4257
Florin Coraseff5f7a2023-02-07 17:36:17 -08004258 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004259 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004260}
4261
Dave Wallace048b1d62018-01-03 22:24:41 -05004262int
4263vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4264{
Florin Coras134a9962018-08-28 11:32:04 -07004265 vcl_worker_t *wrk = vcl_worker_get_current ();
4266 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004267 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004268 svm_msg_q_msg_t msg;
4269 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004270 int rv, num_ev = 0;
4271
Florin Coras5e062572019-03-14 19:07:51 -07004272 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004273
4274 if (!vp)
4275 return VPPCOM_EFAULT;
4276
4277 do
4278 {
Florin Coras7e12d942018-06-27 14:32:43 -07004279 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004280
Florin Coras6917b942018-11-13 22:44:54 -08004281 /* Dequeue all events and drop all unhandled io events */
4282 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4283 {
4284 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4285 vcl_handle_mq_event (wrk, e);
4286 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4287 }
4288 vec_reset_length (wrk->unhandled_evts_vector);
4289
Dave Wallace048b1d62018-01-03 22:24:41 -05004290 for (i = 0; i < n_sids; i++)
4291 {
Florin Coras7baeb712019-01-04 17:05:43 -08004292 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004293 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004294 {
4295 vp[i].revents = POLLHUP;
4296 num_ev++;
4297 continue;
4298 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004299
Florin Coras6917b942018-11-13 22:44:54 -08004300 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004301
4302 if (POLLIN & vp[i].events)
4303 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004304 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004305 if (rv > 0)
4306 {
Florin Coras6917b942018-11-13 22:44:54 -08004307 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004308 num_ev++;
4309 }
4310 else if (rv < 0)
4311 {
4312 switch (rv)
4313 {
4314 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004315 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004316 break;
4317
4318 default:
Florin Coras6917b942018-11-13 22:44:54 -08004319 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004320 break;
4321 }
4322 num_ev++;
4323 }
4324 }
4325
4326 if (POLLOUT & vp[i].events)
4327 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004328 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004329 if (rv > 0)
4330 {
Florin Coras6917b942018-11-13 22:44:54 -08004331 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004332 num_ev++;
4333 }
4334 else if (rv < 0)
4335 {
4336 switch (rv)
4337 {
4338 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004339 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004340 break;
4341
4342 default:
Florin Coras6917b942018-11-13 22:44:54 -08004343 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004344 break;
4345 }
4346 num_ev++;
4347 }
4348 }
4349
Dave Wallace7e607a72018-06-18 18:41:32 -04004350 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004351 {
Florin Coras6917b942018-11-13 22:44:54 -08004352 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004353 num_ev++;
4354 }
4355 }
4356 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004357 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004358 }
4359 while ((num_ev == 0) && keep_trying);
4360
Dave Wallace048b1d62018-01-03 22:24:41 -05004361 return num_ev;
4362}
4363
Florin Coras99368312018-08-02 10:45:44 -07004364int
4365vppcom_mq_epoll_fd (void)
4366{
Florin Coras134a9962018-08-28 11:32:04 -07004367 vcl_worker_t *wrk = vcl_worker_get_current ();
4368 return wrk->mqs_epfd;
4369}
4370
4371int
Florin Coras30e79c22019-01-02 19:31:22 -08004372vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004373{
4374 return session_handle & 0xFFFFFF;
4375}
4376
4377int
Florin Coras30e79c22019-01-02 19:31:22 -08004378vppcom_session_worker (vcl_session_handle_t session_handle)
4379{
4380 return session_handle >> 24;
4381}
4382
4383int
Florin Coras134a9962018-08-28 11:32:04 -07004384vppcom_worker_register (void)
4385{
Florin Coras47c40e22018-11-26 17:01:36 -08004386 if (!vcl_worker_alloc_and_init ())
4387 return VPPCOM_EEXIST;
4388
Florin Coras47c40e22018-11-26 17:01:36 -08004389 if (vcl_worker_register_with_vpp ())
4390 return VPPCOM_EEXIST;
4391
4392 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004393}
4394
Florin Coras369db832019-07-08 12:34:45 -07004395void
4396vppcom_worker_unregister (void)
4397{
4398 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4399 vcl_set_worker_index (~0);
4400}
4401
Pivo6017ff02020-05-04 17:57:33 +02004402void
4403vppcom_worker_index_set (int index)
4404{
4405 vcl_set_worker_index (index);
4406}
4407
Florin Corasdfe4cf42018-11-28 22:13:45 -08004408int
4409vppcom_worker_index (void)
4410{
4411 return vcl_get_worker_index ();
4412}
4413
Florin Corase0982e52019-01-25 13:19:56 -08004414int
4415vppcom_worker_mqs_epfd (void)
4416{
4417 vcl_worker_t *wrk = vcl_worker_get_current ();
4418 if (!vcm->cfg.use_mq_eventfd)
4419 return -1;
4420 return wrk->mqs_epfd;
4421}
4422
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004423int
4424vppcom_session_is_connectable_listener (uint32_t session_handle)
4425{
4426 vcl_session_t *session;
4427 vcl_worker_t *wrk = vcl_worker_get_current ();
4428 session = vcl_session_get_w_handle (wrk, session_handle);
4429 if (!session)
4430 return VPPCOM_EBADFD;
4431 return vcl_session_is_connectable_listener (wrk, session);
4432}
4433
4434int
4435vppcom_session_listener (uint32_t session_handle)
4436{
4437 vcl_worker_t *wrk = vcl_worker_get_current ();
4438 vcl_session_t *listen_session, *session;
4439 session = vcl_session_get_w_handle (wrk, session_handle);
4440 if (!session)
4441 return VPPCOM_EBADFD;
4442 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4443 return VPPCOM_EBADFD;
4444 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4445 if (!listen_session)
4446 return VPPCOM_EBADFD;
4447 return vcl_session_handle (listen_session);
4448}
4449
4450int
4451vppcom_session_n_accepted (uint32_t session_handle)
4452{
4453 vcl_worker_t *wrk = vcl_worker_get_current ();
4454 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4455 if (!session)
4456 return VPPCOM_EBADFD;
4457 return session->n_accepted_sessions;
4458}
4459
Florin Coras66ec4672020-06-15 07:59:40 -07004460const char *
4461vppcom_proto_str (vppcom_proto_t proto)
4462{
4463 char const *proto_str;
4464
4465 switch (proto)
4466 {
4467 case VPPCOM_PROTO_TCP:
4468 proto_str = "TCP";
4469 break;
4470 case VPPCOM_PROTO_UDP:
4471 proto_str = "UDP";
4472 break;
4473 case VPPCOM_PROTO_TLS:
4474 proto_str = "TLS";
4475 break;
4476 case VPPCOM_PROTO_QUIC:
4477 proto_str = "QUIC";
4478 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004479 case VPPCOM_PROTO_DTLS:
4480 proto_str = "DTLS";
4481 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004482 case VPPCOM_PROTO_SRTP:
4483 proto_str = "SRTP";
4484 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004485 default:
4486 proto_str = "UNKNOWN";
4487 break;
4488 }
4489 return proto_str;
4490}
4491
4492const char *
4493vppcom_retval_str (int retval)
4494{
4495 char const *st;
4496
4497 switch (retval)
4498 {
4499 case VPPCOM_OK:
4500 st = "VPPCOM_OK";
4501 break;
4502
4503 case VPPCOM_EAGAIN:
4504 st = "VPPCOM_EAGAIN";
4505 break;
4506
4507 case VPPCOM_EFAULT:
4508 st = "VPPCOM_EFAULT";
4509 break;
4510
4511 case VPPCOM_ENOMEM:
4512 st = "VPPCOM_ENOMEM";
4513 break;
4514
4515 case VPPCOM_EINVAL:
4516 st = "VPPCOM_EINVAL";
4517 break;
4518
4519 case VPPCOM_EBADFD:
4520 st = "VPPCOM_EBADFD";
4521 break;
4522
4523 case VPPCOM_EAFNOSUPPORT:
4524 st = "VPPCOM_EAFNOSUPPORT";
4525 break;
4526
4527 case VPPCOM_ECONNABORTED:
4528 st = "VPPCOM_ECONNABORTED";
4529 break;
4530
4531 case VPPCOM_ECONNRESET:
4532 st = "VPPCOM_ECONNRESET";
4533 break;
4534
4535 case VPPCOM_ENOTCONN:
4536 st = "VPPCOM_ENOTCONN";
4537 break;
4538
4539 case VPPCOM_ECONNREFUSED:
4540 st = "VPPCOM_ECONNREFUSED";
4541 break;
4542
4543 case VPPCOM_ETIMEDOUT:
4544 st = "VPPCOM_ETIMEDOUT";
4545 break;
4546
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304547 case VPPCOM_EADDRINUSE:
4548 st = "VPPCOM_EADDRINUSE";
4549 break;
4550
Florin Coras66ec4672020-06-15 07:59:40 -07004551 default:
4552 st = "UNKNOWN_STATE";
4553 break;
4554 }
4555
4556 return st;
4557}
4558
Florin Corasa5a9efd2021-01-05 17:03:29 -08004559int
4560vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4561{
4562 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004563 return vcl_sapi_add_cert_key_pair (ckpair);
4564 else
4565 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004566}
4567
4568int
4569vppcom_del_cert_key_pair (uint32_t ckpair_index)
4570{
4571 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004572 return vcl_sapi_del_cert_key_pair (ckpair_index);
4573 else
4574 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004575}
4576
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304577int
4578vppcom_session_get_error (uint32_t session_handle)
4579{
4580 vcl_worker_t *wrk = vcl_worker_get_current ();
4581 vcl_session_t *session = 0;
4582
4583 session = vcl_session_get_w_handle (wrk, session_handle);
4584 if (!session)
4585 return VPPCOM_EBADFD;
4586
4587 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4588 {
4589 VWRN ("epoll session %u! will not have connect", session->session_index);
4590 return VPPCOM_EBADFD;
4591 }
4592
4593 if (session->vpp_error == SESSION_E_PORTINUSE)
4594 return VPPCOM_EADDRINUSE;
4595 else if (session->vpp_error == SESSION_E_REFUSED)
4596 return VPPCOM_ECONNREFUSED;
4597 else if (session->vpp_error != SESSION_E_NONE)
4598 return VPPCOM_EFAULT;
4599 else
4600 return VPPCOM_OK;
4601}
4602
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004603int
4604vppcom_worker_is_detached (void)
4605{
4606 vcl_worker_t *wrk = vcl_worker_get_current ();
4607
4608 if (!vcm->cfg.use_mq_eventfd)
4609 return VPPCOM_ENOTSUP;
4610
4611 return wrk->api_client_handle == ~0;
4612}
4613
Dave Wallacee22aa742017-10-20 12:30:38 -04004614/*
4615 * fd.io coding-style-patch-verification: ON
4616 *
4617 * Local Variables:
4618 * eval: (c-set-style "gnu")
4619 * End:
4620 */