blob: 1b0da450781fd99287ca2725984378424436732b [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
2758int
Florin Coras134a9962018-08-28 11:32:04 -07002759vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002760 struct epoll_event *event)
2761{
Florin Coras134a9962018-08-28 11:32:04 -07002762 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002763 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002764 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002765 vcl_session_t *s;
2766 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002767
Florin Coras134a9962018-08-28 11:32:04 -07002768 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002770 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 return VPPCOM_EINVAL;
2772 }
2773
Florin Coras134a9962018-08-28 11:32:04 -07002774 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002775 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002777 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002778 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002779 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002780 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002781 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002782 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002783 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002784 }
2785
Florin Coras134a9962018-08-28 11:32:04 -07002786 ASSERT (vep_session->vep.vep_sh == ~0);
2787 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002788
Florin Coras17ec5772020-08-12 20:46:29 -07002789 s = vcl_session_get_w_handle (wrk, session_handle);
2790 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002792 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002793 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002794 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002795 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002796 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002797 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002798 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002799 }
2800
2801 switch (op)
2802 {
2803 case EPOLL_CTL_ADD:
2804 if (PREDICT_FALSE (!event))
2805 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002806 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002807 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002808 }
Florin Coras2645f682021-06-04 15:59:41 -07002809 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2810 {
2811 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2812 rv = VPPCOM_EEXIST;
2813 goto done;
2814 }
Florin Coras134a9962018-08-28 11:32:04 -07002815 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002816 {
Florin Coras7e12d942018-06-27 14:32:43 -07002817 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002818 next_session = vcl_session_get_w_handle (wrk,
2819 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002820 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002821 {
Florin Coras5e062572019-03-14 19:07:51 -07002822 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002823 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002824 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002825 }
Florin Coras134a9962018-08-28 11:32:04 -07002826 ASSERT (next_session->vep.prev_sh == vep_handle);
2827 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002828 }
Florin Coras17ec5772020-08-12 20:46:29 -07002829 s->vep.next_sh = vep_session->vep.next_sh;
2830 s->vep.prev_sh = vep_handle;
2831 s->vep.vep_sh = vep_handle;
2832 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002833 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002834 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002835 s->flags &= ~VCL_SESSION_F_IS_VEP;
2836 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002837 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002838
Florin Coras17ec5772020-08-12 20:46:29 -07002839 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2840 if (txf && (event->events & EPOLLOUT))
2841 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002842
Florin Coras6e3c1f82020-01-15 01:30:46 +00002843 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002844 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002845 {
2846 session_event_t e = { 0 };
2847 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002848 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002849 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002850 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002851 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002852 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002853 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002854 {
2855 session_event_t e = { 0 };
2856 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002857 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002858 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002859 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2860 add_evt = 1;
2861 }
2862 if (!add_evt && vcl_session_is_closing (s))
2863 {
2864 session_event_t e = { 0 };
2865 if (s->session_state == VCL_STATE_VPP_CLOSING)
2866 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2867 else
2868 e.event_type = SESSION_CTRL_EVT_RESET;
2869 e.session_index = s->session_index;
2870 e.postponed = 1;
2871 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002872 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002873 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2874 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002875 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002876 break;
2877
2878 case EPOLL_CTL_MOD:
2879 if (PREDICT_FALSE (!event))
2880 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002881 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002882 rv = VPPCOM_EINVAL;
2883 goto done;
2884 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002885 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002886 {
Florin Coras5e062572019-03-14 19:07:51 -07002887 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002888 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002889 goto done;
2890 }
Florin Coras17ec5772020-08-12 20:46:29 -07002891 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002892 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002893 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002894 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002895 rv = VPPCOM_EINVAL;
2896 goto done;
2897 }
hanlin475c9d72019-12-26 11:44:28 +08002898
Florin Coras2645f682021-06-04 15:59:41 -07002899 /* Generate EPOLLOUT if session write ready nd event was not on */
2900 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2901 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002902 {
2903 session_event_t e = { 0 };
2904 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002905 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002906 vec_add1 (wrk->unhandled_evts_vector, e);
2907 }
Florin Coras2645f682021-06-04 15:59:41 -07002908 /* Generate EPOLLIN if session read ready and event was not on */
2909 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2910 (vcl_session_read_ready (s) > 0))
2911 {
2912 session_event_t e = { 0 };
2913 e.event_type = SESSION_IO_EVT_RX;
2914 e.session_index = s->session_index;
2915 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002916 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002917 }
Florin Coras17ec5772020-08-12 20:46:29 -07002918 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2919 s->vep.ev = *event;
2920 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002921 if (txf)
2922 {
2923 if (event->events & EPOLLOUT)
2924 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2925 else
2926 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2927 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002928 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2929 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002930 break;
2931
2932 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002933 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002934 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002935 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002936 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002937 goto done;
2938 }
Florin Coras17ec5772020-08-12 20:46:29 -07002939 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002940 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002941 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002942 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002943 rv = VPPCOM_EINVAL;
2944 goto done;
2945 }
2946
Florin Coras17ec5772020-08-12 20:46:29 -07002947 if (s->vep.prev_sh == vep_handle)
2948 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002949 else
2950 {
Florin Coras7e12d942018-06-27 14:32:43 -07002951 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002952 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002953 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002954 {
Florin Coras5e062572019-03-14 19:07:51 -07002955 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002956 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002957 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002958 }
Florin Coras134a9962018-08-28 11:32:04 -07002959 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002960 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002961 }
Florin Coras17ec5772020-08-12 20:46:29 -07002962 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002963 {
Florin Coras7e12d942018-06-27 14:32:43 -07002964 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002965 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002966 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002967 {
Florin Coras5e062572019-03-14 19:07:51 -07002968 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002969 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002970 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002971 }
Florin Coras134a9962018-08-28 11:32:04 -07002972 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002973 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002974 }
2975
Florin Corasfa3884f2021-06-22 20:04:46 -07002976 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2977 vcl_epoll_lt_del (wrk, s);
2978
Florin Coras17ec5772020-08-12 20:46:29 -07002979 memset (&s->vep, 0, sizeof (s->vep));
2980 s->vep.next_sh = ~0;
2981 s->vep.prev_sh = ~0;
2982 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002983 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002984 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002985
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002986 if (vcl_session_is_open (s))
2987 {
2988 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2989 if (txf)
2990 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2991 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002992
Florin Coras5e062572019-03-14 19:07:51 -07002993 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002994 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002995 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002996 break;
2997
2998 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002999 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003000 rv = VPPCOM_EINVAL;
3001 }
3002
Florin Coras134a9962018-08-28 11:32:04 -07003003 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003004
3005done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04003006 return rv;
3007}
3008
Florin Coras86f04502018-09-12 16:08:01 -07003009static inline void
3010vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3011 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003012{
3013 session_disconnected_msg_t *disconnected_msg;
3014 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003015 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003016 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003017 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003018 u8 add_event = 0;
3019
3020 switch (e->event_type)
3021 {
Florin Coras653e43f2019-03-04 10:56:23 -08003022 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003023 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003024 s = vcl_session_get (wrk, sid);
3025 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003026 break;
Florin Corasb242d312020-10-26 15:35:40 -07003027 vcl_fifo_rx_evt_valid_or_break (s);
3028 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003029 if (!(EPOLLIN & s->vep.ev.events) ||
3030 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3031 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003032 break;
3033 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003034 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003035 session_evt_data = s->vep.ev.data.u64;
3036 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003037 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003038 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003039 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003040 s = vcl_session_get (wrk, sid);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003041 if (!s || !vcl_session_is_open (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003042 break;
Florin Corasb242d312020-10-26 15:35:40 -07003043 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003044 if (!(EPOLLOUT & session_events))
3045 break;
3046 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003047 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003048 session_evt_data = s->vep.ev.data.u64;
3049 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3050 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003051 break;
Florin Coras86f04502018-09-12 16:08:01 -07003052 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003053 if (!e->postponed)
3054 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3055 else
3056 s = vcl_session_get (wrk, e->session_index);
3057 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003058 break;
Florin Corasb242d312020-10-26 15:35:40 -07003059 session_events = s->vep.ev.events;
3060 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003061 if (!(EPOLLIN & session_events) ||
3062 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003063 break;
Florin Coras86f04502018-09-12 16:08:01 -07003064 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003065 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003066 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003067 break;
3068 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003069 if (!e->postponed)
3070 {
3071 connected_msg = (session_connected_msg_t *) e->data;
3072 sid = vcl_session_connected_handler (wrk, connected_msg);
3073 }
3074 else
3075 sid = e->session_index;
3076 s = vcl_session_get (wrk, sid);
3077 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003078 break;
Florin Corasb242d312020-10-26 15:35:40 -07003079 session_events = s->vep.ev.events;
3080 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003081 if (!(EPOLLOUT & session_events))
3082 break;
Florin Coras1d84abc2023-01-13 09:44:14 -08003083 /* We didn't have a fifo when the event was added */
3084 svm_fifo_add_want_deq_ntf (
3085 (vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo),
3086 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras72f77822019-01-22 19:05:52 -08003087 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003088 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003089 session_evt_data = s->vep.ev.data.u64;
3090 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003091 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003092 break;
3093 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003094 if (!e->postponed)
3095 {
3096 disconnected_msg = (session_disconnected_msg_t *) e->data;
3097 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3098 }
3099 else
3100 {
3101 s = vcl_session_get (wrk, e->session_index);
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003102 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
Florin Coras87f76002021-06-28 19:13:29 -07003103 }
3104 if (vcl_session_is_closed (s) ||
3105 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003106 {
Florin Coras74254b52021-12-08 11:03:08 -08003107 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003108 vcl_session_free (wrk, s);
3109 break;
3110 }
Florin Corasb242d312020-10-26 15:35:40 -07003111 sid = s->session_index;
3112 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003113 add_event = 1;
liuyacanffd9ddb2021-11-01 10:22:09 +08003114 if (EPOLLRDHUP & session_events)
3115 {
3116 /* If app can distinguish between RDHUP and HUP,
3117 * we make finer control */
3118 events[*num_ev].events = EPOLLRDHUP;
3119 if (s->flags & VCL_SESSION_F_WR_SHUTDOWN)
3120 {
3121 events[*num_ev].events |= EPOLLHUP;
3122 }
3123 }
3124 else
3125 {
3126 events[*num_ev].events = EPOLLHUP;
3127 }
Florin Corasb242d312020-10-26 15:35:40 -07003128 session_evt_data = s->vep.ev.data.u64;
liuyacanffd9ddb2021-11-01 10:22:09 +08003129
Florin Coras86f04502018-09-12 16:08:01 -07003130 break;
3131 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003132 if (!e->postponed)
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003133 {
3134 sid =
3135 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3136 s = vcl_session_get (wrk, sid);
3137 }
Florin Coras87f76002021-06-28 19:13:29 -07003138 else
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003139 {
3140 sid = e->session_index;
3141 s = vcl_session_get (wrk, sid);
3142 s->flags &= ~VCL_SESSION_F_PENDING_DISCONNECT;
3143 }
Florin Coras87f76002021-06-28 19:13:29 -07003144 if (vcl_session_is_closed (s) ||
3145 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003146 {
Florin Coras74254b52021-12-08 11:03:08 -08003147 if (s && (s->flags & VCL_SESSION_F_PENDING_FREE))
Florin Corasf0fe1ea2021-12-03 17:03:37 -08003148 vcl_session_free (wrk, s);
3149 break;
3150 }
Florin Corasb242d312020-10-26 15:35:40 -07003151 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003152 add_event = 1;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003153 events[*num_ev].events = EPOLLERR | EPOLLHUP;
3154 if ((EPOLLRDHUP & session_events) &&
3155 (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3156 {
Yacan Liuab157702022-09-26 16:41:32 +08003157 events[*num_ev].events |= EPOLLRDHUP;
Yacan Liuf5e0a172022-09-20 14:19:19 +08003158 }
3159 if ((EPOLLIN & session_events) && (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
3160 {
3161 events[*num_ev].events |= EPOLLIN;
3162 }
Florin Corasb242d312020-10-26 15:35:40 -07003163 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003164 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003165 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3166 vcl_session_unlisten_reply_handler (wrk, e->data);
3167 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003168 case SESSION_CTRL_EVT_MIGRATED:
3169 vcl_session_migrated_handler (wrk, e->data);
3170 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003171 case SESSION_CTRL_EVT_CLEANUP:
3172 vcl_session_cleanup_handler (wrk, e->data);
3173 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003174 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3175 vcl_session_req_worker_update_handler (wrk, e->data);
3176 break;
3177 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3178 vcl_session_worker_update_reply_handler (wrk, e->data);
3179 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003180 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3181 vcl_session_app_add_segment_handler (wrk, e->data);
3182 break;
3183 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3184 vcl_session_app_del_segment_handler (wrk, e->data);
3185 break;
hanlina3a48962020-07-13 11:09:15 +08003186 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003187 vcl_worker_rpc_handler (wrk, e->data);
3188 break;
Florin Coras86f04502018-09-12 16:08:01 -07003189 default:
3190 VDBG (0, "unhandled: %u", e->event_type);
3191 break;
3192 }
3193
3194 if (add_event)
3195 {
3196 events[*num_ev].data.u64 = session_evt_data;
3197 if (EPOLLONESHOT & session_events)
3198 {
Florin Corasb242d312020-10-26 15:35:40 -07003199 s = vcl_session_get (wrk, sid);
3200 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003201 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003202 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003203 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003204 s = vcl_session_get (wrk, sid);
3205 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3206 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003207 }
Florin Coras86f04502018-09-12 16:08:01 -07003208 *num_ev += 1;
3209 }
3210}
3211
3212static int
3213vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3214 struct epoll_event *events, u32 maxevents,
3215 double wait_for_time, u32 * num_ev)
3216{
Florin Coras99368312018-08-02 10:45:44 -07003217 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003218 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003219 int i;
3220
Florin Coras539663c2018-09-28 14:59:37 -07003221 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3222 goto handle_dequeued;
3223
Florin Coras54693d22018-07-17 10:46:29 -07003224 if (svm_msg_q_is_empty (mq))
3225 {
3226 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003227 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003228 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003229 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003230 else
3231 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003232 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003233 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003234 }
3235 }
Florin Corase003a1b2019-06-05 10:47:16 -07003236 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003237 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003238
Florin Coras539663c2018-09-28 14:59:37 -07003239handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003240 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003241 {
Florin Coras134a9962018-08-28 11:32:04 -07003242 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003243 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003244 if (*num_ev < maxevents)
3245 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3246 else
3247 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003248 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003249 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003250 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003251 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003252 return *num_ev;
3253}
3254
Florin Coras99368312018-08-02 10:45:44 -07003255static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003256vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3257 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003258{
Florin Corasccdb8b82021-04-28 13:01:06 -07003259 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003260
3261 if (!n_evts)
3262 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003263 if (timeout_ms > 0)
3264 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003265 }
3266
3267 do
3268 {
3269 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003270 timeout_ms, &n_evts);
3271 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003272 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003273 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003274 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003275
3276 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003277}
3278
3279static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003280vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3281 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003282{
Florin Coras99368312018-08-02 10:45:44 -07003283 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003284 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003285 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003286 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003287 u64 buf;
3288
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003289 if (PREDICT_FALSE (wrk->api_client_handle == ~0))
3290 {
3291 vcl_api_retry_attach (wrk);
3292 return n_evts;
3293 }
3294
Florin Coras134a9962018-08-28 11:32:04 -07003295 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003296 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003297 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003298 if (timeout_ms > 0)
3299 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003300 }
3301
Florin Corasb13ba132021-01-27 16:05:24 -08003302 do
3303 {
3304 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003305 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003306 if (n_mq_evts < 0)
3307 {
3308 VDBG (0, "epoll_wait error %u", errno);
3309 return n_evts;
3310 }
3311
3312 for (i = 0; i < n_mq_evts; i++)
3313 {
Filip Tehlar8ccc6b32022-02-02 17:38:20 +00003314 if (PREDICT_FALSE (wrk->mq_events[i].data.u32 == ~0))
3315 {
3316 /* api socket was closed */
3317 vcl_api_handle_disconnect (wrk);
3318 continue;
3319 }
3320
Florin Corasb13ba132021-01-27 16:05:24 -08003321 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3322 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3323 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3324 &n_evts);
3325 }
3326
Florin Corasccdb8b82021-04-28 13:01:06 -07003327 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003328 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003329 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003330 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003331
3332 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003333}
3334
Florin Corasfe286f72021-06-04 10:07:55 -07003335static void
Florin Corasfe286f72021-06-04 10:07:55 -07003336vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3337 int maxevents, u32 *n_evts)
3338{
Florin Coras734268f2021-06-30 07:54:29 -07003339 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003340 vcl_session_t *s;
3341 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003342 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003343
Florin Corasfa3884f2021-06-22 20:04:46 -07003344 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003345 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003346 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003347
Florin Corasfa3884f2021-06-22 20:04:46 -07003348 next = wrk->ep_lt_current;
3349 do
Florin Corasfe286f72021-06-04 10:07:55 -07003350 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003351 s = vcl_session_get (wrk, next);
3352 next = s->vep.lt_next;
3353
3354 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003355 {
3356 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003357 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003358 evt_data = s->vep.ev.data.u64;
3359 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003360 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003361 {
3362 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003363 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3364 evt_data = s->vep.ev.data.u64;
3365 }
3366 if (!add_event && s->session_state > VCL_STATE_READY)
3367 {
3368 add_event = 1;
3369 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003370 evt_data = s->vep.ev.data.u64;
3371 }
3372 if (add_event)
3373 {
3374 events[*n_evts].data.u64 = evt_data;
3375 *n_evts += 1;
3376 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003377 if (EPOLLONESHOT & s->vep.ev.events)
3378 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003379 if (*n_evts == maxevents)
3380 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003381 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003382 break;
3383 }
3384 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003385 else
3386 {
3387 vcl_epoll_lt_del (wrk, s);
3388 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3389 break;
3390 }
Florin Corasfe286f72021-06-04 10:07:55 -07003391 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003392 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003393}
3394
Dave Wallacef7f809c2017-10-03 01:48:42 -04003395int
Florin Coras134a9962018-08-28 11:32:04 -07003396vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003397 int maxevents, double wait_for_time)
3398{
Florin Coras134a9962018-08-28 11:32:04 -07003399 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003400 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003401 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003402 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003403
3404 if (PREDICT_FALSE (maxevents <= 0))
3405 {
Florin Coras5e062572019-03-14 19:07:51 -07003406 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003407 return VPPCOM_EINVAL;
3408 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003409
Florin Coras134a9962018-08-28 11:32:04 -07003410 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003411 if (!vep_session)
3412 return VPPCOM_EBADFD;
3413
Florin Coras6c3b2182020-10-19 18:36:48 -07003414 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003415 {
Florin Coras5e062572019-03-14 19:07:51 -07003416 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003417 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003418 }
Florin Coras54693d22018-07-17 10:46:29 -07003419
Florin Coras86f04502018-09-12 16:08:01 -07003420 if (vec_len (wrk->unhandled_evts_vector))
3421 {
3422 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3423 {
3424 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3425 events, &n_evts);
3426 if (n_evts == maxevents)
3427 {
Florin Corase003a1b2019-06-05 10:47:16 -07003428 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3429 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003430 }
3431 }
Florin Corase003a1b2019-06-05 10:47:16 -07003432 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003433 }
liuyacancba87102021-09-10 15:14:05 +08003434
3435 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3436 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3437
wanghanlin8919fec2021-03-18 20:00:41 +08003438 /* Request to only drain unhandled */
3439 if ((int) wait_for_time == -2)
3440 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003441
Florin Coras86f04502018-09-12 16:08:01 -07003442
Florin Corasfe286f72021-06-04 10:07:55 -07003443 if (vcm->cfg.use_mq_eventfd)
3444 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3445 wait_for_time);
3446 else
3447 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3448 wait_for_time);
3449
Florin Corasfe286f72021-06-04 10:07:55 -07003450 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003451}
3452
Dave Wallace35830af2017-10-09 01:43:42 -04003453int
Florin Coras134a9962018-08-28 11:32:04 -07003454vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003455 void *buffer, uint32_t * buflen)
3456{
Florin Coras134a9962018-08-28 11:32:04 -07003457 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003458 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003459 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003460 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003461 vcl_session_t *session;
3462 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003463
Florin Coras134a9962018-08-28 11:32:04 -07003464 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003465 if (!session)
3466 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003467
Dave Wallace35830af2017-10-09 01:43:42 -04003468 switch (op)
3469 {
3470 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003471 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3473 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003474 break;
3475
Dave Wallace227867f2017-11-13 21:21:53 -05003476 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003477 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003478 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3479 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003480 break;
3481
3482 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003483 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003484 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003485 *flags =
3486 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003487 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003488 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003489 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003490 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3491 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003492 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003493 }
3494 else
3495 rv = VPPCOM_EINVAL;
3496 break;
3497
3498 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003499 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003500 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003501 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003502 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003503 else
Florin Corasac422d62020-10-19 20:51:36 -07003504 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003505
Florin Coras5e062572019-03-14 19:07:51 -07003506 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3507 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003508 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003509 }
3510 else
3511 rv = VPPCOM_EINVAL;
3512 break;
3513
3514 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003515 if (PREDICT_TRUE (buffer && buflen &&
3516 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003517 {
Florin Coras7e12d942018-06-27 14:32:43 -07003518 ep->is_ip4 = session->transport.is_ip4;
3519 ep->port = session->transport.rmt_port;
3520 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003521 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3522 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003523 else
Dave Barach178cf492018-11-13 16:34:13 -05003524 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3525 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003526 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003527 VDBG (1,
3528 "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3529 "addr = %U, port %u",
3530 session_handle, ep->is_ip4, vcl_format_ip46_address,
3531 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003532 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3533 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003534 }
3535 else
3536 rv = VPPCOM_EINVAL;
3537 break;
3538
3539 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003540 if (PREDICT_TRUE (buffer && buflen &&
3541 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003542 {
Florin Coras7e12d942018-06-27 14:32:43 -07003543 ep->is_ip4 = session->transport.is_ip4;
3544 ep->port = session->transport.lcl_port;
3545 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003546 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3547 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003548 else
Dave Barach178cf492018-11-13 16:34:13 -05003549 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3550 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003551 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003552 VDBG (1,
3553 "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3554 " port %d",
3555 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003556 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003557 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3558 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003559 }
3560 else
3561 rv = VPPCOM_EINVAL;
3562 break;
Stevenb5a11602017-10-11 09:59:30 -07003563
Florin Corasef7cbf62019-10-17 09:56:27 -07003564 case VPPCOM_ATTR_SET_LCL_ADDR:
3565 if (PREDICT_TRUE (buffer && buflen &&
3566 (*buflen >= sizeof (*ep)) && ep->ip))
3567 {
3568 session->transport.is_ip4 = ep->is_ip4;
3569 session->transport.lcl_port = ep->port;
3570 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3571 *buflen = sizeof (*ep);
Florin Coras60687192021-11-29 21:00:47 -08003572 VDBG (1,
3573 "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3574 " port %d",
3575 session_handle, ep->is_ip4, vcl_format_ip46_address,
Florin Corasef7cbf62019-10-17 09:56:27 -07003576 &session->transport.lcl_ip,
3577 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3578 clib_net_to_host_u16 (ep->port));
3579 }
3580 else
3581 rv = VPPCOM_EINVAL;
3582 break;
3583
Dave Wallace048b1d62018-01-03 22:24:41 -05003584 case VPPCOM_ATTR_GET_LIBC_EPFD:
3585 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003586 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003587 break;
3588
3589 case VPPCOM_ATTR_SET_LIBC_EPFD:
3590 if (PREDICT_TRUE (buffer && buflen &&
3591 (*buflen == sizeof (session->libc_epfd))))
3592 {
3593 session->libc_epfd = *(int *) buffer;
3594 *buflen = sizeof (session->libc_epfd);
3595
Florin Coras5e062572019-03-14 19:07:51 -07003596 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3597 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 }
3599 else
3600 rv = VPPCOM_EINVAL;
3601 break;
3602
3603 case VPPCOM_ATTR_GET_PROTOCOL:
3604 if (buffer && buflen && (*buflen >= sizeof (int)))
3605 {
Florin Coras7e12d942018-06-27 14:32:43 -07003606 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003607 *buflen = sizeof (int);
3608
Florin Coras5e062572019-03-14 19:07:51 -07003609 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3610 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003611 }
3612 else
3613 rv = VPPCOM_EINVAL;
3614 break;
3615
3616 case VPPCOM_ATTR_GET_LISTEN:
3617 if (buffer && buflen && (*buflen >= sizeof (int)))
3618 {
Florin Corasac422d62020-10-19 20:51:36 -07003619 *(int *) buffer = vcl_session_has_attr (session,
3620 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003621 *buflen = sizeof (int);
3622
Florin Coras5e062572019-03-14 19:07:51 -07003623 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3624 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003625 }
3626 else
3627 rv = VPPCOM_EINVAL;
3628 break;
3629
3630 case VPPCOM_ATTR_GET_ERROR:
3631 if (buffer && buflen && (*buflen >= sizeof (int)))
3632 {
3633 *(int *) buffer = 0;
3634 *buflen = sizeof (int);
3635
Florin Coras5e062572019-03-14 19:07:51 -07003636 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3637 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003638 }
3639 else
3640 rv = VPPCOM_EINVAL;
3641 break;
3642
3643 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3644 if (buffer && buflen && (*buflen >= sizeof (u32)))
3645 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003646
3647 /* VPP-TBD */
3648 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003649 session->tx_fifo ?
3650 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003651 vcm->cfg.tx_fifo_size);
3652 *buflen = sizeof (u32);
3653
Florin Coras5e062572019-03-14 19:07:51 -07003654 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3655 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3656 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003657 }
3658 else
3659 rv = VPPCOM_EINVAL;
3660 break;
3661
Filip Tehlar2f09bfc2021-11-15 10:26:56 +00003662 case VPPCOM_ATTR_SET_DSCP:
3663 if (buffer && buflen && (*buflen >= sizeof (u8)))
3664 {
3665 session->dscp = *(u8 *) buffer;
3666
3667 VDBG (2, "VPPCOM_ATTR_SET_DSCP: %u (0x%x), buflen %d,",
3668 *(u8 *) buffer, *(u8 *) buffer, *buflen);
3669 }
3670 else
3671 rv = VPPCOM_EINVAL;
3672 break;
3673
Dave Wallace048b1d62018-01-03 22:24:41 -05003674 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3675 if (buffer && buflen && (*buflen == sizeof (u32)))
3676 {
3677 /* VPP-TBD */
3678 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003679 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3680 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3681 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003682 }
3683 else
3684 rv = VPPCOM_EINVAL;
3685 break;
3686
3687 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3688 if (buffer && buflen && (*buflen >= sizeof (u32)))
3689 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003690
3691 /* VPP-TBD */
3692 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003693 session->rx_fifo ?
3694 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003695 vcm->cfg.rx_fifo_size);
3696 *buflen = sizeof (u32);
3697
Florin Coras5e062572019-03-14 19:07:51 -07003698 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3699 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003700 }
3701 else
3702 rv = VPPCOM_EINVAL;
3703 break;
3704
3705 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3706 if (buffer && buflen && (*buflen == sizeof (u32)))
3707 {
3708 /* VPP-TBD */
3709 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003710 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3711 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3712 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003713 }
3714 else
3715 rv = VPPCOM_EINVAL;
3716 break;
3717
3718 case VPPCOM_ATTR_GET_REUSEADDR:
3719 if (buffer && buflen && (*buflen >= sizeof (int)))
3720 {
3721 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003722 *(int *) buffer = vcl_session_has_attr (session,
3723 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003724 *buflen = sizeof (int);
3725
Florin Coras5e062572019-03-14 19:07:51 -07003726 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3727 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003728 }
3729 else
3730 rv = VPPCOM_EINVAL;
3731 break;
3732
Stevenb5a11602017-10-11 09:59:30 -07003733 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003734 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003735 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003736 {
3737 /* VPP-TBD */
3738 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003739 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003740 else
Florin Corasac422d62020-10-19 20:51:36 -07003741 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003742
Florin Coras5e062572019-03-14 19:07:51 -07003743 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003744 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003745 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003746 }
3747 else
3748 rv = VPPCOM_EINVAL;
3749 break;
3750
3751 case VPPCOM_ATTR_GET_REUSEPORT:
3752 if (buffer && buflen && (*buflen >= sizeof (int)))
3753 {
3754 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003755 *(int *) buffer = vcl_session_has_attr (session,
3756 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003757 *buflen = sizeof (int);
3758
Florin Coras5e062572019-03-14 19:07:51 -07003759 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3760 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003761 }
3762 else
3763 rv = VPPCOM_EINVAL;
3764 break;
3765
3766 case VPPCOM_ATTR_SET_REUSEPORT:
3767 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003768 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003769 {
3770 /* VPP-TBD */
3771 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003772 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003773 else
Florin Corasac422d62020-10-19 20:51:36 -07003774 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003775
Florin Coras5e062572019-03-14 19:07:51 -07003776 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003777 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003778 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003779 }
3780 else
3781 rv = VPPCOM_EINVAL;
3782 break;
3783
3784 case VPPCOM_ATTR_GET_BROADCAST:
3785 if (buffer && buflen && (*buflen >= sizeof (int)))
3786 {
3787 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003788 *(int *) buffer = vcl_session_has_attr (session,
3789 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003790 *buflen = sizeof (int);
3791
Florin Coras5e062572019-03-14 19:07:51 -07003792 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3793 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003794 }
3795 else
3796 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003797 break;
3798
3799 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003800 if (buffer && buflen && (*buflen == sizeof (int)))
3801 {
3802 /* VPP-TBD */
3803 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003804 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003805 else
Florin Corasac422d62020-10-19 20:51:36 -07003806 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003807
Florin Coras5e062572019-03-14 19:07:51 -07003808 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003809 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003810 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 }
3812 else
3813 rv = VPPCOM_EINVAL;
3814 break;
3815
3816 case VPPCOM_ATTR_GET_V6ONLY:
3817 if (buffer && buflen && (*buflen >= sizeof (int)))
3818 {
3819 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003820 *(int *) buffer = vcl_session_has_attr (session,
3821 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003822 *buflen = sizeof (int);
3823
Florin Coras5e062572019-03-14 19:07:51 -07003824 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3825 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003826 }
3827 else
3828 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003829 break;
3830
3831 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 if (buffer && buflen && (*buflen == sizeof (int)))
3833 {
3834 /* VPP-TBD */
3835 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003836 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003837 else
Florin Corasac422d62020-10-19 20:51:36 -07003838 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003839
Florin Coras5e062572019-03-14 19:07:51 -07003840 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003841 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003842 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 }
3844 else
3845 rv = VPPCOM_EINVAL;
3846 break;
3847
3848 case VPPCOM_ATTR_GET_KEEPALIVE:
3849 if (buffer && buflen && (*buflen >= sizeof (int)))
3850 {
3851 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003852 *(int *) buffer = vcl_session_has_attr (session,
3853 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003854 *buflen = sizeof (int);
3855
Florin Coras5e062572019-03-14 19:07:51 -07003856 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3857 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003858 }
3859 else
3860 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003861 break;
Stevenbccd3392017-10-12 20:42:21 -07003862
3863 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003864 if (buffer && buflen && (*buflen == sizeof (int)))
3865 {
3866 /* VPP-TBD */
3867 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003868 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003869 else
Florin Corasac422d62020-10-19 20:51:36 -07003870 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003871
Florin Coras5e062572019-03-14 19:07:51 -07003872 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003873 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003874 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003875 }
3876 else
3877 rv = VPPCOM_EINVAL;
3878 break;
3879
3880 case VPPCOM_ATTR_GET_TCP_NODELAY:
3881 if (buffer && buflen && (*buflen >= sizeof (int)))
3882 {
3883 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003884 *(int *) buffer = vcl_session_has_attr (session,
3885 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003886 *buflen = sizeof (int);
3887
Florin Coras5e062572019-03-14 19:07:51 -07003888 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3889 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003890 }
3891 else
3892 rv = VPPCOM_EINVAL;
3893 break;
3894
3895 case VPPCOM_ATTR_SET_TCP_NODELAY:
3896 if (buffer && buflen && (*buflen == sizeof (int)))
3897 {
3898 /* VPP-TBD */
3899 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003900 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003901 else
Florin Corasac422d62020-10-19 20:51:36 -07003902 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003903
Florin Coras5e062572019-03-14 19:07:51 -07003904 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003905 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003906 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003907 }
3908 else
3909 rv = VPPCOM_EINVAL;
3910 break;
3911
3912 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3913 if (buffer && buflen && (*buflen >= sizeof (int)))
3914 {
3915 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003916 *(int *) buffer = vcl_session_has_attr (session,
3917 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003918 *buflen = sizeof (int);
3919
Florin Coras5e062572019-03-14 19:07:51 -07003920 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3921 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003922 }
3923 else
3924 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003925 break;
3926
3927 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003928 if (buffer && buflen && (*buflen == sizeof (int)))
3929 {
3930 /* VPP-TBD */
3931 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003932 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003933 else
Florin Corasac422d62020-10-19 20:51:36 -07003934 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003935
Florin Coras5e062572019-03-14 19:07:51 -07003936 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003937 vcl_session_has_attr (session,
3938 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003939 }
3940 else
3941 rv = VPPCOM_EINVAL;
3942 break;
3943
3944 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3945 if (buffer && buflen && (*buflen >= sizeof (int)))
3946 {
3947 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003948 *(int *) buffer = vcl_session_has_attr (session,
3949 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003950 *buflen = sizeof (int);
3951
Florin Coras5e062572019-03-14 19:07:51 -07003952 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3953 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003954 }
3955 else
3956 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003957 break;
3958
3959 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003960 if (buffer && buflen && (*buflen == sizeof (int)))
3961 {
3962 /* VPP-TBD */
3963 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003964 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003965 else
Florin Corasac422d62020-10-19 20:51:36 -07003966 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003967
Florin Coras5e062572019-03-14 19:07:51 -07003968 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003969 vcl_session_has_attr (session,
3970 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003971 }
3972 else
3973 rv = VPPCOM_EINVAL;
3974 break;
3975
3976 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003977 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003978 {
Florin Coras04ae8272021-04-12 19:55:37 -07003979 rv = VPPCOM_EINVAL;
3980 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003981 }
Florin Coras04ae8272021-04-12 19:55:37 -07003982
3983 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3984 tea.mss = *(u32 *) buffer;
3985 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3986 rv = VPPCOM_ENOPROTOOPT;
3987
3988 if (!rv)
3989 {
3990 *(u32 *) buffer = tea.mss;
3991 *buflen = sizeof (int);
3992 }
3993
3994 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3995 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003996 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003997 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003998 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003999 {
Florin Coras04ae8272021-04-12 19:55:37 -07004000 rv = VPPCOM_EINVAL;
4001 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05004002 }
Florin Coras04ae8272021-04-12 19:55:37 -07004003
4004 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
4005 tea.mss = *(u32 *) buffer;
4006 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
4007 rv = VPPCOM_ENOPROTOOPT;
4008
4009 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
4010 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07004011 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004012
Florin Coras1e966172020-05-16 18:18:14 +00004013 case VPPCOM_ATTR_SET_CONNECTED:
4014 session->flags |= VCL_SESSION_F_CONNECTED;
4015 break;
4016
Florin Corasa5a9efd2021-01-05 17:03:29 -08004017 case VPPCOM_ATTR_SET_CKPAIR:
4018 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
4019 !vcl_session_has_crypto (session))
4020 {
4021 rv = VPPCOM_EINVAL;
4022 break;
4023 }
Florin Corasa54b62d2021-04-21 09:05:56 -07004024 if (!session->ext_config)
4025 {
Florin Coras87f63892021-05-01 16:01:40 -07004026 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
4027 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07004028 }
4029 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
4030 {
4031 rv = VPPCOM_EINVAL;
4032 break;
4033 }
4034
4035 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08004036 break;
4037
Florin Coras6a6555a2021-01-28 11:39:27 -08004038 case VPPCOM_ATTR_SET_VRF:
4039 if (!(buffer && buflen && (*buflen == sizeof (u32))))
4040 {
4041 rv = VPPCOM_EINVAL;
4042 break;
4043 }
4044 session->vrf = *(u32 *) buffer;
4045 break;
4046
4047 case VPPCOM_ATTR_GET_VRF:
4048 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
4049 {
4050 rv = VPPCOM_EINVAL;
4051 break;
4052 }
4053 *(u32 *) buffer = session->vrf;
4054 *buflen = sizeof (u32);
4055 break;
4056
wanghanlin0674f852021-02-22 10:38:36 +08004057 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08004058 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08004059 {
Florin Corasd77325c2021-02-23 12:03:03 -08004060 rv = VPPCOM_EINVAL;
4061 break;
wanghanlin0674f852021-02-22 10:38:36 +08004062 }
Florin Corasd77325c2021-02-23 12:03:03 -08004063
4064 if (session->transport.is_ip4)
4065 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08004066 else
Florin Corasd77325c2021-02-23 12:03:03 -08004067 *(int *) buffer = AF_INET6;
4068 *buflen = sizeof (int);
4069
wanghanlin0674f852021-02-22 10:38:36 +08004070 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
4071 *buflen);
4072 break;
4073
Florin Coras87f63892021-05-01 16:01:40 -07004074 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
4075 if (!(buffer && buflen && (*buflen > 0)))
4076 {
4077 rv = VPPCOM_EINVAL;
4078 break;
4079 }
4080 if (session->ext_config)
4081 {
4082 rv = VPPCOM_EINVAL;
4083 break;
4084 }
4085 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4086 *buflen + sizeof (u32));
4087 clib_memcpy (session->ext_config->data, buffer, *buflen);
4088 session->ext_config->len = *buflen;
4089 break;
Florin Coraseff5f7a2023-02-07 17:36:17 -08004090 case VPPCOM_ATTR_SET_IP_PKTINFO:
4091 if (buffer && buflen && (*buflen == sizeof (int)) &&
4092 !vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO))
4093 {
4094 if (*(int *) buffer)
4095 vcl_session_set_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4096 else
4097 vcl_session_clear_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4098
4099 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d",
4100 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO),
4101 *buflen);
4102 }
4103 else
4104 rv = VPPCOM_EINVAL;
4105 break;
4106
4107 case VPPCOM_ATTR_GET_IP_PKTINFO:
4108 if (buffer && buflen && (*buflen >= sizeof (int)))
4109 {
4110 *(int *) buffer =
4111 vcl_session_has_attr (session, VCL_SESS_ATTR_IP_PKTINFO);
4112 *buflen = sizeof (int);
4113
4114 VDBG (2, "VCL_SESS_ATTR_IP_PKTINFO: %d, buflen %d", *(int *) buffer,
4115 *buflen);
4116 }
4117 else
4118 rv = VPPCOM_EINVAL;
4119 break;
Florin Coras87f63892021-05-01 16:01:40 -07004120
Dave Wallacee22aa742017-10-20 12:30:38 -04004121 default:
4122 rv = VPPCOM_EINVAL;
4123 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004124 }
4125
Dave Wallace35830af2017-10-09 01:43:42 -04004126 return rv;
4127}
4128
Stevenac1f96d2017-10-24 16:03:58 -07004129int
Florin Coras134a9962018-08-28 11:32:04 -07004130vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004131 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4132{
Florin Coras134a9962018-08-28 11:32:04 -07004133 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004134 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004135 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004136
4137 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004138 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004139 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004140 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004141 else
4142 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004143 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004144 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004145 }
4146
Florin Coras0a1e1832020-03-29 18:54:04 +00004147 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004148 {
Florin Coras5da10c42020-04-01 04:31:21 +00004149 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004150 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004151 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4152 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004153 else
Dave Barach178cf492018-11-13 16:34:13 -05004154 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4155 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004156 ep->is_ip4 = session->transport.is_ip4;
4157 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004158 }
Florin Coras460dce62018-07-27 05:45:06 -07004159
Stevenac1f96d2017-10-24 16:03:58 -07004160 return rv;
4161}
4162
Florin Coraseff5f7a2023-02-07 17:36:17 -08004163static void
4164vcl_handle_ep_app_tlvs (vcl_session_t *s, vppcom_endpt_t *ep)
4165{
4166 vppcom_endpt_tlv_t *tlv = ep->app_tlvs;
4167
4168 do
4169 {
4170 switch (tlv->data_type)
4171 {
4172 case VCL_UDP_SEGMENT:
4173 s->gso_size = *(u16 *) tlv->data;
4174 break;
4175 case VCL_IP_PKTINFO:
4176 clib_memcpy_fast (&s->transport.lcl_ip, (ip4_address_t *) tlv->data,
4177 sizeof (ip4_address_t));
4178 break;
4179 default:
4180 VDBG (0, "Ignorning unsupported app tlv %u", tlv->data_type);
4181 break;
4182 }
4183 tlv = VCL_EP_NEXT_APP_TLV (ep, tlv);
4184 }
4185 while (tlv);
4186}
4187
Stevenac1f96d2017-10-24 16:03:58 -07004188int
Florin Coras134a9962018-08-28 11:32:04 -07004189vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004190 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4191{
Florin Coras7a2abce2020-04-05 19:25:44 +00004192 vcl_worker_t *wrk = vcl_worker_get_current ();
4193 vcl_session_t *s;
4194
4195 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004196 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004197 return VPPCOM_EBADFD;
4198
Dave Wallace617dffa2017-10-26 14:47:06 -04004199 if (ep)
4200 {
Florin Coras5824cc52020-10-20 18:44:41 -07004201 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004202 return VPPCOM_EINVAL;
4203
liuyacanbc0c7542021-08-02 20:15:05 +08004204 s->transport.is_ip4 = ep->is_ip4;
4205 s->transport.rmt_port = ep->port;
4206 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4207
Florin Coraseff5f7a2023-02-07 17:36:17 -08004208 if (ep->app_tlvs)
4209 vcl_handle_ep_app_tlvs (s, ep);
Dou Chao243a0432022-11-29 19:41:34 +08004210
Florin Coras5da10c42020-04-01 04:31:21 +00004211 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004212 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004213 {
Florin Coras5824cc52020-10-20 18:44:41 -07004214 u32 session_index = s->session_index;
4215 f64 timeout = vcm->cfg.session_timeout;
4216 int rv;
4217
Florin Coras5da10c42020-04-01 04:31:21 +00004218 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004219 rv = vppcom_wait_for_session_state_change (session_index,
4220 VCL_STATE_READY,
4221 timeout);
4222 if (rv < 0)
4223 return rv;
4224 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004225 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004226 }
4227
4228 if (flags)
4229 {
4230 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004231 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004232 }
4233
Florin Coraseff5f7a2023-02-07 17:36:17 -08004234 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
Florin Coras7a2abce2020-04-05 19:25:44 +00004235 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004236}
4237
Dave Wallace048b1d62018-01-03 22:24:41 -05004238int
4239vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4240{
Florin Coras134a9962018-08-28 11:32:04 -07004241 vcl_worker_t *wrk = vcl_worker_get_current ();
4242 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004243 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004244 svm_msg_q_msg_t msg;
4245 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004246 int rv, num_ev = 0;
4247
Florin Coras5e062572019-03-14 19:07:51 -07004248 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004249
4250 if (!vp)
4251 return VPPCOM_EFAULT;
4252
4253 do
4254 {
Florin Coras7e12d942018-06-27 14:32:43 -07004255 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004256
Florin Coras6917b942018-11-13 22:44:54 -08004257 /* Dequeue all events and drop all unhandled io events */
4258 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4259 {
4260 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4261 vcl_handle_mq_event (wrk, e);
4262 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4263 }
4264 vec_reset_length (wrk->unhandled_evts_vector);
4265
Dave Wallace048b1d62018-01-03 22:24:41 -05004266 for (i = 0; i < n_sids; i++)
4267 {
Florin Coras7baeb712019-01-04 17:05:43 -08004268 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004269 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004270 {
4271 vp[i].revents = POLLHUP;
4272 num_ev++;
4273 continue;
4274 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004275
Florin Coras6917b942018-11-13 22:44:54 -08004276 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004277
4278 if (POLLIN & vp[i].events)
4279 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004280 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004281 if (rv > 0)
4282 {
Florin Coras6917b942018-11-13 22:44:54 -08004283 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004284 num_ev++;
4285 }
4286 else if (rv < 0)
4287 {
4288 switch (rv)
4289 {
4290 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004291 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004292 break;
4293
4294 default:
Florin Coras6917b942018-11-13 22:44:54 -08004295 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004296 break;
4297 }
4298 num_ev++;
4299 }
4300 }
4301
4302 if (POLLOUT & vp[i].events)
4303 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004304 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004305 if (rv > 0)
4306 {
Florin Coras6917b942018-11-13 22:44:54 -08004307 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004308 num_ev++;
4309 }
4310 else if (rv < 0)
4311 {
4312 switch (rv)
4313 {
4314 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004315 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004316 break;
4317
4318 default:
Florin Coras6917b942018-11-13 22:44:54 -08004319 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004320 break;
4321 }
4322 num_ev++;
4323 }
4324 }
4325
Dave Wallace7e607a72018-06-18 18:41:32 -04004326 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004327 {
Florin Coras6917b942018-11-13 22:44:54 -08004328 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004329 num_ev++;
4330 }
4331 }
4332 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004333 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004334 }
4335 while ((num_ev == 0) && keep_trying);
4336
Dave Wallace048b1d62018-01-03 22:24:41 -05004337 return num_ev;
4338}
4339
Florin Coras99368312018-08-02 10:45:44 -07004340int
4341vppcom_mq_epoll_fd (void)
4342{
Florin Coras134a9962018-08-28 11:32:04 -07004343 vcl_worker_t *wrk = vcl_worker_get_current ();
4344 return wrk->mqs_epfd;
4345}
4346
4347int
Florin Coras30e79c22019-01-02 19:31:22 -08004348vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004349{
4350 return session_handle & 0xFFFFFF;
4351}
4352
4353int
Florin Coras30e79c22019-01-02 19:31:22 -08004354vppcom_session_worker (vcl_session_handle_t session_handle)
4355{
4356 return session_handle >> 24;
4357}
4358
4359int
Florin Coras134a9962018-08-28 11:32:04 -07004360vppcom_worker_register (void)
4361{
Florin Coras47c40e22018-11-26 17:01:36 -08004362 if (!vcl_worker_alloc_and_init ())
4363 return VPPCOM_EEXIST;
4364
Florin Coras47c40e22018-11-26 17:01:36 -08004365 if (vcl_worker_register_with_vpp ())
4366 return VPPCOM_EEXIST;
4367
4368 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004369}
4370
Florin Coras369db832019-07-08 12:34:45 -07004371void
4372vppcom_worker_unregister (void)
4373{
4374 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4375 vcl_set_worker_index (~0);
4376}
4377
Pivo6017ff02020-05-04 17:57:33 +02004378void
4379vppcom_worker_index_set (int index)
4380{
4381 vcl_set_worker_index (index);
4382}
4383
Florin Corasdfe4cf42018-11-28 22:13:45 -08004384int
4385vppcom_worker_index (void)
4386{
4387 return vcl_get_worker_index ();
4388}
4389
Florin Corase0982e52019-01-25 13:19:56 -08004390int
4391vppcom_worker_mqs_epfd (void)
4392{
4393 vcl_worker_t *wrk = vcl_worker_get_current ();
4394 if (!vcm->cfg.use_mq_eventfd)
4395 return -1;
4396 return wrk->mqs_epfd;
4397}
4398
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004399int
4400vppcom_session_is_connectable_listener (uint32_t session_handle)
4401{
4402 vcl_session_t *session;
4403 vcl_worker_t *wrk = vcl_worker_get_current ();
4404 session = vcl_session_get_w_handle (wrk, session_handle);
4405 if (!session)
4406 return VPPCOM_EBADFD;
4407 return vcl_session_is_connectable_listener (wrk, session);
4408}
4409
4410int
4411vppcom_session_listener (uint32_t session_handle)
4412{
4413 vcl_worker_t *wrk = vcl_worker_get_current ();
4414 vcl_session_t *listen_session, *session;
4415 session = vcl_session_get_w_handle (wrk, session_handle);
4416 if (!session)
4417 return VPPCOM_EBADFD;
4418 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4419 return VPPCOM_EBADFD;
4420 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4421 if (!listen_session)
4422 return VPPCOM_EBADFD;
4423 return vcl_session_handle (listen_session);
4424}
4425
4426int
4427vppcom_session_n_accepted (uint32_t session_handle)
4428{
4429 vcl_worker_t *wrk = vcl_worker_get_current ();
4430 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4431 if (!session)
4432 return VPPCOM_EBADFD;
4433 return session->n_accepted_sessions;
4434}
4435
Florin Coras66ec4672020-06-15 07:59:40 -07004436const char *
4437vppcom_proto_str (vppcom_proto_t proto)
4438{
4439 char const *proto_str;
4440
4441 switch (proto)
4442 {
4443 case VPPCOM_PROTO_TCP:
4444 proto_str = "TCP";
4445 break;
4446 case VPPCOM_PROTO_UDP:
4447 proto_str = "UDP";
4448 break;
4449 case VPPCOM_PROTO_TLS:
4450 proto_str = "TLS";
4451 break;
4452 case VPPCOM_PROTO_QUIC:
4453 proto_str = "QUIC";
4454 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004455 case VPPCOM_PROTO_DTLS:
4456 proto_str = "DTLS";
4457 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004458 case VPPCOM_PROTO_SRTP:
4459 proto_str = "SRTP";
4460 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004461 default:
4462 proto_str = "UNKNOWN";
4463 break;
4464 }
4465 return proto_str;
4466}
4467
4468const char *
4469vppcom_retval_str (int retval)
4470{
4471 char const *st;
4472
4473 switch (retval)
4474 {
4475 case VPPCOM_OK:
4476 st = "VPPCOM_OK";
4477 break;
4478
4479 case VPPCOM_EAGAIN:
4480 st = "VPPCOM_EAGAIN";
4481 break;
4482
4483 case VPPCOM_EFAULT:
4484 st = "VPPCOM_EFAULT";
4485 break;
4486
4487 case VPPCOM_ENOMEM:
4488 st = "VPPCOM_ENOMEM";
4489 break;
4490
4491 case VPPCOM_EINVAL:
4492 st = "VPPCOM_EINVAL";
4493 break;
4494
4495 case VPPCOM_EBADFD:
4496 st = "VPPCOM_EBADFD";
4497 break;
4498
4499 case VPPCOM_EAFNOSUPPORT:
4500 st = "VPPCOM_EAFNOSUPPORT";
4501 break;
4502
4503 case VPPCOM_ECONNABORTED:
4504 st = "VPPCOM_ECONNABORTED";
4505 break;
4506
4507 case VPPCOM_ECONNRESET:
4508 st = "VPPCOM_ECONNRESET";
4509 break;
4510
4511 case VPPCOM_ENOTCONN:
4512 st = "VPPCOM_ENOTCONN";
4513 break;
4514
4515 case VPPCOM_ECONNREFUSED:
4516 st = "VPPCOM_ECONNREFUSED";
4517 break;
4518
4519 case VPPCOM_ETIMEDOUT:
4520 st = "VPPCOM_ETIMEDOUT";
4521 break;
4522
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304523 case VPPCOM_EADDRINUSE:
4524 st = "VPPCOM_EADDRINUSE";
4525 break;
4526
Florin Coras66ec4672020-06-15 07:59:40 -07004527 default:
4528 st = "UNKNOWN_STATE";
4529 break;
4530 }
4531
4532 return st;
4533}
4534
Florin Corasa5a9efd2021-01-05 17:03:29 -08004535int
4536vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4537{
4538 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004539 return vcl_sapi_add_cert_key_pair (ckpair);
4540 else
4541 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004542}
4543
4544int
4545vppcom_del_cert_key_pair (uint32_t ckpair_index)
4546{
4547 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004548 return vcl_sapi_del_cert_key_pair (ckpair_index);
4549 else
4550 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004551}
4552
Radha krishna Saragadamdd92bde2022-07-18 19:41:05 +05304553int
4554vppcom_session_get_error (uint32_t session_handle)
4555{
4556 vcl_worker_t *wrk = vcl_worker_get_current ();
4557 vcl_session_t *session = 0;
4558
4559 session = vcl_session_get_w_handle (wrk, session_handle);
4560 if (!session)
4561 return VPPCOM_EBADFD;
4562
4563 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
4564 {
4565 VWRN ("epoll session %u! will not have connect", session->session_index);
4566 return VPPCOM_EBADFD;
4567 }
4568
4569 if (session->vpp_error == SESSION_E_PORTINUSE)
4570 return VPPCOM_EADDRINUSE;
4571 else if (session->vpp_error == SESSION_E_REFUSED)
4572 return VPPCOM_ECONNREFUSED;
4573 else if (session->vpp_error != SESSION_E_NONE)
4574 return VPPCOM_EFAULT;
4575 else
4576 return VPPCOM_OK;
4577}
4578
Maros Ondrejicka6ff8e902022-10-06 18:17:05 +02004579int
4580vppcom_worker_is_detached (void)
4581{
4582 vcl_worker_t *wrk = vcl_worker_get_current ();
4583
4584 if (!vcm->cfg.use_mq_eventfd)
4585 return VPPCOM_ENOTSUP;
4586
4587 return wrk->api_client_handle == ~0;
4588}
4589
Dave Wallacee22aa742017-10-20 12:30:38 -04004590/*
4591 * fd.io coding-style-patch-verification: ON
4592 *
4593 * Local Variables:
4594 * eval: (c-set-style "gnu")
4595 * End:
4596 */