blob: 9e7af0a3a50f8a1a0e65f4329e0e500d5bdd184f [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);
Florin Coras89627e82023-04-27 13:38:35 -0700364 if (session->is_dgram)
365 session->flags |= (listen_session->flags & VCL_SESSION_F_CONNECTED);
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200366 session->listener_index = listen_session->session_index;
367 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700368
Florin Coras54693d22018-07-17 10:46:29 -0700369 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
370
Florin Coras00cca802019-06-06 09:38:44 -0700371 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
372 session->vpp_handle, 0);
373
Florin Coras134a9962018-08-28 11:32:04 -0700374 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700375
376error:
Florin Corasb4624182020-12-11 13:58:12 -0800377 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
378 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700379 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
380 VNET_API_ERROR_INVALID_ARGUMENT);
381 vcl_session_free (wrk, session);
382 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700383}
384
385static u32
Florin Coras134a9962018-08-28 11:32:04 -0700386vcl_session_connected_handler (vcl_worker_t * wrk,
387 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700388{
Florin Coras99368312018-08-02 10:45:44 -0700389 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800390 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700391
392 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700393 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800394 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700395 {
Florin Coras8d42c752021-11-29 23:26:19 -0800396 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
397 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800398 /* Should not happen but if it does, force vpp session cleanup */
399 vcl_session_t tmp_session = {
400 .vpp_handle = mp->handle,
401 .vpp_evt_q = 0,
402 };
403 vcl_segment_attach_session (
404 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
405 mp->vpp_event_queue_address, mp->mq_index, 0, session);
406 if (tmp_session.vpp_evt_q)
407 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700408 return VCL_INVALID_SESSION_INDEX;
409 }
Florin Coras8d42c752021-11-29 23:26:19 -0800410
Florin Coras54693d22018-07-17 10:46:29 -0700411 if (mp->retval)
412 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800413 VDBG (0, "session %u: connect failed! %U", session_index,
Florin Coras8d42c752021-11-29 23:26:19 -0800414 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700415 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800416 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +0530417 session->vpp_error = mp->retval;
Florin Coras070453d2018-08-24 17:04:27 -0700418 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700419 }
420
Florin Corasdbc9c592019-09-25 16:37:43 -0700421 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800422
Florin Coras8d42c752021-11-29 23:26:19 -0800423 /* Add to lookup table. Even if something fails, session cannot be
424 * cleaned up prior to notifying vpp and going through the cleanup
425 * "procedure" see @ref vcl_session_cleanup_handler */
426 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
427
Florin Corasf6e284b2021-07-21 18:17:20 -0700428 if (vcl_segment_attach_session (
429 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
430 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800431 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800432 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
433 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800434 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700435 vcl_send_session_disconnect (wrk, session);
436 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800437 }
438
Florin Coras653e43f2019-03-04 10:56:23 -0800439 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700440 {
Florin Corasc547e912020-12-08 17:50:45 -0800441 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700442 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
443 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800444 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800445 VDBG (0, "session %u [0x%llx]: failed to attach ct fifos",
446 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800447 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700448 vcl_send_session_disconnect (wrk, session);
449 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800450 }
Florin Coras99368312018-08-02 10:45:44 -0700451 }
Florin Coras54693d22018-07-17 10:46:29 -0700452
Florin Coras09d18c22019-04-24 11:10:02 -0700453 session->transport.is_ip4 = mp->lcl.is_ip4;
454 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500455 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700456 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700457
458 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700459 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700460 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700461 vcl_send_session_disconnect (wrk, session);
462 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700463 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700464
Florin Coras5ffb9642021-12-03 17:24:13 -0800465 VDBG (0, "session %u [0x%llx] connected local: %U:%u remote %U:%u",
466 session->session_index, session->vpp_handle, vcl_format_ip46_address,
467 &session->transport.lcl_ip,
468 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
469 clib_net_to_host_u16 (session->transport.lcl_port),
470 vcl_format_ip46_address, &session->transport.rmt_ip,
471 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
472 clib_net_to_host_u16 (session->transport.rmt_port));
Florin Coras070453d2018-08-24 17:04:27 -0700473
Florin Coras54693d22018-07-17 10:46:29 -0700474 return session_index;
475}
476
Florin Coras3c7d4f92018-12-14 11:28:43 -0800477static int
478vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
479{
480 vcl_session_msg_t *accepted_msg;
481 int i;
482
483 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
484 {
485 accepted_msg = &session->accept_evts_fifo[i];
486 if (accepted_msg->accepted_msg.handle == handle)
487 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800488 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800489 return 1;
490 }
491 }
492 return 0;
493}
494
Florin Corasc9fbd662018-08-24 12:59:56 -0700495static u32
Florin Coras134a9962018-08-28 11:32:04 -0700496vcl_session_reset_handler (vcl_worker_t * wrk,
497 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700498{
499 vcl_session_t *session;
500 u32 sid;
501
Florin Coras134a9962018-08-28 11:32:04 -0700502 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
503 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700504 if (!session)
505 {
506 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
507 return VCL_INVALID_SESSION_INDEX;
508 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800509
510 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700511 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800512 {
513
514 if (!vcl_flag_accepted_session (session, reset_msg->handle,
515 VCL_ACCEPTED_F_RESET))
516 VDBG (0, "session was not accepted!");
517 return VCL_INVALID_SESSION_INDEX;
518 }
519
Florin Corasc127d5a2020-10-14 16:35:58 -0700520 if (session->session_state != VCL_STATE_CLOSED)
521 session->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +0800522
523 session->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Coras5ffb9642021-12-03 17:24:13 -0800524 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700525 return sid;
526}
527
Florin Coras60116992018-08-27 09:52:18 -0700528static u32
Florin Coras134a9962018-08-28 11:32:04 -0700529vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700530{
531 vcl_session_t *session;
532 u32 sid = mp->context;
533
Florin Coras134a9962018-08-28 11:32:04 -0700534 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700535 if (mp->retval)
536 {
Florin Coras5e062572019-03-14 19:07:51 -0700537 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700538 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700539 if (session)
540 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700541 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700542 session->vpp_handle = mp->handle;
543 return sid;
544 }
545 else
546 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800547 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
548 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700549 return VCL_INVALID_SESSION_INDEX;
550 }
551 }
552
553 session->vpp_handle = mp->handle;
554 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500555 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
556 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700557 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700558 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700559 session->session_state = VCL_STATE_LISTEN;
Florin Coras32881932023-02-06 13:30:13 -0800560 session->flags &= ~VCL_SESSION_F_PENDING_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800561
Florin Coras6e3c1f82020-01-15 01:30:46 +0000562 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700563 {
Florin Corasc547e912020-12-08 17:50:45 -0800564 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700565 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
566 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800567 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800568 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
569 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800570 session->session_state = VCL_STATE_DETACHED;
571 return VCL_INVALID_SESSION_INDEX;
572 }
Florin Coras60116992018-08-27 09:52:18 -0700573 }
574
Florin Coras05ecfcc2018-12-12 18:19:39 -0800575 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700576 return sid;
577}
578
Florin Corasdfae9f92019-02-20 19:48:31 -0800579static void
580vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
581{
582 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
583 vcl_session_t *s;
584
585 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000586 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800587 {
588 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
589 return;
590 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700591 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000592 {
593 /* Connected udp listener */
594 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700595 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000596 return;
597
598 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
599 return;
600 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800601
602 if (mp->retval)
603 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700604 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800605
606 if (mp->context != wrk->wrk_index)
607 VDBG (0, "wrong context");
608
609 vcl_session_table_del_vpp_handle (wrk, mp->handle);
610 vcl_session_free (wrk, s);
611}
612
Florin Coras68b7e582020-01-21 18:33:23 -0800613static void
614vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
615{
616 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
617 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800618 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800619
620 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
621 if (!s)
622 {
623 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
624 return;
625 }
626
Florin Coras1bb74942021-02-10 15:26:37 -0800627 /* Only validate if a value is provided */
628 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800629 {
Florin Coras1bb74942021-02-10 15:26:37 -0800630 fs_index = vcl_segment_table_lookup (mp->segment_handle);
631 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
632 {
633 VDBG (0, "segment %lx for session %u is not mounted!",
634 mp->segment_handle, s->session_index);
635 s->session_state = VCL_STATE_DETACHED;
636 return;
637 }
Florin Corasc547e912020-12-08 17:50:45 -0800638 }
639
Florin Coras57660d92020-04-04 22:45:34 +0000640 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800641
642 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
643 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800644
Florin Coras68b7e582020-01-21 18:33:23 -0800645 vcl_session_table_del_vpp_handle (wrk, mp->handle);
646 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
647
648 /* Generate new tx event if we have outstanding data */
649 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800650 app_send_io_evt_to_vpp (s->vpp_evt_q,
651 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800652 SESSION_IO_EVT_TX, SVM_Q_WAIT);
653
Florin Coras57660d92020-04-04 22:45:34 +0000654 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800655 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800656}
657
Florin Coras3c7d4f92018-12-14 11:28:43 -0800658static vcl_session_t *
659vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
660{
661 vcl_session_msg_t *vcl_msg;
662 vcl_session_t *session;
663
664 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
665 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800666 VWRN ("session overlap handle %lu state %u!", msg->handle,
667 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800668
669 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
670 if (!session)
671 {
672 VERR ("couldn't find listen session: listener handle %llx",
673 msg->listener_handle);
674 return 0;
675 }
676
677 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000678 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800679 vcl_msg->accepted_msg = *msg;
680 /* Session handle points to listener until fully accepted by app */
681 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
682
683 return session;
684}
685
686static vcl_session_t *
687vcl_session_disconnected_handler (vcl_worker_t * wrk,
688 session_disconnected_msg_t * msg)
689{
690 vcl_session_t *session;
691
692 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
693 if (!session)
694 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800695 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800696 return 0;
697 }
698
Florin Corasadcfb152020-02-12 08:50:29 +0000699 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700700 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000701 return 0;
702
Florin Coras3c7d4f92018-12-14 11:28:43 -0800703 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700704 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800705 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800706 if (!vcl_flag_accepted_session (session, msg->handle,
707 VCL_ACCEPTED_F_CLOSED))
708 VDBG (0, "session was not accepted!");
709 return 0;
710 }
711
Florin Corasadcfb152020-02-12 08:50:29 +0000712 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700713 if (session->session_state != VCL_STATE_DISCONNECT)
714 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000715
Florin Coras3c7d4f92018-12-14 11:28:43 -0800716 return session;
717}
718
liuyacan534468e2021-05-09 03:50:40 +0000719int
liuyacan55c952e2021-06-13 14:54:55 +0800720vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000721{
722 vcl_worker_t *wrk = vcl_worker_get_current ();
723 vcl_session_t *session;
724 vcl_session_state_t state;
725 u64 vpp_handle;
726
727 session = vcl_session_get_w_handle (wrk, session_handle);
728 if (PREDICT_FALSE (!session))
729 return VPPCOM_EBADFD;
730
731 vpp_handle = session->vpp_handle;
732 state = session->session_state;
733
734 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800735 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000736
737 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
738 {
739 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
740 return VPPCOM_EBADFD;
741 }
742
liuyacan55c952e2021-06-13 14:54:55 +0800743 if (how == SHUT_RD || how == SHUT_RDWR)
744 {
745 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
746 if (how == SHUT_RD)
747 return VPPCOM_OK;
748 }
749 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
750
liuyacan534468e2021-05-09 03:50:40 +0000751 if (PREDICT_TRUE (state == VCL_STATE_READY))
752 {
753 VDBG (1, "session %u [0x%llx]: sending shutdown...",
754 session->session_index, vpp_handle);
755
756 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000757 }
758
759 return VPPCOM_OK;
760}
761
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700762static int
763vppcom_session_disconnect (u32 session_handle)
764{
765 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700766 vcl_session_t *session, *listen_session;
767 vcl_session_state_t state;
768 u64 vpp_handle;
769
770 session = vcl_session_get_w_handle (wrk, session_handle);
771 if (!session)
772 return VPPCOM_EBADFD;
773
774 vpp_handle = session->vpp_handle;
775 state = session->session_state;
776
Florin Coras5ffb9642021-12-03 17:24:13 -0800777 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
778 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700779
780 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
781 {
782 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
783 return VPPCOM_EBADFD;
784 }
785
786 if (state == VCL_STATE_VPP_CLOSING)
787 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800788 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700789 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
790 session->session_index, vpp_handle);
791 }
792 else
793 {
794 /* Session doesn't have an event queue yet. Probably a non-blocking
795 * connect. Wait for the reply */
796 if (PREDICT_FALSE (!session->vpp_evt_q))
797 return VPPCOM_OK;
798
Florin Coras5ffb9642021-12-03 17:24:13 -0800799 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700800 session->session_index, vpp_handle);
801 vcl_send_session_disconnect (wrk, session);
802 }
803
804 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
805 {
806 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700807 if (listen_session)
808 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700809 }
810
811 return VPPCOM_OK;
812}
813
Florin Coras30e79c22019-01-02 19:31:22 -0800814static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700815vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
816{
817 session_cleanup_msg_t *msg;
818 vcl_session_t *session;
819
820 msg = (session_cleanup_msg_t *) data;
821 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
822 if (!session)
823 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800824 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700825 return;
826 }
827
Florin Coras36d49392020-04-24 23:00:11 +0000828 if (msg->type == SESSION_CLEANUP_TRANSPORT)
829 {
830 /* Transport was cleaned up before we confirmed close. Probably the
831 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700832 * Confirm close to make sure everything is cleaned up.
833 * Move to undetermined state to ensure that the session is not
834 * removed before both vpp and the app cleanup.
835 * - If the app closes first, the session is moved to CLOSED state
836 * and the session cleanup notification from vpp removes the
837 * session.
838 * - If vpp cleans up the session first, the session is moved to
839 * DETACHED state lower and subsequently the close from the app
840 * frees the session
841 */
842 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700843 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700844 vppcom_session_disconnect (vcl_session_handle (session));
845 session->session_state = VCL_STATE_UPDATED;
846 }
847 else if (session->session_state == VCL_STATE_DISCONNECT)
848 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800849 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700850 session->session_state = VCL_STATE_UPDATED;
851 }
Florin Coras36d49392020-04-24 23:00:11 +0000852 return;
853 }
854
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800855 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700856 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800857
858 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700859 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000860 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800861 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700862 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000863 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
864 return;
865 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800866
867 /* Session probably tracked with epoll, disconnect not yet handled and
868 * 1) both transport and session cleanup completed 2) app closed. Wait
869 * until message is drained to free the session.
870 * See @ref vcl_handle_mq_event */
871 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
872 {
873 session->flags |= VCL_SESSION_F_PENDING_FREE;
874 return;
875 }
876
Florin Coras9ace36d2019-10-28 13:14:17 -0700877 vcl_session_free (wrk, session);
878}
879
880static void
Florin Coras30e79c22019-01-02 19:31:22 -0800881vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
882{
883 session_req_worker_update_msg_t *msg;
884 vcl_session_t *s;
885
886 msg = (session_req_worker_update_msg_t *) data;
887 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
888 if (!s)
889 return;
890
891 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
892}
893
894static void
895vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
896{
897 session_worker_update_reply_msg_t *msg;
898 vcl_session_t *s;
899
900 msg = (session_worker_update_reply_msg_t *) data;
901 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
902 if (!s)
903 {
904 VDBG (0, "unknown handle 0x%llx", msg->handle);
905 return;
906 }
Florin Coras30e79c22019-01-02 19:31:22 -0800907
Florin Coras5f45e012019-01-23 09:21:30 -0800908 if (s->rx_fifo)
909 {
Florin Corasc547e912020-12-08 17:50:45 -0800910 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700911 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800912 {
913 VDBG (0, "failed to attach fifos for %u", s->session_index);
914 return;
915 }
Florin Coras5f45e012019-01-23 09:21:30 -0800916 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700917 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800918
Florin Coras30e79c22019-01-02 19:31:22 -0800919 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
920 s->vpp_handle, wrk->wrk_index);
921}
922
Florin Coras935ce752020-09-08 22:43:47 -0700923static int
924vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
925{
926
927 if (vcm->cfg.vpp_app_socket_api)
928 return vcl_sapi_recv_fds (wrk, fds, n_fds);
929
930 return vcl_bapi_recv_fds (wrk, fds, n_fds);
931}
932
Florin Corasc4c4cf52019-08-24 18:17:34 -0700933static void
934vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
935{
936 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
937 session_app_add_segment_msg_t *msg;
938 u64 segment_handle;
939 int fd = -1;
940
941 msg = (session_app_add_segment_msg_t *) data;
942
943 if (msg->fd_flags)
944 {
Florin Coras935ce752020-09-08 22:43:47 -0700945 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700946 seg_type = SSVM_SEGMENT_MEMFD;
947 }
948
949 segment_handle = msg->segment_handle;
950 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
951 {
952 clib_warning ("invalid segment handle");
953 return;
954 }
955
956 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
957 seg_type, fd))
958 {
959 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
960 return;
961 }
962
963 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
964 msg->segment_size);
965}
966
967static void
968vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
969{
970 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
971 vcl_segment_detach (msg->segment_handle);
972 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
973}
974
Florin Coras40c07ce2020-07-16 20:46:17 -0700975static void
976vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
977{
978 if (!vcm->wrk_rpc_fn)
979 return;
980
hanlina3a48962020-07-13 11:09:15 +0800981 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700982}
983
Florin Coras04ae8272021-04-12 19:55:37 -0700984static void
985vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
986{
987 session_transport_attr_reply_msg_t *mp;
988
989 if (!wrk->session_attr_op)
990 return;
991
992 mp = (session_transport_attr_reply_msg_t *) data;
993
994 wrk->session_attr_op_rv = mp->retval;
995 wrk->session_attr_op = 0;
996 wrk->session_attr_rv = mp->attr;
997}
998
Florin Coras86f04502018-09-12 16:08:01 -0700999static int
1000vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001001{
Florin Coras54693d22018-07-17 10:46:29 -07001002 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001003 session_connected_msg_t *connected_msg;
1004 session_reset_msg_t *reset_msg;
1005 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001006 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001007 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001008
1009 switch (e->event_type)
1010 {
Florin Coras653e43f2019-03-04 10:56:23 -08001011 case SESSION_IO_EVT_RX:
1012 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001013 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001014 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001015 break;
Florin Coras86f04502018-09-12 16:08:01 -07001016 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001017 break;
Florin Corasb242d312020-10-26 15:35:40 -07001018 case SESSION_CTRL_EVT_BOUND:
1019 /* We can only wait for only one listen so not postponed */
1020 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1021 break;
Florin Coras54693d22018-07-17 10:46:29 -07001022 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001023 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1024 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1025 {
1026 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1027 *ecpy = *e;
1028 ecpy->postponed = 1;
1029 ecpy->session_index = s->session_index;
1030 }
Florin Coras54693d22018-07-17 10:46:29 -07001031 break;
1032 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001033 connected_msg = (session_connected_msg_t *) e->data;
1034 sid = vcl_session_connected_handler (wrk, connected_msg);
1035 if (!(s = vcl_session_get (wrk, sid)))
1036 break;
1037 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1038 {
1039 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1040 *ecpy = *e;
1041 ecpy->postponed = 1;
1042 ecpy->session_index = s->session_index;
1043 }
Florin Coras54693d22018-07-17 10:46:29 -07001044 break;
1045 case SESSION_CTRL_EVT_DISCONNECTED:
1046 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001047 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1048 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001049 if (s->session_state == VCL_STATE_CLOSED)
1050 break;
Florin Corasb242d312020-10-26 15:35:40 -07001051 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1052 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001053 s->session_state = VCL_STATE_VPP_CLOSING;
1054 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1055 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1056 *ecpy = *e;
1057 ecpy->postponed = 1;
1058 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001059 break;
1060 }
1061 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001062 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001063 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1064 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001065 break;
1066 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001067 reset_msg = (session_reset_msg_t *) e->data;
1068 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1069 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001070 if (s->session_state == VCL_STATE_CLOSED)
1071 break;
Florin Corasb242d312020-10-26 15:35:40 -07001072 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1073 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001074 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1075 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001076 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001077 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1078 *ecpy = *e;
1079 ecpy->postponed = 1;
1080 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001081 break;
1082 }
Florin Coras134a9962018-08-28 11:32:04 -07001083 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001084 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001085 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1086 vcl_session_unlisten_reply_handler (wrk, e->data);
1087 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001088 case SESSION_CTRL_EVT_MIGRATED:
1089 vcl_session_migrated_handler (wrk, e->data);
1090 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001091 case SESSION_CTRL_EVT_CLEANUP:
1092 vcl_session_cleanup_handler (wrk, e->data);
1093 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001094 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1095 vcl_session_req_worker_update_handler (wrk, e->data);
1096 break;
1097 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1098 vcl_session_worker_update_reply_handler (wrk, e->data);
1099 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001100 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1101 vcl_session_app_add_segment_handler (wrk, e->data);
1102 break;
1103 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1104 vcl_session_app_del_segment_handler (wrk, e->data);
1105 break;
hanlina3a48962020-07-13 11:09:15 +08001106 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001107 vcl_worker_rpc_handler (wrk, e->data);
1108 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001109 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1110 vcl_session_transport_attr_reply_handler (wrk, e->data);
1111 break;
Florin Coras54693d22018-07-17 10:46:29 -07001112 default:
1113 clib_warning ("unhandled %u", e->event_type);
1114 }
1115 return VPPCOM_OK;
1116}
1117
Florin Coras30e79c22019-01-02 19:31:22 -08001118static int
Florin Coras697faea2018-06-27 17:10:49 -07001119vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001120 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001121 f64 wait_for_time)
1122{
Florin Coras134a9962018-08-28 11:32:04 -07001123 vcl_worker_t *wrk = vcl_worker_get_current ();
1124 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001125 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001126 svm_msg_q_msg_t msg;
1127 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001128
Florin Coras697faea2018-06-27 17:10:49 -07001129 do
Dave Wallace543852a2017-08-03 02:11:34 -04001130 {
Florin Coras134a9962018-08-28 11:32:04 -07001131 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001132 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001133 {
Florin Coras070453d2018-08-24 17:04:27 -07001134 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001135 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001136 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001137 {
Florin Coras697faea2018-06-27 17:10:49 -07001138 return VPPCOM_OK;
1139 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001140 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001141 {
Florin Coras697faea2018-06-27 17:10:49 -07001142 return VPPCOM_ECONNREFUSED;
1143 }
Florin Coras54693d22018-07-17 10:46:29 -07001144
Florin Coras134a9962018-08-28 11:32:04 -07001145 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001146 {
1147 usleep (100);
1148 continue;
1149 }
Florin Coras134a9962018-08-28 11:32:04 -07001150 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001151 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001152 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001153 }
Florin Coras134a9962018-08-28 11:32:04 -07001154 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001155
Florin Coras05ecfcc2018-12-12 18:19:39 -08001156 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001157 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001158 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001159
Florin Coras697faea2018-06-27 17:10:49 -07001160 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001161}
1162
Florin Coras30e79c22019-01-02 19:31:22 -08001163static void
1164vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1165{
Florin Coras288eaab2019-02-03 15:26:14 -08001166 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001167 vcl_session_t *s;
1168 u32 *sip;
1169
1170 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1171 return;
1172
1173 vec_foreach (sip, wrk->pending_session_wrk_updates)
1174 {
1175 s = vcl_session_get (wrk, *sip);
1176 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1177 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001178 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1179 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001180 s->session_state = state;
1181 }
1182 vec_reset_length (wrk->pending_session_wrk_updates);
1183}
1184
Florin Corasf9240dc2019-01-15 08:03:17 -08001185void
Florin Coras5398dfb2021-01-25 20:31:27 -08001186vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001187{
Florin Coras30e79c22019-01-02 19:31:22 -08001188 svm_msg_q_msg_t *msg;
1189 session_event_t *e;
1190 svm_msg_q_t *mq;
1191 int i;
1192
1193 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001194 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001195
1196 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1197 {
1198 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1199 e = svm_msg_q_msg_data (mq, msg);
1200 vcl_handle_mq_event (wrk, e);
1201 svm_msg_q_free_msg (mq, msg);
1202 }
1203 vec_reset_length (wrk->mq_msg_vector);
1204 vcl_handle_pending_wrk_updates (wrk);
1205}
1206
Florin Coras5398dfb2021-01-25 20:31:27 -08001207void
1208vcl_flush_mq_events (void)
1209{
1210 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1211}
1212
Florin Coras697faea2018-06-27 17:10:49 -07001213static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001214vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001215{
Florin Coras134a9962018-08-28 11:32:04 -07001216 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001217 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001218 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001219 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001220
Florin Corasab2f6db2018-08-31 14:31:41 -07001221 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001222 if (!session)
1223 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001224
Florin Corasaaff5ee2019-07-08 13:00:00 -07001225 /* Flush pending accept events, if any */
1226 while (clib_fifo_elts (session->accept_evts_fifo))
1227 {
1228 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1229 accepted_msg = &evt->accepted_msg;
1230 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1231 vcl_send_session_accepted_reply (session->vpp_evt_q,
1232 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001233 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001234 }
1235 clib_fifo_free (session->accept_evts_fifo);
1236
Florin Coras458089b2019-08-21 16:20:44 -07001237 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001238
Florin Coras32881932023-02-06 13:30:13 -08001239 VDBG (0, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001240 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001241 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001242
1243 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001244 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001245
Florin Coras070453d2018-08-24 17:04:27 -07001246 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001247}
1248
Florin Coras940f78f2018-11-30 12:11:20 -08001249/**
1250 * Handle app exit
1251 *
1252 * Notify vpp of the disconnect and mark the worker as free. If we're the
1253 * last worker, do a full cleanup otherwise, since we're probably a forked
1254 * child, avoid syscalls as much as possible. We might've lost privileges.
1255 */
1256void
1257vppcom_app_exit (void)
1258{
1259 if (!pool_elts (vcm->workers))
1260 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001261 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1262 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001263 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001264}
1265
Florin Coras935ce752020-09-08 22:43:47 -07001266static int
1267vcl_api_attach (void)
1268{
1269 if (vcm->cfg.vpp_app_socket_api)
1270 return vcl_sapi_attach ();
1271
1272 return vcl_bapi_attach ();
1273}
1274
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001275int
1276vcl_is_first_reattach_to_execute ()
1277{
1278 if (vcm->reattach_count == 0)
1279 return 1;
1280
1281 return 0;
1282}
1283
1284void
1285vcl_set_reattach_counter ()
1286{
1287 ++vcm->reattach_count;
1288
1289 if (vcm->reattach_count == vec_len (vcm->workers))
1290 vcm->reattach_count = 0;
1291}
1292
1293/**
1294 * Reattach vcl to vpp after it has previously been disconnected.
1295 *
1296 * The logic should be:
1297 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1298 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1299 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1300 * from `vppcom_worker_register` since they were already allocated.
1301 */
1302
Florin Coras935ce752020-09-08 22:43:47 -07001303static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001304vcl_api_retry_attach (vcl_worker_t *wrk)
1305{
1306 vcl_session_t *s;
1307
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001308 clib_spinlock_lock (&vcm->workers_lock);
1309 if (vcl_is_first_reattach_to_execute ())
1310 {
1311 if (vcl_api_attach ())
1312 {
1313 clib_spinlock_unlock (&vcm->workers_lock);
1314 return;
1315 }
1316 vcl_set_reattach_counter ();
1317 clib_spinlock_unlock (&vcm->workers_lock);
1318 }
1319 else
1320 {
1321 vcl_set_reattach_counter ();
1322 clib_spinlock_unlock (&vcm->workers_lock);
1323 vcl_worker_register_with_vpp ();
1324 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001325
1326 /* Treat listeners as configuration that needs to be re-added to vpp */
1327 pool_foreach (s, wrk->sessions)
1328 {
1329 if (s->flags & VCL_SESSION_F_IS_VEP)
1330 continue;
1331 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1332 vppcom_session_listen (vcl_session_handle (s), 10);
1333 else
1334 VDBG (0, "internal error: unexpected state %d", s->session_state);
1335 }
1336}
1337
1338static void
1339vcl_api_handle_disconnect (vcl_worker_t *wrk)
1340{
1341 wrk->api_client_handle = ~0;
1342 vcl_worker_detach_sessions (wrk);
1343}
1344
1345static void
Florin Coras935ce752020-09-08 22:43:47 -07001346vcl_api_detach (vcl_worker_t * wrk)
1347{
Florin Corasd04ea442022-03-30 16:08:25 -07001348 if (wrk->api_client_handle == ~0)
1349 return;
1350
Florin Coras935ce752020-09-08 22:43:47 -07001351 vcl_send_app_detach (wrk);
1352
1353 if (vcm->cfg.vpp_app_socket_api)
1354 return vcl_sapi_detach (wrk);
1355
1356 return vcl_bapi_disconnect_from_vpp ();
1357}
1358
Dave Wallace543852a2017-08-03 02:11:34 -04001359/*
1360 * VPPCOM Public API functions
1361 */
1362int
Florin Coras66ec4672020-06-15 07:59:40 -07001363vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001364{
Dave Wallace543852a2017-08-03 02:11:34 -04001365 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001366 int rv;
1367
Florin Coras47c40e22018-11-26 17:01:36 -08001368 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001369 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001370 VDBG (1, "already initialized");
1371 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001372 }
1373
Florin Coras47c40e22018-11-26 17:01:36 -08001374 vcm->is_init = 1;
1375 vppcom_cfg (&vcm->cfg);
1376 vcl_cfg = &vcm->cfg;
1377
1378 vcm->main_cpu = pthread_self ();
1379 vcm->main_pid = getpid ();
1380 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001381 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1382 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001383 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1384 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001385 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001386 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001387 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001388
1389 /* Allocate default worker */
1390 vcl_worker_alloc_and_init ();
1391
Florin Coras935ce752020-09-08 22:43:47 -07001392 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001393 {
1394 vppcom_app_destroy ();
1395 return rv;
1396 }
Florin Coras47c40e22018-11-26 17:01:36 -08001397
1398 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001399 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001400
1401 return VPPCOM_OK;
1402}
1403
1404void
1405vppcom_app_destroy (void)
1406{
Florin Corasdc0ded72020-04-30 02:59:55 +00001407 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001408 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001409
Florin Coras940f78f2018-11-30 12:11:20 -08001410 if (!pool_elts (vcm->workers))
1411 return;
1412
Florin Coras0d427d82018-06-27 03:24:07 -07001413 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001414
Florin Corasdc0ded72020-04-30 02:59:55 +00001415 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001416
Florin Corasce815de2020-04-16 18:47:27 +00001417 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001418 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001419 if (current_wrk != wrk)
1420 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001421 }
Florin Corasce815de2020-04-16 18:47:27 +00001422 /* *INDENT-ON* */
1423
Florin Coras935ce752020-09-08 22:43:47 -07001424 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001425 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001426 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001427
Florin Coras0d427d82018-06-27 03:24:07 -07001428 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001429
1430 /*
1431 * Free the heap and fix vcm
1432 */
1433 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001434 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001435
1436 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001437 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001438}
1439
1440int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001441vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001442{
Florin Coras134a9962018-08-28 11:32:04 -07001443 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001444 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001445
Florin Coras134a9962018-08-28 11:32:04 -07001446 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001447
Florin Coras7e12d942018-06-27 14:32:43 -07001448 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001449 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001450 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001451 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301452 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001453
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001454 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001455 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001456
Florin Coras7e12d942018-06-27 14:32:43 -07001457 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1458 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001459
Florin Coras5e062572019-03-14 19:07:51 -07001460 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001461
Florin Coras134a9962018-08-28 11:32:04 -07001462 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001463}
1464
Florin Corasfe286f72021-06-04 10:07:55 -07001465static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001466vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001467{
Florin Corasfa3884f2021-06-22 20:04:46 -07001468 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001469
Florin Corasc2a14172023-02-28 21:13:50 -08001470 ASSERT (s->vep.lt_next == VCL_INVALID_SESSION_INDEX);
1471
Florin Corasfa3884f2021-06-22 20:04:46 -07001472 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001473 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001474 wrk->ep_lt_current = s->session_index;
1475 s->vep.lt_next = s->session_index;
1476 s->vep.lt_prev = s->session_index;
1477 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001478 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001479
1480 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1481 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1482
1483 prev->vep.lt_next = s->session_index;
1484 s->vep.lt_prev = prev->session_index;
1485
1486 s->vep.lt_next = cur->session_index;
1487 cur->vep.lt_prev = s->session_index;
1488}
1489
1490static void
1491vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1492{
1493 vcl_session_t *prev, *next;
1494
Florin Corasc2a14172023-02-28 21:13:50 -08001495 ASSERT (s->vep.lt_next != VCL_INVALID_SESSION_INDEX);
1496
Florin Corasfa3884f2021-06-22 20:04:46 -07001497 if (s->vep.lt_next == s->session_index)
1498 {
1499 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1500 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001501 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfa3884f2021-06-22 20:04:46 -07001502 return;
1503 }
1504
1505 prev = vcl_session_get (wrk, s->vep.lt_prev);
1506 next = vcl_session_get (wrk, s->vep.lt_next);
1507
1508 prev->vep.lt_next = next->session_index;
1509 next->vep.lt_prev = prev->session_index;
1510
1511 if (s->session_index == wrk->ep_lt_current)
1512 wrk->ep_lt_current = s->vep.lt_next;
1513
1514 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasc2a14172023-02-28 21:13:50 -08001515 s->vep.lt_prev = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001516}
1517
Dave Wallace543852a2017-08-03 02:11:34 -04001518int
Florin Corasc127d5a2020-10-14 16:35:58 -07001519vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001520 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001521{
Florin Coras134a9962018-08-28 11:32:04 -07001522 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001523
Florin Coras6c3b2182020-10-19 18:36:48 -07001524 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001525
Florin Coras6c3b2182020-10-19 18:36:48 -07001526 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001527 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001528 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001529 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001530 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001531 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001532 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001533 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001534 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1535 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001536 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001537 }
Florin Corase4306b62020-10-28 12:51:10 -07001538 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001539 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001540
Florin Coras6c3b2182020-10-19 18:36:48 -07001541 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001542 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001543 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001544 if (rv < 0)
1545 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001546 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1547 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001548 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001549
Florin Coras9ace36d2019-10-28 13:14:17 -07001550 if (!do_disconnect)
1551 {
1552 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001553 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001554 goto cleanup;
1555 }
1556
Florin Coras6c3b2182020-10-19 18:36:48 -07001557 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001558 {
1559 rv = vppcom_session_unbind (sh);
1560 if (PREDICT_FALSE (rv < 0))
1561 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001562 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001563 vppcom_retval_str (rv));
1564 return rv;
1565 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001566 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001567 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001568 {
1569 rv = vppcom_session_disconnect (sh);
1570 if (PREDICT_FALSE (rv < 0))
1571 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001572 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001573 rv, vppcom_retval_str (rv));
1574 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001575 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001576 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001577 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001578 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001579 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001580 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001581 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001582
1583 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1584 goto free_session;
1585
1586 /* Disconnect/reset messages pending but vpp transport and session
1587 * cleanups already done. Free only after messages drained. */
1588 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001589 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001590
Florin Corasc127d5a2020-10-14 16:35:58 -07001591 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001592
Florin Coras9ace36d2019-10-28 13:14:17 -07001593 /* Session is removed only after vpp confirms the disconnect */
1594 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001595
Florin Corasf9240dc2019-01-15 08:03:17 -08001596cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001597 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001598free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001599 vcl_session_free (wrk, s);
1600 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001601
Dave Wallace543852a2017-08-03 02:11:34 -04001602 return rv;
1603}
1604
1605int
Florin Corasf9240dc2019-01-15 08:03:17 -08001606vppcom_session_close (uint32_t session_handle)
1607{
1608 vcl_worker_t *wrk = vcl_worker_get_current ();
1609 vcl_session_t *session;
1610
1611 session = vcl_session_get_w_handle (wrk, session_handle);
1612 if (!session)
1613 return VPPCOM_EBADFD;
1614 return vcl_session_cleanup (wrk, session, session_handle,
1615 1 /* do_disconnect */ );
1616}
1617
1618int
Florin Coras134a9962018-08-28 11:32:04 -07001619vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001620{
Florin Coras134a9962018-08-28 11:32:04 -07001621 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001622 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001623
1624 if (!ep || !ep->ip)
1625 return VPPCOM_EINVAL;
1626
Florin Coras134a9962018-08-28 11:32:04 -07001627 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001628 if (!session)
1629 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001630
Florin Coras6c3b2182020-10-19 18:36:48 -07001631 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001632 {
Florin Coras5e062572019-03-14 19:07:51 -07001633 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1634 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001635 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001636 }
1637
Florin Coras7e12d942018-06-27 14:32:43 -07001638 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001639 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001640 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1641 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001642 else
Dave Barach178cf492018-11-13 16:34:13 -05001643 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1644 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001645 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001646
Florin Coras60687192021-11-29 21:00:47 -08001647 VDBG (0,
1648 "session %u handle %u: binding to local %s address %U port %u, "
1649 "proto %s",
1650 session->session_index, session_handle,
1651 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1652 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001653 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1654 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001655 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001656 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001657
1658 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001659 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001660
Florin Coras070453d2018-08-24 17:04:27 -07001661 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001662}
1663
1664int
Florin Coras134a9962018-08-28 11:32:04 -07001665vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001666{
Florin Coras134a9962018-08-28 11:32:04 -07001667 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001668 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001669 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001670 int rv;
1671
Florin Coras134a9962018-08-28 11:32:04 -07001672 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001673 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001674 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001675
Dave Wallaceee45d412017-11-24 21:44:06 -05001676 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001677 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001678 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001679 VDBG (0, "session %u [0x%llx]: already in listen state!",
1680 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001681 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001682 }
1683
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001684 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001685
Florin Coras070453d2018-08-24 17:04:27 -07001686 /*
1687 * Send listen request to vpp and wait for reply
1688 */
Florin Coras458089b2019-08-21 16:20:44 -07001689 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001690 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001691 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001692 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001693
Florin Coras070453d2018-08-24 17:04:27 -07001694 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001695 {
Florin Coras134a9962018-08-28 11:32:04 -07001696 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001697 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1698 listen_sh, listen_session->vpp_handle, rv,
1699 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001700 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001701 }
1702
Florin Coras070453d2018-08-24 17:04:27 -07001703 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001704}
1705
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001706int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001707vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1708{
1709 if (!strcmp (proto_str, "TCP"))
1710 *proto = VPPCOM_PROTO_TCP;
1711 else if (!strcmp (proto_str, "tcp"))
1712 *proto = VPPCOM_PROTO_TCP;
1713 else if (!strcmp (proto_str, "UDP"))
1714 *proto = VPPCOM_PROTO_UDP;
1715 else if (!strcmp (proto_str, "udp"))
1716 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001717 else if (!strcmp (proto_str, "TLS"))
1718 *proto = VPPCOM_PROTO_TLS;
1719 else if (!strcmp (proto_str, "tls"))
1720 *proto = VPPCOM_PROTO_TLS;
1721 else if (!strcmp (proto_str, "QUIC"))
1722 *proto = VPPCOM_PROTO_QUIC;
1723 else if (!strcmp (proto_str, "quic"))
1724 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001725 else if (!strcmp (proto_str, "DTLS"))
1726 *proto = VPPCOM_PROTO_DTLS;
1727 else if (!strcmp (proto_str, "dtls"))
1728 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001729 else if (!strcmp (proto_str, "SRTP"))
1730 *proto = VPPCOM_PROTO_SRTP;
1731 else if (!strcmp (proto_str, "srtp"))
1732 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001733 else
1734 return 1;
1735 return 0;
1736}
1737
1738int
Florin Coras32881932023-02-06 13:30:13 -08001739vppcom_session_accept (uint32_t ls_handle, vppcom_endpt_t *ep, uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001740{
Florin Coras32881932023-02-06 13:30:13 -08001741 u32 client_session_index = ~0, ls_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001742 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001743 session_accepted_msg_t accepted_msg;
Florin Coras32881932023-02-06 13:30:13 -08001744 vcl_session_t *ls, *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001745 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001746 u8 is_nonblocking;
Dave Wallace543852a2017-08-03 02:11:34 -04001747
Florin Coras5398dfb2021-01-25 20:31:27 -08001748again:
1749
Florin Coras32881932023-02-06 13:30:13 -08001750 ls = vcl_session_get_w_handle (wrk, ls_handle);
1751 if (!ls)
Florin Coras070453d2018-08-24 17:04:27 -07001752 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001753
Florin Coras32881932023-02-06 13:30:13 -08001754 if ((ls->session_state != VCL_STATE_LISTEN) &&
1755 (ls->session_state != VCL_STATE_LISTEN_NO_MQ) &&
1756 (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001757 {
Florin Coras32881932023-02-06 13:30:13 -08001758 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state (%s)",
1759 ls->vpp_handle, vcl_session_state_str (ls->session_state));
1760 return VPPCOM_EBADFD;
1761 }
1762
1763 ls_index = ls->session_index;
1764
1765 if (clib_fifo_elts (ls->accept_evts_fifo))
1766 {
1767 clib_fifo_sub2 (ls->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001768 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001769 accepted_msg = evt->accepted_msg;
1770 goto handle;
1771 }
1772
Florin Coras32881932023-02-06 13:30:13 -08001773 is_nonblocking = vcl_session_has_attr (ls, VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001774 while (1)
1775 {
Carl Smith592a9092019-11-12 14:57:37 +13001776 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1777 return VPPCOM_EAGAIN;
1778
Florin Coras5398dfb2021-01-25 20:31:27 -08001779 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1780 vcl_worker_flush_mq_events (wrk);
1781 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001782 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001783
Florin Coras54693d22018-07-17 10:46:29 -07001784handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001785
Florin Coras32881932023-02-06 13:30:13 -08001786 client_session_index =
1787 vcl_session_accepted_handler (wrk, &accepted_msg, ls_index);
Florin Coras00cca802019-06-06 09:38:44 -07001788 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1789 return VPPCOM_ECONNABORTED;
1790
Florin Coras32881932023-02-06 13:30:13 -08001791 ls = vcl_session_get (wrk, ls_index);
Florin Coras134a9962018-08-28 11:32:04 -07001792 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001793
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001794 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001795 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001796
Florin Coras32881932023-02-06 13:30:13 -08001797 VDBG (1,
1798 "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1799 " flags %d, is_nonblocking %u",
1800 ls->session_index, ls->vpp_handle, client_session_index,
Florin Coras5e062572019-03-14 19:07:51 -07001801 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001802 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001803
Dave Wallace048b1d62018-01-03 22:24:41 -05001804 if (ep)
1805 {
Florin Coras7e12d942018-06-27 14:32:43 -07001806 ep->is_ip4 = client_session->transport.is_ip4;
1807 ep->port = client_session->transport.rmt_port;
1808 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001809 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1810 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001811 else
Dave Barach178cf492018-11-13 16:34:13 -05001812 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1813 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001814 }
Dave Wallace60caa062017-11-10 17:07:13 -05001815
Florin Coras60687192021-11-29 21:00:47 -08001816 VDBG (0,
1817 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1818 "local: %U:%u",
Florin Coras32881932023-02-06 13:30:13 -08001819 ls_handle, ls->vpp_handle, client_session_index,
1820 client_session->vpp_handle, vcl_format_ip46_address,
1821 &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001822 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001823 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001824 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001825 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001826 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras32881932023-02-06 13:30:13 -08001827 vcl_evt (VCL_EVT_ACCEPT, client_session, ls, client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001828
Florin Coras3c7d4f92018-12-14 11:28:43 -08001829 /*
1830 * Session might have been closed already
1831 */
1832 if (accept_flags)
1833 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001834 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001835 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001836 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001837 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001838 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001839 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001840}
1841
1842int
Florin Coras134a9962018-08-28 11:32:04 -07001843vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001844{
Florin Coras134a9962018-08-28 11:32:04 -07001845 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001846 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001847 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001848 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001849
Florin Coras134a9962018-08-28 11:32:04 -07001850 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001851 if (!session)
1852 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001853 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001854
Florin Coras6c3b2182020-10-19 18:36:48 -07001855 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001856 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001857 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001858 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001859 }
1860
Florin Corasc127d5a2020-10-14 16:35:58 -07001861 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001862 {
Florin Coras60687192021-11-29 21:00:47 -08001863 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001864 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1865 " state (%s)",
1866 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001867 vcl_format_ip46_address, &session->transport.rmt_ip,
1868 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001869 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001870 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001871 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001872 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001873 }
1874
Florin Coras0a1e1832020-03-29 18:54:04 +00001875 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001876 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001877 {
1878 if (session->session_type != VPPCOM_PROTO_UDP)
1879 return VPPCOM_EINVAL;
1880 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001881 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001882 }
1883
Florin Coras7e12d942018-06-27 14:32:43 -07001884 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001885 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001886 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001887 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001888 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001889
Florin Coras5ffb9642021-12-03 17:24:13 -08001890 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1891 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001892 &session->transport.rmt_ip,
1893 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001894 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001895 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001896
Florin Coras458089b2019-08-21 16:20:44 -07001897 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001898
Florin Corasac422d62020-10-19 20:51:36 -07001899 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001900 {
fanyfa49d59d2020-10-13 17:07:16 +08001901 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001902 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001903 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001904 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001905 return VPPCOM_EINPROGRESS;
1906 }
Florin Coras57c88932019-08-29 12:03:17 -07001907
1908 /*
1909 * Wait for reply from vpp if blocking
1910 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001911 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001912 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001913
Florin Coras134a9962018-08-28 11:32:04 -07001914 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001915 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1916 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001917
Dave Wallace4878cbe2017-11-21 03:45:09 -05001918 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001919}
1920
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001921int
1922vppcom_session_stream_connect (uint32_t session_handle,
1923 uint32_t parent_session_handle)
1924{
1925 vcl_worker_t *wrk = vcl_worker_get_current ();
1926 vcl_session_t *session, *parent_session;
1927 u32 session_index, parent_session_index;
1928 int rv;
1929
1930 session = vcl_session_get_w_handle (wrk, session_handle);
1931 if (!session)
1932 return VPPCOM_EBADFD;
1933 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1934 if (!parent_session)
1935 return VPPCOM_EBADFD;
1936
1937 session_index = session->session_index;
1938 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001939 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001940 {
1941 VDBG (0, "ERROR: cannot connect epoll session %u!",
1942 session->session_index);
1943 return VPPCOM_EBADFD;
1944 }
1945
Florin Corasc127d5a2020-10-14 16:35:58 -07001946 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001947 {
Florin Coras60687192021-11-29 21:00:47 -08001948 VDBG (0,
1949 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001950 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001951 session_handle, session->vpp_handle, parent_session_handle,
1952 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001953 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001954 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001955 return VPPCOM_OK;
1956 }
1957
1958 /* Connect to quic session specifics */
1959 session->transport.is_ip4 = parent_session->transport.is_ip4;
1960 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1961 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001962 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001963
1964 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1965 session_handle, parent_session_handle, parent_session->vpp_handle);
1966
1967 /*
1968 * Send connect request and wait for reply from vpp
1969 */
Florin Coras458089b2019-08-21 16:20:44 -07001970 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001971 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001972 vcm->cfg.session_timeout);
1973
1974 session->listener_index = parent_session_index;
1975 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001976 if (parent_session)
1977 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001978
1979 session = vcl_session_get (wrk, session_index);
1980 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1981 session->vpp_handle, rv ? "failed" : "succeeded");
1982
1983 return rv;
1984}
1985
Steven58f464e2017-10-25 12:33:12 -07001986static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001987vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001988 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001989{
Florin Coras134a9962018-08-28 11:32:04 -07001990 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001991 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001992 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001993 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001994 session_event_t *e;
1995 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001996 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001997
Florin Coras070453d2018-08-24 17:04:27 -07001998 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001999 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002000
Florin Coras134a9962018-08-28 11:32:04 -07002001 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002002 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07002003 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002004
Florin Coras6d0106e2019-01-29 20:11:58 -08002005 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002006 {
Florin Coras5e062572019-03-14 19:07:51 -07002007 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002008 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002009 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002010 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002011 }
2012
liuyacan55c952e2021-06-13 14:54:55 +08002013 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2014 {
2015 /* Vpp would ack the incoming data and enqueue it for reading.
2016 * So even if SHUT_RD is set, we can still read() the data if
2017 * the session is ready.
2018 */
2019 if (!vcl_session_read_ready (s))
2020 {
2021 return 0;
2022 }
2023 }
2024
Florin Corasac422d62020-10-19 20:51:36 -07002025 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002026 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002027 mq = wrk->app_event_queue;
2028 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002029 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002030
Sirshak Das28aa5392019-02-05 01:33:33 -06002031 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002032 {
Florin Coras54693d22018-07-17 10:46:29 -07002033 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002034 {
Yu Pingb2955352019-12-04 06:49:04 +08002035 if (vcl_session_is_closing (s))
2036 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002037 if (is_ct)
2038 svm_fifo_unset_event (s->rx_fifo);
2039 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002040 return VPPCOM_EWOULDBLOCK;
2041 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002042 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002043 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002044 if (vcl_session_is_closing (s))
2045 return vcl_session_closing_error (s);
2046
Florin Corasd6894562020-10-28 00:37:15 -07002047 if (is_ct)
2048 svm_fifo_unset_event (s->rx_fifo);
2049 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002050
Florin Coras5398dfb2021-01-25 20:31:27 -08002051 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2052 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002053 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002054 }
Florin Coras54693d22018-07-17 10:46:29 -07002055
Florin Coras4a2c7942020-09-10 12:27:14 -07002056read_again:
2057
Florin Coras460dce62018-07-27 05:45:06 -07002058 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002059 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002060 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002061 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2062
2063 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002064
2065 if (peek)
2066 return rv;
2067
Florin Coras4a2c7942020-09-10 12:27:14 -07002068 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002069
Sirshak Das28aa5392019-02-05 01:33:33 -06002070 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002071 {
Florin Corasd6894562020-10-28 00:37:15 -07002072 if (is_ct)
2073 svm_fifo_unset_event (s->rx_fifo);
2074 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002075 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002076 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002077 {
Florin Corasc34118b2020-08-12 18:58:25 -07002078 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2079 e->event_type = SESSION_IO_EVT_RX;
2080 e->session_index = s->session_index;
2081 }
2082 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002083 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002084 {
2085 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002086 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002087 buf += rv;
2088 n -= rv;
2089 goto read_again;
2090 }
Florin Coras41c9e042018-09-11 00:10:41 -07002091
Florin Coras17ec5772020-08-12 20:46:29 -07002092 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002093 {
Florin Coras17ec5772020-08-12 20:46:29 -07002094 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002095 app_send_io_evt_to_vpp (s->vpp_evt_q,
2096 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002097 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002098 }
2099
Florin Coras5e062572019-03-14 19:07:51 -07002100 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2101 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002102
Florin Coras54693d22018-07-17 10:46:29 -07002103 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002104}
2105
Steven58f464e2017-10-25 12:33:12 -07002106int
Florin Coras134a9962018-08-28 11:32:04 -07002107vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002108{
Florin Coras134a9962018-08-28 11:32:04 -07002109 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002110}
2111
2112static int
Florin Coras134a9962018-08-28 11:32:04 -07002113vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002114{
Florin Coras134a9962018-08-28 11:32:04 -07002115 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002116}
2117
Florin Coras2cba8532018-09-11 16:33:36 -07002118int
2119vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002120 vppcom_data_segment_t * ds, uint32_t n_segments,
2121 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002122{
2123 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002124 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002125 vcl_session_t *s = 0;
2126 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002127 svm_msg_q_t *mq;
2128 u8 is_ct;
2129
2130 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002131 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002132 return VPPCOM_EBADFD;
2133
Florin Coras6d0106e2019-01-29 20:11:58 -08002134 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2135 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002136
Florin Corasac422d62020-10-19 20:51:36 -07002137 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002138 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002139 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002140 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002141 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002142
Sirshak Das28aa5392019-02-05 01:33:33 -06002143 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002144 {
2145 if (is_nonblocking)
2146 {
Florin Coras62877022020-10-28 21:22:04 -07002147 if (is_ct)
2148 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002149 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002150 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002151 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002152 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002153 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002154 if (vcl_session_is_closing (s))
2155 return vcl_session_closing_error (s);
2156
Florin Coras62877022020-10-28 21:22:04 -07002157 if (is_ct)
2158 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002159 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002160
Florin Coras5398dfb2021-01-25 20:31:27 -08002161 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2162 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002163 }
2164 }
2165
Florin Corasd1cc38d2020-10-11 11:05:04 -07002166 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002167 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002168 if (n_read < 0)
2169 return VPPCOM_EAGAIN;
2170
2171 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2172 {
Florin Coras62877022020-10-28 21:22:04 -07002173 if (is_ct)
2174 svm_fifo_unset_event (s->rx_fifo);
2175 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002176 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002177 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002178 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002179 {
2180 session_event_t *e;
2181 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2182 e->event_type = SESSION_IO_EVT_RX;
2183 e->session_index = s->session_index;
2184 }
2185 }
2186
2187 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002188 return n_read;
2189}
2190
2191void
Florin Corasd68faf82020-09-25 15:18:13 -07002192vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002193{
2194 vcl_worker_t *wrk = vcl_worker_get_current ();
2195 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002196 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002197
2198 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002199 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002200 return;
2201
Florin Coras62877022020-10-28 21:22:04 -07002202 is_ct = vcl_session_is_ct (s);
2203 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002204
Florin Coras8cc93212021-09-10 11:37:12 -07002205 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002206 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002207}
2208
Florin Coras7a2abce2020-04-05 19:25:44 +00002209always_inline u8
2210vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002211{
Florin Coras7a2abce2020-04-05 19:25:44 +00002212 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2213 if (is_dgram)
2214 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2215 else
2216 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002217}
2218
2219always_inline int
Dou Chao243a0432022-11-29 19:41:34 +08002220vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002221 size_t n, u8 is_flush, u8 is_dgram)
Florin Coras7a2abce2020-04-05 19:25:44 +00002222{
Florin Coras6d0106e2019-01-29 20:11:58 -08002223 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002224 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002225 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002226 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002227 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002228
Florin Coras0b0d28e2021-06-04 17:31:53 -07002229 /* Accept zero length writes but just return */
2230 if (PREDICT_FALSE (!n))
2231 return VPPCOM_OK;
2232
2233 if (PREDICT_FALSE (!buf))
2234 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002235
Florin Coras6c3b2182020-10-19 18:36:48 -07002236 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002237 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002238 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2239 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002240 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002241 }
2242
liuyacan55c952e2021-06-13 14:54:55 +08002243 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002244 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002245 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2246 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002247 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002248 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002249 }
2250
liuyacan55c952e2021-06-13 14:54:55 +08002251 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2252 {
2253 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2254 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002255 vcl_session_state_str (s->session_state));
liuyacan55c952e2021-06-13 14:54:55 +08002256 return VPPCOM_EPIPE;
2257 }
2258
Florin Coras0e88e852018-09-17 22:09:02 -07002259 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002260 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002261 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002262
Florin Coras653e43f2019-03-04 10:56:23 -08002263 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002264 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002265 {
Florin Coras54693d22018-07-17 10:46:29 -07002266 if (is_nonblocking)
2267 {
Florin Coras070453d2018-08-24 17:04:27 -07002268 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002269 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002270 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002271 {
Florin Coras2d379d82019-06-28 12:45:12 -07002272 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002273 if (vcl_session_is_closing (s))
2274 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002275
Florin Coras5398dfb2021-01-25 20:31:27 -08002276 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2277 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002278 }
Dave Wallace543852a2017-08-03 02:11:34 -04002279 }
Dave Wallace543852a2017-08-03 02:11:34 -04002280
Florin Coras653e43f2019-03-04 10:56:23 -08002281 et = SESSION_IO_EVT_TX;
2282 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002283 et = SESSION_IO_EVT_TX_FLUSH;
2284
Florin Coras7a2abce2020-04-05 19:25:44 +00002285 if (is_dgram)
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002286 {
2287 et = vcl_session_dgram_tx_evt (s, et);
2288 n_write =
2289 app_send_dgram_raw_gso (tx_fifo, &s->transport, s->vpp_evt_q, buf, n,
2290 s->gso_size, et, 0 /* do_evt */, SVM_Q_WAIT);
2291 }
Florin Coras460dce62018-07-27 05:45:06 -07002292 else
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002293 {
2294 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
2295 0 /* do_evt */, SVM_Q_WAIT);
2296 }
Florin Coras653e43f2019-03-04 10:56:23 -08002297
Florin Coras14ed6df2019-03-06 21:13:42 -08002298 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002299 app_send_io_evt_to_vpp (
2300 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002301
Florin Coras56230092020-09-02 20:52:58 -07002302 /* The underlying fifo segment can run out of memory */
2303 if (PREDICT_FALSE (n_write < 0))
2304 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002305
Florin Coras6d0106e2019-01-29 20:11:58 -08002306 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2307 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002308
Florin Coras54693d22018-07-17 10:46:29 -07002309 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002310}
2311
Florin Coras42ceddb2018-12-12 10:56:01 -08002312int
2313vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2314{
Florin Coras7a2abce2020-04-05 19:25:44 +00002315 vcl_worker_t *wrk = vcl_worker_get_current ();
2316 vcl_session_t *s;
2317
2318 s = vcl_session_get_w_handle (wrk, session_handle);
2319 if (PREDICT_FALSE (!s))
2320 return VPPCOM_EBADFD;
2321
Florin Coraseff5f7a2023-02-07 17:36:17 -08002322 return vppcom_session_write_inline (wrk, s, buf, n, 0 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002323 s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002324}
2325
Florin Corasb0f662f2018-12-27 14:51:46 -08002326int
2327vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2328{
Florin Coras7a2abce2020-04-05 19:25:44 +00002329 vcl_worker_t *wrk = vcl_worker_get_current ();
2330 vcl_session_t *s;
2331
2332 s = vcl_session_get_w_handle (wrk, session_handle);
2333 if (PREDICT_FALSE (!s))
2334 return VPPCOM_EBADFD;
2335
Florin Coraseff5f7a2023-02-07 17:36:17 -08002336 return vppcom_session_write_inline (wrk, s, buf, n, 1 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002337 s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002338}
2339
Florin Corasc0737e92019-03-04 14:19:39 -08002340#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002341if (PREDICT_FALSE (!_s->rx_fifo)) \
2342 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002343if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2344 { \
2345 if (!vcl_session_is_ct (_s)) \
2346 { \
2347 svm_fifo_unset_event (_s->rx_fifo); \
2348 if (svm_fifo_is_empty (_s->rx_fifo)) \
2349 break; \
2350 } \
2351 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2352 { \
Florin Coras2f630182020-08-13 00:17:51 -07002353 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002354 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2355 break; \
2356 } \
2357 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002358
Florin Coras86f04502018-09-12 16:08:01 -07002359static void
2360vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2361 unsigned long n_bits, unsigned long *read_map,
2362 unsigned long *write_map,
2363 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002364{
2365 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002366 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002367 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002368 u32 sid;
2369
2370 switch (e->event_type)
2371 {
Florin Corasc0737e92019-03-04 14:19:39 -08002372 case SESSION_IO_EVT_RX:
2373 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002374 s = vcl_session_get (wrk, sid);
2375 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002376 break;
Florin Corasb242d312020-10-26 15:35:40 -07002377 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002378 if (sid < n_bits && read_map)
2379 {
David Johnsond9818dd2018-12-14 14:53:41 -05002380 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002381 *bits_set += 1;
2382 }
2383 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002384 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002385 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002386 s = vcl_session_get (wrk, sid);
2387 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002388 break;
2389 if (sid < n_bits && write_map)
2390 {
David Johnsond9818dd2018-12-14 14:53:41 -05002391 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002392 *bits_set += 1;
2393 }
2394 break;
Florin Coras86f04502018-09-12 16:08:01 -07002395 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002396 if (!e->postponed)
2397 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2398 else
2399 s = vcl_session_get (wrk, e->session_index);
2400 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002401 break;
Florin Corasb242d312020-10-26 15:35:40 -07002402 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002403 if (sid < n_bits && read_map)
2404 {
David Johnsond9818dd2018-12-14 14:53:41 -05002405 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002406 *bits_set += 1;
2407 }
2408 break;
2409 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002410 if (!e->postponed)
2411 {
2412 connected_msg = (session_connected_msg_t *) e->data;
2413 sid = vcl_session_connected_handler (wrk, connected_msg);
2414 }
2415 else
2416 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002417 if (sid == VCL_INVALID_SESSION_INDEX)
2418 break;
Florin Coras66c675e2023-03-09 16:43:02 -08002419 if (!(sid < n_bits && write_map))
2420 break;
2421 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2422 *bits_set += 1;
2423 s = vcl_session_get (wrk, sid);
Florin Coras66c675e2023-03-09 16:43:02 -08002424 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07002425 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras86f04502018-09-12 16:08:01 -07002426 break;
2427 case SESSION_CTRL_EVT_DISCONNECTED:
2428 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002429 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2430 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002431 break;
Florin Corasb242d312020-10-26 15:35:40 -07002432 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002433 if (sid < n_bits && except_map)
2434 {
David Johnsond9818dd2018-12-14 14:53:41 -05002435 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002436 *bits_set += 1;
2437 }
2438 break;
2439 case SESSION_CTRL_EVT_RESET:
2440 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2441 if (sid < n_bits && except_map)
2442 {
David Johnsond9818dd2018-12-14 14:53:41 -05002443 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002444 *bits_set += 1;
2445 }
2446 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002447 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2448 vcl_session_unlisten_reply_handler (wrk, e->data);
2449 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002450 case SESSION_CTRL_EVT_MIGRATED:
2451 vcl_session_migrated_handler (wrk, e->data);
2452 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002453 case SESSION_CTRL_EVT_CLEANUP:
2454 vcl_session_cleanup_handler (wrk, e->data);
2455 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002456 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2457 vcl_session_worker_update_reply_handler (wrk, e->data);
2458 break;
2459 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2460 vcl_session_req_worker_update_handler (wrk, e->data);
2461 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002462 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2463 vcl_session_app_add_segment_handler (wrk, e->data);
2464 break;
2465 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2466 vcl_session_app_del_segment_handler (wrk, e->data);
2467 break;
hanlina3a48962020-07-13 11:09:15 +08002468 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002469 vcl_worker_rpc_handler (wrk, e->data);
2470 break;
Florin Coras86f04502018-09-12 16:08:01 -07002471 default:
2472 clib_warning ("unhandled: %u", e->event_type);
2473 break;
2474 }
2475}
2476
2477static int
2478vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2479 unsigned long n_bits, unsigned long *read_map,
2480 unsigned long *write_map, unsigned long *except_map,
2481 double time_to_wait, u32 * bits_set)
2482{
Florin Coras99368312018-08-02 10:45:44 -07002483 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002484 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002485 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002486
Florin Coras54693d22018-07-17 10:46:29 -07002487 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002488 {
Florin Coras54693d22018-07-17 10:46:29 -07002489 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002490 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002491
Florin Coras54693d22018-07-17 10:46:29 -07002492 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002493 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002494 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002495 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002496 else
2497 {
2498 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002499 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002500 }
2501 }
Florin Corase003a1b2019-06-05 10:47:16 -07002502 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002503
Florin Coras134a9962018-08-28 11:32:04 -07002504 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002505 {
Florin Coras134a9962018-08-28 11:32:04 -07002506 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002507 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002508 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2509 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002510 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002511 }
Florin Coras134a9962018-08-28 11:32:04 -07002512 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002513 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002514 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002515}
2516
Florin Coras99368312018-08-02 10:45:44 -07002517static int
Florin Coras294afe22019-01-07 17:49:17 -08002518vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2519 vcl_si_set * read_map, vcl_si_set * write_map,
2520 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002521 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002522{
Florin Coras14ed6df2019-03-06 21:13:42 -08002523 double wait = 0, start = 0;
2524
2525 if (!*bits_set)
2526 {
2527 wait = time_to_wait;
2528 start = clib_time_now (&wrk->clib_time);
2529 }
2530
2531 do
2532 {
2533 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2534 write_map, except_map, wait, bits_set);
2535 if (*bits_set)
2536 return *bits_set;
2537 if (wait == -1)
2538 continue;
2539
2540 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2541 }
2542 while (wait > 0);
2543
2544 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002545}
2546
2547static int
Florin Coras294afe22019-01-07 17:49:17 -08002548vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2549 vcl_si_set * read_map, vcl_si_set * write_map,
2550 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002551 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002552{
2553 vcl_mq_evt_conn_t *mqc;
2554 int __clib_unused n_read;
2555 int n_mq_evts, i;
2556 u64 buf;
2557
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002558 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2559 {
2560 vcl_api_retry_attach (wrk);
2561 return 0;
2562 }
2563
Florin Coras134a9962018-08-28 11:32:04 -07002564 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2565 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2566 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002567 for (i = 0; i < n_mq_evts; i++)
2568 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002569 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2570 {
2571 vcl_api_handle_disconnect (wrk);
2572 continue;
2573 }
2574
Florin Coras134a9962018-08-28 11:32:04 -07002575 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002576 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002577 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002578 except_map, 0, bits_set);
2579 }
2580
2581 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2582}
2583
Dave Wallace543852a2017-08-03 02:11:34 -04002584int
Florin Coras294afe22019-01-07 17:49:17 -08002585vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2586 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002587{
Florin Coras54693d22018-07-17 10:46:29 -07002588 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002589 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002590 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002591 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002592
Dave Wallace7876d392017-10-19 03:53:57 -04002593 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002594 {
Florin Coras134a9962018-08-28 11:32:04 -07002595 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002596 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002597 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2598 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002599 }
Dave Wallace7876d392017-10-19 03:53:57 -04002600 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002601 {
Florin Coras134a9962018-08-28 11:32:04 -07002602 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002603 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002604 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2605 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002606 }
Dave Wallace7876d392017-10-19 03:53:57 -04002607 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002608 {
Florin Coras134a9962018-08-28 11:32:04 -07002609 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002610 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002611 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2612 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002613 }
2614
Florin Coras54693d22018-07-17 10:46:29 -07002615 if (!n_bits)
2616 return 0;
2617
2618 if (!write_map)
2619 goto check_rd;
2620
Florin Coras096a1d52021-01-27 15:33:51 -08002621 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2622 {
2623 if (!(s = vcl_session_get (wrk, sid)))
2624 {
2625 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2626 bits_set++;
2627 continue;
2628 }
Florin Coras54693d22018-07-17 10:46:29 -07002629
Florin Coras096a1d52021-01-27 15:33:51 -08002630 if (vcl_session_write_ready (s))
2631 {
2632 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2633 bits_set++;
2634 }
Florin Coras607eb202023-05-29 16:19:38 -07002635 else
Florin Coras096a1d52021-01-27 15:33:51 -08002636 {
Florin Coras607eb202023-05-29 16:19:38 -07002637 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras096a1d52021-01-27 15:33:51 -08002638 }
2639 }
Florin Coras54693d22018-07-17 10:46:29 -07002640
2641check_rd:
2642 if (!read_map)
2643 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002644
Florin Coras096a1d52021-01-27 15:33:51 -08002645 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2646 {
2647 if (!(s = vcl_session_get (wrk, sid)))
2648 {
2649 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2650 bits_set++;
2651 continue;
2652 }
Florin Coras54693d22018-07-17 10:46:29 -07002653
Florin Coras096a1d52021-01-27 15:33:51 -08002654 if (vcl_session_read_ready (s))
2655 {
2656 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2657 bits_set++;
2658 }
2659 }
Florin Coras54693d22018-07-17 10:46:29 -07002660
2661check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002662
Florin Coras86f04502018-09-12 16:08:01 -07002663 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2664 {
2665 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2666 read_map, write_map, except_map, &bits_set);
2667 }
2668 vec_reset_length (wrk->unhandled_evts_vector);
2669
Florin Coras99368312018-08-02 10:45:44 -07002670 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002671 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002672 time_to_wait, &bits_set);
2673 else
Florin Coras134a9962018-08-28 11:32:04 -07002674 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002675 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002676
Dave Wallace543852a2017-08-03 02:11:34 -04002677 return (bits_set);
2678}
2679
Dave Wallacef7f809c2017-10-03 01:48:42 -04002680static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002681vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002683 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002684 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002685 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686
Florin Corasaeb7c1c2023-03-10 10:22:21 -08002687 if (VPPCOM_DEBUG <= 3)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688 return;
2689
Florin Coras6c3b2182020-10-19 18:36:48 -07002690 s = vcl_session_get_w_handle (wrk, vep_handle);
2691 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002692 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002693 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002694 goto done;
2695 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002696 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002698 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002699 goto done;
2700 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002701 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002702 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002703 "{\n"
2704 " is_vep = %u\n"
2705 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002706 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002707 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2708 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002709
Florin Coras7e5e62b2019-07-30 14:08:23 -07002710 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002711 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002712 s = vcl_session_get_w_handle (wrk, sh);
2713 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002714 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002715 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002716 goto done;
2717 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002718 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002719 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002720 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002721 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002722 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002723 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002724 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002725 goto done;
2726 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002727 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002728 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2729 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002730 sh, s->vep.vep_sh, vep_handle);
2731 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002732 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002733 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002734 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002735 " next_sh = 0x%x (%u)\n"
2736 " prev_sh = 0x%x (%u)\n"
2737 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002738 " ev.events = 0x%x\n"
2739 " ev.data.u64 = 0x%llx\n"
2740 " et_mask = 0x%x\n"
2741 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002742 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002743 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2744 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002745 }
2746 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002747
2748done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002749 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002750}
2751
2752int
2753vppcom_epoll_create (void)
2754{
Florin Coras134a9962018-08-28 11:32:04 -07002755 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002756 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757
Florin Coras134a9962018-08-28 11:32:04 -07002758 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002759
Florin Coras6c3b2182020-10-19 18:36:48 -07002760 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002761 vep_session->vep.vep_sh = ~0;
2762 vep_session->vep.next_sh = ~0;
2763 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002764 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002765
Florin Corasa7a1a222018-12-30 17:11:31 -08002766 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2767 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002768
Florin Corasab2f6db2018-08-31 14:31:41 -07002769 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002770}
2771
Florin Corasb0116a12023-02-07 09:11:47 -08002772static void
2773vcl_epoll_ctl_add_unhandled_event (vcl_worker_t *wrk, vcl_session_t *s,
2774 u8 is_epollet, session_evt_type_t evt)
2775{
2776 if (!is_epollet)
2777 {
Florin Corasc2a14172023-02-28 21:13:50 -08002778 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
2779 vcl_epoll_lt_add (wrk, s);
Florin Corasb0116a12023-02-07 09:11:47 -08002780 return;
2781 }
2782
2783 session_event_t e = { 0 };
2784 e.session_index = s->session_index;
2785 e.event_type = evt;
2786 if (evt == SESSION_IO_EVT_RX)
2787 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2788 vec_add1 (wrk->unhandled_evts_vector, e);
2789}
2790
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791int
Florin Coras134a9962018-08-28 11:32:04 -07002792vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002793 struct epoll_event *event)
2794{
Florin Coras134a9962018-08-28 11:32:04 -07002795 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002796 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002797 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002798 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002799
Florin Coras134a9962018-08-28 11:32:04 -07002800 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002801 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002802 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002803 return VPPCOM_EINVAL;
2804 }
2805
Florin Coras134a9962018-08-28 11:32:04 -07002806 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002807 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002808 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002809 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002810 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002811 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002812 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002813 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002814 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002815 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002816 }
2817
Florin Coras134a9962018-08-28 11:32:04 -07002818 ASSERT (vep_session->vep.vep_sh == ~0);
2819 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002820
Florin Coras17ec5772020-08-12 20:46:29 -07002821 s = vcl_session_get_w_handle (wrk, session_handle);
2822 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002823 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002824 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002825 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002826 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002827 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002828 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002829 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002830 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002831 }
2832
2833 switch (op)
2834 {
2835 case EPOLL_CTL_ADD:
2836 if (PREDICT_FALSE (!event))
2837 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002838 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002839 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002840 }
Florin Coras2645f682021-06-04 15:59:41 -07002841 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2842 {
2843 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2844 rv = VPPCOM_EEXIST;
2845 goto done;
2846 }
Florin Coras134a9962018-08-28 11:32:04 -07002847 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002848 {
Florin Coras7e12d942018-06-27 14:32:43 -07002849 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002850 next_session = vcl_session_get_w_handle (wrk,
2851 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002852 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002853 {
Florin Coras5e062572019-03-14 19:07:51 -07002854 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002855 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002856 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002857 }
Florin Coras134a9962018-08-28 11:32:04 -07002858 ASSERT (next_session->vep.prev_sh == vep_handle);
2859 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002860 }
Florin Coras17ec5772020-08-12 20:46:29 -07002861 s->vep.next_sh = vep_session->vep.next_sh;
2862 s->vep.prev_sh = vep_handle;
2863 s->vep.vep_sh = vep_handle;
2864 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002865 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002866 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002867 s->flags &= ~VCL_SESSION_F_IS_VEP;
2868 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002869 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002870
Florin Coras607eb202023-05-29 16:19:38 -07002871 if (event->events & EPOLLOUT)
2872 vcl_session_add_want_deq_ntf (s, 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 Coras607eb202023-05-29 16:19:38 -07002925 if (event->events & EPOLLOUT)
2926 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2927 else
2928 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasbc4d5b02023-05-17 23:20:56 -07002929
Florin Coras2645f682021-06-04 15:59:41 -07002930 /* Generate EPOLLOUT if session write ready nd event was not on */
2931 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2932 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002933 {
Florin Corasb0116a12023-02-07 09:11:47 -08002934 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2935 SESSION_IO_EVT_TX);
hanlin475c9d72019-12-26 11:44:28 +08002936 }
Florin Coras2645f682021-06-04 15:59:41 -07002937 /* Generate EPOLLIN if session read ready and event was not on */
2938 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2939 (vcl_session_read_ready (s) > 0))
2940 {
Florin Corasb0116a12023-02-07 09:11:47 -08002941 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2942 SESSION_IO_EVT_RX);
Florin Coras2645f682021-06-04 15:59:41 -07002943 }
Florin Coras17ec5772020-08-12 20:46:29 -07002944 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2945 s->vep.ev = *event;
Florin Corasbc4d5b02023-05-17 23:20:56 -07002946
Florin Corasa7a1a222018-12-30 17:11:31 -08002947 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2948 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002949 break;
2950
2951 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002952 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002953 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002954 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002955 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002956 goto done;
2957 }
Florin Coras17ec5772020-08-12 20:46:29 -07002958 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002959 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002960 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002961 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002962 rv = VPPCOM_EINVAL;
2963 goto done;
2964 }
2965
Florin Coras17ec5772020-08-12 20:46:29 -07002966 if (s->vep.prev_sh == vep_handle)
2967 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002968 else
2969 {
Florin Coras7e12d942018-06-27 14:32:43 -07002970 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002971 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002972 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002973 {
Florin Coras5e062572019-03-14 19:07:51 -07002974 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002975 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002976 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002977 }
Florin Coras134a9962018-08-28 11:32:04 -07002978 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002979 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002980 }
Florin Coras17ec5772020-08-12 20:46:29 -07002981 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002982 {
Florin Coras7e12d942018-06-27 14:32:43 -07002983 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002984 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002985 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002986 {
Florin Coras5e062572019-03-14 19:07:51 -07002987 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002988 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002989 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002990 }
Florin Coras134a9962018-08-28 11:32:04 -07002991 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002992 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002993 }
2994
Florin Corasfa3884f2021-06-22 20:04:46 -07002995 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2996 vcl_epoll_lt_del (wrk, s);
2997
Florin Coras17ec5772020-08-12 20:46:29 -07002998 memset (&s->vep, 0, sizeof (s->vep));
2999 s->vep.next_sh = ~0;
3000 s->vep.prev_sh = ~0;
3001 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003002 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07003003 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08003004
Florin Corasf1ddeeb2021-06-10 08:08:53 -07003005 if (vcl_session_is_open (s))
Florin Coras607eb202023-05-29 16:19:38 -07003006 vcl_session_del_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08003007
Florin Coras5e062572019-03-14 19:07:51 -07003008 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003009 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003010 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003011 break;
3012
3013 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003014 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003015 rv = VPPCOM_EINVAL;
3016 }
3017
Florin Coras134a9962018-08-28 11:32:04 -07003018 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003019
3020done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003021 return rv;
3022}
3023
Florin Coras86f04502018-09-12 16:08:01 -07003024static inline void
3025vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3026 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003027{
3028 session_disconnected_msg_t *disconnected_msg;
3029 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003030 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003031 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003032 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003033 u8 add_event = 0;
3034
3035 switch (e->event_type)
3036 {
Florin Coras653e43f2019-03-04 10:56:23 -08003037 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003038 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003039 s = vcl_session_get (wrk, sid);
3040 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003041 break;
Florin Corasb242d312020-10-26 15:35:40 -07003042 vcl_fifo_rx_evt_valid_or_break (s);
3043 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003044 if (!(EPOLLIN & s->vep.ev.events) ||
3045 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3046 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003047 break;
3048 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003049 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003050 session_evt_data = s->vep.ev.data.u64;
3051 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003052 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003053 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003054 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003055 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003056 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003057 break;
Florin Coras48178552023-05-17 22:59:40 -07003058 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ? s->ct_tx_fifo :
3059 s->tx_fifo);
Florin Corasb242d312020-10-26 15:35:40 -07003060 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003061 if (!(EPOLLOUT & session_events))
3062 break;
3063 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003064 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003065 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003066 break;
Florin Coras86f04502018-09-12 16:08:01 -07003067 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003068 if (!e->postponed)
3069 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3070 else
3071 s = vcl_session_get (wrk, e->session_index);
3072 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003073 break;
Florin Corasb242d312020-10-26 15:35:40 -07003074 session_events = s->vep.ev.events;
3075 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003076 if (!(EPOLLIN & session_events) ||
3077 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003078 break;
Florin Coras86f04502018-09-12 16:08:01 -07003079 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003080 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003081 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003082 break;
3083 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003084 if (!e->postponed)
3085 {
3086 connected_msg = (session_connected_msg_t *) e->data;
3087 sid = vcl_session_connected_handler (wrk, connected_msg);
3088 }
3089 else
3090 sid = e->session_index;
3091 s = vcl_session_get (wrk, sid);
3092 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003093 break;
Florin Corasb242d312020-10-26 15:35:40 -07003094 session_events = s->vep.ev.events;
3095 /* Generate EPOLLOUT because there's no connected event */
Florin Coras607eb202023-05-29 16:19:38 -07003096 if (!(EPOLLOUT & session_events))
Florin Coras72f77822019-01-22 19:05:52 -08003097 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003098 /* We didn't have a fifo when the event was added */
Florin Coras607eb202023-05-29 16:19:38 -07003099 vcl_session_add_want_deq_ntf (s, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003100 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003101 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003102 session_evt_data = s->vep.ev.data.u64;
3103 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003104 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003105 break;
3106 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003107 if (!e->postponed)
3108 {
3109 disconnected_msg = (session_disconnected_msg_t *) e->data;
3110 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3111 }
3112 else
3113 {
3114 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003115 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003116 }
3117 if (vcl_session_is_closed (s) ||
3118 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003119 {
Florin Coras74254b52021-12-08 11:03:08 -08003120 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003121 vcl_session_free (wrk, s);
3122 break;
3123 }
Florin Corasb242d312020-10-26 15:35:40 -07003124 sid = s->session_index;
3125 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003126 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003127 if (EPOLLRDHUP & session_events)
3128 {
3129 /* If app can distinguish between RDHUP and HUP,
3130 * we make finer control */
3131 events[*num_ev].events = EPOLLRDHUP;
3132 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3133 {
3134 events[*num_ev].events |= EPOLLHUP;
3135 }
3136 }
3137 else
3138 {
3139 events[*num_ev].events = EPOLLHUP;
3140 }
Florin Corasb242d312020-10-26 15:35:40 -07003141 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003142
Florin Coras86f04502018-09-12 16:08:01 -07003143 break;
Florin Coras01ee7a72023-03-01 00:49:25 -08003144 case SESSION_CTRL_EVT_BOUND:
3145 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
3146 break;
Florin Coras86f04502018-09-12 16:08:01 -07003147 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003148 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003149 {
3150 sid =
3151 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3152 s = vcl_session_get (wrk, sid);
3153 }
Florin Coras87f76002021-06-28 19:13:29 -07003154 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003155 {
3156 sid = e->session_index;
3157 s = vcl_session_get (wrk, sid);
3158 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3159 }
Florin Coras87f76002021-06-28 19:13:29 -07003160 if (vcl_session_is_closed (s) ||
3161 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003162 {
Florin Coras74254b52021-12-08 11:03:08 -08003163 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003164 vcl_session_free (wrk, s);
3165 break;
3166 }
Florin Corasb242d312020-10-26 15:35:40 -07003167 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003168 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003169 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3170 if ((EPOLLRDHUP & session_events) &&
3171 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3172 {
Yacan Liuab157702022-09-26 16:41:32 +08003173 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003174 }
3175 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3176 {
3177 events[*num_ev].events |= EPOLLIN;
3178 }
Florin Corasb242d312020-10-26 15:35:40 -07003179 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003180 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003181 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3182 vcl_session_unlisten_reply_handler (wrk, e->data);
3183 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003184 case SESSION_CTRL_EVT_MIGRATED:
3185 vcl_session_migrated_handler (wrk, e->data);
3186 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003187 case SESSION_CTRL_EVT_CLEANUP:
3188 vcl_session_cleanup_handler (wrk, e->data);
3189 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003190 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3191 vcl_session_req_worker_update_handler (wrk, e->data);
3192 break;
3193 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3194 vcl_session_worker_update_reply_handler (wrk, e->data);
3195 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003196 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3197 vcl_session_app_add_segment_handler (wrk, e->data);
3198 break;
3199 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3200 vcl_session_app_del_segment_handler (wrk, e->data);
3201 break;
hanlina3a48962020-07-13 11:09:15 +08003202 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003203 vcl_worker_rpc_handler (wrk, e->data);
3204 break;
Florin Coras86f04502018-09-12 16:08:01 -07003205 default:
3206 VDBG (0, "unhandled: %u", e->event_type);
3207 break;
3208 }
3209
3210 if (add_event)
3211 {
3212 events[*num_ev].data.u64 = session_evt_data;
3213 if (EPOLLONESHOT & session_events)
3214 {
Florin Corasb242d312020-10-26 15:35:40 -07003215 s = vcl_session_get (wrk, sid);
3216 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003217 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003218 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003219 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003220 s = vcl_session_get (wrk, sid);
3221 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3222 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003223 }
Florin Coras86f04502018-09-12 16:08:01 -07003224 *num_ev += 1;
3225 }
3226}
3227
3228static int
3229vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3230 struct epoll_event *events, u32 maxevents,
3231 double wait_for_time, u32 * num_ev)
3232{
Florin Coras99368312018-08-02 10:45:44 -07003233 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003234 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003235 int i;
3236
Florin Coras539663c2018-09-28 14:59:37 -07003237 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3238 goto handle_dequeued;
3239
Florin Coras54693d22018-07-17 10:46:29 -07003240 if (svm_msg_q_is_empty (mq))
3241 {
3242 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003243 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003244 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003245 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003246 else
3247 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003248 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003249 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003250 }
3251 }
Florin Corase003a1b2019-06-05 10:47:16 -07003252 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003253 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003254
Florin Coras539663c2018-09-28 14:59:37 -07003255handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003256 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003257 {
Florin Coras134a9962018-08-28 11:32:04 -07003258 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003259 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003260 if (*num_ev < maxevents)
3261 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3262 else
3263 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003264 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003265 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003266 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003267 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003268 return *num_ev;
3269}
3270
Florin Coras99368312018-08-02 10:45:44 -07003271static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003272vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3273 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003274{
Florin Corasccdb8b82021-04-28 13:01:06 -07003275 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003276
3277 if (!n_evts)
3278 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003279 if (timeout_ms > 0)
3280 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003281 }
3282
3283 do
3284 {
3285 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003286 timeout_ms, &n_evts);
3287 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003288 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003289 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003290 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003291
3292 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003293}
3294
3295static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003296vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3297 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003298{
Florin Coras99368312018-08-02 10:45:44 -07003299 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003300 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003301 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003302 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003303 u64 buf;
3304
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003305 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3306 {
3307 vcl_api_retry_attach (wrk);
3308 return n_evts;
3309 }
3310
Florin Coras134a9962018-08-28 11:32:04 -07003311 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003312 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003313 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003314 if (timeout_ms > 0)
3315 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003316 }
3317
Florin Corasb13ba132021-01-27 16:05:24 -08003318 do
3319 {
3320 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003321 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003322 if (n_mq_evts < 0)
3323 {
3324 VDBG (0, "epoll_wait error %u", errno);
3325 return n_evts;
3326 }
3327
3328 for (i = 0; i < n_mq_evts; i++)
3329 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003330 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3331 {
3332 /* api socket was closed */
3333 vcl_api_handle_disconnect (wrk);
3334 continue;
3335 }
3336
Florin Corasb13ba132021-01-27 16:05:24 -08003337 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3338 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3339 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3340 &n_evts);
3341 }
3342
Florin Corasccdb8b82021-04-28 13:01:06 -07003343 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003344 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003345 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003346 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003347
3348 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003349}
3350
Florin Corasfe286f72021-06-04 10:07:55 -07003351static void
Florin Corasfe286f72021-06-04 10:07:55 -07003352vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3353 int maxevents, u32 *n_evts)
3354{
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003355 u32 add_event = 0, evt_flags = 0, next, *to_remove = 0, *si;
Florin Corasfe286f72021-06-04 10:07:55 -07003356 vcl_session_t *s;
3357 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003358 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003359
Florin Corasfa3884f2021-06-22 20:04:46 -07003360 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003361 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003362 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003363
Florin Corasfa3884f2021-06-22 20:04:46 -07003364 next = wrk->ep_lt_current;
3365 do
Florin Corasfe286f72021-06-04 10:07:55 -07003366 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003367 s = vcl_session_get (wrk, next);
3368 next = s->vep.lt_next;
3369
3370 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003371 {
3372 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003373 evt_flags |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003374 evt_data = s->vep.ev.data.u64;
3375 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003376 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003377 {
3378 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003379 evt_flags |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
Florin Corasfa3884f2021-06-22 20:04:46 -07003380 evt_data = s->vep.ev.data.u64;
3381 }
3382 if (!add_event && s->session_state > VCL_STATE_READY)
3383 {
3384 add_event = 1;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003385 evt_flags |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003386 evt_data = s->vep.ev.data.u64;
3387 }
3388 if (add_event)
3389 {
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003390 events[*n_evts].events = evt_flags;
Florin Corasfe286f72021-06-04 10:07:55 -07003391 events[*n_evts].data.u64 = evt_data;
3392 *n_evts += 1;
3393 add_event = 0;
Florin Corasdb3fbfc2023-05-22 09:19:57 -07003394 evt_flags = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003395 if (EPOLLONESHOT & s->vep.ev.events)
3396 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003397 if (*n_evts == maxevents)
3398 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003399 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003400 break;
3401 }
3402 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003403 else
3404 {
Florin Coras7ff72742023-05-16 13:05:28 -07003405 vec_add1 (to_remove, s->session_index);
Florin Corasfa3884f2021-06-22 20:04:46 -07003406 }
Florin Corasfe286f72021-06-04 10:07:55 -07003407 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003408 while (next != wrk->ep_lt_current);
Florin Coras7ff72742023-05-16 13:05:28 -07003409
3410 vec_foreach (si, to_remove)
3411 {
3412 s = vcl_session_get (wrk, *si);
3413 vcl_epoll_lt_del (wrk, s);
3414 }
3415 vec_free (to_remove);
Florin Corasfe286f72021-06-04 10:07:55 -07003416}
3417
Dave Wallacef7f809c2017-10-03 01:48:42 -04003418int
Florin Coras134a9962018-08-28 11:32:04 -07003419vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003420 int maxevents, double wait_for_time)
3421{
Florin Coras134a9962018-08-28 11:32:04 -07003422 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003423 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003424 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003425 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003426
3427 if (PREDICT_FALSE (maxevents <= 0))
3428 {
Florin Coras5e062572019-03-14 19:07:51 -07003429 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003430 return VPPCOM_EINVAL;
3431 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003432
Florin Coras134a9962018-08-28 11:32:04 -07003433 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003434 if (!vep_session)
3435 return VPPCOM_EBADFD;
3436
Florin Coras6c3b2182020-10-19 18:36:48 -07003437 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003438 {
Florin Coras5e062572019-03-14 19:07:51 -07003439 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003440 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003441 }
Florin Coras54693d22018-07-17 10:46:29 -07003442
Florin Coras86f04502018-09-12 16:08:01 -07003443 if (vec_len (wrk->unhandled_evts_vector))
3444 {
3445 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3446 {
3447 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3448 events, &n_evts);
3449 if (n_evts == maxevents)
3450 {
Florin Corase003a1b2019-06-05 10:47:16 -07003451 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3452 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003453 }
3454 }
Florin Corase003a1b2019-06-05 10:47:16 -07003455 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003456 }
liuyacancba87102021-09-10 15:14:05 +08003457
3458 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3459 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3460
wanghanlin8919fec2021-03-18 20:00:41 +08003461 /* Request to only drain unhandled */
3462 if ((int) wait_for_time == -2)
3463 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003464
Florin Coras86f04502018-09-12 16:08:01 -07003465
Florin Corasfe286f72021-06-04 10:07:55 -07003466 if (vcm->cfg.use_mq_eventfd)
3467 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3468 wait_for_time);
3469 else
3470 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3471 wait_for_time);
3472
Florin Corasfe286f72021-06-04 10:07:55 -07003473 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003474}
3475
Dave Wallace35830af2017-10-09 01:43:42 -04003476int
Florin Coras134a9962018-08-28 11:32:04 -07003477vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003478 void *buffer, uint32_t * buflen)
3479{
Florin Coras134a9962018-08-28 11:32:04 -07003480 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003481 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003482 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003483 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003484 vcl_session_t *session;
3485 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003486
Florin Coras134a9962018-08-28 11:32:04 -07003487 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003488 if (!session)
3489 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003490
Dave Wallace35830af2017-10-09 01:43:42 -04003491 switch (op)
3492 {
3493 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003494 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003495 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3496 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003497 break;
3498
Dave Wallace227867f2017-11-13 21:21:53 -05003499 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003500 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003501 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3502 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003503 break;
3504
3505 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003506 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003507 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003508 *flags =
3509 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003510 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003511 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003512 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003513 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3514 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003515 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003516 }
3517 else
3518 rv = VPPCOM_EINVAL;
3519 break;
3520
3521 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003522 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003523 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003524 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003525 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003526 else
Florin Corasac422d62020-10-19 20:51:36 -07003527 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003528
Florin Coras5e062572019-03-14 19:07:51 -07003529 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3530 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003531 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003532 }
3533 else
3534 rv = VPPCOM_EINVAL;
3535 break;
3536
3537 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003538 if (PREDICT_TRUE (buffer && buflen &&
3539 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003540 {
Florin Coras7e12d942018-06-27 14:32:43 -07003541 ep->is_ip4 = session->transport.is_ip4;
3542 ep->port = session->transport.rmt_port;
3543 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003544 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3545 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003546 else
Dave Barach178cf492018-11-13 16:34:13 -05003547 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3548 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003549 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003550 VDBG (1,
3551 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3552 "addr = %U, port %u",
3553 session_handle, ep->is_ip4, vcl_format_ip46_address,
3554 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003555 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3556 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003557 }
3558 else
3559 rv = VPPCOM_EINVAL;
3560 break;
3561
3562 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003563 if (PREDICT_TRUE (buffer && buflen &&
3564 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003565 {
Florin Coras7e12d942018-06-27 14:32:43 -07003566 ep->is_ip4 = session->transport.is_ip4;
3567 ep->port = session->transport.lcl_port;
3568 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003569 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3570 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003571 else
Dave Barach178cf492018-11-13 16:34:13 -05003572 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3573 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003574 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003575 VDBG (1,
3576 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3577 " port %d",
3578 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003579 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003580 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3581 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003582 }
3583 else
3584 rv = VPPCOM_EINVAL;
3585 break;
Stevenb5a11602017-10-11 09:59:30 -07003586
Florin Corasef7cbf62019-10-17 09:56:27 -07003587 case VPPCOM_ATTR_SET_LCL_ADDR:
3588 if (PREDICT_TRUE (buffer && buflen &&
3589 (*buflen >= sizeof (*ep)) && ep->ip))
3590 {
3591 session->transport.is_ip4 = ep->is_ip4;
3592 session->transport.lcl_port = ep->port;
3593 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3594 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003595 VDBG (1,
3596 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3597 " port %d",
3598 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003599 &session->transport.lcl_ip,
3600 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3601 clib_net_to_host_u16 (ep->port));
3602 }
3603 else
3604 rv = VPPCOM_EINVAL;
3605 break;
3606
Dave Wallace048b1d62018-01-03 22:24:41 -05003607 case VPPCOM_ATTR_GET_LIBC_EPFD:
3608 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003609 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003610 break;
3611
3612 case VPPCOM_ATTR_SET_LIBC_EPFD:
3613 if (PREDICT_TRUE (buffer && buflen &&
3614 (*buflen == sizeof (session->libc_epfd))))
3615 {
3616 session->libc_epfd = *(int *) buffer;
3617 *buflen = sizeof (session->libc_epfd);
3618
Florin Coras5e062572019-03-14 19:07:51 -07003619 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3620 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003621 }
3622 else
3623 rv = VPPCOM_EINVAL;
3624 break;
3625
3626 case VPPCOM_ATTR_GET_PROTOCOL:
3627 if (buffer && buflen && (*buflen >= sizeof (int)))
3628 {
Florin Coras7e12d942018-06-27 14:32:43 -07003629 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003630 *buflen = sizeof (int);
3631
Florin Coras5e062572019-03-14 19:07:51 -07003632 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3633 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003634 }
3635 else
3636 rv = VPPCOM_EINVAL;
3637 break;
3638
3639 case VPPCOM_ATTR_GET_LISTEN:
3640 if (buffer && buflen && (*buflen >= sizeof (int)))
3641 {
Florin Corasac422d62020-10-19 20:51:36 -07003642 *(int *) buffer = vcl_session_has_attr (session,
3643 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003644 *buflen = sizeof (int);
3645
Florin Coras5e062572019-03-14 19:07:51 -07003646 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3647 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003648 }
3649 else
3650 rv = VPPCOM_EINVAL;
3651 break;
3652
3653 case VPPCOM_ATTR_GET_ERROR:
3654 if (buffer && buflen && (*buflen >= sizeof (int)))
3655 {
3656 *(int *) buffer = 0;
3657 *buflen = sizeof (int);
3658
Florin Coras5e062572019-03-14 19:07:51 -07003659 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3660 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003661 }
3662 else
3663 rv = VPPCOM_EINVAL;
3664 break;
3665
3666 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3667 if (buffer && buflen && (*buflen >= sizeof (u32)))
3668 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003669
3670 /* VPP-TBD */
3671 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003672 session->tx_fifo ?
3673 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003674 vcm->cfg.tx_fifo_size);
3675 *buflen = sizeof (u32);
3676
Florin Coras5e062572019-03-14 19:07:51 -07003677 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3678 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3679 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003680 }
3681 else
3682 rv = VPPCOM_EINVAL;
3683 break;
3684
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003685 case VPPCOM_ATTR_SET_DSCP:
3686 if (buffer && buflen && (*buflen >= sizeof (u8)))
3687 {
3688 session->dscp = *(u8 *) buffer;
3689
3690 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3691 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3692 }
3693 else
3694 rv = VPPCOM_EINVAL;
3695 break;
3696
Dave Wallace048b1d62018-01-03 22:24:41 -05003697 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3698 if (buffer && buflen && (*buflen == sizeof (u32)))
3699 {
3700 /* VPP-TBD */
3701 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003702 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3703 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3704 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003705 }
3706 else
3707 rv = VPPCOM_EINVAL;
3708 break;
3709
3710 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3711 if (buffer && buflen && (*buflen >= sizeof (u32)))
3712 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003713
3714 /* VPP-TBD */
3715 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003716 session->rx_fifo ?
3717 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003718 vcm->cfg.rx_fifo_size);
3719 *buflen = sizeof (u32);
3720
Florin Coras5e062572019-03-14 19:07:51 -07003721 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3722 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003723 }
3724 else
3725 rv = VPPCOM_EINVAL;
3726 break;
3727
3728 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3729 if (buffer && buflen && (*buflen == sizeof (u32)))
3730 {
3731 /* VPP-TBD */
3732 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003733 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3734 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3735 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003736 }
3737 else
3738 rv = VPPCOM_EINVAL;
3739 break;
3740
3741 case VPPCOM_ATTR_GET_REUSEADDR:
3742 if (buffer && buflen && (*buflen >= sizeof (int)))
3743 {
3744 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003745 *(int *) buffer = vcl_session_has_attr (session,
3746 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003747 *buflen = sizeof (int);
3748
Florin Coras5e062572019-03-14 19:07:51 -07003749 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3750 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003751 }
3752 else
3753 rv = VPPCOM_EINVAL;
3754 break;
3755
Stevenb5a11602017-10-11 09:59:30 -07003756 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003757 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003758 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003759 {
3760 /* VPP-TBD */
3761 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003762 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003763 else
Florin Corasac422d62020-10-19 20:51:36 -07003764 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003765
Florin Coras5e062572019-03-14 19:07:51 -07003766 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003767 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003768 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003769 }
3770 else
3771 rv = VPPCOM_EINVAL;
3772 break;
3773
3774 case VPPCOM_ATTR_GET_REUSEPORT:
3775 if (buffer && buflen && (*buflen >= sizeof (int)))
3776 {
3777 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003778 *(int *) buffer = vcl_session_has_attr (session,
3779 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003780 *buflen = sizeof (int);
3781
Florin Coras5e062572019-03-14 19:07:51 -07003782 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3783 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003784 }
3785 else
3786 rv = VPPCOM_EINVAL;
3787 break;
3788
3789 case VPPCOM_ATTR_SET_REUSEPORT:
3790 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003791 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003792 {
3793 /* VPP-TBD */
3794 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003795 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003796 else
Florin Corasac422d62020-10-19 20:51:36 -07003797 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003798
Florin Coras5e062572019-03-14 19:07:51 -07003799 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003800 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003801 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003802 }
3803 else
3804 rv = VPPCOM_EINVAL;
3805 break;
3806
3807 case VPPCOM_ATTR_GET_BROADCAST:
3808 if (buffer && buflen && (*buflen >= sizeof (int)))
3809 {
3810 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003811 *(int *) buffer = vcl_session_has_attr (session,
3812 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003813 *buflen = sizeof (int);
3814
Florin Coras5e062572019-03-14 19:07:51 -07003815 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3816 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 }
3818 else
3819 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003820 break;
3821
3822 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003823 if (buffer && buflen && (*buflen == sizeof (int)))
3824 {
3825 /* VPP-TBD */
3826 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003827 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 else
Florin Corasac422d62020-10-19 20:51:36 -07003829 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003830
Florin Coras5e062572019-03-14 19:07:51 -07003831 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003832 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003833 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003834 }
3835 else
3836 rv = VPPCOM_EINVAL;
3837 break;
3838
3839 case VPPCOM_ATTR_GET_V6ONLY:
3840 if (buffer && buflen && (*buflen >= sizeof (int)))
3841 {
3842 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003843 *(int *) buffer = vcl_session_has_attr (session,
3844 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003845 *buflen = sizeof (int);
3846
Florin Coras5e062572019-03-14 19:07:51 -07003847 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3848 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003849 }
3850 else
3851 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003852 break;
3853
3854 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003855 if (buffer && buflen && (*buflen == sizeof (int)))
3856 {
3857 /* VPP-TBD */
3858 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003859 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003860 else
Florin Corasac422d62020-10-19 20:51:36 -07003861 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003862
Florin Coras5e062572019-03-14 19:07:51 -07003863 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003864 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003865 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003866 }
3867 else
3868 rv = VPPCOM_EINVAL;
3869 break;
3870
3871 case VPPCOM_ATTR_GET_KEEPALIVE:
3872 if (buffer && buflen && (*buflen >= sizeof (int)))
3873 {
3874 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003875 *(int *) buffer = vcl_session_has_attr (session,
3876 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003877 *buflen = sizeof (int);
3878
Florin Coras5e062572019-03-14 19:07:51 -07003879 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3880 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003881 }
3882 else
3883 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003884 break;
Stevenbccd3392017-10-12 20:42:21 -07003885
3886 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003887 if (buffer && buflen && (*buflen == sizeof (int)))
3888 {
3889 /* VPP-TBD */
3890 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003891 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003892 else
Florin Corasac422d62020-10-19 20:51:36 -07003893 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003894
Florin Coras5e062572019-03-14 19:07:51 -07003895 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003896 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003897 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003898 }
3899 else
3900 rv = VPPCOM_EINVAL;
3901 break;
3902
3903 case VPPCOM_ATTR_GET_TCP_NODELAY:
3904 if (buffer && buflen && (*buflen >= sizeof (int)))
3905 {
3906 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003907 *(int *) buffer = vcl_session_has_attr (session,
3908 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003909 *buflen = sizeof (int);
3910
Florin Coras5e062572019-03-14 19:07:51 -07003911 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3912 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003913 }
3914 else
3915 rv = VPPCOM_EINVAL;
3916 break;
3917
3918 case VPPCOM_ATTR_SET_TCP_NODELAY:
3919 if (buffer && buflen && (*buflen == sizeof (int)))
3920 {
3921 /* VPP-TBD */
3922 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003923 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003924 else
Florin Corasac422d62020-10-19 20:51:36 -07003925 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003926
Florin Coras5e062572019-03-14 19:07:51 -07003927 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003928 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003929 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003930 }
3931 else
3932 rv = VPPCOM_EINVAL;
3933 break;
3934
3935 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3936 if (buffer && buflen && (*buflen >= sizeof (int)))
3937 {
3938 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003939 *(int *) buffer = vcl_session_has_attr (session,
3940 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003941 *buflen = sizeof (int);
3942
Florin Coras5e062572019-03-14 19:07:51 -07003943 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3944 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003945 }
3946 else
3947 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003948 break;
3949
3950 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003951 if (buffer && buflen && (*buflen == sizeof (int)))
3952 {
3953 /* VPP-TBD */
3954 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003955 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003956 else
Florin Corasac422d62020-10-19 20:51:36 -07003957 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003958
Florin Coras5e062572019-03-14 19:07:51 -07003959 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003960 vcl_session_has_attr (session,
3961 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003962 }
3963 else
3964 rv = VPPCOM_EINVAL;
3965 break;
3966
3967 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3968 if (buffer && buflen && (*buflen >= sizeof (int)))
3969 {
3970 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003971 *(int *) buffer = vcl_session_has_attr (session,
3972 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003973 *buflen = sizeof (int);
3974
Florin Coras5e062572019-03-14 19:07:51 -07003975 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3976 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003977 }
3978 else
3979 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003980 break;
3981
3982 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003983 if (buffer && buflen && (*buflen == sizeof (int)))
3984 {
3985 /* VPP-TBD */
3986 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003987 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003988 else
Florin Corasac422d62020-10-19 20:51:36 -07003989 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003990
Florin Coras5e062572019-03-14 19:07:51 -07003991 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003992 vcl_session_has_attr (session,
3993 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003994 }
3995 else
3996 rv = VPPCOM_EINVAL;
3997 break;
3998
3999 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004000 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004001 {
Florin Coras04ae8272021-04-12 19:55:37 -07004002 rv = VPPCOM_EINVAL;
4003 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004004 }
Florin Coras04ae8272021-04-12 19:55:37 -07004005
4006 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4007 tea.mss = *(u32 *) buffer;
4008 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
4009 rv = VPPCOM_ENOPROTOOPT;
4010
4011 if (!rv)
4012 {
4013 *(u32 *) buffer = tea.mss;
4014 *buflen = sizeof (int);
4015 }
4016
4017 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4018 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004019 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004020 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004021 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004022 {
Florin Coras04ae8272021-04-12 19:55:37 -07004023 rv = VPPCOM_EINVAL;
4024 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004025 }
Florin Coras04ae8272021-04-12 19:55:37 -07004026
4027 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4028 tea.mss = *(u32 *) buffer;
4029 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4030 rv = VPPCOM_ENOPROTOOPT;
4031
4032 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4033 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004034 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004035
Florin Coras1e966172020-05-16 18:18:14 +00004036 case VPPCOM_ATTR_SET_CONNECTED:
4037 session->flags |= VCL_SESSION_F_CONNECTED;
4038 break;
4039
Florin Corasa5a9efd2021-01-05 17:03:29 -08004040 case VPPCOM_ATTR_SET_CKPAIR:
4041 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4042 !vcl_session_has_crypto (session))
4043 {
4044 rv = VPPCOM_EINVAL;
4045 break;
4046 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004047 if (!session->ext_config)
4048 {
Florin Coras87f63892021-05-01 16:01:40 -07004049 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4050 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004051 }
4052 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4053 {
4054 rv = VPPCOM_EINVAL;
4055 break;
4056 }
4057
4058 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004059 break;
4060
Florin Coras6a6555a2021-01-28 11:39:27 -08004061 case VPPCOM_ATTR_SET_VRF:
4062 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4063 {
4064 rv = VPPCOM_EINVAL;
4065 break;
4066 }
4067 session->vrf = *(u32 *) buffer;
4068 break;
4069
4070 case VPPCOM_ATTR_GET_VRF:
4071 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4072 {
4073 rv = VPPCOM_EINVAL;
4074 break;
4075 }
4076 *(u32 *) buffer = session->vrf;
4077 *buflen = sizeof (u32);
4078 break;
4079
wanghanlin0674f852021-02-22 10:38:36 +08004080 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004081 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004082 {
Florin Corasd77325c2021-02-23 12:03:03 -08004083 rv = VPPCOM_EINVAL;
4084 break;
wanghanlin0674f852021-02-22 10:38:36 +08004085 }
Florin Corasd77325c2021-02-23 12:03:03 -08004086
4087 if (session->transport.is_ip4)
4088 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004089 else
Florin Corasd77325c2021-02-23 12:03:03 -08004090 *(int *) buffer = AF_INET6;
4091 *buflen = sizeof (int);
4092
wanghanlin0674f852021-02-22 10:38:36 +08004093 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4094 *buflen);
4095 break;
4096
Florin Coras87f63892021-05-01 16:01:40 -07004097 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4098 if (!(buffer && buflen && (*buflen > 0)))
4099 {
4100 rv = VPPCOM_EINVAL;
4101 break;
4102 }
4103 if (session->ext_config)
4104 {
4105 rv = VPPCOM_EINVAL;
4106 break;
4107 }
4108 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4109 *buflen + sizeof (u32));
4110 clib_memcpy (session->ext_config->data, buffer, *buflen);
4111 session->ext_config->len = *buflen;
4112 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004113 case VPPCOM_ATTR_SET_IP_PKTINFO:
4114 if (buffer && buflen && (*buflen == sizeof (int)) &&
4115 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4116 {
4117 if (*(int *) buffer)
4118 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4119 else
4120 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4121
4122 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4123 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4124 *buflen);
4125 }
4126 else
4127 rv = VPPCOM_EINVAL;
4128 break;
4129
4130 case VPPCOM_ATTR_GET_IP_PKTINFO:
4131 if (buffer && buflen && (*buflen >= sizeof (int)))
4132 {
4133 *(int *) buffer =
4134 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4135 *buflen = sizeof (int);
4136
4137 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4138 *buflen);
4139 }
4140 else
4141 rv = VPPCOM_EINVAL;
4142 break;
Florin Coras87f63892021-05-01 16:01:40 -07004143
Dave Wallacee22aa742017-10-20 12:30:38 -04004144 default:
4145 rv = VPPCOM_EINVAL;
4146 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004147 }
4148
Dave Wallace35830af2017-10-09 01:43:42 -04004149 return rv;
4150}
4151
Stevenac1f96d2017-10-24 16:03:58 -07004152int
Florin Coras134a9962018-08-28 11:32:04 -07004153vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004154 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4155{
Florin Coras134a9962018-08-28 11:32:04 -07004156 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004157 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004158 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004159
4160 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004161 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004162 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004163 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004164 else
4165 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004166 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004167 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004168 }
4169
Florin Coras0a1e1832020-03-29 18:54:04 +00004170 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004171 {
Florin Coras5da10c42020-04-01 04:31:21 +00004172 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004173 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004174 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4175 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004176 else
Dave Barach178cf492018-11-13 16:34:13 -05004177 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4178 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004179 ep->is_ip4 = session->transport.is_ip4;
4180 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004181 }
Florin Coras460dce62018-07-27 05:45:06 -07004182
Stevenac1f96d2017-10-24 16:03:58 -07004183 return rv;
4184}
4185
Florin Coraseff5f7a2023-02-07 17:36:17 -08004186static void
4187vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4188{
4189 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4190
4191 do
4192 {
4193 switch (tlv->data_type)
4194 {
4195 case VCL_UDP_SEGMENT:
4196 s->gso_size = *(u16 *) tlv->data;
4197 break;
4198 case VCL_IP_PKTINFO:
4199 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4200 sizeof (ip4_address_t));
4201 break;
4202 default:
4203 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4204 break;
4205 }
4206 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4207 }
4208 while (tlv);
4209}
4210
Stevenac1f96d2017-10-24 16:03:58 -07004211int
Florin Coras134a9962018-08-28 11:32:04 -07004212vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004213 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4214{
Florin Coras7a2abce2020-04-05 19:25:44 +00004215 vcl_worker_t *wrk = vcl_worker_get_current ();
4216 vcl_session_t *s;
4217
4218 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004219 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004220 return VPPCOM_EBADFD;
4221
Dave Wallace617dffa2017-10-26 14:47:06 -04004222 if (ep)
4223 {
Florin Coras5824cc52020-10-20 18:44:41 -07004224 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004225 return VPPCOM_EINVAL;
4226
liuyacanbc0c7542021-08-02 20:15:05 +08004227 s->transport.is_ip4 = ep->is_ip4;
4228 s->transport.rmt_port = ep->port;
4229 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4230
Florin Coraseff5f7a2023-02-07 17:36:17 -08004231 if (ep->app_tlvs)
4232 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004233
Florin Coras5da10c42020-04-01 04:31:21 +00004234 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004235 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004236 {
Florin Coras5824cc52020-10-20 18:44:41 -07004237 u32 session_index = s->session_index;
4238 f64 timeout = vcm->cfg.session_timeout;
4239 int rv;
4240
Florin Coras5da10c42020-04-01 04:31:21 +00004241 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004242 rv = vppcom_wait_for_session_state_change (session_index,
4243 VCL_STATE_READY,
4244 timeout);
4245 if (rv < 0)
4246 return rv;
4247 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004248 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004249 }
4250
4251 if (flags)
4252 {
4253 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004254 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004255 }
4256
Florin Coraseff5f7a2023-02-07 17:36:17 -08004257 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004258 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004259}
4260
Dave Wallace048b1d62018-01-03 22:24:41 -05004261int
4262vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4263{
Florin Coras134a9962018-08-28 11:32:04 -07004264 vcl_worker_t *wrk = vcl_worker_get_current ();
4265 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004266 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004267 svm_msg_q_msg_t msg;
4268 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004269 int rv, num_ev = 0;
4270
Florin Coras5e062572019-03-14 19:07:51 -07004271 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004272
4273 if (!vp)
4274 return VPPCOM_EFAULT;
4275
4276 do
4277 {
Florin Coras7e12d942018-06-27 14:32:43 -07004278 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004279
Florin Coras6917b942018-11-13 22:44:54 -08004280 /* Dequeue all events and drop all unhandled io events */
4281 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4282 {
4283 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4284 vcl_handle_mq_event (wrk, e);
4285 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4286 }
4287 vec_reset_length (wrk->unhandled_evts_vector);
4288
Dave Wallace048b1d62018-01-03 22:24:41 -05004289 for (i = 0; i < n_sids; i++)
4290 {
Florin Coras7baeb712019-01-04 17:05:43 -08004291 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004292 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004293 {
4294 vp[i].revents = POLLHUP;
4295 num_ev++;
4296 continue;
4297 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004298
Florin Coras6917b942018-11-13 22:44:54 -08004299 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004300
4301 if (POLLIN & vp[i].events)
4302 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004303 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004304 if (rv > 0)
4305 {
Florin Coras6917b942018-11-13 22:44:54 -08004306 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004307 num_ev++;
4308 }
4309 else if (rv < 0)
4310 {
4311 switch (rv)
4312 {
4313 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004314 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004315 break;
4316
4317 default:
Florin Coras6917b942018-11-13 22:44:54 -08004318 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004319 break;
4320 }
4321 num_ev++;
4322 }
4323 }
4324
4325 if (POLLOUT & vp[i].events)
4326 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004327 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004328 if (rv > 0)
4329 {
Florin Coras6917b942018-11-13 22:44:54 -08004330 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004331 num_ev++;
4332 }
4333 else if (rv < 0)
4334 {
4335 switch (rv)
4336 {
4337 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004338 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004339 break;
4340
4341 default:
Florin Coras6917b942018-11-13 22:44:54 -08004342 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004343 break;
4344 }
4345 num_ev++;
4346 }
4347 }
4348
Dave Wallace7e607a72018-06-18 18:41:32 -04004349 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004350 {
Florin Coras6917b942018-11-13 22:44:54 -08004351 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004352 num_ev++;
4353 }
4354 }
4355 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004356 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004357 }
4358 while ((num_ev == 0) && keep_trying);
4359
Dave Wallace048b1d62018-01-03 22:24:41 -05004360 return num_ev;
4361}
4362
Florin Coras99368312018-08-02 10:45:44 -07004363int
4364vppcom_mq_epoll_fd (void)
4365{
Florin Coras134a9962018-08-28 11:32:04 -07004366 vcl_worker_t *wrk = vcl_worker_get_current ();
4367 return wrk->mqs_epfd;
4368}
4369
4370int
Florin Coras30e79c22019-01-02 19:31:22 -08004371vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004372{
4373 return session_handle & 0xFFFFFF;
4374}
4375
4376int
Florin Coras30e79c22019-01-02 19:31:22 -08004377vppcom_session_worker (vcl_session_handle_t session_handle)
4378{
4379 return session_handle >> 24;
4380}
4381
4382int
Florin Coras134a9962018-08-28 11:32:04 -07004383vppcom_worker_register (void)
4384{
Florin Coras47c40e22018-11-26 17:01:36 -08004385 if (!vcl_worker_alloc_and_init ())
4386 return VPPCOM_EEXIST;
4387
Florin Coras47c40e22018-11-26 17:01:36 -08004388 if (vcl_worker_register_with_vpp ())
4389 return VPPCOM_EEXIST;
4390
4391 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004392}
4393
Florin Coras369db832019-07-08 12:34:45 -07004394void
4395vppcom_worker_unregister (void)
4396{
4397 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4398 vcl_set_worker_index (~0);
4399}
4400
Pivo6017ff02020-05-04 17:57:33 +02004401void
4402vppcom_worker_index_set (int index)
4403{
4404 vcl_set_worker_index (index);
4405}
4406
Florin Corasdfe4cf42018-11-28 22:13:45 -08004407int
4408vppcom_worker_index (void)
4409{
4410 return vcl_get_worker_index ();
4411}
4412
Florin Corase0982e52019-01-25 13:19:56 -08004413int
4414vppcom_worker_mqs_epfd (void)
4415{
4416 vcl_worker_t *wrk = vcl_worker_get_current ();
4417 if (!vcm->cfg.use_mq_eventfd)
4418 return -1;
4419 return wrk->mqs_epfd;
4420}
4421
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004422int
4423vppcom_session_is_connectable_listener (uint32_t session_handle)
4424{
4425 vcl_session_t *session;
4426 vcl_worker_t *wrk = vcl_worker_get_current ();
4427 session = vcl_session_get_w_handle (wrk, session_handle);
4428 if (!session)
4429 return VPPCOM_EBADFD;
4430 return vcl_session_is_connectable_listener (wrk, session);
4431}
4432
4433int
4434vppcom_session_listener (uint32_t session_handle)
4435{
4436 vcl_worker_t *wrk = vcl_worker_get_current ();
4437 vcl_session_t *listen_session, *session;
4438 session = vcl_session_get_w_handle (wrk, session_handle);
4439 if (!session)
4440 return VPPCOM_EBADFD;
4441 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4442 return VPPCOM_EBADFD;
4443 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4444 if (!listen_session)
4445 return VPPCOM_EBADFD;
4446 return vcl_session_handle (listen_session);
4447}
4448
4449int
4450vppcom_session_n_accepted (uint32_t session_handle)
4451{
4452 vcl_worker_t *wrk = vcl_worker_get_current ();
4453 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4454 if (!session)
4455 return VPPCOM_EBADFD;
4456 return session->n_accepted_sessions;
4457}
4458
Florin Coras66ec4672020-06-15 07:59:40 -07004459const char *
4460vppcom_proto_str (vppcom_proto_t proto)
4461{
4462 char const *proto_str;
4463
4464 switch (proto)
4465 {
4466 case VPPCOM_PROTO_TCP:
4467 proto_str = "TCP";
4468 break;
4469 case VPPCOM_PROTO_UDP:
4470 proto_str = "UDP";
4471 break;
4472 case VPPCOM_PROTO_TLS:
4473 proto_str = "TLS";
4474 break;
4475 case VPPCOM_PROTO_QUIC:
4476 proto_str = "QUIC";
4477 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004478 case VPPCOM_PROTO_DTLS:
4479 proto_str = "DTLS";
4480 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004481 case VPPCOM_PROTO_SRTP:
4482 proto_str = "SRTP";
4483 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004484 default:
4485 proto_str = "UNKNOWN";
4486 break;
4487 }
4488 return proto_str;
4489}
4490
4491const char *
4492vppcom_retval_str (int retval)
4493{
4494 char const *st;
4495
4496 switch (retval)
4497 {
4498 case VPPCOM_OK:
4499 st = "VPPCOM_OK";
4500 break;
4501
4502 case VPPCOM_EAGAIN:
4503 st = "VPPCOM_EAGAIN";
4504 break;
4505
4506 case VPPCOM_EFAULT:
4507 st = "VPPCOM_EFAULT";
4508 break;
4509
4510 case VPPCOM_ENOMEM:
4511 st = "VPPCOM_ENOMEM";
4512 break;
4513
4514 case VPPCOM_EINVAL:
4515 st = "VPPCOM_EINVAL";
4516 break;
4517
4518 case VPPCOM_EBADFD:
4519 st = "VPPCOM_EBADFD";
4520 break;
4521
4522 case VPPCOM_EAFNOSUPPORT:
4523 st = "VPPCOM_EAFNOSUPPORT";
4524 break;
4525
4526 case VPPCOM_ECONNABORTED:
4527 st = "VPPCOM_ECONNABORTED";
4528 break;
4529
4530 case VPPCOM_ECONNRESET:
4531 st = "VPPCOM_ECONNRESET";
4532 break;
4533
4534 case VPPCOM_ENOTCONN:
4535 st = "VPPCOM_ENOTCONN";
4536 break;
4537
4538 case VPPCOM_ECONNREFUSED:
4539 st = "VPPCOM_ECONNREFUSED";
4540 break;
4541
4542 case VPPCOM_ETIMEDOUT:
4543 st = "VPPCOM_ETIMEDOUT";
4544 break;
4545
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304546 case VPPCOM_EADDRINUSE:
4547 st = "VPPCOM_EADDRINUSE";
4548 break;
4549
Florin Coras66ec4672020-06-15 07:59:40 -07004550 default:
4551 st = "UNKNOWN_STATE";
4552 break;
4553 }
4554
4555 return st;
4556}
4557
Florin Corasa5a9efd2021-01-05 17:03:29 -08004558int
4559vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4560{
4561 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004562 return vcl_sapi_add_cert_key_pair (ckpair);
4563 else
4564 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004565}
4566
4567int
4568vppcom_del_cert_key_pair (uint32_t ckpair_index)
4569{
4570 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004571 return vcl_sapi_del_cert_key_pair (ckpair_index);
4572 else
4573 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004574}
4575
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304576int
4577vppcom_session_get_error (uint32_t session_handle)
4578{
4579 vcl_worker_t *wrk = vcl_worker_get_current ();
4580 vcl_session_t *session = 0;
4581
4582 session = vcl_session_get_w_handle (wrk, session_handle);
4583 if (!session)
4584 return VPPCOM_EBADFD;
4585
4586 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4587 {
4588 VWRN ("epoll session %u! will not have connect", session->session_index);
4589 return VPPCOM_EBADFD;
4590 }
4591
4592 if (session->vpp_error == SESSION_E_PORTINUSE)
4593 return VPPCOM_EADDRINUSE;
4594 else if (session->vpp_error == SESSION_E_REFUSED)
4595 return VPPCOM_ECONNREFUSED;
4596 else if (session->vpp_error != SESSION_E_NONE)
4597 return VPPCOM_EFAULT;
4598 else
4599 return VPPCOM_OK;
4600}
4601
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004602int
4603vppcom_worker_is_detached (void)
4604{
4605 vcl_worker_t *wrk = vcl_worker_get_current ();
4606
4607 if (!vcm->cfg.use_mq_eventfd)
4608 return VPPCOM_ENOTSUP;
4609
4610 return wrk->api_client_handle == ~0;
4611}
4612
Dave Wallacee22aa742017-10-20 12:30:38 -04004613/*
4614 * fd.io coding-style-patch-verification: ON
4615 *
4616 * Local Variables:
4617 * eval: (c-set-style "gnu")
4618 * End:
4619 */