blob: a0f4338f902d1dba9e50b4108a0fb7f637ec4fce [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
263vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
264{
265 app_session_evt_t _app_evt, *app_evt = &_app_evt;
266 session_disconnect_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_DISCONNECT);
272 mp = (session_disconnect_msg_t *) app_evt->evt->data;
273 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700274 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700275 mp->handle = s->vpp_handle;
276 app_send_ctrl_evt_to_vpp (mq, app_evt);
277}
278
279static void
280vcl_send_app_detach (vcl_worker_t * wrk)
281{
282 app_session_evt_t _app_evt, *app_evt = &_app_evt;
283 session_app_detach_msg_t *mp;
284 svm_msg_q_t *mq;
285
286 mq = vcl_worker_ctrl_mq (wrk);
287 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
288 mp = (session_app_detach_msg_t *) app_evt->evt->data;
289 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700290 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700291 app_send_ctrl_evt_to_vpp (mq, app_evt);
292}
Florin Coras697faea2018-06-27 17:10:49 -0700293
Florin Coras54693d22018-07-17 10:46:29 -0700294static void
295vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
296 session_handle_t handle, int retval)
297{
298 app_session_evt_t _app_evt, *app_evt = &_app_evt;
299 session_accepted_reply_msg_t *rmp;
300 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
301 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
302 rmp->handle = handle;
303 rmp->context = context;
304 rmp->retval = retval;
305 app_send_ctrl_evt_to_vpp (mq, app_evt);
306}
307
Florin Coras99368312018-08-02 10:45:44 -0700308static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800309vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
310 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700311{
312 app_session_evt_t _app_evt, *app_evt = &_app_evt;
313 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800314 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700315 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
316 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800317 rmp->handle = s->vpp_handle;
318 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700319 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800320 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700321}
322
Florin Corasc9fbd662018-08-24 12:59:56 -0700323static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800324vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
325 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700326{
327 app_session_evt_t _app_evt, *app_evt = &_app_evt;
328 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800329 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
330 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700331 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800332 rmp->handle = s->vpp_handle;
333 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700334 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800335 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700336}
337
Florin Coras30e79c22019-01-02 19:31:22 -0800338void
339vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
340 u32 wrk_index)
341{
342 app_session_evt_t _app_evt, *app_evt = &_app_evt;
343 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800344
Florin Coras52dd29f2020-11-18 19:02:17 -0800345 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
346 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800347 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700348 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800349 mp->handle = s->vpp_handle;
350 mp->req_wrk_index = wrk->vpp_wrk_index;
351 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800352 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800353}
354
hanlina3a48962020-07-13 11:09:15 +0800355int
Florin Coras40c07ce2020-07-16 20:46:17 -0700356vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
357{
358 app_session_evt_t _app_evt, *app_evt = &_app_evt;
359 session_app_wrk_rpc_msg_t *mp;
360 vcl_worker_t *dst_wrk, *wrk;
361 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800362 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700363
364 if (data_len > sizeof (mp->data))
365 goto done;
366
367 clib_spinlock_lock (&vcm->workers_lock);
368
369 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
370 if (!dst_wrk)
371 goto done;
372
373 wrk = vcl_worker_get_current ();
374 mq = vcl_worker_ctrl_mq (wrk);
375 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
376 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700377 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700378 mp->wrk_index = dst_wrk->vpp_wrk_index;
379 clib_memcpy (mp->data, data, data_len);
380 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800381 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700382
383done:
384 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800385 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700386}
387
Florin Coras04ae8272021-04-12 19:55:37 -0700388int
389vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
390 transport_endpt_attr_t *attr)
391{
392 app_session_evt_t _app_evt, *app_evt = &_app_evt;
393 session_transport_attr_msg_t *mp;
394 svm_msg_q_t *mq;
395 f64 timeout;
396
397 ASSERT (!wrk->session_attr_op);
398 wrk->session_attr_op = 1;
399 wrk->session_attr_op_rv = -1;
400
401 mq = s->vpp_evt_q;
402 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
403 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
404 memset (mp, 0, sizeof (*mp));
405 mp->client_index = wrk->api_client_handle;
406 mp->handle = s->vpp_handle;
407 mp->is_get = is_get;
408 mp->attr = *attr;
409 app_send_ctrl_evt_to_vpp (mq, app_evt);
410
411 timeout = clib_time_now (&wrk->clib_time) + 1;
412
413 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
414 vcl_flush_mq_events ();
415
416 if (!wrk->session_attr_op_rv && is_get)
417 *attr = wrk->session_attr_rv;
418
419 wrk->session_attr_op = 0;
420
421 return wrk->session_attr_op_rv;
422}
423
Florin Coras54693d22018-07-17 10:46:29 -0700424static u32
Florin Coras00cca802019-06-06 09:38:44 -0700425vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
426 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700427{
428 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700429 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700430
Florin Coras134a9962018-08-28 11:32:04 -0700431 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700432
Florin Coras00cca802019-06-06 09:38:44 -0700433 listen_session = vcl_session_get (wrk, ls_index);
434 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700435 {
Florin Coras00cca802019-06-06 09:38:44 -0700436 VDBG (0, "ERROR: listener handle %lu does not match session %u",
437 mp->listener_handle, ls_index);
438 goto error;
439 }
440
Florin Corasc547e912020-12-08 17:50:45 -0800441 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800442 mp->server_tx_fifo,
443 mp->vpp_event_queue_address, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700444 {
Florin Corasc547e912020-12-08 17:50:45 -0800445 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700446 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700447 }
448
Florin Coras54693d22018-07-17 10:46:29 -0700449 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700450 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700451 session->transport.rmt_port = mp->rmt.port;
452 session->transport.is_ip4 = mp->rmt.is_ip4;
453 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500454 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700455
Florin Coras134a9962018-08-28 11:32:04 -0700456 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700457 session->transport.lcl_port = listen_session->transport.lcl_port;
458 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700459 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200460 session->is_dgram = vcl_proto_is_dgram (session->session_type);
461 session->listener_index = listen_session->session_index;
462 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700463
Florin Coras5e062572019-03-14 19:07:51 -0700464 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
465 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700466 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
467 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
468 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700469 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
470
Florin Coras00cca802019-06-06 09:38:44 -0700471 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
472 session->vpp_handle, 0);
473
Florin Coras134a9962018-08-28 11:32:04 -0700474 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700475
476error:
Florin Corasb4624182020-12-11 13:58:12 -0800477 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
478 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700479 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
480 VNET_API_ERROR_INVALID_ARGUMENT);
481 vcl_session_free (wrk, session);
482 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700483}
484
485static u32
Florin Coras134a9962018-08-28 11:32:04 -0700486vcl_session_connected_handler (vcl_worker_t * wrk,
487 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700488{
Florin Coras99368312018-08-02 10:45:44 -0700489 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800490 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700491
492 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700493 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700494 if (!session)
495 {
Florin Coras5e062572019-03-14 19:07:51 -0700496 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
497 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700498 return VCL_INVALID_SESSION_INDEX;
499 }
Florin Coras54693d22018-07-17 10:46:29 -0700500 if (mp->retval)
501 {
Florin Coras5e062572019-03-14 19:07:51 -0700502 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700503 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700504 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700505 session->vpp_handle = mp->handle;
506 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700507 }
508
Florin Corasdbc9c592019-09-25 16:37:43 -0700509 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800510
511 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800512 mp->server_tx_fifo,
513 mp->vpp_event_queue_address, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800514 {
Florin Corasc547e912020-12-08 17:50:45 -0800515 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700516 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700517 vcl_send_session_disconnect (wrk, session);
518 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800519 }
520
Florin Coras653e43f2019-03-04 10:56:23 -0800521 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700522 {
Florin Corasc547e912020-12-08 17:50:45 -0800523 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800524 mp->ct_tx_fifo, (uword) ~0, 1, session))
Florin Coras653e43f2019-03-04 10:56:23 -0800525 {
Florin Corasc547e912020-12-08 17:50:45 -0800526 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700527 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700528 vcl_send_session_disconnect (wrk, session);
529 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800530 }
Florin Coras99368312018-08-02 10:45:44 -0700531 }
Florin Coras54693d22018-07-17 10:46:29 -0700532
Florin Coras09d18c22019-04-24 11:10:02 -0700533 session->transport.is_ip4 = mp->lcl.is_ip4;
534 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500535 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700536 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700537
538 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700539 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700540 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700541 vcl_send_session_disconnect (wrk, session);
542 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700543 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700544
545 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800546 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700547
Florin Coras5e062572019-03-14 19:07:51 -0700548 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
549 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700550 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700551
Florin Coras54693d22018-07-17 10:46:29 -0700552 return session_index;
553}
554
Florin Coras3c7d4f92018-12-14 11:28:43 -0800555static int
556vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
557{
558 vcl_session_msg_t *accepted_msg;
559 int i;
560
561 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
562 {
563 accepted_msg = &session->accept_evts_fifo[i];
564 if (accepted_msg->accepted_msg.handle == handle)
565 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800566 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800567 return 1;
568 }
569 }
570 return 0;
571}
572
Florin Corasc9fbd662018-08-24 12:59:56 -0700573static u32
Florin Coras134a9962018-08-28 11:32:04 -0700574vcl_session_reset_handler (vcl_worker_t * wrk,
575 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700576{
577 vcl_session_t *session;
578 u32 sid;
579
Florin Coras134a9962018-08-28 11:32:04 -0700580 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
581 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700582 if (!session)
583 {
584 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
585 return VCL_INVALID_SESSION_INDEX;
586 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800587
588 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700589 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800590 {
591
592 if (!vcl_flag_accepted_session (session, reset_msg->handle,
593 VCL_ACCEPTED_F_RESET))
594 VDBG (0, "session was not accepted!");
595 return VCL_INVALID_SESSION_INDEX;
596 }
597
Florin Corasc127d5a2020-10-14 16:35:58 -0700598 if (session->session_state != VCL_STATE_CLOSED)
599 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800600 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700601 return sid;
602}
603
Florin Coras60116992018-08-27 09:52:18 -0700604static u32
Florin Coras134a9962018-08-28 11:32:04 -0700605vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700606{
607 vcl_session_t *session;
608 u32 sid = mp->context;
609
Florin Coras134a9962018-08-28 11:32:04 -0700610 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700611 if (mp->retval)
612 {
Florin Coras5e062572019-03-14 19:07:51 -0700613 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700614 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700615 if (session)
616 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700617 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700618 session->vpp_handle = mp->handle;
619 return sid;
620 }
621 else
622 {
Florin Coras5e062572019-03-14 19:07:51 -0700623 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
624 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700625 return VCL_INVALID_SESSION_INDEX;
626 }
627 }
628
629 session->vpp_handle = mp->handle;
630 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500631 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
632 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700633 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700634 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700635 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800636
Florin Coras6e3c1f82020-01-15 01:30:46 +0000637 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700638 {
Florin Corasc547e912020-12-08 17:50:45 -0800639 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800640 mp->tx_fifo, mp->vpp_evt_q, 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800641 {
642 VDBG (0, "failed to attach fifos for %u", session->session_index);
643 session->session_state = VCL_STATE_DETACHED;
644 return VCL_INVALID_SESSION_INDEX;
645 }
Florin Coras60116992018-08-27 09:52:18 -0700646 }
647
Florin Coras05ecfcc2018-12-12 18:19:39 -0800648 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700649 return sid;
650}
651
Florin Corasdfae9f92019-02-20 19:48:31 -0800652static void
653vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
654{
655 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
656 vcl_session_t *s;
657
658 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000659 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800660 {
661 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
662 return;
663 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700664 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000665 {
666 /* Connected udp listener */
667 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700668 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000669 return;
670
671 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
672 return;
673 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800674
675 if (mp->retval)
676 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700677 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800678
679 if (mp->context != wrk->wrk_index)
680 VDBG (0, "wrong context");
681
682 vcl_session_table_del_vpp_handle (wrk, mp->handle);
683 vcl_session_free (wrk, s);
684}
685
Florin Coras68b7e582020-01-21 18:33:23 -0800686static void
687vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
688{
689 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
690 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800691 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800692
693 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
694 if (!s)
695 {
696 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
697 return;
698 }
699
Florin Coras1bb74942021-02-10 15:26:37 -0800700 /* Only validate if a value is provided */
701 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800702 {
Florin Coras1bb74942021-02-10 15:26:37 -0800703 fs_index = vcl_segment_table_lookup (mp->segment_handle);
704 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
705 {
706 VDBG (0, "segment %lx for session %u is not mounted!",
707 mp->segment_handle, s->session_index);
708 s->session_state = VCL_STATE_DETACHED;
709 return;
710 }
Florin Corasc547e912020-12-08 17:50:45 -0800711 }
712
Florin Coras57660d92020-04-04 22:45:34 +0000713 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800714
715 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
716 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800717
Florin Coras68b7e582020-01-21 18:33:23 -0800718 vcl_session_table_del_vpp_handle (wrk, mp->handle);
719 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
720
721 /* Generate new tx event if we have outstanding data */
722 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800723 app_send_io_evt_to_vpp (s->vpp_evt_q,
724 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800725 SESSION_IO_EVT_TX, SVM_Q_WAIT);
726
Florin Coras57660d92020-04-04 22:45:34 +0000727 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800728 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800729}
730
Florin Coras3c7d4f92018-12-14 11:28:43 -0800731static vcl_session_t *
732vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
733{
734 vcl_session_msg_t *vcl_msg;
735 vcl_session_t *session;
736
737 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
738 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800739 VWRN ("session overlap handle %lu state %u!", msg->handle,
740 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800741
742 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
743 if (!session)
744 {
745 VERR ("couldn't find listen session: listener handle %llx",
746 msg->listener_handle);
747 return 0;
748 }
749
750 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000751 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800752 vcl_msg->accepted_msg = *msg;
753 /* Session handle points to listener until fully accepted by app */
754 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
755
756 return session;
757}
758
759static vcl_session_t *
760vcl_session_disconnected_handler (vcl_worker_t * wrk,
761 session_disconnected_msg_t * msg)
762{
763 vcl_session_t *session;
764
765 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
766 if (!session)
767 {
768 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
769 return 0;
770 }
771
Florin Corasadcfb152020-02-12 08:50:29 +0000772 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700773 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000774 return 0;
775
Florin Coras3c7d4f92018-12-14 11:28:43 -0800776 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700777 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800778 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800779 if (!vcl_flag_accepted_session (session, msg->handle,
780 VCL_ACCEPTED_F_CLOSED))
781 VDBG (0, "session was not accepted!");
782 return 0;
783 }
784
Florin Corasadcfb152020-02-12 08:50:29 +0000785 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700786 if (session->session_state != VCL_STATE_DISCONNECT)
787 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000788
Florin Coras3c7d4f92018-12-14 11:28:43 -0800789 return session;
790}
791
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700792static int
793vppcom_session_disconnect (u32 session_handle)
794{
795 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700796 vcl_session_t *session, *listen_session;
797 vcl_session_state_t state;
798 u64 vpp_handle;
799
800 session = vcl_session_get_w_handle (wrk, session_handle);
801 if (!session)
802 return VPPCOM_EBADFD;
803
804 vpp_handle = session->vpp_handle;
805 state = session->session_state;
806
807 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
808 vpp_handle, state, vppcom_session_state_str (state));
809
810 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
811 {
812 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
813 return VPPCOM_EBADFD;
814 }
815
816 if (state == VCL_STATE_VPP_CLOSING)
817 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800818 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700819 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
820 session->session_index, vpp_handle);
821 }
822 else
823 {
824 /* Session doesn't have an event queue yet. Probably a non-blocking
825 * connect. Wait for the reply */
826 if (PREDICT_FALSE (!session->vpp_evt_q))
827 return VPPCOM_OK;
828
829 VDBG (1, "session %u [0x%llx]: sending disconnect...",
830 session->session_index, vpp_handle);
831 vcl_send_session_disconnect (wrk, session);
832 }
833
834 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
835 {
836 listen_session = vcl_session_get (wrk, session->listener_index);
837 listen_session->n_accepted_sessions--;
838 }
839
840 return VPPCOM_OK;
841}
842
Florin Coras30e79c22019-01-02 19:31:22 -0800843static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700844vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
845{
846 session_cleanup_msg_t *msg;
847 vcl_session_t *session;
848
849 msg = (session_cleanup_msg_t *) data;
850 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
851 if (!session)
852 {
853 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
854 return;
855 }
856
Florin Coras36d49392020-04-24 23:00:11 +0000857 if (msg->type == SESSION_CLEANUP_TRANSPORT)
858 {
859 /* Transport was cleaned up before we confirmed close. Probably the
860 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700861 * Confirm close to make sure everything is cleaned up.
862 * Move to undetermined state to ensure that the session is not
863 * removed before both vpp and the app cleanup.
864 * - If the app closes first, the session is moved to CLOSED state
865 * and the session cleanup notification from vpp removes the
866 * session.
867 * - If vpp cleans up the session first, the session is moved to
868 * DETACHED state lower and subsequently the close from the app
869 * frees the session
870 */
871 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700872 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700873 vppcom_session_disconnect (vcl_session_handle (session));
874 session->session_state = VCL_STATE_UPDATED;
875 }
876 else if (session->session_state == VCL_STATE_DISCONNECT)
877 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800878 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700879 session->session_state = VCL_STATE_UPDATED;
880 }
Florin Coras36d49392020-04-24 23:00:11 +0000881 return;
882 }
883
Florin Coras9ace36d2019-10-28 13:14:17 -0700884 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000885 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700886 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000887 {
888 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700889 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000890 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
891 return;
892 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700893 vcl_session_free (wrk, session);
894}
895
896static void
Florin Coras30e79c22019-01-02 19:31:22 -0800897vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
898{
899 session_req_worker_update_msg_t *msg;
900 vcl_session_t *s;
901
902 msg = (session_req_worker_update_msg_t *) data;
903 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
904 if (!s)
905 return;
906
907 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
908}
909
910static void
911vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
912{
913 session_worker_update_reply_msg_t *msg;
914 vcl_session_t *s;
915
916 msg = (session_worker_update_reply_msg_t *) data;
917 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
918 if (!s)
919 {
920 VDBG (0, "unknown handle 0x%llx", msg->handle);
921 return;
922 }
Florin Coras30e79c22019-01-02 19:31:22 -0800923
Florin Coras5f45e012019-01-23 09:21:30 -0800924 if (s->rx_fifo)
925 {
Florin Corasc547e912020-12-08 17:50:45 -0800926 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800927 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800928 {
929 VDBG (0, "failed to attach fifos for %u", s->session_index);
930 return;
931 }
Florin Coras5f45e012019-01-23 09:21:30 -0800932 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700933 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800934
Florin Coras30e79c22019-01-02 19:31:22 -0800935 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
936 s->vpp_handle, wrk->wrk_index);
937}
938
Florin Coras935ce752020-09-08 22:43:47 -0700939static int
940vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
941{
942
943 if (vcm->cfg.vpp_app_socket_api)
944 return vcl_sapi_recv_fds (wrk, fds, n_fds);
945
946 return vcl_bapi_recv_fds (wrk, fds, n_fds);
947}
948
Florin Corasc4c4cf52019-08-24 18:17:34 -0700949static void
950vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
951{
952 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
953 session_app_add_segment_msg_t *msg;
954 u64 segment_handle;
955 int fd = -1;
956
957 msg = (session_app_add_segment_msg_t *) data;
958
959 if (msg->fd_flags)
960 {
Florin Coras935ce752020-09-08 22:43:47 -0700961 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700962 seg_type = SSVM_SEGMENT_MEMFD;
963 }
964
965 segment_handle = msg->segment_handle;
966 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
967 {
968 clib_warning ("invalid segment handle");
969 return;
970 }
971
972 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
973 seg_type, fd))
974 {
975 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
976 return;
977 }
978
979 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
980 msg->segment_size);
981}
982
983static void
984vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
985{
986 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
987 vcl_segment_detach (msg->segment_handle);
988 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
989}
990
Florin Coras40c07ce2020-07-16 20:46:17 -0700991static void
992vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
993{
994 if (!vcm->wrk_rpc_fn)
995 return;
996
hanlina3a48962020-07-13 11:09:15 +0800997 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700998}
999
Florin Coras04ae8272021-04-12 19:55:37 -07001000static void
1001vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
1002{
1003 session_transport_attr_reply_msg_t *mp;
1004
1005 if (!wrk->session_attr_op)
1006 return;
1007
1008 mp = (session_transport_attr_reply_msg_t *) data;
1009
1010 wrk->session_attr_op_rv = mp->retval;
1011 wrk->session_attr_op = 0;
1012 wrk->session_attr_rv = mp->attr;
1013}
1014
Florin Coras86f04502018-09-12 16:08:01 -07001015static int
1016vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001017{
Florin Coras54693d22018-07-17 10:46:29 -07001018 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001019 session_connected_msg_t *connected_msg;
1020 session_reset_msg_t *reset_msg;
1021 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001022 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001023 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001024
1025 switch (e->event_type)
1026 {
Florin Coras653e43f2019-03-04 10:56:23 -08001027 case SESSION_IO_EVT_RX:
1028 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001029 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001030 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001031 break;
Florin Coras86f04502018-09-12 16:08:01 -07001032 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001033 break;
Florin Corasb242d312020-10-26 15:35:40 -07001034 case SESSION_CTRL_EVT_BOUND:
1035 /* We can only wait for only one listen so not postponed */
1036 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1037 break;
Florin Coras54693d22018-07-17 10:46:29 -07001038 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001039 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1040 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1041 {
1042 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1043 *ecpy = *e;
1044 ecpy->postponed = 1;
1045 ecpy->session_index = s->session_index;
1046 }
Florin Coras54693d22018-07-17 10:46:29 -07001047 break;
1048 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001049 connected_msg = (session_connected_msg_t *) e->data;
1050 sid = vcl_session_connected_handler (wrk, connected_msg);
1051 if (!(s = vcl_session_get (wrk, sid)))
1052 break;
1053 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1054 {
1055 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1056 *ecpy = *e;
1057 ecpy->postponed = 1;
1058 ecpy->session_index = s->session_index;
1059 }
Florin Coras54693d22018-07-17 10:46:29 -07001060 break;
1061 case SESSION_CTRL_EVT_DISCONNECTED:
1062 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001063 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1064 break;
1065 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1066 {
1067 vec_add1 (wrk->unhandled_evts_vector, *e);
1068 break;
1069 }
1070 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001071 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001072 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1073 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001074 break;
1075 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001076 reset_msg = (session_reset_msg_t *) e->data;
1077 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1078 break;
1079 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1080 {
1081 vec_add1 (wrk->unhandled_evts_vector, *e);
1082 break;
1083 }
Florin Coras134a9962018-08-28 11:32:04 -07001084 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001085 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001086 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1087 vcl_session_unlisten_reply_handler (wrk, e->data);
1088 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001089 case SESSION_CTRL_EVT_MIGRATED:
1090 vcl_session_migrated_handler (wrk, e->data);
1091 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001092 case SESSION_CTRL_EVT_CLEANUP:
1093 vcl_session_cleanup_handler (wrk, e->data);
1094 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001095 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1096 vcl_session_req_worker_update_handler (wrk, e->data);
1097 break;
1098 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1099 vcl_session_worker_update_reply_handler (wrk, e->data);
1100 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001101 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1102 vcl_session_app_add_segment_handler (wrk, e->data);
1103 break;
1104 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1105 vcl_session_app_del_segment_handler (wrk, e->data);
1106 break;
hanlina3a48962020-07-13 11:09:15 +08001107 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001108 vcl_worker_rpc_handler (wrk, e->data);
1109 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001110 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1111 vcl_session_transport_attr_reply_handler (wrk, e->data);
1112 break;
Florin Coras54693d22018-07-17 10:46:29 -07001113 default:
1114 clib_warning ("unhandled %u", e->event_type);
1115 }
1116 return VPPCOM_OK;
1117}
1118
Florin Coras30e79c22019-01-02 19:31:22 -08001119static int
Florin Coras697faea2018-06-27 17:10:49 -07001120vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001121 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001122 f64 wait_for_time)
1123{
Florin Coras134a9962018-08-28 11:32:04 -07001124 vcl_worker_t *wrk = vcl_worker_get_current ();
1125 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001126 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001127 svm_msg_q_msg_t msg;
1128 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001129
Florin Coras697faea2018-06-27 17:10:49 -07001130 do
Dave Wallace543852a2017-08-03 02:11:34 -04001131 {
Florin Coras134a9962018-08-28 11:32:04 -07001132 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001133 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001134 {
Florin Coras070453d2018-08-24 17:04:27 -07001135 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001136 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001137 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001138 {
Florin Coras697faea2018-06-27 17:10:49 -07001139 return VPPCOM_OK;
1140 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001141 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001142 {
Florin Coras697faea2018-06-27 17:10:49 -07001143 return VPPCOM_ECONNREFUSED;
1144 }
Florin Coras54693d22018-07-17 10:46:29 -07001145
Florin Coras134a9962018-08-28 11:32:04 -07001146 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001147 {
1148 usleep (100);
1149 continue;
1150 }
Florin Coras134a9962018-08-28 11:32:04 -07001151 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001152 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001153 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001154 }
Florin Coras134a9962018-08-28 11:32:04 -07001155 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001156
Florin Coras05ecfcc2018-12-12 18:19:39 -08001157 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001158 vppcom_session_state_str (state));
1159 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001160
Florin Coras697faea2018-06-27 17:10:49 -07001161 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001162}
1163
Florin Coras30e79c22019-01-02 19:31:22 -08001164static void
1165vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1166{
Florin Coras288eaab2019-02-03 15:26:14 -08001167 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001168 vcl_session_t *s;
1169 u32 *sip;
1170
1171 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1172 return;
1173
1174 vec_foreach (sip, wrk->pending_session_wrk_updates)
1175 {
1176 s = vcl_session_get (wrk, *sip);
1177 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1178 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001179 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1180 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001181 s->session_state = state;
1182 }
1183 vec_reset_length (wrk->pending_session_wrk_updates);
1184}
1185
Florin Corasf9240dc2019-01-15 08:03:17 -08001186void
Florin Coras5398dfb2021-01-25 20:31:27 -08001187vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001188{
Florin Coras30e79c22019-01-02 19:31:22 -08001189 svm_msg_q_msg_t *msg;
1190 session_event_t *e;
1191 svm_msg_q_t *mq;
1192 int i;
1193
1194 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001195 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001196
1197 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1198 {
1199 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1200 e = svm_msg_q_msg_data (mq, msg);
1201 vcl_handle_mq_event (wrk, e);
1202 svm_msg_q_free_msg (mq, msg);
1203 }
1204 vec_reset_length (wrk->mq_msg_vector);
1205 vcl_handle_pending_wrk_updates (wrk);
1206}
1207
Florin Coras5398dfb2021-01-25 20:31:27 -08001208void
1209vcl_flush_mq_events (void)
1210{
1211 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1212}
1213
Florin Coras697faea2018-06-27 17:10:49 -07001214static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001215vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001216{
Florin Coras134a9962018-08-28 11:32:04 -07001217 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001218 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001219 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001220 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001221
Florin Corasab2f6db2018-08-31 14:31:41 -07001222 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001223 if (!session)
1224 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001225
Florin Corasaaff5ee2019-07-08 13:00:00 -07001226 /* Flush pending accept events, if any */
1227 while (clib_fifo_elts (session->accept_evts_fifo))
1228 {
1229 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1230 accepted_msg = &evt->accepted_msg;
1231 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1232 vcl_send_session_accepted_reply (session->vpp_evt_q,
1233 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001234 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001235 }
1236 clib_fifo_free (session->accept_evts_fifo);
1237
Florin Coras458089b2019-08-21 16:20:44 -07001238 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001239
Florin Coras5e062572019-03-14 19:07:51 -07001240 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001241 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001242 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001243
1244 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001245 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001246
Florin Coras070453d2018-08-24 17:04:27 -07001247 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001248}
1249
Florin Coras940f78f2018-11-30 12:11:20 -08001250/**
1251 * Handle app exit
1252 *
1253 * Notify vpp of the disconnect and mark the worker as free. If we're the
1254 * last worker, do a full cleanup otherwise, since we're probably a forked
1255 * child, avoid syscalls as much as possible. We might've lost privileges.
1256 */
1257void
1258vppcom_app_exit (void)
1259{
1260 if (!pool_elts (vcm->workers))
1261 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001262 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1263 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001264 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001265}
1266
Florin Coras935ce752020-09-08 22:43:47 -07001267static int
1268vcl_api_attach (void)
1269{
1270 if (vcm->cfg.vpp_app_socket_api)
1271 return vcl_sapi_attach ();
1272
1273 return vcl_bapi_attach ();
1274}
1275
1276static void
1277vcl_api_detach (vcl_worker_t * wrk)
1278{
1279 vcl_send_app_detach (wrk);
1280
1281 if (vcm->cfg.vpp_app_socket_api)
1282 return vcl_sapi_detach (wrk);
1283
1284 return vcl_bapi_disconnect_from_vpp ();
1285}
1286
Dave Wallace543852a2017-08-03 02:11:34 -04001287/*
1288 * VPPCOM Public API functions
1289 */
1290int
Florin Coras66ec4672020-06-15 07:59:40 -07001291vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001292{
Dave Wallace543852a2017-08-03 02:11:34 -04001293 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001294 int rv;
1295
Florin Coras47c40e22018-11-26 17:01:36 -08001296 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001297 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001298 VDBG (1, "already initialized");
1299 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001300 }
1301
Florin Coras47c40e22018-11-26 17:01:36 -08001302 vcm->is_init = 1;
1303 vppcom_cfg (&vcm->cfg);
1304 vcl_cfg = &vcm->cfg;
1305
1306 vcm->main_cpu = pthread_self ();
1307 vcm->main_pid = getpid ();
1308 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001309 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1310 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001311 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1312 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001313 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001314 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001315 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001316
1317 /* Allocate default worker */
1318 vcl_worker_alloc_and_init ();
1319
Florin Coras935ce752020-09-08 22:43:47 -07001320 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001321 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001322
1323 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001324 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001325
1326 return VPPCOM_OK;
1327}
1328
1329void
1330vppcom_app_destroy (void)
1331{
Florin Corasdc0ded72020-04-30 02:59:55 +00001332 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001333 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001334
Florin Coras940f78f2018-11-30 12:11:20 -08001335 if (!pool_elts (vcm->workers))
1336 return;
1337
Florin Coras0d427d82018-06-27 03:24:07 -07001338 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001339
Florin Corasdc0ded72020-04-30 02:59:55 +00001340 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001341
Florin Corasce815de2020-04-16 18:47:27 +00001342 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001343 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001344 if (current_wrk != wrk)
1345 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001346 }
Florin Corasce815de2020-04-16 18:47:27 +00001347 /* *INDENT-ON* */
1348
Florin Coras935ce752020-09-08 22:43:47 -07001349 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001350 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1351
Florin Coras0d427d82018-06-27 03:24:07 -07001352 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001353
1354 /*
1355 * Free the heap and fix vcm
1356 */
1357 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001358 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001359
1360 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001361 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001362}
1363
1364int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001365vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001366{
Florin Coras134a9962018-08-28 11:32:04 -07001367 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001368 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001369
Florin Coras134a9962018-08-28 11:32:04 -07001370 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001371
Florin Coras7e12d942018-06-27 14:32:43 -07001372 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001373 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001374 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001375 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001376
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001377 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001378 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001379
Florin Coras7e12d942018-06-27 14:32:43 -07001380 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1381 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001382
Florin Coras5e062572019-03-14 19:07:51 -07001383 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001384
Florin Coras134a9962018-08-28 11:32:04 -07001385 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001386}
1387
1388int
Florin Corasc127d5a2020-10-14 16:35:58 -07001389vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001390 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001391{
Florin Coras134a9962018-08-28 11:32:04 -07001392 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001393
Florin Coras6c3b2182020-10-19 18:36:48 -07001394 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001395
Florin Coras6c3b2182020-10-19 18:36:48 -07001396 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001397 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001398 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001399 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001400 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001401 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001402 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001403 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001404 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1405 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001406 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001407 }
Florin Corase4306b62020-10-28 12:51:10 -07001408 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001409 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001410
Florin Coras6c3b2182020-10-19 18:36:48 -07001411 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001412 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001413 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001414 if (rv < 0)
1415 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001416 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1417 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001418 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001419
Florin Coras9ace36d2019-10-28 13:14:17 -07001420 if (!do_disconnect)
1421 {
1422 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001423 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001424 goto cleanup;
1425 }
1426
Florin Coras6c3b2182020-10-19 18:36:48 -07001427 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001428 {
1429 rv = vppcom_session_unbind (sh);
1430 if (PREDICT_FALSE (rv < 0))
1431 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001432 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001433 vppcom_retval_str (rv));
1434 return rv;
1435 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001436 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001437 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001438 {
1439 rv = vppcom_session_disconnect (sh);
1440 if (PREDICT_FALSE (rv < 0))
1441 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001442 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001443 rv, vppcom_retval_str (rv));
1444 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001445 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001446 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001447 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001448 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001449 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001450 {
1451 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001452 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001453 goto free_session;
1454 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001455
Florin Corasc127d5a2020-10-14 16:35:58 -07001456 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001457
Florin Coras9ace36d2019-10-28 13:14:17 -07001458 /* Session is removed only after vpp confirms the disconnect */
1459 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001460
Florin Corasf9240dc2019-01-15 08:03:17 -08001461cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001462 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001463free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001464 vcl_session_free (wrk, s);
1465 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001466
Dave Wallace543852a2017-08-03 02:11:34 -04001467 return rv;
1468}
1469
1470int
Florin Corasf9240dc2019-01-15 08:03:17 -08001471vppcom_session_close (uint32_t session_handle)
1472{
1473 vcl_worker_t *wrk = vcl_worker_get_current ();
1474 vcl_session_t *session;
1475
1476 session = vcl_session_get_w_handle (wrk, session_handle);
1477 if (!session)
1478 return VPPCOM_EBADFD;
1479 return vcl_session_cleanup (wrk, session, session_handle,
1480 1 /* do_disconnect */ );
1481}
1482
1483int
Florin Coras134a9962018-08-28 11:32:04 -07001484vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001485{
Florin Coras134a9962018-08-28 11:32:04 -07001486 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001487 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001488
1489 if (!ep || !ep->ip)
1490 return VPPCOM_EINVAL;
1491
Florin Coras134a9962018-08-28 11:32:04 -07001492 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001493 if (!session)
1494 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001495
Florin Coras6c3b2182020-10-19 18:36:48 -07001496 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001497 {
Florin Coras5e062572019-03-14 19:07:51 -07001498 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1499 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001500 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001501 }
1502
Florin Coras7e12d942018-06-27 14:32:43 -07001503 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001504 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001505 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1506 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001507 else
Dave Barach178cf492018-11-13 16:34:13 -05001508 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1509 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001510 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001511
Florin Coras5e062572019-03-14 19:07:51 -07001512 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1513 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001514 session->transport.is_ip4 ? "IPv4" : "IPv6",
1515 format_ip46_address, &session->transport.lcl_ip,
1516 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1517 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001518 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001519 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001520
1521 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001522 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001523
Florin Coras070453d2018-08-24 17:04:27 -07001524 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001525}
1526
1527int
Florin Coras134a9962018-08-28 11:32:04 -07001528vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001529{
Florin Coras134a9962018-08-28 11:32:04 -07001530 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001531 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001532 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001533 int rv;
1534
Florin Coras134a9962018-08-28 11:32:04 -07001535 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001536 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001537 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001538
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001539 if (q_len == 0 || q_len == ~0)
1540 q_len = vcm->cfg.listen_queue_size;
1541
Dave Wallaceee45d412017-11-24 21:44:06 -05001542 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001543 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001544 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001545 VDBG (0, "session %u [0x%llx]: already in listen state!",
1546 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001547 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001548 }
1549
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001550 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001551
Florin Coras070453d2018-08-24 17:04:27 -07001552 /*
1553 * Send listen request to vpp and wait for reply
1554 */
Florin Coras458089b2019-08-21 16:20:44 -07001555 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001556 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001557 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001558 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001559
Florin Coras070453d2018-08-24 17:04:27 -07001560 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001561 {
Florin Coras134a9962018-08-28 11:32:04 -07001562 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001563 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1564 listen_sh, listen_session->vpp_handle, rv,
1565 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001566 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001567 }
1568
Florin Coras070453d2018-08-24 17:04:27 -07001569 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001570}
1571
Florin Coras134a9962018-08-28 11:32:04 -07001572static int
Florin Coras5e062572019-03-14 19:07:51 -07001573validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001574{
Florin Coras6c3b2182020-10-19 18:36:48 -07001575 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001576 {
Florin Coras5e062572019-03-14 19:07:51 -07001577 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1578 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001579 return VPPCOM_EBADFD;
1580 }
1581
Florin Corasc127d5a2020-10-14 16:35:58 -07001582 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001583 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001584 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001585 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001586 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001587 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001588 return VPPCOM_EBADFD;
1589 }
1590 return VPPCOM_OK;
1591}
1592
1593int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001594vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1595{
1596 if (!strcmp (proto_str, "TCP"))
1597 *proto = VPPCOM_PROTO_TCP;
1598 else if (!strcmp (proto_str, "tcp"))
1599 *proto = VPPCOM_PROTO_TCP;
1600 else if (!strcmp (proto_str, "UDP"))
1601 *proto = VPPCOM_PROTO_UDP;
1602 else if (!strcmp (proto_str, "udp"))
1603 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001604 else if (!strcmp (proto_str, "TLS"))
1605 *proto = VPPCOM_PROTO_TLS;
1606 else if (!strcmp (proto_str, "tls"))
1607 *proto = VPPCOM_PROTO_TLS;
1608 else if (!strcmp (proto_str, "QUIC"))
1609 *proto = VPPCOM_PROTO_QUIC;
1610 else if (!strcmp (proto_str, "quic"))
1611 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001612 else if (!strcmp (proto_str, "DTLS"))
1613 *proto = VPPCOM_PROTO_DTLS;
1614 else if (!strcmp (proto_str, "dtls"))
1615 *proto = VPPCOM_PROTO_DTLS;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001616 else
1617 return 1;
1618 return 0;
1619}
1620
1621int
Florin Coras134a9962018-08-28 11:32:04 -07001622vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001623 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001624{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001625 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001626 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001627 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001628 vcl_session_t *listen_session = 0;
1629 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001630 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001631 u8 is_nonblocking;
1632 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001633
Florin Coras5398dfb2021-01-25 20:31:27 -08001634again:
1635
Florin Coras134a9962018-08-28 11:32:04 -07001636 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001637 if (!listen_session)
1638 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001639
Florin Coras134a9962018-08-28 11:32:04 -07001640 listen_session_index = listen_session->session_index;
1641 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001642 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001643
Florin Coras54693d22018-07-17 10:46:29 -07001644 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001645 {
Florin Coras54693d22018-07-17 10:46:29 -07001646 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001647 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001648 accepted_msg = evt->accepted_msg;
1649 goto handle;
1650 }
1651
Florin Corasac422d62020-10-19 20:51:36 -07001652 is_nonblocking = vcl_session_has_attr (listen_session,
1653 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001654 while (1)
1655 {
Carl Smith592a9092019-11-12 14:57:37 +13001656 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1657 return VPPCOM_EAGAIN;
1658
Florin Coras5398dfb2021-01-25 20:31:27 -08001659 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1660 vcl_worker_flush_mq_events (wrk);
1661 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001662 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001663
Florin Coras54693d22018-07-17 10:46:29 -07001664handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001665
Florin Coras00cca802019-06-06 09:38:44 -07001666 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1667 listen_session_index);
1668 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1669 return VPPCOM_ECONNABORTED;
1670
Florin Coras134a9962018-08-28 11:32:04 -07001671 listen_session = vcl_session_get (wrk, listen_session_index);
1672 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001673
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001674 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001675 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001676
Florin Coras5e062572019-03-14 19:07:51 -07001677 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1678 " flags %d, is_nonblocking %u", listen_session->session_index,
1679 listen_session->vpp_handle, client_session_index,
1680 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001681 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001682
Dave Wallace048b1d62018-01-03 22:24:41 -05001683 if (ep)
1684 {
Florin Coras7e12d942018-06-27 14:32:43 -07001685 ep->is_ip4 = client_session->transport.is_ip4;
1686 ep->port = client_session->transport.rmt_port;
1687 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001688 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1689 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001690 else
Dave Barach178cf492018-11-13 16:34:13 -05001691 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1692 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001693 }
Dave Wallace60caa062017-11-10 17:07:13 -05001694
Florin Coras05ecfcc2018-12-12 18:19:39 -08001695 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001696 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001697 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001698 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001699 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001700 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001701 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001702 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001703 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001704 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1705 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001706
Florin Coras3c7d4f92018-12-14 11:28:43 -08001707 /*
1708 * Session might have been closed already
1709 */
1710 if (accept_flags)
1711 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001712 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001713 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001714 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001715 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001716 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001717 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001718}
1719
1720int
Florin Coras134a9962018-08-28 11:32:04 -07001721vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001722{
Florin Coras134a9962018-08-28 11:32:04 -07001723 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001724 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001725 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001726 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001727
Florin Coras134a9962018-08-28 11:32:04 -07001728 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001729 if (!session)
1730 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001731 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001732
Florin Coras6c3b2182020-10-19 18:36:48 -07001733 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001734 {
Florin Coras5e062572019-03-14 19:07:51 -07001735 VDBG (0, "ERROR: cannot connect epoll session %u!",
1736 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001737 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001738 }
1739
Florin Corasc127d5a2020-10-14 16:35:58 -07001740 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001741 {
Florin Coras7baeb712019-01-04 17:05:43 -08001742 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001743 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001744 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001745 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001746 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001747 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001748 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001749 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001750 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001751 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001752 }
1753
Florin Coras0a1e1832020-03-29 18:54:04 +00001754 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001755 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001756 {
1757 if (session->session_type != VPPCOM_PROTO_UDP)
1758 return VPPCOM_EINVAL;
1759 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001760 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001761 }
1762
Florin Coras7e12d942018-06-27 14:32:43 -07001763 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001764 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001765 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001766 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001767 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001768
Florin Coras0a1e1832020-03-29 18:54:04 +00001769 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001770 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001771 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001772 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001773 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001774 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001775 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001776 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001777 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001778
Florin Coras458089b2019-08-21 16:20:44 -07001779 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001780
Florin Corasac422d62020-10-19 20:51:36 -07001781 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001782 {
fanyfa49d59d2020-10-13 17:07:16 +08001783 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001784 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001785 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001786 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001787 return VPPCOM_EINPROGRESS;
1788 }
Florin Coras57c88932019-08-29 12:03:17 -07001789
1790 /*
1791 * Wait for reply from vpp if blocking
1792 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001793 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001794 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001795
Florin Coras134a9962018-08-28 11:32:04 -07001796 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001797 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1798 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001799
Dave Wallace4878cbe2017-11-21 03:45:09 -05001800 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001801}
1802
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001803int
1804vppcom_session_stream_connect (uint32_t session_handle,
1805 uint32_t parent_session_handle)
1806{
1807 vcl_worker_t *wrk = vcl_worker_get_current ();
1808 vcl_session_t *session, *parent_session;
1809 u32 session_index, parent_session_index;
1810 int rv;
1811
1812 session = vcl_session_get_w_handle (wrk, session_handle);
1813 if (!session)
1814 return VPPCOM_EBADFD;
1815 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1816 if (!parent_session)
1817 return VPPCOM_EBADFD;
1818
1819 session_index = session->session_index;
1820 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001821 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001822 {
1823 VDBG (0, "ERROR: cannot connect epoll session %u!",
1824 session->session_index);
1825 return VPPCOM_EBADFD;
1826 }
1827
Florin Corasc127d5a2020-10-14 16:35:58 -07001828 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001829 {
1830 VDBG (0, "session handle %u [0x%llx]: session already "
1831 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1832 session_handle, session->vpp_handle,
1833 parent_session_handle, parent_session->vpp_handle,
1834 vppcom_proto_str (session->session_type), session->session_state,
1835 vppcom_session_state_str (session->session_state));
1836 return VPPCOM_OK;
1837 }
1838
1839 /* Connect to quic session specifics */
1840 session->transport.is_ip4 = parent_session->transport.is_ip4;
1841 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1842 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001843 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001844
1845 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1846 session_handle, parent_session_handle, parent_session->vpp_handle);
1847
1848 /*
1849 * Send connect request and wait for reply from vpp
1850 */
Florin Coras458089b2019-08-21 16:20:44 -07001851 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001852 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001853 vcm->cfg.session_timeout);
1854
1855 session->listener_index = parent_session_index;
1856 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001857 if (parent_session)
1858 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001859
1860 session = vcl_session_get (wrk, session_index);
1861 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1862 session->vpp_handle, rv ? "failed" : "succeeded");
1863
1864 return rv;
1865}
1866
Steven58f464e2017-10-25 12:33:12 -07001867static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001868vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001869 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001870{
Florin Coras134a9962018-08-28 11:32:04 -07001871 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001872 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001873 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001874 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001875 session_event_t *e;
1876 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001877 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001878
Florin Coras070453d2018-08-24 17:04:27 -07001879 if (PREDICT_FALSE (!buf))
1880 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001881
Florin Coras134a9962018-08-28 11:32:04 -07001882 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001883 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001884 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001885
Florin Coras6d0106e2019-01-29 20:11:58 -08001886 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001887 {
Florin Coras5e062572019-03-14 19:07:51 -07001888 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001889 s->session_index, s->vpp_handle, s->session_state,
1890 vppcom_session_state_str (s->session_state));
1891 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001892 }
1893
Florin Corasac422d62020-10-19 20:51:36 -07001894 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001895 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001896 mq = wrk->app_event_queue;
1897 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001898 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001899
Sirshak Das28aa5392019-02-05 01:33:33 -06001900 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001901 {
Florin Coras54693d22018-07-17 10:46:29 -07001902 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001903 {
Yu Pingb2955352019-12-04 06:49:04 +08001904 if (vcl_session_is_closing (s))
1905 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001906 if (is_ct)
1907 svm_fifo_unset_event (s->rx_fifo);
1908 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001909 return VPPCOM_EWOULDBLOCK;
1910 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001911 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001912 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001913 if (vcl_session_is_closing (s))
1914 return vcl_session_closing_error (s);
1915
Florin Corasd6894562020-10-28 00:37:15 -07001916 if (is_ct)
1917 svm_fifo_unset_event (s->rx_fifo);
1918 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001919
Florin Coras5398dfb2021-01-25 20:31:27 -08001920 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1921 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001922 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001923 }
Florin Coras54693d22018-07-17 10:46:29 -07001924
Florin Coras4a2c7942020-09-10 12:27:14 -07001925read_again:
1926
Florin Coras460dce62018-07-27 05:45:06 -07001927 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001928 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001929 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001930 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1931
1932 ASSERT (rv >= 0);
1933 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001934
Sirshak Das28aa5392019-02-05 01:33:33 -06001935 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001936 {
Florin Corasd6894562020-10-28 00:37:15 -07001937 if (is_ct)
1938 svm_fifo_unset_event (s->rx_fifo);
1939 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001940 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001941 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001942 {
Florin Corasc34118b2020-08-12 18:58:25 -07001943 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1944 e->event_type = SESSION_IO_EVT_RX;
1945 e->session_index = s->session_index;
1946 }
1947 }
Florin Coras54fe32c2020-11-25 20:26:32 -08001948 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07001949 {
1950 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08001951 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07001952 buf += rv;
1953 n -= rv;
1954 goto read_again;
1955 }
Florin Coras41c9e042018-09-11 00:10:41 -07001956
Florin Coras17ec5772020-08-12 20:46:29 -07001957 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001958 {
Florin Coras17ec5772020-08-12 20:46:29 -07001959 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08001960 app_send_io_evt_to_vpp (s->vpp_evt_q,
1961 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07001962 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001963 }
1964
Florin Coras5e062572019-03-14 19:07:51 -07001965 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1966 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001967
Florin Coras54693d22018-07-17 10:46:29 -07001968 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001969}
1970
Steven58f464e2017-10-25 12:33:12 -07001971int
Florin Coras134a9962018-08-28 11:32:04 -07001972vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001973{
Florin Coras134a9962018-08-28 11:32:04 -07001974 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001975}
1976
1977static int
Florin Coras134a9962018-08-28 11:32:04 -07001978vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001979{
Florin Coras134a9962018-08-28 11:32:04 -07001980 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001981}
1982
Florin Coras2cba8532018-09-11 16:33:36 -07001983int
1984vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001985 vppcom_data_segment_t * ds, uint32_t n_segments,
1986 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001987{
1988 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001989 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001990 vcl_session_t *s = 0;
1991 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07001992 svm_msg_q_t *mq;
1993 u8 is_ct;
1994
1995 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001996 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001997 return VPPCOM_EBADFD;
1998
Florin Coras6d0106e2019-01-29 20:11:58 -08001999 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2000 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002001
Florin Corasac422d62020-10-19 20:51:36 -07002002 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002003 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002004 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002005 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002006 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002007
Sirshak Das28aa5392019-02-05 01:33:33 -06002008 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002009 {
2010 if (is_nonblocking)
2011 {
Florin Coras62877022020-10-28 21:22:04 -07002012 if (is_ct)
2013 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002014 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002015 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002016 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002017 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002018 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002019 if (vcl_session_is_closing (s))
2020 return vcl_session_closing_error (s);
2021
Florin Coras62877022020-10-28 21:22:04 -07002022 if (is_ct)
2023 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002024 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002025
Florin Coras5398dfb2021-01-25 20:31:27 -08002026 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2027 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002028 }
2029 }
2030
Florin Corasd1cc38d2020-10-11 11:05:04 -07002031 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2032 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2033 if (n_read < 0)
2034 return VPPCOM_EAGAIN;
2035
2036 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2037 {
Florin Coras62877022020-10-28 21:22:04 -07002038 if (is_ct)
2039 svm_fifo_unset_event (s->rx_fifo);
2040 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002041 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002042 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002043 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002044 {
2045 session_event_t *e;
2046 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2047 e->event_type = SESSION_IO_EVT_RX;
2048 e->session_index = s->session_index;
2049 }
2050 }
2051
2052 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002053 return n_read;
2054}
2055
2056void
Florin Corasd68faf82020-09-25 15:18:13 -07002057vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002058{
2059 vcl_worker_t *wrk = vcl_worker_get_current ();
2060 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002061 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002062
2063 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002064 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002065 return;
2066
Florin Coras62877022020-10-28 21:22:04 -07002067 is_ct = vcl_session_is_ct (s);
2068 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002069
2070 ASSERT (s->rx_bytes_pending < n_bytes);
2071 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002072}
2073
Florin Coras7a2abce2020-04-05 19:25:44 +00002074always_inline u8
2075vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002076{
Florin Coras7a2abce2020-04-05 19:25:44 +00002077 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2078 if (is_dgram)
2079 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2080 else
2081 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002082}
2083
2084always_inline int
2085vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2086 size_t n, u8 is_flush, u8 is_dgram)
2087{
Florin Coras6d0106e2019-01-29 20:11:58 -08002088 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002089 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002090 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002091 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002092 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002093
jiangxiaomingff31ac62019-11-21 23:34:56 +08002094 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002095 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002096
Florin Coras6c3b2182020-10-19 18:36:48 -07002097 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002098 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002099 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2100 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002101 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002102 }
2103
Florin Coras6d0106e2019-01-29 20:11:58 -08002104 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002105 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002106 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2107 s->session_index, s->vpp_handle, s->session_state,
2108 vppcom_session_state_str (s->session_state));
2109 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002110 }
2111
Florin Coras0e88e852018-09-17 22:09:02 -07002112 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002113 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002114 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002115
Florin Coras653e43f2019-03-04 10:56:23 -08002116 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002117 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002118 {
Florin Coras54693d22018-07-17 10:46:29 -07002119 if (is_nonblocking)
2120 {
Florin Coras070453d2018-08-24 17:04:27 -07002121 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002122 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002123 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002124 {
Florin Coras2d379d82019-06-28 12:45:12 -07002125 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002126 if (vcl_session_is_closing (s))
2127 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002128
Florin Coras5398dfb2021-01-25 20:31:27 -08002129 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2130 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002131 }
Dave Wallace543852a2017-08-03 02:11:34 -04002132 }
Dave Wallace543852a2017-08-03 02:11:34 -04002133
Florin Coras653e43f2019-03-04 10:56:23 -08002134 et = SESSION_IO_EVT_TX;
2135 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002136 et = SESSION_IO_EVT_TX_FLUSH;
2137
Florin Coras7a2abce2020-04-05 19:25:44 +00002138 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002139 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002140 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002141 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002142 else
2143 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002144 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002145
Florin Coras14ed6df2019-03-06 21:13:42 -08002146 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002147 app_send_io_evt_to_vpp (
2148 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002149
Florin Coras56230092020-09-02 20:52:58 -07002150 /* The underlying fifo segment can run out of memory */
2151 if (PREDICT_FALSE (n_write < 0))
2152 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002153
Florin Coras6d0106e2019-01-29 20:11:58 -08002154 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2155 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002156
Florin Coras54693d22018-07-17 10:46:29 -07002157 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002158}
2159
Florin Coras42ceddb2018-12-12 10:56:01 -08002160int
2161vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2162{
Florin Coras7a2abce2020-04-05 19:25:44 +00002163 vcl_worker_t *wrk = vcl_worker_get_current ();
2164 vcl_session_t *s;
2165
2166 s = vcl_session_get_w_handle (wrk, session_handle);
2167 if (PREDICT_FALSE (!s))
2168 return VPPCOM_EBADFD;
2169
2170 return vppcom_session_write_inline (wrk, s, buf, n,
2171 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002172}
2173
Florin Corasb0f662f2018-12-27 14:51:46 -08002174int
2175vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2176{
Florin Coras7a2abce2020-04-05 19:25:44 +00002177 vcl_worker_t *wrk = vcl_worker_get_current ();
2178 vcl_session_t *s;
2179
2180 s = vcl_session_get_w_handle (wrk, session_handle);
2181 if (PREDICT_FALSE (!s))
2182 return VPPCOM_EBADFD;
2183
2184 return vppcom_session_write_inline (wrk, s, buf, n,
2185 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002186}
2187
Florin Corasc0737e92019-03-04 14:19:39 -08002188#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002189if (PREDICT_FALSE (!_s->rx_fifo)) \
2190 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002191if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2192 { \
2193 if (!vcl_session_is_ct (_s)) \
2194 { \
2195 svm_fifo_unset_event (_s->rx_fifo); \
2196 if (svm_fifo_is_empty (_s->rx_fifo)) \
2197 break; \
2198 } \
2199 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2200 { \
Florin Coras2f630182020-08-13 00:17:51 -07002201 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002202 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2203 break; \
2204 } \
2205 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002206
Florin Coras86f04502018-09-12 16:08:01 -07002207static void
2208vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2209 unsigned long n_bits, unsigned long *read_map,
2210 unsigned long *write_map,
2211 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002212{
2213 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002214 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002215 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002216 u32 sid;
2217
2218 switch (e->event_type)
2219 {
Florin Corasc0737e92019-03-04 14:19:39 -08002220 case SESSION_IO_EVT_RX:
2221 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002222 s = vcl_session_get (wrk, sid);
2223 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002224 break;
Florin Corasb242d312020-10-26 15:35:40 -07002225 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002226 if (sid < n_bits && read_map)
2227 {
David Johnsond9818dd2018-12-14 14:53:41 -05002228 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002229 *bits_set += 1;
2230 }
2231 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002232 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002233 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002234 s = vcl_session_get (wrk, sid);
2235 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002236 break;
2237 if (sid < n_bits && write_map)
2238 {
David Johnsond9818dd2018-12-14 14:53:41 -05002239 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002240 *bits_set += 1;
2241 }
2242 break;
Florin Coras86f04502018-09-12 16:08:01 -07002243 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002244 if (!e->postponed)
2245 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2246 else
2247 s = vcl_session_get (wrk, e->session_index);
2248 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002249 break;
Florin Corasb242d312020-10-26 15:35:40 -07002250 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002251 if (sid < n_bits && read_map)
2252 {
David Johnsond9818dd2018-12-14 14:53:41 -05002253 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002254 *bits_set += 1;
2255 }
2256 break;
2257 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002258 if (!e->postponed)
2259 {
2260 connected_msg = (session_connected_msg_t *) e->data;
2261 sid = vcl_session_connected_handler (wrk, connected_msg);
2262 }
2263 else
2264 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002265 if (sid == VCL_INVALID_SESSION_INDEX)
2266 break;
2267 if (sid < n_bits && write_map)
2268 {
2269 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2270 *bits_set += 1;
2271 }
Florin Coras86f04502018-09-12 16:08:01 -07002272 break;
2273 case SESSION_CTRL_EVT_DISCONNECTED:
2274 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002275 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2276 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002277 break;
Florin Corasb242d312020-10-26 15:35:40 -07002278 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002279 if (sid < n_bits && except_map)
2280 {
David Johnsond9818dd2018-12-14 14:53:41 -05002281 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002282 *bits_set += 1;
2283 }
2284 break;
2285 case SESSION_CTRL_EVT_RESET:
2286 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2287 if (sid < n_bits && except_map)
2288 {
David Johnsond9818dd2018-12-14 14:53:41 -05002289 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002290 *bits_set += 1;
2291 }
2292 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002293 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2294 vcl_session_unlisten_reply_handler (wrk, e->data);
2295 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002296 case SESSION_CTRL_EVT_MIGRATED:
2297 vcl_session_migrated_handler (wrk, e->data);
2298 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002299 case SESSION_CTRL_EVT_CLEANUP:
2300 vcl_session_cleanup_handler (wrk, e->data);
2301 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002302 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2303 vcl_session_worker_update_reply_handler (wrk, e->data);
2304 break;
2305 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2306 vcl_session_req_worker_update_handler (wrk, e->data);
2307 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002308 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2309 vcl_session_app_add_segment_handler (wrk, e->data);
2310 break;
2311 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2312 vcl_session_app_del_segment_handler (wrk, e->data);
2313 break;
hanlina3a48962020-07-13 11:09:15 +08002314 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002315 vcl_worker_rpc_handler (wrk, e->data);
2316 break;
Florin Coras86f04502018-09-12 16:08:01 -07002317 default:
2318 clib_warning ("unhandled: %u", e->event_type);
2319 break;
2320 }
2321}
2322
2323static int
2324vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2325 unsigned long n_bits, unsigned long *read_map,
2326 unsigned long *write_map, unsigned long *except_map,
2327 double time_to_wait, u32 * bits_set)
2328{
Florin Coras99368312018-08-02 10:45:44 -07002329 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002330 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002331 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002332
Florin Coras54693d22018-07-17 10:46:29 -07002333 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002334 {
Florin Coras54693d22018-07-17 10:46:29 -07002335 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002336 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002337
Florin Coras54693d22018-07-17 10:46:29 -07002338 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002339 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002340 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002341 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002342 else
2343 {
2344 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002345 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002346 }
2347 }
Florin Corase003a1b2019-06-05 10:47:16 -07002348 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002349
Florin Coras134a9962018-08-28 11:32:04 -07002350 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002351 {
Florin Coras134a9962018-08-28 11:32:04 -07002352 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002353 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002354 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2355 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002356 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002357 }
Florin Coras134a9962018-08-28 11:32:04 -07002358 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002359 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002360 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002361}
2362
Florin Coras99368312018-08-02 10:45:44 -07002363static int
Florin Coras294afe22019-01-07 17:49:17 -08002364vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2365 vcl_si_set * read_map, vcl_si_set * write_map,
2366 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002367 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002368{
Florin Coras14ed6df2019-03-06 21:13:42 -08002369 double wait = 0, start = 0;
2370
2371 if (!*bits_set)
2372 {
2373 wait = time_to_wait;
2374 start = clib_time_now (&wrk->clib_time);
2375 }
2376
2377 do
2378 {
2379 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2380 write_map, except_map, wait, bits_set);
2381 if (*bits_set)
2382 return *bits_set;
2383 if (wait == -1)
2384 continue;
2385
2386 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2387 }
2388 while (wait > 0);
2389
2390 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002391}
2392
2393static int
Florin Coras294afe22019-01-07 17:49:17 -08002394vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2395 vcl_si_set * read_map, vcl_si_set * write_map,
2396 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002397 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002398{
2399 vcl_mq_evt_conn_t *mqc;
2400 int __clib_unused n_read;
2401 int n_mq_evts, i;
2402 u64 buf;
2403
Florin Coras134a9962018-08-28 11:32:04 -07002404 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2405 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2406 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002407 for (i = 0; i < n_mq_evts; i++)
2408 {
Florin Coras134a9962018-08-28 11:32:04 -07002409 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002410 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002411 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002412 except_map, 0, bits_set);
2413 }
2414
2415 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2416}
2417
Dave Wallace543852a2017-08-03 02:11:34 -04002418int
Florin Coras294afe22019-01-07 17:49:17 -08002419vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2420 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002421{
Florin Coras54693d22018-07-17 10:46:29 -07002422 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002423 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002424 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002425 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002426
Dave Wallace7876d392017-10-19 03:53:57 -04002427 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002428 {
Florin Coras134a9962018-08-28 11:32:04 -07002429 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002430 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002431 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2432 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002433 }
Dave Wallace7876d392017-10-19 03:53:57 -04002434 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002435 {
Florin Coras134a9962018-08-28 11:32:04 -07002436 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002437 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002438 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2439 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002440 }
Dave Wallace7876d392017-10-19 03:53:57 -04002441 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002442 {
Florin Coras134a9962018-08-28 11:32:04 -07002443 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002444 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002445 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2446 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002447 }
2448
Florin Coras54693d22018-07-17 10:46:29 -07002449 if (!n_bits)
2450 return 0;
2451
2452 if (!write_map)
2453 goto check_rd;
2454
Florin Coras096a1d52021-01-27 15:33:51 -08002455 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2456 {
2457 if (!(s = vcl_session_get (wrk, sid)))
2458 {
2459 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2460 bits_set++;
2461 continue;
2462 }
Florin Coras54693d22018-07-17 10:46:29 -07002463
Florin Coras096a1d52021-01-27 15:33:51 -08002464 if (vcl_session_write_ready (s))
2465 {
2466 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2467 bits_set++;
2468 }
2469 else
2470 {
2471 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2472 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2473 }
2474 }
Florin Coras54693d22018-07-17 10:46:29 -07002475
2476check_rd:
2477 if (!read_map)
2478 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002479
Florin Coras096a1d52021-01-27 15:33:51 -08002480 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2481 {
2482 if (!(s = vcl_session_get (wrk, sid)))
2483 {
2484 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2485 bits_set++;
2486 continue;
2487 }
Florin Coras54693d22018-07-17 10:46:29 -07002488
Florin Coras096a1d52021-01-27 15:33:51 -08002489 if (vcl_session_read_ready (s))
2490 {
2491 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2492 bits_set++;
2493 }
2494 }
Florin Coras54693d22018-07-17 10:46:29 -07002495
2496check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002497
Florin Coras86f04502018-09-12 16:08:01 -07002498 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2499 {
2500 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2501 read_map, write_map, except_map, &bits_set);
2502 }
2503 vec_reset_length (wrk->unhandled_evts_vector);
2504
Florin Coras99368312018-08-02 10:45:44 -07002505 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002506 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002507 time_to_wait, &bits_set);
2508 else
Florin Coras134a9962018-08-28 11:32:04 -07002509 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002510 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002511
Dave Wallace543852a2017-08-03 02:11:34 -04002512 return (bits_set);
2513}
2514
Dave Wallacef7f809c2017-10-03 01:48:42 -04002515static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002516vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002517{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002518 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002519 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002520 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002521
Florin Corasc0737e92019-03-04 14:19:39 -08002522 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002523 return;
2524
Florin Coras6c3b2182020-10-19 18:36:48 -07002525 s = vcl_session_get_w_handle (wrk, vep_handle);
2526 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002527 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002528 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002529 goto done;
2530 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002531 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002532 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002533 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002534 goto done;
2535 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002536 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002537 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002538 "{\n"
2539 " is_vep = %u\n"
2540 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002541 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002542 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2543 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002544
Florin Coras7e5e62b2019-07-30 14:08:23 -07002545 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002546 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002547 s = vcl_session_get_w_handle (wrk, sh);
2548 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002549 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002550 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002551 goto done;
2552 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002553 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002554 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002555 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002556 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002557 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002558 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002559 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002560 goto done;
2561 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002562 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002563 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2564 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002565 sh, s->vep.vep_sh, vep_handle);
2566 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002567 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002568 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002569 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002570 " next_sh = 0x%x (%u)\n"
2571 " prev_sh = 0x%x (%u)\n"
2572 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002573 " ev.events = 0x%x\n"
2574 " ev.data.u64 = 0x%llx\n"
2575 " et_mask = 0x%x\n"
2576 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002577 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002578 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2579 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002580 }
2581 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002582
2583done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002584 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002585}
2586
2587int
2588vppcom_epoll_create (void)
2589{
Florin Coras134a9962018-08-28 11:32:04 -07002590 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002591 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002592
Florin Coras134a9962018-08-28 11:32:04 -07002593 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002594
Florin Coras6c3b2182020-10-19 18:36:48 -07002595 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002596 vep_session->vep.vep_sh = ~0;
2597 vep_session->vep.next_sh = ~0;
2598 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002599 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002600
Florin Corasa7a1a222018-12-30 17:11:31 -08002601 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2602 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002603
Florin Corasab2f6db2018-08-31 14:31:41 -07002604 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002605}
2606
2607int
Florin Coras134a9962018-08-28 11:32:04 -07002608vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002609 struct epoll_event *event)
2610{
Florin Coras134a9962018-08-28 11:32:04 -07002611 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002612 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002613 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002614 vcl_session_t *s;
2615 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616
Florin Coras134a9962018-08-28 11:32:04 -07002617 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002618 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002619 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002620 return VPPCOM_EINVAL;
2621 }
2622
Florin Coras134a9962018-08-28 11:32:04 -07002623 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002624 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002625 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002626 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002627 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002628 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002629 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002630 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002631 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002632 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002633 }
2634
Florin Coras134a9962018-08-28 11:32:04 -07002635 ASSERT (vep_session->vep.vep_sh == ~0);
2636 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002637
Florin Coras17ec5772020-08-12 20:46:29 -07002638 s = vcl_session_get_w_handle (wrk, session_handle);
2639 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002640 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002641 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002642 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002643 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002644 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002645 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002646 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002647 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002648 }
2649
2650 switch (op)
2651 {
2652 case EPOLL_CTL_ADD:
2653 if (PREDICT_FALSE (!event))
2654 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002655 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002656 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002657 }
Florin Coras134a9962018-08-28 11:32:04 -07002658 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002659 {
Florin Coras7e12d942018-06-27 14:32:43 -07002660 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002661 next_session = vcl_session_get_w_handle (wrk,
2662 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002663 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002664 {
Florin Coras5e062572019-03-14 19:07:51 -07002665 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002666 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002667 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002668 }
Florin Coras134a9962018-08-28 11:32:04 -07002669 ASSERT (next_session->vep.prev_sh == vep_handle);
2670 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002671 }
Florin Coras17ec5772020-08-12 20:46:29 -07002672 s->vep.next_sh = vep_session->vep.next_sh;
2673 s->vep.prev_sh = vep_handle;
2674 s->vep.vep_sh = vep_handle;
2675 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2676 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002677 s->flags &= ~VCL_SESSION_F_IS_VEP;
2678 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002679 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002680
Florin Coras17ec5772020-08-12 20:46:29 -07002681 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2682 if (txf && (event->events & EPOLLOUT))
2683 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002684
Florin Coras6e3c1f82020-01-15 01:30:46 +00002685 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002686 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002687 {
2688 session_event_t e = { 0 };
2689 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002690 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002691 vec_add1 (wrk->unhandled_evts_vector, e);
2692 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002693 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002694 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002695 {
2696 session_event_t e = { 0 };
2697 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002698 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002699 vec_add1 (wrk->unhandled_evts_vector, e);
2700 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002701 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2702 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002703 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002704 break;
2705
2706 case EPOLL_CTL_MOD:
2707 if (PREDICT_FALSE (!event))
2708 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002709 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002710 rv = VPPCOM_EINVAL;
2711 goto done;
2712 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002713 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002714 {
Florin Coras5e062572019-03-14 19:07:51 -07002715 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002716 rv = VPPCOM_EINVAL;
2717 goto done;
2718 }
Florin Coras17ec5772020-08-12 20:46:29 -07002719 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002720 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002721 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002722 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002723 rv = VPPCOM_EINVAL;
2724 goto done;
2725 }
hanlin475c9d72019-12-26 11:44:28 +08002726
2727 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2728 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002729 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002730 {
2731 session_event_t e = { 0 };
2732 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002733 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002734 vec_add1 (wrk->unhandled_evts_vector, e);
2735 }
Florin Coras17ec5772020-08-12 20:46:29 -07002736 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2737 s->vep.ev = *event;
2738 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002739 if (txf)
2740 {
2741 if (event->events & EPOLLOUT)
2742 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2743 else
2744 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2745 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002746 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2747 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002748 break;
2749
2750 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002751 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002752 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002753 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002754 rv = VPPCOM_EINVAL;
2755 goto done;
2756 }
Florin Coras17ec5772020-08-12 20:46:29 -07002757 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002758 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002759 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002760 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002761 rv = VPPCOM_EINVAL;
2762 goto done;
2763 }
2764
Florin Coras17ec5772020-08-12 20:46:29 -07002765 if (s->vep.prev_sh == vep_handle)
2766 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002767 else
2768 {
Florin Coras7e12d942018-06-27 14:32:43 -07002769 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002770 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002771 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002772 {
Florin Coras5e062572019-03-14 19:07:51 -07002773 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002774 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002775 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 }
Florin Coras134a9962018-08-28 11:32:04 -07002777 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002778 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002779 }
Florin Coras17ec5772020-08-12 20:46:29 -07002780 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002781 {
Florin Coras7e12d942018-06-27 14:32:43 -07002782 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002783 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002784 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002785 {
Florin Coras5e062572019-03-14 19:07:51 -07002786 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002787 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002788 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789 }
Florin Coras134a9962018-08-28 11:32:04 -07002790 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002791 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002792 }
2793
Florin Coras17ec5772020-08-12 20:46:29 -07002794 memset (&s->vep, 0, sizeof (s->vep));
2795 s->vep.next_sh = ~0;
2796 s->vep.prev_sh = ~0;
2797 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002798 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002799
Florin Coras17ec5772020-08-12 20:46:29 -07002800 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2801 if (txf)
2802 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002803
Florin Coras5e062572019-03-14 19:07:51 -07002804 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002805 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002806 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002807 break;
2808
2809 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002810 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002811 rv = VPPCOM_EINVAL;
2812 }
2813
Florin Coras134a9962018-08-28 11:32:04 -07002814 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002815
2816done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002817 return rv;
2818}
2819
Florin Coras86f04502018-09-12 16:08:01 -07002820static inline void
2821vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2822 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002823{
2824 session_disconnected_msg_t *disconnected_msg;
2825 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002826 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002827 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002828 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002829 u8 add_event = 0;
2830
2831 switch (e->event_type)
2832 {
Florin Coras653e43f2019-03-04 10:56:23 -08002833 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002834 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002835 s = vcl_session_get (wrk, sid);
2836 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002837 break;
Florin Corasb242d312020-10-26 15:35:40 -07002838 vcl_fifo_rx_evt_valid_or_break (s);
2839 session_events = s->vep.ev.events;
2840 if (!(EPOLLIN & s->vep.ev.events)
2841 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002842 break;
2843 add_event = 1;
2844 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002845 session_evt_data = s->vep.ev.data.u64;
2846 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002847 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002848 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002849 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002850 s = vcl_session_get (wrk, sid);
2851 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002852 break;
Florin Corasb242d312020-10-26 15:35:40 -07002853 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002854 if (!(EPOLLOUT & session_events))
2855 break;
2856 add_event = 1;
2857 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002858 session_evt_data = s->vep.ev.data.u64;
2859 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2860 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002861 break;
Florin Coras86f04502018-09-12 16:08:01 -07002862 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002863 if (!e->postponed)
2864 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2865 else
2866 s = vcl_session_get (wrk, e->session_index);
2867 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002868 break;
Florin Corasb242d312020-10-26 15:35:40 -07002869 session_events = s->vep.ev.events;
2870 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002871 if (!(EPOLLIN & session_events))
2872 break;
Florin Coras86f04502018-09-12 16:08:01 -07002873 add_event = 1;
2874 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002875 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002876 break;
2877 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002878 if (!e->postponed)
2879 {
2880 connected_msg = (session_connected_msg_t *) e->data;
2881 sid = vcl_session_connected_handler (wrk, connected_msg);
2882 }
2883 else
2884 sid = e->session_index;
2885 s = vcl_session_get (wrk, sid);
2886 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002887 break;
Florin Corasb242d312020-10-26 15:35:40 -07002888 session_events = s->vep.ev.events;
2889 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002890 if (!(EPOLLOUT & session_events))
2891 break;
2892 add_event = 1;
2893 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002894 session_evt_data = s->vep.ev.data.u64;
2895 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002896 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002897 break;
2898 case SESSION_CTRL_EVT_DISCONNECTED:
2899 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002900 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2901 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002902 break;
Florin Corasb242d312020-10-26 15:35:40 -07002903 sid = s->session_index;
2904 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002905 add_event = 1;
2906 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002907 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002908 break;
2909 case SESSION_CTRL_EVT_RESET:
2910 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002911 s = vcl_session_get (wrk, sid);
2912 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002913 break;
Florin Corasb242d312020-10-26 15:35:40 -07002914 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002915 add_event = 1;
2916 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002917 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002918 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002919 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2920 vcl_session_unlisten_reply_handler (wrk, e->data);
2921 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002922 case SESSION_CTRL_EVT_MIGRATED:
2923 vcl_session_migrated_handler (wrk, e->data);
2924 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002925 case SESSION_CTRL_EVT_CLEANUP:
2926 vcl_session_cleanup_handler (wrk, e->data);
2927 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002928 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2929 vcl_session_req_worker_update_handler (wrk, e->data);
2930 break;
2931 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2932 vcl_session_worker_update_reply_handler (wrk, e->data);
2933 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002934 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2935 vcl_session_app_add_segment_handler (wrk, e->data);
2936 break;
2937 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2938 vcl_session_app_del_segment_handler (wrk, e->data);
2939 break;
hanlina3a48962020-07-13 11:09:15 +08002940 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002941 vcl_worker_rpc_handler (wrk, e->data);
2942 break;
Florin Coras86f04502018-09-12 16:08:01 -07002943 default:
2944 VDBG (0, "unhandled: %u", e->event_type);
2945 break;
2946 }
2947
2948 if (add_event)
2949 {
2950 events[*num_ev].data.u64 = session_evt_data;
2951 if (EPOLLONESHOT & session_events)
2952 {
Florin Corasb242d312020-10-26 15:35:40 -07002953 s = vcl_session_get (wrk, sid);
2954 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002955 }
2956 *num_ev += 1;
2957 }
2958}
2959
2960static int
2961vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2962 struct epoll_event *events, u32 maxevents,
2963 double wait_for_time, u32 * num_ev)
2964{
Florin Coras99368312018-08-02 10:45:44 -07002965 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002966 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002967 int i;
2968
Florin Coras539663c2018-09-28 14:59:37 -07002969 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2970 goto handle_dequeued;
2971
Florin Coras54693d22018-07-17 10:46:29 -07002972 if (svm_msg_q_is_empty (mq))
2973 {
2974 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08002975 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002976 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002977 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002978 else
2979 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002980 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08002981 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002982 }
2983 }
Florin Corase003a1b2019-06-05 10:47:16 -07002984 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002985 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002986
Florin Coras539663c2018-09-28 14:59:37 -07002987handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002988 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002989 {
Florin Coras134a9962018-08-28 11:32:04 -07002990 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002991 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002992 if (*num_ev < maxevents)
2993 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2994 else
2995 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07002996 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002997 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002998 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002999 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003000 return *num_ev;
3001}
3002
Florin Coras99368312018-08-02 10:45:44 -07003003static int
Florin Coras134a9962018-08-28 11:32:04 -07003004vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003005 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003006{
Florin Corase003a1b2019-06-05 10:47:16 -07003007 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003008
3009 if (!n_evts)
3010 {
3011 wait = wait_for_time;
3012 start = clib_time_now (&wrk->clib_time);
3013 }
3014
3015 do
3016 {
3017 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
3018 wait, &n_evts);
3019 if (n_evts)
3020 return n_evts;
3021 if (wait == -1)
3022 continue;
3023
Florin Corase003a1b2019-06-05 10:47:16 -07003024 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07003025 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07003026 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003027 }
3028 while (wait > 0);
3029
3030 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003031}
3032
3033static int
Florin Coras134a9962018-08-28 11:32:04 -07003034vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003035 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003036{
Florin Corasb13ba132021-01-27 16:05:24 -08003037 double wait = 0, start = 0, now;
Florin Coras99368312018-08-02 10:45:44 -07003038 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003039 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003040 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003041 u64 buf;
3042
Florin Coras134a9962018-08-28 11:32:04 -07003043 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003044 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003045 {
Florin Corasb13ba132021-01-27 16:05:24 -08003046 wait = wait_for_time;
3047 start = clib_time_now (&wrk->clib_time);
Florin Coras99368312018-08-02 10:45:44 -07003048 }
3049
Florin Corasb13ba132021-01-27 16:05:24 -08003050 do
3051 {
3052 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3053 vec_len (wrk->mq_events), wait);
3054 if (n_mq_evts < 0)
3055 {
3056 VDBG (0, "epoll_wait error %u", errno);
3057 return n_evts;
3058 }
3059
3060 for (i = 0; i < n_mq_evts; i++)
3061 {
3062 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3063 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3064 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3065 &n_evts);
3066 }
3067
3068 if (n_evts)
3069 return n_evts;
3070 if (wait == -1)
3071 continue;
3072
3073 now = clib_time_now (&wrk->clib_time);
3074 wait -= (now - start) * 1e3;
3075 start = now;
3076 }
3077 while (wait > 0);
3078
3079 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003080}
3081
Dave Wallacef7f809c2017-10-03 01:48:42 -04003082int
Florin Coras134a9962018-08-28 11:32:04 -07003083vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003084 int maxevents, double wait_for_time)
3085{
Florin Coras134a9962018-08-28 11:32:04 -07003086 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003087 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003088 u32 n_evts = 0;
3089 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003090
3091 if (PREDICT_FALSE (maxevents <= 0))
3092 {
Florin Coras5e062572019-03-14 19:07:51 -07003093 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003094 return VPPCOM_EINVAL;
3095 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003096
Florin Coras134a9962018-08-28 11:32:04 -07003097 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003098 if (!vep_session)
3099 return VPPCOM_EBADFD;
3100
Florin Coras6c3b2182020-10-19 18:36:48 -07003101 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003102 {
Florin Coras5e062572019-03-14 19:07:51 -07003103 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003104 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003105 }
Florin Coras54693d22018-07-17 10:46:29 -07003106
3107 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003108
Florin Coras86f04502018-09-12 16:08:01 -07003109 if (vec_len (wrk->unhandled_evts_vector))
3110 {
3111 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3112 {
3113 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3114 events, &n_evts);
3115 if (n_evts == maxevents)
3116 {
Florin Corase003a1b2019-06-05 10:47:16 -07003117 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3118 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003119 }
3120 }
Florin Corase003a1b2019-06-05 10:47:16 -07003121 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003122 }
wanghanlin8919fec2021-03-18 20:00:41 +08003123 /* Request to only drain unhandled */
3124 if ((int) wait_for_time == -2)
3125 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003126
3127 if (vcm->cfg.use_mq_eventfd)
3128 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3129 wait_for_time);
3130
3131 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3132 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003133}
3134
Dave Wallace35830af2017-10-09 01:43:42 -04003135int
Florin Coras134a9962018-08-28 11:32:04 -07003136vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003137 void *buffer, uint32_t * buflen)
3138{
Florin Coras134a9962018-08-28 11:32:04 -07003139 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003140 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003141 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003142 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003143 vcl_session_t *session;
3144 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003145
Florin Coras134a9962018-08-28 11:32:04 -07003146 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003147 if (!session)
3148 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003149
Dave Wallace35830af2017-10-09 01:43:42 -04003150 switch (op)
3151 {
3152 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003153 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003154 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3155 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003156 break;
3157
Dave Wallace227867f2017-11-13 21:21:53 -05003158 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003159 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003160 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3161 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003162 break;
3163
3164 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003165 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003166 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003167 *flags =
3168 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003169 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003170 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003171 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003172 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3173 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003174 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003175 }
3176 else
3177 rv = VPPCOM_EINVAL;
3178 break;
3179
3180 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003181 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003182 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003183 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003184 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003185 else
Florin Corasac422d62020-10-19 20:51:36 -07003186 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003187
Florin Coras5e062572019-03-14 19:07:51 -07003188 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3189 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003190 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003191 }
3192 else
3193 rv = VPPCOM_EINVAL;
3194 break;
3195
3196 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003197 if (PREDICT_TRUE (buffer && buflen &&
3198 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003199 {
Florin Coras7e12d942018-06-27 14:32:43 -07003200 ep->is_ip4 = session->transport.is_ip4;
3201 ep->port = session->transport.rmt_port;
3202 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003203 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3204 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003205 else
Dave Barach178cf492018-11-13 16:34:13 -05003206 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3207 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003208 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003209 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3210 "addr = %U, port %u", session_handle, ep->is_ip4,
3211 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003212 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3213 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003214 }
3215 else
3216 rv = VPPCOM_EINVAL;
3217 break;
3218
3219 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003220 if (PREDICT_TRUE (buffer && buflen &&
3221 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003222 {
Florin Coras7e12d942018-06-27 14:32:43 -07003223 ep->is_ip4 = session->transport.is_ip4;
3224 ep->port = session->transport.lcl_port;
3225 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003226 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3227 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003228 else
Dave Barach178cf492018-11-13 16:34:13 -05003229 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3230 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003231 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003232 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3233 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003234 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003235 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3236 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003237 }
3238 else
3239 rv = VPPCOM_EINVAL;
3240 break;
Stevenb5a11602017-10-11 09:59:30 -07003241
Florin Corasef7cbf62019-10-17 09:56:27 -07003242 case VPPCOM_ATTR_SET_LCL_ADDR:
3243 if (PREDICT_TRUE (buffer && buflen &&
3244 (*buflen >= sizeof (*ep)) && ep->ip))
3245 {
3246 session->transport.is_ip4 = ep->is_ip4;
3247 session->transport.lcl_port = ep->port;
3248 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3249 *buflen = sizeof (*ep);
3250 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3251 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3252 &session->transport.lcl_ip,
3253 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3254 clib_net_to_host_u16 (ep->port));
3255 }
3256 else
3257 rv = VPPCOM_EINVAL;
3258 break;
3259
Dave Wallace048b1d62018-01-03 22:24:41 -05003260 case VPPCOM_ATTR_GET_LIBC_EPFD:
3261 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003262 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003263 break;
3264
3265 case VPPCOM_ATTR_SET_LIBC_EPFD:
3266 if (PREDICT_TRUE (buffer && buflen &&
3267 (*buflen == sizeof (session->libc_epfd))))
3268 {
3269 session->libc_epfd = *(int *) buffer;
3270 *buflen = sizeof (session->libc_epfd);
3271
Florin Coras5e062572019-03-14 19:07:51 -07003272 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3273 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003274 }
3275 else
3276 rv = VPPCOM_EINVAL;
3277 break;
3278
3279 case VPPCOM_ATTR_GET_PROTOCOL:
3280 if (buffer && buflen && (*buflen >= sizeof (int)))
3281 {
Florin Coras7e12d942018-06-27 14:32:43 -07003282 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003283 *buflen = sizeof (int);
3284
Florin Coras5e062572019-03-14 19:07:51 -07003285 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3286 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003287 }
3288 else
3289 rv = VPPCOM_EINVAL;
3290 break;
3291
3292 case VPPCOM_ATTR_GET_LISTEN:
3293 if (buffer && buflen && (*buflen >= sizeof (int)))
3294 {
Florin Corasac422d62020-10-19 20:51:36 -07003295 *(int *) buffer = vcl_session_has_attr (session,
3296 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003297 *buflen = sizeof (int);
3298
Florin Coras5e062572019-03-14 19:07:51 -07003299 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3300 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003301 }
3302 else
3303 rv = VPPCOM_EINVAL;
3304 break;
3305
3306 case VPPCOM_ATTR_GET_ERROR:
3307 if (buffer && buflen && (*buflen >= sizeof (int)))
3308 {
3309 *(int *) buffer = 0;
3310 *buflen = sizeof (int);
3311
Florin Coras5e062572019-03-14 19:07:51 -07003312 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3313 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003314 }
3315 else
3316 rv = VPPCOM_EINVAL;
3317 break;
3318
3319 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3320 if (buffer && buflen && (*buflen >= sizeof (u32)))
3321 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003322
3323 /* VPP-TBD */
3324 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003325 session->tx_fifo ?
3326 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003327 vcm->cfg.tx_fifo_size);
3328 *buflen = sizeof (u32);
3329
Florin Coras5e062572019-03-14 19:07:51 -07003330 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3331 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3332 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003333 }
3334 else
3335 rv = VPPCOM_EINVAL;
3336 break;
3337
3338 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3339 if (buffer && buflen && (*buflen == sizeof (u32)))
3340 {
3341 /* VPP-TBD */
3342 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003343 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3344 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3345 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003346 }
3347 else
3348 rv = VPPCOM_EINVAL;
3349 break;
3350
3351 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3352 if (buffer && buflen && (*buflen >= sizeof (u32)))
3353 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003354
3355 /* VPP-TBD */
3356 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003357 session->rx_fifo ?
3358 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003359 vcm->cfg.rx_fifo_size);
3360 *buflen = sizeof (u32);
3361
Florin Coras5e062572019-03-14 19:07:51 -07003362 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3363 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003364 }
3365 else
3366 rv = VPPCOM_EINVAL;
3367 break;
3368
3369 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3370 if (buffer && buflen && (*buflen == sizeof (u32)))
3371 {
3372 /* VPP-TBD */
3373 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003374 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3375 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3376 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003377 }
3378 else
3379 rv = VPPCOM_EINVAL;
3380 break;
3381
3382 case VPPCOM_ATTR_GET_REUSEADDR:
3383 if (buffer && buflen && (*buflen >= sizeof (int)))
3384 {
3385 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003386 *(int *) buffer = vcl_session_has_attr (session,
3387 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003388 *buflen = sizeof (int);
3389
Florin Coras5e062572019-03-14 19:07:51 -07003390 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3391 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003392 }
3393 else
3394 rv = VPPCOM_EINVAL;
3395 break;
3396
Stevenb5a11602017-10-11 09:59:30 -07003397 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003398 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003399 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003400 {
3401 /* VPP-TBD */
3402 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003403 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003404 else
Florin Corasac422d62020-10-19 20:51:36 -07003405 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003406
Florin Coras5e062572019-03-14 19:07:51 -07003407 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003408 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003409 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003410 }
3411 else
3412 rv = VPPCOM_EINVAL;
3413 break;
3414
3415 case VPPCOM_ATTR_GET_REUSEPORT:
3416 if (buffer && buflen && (*buflen >= sizeof (int)))
3417 {
3418 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003419 *(int *) buffer = vcl_session_has_attr (session,
3420 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003421 *buflen = sizeof (int);
3422
Florin Coras5e062572019-03-14 19:07:51 -07003423 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3424 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003425 }
3426 else
3427 rv = VPPCOM_EINVAL;
3428 break;
3429
3430 case VPPCOM_ATTR_SET_REUSEPORT:
3431 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003432 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003433 {
3434 /* VPP-TBD */
3435 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003436 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003437 else
Florin Corasac422d62020-10-19 20:51:36 -07003438 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003439
Florin Coras5e062572019-03-14 19:07:51 -07003440 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003441 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003442 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003443 }
3444 else
3445 rv = VPPCOM_EINVAL;
3446 break;
3447
3448 case VPPCOM_ATTR_GET_BROADCAST:
3449 if (buffer && buflen && (*buflen >= sizeof (int)))
3450 {
3451 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003452 *(int *) buffer = vcl_session_has_attr (session,
3453 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003454 *buflen = sizeof (int);
3455
Florin Coras5e062572019-03-14 19:07:51 -07003456 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3457 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003458 }
3459 else
3460 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003461 break;
3462
3463 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003464 if (buffer && buflen && (*buflen == sizeof (int)))
3465 {
3466 /* VPP-TBD */
3467 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003468 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003469 else
Florin Corasac422d62020-10-19 20:51:36 -07003470 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003471
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003473 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003474 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 }
3476 else
3477 rv = VPPCOM_EINVAL;
3478 break;
3479
3480 case VPPCOM_ATTR_GET_V6ONLY:
3481 if (buffer && buflen && (*buflen >= sizeof (int)))
3482 {
3483 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003484 *(int *) buffer = vcl_session_has_attr (session,
3485 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003486 *buflen = sizeof (int);
3487
Florin Coras5e062572019-03-14 19:07:51 -07003488 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3489 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 }
3491 else
3492 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003493 break;
3494
3495 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 if (buffer && buflen && (*buflen == sizeof (int)))
3497 {
3498 /* VPP-TBD */
3499 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003500 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003501 else
Florin Corasac422d62020-10-19 20:51:36 -07003502 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003503
Florin Coras5e062572019-03-14 19:07:51 -07003504 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003505 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003506 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 }
3508 else
3509 rv = VPPCOM_EINVAL;
3510 break;
3511
3512 case VPPCOM_ATTR_GET_KEEPALIVE:
3513 if (buffer && buflen && (*buflen >= sizeof (int)))
3514 {
3515 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003516 *(int *) buffer = vcl_session_has_attr (session,
3517 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003518 *buflen = sizeof (int);
3519
Florin Coras5e062572019-03-14 19:07:51 -07003520 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3521 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003522 }
3523 else
3524 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003525 break;
Stevenbccd3392017-10-12 20:42:21 -07003526
3527 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 if (buffer && buflen && (*buflen == sizeof (int)))
3529 {
3530 /* VPP-TBD */
3531 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003532 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003533 else
Florin Corasac422d62020-10-19 20:51:36 -07003534 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003535
Florin Coras5e062572019-03-14 19:07:51 -07003536 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003537 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003538 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 }
3540 else
3541 rv = VPPCOM_EINVAL;
3542 break;
3543
3544 case VPPCOM_ATTR_GET_TCP_NODELAY:
3545 if (buffer && buflen && (*buflen >= sizeof (int)))
3546 {
3547 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003548 *(int *) buffer = vcl_session_has_attr (session,
3549 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003550 *buflen = sizeof (int);
3551
Florin Coras5e062572019-03-14 19:07:51 -07003552 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3553 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003554 }
3555 else
3556 rv = VPPCOM_EINVAL;
3557 break;
3558
3559 case VPPCOM_ATTR_SET_TCP_NODELAY:
3560 if (buffer && buflen && (*buflen == sizeof (int)))
3561 {
3562 /* VPP-TBD */
3563 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003564 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003565 else
Florin Corasac422d62020-10-19 20:51:36 -07003566 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003567
Florin Coras5e062572019-03-14 19:07:51 -07003568 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003569 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003570 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003571 }
3572 else
3573 rv = VPPCOM_EINVAL;
3574 break;
3575
3576 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3577 if (buffer && buflen && (*buflen >= sizeof (int)))
3578 {
3579 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003580 *(int *) buffer = vcl_session_has_attr (session,
3581 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003582 *buflen = sizeof (int);
3583
Florin Coras5e062572019-03-14 19:07:51 -07003584 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3585 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003586 }
3587 else
3588 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003589 break;
3590
3591 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003592 if (buffer && buflen && (*buflen == sizeof (int)))
3593 {
3594 /* VPP-TBD */
3595 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003596 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003597 else
Florin Corasac422d62020-10-19 20:51:36 -07003598 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003599
Florin Coras5e062572019-03-14 19:07:51 -07003600 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003601 vcl_session_has_attr (session,
3602 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003603 }
3604 else
3605 rv = VPPCOM_EINVAL;
3606 break;
3607
3608 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3609 if (buffer && buflen && (*buflen >= sizeof (int)))
3610 {
3611 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003612 *(int *) buffer = vcl_session_has_attr (session,
3613 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003614 *buflen = sizeof (int);
3615
Florin Coras5e062572019-03-14 19:07:51 -07003616 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3617 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003618 }
3619 else
3620 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003621 break;
3622
3623 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003624 if (buffer && buflen && (*buflen == sizeof (int)))
3625 {
3626 /* VPP-TBD */
3627 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003628 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003629 else
Florin Corasac422d62020-10-19 20:51:36 -07003630 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003631
Florin Coras5e062572019-03-14 19:07:51 -07003632 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003633 vcl_session_has_attr (session,
3634 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003635 }
3636 else
3637 rv = VPPCOM_EINVAL;
3638 break;
3639
3640 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003641 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003642 {
Florin Coras04ae8272021-04-12 19:55:37 -07003643 rv = VPPCOM_EINVAL;
3644 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003645 }
Florin Coras04ae8272021-04-12 19:55:37 -07003646
3647 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3648 tea.mss = *(u32 *) buffer;
3649 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3650 rv = VPPCOM_ENOPROTOOPT;
3651
3652 if (!rv)
3653 {
3654 *(u32 *) buffer = tea.mss;
3655 *buflen = sizeof (int);
3656 }
3657
3658 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3659 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003660 break;
3661
3662 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003663 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003664 {
Florin Coras04ae8272021-04-12 19:55:37 -07003665 rv = VPPCOM_EINVAL;
3666 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003667 }
Florin Coras04ae8272021-04-12 19:55:37 -07003668
3669 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3670 tea.mss = *(u32 *) buffer;
3671 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3672 rv = VPPCOM_ENOPROTOOPT;
3673
3674 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3675 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003676 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003677
Florin Coras7baeb712019-01-04 17:05:43 -08003678 case VPPCOM_ATTR_SET_SHUT:
3679 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003680 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003681 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003682 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003683 break;
3684
3685 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003686 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003687 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003688 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003689 tmp_flags |= 2;
3690 if (tmp_flags == 1)
3691 *(int *) buffer = SHUT_RD;
3692 else if (tmp_flags == 2)
3693 *(int *) buffer = SHUT_WR;
3694 else if (tmp_flags == 3)
3695 *(int *) buffer = SHUT_RDWR;
3696 *buflen = sizeof (int);
3697 break;
Florin Coras1e966172020-05-16 18:18:14 +00003698
3699 case VPPCOM_ATTR_SET_CONNECTED:
3700 session->flags |= VCL_SESSION_F_CONNECTED;
3701 break;
3702
Florin Corasa5a9efd2021-01-05 17:03:29 -08003703 case VPPCOM_ATTR_SET_CKPAIR:
3704 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3705 !vcl_session_has_crypto (session))
3706 {
3707 rv = VPPCOM_EINVAL;
3708 break;
3709 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003710 if (!session->ext_config)
3711 {
3712 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO);
3713 }
3714 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3715 {
3716 rv = VPPCOM_EINVAL;
3717 break;
3718 }
3719
3720 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003721 break;
3722
Florin Coras6a6555a2021-01-28 11:39:27 -08003723 case VPPCOM_ATTR_SET_VRF:
3724 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3725 {
3726 rv = VPPCOM_EINVAL;
3727 break;
3728 }
3729 session->vrf = *(u32 *) buffer;
3730 break;
3731
3732 case VPPCOM_ATTR_GET_VRF:
3733 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3734 {
3735 rv = VPPCOM_EINVAL;
3736 break;
3737 }
3738 *(u32 *) buffer = session->vrf;
3739 *buflen = sizeof (u32);
3740 break;
3741
wanghanlin0674f852021-02-22 10:38:36 +08003742 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003743 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003744 {
Florin Corasd77325c2021-02-23 12:03:03 -08003745 rv = VPPCOM_EINVAL;
3746 break;
wanghanlin0674f852021-02-22 10:38:36 +08003747 }
Florin Corasd77325c2021-02-23 12:03:03 -08003748
3749 if (session->transport.is_ip4)
3750 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003751 else
Florin Corasd77325c2021-02-23 12:03:03 -08003752 *(int *) buffer = AF_INET6;
3753 *buflen = sizeof (int);
3754
wanghanlin0674f852021-02-22 10:38:36 +08003755 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3756 *buflen);
3757 break;
3758
Dave Wallacee22aa742017-10-20 12:30:38 -04003759 default:
3760 rv = VPPCOM_EINVAL;
3761 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003762 }
3763
Dave Wallace35830af2017-10-09 01:43:42 -04003764 return rv;
3765}
3766
Stevenac1f96d2017-10-24 16:03:58 -07003767int
Florin Coras134a9962018-08-28 11:32:04 -07003768vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003769 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3770{
Florin Coras134a9962018-08-28 11:32:04 -07003771 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003772 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003773 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003774
3775 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003776 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003777 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003778 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003779 else
3780 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003781 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003782 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003783 }
3784
Florin Coras0a1e1832020-03-29 18:54:04 +00003785 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003786 {
Florin Coras5da10c42020-04-01 04:31:21 +00003787 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003788 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003789 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3790 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003791 else
Dave Barach178cf492018-11-13 16:34:13 -05003792 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3793 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003794 ep->is_ip4 = session->transport.is_ip4;
3795 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003796 }
Florin Coras460dce62018-07-27 05:45:06 -07003797
Stevenac1f96d2017-10-24 16:03:58 -07003798 return rv;
3799}
3800
3801int
Florin Coras134a9962018-08-28 11:32:04 -07003802vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003803 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3804{
Florin Coras7a2abce2020-04-05 19:25:44 +00003805 vcl_worker_t *wrk = vcl_worker_get_current ();
3806 vcl_session_t *s;
3807
3808 s = vcl_session_get_w_handle (wrk, session_handle);
3809 if (!s)
3810 return VPPCOM_EBADFD;
3811
Dave Wallace617dffa2017-10-26 14:47:06 -04003812 if (!buffer)
3813 return VPPCOM_EINVAL;
3814
3815 if (ep)
3816 {
Florin Coras5824cc52020-10-20 18:44:41 -07003817 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003818 return VPPCOM_EINVAL;
3819
3820 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003821 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003822 {
Florin Coras5824cc52020-10-20 18:44:41 -07003823 u32 session_index = s->session_index;
3824 f64 timeout = vcm->cfg.session_timeout;
3825 int rv;
3826
Florin Coras5da10c42020-04-01 04:31:21 +00003827 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003828 rv = vppcom_wait_for_session_state_change (session_index,
3829 VCL_STATE_READY,
3830 timeout);
3831 if (rv < 0)
3832 return rv;
3833 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003834 }
Florin Coras5824cc52020-10-20 18:44:41 -07003835
3836 s->transport.is_ip4 = ep->is_ip4;
3837 s->transport.rmt_port = ep->port;
3838 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003839 }
3840
3841 if (flags)
3842 {
3843 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003844 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003845 }
3846
Florin Coras7a2abce2020-04-05 19:25:44 +00003847 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3848 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003849}
3850
Dave Wallace048b1d62018-01-03 22:24:41 -05003851int
3852vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3853{
Florin Coras134a9962018-08-28 11:32:04 -07003854 vcl_worker_t *wrk = vcl_worker_get_current ();
3855 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003856 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003857 svm_msg_q_msg_t msg;
3858 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003859 int rv, num_ev = 0;
3860
Florin Coras5e062572019-03-14 19:07:51 -07003861 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003862
3863 if (!vp)
3864 return VPPCOM_EFAULT;
3865
3866 do
3867 {
Florin Coras7e12d942018-06-27 14:32:43 -07003868 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003869
Florin Coras6917b942018-11-13 22:44:54 -08003870 /* Dequeue all events and drop all unhandled io events */
3871 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3872 {
3873 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3874 vcl_handle_mq_event (wrk, e);
3875 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3876 }
3877 vec_reset_length (wrk->unhandled_evts_vector);
3878
Dave Wallace048b1d62018-01-03 22:24:41 -05003879 for (i = 0; i < n_sids; i++)
3880 {
Florin Coras7baeb712019-01-04 17:05:43 -08003881 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003882 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003883 {
3884 vp[i].revents = POLLHUP;
3885 num_ev++;
3886 continue;
3887 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003888
Florin Coras6917b942018-11-13 22:44:54 -08003889 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003890
3891 if (POLLIN & vp[i].events)
3892 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003893 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003894 if (rv > 0)
3895 {
Florin Coras6917b942018-11-13 22:44:54 -08003896 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003897 num_ev++;
3898 }
3899 else if (rv < 0)
3900 {
3901 switch (rv)
3902 {
3903 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003904 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003905 break;
3906
3907 default:
Florin Coras6917b942018-11-13 22:44:54 -08003908 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003909 break;
3910 }
3911 num_ev++;
3912 }
3913 }
3914
3915 if (POLLOUT & vp[i].events)
3916 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003917 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003918 if (rv > 0)
3919 {
Florin Coras6917b942018-11-13 22:44:54 -08003920 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003921 num_ev++;
3922 }
3923 else if (rv < 0)
3924 {
3925 switch (rv)
3926 {
3927 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003928 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003929 break;
3930
3931 default:
Florin Coras6917b942018-11-13 22:44:54 -08003932 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003933 break;
3934 }
3935 num_ev++;
3936 }
3937 }
3938
Dave Wallace7e607a72018-06-18 18:41:32 -04003939 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003940 {
Florin Coras6917b942018-11-13 22:44:54 -08003941 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003942 num_ev++;
3943 }
3944 }
3945 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003946 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003947 }
3948 while ((num_ev == 0) && keep_trying);
3949
Dave Wallace048b1d62018-01-03 22:24:41 -05003950 return num_ev;
3951}
3952
Florin Coras99368312018-08-02 10:45:44 -07003953int
3954vppcom_mq_epoll_fd (void)
3955{
Florin Coras134a9962018-08-28 11:32:04 -07003956 vcl_worker_t *wrk = vcl_worker_get_current ();
3957 return wrk->mqs_epfd;
3958}
3959
3960int
Florin Coras30e79c22019-01-02 19:31:22 -08003961vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003962{
3963 return session_handle & 0xFFFFFF;
3964}
3965
3966int
Florin Coras30e79c22019-01-02 19:31:22 -08003967vppcom_session_worker (vcl_session_handle_t session_handle)
3968{
3969 return session_handle >> 24;
3970}
3971
3972int
Florin Coras134a9962018-08-28 11:32:04 -07003973vppcom_worker_register (void)
3974{
Florin Coras47c40e22018-11-26 17:01:36 -08003975 if (!vcl_worker_alloc_and_init ())
3976 return VPPCOM_EEXIST;
3977
Florin Coras47c40e22018-11-26 17:01:36 -08003978 if (vcl_worker_register_with_vpp ())
3979 return VPPCOM_EEXIST;
3980
3981 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003982}
3983
Florin Coras369db832019-07-08 12:34:45 -07003984void
3985vppcom_worker_unregister (void)
3986{
3987 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3988 vcl_set_worker_index (~0);
3989}
3990
Pivo6017ff02020-05-04 17:57:33 +02003991void
3992vppcom_worker_index_set (int index)
3993{
3994 vcl_set_worker_index (index);
3995}
3996
Florin Corasdfe4cf42018-11-28 22:13:45 -08003997int
3998vppcom_worker_index (void)
3999{
4000 return vcl_get_worker_index ();
4001}
4002
Florin Corase0982e52019-01-25 13:19:56 -08004003int
4004vppcom_worker_mqs_epfd (void)
4005{
4006 vcl_worker_t *wrk = vcl_worker_get_current ();
4007 if (!vcm->cfg.use_mq_eventfd)
4008 return -1;
4009 return wrk->mqs_epfd;
4010}
4011
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004012int
4013vppcom_session_is_connectable_listener (uint32_t session_handle)
4014{
4015 vcl_session_t *session;
4016 vcl_worker_t *wrk = vcl_worker_get_current ();
4017 session = vcl_session_get_w_handle (wrk, session_handle);
4018 if (!session)
4019 return VPPCOM_EBADFD;
4020 return vcl_session_is_connectable_listener (wrk, session);
4021}
4022
4023int
4024vppcom_session_listener (uint32_t session_handle)
4025{
4026 vcl_worker_t *wrk = vcl_worker_get_current ();
4027 vcl_session_t *listen_session, *session;
4028 session = vcl_session_get_w_handle (wrk, session_handle);
4029 if (!session)
4030 return VPPCOM_EBADFD;
4031 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4032 return VPPCOM_EBADFD;
4033 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4034 if (!listen_session)
4035 return VPPCOM_EBADFD;
4036 return vcl_session_handle (listen_session);
4037}
4038
4039int
4040vppcom_session_n_accepted (uint32_t session_handle)
4041{
4042 vcl_worker_t *wrk = vcl_worker_get_current ();
4043 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4044 if (!session)
4045 return VPPCOM_EBADFD;
4046 return session->n_accepted_sessions;
4047}
4048
Florin Coras66ec4672020-06-15 07:59:40 -07004049const char *
4050vppcom_proto_str (vppcom_proto_t proto)
4051{
4052 char const *proto_str;
4053
4054 switch (proto)
4055 {
4056 case VPPCOM_PROTO_TCP:
4057 proto_str = "TCP";
4058 break;
4059 case VPPCOM_PROTO_UDP:
4060 proto_str = "UDP";
4061 break;
4062 case VPPCOM_PROTO_TLS:
4063 proto_str = "TLS";
4064 break;
4065 case VPPCOM_PROTO_QUIC:
4066 proto_str = "QUIC";
4067 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004068 case VPPCOM_PROTO_DTLS:
4069 proto_str = "DTLS";
4070 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004071 default:
4072 proto_str = "UNKNOWN";
4073 break;
4074 }
4075 return proto_str;
4076}
4077
4078const char *
4079vppcom_retval_str (int retval)
4080{
4081 char const *st;
4082
4083 switch (retval)
4084 {
4085 case VPPCOM_OK:
4086 st = "VPPCOM_OK";
4087 break;
4088
4089 case VPPCOM_EAGAIN:
4090 st = "VPPCOM_EAGAIN";
4091 break;
4092
4093 case VPPCOM_EFAULT:
4094 st = "VPPCOM_EFAULT";
4095 break;
4096
4097 case VPPCOM_ENOMEM:
4098 st = "VPPCOM_ENOMEM";
4099 break;
4100
4101 case VPPCOM_EINVAL:
4102 st = "VPPCOM_EINVAL";
4103 break;
4104
4105 case VPPCOM_EBADFD:
4106 st = "VPPCOM_EBADFD";
4107 break;
4108
4109 case VPPCOM_EAFNOSUPPORT:
4110 st = "VPPCOM_EAFNOSUPPORT";
4111 break;
4112
4113 case VPPCOM_ECONNABORTED:
4114 st = "VPPCOM_ECONNABORTED";
4115 break;
4116
4117 case VPPCOM_ECONNRESET:
4118 st = "VPPCOM_ECONNRESET";
4119 break;
4120
4121 case VPPCOM_ENOTCONN:
4122 st = "VPPCOM_ENOTCONN";
4123 break;
4124
4125 case VPPCOM_ECONNREFUSED:
4126 st = "VPPCOM_ECONNREFUSED";
4127 break;
4128
4129 case VPPCOM_ETIMEDOUT:
4130 st = "VPPCOM_ETIMEDOUT";
4131 break;
4132
4133 default:
4134 st = "UNKNOWN_STATE";
4135 break;
4136 }
4137
4138 return st;
4139}
4140
Florin Corasa5a9efd2021-01-05 17:03:29 -08004141int
4142vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4143{
4144 if (vcm->cfg.vpp_app_socket_api)
4145 {
4146 clib_warning ("not supported");
4147 return VPPCOM_EINVAL;
4148 }
4149 return vcl_bapi_add_cert_key_pair (ckpair);
4150}
4151
4152int
4153vppcom_del_cert_key_pair (uint32_t ckpair_index)
4154{
4155 if (vcm->cfg.vpp_app_socket_api)
4156 {
4157 clib_warning ("not supported");
4158 return VPPCOM_EINVAL;
4159 }
4160 return vcl_bapi_del_cert_key_pair (ckpair_index);
4161}
4162
Dave Wallacee22aa742017-10-20 12:30:38 -04004163/*
4164 * fd.io coding-style-patch-verification: ON
4165 *
4166 * Local Variables:
4167 * eval: (c-set-style "gnu")
4168 * End:
4169 */