blob: e3fb14fa4e5ab138e217cdc4e64478e0aeba0d0d [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
Florin Coras5398dfb2021-01-25 20:31:27 -080028 u32 n_msgs = 0, sz, len;
Florin Coras30e79c22019-01-02 19:31:22 -080029
Florin Coras5398dfb2021-01-25 20:31:27 -080030 while ((sz = svm_msg_q_size (mq)))
Florin Coras30e79c22019-01-02 19:31:22 -080031 {
Florin Coras5398dfb2021-01-25 20:31:27 -080032 len = vec_len (wrk->mq_msg_vector);
33 vec_validate (wrk->mq_msg_vector, len + sz - 1);
34 svm_msg_q_sub_raw_batch (mq, wrk->mq_msg_vector + len, sz);
35 n_msgs += sz;
Florin Coras30e79c22019-01-02 19:31:22 -080036 }
37 return n_msgs;
38}
39
Dave Wallace543852a2017-08-03 02:11:34 -040040
Florin Coras697faea2018-06-27 17:10:49 -070041
Florin Coras458089b2019-08-21 16:20:44 -070042static void
Florin Coras4ac25842021-04-19 17:34:54 -070043vcl_msg_add_ext_config (vcl_session_t *s, uword *offset)
44{
45 svm_fifo_chunk_t *c;
46
47 c = vcl_segment_alloc_chunk (vcl_vpp_worker_segment_handle (0),
48 0 /* one slice only */, s->ext_config->len,
49 offset);
50 if (c)
51 clib_memcpy_fast (c->data, s->ext_config, s->ext_config->len);
52}
53
Florin Coras32881932023-02-06 13:30:13 -080054void
55vcl_send_session_listen (vcl_worker_t *wrk, vcl_session_t *s)
Florin Coras458089b2019-08-21 16:20:44 -070056{
57 app_session_evt_t _app_evt, *app_evt = &_app_evt;
58 session_listen_msg_t *mp;
59 svm_msg_q_t *mq;
60
61 mq = vcl_worker_ctrl_mq (wrk);
62 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
63 mp = (session_listen_msg_t *) app_evt->evt->data;
64 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070065 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070066 mp->context = s->session_index;
67 mp->wrk_index = wrk->vpp_wrk_index;
68 mp->is_ip4 = s->transport.is_ip4;
69 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
70 mp->port = s->transport.lcl_port;
71 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -080072 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +000073 if (s->flags & VCL_SESSION_F_CONNECTED)
74 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -070075 if (s->ext_config)
76 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -070077 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -070078 if (s->ext_config)
79 {
80 clib_mem_free (s->ext_config);
81 s->ext_config = 0;
82 }
Florin Coras32881932023-02-06 13:30:13 -080083 s->flags |= VCL_SESSION_F_PENDING_LISTEN;
Florin Coras458089b2019-08-21 16:20:44 -070084}
85
86static void
87vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
88{
89 app_session_evt_t _app_evt, *app_evt = &_app_evt;
90 session_connect_msg_t *mp;
91 svm_msg_q_t *mq;
92
93 mq = vcl_worker_ctrl_mq (wrk);
94 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
95 mp = (session_connect_msg_t *) app_evt->evt->data;
96 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -070097 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -070098 mp->context = s->session_index;
Filip Tehlar2f09bfc2021-11-15 10:26:56 +000099 mp->dscp = s->dscp;
Florin Coras458089b2019-08-21 16:20:44 -0700100 mp->wrk_index = wrk->vpp_wrk_index;
101 mp->is_ip4 = s->transport.is_ip4;
102 mp->parent_handle = s->parent_handle;
103 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700104 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700105 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000106 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700107 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800108 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000109 if (s->flags & VCL_SESSION_F_CONNECTED)
110 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700111 if (s->ext_config)
112 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700113 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700114
115 if (s->ext_config)
116 {
117 clib_mem_free (s->ext_config);
118 s->ext_config = 0;
119 }
Florin Coras458089b2019-08-21 16:20:44 -0700120}
121
122void
123vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
124{
125 app_session_evt_t _app_evt, *app_evt = &_app_evt;
126 session_unlisten_msg_t *mp;
127 svm_msg_q_t *mq;
128
129 mq = vcl_worker_ctrl_mq (wrk);
130 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
131 mp = (session_unlisten_msg_t *) app_evt->evt->data;
132 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700133 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700134 mp->wrk_index = wrk->vpp_wrk_index;
135 mp->handle = s->vpp_handle;
136 mp->context = wrk->wrk_index;
137 app_send_ctrl_evt_to_vpp (mq, app_evt);
138}
139
140static void
liuyacan534468e2021-05-09 03:50:40 +0000141vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
142{
143 app_session_evt_t _app_evt, *app_evt = &_app_evt;
144 session_shutdown_msg_t *mp;
145 svm_msg_q_t *mq;
146
147 /* Send to thread that owns the session */
148 mq = s->vpp_evt_q;
149 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
150 mp = (session_shutdown_msg_t *) app_evt->evt->data;
151 memset (mp, 0, sizeof (*mp));
152 mp->client_index = wrk->api_client_handle;
153 mp->handle = s->vpp_handle;
154 app_send_ctrl_evt_to_vpp (mq, app_evt);
155}
156
157static void
Florin Coras458089b2019-08-21 16:20:44 -0700158vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
159{
160 app_session_evt_t _app_evt, *app_evt = &_app_evt;
161 session_disconnect_msg_t *mp;
162 svm_msg_q_t *mq;
163
164 /* Send to thread that owns the session */
165 mq = s->vpp_evt_q;
166 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
167 mp = (session_disconnect_msg_t *) app_evt->evt->data;
168 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700169 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700170 mp->handle = s->vpp_handle;
171 app_send_ctrl_evt_to_vpp (mq, app_evt);
172}
173
174static void
175vcl_send_app_detach (vcl_worker_t * wrk)
176{
177 app_session_evt_t _app_evt, *app_evt = &_app_evt;
178 session_app_detach_msg_t *mp;
179 svm_msg_q_t *mq;
180
181 mq = vcl_worker_ctrl_mq (wrk);
182 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
183 mp = (session_app_detach_msg_t *) app_evt->evt->data;
184 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700185 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700186 app_send_ctrl_evt_to_vpp (mq, app_evt);
187}
Florin Coras697faea2018-06-27 17:10:49 -0700188
Florin Coras54693d22018-07-17 10:46:29 -0700189static void
190vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
191 session_handle_t handle, int retval)
192{
193 app_session_evt_t _app_evt, *app_evt = &_app_evt;
194 session_accepted_reply_msg_t *rmp;
195 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
196 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
197 rmp->handle = handle;
198 rmp->context = context;
199 rmp->retval = retval;
200 app_send_ctrl_evt_to_vpp (mq, app_evt);
201}
202
Florin Coras99368312018-08-02 10:45:44 -0700203static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800204vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
205 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700206{
207 app_session_evt_t _app_evt, *app_evt = &_app_evt;
208 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800209 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700210 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
211 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800212 rmp->handle = s->vpp_handle;
213 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700214 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800215 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700216}
217
Florin Corasc9fbd662018-08-24 12:59:56 -0700218static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800219vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
220 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700221{
222 app_session_evt_t _app_evt, *app_evt = &_app_evt;
223 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800224 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
225 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700226 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800227 rmp->handle = s->vpp_handle;
228 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700229 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800230 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700231}
232
Florin Coras30e79c22019-01-02 19:31:22 -0800233void
234vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
235 u32 wrk_index)
236{
237 app_session_evt_t _app_evt, *app_evt = &_app_evt;
238 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800239
Florin Coras52dd29f2020-11-18 19:02:17 -0800240 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
241 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800242 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700243 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800244 mp->handle = s->vpp_handle;
245 mp->req_wrk_index = wrk->vpp_wrk_index;
246 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800247 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800248}
249
hanlina3a48962020-07-13 11:09:15 +0800250int
Florin Coras40c07ce2020-07-16 20:46:17 -0700251vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
252{
253 app_session_evt_t _app_evt, *app_evt = &_app_evt;
254 session_app_wrk_rpc_msg_t *mp;
255 vcl_worker_t *dst_wrk, *wrk;
256 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800257 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700258
259 if (data_len > sizeof (mp->data))
260 goto done;
261
262 clib_spinlock_lock (&vcm->workers_lock);
263
264 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
265 if (!dst_wrk)
266 goto done;
267
268 wrk = vcl_worker_get_current ();
269 mq = vcl_worker_ctrl_mq (wrk);
270 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
271 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700272 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700273 mp->wrk_index = dst_wrk->vpp_wrk_index;
274 clib_memcpy (mp->data, data, data_len);
275 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800276 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700277
278done:
279 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800280 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700281}
282
Florin Coras04ae8272021-04-12 19:55:37 -0700283int
284vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
285 transport_endpt_attr_t *attr)
286{
287 app_session_evt_t _app_evt, *app_evt = &_app_evt;
288 session_transport_attr_msg_t *mp;
289 svm_msg_q_t *mq;
290 f64 timeout;
291
292 ASSERT (!wrk->session_attr_op);
Liangxing Wang22112772022-05-13 04:24:19 +0000293 mq = s->vpp_evt_q;
294 if (PREDICT_FALSE (!mq))
295 {
296 /* FIXME: attribute should be stored and sent once session is
297 * bound/connected to vpp */
298 return 0;
299 }
300
Florin Coras04ae8272021-04-12 19:55:37 -0700301 wrk->session_attr_op = 1;
302 wrk->session_attr_op_rv = -1;
303
Florin Coras04ae8272021-04-12 19:55:37 -0700304 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
305 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
306 memset (mp, 0, sizeof (*mp));
307 mp->client_index = wrk->api_client_handle;
308 mp->handle = s->vpp_handle;
309 mp->is_get = is_get;
310 mp->attr = *attr;
311 app_send_ctrl_evt_to_vpp (mq, app_evt);
312
313 timeout = clib_time_now (&wrk->clib_time) + 1;
314
315 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
316 vcl_flush_mq_events ();
317
318 if (!wrk->session_attr_op_rv && is_get)
319 *attr = wrk->session_attr_rv;
320
321 wrk->session_attr_op = 0;
322
323 return wrk->session_attr_op_rv;
324}
325
Florin Coras54693d22018-07-17 10:46:29 -0700326static u32
Florin Coras00cca802019-06-06 09:38:44 -0700327vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
328 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700329{
330 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700331 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700332
Florin Coras134a9962018-08-28 11:32:04 -0700333 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700334
Florin Coras00cca802019-06-06 09:38:44 -0700335 listen_session = vcl_session_get (wrk, ls_index);
336 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700337 {
Florin Coras00cca802019-06-06 09:38:44 -0700338 VDBG (0, "ERROR: listener handle %lu does not match session %u",
339 mp->listener_handle, ls_index);
340 goto error;
341 }
342
Florin Corasf6e284b2021-07-21 18:17:20 -0700343 if (vcl_segment_attach_session (
344 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
345 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700346 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800347 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
348 session->session_index, mp->handle);
Florin Coras00cca802019-06-06 09:38:44 -0700349 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700350 }
351
Florin Coras54693d22018-07-17 10:46:29 -0700352 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700353 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700354 session->transport.rmt_port = mp->rmt.port;
355 session->transport.is_ip4 = mp->rmt.is_ip4;
356 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500357 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700358
Florin Coras134a9962018-08-28 11:32:04 -0700359 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800360 session->transport.lcl_port = mp->lcl.port;
361 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700362 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200363 session->is_dgram = vcl_proto_is_dgram (session->session_type);
364 session->listener_index = listen_session->session_index;
365 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700366
Florin Coras54693d22018-07-17 10:46:29 -0700367 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
368
Florin Coras00cca802019-06-06 09:38:44 -0700369 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
370 session->vpp_handle, 0);
371
Florin Coras134a9962018-08-28 11:32:04 -0700372 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700373
374error:
Florin Corasb4624182020-12-11 13:58:12 -0800375 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
376 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700377 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
378 VNET_API_ERROR_INVALID_ARGUMENT);
379 vcl_session_free (wrk, session);
380 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700381}
382
383static u32
Florin Coras134a9962018-08-28 11:32:04 -0700384vcl_session_connected_handler (vcl_worker_t * wrk,
385 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700386{
Florin Coras99368312018-08-02 10:45:44 -0700387 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800388 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700389
390 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700391 session = vcl_session_get (wrk, session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800392 if (PREDICT_FALSE (!session))
Florin Coras070453d2018-08-24 17:04:27 -0700393 {
Florin Coras8d42c752021-11-29 23:26:19 -0800394 VERR ("vpp handle 0x%llx has no session index (%u)!", mp->handle,
395 session_index);
Florin Corasef2c0f12021-11-10 22:44:52 -0800396 /* Should not happen but if it does, force vpp session cleanup */
397 vcl_session_t tmp_session = {
398 .vpp_handle = mp->handle,
399 .vpp_evt_q = 0,
400 };
401 vcl_segment_attach_session (
402 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
403 mp->vpp_event_queue_address, mp->mq_index, 0, session);
404 if (tmp_session.vpp_evt_q)
405 vcl_send_session_disconnect (wrk, &tmp_session);
Florin Coras070453d2018-08-24 17:04:27 -0700406 return VCL_INVALID_SESSION_INDEX;
407 }
Florin Coras8d42c752021-11-29 23:26:19 -0800408
Florin Coras54693d22018-07-17 10:46:29 -0700409 if (mp->retval)
410 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800411 VDBG (0, "session %u: connect failed! %U", session_index,
Florin Coras8d42c752021-11-29 23:26:19 -0800412 format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700413 session->session_state = VCL_STATE_DETACHED;
Florin Coras8d42c752021-11-29 23:26:19 -0800414 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +0530415 session->vpp_error = mp->retval;
Florin Coras070453d2018-08-24 17:04:27 -0700416 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700417 }
418
Florin Corasdbc9c592019-09-25 16:37:43 -0700419 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800420
Florin Coras8d42c752021-11-29 23:26:19 -0800421 /* Add to lookup table. Even if something fails, session cannot be
422 * cleaned up prior to notifying vpp and going through the cleanup
423 * "procedure" see @ref vcl_session_cleanup_handler */
424 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
425
Florin Corasf6e284b2021-07-21 18:17:20 -0700426 if (vcl_segment_attach_session (
427 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
428 mp->vpp_event_queue_address, mp->mq_index, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800429 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800430 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
431 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800432 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700433 vcl_send_session_disconnect (wrk, session);
434 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800435 }
436
Florin Coras653e43f2019-03-04 10:56:23 -0800437 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700438 {
Florin Corasc547e912020-12-08 17:50:45 -0800439 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700440 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
441 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800442 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800443 VDBG (0, "session %u [0x%llx]: failed to attach ct fifos",
444 session->session_index, session->vpp_handle);
Florin Coras8d42c752021-11-29 23:26:19 -0800445 session->session_state = VCL_STATE_UPDATED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700446 vcl_send_session_disconnect (wrk, session);
447 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800448 }
Florin Coras99368312018-08-02 10:45:44 -0700449 }
Florin Coras54693d22018-07-17 10:46:29 -0700450
Florin Coras09d18c22019-04-24 11:10:02 -0700451 session->transport.is_ip4 = mp->lcl.is_ip4;
452 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500453 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700454 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700455
456 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700457 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700458 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700459 vcl_send_session_disconnect (wrk, session);
460 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700461 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700462
Florin Coras5ffb9642021-12-03 17:24:13 -0800463 VDBG (0, "session %u [0x%llx] connected local: %U:%u remote %U:%u",
464 session->session_index, session->vpp_handle, vcl_format_ip46_address,
465 &session->transport.lcl_ip,
466 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
467 clib_net_to_host_u16 (session->transport.lcl_port),
468 vcl_format_ip46_address, &session->transport.rmt_ip,
469 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
470 clib_net_to_host_u16 (session->transport.rmt_port));
Florin Coras070453d2018-08-24 17:04:27 -0700471
Florin Coras54693d22018-07-17 10:46:29 -0700472 return session_index;
473}
474
Florin Coras3c7d4f92018-12-14 11:28:43 -0800475static int
476vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
477{
478 vcl_session_msg_t *accepted_msg;
479 int i;
480
481 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
482 {
483 accepted_msg = &session->accept_evts_fifo[i];
484 if (accepted_msg->accepted_msg.handle == handle)
485 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800486 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800487 return 1;
488 }
489 }
490 return 0;
491}
492
Florin Corasc9fbd662018-08-24 12:59:56 -0700493static u32
Florin Coras134a9962018-08-28 11:32:04 -0700494vcl_session_reset_handler (vcl_worker_t * wrk,
495 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700496{
497 vcl_session_t *session;
498 u32 sid;
499
Florin Coras134a9962018-08-28 11:32:04 -0700500 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
501 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700502 if (!session)
503 {
504 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
505 return VCL_INVALID_SESSION_INDEX;
506 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800507
508 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700509 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800510 {
511
512 if (!vcl_flag_accepted_session (session, reset_msg->handle,
513 VCL_ACCEPTED_F_RESET))
514 VDBG (0, "session was not accepted!");
515 return VCL_INVALID_SESSION_INDEX;
516 }
517
Florin Corasc127d5a2020-10-14 16:35:58 -0700518 if (session->session_state != VCL_STATE_CLOSED)
519 session->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +0800520
521 session->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Coras5ffb9642021-12-03 17:24:13 -0800522 VDBG (0, "session %u [0x%llx]: reset", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700523 return sid;
524}
525
Florin Coras60116992018-08-27 09:52:18 -0700526static u32
Florin Coras134a9962018-08-28 11:32:04 -0700527vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700528{
529 vcl_session_t *session;
530 u32 sid = mp->context;
531
Florin Coras134a9962018-08-28 11:32:04 -0700532 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700533 if (mp->retval)
534 {
Florin Coras5e062572019-03-14 19:07:51 -0700535 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700536 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700537 if (session)
538 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700539 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700540 session->vpp_handle = mp->handle;
541 return sid;
542 }
543 else
544 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800545 VDBG (0, "session %u [0x%llx]: Invalid session index!", sid,
546 mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700547 return VCL_INVALID_SESSION_INDEX;
548 }
549 }
550
551 session->vpp_handle = mp->handle;
552 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500553 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
554 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700555 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700556 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700557 session->session_state = VCL_STATE_LISTEN;
Florin Coras32881932023-02-06 13:30:13 -0800558 session->flags &= ~VCL_SESSION_F_PENDING_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800559
Florin Coras6e3c1f82020-01-15 01:30:46 +0000560 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700561 {
Florin Corasc547e912020-12-08 17:50:45 -0800562 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700563 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
564 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800565 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800566 VDBG (0, "session %u [0x%llx]: failed to attach fifos",
567 session->session_index, session->vpp_handle);
Florin Corasc547e912020-12-08 17:50:45 -0800568 session->session_state = VCL_STATE_DETACHED;
569 return VCL_INVALID_SESSION_INDEX;
570 }
Florin Coras60116992018-08-27 09:52:18 -0700571 }
572
Florin Coras05ecfcc2018-12-12 18:19:39 -0800573 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700574 return sid;
575}
576
Florin Corasdfae9f92019-02-20 19:48:31 -0800577static void
578vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
579{
580 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
581 vcl_session_t *s;
582
583 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000584 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800585 {
586 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
587 return;
588 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700589 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000590 {
591 /* Connected udp listener */
592 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700593 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000594 return;
595
596 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
597 return;
598 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800599
600 if (mp->retval)
601 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700602 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800603
604 if (mp->context != wrk->wrk_index)
605 VDBG (0, "wrong context");
606
607 vcl_session_table_del_vpp_handle (wrk, mp->handle);
608 vcl_session_free (wrk, s);
609}
610
Florin Coras68b7e582020-01-21 18:33:23 -0800611static void
612vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
613{
614 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
615 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800616 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800617
618 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
619 if (!s)
620 {
621 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
622 return;
623 }
624
Florin Coras1bb74942021-02-10 15:26:37 -0800625 /* Only validate if a value is provided */
626 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800627 {
Florin Coras1bb74942021-02-10 15:26:37 -0800628 fs_index = vcl_segment_table_lookup (mp->segment_handle);
629 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
630 {
631 VDBG (0, "segment %lx for session %u is not mounted!",
632 mp->segment_handle, s->session_index);
633 s->session_state = VCL_STATE_DETACHED;
634 return;
635 }
Florin Corasc547e912020-12-08 17:50:45 -0800636 }
637
Florin Coras57660d92020-04-04 22:45:34 +0000638 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800639
640 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
641 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800642
Florin Coras68b7e582020-01-21 18:33:23 -0800643 vcl_session_table_del_vpp_handle (wrk, mp->handle);
644 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
645
646 /* Generate new tx event if we have outstanding data */
647 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800648 app_send_io_evt_to_vpp (s->vpp_evt_q,
649 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800650 SESSION_IO_EVT_TX, SVM_Q_WAIT);
651
Florin Coras57660d92020-04-04 22:45:34 +0000652 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800653 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800654}
655
Florin Coras3c7d4f92018-12-14 11:28:43 -0800656static vcl_session_t *
657vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
658{
659 vcl_session_msg_t *vcl_msg;
660 vcl_session_t *session;
661
662 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
663 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800664 VWRN ("session overlap handle %lu state %u!", msg->handle,
665 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800666
667 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
668 if (!session)
669 {
670 VERR ("couldn't find listen session: listener handle %llx",
671 msg->listener_handle);
672 return 0;
673 }
674
675 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000676 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800677 vcl_msg->accepted_msg = *msg;
678 /* Session handle points to listener until fully accepted by app */
679 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
680
681 return session;
682}
683
684static vcl_session_t *
685vcl_session_disconnected_handler (vcl_worker_t * wrk,
686 session_disconnected_msg_t * msg)
687{
688 vcl_session_t *session;
689
690 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
691 if (!session)
692 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800693 VWRN ("request to disconnect unknown handle 0x%llx", msg->handle);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800694 return 0;
695 }
696
Florin Corasadcfb152020-02-12 08:50:29 +0000697 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700698 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000699 return 0;
700
Florin Coras3c7d4f92018-12-14 11:28:43 -0800701 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700702 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800703 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800704 if (!vcl_flag_accepted_session (session, msg->handle,
705 VCL_ACCEPTED_F_CLOSED))
706 VDBG (0, "session was not accepted!");
707 return 0;
708 }
709
Florin Corasadcfb152020-02-12 08:50:29 +0000710 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700711 if (session->session_state != VCL_STATE_DISCONNECT)
712 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000713
Florin Coras3c7d4f92018-12-14 11:28:43 -0800714 return session;
715}
716
liuyacan534468e2021-05-09 03:50:40 +0000717int
liuyacan55c952e2021-06-13 14:54:55 +0800718vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000719{
720 vcl_worker_t *wrk = vcl_worker_get_current ();
721 vcl_session_t *session;
722 vcl_session_state_t state;
723 u64 vpp_handle;
724
725 session = vcl_session_get_w_handle (wrk, session_handle);
726 if (PREDICT_FALSE (!session))
727 return VPPCOM_EBADFD;
728
729 vpp_handle = session->vpp_handle;
730 state = session->session_state;
731
732 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
Florin Coras60687192021-11-29 21:00:47 -0800733 vpp_handle, state, vcl_session_state_str (state));
liuyacan534468e2021-05-09 03:50:40 +0000734
735 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
736 {
737 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
738 return VPPCOM_EBADFD;
739 }
740
liuyacan55c952e2021-06-13 14:54:55 +0800741 if (how == SHUT_RD || how == SHUT_RDWR)
742 {
743 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
744 if (how == SHUT_RD)
745 return VPPCOM_OK;
746 }
747 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
748
liuyacan534468e2021-05-09 03:50:40 +0000749 if (PREDICT_TRUE (state == VCL_STATE_READY))
750 {
751 VDBG (1, "session %u [0x%llx]: sending shutdown...",
752 session->session_index, vpp_handle);
753
754 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000755 }
756
757 return VPPCOM_OK;
758}
759
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700760static int
761vppcom_session_disconnect (u32 session_handle)
762{
763 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700764 vcl_session_t *session, *listen_session;
765 vcl_session_state_t state;
766 u64 vpp_handle;
767
768 session = vcl_session_get_w_handle (wrk, session_handle);
769 if (!session)
770 return VPPCOM_EBADFD;
771
772 vpp_handle = session->vpp_handle;
773 state = session->session_state;
774
Florin Coras5ffb9642021-12-03 17:24:13 -0800775 VDBG (1, "session %u [0x%llx]: disconnecting state (%s)",
776 session->session_index, vpp_handle, vcl_session_state_str (state));
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700777
778 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
779 {
780 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
781 return VPPCOM_EBADFD;
782 }
783
784 if (state == VCL_STATE_VPP_CLOSING)
785 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800786 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700787 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
788 session->session_index, vpp_handle);
789 }
790 else
791 {
792 /* Session doesn't have an event queue yet. Probably a non-blocking
793 * connect. Wait for the reply */
794 if (PREDICT_FALSE (!session->vpp_evt_q))
795 return VPPCOM_OK;
796
Florin Coras5ffb9642021-12-03 17:24:13 -0800797 VDBG (1, "session %u [0x%llx]: sending disconnect",
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700798 session->session_index, vpp_handle);
799 vcl_send_session_disconnect (wrk, session);
800 }
801
802 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
803 {
804 listen_session = vcl_session_get (wrk, session->listener_index);
Florin Corasb52bd3a2022-06-28 20:01:20 -0700805 if (listen_session)
806 listen_session->n_accepted_sessions--;
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700807 }
808
809 return VPPCOM_OK;
810}
811
Florin Coras30e79c22019-01-02 19:31:22 -0800812static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700813vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
814{
815 session_cleanup_msg_t *msg;
816 vcl_session_t *session;
817
818 msg = (session_cleanup_msg_t *) data;
819 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
820 if (!session)
821 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800822 VWRN ("disconnect confirmed for unknown handle 0x%llx", msg->handle);
Florin Coras9ace36d2019-10-28 13:14:17 -0700823 return;
824 }
825
Florin Coras36d49392020-04-24 23:00:11 +0000826 if (msg->type == SESSION_CLEANUP_TRANSPORT)
827 {
828 /* Transport was cleaned up before we confirmed close. Probably the
829 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700830 * Confirm close to make sure everything is cleaned up.
831 * Move to undetermined state to ensure that the session is not
832 * removed before both vpp and the app cleanup.
833 * - If the app closes first, the session is moved to CLOSED state
834 * and the session cleanup notification from vpp removes the
835 * session.
836 * - If vpp cleans up the session first, the session is moved to
837 * DETACHED state lower and subsequently the close from the app
838 * frees the session
839 */
840 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700841 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700842 vppcom_session_disconnect (vcl_session_handle (session));
843 session->session_state = VCL_STATE_UPDATED;
844 }
845 else if (session->session_state == VCL_STATE_DISCONNECT)
846 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800847 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700848 session->session_state = VCL_STATE_UPDATED;
849 }
Florin Coras36d49392020-04-24 23:00:11 +0000850 return;
851 }
852
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800853 /* VPP will reuse the handle so clean it up now */
Florin Coras9ace36d2019-10-28 13:14:17 -0700854 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800855
856 /* App did not close the connection yet so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700857 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000858 {
Florin Coras5ffb9642021-12-03 17:24:13 -0800859 VDBG (0, "session %u: app did not close", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700860 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000861 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
862 return;
863 }
Florin Corasf0fe1ea2021-12-03 17:03:37 -0800864
865 /* Session probably tracked with epoll, disconnect not yet handled and
866 * 1) both transport and session cleanup completed 2) app closed. Wait
867 * until message is drained to free the session.
868 * See @ref vcl_handle_mq_event */
869 if (session->flags & VCL_SESSION_F_PENDING_DISCONNECT)
870 {
871 session->flags |= VCL_SESSION_F_PENDING_FREE;
872 return;
873 }
874
Florin Coras9ace36d2019-10-28 13:14:17 -0700875 vcl_session_free (wrk, session);
876}
877
878static void
Florin Coras30e79c22019-01-02 19:31:22 -0800879vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
880{
881 session_req_worker_update_msg_t *msg;
882 vcl_session_t *s;
883
884 msg = (session_req_worker_update_msg_t *) data;
885 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
886 if (!s)
887 return;
888
889 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
890}
891
892static void
893vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
894{
895 session_worker_update_reply_msg_t *msg;
896 vcl_session_t *s;
897
898 msg = (session_worker_update_reply_msg_t *) data;
899 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
900 if (!s)
901 {
902 VDBG (0, "unknown handle 0x%llx", msg->handle);
903 return;
904 }
Florin Coras30e79c22019-01-02 19:31:22 -0800905
Florin Coras5f45e012019-01-23 09:21:30 -0800906 if (s->rx_fifo)
907 {
Florin Corasc547e912020-12-08 17:50:45 -0800908 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700909 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800910 {
911 VDBG (0, "failed to attach fifos for %u", s->session_index);
912 return;
913 }
Florin Coras5f45e012019-01-23 09:21:30 -0800914 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700915 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800916
Florin Coras30e79c22019-01-02 19:31:22 -0800917 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
918 s->vpp_handle, wrk->wrk_index);
919}
920
Florin Coras935ce752020-09-08 22:43:47 -0700921static int
922vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
923{
924
925 if (vcm->cfg.vpp_app_socket_api)
926 return vcl_sapi_recv_fds (wrk, fds, n_fds);
927
928 return vcl_bapi_recv_fds (wrk, fds, n_fds);
929}
930
Florin Corasc4c4cf52019-08-24 18:17:34 -0700931static void
932vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
933{
934 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
935 session_app_add_segment_msg_t *msg;
936 u64 segment_handle;
937 int fd = -1;
938
939 msg = (session_app_add_segment_msg_t *) data;
940
941 if (msg->fd_flags)
942 {
Florin Coras935ce752020-09-08 22:43:47 -0700943 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700944 seg_type = SSVM_SEGMENT_MEMFD;
945 }
946
947 segment_handle = msg->segment_handle;
948 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
949 {
950 clib_warning ("invalid segment handle");
951 return;
952 }
953
954 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
955 seg_type, fd))
956 {
957 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
958 return;
959 }
960
961 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
962 msg->segment_size);
963}
964
965static void
966vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
967{
968 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
969 vcl_segment_detach (msg->segment_handle);
970 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
971}
972
Florin Coras40c07ce2020-07-16 20:46:17 -0700973static void
974vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
975{
976 if (!vcm->wrk_rpc_fn)
977 return;
978
hanlina3a48962020-07-13 11:09:15 +0800979 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700980}
981
Florin Coras04ae8272021-04-12 19:55:37 -0700982static void
983vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
984{
985 session_transport_attr_reply_msg_t *mp;
986
987 if (!wrk->session_attr_op)
988 return;
989
990 mp = (session_transport_attr_reply_msg_t *) data;
991
992 wrk->session_attr_op_rv = mp->retval;
993 wrk->session_attr_op = 0;
994 wrk->session_attr_rv = mp->attr;
995}
996
Florin Coras86f04502018-09-12 16:08:01 -0700997static int
998vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700999{
Florin Coras54693d22018-07-17 10:46:29 -07001000 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001001 session_connected_msg_t *connected_msg;
1002 session_reset_msg_t *reset_msg;
1003 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001004 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001005 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001006
1007 switch (e->event_type)
1008 {
Florin Coras653e43f2019-03-04 10:56:23 -08001009 case SESSION_IO_EVT_RX:
1010 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001011 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001012 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001013 break;
Florin Coras86f04502018-09-12 16:08:01 -07001014 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001015 break;
Florin Corasb242d312020-10-26 15:35:40 -07001016 case SESSION_CTRL_EVT_BOUND:
1017 /* We can only wait for only one listen so not postponed */
1018 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1019 break;
Florin Coras54693d22018-07-17 10:46:29 -07001020 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001021 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1022 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1023 {
1024 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1025 *ecpy = *e;
1026 ecpy->postponed = 1;
1027 ecpy->session_index = s->session_index;
1028 }
Florin Coras54693d22018-07-17 10:46:29 -07001029 break;
1030 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001031 connected_msg = (session_connected_msg_t *) e->data;
1032 sid = vcl_session_connected_handler (wrk, connected_msg);
1033 if (!(s = vcl_session_get (wrk, sid)))
1034 break;
1035 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1036 {
1037 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1038 *ecpy = *e;
1039 ecpy->postponed = 1;
1040 ecpy->session_index = s->session_index;
1041 }
Florin Coras54693d22018-07-17 10:46:29 -07001042 break;
1043 case SESSION_CTRL_EVT_DISCONNECTED:
1044 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001045 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1046 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001047 if (s->session_state == VCL_STATE_CLOSED)
1048 break;
Florin Corasb242d312020-10-26 15:35:40 -07001049 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1050 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001051 s->session_state = VCL_STATE_VPP_CLOSING;
1052 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1053 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1054 *ecpy = *e;
1055 ecpy->postponed = 1;
1056 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001057 break;
1058 }
1059 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001060 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001061 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1062 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001063 break;
1064 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001065 reset_msg = (session_reset_msg_t *) e->data;
1066 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1067 break;
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001068 if (s->session_state == VCL_STATE_CLOSED)
1069 break;
Florin Corasb242d312020-10-26 15:35:40 -07001070 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1071 {
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001072 s->flags |= VCL_SESSION_F_PENDING_DISCONNECT;
1073 s->session_state = VCL_STATE_DISCONNECT;
Yacan Liuf5e0a172022-09-20 14:19:19 +08001074 s->flags |= (VCL_SESSION_F_RD_SHUTDOWN | VCL_SESSION_F_WR_SHUTDOWN);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001075 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1076 *ecpy = *e;
1077 ecpy->postponed = 1;
1078 ecpy->session_index = s->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07001079 break;
1080 }
Florin Coras134a9962018-08-28 11:32:04 -07001081 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001082 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001083 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1084 vcl_session_unlisten_reply_handler (wrk, e->data);
1085 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001086 case SESSION_CTRL_EVT_MIGRATED:
1087 vcl_session_migrated_handler (wrk, e->data);
1088 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001089 case SESSION_CTRL_EVT_CLEANUP:
1090 vcl_session_cleanup_handler (wrk, e->data);
1091 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001092 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1093 vcl_session_req_worker_update_handler (wrk, e->data);
1094 break;
1095 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1096 vcl_session_worker_update_reply_handler (wrk, e->data);
1097 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001098 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1099 vcl_session_app_add_segment_handler (wrk, e->data);
1100 break;
1101 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1102 vcl_session_app_del_segment_handler (wrk, e->data);
1103 break;
hanlina3a48962020-07-13 11:09:15 +08001104 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001105 vcl_worker_rpc_handler (wrk, e->data);
1106 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001107 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1108 vcl_session_transport_attr_reply_handler (wrk, e->data);
1109 break;
Florin Coras54693d22018-07-17 10:46:29 -07001110 default:
1111 clib_warning ("unhandled %u", e->event_type);
1112 }
1113 return VPPCOM_OK;
1114}
1115
Florin Coras30e79c22019-01-02 19:31:22 -08001116static int
Florin Coras697faea2018-06-27 17:10:49 -07001117vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001118 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001119 f64 wait_for_time)
1120{
Florin Coras134a9962018-08-28 11:32:04 -07001121 vcl_worker_t *wrk = vcl_worker_get_current ();
1122 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001123 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001124 svm_msg_q_msg_t msg;
1125 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001126
Florin Coras697faea2018-06-27 17:10:49 -07001127 do
Dave Wallace543852a2017-08-03 02:11:34 -04001128 {
Florin Coras134a9962018-08-28 11:32:04 -07001129 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001130 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001131 {
Florin Coras070453d2018-08-24 17:04:27 -07001132 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001133 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001134 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001135 {
Florin Coras697faea2018-06-27 17:10:49 -07001136 return VPPCOM_OK;
1137 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001138 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001139 {
Florin Coras697faea2018-06-27 17:10:49 -07001140 return VPPCOM_ECONNREFUSED;
1141 }
Florin Coras54693d22018-07-17 10:46:29 -07001142
Florin Coras134a9962018-08-28 11:32:04 -07001143 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001144 {
1145 usleep (100);
1146 continue;
1147 }
Florin Coras134a9962018-08-28 11:32:04 -07001148 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001149 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001150 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001151 }
Florin Coras134a9962018-08-28 11:32:04 -07001152 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001153
Florin Coras05ecfcc2018-12-12 18:19:39 -08001154 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras60687192021-11-29 21:00:47 -08001155 vcl_session_state_str (state));
Florin Coras697faea2018-06-27 17:10:49 -07001156 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001157
Florin Coras697faea2018-06-27 17:10:49 -07001158 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001159}
1160
Florin Coras30e79c22019-01-02 19:31:22 -08001161static void
1162vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1163{
Florin Coras288eaab2019-02-03 15:26:14 -08001164 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001165 vcl_session_t *s;
1166 u32 *sip;
1167
1168 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1169 return;
1170
1171 vec_foreach (sip, wrk->pending_session_wrk_updates)
1172 {
1173 s = vcl_session_get (wrk, *sip);
1174 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1175 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001176 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1177 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001178 s->session_state = state;
1179 }
1180 vec_reset_length (wrk->pending_session_wrk_updates);
1181}
1182
Florin Corasf9240dc2019-01-15 08:03:17 -08001183void
Florin Coras5398dfb2021-01-25 20:31:27 -08001184vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001185{
Florin Coras30e79c22019-01-02 19:31:22 -08001186 svm_msg_q_msg_t *msg;
1187 session_event_t *e;
1188 svm_msg_q_t *mq;
1189 int i;
1190
1191 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001192 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001193
1194 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1195 {
1196 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1197 e = svm_msg_q_msg_data (mq, msg);
1198 vcl_handle_mq_event (wrk, e);
1199 svm_msg_q_free_msg (mq, msg);
1200 }
1201 vec_reset_length (wrk->mq_msg_vector);
1202 vcl_handle_pending_wrk_updates (wrk);
1203}
1204
Florin Coras5398dfb2021-01-25 20:31:27 -08001205void
1206vcl_flush_mq_events (void)
1207{
1208 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1209}
1210
Florin Coras697faea2018-06-27 17:10:49 -07001211static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001212vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001213{
Florin Coras134a9962018-08-28 11:32:04 -07001214 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001215 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001216 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001217 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001218
Florin Corasab2f6db2018-08-31 14:31:41 -07001219 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001220 if (!session)
1221 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001222
Florin Corasaaff5ee2019-07-08 13:00:00 -07001223 /* Flush pending accept events, if any */
1224 while (clib_fifo_elts (session->accept_evts_fifo))
1225 {
1226 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1227 accepted_msg = &evt->accepted_msg;
1228 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1229 vcl_send_session_accepted_reply (session->vpp_evt_q,
1230 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001231 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001232 }
1233 clib_fifo_free (session->accept_evts_fifo);
1234
Florin Coras458089b2019-08-21 16:20:44 -07001235 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001236
Florin Coras32881932023-02-06 13:30:13 -08001237 VDBG (0, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001238 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001239 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001240
1241 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001242 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001243
Florin Coras070453d2018-08-24 17:04:27 -07001244 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001245}
1246
Florin Coras940f78f2018-11-30 12:11:20 -08001247/**
1248 * Handle app exit
1249 *
1250 * Notify vpp of the disconnect and mark the worker as free. If we're the
1251 * last worker, do a full cleanup otherwise, since we're probably a forked
1252 * child, avoid syscalls as much as possible. We might've lost privileges.
1253 */
1254void
1255vppcom_app_exit (void)
1256{
1257 if (!pool_elts (vcm->workers))
1258 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001259 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1260 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001261 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001262}
1263
Florin Coras935ce752020-09-08 22:43:47 -07001264static int
1265vcl_api_attach (void)
1266{
1267 if (vcm->cfg.vpp_app_socket_api)
1268 return vcl_sapi_attach ();
1269
1270 return vcl_bapi_attach ();
1271}
1272
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001273int
1274vcl_is_first_reattach_to_execute ()
1275{
1276 if (vcm->reattach_count == 0)
1277 return 1;
1278
1279 return 0;
1280}
1281
1282void
1283vcl_set_reattach_counter ()
1284{
1285 ++vcm->reattach_count;
1286
1287 if (vcm->reattach_count == vec_len (vcm->workers))
1288 vcm->reattach_count = 0;
1289}
1290
1291/**
1292 * Reattach vcl to vpp after it has previously been disconnected.
1293 *
1294 * The logic should be:
1295 * - first worker to hit `vcl_api_retry_attach` should attach to vpp,
1296 * to reproduce the `vcl_api_attach` in `vppcom_app_create`.
1297 * - the rest of the workers should `reproduce vcl_worker_register_with_vpp`
1298 * from `vppcom_worker_register` since they were already allocated.
1299 */
1300
Florin Coras935ce752020-09-08 22:43:47 -07001301static void
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001302vcl_api_retry_attach (vcl_worker_t *wrk)
1303{
1304 vcl_session_t *s;
1305
Maros Ondrejicka0db15752022-10-12 22:58:01 +02001306 clib_spinlock_lock (&vcm->workers_lock);
1307 if (vcl_is_first_reattach_to_execute ())
1308 {
1309 if (vcl_api_attach ())
1310 {
1311 clib_spinlock_unlock (&vcm->workers_lock);
1312 return;
1313 }
1314 vcl_set_reattach_counter ();
1315 clib_spinlock_unlock (&vcm->workers_lock);
1316 }
1317 else
1318 {
1319 vcl_set_reattach_counter ();
1320 clib_spinlock_unlock (&vcm->workers_lock);
1321 vcl_worker_register_with_vpp ();
1322 }
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00001323
1324 /* Treat listeners as configuration that needs to be re-added to vpp */
1325 pool_foreach (s, wrk->sessions)
1326 {
1327 if (s->flags & VCL_SESSION_F_IS_VEP)
1328 continue;
1329 if (s->session_state == VCL_STATE_LISTEN_NO_MQ)
1330 vppcom_session_listen (vcl_session_handle (s), 10);
1331 else
1332 VDBG (0, "internal error: unexpected state %d", s->session_state);
1333 }
1334}
1335
1336static void
1337vcl_api_handle_disconnect (vcl_worker_t *wrk)
1338{
1339 wrk->api_client_handle = ~0;
1340 vcl_worker_detach_sessions (wrk);
1341}
1342
1343static void
Florin Coras935ce752020-09-08 22:43:47 -07001344vcl_api_detach (vcl_worker_t * wrk)
1345{
Florin Corasd04ea442022-03-30 16:08:25 -07001346 if (wrk->api_client_handle == ~0)
1347 return;
1348
Florin Coras935ce752020-09-08 22:43:47 -07001349 vcl_send_app_detach (wrk);
1350
1351 if (vcm->cfg.vpp_app_socket_api)
1352 return vcl_sapi_detach (wrk);
1353
1354 return vcl_bapi_disconnect_from_vpp ();
1355}
1356
Dave Wallace543852a2017-08-03 02:11:34 -04001357/*
1358 * VPPCOM Public API functions
1359 */
1360int
Florin Coras66ec4672020-06-15 07:59:40 -07001361vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001362{
Dave Wallace543852a2017-08-03 02:11:34 -04001363 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001364 int rv;
1365
Florin Coras47c40e22018-11-26 17:01:36 -08001366 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001367 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001368 VDBG (1, "already initialized");
1369 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001370 }
1371
Florin Coras47c40e22018-11-26 17:01:36 -08001372 vcm->is_init = 1;
1373 vppcom_cfg (&vcm->cfg);
1374 vcl_cfg = &vcm->cfg;
1375
1376 vcm->main_cpu = pthread_self ();
1377 vcm->main_pid = getpid ();
1378 vcm->app_name = format (0, "%s", app_name);
Florin Coras41bc8612021-10-01 14:57:03 -07001379 fifo_segment_main_init (&vcm->segment_main, (uword) ~0,
1380 20 /* timeout in secs */);
Florin Coras47c40e22018-11-26 17:01:36 -08001381 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1382 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001383 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001384 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001385 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001386
1387 /* Allocate default worker */
1388 vcl_worker_alloc_and_init ();
1389
Florin Coras935ce752020-09-08 22:43:47 -07001390 if ((rv = vcl_api_attach ()))
Florin Corasd04ea442022-03-30 16:08:25 -07001391 {
1392 vppcom_app_destroy ();
1393 return rv;
1394 }
Florin Coras47c40e22018-11-26 17:01:36 -08001395
1396 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001397 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001398
1399 return VPPCOM_OK;
1400}
1401
1402void
1403vppcom_app_destroy (void)
1404{
Florin Corasdc0ded72020-04-30 02:59:55 +00001405 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001406 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001407
Florin Coras940f78f2018-11-30 12:11:20 -08001408 if (!pool_elts (vcm->workers))
1409 return;
1410
Florin Coras0d427d82018-06-27 03:24:07 -07001411 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001412
Florin Corasdc0ded72020-04-30 02:59:55 +00001413 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001414
Florin Corasce815de2020-04-16 18:47:27 +00001415 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001416 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001417 if (current_wrk != wrk)
1418 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001419 }
Florin Corasce815de2020-04-16 18:47:27 +00001420 /* *INDENT-ON* */
1421
Florin Coras935ce752020-09-08 22:43:47 -07001422 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001423 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
Florin Corasd04ea442022-03-30 16:08:25 -07001424 vcl_set_worker_index (~0);
Florin Corasdc0ded72020-04-30 02:59:55 +00001425
Florin Coras0d427d82018-06-27 03:24:07 -07001426 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001427
1428 /*
1429 * Free the heap and fix vcm
1430 */
1431 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001432 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001433
1434 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001435 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001436}
1437
1438int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001439vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001440{
Florin Coras134a9962018-08-28 11:32:04 -07001441 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001442 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001443
Florin Coras134a9962018-08-28 11:32:04 -07001444 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001445
Florin Coras7e12d942018-06-27 14:32:43 -07001446 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001447 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001448 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001449 session->is_dgram = vcl_proto_is_dgram (proto);
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05301450 session->vpp_error = SESSION_E_NONE;
Dave Wallace543852a2017-08-03 02:11:34 -04001451
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001452 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001453 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001454
Florin Coras7e12d942018-06-27 14:32:43 -07001455 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1456 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001457
Florin Coras5e062572019-03-14 19:07:51 -07001458 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001459
Florin Coras134a9962018-08-28 11:32:04 -07001460 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001461}
1462
Florin Corasfe286f72021-06-04 10:07:55 -07001463static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001464vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001465{
Florin Corasfa3884f2021-06-22 20:04:46 -07001466 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001467
Florin Corasfa3884f2021-06-22 20:04:46 -07001468 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001469 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001470 wrk->ep_lt_current = s->session_index;
1471 s->vep.lt_next = s->session_index;
1472 s->vep.lt_prev = s->session_index;
1473 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001474 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001475
1476 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1477 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1478
1479 prev->vep.lt_next = s->session_index;
1480 s->vep.lt_prev = prev->session_index;
1481
1482 s->vep.lt_next = cur->session_index;
1483 cur->vep.lt_prev = s->session_index;
1484}
1485
1486static void
1487vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1488{
1489 vcl_session_t *prev, *next;
1490
1491 if (s->vep.lt_next == s->session_index)
1492 {
1493 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1494 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1495 return;
1496 }
1497
1498 prev = vcl_session_get (wrk, s->vep.lt_prev);
1499 next = vcl_session_get (wrk, s->vep.lt_next);
1500
1501 prev->vep.lt_next = next->session_index;
1502 next->vep.lt_prev = prev->session_index;
1503
1504 if (s->session_index == wrk->ep_lt_current)
1505 wrk->ep_lt_current = s->vep.lt_next;
1506
1507 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001508}
1509
Dave Wallace543852a2017-08-03 02:11:34 -04001510int
Florin Corasc127d5a2020-10-14 16:35:58 -07001511vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001512 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001513{
Florin Coras134a9962018-08-28 11:32:04 -07001514 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001515
Florin Coras6c3b2182020-10-19 18:36:48 -07001516 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001517
Florin Coras6c3b2182020-10-19 18:36:48 -07001518 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001519 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001520 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001521 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001522 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001523 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001524 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001525 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001526 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1527 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001528 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001529 }
Florin Corase4306b62020-10-28 12:51:10 -07001530 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001531 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001532
Florin Coras6c3b2182020-10-19 18:36:48 -07001533 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001534 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001535 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001536 if (rv < 0)
1537 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001538 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1539 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001540 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001541
Florin Coras9ace36d2019-10-28 13:14:17 -07001542 if (!do_disconnect)
1543 {
1544 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001545 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001546 goto cleanup;
1547 }
1548
Florin Coras6c3b2182020-10-19 18:36:48 -07001549 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001550 {
1551 rv = vppcom_session_unbind (sh);
1552 if (PREDICT_FALSE (rv < 0))
1553 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001554 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001555 vppcom_retval_str (rv));
1556 return rv;
1557 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001558 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001559 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001560 {
1561 rv = vppcom_session_disconnect (sh);
1562 if (PREDICT_FALSE (rv < 0))
1563 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001564 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001565 rv, vppcom_retval_str (rv));
1566 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001567 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001568 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001569 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001570 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001571 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001572 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001573 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08001574
1575 if (!(s->flags & VCL_SESSION_F_PENDING_DISCONNECT))
1576 goto free_session;
1577
1578 /* Disconnect/reset messages pending but vpp transport and session
1579 * cleanups already done. Free only after messages drained. */
1580 s->flags |= VCL_SESSION_F_PENDING_FREE;
Florin Corasadcfb152020-02-12 08:50:29 +00001581 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001582
Florin Corasc127d5a2020-10-14 16:35:58 -07001583 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001584
Florin Coras9ace36d2019-10-28 13:14:17 -07001585 /* Session is removed only after vpp confirms the disconnect */
1586 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001587
Florin Corasf9240dc2019-01-15 08:03:17 -08001588cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001589 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001590free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001591 vcl_session_free (wrk, s);
1592 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001593
Dave Wallace543852a2017-08-03 02:11:34 -04001594 return rv;
1595}
1596
1597int
Florin Corasf9240dc2019-01-15 08:03:17 -08001598vppcom_session_close (uint32_t session_handle)
1599{
1600 vcl_worker_t *wrk = vcl_worker_get_current ();
1601 vcl_session_t *session;
1602
1603 session = vcl_session_get_w_handle (wrk, session_handle);
1604 if (!session)
1605 return VPPCOM_EBADFD;
1606 return vcl_session_cleanup (wrk, session, session_handle,
1607 1 /* do_disconnect */ );
1608}
1609
1610int
Florin Coras134a9962018-08-28 11:32:04 -07001611vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001612{
Florin Coras134a9962018-08-28 11:32:04 -07001613 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001614 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001615
1616 if (!ep || !ep->ip)
1617 return VPPCOM_EINVAL;
1618
Florin Coras134a9962018-08-28 11:32:04 -07001619 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001620 if (!session)
1621 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001622
Florin Coras6c3b2182020-10-19 18:36:48 -07001623 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001624 {
Florin Coras5e062572019-03-14 19:07:51 -07001625 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1626 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001627 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001628 }
1629
Florin Coras7e12d942018-06-27 14:32:43 -07001630 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001631 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001632 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1633 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001634 else
Dave Barach178cf492018-11-13 16:34:13 -05001635 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1636 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001637 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001638
Florin Coras60687192021-11-29 21:00:47 -08001639 VDBG (0,
1640 "session %u handle %u: binding to local %s address %U port %u, "
1641 "proto %s",
1642 session->session_index, session_handle,
1643 session->transport.is_ip4 ? "IPv4" : "IPv6", vcl_format_ip46_address,
1644 &session->transport.lcl_ip,
Florin Coras7e12d942018-06-27 14:32:43 -07001645 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1646 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001647 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001648 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001649
1650 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001651 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001652
Florin Coras070453d2018-08-24 17:04:27 -07001653 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001654}
1655
1656int
Florin Coras134a9962018-08-28 11:32:04 -07001657vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001658{
Florin Coras134a9962018-08-28 11:32:04 -07001659 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001660 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001661 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001662 int rv;
1663
Florin Coras134a9962018-08-28 11:32:04 -07001664 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001665 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001666 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001667
Dave Wallaceee45d412017-11-24 21:44:06 -05001668 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001669 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001670 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001671 VDBG (0, "session %u [0x%llx]: already in listen state!",
1672 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001673 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001674 }
1675
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001676 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001677
Florin Coras070453d2018-08-24 17:04:27 -07001678 /*
1679 * Send listen request to vpp and wait for reply
1680 */
Florin Coras458089b2019-08-21 16:20:44 -07001681 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001682 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001683 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001684 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001685
Florin Coras070453d2018-08-24 17:04:27 -07001686 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001687 {
Florin Coras134a9962018-08-28 11:32:04 -07001688 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001689 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1690 listen_sh, listen_session->vpp_handle, rv,
1691 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001692 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001693 }
1694
Florin Coras070453d2018-08-24 17:04:27 -07001695 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001696}
1697
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001698int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001699vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1700{
1701 if (!strcmp (proto_str, "TCP"))
1702 *proto = VPPCOM_PROTO_TCP;
1703 else if (!strcmp (proto_str, "tcp"))
1704 *proto = VPPCOM_PROTO_TCP;
1705 else if (!strcmp (proto_str, "UDP"))
1706 *proto = VPPCOM_PROTO_UDP;
1707 else if (!strcmp (proto_str, "udp"))
1708 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001709 else if (!strcmp (proto_str, "TLS"))
1710 *proto = VPPCOM_PROTO_TLS;
1711 else if (!strcmp (proto_str, "tls"))
1712 *proto = VPPCOM_PROTO_TLS;
1713 else if (!strcmp (proto_str, "QUIC"))
1714 *proto = VPPCOM_PROTO_QUIC;
1715 else if (!strcmp (proto_str, "quic"))
1716 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001717 else if (!strcmp (proto_str, "DTLS"))
1718 *proto = VPPCOM_PROTO_DTLS;
1719 else if (!strcmp (proto_str, "dtls"))
1720 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001721 else if (!strcmp (proto_str, "SRTP"))
1722 *proto = VPPCOM_PROTO_SRTP;
1723 else if (!strcmp (proto_str, "srtp"))
1724 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001725 else
1726 return 1;
1727 return 0;
1728}
1729
1730int
Florin Coras32881932023-02-06 13:30:13 -08001731vppcom_session_accept (uint32_t ls_handle, vppcom_endpt_t *ep, uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001732{
Florin Coras32881932023-02-06 13:30:13 -08001733 u32 client_session_index = ~0, ls_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001734 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001735 session_accepted_msg_t accepted_msg;
Florin Coras32881932023-02-06 13:30:13 -08001736 vcl_session_t *ls, *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001737 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001738 u8 is_nonblocking;
Dave Wallace543852a2017-08-03 02:11:34 -04001739
Florin Coras5398dfb2021-01-25 20:31:27 -08001740again:
1741
Florin Coras32881932023-02-06 13:30:13 -08001742 ls = vcl_session_get_w_handle (wrk, ls_handle);
1743 if (!ls)
Florin Coras070453d2018-08-24 17:04:27 -07001744 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001745
Florin Coras32881932023-02-06 13:30:13 -08001746 if ((ls->session_state != VCL_STATE_LISTEN) &&
1747 (ls->session_state != VCL_STATE_LISTEN_NO_MQ) &&
1748 (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001749 {
Florin Coras32881932023-02-06 13:30:13 -08001750 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state (%s)",
1751 ls->vpp_handle, vcl_session_state_str (ls->session_state));
1752 return VPPCOM_EBADFD;
1753 }
1754
1755 ls_index = ls->session_index;
1756
1757 if (clib_fifo_elts (ls->accept_evts_fifo))
1758 {
1759 clib_fifo_sub2 (ls->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001760 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001761 accepted_msg = evt->accepted_msg;
1762 goto handle;
1763 }
1764
Florin Coras32881932023-02-06 13:30:13 -08001765 is_nonblocking = vcl_session_has_attr (ls, VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001766 while (1)
1767 {
Carl Smith592a9092019-11-12 14:57:37 +13001768 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1769 return VPPCOM_EAGAIN;
1770
Florin Coras5398dfb2021-01-25 20:31:27 -08001771 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1772 vcl_worker_flush_mq_events (wrk);
1773 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001774 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001775
Florin Coras54693d22018-07-17 10:46:29 -07001776handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001777
Florin Coras32881932023-02-06 13:30:13 -08001778 client_session_index =
1779 vcl_session_accepted_handler (wrk, &accepted_msg, ls_index);
Florin Coras00cca802019-06-06 09:38:44 -07001780 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1781 return VPPCOM_ECONNABORTED;
1782
Florin Coras32881932023-02-06 13:30:13 -08001783 ls = vcl_session_get (wrk, ls_index);
Florin Coras134a9962018-08-28 11:32:04 -07001784 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001785
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001786 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001787 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001788
Florin Coras32881932023-02-06 13:30:13 -08001789 VDBG (1,
1790 "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1791 " flags %d, is_nonblocking %u",
1792 ls->session_index, ls->vpp_handle, client_session_index,
Florin Coras5e062572019-03-14 19:07:51 -07001793 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001794 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001795
Dave Wallace048b1d62018-01-03 22:24:41 -05001796 if (ep)
1797 {
Florin Coras7e12d942018-06-27 14:32:43 -07001798 ep->is_ip4 = client_session->transport.is_ip4;
1799 ep->port = client_session->transport.rmt_port;
1800 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001801 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1802 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001803 else
Dave Barach178cf492018-11-13 16:34:13 -05001804 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1805 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001806 }
Dave Wallace60caa062017-11-10 17:07:13 -05001807
Florin Coras60687192021-11-29 21:00:47 -08001808 VDBG (0,
1809 "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1810 "local: %U:%u",
Florin Coras32881932023-02-06 13:30:13 -08001811 ls_handle, ls->vpp_handle, client_session_index,
1812 client_session->vpp_handle, vcl_format_ip46_address,
1813 &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001814 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001815 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras60687192021-11-29 21:00:47 -08001816 vcl_format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001817 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001818 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras32881932023-02-06 13:30:13 -08001819 vcl_evt (VCL_EVT_ACCEPT, client_session, ls, client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001820
Florin Coras3c7d4f92018-12-14 11:28:43 -08001821 /*
1822 * Session might have been closed already
1823 */
1824 if (accept_flags)
1825 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001826 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001827 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001828 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001829 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001830 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001831 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001832}
1833
1834int
Florin Coras134a9962018-08-28 11:32:04 -07001835vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001836{
Florin Coras134a9962018-08-28 11:32:04 -07001837 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001838 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001839 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001840 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001841
Florin Coras134a9962018-08-28 11:32:04 -07001842 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001843 if (!session)
1844 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001845 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001846
Florin Coras6c3b2182020-10-19 18:36:48 -07001847 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001848 {
Florin Coras5ffb9642021-12-03 17:24:13 -08001849 VWRN ("cannot connect epoll session %u!", session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001850 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001851 }
1852
Florin Corasc127d5a2020-10-14 16:35:58 -07001853 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001854 {
Florin Coras60687192021-11-29 21:00:47 -08001855 VDBG (0,
Florin Coras5ffb9642021-12-03 17:24:13 -08001856 "session %u [0x%llx]: already connected to %U:%d proto %s,"
1857 " state (%s)",
1858 session->session_index, session->vpp_handle,
Florin Coras60687192021-11-29 21:00:47 -08001859 vcl_format_ip46_address, &session->transport.rmt_ip,
1860 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001861 clib_net_to_host_u16 (session->transport.rmt_port),
Florin Coras5ffb9642021-12-03 17:24:13 -08001862 vppcom_proto_str (session->session_type),
Florin Coras60687192021-11-29 21:00:47 -08001863 vcl_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001864 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001865 }
1866
Florin Coras0a1e1832020-03-29 18:54:04 +00001867 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001868 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001869 {
1870 if (session->session_type != VPPCOM_PROTO_UDP)
1871 return VPPCOM_EINVAL;
1872 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001873 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001874 }
1875
Florin Coras7e12d942018-06-27 14:32:43 -07001876 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001877 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001878 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001879 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001880 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001881
Florin Coras5ffb9642021-12-03 17:24:13 -08001882 VDBG (0, "session %u: connecting to peer %U:%d proto %s",
1883 session->session_index, vcl_format_ip46_address,
Florin Coras60687192021-11-29 21:00:47 -08001884 &session->transport.rmt_ip,
1885 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001886 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001887 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001888
Florin Coras458089b2019-08-21 16:20:44 -07001889 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001890
Florin Corasac422d62020-10-19 20:51:36 -07001891 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001892 {
fanyfa49d59d2020-10-13 17:07:16 +08001893 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001894 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001895 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001896 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001897 return VPPCOM_EINPROGRESS;
1898 }
Florin Coras57c88932019-08-29 12:03:17 -07001899
1900 /*
1901 * Wait for reply from vpp if blocking
1902 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001903 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001904 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001905
Florin Coras134a9962018-08-28 11:32:04 -07001906 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001907 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1908 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001909
Dave Wallace4878cbe2017-11-21 03:45:09 -05001910 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001911}
1912
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001913int
1914vppcom_session_stream_connect (uint32_t session_handle,
1915 uint32_t parent_session_handle)
1916{
1917 vcl_worker_t *wrk = vcl_worker_get_current ();
1918 vcl_session_t *session, *parent_session;
1919 u32 session_index, parent_session_index;
1920 int rv;
1921
1922 session = vcl_session_get_w_handle (wrk, session_handle);
1923 if (!session)
1924 return VPPCOM_EBADFD;
1925 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1926 if (!parent_session)
1927 return VPPCOM_EBADFD;
1928
1929 session_index = session->session_index;
1930 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001931 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001932 {
1933 VDBG (0, "ERROR: cannot connect epoll session %u!",
1934 session->session_index);
1935 return VPPCOM_EBADFD;
1936 }
1937
Florin Corasc127d5a2020-10-14 16:35:58 -07001938 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001939 {
Florin Coras60687192021-11-29 21:00:47 -08001940 VDBG (0,
1941 "session handle %u [0x%llx]: session already "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001942 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
Florin Coras60687192021-11-29 21:00:47 -08001943 session_handle, session->vpp_handle, parent_session_handle,
1944 parent_session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001945 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras60687192021-11-29 21:00:47 -08001946 vcl_session_state_str (session->session_state));
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001947 return VPPCOM_OK;
1948 }
1949
1950 /* Connect to quic session specifics */
1951 session->transport.is_ip4 = parent_session->transport.is_ip4;
1952 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1953 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001954 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001955
1956 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1957 session_handle, parent_session_handle, parent_session->vpp_handle);
1958
1959 /*
1960 * Send connect request and wait for reply from vpp
1961 */
Florin Coras458089b2019-08-21 16:20:44 -07001962 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001963 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001964 vcm->cfg.session_timeout);
1965
1966 session->listener_index = parent_session_index;
1967 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001968 if (parent_session)
1969 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001970
1971 session = vcl_session_get (wrk, session_index);
1972 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1973 session->vpp_handle, rv ? "failed" : "succeeded");
1974
1975 return rv;
1976}
1977
Steven58f464e2017-10-25 12:33:12 -07001978static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001979vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001980 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001981{
Florin Coras134a9962018-08-28 11:32:04 -07001982 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001983 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001984 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001985 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001986 session_event_t *e;
1987 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001988 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001989
Florin Coras070453d2018-08-24 17:04:27 -07001990 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001991 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001992
Florin Coras134a9962018-08-28 11:32:04 -07001993 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001994 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001995 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001996
Florin Coras6d0106e2019-01-29 20:11:58 -08001997 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001998 {
Florin Coras5e062572019-03-14 19:07:51 -07001999 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002000 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002001 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002002 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002003 }
2004
liuyacan55c952e2021-06-13 14:54:55 +08002005 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2006 {
2007 /* Vpp would ack the incoming data and enqueue it for reading.
2008 * So even if SHUT_RD is set, we can still read() the data if
2009 * the session is ready.
2010 */
2011 if (!vcl_session_read_ready (s))
2012 {
2013 return 0;
2014 }
2015 }
2016
Florin Corasac422d62020-10-19 20:51:36 -07002017 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002018 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002019 mq = wrk->app_event_queue;
2020 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002021 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002022
Sirshak Das28aa5392019-02-05 01:33:33 -06002023 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002024 {
Florin Coras54693d22018-07-17 10:46:29 -07002025 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002026 {
Yu Pingb2955352019-12-04 06:49:04 +08002027 if (vcl_session_is_closing (s))
2028 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002029 if (is_ct)
2030 svm_fifo_unset_event (s->rx_fifo);
2031 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002032 return VPPCOM_EWOULDBLOCK;
2033 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002034 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002035 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002036 if (vcl_session_is_closing (s))
2037 return vcl_session_closing_error (s);
2038
Florin Corasd6894562020-10-28 00:37:15 -07002039 if (is_ct)
2040 svm_fifo_unset_event (s->rx_fifo);
2041 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002042
Florin Coras5398dfb2021-01-25 20:31:27 -08002043 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2044 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002045 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002046 }
Florin Coras54693d22018-07-17 10:46:29 -07002047
Florin Coras4a2c7942020-09-10 12:27:14 -07002048read_again:
2049
Florin Coras460dce62018-07-27 05:45:06 -07002050 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002051 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002052 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002053 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2054
2055 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002056
2057 if (peek)
2058 return rv;
2059
Florin Coras4a2c7942020-09-10 12:27:14 -07002060 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002061
Sirshak Das28aa5392019-02-05 01:33:33 -06002062 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002063 {
Florin Corasd6894562020-10-28 00:37:15 -07002064 if (is_ct)
2065 svm_fifo_unset_event (s->rx_fifo);
2066 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002067 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002068 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002069 {
Florin Corasc34118b2020-08-12 18:58:25 -07002070 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2071 e->event_type = SESSION_IO_EVT_RX;
2072 e->session_index = s->session_index;
2073 }
2074 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002075 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002076 {
2077 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002078 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002079 buf += rv;
2080 n -= rv;
2081 goto read_again;
2082 }
Florin Coras41c9e042018-09-11 00:10:41 -07002083
Florin Coras17ec5772020-08-12 20:46:29 -07002084 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002085 {
Florin Coras17ec5772020-08-12 20:46:29 -07002086 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002087 app_send_io_evt_to_vpp (s->vpp_evt_q,
2088 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002089 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002090 }
2091
Florin Coras5e062572019-03-14 19:07:51 -07002092 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2093 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002094
Florin Coras54693d22018-07-17 10:46:29 -07002095 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002096}
2097
Steven58f464e2017-10-25 12:33:12 -07002098int
Florin Coras134a9962018-08-28 11:32:04 -07002099vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002100{
Florin Coras134a9962018-08-28 11:32:04 -07002101 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002102}
2103
2104static int
Florin Coras134a9962018-08-28 11:32:04 -07002105vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002106{
Florin Coras134a9962018-08-28 11:32:04 -07002107 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002108}
2109
Florin Coras2cba8532018-09-11 16:33:36 -07002110int
2111vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002112 vppcom_data_segment_t * ds, uint32_t n_segments,
2113 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002114{
2115 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002116 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002117 vcl_session_t *s = 0;
2118 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002119 svm_msg_q_t *mq;
2120 u8 is_ct;
2121
2122 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002123 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002124 return VPPCOM_EBADFD;
2125
Florin Coras6d0106e2019-01-29 20:11:58 -08002126 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2127 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002128
Florin Corasac422d62020-10-19 20:51:36 -07002129 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002130 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002131 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002132 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002133 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002134
Sirshak Das28aa5392019-02-05 01:33:33 -06002135 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002136 {
2137 if (is_nonblocking)
2138 {
Florin Coras62877022020-10-28 21:22:04 -07002139 if (is_ct)
2140 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002141 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002142 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002143 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002144 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002145 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002146 if (vcl_session_is_closing (s))
2147 return vcl_session_closing_error (s);
2148
Florin Coras62877022020-10-28 21:22:04 -07002149 if (is_ct)
2150 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002151 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002152
Florin Coras5398dfb2021-01-25 20:31:27 -08002153 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2154 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002155 }
2156 }
2157
Florin Corasd1cc38d2020-10-11 11:05:04 -07002158 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
Florin Corasb85de192022-01-18 20:51:08 -08002159 (svm_fifo_seg_t *) ds, &n_segments, max_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002160 if (n_read < 0)
2161 return VPPCOM_EAGAIN;
2162
2163 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2164 {
Florin Coras62877022020-10-28 21:22:04 -07002165 if (is_ct)
2166 svm_fifo_unset_event (s->rx_fifo);
2167 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002168 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002169 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002170 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002171 {
2172 session_event_t *e;
2173 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2174 e->event_type = SESSION_IO_EVT_RX;
2175 e->session_index = s->session_index;
2176 }
2177 }
2178
2179 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002180 return n_read;
2181}
2182
2183void
Florin Corasd68faf82020-09-25 15:18:13 -07002184vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002185{
2186 vcl_worker_t *wrk = vcl_worker_get_current ();
2187 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002188 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002189
2190 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002191 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002192 return;
2193
Florin Coras62877022020-10-28 21:22:04 -07002194 is_ct = vcl_session_is_ct (s);
2195 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002196
Florin Coras8cc93212021-09-10 11:37:12 -07002197 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002198 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002199}
2200
Florin Coras7a2abce2020-04-05 19:25:44 +00002201always_inline u8
2202vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002203{
Florin Coras7a2abce2020-04-05 19:25:44 +00002204 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2205 if (is_dgram)
2206 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2207 else
2208 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002209}
2210
2211always_inline int
Dou Chao243a0432022-11-29 19:41:34 +08002212vppcom_session_write_inline (vcl_worker_t *wrk, vcl_session_t *s, void *buf,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002213 size_t n, u8 is_flush, u8 is_dgram)
Florin Coras7a2abce2020-04-05 19:25:44 +00002214{
Florin Coras6d0106e2019-01-29 20:11:58 -08002215 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002216 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002217 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002218 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002219 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002220
Florin Coras0b0d28e2021-06-04 17:31:53 -07002221 /* Accept zero length writes but just return */
2222 if (PREDICT_FALSE (!n))
2223 return VPPCOM_OK;
2224
2225 if (PREDICT_FALSE (!buf))
2226 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002227
Florin Coras6c3b2182020-10-19 18:36:48 -07002228 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002229 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002230 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2231 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002232 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002233 }
2234
liuyacan55c952e2021-06-13 14:54:55 +08002235 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002236 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002237 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2238 s->session_index, s->vpp_handle, s->session_state,
Florin Coras60687192021-11-29 21:00:47 -08002239 vcl_session_state_str (s->session_state));
Florin Coras6d0106e2019-01-29 20:11:58 -08002240 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002241 }
2242
liuyacan55c952e2021-06-13 14:54:55 +08002243 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2244 {
2245 VDBG (1, "session %u [0x%llx]: is shutdown! 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));
liuyacan55c952e2021-06-13 14:54:55 +08002248 return VPPCOM_EPIPE;
2249 }
2250
Florin Coras0e88e852018-09-17 22:09:02 -07002251 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002252 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002253 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002254
Florin Coras653e43f2019-03-04 10:56:23 -08002255 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002256 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002257 {
Florin Coras54693d22018-07-17 10:46:29 -07002258 if (is_nonblocking)
2259 {
Florin Coras070453d2018-08-24 17:04:27 -07002260 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002261 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002262 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002263 {
Florin Coras2d379d82019-06-28 12:45:12 -07002264 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002265 if (vcl_session_is_closing (s))
2266 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002267
Florin Coras5398dfb2021-01-25 20:31:27 -08002268 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2269 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002270 }
Dave Wallace543852a2017-08-03 02:11:34 -04002271 }
Dave Wallace543852a2017-08-03 02:11:34 -04002272
Florin Coras653e43f2019-03-04 10:56:23 -08002273 et = SESSION_IO_EVT_TX;
2274 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002275 et = SESSION_IO_EVT_TX_FLUSH;
2276
Florin Coras7a2abce2020-04-05 19:25:44 +00002277 if (is_dgram)
Dou Chao243a0432022-11-29 19:41:34 +08002278 n_write =
2279 app_send_dgram_raw_gso (tx_fifo, &s->transport, s->vpp_evt_q, buf, n,
Florin Coraseff5f7a2023-02-07 17:36:17 -08002280 s->gso_size, et, 0 /* do_evt */, SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002281 else
2282 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002283 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002284
Florin Coras14ed6df2019-03-06 21:13:42 -08002285 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002286 app_send_io_evt_to_vpp (
2287 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002288
Florin Coras56230092020-09-02 20:52:58 -07002289 /* The underlying fifo segment can run out of memory */
2290 if (PREDICT_FALSE (n_write < 0))
2291 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002292
Florin Coras6d0106e2019-01-29 20:11:58 -08002293 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2294 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002295
Florin Coras54693d22018-07-17 10:46:29 -07002296 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002297}
2298
Florin Coras42ceddb2018-12-12 10:56:01 -08002299int
2300vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2301{
Florin Coras7a2abce2020-04-05 19:25:44 +00002302 vcl_worker_t *wrk = vcl_worker_get_current ();
2303 vcl_session_t *s;
2304
2305 s = vcl_session_get_w_handle (wrk, session_handle);
2306 if (PREDICT_FALSE (!s))
2307 return VPPCOM_EBADFD;
2308
Florin Coraseff5f7a2023-02-07 17:36:17 -08002309 return vppcom_session_write_inline (wrk, s, buf, n, 0 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002310 s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002311}
2312
Florin Corasb0f662f2018-12-27 14:51:46 -08002313int
2314vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2315{
Florin Coras7a2abce2020-04-05 19:25:44 +00002316 vcl_worker_t *wrk = vcl_worker_get_current ();
2317 vcl_session_t *s;
2318
2319 s = vcl_session_get_w_handle (wrk, session_handle);
2320 if (PREDICT_FALSE (!s))
2321 return VPPCOM_EBADFD;
2322
Florin Coraseff5f7a2023-02-07 17:36:17 -08002323 return vppcom_session_write_inline (wrk, s, buf, n, 1 /* is_flush */,
Dou Chao243a0432022-11-29 19:41:34 +08002324 s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002325}
2326
Florin Corasc0737e92019-03-04 14:19:39 -08002327#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002328if (PREDICT_FALSE (!_s->rx_fifo)) \
2329 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002330if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2331 { \
2332 if (!vcl_session_is_ct (_s)) \
2333 { \
2334 svm_fifo_unset_event (_s->rx_fifo); \
2335 if (svm_fifo_is_empty (_s->rx_fifo)) \
2336 break; \
2337 } \
2338 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2339 { \
Florin Coras2f630182020-08-13 00:17:51 -07002340 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002341 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2342 break; \
2343 } \
2344 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002345
Florin Coras86f04502018-09-12 16:08:01 -07002346static void
2347vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2348 unsigned long n_bits, unsigned long *read_map,
2349 unsigned long *write_map,
2350 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002351{
2352 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002353 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002354 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002355 u32 sid;
2356
2357 switch (e->event_type)
2358 {
Florin Corasc0737e92019-03-04 14:19:39 -08002359 case SESSION_IO_EVT_RX:
2360 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002361 s = vcl_session_get (wrk, sid);
2362 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002363 break;
Florin Corasb242d312020-10-26 15:35:40 -07002364 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002365 if (sid < n_bits && read_map)
2366 {
David Johnsond9818dd2018-12-14 14:53:41 -05002367 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002368 *bits_set += 1;
2369 }
2370 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002371 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002372 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002373 s = vcl_session_get (wrk, sid);
2374 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002375 break;
2376 if (sid < n_bits && write_map)
2377 {
David Johnsond9818dd2018-12-14 14:53:41 -05002378 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002379 *bits_set += 1;
2380 }
2381 break;
Florin Coras86f04502018-09-12 16:08:01 -07002382 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002383 if (!e->postponed)
2384 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2385 else
2386 s = vcl_session_get (wrk, e->session_index);
2387 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002388 break;
Florin Corasb242d312020-10-26 15:35:40 -07002389 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002390 if (sid < n_bits && read_map)
2391 {
David Johnsond9818dd2018-12-14 14:53:41 -05002392 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002393 *bits_set += 1;
2394 }
2395 break;
2396 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002397 if (!e->postponed)
2398 {
2399 connected_msg = (session_connected_msg_t *) e->data;
2400 sid = vcl_session_connected_handler (wrk, connected_msg);
2401 }
2402 else
2403 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002404 if (sid == VCL_INVALID_SESSION_INDEX)
2405 break;
2406 if (sid < n_bits && write_map)
2407 {
2408 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2409 *bits_set += 1;
2410 }
Florin Coras86f04502018-09-12 16:08:01 -07002411 break;
2412 case SESSION_CTRL_EVT_DISCONNECTED:
2413 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002414 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2415 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002416 break;
Florin Corasb242d312020-10-26 15:35:40 -07002417 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002418 if (sid < n_bits && except_map)
2419 {
David Johnsond9818dd2018-12-14 14:53:41 -05002420 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002421 *bits_set += 1;
2422 }
2423 break;
2424 case SESSION_CTRL_EVT_RESET:
2425 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2426 if (sid < n_bits && except_map)
2427 {
David Johnsond9818dd2018-12-14 14:53:41 -05002428 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002429 *bits_set += 1;
2430 }
2431 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002432 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2433 vcl_session_unlisten_reply_handler (wrk, e->data);
2434 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002435 case SESSION_CTRL_EVT_MIGRATED:
2436 vcl_session_migrated_handler (wrk, e->data);
2437 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002438 case SESSION_CTRL_EVT_CLEANUP:
2439 vcl_session_cleanup_handler (wrk, e->data);
2440 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002441 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2442 vcl_session_worker_update_reply_handler (wrk, e->data);
2443 break;
2444 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2445 vcl_session_req_worker_update_handler (wrk, e->data);
2446 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002447 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2448 vcl_session_app_add_segment_handler (wrk, e->data);
2449 break;
2450 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2451 vcl_session_app_del_segment_handler (wrk, e->data);
2452 break;
hanlina3a48962020-07-13 11:09:15 +08002453 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002454 vcl_worker_rpc_handler (wrk, e->data);
2455 break;
Florin Coras86f04502018-09-12 16:08:01 -07002456 default:
2457 clib_warning ("unhandled: %u", e->event_type);
2458 break;
2459 }
2460}
2461
2462static int
2463vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2464 unsigned long n_bits, unsigned long *read_map,
2465 unsigned long *write_map, unsigned long *except_map,
2466 double time_to_wait, u32 * bits_set)
2467{
Florin Coras99368312018-08-02 10:45:44 -07002468 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002469 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002470 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002471
Florin Coras54693d22018-07-17 10:46:29 -07002472 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002473 {
Florin Coras54693d22018-07-17 10:46:29 -07002474 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002475 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002476
Florin Coras54693d22018-07-17 10:46:29 -07002477 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002478 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002479 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002480 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002481 else
2482 {
2483 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002484 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002485 }
2486 }
Florin Corase003a1b2019-06-05 10:47:16 -07002487 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002488
Florin Coras134a9962018-08-28 11:32:04 -07002489 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002490 {
Florin Coras134a9962018-08-28 11:32:04 -07002491 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002492 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002493 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2494 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002495 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002496 }
Florin Coras134a9962018-08-28 11:32:04 -07002497 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002498 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002499 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002500}
2501
Florin Coras99368312018-08-02 10:45:44 -07002502static int
Florin Coras294afe22019-01-07 17:49:17 -08002503vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2504 vcl_si_set * read_map, vcl_si_set * write_map,
2505 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002506 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002507{
Florin Coras14ed6df2019-03-06 21:13:42 -08002508 double wait = 0, start = 0;
2509
2510 if (!*bits_set)
2511 {
2512 wait = time_to_wait;
2513 start = clib_time_now (&wrk->clib_time);
2514 }
2515
2516 do
2517 {
2518 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2519 write_map, except_map, wait, bits_set);
2520 if (*bits_set)
2521 return *bits_set;
2522 if (wait == -1)
2523 continue;
2524
2525 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2526 }
2527 while (wait > 0);
2528
2529 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002530}
2531
2532static int
Florin Coras294afe22019-01-07 17:49:17 -08002533vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2534 vcl_si_set * read_map, vcl_si_set * write_map,
2535 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002536 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002537{
2538 vcl_mq_evt_conn_t *mqc;
2539 int __clib_unused n_read;
2540 int n_mq_evts, i;
2541 u64 buf;
2542
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002543 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
2544 {
2545 vcl_api_retry_attach (wrk);
2546 return 0;
2547 }
2548
Florin Coras134a9962018-08-28 11:32:04 -07002549 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2550 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2551 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002552 for (i = 0; i < n_mq_evts; i++)
2553 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00002554 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
2555 {
2556 vcl_api_handle_disconnect (wrk);
2557 continue;
2558 }
2559
Florin Coras134a9962018-08-28 11:32:04 -07002560 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002561 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002562 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002563 except_map, 0, bits_set);
2564 }
2565
2566 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2567}
2568
Dave Wallace543852a2017-08-03 02:11:34 -04002569int
Florin Coras294afe22019-01-07 17:49:17 -08002570vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2571 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002572{
Florin Coras54693d22018-07-17 10:46:29 -07002573 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002574 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002575 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002576 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002577
Dave Wallace7876d392017-10-19 03:53:57 -04002578 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002579 {
Florin Coras134a9962018-08-28 11:32:04 -07002580 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002581 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002582 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2583 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002584 }
Dave Wallace7876d392017-10-19 03:53:57 -04002585 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002586 {
Florin Coras134a9962018-08-28 11:32:04 -07002587 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002588 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002589 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2590 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002591 }
Dave Wallace7876d392017-10-19 03:53:57 -04002592 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002593 {
Florin Coras134a9962018-08-28 11:32:04 -07002594 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002595 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002596 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2597 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002598 }
2599
Florin Coras54693d22018-07-17 10:46:29 -07002600 if (!n_bits)
2601 return 0;
2602
2603 if (!write_map)
2604 goto check_rd;
2605
Florin Coras096a1d52021-01-27 15:33:51 -08002606 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2607 {
2608 if (!(s = vcl_session_get (wrk, sid)))
2609 {
2610 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2611 bits_set++;
2612 continue;
2613 }
Florin Coras54693d22018-07-17 10:46:29 -07002614
Florin Coras096a1d52021-01-27 15:33:51 -08002615 if (vcl_session_write_ready (s))
2616 {
2617 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2618 bits_set++;
2619 }
2620 else
2621 {
2622 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2623 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2624 }
2625 }
Florin Coras54693d22018-07-17 10:46:29 -07002626
2627check_rd:
2628 if (!read_map)
2629 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002630
Florin Coras096a1d52021-01-27 15:33:51 -08002631 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2632 {
2633 if (!(s = vcl_session_get (wrk, sid)))
2634 {
2635 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2636 bits_set++;
2637 continue;
2638 }
Florin Coras54693d22018-07-17 10:46:29 -07002639
Florin Coras096a1d52021-01-27 15:33:51 -08002640 if (vcl_session_read_ready (s))
2641 {
2642 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2643 bits_set++;
2644 }
2645 }
Florin Coras54693d22018-07-17 10:46:29 -07002646
2647check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002648
Florin Coras86f04502018-09-12 16:08:01 -07002649 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2650 {
2651 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2652 read_map, write_map, except_map, &bits_set);
2653 }
2654 vec_reset_length (wrk->unhandled_evts_vector);
2655
Florin Coras99368312018-08-02 10:45:44 -07002656 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002657 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002658 time_to_wait, &bits_set);
2659 else
Florin Coras134a9962018-08-28 11:32:04 -07002660 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002661 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002662
Dave Wallace543852a2017-08-03 02:11:34 -04002663 return (bits_set);
2664}
2665
Dave Wallacef7f809c2017-10-03 01:48:42 -04002666static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002667vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002668{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002669 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002670 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002671 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002672
Florin Corasc0737e92019-03-04 14:19:39 -08002673 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002674 return;
2675
Florin Coras6c3b2182020-10-19 18:36:48 -07002676 s = vcl_session_get_w_handle (wrk, vep_handle);
2677 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002678 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002679 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002680 goto done;
2681 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002682 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002683 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002684 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002685 goto done;
2686 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002687 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002688 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002689 "{\n"
2690 " is_vep = %u\n"
2691 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002692 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002693 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2694 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002695
Florin Coras7e5e62b2019-07-30 14:08:23 -07002696 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002698 s = vcl_session_get_w_handle (wrk, sh);
2699 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002700 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002701 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002702 goto done;
2703 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002704 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002705 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002706 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002707 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002708 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002709 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002710 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002711 goto done;
2712 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002713 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002714 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2715 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002716 sh, s->vep.vep_sh, vep_handle);
2717 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002718 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002719 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002720 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002721 " next_sh = 0x%x (%u)\n"
2722 " prev_sh = 0x%x (%u)\n"
2723 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002724 " ev.events = 0x%x\n"
2725 " ev.data.u64 = 0x%llx\n"
2726 " et_mask = 0x%x\n"
2727 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002728 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002729 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2730 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002731 }
2732 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002733
2734done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002735 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002736}
2737
2738int
2739vppcom_epoll_create (void)
2740{
Florin Coras134a9962018-08-28 11:32:04 -07002741 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002742 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743
Florin Coras134a9962018-08-28 11:32:04 -07002744 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002745
Florin Coras6c3b2182020-10-19 18:36:48 -07002746 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002747 vep_session->vep.vep_sh = ~0;
2748 vep_session->vep.next_sh = ~0;
2749 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002750 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002751
Florin Corasa7a1a222018-12-30 17:11:31 -08002752 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2753 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002754
Florin Corasab2f6db2018-08-31 14:31:41 -07002755 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002756}
2757
Florin Corasb0116a12023-02-07 09:11:47 -08002758static void
2759vcl_epoll_ctl_add_unhandled_event (vcl_worker_t *wrk, vcl_session_t *s,
2760 u8 is_epollet, session_evt_type_t evt)
2761{
2762 if (!is_epollet)
2763 {
2764 vcl_epoll_lt_add (wrk, s);
2765 return;
2766 }
2767
2768 session_event_t e = { 0 };
2769 e.session_index = s->session_index;
2770 e.event_type = evt;
2771 if (evt == SESSION_IO_EVT_RX)
2772 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2773 vec_add1 (wrk->unhandled_evts_vector, e);
2774}
2775
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776int
Florin Coras134a9962018-08-28 11:32:04 -07002777vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002778 struct epoll_event *event)
2779{
Florin Coras134a9962018-08-28 11:32:04 -07002780 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002781 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002782 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002783 vcl_session_t *s;
2784 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002785
Florin Coras134a9962018-08-28 11:32:04 -07002786 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002787 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002788 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789 return VPPCOM_EINVAL;
2790 }
2791
Florin Coras134a9962018-08-28 11:32:04 -07002792 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002793 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002794 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002795 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002796 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002797 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002798 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002799 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002800 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002801 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802 }
2803
Florin Coras134a9962018-08-28 11:32:04 -07002804 ASSERT (vep_session->vep.vep_sh == ~0);
2805 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002806
Florin Coras17ec5772020-08-12 20:46:29 -07002807 s = vcl_session_get_w_handle (wrk, session_handle);
2808 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002809 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002810 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002811 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002812 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002813 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002814 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002815 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002816 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002817 }
2818
2819 switch (op)
2820 {
2821 case EPOLL_CTL_ADD:
2822 if (PREDICT_FALSE (!event))
2823 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002824 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002825 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002826 }
Florin Coras2645f682021-06-04 15:59:41 -07002827 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2828 {
2829 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2830 rv = VPPCOM_EEXIST;
2831 goto done;
2832 }
Florin Coras134a9962018-08-28 11:32:04 -07002833 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002834 {
Florin Coras7e12d942018-06-27 14:32:43 -07002835 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002836 next_session = vcl_session_get_w_handle (wrk,
2837 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002838 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002839 {
Florin Coras5e062572019-03-14 19:07:51 -07002840 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002841 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002842 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002843 }
Florin Coras134a9962018-08-28 11:32:04 -07002844 ASSERT (next_session->vep.prev_sh == vep_handle);
2845 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002846 }
Florin Coras17ec5772020-08-12 20:46:29 -07002847 s->vep.next_sh = vep_session->vep.next_sh;
2848 s->vep.prev_sh = vep_handle;
2849 s->vep.vep_sh = vep_handle;
2850 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002851 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002852 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002853 s->flags &= ~VCL_SESSION_F_IS_VEP;
2854 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002855 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002856
Florin Coras17ec5772020-08-12 20:46:29 -07002857 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2858 if (txf && (event->events & EPOLLOUT))
2859 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002860
Florin Coras6e3c1f82020-01-15 01:30:46 +00002861 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002862 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002863 {
Florin Corasb0116a12023-02-07 09:11:47 -08002864 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2865 SESSION_IO_EVT_TX);
Florin Coras87f76002021-06-28 19:13:29 -07002866 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002867 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002868 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002869 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002870 {
Florin Corasb0116a12023-02-07 09:11:47 -08002871 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2872 SESSION_IO_EVT_RX);
Florin Coras87f76002021-06-28 19:13:29 -07002873 add_evt = 1;
2874 }
2875 if (!add_evt && vcl_session_is_closing (s))
2876 {
2877 session_event_t e = { 0 };
2878 if (s->session_state == VCL_STATE_VPP_CLOSING)
2879 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2880 else
2881 e.event_type = SESSION_CTRL_EVT_RESET;
2882 e.session_index = s->session_index;
2883 e.postponed = 1;
2884 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002885 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002886 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2887 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002888 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002889 break;
2890
2891 case EPOLL_CTL_MOD:
2892 if (PREDICT_FALSE (!event))
2893 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002894 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002895 rv = VPPCOM_EINVAL;
2896 goto done;
2897 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002898 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002899 {
Florin Coras5e062572019-03-14 19:07:51 -07002900 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002901 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002902 goto done;
2903 }
Florin Coras17ec5772020-08-12 20:46:29 -07002904 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002905 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002906 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002907 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002908 rv = VPPCOM_EINVAL;
2909 goto done;
2910 }
hanlin475c9d72019-12-26 11:44:28 +08002911
Florin Coras2645f682021-06-04 15:59:41 -07002912 /* Generate EPOLLOUT if session write ready nd event was not on */
2913 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2914 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002915 {
Florin Corasb0116a12023-02-07 09:11:47 -08002916 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2917 SESSION_IO_EVT_TX);
hanlin475c9d72019-12-26 11:44:28 +08002918 }
Florin Coras2645f682021-06-04 15:59:41 -07002919 /* Generate EPOLLIN if session read ready and event was not on */
2920 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2921 (vcl_session_read_ready (s) > 0))
2922 {
Florin Corasb0116a12023-02-07 09:11:47 -08002923 vcl_epoll_ctl_add_unhandled_event (wrk, s, event->events & EPOLLET,
2924 SESSION_IO_EVT_RX);
Florin Coras2645f682021-06-04 15:59:41 -07002925 }
Florin Coras17ec5772020-08-12 20:46:29 -07002926 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2927 s->vep.ev = *event;
2928 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002929 if (txf)
2930 {
2931 if (event->events & EPOLLOUT)
2932 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2933 else
2934 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2935 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002936 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2937 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002938 break;
2939
2940 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002941 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002942 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002943 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002944 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002945 goto done;
2946 }
Florin Coras17ec5772020-08-12 20:46:29 -07002947 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002948 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002949 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002950 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002951 rv = VPPCOM_EINVAL;
2952 goto done;
2953 }
2954
Florin Coras17ec5772020-08-12 20:46:29 -07002955 if (s->vep.prev_sh == vep_handle)
2956 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002957 else
2958 {
Florin Coras7e12d942018-06-27 14:32:43 -07002959 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002960 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002961 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002962 {
Florin Coras5e062572019-03-14 19:07:51 -07002963 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002964 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002965 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002966 }
Florin Coras134a9962018-08-28 11:32:04 -07002967 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002968 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002969 }
Florin Coras17ec5772020-08-12 20:46:29 -07002970 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002971 {
Florin Coras7e12d942018-06-27 14:32:43 -07002972 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002973 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002974 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002975 {
Florin Coras5e062572019-03-14 19:07:51 -07002976 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002977 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002978 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002979 }
Florin Coras134a9962018-08-28 11:32:04 -07002980 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002981 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002982 }
2983
Florin Corasfa3884f2021-06-22 20:04:46 -07002984 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2985 vcl_epoll_lt_del (wrk, s);
2986
Florin Coras17ec5772020-08-12 20:46:29 -07002987 memset (&s->vep, 0, sizeof (s->vep));
2988 s->vep.next_sh = ~0;
2989 s->vep.prev_sh = ~0;
2990 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002991 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002992 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002993
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002994 if (vcl_session_is_open (s))
2995 {
2996 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2997 if (txf)
2998 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2999 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08003000
Florin Coras5e062572019-03-14 19:07:51 -07003001 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08003002 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07003003 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003004 break;
3005
3006 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08003007 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003008 rv = VPPCOM_EINVAL;
3009 }
3010
Florin Coras134a9962018-08-28 11:32:04 -07003011 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003012
3013done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003014 return rv;
3015}
3016
Florin Coras86f04502018-09-12 16:08:01 -07003017static inline void
3018vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3019 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003020{
3021 session_disconnected_msg_t *disconnected_msg;
3022 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003023 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003024 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003025 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003026 u8 add_event = 0;
3027
3028 switch (e->event_type)
3029 {
Florin Coras653e43f2019-03-04 10:56:23 -08003030 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003031 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003032 s = vcl_session_get (wrk, sid);
3033 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003034 break;
Florin Corasb242d312020-10-26 15:35:40 -07003035 vcl_fifo_rx_evt_valid_or_break (s);
3036 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003037 if (!(EPOLLIN & s->vep.ev.events) ||
3038 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3039 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003040 break;
3041 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003042 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003043 session_evt_data = s->vep.ev.data.u64;
3044 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003045 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003046 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003047 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003048 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003049 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003050 break;
Florin Corasb242d312020-10-26 15:35:40 -07003051 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003052 if (!(EPOLLOUT & session_events))
3053 break;
3054 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003055 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003056 session_evt_data = s->vep.ev.data.u64;
3057 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3058 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003059 break;
Florin Coras86f04502018-09-12 16:08:01 -07003060 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003061 if (!e->postponed)
3062 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3063 else
3064 s = vcl_session_get (wrk, e->session_index);
3065 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003066 break;
Florin Corasb242d312020-10-26 15:35:40 -07003067 session_events = s->vep.ev.events;
3068 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003069 if (!(EPOLLIN & session_events) ||
3070 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003071 break;
Florin Coras86f04502018-09-12 16:08:01 -07003072 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003073 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003074 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003075 break;
3076 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003077 if (!e->postponed)
3078 {
3079 connected_msg = (session_connected_msg_t *) e->data;
3080 sid = vcl_session_connected_handler (wrk, connected_msg);
3081 }
3082 else
3083 sid = e->session_index;
3084 s = vcl_session_get (wrk, sid);
3085 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003086 break;
Florin Corasb242d312020-10-26 15:35:40 -07003087 session_events = s->vep.ev.events;
3088 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003089 if (!(EPOLLOUT & session_events))
3090 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003091 /* We didn't have a fifo when the event was added */
3092 svm_fifo_add_want_deq_ntf (
3093 (vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo),
3094 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003095 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003096 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003097 session_evt_data = s->vep.ev.data.u64;
3098 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003099 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003100 break;
3101 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003102 if (!e->postponed)
3103 {
3104 disconnected_msg = (session_disconnected_msg_t *) e->data;
3105 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3106 }
3107 else
3108 {
3109 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003110 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003111 }
3112 if (vcl_session_is_closed (s) ||
3113 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003114 {
Florin Coras74254b52021-12-08 11:03:08 -08003115 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003116 vcl_session_free (wrk, s);
3117 break;
3118 }
Florin Corasb242d312020-10-26 15:35:40 -07003119 sid = s->session_index;
3120 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003121 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003122 if (EPOLLRDHUP & session_events)
3123 {
3124 /* If app can distinguish between RDHUP and HUP,
3125 * we make finer control */
3126 events[*num_ev].events = EPOLLRDHUP;
3127 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3128 {
3129 events[*num_ev].events |= EPOLLHUP;
3130 }
3131 }
3132 else
3133 {
3134 events[*num_ev].events = EPOLLHUP;
3135 }
Florin Corasb242d312020-10-26 15:35:40 -07003136 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003137
Florin Coras86f04502018-09-12 16:08:01 -07003138 break;
3139 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003140 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003141 {
3142 sid =
3143 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3144 s = vcl_session_get (wrk, sid);
3145 }
Florin Coras87f76002021-06-28 19:13:29 -07003146 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003147 {
3148 sid = e->session_index;
3149 s = vcl_session_get (wrk, sid);
3150 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3151 }
Florin Coras87f76002021-06-28 19:13:29 -07003152 if (vcl_session_is_closed (s) ||
3153 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003154 {
Florin Coras74254b52021-12-08 11:03:08 -08003155 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003156 vcl_session_free (wrk, s);
3157 break;
3158 }
Florin Corasb242d312020-10-26 15:35:40 -07003159 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003160 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003161 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3162 if ((EPOLLRDHUP & session_events) &&
3163 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3164 {
Yacan Liuab157702022-09-26 16:41:32 +08003165 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003166 }
3167 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3168 {
3169 events[*num_ev].events |= EPOLLIN;
3170 }
Florin Corasb242d312020-10-26 15:35:40 -07003171 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003172 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003173 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3174 vcl_session_unlisten_reply_handler (wrk, e->data);
3175 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003176 case SESSION_CTRL_EVT_MIGRATED:
3177 vcl_session_migrated_handler (wrk, e->data);
3178 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003179 case SESSION_CTRL_EVT_CLEANUP:
3180 vcl_session_cleanup_handler (wrk, e->data);
3181 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003182 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3183 vcl_session_req_worker_update_handler (wrk, e->data);
3184 break;
3185 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3186 vcl_session_worker_update_reply_handler (wrk, e->data);
3187 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003188 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3189 vcl_session_app_add_segment_handler (wrk, e->data);
3190 break;
3191 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3192 vcl_session_app_del_segment_handler (wrk, e->data);
3193 break;
hanlina3a48962020-07-13 11:09:15 +08003194 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003195 vcl_worker_rpc_handler (wrk, e->data);
3196 break;
Florin Coras86f04502018-09-12 16:08:01 -07003197 default:
3198 VDBG (0, "unhandled: %u", e->event_type);
3199 break;
3200 }
3201
3202 if (add_event)
3203 {
3204 events[*num_ev].data.u64 = session_evt_data;
3205 if (EPOLLONESHOT & session_events)
3206 {
Florin Corasb242d312020-10-26 15:35:40 -07003207 s = vcl_session_get (wrk, sid);
3208 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003209 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003210 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003211 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003212 s = vcl_session_get (wrk, sid);
3213 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3214 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003215 }
Florin Coras86f04502018-09-12 16:08:01 -07003216 *num_ev += 1;
3217 }
3218}
3219
3220static int
3221vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3222 struct epoll_event *events, u32 maxevents,
3223 double wait_for_time, u32 * num_ev)
3224{
Florin Coras99368312018-08-02 10:45:44 -07003225 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003226 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003227 int i;
3228
Florin Coras539663c2018-09-28 14:59:37 -07003229 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3230 goto handle_dequeued;
3231
Florin Coras54693d22018-07-17 10:46:29 -07003232 if (svm_msg_q_is_empty (mq))
3233 {
3234 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003235 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003236 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003237 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003238 else
3239 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003240 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003241 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003242 }
3243 }
Florin Corase003a1b2019-06-05 10:47:16 -07003244 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003245 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003246
Florin Coras539663c2018-09-28 14:59:37 -07003247handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003248 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003249 {
Florin Coras134a9962018-08-28 11:32:04 -07003250 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003251 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003252 if (*num_ev < maxevents)
3253 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3254 else
3255 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003256 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003257 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003258 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003259 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003260 return *num_ev;
3261}
3262
Florin Coras99368312018-08-02 10:45:44 -07003263static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003264vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3265 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003266{
Florin Corasccdb8b82021-04-28 13:01:06 -07003267 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003268
3269 if (!n_evts)
3270 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003271 if (timeout_ms > 0)
3272 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003273 }
3274
3275 do
3276 {
3277 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003278 timeout_ms, &n_evts);
3279 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003280 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003281 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003282 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003283
3284 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003285}
3286
3287static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003288vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3289 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003290{
Florin Coras99368312018-08-02 10:45:44 -07003291 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003292 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003293 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003294 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003295 u64 buf;
3296
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003297 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3298 {
3299 vcl_api_retry_attach (wrk);
3300 return n_evts;
3301 }
3302
Florin Coras134a9962018-08-28 11:32:04 -07003303 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003304 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003305 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003306 if (timeout_ms > 0)
3307 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003308 }
3309
Florin Corasb13ba132021-01-27 16:05:24 -08003310 do
3311 {
3312 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003313 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003314 if (n_mq_evts < 0)
3315 {
3316 VDBG (0, "epoll_wait error %u", errno);
3317 return n_evts;
3318 }
3319
3320 for (i = 0; i < n_mq_evts; i++)
3321 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003322 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3323 {
3324 /* api socket was closed */
3325 vcl_api_handle_disconnect (wrk);
3326 continue;
3327 }
3328
Florin Corasb13ba132021-01-27 16:05:24 -08003329 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3330 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3331 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3332 &n_evts);
3333 }
3334
Florin Corasccdb8b82021-04-28 13:01:06 -07003335 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003336 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003337 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003338 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003339
3340 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003341}
3342
Florin Corasfe286f72021-06-04 10:07:55 -07003343static void
Florin Corasfe286f72021-06-04 10:07:55 -07003344vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3345 int maxevents, u32 *n_evts)
3346{
Florin Coras734268f2021-06-30 07:54:29 -07003347 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003348 vcl_session_t *s;
3349 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003350 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003351
Florin Corasfa3884f2021-06-22 20:04:46 -07003352 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003353 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003354 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003355
Florin Corasfa3884f2021-06-22 20:04:46 -07003356 next = wrk->ep_lt_current;
3357 do
Florin Corasfe286f72021-06-04 10:07:55 -07003358 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003359 s = vcl_session_get (wrk, next);
3360 next = s->vep.lt_next;
3361
3362 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003363 {
3364 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003365 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003366 evt_data = s->vep.ev.data.u64;
3367 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003368 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003369 {
3370 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003371 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3372 evt_data = s->vep.ev.data.u64;
3373 }
3374 if (!add_event && s->session_state > VCL_STATE_READY)
3375 {
3376 add_event = 1;
3377 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003378 evt_data = s->vep.ev.data.u64;
3379 }
3380 if (add_event)
3381 {
3382 events[*n_evts].data.u64 = evt_data;
3383 *n_evts += 1;
3384 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003385 if (EPOLLONESHOT & s->vep.ev.events)
3386 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003387 if (*n_evts == maxevents)
3388 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003389 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003390 break;
3391 }
3392 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003393 else
3394 {
3395 vcl_epoll_lt_del (wrk, s);
3396 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3397 break;
3398 }
Florin Corasfe286f72021-06-04 10:07:55 -07003399 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003400 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003401}
3402
Dave Wallacef7f809c2017-10-03 01:48:42 -04003403int
Florin Coras134a9962018-08-28 11:32:04 -07003404vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003405 int maxevents, double wait_for_time)
3406{
Florin Coras134a9962018-08-28 11:32:04 -07003407 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003408 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003409 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003410 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003411
3412 if (PREDICT_FALSE (maxevents <= 0))
3413 {
Florin Coras5e062572019-03-14 19:07:51 -07003414 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003415 return VPPCOM_EINVAL;
3416 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003417
Florin Coras134a9962018-08-28 11:32:04 -07003418 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003419 if (!vep_session)
3420 return VPPCOM_EBADFD;
3421
Florin Coras6c3b2182020-10-19 18:36:48 -07003422 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003423 {
Florin Coras5e062572019-03-14 19:07:51 -07003424 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003425 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003426 }
Florin Coras54693d22018-07-17 10:46:29 -07003427
Florin Coras86f04502018-09-12 16:08:01 -07003428 if (vec_len (wrk->unhandled_evts_vector))
3429 {
3430 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3431 {
3432 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3433 events, &n_evts);
3434 if (n_evts == maxevents)
3435 {
Florin Corase003a1b2019-06-05 10:47:16 -07003436 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3437 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003438 }
3439 }
Florin Corase003a1b2019-06-05 10:47:16 -07003440 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003441 }
liuyacancba87102021-09-10 15:14:05 +08003442
3443 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3444 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3445
wanghanlin8919fec2021-03-18 20:00:41 +08003446 /* Request to only drain unhandled */
3447 if ((int) wait_for_time == -2)
3448 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003449
Florin Coras86f04502018-09-12 16:08:01 -07003450
Florin Corasfe286f72021-06-04 10:07:55 -07003451 if (vcm->cfg.use_mq_eventfd)
3452 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3453 wait_for_time);
3454 else
3455 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3456 wait_for_time);
3457
Florin Corasfe286f72021-06-04 10:07:55 -07003458 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003459}
3460
Dave Wallace35830af2017-10-09 01:43:42 -04003461int
Florin Coras134a9962018-08-28 11:32:04 -07003462vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003463 void *buffer, uint32_t * buflen)
3464{
Florin Coras134a9962018-08-28 11:32:04 -07003465 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003466 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003467 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003468 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003469 vcl_session_t *session;
3470 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003471
Florin Coras134a9962018-08-28 11:32:04 -07003472 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003473 if (!session)
3474 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003475
Dave Wallace35830af2017-10-09 01:43:42 -04003476 switch (op)
3477 {
3478 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003479 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003480 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3481 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003482 break;
3483
Dave Wallace227867f2017-11-13 21:21:53 -05003484 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003485 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003486 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3487 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003488 break;
3489
3490 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003491 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003492 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003493 *flags =
3494 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003495 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003496 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003497 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003498 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3499 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003500 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003501 }
3502 else
3503 rv = VPPCOM_EINVAL;
3504 break;
3505
3506 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003508 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003509 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003510 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003511 else
Florin Corasac422d62020-10-19 20:51:36 -07003512 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003513
Florin Coras5e062572019-03-14 19:07:51 -07003514 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3515 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003516 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003517 }
3518 else
3519 rv = VPPCOM_EINVAL;
3520 break;
3521
3522 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003523 if (PREDICT_TRUE (buffer && buflen &&
3524 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003525 {
Florin Coras7e12d942018-06-27 14:32:43 -07003526 ep->is_ip4 = session->transport.is_ip4;
3527 ep->port = session->transport.rmt_port;
3528 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003529 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3530 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003531 else
Dave Barach178cf492018-11-13 16:34:13 -05003532 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3533 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003534 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003535 VDBG (1,
3536 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3537 "addr = %U, port %u",
3538 session_handle, ep->is_ip4, vcl_format_ip46_address,
3539 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003540 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3541 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003542 }
3543 else
3544 rv = VPPCOM_EINVAL;
3545 break;
3546
3547 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003548 if (PREDICT_TRUE (buffer && buflen &&
3549 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003550 {
Florin Coras7e12d942018-06-27 14:32:43 -07003551 ep->is_ip4 = session->transport.is_ip4;
3552 ep->port = session->transport.lcl_port;
3553 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003554 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3555 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003556 else
Dave Barach178cf492018-11-13 16:34:13 -05003557 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3558 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003559 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003560 VDBG (1,
3561 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3562 " port %d",
3563 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003564 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003565 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3566 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003567 }
3568 else
3569 rv = VPPCOM_EINVAL;
3570 break;
Stevenb5a11602017-10-11 09:59:30 -07003571
Florin Corasef7cbf62019-10-17 09:56:27 -07003572 case VPPCOM_ATTR_SET_LCL_ADDR:
3573 if (PREDICT_TRUE (buffer && buflen &&
3574 (*buflen >= sizeof (*ep)) && ep->ip))
3575 {
3576 session->transport.is_ip4 = ep->is_ip4;
3577 session->transport.lcl_port = ep->port;
3578 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3579 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003580 VDBG (1,
3581 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3582 " port %d",
3583 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003584 &session->transport.lcl_ip,
3585 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3586 clib_net_to_host_u16 (ep->port));
3587 }
3588 else
3589 rv = VPPCOM_EINVAL;
3590 break;
3591
Dave Wallace048b1d62018-01-03 22:24:41 -05003592 case VPPCOM_ATTR_GET_LIBC_EPFD:
3593 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003594 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003595 break;
3596
3597 case VPPCOM_ATTR_SET_LIBC_EPFD:
3598 if (PREDICT_TRUE (buffer && buflen &&
3599 (*buflen == sizeof (session->libc_epfd))))
3600 {
3601 session->libc_epfd = *(int *) buffer;
3602 *buflen = sizeof (session->libc_epfd);
3603
Florin Coras5e062572019-03-14 19:07:51 -07003604 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3605 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003606 }
3607 else
3608 rv = VPPCOM_EINVAL;
3609 break;
3610
3611 case VPPCOM_ATTR_GET_PROTOCOL:
3612 if (buffer && buflen && (*buflen >= sizeof (int)))
3613 {
Florin Coras7e12d942018-06-27 14:32:43 -07003614 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003615 *buflen = sizeof (int);
3616
Florin Coras5e062572019-03-14 19:07:51 -07003617 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3618 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003619 }
3620 else
3621 rv = VPPCOM_EINVAL;
3622 break;
3623
3624 case VPPCOM_ATTR_GET_LISTEN:
3625 if (buffer && buflen && (*buflen >= sizeof (int)))
3626 {
Florin Corasac422d62020-10-19 20:51:36 -07003627 *(int *) buffer = vcl_session_has_attr (session,
3628 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003629 *buflen = sizeof (int);
3630
Florin Coras5e062572019-03-14 19:07:51 -07003631 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3632 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003633 }
3634 else
3635 rv = VPPCOM_EINVAL;
3636 break;
3637
3638 case VPPCOM_ATTR_GET_ERROR:
3639 if (buffer && buflen && (*buflen >= sizeof (int)))
3640 {
3641 *(int *) buffer = 0;
3642 *buflen = sizeof (int);
3643
Florin Coras5e062572019-03-14 19:07:51 -07003644 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3645 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003646 }
3647 else
3648 rv = VPPCOM_EINVAL;
3649 break;
3650
3651 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3652 if (buffer && buflen && (*buflen >= sizeof (u32)))
3653 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003654
3655 /* VPP-TBD */
3656 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003657 session->tx_fifo ?
3658 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003659 vcm->cfg.tx_fifo_size);
3660 *buflen = sizeof (u32);
3661
Florin Coras5e062572019-03-14 19:07:51 -07003662 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3663 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3664 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003665 }
3666 else
3667 rv = VPPCOM_EINVAL;
3668 break;
3669
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003670 case VPPCOM_ATTR_SET_DSCP:
3671 if (buffer && buflen && (*buflen >= sizeof (u8)))
3672 {
3673 session->dscp = *(u8 *) buffer;
3674
3675 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3676 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3677 }
3678 else
3679 rv = VPPCOM_EINVAL;
3680 break;
3681
Dave Wallace048b1d62018-01-03 22:24:41 -05003682 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3683 if (buffer && buflen && (*buflen == sizeof (u32)))
3684 {
3685 /* VPP-TBD */
3686 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003687 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3688 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3689 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003690 }
3691 else
3692 rv = VPPCOM_EINVAL;
3693 break;
3694
3695 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3696 if (buffer && buflen && (*buflen >= sizeof (u32)))
3697 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003698
3699 /* VPP-TBD */
3700 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003701 session->rx_fifo ?
3702 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003703 vcm->cfg.rx_fifo_size);
3704 *buflen = sizeof (u32);
3705
Florin Coras5e062572019-03-14 19:07:51 -07003706 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3707 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003708 }
3709 else
3710 rv = VPPCOM_EINVAL;
3711 break;
3712
3713 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3714 if (buffer && buflen && (*buflen == sizeof (u32)))
3715 {
3716 /* VPP-TBD */
3717 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003718 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3719 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3720 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003721 }
3722 else
3723 rv = VPPCOM_EINVAL;
3724 break;
3725
3726 case VPPCOM_ATTR_GET_REUSEADDR:
3727 if (buffer && buflen && (*buflen >= sizeof (int)))
3728 {
3729 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003730 *(int *) buffer = vcl_session_has_attr (session,
3731 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003732 *buflen = sizeof (int);
3733
Florin Coras5e062572019-03-14 19:07:51 -07003734 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3735 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003736 }
3737 else
3738 rv = VPPCOM_EINVAL;
3739 break;
3740
Stevenb5a11602017-10-11 09:59:30 -07003741 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003742 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003743 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003744 {
3745 /* VPP-TBD */
3746 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003747 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003748 else
Florin Corasac422d62020-10-19 20:51:36 -07003749 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003750
Florin Coras5e062572019-03-14 19:07:51 -07003751 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003752 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003753 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003754 }
3755 else
3756 rv = VPPCOM_EINVAL;
3757 break;
3758
3759 case VPPCOM_ATTR_GET_REUSEPORT:
3760 if (buffer && buflen && (*buflen >= sizeof (int)))
3761 {
3762 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003763 *(int *) buffer = vcl_session_has_attr (session,
3764 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003765 *buflen = sizeof (int);
3766
Florin Coras5e062572019-03-14 19:07:51 -07003767 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3768 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003769 }
3770 else
3771 rv = VPPCOM_EINVAL;
3772 break;
3773
3774 case VPPCOM_ATTR_SET_REUSEPORT:
3775 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003776 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003777 {
3778 /* VPP-TBD */
3779 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003780 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003781 else
Florin Corasac422d62020-10-19 20:51:36 -07003782 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003783
Florin Coras5e062572019-03-14 19:07:51 -07003784 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003785 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003786 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003787 }
3788 else
3789 rv = VPPCOM_EINVAL;
3790 break;
3791
3792 case VPPCOM_ATTR_GET_BROADCAST:
3793 if (buffer && buflen && (*buflen >= sizeof (int)))
3794 {
3795 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003796 *(int *) buffer = vcl_session_has_attr (session,
3797 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003798 *buflen = sizeof (int);
3799
Florin Coras5e062572019-03-14 19:07:51 -07003800 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3801 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003802 }
3803 else
3804 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003805 break;
3806
3807 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003808 if (buffer && buflen && (*buflen == sizeof (int)))
3809 {
3810 /* VPP-TBD */
3811 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003812 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003813 else
Florin Corasac422d62020-10-19 20:51:36 -07003814 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003815
Florin Coras5e062572019-03-14 19:07:51 -07003816 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003817 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003818 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 }
3820 else
3821 rv = VPPCOM_EINVAL;
3822 break;
3823
3824 case VPPCOM_ATTR_GET_V6ONLY:
3825 if (buffer && buflen && (*buflen >= sizeof (int)))
3826 {
3827 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003828 *(int *) buffer = vcl_session_has_attr (session,
3829 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003830 *buflen = sizeof (int);
3831
Florin Coras5e062572019-03-14 19:07:51 -07003832 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3833 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003834 }
3835 else
3836 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003837 break;
3838
3839 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003840 if (buffer && buflen && (*buflen == sizeof (int)))
3841 {
3842 /* VPP-TBD */
3843 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003844 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003845 else
Florin Corasac422d62020-10-19 20:51:36 -07003846 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003847
Florin Coras5e062572019-03-14 19:07:51 -07003848 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003849 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003850 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003851 }
3852 else
3853 rv = VPPCOM_EINVAL;
3854 break;
3855
3856 case VPPCOM_ATTR_GET_KEEPALIVE:
3857 if (buffer && buflen && (*buflen >= sizeof (int)))
3858 {
3859 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003860 *(int *) buffer = vcl_session_has_attr (session,
3861 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003862 *buflen = sizeof (int);
3863
Florin Coras5e062572019-03-14 19:07:51 -07003864 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3865 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003866 }
3867 else
3868 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003869 break;
Stevenbccd3392017-10-12 20:42:21 -07003870
3871 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003872 if (buffer && buflen && (*buflen == sizeof (int)))
3873 {
3874 /* VPP-TBD */
3875 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003876 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003877 else
Florin Corasac422d62020-10-19 20:51:36 -07003878 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003879
Florin Coras5e062572019-03-14 19:07:51 -07003880 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003881 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003882 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003883 }
3884 else
3885 rv = VPPCOM_EINVAL;
3886 break;
3887
3888 case VPPCOM_ATTR_GET_TCP_NODELAY:
3889 if (buffer && buflen && (*buflen >= sizeof (int)))
3890 {
3891 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003892 *(int *) buffer = vcl_session_has_attr (session,
3893 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003894 *buflen = sizeof (int);
3895
Florin Coras5e062572019-03-14 19:07:51 -07003896 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3897 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003898 }
3899 else
3900 rv = VPPCOM_EINVAL;
3901 break;
3902
3903 case VPPCOM_ATTR_SET_TCP_NODELAY:
3904 if (buffer && buflen && (*buflen == sizeof (int)))
3905 {
3906 /* VPP-TBD */
3907 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003908 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003909 else
Florin Corasac422d62020-10-19 20:51:36 -07003910 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003911
Florin Coras5e062572019-03-14 19:07:51 -07003912 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003913 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003914 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003915 }
3916 else
3917 rv = VPPCOM_EINVAL;
3918 break;
3919
3920 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3921 if (buffer && buflen && (*buflen >= sizeof (int)))
3922 {
3923 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003924 *(int *) buffer = vcl_session_has_attr (session,
3925 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003926 *buflen = sizeof (int);
3927
Florin Coras5e062572019-03-14 19:07:51 -07003928 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3929 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003930 }
3931 else
3932 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003933 break;
3934
3935 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003936 if (buffer && buflen && (*buflen == sizeof (int)))
3937 {
3938 /* VPP-TBD */
3939 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003940 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003941 else
Florin Corasac422d62020-10-19 20:51:36 -07003942 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003943
Florin Coras5e062572019-03-14 19:07:51 -07003944 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003945 vcl_session_has_attr (session,
3946 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003947 }
3948 else
3949 rv = VPPCOM_EINVAL;
3950 break;
3951
3952 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3953 if (buffer && buflen && (*buflen >= sizeof (int)))
3954 {
3955 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003956 *(int *) buffer = vcl_session_has_attr (session,
3957 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003958 *buflen = sizeof (int);
3959
Florin Coras5e062572019-03-14 19:07:51 -07003960 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3961 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003962 }
3963 else
3964 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003965 break;
3966
3967 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003968 if (buffer && buflen && (*buflen == sizeof (int)))
3969 {
3970 /* VPP-TBD */
3971 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003972 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003973 else
Florin Corasac422d62020-10-19 20:51:36 -07003974 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003975
Florin Coras5e062572019-03-14 19:07:51 -07003976 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003977 vcl_session_has_attr (session,
3978 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003979 }
3980 else
3981 rv = VPPCOM_EINVAL;
3982 break;
3983
3984 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003985 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003986 {
Florin Coras04ae8272021-04-12 19:55:37 -07003987 rv = VPPCOM_EINVAL;
3988 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003989 }
Florin Coras04ae8272021-04-12 19:55:37 -07003990
3991 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3992 tea.mss = *(u32 *) buffer;
3993 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3994 rv = VPPCOM_ENOPROTOOPT;
3995
3996 if (!rv)
3997 {
3998 *(u32 *) buffer = tea.mss;
3999 *buflen = sizeof (int);
4000 }
4001
4002 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
4003 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004004 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004005 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07004006 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05004007 {
Florin Coras04ae8272021-04-12 19:55:37 -07004008 rv = VPPCOM_EINVAL;
4009 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004010 }
Florin Coras04ae8272021-04-12 19:55:37 -07004011
4012 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4013 tea.mss = *(u32 *) buffer;
4014 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4015 rv = VPPCOM_ENOPROTOOPT;
4016
4017 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4018 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004019 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004020
Florin Coras1e966172020-05-16 18:18:14 +00004021 case VPPCOM_ATTR_SET_CONNECTED:
4022 session->flags |= VCL_SESSION_F_CONNECTED;
4023 break;
4024
Florin Corasa5a9efd2021-01-05 17:03:29 -08004025 case VPPCOM_ATTR_SET_CKPAIR:
4026 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4027 !vcl_session_has_crypto (session))
4028 {
4029 rv = VPPCOM_EINVAL;
4030 break;
4031 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004032 if (!session->ext_config)
4033 {
Florin Coras87f63892021-05-01 16:01:40 -07004034 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4035 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004036 }
4037 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4038 {
4039 rv = VPPCOM_EINVAL;
4040 break;
4041 }
4042
4043 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004044 break;
4045
Florin Coras6a6555a2021-01-28 11:39:27 -08004046 case VPPCOM_ATTR_SET_VRF:
4047 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4048 {
4049 rv = VPPCOM_EINVAL;
4050 break;
4051 }
4052 session->vrf = *(u32 *) buffer;
4053 break;
4054
4055 case VPPCOM_ATTR_GET_VRF:
4056 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4057 {
4058 rv = VPPCOM_EINVAL;
4059 break;
4060 }
4061 *(u32 *) buffer = session->vrf;
4062 *buflen = sizeof (u32);
4063 break;
4064
wanghanlin0674f852021-02-22 10:38:36 +08004065 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004066 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004067 {
Florin Corasd77325c2021-02-23 12:03:03 -08004068 rv = VPPCOM_EINVAL;
4069 break;
wanghanlin0674f852021-02-22 10:38:36 +08004070 }
Florin Corasd77325c2021-02-23 12:03:03 -08004071
4072 if (session->transport.is_ip4)
4073 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004074 else
Florin Corasd77325c2021-02-23 12:03:03 -08004075 *(int *) buffer = AF_INET6;
4076 *buflen = sizeof (int);
4077
wanghanlin0674f852021-02-22 10:38:36 +08004078 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4079 *buflen);
4080 break;
4081
Florin Coras87f63892021-05-01 16:01:40 -07004082 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4083 if (!(buffer && buflen && (*buflen > 0)))
4084 {
4085 rv = VPPCOM_EINVAL;
4086 break;
4087 }
4088 if (session->ext_config)
4089 {
4090 rv = VPPCOM_EINVAL;
4091 break;
4092 }
4093 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4094 *buflen + sizeof (u32));
4095 clib_memcpy (session->ext_config->data, buffer, *buflen);
4096 session->ext_config->len = *buflen;
4097 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004098 case VPPCOM_ATTR_SET_IP_PKTINFO:
4099 if (buffer && buflen && (*buflen == sizeof (int)) &&
4100 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4101 {
4102 if (*(int *) buffer)
4103 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4104 else
4105 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4106
4107 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4108 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4109 *buflen);
4110 }
4111 else
4112 rv = VPPCOM_EINVAL;
4113 break;
4114
4115 case VPPCOM_ATTR_GET_IP_PKTINFO:
4116 if (buffer && buflen && (*buflen >= sizeof (int)))
4117 {
4118 *(int *) buffer =
4119 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4120 *buflen = sizeof (int);
4121
4122 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4123 *buflen);
4124 }
4125 else
4126 rv = VPPCOM_EINVAL;
4127 break;
Florin Coras87f63892021-05-01 16:01:40 -07004128
Dave Wallacee22aa742017-10-20 12:30:38 -04004129 default:
4130 rv = VPPCOM_EINVAL;
4131 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004132 }
4133
Dave Wallace35830af2017-10-09 01:43:42 -04004134 return rv;
4135}
4136
Stevenac1f96d2017-10-24 16:03:58 -07004137int
Florin Coras134a9962018-08-28 11:32:04 -07004138vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004139 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4140{
Florin Coras134a9962018-08-28 11:32:04 -07004141 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004142 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004143 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004144
4145 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004146 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004147 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004148 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004149 else
4150 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004151 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004152 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004153 }
4154
Florin Coras0a1e1832020-03-29 18:54:04 +00004155 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004156 {
Florin Coras5da10c42020-04-01 04:31:21 +00004157 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004158 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004159 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4160 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004161 else
Dave Barach178cf492018-11-13 16:34:13 -05004162 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4163 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004164 ep->is_ip4 = session->transport.is_ip4;
4165 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004166 }
Florin Coras460dce62018-07-27 05:45:06 -07004167
Stevenac1f96d2017-10-24 16:03:58 -07004168 return rv;
4169}
4170
Florin Coraseff5f7a2023-02-07 17:36:17 -08004171static void
4172vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4173{
4174 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4175
4176 do
4177 {
4178 switch (tlv->data_type)
4179 {
4180 case VCL_UDP_SEGMENT:
4181 s->gso_size = *(u16 *) tlv->data;
4182 break;
4183 case VCL_IP_PKTINFO:
4184 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4185 sizeof (ip4_address_t));
4186 break;
4187 default:
4188 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4189 break;
4190 }
4191 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4192 }
4193 while (tlv);
4194}
4195
Stevenac1f96d2017-10-24 16:03:58 -07004196int
Florin Coras134a9962018-08-28 11:32:04 -07004197vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004198 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4199{
Florin Coras7a2abce2020-04-05 19:25:44 +00004200 vcl_worker_t *wrk = vcl_worker_get_current ();
4201 vcl_session_t *s;
4202
4203 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004204 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004205 return VPPCOM_EBADFD;
4206
Dave Wallace617dffa2017-10-26 14:47:06 -04004207 if (ep)
4208 {
Florin Coras5824cc52020-10-20 18:44:41 -07004209 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004210 return VPPCOM_EINVAL;
4211
liuyacanbc0c7542021-08-02 20:15:05 +08004212 s->transport.is_ip4 = ep->is_ip4;
4213 s->transport.rmt_port = ep->port;
4214 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4215
Florin Coraseff5f7a2023-02-07 17:36:17 -08004216 if (ep->app_tlvs)
4217 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004218
Florin Coras5da10c42020-04-01 04:31:21 +00004219 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004220 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004221 {
Florin Coras5824cc52020-10-20 18:44:41 -07004222 u32 session_index = s->session_index;
4223 f64 timeout = vcm->cfg.session_timeout;
4224 int rv;
4225
Florin Coras5da10c42020-04-01 04:31:21 +00004226 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004227 rv = vppcom_wait_for_session_state_change (session_index,
4228 VCL_STATE_READY,
4229 timeout);
4230 if (rv < 0)
4231 return rv;
4232 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004233 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004234 }
4235
4236 if (flags)
4237 {
4238 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004239 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004240 }
4241
Florin Coraseff5f7a2023-02-07 17:36:17 -08004242 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004243 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004244}
4245
Dave Wallace048b1d62018-01-03 22:24:41 -05004246int
4247vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4248{
Florin Coras134a9962018-08-28 11:32:04 -07004249 vcl_worker_t *wrk = vcl_worker_get_current ();
4250 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004251 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004252 svm_msg_q_msg_t msg;
4253 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004254 int rv, num_ev = 0;
4255
Florin Coras5e062572019-03-14 19:07:51 -07004256 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004257
4258 if (!vp)
4259 return VPPCOM_EFAULT;
4260
4261 do
4262 {
Florin Coras7e12d942018-06-27 14:32:43 -07004263 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004264
Florin Coras6917b942018-11-13 22:44:54 -08004265 /* Dequeue all events and drop all unhandled io events */
4266 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4267 {
4268 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4269 vcl_handle_mq_event (wrk, e);
4270 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4271 }
4272 vec_reset_length (wrk->unhandled_evts_vector);
4273
Dave Wallace048b1d62018-01-03 22:24:41 -05004274 for (i = 0; i < n_sids; i++)
4275 {
Florin Coras7baeb712019-01-04 17:05:43 -08004276 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004277 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004278 {
4279 vp[i].revents = POLLHUP;
4280 num_ev++;
4281 continue;
4282 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004283
Florin Coras6917b942018-11-13 22:44:54 -08004284 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004285
4286 if (POLLIN & vp[i].events)
4287 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004288 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004289 if (rv > 0)
4290 {
Florin Coras6917b942018-11-13 22:44:54 -08004291 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004292 num_ev++;
4293 }
4294 else if (rv < 0)
4295 {
4296 switch (rv)
4297 {
4298 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004299 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004300 break;
4301
4302 default:
Florin Coras6917b942018-11-13 22:44:54 -08004303 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004304 break;
4305 }
4306 num_ev++;
4307 }
4308 }
4309
4310 if (POLLOUT & vp[i].events)
4311 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004312 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004313 if (rv > 0)
4314 {
Florin Coras6917b942018-11-13 22:44:54 -08004315 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004316 num_ev++;
4317 }
4318 else if (rv < 0)
4319 {
4320 switch (rv)
4321 {
4322 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004323 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004324 break;
4325
4326 default:
Florin Coras6917b942018-11-13 22:44:54 -08004327 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004328 break;
4329 }
4330 num_ev++;
4331 }
4332 }
4333
Dave Wallace7e607a72018-06-18 18:41:32 -04004334 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004335 {
Florin Coras6917b942018-11-13 22:44:54 -08004336 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004337 num_ev++;
4338 }
4339 }
4340 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004341 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004342 }
4343 while ((num_ev == 0) && keep_trying);
4344
Dave Wallace048b1d62018-01-03 22:24:41 -05004345 return num_ev;
4346}
4347
Florin Coras99368312018-08-02 10:45:44 -07004348int
4349vppcom_mq_epoll_fd (void)
4350{
Florin Coras134a9962018-08-28 11:32:04 -07004351 vcl_worker_t *wrk = vcl_worker_get_current ();
4352 return wrk->mqs_epfd;
4353}
4354
4355int
Florin Coras30e79c22019-01-02 19:31:22 -08004356vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004357{
4358 return session_handle & 0xFFFFFF;
4359}
4360
4361int
Florin Coras30e79c22019-01-02 19:31:22 -08004362vppcom_session_worker (vcl_session_handle_t session_handle)
4363{
4364 return session_handle >> 24;
4365}
4366
4367int
Florin Coras134a9962018-08-28 11:32:04 -07004368vppcom_worker_register (void)
4369{
Florin Coras47c40e22018-11-26 17:01:36 -08004370 if (!vcl_worker_alloc_and_init ())
4371 return VPPCOM_EEXIST;
4372
Florin Coras47c40e22018-11-26 17:01:36 -08004373 if (vcl_worker_register_with_vpp ())
4374 return VPPCOM_EEXIST;
4375
4376 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004377}
4378
Florin Coras369db832019-07-08 12:34:45 -07004379void
4380vppcom_worker_unregister (void)
4381{
4382 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4383 vcl_set_worker_index (~0);
4384}
4385
Pivo6017ff02020-05-04 17:57:33 +02004386void
4387vppcom_worker_index_set (int index)
4388{
4389 vcl_set_worker_index (index);
4390}
4391
Florin Corasdfe4cf42018-11-28 22:13:45 -08004392int
4393vppcom_worker_index (void)
4394{
4395 return vcl_get_worker_index ();
4396}
4397
Florin Corase0982e52019-01-25 13:19:56 -08004398int
4399vppcom_worker_mqs_epfd (void)
4400{
4401 vcl_worker_t *wrk = vcl_worker_get_current ();
4402 if (!vcm->cfg.use_mq_eventfd)
4403 return -1;
4404 return wrk->mqs_epfd;
4405}
4406
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004407int
4408vppcom_session_is_connectable_listener (uint32_t session_handle)
4409{
4410 vcl_session_t *session;
4411 vcl_worker_t *wrk = vcl_worker_get_current ();
4412 session = vcl_session_get_w_handle (wrk, session_handle);
4413 if (!session)
4414 return VPPCOM_EBADFD;
4415 return vcl_session_is_connectable_listener (wrk, session);
4416}
4417
4418int
4419vppcom_session_listener (uint32_t session_handle)
4420{
4421 vcl_worker_t *wrk = vcl_worker_get_current ();
4422 vcl_session_t *listen_session, *session;
4423 session = vcl_session_get_w_handle (wrk, session_handle);
4424 if (!session)
4425 return VPPCOM_EBADFD;
4426 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4427 return VPPCOM_EBADFD;
4428 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4429 if (!listen_session)
4430 return VPPCOM_EBADFD;
4431 return vcl_session_handle (listen_session);
4432}
4433
4434int
4435vppcom_session_n_accepted (uint32_t session_handle)
4436{
4437 vcl_worker_t *wrk = vcl_worker_get_current ();
4438 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4439 if (!session)
4440 return VPPCOM_EBADFD;
4441 return session->n_accepted_sessions;
4442}
4443
Florin Coras66ec4672020-06-15 07:59:40 -07004444const char *
4445vppcom_proto_str (vppcom_proto_t proto)
4446{
4447 char const *proto_str;
4448
4449 switch (proto)
4450 {
4451 case VPPCOM_PROTO_TCP:
4452 proto_str = "TCP";
4453 break;
4454 case VPPCOM_PROTO_UDP:
4455 proto_str = "UDP";
4456 break;
4457 case VPPCOM_PROTO_TLS:
4458 proto_str = "TLS";
4459 break;
4460 case VPPCOM_PROTO_QUIC:
4461 proto_str = "QUIC";
4462 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004463 case VPPCOM_PROTO_DTLS:
4464 proto_str = "DTLS";
4465 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004466 case VPPCOM_PROTO_SRTP:
4467 proto_str = "SRTP";
4468 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004469 default:
4470 proto_str = "UNKNOWN";
4471 break;
4472 }
4473 return proto_str;
4474}
4475
4476const char *
4477vppcom_retval_str (int retval)
4478{
4479 char const *st;
4480
4481 switch (retval)
4482 {
4483 case VPPCOM_OK:
4484 st = "VPPCOM_OK";
4485 break;
4486
4487 case VPPCOM_EAGAIN:
4488 st = "VPPCOM_EAGAIN";
4489 break;
4490
4491 case VPPCOM_EFAULT:
4492 st = "VPPCOM_EFAULT";
4493 break;
4494
4495 case VPPCOM_ENOMEM:
4496 st = "VPPCOM_ENOMEM";
4497 break;
4498
4499 case VPPCOM_EINVAL:
4500 st = "VPPCOM_EINVAL";
4501 break;
4502
4503 case VPPCOM_EBADFD:
4504 st = "VPPCOM_EBADFD";
4505 break;
4506
4507 case VPPCOM_EAFNOSUPPORT:
4508 st = "VPPCOM_EAFNOSUPPORT";
4509 break;
4510
4511 case VPPCOM_ECONNABORTED:
4512 st = "VPPCOM_ECONNABORTED";
4513 break;
4514
4515 case VPPCOM_ECONNRESET:
4516 st = "VPPCOM_ECONNRESET";
4517 break;
4518
4519 case VPPCOM_ENOTCONN:
4520 st = "VPPCOM_ENOTCONN";
4521 break;
4522
4523 case VPPCOM_ECONNREFUSED:
4524 st = "VPPCOM_ECONNREFUSED";
4525 break;
4526
4527 case VPPCOM_ETIMEDOUT:
4528 st = "VPPCOM_ETIMEDOUT";
4529 break;
4530
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304531 case VPPCOM_EADDRINUSE:
4532 st = "VPPCOM_EADDRINUSE";
4533 break;
4534
Florin Coras66ec4672020-06-15 07:59:40 -07004535 default:
4536 st = "UNKNOWN_STATE";
4537 break;
4538 }
4539
4540 return st;
4541}
4542
Florin Corasa5a9efd2021-01-05 17:03:29 -08004543int
4544vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4545{
4546 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004547 return vcl_sapi_add_cert_key_pair (ckpair);
4548 else
4549 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004550}
4551
4552int
4553vppcom_del_cert_key_pair (uint32_t ckpair_index)
4554{
4555 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004556 return vcl_sapi_del_cert_key_pair (ckpair_index);
4557 else
4558 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004559}
4560
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304561int
4562vppcom_session_get_error (uint32_t session_handle)
4563{
4564 vcl_worker_t *wrk = vcl_worker_get_current ();
4565 vcl_session_t *session = 0;
4566
4567 session = vcl_session_get_w_handle (wrk, session_handle);
4568 if (!session)
4569 return VPPCOM_EBADFD;
4570
4571 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4572 {
4573 VWRN ("epoll session %u! will not have connect", session->session_index);
4574 return VPPCOM_EBADFD;
4575 }
4576
4577 if (session->vpp_error == SESSION_E_PORTINUSE)
4578 return VPPCOM_EADDRINUSE;
4579 else if (session->vpp_error == SESSION_E_REFUSED)
4580 return VPPCOM_ECONNREFUSED;
4581 else if (session->vpp_error != SESSION_E_NONE)
4582 return VPPCOM_EFAULT;
4583 else
4584 return VPPCOM_OK;
4585}
4586
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004587int
4588vppcom_worker_is_detached (void)
4589{
4590 vcl_worker_t *wrk = vcl_worker_get_current ();
4591
4592 if (!vcm->cfg.use_mq_eventfd)
4593 return VPPCOM_ENOTSUP;
4594
4595 return wrk->api_client_handle == ~0;
4596}
4597
Dave Wallacee22aa742017-10-20 12:30:38 -04004598/*
4599 * fd.io coding-style-patch-verification: ON
4600 *
4601 * Local Variables:
4602 * eval: (c-set-style "gnu")
4603 * End:
4604 */