blob: b74570d10da669efef4c837e1762eca6057bba65 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
Florin Coras5398dfb2021-01-25 20:31:27 -080028 u32 n_msgs = 0, sz, len;
Florin Coras30e79c22019-01-02 19:31:22 -080029
Florin Coras5398dfb2021-01-25 20:31:27 -080030 while ((sz = svm_msg_q_size (mq)))
Florin Coras30e79c22019-01-02 19:31:22 -080031 {
Florin Coras5398dfb2021-01-25 20:31:27 -080032 len = vec_len (wrk->mq_msg_vector);
33 vec_validate (wrk->mq_msg_vector, len + sz - 1);
34 svm_msg_q_sub_raw_batch (mq, wrk->mq_msg_vector + len, sz);
35 n_msgs += sz;
Florin Coras30e79c22019-01-02 19:31:22 -080036 }
37 return n_msgs;
38}
39
Florin Coras697faea2018-06-27 17:10:49 -070040const char *
Florin Coras288eaab2019-02-03 15:26:14 -080041vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040042{
43 char *st;
44
45 switch (state)
46 {
Florin Corasc127d5a2020-10-14 16:35:58 -070047 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000048 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040049 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070050 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040051 st = "STATE_LISTEN";
52 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070053 case VCL_STATE_READY:
54 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040055 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070056 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080057 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050058 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070059 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040060 st = "STATE_DISCONNECT";
61 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070062 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000063 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040064 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070065 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080066 st = "STATE_UPDATED";
67 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070068 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080069 st = "STATE_LISTEN_NO_MQ";
70 break;
Dave Wallace543852a2017-08-03 02:11:34 -040071 default:
72 st = "UNKNOWN_STATE";
73 break;
74 }
75
76 return st;
77}
78
Dave Wallace543852a2017-08-03 02:11:34 -040079u8 *
80format_ip4_address (u8 * s, va_list * args)
81{
82 u8 *a = va_arg (*args, u8 *);
83 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
84}
85
86u8 *
87format_ip6_address (u8 * s, va_list * args)
88{
89 ip6_address_t *a = va_arg (*args, ip6_address_t *);
90 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
91
92 i_max_n_zero = ARRAY_LEN (a->as_u16);
93 max_n_zeros = 0;
94 i_first_zero = i_max_n_zero;
95 n_zeros = 0;
96 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
97 {
98 u32 is_zero = a->as_u16[i] == 0;
99 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
100 {
101 i_first_zero = i;
102 n_zeros = 0;
103 }
104 n_zeros += is_zero;
105 if ((!is_zero && n_zeros > max_n_zeros)
106 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
107 {
108 i_max_n_zero = i_first_zero;
109 max_n_zeros = n_zeros;
110 i_first_zero = ARRAY_LEN (a->as_u16);
111 n_zeros = 0;
112 }
113 }
114
115 last_double_colon = 0;
116 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
117 {
118 if (i == i_max_n_zero && max_n_zeros > 1)
119 {
120 s = format (s, "::");
121 i += max_n_zeros - 1;
122 last_double_colon = 1;
123 }
124 else
125 {
126 s = format (s, "%s%x",
127 (last_double_colon || i == 0) ? "" : ":",
128 clib_net_to_host_u16 (a->as_u16[i]));
129 last_double_colon = 0;
130 }
131 }
132
133 return s;
134}
135
136/* Format an IP46 address. */
137u8 *
138format_ip46_address (u8 * s, va_list * args)
139{
140 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
141 ip46_type_t type = va_arg (*args, ip46_type_t);
142 int is_ip4 = 1;
143
144 switch (type)
145 {
146 case IP46_TYPE_ANY:
147 is_ip4 = ip46_address_is_ip4 (ip46);
148 break;
149 case IP46_TYPE_IP4:
150 is_ip4 = 1;
151 break;
152 case IP46_TYPE_IP6:
153 is_ip4 = 0;
154 break;
155 }
156
157 return is_ip4 ?
158 format (s, "%U", format_ip4_address, &ip46->ip4) :
159 format (s, "%U", format_ip6_address, &ip46->ip6);
160}
161
Florin Coras697faea2018-06-27 17:10:49 -0700162/*
163 * VPPCOM Utility Functions
164 */
165
Florin Coras458089b2019-08-21 16:20:44 -0700166static void
Florin Coras4ac25842021-04-19 17:34:54 -0700167vcl_msg_add_ext_config (vcl_session_t *s, uword *offset)
168{
169 svm_fifo_chunk_t *c;
170
171 c = vcl_segment_alloc_chunk (vcl_vpp_worker_segment_handle (0),
172 0 /* one slice only */, s->ext_config->len,
173 offset);
174 if (c)
175 clib_memcpy_fast (c->data, s->ext_config, s->ext_config->len);
176}
177
178static void
Florin Coras458089b2019-08-21 16:20:44 -0700179vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
180{
181 app_session_evt_t _app_evt, *app_evt = &_app_evt;
182 session_listen_msg_t *mp;
183 svm_msg_q_t *mq;
184
185 mq = vcl_worker_ctrl_mq (wrk);
186 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
187 mp = (session_listen_msg_t *) app_evt->evt->data;
188 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700189 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700190 mp->context = s->session_index;
191 mp->wrk_index = wrk->vpp_wrk_index;
192 mp->is_ip4 = s->transport.is_ip4;
193 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
194 mp->port = s->transport.lcl_port;
195 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800196 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +0000197 if (s->flags & VCL_SESSION_F_CONNECTED)
198 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700199 if (s->ext_config)
200 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700201 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700202 if (s->ext_config)
203 {
204 clib_mem_free (s->ext_config);
205 s->ext_config = 0;
206 }
Florin Coras458089b2019-08-21 16:20:44 -0700207}
208
209static void
210vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
211{
212 app_session_evt_t _app_evt, *app_evt = &_app_evt;
213 session_connect_msg_t *mp;
214 svm_msg_q_t *mq;
215
216 mq = vcl_worker_ctrl_mq (wrk);
217 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
218 mp = (session_connect_msg_t *) app_evt->evt->data;
219 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700220 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700221 mp->context = s->session_index;
222 mp->wrk_index = wrk->vpp_wrk_index;
223 mp->is_ip4 = s->transport.is_ip4;
224 mp->parent_handle = s->parent_handle;
225 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700226 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700227 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000228 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700229 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800230 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000231 if (s->flags & VCL_SESSION_F_CONNECTED)
232 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700233 if (s->ext_config)
234 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700235 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700236
237 if (s->ext_config)
238 {
239 clib_mem_free (s->ext_config);
240 s->ext_config = 0;
241 }
Florin Coras458089b2019-08-21 16:20:44 -0700242}
243
244void
245vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
246{
247 app_session_evt_t _app_evt, *app_evt = &_app_evt;
248 session_unlisten_msg_t *mp;
249 svm_msg_q_t *mq;
250
251 mq = vcl_worker_ctrl_mq (wrk);
252 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
253 mp = (session_unlisten_msg_t *) app_evt->evt->data;
254 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700255 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700256 mp->wrk_index = wrk->vpp_wrk_index;
257 mp->handle = s->vpp_handle;
258 mp->context = wrk->wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
260}
261
262static void
liuyacan534468e2021-05-09 03:50:40 +0000263vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
264{
265 app_session_evt_t _app_evt, *app_evt = &_app_evt;
266 session_shutdown_msg_t *mp;
267 svm_msg_q_t *mq;
268
269 /* Send to thread that owns the session */
270 mq = s->vpp_evt_q;
271 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
272 mp = (session_shutdown_msg_t *) app_evt->evt->data;
273 memset (mp, 0, sizeof (*mp));
274 mp->client_index = wrk->api_client_handle;
275 mp->handle = s->vpp_handle;
276 app_send_ctrl_evt_to_vpp (mq, app_evt);
277}
278
279static void
Florin Coras458089b2019-08-21 16:20:44 -0700280vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
281{
282 app_session_evt_t _app_evt, *app_evt = &_app_evt;
283 session_disconnect_msg_t *mp;
284 svm_msg_q_t *mq;
285
286 /* Send to thread that owns the session */
287 mq = s->vpp_evt_q;
288 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
289 mp = (session_disconnect_msg_t *) app_evt->evt->data;
290 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700291 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700292 mp->handle = s->vpp_handle;
293 app_send_ctrl_evt_to_vpp (mq, app_evt);
294}
295
296static void
297vcl_send_app_detach (vcl_worker_t * wrk)
298{
299 app_session_evt_t _app_evt, *app_evt = &_app_evt;
300 session_app_detach_msg_t *mp;
301 svm_msg_q_t *mq;
302
303 mq = vcl_worker_ctrl_mq (wrk);
304 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
305 mp = (session_app_detach_msg_t *) app_evt->evt->data;
306 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700307 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700308 app_send_ctrl_evt_to_vpp (mq, app_evt);
309}
Florin Coras697faea2018-06-27 17:10:49 -0700310
Florin Coras54693d22018-07-17 10:46:29 -0700311static void
312vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
313 session_handle_t handle, int retval)
314{
315 app_session_evt_t _app_evt, *app_evt = &_app_evt;
316 session_accepted_reply_msg_t *rmp;
317 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
318 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
319 rmp->handle = handle;
320 rmp->context = context;
321 rmp->retval = retval;
322 app_send_ctrl_evt_to_vpp (mq, app_evt);
323}
324
Florin Coras99368312018-08-02 10:45:44 -0700325static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800326vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
327 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700328{
329 app_session_evt_t _app_evt, *app_evt = &_app_evt;
330 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800331 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700332 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
333 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800334 rmp->handle = s->vpp_handle;
335 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700336 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800337 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700338}
339
Florin Corasc9fbd662018-08-24 12:59:56 -0700340static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800341vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
342 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700343{
344 app_session_evt_t _app_evt, *app_evt = &_app_evt;
345 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800346 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
347 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700348 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800349 rmp->handle = s->vpp_handle;
350 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700351 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800352 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700353}
354
Florin Coras30e79c22019-01-02 19:31:22 -0800355void
356vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
357 u32 wrk_index)
358{
359 app_session_evt_t _app_evt, *app_evt = &_app_evt;
360 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800361
Florin Coras52dd29f2020-11-18 19:02:17 -0800362 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
363 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800364 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700365 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800366 mp->handle = s->vpp_handle;
367 mp->req_wrk_index = wrk->vpp_wrk_index;
368 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800369 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800370}
371
hanlina3a48962020-07-13 11:09:15 +0800372int
Florin Coras40c07ce2020-07-16 20:46:17 -0700373vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
374{
375 app_session_evt_t _app_evt, *app_evt = &_app_evt;
376 session_app_wrk_rpc_msg_t *mp;
377 vcl_worker_t *dst_wrk, *wrk;
378 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800379 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700380
381 if (data_len > sizeof (mp->data))
382 goto done;
383
384 clib_spinlock_lock (&vcm->workers_lock);
385
386 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
387 if (!dst_wrk)
388 goto done;
389
390 wrk = vcl_worker_get_current ();
391 mq = vcl_worker_ctrl_mq (wrk);
392 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
393 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700394 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700395 mp->wrk_index = dst_wrk->vpp_wrk_index;
396 clib_memcpy (mp->data, data, data_len);
397 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800398 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700399
400done:
401 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800402 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700403}
404
Florin Coras04ae8272021-04-12 19:55:37 -0700405int
406vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
407 transport_endpt_attr_t *attr)
408{
409 app_session_evt_t _app_evt, *app_evt = &_app_evt;
410 session_transport_attr_msg_t *mp;
411 svm_msg_q_t *mq;
412 f64 timeout;
413
414 ASSERT (!wrk->session_attr_op);
415 wrk->session_attr_op = 1;
416 wrk->session_attr_op_rv = -1;
417
418 mq = s->vpp_evt_q;
419 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
420 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
421 memset (mp, 0, sizeof (*mp));
422 mp->client_index = wrk->api_client_handle;
423 mp->handle = s->vpp_handle;
424 mp->is_get = is_get;
425 mp->attr = *attr;
426 app_send_ctrl_evt_to_vpp (mq, app_evt);
427
428 timeout = clib_time_now (&wrk->clib_time) + 1;
429
430 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
431 vcl_flush_mq_events ();
432
433 if (!wrk->session_attr_op_rv && is_get)
434 *attr = wrk->session_attr_rv;
435
436 wrk->session_attr_op = 0;
437
438 return wrk->session_attr_op_rv;
439}
440
Florin Coras54693d22018-07-17 10:46:29 -0700441static u32
Florin Coras00cca802019-06-06 09:38:44 -0700442vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
443 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700444{
445 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700446 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700447
Florin Coras134a9962018-08-28 11:32:04 -0700448 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700449
Florin Coras00cca802019-06-06 09:38:44 -0700450 listen_session = vcl_session_get (wrk, ls_index);
451 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700452 {
Florin Coras00cca802019-06-06 09:38:44 -0700453 VDBG (0, "ERROR: listener handle %lu does not match session %u",
454 mp->listener_handle, ls_index);
455 goto error;
456 }
457
Florin Corasc547e912020-12-08 17:50:45 -0800458 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800459 mp->server_tx_fifo,
460 mp->vpp_event_queue_address, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700461 {
Florin Corasc547e912020-12-08 17:50:45 -0800462 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700463 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700464 }
465
Florin Coras54693d22018-07-17 10:46:29 -0700466 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700467 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700468 session->transport.rmt_port = mp->rmt.port;
469 session->transport.is_ip4 = mp->rmt.is_ip4;
470 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500471 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700472
Florin Coras134a9962018-08-28 11:32:04 -0700473 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800474 session->transport.lcl_port = mp->lcl.port;
475 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700476 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200477 session->is_dgram = vcl_proto_is_dgram (session->session_type);
478 session->listener_index = listen_session->session_index;
479 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700480
Florin Coras5e062572019-03-14 19:07:51 -0700481 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
482 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700483 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
484 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
485 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700486 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
487
Florin Coras00cca802019-06-06 09:38:44 -0700488 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
489 session->vpp_handle, 0);
490
Florin Coras134a9962018-08-28 11:32:04 -0700491 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700492
493error:
Florin Corasb4624182020-12-11 13:58:12 -0800494 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
495 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700496 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
497 VNET_API_ERROR_INVALID_ARGUMENT);
498 vcl_session_free (wrk, session);
499 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700500}
501
502static u32
Florin Coras134a9962018-08-28 11:32:04 -0700503vcl_session_connected_handler (vcl_worker_t * wrk,
504 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700505{
Florin Coras99368312018-08-02 10:45:44 -0700506 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800507 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700508
509 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700510 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700511 if (!session)
512 {
Florin Coras5e062572019-03-14 19:07:51 -0700513 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
514 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700515 return VCL_INVALID_SESSION_INDEX;
516 }
Florin Coras54693d22018-07-17 10:46:29 -0700517 if (mp->retval)
518 {
Florin Coras5e062572019-03-14 19:07:51 -0700519 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700520 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700521 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700522 session->vpp_handle = mp->handle;
523 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700524 }
525
Florin Corasdbc9c592019-09-25 16:37:43 -0700526 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800527
528 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800529 mp->server_tx_fifo,
530 mp->vpp_event_queue_address, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800531 {
Florin Corasc547e912020-12-08 17:50:45 -0800532 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700533 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700534 vcl_send_session_disconnect (wrk, session);
535 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800536 }
537
Florin Coras653e43f2019-03-04 10:56:23 -0800538 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700539 {
Florin Corasc547e912020-12-08 17:50:45 -0800540 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800541 mp->ct_tx_fifo, (uword) ~0, 1, session))
Florin Coras653e43f2019-03-04 10:56:23 -0800542 {
Florin Corasc547e912020-12-08 17:50:45 -0800543 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700544 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700545 vcl_send_session_disconnect (wrk, session);
546 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800547 }
Florin Coras99368312018-08-02 10:45:44 -0700548 }
Florin Coras54693d22018-07-17 10:46:29 -0700549
Florin Coras09d18c22019-04-24 11:10:02 -0700550 session->transport.is_ip4 = mp->lcl.is_ip4;
551 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500552 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700553 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700554
555 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700556 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700557 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700558 vcl_send_session_disconnect (wrk, session);
559 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700560 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700561
562 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800563 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700564
Florin Coras5e062572019-03-14 19:07:51 -0700565 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
566 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700567 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700568
Florin Coras54693d22018-07-17 10:46:29 -0700569 return session_index;
570}
571
Florin Coras3c7d4f92018-12-14 11:28:43 -0800572static int
573vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
574{
575 vcl_session_msg_t *accepted_msg;
576 int i;
577
578 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
579 {
580 accepted_msg = &session->accept_evts_fifo[i];
581 if (accepted_msg->accepted_msg.handle == handle)
582 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800583 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800584 return 1;
585 }
586 }
587 return 0;
588}
589
Florin Corasc9fbd662018-08-24 12:59:56 -0700590static u32
Florin Coras134a9962018-08-28 11:32:04 -0700591vcl_session_reset_handler (vcl_worker_t * wrk,
592 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700593{
594 vcl_session_t *session;
595 u32 sid;
596
Florin Coras134a9962018-08-28 11:32:04 -0700597 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
598 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700599 if (!session)
600 {
601 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
602 return VCL_INVALID_SESSION_INDEX;
603 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800604
605 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700606 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800607 {
608
609 if (!vcl_flag_accepted_session (session, reset_msg->handle,
610 VCL_ACCEPTED_F_RESET))
611 VDBG (0, "session was not accepted!");
612 return VCL_INVALID_SESSION_INDEX;
613 }
614
Florin Corasc127d5a2020-10-14 16:35:58 -0700615 if (session->session_state != VCL_STATE_CLOSED)
616 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800617 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700618 return sid;
619}
620
Florin Coras60116992018-08-27 09:52:18 -0700621static u32
Florin Coras134a9962018-08-28 11:32:04 -0700622vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700623{
624 vcl_session_t *session;
625 u32 sid = mp->context;
626
Florin Coras134a9962018-08-28 11:32:04 -0700627 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700628 if (mp->retval)
629 {
Florin Coras5e062572019-03-14 19:07:51 -0700630 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700631 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700632 if (session)
633 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700634 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700635 session->vpp_handle = mp->handle;
636 return sid;
637 }
638 else
639 {
Florin Coras5e062572019-03-14 19:07:51 -0700640 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
641 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700642 return VCL_INVALID_SESSION_INDEX;
643 }
644 }
645
646 session->vpp_handle = mp->handle;
647 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500648 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
649 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700650 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700651 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700652 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800653
Florin Coras6e3c1f82020-01-15 01:30:46 +0000654 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700655 {
Florin Corasc547e912020-12-08 17:50:45 -0800656 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800657 mp->tx_fifo, mp->vpp_evt_q, 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800658 {
659 VDBG (0, "failed to attach fifos for %u", session->session_index);
660 session->session_state = VCL_STATE_DETACHED;
661 return VCL_INVALID_SESSION_INDEX;
662 }
Florin Coras60116992018-08-27 09:52:18 -0700663 }
664
Florin Coras05ecfcc2018-12-12 18:19:39 -0800665 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700666 return sid;
667}
668
Florin Corasdfae9f92019-02-20 19:48:31 -0800669static void
670vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
671{
672 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
673 vcl_session_t *s;
674
675 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000676 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800677 {
678 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
679 return;
680 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700681 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000682 {
683 /* Connected udp listener */
684 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700685 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000686 return;
687
688 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
689 return;
690 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800691
692 if (mp->retval)
693 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700694 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800695
696 if (mp->context != wrk->wrk_index)
697 VDBG (0, "wrong context");
698
699 vcl_session_table_del_vpp_handle (wrk, mp->handle);
700 vcl_session_free (wrk, s);
701}
702
Florin Coras68b7e582020-01-21 18:33:23 -0800703static void
704vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
705{
706 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
707 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800708 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800709
710 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
711 if (!s)
712 {
713 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
714 return;
715 }
716
Florin Coras1bb74942021-02-10 15:26:37 -0800717 /* Only validate if a value is provided */
718 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800719 {
Florin Coras1bb74942021-02-10 15:26:37 -0800720 fs_index = vcl_segment_table_lookup (mp->segment_handle);
721 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
722 {
723 VDBG (0, "segment %lx for session %u is not mounted!",
724 mp->segment_handle, s->session_index);
725 s->session_state = VCL_STATE_DETACHED;
726 return;
727 }
Florin Corasc547e912020-12-08 17:50:45 -0800728 }
729
Florin Coras57660d92020-04-04 22:45:34 +0000730 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800731
732 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
733 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800734
Florin Coras68b7e582020-01-21 18:33:23 -0800735 vcl_session_table_del_vpp_handle (wrk, mp->handle);
736 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
737
738 /* Generate new tx event if we have outstanding data */
739 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800740 app_send_io_evt_to_vpp (s->vpp_evt_q,
741 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800742 SESSION_IO_EVT_TX, SVM_Q_WAIT);
743
Florin Coras57660d92020-04-04 22:45:34 +0000744 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800745 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800746}
747
Florin Coras3c7d4f92018-12-14 11:28:43 -0800748static vcl_session_t *
749vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
750{
751 vcl_session_msg_t *vcl_msg;
752 vcl_session_t *session;
753
754 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
755 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800756 VWRN ("session overlap handle %lu state %u!", msg->handle,
757 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800758
759 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
760 if (!session)
761 {
762 VERR ("couldn't find listen session: listener handle %llx",
763 msg->listener_handle);
764 return 0;
765 }
766
767 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000768 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800769 vcl_msg->accepted_msg = *msg;
770 /* Session handle points to listener until fully accepted by app */
771 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
772
773 return session;
774}
775
776static vcl_session_t *
777vcl_session_disconnected_handler (vcl_worker_t * wrk,
778 session_disconnected_msg_t * msg)
779{
780 vcl_session_t *session;
781
782 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
783 if (!session)
784 {
785 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
786 return 0;
787 }
788
Florin Corasadcfb152020-02-12 08:50:29 +0000789 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700790 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000791 return 0;
792
Florin Coras3c7d4f92018-12-14 11:28:43 -0800793 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700794 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800795 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800796 if (!vcl_flag_accepted_session (session, msg->handle,
797 VCL_ACCEPTED_F_CLOSED))
798 VDBG (0, "session was not accepted!");
799 return 0;
800 }
801
Florin Corasadcfb152020-02-12 08:50:29 +0000802 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700803 if (session->session_state != VCL_STATE_DISCONNECT)
804 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000805
Florin Coras3c7d4f92018-12-14 11:28:43 -0800806 return session;
807}
808
liuyacan534468e2021-05-09 03:50:40 +0000809int
liuyacan55c952e2021-06-13 14:54:55 +0800810vppcom_session_shutdown (uint32_t session_handle, int how)
liuyacan534468e2021-05-09 03:50:40 +0000811{
812 vcl_worker_t *wrk = vcl_worker_get_current ();
813 vcl_session_t *session;
814 vcl_session_state_t state;
815 u64 vpp_handle;
816
817 session = vcl_session_get_w_handle (wrk, session_handle);
818 if (PREDICT_FALSE (!session))
819 return VPPCOM_EBADFD;
820
821 vpp_handle = session->vpp_handle;
822 state = session->session_state;
823
824 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
825 vpp_handle, state, vppcom_session_state_str (state));
826
827 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
828 {
829 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
830 return VPPCOM_EBADFD;
831 }
832
liuyacan55c952e2021-06-13 14:54:55 +0800833 if (how == SHUT_RD || how == SHUT_RDWR)
834 {
835 session->flags |= VCL_SESSION_F_RD_SHUTDOWN;
836 if (how == SHUT_RD)
837 return VPPCOM_OK;
838 }
839 session->flags |= VCL_SESSION_F_WR_SHUTDOWN;
840
liuyacan534468e2021-05-09 03:50:40 +0000841 if (PREDICT_TRUE (state == VCL_STATE_READY))
842 {
843 VDBG (1, "session %u [0x%llx]: sending shutdown...",
844 session->session_index, vpp_handle);
845
846 vcl_send_session_shutdown (wrk, session);
liuyacan534468e2021-05-09 03:50:40 +0000847 }
848
849 return VPPCOM_OK;
850}
851
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700852static int
853vppcom_session_disconnect (u32 session_handle)
854{
855 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700856 vcl_session_t *session, *listen_session;
857 vcl_session_state_t state;
858 u64 vpp_handle;
859
860 session = vcl_session_get_w_handle (wrk, session_handle);
861 if (!session)
862 return VPPCOM_EBADFD;
863
864 vpp_handle = session->vpp_handle;
865 state = session->session_state;
866
867 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
868 vpp_handle, state, vppcom_session_state_str (state));
869
870 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
871 {
872 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
873 return VPPCOM_EBADFD;
874 }
875
876 if (state == VCL_STATE_VPP_CLOSING)
877 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800878 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700879 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
880 session->session_index, vpp_handle);
881 }
882 else
883 {
884 /* Session doesn't have an event queue yet. Probably a non-blocking
885 * connect. Wait for the reply */
886 if (PREDICT_FALSE (!session->vpp_evt_q))
887 return VPPCOM_OK;
888
889 VDBG (1, "session %u [0x%llx]: sending disconnect...",
890 session->session_index, vpp_handle);
891 vcl_send_session_disconnect (wrk, session);
892 }
893
894 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
895 {
896 listen_session = vcl_session_get (wrk, session->listener_index);
897 listen_session->n_accepted_sessions--;
898 }
899
900 return VPPCOM_OK;
901}
902
Florin Coras30e79c22019-01-02 19:31:22 -0800903static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700904vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
905{
906 session_cleanup_msg_t *msg;
907 vcl_session_t *session;
908
909 msg = (session_cleanup_msg_t *) data;
910 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
911 if (!session)
912 {
913 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
914 return;
915 }
916
Florin Coras36d49392020-04-24 23:00:11 +0000917 if (msg->type == SESSION_CLEANUP_TRANSPORT)
918 {
919 /* Transport was cleaned up before we confirmed close. Probably the
920 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700921 * Confirm close to make sure everything is cleaned up.
922 * Move to undetermined state to ensure that the session is not
923 * removed before both vpp and the app cleanup.
924 * - If the app closes first, the session is moved to CLOSED state
925 * and the session cleanup notification from vpp removes the
926 * session.
927 * - If vpp cleans up the session first, the session is moved to
928 * DETACHED state lower and subsequently the close from the app
929 * frees the session
930 */
931 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700932 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700933 vppcom_session_disconnect (vcl_session_handle (session));
934 session->session_state = VCL_STATE_UPDATED;
935 }
936 else if (session->session_state == VCL_STATE_DISCONNECT)
937 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800938 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700939 session->session_state = VCL_STATE_UPDATED;
940 }
Florin Coras36d49392020-04-24 23:00:11 +0000941 return;
942 }
943
Florin Coras9ace36d2019-10-28 13:14:17 -0700944 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000945 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700946 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000947 {
948 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700949 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000950 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
951 return;
952 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700953 vcl_session_free (wrk, session);
954}
955
956static void
Florin Coras30e79c22019-01-02 19:31:22 -0800957vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
958{
959 session_req_worker_update_msg_t *msg;
960 vcl_session_t *s;
961
962 msg = (session_req_worker_update_msg_t *) data;
963 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
964 if (!s)
965 return;
966
967 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
968}
969
970static void
971vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
972{
973 session_worker_update_reply_msg_t *msg;
974 vcl_session_t *s;
975
976 msg = (session_worker_update_reply_msg_t *) data;
977 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
978 if (!s)
979 {
980 VDBG (0, "unknown handle 0x%llx", msg->handle);
981 return;
982 }
Florin Coras30e79c22019-01-02 19:31:22 -0800983
Florin Coras5f45e012019-01-23 09:21:30 -0800984 if (s->rx_fifo)
985 {
Florin Corasc547e912020-12-08 17:50:45 -0800986 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800987 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800988 {
989 VDBG (0, "failed to attach fifos for %u", s->session_index);
990 return;
991 }
Florin Coras5f45e012019-01-23 09:21:30 -0800992 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700993 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800994
Florin Coras30e79c22019-01-02 19:31:22 -0800995 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
996 s->vpp_handle, wrk->wrk_index);
997}
998
Florin Coras935ce752020-09-08 22:43:47 -0700999static int
1000vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
1001{
1002
1003 if (vcm->cfg.vpp_app_socket_api)
1004 return vcl_sapi_recv_fds (wrk, fds, n_fds);
1005
1006 return vcl_bapi_recv_fds (wrk, fds, n_fds);
1007}
1008
Florin Corasc4c4cf52019-08-24 18:17:34 -07001009static void
1010vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
1011{
1012 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
1013 session_app_add_segment_msg_t *msg;
1014 u64 segment_handle;
1015 int fd = -1;
1016
1017 msg = (session_app_add_segment_msg_t *) data;
1018
1019 if (msg->fd_flags)
1020 {
Florin Coras935ce752020-09-08 22:43:47 -07001021 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -07001022 seg_type = SSVM_SEGMENT_MEMFD;
1023 }
1024
1025 segment_handle = msg->segment_handle;
1026 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
1027 {
1028 clib_warning ("invalid segment handle");
1029 return;
1030 }
1031
1032 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
1033 seg_type, fd))
1034 {
1035 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
1036 return;
1037 }
1038
1039 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
1040 msg->segment_size);
1041}
1042
1043static void
1044vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
1045{
1046 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
1047 vcl_segment_detach (msg->segment_handle);
1048 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
1049}
1050
Florin Coras40c07ce2020-07-16 20:46:17 -07001051static void
1052vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
1053{
1054 if (!vcm->wrk_rpc_fn)
1055 return;
1056
hanlina3a48962020-07-13 11:09:15 +08001057 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -07001058}
1059
Florin Coras04ae8272021-04-12 19:55:37 -07001060static void
1061vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
1062{
1063 session_transport_attr_reply_msg_t *mp;
1064
1065 if (!wrk->session_attr_op)
1066 return;
1067
1068 mp = (session_transport_attr_reply_msg_t *) data;
1069
1070 wrk->session_attr_op_rv = mp->retval;
1071 wrk->session_attr_op = 0;
1072 wrk->session_attr_rv = mp->attr;
1073}
1074
Florin Coras86f04502018-09-12 16:08:01 -07001075static int
1076vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001077{
Florin Coras54693d22018-07-17 10:46:29 -07001078 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001079 session_connected_msg_t *connected_msg;
1080 session_reset_msg_t *reset_msg;
1081 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001082 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001083 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001084
1085 switch (e->event_type)
1086 {
Florin Coras653e43f2019-03-04 10:56:23 -08001087 case SESSION_IO_EVT_RX:
1088 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001089 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001090 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001091 break;
Florin Coras86f04502018-09-12 16:08:01 -07001092 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001093 break;
Florin Corasb242d312020-10-26 15:35:40 -07001094 case SESSION_CTRL_EVT_BOUND:
1095 /* We can only wait for only one listen so not postponed */
1096 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1097 break;
Florin Coras54693d22018-07-17 10:46:29 -07001098 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001099 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1100 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1101 {
1102 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1103 *ecpy = *e;
1104 ecpy->postponed = 1;
1105 ecpy->session_index = s->session_index;
1106 }
Florin Coras54693d22018-07-17 10:46:29 -07001107 break;
1108 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001109 connected_msg = (session_connected_msg_t *) e->data;
1110 sid = vcl_session_connected_handler (wrk, connected_msg);
1111 if (!(s = vcl_session_get (wrk, sid)))
1112 break;
1113 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1114 {
1115 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1116 *ecpy = *e;
1117 ecpy->postponed = 1;
1118 ecpy->session_index = s->session_index;
1119 }
Florin Coras54693d22018-07-17 10:46:29 -07001120 break;
1121 case SESSION_CTRL_EVT_DISCONNECTED:
1122 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001123 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1124 break;
1125 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1126 {
1127 vec_add1 (wrk->unhandled_evts_vector, *e);
1128 break;
1129 }
1130 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001131 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001132 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1133 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001134 break;
1135 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001136 reset_msg = (session_reset_msg_t *) e->data;
1137 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1138 break;
1139 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1140 {
1141 vec_add1 (wrk->unhandled_evts_vector, *e);
1142 break;
1143 }
Florin Coras134a9962018-08-28 11:32:04 -07001144 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001145 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001146 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1147 vcl_session_unlisten_reply_handler (wrk, e->data);
1148 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001149 case SESSION_CTRL_EVT_MIGRATED:
1150 vcl_session_migrated_handler (wrk, e->data);
1151 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001152 case SESSION_CTRL_EVT_CLEANUP:
1153 vcl_session_cleanup_handler (wrk, e->data);
1154 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001155 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1156 vcl_session_req_worker_update_handler (wrk, e->data);
1157 break;
1158 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1159 vcl_session_worker_update_reply_handler (wrk, e->data);
1160 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001161 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1162 vcl_session_app_add_segment_handler (wrk, e->data);
1163 break;
1164 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1165 vcl_session_app_del_segment_handler (wrk, e->data);
1166 break;
hanlina3a48962020-07-13 11:09:15 +08001167 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001168 vcl_worker_rpc_handler (wrk, e->data);
1169 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001170 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1171 vcl_session_transport_attr_reply_handler (wrk, e->data);
1172 break;
Florin Coras54693d22018-07-17 10:46:29 -07001173 default:
1174 clib_warning ("unhandled %u", e->event_type);
1175 }
1176 return VPPCOM_OK;
1177}
1178
Florin Coras30e79c22019-01-02 19:31:22 -08001179static int
Florin Coras697faea2018-06-27 17:10:49 -07001180vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001181 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001182 f64 wait_for_time)
1183{
Florin Coras134a9962018-08-28 11:32:04 -07001184 vcl_worker_t *wrk = vcl_worker_get_current ();
1185 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001186 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001187 svm_msg_q_msg_t msg;
1188 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001189
Florin Coras697faea2018-06-27 17:10:49 -07001190 do
Dave Wallace543852a2017-08-03 02:11:34 -04001191 {
Florin Coras134a9962018-08-28 11:32:04 -07001192 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001193 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001194 {
Florin Coras070453d2018-08-24 17:04:27 -07001195 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001196 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001197 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001198 {
Florin Coras697faea2018-06-27 17:10:49 -07001199 return VPPCOM_OK;
1200 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001201 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001202 {
Florin Coras697faea2018-06-27 17:10:49 -07001203 return VPPCOM_ECONNREFUSED;
1204 }
Florin Coras54693d22018-07-17 10:46:29 -07001205
Florin Coras134a9962018-08-28 11:32:04 -07001206 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001207 {
1208 usleep (100);
1209 continue;
1210 }
Florin Coras134a9962018-08-28 11:32:04 -07001211 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001212 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001213 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001214 }
Florin Coras134a9962018-08-28 11:32:04 -07001215 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001216
Florin Coras05ecfcc2018-12-12 18:19:39 -08001217 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001218 vppcom_session_state_str (state));
1219 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001220
Florin Coras697faea2018-06-27 17:10:49 -07001221 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001222}
1223
Florin Coras30e79c22019-01-02 19:31:22 -08001224static void
1225vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1226{
Florin Coras288eaab2019-02-03 15:26:14 -08001227 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001228 vcl_session_t *s;
1229 u32 *sip;
1230
1231 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1232 return;
1233
1234 vec_foreach (sip, wrk->pending_session_wrk_updates)
1235 {
1236 s = vcl_session_get (wrk, *sip);
1237 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1238 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001239 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1240 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001241 s->session_state = state;
1242 }
1243 vec_reset_length (wrk->pending_session_wrk_updates);
1244}
1245
Florin Corasf9240dc2019-01-15 08:03:17 -08001246void
Florin Coras5398dfb2021-01-25 20:31:27 -08001247vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001248{
Florin Coras30e79c22019-01-02 19:31:22 -08001249 svm_msg_q_msg_t *msg;
1250 session_event_t *e;
1251 svm_msg_q_t *mq;
1252 int i;
1253
1254 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001255 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001256
1257 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1258 {
1259 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1260 e = svm_msg_q_msg_data (mq, msg);
1261 vcl_handle_mq_event (wrk, e);
1262 svm_msg_q_free_msg (mq, msg);
1263 }
1264 vec_reset_length (wrk->mq_msg_vector);
1265 vcl_handle_pending_wrk_updates (wrk);
1266}
1267
Florin Coras5398dfb2021-01-25 20:31:27 -08001268void
1269vcl_flush_mq_events (void)
1270{
1271 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1272}
1273
Florin Coras697faea2018-06-27 17:10:49 -07001274static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001275vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001276{
Florin Coras134a9962018-08-28 11:32:04 -07001277 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001278 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001279 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001280 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001281
Florin Corasab2f6db2018-08-31 14:31:41 -07001282 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001283 if (!session)
1284 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001285
Florin Corasaaff5ee2019-07-08 13:00:00 -07001286 /* Flush pending accept events, if any */
1287 while (clib_fifo_elts (session->accept_evts_fifo))
1288 {
1289 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1290 accepted_msg = &evt->accepted_msg;
1291 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1292 vcl_send_session_accepted_reply (session->vpp_evt_q,
1293 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001294 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001295 }
1296 clib_fifo_free (session->accept_evts_fifo);
1297
Florin Coras458089b2019-08-21 16:20:44 -07001298 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001299
Florin Coras5e062572019-03-14 19:07:51 -07001300 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001301 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001302 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001303
1304 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001305 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001306
Florin Coras070453d2018-08-24 17:04:27 -07001307 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001308}
1309
Florin Coras940f78f2018-11-30 12:11:20 -08001310/**
1311 * Handle app exit
1312 *
1313 * Notify vpp of the disconnect and mark the worker as free. If we're the
1314 * last worker, do a full cleanup otherwise, since we're probably a forked
1315 * child, avoid syscalls as much as possible. We might've lost privileges.
1316 */
1317void
1318vppcom_app_exit (void)
1319{
1320 if (!pool_elts (vcm->workers))
1321 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001322 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1323 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001324 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001325}
1326
Florin Coras935ce752020-09-08 22:43:47 -07001327static int
1328vcl_api_attach (void)
1329{
1330 if (vcm->cfg.vpp_app_socket_api)
1331 return vcl_sapi_attach ();
1332
1333 return vcl_bapi_attach ();
1334}
1335
1336static void
1337vcl_api_detach (vcl_worker_t * wrk)
1338{
1339 vcl_send_app_detach (wrk);
1340
1341 if (vcm->cfg.vpp_app_socket_api)
1342 return vcl_sapi_detach (wrk);
1343
1344 return vcl_bapi_disconnect_from_vpp ();
1345}
1346
Dave Wallace543852a2017-08-03 02:11:34 -04001347/*
1348 * VPPCOM Public API functions
1349 */
1350int
Florin Coras66ec4672020-06-15 07:59:40 -07001351vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001352{
Dave Wallace543852a2017-08-03 02:11:34 -04001353 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001354 int rv;
1355
Florin Coras47c40e22018-11-26 17:01:36 -08001356 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001357 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001358 VDBG (1, "already initialized");
1359 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001360 }
1361
Florin Coras47c40e22018-11-26 17:01:36 -08001362 vcm->is_init = 1;
1363 vppcom_cfg (&vcm->cfg);
1364 vcl_cfg = &vcm->cfg;
1365
1366 vcm->main_cpu = pthread_self ();
1367 vcm->main_pid = getpid ();
1368 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001369 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1370 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001371 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1372 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001373 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001374 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001375 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001376
1377 /* Allocate default worker */
1378 vcl_worker_alloc_and_init ();
1379
Florin Coras935ce752020-09-08 22:43:47 -07001380 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001381 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001382
1383 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001384 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001385
1386 return VPPCOM_OK;
1387}
1388
1389void
1390vppcom_app_destroy (void)
1391{
Florin Corasdc0ded72020-04-30 02:59:55 +00001392 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001393 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001394
Florin Coras940f78f2018-11-30 12:11:20 -08001395 if (!pool_elts (vcm->workers))
1396 return;
1397
Florin Coras0d427d82018-06-27 03:24:07 -07001398 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001399
Florin Corasdc0ded72020-04-30 02:59:55 +00001400 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001401
Florin Corasce815de2020-04-16 18:47:27 +00001402 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001403 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001404 if (current_wrk != wrk)
1405 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001406 }
Florin Corasce815de2020-04-16 18:47:27 +00001407 /* *INDENT-ON* */
1408
Florin Coras935ce752020-09-08 22:43:47 -07001409 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001410 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1411
Florin Coras0d427d82018-06-27 03:24:07 -07001412 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001413
1414 /*
1415 * Free the heap and fix vcm
1416 */
1417 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001418 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001419
1420 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001421 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001422}
1423
1424int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001425vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001426{
Florin Coras134a9962018-08-28 11:32:04 -07001427 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001428 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001429
Florin Coras134a9962018-08-28 11:32:04 -07001430 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001431
Florin Coras7e12d942018-06-27 14:32:43 -07001432 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001433 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001434 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001435 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001436
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001437 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001438 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001439
Florin Coras7e12d942018-06-27 14:32:43 -07001440 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1441 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001442
Florin Coras5e062572019-03-14 19:07:51 -07001443 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001444
Florin Coras134a9962018-08-28 11:32:04 -07001445 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001446}
1447
Florin Corasfe286f72021-06-04 10:07:55 -07001448static void
1449vcl_epoll_wait_clean_lt (vcl_worker_t *wrk, u32 sid)
1450{
1451 int i;
1452
1453 for (i = vec_len (wrk->ep_level_evts) - 1; i >= 0; i--)
1454 {
1455 if (wrk->ep_level_evts[i] == sid)
1456 vec_del1 (wrk->ep_level_evts, i);
1457 }
1458}
1459
Dave Wallace543852a2017-08-03 02:11:34 -04001460int
Florin Corasc127d5a2020-10-14 16:35:58 -07001461vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001462 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001463{
Florin Coras134a9962018-08-28 11:32:04 -07001464 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001465
Florin Coras6c3b2182020-10-19 18:36:48 -07001466 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001467
Florin Coras6c3b2182020-10-19 18:36:48 -07001468 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001469 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001470 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001471 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001472 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001473 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001474 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001475 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001476 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1477 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001478 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001479 }
Florin Corase4306b62020-10-28 12:51:10 -07001480 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001481 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001482
Florin Coras6c3b2182020-10-19 18:36:48 -07001483 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001484 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001485 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001486 if (rv < 0)
1487 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001488 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1489 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasfe286f72021-06-04 10:07:55 -07001490 if (PREDICT_FALSE (vec_len (wrk->ep_level_evts)))
1491 vcl_epoll_wait_clean_lt (wrk, s->session_index);
Dave Wallace19481612017-09-15 18:47:44 -04001492 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001493
Florin Coras9ace36d2019-10-28 13:14:17 -07001494 if (!do_disconnect)
1495 {
1496 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001497 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001498 goto cleanup;
1499 }
1500
Florin Coras6c3b2182020-10-19 18:36:48 -07001501 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001502 {
1503 rv = vppcom_session_unbind (sh);
1504 if (PREDICT_FALSE (rv < 0))
1505 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001506 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001507 vppcom_retval_str (rv));
1508 return rv;
1509 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001510 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001511 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001512 {
1513 rv = vppcom_session_disconnect (sh);
1514 if (PREDICT_FALSE (rv < 0))
1515 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001516 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001517 rv, vppcom_retval_str (rv));
1518 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001519 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001520 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001521 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001522 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001523 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001524 {
1525 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001526 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001527 goto free_session;
1528 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001529
Florin Corasc127d5a2020-10-14 16:35:58 -07001530 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001531
Florin Coras9ace36d2019-10-28 13:14:17 -07001532 /* Session is removed only after vpp confirms the disconnect */
1533 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001534
Florin Corasf9240dc2019-01-15 08:03:17 -08001535cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001536 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001537free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001538 vcl_session_free (wrk, s);
1539 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001540
Dave Wallace543852a2017-08-03 02:11:34 -04001541 return rv;
1542}
1543
1544int
Florin Corasf9240dc2019-01-15 08:03:17 -08001545vppcom_session_close (uint32_t session_handle)
1546{
1547 vcl_worker_t *wrk = vcl_worker_get_current ();
1548 vcl_session_t *session;
1549
1550 session = vcl_session_get_w_handle (wrk, session_handle);
1551 if (!session)
1552 return VPPCOM_EBADFD;
1553 return vcl_session_cleanup (wrk, session, session_handle,
1554 1 /* do_disconnect */ );
1555}
1556
1557int
Florin Coras134a9962018-08-28 11:32:04 -07001558vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001559{
Florin Coras134a9962018-08-28 11:32:04 -07001560 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001561 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001562
1563 if (!ep || !ep->ip)
1564 return VPPCOM_EINVAL;
1565
Florin Coras134a9962018-08-28 11:32:04 -07001566 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001567 if (!session)
1568 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001569
Florin Coras6c3b2182020-10-19 18:36:48 -07001570 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001571 {
Florin Coras5e062572019-03-14 19:07:51 -07001572 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1573 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001574 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001575 }
1576
Florin Coras7e12d942018-06-27 14:32:43 -07001577 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001578 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001579 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1580 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001581 else
Dave Barach178cf492018-11-13 16:34:13 -05001582 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1583 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001584 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001585
Florin Coras5e062572019-03-14 19:07:51 -07001586 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1587 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001588 session->transport.is_ip4 ? "IPv4" : "IPv6",
1589 format_ip46_address, &session->transport.lcl_ip,
1590 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1591 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001592 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001593 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001594
1595 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001596 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001597
Florin Coras070453d2018-08-24 17:04:27 -07001598 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001599}
1600
1601int
Florin Coras134a9962018-08-28 11:32:04 -07001602vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001603{
Florin Coras134a9962018-08-28 11:32:04 -07001604 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001605 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001606 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001607 int rv;
1608
Florin Coras134a9962018-08-28 11:32:04 -07001609 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001610 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001611 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001612
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001613 if (q_len == 0 || q_len == ~0)
1614 q_len = vcm->cfg.listen_queue_size;
1615
Dave Wallaceee45d412017-11-24 21:44:06 -05001616 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001617 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001618 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001619 VDBG (0, "session %u [0x%llx]: already in listen state!",
1620 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001621 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001622 }
1623
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001624 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001625
Florin Coras070453d2018-08-24 17:04:27 -07001626 /*
1627 * Send listen request to vpp and wait for reply
1628 */
Florin Coras458089b2019-08-21 16:20:44 -07001629 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001630 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001631 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001632 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001633
Florin Coras070453d2018-08-24 17:04:27 -07001634 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001635 {
Florin Coras134a9962018-08-28 11:32:04 -07001636 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001637 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1638 listen_sh, listen_session->vpp_handle, rv,
1639 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001640 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001641 }
1642
Florin Coras070453d2018-08-24 17:04:27 -07001643 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001644}
1645
Florin Coras134a9962018-08-28 11:32:04 -07001646static int
Florin Coras5e062572019-03-14 19:07:51 -07001647validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001648{
Florin Coras6c3b2182020-10-19 18:36:48 -07001649 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001650 {
Florin Coras5e062572019-03-14 19:07:51 -07001651 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1652 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001653 return VPPCOM_EBADFD;
1654 }
1655
Florin Corasc127d5a2020-10-14 16:35:58 -07001656 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001657 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001658 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001659 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001660 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001661 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001662 return VPPCOM_EBADFD;
1663 }
1664 return VPPCOM_OK;
1665}
1666
1667int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001668vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1669{
1670 if (!strcmp (proto_str, "TCP"))
1671 *proto = VPPCOM_PROTO_TCP;
1672 else if (!strcmp (proto_str, "tcp"))
1673 *proto = VPPCOM_PROTO_TCP;
1674 else if (!strcmp (proto_str, "UDP"))
1675 *proto = VPPCOM_PROTO_UDP;
1676 else if (!strcmp (proto_str, "udp"))
1677 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001678 else if (!strcmp (proto_str, "TLS"))
1679 *proto = VPPCOM_PROTO_TLS;
1680 else if (!strcmp (proto_str, "tls"))
1681 *proto = VPPCOM_PROTO_TLS;
1682 else if (!strcmp (proto_str, "QUIC"))
1683 *proto = VPPCOM_PROTO_QUIC;
1684 else if (!strcmp (proto_str, "quic"))
1685 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001686 else if (!strcmp (proto_str, "DTLS"))
1687 *proto = VPPCOM_PROTO_DTLS;
1688 else if (!strcmp (proto_str, "dtls"))
1689 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001690 else if (!strcmp (proto_str, "SRTP"))
1691 *proto = VPPCOM_PROTO_SRTP;
1692 else if (!strcmp (proto_str, "srtp"))
1693 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001694 else
1695 return 1;
1696 return 0;
1697}
1698
1699int
Florin Coras134a9962018-08-28 11:32:04 -07001700vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001701 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001702{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001703 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001704 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001705 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001706 vcl_session_t *listen_session = 0;
1707 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001708 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001709 u8 is_nonblocking;
1710 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001711
Florin Coras5398dfb2021-01-25 20:31:27 -08001712again:
1713
Florin Coras134a9962018-08-28 11:32:04 -07001714 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001715 if (!listen_session)
1716 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001717
Florin Coras134a9962018-08-28 11:32:04 -07001718 listen_session_index = listen_session->session_index;
1719 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001720 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001721
Florin Coras54693d22018-07-17 10:46:29 -07001722 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001723 {
Florin Coras54693d22018-07-17 10:46:29 -07001724 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001725 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001726 accepted_msg = evt->accepted_msg;
1727 goto handle;
1728 }
1729
Florin Corasac422d62020-10-19 20:51:36 -07001730 is_nonblocking = vcl_session_has_attr (listen_session,
1731 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001732 while (1)
1733 {
Carl Smith592a9092019-11-12 14:57:37 +13001734 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1735 return VPPCOM_EAGAIN;
1736
Florin Coras5398dfb2021-01-25 20:31:27 -08001737 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1738 vcl_worker_flush_mq_events (wrk);
1739 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001740 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001741
Florin Coras54693d22018-07-17 10:46:29 -07001742handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001743
Florin Coras00cca802019-06-06 09:38:44 -07001744 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1745 listen_session_index);
1746 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1747 return VPPCOM_ECONNABORTED;
1748
Florin Coras134a9962018-08-28 11:32:04 -07001749 listen_session = vcl_session_get (wrk, listen_session_index);
1750 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001751
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001752 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001753 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001754
Florin Coras5e062572019-03-14 19:07:51 -07001755 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1756 " flags %d, is_nonblocking %u", listen_session->session_index,
1757 listen_session->vpp_handle, client_session_index,
1758 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001759 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001760
Dave Wallace048b1d62018-01-03 22:24:41 -05001761 if (ep)
1762 {
Florin Coras7e12d942018-06-27 14:32:43 -07001763 ep->is_ip4 = client_session->transport.is_ip4;
1764 ep->port = client_session->transport.rmt_port;
1765 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001766 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1767 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001768 else
Dave Barach178cf492018-11-13 16:34:13 -05001769 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1770 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001771 }
Dave Wallace60caa062017-11-10 17:07:13 -05001772
Florin Coras05ecfcc2018-12-12 18:19:39 -08001773 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001774 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001775 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001776 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001777 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001778 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001779 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001780 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001781 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001782 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1783 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001784
Florin Coras3c7d4f92018-12-14 11:28:43 -08001785 /*
1786 * Session might have been closed already
1787 */
1788 if (accept_flags)
1789 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001790 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001791 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001792 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001793 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001794 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001795 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001796}
1797
1798int
Florin Coras134a9962018-08-28 11:32:04 -07001799vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001800{
Florin Coras134a9962018-08-28 11:32:04 -07001801 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001802 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001803 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001804 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001805
Florin Coras134a9962018-08-28 11:32:04 -07001806 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001807 if (!session)
1808 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001809 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001810
Florin Coras6c3b2182020-10-19 18:36:48 -07001811 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001812 {
Florin Coras5e062572019-03-14 19:07:51 -07001813 VDBG (0, "ERROR: cannot connect epoll session %u!",
1814 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001815 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001816 }
1817
Florin Corasc127d5a2020-10-14 16:35:58 -07001818 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001819 {
Florin Coras7baeb712019-01-04 17:05:43 -08001820 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001821 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001822 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001823 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001824 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001825 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001826 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001827 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001828 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001829 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001830 }
1831
Florin Coras0a1e1832020-03-29 18:54:04 +00001832 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001833 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001834 {
1835 if (session->session_type != VPPCOM_PROTO_UDP)
1836 return VPPCOM_EINVAL;
1837 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001838 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001839 }
1840
Florin Coras7e12d942018-06-27 14:32:43 -07001841 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001842 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001843 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001844 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001845 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001846
Florin Coras0a1e1832020-03-29 18:54:04 +00001847 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001848 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001849 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001850 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001851 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001852 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001853 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001854 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001855 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001856
Florin Coras458089b2019-08-21 16:20:44 -07001857 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001858
Florin Corasac422d62020-10-19 20:51:36 -07001859 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001860 {
fanyfa49d59d2020-10-13 17:07:16 +08001861 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001862 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001863 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001864 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001865 return VPPCOM_EINPROGRESS;
1866 }
Florin Coras57c88932019-08-29 12:03:17 -07001867
1868 /*
1869 * Wait for reply from vpp if blocking
1870 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001871 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001872 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001873
Florin Coras134a9962018-08-28 11:32:04 -07001874 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001875 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1876 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001877
Dave Wallace4878cbe2017-11-21 03:45:09 -05001878 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001879}
1880
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001881int
1882vppcom_session_stream_connect (uint32_t session_handle,
1883 uint32_t parent_session_handle)
1884{
1885 vcl_worker_t *wrk = vcl_worker_get_current ();
1886 vcl_session_t *session, *parent_session;
1887 u32 session_index, parent_session_index;
1888 int rv;
1889
1890 session = vcl_session_get_w_handle (wrk, session_handle);
1891 if (!session)
1892 return VPPCOM_EBADFD;
1893 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1894 if (!parent_session)
1895 return VPPCOM_EBADFD;
1896
1897 session_index = session->session_index;
1898 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001899 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001900 {
1901 VDBG (0, "ERROR: cannot connect epoll session %u!",
1902 session->session_index);
1903 return VPPCOM_EBADFD;
1904 }
1905
Florin Corasc127d5a2020-10-14 16:35:58 -07001906 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001907 {
1908 VDBG (0, "session handle %u [0x%llx]: session already "
1909 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1910 session_handle, session->vpp_handle,
1911 parent_session_handle, parent_session->vpp_handle,
1912 vppcom_proto_str (session->session_type), session->session_state,
1913 vppcom_session_state_str (session->session_state));
1914 return VPPCOM_OK;
1915 }
1916
1917 /* Connect to quic session specifics */
1918 session->transport.is_ip4 = parent_session->transport.is_ip4;
1919 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1920 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001921 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001922
1923 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1924 session_handle, parent_session_handle, parent_session->vpp_handle);
1925
1926 /*
1927 * Send connect request and wait for reply from vpp
1928 */
Florin Coras458089b2019-08-21 16:20:44 -07001929 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001930 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001931 vcm->cfg.session_timeout);
1932
1933 session->listener_index = parent_session_index;
1934 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001935 if (parent_session)
1936 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001937
1938 session = vcl_session_get (wrk, session_index);
1939 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1940 session->vpp_handle, rv ? "failed" : "succeeded");
1941
1942 return rv;
1943}
1944
Steven58f464e2017-10-25 12:33:12 -07001945static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001946vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001947 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001948{
Florin Coras134a9962018-08-28 11:32:04 -07001949 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001950 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001951 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001952 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001953 session_event_t *e;
1954 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001955 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001956
Florin Coras070453d2018-08-24 17:04:27 -07001957 if (PREDICT_FALSE (!buf))
1958 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001959
Florin Coras134a9962018-08-28 11:32:04 -07001960 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001961 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001962 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001963
Florin Coras6d0106e2019-01-29 20:11:58 -08001964 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001965 {
Florin Coras5e062572019-03-14 19:07:51 -07001966 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001967 s->session_index, s->vpp_handle, s->session_state,
1968 vppcom_session_state_str (s->session_state));
1969 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001970 }
1971
liuyacan55c952e2021-06-13 14:54:55 +08001972 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
1973 {
1974 /* Vpp would ack the incoming data and enqueue it for reading.
1975 * So even if SHUT_RD is set, we can still read() the data if
1976 * the session is ready.
1977 */
1978 if (!vcl_session_read_ready (s))
1979 {
1980 return 0;
1981 }
1982 }
1983
Florin Corasac422d62020-10-19 20:51:36 -07001984 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001985 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001986 mq = wrk->app_event_queue;
1987 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001988 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001989
Sirshak Das28aa5392019-02-05 01:33:33 -06001990 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001991 {
Florin Coras54693d22018-07-17 10:46:29 -07001992 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001993 {
Yu Pingb2955352019-12-04 06:49:04 +08001994 if (vcl_session_is_closing (s))
1995 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001996 if (is_ct)
1997 svm_fifo_unset_event (s->rx_fifo);
1998 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001999 return VPPCOM_EWOULDBLOCK;
2000 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002001 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002002 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002003 if (vcl_session_is_closing (s))
2004 return vcl_session_closing_error (s);
2005
Florin Corasd6894562020-10-28 00:37:15 -07002006 if (is_ct)
2007 svm_fifo_unset_event (s->rx_fifo);
2008 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07002009
Florin Coras5398dfb2021-01-25 20:31:27 -08002010 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2011 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002012 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002013 }
Florin Coras54693d22018-07-17 10:46:29 -07002014
Florin Coras4a2c7942020-09-10 12:27:14 -07002015read_again:
2016
Florin Coras460dce62018-07-27 05:45:06 -07002017 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002018 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002019 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002020 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2021
2022 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002023
2024 if (peek)
2025 return rv;
2026
Florin Coras4a2c7942020-09-10 12:27:14 -07002027 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002028
Sirshak Das28aa5392019-02-05 01:33:33 -06002029 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002030 {
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 Corasc34118b2020-08-12 18:58:25 -07002034 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002035 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002036 {
Florin Corasc34118b2020-08-12 18:58:25 -07002037 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2038 e->event_type = SESSION_IO_EVT_RX;
2039 e->session_index = s->session_index;
2040 }
2041 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002042 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002043 {
2044 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002045 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002046 buf += rv;
2047 n -= rv;
2048 goto read_again;
2049 }
Florin Coras41c9e042018-09-11 00:10:41 -07002050
Florin Coras17ec5772020-08-12 20:46:29 -07002051 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002052 {
Florin Coras17ec5772020-08-12 20:46:29 -07002053 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002054 app_send_io_evt_to_vpp (s->vpp_evt_q,
2055 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002056 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002057 }
2058
Florin Coras5e062572019-03-14 19:07:51 -07002059 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2060 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002061
Florin Coras54693d22018-07-17 10:46:29 -07002062 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002063}
2064
Steven58f464e2017-10-25 12:33:12 -07002065int
Florin Coras134a9962018-08-28 11:32:04 -07002066vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002067{
Florin Coras134a9962018-08-28 11:32:04 -07002068 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002069}
2070
2071static int
Florin Coras134a9962018-08-28 11:32:04 -07002072vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002073{
Florin Coras134a9962018-08-28 11:32:04 -07002074 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002075}
2076
Florin Coras2cba8532018-09-11 16:33:36 -07002077int
2078vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002079 vppcom_data_segment_t * ds, uint32_t n_segments,
2080 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002081{
2082 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002083 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002084 vcl_session_t *s = 0;
2085 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002086 svm_msg_q_t *mq;
2087 u8 is_ct;
2088
2089 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002090 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002091 return VPPCOM_EBADFD;
2092
Florin Coras6d0106e2019-01-29 20:11:58 -08002093 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2094 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002095
Florin Corasac422d62020-10-19 20:51:36 -07002096 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002097 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002098 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002099 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002100 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002101
Sirshak Das28aa5392019-02-05 01:33:33 -06002102 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002103 {
2104 if (is_nonblocking)
2105 {
Florin Coras62877022020-10-28 21:22:04 -07002106 if (is_ct)
2107 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002108 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002109 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002110 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002111 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002112 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002113 if (vcl_session_is_closing (s))
2114 return vcl_session_closing_error (s);
2115
Florin Coras62877022020-10-28 21:22:04 -07002116 if (is_ct)
2117 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002118 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002119
Florin Coras5398dfb2021-01-25 20:31:27 -08002120 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2121 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002122 }
2123 }
2124
Florin Corasd1cc38d2020-10-11 11:05:04 -07002125 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2126 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2127 if (n_read < 0)
2128 return VPPCOM_EAGAIN;
2129
2130 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2131 {
Florin Coras62877022020-10-28 21:22:04 -07002132 if (is_ct)
2133 svm_fifo_unset_event (s->rx_fifo);
2134 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002135 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002136 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002137 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002138 {
2139 session_event_t *e;
2140 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2141 e->event_type = SESSION_IO_EVT_RX;
2142 e->session_index = s->session_index;
2143 }
2144 }
2145
2146 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002147 return n_read;
2148}
2149
2150void
Florin Corasd68faf82020-09-25 15:18:13 -07002151vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002152{
2153 vcl_worker_t *wrk = vcl_worker_get_current ();
2154 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002155 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002156
2157 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002158 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002159 return;
2160
Florin Coras62877022020-10-28 21:22:04 -07002161 is_ct = vcl_session_is_ct (s);
2162 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002163
2164 ASSERT (s->rx_bytes_pending < n_bytes);
2165 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002166}
2167
Florin Coras7a2abce2020-04-05 19:25:44 +00002168always_inline u8
2169vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002170{
Florin Coras7a2abce2020-04-05 19:25:44 +00002171 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2172 if (is_dgram)
2173 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2174 else
2175 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002176}
2177
2178always_inline int
2179vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2180 size_t n, u8 is_flush, u8 is_dgram)
2181{
Florin Coras6d0106e2019-01-29 20:11:58 -08002182 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002183 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002184 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002185 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002186 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002187
Florin Coras0b0d28e2021-06-04 17:31:53 -07002188 /* Accept zero length writes but just return */
2189 if (PREDICT_FALSE (!n))
2190 return VPPCOM_OK;
2191
2192 if (PREDICT_FALSE (!buf))
2193 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002194
Florin Coras6c3b2182020-10-19 18:36:48 -07002195 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002196 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002197 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2198 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002199 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002200 }
2201
liuyacan55c952e2021-06-13 14:54:55 +08002202 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002203 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002204 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2205 s->session_index, s->vpp_handle, s->session_state,
2206 vppcom_session_state_str (s->session_state));
2207 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002208 }
2209
liuyacan55c952e2021-06-13 14:54:55 +08002210 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2211 {
2212 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2213 s->session_index, s->vpp_handle, s->session_state,
2214 vppcom_session_state_str (s->session_state));
2215 return VPPCOM_EPIPE;
2216 }
2217
Florin Coras0e88e852018-09-17 22:09:02 -07002218 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002219 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002220 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002221
Florin Coras653e43f2019-03-04 10:56:23 -08002222 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002223 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002224 {
Florin Coras54693d22018-07-17 10:46:29 -07002225 if (is_nonblocking)
2226 {
Florin Coras070453d2018-08-24 17:04:27 -07002227 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002228 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002229 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002230 {
Florin Coras2d379d82019-06-28 12:45:12 -07002231 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002232 if (vcl_session_is_closing (s))
2233 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002234
Florin Coras5398dfb2021-01-25 20:31:27 -08002235 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2236 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002237 }
Dave Wallace543852a2017-08-03 02:11:34 -04002238 }
Dave Wallace543852a2017-08-03 02:11:34 -04002239
Florin Coras653e43f2019-03-04 10:56:23 -08002240 et = SESSION_IO_EVT_TX;
2241 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002242 et = SESSION_IO_EVT_TX_FLUSH;
2243
Florin Coras7a2abce2020-04-05 19:25:44 +00002244 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002245 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002246 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002247 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002248 else
2249 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002250 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002251
Florin Coras14ed6df2019-03-06 21:13:42 -08002252 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002253 app_send_io_evt_to_vpp (
2254 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002255
Florin Coras56230092020-09-02 20:52:58 -07002256 /* The underlying fifo segment can run out of memory */
2257 if (PREDICT_FALSE (n_write < 0))
2258 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002259
Florin Coras6d0106e2019-01-29 20:11:58 -08002260 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2261 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002262
Florin Coras54693d22018-07-17 10:46:29 -07002263 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002264}
2265
Florin Coras42ceddb2018-12-12 10:56:01 -08002266int
2267vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2268{
Florin Coras7a2abce2020-04-05 19:25:44 +00002269 vcl_worker_t *wrk = vcl_worker_get_current ();
2270 vcl_session_t *s;
2271
2272 s = vcl_session_get_w_handle (wrk, session_handle);
2273 if (PREDICT_FALSE (!s))
2274 return VPPCOM_EBADFD;
2275
2276 return vppcom_session_write_inline (wrk, s, buf, n,
2277 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002278}
2279
Florin Corasb0f662f2018-12-27 14:51:46 -08002280int
2281vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2282{
Florin Coras7a2abce2020-04-05 19:25:44 +00002283 vcl_worker_t *wrk = vcl_worker_get_current ();
2284 vcl_session_t *s;
2285
2286 s = vcl_session_get_w_handle (wrk, session_handle);
2287 if (PREDICT_FALSE (!s))
2288 return VPPCOM_EBADFD;
2289
2290 return vppcom_session_write_inline (wrk, s, buf, n,
2291 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002292}
2293
Florin Corasc0737e92019-03-04 14:19:39 -08002294#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002295if (PREDICT_FALSE (!_s->rx_fifo)) \
2296 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002297if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2298 { \
2299 if (!vcl_session_is_ct (_s)) \
2300 { \
2301 svm_fifo_unset_event (_s->rx_fifo); \
2302 if (svm_fifo_is_empty (_s->rx_fifo)) \
2303 break; \
2304 } \
2305 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2306 { \
Florin Coras2f630182020-08-13 00:17:51 -07002307 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002308 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2309 break; \
2310 } \
2311 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002312
Florin Coras86f04502018-09-12 16:08:01 -07002313static void
2314vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2315 unsigned long n_bits, unsigned long *read_map,
2316 unsigned long *write_map,
2317 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002318{
2319 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002320 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002321 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002322 u32 sid;
2323
2324 switch (e->event_type)
2325 {
Florin Corasc0737e92019-03-04 14:19:39 -08002326 case SESSION_IO_EVT_RX:
2327 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002328 s = vcl_session_get (wrk, sid);
2329 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002330 break;
Florin Corasb242d312020-10-26 15:35:40 -07002331 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002332 if (sid < n_bits && read_map)
2333 {
David Johnsond9818dd2018-12-14 14:53:41 -05002334 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002335 *bits_set += 1;
2336 }
2337 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002338 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002339 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002340 s = vcl_session_get (wrk, sid);
2341 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002342 break;
2343 if (sid < n_bits && write_map)
2344 {
David Johnsond9818dd2018-12-14 14:53:41 -05002345 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002346 *bits_set += 1;
2347 }
2348 break;
Florin Coras86f04502018-09-12 16:08:01 -07002349 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002350 if (!e->postponed)
2351 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2352 else
2353 s = vcl_session_get (wrk, e->session_index);
2354 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002355 break;
Florin Corasb242d312020-10-26 15:35:40 -07002356 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002357 if (sid < n_bits && read_map)
2358 {
David Johnsond9818dd2018-12-14 14:53:41 -05002359 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002360 *bits_set += 1;
2361 }
2362 break;
2363 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002364 if (!e->postponed)
2365 {
2366 connected_msg = (session_connected_msg_t *) e->data;
2367 sid = vcl_session_connected_handler (wrk, connected_msg);
2368 }
2369 else
2370 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002371 if (sid == VCL_INVALID_SESSION_INDEX)
2372 break;
2373 if (sid < n_bits && write_map)
2374 {
2375 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2376 *bits_set += 1;
2377 }
Florin Coras86f04502018-09-12 16:08:01 -07002378 break;
2379 case SESSION_CTRL_EVT_DISCONNECTED:
2380 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002381 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2382 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002383 break;
Florin Corasb242d312020-10-26 15:35:40 -07002384 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002385 if (sid < n_bits && except_map)
2386 {
David Johnsond9818dd2018-12-14 14:53:41 -05002387 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002388 *bits_set += 1;
2389 }
2390 break;
2391 case SESSION_CTRL_EVT_RESET:
2392 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2393 if (sid < n_bits && except_map)
2394 {
David Johnsond9818dd2018-12-14 14:53:41 -05002395 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002396 *bits_set += 1;
2397 }
2398 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002399 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2400 vcl_session_unlisten_reply_handler (wrk, e->data);
2401 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002402 case SESSION_CTRL_EVT_MIGRATED:
2403 vcl_session_migrated_handler (wrk, e->data);
2404 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002405 case SESSION_CTRL_EVT_CLEANUP:
2406 vcl_session_cleanup_handler (wrk, e->data);
2407 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002408 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2409 vcl_session_worker_update_reply_handler (wrk, e->data);
2410 break;
2411 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2412 vcl_session_req_worker_update_handler (wrk, e->data);
2413 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002414 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2415 vcl_session_app_add_segment_handler (wrk, e->data);
2416 break;
2417 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2418 vcl_session_app_del_segment_handler (wrk, e->data);
2419 break;
hanlina3a48962020-07-13 11:09:15 +08002420 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002421 vcl_worker_rpc_handler (wrk, e->data);
2422 break;
Florin Coras86f04502018-09-12 16:08:01 -07002423 default:
2424 clib_warning ("unhandled: %u", e->event_type);
2425 break;
2426 }
2427}
2428
2429static int
2430vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2431 unsigned long n_bits, unsigned long *read_map,
2432 unsigned long *write_map, unsigned long *except_map,
2433 double time_to_wait, u32 * bits_set)
2434{
Florin Coras99368312018-08-02 10:45:44 -07002435 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002436 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002437 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002438
Florin Coras54693d22018-07-17 10:46:29 -07002439 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002440 {
Florin Coras54693d22018-07-17 10:46:29 -07002441 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002442 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002443
Florin Coras54693d22018-07-17 10:46:29 -07002444 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002445 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002446 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002447 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002448 else
2449 {
2450 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002451 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002452 }
2453 }
Florin Corase003a1b2019-06-05 10:47:16 -07002454 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002455
Florin Coras134a9962018-08-28 11:32:04 -07002456 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002457 {
Florin Coras134a9962018-08-28 11:32:04 -07002458 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002459 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002460 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2461 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002462 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002463 }
Florin Coras134a9962018-08-28 11:32:04 -07002464 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002465 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002466 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002467}
2468
Florin Coras99368312018-08-02 10:45:44 -07002469static int
Florin Coras294afe22019-01-07 17:49:17 -08002470vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2471 vcl_si_set * read_map, vcl_si_set * write_map,
2472 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002473 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002474{
Florin Coras14ed6df2019-03-06 21:13:42 -08002475 double wait = 0, start = 0;
2476
2477 if (!*bits_set)
2478 {
2479 wait = time_to_wait;
2480 start = clib_time_now (&wrk->clib_time);
2481 }
2482
2483 do
2484 {
2485 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2486 write_map, except_map, wait, bits_set);
2487 if (*bits_set)
2488 return *bits_set;
2489 if (wait == -1)
2490 continue;
2491
2492 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2493 }
2494 while (wait > 0);
2495
2496 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002497}
2498
2499static int
Florin Coras294afe22019-01-07 17:49:17 -08002500vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2501 vcl_si_set * read_map, vcl_si_set * write_map,
2502 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002503 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002504{
2505 vcl_mq_evt_conn_t *mqc;
2506 int __clib_unused n_read;
2507 int n_mq_evts, i;
2508 u64 buf;
2509
Florin Coras134a9962018-08-28 11:32:04 -07002510 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2511 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2512 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002513 for (i = 0; i < n_mq_evts; i++)
2514 {
Florin Coras134a9962018-08-28 11:32:04 -07002515 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002516 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002517 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002518 except_map, 0, bits_set);
2519 }
2520
2521 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2522}
2523
Dave Wallace543852a2017-08-03 02:11:34 -04002524int
Florin Coras294afe22019-01-07 17:49:17 -08002525vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2526 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002527{
Florin Coras54693d22018-07-17 10:46:29 -07002528 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002529 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002530 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002531 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002532
Dave Wallace7876d392017-10-19 03:53:57 -04002533 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002534 {
Florin Coras134a9962018-08-28 11:32:04 -07002535 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002536 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002537 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2538 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002539 }
Dave Wallace7876d392017-10-19 03:53:57 -04002540 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002541 {
Florin Coras134a9962018-08-28 11:32:04 -07002542 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002543 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002544 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2545 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002546 }
Dave Wallace7876d392017-10-19 03:53:57 -04002547 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002548 {
Florin Coras134a9962018-08-28 11:32:04 -07002549 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002550 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002551 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2552 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002553 }
2554
Florin Coras54693d22018-07-17 10:46:29 -07002555 if (!n_bits)
2556 return 0;
2557
2558 if (!write_map)
2559 goto check_rd;
2560
Florin Coras096a1d52021-01-27 15:33:51 -08002561 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2562 {
2563 if (!(s = vcl_session_get (wrk, sid)))
2564 {
2565 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2566 bits_set++;
2567 continue;
2568 }
Florin Coras54693d22018-07-17 10:46:29 -07002569
Florin Coras096a1d52021-01-27 15:33:51 -08002570 if (vcl_session_write_ready (s))
2571 {
2572 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2573 bits_set++;
2574 }
2575 else
2576 {
2577 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2578 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2579 }
2580 }
Florin Coras54693d22018-07-17 10:46:29 -07002581
2582check_rd:
2583 if (!read_map)
2584 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002585
Florin Coras096a1d52021-01-27 15:33:51 -08002586 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2587 {
2588 if (!(s = vcl_session_get (wrk, sid)))
2589 {
2590 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2591 bits_set++;
2592 continue;
2593 }
Florin Coras54693d22018-07-17 10:46:29 -07002594
Florin Coras096a1d52021-01-27 15:33:51 -08002595 if (vcl_session_read_ready (s))
2596 {
2597 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2598 bits_set++;
2599 }
2600 }
Florin Coras54693d22018-07-17 10:46:29 -07002601
2602check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002603
Florin Coras86f04502018-09-12 16:08:01 -07002604 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2605 {
2606 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2607 read_map, write_map, except_map, &bits_set);
2608 }
2609 vec_reset_length (wrk->unhandled_evts_vector);
2610
Florin Coras99368312018-08-02 10:45:44 -07002611 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002612 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002613 time_to_wait, &bits_set);
2614 else
Florin Coras134a9962018-08-28 11:32:04 -07002615 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002616 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002617
Dave Wallace543852a2017-08-03 02:11:34 -04002618 return (bits_set);
2619}
2620
Dave Wallacef7f809c2017-10-03 01:48:42 -04002621static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002622vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002623{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002624 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002625 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002626 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002627
Florin Corasc0737e92019-03-04 14:19:39 -08002628 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002629 return;
2630
Florin Coras6c3b2182020-10-19 18:36:48 -07002631 s = vcl_session_get_w_handle (wrk, vep_handle);
2632 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002633 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002634 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002635 goto done;
2636 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002637 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002638 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002639 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002640 goto done;
2641 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002642 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002643 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002644 "{\n"
2645 " is_vep = %u\n"
2646 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002647 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002648 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2649 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002650
Florin Coras7e5e62b2019-07-30 14:08:23 -07002651 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002652 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002653 s = vcl_session_get_w_handle (wrk, sh);
2654 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002656 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002657 goto done;
2658 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002659 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002660 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002661 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002662 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002663 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002664 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002665 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002666 goto done;
2667 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002668 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002669 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2670 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002671 sh, s->vep.vep_sh, vep_handle);
2672 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002673 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002674 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002675 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002676 " next_sh = 0x%x (%u)\n"
2677 " prev_sh = 0x%x (%u)\n"
2678 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002679 " ev.events = 0x%x\n"
2680 " ev.data.u64 = 0x%llx\n"
2681 " et_mask = 0x%x\n"
2682 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002683 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002684 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2685 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686 }
2687 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688
2689done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002690 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691}
2692
2693int
2694vppcom_epoll_create (void)
2695{
Florin Coras134a9962018-08-28 11:32:04 -07002696 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002697 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002698
Florin Coras134a9962018-08-28 11:32:04 -07002699 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002700
Florin Coras6c3b2182020-10-19 18:36:48 -07002701 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002702 vep_session->vep.vep_sh = ~0;
2703 vep_session->vep.next_sh = ~0;
2704 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002705 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002706
Florin Corasa7a1a222018-12-30 17:11:31 -08002707 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2708 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002709
Florin Corasab2f6db2018-08-31 14:31:41 -07002710 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002711}
2712
2713int
Florin Coras134a9962018-08-28 11:32:04 -07002714vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 struct epoll_event *event)
2716{
Florin Coras134a9962018-08-28 11:32:04 -07002717 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras87f76002021-06-28 19:13:29 -07002718 int rv = VPPCOM_OK, add_evt = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07002719 vcl_session_t *vep_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002720 vcl_session_t *s;
2721 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002722
Florin Coras134a9962018-08-28 11:32:04 -07002723 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002724 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002725 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002726 return VPPCOM_EINVAL;
2727 }
2728
Florin Coras134a9962018-08-28 11:32:04 -07002729 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002730 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002731 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002732 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002733 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002734 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002735 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002736 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002737 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002738 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002739 }
2740
Florin Coras134a9962018-08-28 11:32:04 -07002741 ASSERT (vep_session->vep.vep_sh == ~0);
2742 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743
Florin Coras17ec5772020-08-12 20:46:29 -07002744 s = vcl_session_get_w_handle (wrk, session_handle);
2745 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002747 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002748 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002749 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002750 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002752 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002753 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002754 }
2755
2756 switch (op)
2757 {
2758 case EPOLL_CTL_ADD:
2759 if (PREDICT_FALSE (!event))
2760 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002761 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002762 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002763 }
Florin Coras2645f682021-06-04 15:59:41 -07002764 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2765 {
2766 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2767 rv = VPPCOM_EEXIST;
2768 goto done;
2769 }
Florin Coras134a9962018-08-28 11:32:04 -07002770 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 {
Florin Coras7e12d942018-06-27 14:32:43 -07002772 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002773 next_session = vcl_session_get_w_handle (wrk,
2774 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002775 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 {
Florin Coras5e062572019-03-14 19:07:51 -07002777 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002778 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002779 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002780 }
Florin Coras134a9962018-08-28 11:32:04 -07002781 ASSERT (next_session->vep.prev_sh == vep_handle);
2782 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002783 }
Florin Coras17ec5772020-08-12 20:46:29 -07002784 s->vep.next_sh = vep_session->vep.next_sh;
2785 s->vep.prev_sh = vep_handle;
2786 s->vep.vep_sh = vep_handle;
2787 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2788 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002789 s->flags &= ~VCL_SESSION_F_IS_VEP;
2790 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002791 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002792
Florin Coras17ec5772020-08-12 20:46:29 -07002793 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2794 if (txf && (event->events & EPOLLOUT))
2795 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002796
Florin Coras6e3c1f82020-01-15 01:30:46 +00002797 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002798 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002799 {
2800 session_event_t e = { 0 };
2801 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002802 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002803 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002804 add_evt = 1;
hanlin475c9d72019-12-26 11:44:28 +08002805 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002806 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002807 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002808 {
2809 session_event_t e = { 0 };
2810 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002811 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002812 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002813 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
2814 add_evt = 1;
2815 }
2816 if (!add_evt && vcl_session_is_closing (s))
2817 {
2818 session_event_t e = { 0 };
2819 if (s->session_state == VCL_STATE_VPP_CLOSING)
2820 e.event_type = SESSION_CTRL_EVT_DISCONNECTED;
2821 else
2822 e.event_type = SESSION_CTRL_EVT_RESET;
2823 e.session_index = s->session_index;
2824 e.postponed = 1;
2825 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras6e3c1f82020-01-15 01:30:46 +00002826 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002827 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2828 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002829 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002830 break;
2831
2832 case EPOLL_CTL_MOD:
2833 if (PREDICT_FALSE (!event))
2834 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002835 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002836 rv = VPPCOM_EINVAL;
2837 goto done;
2838 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002839 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002840 {
Florin Coras5e062572019-03-14 19:07:51 -07002841 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002842 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002843 goto done;
2844 }
Florin Coras17ec5772020-08-12 20:46:29 -07002845 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002846 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002847 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002848 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002849 rv = VPPCOM_EINVAL;
2850 goto done;
2851 }
hanlin475c9d72019-12-26 11:44:28 +08002852
Florin Coras2645f682021-06-04 15:59:41 -07002853 /* Generate EPOLLOUT if session write ready nd event was not on */
2854 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2855 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002856 {
2857 session_event_t e = { 0 };
2858 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002859 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002860 vec_add1 (wrk->unhandled_evts_vector, e);
2861 }
Florin Coras2645f682021-06-04 15:59:41 -07002862 /* Generate EPOLLIN if session read ready and event was not on */
2863 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2864 (vcl_session_read_ready (s) > 0))
2865 {
2866 session_event_t e = { 0 };
2867 e.event_type = SESSION_IO_EVT_RX;
2868 e.session_index = s->session_index;
2869 vec_add1 (wrk->unhandled_evts_vector, e);
Florin Coras87f76002021-06-28 19:13:29 -07002870 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2645f682021-06-04 15:59:41 -07002871 }
Florin Coras17ec5772020-08-12 20:46:29 -07002872 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2873 s->vep.ev = *event;
2874 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002875 if (txf)
2876 {
2877 if (event->events & EPOLLOUT)
2878 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2879 else
2880 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2881 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002882 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2883 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002884 break;
2885
2886 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002887 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002888 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002889 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002890 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002891 goto done;
2892 }
Florin Coras17ec5772020-08-12 20:46:29 -07002893 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002894 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002895 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002896 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002897 rv = VPPCOM_EINVAL;
2898 goto done;
2899 }
2900
Florin Coras17ec5772020-08-12 20:46:29 -07002901 if (s->vep.prev_sh == vep_handle)
2902 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002903 else
2904 {
Florin Coras7e12d942018-06-27 14:32:43 -07002905 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002906 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002907 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002908 {
Florin Coras5e062572019-03-14 19:07:51 -07002909 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002910 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002911 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002912 }
Florin Coras134a9962018-08-28 11:32:04 -07002913 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002914 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002915 }
Florin Coras17ec5772020-08-12 20:46:29 -07002916 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002917 {
Florin Coras7e12d942018-06-27 14:32:43 -07002918 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002919 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002920 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002921 {
Florin Coras5e062572019-03-14 19:07:51 -07002922 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002923 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002924 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002925 }
Florin Coras134a9962018-08-28 11:32:04 -07002926 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002927 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002928 }
2929
Florin Coras17ec5772020-08-12 20:46:29 -07002930 memset (&s->vep, 0, sizeof (s->vep));
2931 s->vep.next_sh = ~0;
2932 s->vep.prev_sh = ~0;
2933 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002934 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002935
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002936 if (vcl_session_is_open (s))
2937 {
2938 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2939 if (txf)
2940 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2941 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002942
Florin Coras5e062572019-03-14 19:07:51 -07002943 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002944 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002945 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002946 break;
2947
2948 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002949 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002950 rv = VPPCOM_EINVAL;
2951 }
2952
Florin Coras134a9962018-08-28 11:32:04 -07002953 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002954
2955done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002956 return rv;
2957}
2958
Florin Coras86f04502018-09-12 16:08:01 -07002959static inline void
2960vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2961 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002962{
2963 session_disconnected_msg_t *disconnected_msg;
2964 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002965 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002966 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002967 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002968 u8 add_event = 0;
2969
2970 switch (e->event_type)
2971 {
Florin Coras653e43f2019-03-04 10:56:23 -08002972 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002973 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002974 s = vcl_session_get (wrk, sid);
2975 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002976 break;
Florin Corasb242d312020-10-26 15:35:40 -07002977 vcl_fifo_rx_evt_valid_or_break (s);
2978 session_events = s->vep.ev.events;
2979 if (!(EPOLLIN & s->vep.ev.events)
2980 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002981 break;
2982 add_event = 1;
2983 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002984 session_evt_data = s->vep.ev.data.u64;
2985 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002986 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002987 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002988 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002989 s = vcl_session_get (wrk, sid);
2990 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002991 break;
Florin Corasb242d312020-10-26 15:35:40 -07002992 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002993 if (!(EPOLLOUT & session_events))
2994 break;
2995 add_event = 1;
2996 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002997 session_evt_data = s->vep.ev.data.u64;
2998 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2999 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07003000 break;
Florin Coras86f04502018-09-12 16:08:01 -07003001 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07003002 if (!e->postponed)
3003 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
3004 else
3005 s = vcl_session_get (wrk, e->session_index);
3006 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08003007 break;
Florin Corasb242d312020-10-26 15:35:40 -07003008 session_events = s->vep.ev.events;
3009 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07003010 if (!(EPOLLIN & session_events))
3011 break;
Florin Coras86f04502018-09-12 16:08:01 -07003012 add_event = 1;
3013 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07003014 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003015 break;
3016 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07003017 if (!e->postponed)
3018 {
3019 connected_msg = (session_connected_msg_t *) e->data;
3020 sid = vcl_session_connected_handler (wrk, connected_msg);
3021 }
3022 else
3023 sid = e->session_index;
3024 s = vcl_session_get (wrk, sid);
3025 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08003026 break;
Florin Corasb242d312020-10-26 15:35:40 -07003027 session_events = s->vep.ev.events;
3028 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003029 if (!(EPOLLOUT & session_events))
3030 break;
3031 add_event = 1;
3032 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003033 session_evt_data = s->vep.ev.data.u64;
3034 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003035 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003036 break;
3037 case SESSION_CTRL_EVT_DISCONNECTED:
Florin Coras87f76002021-06-28 19:13:29 -07003038 if (!e->postponed)
3039 {
3040 disconnected_msg = (session_disconnected_msg_t *) e->data;
3041 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3042 }
3043 else
3044 {
3045 s = vcl_session_get (wrk, e->session_index);
3046 }
3047 if (vcl_session_is_closed (s) ||
3048 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Coras86f04502018-09-12 16:08:01 -07003049 break;
Florin Corasb242d312020-10-26 15:35:40 -07003050 sid = s->session_index;
3051 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003052 add_event = 1;
3053 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003054 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003055 break;
3056 case SESSION_CTRL_EVT_RESET:
Florin Coras87f76002021-06-28 19:13:29 -07003057 if (!e->postponed)
3058 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
3059 else
3060 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07003061 s = vcl_session_get (wrk, sid);
Florin Coras87f76002021-06-28 19:13:29 -07003062 if (vcl_session_is_closed (s) ||
3063 !(s->flags & VCL_SESSION_F_IS_VEP_SESSION))
Florin Coras86f04502018-09-12 16:08:01 -07003064 break;
Florin Corasb242d312020-10-26 15:35:40 -07003065 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003066 add_event = 1;
3067 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003068 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003069 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003070 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3071 vcl_session_unlisten_reply_handler (wrk, e->data);
3072 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003073 case SESSION_CTRL_EVT_MIGRATED:
3074 vcl_session_migrated_handler (wrk, e->data);
3075 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003076 case SESSION_CTRL_EVT_CLEANUP:
3077 vcl_session_cleanup_handler (wrk, e->data);
3078 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003079 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3080 vcl_session_req_worker_update_handler (wrk, e->data);
3081 break;
3082 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3083 vcl_session_worker_update_reply_handler (wrk, e->data);
3084 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003085 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3086 vcl_session_app_add_segment_handler (wrk, e->data);
3087 break;
3088 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3089 vcl_session_app_del_segment_handler (wrk, e->data);
3090 break;
hanlina3a48962020-07-13 11:09:15 +08003091 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003092 vcl_worker_rpc_handler (wrk, e->data);
3093 break;
Florin Coras86f04502018-09-12 16:08:01 -07003094 default:
3095 VDBG (0, "unhandled: %u", e->event_type);
3096 break;
3097 }
3098
3099 if (add_event)
3100 {
3101 events[*num_ev].data.u64 = session_evt_data;
3102 if (EPOLLONESHOT & session_events)
3103 {
Florin Corasb242d312020-10-26 15:35:40 -07003104 s = vcl_session_get (wrk, sid);
3105 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003106 }
Florin Corasfe286f72021-06-04 10:07:55 -07003107 if (!(EPOLLET & session_events))
3108 {
3109 vec_add1 (wrk->ep_level_evts, sid);
3110 }
Florin Coras86f04502018-09-12 16:08:01 -07003111 *num_ev += 1;
3112 }
3113}
3114
3115static int
3116vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3117 struct epoll_event *events, u32 maxevents,
3118 double wait_for_time, u32 * num_ev)
3119{
Florin Coras99368312018-08-02 10:45:44 -07003120 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003121 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003122 int i;
3123
Florin Coras539663c2018-09-28 14:59:37 -07003124 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3125 goto handle_dequeued;
3126
Florin Coras54693d22018-07-17 10:46:29 -07003127 if (svm_msg_q_is_empty (mq))
3128 {
3129 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003130 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003131 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003132 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003133 else
3134 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003135 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003136 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003137 }
3138 }
Florin Corase003a1b2019-06-05 10:47:16 -07003139 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003140 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003141
Florin Coras539663c2018-09-28 14:59:37 -07003142handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003143 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003144 {
Florin Coras134a9962018-08-28 11:32:04 -07003145 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003146 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003147 if (*num_ev < maxevents)
3148 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3149 else
3150 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003151 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003152 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003153 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003154 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003155 return *num_ev;
3156}
3157
Florin Coras99368312018-08-02 10:45:44 -07003158static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003159vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3160 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003161{
Florin Corasccdb8b82021-04-28 13:01:06 -07003162 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003163
3164 if (!n_evts)
3165 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003166 if (timeout_ms > 0)
3167 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003168 }
3169
3170 do
3171 {
3172 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003173 timeout_ms, &n_evts);
3174 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003175 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003176 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003177 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003178
3179 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003180}
3181
3182static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003183vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3184 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003185{
Florin Coras99368312018-08-02 10:45:44 -07003186 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003187 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003188 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003189 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003190 u64 buf;
3191
Florin Coras134a9962018-08-28 11:32:04 -07003192 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003193 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003194 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003195 if (timeout_ms > 0)
3196 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003197 }
3198
Florin Corasb13ba132021-01-27 16:05:24 -08003199 do
3200 {
3201 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003202 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003203 if (n_mq_evts < 0)
3204 {
3205 VDBG (0, "epoll_wait error %u", errno);
3206 return n_evts;
3207 }
3208
3209 for (i = 0; i < n_mq_evts; i++)
3210 {
3211 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3212 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3213 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3214 &n_evts);
3215 }
3216
Florin Corasccdb8b82021-04-28 13:01:06 -07003217 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003218 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003219 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003220 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003221
3222 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003223}
3224
Florin Corasfe286f72021-06-04 10:07:55 -07003225static void
3226vcl_epoll_swap_lt_lists (vcl_worker_t *wrk)
3227{
3228 u32 *le;
3229
3230 le = wrk->ep_level_evts;
3231 wrk->ep_level_evts = wrk->ep_level_evts_fl;
3232 wrk->ep_level_evts_fl = le;
3233}
3234
3235static void
3236vcl_epoll_wait_handle_lt (vcl_worker_t *wrk, struct epoll_event *events,
3237 int maxevents, u32 *n_evts)
3238{
3239 u32 *sid, add_event = 0, *le = wrk->ep_level_evts_fl;
3240 vcl_session_t *s;
3241 u64 evt_data;
3242
3243 if (*n_evts >= maxevents)
3244 {
3245 vec_add (wrk->ep_level_evts, le, vec_len (le));
3246 vec_reset_length (wrk->ep_level_evts_fl);
3247 return;
3248 }
3249
3250 vec_foreach (sid, le)
3251 {
3252 s = vcl_session_get (wrk, sid[0]);
3253 if (!s)
3254 continue;
3255 if ((s->vep.ev.events & EPOLLIN) && vcl_session_read_ready (s))
3256 {
3257 add_event = 1;
3258 events[*n_evts].events |= EPOLLIN;
3259 evt_data = s->vep.ev.data.u64;
3260 }
3261 if ((s->vep.ev.events & EPOLLOUT) && vcl_session_write_ready (s))
3262 {
3263 add_event = 1;
3264 events[*n_evts].events |= EPOLLOUT;
3265 evt_data = s->vep.ev.data.u64;
3266 }
3267 if (add_event)
3268 {
3269 events[*n_evts].data.u64 = evt_data;
3270 *n_evts += 1;
3271 add_event = 0;
3272 vec_add1 (wrk->ep_level_evts, sid[0]);
3273 if (*n_evts == maxevents)
3274 {
3275 u32 pos = (sid - le) + 1;
3276 vec_add (wrk->ep_level_evts, &le[pos], vec_len (le) - pos);
3277 break;
3278 }
3279 }
3280 }
3281
3282 vec_reset_length (wrk->ep_level_evts_fl);
3283}
3284
Dave Wallacef7f809c2017-10-03 01:48:42 -04003285int
Florin Coras134a9962018-08-28 11:32:04 -07003286vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003287 int maxevents, double wait_for_time)
3288{
Florin Coras134a9962018-08-28 11:32:04 -07003289 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003290 vcl_session_t *vep_session;
Florin Corasfe286f72021-06-04 10:07:55 -07003291 u32 n_evts = 0, do_lt = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003292 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003293
3294 if (PREDICT_FALSE (maxevents <= 0))
3295 {
Florin Coras5e062572019-03-14 19:07:51 -07003296 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003297 return VPPCOM_EINVAL;
3298 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003299
Florin Coras134a9962018-08-28 11:32:04 -07003300 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003301 if (!vep_session)
3302 return VPPCOM_EBADFD;
3303
Florin Coras6c3b2182020-10-19 18:36:48 -07003304 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003305 {
Florin Coras5e062572019-03-14 19:07:51 -07003306 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003307 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003308 }
Florin Coras54693d22018-07-17 10:46:29 -07003309
3310 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003311
Florin Coras86f04502018-09-12 16:08:01 -07003312 if (vec_len (wrk->unhandled_evts_vector))
3313 {
3314 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3315 {
3316 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3317 events, &n_evts);
3318 if (n_evts == maxevents)
3319 {
Florin Corase003a1b2019-06-05 10:47:16 -07003320 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3321 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003322 }
3323 }
Florin Corase003a1b2019-06-05 10:47:16 -07003324 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003325 }
wanghanlin8919fec2021-03-18 20:00:41 +08003326 /* Request to only drain unhandled */
3327 if ((int) wait_for_time == -2)
3328 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003329
Florin Corasfe286f72021-06-04 10:07:55 -07003330 if (PREDICT_FALSE (vec_len (wrk->ep_level_evts)))
3331 {
3332 vcl_epoll_swap_lt_lists (wrk);
3333 do_lt = 1;
3334 }
Florin Coras86f04502018-09-12 16:08:01 -07003335
Florin Corasfe286f72021-06-04 10:07:55 -07003336 if (vcm->cfg.use_mq_eventfd)
3337 n_evts = vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3338 wait_for_time);
3339 else
3340 n_evts = vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3341 wait_for_time);
3342
3343 if (PREDICT_FALSE (do_lt))
3344 vcl_epoll_wait_handle_lt (wrk, events, maxevents, &n_evts);
3345
3346 return n_evts;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003347}
3348
Dave Wallace35830af2017-10-09 01:43:42 -04003349int
Florin Coras134a9962018-08-28 11:32:04 -07003350vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003351 void *buffer, uint32_t * buflen)
3352{
Florin Coras134a9962018-08-28 11:32:04 -07003353 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003354 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003355 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003356 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003357 vcl_session_t *session;
3358 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003359
Florin Coras134a9962018-08-28 11:32:04 -07003360 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003361 if (!session)
3362 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003363
Dave Wallace35830af2017-10-09 01:43:42 -04003364 switch (op)
3365 {
3366 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003367 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003368 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3369 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003370 break;
3371
Dave Wallace227867f2017-11-13 21:21:53 -05003372 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003373 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003374 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3375 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003376 break;
3377
3378 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003379 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003380 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003381 *flags =
3382 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003383 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003384 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003385 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003386 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3387 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003388 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003389 }
3390 else
3391 rv = VPPCOM_EINVAL;
3392 break;
3393
3394 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003395 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003396 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003397 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003398 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003399 else
Florin Corasac422d62020-10-19 20:51:36 -07003400 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003401
Florin Coras5e062572019-03-14 19:07:51 -07003402 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3403 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003404 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003405 }
3406 else
3407 rv = VPPCOM_EINVAL;
3408 break;
3409
3410 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 if (PREDICT_TRUE (buffer && buflen &&
3412 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003413 {
Florin Coras7e12d942018-06-27 14:32:43 -07003414 ep->is_ip4 = session->transport.is_ip4;
3415 ep->port = session->transport.rmt_port;
3416 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003417 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3418 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003419 else
Dave Barach178cf492018-11-13 16:34:13 -05003420 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3421 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003422 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003423 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3424 "addr = %U, port %u", session_handle, ep->is_ip4,
3425 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003426 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3427 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003428 }
3429 else
3430 rv = VPPCOM_EINVAL;
3431 break;
3432
3433 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 if (PREDICT_TRUE (buffer && buflen &&
3435 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003436 {
Florin Coras7e12d942018-06-27 14:32:43 -07003437 ep->is_ip4 = session->transport.is_ip4;
3438 ep->port = session->transport.lcl_port;
3439 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003440 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3441 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003442 else
Dave Barach178cf492018-11-13 16:34:13 -05003443 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3444 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003445 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003446 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3447 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003448 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003449 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3450 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003451 }
3452 else
3453 rv = VPPCOM_EINVAL;
3454 break;
Stevenb5a11602017-10-11 09:59:30 -07003455
Florin Corasef7cbf62019-10-17 09:56:27 -07003456 case VPPCOM_ATTR_SET_LCL_ADDR:
3457 if (PREDICT_TRUE (buffer && buflen &&
3458 (*buflen >= sizeof (*ep)) && ep->ip))
3459 {
3460 session->transport.is_ip4 = ep->is_ip4;
3461 session->transport.lcl_port = ep->port;
3462 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3463 *buflen = sizeof (*ep);
3464 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3465 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3466 &session->transport.lcl_ip,
3467 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3468 clib_net_to_host_u16 (ep->port));
3469 }
3470 else
3471 rv = VPPCOM_EINVAL;
3472 break;
3473
Dave Wallace048b1d62018-01-03 22:24:41 -05003474 case VPPCOM_ATTR_GET_LIBC_EPFD:
3475 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003476 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003477 break;
3478
3479 case VPPCOM_ATTR_SET_LIBC_EPFD:
3480 if (PREDICT_TRUE (buffer && buflen &&
3481 (*buflen == sizeof (session->libc_epfd))))
3482 {
3483 session->libc_epfd = *(int *) buffer;
3484 *buflen = sizeof (session->libc_epfd);
3485
Florin Coras5e062572019-03-14 19:07:51 -07003486 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3487 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003488 }
3489 else
3490 rv = VPPCOM_EINVAL;
3491 break;
3492
3493 case VPPCOM_ATTR_GET_PROTOCOL:
3494 if (buffer && buflen && (*buflen >= sizeof (int)))
3495 {
Florin Coras7e12d942018-06-27 14:32:43 -07003496 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003497 *buflen = sizeof (int);
3498
Florin Coras5e062572019-03-14 19:07:51 -07003499 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3500 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003501 }
3502 else
3503 rv = VPPCOM_EINVAL;
3504 break;
3505
3506 case VPPCOM_ATTR_GET_LISTEN:
3507 if (buffer && buflen && (*buflen >= sizeof (int)))
3508 {
Florin Corasac422d62020-10-19 20:51:36 -07003509 *(int *) buffer = vcl_session_has_attr (session,
3510 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003511 *buflen = sizeof (int);
3512
Florin Coras5e062572019-03-14 19:07:51 -07003513 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3514 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003515 }
3516 else
3517 rv = VPPCOM_EINVAL;
3518 break;
3519
3520 case VPPCOM_ATTR_GET_ERROR:
3521 if (buffer && buflen && (*buflen >= sizeof (int)))
3522 {
3523 *(int *) buffer = 0;
3524 *buflen = sizeof (int);
3525
Florin Coras5e062572019-03-14 19:07:51 -07003526 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3527 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 }
3529 else
3530 rv = VPPCOM_EINVAL;
3531 break;
3532
3533 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3534 if (buffer && buflen && (*buflen >= sizeof (u32)))
3535 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003536
3537 /* VPP-TBD */
3538 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003539 session->tx_fifo ?
3540 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003541 vcm->cfg.tx_fifo_size);
3542 *buflen = sizeof (u32);
3543
Florin Coras5e062572019-03-14 19:07:51 -07003544 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3545 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3546 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003547 }
3548 else
3549 rv = VPPCOM_EINVAL;
3550 break;
3551
3552 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3553 if (buffer && buflen && (*buflen == sizeof (u32)))
3554 {
3555 /* VPP-TBD */
3556 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003557 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3558 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3559 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003560 }
3561 else
3562 rv = VPPCOM_EINVAL;
3563 break;
3564
3565 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3566 if (buffer && buflen && (*buflen >= sizeof (u32)))
3567 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003568
3569 /* VPP-TBD */
3570 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003571 session->rx_fifo ?
3572 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003573 vcm->cfg.rx_fifo_size);
3574 *buflen = sizeof (u32);
3575
Florin Coras5e062572019-03-14 19:07:51 -07003576 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3577 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003578 }
3579 else
3580 rv = VPPCOM_EINVAL;
3581 break;
3582
3583 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3584 if (buffer && buflen && (*buflen == sizeof (u32)))
3585 {
3586 /* VPP-TBD */
3587 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003588 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3589 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3590 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003591 }
3592 else
3593 rv = VPPCOM_EINVAL;
3594 break;
3595
3596 case VPPCOM_ATTR_GET_REUSEADDR:
3597 if (buffer && buflen && (*buflen >= sizeof (int)))
3598 {
3599 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003600 *(int *) buffer = vcl_session_has_attr (session,
3601 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003602 *buflen = sizeof (int);
3603
Florin Coras5e062572019-03-14 19:07:51 -07003604 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3605 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003606 }
3607 else
3608 rv = VPPCOM_EINVAL;
3609 break;
3610
Stevenb5a11602017-10-11 09:59:30 -07003611 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003612 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003613 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003614 {
3615 /* VPP-TBD */
3616 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003617 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003618 else
Florin Corasac422d62020-10-19 20:51:36 -07003619 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003620
Florin Coras5e062572019-03-14 19:07:51 -07003621 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003622 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003623 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003624 }
3625 else
3626 rv = VPPCOM_EINVAL;
3627 break;
3628
3629 case VPPCOM_ATTR_GET_REUSEPORT:
3630 if (buffer && buflen && (*buflen >= sizeof (int)))
3631 {
3632 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003633 *(int *) buffer = vcl_session_has_attr (session,
3634 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003635 *buflen = sizeof (int);
3636
Florin Coras5e062572019-03-14 19:07:51 -07003637 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3638 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003639 }
3640 else
3641 rv = VPPCOM_EINVAL;
3642 break;
3643
3644 case VPPCOM_ATTR_SET_REUSEPORT:
3645 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003646 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003647 {
3648 /* VPP-TBD */
3649 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003650 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003651 else
Florin Corasac422d62020-10-19 20:51:36 -07003652 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003653
Florin Coras5e062572019-03-14 19:07:51 -07003654 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003655 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003656 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003657 }
3658 else
3659 rv = VPPCOM_EINVAL;
3660 break;
3661
3662 case VPPCOM_ATTR_GET_BROADCAST:
3663 if (buffer && buflen && (*buflen >= sizeof (int)))
3664 {
3665 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003666 *(int *) buffer = vcl_session_has_attr (session,
3667 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003668 *buflen = sizeof (int);
3669
Florin Coras5e062572019-03-14 19:07:51 -07003670 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3671 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003672 }
3673 else
3674 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003675 break;
3676
3677 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003678 if (buffer && buflen && (*buflen == sizeof (int)))
3679 {
3680 /* VPP-TBD */
3681 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003682 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003683 else
Florin Corasac422d62020-10-19 20:51:36 -07003684 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003685
Florin Coras5e062572019-03-14 19:07:51 -07003686 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003687 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003688 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003689 }
3690 else
3691 rv = VPPCOM_EINVAL;
3692 break;
3693
3694 case VPPCOM_ATTR_GET_V6ONLY:
3695 if (buffer && buflen && (*buflen >= sizeof (int)))
3696 {
3697 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003698 *(int *) buffer = vcl_session_has_attr (session,
3699 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003700 *buflen = sizeof (int);
3701
Florin Coras5e062572019-03-14 19:07:51 -07003702 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3703 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003704 }
3705 else
3706 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003707 break;
3708
3709 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003710 if (buffer && buflen && (*buflen == sizeof (int)))
3711 {
3712 /* VPP-TBD */
3713 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003714 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003715 else
Florin Corasac422d62020-10-19 20:51:36 -07003716 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003717
Florin Coras5e062572019-03-14 19:07:51 -07003718 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003719 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003720 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003721 }
3722 else
3723 rv = VPPCOM_EINVAL;
3724 break;
3725
3726 case VPPCOM_ATTR_GET_KEEPALIVE:
3727 if (buffer && buflen && (*buflen >= sizeof (int)))
3728 {
3729 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003730 *(int *) buffer = vcl_session_has_attr (session,
3731 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003732 *buflen = sizeof (int);
3733
Florin Coras5e062572019-03-14 19:07:51 -07003734 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3735 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003736 }
3737 else
3738 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003739 break;
Stevenbccd3392017-10-12 20:42:21 -07003740
3741 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003742 if (buffer && buflen && (*buflen == sizeof (int)))
3743 {
3744 /* VPP-TBD */
3745 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003746 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003747 else
Florin Corasac422d62020-10-19 20:51:36 -07003748 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003749
Florin Coras5e062572019-03-14 19:07:51 -07003750 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003751 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003752 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003753 }
3754 else
3755 rv = VPPCOM_EINVAL;
3756 break;
3757
3758 case VPPCOM_ATTR_GET_TCP_NODELAY:
3759 if (buffer && buflen && (*buflen >= sizeof (int)))
3760 {
3761 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003762 *(int *) buffer = vcl_session_has_attr (session,
3763 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003764 *buflen = sizeof (int);
3765
Florin Coras5e062572019-03-14 19:07:51 -07003766 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3767 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003768 }
3769 else
3770 rv = VPPCOM_EINVAL;
3771 break;
3772
3773 case VPPCOM_ATTR_SET_TCP_NODELAY:
3774 if (buffer && buflen && (*buflen == sizeof (int)))
3775 {
3776 /* VPP-TBD */
3777 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003778 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003779 else
Florin Corasac422d62020-10-19 20:51:36 -07003780 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003781
Florin Coras5e062572019-03-14 19:07:51 -07003782 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003783 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003784 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003785 }
3786 else
3787 rv = VPPCOM_EINVAL;
3788 break;
3789
3790 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3791 if (buffer && buflen && (*buflen >= sizeof (int)))
3792 {
3793 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003794 *(int *) buffer = vcl_session_has_attr (session,
3795 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003796 *buflen = sizeof (int);
3797
Florin Coras5e062572019-03-14 19:07:51 -07003798 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3799 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003800 }
3801 else
3802 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003803 break;
3804
3805 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003806 if (buffer && buflen && (*buflen == sizeof (int)))
3807 {
3808 /* VPP-TBD */
3809 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003810 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 else
Florin Corasac422d62020-10-19 20:51:36 -07003812 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003813
Florin Coras5e062572019-03-14 19:07:51 -07003814 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003815 vcl_session_has_attr (session,
3816 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 }
3818 else
3819 rv = VPPCOM_EINVAL;
3820 break;
3821
3822 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3823 if (buffer && buflen && (*buflen >= sizeof (int)))
3824 {
3825 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003826 *(int *) buffer = vcl_session_has_attr (session,
3827 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 *buflen = sizeof (int);
3829
Florin Coras5e062572019-03-14 19:07:51 -07003830 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3831 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 }
3833 else
3834 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003835 break;
3836
3837 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003838 if (buffer && buflen && (*buflen == sizeof (int)))
3839 {
3840 /* VPP-TBD */
3841 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003842 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 else
Florin Corasac422d62020-10-19 20:51:36 -07003844 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003845
Florin Coras5e062572019-03-14 19:07:51 -07003846 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003847 vcl_session_has_attr (session,
3848 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003849 }
3850 else
3851 rv = VPPCOM_EINVAL;
3852 break;
3853
3854 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003855 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003856 {
Florin Coras04ae8272021-04-12 19:55:37 -07003857 rv = VPPCOM_EINVAL;
3858 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003859 }
Florin Coras04ae8272021-04-12 19:55:37 -07003860
3861 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3862 tea.mss = *(u32 *) buffer;
3863 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3864 rv = VPPCOM_ENOPROTOOPT;
3865
3866 if (!rv)
3867 {
3868 *(u32 *) buffer = tea.mss;
3869 *buflen = sizeof (int);
3870 }
3871
3872 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3873 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003874 break;
3875
3876 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003877 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003878 {
Florin Coras04ae8272021-04-12 19:55:37 -07003879 rv = VPPCOM_EINVAL;
3880 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003881 }
Florin Coras04ae8272021-04-12 19:55:37 -07003882
3883 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3884 tea.mss = *(u32 *) buffer;
3885 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3886 rv = VPPCOM_ENOPROTOOPT;
3887
3888 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3889 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003890 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003891
Florin Coras1e966172020-05-16 18:18:14 +00003892 case VPPCOM_ATTR_SET_CONNECTED:
3893 session->flags |= VCL_SESSION_F_CONNECTED;
3894 break;
3895
Florin Corasa5a9efd2021-01-05 17:03:29 -08003896 case VPPCOM_ATTR_SET_CKPAIR:
3897 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3898 !vcl_session_has_crypto (session))
3899 {
3900 rv = VPPCOM_EINVAL;
3901 break;
3902 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003903 if (!session->ext_config)
3904 {
Florin Coras87f63892021-05-01 16:01:40 -07003905 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3906 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003907 }
3908 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3909 {
3910 rv = VPPCOM_EINVAL;
3911 break;
3912 }
3913
3914 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003915 break;
3916
Florin Coras6a6555a2021-01-28 11:39:27 -08003917 case VPPCOM_ATTR_SET_VRF:
3918 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3919 {
3920 rv = VPPCOM_EINVAL;
3921 break;
3922 }
3923 session->vrf = *(u32 *) buffer;
3924 break;
3925
3926 case VPPCOM_ATTR_GET_VRF:
3927 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3928 {
3929 rv = VPPCOM_EINVAL;
3930 break;
3931 }
3932 *(u32 *) buffer = session->vrf;
3933 *buflen = sizeof (u32);
3934 break;
3935
wanghanlin0674f852021-02-22 10:38:36 +08003936 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003937 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003938 {
Florin Corasd77325c2021-02-23 12:03:03 -08003939 rv = VPPCOM_EINVAL;
3940 break;
wanghanlin0674f852021-02-22 10:38:36 +08003941 }
Florin Corasd77325c2021-02-23 12:03:03 -08003942
3943 if (session->transport.is_ip4)
3944 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003945 else
Florin Corasd77325c2021-02-23 12:03:03 -08003946 *(int *) buffer = AF_INET6;
3947 *buflen = sizeof (int);
3948
wanghanlin0674f852021-02-22 10:38:36 +08003949 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3950 *buflen);
3951 break;
3952
Florin Coras87f63892021-05-01 16:01:40 -07003953 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3954 if (!(buffer && buflen && (*buflen > 0)))
3955 {
3956 rv = VPPCOM_EINVAL;
3957 break;
3958 }
3959 if (session->ext_config)
3960 {
3961 rv = VPPCOM_EINVAL;
3962 break;
3963 }
3964 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3965 *buflen + sizeof (u32));
3966 clib_memcpy (session->ext_config->data, buffer, *buflen);
3967 session->ext_config->len = *buflen;
3968 break;
3969
Dave Wallacee22aa742017-10-20 12:30:38 -04003970 default:
3971 rv = VPPCOM_EINVAL;
3972 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003973 }
3974
Dave Wallace35830af2017-10-09 01:43:42 -04003975 return rv;
3976}
3977
Stevenac1f96d2017-10-24 16:03:58 -07003978int
Florin Coras134a9962018-08-28 11:32:04 -07003979vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003980 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3981{
Florin Coras134a9962018-08-28 11:32:04 -07003982 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003983 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003984 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003985
3986 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003987 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003988 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003989 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003990 else
3991 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003992 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003993 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003994 }
3995
Florin Coras0a1e1832020-03-29 18:54:04 +00003996 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003997 {
Florin Coras5da10c42020-04-01 04:31:21 +00003998 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003999 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05004000 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
4001 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07004002 else
Dave Barach178cf492018-11-13 16:34:13 -05004003 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
4004 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00004005 ep->is_ip4 = session->transport.is_ip4;
4006 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07004007 }
Florin Coras460dce62018-07-27 05:45:06 -07004008
Stevenac1f96d2017-10-24 16:03:58 -07004009 return rv;
4010}
4011
4012int
Florin Coras134a9962018-08-28 11:32:04 -07004013vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07004014 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4015{
Florin Coras7a2abce2020-04-05 19:25:44 +00004016 vcl_worker_t *wrk = vcl_worker_get_current ();
4017 vcl_session_t *s;
4018
4019 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07004020 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00004021 return VPPCOM_EBADFD;
4022
Dave Wallace617dffa2017-10-26 14:47:06 -04004023 if (ep)
4024 {
Florin Coras5824cc52020-10-20 18:44:41 -07004025 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00004026 return VPPCOM_EINVAL;
4027
4028 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07004029 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00004030 {
Florin Coras5824cc52020-10-20 18:44:41 -07004031 u32 session_index = s->session_index;
4032 f64 timeout = vcm->cfg.session_timeout;
4033 int rv;
4034
Florin Coras5da10c42020-04-01 04:31:21 +00004035 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07004036 rv = vppcom_wait_for_session_state_change (session_index,
4037 VCL_STATE_READY,
4038 timeout);
4039 if (rv < 0)
4040 return rv;
4041 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00004042 }
Florin Coras5824cc52020-10-20 18:44:41 -07004043
4044 s->transport.is_ip4 = ep->is_ip4;
4045 s->transport.rmt_port = ep->port;
4046 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04004047 }
4048
4049 if (flags)
4050 {
4051 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07004052 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004053 }
4054
Florin Coras7a2abce2020-04-05 19:25:44 +00004055 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
4056 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07004057}
4058
Dave Wallace048b1d62018-01-03 22:24:41 -05004059int
4060vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4061{
Florin Coras134a9962018-08-28 11:32:04 -07004062 vcl_worker_t *wrk = vcl_worker_get_current ();
4063 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05004064 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08004065 svm_msg_q_msg_t msg;
4066 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05004067 int rv, num_ev = 0;
4068
Florin Coras5e062572019-03-14 19:07:51 -07004069 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004070
4071 if (!vp)
4072 return VPPCOM_EFAULT;
4073
4074 do
4075 {
Florin Coras7e12d942018-06-27 14:32:43 -07004076 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004077
Florin Coras6917b942018-11-13 22:44:54 -08004078 /* Dequeue all events and drop all unhandled io events */
4079 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
4080 {
4081 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
4082 vcl_handle_mq_event (wrk, e);
4083 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
4084 }
4085 vec_reset_length (wrk->unhandled_evts_vector);
4086
Dave Wallace048b1d62018-01-03 22:24:41 -05004087 for (i = 0; i < n_sids; i++)
4088 {
Florin Coras7baeb712019-01-04 17:05:43 -08004089 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07004090 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08004091 {
4092 vp[i].revents = POLLHUP;
4093 num_ev++;
4094 continue;
4095 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004096
Florin Coras6917b942018-11-13 22:44:54 -08004097 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004098
4099 if (POLLIN & vp[i].events)
4100 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004101 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004102 if (rv > 0)
4103 {
Florin Coras6917b942018-11-13 22:44:54 -08004104 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05004105 num_ev++;
4106 }
4107 else if (rv < 0)
4108 {
4109 switch (rv)
4110 {
4111 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004112 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004113 break;
4114
4115 default:
Florin Coras6917b942018-11-13 22:44:54 -08004116 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004117 break;
4118 }
4119 num_ev++;
4120 }
4121 }
4122
4123 if (POLLOUT & vp[i].events)
4124 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004125 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004126 if (rv > 0)
4127 {
Florin Coras6917b942018-11-13 22:44:54 -08004128 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004129 num_ev++;
4130 }
4131 else if (rv < 0)
4132 {
4133 switch (rv)
4134 {
4135 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004136 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004137 break;
4138
4139 default:
Florin Coras6917b942018-11-13 22:44:54 -08004140 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004141 break;
4142 }
4143 num_ev++;
4144 }
4145 }
4146
Dave Wallace7e607a72018-06-18 18:41:32 -04004147 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004148 {
Florin Coras6917b942018-11-13 22:44:54 -08004149 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004150 num_ev++;
4151 }
4152 }
4153 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004154 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004155 }
4156 while ((num_ev == 0) && keep_trying);
4157
Dave Wallace048b1d62018-01-03 22:24:41 -05004158 return num_ev;
4159}
4160
Florin Coras99368312018-08-02 10:45:44 -07004161int
4162vppcom_mq_epoll_fd (void)
4163{
Florin Coras134a9962018-08-28 11:32:04 -07004164 vcl_worker_t *wrk = vcl_worker_get_current ();
4165 return wrk->mqs_epfd;
4166}
4167
4168int
Florin Coras30e79c22019-01-02 19:31:22 -08004169vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004170{
4171 return session_handle & 0xFFFFFF;
4172}
4173
4174int
Florin Coras30e79c22019-01-02 19:31:22 -08004175vppcom_session_worker (vcl_session_handle_t session_handle)
4176{
4177 return session_handle >> 24;
4178}
4179
4180int
Florin Coras134a9962018-08-28 11:32:04 -07004181vppcom_worker_register (void)
4182{
Florin Coras47c40e22018-11-26 17:01:36 -08004183 if (!vcl_worker_alloc_and_init ())
4184 return VPPCOM_EEXIST;
4185
Florin Coras47c40e22018-11-26 17:01:36 -08004186 if (vcl_worker_register_with_vpp ())
4187 return VPPCOM_EEXIST;
4188
4189 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004190}
4191
Florin Coras369db832019-07-08 12:34:45 -07004192void
4193vppcom_worker_unregister (void)
4194{
4195 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4196 vcl_set_worker_index (~0);
4197}
4198
Pivo6017ff02020-05-04 17:57:33 +02004199void
4200vppcom_worker_index_set (int index)
4201{
4202 vcl_set_worker_index (index);
4203}
4204
Florin Corasdfe4cf42018-11-28 22:13:45 -08004205int
4206vppcom_worker_index (void)
4207{
4208 return vcl_get_worker_index ();
4209}
4210
Florin Corase0982e52019-01-25 13:19:56 -08004211int
4212vppcom_worker_mqs_epfd (void)
4213{
4214 vcl_worker_t *wrk = vcl_worker_get_current ();
4215 if (!vcm->cfg.use_mq_eventfd)
4216 return -1;
4217 return wrk->mqs_epfd;
4218}
4219
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004220int
4221vppcom_session_is_connectable_listener (uint32_t session_handle)
4222{
4223 vcl_session_t *session;
4224 vcl_worker_t *wrk = vcl_worker_get_current ();
4225 session = vcl_session_get_w_handle (wrk, session_handle);
4226 if (!session)
4227 return VPPCOM_EBADFD;
4228 return vcl_session_is_connectable_listener (wrk, session);
4229}
4230
4231int
4232vppcom_session_listener (uint32_t session_handle)
4233{
4234 vcl_worker_t *wrk = vcl_worker_get_current ();
4235 vcl_session_t *listen_session, *session;
4236 session = vcl_session_get_w_handle (wrk, session_handle);
4237 if (!session)
4238 return VPPCOM_EBADFD;
4239 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4240 return VPPCOM_EBADFD;
4241 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4242 if (!listen_session)
4243 return VPPCOM_EBADFD;
4244 return vcl_session_handle (listen_session);
4245}
4246
4247int
4248vppcom_session_n_accepted (uint32_t session_handle)
4249{
4250 vcl_worker_t *wrk = vcl_worker_get_current ();
4251 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4252 if (!session)
4253 return VPPCOM_EBADFD;
4254 return session->n_accepted_sessions;
4255}
4256
Florin Coras66ec4672020-06-15 07:59:40 -07004257const char *
4258vppcom_proto_str (vppcom_proto_t proto)
4259{
4260 char const *proto_str;
4261
4262 switch (proto)
4263 {
4264 case VPPCOM_PROTO_TCP:
4265 proto_str = "TCP";
4266 break;
4267 case VPPCOM_PROTO_UDP:
4268 proto_str = "UDP";
4269 break;
4270 case VPPCOM_PROTO_TLS:
4271 proto_str = "TLS";
4272 break;
4273 case VPPCOM_PROTO_QUIC:
4274 proto_str = "QUIC";
4275 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004276 case VPPCOM_PROTO_DTLS:
4277 proto_str = "DTLS";
4278 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004279 case VPPCOM_PROTO_SRTP:
4280 proto_str = "SRTP";
4281 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004282 default:
4283 proto_str = "UNKNOWN";
4284 break;
4285 }
4286 return proto_str;
4287}
4288
4289const char *
4290vppcom_retval_str (int retval)
4291{
4292 char const *st;
4293
4294 switch (retval)
4295 {
4296 case VPPCOM_OK:
4297 st = "VPPCOM_OK";
4298 break;
4299
4300 case VPPCOM_EAGAIN:
4301 st = "VPPCOM_EAGAIN";
4302 break;
4303
4304 case VPPCOM_EFAULT:
4305 st = "VPPCOM_EFAULT";
4306 break;
4307
4308 case VPPCOM_ENOMEM:
4309 st = "VPPCOM_ENOMEM";
4310 break;
4311
4312 case VPPCOM_EINVAL:
4313 st = "VPPCOM_EINVAL";
4314 break;
4315
4316 case VPPCOM_EBADFD:
4317 st = "VPPCOM_EBADFD";
4318 break;
4319
4320 case VPPCOM_EAFNOSUPPORT:
4321 st = "VPPCOM_EAFNOSUPPORT";
4322 break;
4323
4324 case VPPCOM_ECONNABORTED:
4325 st = "VPPCOM_ECONNABORTED";
4326 break;
4327
4328 case VPPCOM_ECONNRESET:
4329 st = "VPPCOM_ECONNRESET";
4330 break;
4331
4332 case VPPCOM_ENOTCONN:
4333 st = "VPPCOM_ENOTCONN";
4334 break;
4335
4336 case VPPCOM_ECONNREFUSED:
4337 st = "VPPCOM_ECONNREFUSED";
4338 break;
4339
4340 case VPPCOM_ETIMEDOUT:
4341 st = "VPPCOM_ETIMEDOUT";
4342 break;
4343
4344 default:
4345 st = "UNKNOWN_STATE";
4346 break;
4347 }
4348
4349 return st;
4350}
4351
Florin Corasa5a9efd2021-01-05 17:03:29 -08004352int
4353vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4354{
4355 if (vcm->cfg.vpp_app_socket_api)
4356 {
4357 clib_warning ("not supported");
4358 return VPPCOM_EINVAL;
4359 }
4360 return vcl_bapi_add_cert_key_pair (ckpair);
4361}
4362
4363int
4364vppcom_del_cert_key_pair (uint32_t ckpair_index)
4365{
4366 if (vcm->cfg.vpp_app_socket_api)
4367 {
4368 clib_warning ("not supported");
4369 return VPPCOM_EINVAL;
4370 }
4371 return vcl_bapi_del_cert_key_pair (ckpair_index);
4372}
4373
Dave Wallacee22aa742017-10-20 12:30:38 -04004374/*
4375 * fd.io coding-style-patch-verification: ON
4376 *
4377 * Local Variables:
4378 * eval: (c-set-style "gnu")
4379 * End:
4380 */