blob: 0713a7b2d333842e20b4bb97419a37043817ace4 [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 Coras54693d22018-07-17 10:46:29 -0700474 session->transport.lcl_port = listen_session->transport.lcl_port;
475 session->transport.lcl_ip = listen_session->transport.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
810vppcom_session_shutdown (uint32_t session_handle)
811{
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
833 if (PREDICT_TRUE (state == VCL_STATE_READY))
834 {
835 VDBG (1, "session %u [0x%llx]: sending shutdown...",
836 session->session_index, vpp_handle);
837
838 vcl_send_session_shutdown (wrk, session);
839 session->flags |= VCL_SESSION_F_SHUTDOWN;
840 }
841
842 return VPPCOM_OK;
843}
844
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700845static int
846vppcom_session_disconnect (u32 session_handle)
847{
848 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700849 vcl_session_t *session, *listen_session;
850 vcl_session_state_t state;
851 u64 vpp_handle;
852
853 session = vcl_session_get_w_handle (wrk, session_handle);
854 if (!session)
855 return VPPCOM_EBADFD;
856
857 vpp_handle = session->vpp_handle;
858 state = session->session_state;
859
860 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
861 vpp_handle, state, vppcom_session_state_str (state));
862
863 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
864 {
865 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
866 return VPPCOM_EBADFD;
867 }
868
869 if (state == VCL_STATE_VPP_CLOSING)
870 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800871 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700872 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
873 session->session_index, vpp_handle);
874 }
875 else
876 {
877 /* Session doesn't have an event queue yet. Probably a non-blocking
878 * connect. Wait for the reply */
879 if (PREDICT_FALSE (!session->vpp_evt_q))
880 return VPPCOM_OK;
881
882 VDBG (1, "session %u [0x%llx]: sending disconnect...",
883 session->session_index, vpp_handle);
884 vcl_send_session_disconnect (wrk, session);
885 }
886
887 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
888 {
889 listen_session = vcl_session_get (wrk, session->listener_index);
890 listen_session->n_accepted_sessions--;
891 }
892
893 return VPPCOM_OK;
894}
895
Florin Coras30e79c22019-01-02 19:31:22 -0800896static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700897vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
898{
899 session_cleanup_msg_t *msg;
900 vcl_session_t *session;
901
902 msg = (session_cleanup_msg_t *) data;
903 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
904 if (!session)
905 {
906 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
907 return;
908 }
909
Florin Coras36d49392020-04-24 23:00:11 +0000910 if (msg->type == SESSION_CLEANUP_TRANSPORT)
911 {
912 /* Transport was cleaned up before we confirmed close. Probably the
913 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700914 * Confirm close to make sure everything is cleaned up.
915 * Move to undetermined state to ensure that the session is not
916 * removed before both vpp and the app cleanup.
917 * - If the app closes first, the session is moved to CLOSED state
918 * and the session cleanup notification from vpp removes the
919 * session.
920 * - If vpp cleans up the session first, the session is moved to
921 * DETACHED state lower and subsequently the close from the app
922 * frees the session
923 */
924 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700925 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700926 vppcom_session_disconnect (vcl_session_handle (session));
927 session->session_state = VCL_STATE_UPDATED;
928 }
929 else if (session->session_state == VCL_STATE_DISCONNECT)
930 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800931 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700932 session->session_state = VCL_STATE_UPDATED;
933 }
Florin Coras36d49392020-04-24 23:00:11 +0000934 return;
935 }
936
Florin Coras9ace36d2019-10-28 13:14:17 -0700937 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000938 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700939 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000940 {
941 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700942 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000943 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
944 return;
945 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700946 vcl_session_free (wrk, session);
947}
948
949static void
Florin Coras30e79c22019-01-02 19:31:22 -0800950vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
951{
952 session_req_worker_update_msg_t *msg;
953 vcl_session_t *s;
954
955 msg = (session_req_worker_update_msg_t *) data;
956 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
957 if (!s)
958 return;
959
960 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
961}
962
963static void
964vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
965{
966 session_worker_update_reply_msg_t *msg;
967 vcl_session_t *s;
968
969 msg = (session_worker_update_reply_msg_t *) data;
970 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
971 if (!s)
972 {
973 VDBG (0, "unknown handle 0x%llx", msg->handle);
974 return;
975 }
Florin Coras30e79c22019-01-02 19:31:22 -0800976
Florin Coras5f45e012019-01-23 09:21:30 -0800977 if (s->rx_fifo)
978 {
Florin Corasc547e912020-12-08 17:50:45 -0800979 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800980 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800981 {
982 VDBG (0, "failed to attach fifos for %u", s->session_index);
983 return;
984 }
Florin Coras5f45e012019-01-23 09:21:30 -0800985 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700986 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800987
Florin Coras30e79c22019-01-02 19:31:22 -0800988 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
989 s->vpp_handle, wrk->wrk_index);
990}
991
Florin Coras935ce752020-09-08 22:43:47 -0700992static int
993vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
994{
995
996 if (vcm->cfg.vpp_app_socket_api)
997 return vcl_sapi_recv_fds (wrk, fds, n_fds);
998
999 return vcl_bapi_recv_fds (wrk, fds, n_fds);
1000}
1001
Florin Corasc4c4cf52019-08-24 18:17:34 -07001002static void
1003vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
1004{
1005 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
1006 session_app_add_segment_msg_t *msg;
1007 u64 segment_handle;
1008 int fd = -1;
1009
1010 msg = (session_app_add_segment_msg_t *) data;
1011
1012 if (msg->fd_flags)
1013 {
Florin Coras935ce752020-09-08 22:43:47 -07001014 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -07001015 seg_type = SSVM_SEGMENT_MEMFD;
1016 }
1017
1018 segment_handle = msg->segment_handle;
1019 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
1020 {
1021 clib_warning ("invalid segment handle");
1022 return;
1023 }
1024
1025 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
1026 seg_type, fd))
1027 {
1028 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
1029 return;
1030 }
1031
1032 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
1033 msg->segment_size);
1034}
1035
1036static void
1037vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
1038{
1039 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
1040 vcl_segment_detach (msg->segment_handle);
1041 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
1042}
1043
Florin Coras40c07ce2020-07-16 20:46:17 -07001044static void
1045vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
1046{
1047 if (!vcm->wrk_rpc_fn)
1048 return;
1049
hanlina3a48962020-07-13 11:09:15 +08001050 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -07001051}
1052
Florin Coras04ae8272021-04-12 19:55:37 -07001053static void
1054vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
1055{
1056 session_transport_attr_reply_msg_t *mp;
1057
1058 if (!wrk->session_attr_op)
1059 return;
1060
1061 mp = (session_transport_attr_reply_msg_t *) data;
1062
1063 wrk->session_attr_op_rv = mp->retval;
1064 wrk->session_attr_op = 0;
1065 wrk->session_attr_rv = mp->attr;
1066}
1067
Florin Coras86f04502018-09-12 16:08:01 -07001068static int
1069vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001070{
Florin Coras54693d22018-07-17 10:46:29 -07001071 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001072 session_connected_msg_t *connected_msg;
1073 session_reset_msg_t *reset_msg;
1074 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001075 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001076 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001077
1078 switch (e->event_type)
1079 {
Florin Coras653e43f2019-03-04 10:56:23 -08001080 case SESSION_IO_EVT_RX:
1081 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001082 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001083 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001084 break;
Florin Coras86f04502018-09-12 16:08:01 -07001085 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001086 break;
Florin Corasb242d312020-10-26 15:35:40 -07001087 case SESSION_CTRL_EVT_BOUND:
1088 /* We can only wait for only one listen so not postponed */
1089 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1090 break;
Florin Coras54693d22018-07-17 10:46:29 -07001091 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001092 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1093 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1094 {
1095 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1096 *ecpy = *e;
1097 ecpy->postponed = 1;
1098 ecpy->session_index = s->session_index;
1099 }
Florin Coras54693d22018-07-17 10:46:29 -07001100 break;
1101 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001102 connected_msg = (session_connected_msg_t *) e->data;
1103 sid = vcl_session_connected_handler (wrk, connected_msg);
1104 if (!(s = vcl_session_get (wrk, sid)))
1105 break;
1106 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1107 {
1108 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1109 *ecpy = *e;
1110 ecpy->postponed = 1;
1111 ecpy->session_index = s->session_index;
1112 }
Florin Coras54693d22018-07-17 10:46:29 -07001113 break;
1114 case SESSION_CTRL_EVT_DISCONNECTED:
1115 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001116 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1117 break;
1118 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1119 {
1120 vec_add1 (wrk->unhandled_evts_vector, *e);
1121 break;
1122 }
1123 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001124 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001125 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1126 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001127 break;
1128 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001129 reset_msg = (session_reset_msg_t *) e->data;
1130 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1131 break;
1132 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1133 {
1134 vec_add1 (wrk->unhandled_evts_vector, *e);
1135 break;
1136 }
Florin Coras134a9962018-08-28 11:32:04 -07001137 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001138 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001139 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1140 vcl_session_unlisten_reply_handler (wrk, e->data);
1141 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001142 case SESSION_CTRL_EVT_MIGRATED:
1143 vcl_session_migrated_handler (wrk, e->data);
1144 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001145 case SESSION_CTRL_EVT_CLEANUP:
1146 vcl_session_cleanup_handler (wrk, e->data);
1147 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001148 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1149 vcl_session_req_worker_update_handler (wrk, e->data);
1150 break;
1151 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1152 vcl_session_worker_update_reply_handler (wrk, e->data);
1153 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001154 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1155 vcl_session_app_add_segment_handler (wrk, e->data);
1156 break;
1157 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1158 vcl_session_app_del_segment_handler (wrk, e->data);
1159 break;
hanlina3a48962020-07-13 11:09:15 +08001160 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001161 vcl_worker_rpc_handler (wrk, e->data);
1162 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001163 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1164 vcl_session_transport_attr_reply_handler (wrk, e->data);
1165 break;
Florin Coras54693d22018-07-17 10:46:29 -07001166 default:
1167 clib_warning ("unhandled %u", e->event_type);
1168 }
1169 return VPPCOM_OK;
1170}
1171
Florin Coras30e79c22019-01-02 19:31:22 -08001172static int
Florin Coras697faea2018-06-27 17:10:49 -07001173vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001174 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001175 f64 wait_for_time)
1176{
Florin Coras134a9962018-08-28 11:32:04 -07001177 vcl_worker_t *wrk = vcl_worker_get_current ();
1178 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001179 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001180 svm_msg_q_msg_t msg;
1181 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001182
Florin Coras697faea2018-06-27 17:10:49 -07001183 do
Dave Wallace543852a2017-08-03 02:11:34 -04001184 {
Florin Coras134a9962018-08-28 11:32:04 -07001185 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001186 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001187 {
Florin Coras070453d2018-08-24 17:04:27 -07001188 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001189 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001190 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001191 {
Florin Coras697faea2018-06-27 17:10:49 -07001192 return VPPCOM_OK;
1193 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001194 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001195 {
Florin Coras697faea2018-06-27 17:10:49 -07001196 return VPPCOM_ECONNREFUSED;
1197 }
Florin Coras54693d22018-07-17 10:46:29 -07001198
Florin Coras134a9962018-08-28 11:32:04 -07001199 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001200 {
1201 usleep (100);
1202 continue;
1203 }
Florin Coras134a9962018-08-28 11:32:04 -07001204 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001205 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001206 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001207 }
Florin Coras134a9962018-08-28 11:32:04 -07001208 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001209
Florin Coras05ecfcc2018-12-12 18:19:39 -08001210 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001211 vppcom_session_state_str (state));
1212 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001213
Florin Coras697faea2018-06-27 17:10:49 -07001214 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001215}
1216
Florin Coras30e79c22019-01-02 19:31:22 -08001217static void
1218vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1219{
Florin Coras288eaab2019-02-03 15:26:14 -08001220 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001221 vcl_session_t *s;
1222 u32 *sip;
1223
1224 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1225 return;
1226
1227 vec_foreach (sip, wrk->pending_session_wrk_updates)
1228 {
1229 s = vcl_session_get (wrk, *sip);
1230 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1231 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001232 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1233 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001234 s->session_state = state;
1235 }
1236 vec_reset_length (wrk->pending_session_wrk_updates);
1237}
1238
Florin Corasf9240dc2019-01-15 08:03:17 -08001239void
Florin Coras5398dfb2021-01-25 20:31:27 -08001240vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001241{
Florin Coras30e79c22019-01-02 19:31:22 -08001242 svm_msg_q_msg_t *msg;
1243 session_event_t *e;
1244 svm_msg_q_t *mq;
1245 int i;
1246
1247 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001248 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001249
1250 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1251 {
1252 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1253 e = svm_msg_q_msg_data (mq, msg);
1254 vcl_handle_mq_event (wrk, e);
1255 svm_msg_q_free_msg (mq, msg);
1256 }
1257 vec_reset_length (wrk->mq_msg_vector);
1258 vcl_handle_pending_wrk_updates (wrk);
1259}
1260
Florin Coras5398dfb2021-01-25 20:31:27 -08001261void
1262vcl_flush_mq_events (void)
1263{
1264 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1265}
1266
Florin Coras697faea2018-06-27 17:10:49 -07001267static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001268vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001269{
Florin Coras134a9962018-08-28 11:32:04 -07001270 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001271 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001272 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001273 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001274
Florin Corasab2f6db2018-08-31 14:31:41 -07001275 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001276 if (!session)
1277 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001278
Florin Corasaaff5ee2019-07-08 13:00:00 -07001279 /* Flush pending accept events, if any */
1280 while (clib_fifo_elts (session->accept_evts_fifo))
1281 {
1282 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1283 accepted_msg = &evt->accepted_msg;
1284 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1285 vcl_send_session_accepted_reply (session->vpp_evt_q,
1286 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001287 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001288 }
1289 clib_fifo_free (session->accept_evts_fifo);
1290
Florin Coras458089b2019-08-21 16:20:44 -07001291 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001292
Florin Coras5e062572019-03-14 19:07:51 -07001293 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001294 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001295 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001296
1297 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001298 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001299
Florin Coras070453d2018-08-24 17:04:27 -07001300 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001301}
1302
Florin Coras940f78f2018-11-30 12:11:20 -08001303/**
1304 * Handle app exit
1305 *
1306 * Notify vpp of the disconnect and mark the worker as free. If we're the
1307 * last worker, do a full cleanup otherwise, since we're probably a forked
1308 * child, avoid syscalls as much as possible. We might've lost privileges.
1309 */
1310void
1311vppcom_app_exit (void)
1312{
1313 if (!pool_elts (vcm->workers))
1314 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001315 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1316 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001317 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001318}
1319
Florin Coras935ce752020-09-08 22:43:47 -07001320static int
1321vcl_api_attach (void)
1322{
1323 if (vcm->cfg.vpp_app_socket_api)
1324 return vcl_sapi_attach ();
1325
1326 return vcl_bapi_attach ();
1327}
1328
1329static void
1330vcl_api_detach (vcl_worker_t * wrk)
1331{
1332 vcl_send_app_detach (wrk);
1333
1334 if (vcm->cfg.vpp_app_socket_api)
1335 return vcl_sapi_detach (wrk);
1336
1337 return vcl_bapi_disconnect_from_vpp ();
1338}
1339
Dave Wallace543852a2017-08-03 02:11:34 -04001340/*
1341 * VPPCOM Public API functions
1342 */
1343int
Florin Coras66ec4672020-06-15 07:59:40 -07001344vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001345{
Dave Wallace543852a2017-08-03 02:11:34 -04001346 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001347 int rv;
1348
Florin Coras47c40e22018-11-26 17:01:36 -08001349 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001350 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001351 VDBG (1, "already initialized");
1352 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001353 }
1354
Florin Coras47c40e22018-11-26 17:01:36 -08001355 vcm->is_init = 1;
1356 vppcom_cfg (&vcm->cfg);
1357 vcl_cfg = &vcm->cfg;
1358
1359 vcm->main_cpu = pthread_self ();
1360 vcm->main_pid = getpid ();
1361 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001362 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1363 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001364 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1365 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001366 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001367 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001368 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001369
1370 /* Allocate default worker */
1371 vcl_worker_alloc_and_init ();
1372
Florin Coras935ce752020-09-08 22:43:47 -07001373 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001374 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001375
1376 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001377 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001378
1379 return VPPCOM_OK;
1380}
1381
1382void
1383vppcom_app_destroy (void)
1384{
Florin Corasdc0ded72020-04-30 02:59:55 +00001385 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001386 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001387
Florin Coras940f78f2018-11-30 12:11:20 -08001388 if (!pool_elts (vcm->workers))
1389 return;
1390
Florin Coras0d427d82018-06-27 03:24:07 -07001391 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001392
Florin Corasdc0ded72020-04-30 02:59:55 +00001393 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001394
Florin Corasce815de2020-04-16 18:47:27 +00001395 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001396 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001397 if (current_wrk != wrk)
1398 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001399 }
Florin Corasce815de2020-04-16 18:47:27 +00001400 /* *INDENT-ON* */
1401
Florin Coras935ce752020-09-08 22:43:47 -07001402 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001403 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1404
Florin Coras0d427d82018-06-27 03:24:07 -07001405 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001406
1407 /*
1408 * Free the heap and fix vcm
1409 */
1410 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001411 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001412
1413 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001414 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001415}
1416
1417int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001418vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001419{
Florin Coras134a9962018-08-28 11:32:04 -07001420 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001421 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001422
Florin Coras134a9962018-08-28 11:32:04 -07001423 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001424
Florin Coras7e12d942018-06-27 14:32:43 -07001425 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001426 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001427 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001428 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001429
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001430 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001431 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001432
Florin Coras7e12d942018-06-27 14:32:43 -07001433 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1434 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001435
Florin Coras5e062572019-03-14 19:07:51 -07001436 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001437
Florin Coras134a9962018-08-28 11:32:04 -07001438 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001439}
1440
1441int
Florin Corasc127d5a2020-10-14 16:35:58 -07001442vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001443 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001444{
Florin Coras134a9962018-08-28 11:32:04 -07001445 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001446
Florin Coras6c3b2182020-10-19 18:36:48 -07001447 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001448
Florin Coras6c3b2182020-10-19 18:36:48 -07001449 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001450 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001451 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001452 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001453 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001454 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001455 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001456 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001457 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1458 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001459 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001460 }
Florin Corase4306b62020-10-28 12:51:10 -07001461 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001462 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001463
Florin Coras6c3b2182020-10-19 18:36:48 -07001464 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001465 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001466 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001467 if (rv < 0)
1468 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001469 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1470 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001471 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001472
Florin Coras9ace36d2019-10-28 13:14:17 -07001473 if (!do_disconnect)
1474 {
1475 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001476 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001477 goto cleanup;
1478 }
1479
Florin Coras6c3b2182020-10-19 18:36:48 -07001480 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001481 {
1482 rv = vppcom_session_unbind (sh);
1483 if (PREDICT_FALSE (rv < 0))
1484 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001485 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001486 vppcom_retval_str (rv));
1487 return rv;
1488 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001489 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001490 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001491 {
1492 rv = vppcom_session_disconnect (sh);
1493 if (PREDICT_FALSE (rv < 0))
1494 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001495 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001496 rv, vppcom_retval_str (rv));
1497 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001498 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001499 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001500 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001501 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001502 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001503 {
1504 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001505 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001506 goto free_session;
1507 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001508
Florin Corasc127d5a2020-10-14 16:35:58 -07001509 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001510
Florin Coras9ace36d2019-10-28 13:14:17 -07001511 /* Session is removed only after vpp confirms the disconnect */
1512 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001513
Florin Corasf9240dc2019-01-15 08:03:17 -08001514cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001515 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001516free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001517 vcl_session_free (wrk, s);
1518 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001519
Dave Wallace543852a2017-08-03 02:11:34 -04001520 return rv;
1521}
1522
1523int
Florin Corasf9240dc2019-01-15 08:03:17 -08001524vppcom_session_close (uint32_t session_handle)
1525{
1526 vcl_worker_t *wrk = vcl_worker_get_current ();
1527 vcl_session_t *session;
1528
1529 session = vcl_session_get_w_handle (wrk, session_handle);
1530 if (!session)
1531 return VPPCOM_EBADFD;
1532 return vcl_session_cleanup (wrk, session, session_handle,
1533 1 /* do_disconnect */ );
1534}
1535
1536int
Florin Coras134a9962018-08-28 11:32:04 -07001537vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001538{
Florin Coras134a9962018-08-28 11:32:04 -07001539 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001540 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001541
1542 if (!ep || !ep->ip)
1543 return VPPCOM_EINVAL;
1544
Florin Coras134a9962018-08-28 11:32:04 -07001545 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001546 if (!session)
1547 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001548
Florin Coras6c3b2182020-10-19 18:36:48 -07001549 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001550 {
Florin Coras5e062572019-03-14 19:07:51 -07001551 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1552 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001553 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001554 }
1555
Florin Coras7e12d942018-06-27 14:32:43 -07001556 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001557 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001558 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1559 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001560 else
Dave Barach178cf492018-11-13 16:34:13 -05001561 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1562 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001563 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001564
Florin Coras5e062572019-03-14 19:07:51 -07001565 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1566 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001567 session->transport.is_ip4 ? "IPv4" : "IPv6",
1568 format_ip46_address, &session->transport.lcl_ip,
1569 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1570 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001571 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001572 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001573
1574 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001575 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001576
Florin Coras070453d2018-08-24 17:04:27 -07001577 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001578}
1579
1580int
Florin Coras134a9962018-08-28 11:32:04 -07001581vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001582{
Florin Coras134a9962018-08-28 11:32:04 -07001583 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001584 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001585 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001586 int rv;
1587
Florin Coras134a9962018-08-28 11:32:04 -07001588 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001589 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001590 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001591
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001592 if (q_len == 0 || q_len == ~0)
1593 q_len = vcm->cfg.listen_queue_size;
1594
Dave Wallaceee45d412017-11-24 21:44:06 -05001595 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001596 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001597 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001598 VDBG (0, "session %u [0x%llx]: already in listen state!",
1599 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001600 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001601 }
1602
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001603 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001604
Florin Coras070453d2018-08-24 17:04:27 -07001605 /*
1606 * Send listen request to vpp and wait for reply
1607 */
Florin Coras458089b2019-08-21 16:20:44 -07001608 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001609 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001610 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001611 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001612
Florin Coras070453d2018-08-24 17:04:27 -07001613 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001614 {
Florin Coras134a9962018-08-28 11:32:04 -07001615 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001616 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1617 listen_sh, listen_session->vpp_handle, rv,
1618 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001619 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001620 }
1621
Florin Coras070453d2018-08-24 17:04:27 -07001622 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001623}
1624
Florin Coras134a9962018-08-28 11:32:04 -07001625static int
Florin Coras5e062572019-03-14 19:07:51 -07001626validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001627{
Florin Coras6c3b2182020-10-19 18:36:48 -07001628 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001629 {
Florin Coras5e062572019-03-14 19:07:51 -07001630 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1631 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001632 return VPPCOM_EBADFD;
1633 }
1634
Florin Corasc127d5a2020-10-14 16:35:58 -07001635 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001636 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001637 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001638 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001639 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001640 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001641 return VPPCOM_EBADFD;
1642 }
1643 return VPPCOM_OK;
1644}
1645
1646int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001647vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1648{
1649 if (!strcmp (proto_str, "TCP"))
1650 *proto = VPPCOM_PROTO_TCP;
1651 else if (!strcmp (proto_str, "tcp"))
1652 *proto = VPPCOM_PROTO_TCP;
1653 else if (!strcmp (proto_str, "UDP"))
1654 *proto = VPPCOM_PROTO_UDP;
1655 else if (!strcmp (proto_str, "udp"))
1656 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001657 else if (!strcmp (proto_str, "TLS"))
1658 *proto = VPPCOM_PROTO_TLS;
1659 else if (!strcmp (proto_str, "tls"))
1660 *proto = VPPCOM_PROTO_TLS;
1661 else if (!strcmp (proto_str, "QUIC"))
1662 *proto = VPPCOM_PROTO_QUIC;
1663 else if (!strcmp (proto_str, "quic"))
1664 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001665 else if (!strcmp (proto_str, "DTLS"))
1666 *proto = VPPCOM_PROTO_DTLS;
1667 else if (!strcmp (proto_str, "dtls"))
1668 *proto = VPPCOM_PROTO_DTLS;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001669 else
1670 return 1;
1671 return 0;
1672}
1673
1674int
Florin Coras134a9962018-08-28 11:32:04 -07001675vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001676 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001677{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001678 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001679 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001680 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001681 vcl_session_t *listen_session = 0;
1682 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001683 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001684 u8 is_nonblocking;
1685 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001686
Florin Coras5398dfb2021-01-25 20:31:27 -08001687again:
1688
Florin Coras134a9962018-08-28 11:32:04 -07001689 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001690 if (!listen_session)
1691 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001692
Florin Coras134a9962018-08-28 11:32:04 -07001693 listen_session_index = listen_session->session_index;
1694 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001695 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001696
Florin Coras54693d22018-07-17 10:46:29 -07001697 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001698 {
Florin Coras54693d22018-07-17 10:46:29 -07001699 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001700 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001701 accepted_msg = evt->accepted_msg;
1702 goto handle;
1703 }
1704
Florin Corasac422d62020-10-19 20:51:36 -07001705 is_nonblocking = vcl_session_has_attr (listen_session,
1706 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001707 while (1)
1708 {
Carl Smith592a9092019-11-12 14:57:37 +13001709 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1710 return VPPCOM_EAGAIN;
1711
Florin Coras5398dfb2021-01-25 20:31:27 -08001712 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1713 vcl_worker_flush_mq_events (wrk);
1714 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001715 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001716
Florin Coras54693d22018-07-17 10:46:29 -07001717handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001718
Florin Coras00cca802019-06-06 09:38:44 -07001719 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1720 listen_session_index);
1721 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1722 return VPPCOM_ECONNABORTED;
1723
Florin Coras134a9962018-08-28 11:32:04 -07001724 listen_session = vcl_session_get (wrk, listen_session_index);
1725 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001726
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001727 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001728 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001729
Florin Coras5e062572019-03-14 19:07:51 -07001730 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1731 " flags %d, is_nonblocking %u", listen_session->session_index,
1732 listen_session->vpp_handle, client_session_index,
1733 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001734 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001735
Dave Wallace048b1d62018-01-03 22:24:41 -05001736 if (ep)
1737 {
Florin Coras7e12d942018-06-27 14:32:43 -07001738 ep->is_ip4 = client_session->transport.is_ip4;
1739 ep->port = client_session->transport.rmt_port;
1740 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001741 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1742 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001743 else
Dave Barach178cf492018-11-13 16:34:13 -05001744 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1745 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001746 }
Dave Wallace60caa062017-11-10 17:07:13 -05001747
Florin Coras05ecfcc2018-12-12 18:19:39 -08001748 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001749 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001750 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001751 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001752 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001753 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001754 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001755 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001756 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001757 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1758 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001759
Florin Coras3c7d4f92018-12-14 11:28:43 -08001760 /*
1761 * Session might have been closed already
1762 */
1763 if (accept_flags)
1764 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001765 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001766 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001767 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001768 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001769 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001770 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001771}
1772
1773int
Florin Coras134a9962018-08-28 11:32:04 -07001774vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001775{
Florin Coras134a9962018-08-28 11:32:04 -07001776 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001777 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001778 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001779 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001780
Florin Coras134a9962018-08-28 11:32:04 -07001781 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001782 if (!session)
1783 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001784 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001785
Florin Coras6c3b2182020-10-19 18:36:48 -07001786 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001787 {
Florin Coras5e062572019-03-14 19:07:51 -07001788 VDBG (0, "ERROR: cannot connect epoll session %u!",
1789 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001790 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001791 }
1792
Florin Corasc127d5a2020-10-14 16:35:58 -07001793 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001794 {
Florin Coras7baeb712019-01-04 17:05:43 -08001795 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001796 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001797 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001798 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001799 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001800 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001801 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001802 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001803 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001804 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001805 }
1806
Florin Coras0a1e1832020-03-29 18:54:04 +00001807 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001808 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001809 {
1810 if (session->session_type != VPPCOM_PROTO_UDP)
1811 return VPPCOM_EINVAL;
1812 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001813 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001814 }
1815
Florin Coras7e12d942018-06-27 14:32:43 -07001816 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001817 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001818 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001819 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001820 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001821
Florin Coras0a1e1832020-03-29 18:54:04 +00001822 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001823 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001824 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001825 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001826 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001827 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001828 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001829 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001830 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001831
Florin Coras458089b2019-08-21 16:20:44 -07001832 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001833
Florin Corasac422d62020-10-19 20:51:36 -07001834 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001835 {
fanyfa49d59d2020-10-13 17:07:16 +08001836 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001837 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001838 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001839 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001840 return VPPCOM_EINPROGRESS;
1841 }
Florin Coras57c88932019-08-29 12:03:17 -07001842
1843 /*
1844 * Wait for reply from vpp if blocking
1845 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001846 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001847 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001848
Florin Coras134a9962018-08-28 11:32:04 -07001849 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001850 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1851 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001852
Dave Wallace4878cbe2017-11-21 03:45:09 -05001853 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001854}
1855
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001856int
1857vppcom_session_stream_connect (uint32_t session_handle,
1858 uint32_t parent_session_handle)
1859{
1860 vcl_worker_t *wrk = vcl_worker_get_current ();
1861 vcl_session_t *session, *parent_session;
1862 u32 session_index, parent_session_index;
1863 int rv;
1864
1865 session = vcl_session_get_w_handle (wrk, session_handle);
1866 if (!session)
1867 return VPPCOM_EBADFD;
1868 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1869 if (!parent_session)
1870 return VPPCOM_EBADFD;
1871
1872 session_index = session->session_index;
1873 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001874 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001875 {
1876 VDBG (0, "ERROR: cannot connect epoll session %u!",
1877 session->session_index);
1878 return VPPCOM_EBADFD;
1879 }
1880
Florin Corasc127d5a2020-10-14 16:35:58 -07001881 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001882 {
1883 VDBG (0, "session handle %u [0x%llx]: session already "
1884 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1885 session_handle, session->vpp_handle,
1886 parent_session_handle, parent_session->vpp_handle,
1887 vppcom_proto_str (session->session_type), session->session_state,
1888 vppcom_session_state_str (session->session_state));
1889 return VPPCOM_OK;
1890 }
1891
1892 /* Connect to quic session specifics */
1893 session->transport.is_ip4 = parent_session->transport.is_ip4;
1894 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1895 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001896 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001897
1898 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1899 session_handle, parent_session_handle, parent_session->vpp_handle);
1900
1901 /*
1902 * Send connect request and wait for reply from vpp
1903 */
Florin Coras458089b2019-08-21 16:20:44 -07001904 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001905 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001906 vcm->cfg.session_timeout);
1907
1908 session->listener_index = parent_session_index;
1909 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001910 if (parent_session)
1911 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001912
1913 session = vcl_session_get (wrk, session_index);
1914 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1915 session->vpp_handle, rv ? "failed" : "succeeded");
1916
1917 return rv;
1918}
1919
Steven58f464e2017-10-25 12:33:12 -07001920static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001921vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001922 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001923{
Florin Coras134a9962018-08-28 11:32:04 -07001924 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001925 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001926 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001927 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001928 session_event_t *e;
1929 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001930 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001931
Florin Coras070453d2018-08-24 17:04:27 -07001932 if (PREDICT_FALSE (!buf))
1933 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001934
Florin Coras134a9962018-08-28 11:32:04 -07001935 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001936 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001937 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001938
Florin Coras6d0106e2019-01-29 20:11:58 -08001939 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001940 {
Florin Coras5e062572019-03-14 19:07:51 -07001941 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001942 s->session_index, s->vpp_handle, s->session_state,
1943 vppcom_session_state_str (s->session_state));
1944 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001945 }
1946
Florin Corasac422d62020-10-19 20:51:36 -07001947 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001948 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001949 mq = wrk->app_event_queue;
1950 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001951 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001952
Sirshak Das28aa5392019-02-05 01:33:33 -06001953 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001954 {
Florin Coras54693d22018-07-17 10:46:29 -07001955 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001956 {
Yu Pingb2955352019-12-04 06:49:04 +08001957 if (vcl_session_is_closing (s))
1958 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001959 if (is_ct)
1960 svm_fifo_unset_event (s->rx_fifo);
1961 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001962 return VPPCOM_EWOULDBLOCK;
1963 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001964 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001965 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001966 if (vcl_session_is_closing (s))
1967 return vcl_session_closing_error (s);
1968
Florin Corasd6894562020-10-28 00:37:15 -07001969 if (is_ct)
1970 svm_fifo_unset_event (s->rx_fifo);
1971 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001972
Florin Coras5398dfb2021-01-25 20:31:27 -08001973 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1974 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001975 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001976 }
Florin Coras54693d22018-07-17 10:46:29 -07001977
Florin Coras4a2c7942020-09-10 12:27:14 -07001978read_again:
1979
Florin Coras460dce62018-07-27 05:45:06 -07001980 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001981 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001982 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001983 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1984
1985 ASSERT (rv >= 0);
1986 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001987
Sirshak Das28aa5392019-02-05 01:33:33 -06001988 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001989 {
Florin Corasd6894562020-10-28 00:37:15 -07001990 if (is_ct)
1991 svm_fifo_unset_event (s->rx_fifo);
1992 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001993 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001994 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001995 {
Florin Corasc34118b2020-08-12 18:58:25 -07001996 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1997 e->event_type = SESSION_IO_EVT_RX;
1998 e->session_index = s->session_index;
1999 }
2000 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002001 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002002 {
2003 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002004 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002005 buf += rv;
2006 n -= rv;
2007 goto read_again;
2008 }
Florin Coras41c9e042018-09-11 00:10:41 -07002009
Florin Coras17ec5772020-08-12 20:46:29 -07002010 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002011 {
Florin Coras17ec5772020-08-12 20:46:29 -07002012 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002013 app_send_io_evt_to_vpp (s->vpp_evt_q,
2014 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002015 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002016 }
2017
Florin Coras5e062572019-03-14 19:07:51 -07002018 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2019 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002020
Florin Coras54693d22018-07-17 10:46:29 -07002021 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002022}
2023
Steven58f464e2017-10-25 12:33:12 -07002024int
Florin Coras134a9962018-08-28 11:32:04 -07002025vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002026{
Florin Coras134a9962018-08-28 11:32:04 -07002027 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002028}
2029
2030static int
Florin Coras134a9962018-08-28 11:32:04 -07002031vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002032{
Florin Coras134a9962018-08-28 11:32:04 -07002033 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002034}
2035
Florin Coras2cba8532018-09-11 16:33:36 -07002036int
2037vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002038 vppcom_data_segment_t * ds, uint32_t n_segments,
2039 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002040{
2041 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002042 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002043 vcl_session_t *s = 0;
2044 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002045 svm_msg_q_t *mq;
2046 u8 is_ct;
2047
2048 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002049 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002050 return VPPCOM_EBADFD;
2051
Florin Coras6d0106e2019-01-29 20:11:58 -08002052 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2053 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002054
Florin Corasac422d62020-10-19 20:51:36 -07002055 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002056 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002057 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002058 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002059 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002060
Sirshak Das28aa5392019-02-05 01:33:33 -06002061 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002062 {
2063 if (is_nonblocking)
2064 {
Florin Coras62877022020-10-28 21:22:04 -07002065 if (is_ct)
2066 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002067 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002068 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002069 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002070 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002071 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002072 if (vcl_session_is_closing (s))
2073 return vcl_session_closing_error (s);
2074
Florin Coras62877022020-10-28 21:22:04 -07002075 if (is_ct)
2076 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002077 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002078
Florin Coras5398dfb2021-01-25 20:31:27 -08002079 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2080 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002081 }
2082 }
2083
Florin Corasd1cc38d2020-10-11 11:05:04 -07002084 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2085 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2086 if (n_read < 0)
2087 return VPPCOM_EAGAIN;
2088
2089 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2090 {
Florin Coras62877022020-10-28 21:22:04 -07002091 if (is_ct)
2092 svm_fifo_unset_event (s->rx_fifo);
2093 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002094 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002095 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002096 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002097 {
2098 session_event_t *e;
2099 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2100 e->event_type = SESSION_IO_EVT_RX;
2101 e->session_index = s->session_index;
2102 }
2103 }
2104
2105 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002106 return n_read;
2107}
2108
2109void
Florin Corasd68faf82020-09-25 15:18:13 -07002110vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002111{
2112 vcl_worker_t *wrk = vcl_worker_get_current ();
2113 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002114 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002115
2116 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002117 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002118 return;
2119
Florin Coras62877022020-10-28 21:22:04 -07002120 is_ct = vcl_session_is_ct (s);
2121 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002122
2123 ASSERT (s->rx_bytes_pending < n_bytes);
2124 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002125}
2126
Florin Coras7a2abce2020-04-05 19:25:44 +00002127always_inline u8
2128vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002129{
Florin Coras7a2abce2020-04-05 19:25:44 +00002130 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2131 if (is_dgram)
2132 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2133 else
2134 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002135}
2136
2137always_inline int
2138vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2139 size_t n, u8 is_flush, u8 is_dgram)
2140{
Florin Coras6d0106e2019-01-29 20:11:58 -08002141 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002142 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002143 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002144 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002145 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002146
jiangxiaomingff31ac62019-11-21 23:34:56 +08002147 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002148 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002149
Florin Coras6c3b2182020-10-19 18:36:48 -07002150 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002151 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002152 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2153 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002154 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002155 }
2156
liuyacan534468e2021-05-09 03:50:40 +00002157 if (PREDICT_FALSE (!vcl_session_is_open (s) ||
2158 s->flags & VCL_SESSION_F_SHUTDOWN))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002159 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002160 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2161 s->session_index, s->vpp_handle, s->session_state,
2162 vppcom_session_state_str (s->session_state));
2163 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002164 }
2165
Florin Coras0e88e852018-09-17 22:09:02 -07002166 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002167 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002168 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002169
Florin Coras653e43f2019-03-04 10:56:23 -08002170 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002171 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002172 {
Florin Coras54693d22018-07-17 10:46:29 -07002173 if (is_nonblocking)
2174 {
Florin Coras070453d2018-08-24 17:04:27 -07002175 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002176 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002177 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002178 {
Florin Coras2d379d82019-06-28 12:45:12 -07002179 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002180 if (vcl_session_is_closing (s))
2181 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002182
Florin Coras5398dfb2021-01-25 20:31:27 -08002183 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2184 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002185 }
Dave Wallace543852a2017-08-03 02:11:34 -04002186 }
Dave Wallace543852a2017-08-03 02:11:34 -04002187
Florin Coras653e43f2019-03-04 10:56:23 -08002188 et = SESSION_IO_EVT_TX;
2189 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002190 et = SESSION_IO_EVT_TX_FLUSH;
2191
Florin Coras7a2abce2020-04-05 19:25:44 +00002192 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002193 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002194 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002195 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002196 else
2197 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002198 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002199
Florin Coras14ed6df2019-03-06 21:13:42 -08002200 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002201 app_send_io_evt_to_vpp (
2202 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002203
Florin Coras56230092020-09-02 20:52:58 -07002204 /* The underlying fifo segment can run out of memory */
2205 if (PREDICT_FALSE (n_write < 0))
2206 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002207
Florin Coras6d0106e2019-01-29 20:11:58 -08002208 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2209 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002210
Florin Coras54693d22018-07-17 10:46:29 -07002211 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002212}
2213
Florin Coras42ceddb2018-12-12 10:56:01 -08002214int
2215vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2216{
Florin Coras7a2abce2020-04-05 19:25:44 +00002217 vcl_worker_t *wrk = vcl_worker_get_current ();
2218 vcl_session_t *s;
2219
2220 s = vcl_session_get_w_handle (wrk, session_handle);
2221 if (PREDICT_FALSE (!s))
2222 return VPPCOM_EBADFD;
2223
2224 return vppcom_session_write_inline (wrk, s, buf, n,
2225 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002226}
2227
Florin Corasb0f662f2018-12-27 14:51:46 -08002228int
2229vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2230{
Florin Coras7a2abce2020-04-05 19:25:44 +00002231 vcl_worker_t *wrk = vcl_worker_get_current ();
2232 vcl_session_t *s;
2233
2234 s = vcl_session_get_w_handle (wrk, session_handle);
2235 if (PREDICT_FALSE (!s))
2236 return VPPCOM_EBADFD;
2237
2238 return vppcom_session_write_inline (wrk, s, buf, n,
2239 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002240}
2241
Florin Corasc0737e92019-03-04 14:19:39 -08002242#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002243if (PREDICT_FALSE (!_s->rx_fifo)) \
2244 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002245if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2246 { \
2247 if (!vcl_session_is_ct (_s)) \
2248 { \
2249 svm_fifo_unset_event (_s->rx_fifo); \
2250 if (svm_fifo_is_empty (_s->rx_fifo)) \
2251 break; \
2252 } \
2253 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2254 { \
Florin Coras2f630182020-08-13 00:17:51 -07002255 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002256 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2257 break; \
2258 } \
2259 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002260
Florin Coras86f04502018-09-12 16:08:01 -07002261static void
2262vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2263 unsigned long n_bits, unsigned long *read_map,
2264 unsigned long *write_map,
2265 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002266{
2267 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002268 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002269 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002270 u32 sid;
2271
2272 switch (e->event_type)
2273 {
Florin Corasc0737e92019-03-04 14:19:39 -08002274 case SESSION_IO_EVT_RX:
2275 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002276 s = vcl_session_get (wrk, sid);
2277 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002278 break;
Florin Corasb242d312020-10-26 15:35:40 -07002279 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002280 if (sid < n_bits && read_map)
2281 {
David Johnsond9818dd2018-12-14 14:53:41 -05002282 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002283 *bits_set += 1;
2284 }
2285 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002286 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002287 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002288 s = vcl_session_get (wrk, sid);
2289 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002290 break;
2291 if (sid < n_bits && write_map)
2292 {
David Johnsond9818dd2018-12-14 14:53:41 -05002293 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002294 *bits_set += 1;
2295 }
2296 break;
Florin Coras86f04502018-09-12 16:08:01 -07002297 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002298 if (!e->postponed)
2299 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2300 else
2301 s = vcl_session_get (wrk, e->session_index);
2302 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002303 break;
Florin Corasb242d312020-10-26 15:35:40 -07002304 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002305 if (sid < n_bits && read_map)
2306 {
David Johnsond9818dd2018-12-14 14:53:41 -05002307 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002308 *bits_set += 1;
2309 }
2310 break;
2311 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002312 if (!e->postponed)
2313 {
2314 connected_msg = (session_connected_msg_t *) e->data;
2315 sid = vcl_session_connected_handler (wrk, connected_msg);
2316 }
2317 else
2318 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002319 if (sid == VCL_INVALID_SESSION_INDEX)
2320 break;
2321 if (sid < n_bits && write_map)
2322 {
2323 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2324 *bits_set += 1;
2325 }
Florin Coras86f04502018-09-12 16:08:01 -07002326 break;
2327 case SESSION_CTRL_EVT_DISCONNECTED:
2328 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002329 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2330 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002331 break;
Florin Corasb242d312020-10-26 15:35:40 -07002332 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002333 if (sid < n_bits && except_map)
2334 {
David Johnsond9818dd2018-12-14 14:53:41 -05002335 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002336 *bits_set += 1;
2337 }
2338 break;
2339 case SESSION_CTRL_EVT_RESET:
2340 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2341 if (sid < n_bits && except_map)
2342 {
David Johnsond9818dd2018-12-14 14:53:41 -05002343 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002344 *bits_set += 1;
2345 }
2346 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002347 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2348 vcl_session_unlisten_reply_handler (wrk, e->data);
2349 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002350 case SESSION_CTRL_EVT_MIGRATED:
2351 vcl_session_migrated_handler (wrk, e->data);
2352 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002353 case SESSION_CTRL_EVT_CLEANUP:
2354 vcl_session_cleanup_handler (wrk, e->data);
2355 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002356 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2357 vcl_session_worker_update_reply_handler (wrk, e->data);
2358 break;
2359 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2360 vcl_session_req_worker_update_handler (wrk, e->data);
2361 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002362 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2363 vcl_session_app_add_segment_handler (wrk, e->data);
2364 break;
2365 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2366 vcl_session_app_del_segment_handler (wrk, e->data);
2367 break;
hanlina3a48962020-07-13 11:09:15 +08002368 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002369 vcl_worker_rpc_handler (wrk, e->data);
2370 break;
Florin Coras86f04502018-09-12 16:08:01 -07002371 default:
2372 clib_warning ("unhandled: %u", e->event_type);
2373 break;
2374 }
2375}
2376
2377static int
2378vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2379 unsigned long n_bits, unsigned long *read_map,
2380 unsigned long *write_map, unsigned long *except_map,
2381 double time_to_wait, u32 * bits_set)
2382{
Florin Coras99368312018-08-02 10:45:44 -07002383 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002384 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002385 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002386
Florin Coras54693d22018-07-17 10:46:29 -07002387 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002388 {
Florin Coras54693d22018-07-17 10:46:29 -07002389 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002390 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002391
Florin Coras54693d22018-07-17 10:46:29 -07002392 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002393 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002394 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002395 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002396 else
2397 {
2398 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002399 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002400 }
2401 }
Florin Corase003a1b2019-06-05 10:47:16 -07002402 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002403
Florin Coras134a9962018-08-28 11:32:04 -07002404 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002405 {
Florin Coras134a9962018-08-28 11:32:04 -07002406 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002407 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002408 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2409 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002410 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002411 }
Florin Coras134a9962018-08-28 11:32:04 -07002412 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002413 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002414 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002415}
2416
Florin Coras99368312018-08-02 10:45:44 -07002417static int
Florin Coras294afe22019-01-07 17:49:17 -08002418vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2419 vcl_si_set * read_map, vcl_si_set * write_map,
2420 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002421 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002422{
Florin Coras14ed6df2019-03-06 21:13:42 -08002423 double wait = 0, start = 0;
2424
2425 if (!*bits_set)
2426 {
2427 wait = time_to_wait;
2428 start = clib_time_now (&wrk->clib_time);
2429 }
2430
2431 do
2432 {
2433 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2434 write_map, except_map, wait, bits_set);
2435 if (*bits_set)
2436 return *bits_set;
2437 if (wait == -1)
2438 continue;
2439
2440 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2441 }
2442 while (wait > 0);
2443
2444 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002445}
2446
2447static int
Florin Coras294afe22019-01-07 17:49:17 -08002448vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2449 vcl_si_set * read_map, vcl_si_set * write_map,
2450 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002451 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002452{
2453 vcl_mq_evt_conn_t *mqc;
2454 int __clib_unused n_read;
2455 int n_mq_evts, i;
2456 u64 buf;
2457
Florin Coras134a9962018-08-28 11:32:04 -07002458 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2459 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2460 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002461 for (i = 0; i < n_mq_evts; i++)
2462 {
Florin Coras134a9962018-08-28 11:32:04 -07002463 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002464 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002465 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002466 except_map, 0, bits_set);
2467 }
2468
2469 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2470}
2471
Dave Wallace543852a2017-08-03 02:11:34 -04002472int
Florin Coras294afe22019-01-07 17:49:17 -08002473vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2474 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002475{
Florin Coras54693d22018-07-17 10:46:29 -07002476 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002477 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002478 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002479 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002480
Dave Wallace7876d392017-10-19 03:53:57 -04002481 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002482 {
Florin Coras134a9962018-08-28 11:32:04 -07002483 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002484 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002485 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2486 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002487 }
Dave Wallace7876d392017-10-19 03:53:57 -04002488 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002489 {
Florin Coras134a9962018-08-28 11:32:04 -07002490 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002491 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002492 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2493 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002494 }
Dave Wallace7876d392017-10-19 03:53:57 -04002495 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002496 {
Florin Coras134a9962018-08-28 11:32:04 -07002497 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002498 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002499 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2500 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002501 }
2502
Florin Coras54693d22018-07-17 10:46:29 -07002503 if (!n_bits)
2504 return 0;
2505
2506 if (!write_map)
2507 goto check_rd;
2508
Florin Coras096a1d52021-01-27 15:33:51 -08002509 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2510 {
2511 if (!(s = vcl_session_get (wrk, sid)))
2512 {
2513 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2514 bits_set++;
2515 continue;
2516 }
Florin Coras54693d22018-07-17 10:46:29 -07002517
Florin Coras096a1d52021-01-27 15:33:51 -08002518 if (vcl_session_write_ready (s))
2519 {
2520 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2521 bits_set++;
2522 }
2523 else
2524 {
2525 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2526 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2527 }
2528 }
Florin Coras54693d22018-07-17 10:46:29 -07002529
2530check_rd:
2531 if (!read_map)
2532 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002533
Florin Coras096a1d52021-01-27 15:33:51 -08002534 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2535 {
2536 if (!(s = vcl_session_get (wrk, sid)))
2537 {
2538 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2539 bits_set++;
2540 continue;
2541 }
Florin Coras54693d22018-07-17 10:46:29 -07002542
Florin Coras096a1d52021-01-27 15:33:51 -08002543 if (vcl_session_read_ready (s))
2544 {
2545 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2546 bits_set++;
2547 }
2548 }
Florin Coras54693d22018-07-17 10:46:29 -07002549
2550check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002551
Florin Coras86f04502018-09-12 16:08:01 -07002552 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2553 {
2554 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2555 read_map, write_map, except_map, &bits_set);
2556 }
2557 vec_reset_length (wrk->unhandled_evts_vector);
2558
Florin Coras99368312018-08-02 10:45:44 -07002559 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002560 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002561 time_to_wait, &bits_set);
2562 else
Florin Coras134a9962018-08-28 11:32:04 -07002563 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002564 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002565
Dave Wallace543852a2017-08-03 02:11:34 -04002566 return (bits_set);
2567}
2568
Dave Wallacef7f809c2017-10-03 01:48:42 -04002569static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002570vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002571{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002572 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002573 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002574 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002575
Florin Corasc0737e92019-03-04 14:19:39 -08002576 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002577 return;
2578
Florin Coras6c3b2182020-10-19 18:36:48 -07002579 s = vcl_session_get_w_handle (wrk, vep_handle);
2580 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002581 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002582 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002583 goto done;
2584 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002585 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002586 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002587 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002588 goto done;
2589 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002590 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002591 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002592 "{\n"
2593 " is_vep = %u\n"
2594 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002595 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002596 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2597 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002598
Florin Coras7e5e62b2019-07-30 14:08:23 -07002599 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002600 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002601 s = vcl_session_get_w_handle (wrk, sh);
2602 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002603 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002604 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002605 goto done;
2606 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002607 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002608 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002609 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002610 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002611 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002612 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002613 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002614 goto done;
2615 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002616 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002617 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2618 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002619 sh, s->vep.vep_sh, vep_handle);
2620 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002621 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002622 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002623 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002624 " next_sh = 0x%x (%u)\n"
2625 " prev_sh = 0x%x (%u)\n"
2626 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002627 " ev.events = 0x%x\n"
2628 " ev.data.u64 = 0x%llx\n"
2629 " et_mask = 0x%x\n"
2630 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002631 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002632 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2633 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002634 }
2635 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002636
2637done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002638 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002639}
2640
2641int
2642vppcom_epoll_create (void)
2643{
Florin Coras134a9962018-08-28 11:32:04 -07002644 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002645 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002646
Florin Coras134a9962018-08-28 11:32:04 -07002647 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002648
Florin Coras6c3b2182020-10-19 18:36:48 -07002649 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002650 vep_session->vep.vep_sh = ~0;
2651 vep_session->vep.next_sh = ~0;
2652 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002653 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002654
Florin Corasa7a1a222018-12-30 17:11:31 -08002655 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2656 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002657
Florin Corasab2f6db2018-08-31 14:31:41 -07002658 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002659}
2660
2661int
Florin Coras134a9962018-08-28 11:32:04 -07002662vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002663 struct epoll_event *event)
2664{
Florin Coras134a9962018-08-28 11:32:04 -07002665 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002666 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002667 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002668 vcl_session_t *s;
2669 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002670
Florin Coras134a9962018-08-28 11:32:04 -07002671 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002672 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002673 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002674 return VPPCOM_EINVAL;
2675 }
2676
Florin Coras134a9962018-08-28 11:32:04 -07002677 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002678 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002679 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002680 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002681 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002683 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002684 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002685 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002686 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687 }
2688
Florin Coras134a9962018-08-28 11:32:04 -07002689 ASSERT (vep_session->vep.vep_sh == ~0);
2690 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691
Florin Coras17ec5772020-08-12 20:46:29 -07002692 s = vcl_session_get_w_handle (wrk, session_handle);
2693 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002694 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002695 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002696 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002698 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002699 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002700 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002701 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002702 }
2703
2704 switch (op)
2705 {
2706 case EPOLL_CTL_ADD:
2707 if (PREDICT_FALSE (!event))
2708 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002709 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002710 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002711 }
Florin Coras134a9962018-08-28 11:32:04 -07002712 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002713 {
Florin Coras7e12d942018-06-27 14:32:43 -07002714 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002715 next_session = vcl_session_get_w_handle (wrk,
2716 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002717 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002718 {
Florin Coras5e062572019-03-14 19:07:51 -07002719 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002720 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002721 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002722 }
Florin Coras134a9962018-08-28 11:32:04 -07002723 ASSERT (next_session->vep.prev_sh == vep_handle);
2724 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002725 }
Florin Coras17ec5772020-08-12 20:46:29 -07002726 s->vep.next_sh = vep_session->vep.next_sh;
2727 s->vep.prev_sh = vep_handle;
2728 s->vep.vep_sh = vep_handle;
2729 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2730 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002731 s->flags &= ~VCL_SESSION_F_IS_VEP;
2732 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002733 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002734
Florin Coras17ec5772020-08-12 20:46:29 -07002735 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2736 if (txf && (event->events & EPOLLOUT))
2737 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002738
Florin Coras6e3c1f82020-01-15 01:30:46 +00002739 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002740 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002741 {
2742 session_event_t e = { 0 };
2743 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002744 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002745 vec_add1 (wrk->unhandled_evts_vector, e);
2746 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002747 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002748 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002749 {
2750 session_event_t e = { 0 };
2751 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002752 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002753 vec_add1 (wrk->unhandled_evts_vector, e);
2754 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002755 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2756 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002757 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002758 break;
2759
2760 case EPOLL_CTL_MOD:
2761 if (PREDICT_FALSE (!event))
2762 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002763 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002764 rv = VPPCOM_EINVAL;
2765 goto done;
2766 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002767 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002768 {
Florin Coras5e062572019-03-14 19:07:51 -07002769 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002770 rv = VPPCOM_EINVAL;
2771 goto done;
2772 }
Florin Coras17ec5772020-08-12 20:46:29 -07002773 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002774 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002775 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002776 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002777 rv = VPPCOM_EINVAL;
2778 goto done;
2779 }
hanlin475c9d72019-12-26 11:44:28 +08002780
2781 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2782 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002783 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002784 {
2785 session_event_t e = { 0 };
2786 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002787 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002788 vec_add1 (wrk->unhandled_evts_vector, e);
2789 }
Florin Coras17ec5772020-08-12 20:46:29 -07002790 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2791 s->vep.ev = *event;
2792 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002793 if (txf)
2794 {
2795 if (event->events & EPOLLOUT)
2796 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2797 else
2798 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2799 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002800 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2801 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802 break;
2803
2804 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002805 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002806 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002807 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002808 rv = VPPCOM_EINVAL;
2809 goto done;
2810 }
Florin Coras17ec5772020-08-12 20:46:29 -07002811 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002812 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002813 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002814 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002815 rv = VPPCOM_EINVAL;
2816 goto done;
2817 }
2818
Florin Coras17ec5772020-08-12 20:46:29 -07002819 if (s->vep.prev_sh == vep_handle)
2820 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002821 else
2822 {
Florin Coras7e12d942018-06-27 14:32:43 -07002823 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002824 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002825 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002826 {
Florin Coras5e062572019-03-14 19:07:51 -07002827 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002828 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002829 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002830 }
Florin Coras134a9962018-08-28 11:32:04 -07002831 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002832 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002833 }
Florin Coras17ec5772020-08-12 20:46:29 -07002834 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002835 {
Florin Coras7e12d942018-06-27 14:32:43 -07002836 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002837 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002838 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002839 {
Florin Coras5e062572019-03-14 19:07:51 -07002840 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002841 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002842 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002843 }
Florin Coras134a9962018-08-28 11:32:04 -07002844 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002845 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002846 }
2847
Florin Coras17ec5772020-08-12 20:46:29 -07002848 memset (&s->vep, 0, sizeof (s->vep));
2849 s->vep.next_sh = ~0;
2850 s->vep.prev_sh = ~0;
2851 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002852 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002853
Florin Coras17ec5772020-08-12 20:46:29 -07002854 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2855 if (txf)
2856 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002857
Florin Coras5e062572019-03-14 19:07:51 -07002858 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002859 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002860 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002861 break;
2862
2863 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002864 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002865 rv = VPPCOM_EINVAL;
2866 }
2867
Florin Coras134a9962018-08-28 11:32:04 -07002868 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002869
2870done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002871 return rv;
2872}
2873
Florin Coras86f04502018-09-12 16:08:01 -07002874static inline void
2875vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2876 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002877{
2878 session_disconnected_msg_t *disconnected_msg;
2879 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002880 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002881 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002882 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002883 u8 add_event = 0;
2884
2885 switch (e->event_type)
2886 {
Florin Coras653e43f2019-03-04 10:56:23 -08002887 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002888 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002889 s = vcl_session_get (wrk, sid);
2890 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002891 break;
Florin Corasb242d312020-10-26 15:35:40 -07002892 vcl_fifo_rx_evt_valid_or_break (s);
2893 session_events = s->vep.ev.events;
2894 if (!(EPOLLIN & s->vep.ev.events)
2895 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002896 break;
2897 add_event = 1;
2898 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002899 session_evt_data = s->vep.ev.data.u64;
2900 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002901 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002902 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002903 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002904 s = vcl_session_get (wrk, sid);
2905 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002906 break;
Florin Corasb242d312020-10-26 15:35:40 -07002907 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002908 if (!(EPOLLOUT & session_events))
2909 break;
2910 add_event = 1;
2911 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002912 session_evt_data = s->vep.ev.data.u64;
2913 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2914 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002915 break;
Florin Coras86f04502018-09-12 16:08:01 -07002916 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002917 if (!e->postponed)
2918 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2919 else
2920 s = vcl_session_get (wrk, e->session_index);
2921 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002922 break;
Florin Corasb242d312020-10-26 15:35:40 -07002923 session_events = s->vep.ev.events;
2924 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002925 if (!(EPOLLIN & session_events))
2926 break;
Florin Coras86f04502018-09-12 16:08:01 -07002927 add_event = 1;
2928 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002929 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002930 break;
2931 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002932 if (!e->postponed)
2933 {
2934 connected_msg = (session_connected_msg_t *) e->data;
2935 sid = vcl_session_connected_handler (wrk, connected_msg);
2936 }
2937 else
2938 sid = e->session_index;
2939 s = vcl_session_get (wrk, sid);
2940 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002941 break;
Florin Corasb242d312020-10-26 15:35:40 -07002942 session_events = s->vep.ev.events;
2943 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002944 if (!(EPOLLOUT & session_events))
2945 break;
2946 add_event = 1;
2947 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002948 session_evt_data = s->vep.ev.data.u64;
2949 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002950 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002951 break;
2952 case SESSION_CTRL_EVT_DISCONNECTED:
2953 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002954 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2955 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002956 break;
Florin Corasb242d312020-10-26 15:35:40 -07002957 sid = s->session_index;
2958 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002959 add_event = 1;
2960 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002961 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002962 break;
2963 case SESSION_CTRL_EVT_RESET:
2964 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002965 s = vcl_session_get (wrk, sid);
2966 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002967 break;
Florin Corasb242d312020-10-26 15:35:40 -07002968 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002969 add_event = 1;
2970 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002971 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002972 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002973 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2974 vcl_session_unlisten_reply_handler (wrk, e->data);
2975 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002976 case SESSION_CTRL_EVT_MIGRATED:
2977 vcl_session_migrated_handler (wrk, e->data);
2978 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002979 case SESSION_CTRL_EVT_CLEANUP:
2980 vcl_session_cleanup_handler (wrk, e->data);
2981 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002982 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2983 vcl_session_req_worker_update_handler (wrk, e->data);
2984 break;
2985 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2986 vcl_session_worker_update_reply_handler (wrk, e->data);
2987 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002988 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2989 vcl_session_app_add_segment_handler (wrk, e->data);
2990 break;
2991 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2992 vcl_session_app_del_segment_handler (wrk, e->data);
2993 break;
hanlina3a48962020-07-13 11:09:15 +08002994 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002995 vcl_worker_rpc_handler (wrk, e->data);
2996 break;
Florin Coras86f04502018-09-12 16:08:01 -07002997 default:
2998 VDBG (0, "unhandled: %u", e->event_type);
2999 break;
3000 }
3001
3002 if (add_event)
3003 {
3004 events[*num_ev].data.u64 = session_evt_data;
3005 if (EPOLLONESHOT & session_events)
3006 {
Florin Corasb242d312020-10-26 15:35:40 -07003007 s = vcl_session_get (wrk, sid);
3008 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003009 }
3010 *num_ev += 1;
3011 }
3012}
3013
3014static int
3015vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3016 struct epoll_event *events, u32 maxevents,
3017 double wait_for_time, u32 * num_ev)
3018{
Florin Coras99368312018-08-02 10:45:44 -07003019 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003020 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003021 int i;
3022
Florin Coras539663c2018-09-28 14:59:37 -07003023 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3024 goto handle_dequeued;
3025
Florin Coras54693d22018-07-17 10:46:29 -07003026 if (svm_msg_q_is_empty (mq))
3027 {
3028 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003029 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003030 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003031 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003032 else
3033 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003034 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003035 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003036 }
3037 }
Florin Corase003a1b2019-06-05 10:47:16 -07003038 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003039 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003040
Florin Coras539663c2018-09-28 14:59:37 -07003041handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003042 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003043 {
Florin Coras134a9962018-08-28 11:32:04 -07003044 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003045 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003046 if (*num_ev < maxevents)
3047 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3048 else
3049 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003050 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003051 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003052 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003053 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003054 return *num_ev;
3055}
3056
Florin Coras99368312018-08-02 10:45:44 -07003057static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003058vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3059 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003060{
Florin Corasccdb8b82021-04-28 13:01:06 -07003061 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003062
3063 if (!n_evts)
3064 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003065 if (timeout_ms > 0)
3066 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003067 }
3068
3069 do
3070 {
3071 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003072 timeout_ms, &n_evts);
3073 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003074 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003075 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003076 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003077
3078 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003079}
3080
3081static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003082vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3083 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003084{
Florin Coras99368312018-08-02 10:45:44 -07003085 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003086 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003087 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003088 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003089 u64 buf;
3090
Florin Coras134a9962018-08-28 11:32:04 -07003091 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003092 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003093 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003094 if (timeout_ms > 0)
3095 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003096 }
3097
Florin Corasb13ba132021-01-27 16:05:24 -08003098 do
3099 {
3100 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003101 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003102 if (n_mq_evts < 0)
3103 {
3104 VDBG (0, "epoll_wait error %u", errno);
3105 return n_evts;
3106 }
3107
3108 for (i = 0; i < n_mq_evts; i++)
3109 {
3110 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3111 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3112 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3113 &n_evts);
3114 }
3115
Florin Corasccdb8b82021-04-28 13:01:06 -07003116 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003117 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003118 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003119 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003120
3121 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003122}
3123
Dave Wallacef7f809c2017-10-03 01:48:42 -04003124int
Florin Coras134a9962018-08-28 11:32:04 -07003125vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003126 int maxevents, double wait_for_time)
3127{
Florin Coras134a9962018-08-28 11:32:04 -07003128 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003129 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003130 u32 n_evts = 0;
3131 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003132
3133 if (PREDICT_FALSE (maxevents <= 0))
3134 {
Florin Coras5e062572019-03-14 19:07:51 -07003135 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003136 return VPPCOM_EINVAL;
3137 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003138
Florin Coras134a9962018-08-28 11:32:04 -07003139 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003140 if (!vep_session)
3141 return VPPCOM_EBADFD;
3142
Florin Coras6c3b2182020-10-19 18:36:48 -07003143 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003144 {
Florin Coras5e062572019-03-14 19:07:51 -07003145 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003146 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003147 }
Florin Coras54693d22018-07-17 10:46:29 -07003148
3149 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003150
Florin Coras86f04502018-09-12 16:08:01 -07003151 if (vec_len (wrk->unhandled_evts_vector))
3152 {
3153 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3154 {
3155 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3156 events, &n_evts);
3157 if (n_evts == maxevents)
3158 {
Florin Corase003a1b2019-06-05 10:47:16 -07003159 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3160 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003161 }
3162 }
Florin Corase003a1b2019-06-05 10:47:16 -07003163 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003164 }
wanghanlin8919fec2021-03-18 20:00:41 +08003165 /* Request to only drain unhandled */
3166 if ((int) wait_for_time == -2)
3167 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003168
3169 if (vcm->cfg.use_mq_eventfd)
3170 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3171 wait_for_time);
3172
3173 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3174 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003175}
3176
Dave Wallace35830af2017-10-09 01:43:42 -04003177int
Florin Coras134a9962018-08-28 11:32:04 -07003178vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003179 void *buffer, uint32_t * buflen)
3180{
Florin Coras134a9962018-08-28 11:32:04 -07003181 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003182 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003183 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003184 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003185 vcl_session_t *session;
3186 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003187
Florin Coras134a9962018-08-28 11:32:04 -07003188 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003189 if (!session)
3190 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003191
Dave Wallace35830af2017-10-09 01:43:42 -04003192 switch (op)
3193 {
3194 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003195 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003196 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3197 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003198 break;
3199
Dave Wallace227867f2017-11-13 21:21:53 -05003200 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003201 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003202 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3203 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003204 break;
3205
3206 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003207 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003208 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003209 *flags =
3210 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003211 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003212 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003213 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003214 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3215 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003216 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003217 }
3218 else
3219 rv = VPPCOM_EINVAL;
3220 break;
3221
3222 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003223 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003224 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003225 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003226 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003227 else
Florin Corasac422d62020-10-19 20:51:36 -07003228 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003229
Florin Coras5e062572019-03-14 19:07:51 -07003230 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3231 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003232 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003233 }
3234 else
3235 rv = VPPCOM_EINVAL;
3236 break;
3237
3238 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003239 if (PREDICT_TRUE (buffer && buflen &&
3240 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003241 {
Florin Coras7e12d942018-06-27 14:32:43 -07003242 ep->is_ip4 = session->transport.is_ip4;
3243 ep->port = session->transport.rmt_port;
3244 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003245 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3246 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003247 else
Dave Barach178cf492018-11-13 16:34:13 -05003248 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3249 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003250 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003251 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3252 "addr = %U, port %u", session_handle, ep->is_ip4,
3253 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003254 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3255 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003256 }
3257 else
3258 rv = VPPCOM_EINVAL;
3259 break;
3260
3261 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003262 if (PREDICT_TRUE (buffer && buflen &&
3263 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003264 {
Florin Coras7e12d942018-06-27 14:32:43 -07003265 ep->is_ip4 = session->transport.is_ip4;
3266 ep->port = session->transport.lcl_port;
3267 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003268 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3269 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003270 else
Dave Barach178cf492018-11-13 16:34:13 -05003271 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3272 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003273 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003274 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3275 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003276 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003277 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3278 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003279 }
3280 else
3281 rv = VPPCOM_EINVAL;
3282 break;
Stevenb5a11602017-10-11 09:59:30 -07003283
Florin Corasef7cbf62019-10-17 09:56:27 -07003284 case VPPCOM_ATTR_SET_LCL_ADDR:
3285 if (PREDICT_TRUE (buffer && buflen &&
3286 (*buflen >= sizeof (*ep)) && ep->ip))
3287 {
3288 session->transport.is_ip4 = ep->is_ip4;
3289 session->transport.lcl_port = ep->port;
3290 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3291 *buflen = sizeof (*ep);
3292 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3293 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3294 &session->transport.lcl_ip,
3295 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3296 clib_net_to_host_u16 (ep->port));
3297 }
3298 else
3299 rv = VPPCOM_EINVAL;
3300 break;
3301
Dave Wallace048b1d62018-01-03 22:24:41 -05003302 case VPPCOM_ATTR_GET_LIBC_EPFD:
3303 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003304 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003305 break;
3306
3307 case VPPCOM_ATTR_SET_LIBC_EPFD:
3308 if (PREDICT_TRUE (buffer && buflen &&
3309 (*buflen == sizeof (session->libc_epfd))))
3310 {
3311 session->libc_epfd = *(int *) buffer;
3312 *buflen = sizeof (session->libc_epfd);
3313
Florin Coras5e062572019-03-14 19:07:51 -07003314 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3315 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003316 }
3317 else
3318 rv = VPPCOM_EINVAL;
3319 break;
3320
3321 case VPPCOM_ATTR_GET_PROTOCOL:
3322 if (buffer && buflen && (*buflen >= sizeof (int)))
3323 {
Florin Coras7e12d942018-06-27 14:32:43 -07003324 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003325 *buflen = sizeof (int);
3326
Florin Coras5e062572019-03-14 19:07:51 -07003327 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3328 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003329 }
3330 else
3331 rv = VPPCOM_EINVAL;
3332 break;
3333
3334 case VPPCOM_ATTR_GET_LISTEN:
3335 if (buffer && buflen && (*buflen >= sizeof (int)))
3336 {
Florin Corasac422d62020-10-19 20:51:36 -07003337 *(int *) buffer = vcl_session_has_attr (session,
3338 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003339 *buflen = sizeof (int);
3340
Florin Coras5e062572019-03-14 19:07:51 -07003341 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3342 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003343 }
3344 else
3345 rv = VPPCOM_EINVAL;
3346 break;
3347
3348 case VPPCOM_ATTR_GET_ERROR:
3349 if (buffer && buflen && (*buflen >= sizeof (int)))
3350 {
3351 *(int *) buffer = 0;
3352 *buflen = sizeof (int);
3353
Florin Coras5e062572019-03-14 19:07:51 -07003354 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3355 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003356 }
3357 else
3358 rv = VPPCOM_EINVAL;
3359 break;
3360
3361 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3362 if (buffer && buflen && (*buflen >= sizeof (u32)))
3363 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003364
3365 /* VPP-TBD */
3366 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003367 session->tx_fifo ?
3368 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003369 vcm->cfg.tx_fifo_size);
3370 *buflen = sizeof (u32);
3371
Florin Coras5e062572019-03-14 19:07:51 -07003372 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3373 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3374 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003375 }
3376 else
3377 rv = VPPCOM_EINVAL;
3378 break;
3379
3380 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3381 if (buffer && buflen && (*buflen == sizeof (u32)))
3382 {
3383 /* VPP-TBD */
3384 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003385 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3386 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3387 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003388 }
3389 else
3390 rv = VPPCOM_EINVAL;
3391 break;
3392
3393 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3394 if (buffer && buflen && (*buflen >= sizeof (u32)))
3395 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003396
3397 /* VPP-TBD */
3398 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003399 session->rx_fifo ?
3400 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003401 vcm->cfg.rx_fifo_size);
3402 *buflen = sizeof (u32);
3403
Florin Coras5e062572019-03-14 19:07:51 -07003404 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3405 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003406 }
3407 else
3408 rv = VPPCOM_EINVAL;
3409 break;
3410
3411 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3412 if (buffer && buflen && (*buflen == sizeof (u32)))
3413 {
3414 /* VPP-TBD */
3415 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003416 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3417 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3418 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003419 }
3420 else
3421 rv = VPPCOM_EINVAL;
3422 break;
3423
3424 case VPPCOM_ATTR_GET_REUSEADDR:
3425 if (buffer && buflen && (*buflen >= sizeof (int)))
3426 {
3427 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003428 *(int *) buffer = vcl_session_has_attr (session,
3429 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003430 *buflen = sizeof (int);
3431
Florin Coras5e062572019-03-14 19:07:51 -07003432 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3433 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 }
3435 else
3436 rv = VPPCOM_EINVAL;
3437 break;
3438
Stevenb5a11602017-10-11 09:59:30 -07003439 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003440 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003441 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003442 {
3443 /* VPP-TBD */
3444 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003445 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003446 else
Florin Corasac422d62020-10-19 20:51:36 -07003447 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003448
Florin Coras5e062572019-03-14 19:07:51 -07003449 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003450 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003451 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003452 }
3453 else
3454 rv = VPPCOM_EINVAL;
3455 break;
3456
3457 case VPPCOM_ATTR_GET_REUSEPORT:
3458 if (buffer && buflen && (*buflen >= sizeof (int)))
3459 {
3460 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003461 *(int *) buffer = vcl_session_has_attr (session,
3462 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003463 *buflen = sizeof (int);
3464
Florin Coras5e062572019-03-14 19:07:51 -07003465 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3466 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003467 }
3468 else
3469 rv = VPPCOM_EINVAL;
3470 break;
3471
3472 case VPPCOM_ATTR_SET_REUSEPORT:
3473 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003474 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 {
3476 /* VPP-TBD */
3477 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003478 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003479 else
Florin Corasac422d62020-10-19 20:51:36 -07003480 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003481
Florin Coras5e062572019-03-14 19:07:51 -07003482 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003483 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003484 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003485 }
3486 else
3487 rv = VPPCOM_EINVAL;
3488 break;
3489
3490 case VPPCOM_ATTR_GET_BROADCAST:
3491 if (buffer && buflen && (*buflen >= sizeof (int)))
3492 {
3493 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003494 *(int *) buffer = vcl_session_has_attr (session,
3495 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 *buflen = sizeof (int);
3497
Florin Coras5e062572019-03-14 19:07:51 -07003498 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3499 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003500 }
3501 else
3502 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003503 break;
3504
3505 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003506 if (buffer && buflen && (*buflen == sizeof (int)))
3507 {
3508 /* VPP-TBD */
3509 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003510 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003511 else
Florin Corasac422d62020-10-19 20:51:36 -07003512 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003513
Florin Coras5e062572019-03-14 19:07:51 -07003514 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003515 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003516 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003517 }
3518 else
3519 rv = VPPCOM_EINVAL;
3520 break;
3521
3522 case VPPCOM_ATTR_GET_V6ONLY:
3523 if (buffer && buflen && (*buflen >= sizeof (int)))
3524 {
3525 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003526 *(int *) buffer = vcl_session_has_attr (session,
3527 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 *buflen = sizeof (int);
3529
Florin Coras5e062572019-03-14 19:07:51 -07003530 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3531 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003532 }
3533 else
3534 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003535 break;
3536
3537 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003538 if (buffer && buflen && (*buflen == sizeof (int)))
3539 {
3540 /* VPP-TBD */
3541 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003542 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003543 else
Florin Corasac422d62020-10-19 20:51:36 -07003544 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003545
Florin Coras5e062572019-03-14 19:07:51 -07003546 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003547 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003548 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003549 }
3550 else
3551 rv = VPPCOM_EINVAL;
3552 break;
3553
3554 case VPPCOM_ATTR_GET_KEEPALIVE:
3555 if (buffer && buflen && (*buflen >= sizeof (int)))
3556 {
3557 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003558 *(int *) buffer = vcl_session_has_attr (session,
3559 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003560 *buflen = sizeof (int);
3561
Florin Coras5e062572019-03-14 19:07:51 -07003562 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3563 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003564 }
3565 else
3566 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003567 break;
Stevenbccd3392017-10-12 20:42:21 -07003568
3569 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003570 if (buffer && buflen && (*buflen == sizeof (int)))
3571 {
3572 /* VPP-TBD */
3573 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003574 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003575 else
Florin Corasac422d62020-10-19 20:51:36 -07003576 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003577
Florin Coras5e062572019-03-14 19:07:51 -07003578 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003579 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003580 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003581 }
3582 else
3583 rv = VPPCOM_EINVAL;
3584 break;
3585
3586 case VPPCOM_ATTR_GET_TCP_NODELAY:
3587 if (buffer && buflen && (*buflen >= sizeof (int)))
3588 {
3589 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003590 *(int *) buffer = vcl_session_has_attr (session,
3591 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003592 *buflen = sizeof (int);
3593
Florin Coras5e062572019-03-14 19:07:51 -07003594 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3595 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003596 }
3597 else
3598 rv = VPPCOM_EINVAL;
3599 break;
3600
3601 case VPPCOM_ATTR_SET_TCP_NODELAY:
3602 if (buffer && buflen && (*buflen == sizeof (int)))
3603 {
3604 /* VPP-TBD */
3605 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003606 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003607 else
Florin Corasac422d62020-10-19 20:51:36 -07003608 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003609
Florin Coras5e062572019-03-14 19:07:51 -07003610 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003611 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003612 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003613 }
3614 else
3615 rv = VPPCOM_EINVAL;
3616 break;
3617
3618 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3619 if (buffer && buflen && (*buflen >= sizeof (int)))
3620 {
3621 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003622 *(int *) buffer = vcl_session_has_attr (session,
3623 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003624 *buflen = sizeof (int);
3625
Florin Coras5e062572019-03-14 19:07:51 -07003626 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3627 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003628 }
3629 else
3630 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003631 break;
3632
3633 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003634 if (buffer && buflen && (*buflen == sizeof (int)))
3635 {
3636 /* VPP-TBD */
3637 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003638 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003639 else
Florin Corasac422d62020-10-19 20:51:36 -07003640 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003641
Florin Coras5e062572019-03-14 19:07:51 -07003642 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003643 vcl_session_has_attr (session,
3644 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003645 }
3646 else
3647 rv = VPPCOM_EINVAL;
3648 break;
3649
3650 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3651 if (buffer && buflen && (*buflen >= sizeof (int)))
3652 {
3653 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003654 *(int *) buffer = vcl_session_has_attr (session,
3655 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003656 *buflen = sizeof (int);
3657
Florin Coras5e062572019-03-14 19:07:51 -07003658 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3659 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003660 }
3661 else
3662 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003663 break;
3664
3665 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003666 if (buffer && buflen && (*buflen == sizeof (int)))
3667 {
3668 /* VPP-TBD */
3669 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003670 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003671 else
Florin Corasac422d62020-10-19 20:51:36 -07003672 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003673
Florin Coras5e062572019-03-14 19:07:51 -07003674 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003675 vcl_session_has_attr (session,
3676 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003677 }
3678 else
3679 rv = VPPCOM_EINVAL;
3680 break;
3681
3682 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003683 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003684 {
Florin Coras04ae8272021-04-12 19:55:37 -07003685 rv = VPPCOM_EINVAL;
3686 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003687 }
Florin Coras04ae8272021-04-12 19:55:37 -07003688
3689 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3690 tea.mss = *(u32 *) buffer;
3691 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3692 rv = VPPCOM_ENOPROTOOPT;
3693
3694 if (!rv)
3695 {
3696 *(u32 *) buffer = tea.mss;
3697 *buflen = sizeof (int);
3698 }
3699
3700 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3701 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003702 break;
3703
3704 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003705 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003706 {
Florin Coras04ae8272021-04-12 19:55:37 -07003707 rv = VPPCOM_EINVAL;
3708 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003709 }
Florin Coras04ae8272021-04-12 19:55:37 -07003710
3711 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3712 tea.mss = *(u32 *) buffer;
3713 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3714 rv = VPPCOM_ENOPROTOOPT;
3715
3716 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3717 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003718 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003719
Florin Coras7baeb712019-01-04 17:05:43 -08003720 case VPPCOM_ATTR_SET_SHUT:
3721 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003722 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003723 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003724 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003725 break;
3726
3727 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003728 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003729 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003730 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003731 tmp_flags |= 2;
3732 if (tmp_flags == 1)
3733 *(int *) buffer = SHUT_RD;
3734 else if (tmp_flags == 2)
3735 *(int *) buffer = SHUT_WR;
3736 else if (tmp_flags == 3)
3737 *(int *) buffer = SHUT_RDWR;
3738 *buflen = sizeof (int);
3739 break;
Florin Coras1e966172020-05-16 18:18:14 +00003740
3741 case VPPCOM_ATTR_SET_CONNECTED:
3742 session->flags |= VCL_SESSION_F_CONNECTED;
3743 break;
3744
Florin Corasa5a9efd2021-01-05 17:03:29 -08003745 case VPPCOM_ATTR_SET_CKPAIR:
3746 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3747 !vcl_session_has_crypto (session))
3748 {
3749 rv = VPPCOM_EINVAL;
3750 break;
3751 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003752 if (!session->ext_config)
3753 {
Florin Coras87f63892021-05-01 16:01:40 -07003754 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3755 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003756 }
3757 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3758 {
3759 rv = VPPCOM_EINVAL;
3760 break;
3761 }
3762
3763 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003764 break;
3765
Florin Coras6a6555a2021-01-28 11:39:27 -08003766 case VPPCOM_ATTR_SET_VRF:
3767 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3768 {
3769 rv = VPPCOM_EINVAL;
3770 break;
3771 }
3772 session->vrf = *(u32 *) buffer;
3773 break;
3774
3775 case VPPCOM_ATTR_GET_VRF:
3776 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3777 {
3778 rv = VPPCOM_EINVAL;
3779 break;
3780 }
3781 *(u32 *) buffer = session->vrf;
3782 *buflen = sizeof (u32);
3783 break;
3784
wanghanlin0674f852021-02-22 10:38:36 +08003785 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003786 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003787 {
Florin Corasd77325c2021-02-23 12:03:03 -08003788 rv = VPPCOM_EINVAL;
3789 break;
wanghanlin0674f852021-02-22 10:38:36 +08003790 }
Florin Corasd77325c2021-02-23 12:03:03 -08003791
3792 if (session->transport.is_ip4)
3793 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003794 else
Florin Corasd77325c2021-02-23 12:03:03 -08003795 *(int *) buffer = AF_INET6;
3796 *buflen = sizeof (int);
3797
wanghanlin0674f852021-02-22 10:38:36 +08003798 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3799 *buflen);
3800 break;
3801
Florin Coras87f63892021-05-01 16:01:40 -07003802 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3803 if (!(buffer && buflen && (*buflen > 0)))
3804 {
3805 rv = VPPCOM_EINVAL;
3806 break;
3807 }
3808 if (session->ext_config)
3809 {
3810 rv = VPPCOM_EINVAL;
3811 break;
3812 }
3813 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3814 *buflen + sizeof (u32));
3815 clib_memcpy (session->ext_config->data, buffer, *buflen);
3816 session->ext_config->len = *buflen;
3817 break;
3818
Dave Wallacee22aa742017-10-20 12:30:38 -04003819 default:
3820 rv = VPPCOM_EINVAL;
3821 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003822 }
3823
Dave Wallace35830af2017-10-09 01:43:42 -04003824 return rv;
3825}
3826
Stevenac1f96d2017-10-24 16:03:58 -07003827int
Florin Coras134a9962018-08-28 11:32:04 -07003828vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003829 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3830{
Florin Coras134a9962018-08-28 11:32:04 -07003831 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003832 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003833 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003834
3835 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003836 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003837 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003838 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003839 else
3840 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003841 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003842 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003843 }
3844
Florin Coras0a1e1832020-03-29 18:54:04 +00003845 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003846 {
Florin Coras5da10c42020-04-01 04:31:21 +00003847 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003848 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003849 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3850 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003851 else
Dave Barach178cf492018-11-13 16:34:13 -05003852 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3853 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003854 ep->is_ip4 = session->transport.is_ip4;
3855 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003856 }
Florin Coras460dce62018-07-27 05:45:06 -07003857
Stevenac1f96d2017-10-24 16:03:58 -07003858 return rv;
3859}
3860
3861int
Florin Coras134a9962018-08-28 11:32:04 -07003862vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003863 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3864{
Florin Coras7a2abce2020-04-05 19:25:44 +00003865 vcl_worker_t *wrk = vcl_worker_get_current ();
3866 vcl_session_t *s;
3867
3868 s = vcl_session_get_w_handle (wrk, session_handle);
3869 if (!s)
3870 return VPPCOM_EBADFD;
3871
Dave Wallace617dffa2017-10-26 14:47:06 -04003872 if (!buffer)
3873 return VPPCOM_EINVAL;
3874
3875 if (ep)
3876 {
Florin Coras5824cc52020-10-20 18:44:41 -07003877 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003878 return VPPCOM_EINVAL;
3879
3880 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003881 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003882 {
Florin Coras5824cc52020-10-20 18:44:41 -07003883 u32 session_index = s->session_index;
3884 f64 timeout = vcm->cfg.session_timeout;
3885 int rv;
3886
Florin Coras5da10c42020-04-01 04:31:21 +00003887 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003888 rv = vppcom_wait_for_session_state_change (session_index,
3889 VCL_STATE_READY,
3890 timeout);
3891 if (rv < 0)
3892 return rv;
3893 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003894 }
Florin Coras5824cc52020-10-20 18:44:41 -07003895
3896 s->transport.is_ip4 = ep->is_ip4;
3897 s->transport.rmt_port = ep->port;
3898 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003899 }
3900
3901 if (flags)
3902 {
3903 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003904 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003905 }
3906
Florin Coras7a2abce2020-04-05 19:25:44 +00003907 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3908 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003909}
3910
Dave Wallace048b1d62018-01-03 22:24:41 -05003911int
3912vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3913{
Florin Coras134a9962018-08-28 11:32:04 -07003914 vcl_worker_t *wrk = vcl_worker_get_current ();
3915 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003916 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003917 svm_msg_q_msg_t msg;
3918 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003919 int rv, num_ev = 0;
3920
Florin Coras5e062572019-03-14 19:07:51 -07003921 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003922
3923 if (!vp)
3924 return VPPCOM_EFAULT;
3925
3926 do
3927 {
Florin Coras7e12d942018-06-27 14:32:43 -07003928 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003929
Florin Coras6917b942018-11-13 22:44:54 -08003930 /* Dequeue all events and drop all unhandled io events */
3931 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3932 {
3933 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3934 vcl_handle_mq_event (wrk, e);
3935 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3936 }
3937 vec_reset_length (wrk->unhandled_evts_vector);
3938
Dave Wallace048b1d62018-01-03 22:24:41 -05003939 for (i = 0; i < n_sids; i++)
3940 {
Florin Coras7baeb712019-01-04 17:05:43 -08003941 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003942 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003943 {
3944 vp[i].revents = POLLHUP;
3945 num_ev++;
3946 continue;
3947 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003948
Florin Coras6917b942018-11-13 22:44:54 -08003949 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003950
3951 if (POLLIN & vp[i].events)
3952 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003953 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003954 if (rv > 0)
3955 {
Florin Coras6917b942018-11-13 22:44:54 -08003956 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003957 num_ev++;
3958 }
3959 else if (rv < 0)
3960 {
3961 switch (rv)
3962 {
3963 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003964 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003965 break;
3966
3967 default:
Florin Coras6917b942018-11-13 22:44:54 -08003968 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003969 break;
3970 }
3971 num_ev++;
3972 }
3973 }
3974
3975 if (POLLOUT & vp[i].events)
3976 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003977 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003978 if (rv > 0)
3979 {
Florin Coras6917b942018-11-13 22:44:54 -08003980 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003981 num_ev++;
3982 }
3983 else if (rv < 0)
3984 {
3985 switch (rv)
3986 {
3987 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003988 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003989 break;
3990
3991 default:
Florin Coras6917b942018-11-13 22:44:54 -08003992 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003993 break;
3994 }
3995 num_ev++;
3996 }
3997 }
3998
Dave Wallace7e607a72018-06-18 18:41:32 -04003999 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004000 {
Florin Coras6917b942018-11-13 22:44:54 -08004001 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004002 num_ev++;
4003 }
4004 }
4005 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004006 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004007 }
4008 while ((num_ev == 0) && keep_trying);
4009
Dave Wallace048b1d62018-01-03 22:24:41 -05004010 return num_ev;
4011}
4012
Florin Coras99368312018-08-02 10:45:44 -07004013int
4014vppcom_mq_epoll_fd (void)
4015{
Florin Coras134a9962018-08-28 11:32:04 -07004016 vcl_worker_t *wrk = vcl_worker_get_current ();
4017 return wrk->mqs_epfd;
4018}
4019
4020int
Florin Coras30e79c22019-01-02 19:31:22 -08004021vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004022{
4023 return session_handle & 0xFFFFFF;
4024}
4025
4026int
Florin Coras30e79c22019-01-02 19:31:22 -08004027vppcom_session_worker (vcl_session_handle_t session_handle)
4028{
4029 return session_handle >> 24;
4030}
4031
4032int
Florin Coras134a9962018-08-28 11:32:04 -07004033vppcom_worker_register (void)
4034{
Florin Coras47c40e22018-11-26 17:01:36 -08004035 if (!vcl_worker_alloc_and_init ())
4036 return VPPCOM_EEXIST;
4037
Florin Coras47c40e22018-11-26 17:01:36 -08004038 if (vcl_worker_register_with_vpp ())
4039 return VPPCOM_EEXIST;
4040
4041 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004042}
4043
Florin Coras369db832019-07-08 12:34:45 -07004044void
4045vppcom_worker_unregister (void)
4046{
4047 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4048 vcl_set_worker_index (~0);
4049}
4050
Pivo6017ff02020-05-04 17:57:33 +02004051void
4052vppcom_worker_index_set (int index)
4053{
4054 vcl_set_worker_index (index);
4055}
4056
Florin Corasdfe4cf42018-11-28 22:13:45 -08004057int
4058vppcom_worker_index (void)
4059{
4060 return vcl_get_worker_index ();
4061}
4062
Florin Corase0982e52019-01-25 13:19:56 -08004063int
4064vppcom_worker_mqs_epfd (void)
4065{
4066 vcl_worker_t *wrk = vcl_worker_get_current ();
4067 if (!vcm->cfg.use_mq_eventfd)
4068 return -1;
4069 return wrk->mqs_epfd;
4070}
4071
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004072int
4073vppcom_session_is_connectable_listener (uint32_t session_handle)
4074{
4075 vcl_session_t *session;
4076 vcl_worker_t *wrk = vcl_worker_get_current ();
4077 session = vcl_session_get_w_handle (wrk, session_handle);
4078 if (!session)
4079 return VPPCOM_EBADFD;
4080 return vcl_session_is_connectable_listener (wrk, session);
4081}
4082
4083int
4084vppcom_session_listener (uint32_t session_handle)
4085{
4086 vcl_worker_t *wrk = vcl_worker_get_current ();
4087 vcl_session_t *listen_session, *session;
4088 session = vcl_session_get_w_handle (wrk, session_handle);
4089 if (!session)
4090 return VPPCOM_EBADFD;
4091 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4092 return VPPCOM_EBADFD;
4093 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4094 if (!listen_session)
4095 return VPPCOM_EBADFD;
4096 return vcl_session_handle (listen_session);
4097}
4098
4099int
4100vppcom_session_n_accepted (uint32_t session_handle)
4101{
4102 vcl_worker_t *wrk = vcl_worker_get_current ();
4103 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4104 if (!session)
4105 return VPPCOM_EBADFD;
4106 return session->n_accepted_sessions;
4107}
4108
Florin Coras66ec4672020-06-15 07:59:40 -07004109const char *
4110vppcom_proto_str (vppcom_proto_t proto)
4111{
4112 char const *proto_str;
4113
4114 switch (proto)
4115 {
4116 case VPPCOM_PROTO_TCP:
4117 proto_str = "TCP";
4118 break;
4119 case VPPCOM_PROTO_UDP:
4120 proto_str = "UDP";
4121 break;
4122 case VPPCOM_PROTO_TLS:
4123 proto_str = "TLS";
4124 break;
4125 case VPPCOM_PROTO_QUIC:
4126 proto_str = "QUIC";
4127 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004128 case VPPCOM_PROTO_DTLS:
4129 proto_str = "DTLS";
4130 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004131 default:
4132 proto_str = "UNKNOWN";
4133 break;
4134 }
4135 return proto_str;
4136}
4137
4138const char *
4139vppcom_retval_str (int retval)
4140{
4141 char const *st;
4142
4143 switch (retval)
4144 {
4145 case VPPCOM_OK:
4146 st = "VPPCOM_OK";
4147 break;
4148
4149 case VPPCOM_EAGAIN:
4150 st = "VPPCOM_EAGAIN";
4151 break;
4152
4153 case VPPCOM_EFAULT:
4154 st = "VPPCOM_EFAULT";
4155 break;
4156
4157 case VPPCOM_ENOMEM:
4158 st = "VPPCOM_ENOMEM";
4159 break;
4160
4161 case VPPCOM_EINVAL:
4162 st = "VPPCOM_EINVAL";
4163 break;
4164
4165 case VPPCOM_EBADFD:
4166 st = "VPPCOM_EBADFD";
4167 break;
4168
4169 case VPPCOM_EAFNOSUPPORT:
4170 st = "VPPCOM_EAFNOSUPPORT";
4171 break;
4172
4173 case VPPCOM_ECONNABORTED:
4174 st = "VPPCOM_ECONNABORTED";
4175 break;
4176
4177 case VPPCOM_ECONNRESET:
4178 st = "VPPCOM_ECONNRESET";
4179 break;
4180
4181 case VPPCOM_ENOTCONN:
4182 st = "VPPCOM_ENOTCONN";
4183 break;
4184
4185 case VPPCOM_ECONNREFUSED:
4186 st = "VPPCOM_ECONNREFUSED";
4187 break;
4188
4189 case VPPCOM_ETIMEDOUT:
4190 st = "VPPCOM_ETIMEDOUT";
4191 break;
4192
4193 default:
4194 st = "UNKNOWN_STATE";
4195 break;
4196 }
4197
4198 return st;
4199}
4200
Florin Corasa5a9efd2021-01-05 17:03:29 -08004201int
4202vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4203{
4204 if (vcm->cfg.vpp_app_socket_api)
4205 {
4206 clib_warning ("not supported");
4207 return VPPCOM_EINVAL;
4208 }
4209 return vcl_bapi_add_cert_key_pair (ckpair);
4210}
4211
4212int
4213vppcom_del_cert_key_pair (uint32_t ckpair_index)
4214{
4215 if (vcm->cfg.vpp_app_socket_api)
4216 {
4217 clib_warning ("not supported");
4218 return VPPCOM_EINVAL;
4219 }
4220 return vcl_bapi_del_cert_key_pair (ckpair_index);
4221}
4222
Dave Wallacee22aa742017-10-20 12:30:38 -04004223/*
4224 * fd.io coding-style-patch-verification: ON
4225 *
4226 * Local Variables:
4227 * eval: (c-set-style "gnu")
4228 * End:
4229 */