blob: 987a19c896a71e003f706d3ce48698a2b873531d [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 Corasf6e284b2021-07-21 18:17:20 -0700458 if (vcl_segment_attach_session (
459 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
460 mp->vpp_event_queue_address, mp->mq_index, 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
Florin Corasf6e284b2021-07-21 18:17:20 -0700528 if (vcl_segment_attach_session (
529 mp->segment_handle, mp->server_rx_fifo, mp->server_tx_fifo,
530 mp->vpp_event_queue_address, mp->mq_index, 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 Corasf6e284b2021-07-21 18:17:20 -0700541 mp->ct_tx_fifo, (uword) ~0, ~0, 1,
542 session))
Florin Coras653e43f2019-03-04 10:56:23 -0800543 {
Florin Corasc547e912020-12-08 17:50:45 -0800544 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700545 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700546 vcl_send_session_disconnect (wrk, session);
547 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800548 }
Florin Coras99368312018-08-02 10:45:44 -0700549 }
Florin Coras54693d22018-07-17 10:46:29 -0700550
Florin Coras09d18c22019-04-24 11:10:02 -0700551 session->transport.is_ip4 = mp->lcl.is_ip4;
552 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500553 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700554 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700555
556 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700557 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700558 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700559 vcl_send_session_disconnect (wrk, session);
560 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700561 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700562
563 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800564 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700565
Florin Coras5e062572019-03-14 19:07:51 -0700566 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
567 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700568 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700569
Florin Coras54693d22018-07-17 10:46:29 -0700570 return session_index;
571}
572
Florin Coras3c7d4f92018-12-14 11:28:43 -0800573static int
574vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
575{
576 vcl_session_msg_t *accepted_msg;
577 int i;
578
579 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
580 {
581 accepted_msg = &session->accept_evts_fifo[i];
582 if (accepted_msg->accepted_msg.handle == handle)
583 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800584 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800585 return 1;
586 }
587 }
588 return 0;
589}
590
Florin Corasc9fbd662018-08-24 12:59:56 -0700591static u32
Florin Coras134a9962018-08-28 11:32:04 -0700592vcl_session_reset_handler (vcl_worker_t * wrk,
593 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700594{
595 vcl_session_t *session;
596 u32 sid;
597
Florin Coras134a9962018-08-28 11:32:04 -0700598 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
599 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700600 if (!session)
601 {
602 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
603 return VCL_INVALID_SESSION_INDEX;
604 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800605
606 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700607 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800608 {
609
610 if (!vcl_flag_accepted_session (session, reset_msg->handle,
611 VCL_ACCEPTED_F_RESET))
612 VDBG (0, "session was not accepted!");
613 return VCL_INVALID_SESSION_INDEX;
614 }
615
Florin Corasc127d5a2020-10-14 16:35:58 -0700616 if (session->session_state != VCL_STATE_CLOSED)
617 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800618 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700619 return sid;
620}
621
Florin Coras60116992018-08-27 09:52:18 -0700622static u32
Florin Coras134a9962018-08-28 11:32:04 -0700623vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700624{
625 vcl_session_t *session;
626 u32 sid = mp->context;
627
Florin Coras134a9962018-08-28 11:32:04 -0700628 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700629 if (mp->retval)
630 {
Florin Coras5e062572019-03-14 19:07:51 -0700631 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700632 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700633 if (session)
634 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700635 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700636 session->vpp_handle = mp->handle;
637 return sid;
638 }
639 else
640 {
Florin Coras5e062572019-03-14 19:07:51 -0700641 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
642 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700643 return VCL_INVALID_SESSION_INDEX;
644 }
645 }
646
647 session->vpp_handle = mp->handle;
648 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500649 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
650 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700651 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700652 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700653 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800654
Florin Coras6e3c1f82020-01-15 01:30:46 +0000655 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700656 {
Florin Corasc547e912020-12-08 17:50:45 -0800657 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700658 mp->tx_fifo, mp->vpp_evt_q, mp->mq_index,
659 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800660 {
661 VDBG (0, "failed to attach fifos for %u", session->session_index);
662 session->session_state = VCL_STATE_DETACHED;
663 return VCL_INVALID_SESSION_INDEX;
664 }
Florin Coras60116992018-08-27 09:52:18 -0700665 }
666
Florin Coras05ecfcc2018-12-12 18:19:39 -0800667 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700668 return sid;
669}
670
Florin Corasdfae9f92019-02-20 19:48:31 -0800671static void
672vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
673{
674 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
675 vcl_session_t *s;
676
677 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000678 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800679 {
680 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
681 return;
682 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700683 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000684 {
685 /* Connected udp listener */
686 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700687 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000688 return;
689
690 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
691 return;
692 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800693
694 if (mp->retval)
695 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700696 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800697
698 if (mp->context != wrk->wrk_index)
699 VDBG (0, "wrong context");
700
701 vcl_session_table_del_vpp_handle (wrk, mp->handle);
702 vcl_session_free (wrk, s);
703}
704
Florin Coras68b7e582020-01-21 18:33:23 -0800705static void
706vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
707{
708 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
709 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800710 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800711
712 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
713 if (!s)
714 {
715 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
716 return;
717 }
718
Florin Coras1bb74942021-02-10 15:26:37 -0800719 /* Only validate if a value is provided */
720 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800721 {
Florin Coras1bb74942021-02-10 15:26:37 -0800722 fs_index = vcl_segment_table_lookup (mp->segment_handle);
723 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
724 {
725 VDBG (0, "segment %lx for session %u is not mounted!",
726 mp->segment_handle, s->session_index);
727 s->session_state = VCL_STATE_DETACHED;
728 return;
729 }
Florin Corasc547e912020-12-08 17:50:45 -0800730 }
731
Florin Coras57660d92020-04-04 22:45:34 +0000732 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800733
734 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
735 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800736
Florin Coras68b7e582020-01-21 18:33:23 -0800737 vcl_session_table_del_vpp_handle (wrk, mp->handle);
738 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
739
740 /* Generate new tx event if we have outstanding data */
741 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800742 app_send_io_evt_to_vpp (s->vpp_evt_q,
743 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800744 SESSION_IO_EVT_TX, SVM_Q_WAIT);
745
Florin Coras57660d92020-04-04 22:45:34 +0000746 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800747 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800748}
749
Florin Coras3c7d4f92018-12-14 11:28:43 -0800750static vcl_session_t *
751vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
752{
753 vcl_session_msg_t *vcl_msg;
754 vcl_session_t *session;
755
756 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
757 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800758 VWRN ("session overlap handle %lu state %u!", msg->handle,
759 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800760
761 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
762 if (!session)
763 {
764 VERR ("couldn't find listen session: listener handle %llx",
765 msg->listener_handle);
766 return 0;
767 }
768
769 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000770 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800771 vcl_msg->accepted_msg = *msg;
772 /* Session handle points to listener until fully accepted by app */
773 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
774
775 return session;
776}
777
778static vcl_session_t *
779vcl_session_disconnected_handler (vcl_worker_t * wrk,
780 session_disconnected_msg_t * msg)
781{
782 vcl_session_t *session;
783
784 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
785 if (!session)
786 {
787 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
788 return 0;
789 }
790
Florin Corasadcfb152020-02-12 08:50:29 +0000791 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700792 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000793 return 0;
794
Florin Coras3c7d4f92018-12-14 11:28:43 -0800795 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700796 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800797 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800798 if (!vcl_flag_accepted_session (session, msg->handle,
799 VCL_ACCEPTED_F_CLOSED))
800 VDBG (0, "session was not accepted!");
801 return 0;
802 }
803
Florin Corasadcfb152020-02-12 08:50:29 +0000804 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700805 if (session->session_state != VCL_STATE_DISCONNECT)
806 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000807
Florin Coras3c7d4f92018-12-14 11:28:43 -0800808 return session;
809}
810
liuyacan534468e2021-05-09 03:50:40 +0000811int
liuyacan55c952e2021-06-13 14:54:55 +0800812vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000813{
814 vcl_worker_t *wrk = vcl_worker_get_current ();
815 vcl_session_t *session;
816 vcl_session_state_t state;
817 u64 vpp_handle;
818
819 session = vcl_session_get_w_handle (wrk, session_handle);
820 if (PREDICT_FALSE (!session))
821 return VPPCOM_EBADFD;
822
823 vpp_handle = session->vpp_handle;
824 state = session->session_state;
825
826 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
827 vpp_handle, state, vppcom_session_state_str (state));
828
829 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
830 {
831 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
832 return VPPCOM_EBADFD;
833 }
834
liuyacan55c952e2021-06-13 14:54:55 +0800835 if (how == SHUT_RD || how == SHUT_RDWR)
836 {
837 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
838 if (how == SHUT_RD)
839 return VPPCOM_OK;
840 }
841 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
842
liuyacan534468e2021-05-09 03:50:40 +0000843 if (PREDICT_TRUE (state == VCL_STATE_READY))
844 {
845 VDBG (1, "session %u [0x%llx]: sending shutdown...",
846 session->session_index, vpp_handle);
847
848 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000849 }
850
851 return VPPCOM_OK;
852}
853
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700854static int
855vppcom_session_disconnect (u32 session_handle)
856{
857 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700858 vcl_session_t *session, *listen_session;
859 vcl_session_state_t state;
860 u64 vpp_handle;
861
862 session = vcl_session_get_w_handle (wrk, session_handle);
863 if (!session)
864 return VPPCOM_EBADFD;
865
866 vpp_handle = session->vpp_handle;
867 state = session->session_state;
868
869 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
870 vpp_handle, state, vppcom_session_state_str (state));
871
872 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
873 {
874 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
875 return VPPCOM_EBADFD;
876 }
877
878 if (state == VCL_STATE_VPP_CLOSING)
879 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800880 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700881 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
882 session->session_index, vpp_handle);
883 }
884 else
885 {
886 /* Session doesn't have an event queue yet. Probably a non-blocking
887 * connect. Wait for the reply */
888 if (PREDICT_FALSE (!session->vpp_evt_q))
889 return VPPCOM_OK;
890
891 VDBG (1, "session %u [0x%llx]: sending disconnect...",
892 session->session_index, vpp_handle);
893 vcl_send_session_disconnect (wrk, session);
894 }
895
896 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
897 {
898 listen_session = vcl_session_get (wrk, session->listener_index);
899 listen_session->n_accepted_sessions--;
900 }
901
902 return VPPCOM_OK;
903}
904
Florin Coras30e79c22019-01-02 19:31:22 -0800905static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700906vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
907{
908 session_cleanup_msg_t *msg;
909 vcl_session_t *session;
910
911 msg = (session_cleanup_msg_t *) data;
912 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
913 if (!session)
914 {
915 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
916 return;
917 }
918
Florin Coras36d49392020-04-24 23:00:11 +0000919 if (msg->type == SESSION_CLEANUP_TRANSPORT)
920 {
921 /* Transport was cleaned up before we confirmed close. Probably the
922 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700923 * Confirm close to make sure everything is cleaned up.
924 * Move to undetermined state to ensure that the session is not
925 * removed before both vpp and the app cleanup.
926 * - If the app closes first, the session is moved to CLOSED state
927 * and the session cleanup notification from vpp removes the
928 * session.
929 * - If vpp cleans up the session first, the session is moved to
930 * DETACHED state lower and subsequently the close from the app
931 * frees the session
932 */
933 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700934 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700935 vppcom_session_disconnect (vcl_session_handle (session));
936 session->session_state = VCL_STATE_UPDATED;
937 }
938 else if (session->session_state == VCL_STATE_DISCONNECT)
939 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800940 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700941 session->session_state = VCL_STATE_UPDATED;
942 }
Florin Coras36d49392020-04-24 23:00:11 +0000943 return;
944 }
945
Florin Coras9ace36d2019-10-28 13:14:17 -0700946 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000947 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700948 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000949 {
950 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700951 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000952 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
953 return;
954 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700955 vcl_session_free (wrk, session);
956}
957
958static void
Florin Coras30e79c22019-01-02 19:31:22 -0800959vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
960{
961 session_req_worker_update_msg_t *msg;
962 vcl_session_t *s;
963
964 msg = (session_req_worker_update_msg_t *) data;
965 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
966 if (!s)
967 return;
968
969 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
970}
971
972static void
973vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
974{
975 session_worker_update_reply_msg_t *msg;
976 vcl_session_t *s;
977
978 msg = (session_worker_update_reply_msg_t *) data;
979 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
980 if (!s)
981 {
982 VDBG (0, "unknown handle 0x%llx", msg->handle);
983 return;
984 }
Florin Coras30e79c22019-01-02 19:31:22 -0800985
Florin Coras5f45e012019-01-23 09:21:30 -0800986 if (s->rx_fifo)
987 {
Florin Corasc547e912020-12-08 17:50:45 -0800988 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasf6e284b2021-07-21 18:17:20 -0700989 msg->tx_fifo, (uword) ~0, ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800990 {
991 VDBG (0, "failed to attach fifos for %u", s->session_index);
992 return;
993 }
Florin Coras5f45e012019-01-23 09:21:30 -0800994 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700995 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800996
Florin Coras30e79c22019-01-02 19:31:22 -0800997 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
998 s->vpp_handle, wrk->wrk_index);
999}
1000
Florin Coras935ce752020-09-08 22:43:47 -07001001static int
1002vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
1003{
1004
1005 if (vcm->cfg.vpp_app_socket_api)
1006 return vcl_sapi_recv_fds (wrk, fds, n_fds);
1007
1008 return vcl_bapi_recv_fds (wrk, fds, n_fds);
1009}
1010
Florin Corasc4c4cf52019-08-24 18:17:34 -07001011static void
1012vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
1013{
1014 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
1015 session_app_add_segment_msg_t *msg;
1016 u64 segment_handle;
1017 int fd = -1;
1018
1019 msg = (session_app_add_segment_msg_t *) data;
1020
1021 if (msg->fd_flags)
1022 {
Florin Coras935ce752020-09-08 22:43:47 -07001023 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -07001024 seg_type = SSVM_SEGMENT_MEMFD;
1025 }
1026
1027 segment_handle = msg->segment_handle;
1028 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
1029 {
1030 clib_warning ("invalid segment handle");
1031 return;
1032 }
1033
1034 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
1035 seg_type, fd))
1036 {
1037 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
1038 return;
1039 }
1040
1041 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
1042 msg->segment_size);
1043}
1044
1045static void
1046vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
1047{
1048 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
1049 vcl_segment_detach (msg->segment_handle);
1050 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
1051}
1052
Florin Coras40c07ce2020-07-16 20:46:17 -07001053static void
1054vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
1055{
1056 if (!vcm->wrk_rpc_fn)
1057 return;
1058
hanlina3a48962020-07-13 11:09:15 +08001059 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -07001060}
1061
Florin Coras04ae8272021-04-12 19:55:37 -07001062static void
1063vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
1064{
1065 session_transport_attr_reply_msg_t *mp;
1066
1067 if (!wrk->session_attr_op)
1068 return;
1069
1070 mp = (session_transport_attr_reply_msg_t *) data;
1071
1072 wrk->session_attr_op_rv = mp->retval;
1073 wrk->session_attr_op = 0;
1074 wrk->session_attr_rv = mp->attr;
1075}
1076
Florin Coras86f04502018-09-12 16:08:01 -07001077static int
1078vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001079{
Florin Coras54693d22018-07-17 10:46:29 -07001080 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001081 session_connected_msg_t *connected_msg;
1082 session_reset_msg_t *reset_msg;
1083 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001084 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001085 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001086
1087 switch (e->event_type)
1088 {
Florin Coras653e43f2019-03-04 10:56:23 -08001089 case SESSION_IO_EVT_RX:
1090 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001091 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001092 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001093 break;
Florin Coras86f04502018-09-12 16:08:01 -07001094 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001095 break;
Florin Corasb242d312020-10-26 15:35:40 -07001096 case SESSION_CTRL_EVT_BOUND:
1097 /* We can only wait for only one listen so not postponed */
1098 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1099 break;
Florin Coras54693d22018-07-17 10:46:29 -07001100 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001101 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1102 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1103 {
1104 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1105 *ecpy = *e;
1106 ecpy->postponed = 1;
1107 ecpy->session_index = s->session_index;
1108 }
Florin Coras54693d22018-07-17 10:46:29 -07001109 break;
1110 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001111 connected_msg = (session_connected_msg_t *) e->data;
1112 sid = vcl_session_connected_handler (wrk, connected_msg);
1113 if (!(s = vcl_session_get (wrk, sid)))
1114 break;
1115 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1116 {
1117 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1118 *ecpy = *e;
1119 ecpy->postponed = 1;
1120 ecpy->session_index = s->session_index;
1121 }
Florin Coras54693d22018-07-17 10:46:29 -07001122 break;
1123 case SESSION_CTRL_EVT_DISCONNECTED:
1124 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001125 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1126 break;
1127 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1128 {
1129 vec_add1 (wrk->unhandled_evts_vector, *e);
1130 break;
1131 }
1132 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001133 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001134 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1135 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001136 break;
1137 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001138 reset_msg = (session_reset_msg_t *) e->data;
1139 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1140 break;
1141 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1142 {
1143 vec_add1 (wrk->unhandled_evts_vector, *e);
1144 break;
1145 }
Florin Coras134a9962018-08-28 11:32:04 -07001146 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001147 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001148 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1149 vcl_session_unlisten_reply_handler (wrk, e->data);
1150 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001151 case SESSION_CTRL_EVT_MIGRATED:
1152 vcl_session_migrated_handler (wrk, e->data);
1153 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001154 case SESSION_CTRL_EVT_CLEANUP:
1155 vcl_session_cleanup_handler (wrk, e->data);
1156 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001157 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1158 vcl_session_req_worker_update_handler (wrk, e->data);
1159 break;
1160 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1161 vcl_session_worker_update_reply_handler (wrk, e->data);
1162 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001163 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1164 vcl_session_app_add_segment_handler (wrk, e->data);
1165 break;
1166 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1167 vcl_session_app_del_segment_handler (wrk, e->data);
1168 break;
hanlina3a48962020-07-13 11:09:15 +08001169 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001170 vcl_worker_rpc_handler (wrk, e->data);
1171 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001172 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1173 vcl_session_transport_attr_reply_handler (wrk, e->data);
1174 break;
Florin Coras54693d22018-07-17 10:46:29 -07001175 default:
1176 clib_warning ("unhandled %u", e->event_type);
1177 }
1178 return VPPCOM_OK;
1179}
1180
Florin Coras30e79c22019-01-02 19:31:22 -08001181static int
Florin Coras697faea2018-06-27 17:10:49 -07001182vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001183 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001184 f64 wait_for_time)
1185{
Florin Coras134a9962018-08-28 11:32:04 -07001186 vcl_worker_t *wrk = vcl_worker_get_current ();
1187 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001188 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001189 svm_msg_q_msg_t msg;
1190 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001191
Florin Coras697faea2018-06-27 17:10:49 -07001192 do
Dave Wallace543852a2017-08-03 02:11:34 -04001193 {
Florin Coras134a9962018-08-28 11:32:04 -07001194 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001195 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001196 {
Florin Coras070453d2018-08-24 17:04:27 -07001197 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001198 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001199 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001200 {
Florin Coras697faea2018-06-27 17:10:49 -07001201 return VPPCOM_OK;
1202 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001203 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001204 {
Florin Coras697faea2018-06-27 17:10:49 -07001205 return VPPCOM_ECONNREFUSED;
1206 }
Florin Coras54693d22018-07-17 10:46:29 -07001207
Florin Coras134a9962018-08-28 11:32:04 -07001208 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001209 {
1210 usleep (100);
1211 continue;
1212 }
Florin Coras134a9962018-08-28 11:32:04 -07001213 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001214 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001215 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001216 }
Florin Coras134a9962018-08-28 11:32:04 -07001217 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001218
Florin Coras05ecfcc2018-12-12 18:19:39 -08001219 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001220 vppcom_session_state_str (state));
1221 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001222
Florin Coras697faea2018-06-27 17:10:49 -07001223 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001224}
1225
Florin Coras30e79c22019-01-02 19:31:22 -08001226static void
1227vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1228{
Florin Coras288eaab2019-02-03 15:26:14 -08001229 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001230 vcl_session_t *s;
1231 u32 *sip;
1232
1233 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1234 return;
1235
1236 vec_foreach (sip, wrk->pending_session_wrk_updates)
1237 {
1238 s = vcl_session_get (wrk, *sip);
1239 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1240 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001241 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1242 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001243 s->session_state = state;
1244 }
1245 vec_reset_length (wrk->pending_session_wrk_updates);
1246}
1247
Florin Corasf9240dc2019-01-15 08:03:17 -08001248void
Florin Coras5398dfb2021-01-25 20:31:27 -08001249vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001250{
Florin Coras30e79c22019-01-02 19:31:22 -08001251 svm_msg_q_msg_t *msg;
1252 session_event_t *e;
1253 svm_msg_q_t *mq;
1254 int i;
1255
1256 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001257 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001258
1259 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1260 {
1261 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1262 e = svm_msg_q_msg_data (mq, msg);
1263 vcl_handle_mq_event (wrk, e);
1264 svm_msg_q_free_msg (mq, msg);
1265 }
1266 vec_reset_length (wrk->mq_msg_vector);
1267 vcl_handle_pending_wrk_updates (wrk);
1268}
1269
Florin Coras5398dfb2021-01-25 20:31:27 -08001270void
1271vcl_flush_mq_events (void)
1272{
1273 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1274}
1275
Florin Coras697faea2018-06-27 17:10:49 -07001276static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001277vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001278{
Florin Coras134a9962018-08-28 11:32:04 -07001279 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001280 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001281 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001282 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001283
Florin Corasab2f6db2018-08-31 14:31:41 -07001284 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001285 if (!session)
1286 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001287
Florin Corasaaff5ee2019-07-08 13:00:00 -07001288 /* Flush pending accept events, if any */
1289 while (clib_fifo_elts (session->accept_evts_fifo))
1290 {
1291 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1292 accepted_msg = &evt->accepted_msg;
1293 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1294 vcl_send_session_accepted_reply (session->vpp_evt_q,
1295 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001296 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001297 }
1298 clib_fifo_free (session->accept_evts_fifo);
1299
Florin Coras458089b2019-08-21 16:20:44 -07001300 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001301
Florin Coras5e062572019-03-14 19:07:51 -07001302 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001303 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001304 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001305
1306 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001307 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001308
Florin Coras070453d2018-08-24 17:04:27 -07001309 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001310}
1311
Florin Coras940f78f2018-11-30 12:11:20 -08001312/**
1313 * Handle app exit
1314 *
1315 * Notify vpp of the disconnect and mark the worker as free. If we're the
1316 * last worker, do a full cleanup otherwise, since we're probably a forked
1317 * child, avoid syscalls as much as possible. We might've lost privileges.
1318 */
1319void
1320vppcom_app_exit (void)
1321{
1322 if (!pool_elts (vcm->workers))
1323 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001324 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1325 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001326 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001327}
1328
Florin Coras935ce752020-09-08 22:43:47 -07001329static int
1330vcl_api_attach (void)
1331{
1332 if (vcm->cfg.vpp_app_socket_api)
1333 return vcl_sapi_attach ();
1334
1335 return vcl_bapi_attach ();
1336}
1337
1338static void
1339vcl_api_detach (vcl_worker_t * wrk)
1340{
1341 vcl_send_app_detach (wrk);
1342
1343 if (vcm->cfg.vpp_app_socket_api)
1344 return vcl_sapi_detach (wrk);
1345
1346 return vcl_bapi_disconnect_from_vpp ();
1347}
1348
Dave Wallace543852a2017-08-03 02:11:34 -04001349/*
1350 * VPPCOM Public API functions
1351 */
1352int
Florin Coras66ec4672020-06-15 07:59:40 -07001353vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001354{
Dave Wallace543852a2017-08-03 02:11:34 -04001355 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001356 int rv;
1357
Florin Coras47c40e22018-11-26 17:01:36 -08001358 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001359 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001360 VDBG (1, "already initialized");
1361 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001362 }
1363
Florin Coras47c40e22018-11-26 17:01:36 -08001364 vcm->is_init = 1;
1365 vppcom_cfg (&vcm->cfg);
1366 vcl_cfg = &vcm->cfg;
1367
1368 vcm->main_cpu = pthread_self ();
1369 vcm->main_pid = getpid ();
1370 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001371 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1372 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001373 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1374 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001375 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001376 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001377 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001378
1379 /* Allocate default worker */
1380 vcl_worker_alloc_and_init ();
1381
Florin Coras935ce752020-09-08 22:43:47 -07001382 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001383 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001384
1385 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001386 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001387
1388 return VPPCOM_OK;
1389}
1390
1391void
1392vppcom_app_destroy (void)
1393{
Florin Corasdc0ded72020-04-30 02:59:55 +00001394 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001395 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001396
Florin Coras940f78f2018-11-30 12:11:20 -08001397 if (!pool_elts (vcm->workers))
1398 return;
1399
Florin Coras0d427d82018-06-27 03:24:07 -07001400 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001401
Florin Corasdc0ded72020-04-30 02:59:55 +00001402 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001403
Florin Corasce815de2020-04-16 18:47:27 +00001404 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001405 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001406 if (current_wrk != wrk)
1407 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001408 }
Florin Corasce815de2020-04-16 18:47:27 +00001409 /* *INDENT-ON* */
1410
Florin Coras935ce752020-09-08 22:43:47 -07001411 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001412 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1413
Florin Coras0d427d82018-06-27 03:24:07 -07001414 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001415
1416 /*
1417 * Free the heap and fix vcm
1418 */
1419 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001420 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001421
1422 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001423 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001424}
1425
1426int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001427vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001428{
Florin Coras134a9962018-08-28 11:32:04 -07001429 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001430 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001431
Florin Coras134a9962018-08-28 11:32:04 -07001432 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001433
Florin Coras7e12d942018-06-27 14:32:43 -07001434 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001435 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001436 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001437 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001438
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001439 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001440 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001441
Florin Coras7e12d942018-06-27 14:32:43 -07001442 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1443 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001444
Florin Coras5e062572019-03-14 19:07:51 -07001445 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001446
Florin Coras134a9962018-08-28 11:32:04 -07001447 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001448}
1449
Florin Corasfe286f72021-06-04 10:07:55 -07001450static void
Florin Corasfa3884f2021-06-22 20:04:46 -07001451vcl_epoll_lt_add (vcl_worker_t *wrk, vcl_session_t *s)
Florin Corasfe286f72021-06-04 10:07:55 -07001452{
Florin Corasfa3884f2021-06-22 20:04:46 -07001453 vcl_session_t *cur, *prev;
Florin Corasfe286f72021-06-04 10:07:55 -07001454
Florin Corasfa3884f2021-06-22 20:04:46 -07001455 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
Florin Corasfe286f72021-06-04 10:07:55 -07001456 {
Florin Corasfa3884f2021-06-22 20:04:46 -07001457 wrk->ep_lt_current = s->session_index;
1458 s->vep.lt_next = s->session_index;
1459 s->vep.lt_prev = s->session_index;
1460 return;
Florin Corasfe286f72021-06-04 10:07:55 -07001461 }
Florin Corasfa3884f2021-06-22 20:04:46 -07001462
1463 cur = vcl_session_get (wrk, wrk->ep_lt_current);
1464 prev = vcl_session_get (wrk, cur->vep.lt_prev);
1465
1466 prev->vep.lt_next = s->session_index;
1467 s->vep.lt_prev = prev->session_index;
1468
1469 s->vep.lt_next = cur->session_index;
1470 cur->vep.lt_prev = s->session_index;
1471}
1472
1473static void
1474vcl_epoll_lt_del (vcl_worker_t *wrk, vcl_session_t *s)
1475{
1476 vcl_session_t *prev, *next;
1477
1478 if (s->vep.lt_next == s->session_index)
1479 {
1480 wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
1481 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
1482 return;
1483 }
1484
1485 prev = vcl_session_get (wrk, s->vep.lt_prev);
1486 next = vcl_session_get (wrk, s->vep.lt_next);
1487
1488 prev->vep.lt_next = next->session_index;
1489 next->vep.lt_prev = prev->session_index;
1490
1491 if (s->session_index == wrk->ep_lt_current)
1492 wrk->ep_lt_current = s->vep.lt_next;
1493
1494 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Corasfe286f72021-06-04 10:07:55 -07001495}
1496
Dave Wallace543852a2017-08-03 02:11:34 -04001497int
Florin Corasc127d5a2020-10-14 16:35:58 -07001498vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001499 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001500{
Florin Coras134a9962018-08-28 11:32:04 -07001501 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001502
Florin Coras6c3b2182020-10-19 18:36:48 -07001503 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001504
Florin Coras6c3b2182020-10-19 18:36:48 -07001505 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001506 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001507 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001508 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001509 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001510 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001511 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001512 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001513 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1514 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001515 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001516 }
Florin Corase4306b62020-10-28 12:51:10 -07001517 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001518 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001519
Florin Coras6c3b2182020-10-19 18:36:48 -07001520 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001521 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001522 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001523 if (rv < 0)
1524 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001525 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1526 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001527 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001528
Florin Coras9ace36d2019-10-28 13:14:17 -07001529 if (!do_disconnect)
1530 {
1531 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001532 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001533 goto cleanup;
1534 }
1535
Florin Coras6c3b2182020-10-19 18:36:48 -07001536 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001537 {
1538 rv = vppcom_session_unbind (sh);
1539 if (PREDICT_FALSE (rv < 0))
1540 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001541 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001542 vppcom_retval_str (rv));
1543 return rv;
1544 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001545 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001546 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001547 {
1548 rv = vppcom_session_disconnect (sh);
1549 if (PREDICT_FALSE (rv < 0))
1550 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001551 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001552 rv, vppcom_retval_str (rv));
1553 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001554 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001555 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001556 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001557 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001558 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001559 {
1560 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001561 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001562 goto free_session;
1563 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001564
Florin Corasc127d5a2020-10-14 16:35:58 -07001565 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001566
Florin Coras9ace36d2019-10-28 13:14:17 -07001567 /* Session is removed only after vpp confirms the disconnect */
1568 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001569
Florin Corasf9240dc2019-01-15 08:03:17 -08001570cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001571 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001572free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001573 vcl_session_free (wrk, s);
1574 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001575
Dave Wallace543852a2017-08-03 02:11:34 -04001576 return rv;
1577}
1578
1579int
Florin Corasf9240dc2019-01-15 08:03:17 -08001580vppcom_session_close (uint32_t session_handle)
1581{
1582 vcl_worker_t *wrk = vcl_worker_get_current ();
1583 vcl_session_t *session;
1584
1585 session = vcl_session_get_w_handle (wrk, session_handle);
1586 if (!session)
1587 return VPPCOM_EBADFD;
1588 return vcl_session_cleanup (wrk, session, session_handle,
1589 1 /* do_disconnect */ );
1590}
1591
1592int
Florin Coras134a9962018-08-28 11:32:04 -07001593vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001594{
Florin Coras134a9962018-08-28 11:32:04 -07001595 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001596 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001597
1598 if (!ep || !ep->ip)
1599 return VPPCOM_EINVAL;
1600
Florin Coras134a9962018-08-28 11:32:04 -07001601 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001602 if (!session)
1603 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001604
Florin Coras6c3b2182020-10-19 18:36:48 -07001605 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001606 {
Florin Coras5e062572019-03-14 19:07:51 -07001607 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1608 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001609 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001610 }
1611
Florin Coras7e12d942018-06-27 14:32:43 -07001612 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001613 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001614 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1615 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001616 else
Dave Barach178cf492018-11-13 16:34:13 -05001617 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1618 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001619 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001620
Florin Coras5e062572019-03-14 19:07:51 -07001621 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1622 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001623 session->transport.is_ip4 ? "IPv4" : "IPv6",
1624 format_ip46_address, &session->transport.lcl_ip,
1625 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1626 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001627 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001628 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001629
1630 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001631 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001632
Florin Coras070453d2018-08-24 17:04:27 -07001633 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001634}
1635
1636int
Florin Coras134a9962018-08-28 11:32:04 -07001637vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001638{
Florin Coras134a9962018-08-28 11:32:04 -07001639 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001640 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001641 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001642 int rv;
1643
Florin Coras134a9962018-08-28 11:32:04 -07001644 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001645 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001646 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001647
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001648 if (q_len == 0 || q_len == ~0)
1649 q_len = vcm->cfg.listen_queue_size;
1650
Dave Wallaceee45d412017-11-24 21:44:06 -05001651 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001652 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001653 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001654 VDBG (0, "session %u [0x%llx]: already in listen state!",
1655 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001656 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001657 }
1658
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001659 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001660
Florin Coras070453d2018-08-24 17:04:27 -07001661 /*
1662 * Send listen request to vpp and wait for reply
1663 */
Florin Coras458089b2019-08-21 16:20:44 -07001664 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001665 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001666 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001667 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001668
Florin Coras070453d2018-08-24 17:04:27 -07001669 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001670 {
Florin Coras134a9962018-08-28 11:32:04 -07001671 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001672 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1673 listen_sh, listen_session->vpp_handle, rv,
1674 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001675 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001676 }
1677
Florin Coras070453d2018-08-24 17:04:27 -07001678 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001679}
1680
Florin Coras134a9962018-08-28 11:32:04 -07001681static int
Florin Coras5e062572019-03-14 19:07:51 -07001682validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001683{
Florin Coras6c3b2182020-10-19 18:36:48 -07001684 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001685 {
Florin Coras5e062572019-03-14 19:07:51 -07001686 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1687 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001688 return VPPCOM_EBADFD;
1689 }
1690
Florin Corasc127d5a2020-10-14 16:35:58 -07001691 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001692 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001693 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001694 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001695 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001696 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001697 return VPPCOM_EBADFD;
1698 }
1699 return VPPCOM_OK;
1700}
1701
1702int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001703vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1704{
1705 if (!strcmp (proto_str, "TCP"))
1706 *proto = VPPCOM_PROTO_TCP;
1707 else if (!strcmp (proto_str, "tcp"))
1708 *proto = VPPCOM_PROTO_TCP;
1709 else if (!strcmp (proto_str, "UDP"))
1710 *proto = VPPCOM_PROTO_UDP;
1711 else if (!strcmp (proto_str, "udp"))
1712 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001713 else if (!strcmp (proto_str, "TLS"))
1714 *proto = VPPCOM_PROTO_TLS;
1715 else if (!strcmp (proto_str, "tls"))
1716 *proto = VPPCOM_PROTO_TLS;
1717 else if (!strcmp (proto_str, "QUIC"))
1718 *proto = VPPCOM_PROTO_QUIC;
1719 else if (!strcmp (proto_str, "quic"))
1720 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001721 else if (!strcmp (proto_str, "DTLS"))
1722 *proto = VPPCOM_PROTO_DTLS;
1723 else if (!strcmp (proto_str, "dtls"))
1724 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001725 else if (!strcmp (proto_str, "SRTP"))
1726 *proto = VPPCOM_PROTO_SRTP;
1727 else if (!strcmp (proto_str, "srtp"))
1728 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001729 else
1730 return 1;
1731 return 0;
1732}
1733
1734int
Florin Coras134a9962018-08-28 11:32:04 -07001735vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001736 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001737{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001738 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001739 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001740 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001741 vcl_session_t *listen_session = 0;
1742 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001743 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001744 u8 is_nonblocking;
1745 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001746
Florin Coras5398dfb2021-01-25 20:31:27 -08001747again:
1748
Florin Coras134a9962018-08-28 11:32:04 -07001749 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001750 if (!listen_session)
1751 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001752
Florin Coras134a9962018-08-28 11:32:04 -07001753 listen_session_index = listen_session->session_index;
1754 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001755 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001756
Florin Coras54693d22018-07-17 10:46:29 -07001757 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001758 {
Florin Coras54693d22018-07-17 10:46:29 -07001759 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001760 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001761 accepted_msg = evt->accepted_msg;
1762 goto handle;
1763 }
1764
Florin Corasac422d62020-10-19 20:51:36 -07001765 is_nonblocking = vcl_session_has_attr (listen_session,
1766 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001767 while (1)
1768 {
Carl Smith592a9092019-11-12 14:57:37 +13001769 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1770 return VPPCOM_EAGAIN;
1771
Florin Coras5398dfb2021-01-25 20:31:27 -08001772 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1773 vcl_worker_flush_mq_events (wrk);
1774 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001775 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001776
Florin Coras54693d22018-07-17 10:46:29 -07001777handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001778
Florin Coras00cca802019-06-06 09:38:44 -07001779 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1780 listen_session_index);
1781 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1782 return VPPCOM_ECONNABORTED;
1783
Florin Coras134a9962018-08-28 11:32:04 -07001784 listen_session = vcl_session_get (wrk, listen_session_index);
1785 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001786
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001787 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001788 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001789
Florin Coras5e062572019-03-14 19:07:51 -07001790 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1791 " flags %d, is_nonblocking %u", listen_session->session_index,
1792 listen_session->vpp_handle, client_session_index,
1793 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001794 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001795
Dave Wallace048b1d62018-01-03 22:24:41 -05001796 if (ep)
1797 {
Florin Coras7e12d942018-06-27 14:32:43 -07001798 ep->is_ip4 = client_session->transport.is_ip4;
1799 ep->port = client_session->transport.rmt_port;
1800 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001801 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1802 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001803 else
Dave Barach178cf492018-11-13 16:34:13 -05001804 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1805 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001806 }
Dave Wallace60caa062017-11-10 17:07:13 -05001807
Florin Coras05ecfcc2018-12-12 18:19:39 -08001808 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001809 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001810 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001811 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001812 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001813 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001814 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001815 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001816 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001817 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1818 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001819
Florin Coras3c7d4f92018-12-14 11:28:43 -08001820 /*
1821 * Session might have been closed already
1822 */
1823 if (accept_flags)
1824 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001825 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001826 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001827 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001828 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001829 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001830 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001831}
1832
1833int
Florin Coras134a9962018-08-28 11:32:04 -07001834vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001835{
Florin Coras134a9962018-08-28 11:32:04 -07001836 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001837 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001838 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001839 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001840
Florin Coras134a9962018-08-28 11:32:04 -07001841 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001842 if (!session)
1843 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001844 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001845
Florin Coras6c3b2182020-10-19 18:36:48 -07001846 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001847 {
Florin Coras5e062572019-03-14 19:07:51 -07001848 VDBG (0, "ERROR: cannot connect epoll session %u!",
1849 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001850 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001851 }
1852
Florin Corasc127d5a2020-10-14 16:35:58 -07001853 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001854 {
Florin Coras7baeb712019-01-04 17:05:43 -08001855 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001856 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001857 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001858 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001859 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001860 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001861 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001862 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001863 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001864 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001865 }
1866
Florin Coras0a1e1832020-03-29 18:54:04 +00001867 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001868 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001869 {
1870 if (session->session_type != VPPCOM_PROTO_UDP)
1871 return VPPCOM_EINVAL;
1872 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001873 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001874 }
1875
Florin Coras7e12d942018-06-27 14:32:43 -07001876 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001877 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001878 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001879 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001880 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001881
Florin Coras0a1e1832020-03-29 18:54:04 +00001882 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001883 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001884 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001885 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001886 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001887 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001888 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001889 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001890 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001891
Florin Coras458089b2019-08-21 16:20:44 -07001892 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001893
Florin Corasac422d62020-10-19 20:51:36 -07001894 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001895 {
fanyfa49d59d2020-10-13 17:07:16 +08001896 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001897 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001898 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001899 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001900 return VPPCOM_EINPROGRESS;
1901 }
Florin Coras57c88932019-08-29 12:03:17 -07001902
1903 /*
1904 * Wait for reply from vpp if blocking
1905 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001906 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001907 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001908
Florin Coras134a9962018-08-28 11:32:04 -07001909 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001910 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1911 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001912
Dave Wallace4878cbe2017-11-21 03:45:09 -05001913 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001914}
1915
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001916int
1917vppcom_session_stream_connect (uint32_t session_handle,
1918 uint32_t parent_session_handle)
1919{
1920 vcl_worker_t *wrk = vcl_worker_get_current ();
1921 vcl_session_t *session, *parent_session;
1922 u32 session_index, parent_session_index;
1923 int rv;
1924
1925 session = vcl_session_get_w_handle (wrk, session_handle);
1926 if (!session)
1927 return VPPCOM_EBADFD;
1928 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1929 if (!parent_session)
1930 return VPPCOM_EBADFD;
1931
1932 session_index = session->session_index;
1933 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001934 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001935 {
1936 VDBG (0, "ERROR: cannot connect epoll session %u!",
1937 session->session_index);
1938 return VPPCOM_EBADFD;
1939 }
1940
Florin Corasc127d5a2020-10-14 16:35:58 -07001941 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001942 {
1943 VDBG (0, "session handle %u [0x%llx]: session already "
1944 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1945 session_handle, session->vpp_handle,
1946 parent_session_handle, parent_session->vpp_handle,
1947 vppcom_proto_str (session->session_type), session->session_state,
1948 vppcom_session_state_str (session->session_state));
1949 return VPPCOM_OK;
1950 }
1951
1952 /* Connect to quic session specifics */
1953 session->transport.is_ip4 = parent_session->transport.is_ip4;
1954 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1955 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001956 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001957
1958 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1959 session_handle, parent_session_handle, parent_session->vpp_handle);
1960
1961 /*
1962 * Send connect request and wait for reply from vpp
1963 */
Florin Coras458089b2019-08-21 16:20:44 -07001964 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001965 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001966 vcm->cfg.session_timeout);
1967
1968 session->listener_index = parent_session_index;
1969 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001970 if (parent_session)
1971 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001972
1973 session = vcl_session_get (wrk, session_index);
1974 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1975 session->vpp_handle, rv ? "failed" : "succeeded");
1976
1977 return rv;
1978}
1979
Steven58f464e2017-10-25 12:33:12 -07001980static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001981vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001982 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001983{
Florin Coras134a9962018-08-28 11:32:04 -07001984 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001985 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001986 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001987 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001988 session_event_t *e;
1989 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001990 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001991
Florin Coras070453d2018-08-24 17:04:27 -07001992 if (PREDICT_FALSE (!buf))
wanghanlin72228a22021-07-06 17:18:29 +08001993 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001994
Florin Coras134a9962018-08-28 11:32:04 -07001995 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001996 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001997 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001998
Florin Coras6d0106e2019-01-29 20:11:58 -08001999 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002000 {
Florin Coras5e062572019-03-14 19:07:51 -07002001 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08002002 s->session_index, s->vpp_handle, s->session_state,
2003 vppcom_session_state_str (s->session_state));
2004 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002005 }
2006
liuyacan55c952e2021-06-13 14:54:55 +08002007 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
2008 {
2009 /* Vpp would ack the incoming data and enqueue it for reading.
2010 * So even if SHUT_RD is set, we can still read() the data if
2011 * the session is ready.
2012 */
2013 if (!vcl_session_read_ready (s))
2014 {
2015 return 0;
2016 }
2017 }
2018
Florin Corasac422d62020-10-19 20:51:36 -07002019 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07002020 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002021 mq = wrk->app_event_queue;
2022 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002023 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002024
Sirshak Das28aa5392019-02-05 01:33:33 -06002025 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002026 {
Florin Coras54693d22018-07-17 10:46:29 -07002027 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08002028 {
Yu Pingb2955352019-12-04 06:49:04 +08002029 if (vcl_session_is_closing (s))
2030 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07002031 if (is_ct)
2032 svm_fifo_unset_event (s->rx_fifo);
2033 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08002034 return VPPCOM_EWOULDBLOCK;
2035 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002036 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002037 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002038 if (vcl_session_is_closing (s))
2039 return vcl_session_closing_error (s);
2040
Florin Corasd6894562020-10-28 00:37:15 -07002041 if (is_ct)
2042 svm_fifo_unset_event (s->rx_fifo);
2043 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002044
Florin Coras5398dfb2021-01-25 20:31:27 -08002045 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2046 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002047 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002048 }
Florin Coras54693d22018-07-17 10:46:29 -07002049
Florin Coras4a2c7942020-09-10 12:27:14 -07002050read_again:
2051
Florin Coras460dce62018-07-27 05:45:06 -07002052 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002053 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002054 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002055 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2056
2057 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002058
2059 if (peek)
2060 return rv;
2061
Florin Coras4a2c7942020-09-10 12:27:14 -07002062 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002063
Sirshak Das28aa5392019-02-05 01:33:33 -06002064 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002065 {
Florin Corasd6894562020-10-28 00:37:15 -07002066 if (is_ct)
2067 svm_fifo_unset_event (s->rx_fifo);
2068 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002069 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002070 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002071 {
Florin Corasc34118b2020-08-12 18:58:25 -07002072 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2073 e->event_type = SESSION_IO_EVT_RX;
2074 e->session_index = s->session_index;
2075 }
2076 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002077 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002078 {
2079 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002080 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002081 buf += rv;
2082 n -= rv;
2083 goto read_again;
2084 }
Florin Coras41c9e042018-09-11 00:10:41 -07002085
Florin Coras17ec5772020-08-12 20:46:29 -07002086 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002087 {
Florin Coras17ec5772020-08-12 20:46:29 -07002088 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002089 app_send_io_evt_to_vpp (s->vpp_evt_q,
2090 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002091 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002092 }
2093
Florin Coras5e062572019-03-14 19:07:51 -07002094 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2095 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002096
Florin Coras54693d22018-07-17 10:46:29 -07002097 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002098}
2099
Steven58f464e2017-10-25 12:33:12 -07002100int
Florin Coras134a9962018-08-28 11:32:04 -07002101vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002102{
Florin Coras134a9962018-08-28 11:32:04 -07002103 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002104}
2105
2106static int
Florin Coras134a9962018-08-28 11:32:04 -07002107vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002108{
Florin Coras134a9962018-08-28 11:32:04 -07002109 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002110}
2111
Florin Coras2cba8532018-09-11 16:33:36 -07002112int
2113vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002114 vppcom_data_segment_t * ds, uint32_t n_segments,
2115 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002116{
2117 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002118 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002119 vcl_session_t *s = 0;
2120 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002121 svm_msg_q_t *mq;
2122 u8 is_ct;
2123
2124 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002125 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002126 return VPPCOM_EBADFD;
2127
Florin Coras6d0106e2019-01-29 20:11:58 -08002128 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2129 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002130
Florin Corasac422d62020-10-19 20:51:36 -07002131 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002132 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002133 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002134 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002135 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002136
Sirshak Das28aa5392019-02-05 01:33:33 -06002137 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002138 {
2139 if (is_nonblocking)
2140 {
Florin Coras62877022020-10-28 21:22:04 -07002141 if (is_ct)
2142 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002143 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002144 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002145 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002146 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002147 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002148 if (vcl_session_is_closing (s))
2149 return vcl_session_closing_error (s);
2150
Florin Coras62877022020-10-28 21:22:04 -07002151 if (is_ct)
2152 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002153 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002154
Florin Coras5398dfb2021-01-25 20:31:27 -08002155 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2156 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002157 }
2158 }
2159
Florin Corasd1cc38d2020-10-11 11:05:04 -07002160 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2161 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2162 if (n_read < 0)
2163 return VPPCOM_EAGAIN;
2164
2165 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2166 {
Florin Coras62877022020-10-28 21:22:04 -07002167 if (is_ct)
2168 svm_fifo_unset_event (s->rx_fifo);
2169 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002170 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002171 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002172 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002173 {
2174 session_event_t *e;
2175 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2176 e->event_type = SESSION_IO_EVT_RX;
2177 e->session_index = s->session_index;
2178 }
2179 }
2180
2181 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002182 return n_read;
2183}
2184
2185void
Florin Corasd68faf82020-09-25 15:18:13 -07002186vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002187{
2188 vcl_worker_t *wrk = vcl_worker_get_current ();
2189 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002190 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002191
2192 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002193 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002194 return;
2195
Florin Coras62877022020-10-28 21:22:04 -07002196 is_ct = vcl_session_is_ct (s);
2197 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002198
Florin Coras8cc93212021-09-10 11:37:12 -07002199 ASSERT (s->rx_bytes_pending >= n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002200 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002201}
2202
Florin Coras7a2abce2020-04-05 19:25:44 +00002203always_inline u8
2204vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002205{
Florin Coras7a2abce2020-04-05 19:25:44 +00002206 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2207 if (is_dgram)
2208 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2209 else
2210 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002211}
2212
2213always_inline int
2214vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2215 size_t n, u8 is_flush, u8 is_dgram)
2216{
Florin Coras6d0106e2019-01-29 20:11:58 -08002217 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002218 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002219 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002220 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002221 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002222
Florin Coras0b0d28e2021-06-04 17:31:53 -07002223 /* Accept zero length writes but just return */
2224 if (PREDICT_FALSE (!n))
2225 return VPPCOM_OK;
2226
2227 if (PREDICT_FALSE (!buf))
2228 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002229
Florin Coras6c3b2182020-10-19 18:36:48 -07002230 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002231 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002232 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2233 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002234 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002235 }
2236
liuyacan55c952e2021-06-13 14:54:55 +08002237 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002238 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002239 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2240 s->session_index, s->vpp_handle, s->session_state,
2241 vppcom_session_state_str (s->session_state));
2242 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002243 }
2244
liuyacan55c952e2021-06-13 14:54:55 +08002245 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2246 {
2247 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2248 s->session_index, s->vpp_handle, s->session_state,
2249 vppcom_session_state_str (s->session_state));
2250 return VPPCOM_EPIPE;
2251 }
2252
Florin Coras0e88e852018-09-17 22:09:02 -07002253 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002254 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002255 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002256
Florin Coras653e43f2019-03-04 10:56:23 -08002257 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002258 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002259 {
Florin Coras54693d22018-07-17 10:46:29 -07002260 if (is_nonblocking)
2261 {
Florin Coras070453d2018-08-24 17:04:27 -07002262 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002263 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002264 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002265 {
Florin Coras2d379d82019-06-28 12:45:12 -07002266 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002267 if (vcl_session_is_closing (s))
2268 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002269
Florin Coras5398dfb2021-01-25 20:31:27 -08002270 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2271 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002272 }
Dave Wallace543852a2017-08-03 02:11:34 -04002273 }
Dave Wallace543852a2017-08-03 02:11:34 -04002274
Florin Coras653e43f2019-03-04 10:56:23 -08002275 et = SESSION_IO_EVT_TX;
2276 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002277 et = SESSION_IO_EVT_TX_FLUSH;
2278
Florin Coras7a2abce2020-04-05 19:25:44 +00002279 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002280 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002281 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002282 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002283 else
2284 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002285 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002286
Florin Coras14ed6df2019-03-06 21:13:42 -08002287 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002288 app_send_io_evt_to_vpp (
2289 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002290
Florin Coras56230092020-09-02 20:52:58 -07002291 /* The underlying fifo segment can run out of memory */
2292 if (PREDICT_FALSE (n_write < 0))
2293 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002294
Florin Coras6d0106e2019-01-29 20:11:58 -08002295 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2296 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002297
Florin Coras54693d22018-07-17 10:46:29 -07002298 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002299}
2300
Florin Coras42ceddb2018-12-12 10:56:01 -08002301int
2302vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2303{
Florin Coras7a2abce2020-04-05 19:25:44 +00002304 vcl_worker_t *wrk = vcl_worker_get_current ();
2305 vcl_session_t *s;
2306
2307 s = vcl_session_get_w_handle (wrk, session_handle);
2308 if (PREDICT_FALSE (!s))
2309 return VPPCOM_EBADFD;
2310
2311 return vppcom_session_write_inline (wrk, s, buf, n,
2312 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002313}
2314
Florin Corasb0f662f2018-12-27 14:51:46 -08002315int
2316vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2317{
Florin Coras7a2abce2020-04-05 19:25:44 +00002318 vcl_worker_t *wrk = vcl_worker_get_current ();
2319 vcl_session_t *s;
2320
2321 s = vcl_session_get_w_handle (wrk, session_handle);
2322 if (PREDICT_FALSE (!s))
2323 return VPPCOM_EBADFD;
2324
2325 return vppcom_session_write_inline (wrk, s, buf, n,
2326 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002327}
2328
Florin Corasc0737e92019-03-04 14:19:39 -08002329#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002330if (PREDICT_FALSE (!_s->rx_fifo)) \
2331 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002332if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2333 { \
2334 if (!vcl_session_is_ct (_s)) \
2335 { \
2336 svm_fifo_unset_event (_s->rx_fifo); \
2337 if (svm_fifo_is_empty (_s->rx_fifo)) \
2338 break; \
2339 } \
2340 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2341 { \
Florin Coras2f630182020-08-13 00:17:51 -07002342 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002343 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2344 break; \
2345 } \
2346 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002347
Florin Coras86f04502018-09-12 16:08:01 -07002348static void
2349vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2350 unsigned long n_bits, unsigned long *read_map,
2351 unsigned long *write_map,
2352 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002353{
2354 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002355 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002356 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002357 u32 sid;
2358
2359 switch (e->event_type)
2360 {
Florin Corasc0737e92019-03-04 14:19:39 -08002361 case SESSION_IO_EVT_RX:
2362 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002363 s = vcl_session_get (wrk, sid);
2364 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002365 break;
Florin Corasb242d312020-10-26 15:35:40 -07002366 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002367 if (sid < n_bits && read_map)
2368 {
David Johnsond9818dd2018-12-14 14:53:41 -05002369 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002370 *bits_set += 1;
2371 }
2372 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002373 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002374 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002375 s = vcl_session_get (wrk, sid);
2376 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002377 break;
2378 if (sid < n_bits && write_map)
2379 {
David Johnsond9818dd2018-12-14 14:53:41 -05002380 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002381 *bits_set += 1;
2382 }
2383 break;
Florin Coras86f04502018-09-12 16:08:01 -07002384 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002385 if (!e->postponed)
2386 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2387 else
2388 s = vcl_session_get (wrk, e->session_index);
2389 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002390 break;
Florin Corasb242d312020-10-26 15:35:40 -07002391 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002392 if (sid < n_bits && read_map)
2393 {
David Johnsond9818dd2018-12-14 14:53:41 -05002394 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002395 *bits_set += 1;
2396 }
2397 break;
2398 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002399 if (!e->postponed)
2400 {
2401 connected_msg = (session_connected_msg_t *) e->data;
2402 sid = vcl_session_connected_handler (wrk, connected_msg);
2403 }
2404 else
2405 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002406 if (sid == VCL_INVALID_SESSION_INDEX)
2407 break;
2408 if (sid < n_bits && write_map)
2409 {
2410 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2411 *bits_set += 1;
2412 }
Florin Coras86f04502018-09-12 16:08:01 -07002413 break;
2414 case SESSION_CTRL_EVT_DISCONNECTED:
2415 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002416 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2417 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002418 break;
Florin Corasb242d312020-10-26 15:35:40 -07002419 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002420 if (sid < n_bits && except_map)
2421 {
David Johnsond9818dd2018-12-14 14:53:41 -05002422 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002423 *bits_set += 1;
2424 }
2425 break;
2426 case SESSION_CTRL_EVT_RESET:
2427 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2428 if (sid < n_bits && except_map)
2429 {
David Johnsond9818dd2018-12-14 14:53:41 -05002430 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002431 *bits_set += 1;
2432 }
2433 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002434 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2435 vcl_session_unlisten_reply_handler (wrk, e->data);
2436 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002437 case SESSION_CTRL_EVT_MIGRATED:
2438 vcl_session_migrated_handler (wrk, e->data);
2439 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002440 case SESSION_CTRL_EVT_CLEANUP:
2441 vcl_session_cleanup_handler (wrk, e->data);
2442 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002443 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2444 vcl_session_worker_update_reply_handler (wrk, e->data);
2445 break;
2446 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2447 vcl_session_req_worker_update_handler (wrk, e->data);
2448 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002449 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2450 vcl_session_app_add_segment_handler (wrk, e->data);
2451 break;
2452 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2453 vcl_session_app_del_segment_handler (wrk, e->data);
2454 break;
hanlina3a48962020-07-13 11:09:15 +08002455 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002456 vcl_worker_rpc_handler (wrk, e->data);
2457 break;
Florin Coras86f04502018-09-12 16:08:01 -07002458 default:
2459 clib_warning ("unhandled: %u", e->event_type);
2460 break;
2461 }
2462}
2463
2464static int
2465vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2466 unsigned long n_bits, unsigned long *read_map,
2467 unsigned long *write_map, unsigned long *except_map,
2468 double time_to_wait, u32 * bits_set)
2469{
Florin Coras99368312018-08-02 10:45:44 -07002470 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002471 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002472 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002473
Florin Coras54693d22018-07-17 10:46:29 -07002474 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002475 {
Florin Coras54693d22018-07-17 10:46:29 -07002476 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002477 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002478
Florin Coras54693d22018-07-17 10:46:29 -07002479 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002480 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002481 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002482 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002483 else
2484 {
2485 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002486 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002487 }
2488 }
Florin Corase003a1b2019-06-05 10:47:16 -07002489 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002490
Florin Coras134a9962018-08-28 11:32:04 -07002491 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002492 {
Florin Coras134a9962018-08-28 11:32:04 -07002493 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002494 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002495 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2496 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002497 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002498 }
Florin Coras134a9962018-08-28 11:32:04 -07002499 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002500 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002501 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002502}
2503
Florin Coras99368312018-08-02 10:45:44 -07002504static int
Florin Coras294afe22019-01-07 17:49:17 -08002505vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2506 vcl_si_set * read_map, vcl_si_set * write_map,
2507 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002508 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002509{
Florin Coras14ed6df2019-03-06 21:13:42 -08002510 double wait = 0, start = 0;
2511
2512 if (!*bits_set)
2513 {
2514 wait = time_to_wait;
2515 start = clib_time_now (&wrk->clib_time);
2516 }
2517
2518 do
2519 {
2520 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2521 write_map, except_map, wait, bits_set);
2522 if (*bits_set)
2523 return *bits_set;
2524 if (wait == -1)
2525 continue;
2526
2527 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2528 }
2529 while (wait > 0);
2530
2531 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002532}
2533
2534static int
Florin Coras294afe22019-01-07 17:49:17 -08002535vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2536 vcl_si_set * read_map, vcl_si_set * write_map,
2537 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002538 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002539{
2540 vcl_mq_evt_conn_t *mqc;
2541 int __clib_unused n_read;
2542 int n_mq_evts, i;
2543 u64 buf;
2544
Florin Coras134a9962018-08-28 11:32:04 -07002545 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2546 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2547 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002548 for (i = 0; i < n_mq_evts; i++)
2549 {
Florin Coras134a9962018-08-28 11:32:04 -07002550 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002551 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002552 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002553 except_map, 0, bits_set);
2554 }
2555
2556 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2557}
2558
Dave Wallace543852a2017-08-03 02:11:34 -04002559int
Florin Coras294afe22019-01-07 17:49:17 -08002560vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2561 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002562{
Florin Coras54693d22018-07-17 10:46:29 -07002563 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002564 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002565 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002566 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002567
Dave Wallace7876d392017-10-19 03:53:57 -04002568 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002569 {
Florin Coras134a9962018-08-28 11:32:04 -07002570 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002571 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002572 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2573 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002574 }
Dave Wallace7876d392017-10-19 03:53:57 -04002575 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002576 {
Florin Coras134a9962018-08-28 11:32:04 -07002577 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002578 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002579 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2580 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002581 }
Dave Wallace7876d392017-10-19 03:53:57 -04002582 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002583 {
Florin Coras134a9962018-08-28 11:32:04 -07002584 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002585 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002586 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2587 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002588 }
2589
Florin Coras54693d22018-07-17 10:46:29 -07002590 if (!n_bits)
2591 return 0;
2592
2593 if (!write_map)
2594 goto check_rd;
2595
Florin Coras096a1d52021-01-27 15:33:51 -08002596 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2597 {
2598 if (!(s = vcl_session_get (wrk, sid)))
2599 {
2600 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2601 bits_set++;
2602 continue;
2603 }
Florin Coras54693d22018-07-17 10:46:29 -07002604
Florin Coras096a1d52021-01-27 15:33:51 -08002605 if (vcl_session_write_ready (s))
2606 {
2607 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2608 bits_set++;
2609 }
2610 else
2611 {
2612 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2613 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2614 }
2615 }
Florin Coras54693d22018-07-17 10:46:29 -07002616
2617check_rd:
2618 if (!read_map)
2619 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002620
Florin Coras096a1d52021-01-27 15:33:51 -08002621 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2622 {
2623 if (!(s = vcl_session_get (wrk, sid)))
2624 {
2625 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2626 bits_set++;
2627 continue;
2628 }
Florin Coras54693d22018-07-17 10:46:29 -07002629
Florin Coras096a1d52021-01-27 15:33:51 -08002630 if (vcl_session_read_ready (s))
2631 {
2632 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2633 bits_set++;
2634 }
2635 }
Florin Coras54693d22018-07-17 10:46:29 -07002636
2637check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002638
Florin Coras86f04502018-09-12 16:08:01 -07002639 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2640 {
2641 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2642 read_map, write_map, except_map, &bits_set);
2643 }
2644 vec_reset_length (wrk->unhandled_evts_vector);
2645
Florin Coras99368312018-08-02 10:45:44 -07002646 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002647 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002648 time_to_wait, &bits_set);
2649 else
Florin Coras134a9962018-08-28 11:32:04 -07002650 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002651 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002652
Dave Wallace543852a2017-08-03 02:11:34 -04002653 return (bits_set);
2654}
2655
Dave Wallacef7f809c2017-10-03 01:48:42 -04002656static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002657vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002658{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002659 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002660 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002661 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002662
Florin Corasc0737e92019-03-04 14:19:39 -08002663 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002664 return;
2665
Florin Coras6c3b2182020-10-19 18:36:48 -07002666 s = vcl_session_get_w_handle (wrk, vep_handle);
2667 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002668 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002669 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002670 goto done;
2671 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002672 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002673 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002674 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675 goto done;
2676 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002677 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002678 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002679 "{\n"
2680 " is_vep = %u\n"
2681 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002682 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002683 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2684 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002685
Florin Coras7e5e62b2019-07-30 14:08:23 -07002686 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002688 s = vcl_session_get_w_handle (wrk, sh);
2689 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002691 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002692 goto done;
2693 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002694 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002695 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002696 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002697 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002698 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002699 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002700 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002701 goto done;
2702 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002703 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002704 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2705 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002706 sh, s->vep.vep_sh, vep_handle);
2707 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002708 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002709 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002710 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002711 " next_sh = 0x%x (%u)\n"
2712 " prev_sh = 0x%x (%u)\n"
2713 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002714 " ev.events = 0x%x\n"
2715 " ev.data.u64 = 0x%llx\n"
2716 " et_mask = 0x%x\n"
2717 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002718 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002719 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2720 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002721 }
2722 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002723
2724done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002725 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002726}
2727
2728int
2729vppcom_epoll_create (void)
2730{
Florin Coras134a9962018-08-28 11:32:04 -07002731 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002732 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002733
Florin Coras134a9962018-08-28 11:32:04 -07002734 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002735
Florin Coras6c3b2182020-10-19 18:36:48 -07002736 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002737 vep_session->vep.vep_sh = ~0;
2738 vep_session->vep.next_sh = ~0;
2739 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002740 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002741
Florin Corasa7a1a222018-12-30 17:11:31 -08002742 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2743 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002744
Florin Corasab2f6db2018-08-31 14:31:41 -07002745 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746}
2747
2748int
Florin Coras134a9962018-08-28 11:32:04 -07002749vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002750 struct epoll_event *event)
2751{
Florin Coras134a9962018-08-28 11:32:04 -07002752 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002753 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002754 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002755 vcl_session_t *s;
2756 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757
Florin Coras134a9962018-08-28 11:32:04 -07002758 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002759 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002760 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002761 return VPPCOM_EINVAL;
2762 }
2763
Florin Coras134a9962018-08-28 11:32:04 -07002764 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002765 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002766 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002767 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002768 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002770 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002772 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002773 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002774 }
2775
Florin Coras134a9962018-08-28 11:32:04 -07002776 ASSERT (vep_session->vep.vep_sh == ~0);
2777 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002778
Florin Coras17ec5772020-08-12 20:46:29 -07002779 s = vcl_session_get_w_handle (wrk, session_handle);
2780 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002781 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002782 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002783 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002784 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002785 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002786 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002787 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002788 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789 }
2790
2791 switch (op)
2792 {
2793 case EPOLL_CTL_ADD:
2794 if (PREDICT_FALSE (!event))
2795 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002796 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002797 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002798 }
Florin Coras2645f682021-06-04 15:59:41 -07002799 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2800 {
2801 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2802 rv = VPPCOM_EEXIST;
2803 goto done;
2804 }
Florin Coras134a9962018-08-28 11:32:04 -07002805 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002806 {
Florin Coras7e12d942018-06-27 14:32:43 -07002807 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002808 next_session = vcl_session_get_w_handle (wrk,
2809 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002810 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002811 {
Florin Coras5e062572019-03-14 19:07:51 -07002812 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002813 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002814 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002815 }
Florin Coras134a9962018-08-28 11:32:04 -07002816 ASSERT (next_session->vep.prev_sh == vep_handle);
2817 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002818 }
Florin Coras17ec5772020-08-12 20:46:29 -07002819 s->vep.next_sh = vep_session->vep.next_sh;
2820 s->vep.prev_sh = vep_handle;
2821 s->vep.vep_sh = vep_handle;
2822 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
Florin Corasfa3884f2021-06-22 20:04:46 -07002823 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras17ec5772020-08-12 20:46:29 -07002824 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002825 s->flags &= ~VCL_SESSION_F_IS_VEP;
2826 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002827 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002828
Florin Coras17ec5772020-08-12 20:46:29 -07002829 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2830 if (txf && (event->events & EPOLLOUT))
2831 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002832
Florin Coras6e3c1f82020-01-15 01:30:46 +00002833 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002834 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002835 {
2836 session_event_t e = { 0 };
2837 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002838 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002839 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002840 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002841 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002842 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002843 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002844 {
2845 session_event_t e = { 0 };
2846 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002847 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002848 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002849 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2850 add_evt = 1;
2851 }
2852 if (!add_evt && vcl_session_is_closing (s))
2853 {
2854 session_event_t e = { 0 };
2855 if (s->session_state == VCL_STATE_VPP_CLOSING)
2856 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2857 else
2858 e.event_type = SESSION_CTRL_EVT_RESET;
2859 e.session_index = s->session_index;
2860 e.postponed = 1;
2861 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002862 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002863 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2864 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002865 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002866 break;
2867
2868 case EPOLL_CTL_MOD:
2869 if (PREDICT_FALSE (!event))
2870 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002871 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002872 rv = VPPCOM_EINVAL;
2873 goto done;
2874 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002875 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002876 {
Florin Coras5e062572019-03-14 19:07:51 -07002877 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002878 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002879 goto done;
2880 }
Florin Coras17ec5772020-08-12 20:46:29 -07002881 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002882 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002883 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002884 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002885 rv = VPPCOM_EINVAL;
2886 goto done;
2887 }
hanlin475c9d72019-12-26 11:44:28 +08002888
Florin Coras2645f682021-06-04 15:59:41 -07002889 /* Generate EPOLLOUT if session write ready nd event was not on */
2890 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2891 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002892 {
2893 session_event_t e = { 0 };
2894 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002895 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002896 vec_add1 (wrk->unhandled_evts_vector, e);
2897 }
Florin Coras2645f682021-06-04 15:59:41 -07002898 /* Generate EPOLLIN if session read ready and event was not on */
2899 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2900 (vcl_session_read_ready (s) > 0))
2901 {
2902 session_event_t e = { 0 };
2903 e.event_type = SESSION_IO_EVT_RX;
2904 e.session_index = s->session_index;
2905 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002906 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002907 }
Florin Coras17ec5772020-08-12 20:46:29 -07002908 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2909 s->vep.ev = *event;
2910 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002911 if (txf)
2912 {
2913 if (event->events & EPOLLOUT)
2914 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2915 else
2916 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2917 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002918 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2919 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002920 break;
2921
2922 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002923 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002924 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002925 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002926 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002927 goto done;
2928 }
Florin Coras17ec5772020-08-12 20:46:29 -07002929 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002930 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002931 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002932 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002933 rv = VPPCOM_EINVAL;
2934 goto done;
2935 }
2936
Florin Coras17ec5772020-08-12 20:46:29 -07002937 if (s->vep.prev_sh == vep_handle)
2938 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002939 else
2940 {
Florin Coras7e12d942018-06-27 14:32:43 -07002941 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002942 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002943 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002944 {
Florin Coras5e062572019-03-14 19:07:51 -07002945 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002946 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002947 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002948 }
Florin Coras134a9962018-08-28 11:32:04 -07002949 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002950 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002951 }
Florin Coras17ec5772020-08-12 20:46:29 -07002952 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002953 {
Florin Coras7e12d942018-06-27 14:32:43 -07002954 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002955 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002956 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002957 {
Florin Coras5e062572019-03-14 19:07:51 -07002958 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002959 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002960 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002961 }
Florin Coras134a9962018-08-28 11:32:04 -07002962 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002963 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002964 }
2965
Florin Corasfa3884f2021-06-22 20:04:46 -07002966 if (s->vep.lt_next != VCL_INVALID_SESSION_INDEX)
2967 vcl_epoll_lt_del (wrk, s);
2968
Florin Coras17ec5772020-08-12 20:46:29 -07002969 memset (&s->vep, 0, sizeof (s->vep));
2970 s->vep.next_sh = ~0;
2971 s->vep.prev_sh = ~0;
2972 s->vep.vep_sh = ~0;
Florin Corasfa3884f2021-06-22 20:04:46 -07002973 s->vep.lt_next = VCL_INVALID_SESSION_INDEX;
Florin Coras6c3b2182020-10-19 18:36:48 -07002974 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002975
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002976 if (vcl_session_is_open (s))
2977 {
2978 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2979 if (txf)
2980 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2981 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002982
Florin Coras5e062572019-03-14 19:07:51 -07002983 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002984 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002985 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002986 break;
2987
2988 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002989 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002990 rv = VPPCOM_EINVAL;
2991 }
2992
Florin Coras134a9962018-08-28 11:32:04 -07002993 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002994
2995done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002996 return rv;
2997}
2998
Florin Coras86f04502018-09-12 16:08:01 -07002999static inline void
3000vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
3001 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07003002{
3003 session_disconnected_msg_t *disconnected_msg;
3004 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07003005 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08003006 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07003007 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07003008 u8 add_event = 0;
3009
3010 switch (e->event_type)
3011 {
Florin Coras653e43f2019-03-04 10:56:23 -08003012 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08003013 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003014 s = vcl_session_get (wrk, sid);
3015 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003016 break;
Florin Corasb242d312020-10-26 15:35:40 -07003017 vcl_fifo_rx_evt_valid_or_break (s);
3018 session_events = s->vep.ev.events;
Florin Coras99e41452021-08-31 13:29:41 -07003019 if (!(EPOLLIN & s->vep.ev.events) ||
3020 (s->flags & VCL_SESSION_F_HAS_RX_EVT) ||
3021 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003022 break;
3023 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003024 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003025 session_evt_data = s->vep.ev.data.u64;
3026 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07003027 break;
Florin Coras653e43f2019-03-04 10:56:23 -08003028 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08003029 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003030 s = vcl_session_get (wrk, sid);
3031 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003032 break;
Florin Corasb242d312020-10-26 15:35:40 -07003033 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003034 if (!(EPOLLOUT & session_events))
3035 break;
3036 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003037 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003038 session_evt_data = s->vep.ev.data.u64;
3039 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
3040 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003041 break;
Florin Coras86f04502018-09-12 16:08:01 -07003042 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003043 if (!e->postponed)
3044 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3045 else
3046 s = vcl_session_get (wrk, e->session_index);
3047 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003048 break;
Florin Corasb242d312020-10-26 15:35:40 -07003049 session_events = s->vep.ev.events;
3050 sid = s->session_index;
liuyacancba87102021-09-10 15:14:05 +08003051 if (!(EPOLLIN & session_events) ||
3052 (s->vep.lt_next != VCL_INVALID_SESSION_INDEX))
Florin Coras86f04502018-09-12 16:08:01 -07003053 break;
Florin Coras86f04502018-09-12 16:08:01 -07003054 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003055 events[*num_ev].events = EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003056 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003057 break;
3058 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003059 if (!e->postponed)
3060 {
3061 connected_msg = (session_connected_msg_t *) e->data;
3062 sid = vcl_session_connected_handler (wrk, connected_msg);
3063 }
3064 else
3065 sid = e->session_index;
3066 s = vcl_session_get (wrk, sid);
3067 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003068 break;
Florin Corasb242d312020-10-26 15:35:40 -07003069 session_events = s->vep.ev.events;
3070 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003071 if (!(EPOLLOUT & session_events))
3072 break;
3073 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003074 events[*num_ev].events = EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003075 session_evt_data = s->vep.ev.data.u64;
3076 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003077 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003078 break;
3079 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003080 if (!e->postponed)
3081 {
3082 disconnected_msg = (session_disconnected_msg_t *) e->data;
3083 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3084 }
3085 else
3086 {
3087 s = vcl_session_get (wrk, e->session_index);
3088 }
3089 if (vcl_session_is_closed (s) ||
3090 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Coras86f04502018-09-12 16:08:01 -07003091 break;
Florin Corasb242d312020-10-26 15:35:40 -07003092 sid = s->session_index;
3093 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003094 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003095 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003096 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003097 break;
3098 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003099 if (!e->postponed)
3100 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3101 else
3102 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003103 s = vcl_session_get (wrk, sid);
Florin Coras87f76002021-06-28 19:13:29 -07003104 if (vcl_session_is_closed (s) ||
3105 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Coras86f04502018-09-12 16:08:01 -07003106 break;
Florin Corasb242d312020-10-26 15:35:40 -07003107 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003108 add_event = 1;
wanghanlin9e42cc22021-06-25 17:40:13 +08003109 events[*num_ev].events = EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003110 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003111 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003112 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3113 vcl_session_unlisten_reply_handler (wrk, e->data);
3114 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003115 case SESSION_CTRL_EVT_MIGRATED:
3116 vcl_session_migrated_handler (wrk, e->data);
3117 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003118 case SESSION_CTRL_EVT_CLEANUP:
3119 vcl_session_cleanup_handler (wrk, e->data);
3120 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003121 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3122 vcl_session_req_worker_update_handler (wrk, e->data);
3123 break;
3124 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3125 vcl_session_worker_update_reply_handler (wrk, e->data);
3126 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003127 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3128 vcl_session_app_add_segment_handler (wrk, e->data);
3129 break;
3130 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3131 vcl_session_app_del_segment_handler (wrk, e->data);
3132 break;
hanlina3a48962020-07-13 11:09:15 +08003133 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003134 vcl_worker_rpc_handler (wrk, e->data);
3135 break;
Florin Coras86f04502018-09-12 16:08:01 -07003136 default:
3137 VDBG (0, "unhandled: %u", e->event_type);
3138 break;
3139 }
3140
3141 if (add_event)
3142 {
3143 events[*num_ev].data.u64 = session_evt_data;
3144 if (EPOLLONESHOT & session_events)
3145 {
Florin Corasb242d312020-10-26 15:35:40 -07003146 s = vcl_session_get (wrk, sid);
3147 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003148 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003149 else if (!(EPOLLET & session_events))
Florin Corasfe286f72021-06-04 10:07:55 -07003150 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003151 s = vcl_session_get (wrk, sid);
3152 if (s->vep.lt_next == VCL_INVALID_SESSION_INDEX)
3153 vcl_epoll_lt_add (wrk, s);
Florin Corasfe286f72021-06-04 10:07:55 -07003154 }
Florin Coras86f04502018-09-12 16:08:01 -07003155 *num_ev += 1;
3156 }
3157}
3158
3159static int
3160vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3161 struct epoll_event *events, u32 maxevents,
3162 double wait_for_time, u32 * num_ev)
3163{
Florin Coras99368312018-08-02 10:45:44 -07003164 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003165 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003166 int i;
3167
Florin Coras539663c2018-09-28 14:59:37 -07003168 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3169 goto handle_dequeued;
3170
Florin Coras54693d22018-07-17 10:46:29 -07003171 if (svm_msg_q_is_empty (mq))
3172 {
3173 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003174 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003175 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003176 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003177 else
3178 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003179 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003180 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003181 }
3182 }
Florin Corase003a1b2019-06-05 10:47:16 -07003183 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003184 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003185
Florin Coras539663c2018-09-28 14:59:37 -07003186handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003187 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003188 {
Florin Coras134a9962018-08-28 11:32:04 -07003189 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003190 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003191 if (*num_ev < maxevents)
3192 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3193 else
3194 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003195 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003196 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003197 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003198 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003199 return *num_ev;
3200}
3201
Florin Coras99368312018-08-02 10:45:44 -07003202static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003203vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3204 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003205{
Florin Corasccdb8b82021-04-28 13:01:06 -07003206 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003207
3208 if (!n_evts)
3209 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003210 if (timeout_ms > 0)
3211 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003212 }
3213
3214 do
3215 {
3216 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003217 timeout_ms, &n_evts);
3218 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003219 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003220 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003221 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003222
3223 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003224}
3225
3226static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003227vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3228 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003229{
Florin Coras99368312018-08-02 10:45:44 -07003230 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003231 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003232 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003233 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003234 u64 buf;
3235
Florin Coras134a9962018-08-28 11:32:04 -07003236 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003237 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003238 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003239 if (timeout_ms > 0)
3240 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003241 }
3242
Florin Corasb13ba132021-01-27 16:05:24 -08003243 do
3244 {
3245 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003246 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003247 if (n_mq_evts < 0)
3248 {
3249 VDBG (0, "epoll_wait error %u", errno);
3250 return n_evts;
3251 }
3252
3253 for (i = 0; i < n_mq_evts; i++)
3254 {
3255 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3256 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3257 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3258 &n_evts);
3259 }
3260
Florin Corasccdb8b82021-04-28 13:01:06 -07003261 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003262 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003263 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003264 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003265
3266 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003267}
3268
Florin Corasfe286f72021-06-04 10:07:55 -07003269static void
Florin Corasfe286f72021-06-04 10:07:55 -07003270vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3271 int maxevents, u32 *n_evts)
3272{
Florin Coras734268f2021-06-30 07:54:29 -07003273 u32 add_event = 0, next;
Florin Corasfe286f72021-06-04 10:07:55 -07003274 vcl_session_t *s;
3275 u64 evt_data;
Florin Corasfa3884f2021-06-22 20:04:46 -07003276 int rv;
Florin Corasfe286f72021-06-04 10:07:55 -07003277
Florin Corasfa3884f2021-06-22 20:04:46 -07003278 ASSERT (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX);
Florin Corasfe286f72021-06-04 10:07:55 -07003279 if (*n_evts >= maxevents)
Florin Corasfa3884f2021-06-22 20:04:46 -07003280 return;
Florin Corasfe286f72021-06-04 10:07:55 -07003281
Florin Corasfa3884f2021-06-22 20:04:46 -07003282 next = wrk->ep_lt_current;
3283 do
Florin Corasfe286f72021-06-04 10:07:55 -07003284 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003285 s = vcl_session_get (wrk, next);
3286 next = s->vep.lt_next;
3287
3288 if ((s->vep.ev.events & EPOLLIN) && (rv = vcl_session_read_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003289 {
3290 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003291 events[*n_evts].events |= rv > 0 ? EPOLLIN : EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003292 evt_data = s->vep.ev.data.u64;
3293 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003294 if ((s->vep.ev.events & EPOLLOUT) && (rv = vcl_session_write_ready (s)))
Florin Corasfe286f72021-06-04 10:07:55 -07003295 {
3296 add_event = 1;
Florin Corasfa3884f2021-06-22 20:04:46 -07003297 events[*n_evts].events |= rv > 0 ? EPOLLOUT : EPOLLHUP | EPOLLRDHUP;
3298 evt_data = s->vep.ev.data.u64;
3299 }
3300 if (!add_event && s->session_state > VCL_STATE_READY)
3301 {
3302 add_event = 1;
3303 events[*n_evts].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasfe286f72021-06-04 10:07:55 -07003304 evt_data = s->vep.ev.data.u64;
3305 }
3306 if (add_event)
3307 {
3308 events[*n_evts].data.u64 = evt_data;
3309 *n_evts += 1;
3310 add_event = 0;
Florin Corasfa3884f2021-06-22 20:04:46 -07003311 if (EPOLLONESHOT & s->vep.ev.events)
3312 s->vep.ev.events = 0;
Florin Corasfe286f72021-06-04 10:07:55 -07003313 if (*n_evts == maxevents)
3314 {
Florin Corasfa3884f2021-06-22 20:04:46 -07003315 wrk->ep_lt_current = next;
Florin Corasfe286f72021-06-04 10:07:55 -07003316 break;
3317 }
3318 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003319 else
3320 {
3321 vcl_epoll_lt_del (wrk, s);
3322 if (wrk->ep_lt_current == VCL_INVALID_SESSION_INDEX)
3323 break;
3324 }
Florin Corasfe286f72021-06-04 10:07:55 -07003325 }
Florin Corasfa3884f2021-06-22 20:04:46 -07003326 while (next != wrk->ep_lt_current);
Florin Corasfe286f72021-06-04 10:07:55 -07003327}
3328
Dave Wallacef7f809c2017-10-03 01:48:42 -04003329int
Florin Coras134a9962018-08-28 11:32:04 -07003330vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003331 int maxevents, double wait_for_time)
3332{
Florin Coras134a9962018-08-28 11:32:04 -07003333 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003334 vcl_session_t *vep_session;
Florin Corasfa3884f2021-06-22 20:04:46 -07003335 u32 n_evts = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003336 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003337
3338 if (PREDICT_FALSE (maxevents <= 0))
3339 {
Florin Coras5e062572019-03-14 19:07:51 -07003340 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003341 return VPPCOM_EINVAL;
3342 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003343
Florin Coras134a9962018-08-28 11:32:04 -07003344 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003345 if (!vep_session)
3346 return VPPCOM_EBADFD;
3347
Florin Coras6c3b2182020-10-19 18:36:48 -07003348 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003349 {
Florin Coras5e062572019-03-14 19:07:51 -07003350 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003351 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003352 }
Florin Coras54693d22018-07-17 10:46:29 -07003353
Florin Coras86f04502018-09-12 16:08:01 -07003354 if (vec_len (wrk->unhandled_evts_vector))
3355 {
3356 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3357 {
3358 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3359 events, &n_evts);
3360 if (n_evts == maxevents)
3361 {
Florin Corase003a1b2019-06-05 10:47:16 -07003362 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3363 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003364 }
3365 }
Florin Corase003a1b2019-06-05 10:47:16 -07003366 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003367 }
liuyacancba87102021-09-10 15:14:05 +08003368
3369 if (PREDICT_FALSE (wrk->ep_lt_current != VCL_INVALID_SESSION_INDEX))
3370 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3371
wanghanlin8919fec2021-03-18 20:00:41 +08003372 /* Request to only drain unhandled */
3373 if ((int) wait_for_time == -2)
3374 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003375
Florin Coras86f04502018-09-12 16:08:01 -07003376
Florin Corasfe286f72021-06-04 10:07:55 -07003377 if (vcm->cfg.use_mq_eventfd)
3378 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3379 wait_for_time);
3380 else
3381 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3382 wait_for_time);
3383
Florin Corasfe286f72021-06-04 10:07:55 -07003384 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003385}
3386
Dave Wallace35830af2017-10-09 01:43:42 -04003387int
Florin Coras134a9962018-08-28 11:32:04 -07003388vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003389 void *buffer, uint32_t * buflen)
3390{
Florin Coras134a9962018-08-28 11:32:04 -07003391 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003392 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003393 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003394 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003395 vcl_session_t *session;
3396 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003397
Florin Coras134a9962018-08-28 11:32:04 -07003398 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003399 if (!session)
3400 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003401
Dave Wallace35830af2017-10-09 01:43:42 -04003402 switch (op)
3403 {
3404 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003405 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003406 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3407 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003408 break;
3409
Dave Wallace227867f2017-11-13 21:21:53 -05003410 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003411 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003412 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3413 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003414 break;
3415
3416 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003417 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003418 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003419 *flags =
3420 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003421 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003422 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003423 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003424 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3425 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003426 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003427 }
3428 else
3429 rv = VPPCOM_EINVAL;
3430 break;
3431
3432 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003433 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003434 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003435 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003436 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003437 else
Florin Corasac422d62020-10-19 20:51:36 -07003438 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003439
Florin Coras5e062572019-03-14 19:07:51 -07003440 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3441 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003442 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003443 }
3444 else
3445 rv = VPPCOM_EINVAL;
3446 break;
3447
3448 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003449 if (PREDICT_TRUE (buffer && buflen &&
3450 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003451 {
Florin Coras7e12d942018-06-27 14:32:43 -07003452 ep->is_ip4 = session->transport.is_ip4;
3453 ep->port = session->transport.rmt_port;
3454 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003455 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3456 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003457 else
Dave Barach178cf492018-11-13 16:34:13 -05003458 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3459 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003460 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003461 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3462 "addr = %U, port %u", session_handle, ep->is_ip4,
3463 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003464 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3465 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003466 }
3467 else
3468 rv = VPPCOM_EINVAL;
3469 break;
3470
3471 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003472 if (PREDICT_TRUE (buffer && buflen &&
3473 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003474 {
Florin Coras7e12d942018-06-27 14:32:43 -07003475 ep->is_ip4 = session->transport.is_ip4;
3476 ep->port = session->transport.lcl_port;
3477 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003478 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3479 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003480 else
Dave Barach178cf492018-11-13 16:34:13 -05003481 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3482 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003483 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003484 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3485 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003486 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003487 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3488 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003489 }
3490 else
3491 rv = VPPCOM_EINVAL;
3492 break;
Stevenb5a11602017-10-11 09:59:30 -07003493
Florin Corasef7cbf62019-10-17 09:56:27 -07003494 case VPPCOM_ATTR_SET_LCL_ADDR:
3495 if (PREDICT_TRUE (buffer && buflen &&
3496 (*buflen >= sizeof (*ep)) && ep->ip))
3497 {
3498 session->transport.is_ip4 = ep->is_ip4;
3499 session->transport.lcl_port = ep->port;
3500 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3501 *buflen = sizeof (*ep);
3502 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3503 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3504 &session->transport.lcl_ip,
3505 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3506 clib_net_to_host_u16 (ep->port));
3507 }
3508 else
3509 rv = VPPCOM_EINVAL;
3510 break;
3511
Dave Wallace048b1d62018-01-03 22:24:41 -05003512 case VPPCOM_ATTR_GET_LIBC_EPFD:
3513 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003514 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003515 break;
3516
3517 case VPPCOM_ATTR_SET_LIBC_EPFD:
3518 if (PREDICT_TRUE (buffer && buflen &&
3519 (*buflen == sizeof (session->libc_epfd))))
3520 {
3521 session->libc_epfd = *(int *) buffer;
3522 *buflen = sizeof (session->libc_epfd);
3523
Florin Coras5e062572019-03-14 19:07:51 -07003524 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3525 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003526 }
3527 else
3528 rv = VPPCOM_EINVAL;
3529 break;
3530
3531 case VPPCOM_ATTR_GET_PROTOCOL:
3532 if (buffer && buflen && (*buflen >= sizeof (int)))
3533 {
Florin Coras7e12d942018-06-27 14:32:43 -07003534 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003535 *buflen = sizeof (int);
3536
Florin Coras5e062572019-03-14 19:07:51 -07003537 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3538 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 }
3540 else
3541 rv = VPPCOM_EINVAL;
3542 break;
3543
3544 case VPPCOM_ATTR_GET_LISTEN:
3545 if (buffer && buflen && (*buflen >= sizeof (int)))
3546 {
Florin Corasac422d62020-10-19 20:51:36 -07003547 *(int *) buffer = vcl_session_has_attr (session,
3548 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003549 *buflen = sizeof (int);
3550
Florin Coras5e062572019-03-14 19:07:51 -07003551 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3552 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003553 }
3554 else
3555 rv = VPPCOM_EINVAL;
3556 break;
3557
3558 case VPPCOM_ATTR_GET_ERROR:
3559 if (buffer && buflen && (*buflen >= sizeof (int)))
3560 {
3561 *(int *) buffer = 0;
3562 *buflen = sizeof (int);
3563
Florin Coras5e062572019-03-14 19:07:51 -07003564 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3565 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003566 }
3567 else
3568 rv = VPPCOM_EINVAL;
3569 break;
3570
3571 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3572 if (buffer && buflen && (*buflen >= sizeof (u32)))
3573 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003574
3575 /* VPP-TBD */
3576 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003577 session->tx_fifo ?
3578 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003579 vcm->cfg.tx_fifo_size);
3580 *buflen = sizeof (u32);
3581
Florin Coras5e062572019-03-14 19:07:51 -07003582 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3583 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3584 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003585 }
3586 else
3587 rv = VPPCOM_EINVAL;
3588 break;
3589
3590 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3591 if (buffer && buflen && (*buflen == sizeof (u32)))
3592 {
3593 /* VPP-TBD */
3594 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003595 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3596 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3597 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 }
3599 else
3600 rv = VPPCOM_EINVAL;
3601 break;
3602
3603 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3604 if (buffer && buflen && (*buflen >= sizeof (u32)))
3605 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003606
3607 /* VPP-TBD */
3608 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003609 session->rx_fifo ?
3610 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003611 vcm->cfg.rx_fifo_size);
3612 *buflen = sizeof (u32);
3613
Florin Coras5e062572019-03-14 19:07:51 -07003614 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3615 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003616 }
3617 else
3618 rv = VPPCOM_EINVAL;
3619 break;
3620
3621 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3622 if (buffer && buflen && (*buflen == sizeof (u32)))
3623 {
3624 /* VPP-TBD */
3625 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003626 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3627 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3628 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003629 }
3630 else
3631 rv = VPPCOM_EINVAL;
3632 break;
3633
3634 case VPPCOM_ATTR_GET_REUSEADDR:
3635 if (buffer && buflen && (*buflen >= sizeof (int)))
3636 {
3637 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003638 *(int *) buffer = vcl_session_has_attr (session,
3639 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003640 *buflen = sizeof (int);
3641
Florin Coras5e062572019-03-14 19:07:51 -07003642 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3643 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003644 }
3645 else
3646 rv = VPPCOM_EINVAL;
3647 break;
3648
Stevenb5a11602017-10-11 09:59:30 -07003649 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003650 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003651 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003652 {
3653 /* VPP-TBD */
3654 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003655 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003656 else
Florin Corasac422d62020-10-19 20:51:36 -07003657 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003658
Florin Coras5e062572019-03-14 19:07:51 -07003659 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003660 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003661 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003662 }
3663 else
3664 rv = VPPCOM_EINVAL;
3665 break;
3666
3667 case VPPCOM_ATTR_GET_REUSEPORT:
3668 if (buffer && buflen && (*buflen >= sizeof (int)))
3669 {
3670 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003671 *(int *) buffer = vcl_session_has_attr (session,
3672 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003673 *buflen = sizeof (int);
3674
Florin Coras5e062572019-03-14 19:07:51 -07003675 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3676 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003677 }
3678 else
3679 rv = VPPCOM_EINVAL;
3680 break;
3681
3682 case VPPCOM_ATTR_SET_REUSEPORT:
3683 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003684 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003685 {
3686 /* VPP-TBD */
3687 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003688 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003689 else
Florin Corasac422d62020-10-19 20:51:36 -07003690 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003691
Florin Coras5e062572019-03-14 19:07:51 -07003692 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003693 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003694 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003695 }
3696 else
3697 rv = VPPCOM_EINVAL;
3698 break;
3699
3700 case VPPCOM_ATTR_GET_BROADCAST:
3701 if (buffer && buflen && (*buflen >= sizeof (int)))
3702 {
3703 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003704 *(int *) buffer = vcl_session_has_attr (session,
3705 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003706 *buflen = sizeof (int);
3707
Florin Coras5e062572019-03-14 19:07:51 -07003708 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3709 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003710 }
3711 else
3712 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003713 break;
3714
3715 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003716 if (buffer && buflen && (*buflen == sizeof (int)))
3717 {
3718 /* VPP-TBD */
3719 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003720 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003721 else
Florin Corasac422d62020-10-19 20:51:36 -07003722 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003723
Florin Coras5e062572019-03-14 19:07:51 -07003724 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003725 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003726 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003727 }
3728 else
3729 rv = VPPCOM_EINVAL;
3730 break;
3731
3732 case VPPCOM_ATTR_GET_V6ONLY:
3733 if (buffer && buflen && (*buflen >= sizeof (int)))
3734 {
3735 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003736 *(int *) buffer = vcl_session_has_attr (session,
3737 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003738 *buflen = sizeof (int);
3739
Florin Coras5e062572019-03-14 19:07:51 -07003740 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3741 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003742 }
3743 else
3744 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003745 break;
3746
3747 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003748 if (buffer && buflen && (*buflen == sizeof (int)))
3749 {
3750 /* VPP-TBD */
3751 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003752 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003753 else
Florin Corasac422d62020-10-19 20:51:36 -07003754 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003755
Florin Coras5e062572019-03-14 19:07:51 -07003756 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003757 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003758 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003759 }
3760 else
3761 rv = VPPCOM_EINVAL;
3762 break;
3763
3764 case VPPCOM_ATTR_GET_KEEPALIVE:
3765 if (buffer && buflen && (*buflen >= sizeof (int)))
3766 {
3767 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003768 *(int *) buffer = vcl_session_has_attr (session,
3769 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003770 *buflen = sizeof (int);
3771
Florin Coras5e062572019-03-14 19:07:51 -07003772 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3773 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003774 }
3775 else
3776 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003777 break;
Stevenbccd3392017-10-12 20:42:21 -07003778
3779 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003780 if (buffer && buflen && (*buflen == sizeof (int)))
3781 {
3782 /* VPP-TBD */
3783 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003784 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003785 else
Florin Corasac422d62020-10-19 20:51:36 -07003786 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003787
Florin Coras5e062572019-03-14 19:07:51 -07003788 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003789 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003790 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003791 }
3792 else
3793 rv = VPPCOM_EINVAL;
3794 break;
3795
3796 case VPPCOM_ATTR_GET_TCP_NODELAY:
3797 if (buffer && buflen && (*buflen >= sizeof (int)))
3798 {
3799 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003800 *(int *) buffer = vcl_session_has_attr (session,
3801 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003802 *buflen = sizeof (int);
3803
Florin Coras5e062572019-03-14 19:07:51 -07003804 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3805 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003806 }
3807 else
3808 rv = VPPCOM_EINVAL;
3809 break;
3810
3811 case VPPCOM_ATTR_SET_TCP_NODELAY:
3812 if (buffer && buflen && (*buflen == sizeof (int)))
3813 {
3814 /* VPP-TBD */
3815 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003816 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 else
Florin Corasac422d62020-10-19 20:51:36 -07003818 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003819
Florin Coras5e062572019-03-14 19:07:51 -07003820 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003821 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003822 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003823 }
3824 else
3825 rv = VPPCOM_EINVAL;
3826 break;
3827
3828 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3829 if (buffer && buflen && (*buflen >= sizeof (int)))
3830 {
3831 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003832 *(int *) buffer = vcl_session_has_attr (session,
3833 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003834 *buflen = sizeof (int);
3835
Florin Coras5e062572019-03-14 19:07:51 -07003836 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3837 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003838 }
3839 else
3840 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003841 break;
3842
3843 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003844 if (buffer && buflen && (*buflen == sizeof (int)))
3845 {
3846 /* VPP-TBD */
3847 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003848 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003849 else
Florin Corasac422d62020-10-19 20:51:36 -07003850 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003851
Florin Coras5e062572019-03-14 19:07:51 -07003852 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003853 vcl_session_has_attr (session,
3854 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003855 }
3856 else
3857 rv = VPPCOM_EINVAL;
3858 break;
3859
3860 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3861 if (buffer && buflen && (*buflen >= sizeof (int)))
3862 {
3863 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003864 *(int *) buffer = vcl_session_has_attr (session,
3865 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003866 *buflen = sizeof (int);
3867
Florin Coras5e062572019-03-14 19:07:51 -07003868 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3869 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003870 }
3871 else
3872 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003873 break;
3874
3875 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003876 if (buffer && buflen && (*buflen == sizeof (int)))
3877 {
3878 /* VPP-TBD */
3879 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003880 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003881 else
Florin Corasac422d62020-10-19 20:51:36 -07003882 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003883
Florin Coras5e062572019-03-14 19:07:51 -07003884 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003885 vcl_session_has_attr (session,
3886 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003887 }
3888 else
3889 rv = VPPCOM_EINVAL;
3890 break;
3891
3892 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003893 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003894 {
Florin Coras04ae8272021-04-12 19:55:37 -07003895 rv = VPPCOM_EINVAL;
3896 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003897 }
Florin Coras04ae8272021-04-12 19:55:37 -07003898
3899 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3900 tea.mss = *(u32 *) buffer;
3901 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3902 rv = VPPCOM_ENOPROTOOPT;
3903
3904 if (!rv)
3905 {
3906 *(u32 *) buffer = tea.mss;
3907 *buflen = sizeof (int);
3908 }
3909
3910 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3911 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003912 break;
3913
3914 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003915 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003916 {
Florin Coras04ae8272021-04-12 19:55:37 -07003917 rv = VPPCOM_EINVAL;
3918 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003919 }
Florin Coras04ae8272021-04-12 19:55:37 -07003920
3921 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3922 tea.mss = *(u32 *) buffer;
3923 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3924 rv = VPPCOM_ENOPROTOOPT;
3925
3926 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3927 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003928 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003929
Florin Coras1e966172020-05-16 18:18:14 +00003930 case VPPCOM_ATTR_SET_CONNECTED:
3931 session->flags |= VCL_SESSION_F_CONNECTED;
3932 break;
3933
Florin Corasa5a9efd2021-01-05 17:03:29 -08003934 case VPPCOM_ATTR_SET_CKPAIR:
3935 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3936 !vcl_session_has_crypto (session))
3937 {
3938 rv = VPPCOM_EINVAL;
3939 break;
3940 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003941 if (!session->ext_config)
3942 {
Florin Coras87f63892021-05-01 16:01:40 -07003943 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3944 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003945 }
3946 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3947 {
3948 rv = VPPCOM_EINVAL;
3949 break;
3950 }
3951
3952 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003953 break;
3954
Florin Coras6a6555a2021-01-28 11:39:27 -08003955 case VPPCOM_ATTR_SET_VRF:
3956 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3957 {
3958 rv = VPPCOM_EINVAL;
3959 break;
3960 }
3961 session->vrf = *(u32 *) buffer;
3962 break;
3963
3964 case VPPCOM_ATTR_GET_VRF:
3965 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3966 {
3967 rv = VPPCOM_EINVAL;
3968 break;
3969 }
3970 *(u32 *) buffer = session->vrf;
3971 *buflen = sizeof (u32);
3972 break;
3973
wanghanlin0674f852021-02-22 10:38:36 +08003974 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003975 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003976 {
Florin Corasd77325c2021-02-23 12:03:03 -08003977 rv = VPPCOM_EINVAL;
3978 break;
wanghanlin0674f852021-02-22 10:38:36 +08003979 }
Florin Corasd77325c2021-02-23 12:03:03 -08003980
3981 if (session->transport.is_ip4)
3982 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003983 else
Florin Corasd77325c2021-02-23 12:03:03 -08003984 *(int *) buffer = AF_INET6;
3985 *buflen = sizeof (int);
3986
wanghanlin0674f852021-02-22 10:38:36 +08003987 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3988 *buflen);
3989 break;
3990
Florin Coras87f63892021-05-01 16:01:40 -07003991 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3992 if (!(buffer && buflen && (*buflen > 0)))
3993 {
3994 rv = VPPCOM_EINVAL;
3995 break;
3996 }
3997 if (session->ext_config)
3998 {
3999 rv = VPPCOM_EINVAL;
4000 break;
4001 }
4002 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
4003 *buflen + sizeof (u32));
4004 clib_memcpy (session->ext_config->data, buffer, *buflen);
4005 session->ext_config->len = *buflen;
4006 break;
4007
Dave Wallacee22aa742017-10-20 12:30:38 -04004008 default:
4009 rv = VPPCOM_EINVAL;
4010 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004011 }
4012
Dave Wallace35830af2017-10-09 01:43:42 -04004013 return rv;
4014}
4015
Stevenac1f96d2017-10-24 16:03:58 -07004016int
Florin Coras134a9962018-08-28 11:32:04 -07004017vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004018 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4019{
Florin Coras134a9962018-08-28 11:32:04 -07004020 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00004021 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07004022 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07004023
4024 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07004025 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004026 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07004027 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004028 else
4029 {
Florin Corasa7a1a222018-12-30 17:11:31 -08004030 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07004031 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07004032 }
4033
Florin Coras0a1e1832020-03-29 18:54:04 +00004034 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07004035 {
Florin Coras5da10c42020-04-01 04:31:21 +00004036 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07004037 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004038 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4039 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004040 else
Dave Barach178cf492018-11-13 16:34:13 -05004041 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4042 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004043 ep->is_ip4 = session->transport.is_ip4;
4044 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004045 }
Florin Coras460dce62018-07-27 05:45:06 -07004046
Stevenac1f96d2017-10-24 16:03:58 -07004047 return rv;
4048}
4049
4050int
Florin Coras134a9962018-08-28 11:32:04 -07004051vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004052 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4053{
Florin Coras7a2abce2020-04-05 19:25:44 +00004054 vcl_worker_t *wrk = vcl_worker_get_current ();
4055 vcl_session_t *s;
4056
4057 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004058 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004059 return VPPCOM_EBADFD;
4060
Dave Wallace617dffa2017-10-26 14:47:06 -04004061 if (ep)
4062 {
Florin Coras5824cc52020-10-20 18:44:41 -07004063 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004064 return VPPCOM_EINVAL;
4065
liuyacanbc0c7542021-08-02 20:15:05 +08004066 s->transport.is_ip4 = ep->is_ip4;
4067 s->transport.rmt_port = ep->port;
4068 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
4069
Florin Coras5da10c42020-04-01 04:31:21 +00004070 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004071 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004072 {
Florin Coras5824cc52020-10-20 18:44:41 -07004073 u32 session_index = s->session_index;
4074 f64 timeout = vcm->cfg.session_timeout;
4075 int rv;
4076
Florin Coras5da10c42020-04-01 04:31:21 +00004077 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004078 rv = vppcom_wait_for_session_state_change (session_index,
4079 VCL_STATE_READY,
4080 timeout);
4081 if (rv < 0)
4082 return rv;
4083 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004084 }
Dave Wallace617dffa2017-10-26 14:47:06 -04004085 }
4086
4087 if (flags)
4088 {
4089 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004090 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004091 }
4092
Florin Coras7a2abce2020-04-05 19:25:44 +00004093 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4094 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004095}
4096
Dave Wallace048b1d62018-01-03 22:24:41 -05004097int
4098vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4099{
Florin Coras134a9962018-08-28 11:32:04 -07004100 vcl_worker_t *wrk = vcl_worker_get_current ();
4101 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004102 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004103 svm_msg_q_msg_t msg;
4104 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004105 int rv, num_ev = 0;
4106
Florin Coras5e062572019-03-14 19:07:51 -07004107 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004108
4109 if (!vp)
4110 return VPPCOM_EFAULT;
4111
4112 do
4113 {
Florin Coras7e12d942018-06-27 14:32:43 -07004114 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004115
Florin Coras6917b942018-11-13 22:44:54 -08004116 /* Dequeue all events and drop all unhandled io events */
4117 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4118 {
4119 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4120 vcl_handle_mq_event (wrk, e);
4121 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4122 }
4123 vec_reset_length (wrk->unhandled_evts_vector);
4124
Dave Wallace048b1d62018-01-03 22:24:41 -05004125 for (i = 0; i < n_sids; i++)
4126 {
Florin Coras7baeb712019-01-04 17:05:43 -08004127 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004128 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004129 {
4130 vp[i].revents = POLLHUP;
4131 num_ev++;
4132 continue;
4133 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004134
Florin Coras6917b942018-11-13 22:44:54 -08004135 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004136
4137 if (POLLIN & vp[i].events)
4138 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004139 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004140 if (rv > 0)
4141 {
Florin Coras6917b942018-11-13 22:44:54 -08004142 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004143 num_ev++;
4144 }
4145 else if (rv < 0)
4146 {
4147 switch (rv)
4148 {
4149 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004150 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004151 break;
4152
4153 default:
Florin Coras6917b942018-11-13 22:44:54 -08004154 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004155 break;
4156 }
4157 num_ev++;
4158 }
4159 }
4160
4161 if (POLLOUT & vp[i].events)
4162 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004163 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004164 if (rv > 0)
4165 {
Florin Coras6917b942018-11-13 22:44:54 -08004166 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004167 num_ev++;
4168 }
4169 else if (rv < 0)
4170 {
4171 switch (rv)
4172 {
4173 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004174 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004175 break;
4176
4177 default:
Florin Coras6917b942018-11-13 22:44:54 -08004178 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004179 break;
4180 }
4181 num_ev++;
4182 }
4183 }
4184
Dave Wallace7e607a72018-06-18 18:41:32 -04004185 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004186 {
Florin Coras6917b942018-11-13 22:44:54 -08004187 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004188 num_ev++;
4189 }
4190 }
4191 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004192 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004193 }
4194 while ((num_ev == 0) && keep_trying);
4195
Dave Wallace048b1d62018-01-03 22:24:41 -05004196 return num_ev;
4197}
4198
Florin Coras99368312018-08-02 10:45:44 -07004199int
4200vppcom_mq_epoll_fd (void)
4201{
Florin Coras134a9962018-08-28 11:32:04 -07004202 vcl_worker_t *wrk = vcl_worker_get_current ();
4203 return wrk->mqs_epfd;
4204}
4205
4206int
Florin Coras30e79c22019-01-02 19:31:22 -08004207vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004208{
4209 return session_handle & 0xFFFFFF;
4210}
4211
4212int
Florin Coras30e79c22019-01-02 19:31:22 -08004213vppcom_session_worker (vcl_session_handle_t session_handle)
4214{
4215 return session_handle >> 24;
4216}
4217
4218int
Florin Coras134a9962018-08-28 11:32:04 -07004219vppcom_worker_register (void)
4220{
Florin Coras47c40e22018-11-26 17:01:36 -08004221 if (!vcl_worker_alloc_and_init ())
4222 return VPPCOM_EEXIST;
4223
Florin Coras47c40e22018-11-26 17:01:36 -08004224 if (vcl_worker_register_with_vpp ())
4225 return VPPCOM_EEXIST;
4226
4227 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004228}
4229
Florin Coras369db832019-07-08 12:34:45 -07004230void
4231vppcom_worker_unregister (void)
4232{
4233 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4234 vcl_set_worker_index (~0);
4235}
4236
Pivo6017ff02020-05-04 17:57:33 +02004237void
4238vppcom_worker_index_set (int index)
4239{
4240 vcl_set_worker_index (index);
4241}
4242
Florin Corasdfe4cf42018-11-28 22:13:45 -08004243int
4244vppcom_worker_index (void)
4245{
4246 return vcl_get_worker_index ();
4247}
4248
Florin Corase0982e52019-01-25 13:19:56 -08004249int
4250vppcom_worker_mqs_epfd (void)
4251{
4252 vcl_worker_t *wrk = vcl_worker_get_current ();
4253 if (!vcm->cfg.use_mq_eventfd)
4254 return -1;
4255 return wrk->mqs_epfd;
4256}
4257
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004258int
4259vppcom_session_is_connectable_listener (uint32_t session_handle)
4260{
4261 vcl_session_t *session;
4262 vcl_worker_t *wrk = vcl_worker_get_current ();
4263 session = vcl_session_get_w_handle (wrk, session_handle);
4264 if (!session)
4265 return VPPCOM_EBADFD;
4266 return vcl_session_is_connectable_listener (wrk, session);
4267}
4268
4269int
4270vppcom_session_listener (uint32_t session_handle)
4271{
4272 vcl_worker_t *wrk = vcl_worker_get_current ();
4273 vcl_session_t *listen_session, *session;
4274 session = vcl_session_get_w_handle (wrk, session_handle);
4275 if (!session)
4276 return VPPCOM_EBADFD;
4277 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4278 return VPPCOM_EBADFD;
4279 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4280 if (!listen_session)
4281 return VPPCOM_EBADFD;
4282 return vcl_session_handle (listen_session);
4283}
4284
4285int
4286vppcom_session_n_accepted (uint32_t session_handle)
4287{
4288 vcl_worker_t *wrk = vcl_worker_get_current ();
4289 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4290 if (!session)
4291 return VPPCOM_EBADFD;
4292 return session->n_accepted_sessions;
4293}
4294
Florin Coras66ec4672020-06-15 07:59:40 -07004295const char *
4296vppcom_proto_str (vppcom_proto_t proto)
4297{
4298 char const *proto_str;
4299
4300 switch (proto)
4301 {
4302 case VPPCOM_PROTO_TCP:
4303 proto_str = "TCP";
4304 break;
4305 case VPPCOM_PROTO_UDP:
4306 proto_str = "UDP";
4307 break;
4308 case VPPCOM_PROTO_TLS:
4309 proto_str = "TLS";
4310 break;
4311 case VPPCOM_PROTO_QUIC:
4312 proto_str = "QUIC";
4313 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004314 case VPPCOM_PROTO_DTLS:
4315 proto_str = "DTLS";
4316 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004317 case VPPCOM_PROTO_SRTP:
4318 proto_str = "SRTP";
4319 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004320 default:
4321 proto_str = "UNKNOWN";
4322 break;
4323 }
4324 return proto_str;
4325}
4326
4327const char *
4328vppcom_retval_str (int retval)
4329{
4330 char const *st;
4331
4332 switch (retval)
4333 {
4334 case VPPCOM_OK:
4335 st = "VPPCOM_OK";
4336 break;
4337
4338 case VPPCOM_EAGAIN:
4339 st = "VPPCOM_EAGAIN";
4340 break;
4341
4342 case VPPCOM_EFAULT:
4343 st = "VPPCOM_EFAULT";
4344 break;
4345
4346 case VPPCOM_ENOMEM:
4347 st = "VPPCOM_ENOMEM";
4348 break;
4349
4350 case VPPCOM_EINVAL:
4351 st = "VPPCOM_EINVAL";
4352 break;
4353
4354 case VPPCOM_EBADFD:
4355 st = "VPPCOM_EBADFD";
4356 break;
4357
4358 case VPPCOM_EAFNOSUPPORT:
4359 st = "VPPCOM_EAFNOSUPPORT";
4360 break;
4361
4362 case VPPCOM_ECONNABORTED:
4363 st = "VPPCOM_ECONNABORTED";
4364 break;
4365
4366 case VPPCOM_ECONNRESET:
4367 st = "VPPCOM_ECONNRESET";
4368 break;
4369
4370 case VPPCOM_ENOTCONN:
4371 st = "VPPCOM_ENOTCONN";
4372 break;
4373
4374 case VPPCOM_ECONNREFUSED:
4375 st = "VPPCOM_ECONNREFUSED";
4376 break;
4377
4378 case VPPCOM_ETIMEDOUT:
4379 st = "VPPCOM_ETIMEDOUT";
4380 break;
4381
4382 default:
4383 st = "UNKNOWN_STATE";
4384 break;
4385 }
4386
4387 return st;
4388}
4389
Florin Corasa5a9efd2021-01-05 17:03:29 -08004390int
4391vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4392{
4393 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004394 return vcl_sapi_add_cert_key_pair (ckpair);
4395 else
4396 return vcl_bapi_add_cert_key_pair (ckpair);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004397}
4398
4399int
4400vppcom_del_cert_key_pair (uint32_t ckpair_index)
4401{
4402 if (vcm->cfg.vpp_app_socket_api)
Florin Corase191d762021-08-11 14:55:49 -07004403 return vcl_sapi_del_cert_key_pair (ckpair_index);
4404 else
4405 return vcl_bapi_del_cert_key_pair (ckpair_index);
Florin Corasa5a9efd2021-01-05 17:03:29 -08004406}
4407
Dave Wallacee22aa742017-10-20 12:30:38 -04004408/*
4409 * fd.io coding-style-patch-verification: ON
4410 *
4411 * Local Variables:
4412 * eval: (c-set-style "gnu")
4413 * End:
4414 */