blob: d378f4043813388a25158fb9d5f88f28eee6e090 [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
1448int
Florin Corasc127d5a2020-10-14 16:35:58 -07001449vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001450 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001451{
Florin Coras134a9962018-08-28 11:32:04 -07001452 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001453
Florin Coras6c3b2182020-10-19 18:36:48 -07001454 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001455
Florin Coras6c3b2182020-10-19 18:36:48 -07001456 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001457 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001458 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001459 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001460 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001461 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001462 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001463 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001464 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1465 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001466 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001467 }
Florin Corase4306b62020-10-28 12:51:10 -07001468 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001469 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001470
Florin Coras6c3b2182020-10-19 18:36:48 -07001471 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001472 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001473 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001474 if (rv < 0)
1475 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001476 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1477 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001478 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001479
Florin Coras9ace36d2019-10-28 13:14:17 -07001480 if (!do_disconnect)
1481 {
1482 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001483 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001484 goto cleanup;
1485 }
1486
Florin Coras6c3b2182020-10-19 18:36:48 -07001487 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001488 {
1489 rv = vppcom_session_unbind (sh);
1490 if (PREDICT_FALSE (rv < 0))
1491 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001492 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001493 vppcom_retval_str (rv));
1494 return rv;
1495 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001496 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001497 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001498 {
1499 rv = vppcom_session_disconnect (sh);
1500 if (PREDICT_FALSE (rv < 0))
1501 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001502 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001503 rv, vppcom_retval_str (rv));
1504 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001505 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001506 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001507 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001508 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001509 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001510 {
1511 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001512 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001513 goto free_session;
1514 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001515
Florin Corasc127d5a2020-10-14 16:35:58 -07001516 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001517
Florin Coras9ace36d2019-10-28 13:14:17 -07001518 /* Session is removed only after vpp confirms the disconnect */
1519 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001520
Florin Corasf9240dc2019-01-15 08:03:17 -08001521cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001522 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001523free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001524 vcl_session_free (wrk, s);
1525 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001526
Dave Wallace543852a2017-08-03 02:11:34 -04001527 return rv;
1528}
1529
1530int
Florin Corasf9240dc2019-01-15 08:03:17 -08001531vppcom_session_close (uint32_t session_handle)
1532{
1533 vcl_worker_t *wrk = vcl_worker_get_current ();
1534 vcl_session_t *session;
1535
1536 session = vcl_session_get_w_handle (wrk, session_handle);
1537 if (!session)
1538 return VPPCOM_EBADFD;
1539 return vcl_session_cleanup (wrk, session, session_handle,
1540 1 /* do_disconnect */ );
1541}
1542
1543int
Florin Coras134a9962018-08-28 11:32:04 -07001544vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001545{
Florin Coras134a9962018-08-28 11:32:04 -07001546 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001547 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001548
1549 if (!ep || !ep->ip)
1550 return VPPCOM_EINVAL;
1551
Florin Coras134a9962018-08-28 11:32:04 -07001552 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001553 if (!session)
1554 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001555
Florin Coras6c3b2182020-10-19 18:36:48 -07001556 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001557 {
Florin Coras5e062572019-03-14 19:07:51 -07001558 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1559 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001560 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001561 }
1562
Florin Coras7e12d942018-06-27 14:32:43 -07001563 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001564 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001565 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1566 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001567 else
Dave Barach178cf492018-11-13 16:34:13 -05001568 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1569 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001570 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001571
Florin Coras5e062572019-03-14 19:07:51 -07001572 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1573 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001574 session->transport.is_ip4 ? "IPv4" : "IPv6",
1575 format_ip46_address, &session->transport.lcl_ip,
1576 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1577 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001578 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001579 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001580
1581 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001582 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001583
Florin Coras070453d2018-08-24 17:04:27 -07001584 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001585}
1586
1587int
Florin Coras134a9962018-08-28 11:32:04 -07001588vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001589{
Florin Coras134a9962018-08-28 11:32:04 -07001590 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001591 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001592 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001593 int rv;
1594
Florin Coras134a9962018-08-28 11:32:04 -07001595 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001596 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001597 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001598
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001599 if (q_len == 0 || q_len == ~0)
1600 q_len = vcm->cfg.listen_queue_size;
1601
Dave Wallaceee45d412017-11-24 21:44:06 -05001602 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001603 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001604 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001605 VDBG (0, "session %u [0x%llx]: already in listen state!",
1606 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001607 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001608 }
1609
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001610 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001611
Florin Coras070453d2018-08-24 17:04:27 -07001612 /*
1613 * Send listen request to vpp and wait for reply
1614 */
Florin Coras458089b2019-08-21 16:20:44 -07001615 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001616 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001617 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001618 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001619
Florin Coras070453d2018-08-24 17:04:27 -07001620 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001621 {
Florin Coras134a9962018-08-28 11:32:04 -07001622 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001623 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1624 listen_sh, listen_session->vpp_handle, rv,
1625 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001626 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001627 }
1628
Florin Coras070453d2018-08-24 17:04:27 -07001629 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001630}
1631
Florin Coras134a9962018-08-28 11:32:04 -07001632static int
Florin Coras5e062572019-03-14 19:07:51 -07001633validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001634{
Florin Coras6c3b2182020-10-19 18:36:48 -07001635 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001636 {
Florin Coras5e062572019-03-14 19:07:51 -07001637 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1638 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001639 return VPPCOM_EBADFD;
1640 }
1641
Florin Corasc127d5a2020-10-14 16:35:58 -07001642 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001643 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001644 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001645 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001646 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001647 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001648 return VPPCOM_EBADFD;
1649 }
1650 return VPPCOM_OK;
1651}
1652
1653int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001654vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1655{
1656 if (!strcmp (proto_str, "TCP"))
1657 *proto = VPPCOM_PROTO_TCP;
1658 else if (!strcmp (proto_str, "tcp"))
1659 *proto = VPPCOM_PROTO_TCP;
1660 else if (!strcmp (proto_str, "UDP"))
1661 *proto = VPPCOM_PROTO_UDP;
1662 else if (!strcmp (proto_str, "udp"))
1663 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001664 else if (!strcmp (proto_str, "TLS"))
1665 *proto = VPPCOM_PROTO_TLS;
1666 else if (!strcmp (proto_str, "tls"))
1667 *proto = VPPCOM_PROTO_TLS;
1668 else if (!strcmp (proto_str, "QUIC"))
1669 *proto = VPPCOM_PROTO_QUIC;
1670 else if (!strcmp (proto_str, "quic"))
1671 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001672 else if (!strcmp (proto_str, "DTLS"))
1673 *proto = VPPCOM_PROTO_DTLS;
1674 else if (!strcmp (proto_str, "dtls"))
1675 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001676 else if (!strcmp (proto_str, "SRTP"))
1677 *proto = VPPCOM_PROTO_SRTP;
1678 else if (!strcmp (proto_str, "srtp"))
1679 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001680 else
1681 return 1;
1682 return 0;
1683}
1684
1685int
Florin Coras134a9962018-08-28 11:32:04 -07001686vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001687 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001688{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001689 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001690 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001691 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001692 vcl_session_t *listen_session = 0;
1693 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001694 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001695 u8 is_nonblocking;
1696 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001697
Florin Coras5398dfb2021-01-25 20:31:27 -08001698again:
1699
Florin Coras134a9962018-08-28 11:32:04 -07001700 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001701 if (!listen_session)
1702 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001703
Florin Coras134a9962018-08-28 11:32:04 -07001704 listen_session_index = listen_session->session_index;
1705 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001706 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001707
Florin Coras54693d22018-07-17 10:46:29 -07001708 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001709 {
Florin Coras54693d22018-07-17 10:46:29 -07001710 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001711 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001712 accepted_msg = evt->accepted_msg;
1713 goto handle;
1714 }
1715
Florin Corasac422d62020-10-19 20:51:36 -07001716 is_nonblocking = vcl_session_has_attr (listen_session,
1717 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001718 while (1)
1719 {
Carl Smith592a9092019-11-12 14:57:37 +13001720 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1721 return VPPCOM_EAGAIN;
1722
Florin Coras5398dfb2021-01-25 20:31:27 -08001723 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1724 vcl_worker_flush_mq_events (wrk);
1725 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001726 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001727
Florin Coras54693d22018-07-17 10:46:29 -07001728handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001729
Florin Coras00cca802019-06-06 09:38:44 -07001730 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1731 listen_session_index);
1732 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1733 return VPPCOM_ECONNABORTED;
1734
Florin Coras134a9962018-08-28 11:32:04 -07001735 listen_session = vcl_session_get (wrk, listen_session_index);
1736 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001737
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001738 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001739 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001740
Florin Coras5e062572019-03-14 19:07:51 -07001741 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1742 " flags %d, is_nonblocking %u", listen_session->session_index,
1743 listen_session->vpp_handle, client_session_index,
1744 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001745 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001746
Dave Wallace048b1d62018-01-03 22:24:41 -05001747 if (ep)
1748 {
Florin Coras7e12d942018-06-27 14:32:43 -07001749 ep->is_ip4 = client_session->transport.is_ip4;
1750 ep->port = client_session->transport.rmt_port;
1751 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001752 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1753 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001754 else
Dave Barach178cf492018-11-13 16:34:13 -05001755 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1756 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001757 }
Dave Wallace60caa062017-11-10 17:07:13 -05001758
Florin Coras05ecfcc2018-12-12 18:19:39 -08001759 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001760 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001761 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001762 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001763 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001764 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001765 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001766 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001767 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001768 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1769 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001770
Florin Coras3c7d4f92018-12-14 11:28:43 -08001771 /*
1772 * Session might have been closed already
1773 */
1774 if (accept_flags)
1775 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001776 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001777 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001778 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001779 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001780 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001781 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001782}
1783
1784int
Florin Coras134a9962018-08-28 11:32:04 -07001785vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001786{
Florin Coras134a9962018-08-28 11:32:04 -07001787 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001788 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001789 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001790 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001791
Florin Coras134a9962018-08-28 11:32:04 -07001792 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001793 if (!session)
1794 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001795 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001796
Florin Coras6c3b2182020-10-19 18:36:48 -07001797 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001798 {
Florin Coras5e062572019-03-14 19:07:51 -07001799 VDBG (0, "ERROR: cannot connect epoll session %u!",
1800 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001801 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001802 }
1803
Florin Corasc127d5a2020-10-14 16:35:58 -07001804 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001805 {
Florin Coras7baeb712019-01-04 17:05:43 -08001806 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001807 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001808 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001809 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001810 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001811 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001812 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001813 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001814 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001815 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001816 }
1817
Florin Coras0a1e1832020-03-29 18:54:04 +00001818 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001819 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001820 {
1821 if (session->session_type != VPPCOM_PROTO_UDP)
1822 return VPPCOM_EINVAL;
1823 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001824 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001825 }
1826
Florin Coras7e12d942018-06-27 14:32:43 -07001827 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001828 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001829 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001830 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001831 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001832
Florin Coras0a1e1832020-03-29 18:54:04 +00001833 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001834 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001835 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001836 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001837 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001838 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001839 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001840 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001841 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001842
Florin Coras458089b2019-08-21 16:20:44 -07001843 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001844
Florin Corasac422d62020-10-19 20:51:36 -07001845 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001846 {
fanyfa49d59d2020-10-13 17:07:16 +08001847 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001848 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001849 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001850 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001851 return VPPCOM_EINPROGRESS;
1852 }
Florin Coras57c88932019-08-29 12:03:17 -07001853
1854 /*
1855 * Wait for reply from vpp if blocking
1856 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001857 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001858 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001859
Florin Coras134a9962018-08-28 11:32:04 -07001860 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001861 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1862 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001863
Dave Wallace4878cbe2017-11-21 03:45:09 -05001864 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001865}
1866
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001867int
1868vppcom_session_stream_connect (uint32_t session_handle,
1869 uint32_t parent_session_handle)
1870{
1871 vcl_worker_t *wrk = vcl_worker_get_current ();
1872 vcl_session_t *session, *parent_session;
1873 u32 session_index, parent_session_index;
1874 int rv;
1875
1876 session = vcl_session_get_w_handle (wrk, session_handle);
1877 if (!session)
1878 return VPPCOM_EBADFD;
1879 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1880 if (!parent_session)
1881 return VPPCOM_EBADFD;
1882
1883 session_index = session->session_index;
1884 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001885 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001886 {
1887 VDBG (0, "ERROR: cannot connect epoll session %u!",
1888 session->session_index);
1889 return VPPCOM_EBADFD;
1890 }
1891
Florin Corasc127d5a2020-10-14 16:35:58 -07001892 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001893 {
1894 VDBG (0, "session handle %u [0x%llx]: session already "
1895 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1896 session_handle, session->vpp_handle,
1897 parent_session_handle, parent_session->vpp_handle,
1898 vppcom_proto_str (session->session_type), session->session_state,
1899 vppcom_session_state_str (session->session_state));
1900 return VPPCOM_OK;
1901 }
1902
1903 /* Connect to quic session specifics */
1904 session->transport.is_ip4 = parent_session->transport.is_ip4;
1905 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1906 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001907 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001908
1909 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1910 session_handle, parent_session_handle, parent_session->vpp_handle);
1911
1912 /*
1913 * Send connect request and wait for reply from vpp
1914 */
Florin Coras458089b2019-08-21 16:20:44 -07001915 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001916 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001917 vcm->cfg.session_timeout);
1918
1919 session->listener_index = parent_session_index;
1920 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001921 if (parent_session)
1922 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001923
1924 session = vcl_session_get (wrk, session_index);
1925 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1926 session->vpp_handle, rv ? "failed" : "succeeded");
1927
1928 return rv;
1929}
1930
Steven58f464e2017-10-25 12:33:12 -07001931static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001932vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001933 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001934{
Florin Coras134a9962018-08-28 11:32:04 -07001935 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001936 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001937 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001938 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001939 session_event_t *e;
1940 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001941 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001942
Florin Coras070453d2018-08-24 17:04:27 -07001943 if (PREDICT_FALSE (!buf))
1944 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001945
Florin Coras134a9962018-08-28 11:32:04 -07001946 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001947 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001948 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001949
Florin Coras6d0106e2019-01-29 20:11:58 -08001950 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001951 {
Florin Coras5e062572019-03-14 19:07:51 -07001952 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001953 s->session_index, s->vpp_handle, s->session_state,
1954 vppcom_session_state_str (s->session_state));
1955 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001956 }
1957
liuyacan55c952e2021-06-13 14:54:55 +08001958 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_RD_SHUTDOWN))
1959 {
1960 /* Vpp would ack the incoming data and enqueue it for reading.
1961 * So even if SHUT_RD is set, we can still read() the data if
1962 * the session is ready.
1963 */
1964 if (!vcl_session_read_ready (s))
1965 {
1966 return 0;
1967 }
1968 }
1969
Florin Corasac422d62020-10-19 20:51:36 -07001970 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001971 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001972 mq = wrk->app_event_queue;
1973 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001974 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001975
Sirshak Das28aa5392019-02-05 01:33:33 -06001976 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001977 {
Florin Coras54693d22018-07-17 10:46:29 -07001978 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001979 {
Yu Pingb2955352019-12-04 06:49:04 +08001980 if (vcl_session_is_closing (s))
1981 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001982 if (is_ct)
1983 svm_fifo_unset_event (s->rx_fifo);
1984 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001985 return VPPCOM_EWOULDBLOCK;
1986 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001987 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001988 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001989 if (vcl_session_is_closing (s))
1990 return vcl_session_closing_error (s);
1991
Florin Corasd6894562020-10-28 00:37:15 -07001992 if (is_ct)
1993 svm_fifo_unset_event (s->rx_fifo);
1994 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001995
Florin Coras5398dfb2021-01-25 20:31:27 -08001996 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1997 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001998 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001999 }
Florin Coras54693d22018-07-17 10:46:29 -07002000
Florin Coras4a2c7942020-09-10 12:27:14 -07002001read_again:
2002
Florin Coras460dce62018-07-27 05:45:06 -07002003 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07002004 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002005 else
Florin Coras4a2c7942020-09-10 12:27:14 -07002006 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
2007
2008 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07002009
2010 if (peek)
2011 return rv;
2012
Florin Coras4a2c7942020-09-10 12:27:14 -07002013 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07002014
Sirshak Das28aa5392019-02-05 01:33:33 -06002015 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07002016 {
Florin Corasd6894562020-10-28 00:37:15 -07002017 if (is_ct)
2018 svm_fifo_unset_event (s->rx_fifo);
2019 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002020 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002021 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002022 {
Florin Corasc34118b2020-08-12 18:58:25 -07002023 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2024 e->event_type = SESSION_IO_EVT_RX;
2025 e->session_index = s->session_index;
2026 }
2027 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002028 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002029 {
2030 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002031 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002032 buf += rv;
2033 n -= rv;
2034 goto read_again;
2035 }
Florin Coras41c9e042018-09-11 00:10:41 -07002036
Florin Coras17ec5772020-08-12 20:46:29 -07002037 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002038 {
Florin Coras17ec5772020-08-12 20:46:29 -07002039 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002040 app_send_io_evt_to_vpp (s->vpp_evt_q,
2041 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002042 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002043 }
2044
Florin Coras5e062572019-03-14 19:07:51 -07002045 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2046 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002047
Florin Coras54693d22018-07-17 10:46:29 -07002048 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002049}
2050
Steven58f464e2017-10-25 12:33:12 -07002051int
Florin Coras134a9962018-08-28 11:32:04 -07002052vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002053{
Florin Coras134a9962018-08-28 11:32:04 -07002054 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002055}
2056
2057static int
Florin Coras134a9962018-08-28 11:32:04 -07002058vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002059{
Florin Coras134a9962018-08-28 11:32:04 -07002060 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002061}
2062
Florin Coras2cba8532018-09-11 16:33:36 -07002063int
2064vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002065 vppcom_data_segment_t * ds, uint32_t n_segments,
2066 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002067{
2068 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002069 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002070 vcl_session_t *s = 0;
2071 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002072 svm_msg_q_t *mq;
2073 u8 is_ct;
2074
2075 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002076 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002077 return VPPCOM_EBADFD;
2078
Florin Coras6d0106e2019-01-29 20:11:58 -08002079 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2080 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002081
Florin Corasac422d62020-10-19 20:51:36 -07002082 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002083 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002084 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002085 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002086 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002087
Sirshak Das28aa5392019-02-05 01:33:33 -06002088 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002089 {
2090 if (is_nonblocking)
2091 {
Florin Coras62877022020-10-28 21:22:04 -07002092 if (is_ct)
2093 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002094 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002095 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002096 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002097 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002098 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002099 if (vcl_session_is_closing (s))
2100 return vcl_session_closing_error (s);
2101
Florin Coras62877022020-10-28 21:22:04 -07002102 if (is_ct)
2103 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002104 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002105
Florin Coras5398dfb2021-01-25 20:31:27 -08002106 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2107 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002108 }
2109 }
2110
Florin Corasd1cc38d2020-10-11 11:05:04 -07002111 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2112 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2113 if (n_read < 0)
2114 return VPPCOM_EAGAIN;
2115
2116 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2117 {
Florin Coras62877022020-10-28 21:22:04 -07002118 if (is_ct)
2119 svm_fifo_unset_event (s->rx_fifo);
2120 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002121 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002122 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002123 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002124 {
2125 session_event_t *e;
2126 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2127 e->event_type = SESSION_IO_EVT_RX;
2128 e->session_index = s->session_index;
2129 }
2130 }
2131
2132 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002133 return n_read;
2134}
2135
2136void
Florin Corasd68faf82020-09-25 15:18:13 -07002137vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002138{
2139 vcl_worker_t *wrk = vcl_worker_get_current ();
2140 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002141 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002142
2143 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002144 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002145 return;
2146
Florin Coras62877022020-10-28 21:22:04 -07002147 is_ct = vcl_session_is_ct (s);
2148 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002149
2150 ASSERT (s->rx_bytes_pending < n_bytes);
2151 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002152}
2153
Florin Coras7a2abce2020-04-05 19:25:44 +00002154always_inline u8
2155vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002156{
Florin Coras7a2abce2020-04-05 19:25:44 +00002157 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2158 if (is_dgram)
2159 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2160 else
2161 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002162}
2163
2164always_inline int
2165vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2166 size_t n, u8 is_flush, u8 is_dgram)
2167{
Florin Coras6d0106e2019-01-29 20:11:58 -08002168 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002169 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002170 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002171 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002172 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002173
Florin Coras0b0d28e2021-06-04 17:31:53 -07002174 /* Accept zero length writes but just return */
2175 if (PREDICT_FALSE (!n))
2176 return VPPCOM_OK;
2177
2178 if (PREDICT_FALSE (!buf))
2179 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002180
Florin Coras6c3b2182020-10-19 18:36:48 -07002181 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002182 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002183 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2184 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002185 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002186 }
2187
liuyacan55c952e2021-06-13 14:54:55 +08002188 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002189 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002190 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2191 s->session_index, s->vpp_handle, s->session_state,
2192 vppcom_session_state_str (s->session_state));
2193 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002194 }
2195
liuyacan55c952e2021-06-13 14:54:55 +08002196 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_WR_SHUTDOWN))
2197 {
2198 VDBG (1, "session %u [0x%llx]: is shutdown! state 0x%x (%s)",
2199 s->session_index, s->vpp_handle, s->session_state,
2200 vppcom_session_state_str (s->session_state));
2201 return VPPCOM_EPIPE;
2202 }
2203
Florin Coras0e88e852018-09-17 22:09:02 -07002204 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002205 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002206 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002207
Florin Coras653e43f2019-03-04 10:56:23 -08002208 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002209 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002210 {
Florin Coras54693d22018-07-17 10:46:29 -07002211 if (is_nonblocking)
2212 {
Florin Coras070453d2018-08-24 17:04:27 -07002213 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002214 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002215 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002216 {
Florin Coras2d379d82019-06-28 12:45:12 -07002217 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002218 if (vcl_session_is_closing (s))
2219 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002220
Florin Coras5398dfb2021-01-25 20:31:27 -08002221 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2222 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002223 }
Dave Wallace543852a2017-08-03 02:11:34 -04002224 }
Dave Wallace543852a2017-08-03 02:11:34 -04002225
Florin Coras653e43f2019-03-04 10:56:23 -08002226 et = SESSION_IO_EVT_TX;
2227 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002228 et = SESSION_IO_EVT_TX_FLUSH;
2229
Florin Coras7a2abce2020-04-05 19:25:44 +00002230 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002231 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002232 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002233 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002234 else
2235 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002236 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002237
Florin Coras14ed6df2019-03-06 21:13:42 -08002238 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002239 app_send_io_evt_to_vpp (
2240 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002241
Florin Coras56230092020-09-02 20:52:58 -07002242 /* The underlying fifo segment can run out of memory */
2243 if (PREDICT_FALSE (n_write < 0))
2244 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002245
Florin Coras6d0106e2019-01-29 20:11:58 -08002246 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2247 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002248
Florin Coras54693d22018-07-17 10:46:29 -07002249 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002250}
2251
Florin Coras42ceddb2018-12-12 10:56:01 -08002252int
2253vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2254{
Florin Coras7a2abce2020-04-05 19:25:44 +00002255 vcl_worker_t *wrk = vcl_worker_get_current ();
2256 vcl_session_t *s;
2257
2258 s = vcl_session_get_w_handle (wrk, session_handle);
2259 if (PREDICT_FALSE (!s))
2260 return VPPCOM_EBADFD;
2261
2262 return vppcom_session_write_inline (wrk, s, buf, n,
2263 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002264}
2265
Florin Corasb0f662f2018-12-27 14:51:46 -08002266int
2267vppcom_session_write_msg (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 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002278}
2279
Florin Corasc0737e92019-03-04 14:19:39 -08002280#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002281if (PREDICT_FALSE (!_s->rx_fifo)) \
2282 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002283if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2284 { \
2285 if (!vcl_session_is_ct (_s)) \
2286 { \
2287 svm_fifo_unset_event (_s->rx_fifo); \
2288 if (svm_fifo_is_empty (_s->rx_fifo)) \
2289 break; \
2290 } \
2291 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2292 { \
Florin Coras2f630182020-08-13 00:17:51 -07002293 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002294 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2295 break; \
2296 } \
2297 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002298
Florin Coras86f04502018-09-12 16:08:01 -07002299static void
2300vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2301 unsigned long n_bits, unsigned long *read_map,
2302 unsigned long *write_map,
2303 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002304{
2305 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002306 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002307 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002308 u32 sid;
2309
2310 switch (e->event_type)
2311 {
Florin Corasc0737e92019-03-04 14:19:39 -08002312 case SESSION_IO_EVT_RX:
2313 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002314 s = vcl_session_get (wrk, sid);
2315 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002316 break;
Florin Corasb242d312020-10-26 15:35:40 -07002317 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002318 if (sid < n_bits && read_map)
2319 {
David Johnsond9818dd2018-12-14 14:53:41 -05002320 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002321 *bits_set += 1;
2322 }
2323 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002324 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002325 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002326 s = vcl_session_get (wrk, sid);
2327 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002328 break;
2329 if (sid < n_bits && write_map)
2330 {
David Johnsond9818dd2018-12-14 14:53:41 -05002331 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002332 *bits_set += 1;
2333 }
2334 break;
Florin Coras86f04502018-09-12 16:08:01 -07002335 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002336 if (!e->postponed)
2337 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2338 else
2339 s = vcl_session_get (wrk, e->session_index);
2340 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002341 break;
Florin Corasb242d312020-10-26 15:35:40 -07002342 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002343 if (sid < n_bits && read_map)
2344 {
David Johnsond9818dd2018-12-14 14:53:41 -05002345 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002346 *bits_set += 1;
2347 }
2348 break;
2349 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002350 if (!e->postponed)
2351 {
2352 connected_msg = (session_connected_msg_t *) e->data;
2353 sid = vcl_session_connected_handler (wrk, connected_msg);
2354 }
2355 else
2356 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002357 if (sid == VCL_INVALID_SESSION_INDEX)
2358 break;
2359 if (sid < n_bits && write_map)
2360 {
2361 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2362 *bits_set += 1;
2363 }
Florin Coras86f04502018-09-12 16:08:01 -07002364 break;
2365 case SESSION_CTRL_EVT_DISCONNECTED:
2366 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002367 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2368 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002369 break;
Florin Corasb242d312020-10-26 15:35:40 -07002370 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002371 if (sid < n_bits && except_map)
2372 {
David Johnsond9818dd2018-12-14 14:53:41 -05002373 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002374 *bits_set += 1;
2375 }
2376 break;
2377 case SESSION_CTRL_EVT_RESET:
2378 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2379 if (sid < n_bits && except_map)
2380 {
David Johnsond9818dd2018-12-14 14:53:41 -05002381 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002382 *bits_set += 1;
2383 }
2384 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002385 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2386 vcl_session_unlisten_reply_handler (wrk, e->data);
2387 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002388 case SESSION_CTRL_EVT_MIGRATED:
2389 vcl_session_migrated_handler (wrk, e->data);
2390 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002391 case SESSION_CTRL_EVT_CLEANUP:
2392 vcl_session_cleanup_handler (wrk, e->data);
2393 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002394 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2395 vcl_session_worker_update_reply_handler (wrk, e->data);
2396 break;
2397 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2398 vcl_session_req_worker_update_handler (wrk, e->data);
2399 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002400 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2401 vcl_session_app_add_segment_handler (wrk, e->data);
2402 break;
2403 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2404 vcl_session_app_del_segment_handler (wrk, e->data);
2405 break;
hanlina3a48962020-07-13 11:09:15 +08002406 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002407 vcl_worker_rpc_handler (wrk, e->data);
2408 break;
Florin Coras86f04502018-09-12 16:08:01 -07002409 default:
2410 clib_warning ("unhandled: %u", e->event_type);
2411 break;
2412 }
2413}
2414
2415static int
2416vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2417 unsigned long n_bits, unsigned long *read_map,
2418 unsigned long *write_map, unsigned long *except_map,
2419 double time_to_wait, u32 * bits_set)
2420{
Florin Coras99368312018-08-02 10:45:44 -07002421 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002422 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002423 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002424
Florin Coras54693d22018-07-17 10:46:29 -07002425 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002426 {
Florin Coras54693d22018-07-17 10:46:29 -07002427 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002428 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002429
Florin Coras54693d22018-07-17 10:46:29 -07002430 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002431 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002432 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002433 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002434 else
2435 {
2436 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002437 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002438 }
2439 }
Florin Corase003a1b2019-06-05 10:47:16 -07002440 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002441
Florin Coras134a9962018-08-28 11:32:04 -07002442 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002443 {
Florin Coras134a9962018-08-28 11:32:04 -07002444 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002445 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002446 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2447 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002448 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002449 }
Florin Coras134a9962018-08-28 11:32:04 -07002450 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002451 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002452 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002453}
2454
Florin Coras99368312018-08-02 10:45:44 -07002455static int
Florin Coras294afe22019-01-07 17:49:17 -08002456vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2457 vcl_si_set * read_map, vcl_si_set * write_map,
2458 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002459 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002460{
Florin Coras14ed6df2019-03-06 21:13:42 -08002461 double wait = 0, start = 0;
2462
2463 if (!*bits_set)
2464 {
2465 wait = time_to_wait;
2466 start = clib_time_now (&wrk->clib_time);
2467 }
2468
2469 do
2470 {
2471 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2472 write_map, except_map, wait, bits_set);
2473 if (*bits_set)
2474 return *bits_set;
2475 if (wait == -1)
2476 continue;
2477
2478 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2479 }
2480 while (wait > 0);
2481
2482 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002483}
2484
2485static int
Florin Coras294afe22019-01-07 17:49:17 -08002486vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2487 vcl_si_set * read_map, vcl_si_set * write_map,
2488 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002489 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002490{
2491 vcl_mq_evt_conn_t *mqc;
2492 int __clib_unused n_read;
2493 int n_mq_evts, i;
2494 u64 buf;
2495
Florin Coras134a9962018-08-28 11:32:04 -07002496 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2497 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2498 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002499 for (i = 0; i < n_mq_evts; i++)
2500 {
Florin Coras134a9962018-08-28 11:32:04 -07002501 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002502 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002503 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002504 except_map, 0, bits_set);
2505 }
2506
2507 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2508}
2509
Dave Wallace543852a2017-08-03 02:11:34 -04002510int
Florin Coras294afe22019-01-07 17:49:17 -08002511vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2512 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002513{
Florin Coras54693d22018-07-17 10:46:29 -07002514 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002515 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002516 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002517 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002518
Dave Wallace7876d392017-10-19 03:53:57 -04002519 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002520 {
Florin Coras134a9962018-08-28 11:32:04 -07002521 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002522 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002523 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2524 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002525 }
Dave Wallace7876d392017-10-19 03:53:57 -04002526 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002527 {
Florin Coras134a9962018-08-28 11:32:04 -07002528 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002529 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002530 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2531 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002532 }
Dave Wallace7876d392017-10-19 03:53:57 -04002533 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002534 {
Florin Coras134a9962018-08-28 11:32:04 -07002535 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002536 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002537 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2538 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002539 }
2540
Florin Coras54693d22018-07-17 10:46:29 -07002541 if (!n_bits)
2542 return 0;
2543
2544 if (!write_map)
2545 goto check_rd;
2546
Florin Coras096a1d52021-01-27 15:33:51 -08002547 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2548 {
2549 if (!(s = vcl_session_get (wrk, sid)))
2550 {
2551 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2552 bits_set++;
2553 continue;
2554 }
Florin Coras54693d22018-07-17 10:46:29 -07002555
Florin Coras096a1d52021-01-27 15:33:51 -08002556 if (vcl_session_write_ready (s))
2557 {
2558 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2559 bits_set++;
2560 }
2561 else
2562 {
2563 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2564 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2565 }
2566 }
Florin Coras54693d22018-07-17 10:46:29 -07002567
2568check_rd:
2569 if (!read_map)
2570 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002571
Florin Coras096a1d52021-01-27 15:33:51 -08002572 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2573 {
2574 if (!(s = vcl_session_get (wrk, sid)))
2575 {
2576 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2577 bits_set++;
2578 continue;
2579 }
Florin Coras54693d22018-07-17 10:46:29 -07002580
Florin Coras096a1d52021-01-27 15:33:51 -08002581 if (vcl_session_read_ready (s))
2582 {
2583 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2584 bits_set++;
2585 }
2586 }
Florin Coras54693d22018-07-17 10:46:29 -07002587
2588check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002589
Florin Coras86f04502018-09-12 16:08:01 -07002590 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2591 {
2592 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2593 read_map, write_map, except_map, &bits_set);
2594 }
2595 vec_reset_length (wrk->unhandled_evts_vector);
2596
Florin Coras99368312018-08-02 10:45:44 -07002597 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002598 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002599 time_to_wait, &bits_set);
2600 else
Florin Coras134a9962018-08-28 11:32:04 -07002601 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002602 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002603
Dave Wallace543852a2017-08-03 02:11:34 -04002604 return (bits_set);
2605}
2606
Dave Wallacef7f809c2017-10-03 01:48:42 -04002607static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002608vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002609{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002610 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002611 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002612 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002613
Florin Corasc0737e92019-03-04 14:19:39 -08002614 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002615 return;
2616
Florin Coras6c3b2182020-10-19 18:36:48 -07002617 s = vcl_session_get_w_handle (wrk, vep_handle);
2618 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002619 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002620 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002621 goto done;
2622 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002623 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002624 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002625 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002626 goto done;
2627 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002628 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002629 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002630 "{\n"
2631 " is_vep = %u\n"
2632 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002633 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002634 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2635 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002636
Florin Coras7e5e62b2019-07-30 14:08:23 -07002637 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002638 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002639 s = vcl_session_get_w_handle (wrk, sh);
2640 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002641 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002642 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002643 goto done;
2644 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002645 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002646 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002647 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002648 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002649 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002650 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002651 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002652 goto done;
2653 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002654 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002655 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2656 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002657 sh, s->vep.vep_sh, vep_handle);
2658 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002659 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002660 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002661 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002662 " next_sh = 0x%x (%u)\n"
2663 " prev_sh = 0x%x (%u)\n"
2664 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002665 " ev.events = 0x%x\n"
2666 " ev.data.u64 = 0x%llx\n"
2667 " et_mask = 0x%x\n"
2668 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002669 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002670 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2671 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002672 }
2673 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002674
2675done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002676 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002677}
2678
2679int
2680vppcom_epoll_create (void)
2681{
Florin Coras134a9962018-08-28 11:32:04 -07002682 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002683 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002684
Florin Coras134a9962018-08-28 11:32:04 -07002685 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686
Florin Coras6c3b2182020-10-19 18:36:48 -07002687 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002688 vep_session->vep.vep_sh = ~0;
2689 vep_session->vep.next_sh = ~0;
2690 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002691 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002692
Florin Corasa7a1a222018-12-30 17:11:31 -08002693 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2694 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002695
Florin Corasab2f6db2018-08-31 14:31:41 -07002696 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697}
2698
2699int
Florin Coras134a9962018-08-28 11:32:04 -07002700vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002701 struct epoll_event *event)
2702{
Florin Coras134a9962018-08-28 11:32:04 -07002703 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002704 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002705 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002706 vcl_session_t *s;
2707 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002708
Florin Coras134a9962018-08-28 11:32:04 -07002709 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002710 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002711 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002712 return VPPCOM_EINVAL;
2713 }
2714
Florin Coras134a9962018-08-28 11:32:04 -07002715 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002716 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002717 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002718 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002719 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002720 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002721 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002722 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002723 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002724 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002725 }
2726
Florin Coras134a9962018-08-28 11:32:04 -07002727 ASSERT (vep_session->vep.vep_sh == ~0);
2728 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002729
Florin Coras17ec5772020-08-12 20:46:29 -07002730 s = vcl_session_get_w_handle (wrk, session_handle);
2731 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002732 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002733 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002734 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002735 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002736 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002737 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002738 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002739 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002740 }
2741
2742 switch (op)
2743 {
2744 case EPOLL_CTL_ADD:
2745 if (PREDICT_FALSE (!event))
2746 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002747 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002748 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002749 }
Florin Coras2645f682021-06-04 15:59:41 -07002750 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2751 {
2752 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2753 rv = VPPCOM_EEXIST;
2754 goto done;
2755 }
Florin Coras134a9962018-08-28 11:32:04 -07002756 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757 {
Florin Coras7e12d942018-06-27 14:32:43 -07002758 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002759 next_session = vcl_session_get_w_handle (wrk,
2760 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002761 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002762 {
Florin Coras5e062572019-03-14 19:07:51 -07002763 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002764 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002765 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002766 }
Florin Coras134a9962018-08-28 11:32:04 -07002767 ASSERT (next_session->vep.prev_sh == vep_handle);
2768 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 }
Florin Coras17ec5772020-08-12 20:46:29 -07002770 s->vep.next_sh = vep_session->vep.next_sh;
2771 s->vep.prev_sh = vep_handle;
2772 s->vep.vep_sh = vep_handle;
2773 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2774 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002775 s->flags &= ~VCL_SESSION_F_IS_VEP;
2776 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002777 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002778
Florin Coras17ec5772020-08-12 20:46:29 -07002779 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2780 if (txf && (event->events & EPOLLOUT))
2781 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002782
Florin Coras6e3c1f82020-01-15 01:30:46 +00002783 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002784 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002785 {
2786 session_event_t e = { 0 };
2787 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002788 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002789 vec_add1 (wrk->unhandled_evts_vector, e);
2790 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002791 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002792 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002793 {
2794 session_event_t e = { 0 };
2795 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002796 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002797 vec_add1 (wrk->unhandled_evts_vector, e);
2798 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002799 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2800 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002801 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802 break;
2803
2804 case EPOLL_CTL_MOD:
2805 if (PREDICT_FALSE (!event))
2806 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002807 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002808 rv = VPPCOM_EINVAL;
2809 goto done;
2810 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002811 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002812 {
Florin Coras5e062572019-03-14 19:07:51 -07002813 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002814 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002815 goto done;
2816 }
Florin Coras17ec5772020-08-12 20:46:29 -07002817 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002818 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002819 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002820 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002821 rv = VPPCOM_EINVAL;
2822 goto done;
2823 }
hanlin475c9d72019-12-26 11:44:28 +08002824
Florin Coras2645f682021-06-04 15:59:41 -07002825 /* Generate EPOLLOUT if session write ready nd event was not on */
2826 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2827 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002828 {
2829 session_event_t e = { 0 };
2830 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002831 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002832 vec_add1 (wrk->unhandled_evts_vector, e);
2833 }
Florin Coras2645f682021-06-04 15:59:41 -07002834 /* Generate EPOLLIN if session read ready and event was not on */
2835 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2836 (vcl_session_read_ready (s) > 0))
2837 {
2838 session_event_t e = { 0 };
2839 e.event_type = SESSION_IO_EVT_RX;
2840 e.session_index = s->session_index;
2841 vec_add1 (wrk->unhandled_evts_vector, e);
2842 }
Florin Coras17ec5772020-08-12 20:46:29 -07002843 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2844 s->vep.ev = *event;
2845 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002846 if (txf)
2847 {
2848 if (event->events & EPOLLOUT)
2849 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2850 else
2851 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2852 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002853 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2854 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002855 break;
2856
2857 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002858 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002859 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002860 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002861 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002862 goto done;
2863 }
Florin Coras17ec5772020-08-12 20:46:29 -07002864 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002865 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002866 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002867 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002868 rv = VPPCOM_EINVAL;
2869 goto done;
2870 }
2871
Florin Coras17ec5772020-08-12 20:46:29 -07002872 if (s->vep.prev_sh == vep_handle)
2873 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002874 else
2875 {
Florin Coras7e12d942018-06-27 14:32:43 -07002876 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002877 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002878 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002879 {
Florin Coras5e062572019-03-14 19:07:51 -07002880 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002881 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002882 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002883 }
Florin Coras134a9962018-08-28 11:32:04 -07002884 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002885 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002886 }
Florin Coras17ec5772020-08-12 20:46:29 -07002887 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002888 {
Florin Coras7e12d942018-06-27 14:32:43 -07002889 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002890 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002891 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002892 {
Florin Coras5e062572019-03-14 19:07:51 -07002893 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002894 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002895 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002896 }
Florin Coras134a9962018-08-28 11:32:04 -07002897 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002898 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002899 }
2900
Florin Coras17ec5772020-08-12 20:46:29 -07002901 memset (&s->vep, 0, sizeof (s->vep));
2902 s->vep.next_sh = ~0;
2903 s->vep.prev_sh = ~0;
2904 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002905 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002906
Florin Corasf1ddeeb2021-06-10 08:08:53 -07002907 if (vcl_session_is_open (s))
2908 {
2909 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2910 if (txf)
2911 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2912 }
Florin Coras1bcad5c2019-01-09 20:04:38 -08002913
Florin Coras5e062572019-03-14 19:07:51 -07002914 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002915 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002916 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002917 break;
2918
2919 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002920 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002921 rv = VPPCOM_EINVAL;
2922 }
2923
Florin Coras134a9962018-08-28 11:32:04 -07002924 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002925
2926done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002927 return rv;
2928}
2929
Florin Coras86f04502018-09-12 16:08:01 -07002930static inline void
2931vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2932 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002933{
2934 session_disconnected_msg_t *disconnected_msg;
2935 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002936 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002937 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002938 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002939 u8 add_event = 0;
2940
2941 switch (e->event_type)
2942 {
Florin Coras653e43f2019-03-04 10:56:23 -08002943 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002944 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002945 s = vcl_session_get (wrk, sid);
2946 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002947 break;
Florin Corasb242d312020-10-26 15:35:40 -07002948 vcl_fifo_rx_evt_valid_or_break (s);
2949 session_events = s->vep.ev.events;
2950 if (!(EPOLLIN & s->vep.ev.events)
2951 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002952 break;
2953 add_event = 1;
2954 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002955 session_evt_data = s->vep.ev.data.u64;
2956 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002957 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002958 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002959 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002960 s = vcl_session_get (wrk, sid);
2961 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002962 break;
Florin Corasb242d312020-10-26 15:35:40 -07002963 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002964 if (!(EPOLLOUT & session_events))
2965 break;
2966 add_event = 1;
2967 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002968 session_evt_data = s->vep.ev.data.u64;
2969 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2970 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002971 break;
Florin Coras86f04502018-09-12 16:08:01 -07002972 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002973 if (!e->postponed)
2974 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2975 else
2976 s = vcl_session_get (wrk, e->session_index);
2977 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002978 break;
Florin Corasb242d312020-10-26 15:35:40 -07002979 session_events = s->vep.ev.events;
2980 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002981 if (!(EPOLLIN & session_events))
2982 break;
Florin Coras86f04502018-09-12 16:08:01 -07002983 add_event = 1;
2984 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002985 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002986 break;
2987 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002988 if (!e->postponed)
2989 {
2990 connected_msg = (session_connected_msg_t *) e->data;
2991 sid = vcl_session_connected_handler (wrk, connected_msg);
2992 }
2993 else
2994 sid = e->session_index;
2995 s = vcl_session_get (wrk, sid);
2996 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002997 break;
Florin Corasb242d312020-10-26 15:35:40 -07002998 session_events = s->vep.ev.events;
2999 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08003000 if (!(EPOLLOUT & session_events))
3001 break;
3002 add_event = 1;
3003 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07003004 session_evt_data = s->vep.ev.data.u64;
3005 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07003006 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07003007 break;
3008 case SESSION_CTRL_EVT_DISCONNECTED:
3009 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07003010 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
3011 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07003012 break;
Florin Corasb242d312020-10-26 15:35:40 -07003013 sid = s->session_index;
3014 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003015 add_event = 1;
3016 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003017 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003018 break;
3019 case SESSION_CTRL_EVT_RESET:
3020 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07003021 s = vcl_session_get (wrk, sid);
3022 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07003023 break;
Florin Corasb242d312020-10-26 15:35:40 -07003024 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07003025 add_event = 1;
3026 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07003027 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07003028 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003029 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3030 vcl_session_unlisten_reply_handler (wrk, e->data);
3031 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003032 case SESSION_CTRL_EVT_MIGRATED:
3033 vcl_session_migrated_handler (wrk, e->data);
3034 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003035 case SESSION_CTRL_EVT_CLEANUP:
3036 vcl_session_cleanup_handler (wrk, e->data);
3037 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003038 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3039 vcl_session_req_worker_update_handler (wrk, e->data);
3040 break;
3041 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3042 vcl_session_worker_update_reply_handler (wrk, e->data);
3043 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003044 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3045 vcl_session_app_add_segment_handler (wrk, e->data);
3046 break;
3047 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3048 vcl_session_app_del_segment_handler (wrk, e->data);
3049 break;
hanlina3a48962020-07-13 11:09:15 +08003050 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003051 vcl_worker_rpc_handler (wrk, e->data);
3052 break;
Florin Coras86f04502018-09-12 16:08:01 -07003053 default:
3054 VDBG (0, "unhandled: %u", e->event_type);
3055 break;
3056 }
3057
3058 if (add_event)
3059 {
3060 events[*num_ev].data.u64 = session_evt_data;
3061 if (EPOLLONESHOT & session_events)
3062 {
Florin Corasb242d312020-10-26 15:35:40 -07003063 s = vcl_session_get (wrk, sid);
3064 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003065 }
3066 *num_ev += 1;
3067 }
3068}
3069
3070static int
3071vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3072 struct epoll_event *events, u32 maxevents,
3073 double wait_for_time, u32 * num_ev)
3074{
Florin Coras99368312018-08-02 10:45:44 -07003075 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003076 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003077 int i;
3078
Florin Coras539663c2018-09-28 14:59:37 -07003079 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3080 goto handle_dequeued;
3081
Florin Coras54693d22018-07-17 10:46:29 -07003082 if (svm_msg_q_is_empty (mq))
3083 {
3084 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003085 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003086 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003087 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003088 else
3089 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003090 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003091 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003092 }
3093 }
Florin Corase003a1b2019-06-05 10:47:16 -07003094 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003095 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003096
Florin Coras539663c2018-09-28 14:59:37 -07003097handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003098 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003099 {
Florin Coras134a9962018-08-28 11:32:04 -07003100 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003101 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003102 if (*num_ev < maxevents)
3103 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3104 else
3105 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003106 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003107 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003108 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003109 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003110 return *num_ev;
3111}
3112
Florin Coras99368312018-08-02 10:45:44 -07003113static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003114vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3115 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003116{
Florin Corasccdb8b82021-04-28 13:01:06 -07003117 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003118
3119 if (!n_evts)
3120 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003121 if (timeout_ms > 0)
3122 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003123 }
3124
3125 do
3126 {
3127 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003128 timeout_ms, &n_evts);
3129 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003130 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003131 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003132 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003133
3134 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003135}
3136
3137static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003138vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3139 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003140{
Florin Coras99368312018-08-02 10:45:44 -07003141 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003142 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003143 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003144 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003145 u64 buf;
3146
Florin Coras134a9962018-08-28 11:32:04 -07003147 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003148 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003149 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003150 if (timeout_ms > 0)
3151 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003152 }
3153
Florin Corasb13ba132021-01-27 16:05:24 -08003154 do
3155 {
3156 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003157 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003158 if (n_mq_evts < 0)
3159 {
3160 VDBG (0, "epoll_wait error %u", errno);
3161 return n_evts;
3162 }
3163
3164 for (i = 0; i < n_mq_evts; i++)
3165 {
3166 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3167 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3168 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3169 &n_evts);
3170 }
3171
Florin Corasccdb8b82021-04-28 13:01:06 -07003172 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003173 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003174 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003175 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003176
3177 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003178}
3179
Dave Wallacef7f809c2017-10-03 01:48:42 -04003180int
Florin Coras134a9962018-08-28 11:32:04 -07003181vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003182 int maxevents, double wait_for_time)
3183{
Florin Coras134a9962018-08-28 11:32:04 -07003184 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003185 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003186 u32 n_evts = 0;
3187 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003188
3189 if (PREDICT_FALSE (maxevents <= 0))
3190 {
Florin Coras5e062572019-03-14 19:07:51 -07003191 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003192 return VPPCOM_EINVAL;
3193 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003194
Florin Coras134a9962018-08-28 11:32:04 -07003195 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003196 if (!vep_session)
3197 return VPPCOM_EBADFD;
3198
Florin Coras6c3b2182020-10-19 18:36:48 -07003199 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003200 {
Florin Coras5e062572019-03-14 19:07:51 -07003201 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003202 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003203 }
Florin Coras54693d22018-07-17 10:46:29 -07003204
3205 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003206
Florin Coras86f04502018-09-12 16:08:01 -07003207 if (vec_len (wrk->unhandled_evts_vector))
3208 {
3209 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3210 {
3211 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3212 events, &n_evts);
3213 if (n_evts == maxevents)
3214 {
Florin Corase003a1b2019-06-05 10:47:16 -07003215 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3216 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003217 }
3218 }
Florin Corase003a1b2019-06-05 10:47:16 -07003219 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003220 }
wanghanlin8919fec2021-03-18 20:00:41 +08003221 /* Request to only drain unhandled */
3222 if ((int) wait_for_time == -2)
3223 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003224
3225 if (vcm->cfg.use_mq_eventfd)
3226 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3227 wait_for_time);
3228
3229 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3230 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003231}
3232
Dave Wallace35830af2017-10-09 01:43:42 -04003233int
Florin Coras134a9962018-08-28 11:32:04 -07003234vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003235 void *buffer, uint32_t * buflen)
3236{
Florin Coras134a9962018-08-28 11:32:04 -07003237 vcl_worker_t *wrk = vcl_worker_get_current ();
liuyacan55c952e2021-06-13 14:54:55 +08003238 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003239 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003240 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003241 vcl_session_t *session;
3242 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003243
Florin Coras134a9962018-08-28 11:32:04 -07003244 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003245 if (!session)
3246 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003247
Dave Wallace35830af2017-10-09 01:43:42 -04003248 switch (op)
3249 {
3250 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003251 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003252 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3253 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003254 break;
3255
Dave Wallace227867f2017-11-13 21:21:53 -05003256 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003257 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003258 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3259 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003260 break;
3261
3262 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003263 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003264 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003265 *flags =
3266 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003267 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003268 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003269 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003270 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3271 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003272 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003273 }
3274 else
3275 rv = VPPCOM_EINVAL;
3276 break;
3277
3278 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003279 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003280 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003281 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003282 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003283 else
Florin Corasac422d62020-10-19 20:51:36 -07003284 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003285
Florin Coras5e062572019-03-14 19:07:51 -07003286 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3287 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003288 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003289 }
3290 else
3291 rv = VPPCOM_EINVAL;
3292 break;
3293
3294 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003295 if (PREDICT_TRUE (buffer && buflen &&
3296 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003297 {
Florin Coras7e12d942018-06-27 14:32:43 -07003298 ep->is_ip4 = session->transport.is_ip4;
3299 ep->port = session->transport.rmt_port;
3300 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003301 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3302 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003303 else
Dave Barach178cf492018-11-13 16:34:13 -05003304 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3305 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003306 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003307 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3308 "addr = %U, port %u", session_handle, ep->is_ip4,
3309 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003310 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3311 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003312 }
3313 else
3314 rv = VPPCOM_EINVAL;
3315 break;
3316
3317 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003318 if (PREDICT_TRUE (buffer && buflen &&
3319 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003320 {
Florin Coras7e12d942018-06-27 14:32:43 -07003321 ep->is_ip4 = session->transport.is_ip4;
3322 ep->port = session->transport.lcl_port;
3323 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003324 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3325 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003326 else
Dave Barach178cf492018-11-13 16:34:13 -05003327 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3328 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003329 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003330 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3331 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003332 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003333 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3334 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003335 }
3336 else
3337 rv = VPPCOM_EINVAL;
3338 break;
Stevenb5a11602017-10-11 09:59:30 -07003339
Florin Corasef7cbf62019-10-17 09:56:27 -07003340 case VPPCOM_ATTR_SET_LCL_ADDR:
3341 if (PREDICT_TRUE (buffer && buflen &&
3342 (*buflen >= sizeof (*ep)) && ep->ip))
3343 {
3344 session->transport.is_ip4 = ep->is_ip4;
3345 session->transport.lcl_port = ep->port;
3346 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3347 *buflen = sizeof (*ep);
3348 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3349 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3350 &session->transport.lcl_ip,
3351 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3352 clib_net_to_host_u16 (ep->port));
3353 }
3354 else
3355 rv = VPPCOM_EINVAL;
3356 break;
3357
Dave Wallace048b1d62018-01-03 22:24:41 -05003358 case VPPCOM_ATTR_GET_LIBC_EPFD:
3359 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003360 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003361 break;
3362
3363 case VPPCOM_ATTR_SET_LIBC_EPFD:
3364 if (PREDICT_TRUE (buffer && buflen &&
3365 (*buflen == sizeof (session->libc_epfd))))
3366 {
3367 session->libc_epfd = *(int *) buffer;
3368 *buflen = sizeof (session->libc_epfd);
3369
Florin Coras5e062572019-03-14 19:07:51 -07003370 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3371 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003372 }
3373 else
3374 rv = VPPCOM_EINVAL;
3375 break;
3376
3377 case VPPCOM_ATTR_GET_PROTOCOL:
3378 if (buffer && buflen && (*buflen >= sizeof (int)))
3379 {
Florin Coras7e12d942018-06-27 14:32:43 -07003380 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003381 *buflen = sizeof (int);
3382
Florin Coras5e062572019-03-14 19:07:51 -07003383 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3384 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003385 }
3386 else
3387 rv = VPPCOM_EINVAL;
3388 break;
3389
3390 case VPPCOM_ATTR_GET_LISTEN:
3391 if (buffer && buflen && (*buflen >= sizeof (int)))
3392 {
Florin Corasac422d62020-10-19 20:51:36 -07003393 *(int *) buffer = vcl_session_has_attr (session,
3394 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003395 *buflen = sizeof (int);
3396
Florin Coras5e062572019-03-14 19:07:51 -07003397 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3398 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003399 }
3400 else
3401 rv = VPPCOM_EINVAL;
3402 break;
3403
3404 case VPPCOM_ATTR_GET_ERROR:
3405 if (buffer && buflen && (*buflen >= sizeof (int)))
3406 {
3407 *(int *) buffer = 0;
3408 *buflen = sizeof (int);
3409
Florin Coras5e062572019-03-14 19:07:51 -07003410 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3411 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003412 }
3413 else
3414 rv = VPPCOM_EINVAL;
3415 break;
3416
3417 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3418 if (buffer && buflen && (*buflen >= sizeof (u32)))
3419 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003420
3421 /* VPP-TBD */
3422 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003423 session->tx_fifo ?
3424 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003425 vcm->cfg.tx_fifo_size);
3426 *buflen = sizeof (u32);
3427
Florin Coras5e062572019-03-14 19:07:51 -07003428 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3429 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3430 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003431 }
3432 else
3433 rv = VPPCOM_EINVAL;
3434 break;
3435
3436 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3437 if (buffer && buflen && (*buflen == sizeof (u32)))
3438 {
3439 /* VPP-TBD */
3440 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003441 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3442 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3443 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003444 }
3445 else
3446 rv = VPPCOM_EINVAL;
3447 break;
3448
3449 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3450 if (buffer && buflen && (*buflen >= sizeof (u32)))
3451 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003452
3453 /* VPP-TBD */
3454 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003455 session->rx_fifo ?
3456 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003457 vcm->cfg.rx_fifo_size);
3458 *buflen = sizeof (u32);
3459
Florin Coras5e062572019-03-14 19:07:51 -07003460 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3461 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003462 }
3463 else
3464 rv = VPPCOM_EINVAL;
3465 break;
3466
3467 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3468 if (buffer && buflen && (*buflen == sizeof (u32)))
3469 {
3470 /* VPP-TBD */
3471 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3473 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3474 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 }
3476 else
3477 rv = VPPCOM_EINVAL;
3478 break;
3479
3480 case VPPCOM_ATTR_GET_REUSEADDR:
3481 if (buffer && buflen && (*buflen >= sizeof (int)))
3482 {
3483 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003484 *(int *) buffer = vcl_session_has_attr (session,
3485 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003486 *buflen = sizeof (int);
3487
Florin Coras5e062572019-03-14 19:07:51 -07003488 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3489 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 }
3491 else
3492 rv = VPPCOM_EINVAL;
3493 break;
3494
Stevenb5a11602017-10-11 09:59:30 -07003495 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003497 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003498 {
3499 /* VPP-TBD */
3500 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003501 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003502 else
Florin Corasac422d62020-10-19 20:51:36 -07003503 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003504
Florin Coras5e062572019-03-14 19:07:51 -07003505 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003506 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003507 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003508 }
3509 else
3510 rv = VPPCOM_EINVAL;
3511 break;
3512
3513 case VPPCOM_ATTR_GET_REUSEPORT:
3514 if (buffer && buflen && (*buflen >= sizeof (int)))
3515 {
3516 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003517 *(int *) buffer = vcl_session_has_attr (session,
3518 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003519 *buflen = sizeof (int);
3520
Florin Coras5e062572019-03-14 19:07:51 -07003521 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3522 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003523 }
3524 else
3525 rv = VPPCOM_EINVAL;
3526 break;
3527
3528 case VPPCOM_ATTR_SET_REUSEPORT:
3529 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003530 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003531 {
3532 /* VPP-TBD */
3533 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003534 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003535 else
Florin Corasac422d62020-10-19 20:51:36 -07003536 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003537
Florin Coras5e062572019-03-14 19:07:51 -07003538 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003539 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003540 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003541 }
3542 else
3543 rv = VPPCOM_EINVAL;
3544 break;
3545
3546 case VPPCOM_ATTR_GET_BROADCAST:
3547 if (buffer && buflen && (*buflen >= sizeof (int)))
3548 {
3549 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003550 *(int *) buffer = vcl_session_has_attr (session,
3551 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003552 *buflen = sizeof (int);
3553
Florin Coras5e062572019-03-14 19:07:51 -07003554 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3555 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003556 }
3557 else
3558 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003559 break;
3560
3561 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003562 if (buffer && buflen && (*buflen == sizeof (int)))
3563 {
3564 /* VPP-TBD */
3565 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003566 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003567 else
Florin Corasac422d62020-10-19 20:51:36 -07003568 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003569
Florin Coras5e062572019-03-14 19:07:51 -07003570 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003571 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003572 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003573 }
3574 else
3575 rv = VPPCOM_EINVAL;
3576 break;
3577
3578 case VPPCOM_ATTR_GET_V6ONLY:
3579 if (buffer && buflen && (*buflen >= sizeof (int)))
3580 {
3581 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003582 *(int *) buffer = vcl_session_has_attr (session,
3583 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003584 *buflen = sizeof (int);
3585
Florin Coras5e062572019-03-14 19:07:51 -07003586 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3587 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003588 }
3589 else
3590 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003591 break;
3592
3593 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003594 if (buffer && buflen && (*buflen == sizeof (int)))
3595 {
3596 /* VPP-TBD */
3597 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003598 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003599 else
Florin Corasac422d62020-10-19 20:51:36 -07003600 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003601
Florin Coras5e062572019-03-14 19:07:51 -07003602 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003603 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003604 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003605 }
3606 else
3607 rv = VPPCOM_EINVAL;
3608 break;
3609
3610 case VPPCOM_ATTR_GET_KEEPALIVE:
3611 if (buffer && buflen && (*buflen >= sizeof (int)))
3612 {
3613 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003614 *(int *) buffer = vcl_session_has_attr (session,
3615 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003616 *buflen = sizeof (int);
3617
Florin Coras5e062572019-03-14 19:07:51 -07003618 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3619 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003620 }
3621 else
3622 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003623 break;
Stevenbccd3392017-10-12 20:42:21 -07003624
3625 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003626 if (buffer && buflen && (*buflen == sizeof (int)))
3627 {
3628 /* VPP-TBD */
3629 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003630 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003631 else
Florin Corasac422d62020-10-19 20:51:36 -07003632 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003633
Florin Coras5e062572019-03-14 19:07:51 -07003634 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003635 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003636 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003637 }
3638 else
3639 rv = VPPCOM_EINVAL;
3640 break;
3641
3642 case VPPCOM_ATTR_GET_TCP_NODELAY:
3643 if (buffer && buflen && (*buflen >= sizeof (int)))
3644 {
3645 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003646 *(int *) buffer = vcl_session_has_attr (session,
3647 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003648 *buflen = sizeof (int);
3649
Florin Coras5e062572019-03-14 19:07:51 -07003650 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3651 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003652 }
3653 else
3654 rv = VPPCOM_EINVAL;
3655 break;
3656
3657 case VPPCOM_ATTR_SET_TCP_NODELAY:
3658 if (buffer && buflen && (*buflen == sizeof (int)))
3659 {
3660 /* VPP-TBD */
3661 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003662 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003663 else
Florin Corasac422d62020-10-19 20:51:36 -07003664 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003665
Florin Coras5e062572019-03-14 19:07:51 -07003666 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003667 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003668 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003669 }
3670 else
3671 rv = VPPCOM_EINVAL;
3672 break;
3673
3674 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3675 if (buffer && buflen && (*buflen >= sizeof (int)))
3676 {
3677 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003678 *(int *) buffer = vcl_session_has_attr (session,
3679 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003680 *buflen = sizeof (int);
3681
Florin Coras5e062572019-03-14 19:07:51 -07003682 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3683 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003684 }
3685 else
3686 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003687 break;
3688
3689 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003690 if (buffer && buflen && (*buflen == sizeof (int)))
3691 {
3692 /* VPP-TBD */
3693 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003694 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003695 else
Florin Corasac422d62020-10-19 20:51:36 -07003696 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003697
Florin Coras5e062572019-03-14 19:07:51 -07003698 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003699 vcl_session_has_attr (session,
3700 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003701 }
3702 else
3703 rv = VPPCOM_EINVAL;
3704 break;
3705
3706 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3707 if (buffer && buflen && (*buflen >= sizeof (int)))
3708 {
3709 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003710 *(int *) buffer = vcl_session_has_attr (session,
3711 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003712 *buflen = sizeof (int);
3713
Florin Coras5e062572019-03-14 19:07:51 -07003714 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3715 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003716 }
3717 else
3718 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003719 break;
3720
3721 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003722 if (buffer && buflen && (*buflen == sizeof (int)))
3723 {
3724 /* VPP-TBD */
3725 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003726 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003727 else
Florin Corasac422d62020-10-19 20:51:36 -07003728 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003729
Florin Coras5e062572019-03-14 19:07:51 -07003730 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003731 vcl_session_has_attr (session,
3732 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003733 }
3734 else
3735 rv = VPPCOM_EINVAL;
3736 break;
3737
3738 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003739 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003740 {
Florin Coras04ae8272021-04-12 19:55:37 -07003741 rv = VPPCOM_EINVAL;
3742 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003743 }
Florin Coras04ae8272021-04-12 19:55:37 -07003744
3745 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3746 tea.mss = *(u32 *) buffer;
3747 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3748 rv = VPPCOM_ENOPROTOOPT;
3749
3750 if (!rv)
3751 {
3752 *(u32 *) buffer = tea.mss;
3753 *buflen = sizeof (int);
3754 }
3755
3756 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3757 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003758 break;
3759
3760 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003761 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003762 {
Florin Coras04ae8272021-04-12 19:55:37 -07003763 rv = VPPCOM_EINVAL;
3764 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003765 }
Florin Coras04ae8272021-04-12 19:55:37 -07003766
3767 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3768 tea.mss = *(u32 *) buffer;
3769 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3770 rv = VPPCOM_ENOPROTOOPT;
3771
3772 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3773 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003774 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003775
Florin Coras1e966172020-05-16 18:18:14 +00003776 case VPPCOM_ATTR_SET_CONNECTED:
3777 session->flags |= VCL_SESSION_F_CONNECTED;
3778 break;
3779
Florin Corasa5a9efd2021-01-05 17:03:29 -08003780 case VPPCOM_ATTR_SET_CKPAIR:
3781 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3782 !vcl_session_has_crypto (session))
3783 {
3784 rv = VPPCOM_EINVAL;
3785 break;
3786 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003787 if (!session->ext_config)
3788 {
Florin Coras87f63892021-05-01 16:01:40 -07003789 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3790 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003791 }
3792 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3793 {
3794 rv = VPPCOM_EINVAL;
3795 break;
3796 }
3797
3798 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003799 break;
3800
Florin Coras6a6555a2021-01-28 11:39:27 -08003801 case VPPCOM_ATTR_SET_VRF:
3802 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3803 {
3804 rv = VPPCOM_EINVAL;
3805 break;
3806 }
3807 session->vrf = *(u32 *) buffer;
3808 break;
3809
3810 case VPPCOM_ATTR_GET_VRF:
3811 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3812 {
3813 rv = VPPCOM_EINVAL;
3814 break;
3815 }
3816 *(u32 *) buffer = session->vrf;
3817 *buflen = sizeof (u32);
3818 break;
3819
wanghanlin0674f852021-02-22 10:38:36 +08003820 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003821 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003822 {
Florin Corasd77325c2021-02-23 12:03:03 -08003823 rv = VPPCOM_EINVAL;
3824 break;
wanghanlin0674f852021-02-22 10:38:36 +08003825 }
Florin Corasd77325c2021-02-23 12:03:03 -08003826
3827 if (session->transport.is_ip4)
3828 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003829 else
Florin Corasd77325c2021-02-23 12:03:03 -08003830 *(int *) buffer = AF_INET6;
3831 *buflen = sizeof (int);
3832
wanghanlin0674f852021-02-22 10:38:36 +08003833 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3834 *buflen);
3835 break;
3836
Florin Coras87f63892021-05-01 16:01:40 -07003837 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3838 if (!(buffer && buflen && (*buflen > 0)))
3839 {
3840 rv = VPPCOM_EINVAL;
3841 break;
3842 }
3843 if (session->ext_config)
3844 {
3845 rv = VPPCOM_EINVAL;
3846 break;
3847 }
3848 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3849 *buflen + sizeof (u32));
3850 clib_memcpy (session->ext_config->data, buffer, *buflen);
3851 session->ext_config->len = *buflen;
3852 break;
3853
Dave Wallacee22aa742017-10-20 12:30:38 -04003854 default:
3855 rv = VPPCOM_EINVAL;
3856 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003857 }
3858
Dave Wallace35830af2017-10-09 01:43:42 -04003859 return rv;
3860}
3861
Stevenac1f96d2017-10-24 16:03:58 -07003862int
Florin Coras134a9962018-08-28 11:32:04 -07003863vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003864 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3865{
Florin Coras134a9962018-08-28 11:32:04 -07003866 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003867 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003868 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003869
3870 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003871 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003872 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003873 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003874 else
3875 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003876 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003877 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003878 }
3879
Florin Coras0a1e1832020-03-29 18:54:04 +00003880 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003881 {
Florin Coras5da10c42020-04-01 04:31:21 +00003882 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003883 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003884 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3885 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003886 else
Dave Barach178cf492018-11-13 16:34:13 -05003887 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3888 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003889 ep->is_ip4 = session->transport.is_ip4;
3890 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003891 }
Florin Coras460dce62018-07-27 05:45:06 -07003892
Stevenac1f96d2017-10-24 16:03:58 -07003893 return rv;
3894}
3895
3896int
Florin Coras134a9962018-08-28 11:32:04 -07003897vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003898 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3899{
Florin Coras7a2abce2020-04-05 19:25:44 +00003900 vcl_worker_t *wrk = vcl_worker_get_current ();
3901 vcl_session_t *s;
3902
3903 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07003904 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00003905 return VPPCOM_EBADFD;
3906
Dave Wallace617dffa2017-10-26 14:47:06 -04003907 if (ep)
3908 {
Florin Coras5824cc52020-10-20 18:44:41 -07003909 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003910 return VPPCOM_EINVAL;
3911
3912 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003913 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003914 {
Florin Coras5824cc52020-10-20 18:44:41 -07003915 u32 session_index = s->session_index;
3916 f64 timeout = vcm->cfg.session_timeout;
3917 int rv;
3918
Florin Coras5da10c42020-04-01 04:31:21 +00003919 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003920 rv = vppcom_wait_for_session_state_change (session_index,
3921 VCL_STATE_READY,
3922 timeout);
3923 if (rv < 0)
3924 return rv;
3925 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003926 }
Florin Coras5824cc52020-10-20 18:44:41 -07003927
3928 s->transport.is_ip4 = ep->is_ip4;
3929 s->transport.rmt_port = ep->port;
3930 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003931 }
3932
3933 if (flags)
3934 {
3935 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003936 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003937 }
3938
Florin Coras7a2abce2020-04-05 19:25:44 +00003939 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3940 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003941}
3942
Dave Wallace048b1d62018-01-03 22:24:41 -05003943int
3944vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3945{
Florin Coras134a9962018-08-28 11:32:04 -07003946 vcl_worker_t *wrk = vcl_worker_get_current ();
3947 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003948 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003949 svm_msg_q_msg_t msg;
3950 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003951 int rv, num_ev = 0;
3952
Florin Coras5e062572019-03-14 19:07:51 -07003953 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003954
3955 if (!vp)
3956 return VPPCOM_EFAULT;
3957
3958 do
3959 {
Florin Coras7e12d942018-06-27 14:32:43 -07003960 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003961
Florin Coras6917b942018-11-13 22:44:54 -08003962 /* Dequeue all events and drop all unhandled io events */
3963 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3964 {
3965 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3966 vcl_handle_mq_event (wrk, e);
3967 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3968 }
3969 vec_reset_length (wrk->unhandled_evts_vector);
3970
Dave Wallace048b1d62018-01-03 22:24:41 -05003971 for (i = 0; i < n_sids; i++)
3972 {
Florin Coras7baeb712019-01-04 17:05:43 -08003973 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003974 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003975 {
3976 vp[i].revents = POLLHUP;
3977 num_ev++;
3978 continue;
3979 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003980
Florin Coras6917b942018-11-13 22:44:54 -08003981 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003982
3983 if (POLLIN & vp[i].events)
3984 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003985 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003986 if (rv > 0)
3987 {
Florin Coras6917b942018-11-13 22:44:54 -08003988 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003989 num_ev++;
3990 }
3991 else if (rv < 0)
3992 {
3993 switch (rv)
3994 {
3995 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003996 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003997 break;
3998
3999 default:
Florin Coras6917b942018-11-13 22:44:54 -08004000 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004001 break;
4002 }
4003 num_ev++;
4004 }
4005 }
4006
4007 if (POLLOUT & vp[i].events)
4008 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004009 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004010 if (rv > 0)
4011 {
Florin Coras6917b942018-11-13 22:44:54 -08004012 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004013 num_ev++;
4014 }
4015 else if (rv < 0)
4016 {
4017 switch (rv)
4018 {
4019 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004020 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004021 break;
4022
4023 default:
Florin Coras6917b942018-11-13 22:44:54 -08004024 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004025 break;
4026 }
4027 num_ev++;
4028 }
4029 }
4030
Dave Wallace7e607a72018-06-18 18:41:32 -04004031 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004032 {
Florin Coras6917b942018-11-13 22:44:54 -08004033 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004034 num_ev++;
4035 }
4036 }
4037 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004038 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004039 }
4040 while ((num_ev == 0) && keep_trying);
4041
Dave Wallace048b1d62018-01-03 22:24:41 -05004042 return num_ev;
4043}
4044
Florin Coras99368312018-08-02 10:45:44 -07004045int
4046vppcom_mq_epoll_fd (void)
4047{
Florin Coras134a9962018-08-28 11:32:04 -07004048 vcl_worker_t *wrk = vcl_worker_get_current ();
4049 return wrk->mqs_epfd;
4050}
4051
4052int
Florin Coras30e79c22019-01-02 19:31:22 -08004053vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004054{
4055 return session_handle & 0xFFFFFF;
4056}
4057
4058int
Florin Coras30e79c22019-01-02 19:31:22 -08004059vppcom_session_worker (vcl_session_handle_t session_handle)
4060{
4061 return session_handle >> 24;
4062}
4063
4064int
Florin Coras134a9962018-08-28 11:32:04 -07004065vppcom_worker_register (void)
4066{
Florin Coras47c40e22018-11-26 17:01:36 -08004067 if (!vcl_worker_alloc_and_init ())
4068 return VPPCOM_EEXIST;
4069
Florin Coras47c40e22018-11-26 17:01:36 -08004070 if (vcl_worker_register_with_vpp ())
4071 return VPPCOM_EEXIST;
4072
4073 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004074}
4075
Florin Coras369db832019-07-08 12:34:45 -07004076void
4077vppcom_worker_unregister (void)
4078{
4079 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4080 vcl_set_worker_index (~0);
4081}
4082
Pivo6017ff02020-05-04 17:57:33 +02004083void
4084vppcom_worker_index_set (int index)
4085{
4086 vcl_set_worker_index (index);
4087}
4088
Florin Corasdfe4cf42018-11-28 22:13:45 -08004089int
4090vppcom_worker_index (void)
4091{
4092 return vcl_get_worker_index ();
4093}
4094
Florin Corase0982e52019-01-25 13:19:56 -08004095int
4096vppcom_worker_mqs_epfd (void)
4097{
4098 vcl_worker_t *wrk = vcl_worker_get_current ();
4099 if (!vcm->cfg.use_mq_eventfd)
4100 return -1;
4101 return wrk->mqs_epfd;
4102}
4103
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004104int
4105vppcom_session_is_connectable_listener (uint32_t session_handle)
4106{
4107 vcl_session_t *session;
4108 vcl_worker_t *wrk = vcl_worker_get_current ();
4109 session = vcl_session_get_w_handle (wrk, session_handle);
4110 if (!session)
4111 return VPPCOM_EBADFD;
4112 return vcl_session_is_connectable_listener (wrk, session);
4113}
4114
4115int
4116vppcom_session_listener (uint32_t session_handle)
4117{
4118 vcl_worker_t *wrk = vcl_worker_get_current ();
4119 vcl_session_t *listen_session, *session;
4120 session = vcl_session_get_w_handle (wrk, session_handle);
4121 if (!session)
4122 return VPPCOM_EBADFD;
4123 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4124 return VPPCOM_EBADFD;
4125 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4126 if (!listen_session)
4127 return VPPCOM_EBADFD;
4128 return vcl_session_handle (listen_session);
4129}
4130
4131int
4132vppcom_session_n_accepted (uint32_t session_handle)
4133{
4134 vcl_worker_t *wrk = vcl_worker_get_current ();
4135 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4136 if (!session)
4137 return VPPCOM_EBADFD;
4138 return session->n_accepted_sessions;
4139}
4140
Florin Coras66ec4672020-06-15 07:59:40 -07004141const char *
4142vppcom_proto_str (vppcom_proto_t proto)
4143{
4144 char const *proto_str;
4145
4146 switch (proto)
4147 {
4148 case VPPCOM_PROTO_TCP:
4149 proto_str = "TCP";
4150 break;
4151 case VPPCOM_PROTO_UDP:
4152 proto_str = "UDP";
4153 break;
4154 case VPPCOM_PROTO_TLS:
4155 proto_str = "TLS";
4156 break;
4157 case VPPCOM_PROTO_QUIC:
4158 proto_str = "QUIC";
4159 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004160 case VPPCOM_PROTO_DTLS:
4161 proto_str = "DTLS";
4162 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004163 case VPPCOM_PROTO_SRTP:
4164 proto_str = "SRTP";
4165 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004166 default:
4167 proto_str = "UNKNOWN";
4168 break;
4169 }
4170 return proto_str;
4171}
4172
4173const char *
4174vppcom_retval_str (int retval)
4175{
4176 char const *st;
4177
4178 switch (retval)
4179 {
4180 case VPPCOM_OK:
4181 st = "VPPCOM_OK";
4182 break;
4183
4184 case VPPCOM_EAGAIN:
4185 st = "VPPCOM_EAGAIN";
4186 break;
4187
4188 case VPPCOM_EFAULT:
4189 st = "VPPCOM_EFAULT";
4190 break;
4191
4192 case VPPCOM_ENOMEM:
4193 st = "VPPCOM_ENOMEM";
4194 break;
4195
4196 case VPPCOM_EINVAL:
4197 st = "VPPCOM_EINVAL";
4198 break;
4199
4200 case VPPCOM_EBADFD:
4201 st = "VPPCOM_EBADFD";
4202 break;
4203
4204 case VPPCOM_EAFNOSUPPORT:
4205 st = "VPPCOM_EAFNOSUPPORT";
4206 break;
4207
4208 case VPPCOM_ECONNABORTED:
4209 st = "VPPCOM_ECONNABORTED";
4210 break;
4211
4212 case VPPCOM_ECONNRESET:
4213 st = "VPPCOM_ECONNRESET";
4214 break;
4215
4216 case VPPCOM_ENOTCONN:
4217 st = "VPPCOM_ENOTCONN";
4218 break;
4219
4220 case VPPCOM_ECONNREFUSED:
4221 st = "VPPCOM_ECONNREFUSED";
4222 break;
4223
4224 case VPPCOM_ETIMEDOUT:
4225 st = "VPPCOM_ETIMEDOUT";
4226 break;
4227
4228 default:
4229 st = "UNKNOWN_STATE";
4230 break;
4231 }
4232
4233 return st;
4234}
4235
Florin Corasa5a9efd2021-01-05 17:03:29 -08004236int
4237vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4238{
4239 if (vcm->cfg.vpp_app_socket_api)
4240 {
4241 clib_warning ("not supported");
4242 return VPPCOM_EINVAL;
4243 }
4244 return vcl_bapi_add_cert_key_pair (ckpair);
4245}
4246
4247int
4248vppcom_del_cert_key_pair (uint32_t ckpair_index)
4249{
4250 if (vcm->cfg.vpp_app_socket_api)
4251 {
4252 clib_warning ("not supported");
4253 return VPPCOM_EINVAL;
4254 }
4255 return vcl_bapi_del_cert_key_pair (ckpair_index);
4256}
4257
Dave Wallacee22aa742017-10-20 12:30:38 -04004258/*
4259 * fd.io coding-style-patch-verification: ON
4260 *
4261 * Local Variables:
4262 * eval: (c-set-style "gnu")
4263 * End:
4264 */