blob: 3847bf224a7ecd37f10a3e66c24ecf1b58c281ff [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
Florin Coras697faea2018-06-27 17:10:49 -070040const char *
Florin Coras288eaab2019-02-03 15:26:14 -080041vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040042{
43 char *st;
44
45 switch (state)
46 {
Florin Corasc127d5a2020-10-14 16:35:58 -070047 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000048 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040049 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070050 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040051 st = "STATE_LISTEN";
52 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070053 case VCL_STATE_READY:
54 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040055 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070056 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080057 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050058 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070059 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040060 st = "STATE_DISCONNECT";
61 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070062 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000063 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040064 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070065 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080066 st = "STATE_UPDATED";
67 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070068 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080069 st = "STATE_LISTEN_NO_MQ";
70 break;
Dave Wallace543852a2017-08-03 02:11:34 -040071 default:
72 st = "UNKNOWN_STATE";
73 break;
74 }
75
76 return st;
77}
78
Dave Wallace543852a2017-08-03 02:11:34 -040079u8 *
80format_ip4_address (u8 * s, va_list * args)
81{
82 u8 *a = va_arg (*args, u8 *);
83 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
84}
85
86u8 *
87format_ip6_address (u8 * s, va_list * args)
88{
89 ip6_address_t *a = va_arg (*args, ip6_address_t *);
90 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
91
92 i_max_n_zero = ARRAY_LEN (a->as_u16);
93 max_n_zeros = 0;
94 i_first_zero = i_max_n_zero;
95 n_zeros = 0;
96 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
97 {
98 u32 is_zero = a->as_u16[i] == 0;
99 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
100 {
101 i_first_zero = i;
102 n_zeros = 0;
103 }
104 n_zeros += is_zero;
105 if ((!is_zero && n_zeros > max_n_zeros)
106 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
107 {
108 i_max_n_zero = i_first_zero;
109 max_n_zeros = n_zeros;
110 i_first_zero = ARRAY_LEN (a->as_u16);
111 n_zeros = 0;
112 }
113 }
114
115 last_double_colon = 0;
116 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
117 {
118 if (i == i_max_n_zero && max_n_zeros > 1)
119 {
120 s = format (s, "::");
121 i += max_n_zeros - 1;
122 last_double_colon = 1;
123 }
124 else
125 {
126 s = format (s, "%s%x",
127 (last_double_colon || i == 0) ? "" : ":",
128 clib_net_to_host_u16 (a->as_u16[i]));
129 last_double_colon = 0;
130 }
131 }
132
133 return s;
134}
135
136/* Format an IP46 address. */
137u8 *
138format_ip46_address (u8 * s, va_list * args)
139{
140 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
141 ip46_type_t type = va_arg (*args, ip46_type_t);
142 int is_ip4 = 1;
143
144 switch (type)
145 {
146 case IP46_TYPE_ANY:
147 is_ip4 = ip46_address_is_ip4 (ip46);
148 break;
149 case IP46_TYPE_IP4:
150 is_ip4 = 1;
151 break;
152 case IP46_TYPE_IP6:
153 is_ip4 = 0;
154 break;
155 }
156
157 return is_ip4 ?
158 format (s, "%U", format_ip4_address, &ip46->ip4) :
159 format (s, "%U", format_ip6_address, &ip46->ip6);
160}
161
Florin Coras697faea2018-06-27 17:10:49 -0700162/*
163 * VPPCOM Utility Functions
164 */
165
Florin Coras458089b2019-08-21 16:20:44 -0700166static void
Florin Coras4ac25842021-04-19 17:34:54 -0700167vcl_msg_add_ext_config (vcl_session_t *s, uword *offset)
168{
169 svm_fifo_chunk_t *c;
170
171 c = vcl_segment_alloc_chunk (vcl_vpp_worker_segment_handle (0),
172 0 /* one slice only */, s->ext_config->len,
173 offset);
174 if (c)
175 clib_memcpy_fast (c->data, s->ext_config, s->ext_config->len);
176}
177
178static void
Florin Coras458089b2019-08-21 16:20:44 -0700179vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
180{
181 app_session_evt_t _app_evt, *app_evt = &_app_evt;
182 session_listen_msg_t *mp;
183 svm_msg_q_t *mq;
184
185 mq = vcl_worker_ctrl_mq (wrk);
186 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
187 mp = (session_listen_msg_t *) app_evt->evt->data;
188 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700189 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700190 mp->context = s->session_index;
191 mp->wrk_index = wrk->vpp_wrk_index;
192 mp->is_ip4 = s->transport.is_ip4;
193 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
194 mp->port = s->transport.lcl_port;
195 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800196 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +0000197 if (s->flags & VCL_SESSION_F_CONNECTED)
198 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700199 if (s->ext_config)
200 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700201 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700202 if (s->ext_config)
203 {
204 clib_mem_free (s->ext_config);
205 s->ext_config = 0;
206 }
Florin Coras458089b2019-08-21 16:20:44 -0700207}
208
209static void
210vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
211{
212 app_session_evt_t _app_evt, *app_evt = &_app_evt;
213 session_connect_msg_t *mp;
214 svm_msg_q_t *mq;
215
216 mq = vcl_worker_ctrl_mq (wrk);
217 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
218 mp = (session_connect_msg_t *) app_evt->evt->data;
219 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700220 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700221 mp->context = s->session_index;
222 mp->wrk_index = wrk->vpp_wrk_index;
223 mp->is_ip4 = s->transport.is_ip4;
224 mp->parent_handle = s->parent_handle;
225 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700226 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700227 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000228 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700229 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800230 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000231 if (s->flags & VCL_SESSION_F_CONNECTED)
232 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700233 if (s->ext_config)
234 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700235 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700236
237 if (s->ext_config)
238 {
239 clib_mem_free (s->ext_config);
240 s->ext_config = 0;
241 }
Florin Coras458089b2019-08-21 16:20:44 -0700242}
243
244void
245vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
246{
247 app_session_evt_t _app_evt, *app_evt = &_app_evt;
248 session_unlisten_msg_t *mp;
249 svm_msg_q_t *mq;
250
251 mq = vcl_worker_ctrl_mq (wrk);
252 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
253 mp = (session_unlisten_msg_t *) app_evt->evt->data;
254 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700255 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700256 mp->wrk_index = wrk->vpp_wrk_index;
257 mp->handle = s->vpp_handle;
258 mp->context = wrk->wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
260}
261
262static void
liuyacan534468e2021-05-09 03:50:40 +0000263vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
264{
265 app_session_evt_t _app_evt, *app_evt = &_app_evt;
266 session_shutdown_msg_t *mp;
267 svm_msg_q_t *mq;
268
269 /* Send to thread that owns the session */
270 mq = s->vpp_evt_q;
271 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
272 mp = (session_shutdown_msg_t *) app_evt->evt->data;
273 memset (mp, 0, sizeof (*mp));
274 mp->client_index = wrk->api_client_handle;
275 mp->handle = s->vpp_handle;
276 app_send_ctrl_evt_to_vpp (mq, app_evt);
277}
278
279static void
Florin Coras458089b2019-08-21 16:20:44 -0700280vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
281{
282 app_session_evt_t _app_evt, *app_evt = &_app_evt;
283 session_disconnect_msg_t *mp;
284 svm_msg_q_t *mq;
285
286 /* Send to thread that owns the session */
287 mq = s->vpp_evt_q;
288 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
289 mp = (session_disconnect_msg_t *) app_evt->evt->data;
290 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700291 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700292 mp->handle = s->vpp_handle;
293 app_send_ctrl_evt_to_vpp (mq, app_evt);
294}
295
296static void
297vcl_send_app_detach (vcl_worker_t * wrk)
298{
299 app_session_evt_t _app_evt, *app_evt = &_app_evt;
300 session_app_detach_msg_t *mp;
301 svm_msg_q_t *mq;
302
303 mq = vcl_worker_ctrl_mq (wrk);
304 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
305 mp = (session_app_detach_msg_t *) app_evt->evt->data;
306 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700307 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700308 app_send_ctrl_evt_to_vpp (mq, app_evt);
309}
Florin Coras697faea2018-06-27 17:10:49 -0700310
Florin Coras54693d22018-07-17 10:46:29 -0700311static void
312vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
313 session_handle_t handle, int retval)
314{
315 app_session_evt_t _app_evt, *app_evt = &_app_evt;
316 session_accepted_reply_msg_t *rmp;
317 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
318 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
319 rmp->handle = handle;
320 rmp->context = context;
321 rmp->retval = retval;
322 app_send_ctrl_evt_to_vpp (mq, app_evt);
323}
324
Florin Coras99368312018-08-02 10:45:44 -0700325static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800326vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
327 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700328{
329 app_session_evt_t _app_evt, *app_evt = &_app_evt;
330 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800331 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700332 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
333 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800334 rmp->handle = s->vpp_handle;
335 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700336 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800337 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700338}
339
Florin Corasc9fbd662018-08-24 12:59:56 -0700340static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800341vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
342 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700343{
344 app_session_evt_t _app_evt, *app_evt = &_app_evt;
345 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800346 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
347 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700348 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800349 rmp->handle = s->vpp_handle;
350 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700351 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800352 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700353}
354
Florin Coras30e79c22019-01-02 19:31:22 -0800355void
356vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
357 u32 wrk_index)
358{
359 app_session_evt_t _app_evt, *app_evt = &_app_evt;
360 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800361
Florin Coras52dd29f2020-11-18 19:02:17 -0800362 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
363 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800364 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700365 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800366 mp->handle = s->vpp_handle;
367 mp->req_wrk_index = wrk->vpp_wrk_index;
368 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800369 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800370}
371
hanlina3a48962020-07-13 11:09:15 +0800372int
Florin Coras40c07ce2020-07-16 20:46:17 -0700373vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
374{
375 app_session_evt_t _app_evt, *app_evt = &_app_evt;
376 session_app_wrk_rpc_msg_t *mp;
377 vcl_worker_t *dst_wrk, *wrk;
378 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800379 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700380
381 if (data_len > sizeof (mp->data))
382 goto done;
383
384 clib_spinlock_lock (&vcm->workers_lock);
385
386 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
387 if (!dst_wrk)
388 goto done;
389
390 wrk = vcl_worker_get_current ();
391 mq = vcl_worker_ctrl_mq (wrk);
392 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
393 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700394 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700395 mp->wrk_index = dst_wrk->vpp_wrk_index;
396 clib_memcpy (mp->data, data, data_len);
397 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800398 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700399
400done:
401 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800402 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700403}
404
Florin Coras04ae8272021-04-12 19:55:37 -0700405int
406vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
407 transport_endpt_attr_t *attr)
408{
409 app_session_evt_t _app_evt, *app_evt = &_app_evt;
410 session_transport_attr_msg_t *mp;
411 svm_msg_q_t *mq;
412 f64 timeout;
413
414 ASSERT (!wrk->session_attr_op);
415 wrk->session_attr_op = 1;
416 wrk->session_attr_op_rv = -1;
417
418 mq = s->vpp_evt_q;
419 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
420 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
421 memset (mp, 0, sizeof (*mp));
422 mp->client_index = wrk->api_client_handle;
423 mp->handle = s->vpp_handle;
424 mp->is_get = is_get;
425 mp->attr = *attr;
426 app_send_ctrl_evt_to_vpp (mq, app_evt);
427
428 timeout = clib_time_now (&wrk->clib_time) + 1;
429
430 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
431 vcl_flush_mq_events ();
432
433 if (!wrk->session_attr_op_rv && is_get)
434 *attr = wrk->session_attr_rv;
435
436 wrk->session_attr_op = 0;
437
438 return wrk->session_attr_op_rv;
439}
440
Florin Coras54693d22018-07-17 10:46:29 -0700441static u32
Florin Coras00cca802019-06-06 09:38:44 -0700442vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
443 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700444{
445 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700446 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700447
Florin Coras134a9962018-08-28 11:32:04 -0700448 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700449
Florin Coras00cca802019-06-06 09:38:44 -0700450 listen_session = vcl_session_get (wrk, ls_index);
451 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700452 {
Florin Coras00cca802019-06-06 09:38:44 -0700453 VDBG (0, "ERROR: listener handle %lu does not match session %u",
454 mp->listener_handle, ls_index);
455 goto error;
456 }
457
Florin Corasc547e912020-12-08 17:50:45 -0800458 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800459 mp->server_tx_fifo,
460 mp->vpp_event_queue_address, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700461 {
Florin Corasc547e912020-12-08 17:50:45 -0800462 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700463 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700464 }
465
Florin Coras54693d22018-07-17 10:46:29 -0700466 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700467 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700468 session->transport.rmt_port = mp->rmt.port;
469 session->transport.is_ip4 = mp->rmt.is_ip4;
470 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500471 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700472
Florin Coras134a9962018-08-28 11:32:04 -0700473 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800474 session->transport.lcl_port = mp->lcl.port;
475 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700476 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200477 session->is_dgram = vcl_proto_is_dgram (session->session_type);
478 session->listener_index = listen_session->session_index;
479 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700480
Florin Coras5e062572019-03-14 19:07:51 -0700481 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
482 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700483 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
484 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
485 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700486 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
487
Florin Coras00cca802019-06-06 09:38:44 -0700488 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
489 session->vpp_handle, 0);
490
Florin Coras134a9962018-08-28 11:32:04 -0700491 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700492
493error:
Florin Corasb4624182020-12-11 13:58:12 -0800494 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
495 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700496 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
497 VNET_API_ERROR_INVALID_ARGUMENT);
498 vcl_session_free (wrk, session);
499 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700500}
501
502static u32
Florin Coras134a9962018-08-28 11:32:04 -0700503vcl_session_connected_handler (vcl_worker_t * wrk,
504 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700505{
Florin Coras99368312018-08-02 10:45:44 -0700506 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800507 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700508
509 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700510 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700511 if (!session)
512 {
Florin Coras5e062572019-03-14 19:07:51 -0700513 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
514 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700515 return VCL_INVALID_SESSION_INDEX;
516 }
Florin Coras54693d22018-07-17 10:46:29 -0700517 if (mp->retval)
518 {
Florin Coras5e062572019-03-14 19:07:51 -0700519 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700520 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700521 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700522 session->vpp_handle = mp->handle;
523 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700524 }
525
Florin Corasdbc9c592019-09-25 16:37:43 -0700526 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800527
528 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800529 mp->server_tx_fifo,
530 mp->vpp_event_queue_address, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800531 {
Florin Corasc547e912020-12-08 17:50:45 -0800532 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700533 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700534 vcl_send_session_disconnect (wrk, session);
535 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800536 }
537
Florin Coras653e43f2019-03-04 10:56:23 -0800538 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700539 {
Florin Corasc547e912020-12-08 17:50:45 -0800540 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800541 mp->ct_tx_fifo, (uword) ~0, 1, session))
Florin Coras653e43f2019-03-04 10:56:23 -0800542 {
Florin Corasc547e912020-12-08 17:50:45 -0800543 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700544 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700545 vcl_send_session_disconnect (wrk, session);
546 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800547 }
Florin Coras99368312018-08-02 10:45:44 -0700548 }
Florin Coras54693d22018-07-17 10:46:29 -0700549
Florin Coras09d18c22019-04-24 11:10:02 -0700550 session->transport.is_ip4 = mp->lcl.is_ip4;
551 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500552 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700553 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700554
555 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700556 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700557 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700558 vcl_send_session_disconnect (wrk, session);
559 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700560 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700561
562 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800563 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700564
Florin Coras5e062572019-03-14 19:07:51 -0700565 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
566 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700567 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700568
Florin Coras54693d22018-07-17 10:46:29 -0700569 return session_index;
570}
571
Florin Coras3c7d4f92018-12-14 11:28:43 -0800572static int
573vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
574{
575 vcl_session_msg_t *accepted_msg;
576 int i;
577
578 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
579 {
580 accepted_msg = &session->accept_evts_fifo[i];
581 if (accepted_msg->accepted_msg.handle == handle)
582 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800583 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800584 return 1;
585 }
586 }
587 return 0;
588}
589
Florin Corasc9fbd662018-08-24 12:59:56 -0700590static u32
Florin Coras134a9962018-08-28 11:32:04 -0700591vcl_session_reset_handler (vcl_worker_t * wrk,
592 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700593{
594 vcl_session_t *session;
595 u32 sid;
596
Florin Coras134a9962018-08-28 11:32:04 -0700597 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
598 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700599 if (!session)
600 {
601 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
602 return VCL_INVALID_SESSION_INDEX;
603 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800604
605 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700606 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800607 {
608
609 if (!vcl_flag_accepted_session (session, reset_msg->handle,
610 VCL_ACCEPTED_F_RESET))
611 VDBG (0, "session was not accepted!");
612 return VCL_INVALID_SESSION_INDEX;
613 }
614
Florin Corasc127d5a2020-10-14 16:35:58 -0700615 if (session->session_state != VCL_STATE_CLOSED)
616 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800617 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700618 return sid;
619}
620
Florin Coras60116992018-08-27 09:52:18 -0700621static u32
Florin Coras134a9962018-08-28 11:32:04 -0700622vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700623{
624 vcl_session_t *session;
625 u32 sid = mp->context;
626
Florin Coras134a9962018-08-28 11:32:04 -0700627 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700628 if (mp->retval)
629 {
Florin Coras5e062572019-03-14 19:07:51 -0700630 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700631 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700632 if (session)
633 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700634 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700635 session->vpp_handle = mp->handle;
636 return sid;
637 }
638 else
639 {
Florin Coras5e062572019-03-14 19:07:51 -0700640 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
641 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700642 return VCL_INVALID_SESSION_INDEX;
643 }
644 }
645
646 session->vpp_handle = mp->handle;
647 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500648 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
649 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700650 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700651 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700652 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800653
Florin Coras6e3c1f82020-01-15 01:30:46 +0000654 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700655 {
Florin Corasc547e912020-12-08 17:50:45 -0800656 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800657 mp->tx_fifo, mp->vpp_evt_q, 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800658 {
659 VDBG (0, "failed to attach fifos for %u", session->session_index);
660 session->session_state = VCL_STATE_DETACHED;
661 return VCL_INVALID_SESSION_INDEX;
662 }
Florin Coras60116992018-08-27 09:52:18 -0700663 }
664
Florin Coras05ecfcc2018-12-12 18:19:39 -0800665 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700666 return sid;
667}
668
Florin Corasdfae9f92019-02-20 19:48:31 -0800669static void
670vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
671{
672 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
673 vcl_session_t *s;
674
675 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000676 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800677 {
678 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
679 return;
680 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700681 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000682 {
683 /* Connected udp listener */
684 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700685 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000686 return;
687
688 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
689 return;
690 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800691
692 if (mp->retval)
693 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700694 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800695
696 if (mp->context != wrk->wrk_index)
697 VDBG (0, "wrong context");
698
699 vcl_session_table_del_vpp_handle (wrk, mp->handle);
700 vcl_session_free (wrk, s);
701}
702
Florin Coras68b7e582020-01-21 18:33:23 -0800703static void
704vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
705{
706 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
707 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800708 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800709
710 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
711 if (!s)
712 {
713 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
714 return;
715 }
716
Florin Coras1bb74942021-02-10 15:26:37 -0800717 /* Only validate if a value is provided */
718 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800719 {
Florin Coras1bb74942021-02-10 15:26:37 -0800720 fs_index = vcl_segment_table_lookup (mp->segment_handle);
721 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
722 {
723 VDBG (0, "segment %lx for session %u is not mounted!",
724 mp->segment_handle, s->session_index);
725 s->session_state = VCL_STATE_DETACHED;
726 return;
727 }
Florin Corasc547e912020-12-08 17:50:45 -0800728 }
729
Florin Coras57660d92020-04-04 22:45:34 +0000730 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800731
732 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
733 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800734
Florin Coras68b7e582020-01-21 18:33:23 -0800735 vcl_session_table_del_vpp_handle (wrk, mp->handle);
736 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
737
738 /* Generate new tx event if we have outstanding data */
739 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800740 app_send_io_evt_to_vpp (s->vpp_evt_q,
741 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800742 SESSION_IO_EVT_TX, SVM_Q_WAIT);
743
Florin Coras57660d92020-04-04 22:45:34 +0000744 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800745 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800746}
747
Florin Coras3c7d4f92018-12-14 11:28:43 -0800748static vcl_session_t *
749vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
750{
751 vcl_session_msg_t *vcl_msg;
752 vcl_session_t *session;
753
754 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
755 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800756 VWRN ("session overlap handle %lu state %u!", msg->handle,
757 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800758
759 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
760 if (!session)
761 {
762 VERR ("couldn't find listen session: listener handle %llx",
763 msg->listener_handle);
764 return 0;
765 }
766
767 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000768 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800769 vcl_msg->accepted_msg = *msg;
770 /* Session handle points to listener until fully accepted by app */
771 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
772
773 return session;
774}
775
776static vcl_session_t *
777vcl_session_disconnected_handler (vcl_worker_t * wrk,
778 session_disconnected_msg_t * msg)
779{
780 vcl_session_t *session;
781
782 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
783 if (!session)
784 {
785 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
786 return 0;
787 }
788
Florin Corasadcfb152020-02-12 08:50:29 +0000789 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700790 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000791 return 0;
792
Florin Coras3c7d4f92018-12-14 11:28:43 -0800793 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700794 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800795 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800796 if (!vcl_flag_accepted_session (session, msg->handle,
797 VCL_ACCEPTED_F_CLOSED))
798 VDBG (0, "session was not accepted!");
799 return 0;
800 }
801
Florin Corasadcfb152020-02-12 08:50:29 +0000802 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700803 if (session->session_state != VCL_STATE_DISCONNECT)
804 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000805
Florin Coras3c7d4f92018-12-14 11:28:43 -0800806 return session;
807}
808
liuyacan534468e2021-05-09 03:50:40 +0000809int
liuyacan55c952e2021-06-13 14:54:55 +0800810vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000811{
812 vcl_worker_t *wrk = vcl_worker_get_current ();
813 vcl_session_t *session;
814 vcl_session_state_t state;
815 u64 vpp_handle;
816
817 session = vcl_session_get_w_handle (wrk, session_handle);
818 if (PREDICT_FALSE (!session))
819 return VPPCOM_EBADFD;
820
821 vpp_handle = session->vpp_handle;
822 state = session->session_state;
823
824 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
825 vpp_handle, state, vppcom_session_state_str (state));
826
827 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
828 {
829 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
830 return VPPCOM_EBADFD;
831 }
832
liuyacan55c952e2021-06-13 14:54:55 +0800833 if (how == SHUT_RD || how == SHUT_RDWR)
834 {
835 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
836 if (how == SHUT_RD)
837 return VPPCOM_OK;
838 }
839 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
840
liuyacan534468e2021-05-09 03:50:40 +0000841 if (PREDICT_TRUE (state == VCL_STATE_READY))
842 {
843 VDBG (1, "session %u [0x%llx]: sending shutdown...",
844 session->session_index, vpp_handle);
845
846 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000847 }
848
849 return VPPCOM_OK;
850}
851
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700852static int
853vppcom_session_disconnect (u32 session_handle)
854{
855 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700856 vcl_session_t *session, *listen_session;
857 vcl_session_state_t state;
858 u64 vpp_handle;
859
860 session = vcl_session_get_w_handle (wrk, session_handle);
861 if (!session)
862 return VPPCOM_EBADFD;
863
864 vpp_handle = session->vpp_handle;
865 state = session->session_state;
866
867 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
868 vpp_handle, state, vppcom_session_state_str (state));
869
870 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
871 {
872 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
873 return VPPCOM_EBADFD;
874 }
875
876 if (state == VCL_STATE_VPP_CLOSING)
877 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800878 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700879 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
880 session->session_index, vpp_handle);
881 }
882 else
883 {
884 /* Session doesn't have an event queue yet. Probably a non-blocking
885 * connect. Wait for the reply */
886 if (PREDICT_FALSE (!session->vpp_evt_q))
887 return VPPCOM_OK;
888
889 VDBG (1, "session %u [0x%llx]: sending disconnect...",
890 session->session_index, vpp_handle);
891 vcl_send_session_disconnect (wrk, session);
892 }
893
894 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
895 {
896 listen_session = vcl_session_get (wrk, session->listener_index);
897 listen_session->n_accepted_sessions--;
898 }
899
900 return VPPCOM_OK;
901}
902
Florin Coras30e79c22019-01-02 19:31:22 -0800903static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700904vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
905{
906 session_cleanup_msg_t *msg;
907 vcl_session_t *session;
908
909 msg = (session_cleanup_msg_t *) data;
910 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
911 if (!session)
912 {
913 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
914 return;
915 }
916
Florin Coras36d49392020-04-24 23:00:11 +0000917 if (msg->type == SESSION_CLEANUP_TRANSPORT)
918 {
919 /* Transport was cleaned up before we confirmed close. Probably the
920 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700921 * Confirm close to make sure everything is cleaned up.
922 * Move to undetermined state to ensure that the session is not
923 * removed before both vpp and the app cleanup.
924 * - If the app closes first, the session is moved to CLOSED state
925 * and the session cleanup notification from vpp removes the
926 * session.
927 * - If vpp cleans up the session first, the session is moved to
928 * DETACHED state lower and subsequently the close from the app
929 * frees the session
930 */
931 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700932 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700933 vppcom_session_disconnect (vcl_session_handle (session));
934 session->session_state = VCL_STATE_UPDATED;
935 }
936 else if (session->session_state == VCL_STATE_DISCONNECT)
937 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800938 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700939 session->session_state = VCL_STATE_UPDATED;
940 }
Florin Coras36d49392020-04-24 23:00:11 +0000941 return;
942 }
943
Florin Coras9ace36d2019-10-28 13:14:17 -0700944 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000945 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700946 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000947 {
948 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700949 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000950 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
951 return;
952 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700953 vcl_session_free (wrk, session);
954}
955
956static void
Florin Coras30e79c22019-01-02 19:31:22 -0800957vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
958{
959 session_req_worker_update_msg_t *msg;
960 vcl_session_t *s;
961
962 msg = (session_req_worker_update_msg_t *) data;
963 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
964 if (!s)
965 return;
966
967 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
968}
969
970static void
971vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
972{
973 session_worker_update_reply_msg_t *msg;
974 vcl_session_t *s;
975
976 msg = (session_worker_update_reply_msg_t *) data;
977 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
978 if (!s)
979 {
980 VDBG (0, "unknown handle 0x%llx", msg->handle);
981 return;
982 }
Florin Coras30e79c22019-01-02 19:31:22 -0800983
Florin Coras5f45e012019-01-23 09:21:30 -0800984 if (s->rx_fifo)
985 {
Florin Corasc547e912020-12-08 17:50:45 -0800986 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800987 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800988 {
989 VDBG (0, "failed to attach fifos for %u", s->session_index);
990 return;
991 }
Florin Coras5f45e012019-01-23 09:21:30 -0800992 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700993 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800994
Florin Coras30e79c22019-01-02 19:31:22 -0800995 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
996 s->vpp_handle, wrk->wrk_index);
997}
998
Florin Coras935ce752020-09-08 22:43:47 -0700999static int
1000vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
1001{
1002
1003 if (vcm->cfg.vpp_app_socket_api)
1004 return vcl_sapi_recv_fds (wrk, fds, n_fds);
1005
1006 return vcl_bapi_recv_fds (wrk, fds, n_fds);
1007}
1008
Florin Corasc4c4cf52019-08-24 18:17:34 -07001009static void
1010vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
1011{
1012 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
1013 session_app_add_segment_msg_t *msg;
1014 u64 segment_handle;
1015 int fd = -1;
1016
1017 msg = (session_app_add_segment_msg_t *) data;
1018
1019 if (msg->fd_flags)
1020 {
Florin Coras935ce752020-09-08 22:43:47 -07001021 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -07001022 seg_type = SSVM_SEGMENT_MEMFD;
1023 }
1024
1025 segment_handle = msg->segment_handle;
1026 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
1027 {
1028 clib_warning ("invalid segment handle");
1029 return;
1030 }
1031
1032 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
1033 seg_type, fd))
1034 {
1035 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
1036 return;
1037 }
1038
1039 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
1040 msg->segment_size);
1041}
1042
1043static void
1044vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
1045{
1046 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
1047 vcl_segment_detach (msg->segment_handle);
1048 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
1049}
1050
Florin Coras40c07ce2020-07-16 20:46:17 -07001051static void
1052vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
1053{
1054 if (!vcm->wrk_rpc_fn)
1055 return;
1056
hanlina3a48962020-07-13 11:09:15 +08001057 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -07001058}
1059
Florin Coras04ae8272021-04-12 19:55:37 -07001060static void
1061vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
1062{
1063 session_transport_attr_reply_msg_t *mp;
1064
1065 if (!wrk->session_attr_op)
1066 return;
1067
1068 mp = (session_transport_attr_reply_msg_t *) data;
1069
1070 wrk->session_attr_op_rv = mp->retval;
1071 wrk->session_attr_op = 0;
1072 wrk->session_attr_rv = mp->attr;
1073}
1074
Florin Coras86f04502018-09-12 16:08:01 -07001075static int
1076vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001077{
Florin Coras54693d22018-07-17 10:46:29 -07001078 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001079 session_connected_msg_t *connected_msg;
1080 session_reset_msg_t *reset_msg;
1081 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001082 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001083 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001084
1085 switch (e->event_type)
1086 {
Florin Coras653e43f2019-03-04 10:56:23 -08001087 case SESSION_IO_EVT_RX:
1088 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001089 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001090 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001091 break;
Florin Coras86f04502018-09-12 16:08:01 -07001092 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001093 break;
Florin Corasb242d312020-10-26 15:35:40 -07001094 case SESSION_CTRL_EVT_BOUND:
1095 /* We can only wait for only one listen so not postponed */
1096 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1097 break;
Florin Coras54693d22018-07-17 10:46:29 -07001098 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001099 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1100 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1101 {
1102 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1103 *ecpy = *e;
1104 ecpy->postponed = 1;
1105 ecpy->session_index = s->session_index;
1106 }
Florin Coras54693d22018-07-17 10:46:29 -07001107 break;
1108 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001109 connected_msg = (session_connected_msg_t *) e->data;
1110 sid = vcl_session_connected_handler (wrk, connected_msg);
1111 if (!(s = vcl_session_get (wrk, sid)))
1112 break;
1113 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1114 {
1115 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1116 *ecpy = *e;
1117 ecpy->postponed = 1;
1118 ecpy->session_index = s->session_index;
1119 }
Florin Coras54693d22018-07-17 10:46:29 -07001120 break;
1121 case SESSION_CTRL_EVT_DISCONNECTED:
1122 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001123 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1124 break;
1125 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1126 {
1127 vec_add1 (wrk->unhandled_evts_vector, *e);
1128 break;
1129 }
1130 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001131 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001132 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1133 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001134 break;
1135 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001136 reset_msg = (session_reset_msg_t *) e->data;
1137 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1138 break;
1139 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1140 {
1141 vec_add1 (wrk->unhandled_evts_vector, *e);
1142 break;
1143 }
Florin Coras134a9962018-08-28 11:32:04 -07001144 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001145 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001146 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1147 vcl_session_unlisten_reply_handler (wrk, e->data);
1148 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001149 case SESSION_CTRL_EVT_MIGRATED:
1150 vcl_session_migrated_handler (wrk, e->data);
1151 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001152 case SESSION_CTRL_EVT_CLEANUP:
1153 vcl_session_cleanup_handler (wrk, e->data);
1154 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001155 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1156 vcl_session_req_worker_update_handler (wrk, e->data);
1157 break;
1158 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1159 vcl_session_worker_update_reply_handler (wrk, e->data);
1160 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001161 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1162 vcl_session_app_add_segment_handler (wrk, e->data);
1163 break;
1164 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1165 vcl_session_app_del_segment_handler (wrk, e->data);
1166 break;
hanlina3a48962020-07-13 11:09:15 +08001167 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001168 vcl_worker_rpc_handler (wrk, e->data);
1169 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001170 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1171 vcl_session_transport_attr_reply_handler (wrk, e->data);
1172 break;
Florin Coras54693d22018-07-17 10:46:29 -07001173 default:
1174 clib_warning ("unhandled %u", e->event_type);
1175 }
1176 return VPPCOM_OK;
1177}
1178
Florin Coras30e79c22019-01-02 19:31:22 -08001179static int
Florin Coras697faea2018-06-27 17:10:49 -07001180vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001181 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001182 f64 wait_for_time)
1183{
Florin Coras134a9962018-08-28 11:32:04 -07001184 vcl_worker_t *wrk = vcl_worker_get_current ();
1185 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001186 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001187 svm_msg_q_msg_t msg;
1188 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001189
Florin Coras697faea2018-06-27 17:10:49 -07001190 do
Dave Wallace543852a2017-08-03 02:11:34 -04001191 {
Florin Coras134a9962018-08-28 11:32:04 -07001192 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001193 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001194 {
Florin Coras070453d2018-08-24 17:04:27 -07001195 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001196 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001197 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001198 {
Florin Coras697faea2018-06-27 17:10:49 -07001199 return VPPCOM_OK;
1200 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001201 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001202 {
Florin Coras697faea2018-06-27 17:10:49 -07001203 return VPPCOM_ECONNREFUSED;
1204 }
Florin Coras54693d22018-07-17 10:46:29 -07001205
Florin Coras134a9962018-08-28 11:32:04 -07001206 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001207 {
1208 usleep (100);
1209 continue;
1210 }
Florin Coras134a9962018-08-28 11:32:04 -07001211 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001212 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001213 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001214 }
Florin Coras134a9962018-08-28 11:32:04 -07001215 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001216
Florin Coras05ecfcc2018-12-12 18:19:39 -08001217 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001218 vppcom_session_state_str (state));
1219 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001220
Florin Coras697faea2018-06-27 17:10:49 -07001221 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001222}
1223
Florin Coras30e79c22019-01-02 19:31:22 -08001224static void
1225vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1226{
Florin Coras288eaab2019-02-03 15:26:14 -08001227 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001228 vcl_session_t *s;
1229 u32 *sip;
1230
1231 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1232 return;
1233
1234 vec_foreach (sip, wrk->pending_session_wrk_updates)
1235 {
1236 s = vcl_session_get (wrk, *sip);
1237 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1238 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001239 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1240 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001241 s->session_state = state;
1242 }
1243 vec_reset_length (wrk->pending_session_wrk_updates);
1244}
1245
Florin Corasf9240dc2019-01-15 08:03:17 -08001246void
Florin Coras5398dfb2021-01-25 20:31:27 -08001247vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001248{
Florin Coras30e79c22019-01-02 19:31:22 -08001249 svm_msg_q_msg_t *msg;
1250 session_event_t *e;
1251 svm_msg_q_t *mq;
1252 int i;
1253
1254 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001255 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001256
1257 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1258 {
1259 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1260 e = svm_msg_q_msg_data (mq, msg);
1261 vcl_handle_mq_event (wrk, e);
1262 svm_msg_q_free_msg (mq, msg);
1263 }
1264 vec_reset_length (wrk->mq_msg_vector);
1265 vcl_handle_pending_wrk_updates (wrk);
1266}
1267
Florin Coras5398dfb2021-01-25 20:31:27 -08001268void
1269vcl_flush_mq_events (void)
1270{
1271 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1272}
1273
Florin Coras697faea2018-06-27 17:10:49 -07001274static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001275vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001276{
Florin Coras134a9962018-08-28 11:32:04 -07001277 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001278 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001279 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001280 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001281
Florin Corasab2f6db2018-08-31 14:31:41 -07001282 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001283 if (!session)
1284 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001285
Florin Corasaaff5ee2019-07-08 13:00:00 -07001286 /* Flush pending accept events, if any */
1287 while (clib_fifo_elts (session->accept_evts_fifo))
1288 {
1289 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1290 accepted_msg = &evt->accepted_msg;
1291 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1292 vcl_send_session_accepted_reply (session->vpp_evt_q,
1293 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001294 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001295 }
1296 clib_fifo_free (session->accept_evts_fifo);
1297
Florin Coras458089b2019-08-21 16:20:44 -07001298 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001299
Florin Coras5e062572019-03-14 19:07:51 -07001300 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001301 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001302 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001303
1304 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001305 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001306
Florin Coras070453d2018-08-24 17:04:27 -07001307 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001308}
1309
Florin Coras940f78f2018-11-30 12:11:20 -08001310/**
1311 * Handle app exit
1312 *
1313 * Notify vpp of the disconnect and mark the worker as free. If we're the
1314 * last worker, do a full cleanup otherwise, since we're probably a forked
1315 * child, avoid syscalls as much as possible. We might've lost privileges.
1316 */
1317void
1318vppcom_app_exit (void)
1319{
1320 if (!pool_elts (vcm->workers))
1321 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001322 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1323 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001324 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001325}
1326
Florin Coras935ce752020-09-08 22:43:47 -07001327static int
1328vcl_api_attach (void)
1329{
1330 if (vcm->cfg.vpp_app_socket_api)
1331 return vcl_sapi_attach ();
1332
1333 return vcl_bapi_attach ();
1334}
1335
1336static void
1337vcl_api_detach (vcl_worker_t * wrk)
1338{
1339 vcl_send_app_detach (wrk);
1340
1341 if (vcm->cfg.vpp_app_socket_api)
1342 return vcl_sapi_detach (wrk);
1343
1344 return vcl_bapi_disconnect_from_vpp ();
1345}
1346
Dave Wallace543852a2017-08-03 02:11:34 -04001347/*
1348 * VPPCOM Public API functions
1349 */
1350int
Florin Coras66ec4672020-06-15 07:59:40 -07001351vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001352{
Dave Wallace543852a2017-08-03 02:11:34 -04001353 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001354 int rv;
1355
Florin Coras47c40e22018-11-26 17:01:36 -08001356 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001357 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001358 VDBG (1, "already initialized");
1359 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001360 }
1361
Florin Coras47c40e22018-11-26 17:01:36 -08001362 vcm->is_init = 1;
1363 vppcom_cfg (&vcm->cfg);
1364 vcl_cfg = &vcm->cfg;
1365
1366 vcm->main_cpu = pthread_self ();
1367 vcm->main_pid = getpid ();
1368 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001369 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1370 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001371 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1372 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001373 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001374 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001375 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001376
1377 /* Allocate default worker */
1378 vcl_worker_alloc_and_init ();
1379
Florin Coras935ce752020-09-08 22:43:47 -07001380 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001381 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001382
1383 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001384 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001385
1386 return VPPCOM_OK;
1387}
1388
1389void
1390vppcom_app_destroy (void)
1391{
Florin Corasdc0ded72020-04-30 02:59:55 +00001392 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001393 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001394
Florin Coras940f78f2018-11-30 12:11:20 -08001395 if (!pool_elts (vcm->workers))
1396 return;
1397
Florin Coras0d427d82018-06-27 03:24:07 -07001398 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001399
Florin Corasdc0ded72020-04-30 02:59:55 +00001400 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001401
Florin Corasce815de2020-04-16 18:47:27 +00001402 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001403 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001404 if (current_wrk != wrk)
1405 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001406 }
Florin Corasce815de2020-04-16 18:47:27 +00001407 /* *INDENT-ON* */
1408
Florin Coras935ce752020-09-08 22:43:47 -07001409 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001410 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1411
Florin Coras0d427d82018-06-27 03:24:07 -07001412 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001413
1414 /*
1415 * Free the heap and fix vcm
1416 */
1417 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001418 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001419
1420 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001421 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001422}
1423
1424int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001425vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001426{
Florin Coras134a9962018-08-28 11:32:04 -07001427 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001428 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001429
Florin Coras134a9962018-08-28 11:32:04 -07001430 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001431
Florin Coras7e12d942018-06-27 14:32:43 -07001432 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001433 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001434 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001435 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001436
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001437 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001438 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001439
Florin Coras7e12d942018-06-27 14:32:43 -07001440 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1441 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001442
Florin Coras5e062572019-03-14 19:07:51 -07001443 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001444
Florin Coras134a9962018-08-28 11:32:04 -07001445 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001446}
1447
Florin Corasfe286f72021-06-04 10:07:55 -07001448static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001449vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001450{
Florin Corasfa3884f2021-06-22 20:04:46 -07001451 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001452
Florin Corasfa3884f2021-06-22 20:04:46 -07001453 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001454 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001455 wrk->ep_lt_current = s->session_index;
1456 s->vep.lt_next = s->session_index;
1457 s->vep.lt_prev = s->session_index;
1458 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001459 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001460
1461 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1462 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1463
1464 prev->vep.lt_next = s->session_index;
1465 s->vep.lt_prev = prev->session_index;
1466
1467 s->vep.lt_next = cur->session_index;
1468 cur->vep.lt_prev = s->session_index;
1469}
1470
1471static void
1472vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1473{
1474 vcl_session_t *prev, *next;
1475
1476 if (s->vep.lt_next == s->session_index)
1477 {
1478 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1479 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1480 return;
1481 }
1482
1483 prev = vcl_session_get (wrk, s->vep.lt_prev);
1484 next = vcl_session_get (wrk, s->vep.lt_next);
1485
1486 prev->vep.lt_next = next->session_index;
1487 next->vep.lt_prev = prev->session_index;
1488
1489 if (s->session_index == wrk->ep_lt_current)
1490 wrk->ep_lt_current = s->vep.lt_next;
1491
1492 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001493}
1494
Dave Wallace543852a2017-08-03 02:11:34 -04001495int
Florin Corasc127d5a2020-10-14 16:35:58 -07001496vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001497 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001498{
Florin Coras134a9962018-08-28 11:32:04 -07001499 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001500
Florin Coras6c3b2182020-10-19 18:36:48 -07001501 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001502
Florin Coras6c3b2182020-10-19 18:36:48 -07001503 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001504 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001505 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001506 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001507 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001508 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001509 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001510 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001511 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1512 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001513 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001514 }
Florin Corase4306b62020-10-28 12:51:10 -07001515 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001516 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001517
Florin Coras6c3b2182020-10-19 18:36:48 -07001518 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001519 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001520 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001521 if (rv < 0)
1522 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001523 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1524 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001525 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001526
Florin Coras9ace36d2019-10-28 13:14:17 -07001527 if (!do_disconnect)
1528 {
1529 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001530 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001531 goto cleanup;
1532 }
1533
Florin Coras6c3b2182020-10-19 18:36:48 -07001534 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001535 {
1536 rv = vppcom_session_unbind (sh);
1537 if (PREDICT_FALSE (rv < 0))
1538 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001539 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001540 vppcom_retval_str (rv));
1541 return rv;
1542 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001543 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001544 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001545 {
1546 rv = vppcom_session_disconnect (sh);
1547 if (PREDICT_FALSE (rv < 0))
1548 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001549 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001550 rv, vppcom_retval_str (rv));
1551 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001552 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001553 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001554 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001555 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001556 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001557 {
1558 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001559 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001560 goto free_session;
1561 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001562
Florin Corasc127d5a2020-10-14 16:35:58 -07001563 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001564
Florin Coras9ace36d2019-10-28 13:14:17 -07001565 /* Session is removed only after vpp confirms the disconnect */
1566 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001567
Florin Corasf9240dc2019-01-15 08:03:17 -08001568cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001569 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001570free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001571 vcl_session_free (wrk, s);
1572 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001573
Dave Wallace543852a2017-08-03 02:11:34 -04001574 return rv;
1575}
1576
1577int
Florin Corasf9240dc2019-01-15 08:03:17 -08001578vppcom_session_close (uint32_t session_handle)
1579{
1580 vcl_worker_t *wrk = vcl_worker_get_current ();
1581 vcl_session_t *session;
1582
1583 session = vcl_session_get_w_handle (wrk, session_handle);
1584 if (!session)
1585 return VPPCOM_EBADFD;
1586 return vcl_session_cleanup (wrk, session, session_handle,
1587 1 /* do_disconnect */ );
1588}
1589
1590int
Florin Coras134a9962018-08-28 11:32:04 -07001591vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001592{
Florin Coras134a9962018-08-28 11:32:04 -07001593 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001594 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001595
1596 if (!ep || !ep->ip)
1597 return VPPCOM_EINVAL;
1598
Florin Coras134a9962018-08-28 11:32:04 -07001599 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001600 if (!session)
1601 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001602
Florin Coras6c3b2182020-10-19 18:36:48 -07001603 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001604 {
Florin Coras5e062572019-03-14 19:07:51 -07001605 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1606 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001607 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001608 }
1609
Florin Coras7e12d942018-06-27 14:32:43 -07001610 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001611 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001612 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1613 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001614 else
Dave Barach178cf492018-11-13 16:34:13 -05001615 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1616 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001617 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001618
Florin Coras5e062572019-03-14 19:07:51 -07001619 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1620 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001621 session->transport.is_ip4 ? "IPv4" : "IPv6",
1622 format_ip46_address, &session->transport.lcl_ip,
1623 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1624 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001625 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001626 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001627
1628 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001629 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001630
Florin Coras070453d2018-08-24 17:04:27 -07001631 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001632}
1633
1634int
Florin Coras134a9962018-08-28 11:32:04 -07001635vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001636{
Florin Coras134a9962018-08-28 11:32:04 -07001637 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001638 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001639 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001640 int rv;
1641
Florin Coras134a9962018-08-28 11:32:04 -07001642 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001643 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001644 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001645
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001646 if (q_len == 0 || q_len == ~0)
1647 q_len = vcm->cfg.listen_queue_size;
1648
Dave Wallaceee45d412017-11-24 21:44:06 -05001649 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001650 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001651 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001652 VDBG (0, "session %u [0x%llx]: already in listen state!",
1653 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001654 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001655 }
1656
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001657 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001658
Florin Coras070453d2018-08-24 17:04:27 -07001659 /*
1660 * Send listen request to vpp and wait for reply
1661 */
Florin Coras458089b2019-08-21 16:20:44 -07001662 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001663 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001664 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001665 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001666
Florin Coras070453d2018-08-24 17:04:27 -07001667 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001668 {
Florin Coras134a9962018-08-28 11:32:04 -07001669 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001670 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1671 listen_sh, listen_session->vpp_handle, rv,
1672 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001673 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001674 }
1675
Florin Coras070453d2018-08-24 17:04:27 -07001676 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001677}
1678
Florin Coras134a9962018-08-28 11:32:04 -07001679static int
Florin Coras5e062572019-03-14 19:07:51 -07001680validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001681{
Florin Coras6c3b2182020-10-19 18:36:48 -07001682 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001683 {
Florin Coras5e062572019-03-14 19:07:51 -07001684 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1685 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001686 return VPPCOM_EBADFD;
1687 }
1688
Florin Corasc127d5a2020-10-14 16:35:58 -07001689 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001690 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001691 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001692 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001693 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001694 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001695 return VPPCOM_EBADFD;
1696 }
1697 return VPPCOM_OK;
1698}
1699
1700int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001701vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1702{
1703 if (!strcmp (proto_str, "TCP"))
1704 *proto = VPPCOM_PROTO_TCP;
1705 else if (!strcmp (proto_str, "tcp"))
1706 *proto = VPPCOM_PROTO_TCP;
1707 else if (!strcmp (proto_str, "UDP"))
1708 *proto = VPPCOM_PROTO_UDP;
1709 else if (!strcmp (proto_str, "udp"))
1710 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001711 else if (!strcmp (proto_str, "TLS"))
1712 *proto = VPPCOM_PROTO_TLS;
1713 else if (!strcmp (proto_str, "tls"))
1714 *proto = VPPCOM_PROTO_TLS;
1715 else if (!strcmp (proto_str, "QUIC"))
1716 *proto = VPPCOM_PROTO_QUIC;
1717 else if (!strcmp (proto_str, "quic"))
1718 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001719 else if (!strcmp (proto_str, "DTLS"))
1720 *proto = VPPCOM_PROTO_DTLS;
1721 else if (!strcmp (proto_str, "dtls"))
1722 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001723 else if (!strcmp (proto_str, "SRTP"))
1724 *proto = VPPCOM_PROTO_SRTP;
1725 else if (!strcmp (proto_str, "srtp"))
1726 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001727 else
1728 return 1;
1729 return 0;
1730}
1731
1732int
Florin Coras134a9962018-08-28 11:32:04 -07001733vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001734 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001735{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001736 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001737 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001738 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001739 vcl_session_t *listen_session = 0;
1740 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001741 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001742 u8 is_nonblocking;
1743 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001744
Florin Coras5398dfb2021-01-25 20:31:27 -08001745again:
1746
Florin Coras134a9962018-08-28 11:32:04 -07001747 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001748 if (!listen_session)
1749 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001750
Florin Coras134a9962018-08-28 11:32:04 -07001751 listen_session_index = listen_session->session_index;
1752 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001753 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001754
Florin Coras54693d22018-07-17 10:46:29 -07001755 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001756 {
Florin Coras54693d22018-07-17 10:46:29 -07001757 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001758 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001759 accepted_msg = evt->accepted_msg;
1760 goto handle;
1761 }
1762
Florin Corasac422d62020-10-19 20:51:36 -07001763 is_nonblocking = vcl_session_has_attr (listen_session,
1764 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001765 while (1)
1766 {
Carl Smith592a9092019-11-12 14:57:37 +13001767 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1768 return VPPCOM_EAGAIN;
1769
Florin Coras5398dfb2021-01-25 20:31:27 -08001770 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1771 vcl_worker_flush_mq_events (wrk);
1772 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001773 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001774
Florin Coras54693d22018-07-17 10:46:29 -07001775handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001776
Florin Coras00cca802019-06-06 09:38:44 -07001777 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1778 listen_session_index);
1779 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1780 return VPPCOM_ECONNABORTED;
1781
Florin Coras134a9962018-08-28 11:32:04 -07001782 listen_session = vcl_session_get (wrk, listen_session_index);
1783 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001784
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001785 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001786 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001787
Florin Coras5e062572019-03-14 19:07:51 -07001788 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1789 " flags %d, is_nonblocking %u", listen_session->session_index,
1790 listen_session->vpp_handle, client_session_index,
1791 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001792 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001793
Dave Wallace048b1d62018-01-03 22:24:41 -05001794 if (ep)
1795 {
Florin Coras7e12d942018-06-27 14:32:43 -07001796 ep->is_ip4 = client_session->transport.is_ip4;
1797 ep->port = client_session->transport.rmt_port;
1798 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001799 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1800 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001801 else
Dave Barach178cf492018-11-13 16:34:13 -05001802 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1803 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001804 }
Dave Wallace60caa062017-11-10 17:07:13 -05001805
Florin Coras05ecfcc2018-12-12 18:19:39 -08001806 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001807 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001808 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001809 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001810 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001811 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001812 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001813 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001814 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001815 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1816 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001817
Florin Coras3c7d4f92018-12-14 11:28:43 -08001818 /*
1819 * Session might have been closed already
1820 */
1821 if (accept_flags)
1822 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001823 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001824 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001825 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001826 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001827 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001828 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001829}
1830
1831int
Florin Coras134a9962018-08-28 11:32:04 -07001832vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001833{
Florin Coras134a9962018-08-28 11:32:04 -07001834 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001835 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001836 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001837 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001838
Florin Coras134a9962018-08-28 11:32:04 -07001839 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001840 if (!session)
1841 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001842 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001843
Florin Coras6c3b2182020-10-19 18:36:48 -07001844 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001845 {
Florin Coras5e062572019-03-14 19:07:51 -07001846 VDBG (0, "ERROR: cannot connect epoll session %u!",
1847 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001848 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001849 }
1850
Florin Corasc127d5a2020-10-14 16:35:58 -07001851 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001852 {
Florin Coras7baeb712019-01-04 17:05:43 -08001853 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001854 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001855 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001856 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001857 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001858 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001859 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001860 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001861 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001862 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001863 }
1864
Florin Coras0a1e1832020-03-29 18:54:04 +00001865 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001866 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001867 {
1868 if (session->session_type != VPPCOM_PROTO_UDP)
1869 return VPPCOM_EINVAL;
1870 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001871 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001872 }
1873
Florin Coras7e12d942018-06-27 14:32:43 -07001874 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001875 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001876 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001877 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001878 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001879
Florin Coras0a1e1832020-03-29 18:54:04 +00001880 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001881 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001882 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001883 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001884 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001885 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001886 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001887 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001888 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001889
Florin Coras458089b2019-08-21 16:20:44 -07001890 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001891
Florin Corasac422d62020-10-19 20:51:36 -07001892 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001893 {
fanyfa49d59d2020-10-13 17:07:16 +08001894 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001895 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001896 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001897 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001898 return VPPCOM_EINPROGRESS;
1899 }
Florin Coras57c88932019-08-29 12:03:17 -07001900
1901 /*
1902 * Wait for reply from vpp if blocking
1903 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001904 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001905 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001906
Florin Coras134a9962018-08-28 11:32:04 -07001907 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001908 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1909 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001910
Dave Wallace4878cbe2017-11-21 03:45:09 -05001911 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001912}
1913
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001914int
1915vppcom_session_stream_connect (uint32_t session_handle,
1916 uint32_t parent_session_handle)
1917{
1918 vcl_worker_t *wrk = vcl_worker_get_current ();
1919 vcl_session_t *session, *parent_session;
1920 u32 session_index, parent_session_index;
1921 int rv;
1922
1923 session = vcl_session_get_w_handle (wrk, session_handle);
1924 if (!session)
1925 return VPPCOM_EBADFD;
1926 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1927 if (!parent_session)
1928 return VPPCOM_EBADFD;
1929
1930 session_index = session->session_index;
1931 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001932 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001933 {
1934 VDBG (0, "ERROR: cannot connect epoll session %u!",
1935 session->session_index);
1936 return VPPCOM_EBADFD;
1937 }
1938
Florin Corasc127d5a2020-10-14 16:35:58 -07001939 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001940 {
1941 VDBG (0, "session handle %u [0x%llx]: session already "
1942 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1943 session_handle, session->vpp_handle,
1944 parent_session_handle, parent_session->vpp_handle,
1945 vppcom_proto_str (session->session_type), session->session_state,
1946 vppcom_session_state_str (session->session_state));
1947 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,
2001 vppcom_session_state_str (s->session_state));
2002 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,
2159 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2160 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
2197 ASSERT (s->rx_bytes_pending < n_bytes);
2198 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
2212vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2213 size_t n, u8 is_flush, u8 is_dgram)
2214{
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,
2239 vppcom_session_state_str (s->session_state));
2240 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,
2247 vppcom_session_state_str (s->session_state));
2248 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)
Florin Coras460dce62018-07-27 05:45:06 -07002278 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002279 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002280 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
2309 return vppcom_session_write_inline (wrk, s, buf, n,
2310 0 /* is_flush */ , 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
2323 return vppcom_session_write_inline (wrk, s, buf, n,
2324 1 /* is_flush */ , 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
Florin Coras134a9962018-08-28 11:32:04 -07002543 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2544 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2545 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002546 for (i = 0; i < n_mq_evts; i++)
2547 {
Florin Coras134a9962018-08-28 11:32:04 -07002548 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002549 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002550 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002551 except_map, 0, bits_set);
2552 }
2553
2554 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2555}
2556
Dave Wallace543852a2017-08-03 02:11:34 -04002557int
Florin Coras294afe22019-01-07 17:49:17 -08002558vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2559 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002560{
Florin Coras54693d22018-07-17 10:46:29 -07002561 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002562 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002563 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002564 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002565
Dave Wallace7876d392017-10-19 03:53:57 -04002566 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002567 {
Florin Coras134a9962018-08-28 11:32:04 -07002568 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002569 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002570 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2571 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002572 }
Dave Wallace7876d392017-10-19 03:53:57 -04002573 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002574 {
Florin Coras134a9962018-08-28 11:32:04 -07002575 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002576 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002577 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2578 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002579 }
Dave Wallace7876d392017-10-19 03:53:57 -04002580 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002581 {
Florin Coras134a9962018-08-28 11:32:04 -07002582 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002583 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002584 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2585 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002586 }
2587
Florin Coras54693d22018-07-17 10:46:29 -07002588 if (!n_bits)
2589 return 0;
2590
2591 if (!write_map)
2592 goto check_rd;
2593
Florin Coras096a1d52021-01-27 15:33:51 -08002594 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2595 {
2596 if (!(s = vcl_session_get (wrk, sid)))
2597 {
2598 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2599 bits_set++;
2600 continue;
2601 }
Florin Coras54693d22018-07-17 10:46:29 -07002602
Florin Coras096a1d52021-01-27 15:33:51 -08002603 if (vcl_session_write_ready (s))
2604 {
2605 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2606 bits_set++;
2607 }
2608 else
2609 {
2610 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2611 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2612 }
2613 }
Florin Coras54693d22018-07-17 10:46:29 -07002614
2615check_rd:
2616 if (!read_map)
2617 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002618
Florin Coras096a1d52021-01-27 15:33:51 -08002619 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2620 {
2621 if (!(s = vcl_session_get (wrk, sid)))
2622 {
2623 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2624 bits_set++;
2625 continue;
2626 }
Florin Coras54693d22018-07-17 10:46:29 -07002627
Florin Coras096a1d52021-01-27 15:33:51 -08002628 if (vcl_session_read_ready (s))
2629 {
2630 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2631 bits_set++;
2632 }
2633 }
Florin Coras54693d22018-07-17 10:46:29 -07002634
2635check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002636
Florin Coras86f04502018-09-12 16:08:01 -07002637 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2638 {
2639 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2640 read_map, write_map, except_map, &bits_set);
2641 }
2642 vec_reset_length (wrk->unhandled_evts_vector);
2643
Florin Coras99368312018-08-02 10:45:44 -07002644 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002645 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002646 time_to_wait, &bits_set);
2647 else
Florin Coras134a9962018-08-28 11:32:04 -07002648 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002649 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002650
Dave Wallace543852a2017-08-03 02:11:34 -04002651 return (bits_set);
2652}
2653
Dave Wallacef7f809c2017-10-03 01:48:42 -04002654static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002655vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002656{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002657 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002658 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002659 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002660
Florin Corasc0737e92019-03-04 14:19:39 -08002661 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002662 return;
2663
Florin Coras6c3b2182020-10-19 18:36:48 -07002664 s = vcl_session_get_w_handle (wrk, vep_handle);
2665 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002666 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002667 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002668 goto done;
2669 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002670 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002671 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002672 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002673 goto done;
2674 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002675 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002676 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002677 "{\n"
2678 " is_vep = %u\n"
2679 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002680 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002681 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2682 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002683
Florin Coras7e5e62b2019-07-30 14:08:23 -07002684 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002685 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002686 s = vcl_session_get_w_handle (wrk, sh);
2687 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002689 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002690 goto done;
2691 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002692 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002693 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002694 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002695 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002696 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002697 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002698 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002699 goto done;
2700 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002701 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002702 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2703 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002704 sh, s->vep.vep_sh, vep_handle);
2705 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002706 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002707 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002708 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002709 " next_sh = 0x%x (%u)\n"
2710 " prev_sh = 0x%x (%u)\n"
2711 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002712 " ev.events = 0x%x\n"
2713 " ev.data.u64 = 0x%llx\n"
2714 " et_mask = 0x%x\n"
2715 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002716 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002717 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2718 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002719 }
2720 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002721
2722done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002723 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002724}
2725
2726int
2727vppcom_epoll_create (void)
2728{
Florin Coras134a9962018-08-28 11:32:04 -07002729 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002730 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002731
Florin Coras134a9962018-08-28 11:32:04 -07002732 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002733
Florin Coras6c3b2182020-10-19 18:36:48 -07002734 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002735 vep_session->vep.vep_sh = ~0;
2736 vep_session->vep.next_sh = ~0;
2737 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002738 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002739
Florin Corasa7a1a222018-12-30 17:11:31 -08002740 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2741 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002742
Florin Corasab2f6db2018-08-31 14:31:41 -07002743 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002744}
2745
2746int
Florin Coras134a9962018-08-28 11:32:04 -07002747vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002748 struct epoll_event *event)
2749{
Florin Coras134a9962018-08-28 11:32:04 -07002750 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002751 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002752 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002753 vcl_session_t *s;
2754 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002755
Florin Coras134a9962018-08-28 11:32:04 -07002756 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002758 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002759 return VPPCOM_EINVAL;
2760 }
2761
Florin Coras134a9962018-08-28 11:32:04 -07002762 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002763 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002764 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002765 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002766 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002767 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002768 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002770 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002771 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002772 }
2773
Florin Coras134a9962018-08-28 11:32:04 -07002774 ASSERT (vep_session->vep.vep_sh == ~0);
2775 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776
Florin Coras17ec5772020-08-12 20:46:29 -07002777 s = vcl_session_get_w_handle (wrk, session_handle);
2778 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002779 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002780 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002781 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002782 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002783 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002784 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002785 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002786 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002787 }
2788
2789 switch (op)
2790 {
2791 case EPOLL_CTL_ADD:
2792 if (PREDICT_FALSE (!event))
2793 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002794 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002795 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002796 }
Florin Coras2645f682021-06-04 15:59:41 -07002797 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2798 {
2799 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2800 rv = VPPCOM_EEXIST;
2801 goto done;
2802 }
Florin Coras134a9962018-08-28 11:32:04 -07002803 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002804 {
Florin Coras7e12d942018-06-27 14:32:43 -07002805 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002806 next_session = vcl_session_get_w_handle (wrk,
2807 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002808 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002809 {
Florin Coras5e062572019-03-14 19:07:51 -07002810 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002811 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002812 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002813 }
Florin Coras134a9962018-08-28 11:32:04 -07002814 ASSERT (next_session->vep.prev_sh == vep_handle);
2815 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002816 }
Florin Coras17ec5772020-08-12 20:46:29 -07002817 s->vep.next_sh = vep_session->vep.next_sh;
2818 s->vep.prev_sh = vep_handle;
2819 s->vep.vep_sh = vep_handle;
2820 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002821 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002822 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002823 s->flags &= ~VCL_SESSION_F_IS_VEP;
2824 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002825 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002826
Florin Coras17ec5772020-08-12 20:46:29 -07002827 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2828 if (txf && (event->events & EPOLLOUT))
2829 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002830
Florin Coras6e3c1f82020-01-15 01:30:46 +00002831 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002832 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002833 {
2834 session_event_t e = { 0 };
2835 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002836 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002837 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002838 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002839 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002840 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002841 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002842 {
2843 session_event_t e = { 0 };
2844 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002845 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002846 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002847 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2848 add_evt = 1;
2849 }
2850 if (!add_evt && vcl_session_is_closing (s))
2851 {
2852 session_event_t e = { 0 };
2853 if (s->session_state == VCL_STATE_VPP_CLOSING)
2854 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2855 else
2856 e.event_type = SESSION_CTRL_EVT_RESET;
2857 e.session_index = s->session_index;
2858 e.postponed = 1;
2859 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002860 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002861 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2862 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002863 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002864 break;
2865
2866 case EPOLL_CTL_MOD:
2867 if (PREDICT_FALSE (!event))
2868 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002869 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002870 rv = VPPCOM_EINVAL;
2871 goto done;
2872 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002873 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002874 {
Florin Coras5e062572019-03-14 19:07:51 -07002875 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002876 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002877 goto done;
2878 }
Florin Coras17ec5772020-08-12 20:46:29 -07002879 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002880 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002881 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002882 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002883 rv = VPPCOM_EINVAL;
2884 goto done;
2885 }
hanlin475c9d72019-12-26 11:44:28 +08002886
Florin Coras2645f682021-06-04 15:59:41 -07002887 /* Generate EPOLLOUT if session write ready nd event was not on */
2888 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2889 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002890 {
2891 session_event_t e = { 0 };
2892 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002893 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002894 vec_add1 (wrk->unhandled_evts_vector, e);
2895 }
Florin Coras2645f682021-06-04 15:59:41 -07002896 /* Generate EPOLLIN if session read ready and event was not on */
2897 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2898 (vcl_session_read_ready (s) > 0))
2899 {
2900 session_event_t e = { 0 };
2901 e.event_type = SESSION_IO_EVT_RX;
2902 e.session_index = s->session_index;
2903 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002904 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002905 }
Florin Coras17ec5772020-08-12 20:46:29 -07002906 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2907 s->vep.ev = *event;
2908 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002909 if (txf)
2910 {
2911 if (event->events & EPOLLOUT)
2912 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2913 else
2914 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2915 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002916 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2917 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002918 break;
2919
2920 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002921 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002922 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002923 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002924 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002925 goto done;
2926 }
Florin Coras17ec5772020-08-12 20:46:29 -07002927 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002928 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002929 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002930 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002931 rv = VPPCOM_EINVAL;
2932 goto done;
2933 }
2934
Florin Coras17ec5772020-08-12 20:46:29 -07002935 if (s->vep.prev_sh == vep_handle)
2936 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002937 else
2938 {
Florin Coras7e12d942018-06-27 14:32:43 -07002939 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002940 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002941 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002942 {
Florin Coras5e062572019-03-14 19:07:51 -07002943 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002944 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002945 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002946 }
Florin Coras134a9962018-08-28 11:32:04 -07002947 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002948 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002949 }
Florin Coras17ec5772020-08-12 20:46:29 -07002950 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002951 {
Florin Coras7e12d942018-06-27 14:32:43 -07002952 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002953 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002954 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002955 {
Florin Coras5e062572019-03-14 19:07:51 -07002956 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002957 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002958 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002959 }
Florin Coras134a9962018-08-28 11:32:04 -07002960 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002961 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002962 }
2963
Florin Corasfa3884f2021-06-22 20:04:46 -07002964 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2965 vcl_epoll_lt_del (wrk, s);
2966
Florin Coras17ec5772020-08-12 20:46:29 -07002967 memset (&s->vep, 0, sizeof (s->vep));
2968 s->vep.next_sh = ~0;
2969 s->vep.prev_sh = ~0;
2970 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002971 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002972 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002973
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002974 if (vcl_session_is_open (s))
2975 {
2976 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2977 if (txf)
2978 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2979 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002980
Florin Coras5e062572019-03-14 19:07:51 -07002981 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002982 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002983 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002984 break;
2985
2986 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002987 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002988 rv = VPPCOM_EINVAL;
2989 }
2990
Florin Coras134a9962018-08-28 11:32:04 -07002991 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002992
2993done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002994 return rv;
2995}
2996
Florin Coras86f04502018-09-12 16:08:01 -07002997static inline void
2998vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2999 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003000{
3001 session_disconnected_msg_t *disconnected_msg;
3002 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003003 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003004 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003005 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003006 u8 add_event = 0;
3007
3008 switch (e->event_type)
3009 {
Florin Coras653e43f2019-03-04 10:56:23 -08003010 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003011 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003012 s = vcl_session_get (wrk, sid);
3013 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003014 break;
Florin Corasb242d312020-10-26 15:35:40 -07003015 vcl_fifo_rx_evt_valid_or_break (s);
3016 session_events = s->vep.ev.events;
3017 if (!(EPOLLIN & s->vep.ev.events)
3018 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07003019 break;
3020 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003021 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003022 session_evt_data = s->vep.ev.data.u64;
3023 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003024 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003025 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003026 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003027 s = vcl_session_get (wrk, sid);
3028 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003029 break;
Florin Corasb242d312020-10-26 15:35:40 -07003030 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003031 if (!(EPOLLOUT & session_events))
3032 break;
3033 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003034 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003035 session_evt_data = s->vep.ev.data.u64;
3036 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3037 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003038 break;
Florin Coras86f04502018-09-12 16:08:01 -07003039 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003040 if (!e->postponed)
3041 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3042 else
3043 s = vcl_session_get (wrk, e->session_index);
3044 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003045 break;
Florin Corasb242d312020-10-26 15:35:40 -07003046 session_events = s->vep.ev.events;
3047 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07003048 if (!(EPOLLIN & session_events))
3049 break;
Florin Coras86f04502018-09-12 16:08:01 -07003050 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003051 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003052 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003053 break;
3054 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003055 if (!e->postponed)
3056 {
3057 connected_msg = (session_connected_msg_t *) e->data;
3058 sid = vcl_session_connected_handler (wrk, connected_msg);
3059 }
3060 else
3061 sid = e->session_index;
3062 s = vcl_session_get (wrk, sid);
3063 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003064 break;
Florin Corasb242d312020-10-26 15:35:40 -07003065 session_events = s->vep.ev.events;
3066 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003067 if (!(EPOLLOUT & session_events))
3068 break;
3069 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003070 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003071 session_evt_data = s->vep.ev.data.u64;
3072 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003073 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003074 break;
3075 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003076 if (!e->postponed)
3077 {
3078 disconnected_msg = (session_disconnected_msg_t *) e->data;
3079 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3080 }
3081 else
3082 {
3083 s = vcl_session_get (wrk, e->session_index);
3084 }
3085 if (vcl_session_is_closed (s) ||
3086 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Coras86f04502018-09-12 16:08:01 -07003087 break;
Florin Corasb242d312020-10-26 15:35:40 -07003088 sid = s->session_index;
3089 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003090 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003091 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003092 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003093 break;
3094 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003095 if (!e->postponed)
3096 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3097 else
3098 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003099 s = vcl_session_get (wrk, sid);
Florin Coras87f76002021-06-28 19:13:29 -07003100 if (vcl_session_is_closed (s) ||
3101 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Coras86f04502018-09-12 16:08:01 -07003102 break;
Florin Corasb242d312020-10-26 15:35:40 -07003103 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003104 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003105 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003106 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003107 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003108 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3109 vcl_session_unlisten_reply_handler (wrk, e->data);
3110 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003111 case SESSION_CTRL_EVT_MIGRATED:
3112 vcl_session_migrated_handler (wrk, e->data);
3113 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003114 case SESSION_CTRL_EVT_CLEANUP:
3115 vcl_session_cleanup_handler (wrk, e->data);
3116 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003117 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3118 vcl_session_req_worker_update_handler (wrk, e->data);
3119 break;
3120 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3121 vcl_session_worker_update_reply_handler (wrk, e->data);
3122 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003123 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3124 vcl_session_app_add_segment_handler (wrk, e->data);
3125 break;
3126 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3127 vcl_session_app_del_segment_handler (wrk, e->data);
3128 break;
hanlina3a48962020-07-13 11:09:15 +08003129 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003130 vcl_worker_rpc_handler (wrk, e->data);
3131 break;
Florin Coras86f04502018-09-12 16:08:01 -07003132 default:
3133 VDBG (0, "unhandled: %u", e->event_type);
3134 break;
3135 }
3136
3137 if (add_event)
3138 {
3139 events[*num_ev].data.u64 = session_evt_data;
3140 if (EPOLLONESHOT & session_events)
3141 {
Florin Corasb242d312020-10-26 15:35:40 -07003142 s = vcl_session_get (wrk, sid);
3143 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003144 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003145 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003146 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003147 s = vcl_session_get (wrk, sid);
3148 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3149 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003150 }
Florin Coras86f04502018-09-12 16:08:01 -07003151 *num_ev += 1;
3152 }
3153}
3154
3155static int
3156vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3157 struct epoll_event *events, u32 maxevents,
3158 double wait_for_time, u32 * num_ev)
3159{
Florin Coras99368312018-08-02 10:45:44 -07003160 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003161 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003162 int i;
3163
Florin Coras539663c2018-09-28 14:59:37 -07003164 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3165 goto handle_dequeued;
3166
Florin Coras54693d22018-07-17 10:46:29 -07003167 if (svm_msg_q_is_empty (mq))
3168 {
3169 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003170 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003171 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003172 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003173 else
3174 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003175 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003176 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003177 }
3178 }
Florin Corase003a1b2019-06-05 10:47:16 -07003179 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003180 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003181
Florin Coras539663c2018-09-28 14:59:37 -07003182handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003183 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003184 {
Florin Coras134a9962018-08-28 11:32:04 -07003185 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003186 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003187 if (*num_ev < maxevents)
3188 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3189 else
3190 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003191 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003192 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003193 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003194 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003195 return *num_ev;
3196}
3197
Florin Coras99368312018-08-02 10:45:44 -07003198static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003199vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3200 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003201{
Florin Corasccdb8b82021-04-28 13:01:06 -07003202 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003203
3204 if (!n_evts)
3205 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003206 if (timeout_ms > 0)
3207 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003208 }
3209
3210 do
3211 {
3212 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003213 timeout_ms, &n_evts);
3214 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003215 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003216 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003217 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003218
3219 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003220}
3221
3222static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003223vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3224 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003225{
Florin Coras99368312018-08-02 10:45:44 -07003226 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003227 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003228 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003229 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003230 u64 buf;
3231
Florin Coras134a9962018-08-28 11:32:04 -07003232 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003233 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003234 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003235 if (timeout_ms > 0)
3236 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003237 }
3238
Florin Corasb13ba132021-01-27 16:05:24 -08003239 do
3240 {
3241 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003242 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003243 if (n_mq_evts < 0)
3244 {
3245 VDBG (0, "epoll_wait error %u", errno);
3246 return n_evts;
3247 }
3248
3249 for (i = 0; i < n_mq_evts; i++)
3250 {
3251 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3252 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3253 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3254 &n_evts);
3255 }
3256
Florin Corasccdb8b82021-04-28 13:01:06 -07003257 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003258 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003259 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003260 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003261
3262 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003263}
3264
Florin Corasfe286f72021-06-04 10:07:55 -07003265static void
Florin Corasfe286f72021-06-04 10:07:55 -07003266vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3267 int maxevents, u32 *n_evts)
3268{
Florin Coras734268f2021-06-30 07:54:29 -07003269 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003270 vcl_session_t *s;
3271 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003272 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003273
Florin Corasfa3884f2021-06-22 20:04:46 -07003274 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003275 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003276 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003277
Florin Corasfa3884f2021-06-22 20:04:46 -07003278 next = wrk->ep_lt_current;
3279 do
Florin Corasfe286f72021-06-04 10:07:55 -07003280 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003281 s = vcl_session_get (wrk, next);
3282 next = s->vep.lt_next;
3283
3284 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003285 {
3286 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003287 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003288 evt_data = s->vep.ev.data.u64;
3289 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003290 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003291 {
3292 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003293 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3294 evt_data = s->vep.ev.data.u64;
3295 }
3296 if (!add_event && s->session_state > VCL_STATE_READY)
3297 {
3298 add_event = 1;
3299 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003300 evt_data = s->vep.ev.data.u64;
3301 }
3302 if (add_event)
3303 {
3304 events[*n_evts].data.u64 = evt_data;
3305 *n_evts += 1;
3306 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003307 if (EPOLLONESHOT & s->vep.ev.events)
3308 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003309 if (*n_evts == maxevents)
3310 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003311 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003312 break;
3313 }
3314 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003315 else
3316 {
3317 vcl_epoll_lt_del (wrk, s);
3318 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3319 break;
3320 }
Florin Corasfe286f72021-06-04 10:07:55 -07003321 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003322 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003323}
3324
Dave Wallacef7f809c2017-10-03 01:48:42 -04003325int
Florin Coras134a9962018-08-28 11:32:04 -07003326vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003327 int maxevents, double wait_for_time)
3328{
Florin Coras134a9962018-08-28 11:32:04 -07003329 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003330 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003331 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003332 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003333
3334 if (PREDICT_FALSE (maxevents <= 0))
3335 {
Florin Coras5e062572019-03-14 19:07:51 -07003336 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003337 return VPPCOM_EINVAL;
3338 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003339
Florin Coras134a9962018-08-28 11:32:04 -07003340 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003341 if (!vep_session)
3342 return VPPCOM_EBADFD;
3343
Florin Coras6c3b2182020-10-19 18:36:48 -07003344 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003345 {
Florin Coras5e062572019-03-14 19:07:51 -07003346 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003347 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003348 }
Florin Coras54693d22018-07-17 10:46:29 -07003349
Florin Coras86f04502018-09-12 16:08:01 -07003350 if (vec_len (wrk->unhandled_evts_vector))
3351 {
3352 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3353 {
3354 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3355 events, &n_evts);
3356 if (n_evts == maxevents)
3357 {
Florin Corase003a1b2019-06-05 10:47:16 -07003358 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3359 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003360 }
3361 }
Florin Corase003a1b2019-06-05 10:47:16 -07003362 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003363 }
wanghanlin8919fec2021-03-18 20:00:41 +08003364 /* Request to only drain unhandled */
3365 if ((int) wait_for_time == -2)
3366 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003367
Florin Coras86f04502018-09-12 16:08:01 -07003368
Florin Corasfe286f72021-06-04 10:07:55 -07003369 if (vcm->cfg.use_mq_eventfd)
3370 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3371 wait_for_time);
3372 else
3373 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3374 wait_for_time);
3375
Florin Corasfa3884f2021-06-22 20:04:46 -07003376 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
Florin Corasfe286f72021-06-04 10:07:55 -07003377 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3378
3379 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003380}
3381
Dave Wallace35830af2017-10-09 01:43:42 -04003382int
Florin Coras134a9962018-08-28 11:32:04 -07003383vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003384 void *buffer, uint32_t * buflen)
3385{
Florin Coras134a9962018-08-28 11:32:04 -07003386 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003387 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003388 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003389 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003390 vcl_session_t *session;
3391 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003392
Florin Coras134a9962018-08-28 11:32:04 -07003393 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003394 if (!session)
3395 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003396
Dave Wallace35830af2017-10-09 01:43:42 -04003397 switch (op)
3398 {
3399 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003400 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003401 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3402 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003403 break;
3404
Dave Wallace227867f2017-11-13 21:21:53 -05003405 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003406 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003407 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3408 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003409 break;
3410
3411 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003412 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003413 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003414 *flags =
3415 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003416 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003417 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003418 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003419 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3420 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003421 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003422 }
3423 else
3424 rv = VPPCOM_EINVAL;
3425 break;
3426
3427 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003428 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003429 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003430 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003431 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003432 else
Florin Corasac422d62020-10-19 20:51:36 -07003433 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003434
Florin Coras5e062572019-03-14 19:07:51 -07003435 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3436 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003437 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003438 }
3439 else
3440 rv = VPPCOM_EINVAL;
3441 break;
3442
3443 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003444 if (PREDICT_TRUE (buffer && buflen &&
3445 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003446 {
Florin Coras7e12d942018-06-27 14:32:43 -07003447 ep->is_ip4 = session->transport.is_ip4;
3448 ep->port = session->transport.rmt_port;
3449 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003450 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3451 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003452 else
Dave Barach178cf492018-11-13 16:34:13 -05003453 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3454 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003455 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003456 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3457 "addr = %U, port %u", session_handle, ep->is_ip4,
3458 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003459 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3460 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003461 }
3462 else
3463 rv = VPPCOM_EINVAL;
3464 break;
3465
3466 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003467 if (PREDICT_TRUE (buffer && buflen &&
3468 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003469 {
Florin Coras7e12d942018-06-27 14:32:43 -07003470 ep->is_ip4 = session->transport.is_ip4;
3471 ep->port = session->transport.lcl_port;
3472 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003473 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3474 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003475 else
Dave Barach178cf492018-11-13 16:34:13 -05003476 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3477 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003478 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003479 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3480 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003481 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003482 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3483 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003484 }
3485 else
3486 rv = VPPCOM_EINVAL;
3487 break;
Stevenb5a11602017-10-11 09:59:30 -07003488
Florin Corasef7cbf62019-10-17 09:56:27 -07003489 case VPPCOM_ATTR_SET_LCL_ADDR:
3490 if (PREDICT_TRUE (buffer && buflen &&
3491 (*buflen >= sizeof (*ep)) && ep->ip))
3492 {
3493 session->transport.is_ip4 = ep->is_ip4;
3494 session->transport.lcl_port = ep->port;
3495 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3496 *buflen = sizeof (*ep);
3497 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3498 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3499 &session->transport.lcl_ip,
3500 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3501 clib_net_to_host_u16 (ep->port));
3502 }
3503 else
3504 rv = VPPCOM_EINVAL;
3505 break;
3506
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 case VPPCOM_ATTR_GET_LIBC_EPFD:
3508 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003509 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003510 break;
3511
3512 case VPPCOM_ATTR_SET_LIBC_EPFD:
3513 if (PREDICT_TRUE (buffer && buflen &&
3514 (*buflen == sizeof (session->libc_epfd))))
3515 {
3516 session->libc_epfd = *(int *) buffer;
3517 *buflen = sizeof (session->libc_epfd);
3518
Florin Coras5e062572019-03-14 19:07:51 -07003519 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3520 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003521 }
3522 else
3523 rv = VPPCOM_EINVAL;
3524 break;
3525
3526 case VPPCOM_ATTR_GET_PROTOCOL:
3527 if (buffer && buflen && (*buflen >= sizeof (int)))
3528 {
Florin Coras7e12d942018-06-27 14:32:43 -07003529 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003530 *buflen = sizeof (int);
3531
Florin Coras5e062572019-03-14 19:07:51 -07003532 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3533 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003534 }
3535 else
3536 rv = VPPCOM_EINVAL;
3537 break;
3538
3539 case VPPCOM_ATTR_GET_LISTEN:
3540 if (buffer && buflen && (*buflen >= sizeof (int)))
3541 {
Florin Corasac422d62020-10-19 20:51:36 -07003542 *(int *) buffer = vcl_session_has_attr (session,
3543 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003544 *buflen = sizeof (int);
3545
Florin Coras5e062572019-03-14 19:07:51 -07003546 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3547 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003548 }
3549 else
3550 rv = VPPCOM_EINVAL;
3551 break;
3552
3553 case VPPCOM_ATTR_GET_ERROR:
3554 if (buffer && buflen && (*buflen >= sizeof (int)))
3555 {
3556 *(int *) buffer = 0;
3557 *buflen = sizeof (int);
3558
Florin Coras5e062572019-03-14 19:07:51 -07003559 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3560 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003561 }
3562 else
3563 rv = VPPCOM_EINVAL;
3564 break;
3565
3566 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3567 if (buffer && buflen && (*buflen >= sizeof (u32)))
3568 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003569
3570 /* VPP-TBD */
3571 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003572 session->tx_fifo ?
3573 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003574 vcm->cfg.tx_fifo_size);
3575 *buflen = sizeof (u32);
3576
Florin Coras5e062572019-03-14 19:07:51 -07003577 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3578 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3579 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003580 }
3581 else
3582 rv = VPPCOM_EINVAL;
3583 break;
3584
3585 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3586 if (buffer && buflen && (*buflen == sizeof (u32)))
3587 {
3588 /* VPP-TBD */
3589 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003590 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3591 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3592 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003593 }
3594 else
3595 rv = VPPCOM_EINVAL;
3596 break;
3597
3598 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3599 if (buffer && buflen && (*buflen >= sizeof (u32)))
3600 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003601
3602 /* VPP-TBD */
3603 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003604 session->rx_fifo ?
3605 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003606 vcm->cfg.rx_fifo_size);
3607 *buflen = sizeof (u32);
3608
Florin Coras5e062572019-03-14 19:07:51 -07003609 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3610 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003611 }
3612 else
3613 rv = VPPCOM_EINVAL;
3614 break;
3615
3616 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3617 if (buffer && buflen && (*buflen == sizeof (u32)))
3618 {
3619 /* VPP-TBD */
3620 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003621 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3622 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3623 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003624 }
3625 else
3626 rv = VPPCOM_EINVAL;
3627 break;
3628
3629 case VPPCOM_ATTR_GET_REUSEADDR:
3630 if (buffer && buflen && (*buflen >= sizeof (int)))
3631 {
3632 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003633 *(int *) buffer = vcl_session_has_attr (session,
3634 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003635 *buflen = sizeof (int);
3636
Florin Coras5e062572019-03-14 19:07:51 -07003637 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3638 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003639 }
3640 else
3641 rv = VPPCOM_EINVAL;
3642 break;
3643
Stevenb5a11602017-10-11 09:59:30 -07003644 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003645 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003646 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003647 {
3648 /* VPP-TBD */
3649 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003650 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003651 else
Florin Corasac422d62020-10-19 20:51:36 -07003652 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003653
Florin Coras5e062572019-03-14 19:07:51 -07003654 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003655 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003656 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003657 }
3658 else
3659 rv = VPPCOM_EINVAL;
3660 break;
3661
3662 case VPPCOM_ATTR_GET_REUSEPORT:
3663 if (buffer && buflen && (*buflen >= sizeof (int)))
3664 {
3665 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003666 *(int *) buffer = vcl_session_has_attr (session,
3667 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003668 *buflen = sizeof (int);
3669
Florin Coras5e062572019-03-14 19:07:51 -07003670 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3671 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003672 }
3673 else
3674 rv = VPPCOM_EINVAL;
3675 break;
3676
3677 case VPPCOM_ATTR_SET_REUSEPORT:
3678 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003679 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003680 {
3681 /* VPP-TBD */
3682 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003683 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003684 else
Florin Corasac422d62020-10-19 20:51:36 -07003685 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003686
Florin Coras5e062572019-03-14 19:07:51 -07003687 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003688 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003689 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003690 }
3691 else
3692 rv = VPPCOM_EINVAL;
3693 break;
3694
3695 case VPPCOM_ATTR_GET_BROADCAST:
3696 if (buffer && buflen && (*buflen >= sizeof (int)))
3697 {
3698 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003699 *(int *) buffer = vcl_session_has_attr (session,
3700 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003701 *buflen = sizeof (int);
3702
Florin Coras5e062572019-03-14 19:07:51 -07003703 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3704 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003705 }
3706 else
3707 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003708 break;
3709
3710 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003711 if (buffer && buflen && (*buflen == sizeof (int)))
3712 {
3713 /* VPP-TBD */
3714 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003715 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003716 else
Florin Corasac422d62020-10-19 20:51:36 -07003717 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003718
Florin Coras5e062572019-03-14 19:07:51 -07003719 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003720 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003721 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003722 }
3723 else
3724 rv = VPPCOM_EINVAL;
3725 break;
3726
3727 case VPPCOM_ATTR_GET_V6ONLY:
3728 if (buffer && buflen && (*buflen >= sizeof (int)))
3729 {
3730 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003731 *(int *) buffer = vcl_session_has_attr (session,
3732 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003733 *buflen = sizeof (int);
3734
Florin Coras5e062572019-03-14 19:07:51 -07003735 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3736 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003737 }
3738 else
3739 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003740 break;
3741
3742 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003743 if (buffer && buflen && (*buflen == sizeof (int)))
3744 {
3745 /* VPP-TBD */
3746 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003747 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003748 else
Florin Corasac422d62020-10-19 20:51:36 -07003749 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003750
Florin Coras5e062572019-03-14 19:07:51 -07003751 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003752 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003753 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003754 }
3755 else
3756 rv = VPPCOM_EINVAL;
3757 break;
3758
3759 case VPPCOM_ATTR_GET_KEEPALIVE:
3760 if (buffer && buflen && (*buflen >= sizeof (int)))
3761 {
3762 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003763 *(int *) buffer = vcl_session_has_attr (session,
3764 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003765 *buflen = sizeof (int);
3766
Florin Coras5e062572019-03-14 19:07:51 -07003767 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3768 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003769 }
3770 else
3771 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003772 break;
Stevenbccd3392017-10-12 20:42:21 -07003773
3774 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003775 if (buffer && buflen && (*buflen == sizeof (int)))
3776 {
3777 /* VPP-TBD */
3778 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003779 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003780 else
Florin Corasac422d62020-10-19 20:51:36 -07003781 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003782
Florin Coras5e062572019-03-14 19:07:51 -07003783 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003784 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003785 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003786 }
3787 else
3788 rv = VPPCOM_EINVAL;
3789 break;
3790
3791 case VPPCOM_ATTR_GET_TCP_NODELAY:
3792 if (buffer && buflen && (*buflen >= sizeof (int)))
3793 {
3794 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003795 *(int *) buffer = vcl_session_has_attr (session,
3796 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003797 *buflen = sizeof (int);
3798
Florin Coras5e062572019-03-14 19:07:51 -07003799 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3800 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003801 }
3802 else
3803 rv = VPPCOM_EINVAL;
3804 break;
3805
3806 case VPPCOM_ATTR_SET_TCP_NODELAY:
3807 if (buffer && buflen && (*buflen == sizeof (int)))
3808 {
3809 /* VPP-TBD */
3810 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003811 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003812 else
Florin Corasac422d62020-10-19 20:51:36 -07003813 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003814
Florin Coras5e062572019-03-14 19:07:51 -07003815 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003816 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003817 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003818 }
3819 else
3820 rv = VPPCOM_EINVAL;
3821 break;
3822
3823 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3824 if (buffer && buflen && (*buflen >= sizeof (int)))
3825 {
3826 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003827 *(int *) buffer = vcl_session_has_attr (session,
3828 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003829 *buflen = sizeof (int);
3830
Florin Coras5e062572019-03-14 19:07:51 -07003831 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3832 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003833 }
3834 else
3835 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003836 break;
3837
3838 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003839 if (buffer && buflen && (*buflen == sizeof (int)))
3840 {
3841 /* VPP-TBD */
3842 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003843 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003844 else
Florin Corasac422d62020-10-19 20:51:36 -07003845 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003846
Florin Coras5e062572019-03-14 19:07:51 -07003847 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003848 vcl_session_has_attr (session,
3849 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003850 }
3851 else
3852 rv = VPPCOM_EINVAL;
3853 break;
3854
3855 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3856 if (buffer && buflen && (*buflen >= sizeof (int)))
3857 {
3858 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003859 *(int *) buffer = vcl_session_has_attr (session,
3860 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003861 *buflen = sizeof (int);
3862
Florin Coras5e062572019-03-14 19:07:51 -07003863 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3864 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003865 }
3866 else
3867 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003868 break;
3869
3870 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003871 if (buffer && buflen && (*buflen == sizeof (int)))
3872 {
3873 /* VPP-TBD */
3874 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003875 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003876 else
Florin Corasac422d62020-10-19 20:51:36 -07003877 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003878
Florin Coras5e062572019-03-14 19:07:51 -07003879 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003880 vcl_session_has_attr (session,
3881 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003882 }
3883 else
3884 rv = VPPCOM_EINVAL;
3885 break;
3886
3887 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003888 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003889 {
Florin Coras04ae8272021-04-12 19:55:37 -07003890 rv = VPPCOM_EINVAL;
3891 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003892 }
Florin Coras04ae8272021-04-12 19:55:37 -07003893
3894 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3895 tea.mss = *(u32 *) buffer;
3896 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3897 rv = VPPCOM_ENOPROTOOPT;
3898
3899 if (!rv)
3900 {
3901 *(u32 *) buffer = tea.mss;
3902 *buflen = sizeof (int);
3903 }
3904
3905 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3906 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003907 break;
3908
3909 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003910 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003911 {
Florin Coras04ae8272021-04-12 19:55:37 -07003912 rv = VPPCOM_EINVAL;
3913 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003914 }
Florin Coras04ae8272021-04-12 19:55:37 -07003915
3916 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3917 tea.mss = *(u32 *) buffer;
3918 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3919 rv = VPPCOM_ENOPROTOOPT;
3920
3921 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3922 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003923 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003924
Florin Coras1e966172020-05-16 18:18:14 +00003925 case VPPCOM_ATTR_SET_CONNECTED:
3926 session->flags |= VCL_SESSION_F_CONNECTED;
3927 break;
3928
Florin Corasa5a9efd2021-01-05 17:03:29 -08003929 case VPPCOM_ATTR_SET_CKPAIR:
3930 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3931 !vcl_session_has_crypto (session))
3932 {
3933 rv = VPPCOM_EINVAL;
3934 break;
3935 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003936 if (!session->ext_config)
3937 {
Florin Coras87f63892021-05-01 16:01:40 -07003938 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3939 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003940 }
3941 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3942 {
3943 rv = VPPCOM_EINVAL;
3944 break;
3945 }
3946
3947 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003948 break;
3949
Florin Coras6a6555a2021-01-28 11:39:27 -08003950 case VPPCOM_ATTR_SET_VRF:
3951 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3952 {
3953 rv = VPPCOM_EINVAL;
3954 break;
3955 }
3956 session->vrf = *(u32 *) buffer;
3957 break;
3958
3959 case VPPCOM_ATTR_GET_VRF:
3960 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3961 {
3962 rv = VPPCOM_EINVAL;
3963 break;
3964 }
3965 *(u32 *) buffer = session->vrf;
3966 *buflen = sizeof (u32);
3967 break;
3968
wanghanlin0674f852021-02-22 10:38:36 +08003969 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003970 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003971 {
Florin Corasd77325c2021-02-23 12:03:03 -08003972 rv = VPPCOM_EINVAL;
3973 break;
wanghanlin0674f852021-02-22 10:38:36 +08003974 }
Florin Corasd77325c2021-02-23 12:03:03 -08003975
3976 if (session->transport.is_ip4)
3977 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003978 else
Florin Corasd77325c2021-02-23 12:03:03 -08003979 *(int *) buffer = AF_INET6;
3980 *buflen = sizeof (int);
3981
wanghanlin0674f852021-02-22 10:38:36 +08003982 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3983 *buflen);
3984 break;
3985
Florin Coras87f63892021-05-01 16:01:40 -07003986 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3987 if (!(buffer && buflen && (*buflen > 0)))
3988 {
3989 rv = VPPCOM_EINVAL;
3990 break;
3991 }
3992 if (session->ext_config)
3993 {
3994 rv = VPPCOM_EINVAL;
3995 break;
3996 }
3997 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3998 *buflen + sizeof (u32));
3999 clib_memcpy (session->ext_config->data, buffer, *buflen);
4000 session->ext_config->len = *buflen;
4001 break;
4002
Dave Wallacee22aa742017-10-20 12:30:38 -04004003 default:
4004 rv = VPPCOM_EINVAL;
4005 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004006 }
4007
Dave Wallace35830af2017-10-09 01:43:42 -04004008 return rv;
4009}
4010
Stevenac1f96d2017-10-24 16:03:58 -07004011int
Florin Coras134a9962018-08-28 11:32:04 -07004012vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004013 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4014{
Florin Coras134a9962018-08-28 11:32:04 -07004015 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004016 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004017 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004018
4019 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004020 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004021 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004022 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004023 else
4024 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004025 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004026 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004027 }
4028
Florin Coras0a1e1832020-03-29 18:54:04 +00004029 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004030 {
Florin Coras5da10c42020-04-01 04:31:21 +00004031 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004032 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004033 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4034 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004035 else
Dave Barach178cf492018-11-13 16:34:13 -05004036 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4037 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004038 ep->is_ip4 = session->transport.is_ip4;
4039 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004040 }
Florin Coras460dce62018-07-27 05:45:06 -07004041
Stevenac1f96d2017-10-24 16:03:58 -07004042 return rv;
4043}
4044
4045int
Florin Coras134a9962018-08-28 11:32:04 -07004046vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004047 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4048{
Florin Coras7a2abce2020-04-05 19:25:44 +00004049 vcl_worker_t *wrk = vcl_worker_get_current ();
4050 vcl_session_t *s;
4051
4052 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004053 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004054 return VPPCOM_EBADFD;
4055
Dave Wallace617dffa2017-10-26 14:47:06 -04004056 if (ep)
4057 {
Florin Coras5824cc52020-10-20 18:44:41 -07004058 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004059 return VPPCOM_EINVAL;
4060
4061 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004062 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004063 {
Florin Coras5824cc52020-10-20 18:44:41 -07004064 u32 session_index = s->session_index;
4065 f64 timeout = vcm->cfg.session_timeout;
4066 int rv;
4067
Florin Coras5da10c42020-04-01 04:31:21 +00004068 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004069 rv = vppcom_wait_for_session_state_change (session_index,
4070 VCL_STATE_READY,
4071 timeout);
4072 if (rv < 0)
4073 return rv;
4074 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004075 }
Florin Coras5824cc52020-10-20 18:44:41 -07004076
4077 s->transport.is_ip4 = ep->is_ip4;
4078 s->transport.rmt_port = ep->port;
4079 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04004080 }
4081
4082 if (flags)
4083 {
4084 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004085 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004086 }
4087
Florin Coras7a2abce2020-04-05 19:25:44 +00004088 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4089 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004090}
4091
Dave Wallace048b1d62018-01-03 22:24:41 -05004092int
4093vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4094{
Florin Coras134a9962018-08-28 11:32:04 -07004095 vcl_worker_t *wrk = vcl_worker_get_current ();
4096 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004097 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004098 svm_msg_q_msg_t msg;
4099 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004100 int rv, num_ev = 0;
4101
Florin Coras5e062572019-03-14 19:07:51 -07004102 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004103
4104 if (!vp)
4105 return VPPCOM_EFAULT;
4106
4107 do
4108 {
Florin Coras7e12d942018-06-27 14:32:43 -07004109 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004110
Florin Coras6917b942018-11-13 22:44:54 -08004111 /* Dequeue all events and drop all unhandled io events */
4112 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4113 {
4114 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4115 vcl_handle_mq_event (wrk, e);
4116 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4117 }
4118 vec_reset_length (wrk->unhandled_evts_vector);
4119
Dave Wallace048b1d62018-01-03 22:24:41 -05004120 for (i = 0; i < n_sids; i++)
4121 {
Florin Coras7baeb712019-01-04 17:05:43 -08004122 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004123 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004124 {
4125 vp[i].revents = POLLHUP;
4126 num_ev++;
4127 continue;
4128 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004129
Florin Coras6917b942018-11-13 22:44:54 -08004130 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004131
4132 if (POLLIN & vp[i].events)
4133 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004134 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004135 if (rv > 0)
4136 {
Florin Coras6917b942018-11-13 22:44:54 -08004137 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004138 num_ev++;
4139 }
4140 else if (rv < 0)
4141 {
4142 switch (rv)
4143 {
4144 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004145 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004146 break;
4147
4148 default:
Florin Coras6917b942018-11-13 22:44:54 -08004149 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004150 break;
4151 }
4152 num_ev++;
4153 }
4154 }
4155
4156 if (POLLOUT & vp[i].events)
4157 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004158 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004159 if (rv > 0)
4160 {
Florin Coras6917b942018-11-13 22:44:54 -08004161 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004162 num_ev++;
4163 }
4164 else if (rv < 0)
4165 {
4166 switch (rv)
4167 {
4168 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004169 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004170 break;
4171
4172 default:
Florin Coras6917b942018-11-13 22:44:54 -08004173 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004174 break;
4175 }
4176 num_ev++;
4177 }
4178 }
4179
Dave Wallace7e607a72018-06-18 18:41:32 -04004180 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004181 {
Florin Coras6917b942018-11-13 22:44:54 -08004182 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004183 num_ev++;
4184 }
4185 }
4186 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004187 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004188 }
4189 while ((num_ev == 0) && keep_trying);
4190
Dave Wallace048b1d62018-01-03 22:24:41 -05004191 return num_ev;
4192}
4193
Florin Coras99368312018-08-02 10:45:44 -07004194int
4195vppcom_mq_epoll_fd (void)
4196{
Florin Coras134a9962018-08-28 11:32:04 -07004197 vcl_worker_t *wrk = vcl_worker_get_current ();
4198 return wrk->mqs_epfd;
4199}
4200
4201int
Florin Coras30e79c22019-01-02 19:31:22 -08004202vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004203{
4204 return session_handle & 0xFFFFFF;
4205}
4206
4207int
Florin Coras30e79c22019-01-02 19:31:22 -08004208vppcom_session_worker (vcl_session_handle_t session_handle)
4209{
4210 return session_handle >> 24;
4211}
4212
4213int
Florin Coras134a9962018-08-28 11:32:04 -07004214vppcom_worker_register (void)
4215{
Florin Coras47c40e22018-11-26 17:01:36 -08004216 if (!vcl_worker_alloc_and_init ())
4217 return VPPCOM_EEXIST;
4218
Florin Coras47c40e22018-11-26 17:01:36 -08004219 if (vcl_worker_register_with_vpp ())
4220 return VPPCOM_EEXIST;
4221
4222 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004223}
4224
Florin Coras369db832019-07-08 12:34:45 -07004225void
4226vppcom_worker_unregister (void)
4227{
4228 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4229 vcl_set_worker_index (~0);
4230}
4231
Pivo6017ff02020-05-04 17:57:33 +02004232void
4233vppcom_worker_index_set (int index)
4234{
4235 vcl_set_worker_index (index);
4236}
4237
Florin Corasdfe4cf42018-11-28 22:13:45 -08004238int
4239vppcom_worker_index (void)
4240{
4241 return vcl_get_worker_index ();
4242}
4243
Florin Corase0982e52019-01-25 13:19:56 -08004244int
4245vppcom_worker_mqs_epfd (void)
4246{
4247 vcl_worker_t *wrk = vcl_worker_get_current ();
4248 if (!vcm->cfg.use_mq_eventfd)
4249 return -1;
4250 return wrk->mqs_epfd;
4251}
4252
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004253int
4254vppcom_session_is_connectable_listener (uint32_t session_handle)
4255{
4256 vcl_session_t *session;
4257 vcl_worker_t *wrk = vcl_worker_get_current ();
4258 session = vcl_session_get_w_handle (wrk, session_handle);
4259 if (!session)
4260 return VPPCOM_EBADFD;
4261 return vcl_session_is_connectable_listener (wrk, session);
4262}
4263
4264int
4265vppcom_session_listener (uint32_t session_handle)
4266{
4267 vcl_worker_t *wrk = vcl_worker_get_current ();
4268 vcl_session_t *listen_session, *session;
4269 session = vcl_session_get_w_handle (wrk, session_handle);
4270 if (!session)
4271 return VPPCOM_EBADFD;
4272 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4273 return VPPCOM_EBADFD;
4274 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4275 if (!listen_session)
4276 return VPPCOM_EBADFD;
4277 return vcl_session_handle (listen_session);
4278}
4279
4280int
4281vppcom_session_n_accepted (uint32_t session_handle)
4282{
4283 vcl_worker_t *wrk = vcl_worker_get_current ();
4284 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4285 if (!session)
4286 return VPPCOM_EBADFD;
4287 return session->n_accepted_sessions;
4288}
4289
Florin Coras66ec4672020-06-15 07:59:40 -07004290const char *
4291vppcom_proto_str (vppcom_proto_t proto)
4292{
4293 char const *proto_str;
4294
4295 switch (proto)
4296 {
4297 case VPPCOM_PROTO_TCP:
4298 proto_str = "TCP";
4299 break;
4300 case VPPCOM_PROTO_UDP:
4301 proto_str = "UDP";
4302 break;
4303 case VPPCOM_PROTO_TLS:
4304 proto_str = "TLS";
4305 break;
4306 case VPPCOM_PROTO_QUIC:
4307 proto_str = "QUIC";
4308 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004309 case VPPCOM_PROTO_DTLS:
4310 proto_str = "DTLS";
4311 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004312 case VPPCOM_PROTO_SRTP:
4313 proto_str = "SRTP";
4314 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004315 default:
4316 proto_str = "UNKNOWN";
4317 break;
4318 }
4319 return proto_str;
4320}
4321
4322const char *
4323vppcom_retval_str (int retval)
4324{
4325 char const *st;
4326
4327 switch (retval)
4328 {
4329 case VPPCOM_OK:
4330 st = "VPPCOM_OK";
4331 break;
4332
4333 case VPPCOM_EAGAIN:
4334 st = "VPPCOM_EAGAIN";
4335 break;
4336
4337 case VPPCOM_EFAULT:
4338 st = "VPPCOM_EFAULT";
4339 break;
4340
4341 case VPPCOM_ENOMEM:
4342 st = "VPPCOM_ENOMEM";
4343 break;
4344
4345 case VPPCOM_EINVAL:
4346 st = "VPPCOM_EINVAL";
4347 break;
4348
4349 case VPPCOM_EBADFD:
4350 st = "VPPCOM_EBADFD";
4351 break;
4352
4353 case VPPCOM_EAFNOSUPPORT:
4354 st = "VPPCOM_EAFNOSUPPORT";
4355 break;
4356
4357 case VPPCOM_ECONNABORTED:
4358 st = "VPPCOM_ECONNABORTED";
4359 break;
4360
4361 case VPPCOM_ECONNRESET:
4362 st = "VPPCOM_ECONNRESET";
4363 break;
4364
4365 case VPPCOM_ENOTCONN:
4366 st = "VPPCOM_ENOTCONN";
4367 break;
4368
4369 case VPPCOM_ECONNREFUSED:
4370 st = "VPPCOM_ECONNREFUSED";
4371 break;
4372
4373 case VPPCOM_ETIMEDOUT:
4374 st = "VPPCOM_ETIMEDOUT";
4375 break;
4376
4377 default:
4378 st = "UNKNOWN_STATE";
4379 break;
4380 }
4381
4382 return st;
4383}
4384
Florin Corasa5a9efd2021-01-05 17:03:29 -08004385int
4386vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4387{
4388 if (vcm->cfg.vpp_app_socket_api)
4389 {
4390 clib_warning ("not supported");
4391 return VPPCOM_EINVAL;
4392 }
4393 return vcl_bapi_add_cert_key_pair (ckpair);
4394}
4395
4396int
4397vppcom_del_cert_key_pair (uint32_t ckpair_index)
4398{
4399 if (vcm->cfg.vpp_app_socket_api)
4400 {
4401 clib_warning ("not supported");
4402 return VPPCOM_EINVAL;
4403 }
4404 return vcl_bapi_del_cert_key_pair (ckpair_index);
4405}
4406
Dave Wallacee22aa742017-10-20 12:30:38 -04004407/*
4408 * fd.io coding-style-patch-verification: ON
4409 *
4410 * Local Variables:
4411 * eval: (c-set-style "gnu")
4412 * End:
4413 */