blob: 0c6f6e6bbfd0a77346a6d1e3e511f8535d61482d [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
Florin Coras5398dfb2021-01-25 20:31:27 -080028 u32 n_msgs = 0, sz, len;
Florin Coras30e79c22019-01-02 19:31:22 -080029
Florin Coras5398dfb2021-01-25 20:31:27 -080030 while ((sz = svm_msg_q_size (mq)))
Florin Coras30e79c22019-01-02 19:31:22 -080031 {
Florin Coras5398dfb2021-01-25 20:31:27 -080032 len = vec_len (wrk->mq_msg_vector);
33 vec_validate (wrk->mq_msg_vector, len + sz - 1);
34 svm_msg_q_sub_raw_batch (mq, wrk->mq_msg_vector + len, sz);
35 n_msgs += sz;
Florin Coras30e79c22019-01-02 19:31:22 -080036 }
37 return n_msgs;
38}
39
Florin Coras697faea2018-06-27 17:10:49 -070040const char *
Florin Coras288eaab2019-02-03 15:26:14 -080041vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040042{
43 char *st;
44
45 switch (state)
46 {
Florin Corasc127d5a2020-10-14 16:35:58 -070047 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000048 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040049 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070050 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040051 st = "STATE_LISTEN";
52 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070053 case VCL_STATE_READY:
54 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040055 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070056 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080057 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050058 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070059 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040060 st = "STATE_DISCONNECT";
61 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070062 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000063 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040064 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070065 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080066 st = "STATE_UPDATED";
67 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070068 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080069 st = "STATE_LISTEN_NO_MQ";
70 break;
Dave Wallace543852a2017-08-03 02:11:34 -040071 default:
72 st = "UNKNOWN_STATE";
73 break;
74 }
75
76 return st;
77}
78
Dave Wallace543852a2017-08-03 02:11:34 -040079u8 *
80format_ip4_address (u8 * s, va_list * args)
81{
82 u8 *a = va_arg (*args, u8 *);
83 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
84}
85
86u8 *
87format_ip6_address (u8 * s, va_list * args)
88{
89 ip6_address_t *a = va_arg (*args, ip6_address_t *);
90 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
91
92 i_max_n_zero = ARRAY_LEN (a->as_u16);
93 max_n_zeros = 0;
94 i_first_zero = i_max_n_zero;
95 n_zeros = 0;
96 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
97 {
98 u32 is_zero = a->as_u16[i] == 0;
99 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
100 {
101 i_first_zero = i;
102 n_zeros = 0;
103 }
104 n_zeros += is_zero;
105 if ((!is_zero && n_zeros > max_n_zeros)
106 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
107 {
108 i_max_n_zero = i_first_zero;
109 max_n_zeros = n_zeros;
110 i_first_zero = ARRAY_LEN (a->as_u16);
111 n_zeros = 0;
112 }
113 }
114
115 last_double_colon = 0;
116 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
117 {
118 if (i == i_max_n_zero && max_n_zeros > 1)
119 {
120 s = format (s, "::");
121 i += max_n_zeros - 1;
122 last_double_colon = 1;
123 }
124 else
125 {
126 s = format (s, "%s%x",
127 (last_double_colon || i == 0) ? "" : ":",
128 clib_net_to_host_u16 (a->as_u16[i]));
129 last_double_colon = 0;
130 }
131 }
132
133 return s;
134}
135
136/* Format an IP46 address. */
137u8 *
138format_ip46_address (u8 * s, va_list * args)
139{
140 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
141 ip46_type_t type = va_arg (*args, ip46_type_t);
142 int is_ip4 = 1;
143
144 switch (type)
145 {
146 case IP46_TYPE_ANY:
147 is_ip4 = ip46_address_is_ip4 (ip46);
148 break;
149 case IP46_TYPE_IP4:
150 is_ip4 = 1;
151 break;
152 case IP46_TYPE_IP6:
153 is_ip4 = 0;
154 break;
155 }
156
157 return is_ip4 ?
158 format (s, "%U", format_ip4_address, &ip46->ip4) :
159 format (s, "%U", format_ip6_address, &ip46->ip6);
160}
161
Florin Coras697faea2018-06-27 17:10:49 -0700162/*
163 * VPPCOM Utility Functions
164 */
165
Florin Coras458089b2019-08-21 16:20:44 -0700166static void
Florin Coras4ac25842021-04-19 17:34:54 -0700167vcl_msg_add_ext_config (vcl_session_t *s, uword *offset)
168{
169 svm_fifo_chunk_t *c;
170
171 c = vcl_segment_alloc_chunk (vcl_vpp_worker_segment_handle (0),
172 0 /* one slice only */, s->ext_config->len,
173 offset);
174 if (c)
175 clib_memcpy_fast (c->data, s->ext_config, s->ext_config->len);
176}
177
178static void
Florin Coras458089b2019-08-21 16:20:44 -0700179vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
180{
181 app_session_evt_t _app_evt, *app_evt = &_app_evt;
182 session_listen_msg_t *mp;
183 svm_msg_q_t *mq;
184
185 mq = vcl_worker_ctrl_mq (wrk);
186 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
187 mp = (session_listen_msg_t *) app_evt->evt->data;
188 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700189 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700190 mp->context = s->session_index;
191 mp->wrk_index = wrk->vpp_wrk_index;
192 mp->is_ip4 = s->transport.is_ip4;
193 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
194 mp->port = s->transport.lcl_port;
195 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800196 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +0000197 if (s->flags & VCL_SESSION_F_CONNECTED)
198 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700199 if (s->ext_config)
200 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700201 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700202 if (s->ext_config)
203 {
204 clib_mem_free (s->ext_config);
205 s->ext_config = 0;
206 }
Florin Coras458089b2019-08-21 16:20:44 -0700207}
208
209static void
210vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
211{
212 app_session_evt_t _app_evt, *app_evt = &_app_evt;
213 session_connect_msg_t *mp;
214 svm_msg_q_t *mq;
215
216 mq = vcl_worker_ctrl_mq (wrk);
217 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
218 mp = (session_connect_msg_t *) app_evt->evt->data;
219 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700220 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700221 mp->context = s->session_index;
222 mp->wrk_index = wrk->vpp_wrk_index;
223 mp->is_ip4 = s->transport.is_ip4;
224 mp->parent_handle = s->parent_handle;
225 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700226 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700227 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000228 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700229 mp->proto = s->session_type;
Florin Coras6a6555a2021-01-28 11:39:27 -0800230 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000231 if (s->flags & VCL_SESSION_F_CONNECTED)
232 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras4ac25842021-04-19 17:34:54 -0700233 if (s->ext_config)
234 vcl_msg_add_ext_config (s, &mp->ext_config);
Florin Coras458089b2019-08-21 16:20:44 -0700235 app_send_ctrl_evt_to_vpp (mq, app_evt);
Florin Coras4ac25842021-04-19 17:34:54 -0700236
237 if (s->ext_config)
238 {
239 clib_mem_free (s->ext_config);
240 s->ext_config = 0;
241 }
Florin Coras458089b2019-08-21 16:20:44 -0700242}
243
244void
245vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
246{
247 app_session_evt_t _app_evt, *app_evt = &_app_evt;
248 session_unlisten_msg_t *mp;
249 svm_msg_q_t *mq;
250
251 mq = vcl_worker_ctrl_mq (wrk);
252 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
253 mp = (session_unlisten_msg_t *) app_evt->evt->data;
254 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700255 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700256 mp->wrk_index = wrk->vpp_wrk_index;
257 mp->handle = s->vpp_handle;
258 mp->context = wrk->wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
260}
261
262static void
liuyacan534468e2021-05-09 03:50:40 +0000263vcl_send_session_shutdown (vcl_worker_t *wrk, vcl_session_t *s)
264{
265 app_session_evt_t _app_evt, *app_evt = &_app_evt;
266 session_shutdown_msg_t *mp;
267 svm_msg_q_t *mq;
268
269 /* Send to thread that owns the session */
270 mq = s->vpp_evt_q;
271 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_SHUTDOWN);
272 mp = (session_shutdown_msg_t *) app_evt->evt->data;
273 memset (mp, 0, sizeof (*mp));
274 mp->client_index = wrk->api_client_handle;
275 mp->handle = s->vpp_handle;
276 app_send_ctrl_evt_to_vpp (mq, app_evt);
277}
278
279static void
Florin Coras458089b2019-08-21 16:20:44 -0700280vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
281{
282 app_session_evt_t _app_evt, *app_evt = &_app_evt;
283 session_disconnect_msg_t *mp;
284 svm_msg_q_t *mq;
285
286 /* Send to thread that owns the session */
287 mq = s->vpp_evt_q;
288 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
289 mp = (session_disconnect_msg_t *) app_evt->evt->data;
290 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700291 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700292 mp->handle = s->vpp_handle;
293 app_send_ctrl_evt_to_vpp (mq, app_evt);
294}
295
296static void
297vcl_send_app_detach (vcl_worker_t * wrk)
298{
299 app_session_evt_t _app_evt, *app_evt = &_app_evt;
300 session_app_detach_msg_t *mp;
301 svm_msg_q_t *mq;
302
303 mq = vcl_worker_ctrl_mq (wrk);
304 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
305 mp = (session_app_detach_msg_t *) app_evt->evt->data;
306 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700307 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700308 app_send_ctrl_evt_to_vpp (mq, app_evt);
309}
Florin Coras697faea2018-06-27 17:10:49 -0700310
Florin Coras54693d22018-07-17 10:46:29 -0700311static void
312vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
313 session_handle_t handle, int retval)
314{
315 app_session_evt_t _app_evt, *app_evt = &_app_evt;
316 session_accepted_reply_msg_t *rmp;
317 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
318 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
319 rmp->handle = handle;
320 rmp->context = context;
321 rmp->retval = retval;
322 app_send_ctrl_evt_to_vpp (mq, app_evt);
323}
324
Florin Coras99368312018-08-02 10:45:44 -0700325static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800326vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
327 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700328{
329 app_session_evt_t _app_evt, *app_evt = &_app_evt;
330 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800331 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700332 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
333 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800334 rmp->handle = s->vpp_handle;
335 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700336 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800337 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700338}
339
Florin Corasc9fbd662018-08-24 12:59:56 -0700340static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800341vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
342 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700343{
344 app_session_evt_t _app_evt, *app_evt = &_app_evt;
345 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800346 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
347 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700348 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800349 rmp->handle = s->vpp_handle;
350 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700351 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800352 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700353}
354
Florin Coras30e79c22019-01-02 19:31:22 -0800355void
356vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
357 u32 wrk_index)
358{
359 app_session_evt_t _app_evt, *app_evt = &_app_evt;
360 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800361
Florin Coras52dd29f2020-11-18 19:02:17 -0800362 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
363 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800364 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700365 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800366 mp->handle = s->vpp_handle;
367 mp->req_wrk_index = wrk->vpp_wrk_index;
368 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800369 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800370}
371
hanlina3a48962020-07-13 11:09:15 +0800372int
Florin Coras40c07ce2020-07-16 20:46:17 -0700373vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
374{
375 app_session_evt_t _app_evt, *app_evt = &_app_evt;
376 session_app_wrk_rpc_msg_t *mp;
377 vcl_worker_t *dst_wrk, *wrk;
378 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800379 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700380
381 if (data_len > sizeof (mp->data))
382 goto done;
383
384 clib_spinlock_lock (&vcm->workers_lock);
385
386 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
387 if (!dst_wrk)
388 goto done;
389
390 wrk = vcl_worker_get_current ();
391 mq = vcl_worker_ctrl_mq (wrk);
392 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
393 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700394 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700395 mp->wrk_index = dst_wrk->vpp_wrk_index;
396 clib_memcpy (mp->data, data, data_len);
397 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800398 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700399
400done:
401 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800402 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700403}
404
Florin Coras04ae8272021-04-12 19:55:37 -0700405int
406vcl_session_transport_attr (vcl_worker_t *wrk, vcl_session_t *s, u8 is_get,
407 transport_endpt_attr_t *attr)
408{
409 app_session_evt_t _app_evt, *app_evt = &_app_evt;
410 session_transport_attr_msg_t *mp;
411 svm_msg_q_t *mq;
412 f64 timeout;
413
414 ASSERT (!wrk->session_attr_op);
415 wrk->session_attr_op = 1;
416 wrk->session_attr_op_rv = -1;
417
418 mq = s->vpp_evt_q;
419 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_TRANSPORT_ATTR);
420 mp = (session_transport_attr_msg_t *) app_evt->evt->data;
421 memset (mp, 0, sizeof (*mp));
422 mp->client_index = wrk->api_client_handle;
423 mp->handle = s->vpp_handle;
424 mp->is_get = is_get;
425 mp->attr = *attr;
426 app_send_ctrl_evt_to_vpp (mq, app_evt);
427
428 timeout = clib_time_now (&wrk->clib_time) + 1;
429
430 while (wrk->session_attr_op && clib_time_now (&wrk->clib_time) < timeout)
431 vcl_flush_mq_events ();
432
433 if (!wrk->session_attr_op_rv && is_get)
434 *attr = wrk->session_attr_rv;
435
436 wrk->session_attr_op = 0;
437
438 return wrk->session_attr_op_rv;
439}
440
Florin Coras54693d22018-07-17 10:46:29 -0700441static u32
Florin Coras00cca802019-06-06 09:38:44 -0700442vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
443 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700444{
445 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700446 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700447
Florin Coras134a9962018-08-28 11:32:04 -0700448 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700449
Florin Coras00cca802019-06-06 09:38:44 -0700450 listen_session = vcl_session_get (wrk, ls_index);
451 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700452 {
Florin Coras00cca802019-06-06 09:38:44 -0700453 VDBG (0, "ERROR: listener handle %lu does not match session %u",
454 mp->listener_handle, ls_index);
455 goto error;
456 }
457
Florin Corasc547e912020-12-08 17:50:45 -0800458 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800459 mp->server_tx_fifo,
460 mp->vpp_event_queue_address, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700461 {
Florin Corasc547e912020-12-08 17:50:45 -0800462 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700463 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700464 }
465
Florin Coras54693d22018-07-17 10:46:29 -0700466 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700467 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700468 session->transport.rmt_port = mp->rmt.port;
469 session->transport.is_ip4 = mp->rmt.is_ip4;
470 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500471 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700472
Florin Coras134a9962018-08-28 11:32:04 -0700473 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras67c90a32021-03-09 18:36:06 -0800474 session->transport.lcl_port = mp->lcl.port;
475 session->transport.lcl_ip = mp->lcl.ip;
Florin Coras460dce62018-07-27 05:45:06 -0700476 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200477 session->is_dgram = vcl_proto_is_dgram (session->session_type);
478 session->listener_index = listen_session->session_index;
479 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700480
Florin Coras5e062572019-03-14 19:07:51 -0700481 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
482 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700483 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
484 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
485 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700486 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
487
Florin Coras00cca802019-06-06 09:38:44 -0700488 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
489 session->vpp_handle, 0);
490
Florin Coras134a9962018-08-28 11:32:04 -0700491 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700492
493error:
Florin Corasb4624182020-12-11 13:58:12 -0800494 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
495 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700496 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
497 VNET_API_ERROR_INVALID_ARGUMENT);
498 vcl_session_free (wrk, session);
499 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700500}
501
502static u32
Florin Coras134a9962018-08-28 11:32:04 -0700503vcl_session_connected_handler (vcl_worker_t * wrk,
504 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700505{
Florin Coras99368312018-08-02 10:45:44 -0700506 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800507 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700508
509 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700510 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700511 if (!session)
512 {
Florin Coras5e062572019-03-14 19:07:51 -0700513 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
514 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700515 return VCL_INVALID_SESSION_INDEX;
516 }
Florin Coras54693d22018-07-17 10:46:29 -0700517 if (mp->retval)
518 {
Florin Coras5e062572019-03-14 19:07:51 -0700519 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700520 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700521 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700522 session->vpp_handle = mp->handle;
523 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700524 }
525
Florin Corasdbc9c592019-09-25 16:37:43 -0700526 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800527
528 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800529 mp->server_tx_fifo,
530 mp->vpp_event_queue_address, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800531 {
Florin Corasc547e912020-12-08 17:50:45 -0800532 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700533 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700534 vcl_send_session_disconnect (wrk, session);
535 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800536 }
537
Florin Coras653e43f2019-03-04 10:56:23 -0800538 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700539 {
Florin Corasc547e912020-12-08 17:50:45 -0800540 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800541 mp->ct_tx_fifo, (uword) ~0, 1, session))
Florin Coras653e43f2019-03-04 10:56:23 -0800542 {
Florin Corasc547e912020-12-08 17:50:45 -0800543 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700544 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700545 vcl_send_session_disconnect (wrk, session);
546 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800547 }
Florin Coras99368312018-08-02 10:45:44 -0700548 }
Florin Coras54693d22018-07-17 10:46:29 -0700549
Florin Coras09d18c22019-04-24 11:10:02 -0700550 session->transport.is_ip4 = mp->lcl.is_ip4;
551 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500552 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700553 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700554
555 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700556 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700557 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700558 vcl_send_session_disconnect (wrk, session);
559 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700560 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700561
562 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800563 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700564
Florin Coras5e062572019-03-14 19:07:51 -0700565 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
566 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700567 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700568
Florin Coras54693d22018-07-17 10:46:29 -0700569 return session_index;
570}
571
Florin Coras3c7d4f92018-12-14 11:28:43 -0800572static int
573vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
574{
575 vcl_session_msg_t *accepted_msg;
576 int i;
577
578 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
579 {
580 accepted_msg = &session->accept_evts_fifo[i];
581 if (accepted_msg->accepted_msg.handle == handle)
582 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800583 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800584 return 1;
585 }
586 }
587 return 0;
588}
589
Florin Corasc9fbd662018-08-24 12:59:56 -0700590static u32
Florin Coras134a9962018-08-28 11:32:04 -0700591vcl_session_reset_handler (vcl_worker_t * wrk,
592 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700593{
594 vcl_session_t *session;
595 u32 sid;
596
Florin Coras134a9962018-08-28 11:32:04 -0700597 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
598 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700599 if (!session)
600 {
601 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
602 return VCL_INVALID_SESSION_INDEX;
603 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800604
605 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700606 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800607 {
608
609 if (!vcl_flag_accepted_session (session, reset_msg->handle,
610 VCL_ACCEPTED_F_RESET))
611 VDBG (0, "session was not accepted!");
612 return VCL_INVALID_SESSION_INDEX;
613 }
614
Florin Corasc127d5a2020-10-14 16:35:58 -0700615 if (session->session_state != VCL_STATE_CLOSED)
616 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800617 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700618 return sid;
619}
620
Florin Coras60116992018-08-27 09:52:18 -0700621static u32
Florin Coras134a9962018-08-28 11:32:04 -0700622vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700623{
624 vcl_session_t *session;
625 u32 sid = mp->context;
626
Florin Coras134a9962018-08-28 11:32:04 -0700627 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700628 if (mp->retval)
629 {
Florin Coras5e062572019-03-14 19:07:51 -0700630 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700631 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700632 if (session)
633 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700634 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700635 session->vpp_handle = mp->handle;
636 return sid;
637 }
638 else
639 {
Florin Coras5e062572019-03-14 19:07:51 -0700640 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
641 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700642 return VCL_INVALID_SESSION_INDEX;
643 }
644 }
645
646 session->vpp_handle = mp->handle;
647 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500648 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
649 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700650 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700651 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700652 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800653
Florin Coras6e3c1f82020-01-15 01:30:46 +0000654 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700655 {
Florin Corasc547e912020-12-08 17:50:45 -0800656 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800657 mp->tx_fifo, mp->vpp_evt_q, 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800658 {
659 VDBG (0, "failed to attach fifos for %u", session->session_index);
660 session->session_state = VCL_STATE_DETACHED;
661 return VCL_INVALID_SESSION_INDEX;
662 }
Florin Coras60116992018-08-27 09:52:18 -0700663 }
664
Florin Coras05ecfcc2018-12-12 18:19:39 -0800665 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700666 return sid;
667}
668
Florin Corasdfae9f92019-02-20 19:48:31 -0800669static void
670vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
671{
672 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
673 vcl_session_t *s;
674
675 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000676 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800677 {
678 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
679 return;
680 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700681 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000682 {
683 /* Connected udp listener */
684 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700685 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000686 return;
687
688 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
689 return;
690 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800691
692 if (mp->retval)
693 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700694 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800695
696 if (mp->context != wrk->wrk_index)
697 VDBG (0, "wrong context");
698
699 vcl_session_table_del_vpp_handle (wrk, mp->handle);
700 vcl_session_free (wrk, s);
701}
702
Florin Coras68b7e582020-01-21 18:33:23 -0800703static void
704vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
705{
706 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
707 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800708 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800709
710 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
711 if (!s)
712 {
713 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
714 return;
715 }
716
Florin Coras1bb74942021-02-10 15:26:37 -0800717 /* Only validate if a value is provided */
718 if (mp->segment_handle != SESSION_INVALID_HANDLE)
Florin Corasc547e912020-12-08 17:50:45 -0800719 {
Florin Coras1bb74942021-02-10 15:26:37 -0800720 fs_index = vcl_segment_table_lookup (mp->segment_handle);
721 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
722 {
723 VDBG (0, "segment %lx for session %u is not mounted!",
724 mp->segment_handle, s->session_index);
725 s->session_state = VCL_STATE_DETACHED;
726 return;
727 }
Florin Corasc547e912020-12-08 17:50:45 -0800728 }
729
Florin Coras57660d92020-04-04 22:45:34 +0000730 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800731
732 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
733 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800734
Florin Coras68b7e582020-01-21 18:33:23 -0800735 vcl_session_table_del_vpp_handle (wrk, mp->handle);
736 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
737
738 /* Generate new tx event if we have outstanding data */
739 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800740 app_send_io_evt_to_vpp (s->vpp_evt_q,
741 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800742 SESSION_IO_EVT_TX, SVM_Q_WAIT);
743
Florin Coras57660d92020-04-04 22:45:34 +0000744 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800745 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800746}
747
Florin Coras3c7d4f92018-12-14 11:28:43 -0800748static vcl_session_t *
749vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
750{
751 vcl_session_msg_t *vcl_msg;
752 vcl_session_t *session;
753
754 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
755 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800756 VWRN ("session overlap handle %lu state %u!", msg->handle,
757 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800758
759 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
760 if (!session)
761 {
762 VERR ("couldn't find listen session: listener handle %llx",
763 msg->listener_handle);
764 return 0;
765 }
766
767 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000768 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800769 vcl_msg->accepted_msg = *msg;
770 /* Session handle points to listener until fully accepted by app */
771 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
772
773 return session;
774}
775
776static vcl_session_t *
777vcl_session_disconnected_handler (vcl_worker_t * wrk,
778 session_disconnected_msg_t * msg)
779{
780 vcl_session_t *session;
781
782 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
783 if (!session)
784 {
785 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
786 return 0;
787 }
788
Florin Corasadcfb152020-02-12 08:50:29 +0000789 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700790 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000791 return 0;
792
Florin Coras3c7d4f92018-12-14 11:28:43 -0800793 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700794 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800795 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800796 if (!vcl_flag_accepted_session (session, msg->handle,
797 VCL_ACCEPTED_F_CLOSED))
798 VDBG (0, "session was not accepted!");
799 return 0;
800 }
801
Florin Corasadcfb152020-02-12 08:50:29 +0000802 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700803 if (session->session_state != VCL_STATE_DISCONNECT)
804 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000805
Florin Coras3c7d4f92018-12-14 11:28:43 -0800806 return session;
807}
808
liuyacan534468e2021-05-09 03:50:40 +0000809int
810vppcom_session_shutdown (uint32_t session_handle)
811{
812 vcl_worker_t *wrk = vcl_worker_get_current ();
813 vcl_session_t *session;
814 vcl_session_state_t state;
815 u64 vpp_handle;
816
817 session = vcl_session_get_w_handle (wrk, session_handle);
818 if (PREDICT_FALSE (!session))
819 return VPPCOM_EBADFD;
820
821 vpp_handle = session->vpp_handle;
822 state = session->session_state;
823
824 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
825 vpp_handle, state, vppcom_session_state_str (state));
826
827 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
828 {
829 VDBG (0, "ERROR: Cannot shutdown a listen socket!");
830 return VPPCOM_EBADFD;
831 }
832
833 if (PREDICT_TRUE (state == VCL_STATE_READY))
834 {
835 VDBG (1, "session %u [0x%llx]: sending shutdown...",
836 session->session_index, vpp_handle);
837
838 vcl_send_session_shutdown (wrk, session);
839 session->flags |= VCL_SESSION_F_SHUTDOWN;
840 }
841
842 return VPPCOM_OK;
843}
844
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700845static int
846vppcom_session_disconnect (u32 session_handle)
847{
848 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700849 vcl_session_t *session, *listen_session;
850 vcl_session_state_t state;
851 u64 vpp_handle;
852
853 session = vcl_session_get_w_handle (wrk, session_handle);
854 if (!session)
855 return VPPCOM_EBADFD;
856
857 vpp_handle = session->vpp_handle;
858 state = session->session_state;
859
860 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
861 vpp_handle, state, vppcom_session_state_str (state));
862
863 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
864 {
865 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
866 return VPPCOM_EBADFD;
867 }
868
869 if (state == VCL_STATE_VPP_CLOSING)
870 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800871 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700872 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
873 session->session_index, vpp_handle);
874 }
875 else
876 {
877 /* Session doesn't have an event queue yet. Probably a non-blocking
878 * connect. Wait for the reply */
879 if (PREDICT_FALSE (!session->vpp_evt_q))
880 return VPPCOM_OK;
881
882 VDBG (1, "session %u [0x%llx]: sending disconnect...",
883 session->session_index, vpp_handle);
884 vcl_send_session_disconnect (wrk, session);
885 }
886
887 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
888 {
889 listen_session = vcl_session_get (wrk, session->listener_index);
890 listen_session->n_accepted_sessions--;
891 }
892
893 return VPPCOM_OK;
894}
895
Florin Coras30e79c22019-01-02 19:31:22 -0800896static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700897vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
898{
899 session_cleanup_msg_t *msg;
900 vcl_session_t *session;
901
902 msg = (session_cleanup_msg_t *) data;
903 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
904 if (!session)
905 {
906 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
907 return;
908 }
909
Florin Coras36d49392020-04-24 23:00:11 +0000910 if (msg->type == SESSION_CLEANUP_TRANSPORT)
911 {
912 /* Transport was cleaned up before we confirmed close. Probably the
913 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700914 * Confirm close to make sure everything is cleaned up.
915 * Move to undetermined state to ensure that the session is not
916 * removed before both vpp and the app cleanup.
917 * - If the app closes first, the session is moved to CLOSED state
918 * and the session cleanup notification from vpp removes the
919 * session.
920 * - If vpp cleans up the session first, the session is moved to
921 * DETACHED state lower and subsequently the close from the app
922 * frees the session
923 */
924 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700925 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700926 vppcom_session_disconnect (vcl_session_handle (session));
927 session->session_state = VCL_STATE_UPDATED;
928 }
929 else if (session->session_state == VCL_STATE_DISCONNECT)
930 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800931 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700932 session->session_state = VCL_STATE_UPDATED;
933 }
Florin Coras36d49392020-04-24 23:00:11 +0000934 return;
935 }
936
Florin Coras9ace36d2019-10-28 13:14:17 -0700937 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000938 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700939 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000940 {
941 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700942 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000943 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
944 return;
945 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700946 vcl_session_free (wrk, session);
947}
948
949static void
Florin Coras30e79c22019-01-02 19:31:22 -0800950vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
951{
952 session_req_worker_update_msg_t *msg;
953 vcl_session_t *s;
954
955 msg = (session_req_worker_update_msg_t *) data;
956 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
957 if (!s)
958 return;
959
960 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
961}
962
963static void
964vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
965{
966 session_worker_update_reply_msg_t *msg;
967 vcl_session_t *s;
968
969 msg = (session_worker_update_reply_msg_t *) data;
970 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
971 if (!s)
972 {
973 VDBG (0, "unknown handle 0x%llx", msg->handle);
974 return;
975 }
Florin Coras30e79c22019-01-02 19:31:22 -0800976
Florin Coras5f45e012019-01-23 09:21:30 -0800977 if (s->rx_fifo)
978 {
Florin Corasc547e912020-12-08 17:50:45 -0800979 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800980 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800981 {
982 VDBG (0, "failed to attach fifos for %u", s->session_index);
983 return;
984 }
Florin Coras5f45e012019-01-23 09:21:30 -0800985 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700986 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800987
Florin Coras30e79c22019-01-02 19:31:22 -0800988 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
989 s->vpp_handle, wrk->wrk_index);
990}
991
Florin Coras935ce752020-09-08 22:43:47 -0700992static int
993vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
994{
995
996 if (vcm->cfg.vpp_app_socket_api)
997 return vcl_sapi_recv_fds (wrk, fds, n_fds);
998
999 return vcl_bapi_recv_fds (wrk, fds, n_fds);
1000}
1001
Florin Corasc4c4cf52019-08-24 18:17:34 -07001002static void
1003vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
1004{
1005 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
1006 session_app_add_segment_msg_t *msg;
1007 u64 segment_handle;
1008 int fd = -1;
1009
1010 msg = (session_app_add_segment_msg_t *) data;
1011
1012 if (msg->fd_flags)
1013 {
Florin Coras935ce752020-09-08 22:43:47 -07001014 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -07001015 seg_type = SSVM_SEGMENT_MEMFD;
1016 }
1017
1018 segment_handle = msg->segment_handle;
1019 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
1020 {
1021 clib_warning ("invalid segment handle");
1022 return;
1023 }
1024
1025 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
1026 seg_type, fd))
1027 {
1028 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
1029 return;
1030 }
1031
1032 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
1033 msg->segment_size);
1034}
1035
1036static void
1037vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
1038{
1039 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
1040 vcl_segment_detach (msg->segment_handle);
1041 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
1042}
1043
Florin Coras40c07ce2020-07-16 20:46:17 -07001044static void
1045vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
1046{
1047 if (!vcm->wrk_rpc_fn)
1048 return;
1049
hanlina3a48962020-07-13 11:09:15 +08001050 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -07001051}
1052
Florin Coras04ae8272021-04-12 19:55:37 -07001053static void
1054vcl_session_transport_attr_reply_handler (vcl_worker_t *wrk, void *data)
1055{
1056 session_transport_attr_reply_msg_t *mp;
1057
1058 if (!wrk->session_attr_op)
1059 return;
1060
1061 mp = (session_transport_attr_reply_msg_t *) data;
1062
1063 wrk->session_attr_op_rv = mp->retval;
1064 wrk->session_attr_op = 0;
1065 wrk->session_attr_rv = mp->attr;
1066}
1067
Florin Coras86f04502018-09-12 16:08:01 -07001068static int
1069vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -07001070{
Florin Coras54693d22018-07-17 10:46:29 -07001071 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07001072 session_connected_msg_t *connected_msg;
1073 session_reset_msg_t *reset_msg;
1074 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -07001075 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -07001076 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -07001077
1078 switch (e->event_type)
1079 {
Florin Coras653e43f2019-03-04 10:56:23 -08001080 case SESSION_IO_EVT_RX:
1081 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001082 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001083 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001084 break;
Florin Coras86f04502018-09-12 16:08:01 -07001085 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001086 break;
Florin Corasb242d312020-10-26 15:35:40 -07001087 case SESSION_CTRL_EVT_BOUND:
1088 /* We can only wait for only one listen so not postponed */
1089 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1090 break;
Florin Coras54693d22018-07-17 10:46:29 -07001091 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001092 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1093 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1094 {
1095 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1096 *ecpy = *e;
1097 ecpy->postponed = 1;
1098 ecpy->session_index = s->session_index;
1099 }
Florin Coras54693d22018-07-17 10:46:29 -07001100 break;
1101 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001102 connected_msg = (session_connected_msg_t *) e->data;
1103 sid = vcl_session_connected_handler (wrk, connected_msg);
1104 if (!(s = vcl_session_get (wrk, sid)))
1105 break;
1106 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1107 {
1108 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1109 *ecpy = *e;
1110 ecpy->postponed = 1;
1111 ecpy->session_index = s->session_index;
1112 }
Florin Coras54693d22018-07-17 10:46:29 -07001113 break;
1114 case SESSION_CTRL_EVT_DISCONNECTED:
1115 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001116 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1117 break;
1118 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1119 {
1120 vec_add1 (wrk->unhandled_evts_vector, *e);
1121 break;
1122 }
1123 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001124 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001125 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1126 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001127 break;
1128 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001129 reset_msg = (session_reset_msg_t *) e->data;
1130 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1131 break;
1132 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1133 {
1134 vec_add1 (wrk->unhandled_evts_vector, *e);
1135 break;
1136 }
Florin Coras134a9962018-08-28 11:32:04 -07001137 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001138 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001139 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1140 vcl_session_unlisten_reply_handler (wrk, e->data);
1141 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001142 case SESSION_CTRL_EVT_MIGRATED:
1143 vcl_session_migrated_handler (wrk, e->data);
1144 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001145 case SESSION_CTRL_EVT_CLEANUP:
1146 vcl_session_cleanup_handler (wrk, e->data);
1147 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001148 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1149 vcl_session_req_worker_update_handler (wrk, e->data);
1150 break;
1151 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1152 vcl_session_worker_update_reply_handler (wrk, e->data);
1153 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001154 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1155 vcl_session_app_add_segment_handler (wrk, e->data);
1156 break;
1157 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1158 vcl_session_app_del_segment_handler (wrk, e->data);
1159 break;
hanlina3a48962020-07-13 11:09:15 +08001160 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001161 vcl_worker_rpc_handler (wrk, e->data);
1162 break;
Florin Coras04ae8272021-04-12 19:55:37 -07001163 case SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY:
1164 vcl_session_transport_attr_reply_handler (wrk, e->data);
1165 break;
Florin Coras54693d22018-07-17 10:46:29 -07001166 default:
1167 clib_warning ("unhandled %u", e->event_type);
1168 }
1169 return VPPCOM_OK;
1170}
1171
Florin Coras30e79c22019-01-02 19:31:22 -08001172static int
Florin Coras697faea2018-06-27 17:10:49 -07001173vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001174 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001175 f64 wait_for_time)
1176{
Florin Coras134a9962018-08-28 11:32:04 -07001177 vcl_worker_t *wrk = vcl_worker_get_current ();
1178 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001179 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001180 svm_msg_q_msg_t msg;
1181 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001182
Florin Coras697faea2018-06-27 17:10:49 -07001183 do
Dave Wallace543852a2017-08-03 02:11:34 -04001184 {
Florin Coras134a9962018-08-28 11:32:04 -07001185 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001186 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001187 {
Florin Coras070453d2018-08-24 17:04:27 -07001188 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001189 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001190 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001191 {
Florin Coras697faea2018-06-27 17:10:49 -07001192 return VPPCOM_OK;
1193 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001194 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001195 {
Florin Coras697faea2018-06-27 17:10:49 -07001196 return VPPCOM_ECONNREFUSED;
1197 }
Florin Coras54693d22018-07-17 10:46:29 -07001198
Florin Coras134a9962018-08-28 11:32:04 -07001199 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001200 {
1201 usleep (100);
1202 continue;
1203 }
Florin Coras134a9962018-08-28 11:32:04 -07001204 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001205 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001206 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001207 }
Florin Coras134a9962018-08-28 11:32:04 -07001208 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001209
Florin Coras05ecfcc2018-12-12 18:19:39 -08001210 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001211 vppcom_session_state_str (state));
1212 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001213
Florin Coras697faea2018-06-27 17:10:49 -07001214 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001215}
1216
Florin Coras30e79c22019-01-02 19:31:22 -08001217static void
1218vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1219{
Florin Coras288eaab2019-02-03 15:26:14 -08001220 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001221 vcl_session_t *s;
1222 u32 *sip;
1223
1224 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1225 return;
1226
1227 vec_foreach (sip, wrk->pending_session_wrk_updates)
1228 {
1229 s = vcl_session_get (wrk, *sip);
1230 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1231 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001232 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1233 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001234 s->session_state = state;
1235 }
1236 vec_reset_length (wrk->pending_session_wrk_updates);
1237}
1238
Florin Corasf9240dc2019-01-15 08:03:17 -08001239void
Florin Coras5398dfb2021-01-25 20:31:27 -08001240vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001241{
Florin Coras30e79c22019-01-02 19:31:22 -08001242 svm_msg_q_msg_t *msg;
1243 session_event_t *e;
1244 svm_msg_q_t *mq;
1245 int i;
1246
1247 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001248 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001249
1250 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1251 {
1252 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1253 e = svm_msg_q_msg_data (mq, msg);
1254 vcl_handle_mq_event (wrk, e);
1255 svm_msg_q_free_msg (mq, msg);
1256 }
1257 vec_reset_length (wrk->mq_msg_vector);
1258 vcl_handle_pending_wrk_updates (wrk);
1259}
1260
Florin Coras5398dfb2021-01-25 20:31:27 -08001261void
1262vcl_flush_mq_events (void)
1263{
1264 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1265}
1266
Florin Coras697faea2018-06-27 17:10:49 -07001267static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001268vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001269{
Florin Coras134a9962018-08-28 11:32:04 -07001270 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001271 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001272 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001273 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001274
Florin Corasab2f6db2018-08-31 14:31:41 -07001275 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001276 if (!session)
1277 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001278
Florin Corasaaff5ee2019-07-08 13:00:00 -07001279 /* Flush pending accept events, if any */
1280 while (clib_fifo_elts (session->accept_evts_fifo))
1281 {
1282 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1283 accepted_msg = &evt->accepted_msg;
1284 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1285 vcl_send_session_accepted_reply (session->vpp_evt_q,
1286 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001287 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001288 }
1289 clib_fifo_free (session->accept_evts_fifo);
1290
Florin Coras458089b2019-08-21 16:20:44 -07001291 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001292
Florin Coras5e062572019-03-14 19:07:51 -07001293 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001294 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001295 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001296
1297 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001298 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001299
Florin Coras070453d2018-08-24 17:04:27 -07001300 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001301}
1302
Florin Coras940f78f2018-11-30 12:11:20 -08001303/**
1304 * Handle app exit
1305 *
1306 * Notify vpp of the disconnect and mark the worker as free. If we're the
1307 * last worker, do a full cleanup otherwise, since we're probably a forked
1308 * child, avoid syscalls as much as possible. We might've lost privileges.
1309 */
1310void
1311vppcom_app_exit (void)
1312{
1313 if (!pool_elts (vcm->workers))
1314 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001315 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1316 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001317 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001318}
1319
Florin Coras935ce752020-09-08 22:43:47 -07001320static int
1321vcl_api_attach (void)
1322{
1323 if (vcm->cfg.vpp_app_socket_api)
1324 return vcl_sapi_attach ();
1325
1326 return vcl_bapi_attach ();
1327}
1328
1329static void
1330vcl_api_detach (vcl_worker_t * wrk)
1331{
1332 vcl_send_app_detach (wrk);
1333
1334 if (vcm->cfg.vpp_app_socket_api)
1335 return vcl_sapi_detach (wrk);
1336
1337 return vcl_bapi_disconnect_from_vpp ();
1338}
1339
Dave Wallace543852a2017-08-03 02:11:34 -04001340/*
1341 * VPPCOM Public API functions
1342 */
1343int
Florin Coras66ec4672020-06-15 07:59:40 -07001344vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001345{
Dave Wallace543852a2017-08-03 02:11:34 -04001346 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001347 int rv;
1348
Florin Coras47c40e22018-11-26 17:01:36 -08001349 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001350 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001351 VDBG (1, "already initialized");
1352 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001353 }
1354
Florin Coras47c40e22018-11-26 17:01:36 -08001355 vcm->is_init = 1;
1356 vppcom_cfg (&vcm->cfg);
1357 vcl_cfg = &vcm->cfg;
1358
1359 vcm->main_cpu = pthread_self ();
1360 vcm->main_pid = getpid ();
1361 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001362 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1363 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001364 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1365 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001366 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001367 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001368 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001369
1370 /* Allocate default worker */
1371 vcl_worker_alloc_and_init ();
1372
Florin Coras935ce752020-09-08 22:43:47 -07001373 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001374 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001375
1376 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001377 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001378
1379 return VPPCOM_OK;
1380}
1381
1382void
1383vppcom_app_destroy (void)
1384{
Florin Corasdc0ded72020-04-30 02:59:55 +00001385 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001386 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001387
Florin Coras940f78f2018-11-30 12:11:20 -08001388 if (!pool_elts (vcm->workers))
1389 return;
1390
Florin Coras0d427d82018-06-27 03:24:07 -07001391 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001392
Florin Corasdc0ded72020-04-30 02:59:55 +00001393 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001394
Florin Corasce815de2020-04-16 18:47:27 +00001395 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001396 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001397 if (current_wrk != wrk)
1398 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001399 }
Florin Corasce815de2020-04-16 18:47:27 +00001400 /* *INDENT-ON* */
1401
Florin Coras935ce752020-09-08 22:43:47 -07001402 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001403 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1404
Florin Coras0d427d82018-06-27 03:24:07 -07001405 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001406
1407 /*
1408 * Free the heap and fix vcm
1409 */
1410 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001411 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001412
1413 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001414 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001415}
1416
1417int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001418vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001419{
Florin Coras134a9962018-08-28 11:32:04 -07001420 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001421 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001422
Florin Coras134a9962018-08-28 11:32:04 -07001423 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001424
Florin Coras7e12d942018-06-27 14:32:43 -07001425 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001426 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001427 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001428 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001429
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001430 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001431 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001432
Florin Coras7e12d942018-06-27 14:32:43 -07001433 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1434 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001435
Florin Coras5e062572019-03-14 19:07:51 -07001436 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001437
Florin Coras134a9962018-08-28 11:32:04 -07001438 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001439}
1440
1441int
Florin Corasc127d5a2020-10-14 16:35:58 -07001442vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001443 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001444{
Florin Coras134a9962018-08-28 11:32:04 -07001445 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001446
Florin Coras6c3b2182020-10-19 18:36:48 -07001447 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001448
Florin Coras6c3b2182020-10-19 18:36:48 -07001449 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001450 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001451 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001452 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001453 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001454 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001455 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001456 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001457 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1458 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001459 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001460 }
Florin Corase4306b62020-10-28 12:51:10 -07001461 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001462 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001463
Florin Coras6c3b2182020-10-19 18:36:48 -07001464 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001465 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001466 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001467 if (rv < 0)
1468 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001469 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1470 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001471 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001472
Florin Coras9ace36d2019-10-28 13:14:17 -07001473 if (!do_disconnect)
1474 {
1475 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001476 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001477 goto cleanup;
1478 }
1479
Florin Coras6c3b2182020-10-19 18:36:48 -07001480 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001481 {
1482 rv = vppcom_session_unbind (sh);
1483 if (PREDICT_FALSE (rv < 0))
1484 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001485 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001486 vppcom_retval_str (rv));
1487 return rv;
1488 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001489 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001490 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001491 {
1492 rv = vppcom_session_disconnect (sh);
1493 if (PREDICT_FALSE (rv < 0))
1494 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001495 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001496 rv, vppcom_retval_str (rv));
1497 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001498 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001499 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001500 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001501 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001502 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001503 {
1504 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001505 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001506 goto free_session;
1507 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001508
Florin Corasc127d5a2020-10-14 16:35:58 -07001509 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001510
Florin Coras9ace36d2019-10-28 13:14:17 -07001511 /* Session is removed only after vpp confirms the disconnect */
1512 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001513
Florin Corasf9240dc2019-01-15 08:03:17 -08001514cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001515 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001516free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001517 vcl_session_free (wrk, s);
1518 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001519
Dave Wallace543852a2017-08-03 02:11:34 -04001520 return rv;
1521}
1522
1523int
Florin Corasf9240dc2019-01-15 08:03:17 -08001524vppcom_session_close (uint32_t session_handle)
1525{
1526 vcl_worker_t *wrk = vcl_worker_get_current ();
1527 vcl_session_t *session;
1528
1529 session = vcl_session_get_w_handle (wrk, session_handle);
1530 if (!session)
1531 return VPPCOM_EBADFD;
1532 return vcl_session_cleanup (wrk, session, session_handle,
1533 1 /* do_disconnect */ );
1534}
1535
1536int
Florin Coras134a9962018-08-28 11:32:04 -07001537vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001538{
Florin Coras134a9962018-08-28 11:32:04 -07001539 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001540 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001541
1542 if (!ep || !ep->ip)
1543 return VPPCOM_EINVAL;
1544
Florin Coras134a9962018-08-28 11:32:04 -07001545 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001546 if (!session)
1547 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001548
Florin Coras6c3b2182020-10-19 18:36:48 -07001549 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001550 {
Florin Coras5e062572019-03-14 19:07:51 -07001551 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1552 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001553 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001554 }
1555
Florin Coras7e12d942018-06-27 14:32:43 -07001556 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001557 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001558 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1559 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001560 else
Dave Barach178cf492018-11-13 16:34:13 -05001561 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1562 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001563 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001564
Florin Coras5e062572019-03-14 19:07:51 -07001565 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1566 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001567 session->transport.is_ip4 ? "IPv4" : "IPv6",
1568 format_ip46_address, &session->transport.lcl_ip,
1569 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1570 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001571 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001572 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001573
1574 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001575 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001576
Florin Coras070453d2018-08-24 17:04:27 -07001577 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001578}
1579
1580int
Florin Coras134a9962018-08-28 11:32:04 -07001581vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001582{
Florin Coras134a9962018-08-28 11:32:04 -07001583 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001584 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001585 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001586 int rv;
1587
Florin Coras134a9962018-08-28 11:32:04 -07001588 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001589 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001590 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001591
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001592 if (q_len == 0 || q_len == ~0)
1593 q_len = vcm->cfg.listen_queue_size;
1594
Dave Wallaceee45d412017-11-24 21:44:06 -05001595 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001596 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001597 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001598 VDBG (0, "session %u [0x%llx]: already in listen state!",
1599 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001600 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001601 }
1602
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001603 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001604
Florin Coras070453d2018-08-24 17:04:27 -07001605 /*
1606 * Send listen request to vpp and wait for reply
1607 */
Florin Coras458089b2019-08-21 16:20:44 -07001608 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001609 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001610 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001611 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001612
Florin Coras070453d2018-08-24 17:04:27 -07001613 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001614 {
Florin Coras134a9962018-08-28 11:32:04 -07001615 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001616 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1617 listen_sh, listen_session->vpp_handle, rv,
1618 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001619 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001620 }
1621
Florin Coras070453d2018-08-24 17:04:27 -07001622 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001623}
1624
Florin Coras134a9962018-08-28 11:32:04 -07001625static int
Florin Coras5e062572019-03-14 19:07:51 -07001626validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001627{
Florin Coras6c3b2182020-10-19 18:36:48 -07001628 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001629 {
Florin Coras5e062572019-03-14 19:07:51 -07001630 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1631 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001632 return VPPCOM_EBADFD;
1633 }
1634
Florin Corasc127d5a2020-10-14 16:35:58 -07001635 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001636 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001637 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001638 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001639 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001640 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001641 return VPPCOM_EBADFD;
1642 }
1643 return VPPCOM_OK;
1644}
1645
1646int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001647vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1648{
1649 if (!strcmp (proto_str, "TCP"))
1650 *proto = VPPCOM_PROTO_TCP;
1651 else if (!strcmp (proto_str, "tcp"))
1652 *proto = VPPCOM_PROTO_TCP;
1653 else if (!strcmp (proto_str, "UDP"))
1654 *proto = VPPCOM_PROTO_UDP;
1655 else if (!strcmp (proto_str, "udp"))
1656 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001657 else if (!strcmp (proto_str, "TLS"))
1658 *proto = VPPCOM_PROTO_TLS;
1659 else if (!strcmp (proto_str, "tls"))
1660 *proto = VPPCOM_PROTO_TLS;
1661 else if (!strcmp (proto_str, "QUIC"))
1662 *proto = VPPCOM_PROTO_QUIC;
1663 else if (!strcmp (proto_str, "quic"))
1664 *proto = VPPCOM_PROTO_QUIC;
Florin Coras4b47ee22020-11-19 13:38:26 -08001665 else if (!strcmp (proto_str, "DTLS"))
1666 *proto = VPPCOM_PROTO_DTLS;
1667 else if (!strcmp (proto_str, "dtls"))
1668 *proto = VPPCOM_PROTO_DTLS;
Florin Coras6621abf2021-01-06 17:35:17 -08001669 else if (!strcmp (proto_str, "SRTP"))
1670 *proto = VPPCOM_PROTO_SRTP;
1671 else if (!strcmp (proto_str, "srtp"))
1672 *proto = VPPCOM_PROTO_SRTP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001673 else
1674 return 1;
1675 return 0;
1676}
1677
1678int
Florin Coras134a9962018-08-28 11:32:04 -07001679vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001680 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001681{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001682 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001683 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001684 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001685 vcl_session_t *listen_session = 0;
1686 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001687 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001688 u8 is_nonblocking;
1689 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001690
Florin Coras5398dfb2021-01-25 20:31:27 -08001691again:
1692
Florin Coras134a9962018-08-28 11:32:04 -07001693 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001694 if (!listen_session)
1695 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001696
Florin Coras134a9962018-08-28 11:32:04 -07001697 listen_session_index = listen_session->session_index;
1698 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001699 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001700
Florin Coras54693d22018-07-17 10:46:29 -07001701 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001702 {
Florin Coras54693d22018-07-17 10:46:29 -07001703 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001704 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001705 accepted_msg = evt->accepted_msg;
1706 goto handle;
1707 }
1708
Florin Corasac422d62020-10-19 20:51:36 -07001709 is_nonblocking = vcl_session_has_attr (listen_session,
1710 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001711 while (1)
1712 {
Carl Smith592a9092019-11-12 14:57:37 +13001713 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1714 return VPPCOM_EAGAIN;
1715
Florin Coras5398dfb2021-01-25 20:31:27 -08001716 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1717 vcl_worker_flush_mq_events (wrk);
1718 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001719 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001720
Florin Coras54693d22018-07-17 10:46:29 -07001721handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001722
Florin Coras00cca802019-06-06 09:38:44 -07001723 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1724 listen_session_index);
1725 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1726 return VPPCOM_ECONNABORTED;
1727
Florin Coras134a9962018-08-28 11:32:04 -07001728 listen_session = vcl_session_get (wrk, listen_session_index);
1729 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001730
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001731 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001732 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001733
Florin Coras5e062572019-03-14 19:07:51 -07001734 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1735 " flags %d, is_nonblocking %u", listen_session->session_index,
1736 listen_session->vpp_handle, client_session_index,
1737 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001738 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001739
Dave Wallace048b1d62018-01-03 22:24:41 -05001740 if (ep)
1741 {
Florin Coras7e12d942018-06-27 14:32:43 -07001742 ep->is_ip4 = client_session->transport.is_ip4;
1743 ep->port = client_session->transport.rmt_port;
1744 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001745 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1746 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001747 else
Dave Barach178cf492018-11-13 16:34:13 -05001748 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1749 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001750 }
Dave Wallace60caa062017-11-10 17:07:13 -05001751
Florin Coras05ecfcc2018-12-12 18:19:39 -08001752 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001753 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001754 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001755 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001756 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001757 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001758 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001759 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001760 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001761 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1762 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001763
Florin Coras3c7d4f92018-12-14 11:28:43 -08001764 /*
1765 * Session might have been closed already
1766 */
1767 if (accept_flags)
1768 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001769 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001770 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001771 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001772 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001773 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001774 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001775}
1776
1777int
Florin Coras134a9962018-08-28 11:32:04 -07001778vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001779{
Florin Coras134a9962018-08-28 11:32:04 -07001780 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001781 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001782 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001783 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001784
Florin Coras134a9962018-08-28 11:32:04 -07001785 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001786 if (!session)
1787 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001788 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001789
Florin Coras6c3b2182020-10-19 18:36:48 -07001790 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001791 {
Florin Coras5e062572019-03-14 19:07:51 -07001792 VDBG (0, "ERROR: cannot connect epoll session %u!",
1793 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001794 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001795 }
1796
Florin Corasc127d5a2020-10-14 16:35:58 -07001797 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001798 {
Florin Coras7baeb712019-01-04 17:05:43 -08001799 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001800 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001801 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001802 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001803 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001804 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001805 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001806 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001807 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001808 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001809 }
1810
Florin Coras0a1e1832020-03-29 18:54:04 +00001811 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001812 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001813 {
1814 if (session->session_type != VPPCOM_PROTO_UDP)
1815 return VPPCOM_EINVAL;
1816 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001817 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001818 }
1819
Florin Coras7e12d942018-06-27 14:32:43 -07001820 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001821 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001822 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001823 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001824 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001825
Florin Coras0a1e1832020-03-29 18:54:04 +00001826 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001827 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001828 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001829 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001830 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001831 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001832 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001833 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001834 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001835
Florin Coras458089b2019-08-21 16:20:44 -07001836 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001837
Florin Corasac422d62020-10-19 20:51:36 -07001838 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001839 {
fanyfa49d59d2020-10-13 17:07:16 +08001840 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001841 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001842 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001843 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001844 return VPPCOM_EINPROGRESS;
1845 }
Florin Coras57c88932019-08-29 12:03:17 -07001846
1847 /*
1848 * Wait for reply from vpp if blocking
1849 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001850 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001851 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001852
Florin Coras134a9962018-08-28 11:32:04 -07001853 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001854 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1855 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001856
Dave Wallace4878cbe2017-11-21 03:45:09 -05001857 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001858}
1859
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001860int
1861vppcom_session_stream_connect (uint32_t session_handle,
1862 uint32_t parent_session_handle)
1863{
1864 vcl_worker_t *wrk = vcl_worker_get_current ();
1865 vcl_session_t *session, *parent_session;
1866 u32 session_index, parent_session_index;
1867 int rv;
1868
1869 session = vcl_session_get_w_handle (wrk, session_handle);
1870 if (!session)
1871 return VPPCOM_EBADFD;
1872 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1873 if (!parent_session)
1874 return VPPCOM_EBADFD;
1875
1876 session_index = session->session_index;
1877 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001878 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001879 {
1880 VDBG (0, "ERROR: cannot connect epoll session %u!",
1881 session->session_index);
1882 return VPPCOM_EBADFD;
1883 }
1884
Florin Corasc127d5a2020-10-14 16:35:58 -07001885 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001886 {
1887 VDBG (0, "session handle %u [0x%llx]: session already "
1888 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1889 session_handle, session->vpp_handle,
1890 parent_session_handle, parent_session->vpp_handle,
1891 vppcom_proto_str (session->session_type), session->session_state,
1892 vppcom_session_state_str (session->session_state));
1893 return VPPCOM_OK;
1894 }
1895
1896 /* Connect to quic session specifics */
1897 session->transport.is_ip4 = parent_session->transport.is_ip4;
1898 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1899 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001900 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001901
1902 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1903 session_handle, parent_session_handle, parent_session->vpp_handle);
1904
1905 /*
1906 * Send connect request and wait for reply from vpp
1907 */
Florin Coras458089b2019-08-21 16:20:44 -07001908 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001909 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001910 vcm->cfg.session_timeout);
1911
1912 session->listener_index = parent_session_index;
1913 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001914 if (parent_session)
1915 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001916
1917 session = vcl_session_get (wrk, session_index);
1918 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1919 session->vpp_handle, rv ? "failed" : "succeeded");
1920
1921 return rv;
1922}
1923
Steven58f464e2017-10-25 12:33:12 -07001924static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001925vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001926 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001927{
Florin Coras134a9962018-08-28 11:32:04 -07001928 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001929 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001930 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001931 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001932 session_event_t *e;
1933 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001934 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001935
Florin Coras070453d2018-08-24 17:04:27 -07001936 if (PREDICT_FALSE (!buf))
1937 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001938
Florin Coras134a9962018-08-28 11:32:04 -07001939 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001940 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001941 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001942
Florin Coras6d0106e2019-01-29 20:11:58 -08001943 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001944 {
Florin Coras5e062572019-03-14 19:07:51 -07001945 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001946 s->session_index, s->vpp_handle, s->session_state,
1947 vppcom_session_state_str (s->session_state));
1948 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001949 }
1950
Florin Corasac422d62020-10-19 20:51:36 -07001951 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001952 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001953 mq = wrk->app_event_queue;
1954 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001955 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001956
Sirshak Das28aa5392019-02-05 01:33:33 -06001957 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001958 {
Florin Coras54693d22018-07-17 10:46:29 -07001959 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001960 {
Yu Pingb2955352019-12-04 06:49:04 +08001961 if (vcl_session_is_closing (s))
1962 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001963 if (is_ct)
1964 svm_fifo_unset_event (s->rx_fifo);
1965 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001966 return VPPCOM_EWOULDBLOCK;
1967 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001968 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001969 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001970 if (vcl_session_is_closing (s))
1971 return vcl_session_closing_error (s);
1972
Florin Corasd6894562020-10-28 00:37:15 -07001973 if (is_ct)
1974 svm_fifo_unset_event (s->rx_fifo);
1975 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001976
Florin Coras5398dfb2021-01-25 20:31:27 -08001977 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1978 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001979 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001980 }
Florin Coras54693d22018-07-17 10:46:29 -07001981
Florin Coras4a2c7942020-09-10 12:27:14 -07001982read_again:
1983
Florin Coras460dce62018-07-27 05:45:06 -07001984 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001985 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001986 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001987 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1988
1989 ASSERT (rv >= 0);
Florin Coras23368312021-06-04 16:28:18 -07001990
1991 if (peek)
1992 return rv;
1993
Florin Coras4a2c7942020-09-10 12:27:14 -07001994 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001995
Sirshak Das28aa5392019-02-05 01:33:33 -06001996 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001997 {
Florin Corasd6894562020-10-28 00:37:15 -07001998 if (is_ct)
1999 svm_fifo_unset_event (s->rx_fifo);
2000 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07002001 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07002002 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07002003 {
Florin Corasc34118b2020-08-12 18:58:25 -07002004 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2005 e->event_type = SESSION_IO_EVT_RX;
2006 e->session_index = s->session_index;
2007 }
2008 }
Florin Coras54fe32c2020-11-25 20:26:32 -08002009 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07002010 {
2011 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08002012 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07002013 buf += rv;
2014 n -= rv;
2015 goto read_again;
2016 }
Florin Coras41c9e042018-09-11 00:10:41 -07002017
Florin Coras17ec5772020-08-12 20:46:29 -07002018 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07002019 {
Florin Coras17ec5772020-08-12 20:46:29 -07002020 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08002021 app_send_io_evt_to_vpp (s->vpp_evt_q,
2022 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07002023 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07002024 }
2025
Florin Coras5e062572019-03-14 19:07:51 -07002026 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
2027 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002028
Florin Coras54693d22018-07-17 10:46:29 -07002029 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002030}
2031
Steven58f464e2017-10-25 12:33:12 -07002032int
Florin Coras134a9962018-08-28 11:32:04 -07002033vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002034{
Florin Coras134a9962018-08-28 11:32:04 -07002035 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07002036}
2037
2038static int
Florin Coras134a9962018-08-28 11:32:04 -07002039vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07002040{
Florin Coras134a9962018-08-28 11:32:04 -07002041 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07002042}
2043
Florin Coras2cba8532018-09-11 16:33:36 -07002044int
2045vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07002046 vppcom_data_segment_t * ds, uint32_t n_segments,
2047 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002048{
2049 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002050 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07002051 vcl_session_t *s = 0;
2052 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07002053 svm_msg_q_t *mq;
2054 u8 is_ct;
2055
2056 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002057 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002058 return VPPCOM_EBADFD;
2059
Florin Coras6d0106e2019-01-29 20:11:58 -08002060 if (PREDICT_FALSE (!vcl_session_is_open (s)))
2061 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07002062
Florin Corasac422d62020-10-19 20:51:36 -07002063 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07002064 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07002065 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07002066 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002067 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07002068
Sirshak Das28aa5392019-02-05 01:33:33 -06002069 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002070 {
2071 if (is_nonblocking)
2072 {
Florin Coras62877022020-10-28 21:22:04 -07002073 if (is_ct)
2074 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002075 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002076 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002077 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002078 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002079 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002080 if (vcl_session_is_closing (s))
2081 return vcl_session_closing_error (s);
2082
Florin Coras62877022020-10-28 21:22:04 -07002083 if (is_ct)
2084 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002085 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002086
Florin Coras5398dfb2021-01-25 20:31:27 -08002087 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2088 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07002089 }
2090 }
2091
Florin Corasd1cc38d2020-10-11 11:05:04 -07002092 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2093 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2094 if (n_read < 0)
2095 return VPPCOM_EAGAIN;
2096
2097 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2098 {
Florin Coras62877022020-10-28 21:22:04 -07002099 if (is_ct)
2100 svm_fifo_unset_event (s->rx_fifo);
2101 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002102 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002103 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002104 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002105 {
2106 session_event_t *e;
2107 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2108 e->event_type = SESSION_IO_EVT_RX;
2109 e->session_index = s->session_index;
2110 }
2111 }
2112
2113 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002114 return n_read;
2115}
2116
2117void
Florin Corasd68faf82020-09-25 15:18:13 -07002118vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002119{
2120 vcl_worker_t *wrk = vcl_worker_get_current ();
2121 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002122 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002123
2124 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002125 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002126 return;
2127
Florin Coras62877022020-10-28 21:22:04 -07002128 is_ct = vcl_session_is_ct (s);
2129 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002130
2131 ASSERT (s->rx_bytes_pending < n_bytes);
2132 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002133}
2134
Florin Coras7a2abce2020-04-05 19:25:44 +00002135always_inline u8
2136vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002137{
Florin Coras7a2abce2020-04-05 19:25:44 +00002138 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2139 if (is_dgram)
2140 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2141 else
2142 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002143}
2144
2145always_inline int
2146vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2147 size_t n, u8 is_flush, u8 is_dgram)
2148{
Florin Coras6d0106e2019-01-29 20:11:58 -08002149 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002150 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002151 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002152 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002153 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002154
Florin Coras0b0d28e2021-06-04 17:31:53 -07002155 /* Accept zero length writes but just return */
2156 if (PREDICT_FALSE (!n))
2157 return VPPCOM_OK;
2158
2159 if (PREDICT_FALSE (!buf))
2160 return VPPCOM_EFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002161
Florin Coras6c3b2182020-10-19 18:36:48 -07002162 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002163 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002164 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2165 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002166 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002167 }
2168
liuyacan534468e2021-05-09 03:50:40 +00002169 if (PREDICT_FALSE (!vcl_session_is_open (s) ||
Florin Coras0b0d28e2021-06-04 17:31:53 -07002170 (s->flags & VCL_SESSION_F_SHUTDOWN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002171 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002172 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2173 s->session_index, s->vpp_handle, s->session_state,
2174 vppcom_session_state_str (s->session_state));
2175 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002176 }
2177
Florin Coras0e88e852018-09-17 22:09:02 -07002178 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002179 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002180 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002181
Florin Coras653e43f2019-03-04 10:56:23 -08002182 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002183 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002184 {
Florin Coras54693d22018-07-17 10:46:29 -07002185 if (is_nonblocking)
2186 {
Florin Coras070453d2018-08-24 17:04:27 -07002187 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002188 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002189 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002190 {
Florin Coras2d379d82019-06-28 12:45:12 -07002191 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002192 if (vcl_session_is_closing (s))
2193 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002194
Florin Coras5398dfb2021-01-25 20:31:27 -08002195 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2196 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002197 }
Dave Wallace543852a2017-08-03 02:11:34 -04002198 }
Dave Wallace543852a2017-08-03 02:11:34 -04002199
Florin Coras653e43f2019-03-04 10:56:23 -08002200 et = SESSION_IO_EVT_TX;
2201 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002202 et = SESSION_IO_EVT_TX_FLUSH;
2203
Florin Coras7a2abce2020-04-05 19:25:44 +00002204 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002205 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002206 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002207 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002208 else
2209 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002210 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002211
Florin Coras14ed6df2019-03-06 21:13:42 -08002212 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002213 app_send_io_evt_to_vpp (
2214 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002215
Florin Coras56230092020-09-02 20:52:58 -07002216 /* The underlying fifo segment can run out of memory */
2217 if (PREDICT_FALSE (n_write < 0))
2218 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002219
Florin Coras6d0106e2019-01-29 20:11:58 -08002220 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2221 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002222
Florin Coras54693d22018-07-17 10:46:29 -07002223 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002224}
2225
Florin Coras42ceddb2018-12-12 10:56:01 -08002226int
2227vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2228{
Florin Coras7a2abce2020-04-05 19:25:44 +00002229 vcl_worker_t *wrk = vcl_worker_get_current ();
2230 vcl_session_t *s;
2231
2232 s = vcl_session_get_w_handle (wrk, session_handle);
2233 if (PREDICT_FALSE (!s))
2234 return VPPCOM_EBADFD;
2235
2236 return vppcom_session_write_inline (wrk, s, buf, n,
2237 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002238}
2239
Florin Corasb0f662f2018-12-27 14:51:46 -08002240int
2241vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2242{
Florin Coras7a2abce2020-04-05 19:25:44 +00002243 vcl_worker_t *wrk = vcl_worker_get_current ();
2244 vcl_session_t *s;
2245
2246 s = vcl_session_get_w_handle (wrk, session_handle);
2247 if (PREDICT_FALSE (!s))
2248 return VPPCOM_EBADFD;
2249
2250 return vppcom_session_write_inline (wrk, s, buf, n,
2251 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002252}
2253
Florin Corasc0737e92019-03-04 14:19:39 -08002254#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002255if (PREDICT_FALSE (!_s->rx_fifo)) \
2256 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002257if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2258 { \
2259 if (!vcl_session_is_ct (_s)) \
2260 { \
2261 svm_fifo_unset_event (_s->rx_fifo); \
2262 if (svm_fifo_is_empty (_s->rx_fifo)) \
2263 break; \
2264 } \
2265 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2266 { \
Florin Coras2f630182020-08-13 00:17:51 -07002267 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002268 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2269 break; \
2270 } \
2271 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002272
Florin Coras86f04502018-09-12 16:08:01 -07002273static void
2274vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2275 unsigned long n_bits, unsigned long *read_map,
2276 unsigned long *write_map,
2277 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002278{
2279 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002280 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002281 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002282 u32 sid;
2283
2284 switch (e->event_type)
2285 {
Florin Corasc0737e92019-03-04 14:19:39 -08002286 case SESSION_IO_EVT_RX:
2287 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002288 s = vcl_session_get (wrk, sid);
2289 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002290 break;
Florin Corasb242d312020-10-26 15:35:40 -07002291 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002292 if (sid < n_bits && read_map)
2293 {
David Johnsond9818dd2018-12-14 14:53:41 -05002294 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002295 *bits_set += 1;
2296 }
2297 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002298 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002299 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002300 s = vcl_session_get (wrk, sid);
2301 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002302 break;
2303 if (sid < n_bits && write_map)
2304 {
David Johnsond9818dd2018-12-14 14:53:41 -05002305 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002306 *bits_set += 1;
2307 }
2308 break;
Florin Coras86f04502018-09-12 16:08:01 -07002309 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002310 if (!e->postponed)
2311 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2312 else
2313 s = vcl_session_get (wrk, e->session_index);
2314 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002315 break;
Florin Corasb242d312020-10-26 15:35:40 -07002316 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002317 if (sid < n_bits && read_map)
2318 {
David Johnsond9818dd2018-12-14 14:53:41 -05002319 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002320 *bits_set += 1;
2321 }
2322 break;
2323 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002324 if (!e->postponed)
2325 {
2326 connected_msg = (session_connected_msg_t *) e->data;
2327 sid = vcl_session_connected_handler (wrk, connected_msg);
2328 }
2329 else
2330 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002331 if (sid == VCL_INVALID_SESSION_INDEX)
2332 break;
2333 if (sid < n_bits && write_map)
2334 {
2335 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2336 *bits_set += 1;
2337 }
Florin Coras86f04502018-09-12 16:08:01 -07002338 break;
2339 case SESSION_CTRL_EVT_DISCONNECTED:
2340 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002341 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2342 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002343 break;
Florin Corasb242d312020-10-26 15:35:40 -07002344 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002345 if (sid < n_bits && except_map)
2346 {
David Johnsond9818dd2018-12-14 14:53:41 -05002347 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002348 *bits_set += 1;
2349 }
2350 break;
2351 case SESSION_CTRL_EVT_RESET:
2352 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2353 if (sid < n_bits && except_map)
2354 {
David Johnsond9818dd2018-12-14 14:53:41 -05002355 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002356 *bits_set += 1;
2357 }
2358 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002359 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2360 vcl_session_unlisten_reply_handler (wrk, e->data);
2361 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002362 case SESSION_CTRL_EVT_MIGRATED:
2363 vcl_session_migrated_handler (wrk, e->data);
2364 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002365 case SESSION_CTRL_EVT_CLEANUP:
2366 vcl_session_cleanup_handler (wrk, e->data);
2367 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002368 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2369 vcl_session_worker_update_reply_handler (wrk, e->data);
2370 break;
2371 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2372 vcl_session_req_worker_update_handler (wrk, e->data);
2373 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002374 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2375 vcl_session_app_add_segment_handler (wrk, e->data);
2376 break;
2377 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2378 vcl_session_app_del_segment_handler (wrk, e->data);
2379 break;
hanlina3a48962020-07-13 11:09:15 +08002380 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002381 vcl_worker_rpc_handler (wrk, e->data);
2382 break;
Florin Coras86f04502018-09-12 16:08:01 -07002383 default:
2384 clib_warning ("unhandled: %u", e->event_type);
2385 break;
2386 }
2387}
2388
2389static int
2390vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2391 unsigned long n_bits, unsigned long *read_map,
2392 unsigned long *write_map, unsigned long *except_map,
2393 double time_to_wait, u32 * bits_set)
2394{
Florin Coras99368312018-08-02 10:45:44 -07002395 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002396 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002397 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002398
Florin Coras54693d22018-07-17 10:46:29 -07002399 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002400 {
Florin Coras54693d22018-07-17 10:46:29 -07002401 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002402 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002403
Florin Coras54693d22018-07-17 10:46:29 -07002404 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002405 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002406 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002407 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002408 else
2409 {
2410 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002411 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002412 }
2413 }
Florin Corase003a1b2019-06-05 10:47:16 -07002414 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002415
Florin Coras134a9962018-08-28 11:32:04 -07002416 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002417 {
Florin Coras134a9962018-08-28 11:32:04 -07002418 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002419 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002420 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2421 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002422 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002423 }
Florin Coras134a9962018-08-28 11:32:04 -07002424 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002425 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002426 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002427}
2428
Florin Coras99368312018-08-02 10:45:44 -07002429static int
Florin Coras294afe22019-01-07 17:49:17 -08002430vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2431 vcl_si_set * read_map, vcl_si_set * write_map,
2432 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002433 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002434{
Florin Coras14ed6df2019-03-06 21:13:42 -08002435 double wait = 0, start = 0;
2436
2437 if (!*bits_set)
2438 {
2439 wait = time_to_wait;
2440 start = clib_time_now (&wrk->clib_time);
2441 }
2442
2443 do
2444 {
2445 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2446 write_map, except_map, wait, bits_set);
2447 if (*bits_set)
2448 return *bits_set;
2449 if (wait == -1)
2450 continue;
2451
2452 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2453 }
2454 while (wait > 0);
2455
2456 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002457}
2458
2459static int
Florin Coras294afe22019-01-07 17:49:17 -08002460vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2461 vcl_si_set * read_map, vcl_si_set * write_map,
2462 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002463 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002464{
2465 vcl_mq_evt_conn_t *mqc;
2466 int __clib_unused n_read;
2467 int n_mq_evts, i;
2468 u64 buf;
2469
Florin Coras134a9962018-08-28 11:32:04 -07002470 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2471 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2472 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002473 for (i = 0; i < n_mq_evts; i++)
2474 {
Florin Coras134a9962018-08-28 11:32:04 -07002475 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002476 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002477 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002478 except_map, 0, bits_set);
2479 }
2480
2481 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2482}
2483
Dave Wallace543852a2017-08-03 02:11:34 -04002484int
Florin Coras294afe22019-01-07 17:49:17 -08002485vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2486 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002487{
Florin Coras54693d22018-07-17 10:46:29 -07002488 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002489 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002490 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002491 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002492
Dave Wallace7876d392017-10-19 03:53:57 -04002493 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002494 {
Florin Coras134a9962018-08-28 11:32:04 -07002495 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002496 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002497 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2498 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002499 }
Dave Wallace7876d392017-10-19 03:53:57 -04002500 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002501 {
Florin Coras134a9962018-08-28 11:32:04 -07002502 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002503 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002504 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2505 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002506 }
Dave Wallace7876d392017-10-19 03:53:57 -04002507 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002508 {
Florin Coras134a9962018-08-28 11:32:04 -07002509 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002510 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002511 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2512 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002513 }
2514
Florin Coras54693d22018-07-17 10:46:29 -07002515 if (!n_bits)
2516 return 0;
2517
2518 if (!write_map)
2519 goto check_rd;
2520
Florin Coras096a1d52021-01-27 15:33:51 -08002521 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2522 {
2523 if (!(s = vcl_session_get (wrk, sid)))
2524 {
2525 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2526 bits_set++;
2527 continue;
2528 }
Florin Coras54693d22018-07-17 10:46:29 -07002529
Florin Coras096a1d52021-01-27 15:33:51 -08002530 if (vcl_session_write_ready (s))
2531 {
2532 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2533 bits_set++;
2534 }
2535 else
2536 {
2537 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2538 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2539 }
2540 }
Florin Coras54693d22018-07-17 10:46:29 -07002541
2542check_rd:
2543 if (!read_map)
2544 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002545
Florin Coras096a1d52021-01-27 15:33:51 -08002546 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2547 {
2548 if (!(s = vcl_session_get (wrk, sid)))
2549 {
2550 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2551 bits_set++;
2552 continue;
2553 }
Florin Coras54693d22018-07-17 10:46:29 -07002554
Florin Coras096a1d52021-01-27 15:33:51 -08002555 if (vcl_session_read_ready (s))
2556 {
2557 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2558 bits_set++;
2559 }
2560 }
Florin Coras54693d22018-07-17 10:46:29 -07002561
2562check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002563
Florin Coras86f04502018-09-12 16:08:01 -07002564 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2565 {
2566 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2567 read_map, write_map, except_map, &bits_set);
2568 }
2569 vec_reset_length (wrk->unhandled_evts_vector);
2570
Florin Coras99368312018-08-02 10:45:44 -07002571 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002572 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002573 time_to_wait, &bits_set);
2574 else
Florin Coras134a9962018-08-28 11:32:04 -07002575 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002576 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002577
Dave Wallace543852a2017-08-03 02:11:34 -04002578 return (bits_set);
2579}
2580
Dave Wallacef7f809c2017-10-03 01:48:42 -04002581static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002582vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002583{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002584 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002585 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002586 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002587
Florin Corasc0737e92019-03-04 14:19:39 -08002588 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002589 return;
2590
Florin Coras6c3b2182020-10-19 18:36:48 -07002591 s = vcl_session_get_w_handle (wrk, vep_handle);
2592 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002593 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002594 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002595 goto done;
2596 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002597 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002598 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002599 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002600 goto done;
2601 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002602 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002603 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002604 "{\n"
2605 " is_vep = %u\n"
2606 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002607 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002608 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2609 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002610
Florin Coras7e5e62b2019-07-30 14:08:23 -07002611 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002612 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002613 s = vcl_session_get_w_handle (wrk, sh);
2614 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002615 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002616 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002617 goto done;
2618 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002619 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002620 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002621 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002622 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002623 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002624 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002625 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002626 goto done;
2627 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002628 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002629 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2630 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002631 sh, s->vep.vep_sh, vep_handle);
2632 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002633 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002634 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002635 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002636 " next_sh = 0x%x (%u)\n"
2637 " prev_sh = 0x%x (%u)\n"
2638 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002639 " ev.events = 0x%x\n"
2640 " ev.data.u64 = 0x%llx\n"
2641 " et_mask = 0x%x\n"
2642 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002643 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002644 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2645 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002646 }
2647 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002648
2649done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002650 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002651}
2652
2653int
2654vppcom_epoll_create (void)
2655{
Florin Coras134a9962018-08-28 11:32:04 -07002656 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002657 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002658
Florin Coras134a9962018-08-28 11:32:04 -07002659 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002660
Florin Coras6c3b2182020-10-19 18:36:48 -07002661 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002662 vep_session->vep.vep_sh = ~0;
2663 vep_session->vep.next_sh = ~0;
2664 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002665 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002666
Florin Corasa7a1a222018-12-30 17:11:31 -08002667 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2668 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002669
Florin Corasab2f6db2018-08-31 14:31:41 -07002670 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002671}
2672
2673int
Florin Coras134a9962018-08-28 11:32:04 -07002674vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675 struct epoll_event *event)
2676{
Florin Coras134a9962018-08-28 11:32:04 -07002677 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002678 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002679 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002680 vcl_session_t *s;
2681 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682
Florin Coras134a9962018-08-28 11:32:04 -07002683 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002684 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002685 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686 return VPPCOM_EINVAL;
2687 }
2688
Florin Coras134a9962018-08-28 11:32:04 -07002689 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002690 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002692 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002693 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002694 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002695 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002696 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002697 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002698 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002699 }
2700
Florin Coras134a9962018-08-28 11:32:04 -07002701 ASSERT (vep_session->vep.vep_sh == ~0);
2702 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002703
Florin Coras17ec5772020-08-12 20:46:29 -07002704 s = vcl_session_get_w_handle (wrk, session_handle);
2705 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002706 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002707 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002708 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002709 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002710 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002711 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002712 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002713 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002714 }
2715
2716 switch (op)
2717 {
2718 case EPOLL_CTL_ADD:
2719 if (PREDICT_FALSE (!event))
2720 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002721 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002722 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002723 }
Florin Coras2645f682021-06-04 15:59:41 -07002724 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2725 {
2726 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2727 rv = VPPCOM_EEXIST;
2728 goto done;
2729 }
Florin Coras134a9962018-08-28 11:32:04 -07002730 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002731 {
Florin Coras7e12d942018-06-27 14:32:43 -07002732 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002733 next_session = vcl_session_get_w_handle (wrk,
2734 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002735 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002736 {
Florin Coras5e062572019-03-14 19:07:51 -07002737 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002738 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002739 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002740 }
Florin Coras134a9962018-08-28 11:32:04 -07002741 ASSERT (next_session->vep.prev_sh == vep_handle);
2742 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743 }
Florin Coras17ec5772020-08-12 20:46:29 -07002744 s->vep.next_sh = vep_session->vep.next_sh;
2745 s->vep.prev_sh = vep_handle;
2746 s->vep.vep_sh = vep_handle;
2747 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2748 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002749 s->flags &= ~VCL_SESSION_F_IS_VEP;
2750 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002751 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002752
Florin Coras17ec5772020-08-12 20:46:29 -07002753 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2754 if (txf && (event->events & EPOLLOUT))
2755 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002756
Florin Coras6e3c1f82020-01-15 01:30:46 +00002757 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002758 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002759 {
2760 session_event_t e = { 0 };
2761 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002762 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002763 vec_add1 (wrk->unhandled_evts_vector, e);
2764 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002765 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002766 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002767 {
2768 session_event_t e = { 0 };
2769 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002770 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002771 vec_add1 (wrk->unhandled_evts_vector, e);
2772 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002773 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2774 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002775 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 break;
2777
2778 case EPOLL_CTL_MOD:
2779 if (PREDICT_FALSE (!event))
2780 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002781 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002782 rv = VPPCOM_EINVAL;
2783 goto done;
2784 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002785 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002786 {
Florin Coras5e062572019-03-14 19:07:51 -07002787 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002788 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002789 goto done;
2790 }
Florin Coras17ec5772020-08-12 20:46:29 -07002791 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002792 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002793 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002794 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002795 rv = VPPCOM_EINVAL;
2796 goto done;
2797 }
hanlin475c9d72019-12-26 11:44:28 +08002798
Florin Coras2645f682021-06-04 15:59:41 -07002799 /* Generate EPOLLOUT if session write ready nd event was not on */
2800 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2801 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002802 {
2803 session_event_t e = { 0 };
2804 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002805 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002806 vec_add1 (wrk->unhandled_evts_vector, e);
2807 }
Florin Coras2645f682021-06-04 15:59:41 -07002808 /* Generate EPOLLIN if session read ready and event was not on */
2809 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2810 (vcl_session_read_ready (s) > 0))
2811 {
2812 session_event_t e = { 0 };
2813 e.event_type = SESSION_IO_EVT_RX;
2814 e.session_index = s->session_index;
2815 vec_add1 (wrk->unhandled_evts_vector, e);
2816 }
Florin Coras17ec5772020-08-12 20:46:29 -07002817 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2818 s->vep.ev = *event;
2819 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002820 if (txf)
2821 {
2822 if (event->events & EPOLLOUT)
2823 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2824 else
2825 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2826 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002827 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2828 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002829 break;
2830
2831 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002832 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002833 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002834 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002835 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002836 goto done;
2837 }
Florin Coras17ec5772020-08-12 20:46:29 -07002838 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002839 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002840 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002841 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002842 rv = VPPCOM_EINVAL;
2843 goto done;
2844 }
2845
Florin Coras17ec5772020-08-12 20:46:29 -07002846 if (s->vep.prev_sh == vep_handle)
2847 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002848 else
2849 {
Florin Coras7e12d942018-06-27 14:32:43 -07002850 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002851 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002852 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002853 {
Florin Coras5e062572019-03-14 19:07:51 -07002854 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002855 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002856 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002857 }
Florin Coras134a9962018-08-28 11:32:04 -07002858 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002859 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002860 }
Florin Coras17ec5772020-08-12 20:46:29 -07002861 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002862 {
Florin Coras7e12d942018-06-27 14:32:43 -07002863 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002864 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002865 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002866 {
Florin Coras5e062572019-03-14 19:07:51 -07002867 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002868 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002869 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002870 }
Florin Coras134a9962018-08-28 11:32:04 -07002871 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002872 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002873 }
2874
Florin Coras17ec5772020-08-12 20:46:29 -07002875 memset (&s->vep, 0, sizeof (s->vep));
2876 s->vep.next_sh = ~0;
2877 s->vep.prev_sh = ~0;
2878 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002879 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002880
Florin Coras17ec5772020-08-12 20:46:29 -07002881 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2882 if (txf)
2883 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002884
Florin Coras5e062572019-03-14 19:07:51 -07002885 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002886 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002887 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002888 break;
2889
2890 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002891 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002892 rv = VPPCOM_EINVAL;
2893 }
2894
Florin Coras134a9962018-08-28 11:32:04 -07002895 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002896
2897done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002898 return rv;
2899}
2900
Florin Coras86f04502018-09-12 16:08:01 -07002901static inline void
2902vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2903 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002904{
2905 session_disconnected_msg_t *disconnected_msg;
2906 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002907 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002908 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002909 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002910 u8 add_event = 0;
2911
2912 switch (e->event_type)
2913 {
Florin Coras653e43f2019-03-04 10:56:23 -08002914 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002915 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002916 s = vcl_session_get (wrk, sid);
2917 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002918 break;
Florin Corasb242d312020-10-26 15:35:40 -07002919 vcl_fifo_rx_evt_valid_or_break (s);
2920 session_events = s->vep.ev.events;
2921 if (!(EPOLLIN & s->vep.ev.events)
2922 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002923 break;
2924 add_event = 1;
2925 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002926 session_evt_data = s->vep.ev.data.u64;
2927 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002928 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002929 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002930 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002931 s = vcl_session_get (wrk, sid);
2932 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002933 break;
Florin Corasb242d312020-10-26 15:35:40 -07002934 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002935 if (!(EPOLLOUT & session_events))
2936 break;
2937 add_event = 1;
2938 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002939 session_evt_data = s->vep.ev.data.u64;
2940 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2941 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002942 break;
Florin Coras86f04502018-09-12 16:08:01 -07002943 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002944 if (!e->postponed)
2945 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2946 else
2947 s = vcl_session_get (wrk, e->session_index);
2948 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002949 break;
Florin Corasb242d312020-10-26 15:35:40 -07002950 session_events = s->vep.ev.events;
2951 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002952 if (!(EPOLLIN & session_events))
2953 break;
Florin Coras86f04502018-09-12 16:08:01 -07002954 add_event = 1;
2955 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002956 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002957 break;
2958 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002959 if (!e->postponed)
2960 {
2961 connected_msg = (session_connected_msg_t *) e->data;
2962 sid = vcl_session_connected_handler (wrk, connected_msg);
2963 }
2964 else
2965 sid = e->session_index;
2966 s = vcl_session_get (wrk, sid);
2967 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002968 break;
Florin Corasb242d312020-10-26 15:35:40 -07002969 session_events = s->vep.ev.events;
2970 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002971 if (!(EPOLLOUT & session_events))
2972 break;
2973 add_event = 1;
2974 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002975 session_evt_data = s->vep.ev.data.u64;
2976 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002977 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002978 break;
2979 case SESSION_CTRL_EVT_DISCONNECTED:
2980 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002981 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2982 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002983 break;
Florin Corasb242d312020-10-26 15:35:40 -07002984 sid = s->session_index;
2985 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002986 add_event = 1;
2987 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002988 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002989 break;
2990 case SESSION_CTRL_EVT_RESET:
2991 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002992 s = vcl_session_get (wrk, sid);
2993 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002994 break;
Florin Corasb242d312020-10-26 15:35:40 -07002995 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002996 add_event = 1;
2997 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002998 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002999 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08003000 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
3001 vcl_session_unlisten_reply_handler (wrk, e->data);
3002 break;
Florin Coras68b7e582020-01-21 18:33:23 -08003003 case SESSION_CTRL_EVT_MIGRATED:
3004 vcl_session_migrated_handler (wrk, e->data);
3005 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003006 case SESSION_CTRL_EVT_CLEANUP:
3007 vcl_session_cleanup_handler (wrk, e->data);
3008 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003009 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3010 vcl_session_req_worker_update_handler (wrk, e->data);
3011 break;
3012 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3013 vcl_session_worker_update_reply_handler (wrk, e->data);
3014 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003015 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3016 vcl_session_app_add_segment_handler (wrk, e->data);
3017 break;
3018 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3019 vcl_session_app_del_segment_handler (wrk, e->data);
3020 break;
hanlina3a48962020-07-13 11:09:15 +08003021 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003022 vcl_worker_rpc_handler (wrk, e->data);
3023 break;
Florin Coras86f04502018-09-12 16:08:01 -07003024 default:
3025 VDBG (0, "unhandled: %u", e->event_type);
3026 break;
3027 }
3028
3029 if (add_event)
3030 {
3031 events[*num_ev].data.u64 = session_evt_data;
3032 if (EPOLLONESHOT & session_events)
3033 {
Florin Corasb242d312020-10-26 15:35:40 -07003034 s = vcl_session_get (wrk, sid);
3035 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003036 }
3037 *num_ev += 1;
3038 }
3039}
3040
3041static int
3042vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3043 struct epoll_event *events, u32 maxevents,
3044 double wait_for_time, u32 * num_ev)
3045{
Florin Coras99368312018-08-02 10:45:44 -07003046 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003047 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003048 int i;
3049
Florin Coras539663c2018-09-28 14:59:37 -07003050 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3051 goto handle_dequeued;
3052
Florin Coras54693d22018-07-17 10:46:29 -07003053 if (svm_msg_q_is_empty (mq))
3054 {
3055 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003056 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003057 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003058 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003059 else
3060 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003061 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003062 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003063 }
3064 }
Florin Corase003a1b2019-06-05 10:47:16 -07003065 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003066 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003067
Florin Coras539663c2018-09-28 14:59:37 -07003068handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003069 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003070 {
Florin Coras134a9962018-08-28 11:32:04 -07003071 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003072 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003073 if (*num_ev < maxevents)
3074 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3075 else
3076 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003077 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003078 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003079 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003080 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003081 return *num_ev;
3082}
3083
Florin Coras99368312018-08-02 10:45:44 -07003084static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003085vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3086 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003087{
Florin Corasccdb8b82021-04-28 13:01:06 -07003088 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003089
3090 if (!n_evts)
3091 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003092 if (timeout_ms > 0)
3093 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003094 }
3095
3096 do
3097 {
3098 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003099 timeout_ms, &n_evts);
3100 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003101 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003102 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003103 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003104
3105 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003106}
3107
3108static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003109vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3110 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003111{
Florin Coras99368312018-08-02 10:45:44 -07003112 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003113 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003114 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003115 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003116 u64 buf;
3117
Florin Coras134a9962018-08-28 11:32:04 -07003118 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003119 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003120 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003121 if (timeout_ms > 0)
3122 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003123 }
3124
Florin Corasb13ba132021-01-27 16:05:24 -08003125 do
3126 {
3127 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003128 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003129 if (n_mq_evts < 0)
3130 {
3131 VDBG (0, "epoll_wait error %u", errno);
3132 return n_evts;
3133 }
3134
3135 for (i = 0; i < n_mq_evts; i++)
3136 {
3137 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3138 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3139 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3140 &n_evts);
3141 }
3142
Florin Corasccdb8b82021-04-28 13:01:06 -07003143 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003144 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003145 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003146 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003147
3148 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003149}
3150
Dave Wallacef7f809c2017-10-03 01:48:42 -04003151int
Florin Coras134a9962018-08-28 11:32:04 -07003152vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003153 int maxevents, double wait_for_time)
3154{
Florin Coras134a9962018-08-28 11:32:04 -07003155 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003156 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003157 u32 n_evts = 0;
3158 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003159
3160 if (PREDICT_FALSE (maxevents <= 0))
3161 {
Florin Coras5e062572019-03-14 19:07:51 -07003162 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003163 return VPPCOM_EINVAL;
3164 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003165
Florin Coras134a9962018-08-28 11:32:04 -07003166 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003167 if (!vep_session)
3168 return VPPCOM_EBADFD;
3169
Florin Coras6c3b2182020-10-19 18:36:48 -07003170 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003171 {
Florin Coras5e062572019-03-14 19:07:51 -07003172 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003173 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003174 }
Florin Coras54693d22018-07-17 10:46:29 -07003175
3176 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003177
Florin Coras86f04502018-09-12 16:08:01 -07003178 if (vec_len (wrk->unhandled_evts_vector))
3179 {
3180 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3181 {
3182 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3183 events, &n_evts);
3184 if (n_evts == maxevents)
3185 {
Florin Corase003a1b2019-06-05 10:47:16 -07003186 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3187 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003188 }
3189 }
Florin Corase003a1b2019-06-05 10:47:16 -07003190 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003191 }
wanghanlin8919fec2021-03-18 20:00:41 +08003192 /* Request to only drain unhandled */
3193 if ((int) wait_for_time == -2)
3194 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003195
3196 if (vcm->cfg.use_mq_eventfd)
3197 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3198 wait_for_time);
3199
3200 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3201 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003202}
3203
Dave Wallace35830af2017-10-09 01:43:42 -04003204int
Florin Coras134a9962018-08-28 11:32:04 -07003205vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003206 void *buffer, uint32_t * buflen)
3207{
Florin Coras134a9962018-08-28 11:32:04 -07003208 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003209 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003210 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003211 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003212 vcl_session_t *session;
3213 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003214
Florin Coras134a9962018-08-28 11:32:04 -07003215 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003216 if (!session)
3217 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003218
Dave Wallace35830af2017-10-09 01:43:42 -04003219 switch (op)
3220 {
3221 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003222 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003223 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3224 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003225 break;
3226
Dave Wallace227867f2017-11-13 21:21:53 -05003227 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003228 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003229 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3230 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003231 break;
3232
3233 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003234 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003235 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003236 *flags =
3237 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003238 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003239 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003240 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003241 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3242 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003243 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003244 }
3245 else
3246 rv = VPPCOM_EINVAL;
3247 break;
3248
3249 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003250 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003251 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003252 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003253 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003254 else
Florin Corasac422d62020-10-19 20:51:36 -07003255 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003256
Florin Coras5e062572019-03-14 19:07:51 -07003257 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3258 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003259 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003260 }
3261 else
3262 rv = VPPCOM_EINVAL;
3263 break;
3264
3265 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003266 if (PREDICT_TRUE (buffer && buflen &&
3267 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003268 {
Florin Coras7e12d942018-06-27 14:32:43 -07003269 ep->is_ip4 = session->transport.is_ip4;
3270 ep->port = session->transport.rmt_port;
3271 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003272 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3273 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003274 else
Dave Barach178cf492018-11-13 16:34:13 -05003275 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3276 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003277 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003278 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3279 "addr = %U, port %u", session_handle, ep->is_ip4,
3280 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003281 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3282 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003283 }
3284 else
3285 rv = VPPCOM_EINVAL;
3286 break;
3287
3288 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003289 if (PREDICT_TRUE (buffer && buflen &&
3290 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003291 {
Florin Coras7e12d942018-06-27 14:32:43 -07003292 ep->is_ip4 = session->transport.is_ip4;
3293 ep->port = session->transport.lcl_port;
3294 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003295 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3296 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003297 else
Dave Barach178cf492018-11-13 16:34:13 -05003298 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3299 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003300 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003301 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3302 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003303 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003304 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3305 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003306 }
3307 else
3308 rv = VPPCOM_EINVAL;
3309 break;
Stevenb5a11602017-10-11 09:59:30 -07003310
Florin Corasef7cbf62019-10-17 09:56:27 -07003311 case VPPCOM_ATTR_SET_LCL_ADDR:
3312 if (PREDICT_TRUE (buffer && buflen &&
3313 (*buflen >= sizeof (*ep)) && ep->ip))
3314 {
3315 session->transport.is_ip4 = ep->is_ip4;
3316 session->transport.lcl_port = ep->port;
3317 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3318 *buflen = sizeof (*ep);
3319 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3320 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3321 &session->transport.lcl_ip,
3322 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3323 clib_net_to_host_u16 (ep->port));
3324 }
3325 else
3326 rv = VPPCOM_EINVAL;
3327 break;
3328
Dave Wallace048b1d62018-01-03 22:24:41 -05003329 case VPPCOM_ATTR_GET_LIBC_EPFD:
3330 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003331 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003332 break;
3333
3334 case VPPCOM_ATTR_SET_LIBC_EPFD:
3335 if (PREDICT_TRUE (buffer && buflen &&
3336 (*buflen == sizeof (session->libc_epfd))))
3337 {
3338 session->libc_epfd = *(int *) buffer;
3339 *buflen = sizeof (session->libc_epfd);
3340
Florin Coras5e062572019-03-14 19:07:51 -07003341 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3342 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003343 }
3344 else
3345 rv = VPPCOM_EINVAL;
3346 break;
3347
3348 case VPPCOM_ATTR_GET_PROTOCOL:
3349 if (buffer && buflen && (*buflen >= sizeof (int)))
3350 {
Florin Coras7e12d942018-06-27 14:32:43 -07003351 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003352 *buflen = sizeof (int);
3353
Florin Coras5e062572019-03-14 19:07:51 -07003354 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3355 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003356 }
3357 else
3358 rv = VPPCOM_EINVAL;
3359 break;
3360
3361 case VPPCOM_ATTR_GET_LISTEN:
3362 if (buffer && buflen && (*buflen >= sizeof (int)))
3363 {
Florin Corasac422d62020-10-19 20:51:36 -07003364 *(int *) buffer = vcl_session_has_attr (session,
3365 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003366 *buflen = sizeof (int);
3367
Florin Coras5e062572019-03-14 19:07:51 -07003368 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3369 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003370 }
3371 else
3372 rv = VPPCOM_EINVAL;
3373 break;
3374
3375 case VPPCOM_ATTR_GET_ERROR:
3376 if (buffer && buflen && (*buflen >= sizeof (int)))
3377 {
3378 *(int *) buffer = 0;
3379 *buflen = sizeof (int);
3380
Florin Coras5e062572019-03-14 19:07:51 -07003381 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3382 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003383 }
3384 else
3385 rv = VPPCOM_EINVAL;
3386 break;
3387
3388 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3389 if (buffer && buflen && (*buflen >= sizeof (u32)))
3390 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003391
3392 /* VPP-TBD */
3393 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003394 session->tx_fifo ?
3395 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003396 vcm->cfg.tx_fifo_size);
3397 *buflen = sizeof (u32);
3398
Florin Coras5e062572019-03-14 19:07:51 -07003399 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3400 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3401 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003402 }
3403 else
3404 rv = VPPCOM_EINVAL;
3405 break;
3406
3407 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3408 if (buffer && buflen && (*buflen == sizeof (u32)))
3409 {
3410 /* VPP-TBD */
3411 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003412 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3413 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3414 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003415 }
3416 else
3417 rv = VPPCOM_EINVAL;
3418 break;
3419
3420 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3421 if (buffer && buflen && (*buflen >= sizeof (u32)))
3422 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003423
3424 /* VPP-TBD */
3425 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003426 session->rx_fifo ?
3427 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003428 vcm->cfg.rx_fifo_size);
3429 *buflen = sizeof (u32);
3430
Florin Coras5e062572019-03-14 19:07:51 -07003431 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3432 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003433 }
3434 else
3435 rv = VPPCOM_EINVAL;
3436 break;
3437
3438 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3439 if (buffer && buflen && (*buflen == sizeof (u32)))
3440 {
3441 /* VPP-TBD */
3442 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003443 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3444 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3445 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003446 }
3447 else
3448 rv = VPPCOM_EINVAL;
3449 break;
3450
3451 case VPPCOM_ATTR_GET_REUSEADDR:
3452 if (buffer && buflen && (*buflen >= sizeof (int)))
3453 {
3454 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003455 *(int *) buffer = vcl_session_has_attr (session,
3456 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003457 *buflen = sizeof (int);
3458
Florin Coras5e062572019-03-14 19:07:51 -07003459 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3460 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003461 }
3462 else
3463 rv = VPPCOM_EINVAL;
3464 break;
3465
Stevenb5a11602017-10-11 09:59:30 -07003466 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003467 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003468 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003469 {
3470 /* VPP-TBD */
3471 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003472 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003473 else
Florin Corasac422d62020-10-19 20:51:36 -07003474 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475
Florin Coras5e062572019-03-14 19:07:51 -07003476 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003477 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003478 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003479 }
3480 else
3481 rv = VPPCOM_EINVAL;
3482 break;
3483
3484 case VPPCOM_ATTR_GET_REUSEPORT:
3485 if (buffer && buflen && (*buflen >= sizeof (int)))
3486 {
3487 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003488 *(int *) buffer = vcl_session_has_attr (session,
3489 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 *buflen = sizeof (int);
3491
Florin Coras5e062572019-03-14 19:07:51 -07003492 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3493 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003494 }
3495 else
3496 rv = VPPCOM_EINVAL;
3497 break;
3498
3499 case VPPCOM_ATTR_SET_REUSEPORT:
3500 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003501 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003502 {
3503 /* VPP-TBD */
3504 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003505 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003506 else
Florin Corasac422d62020-10-19 20:51:36 -07003507 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003508
Florin Coras5e062572019-03-14 19:07:51 -07003509 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003510 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003511 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003512 }
3513 else
3514 rv = VPPCOM_EINVAL;
3515 break;
3516
3517 case VPPCOM_ATTR_GET_BROADCAST:
3518 if (buffer && buflen && (*buflen >= sizeof (int)))
3519 {
3520 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003521 *(int *) buffer = vcl_session_has_attr (session,
3522 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003523 *buflen = sizeof (int);
3524
Florin Coras5e062572019-03-14 19:07:51 -07003525 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3526 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003527 }
3528 else
3529 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003530 break;
3531
3532 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003533 if (buffer && buflen && (*buflen == sizeof (int)))
3534 {
3535 /* VPP-TBD */
3536 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003537 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003538 else
Florin Corasac422d62020-10-19 20:51:36 -07003539 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003540
Florin Coras5e062572019-03-14 19:07:51 -07003541 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003542 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003543 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003544 }
3545 else
3546 rv = VPPCOM_EINVAL;
3547 break;
3548
3549 case VPPCOM_ATTR_GET_V6ONLY:
3550 if (buffer && buflen && (*buflen >= sizeof (int)))
3551 {
3552 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003553 *(int *) buffer = vcl_session_has_attr (session,
3554 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003555 *buflen = sizeof (int);
3556
Florin Coras5e062572019-03-14 19:07:51 -07003557 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3558 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003559 }
3560 else
3561 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003562 break;
3563
3564 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003565 if (buffer && buflen && (*buflen == sizeof (int)))
3566 {
3567 /* VPP-TBD */
3568 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003569 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003570 else
Florin Corasac422d62020-10-19 20:51:36 -07003571 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003572
Florin Coras5e062572019-03-14 19:07:51 -07003573 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003574 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003575 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003576 }
3577 else
3578 rv = VPPCOM_EINVAL;
3579 break;
3580
3581 case VPPCOM_ATTR_GET_KEEPALIVE:
3582 if (buffer && buflen && (*buflen >= sizeof (int)))
3583 {
3584 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003585 *(int *) buffer = vcl_session_has_attr (session,
3586 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003587 *buflen = sizeof (int);
3588
Florin Coras5e062572019-03-14 19:07:51 -07003589 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3590 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003591 }
3592 else
3593 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003594 break;
Stevenbccd3392017-10-12 20:42:21 -07003595
3596 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003597 if (buffer && buflen && (*buflen == sizeof (int)))
3598 {
3599 /* VPP-TBD */
3600 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003601 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003602 else
Florin Corasac422d62020-10-19 20:51:36 -07003603 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003604
Florin Coras5e062572019-03-14 19:07:51 -07003605 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003606 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003607 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003608 }
3609 else
3610 rv = VPPCOM_EINVAL;
3611 break;
3612
3613 case VPPCOM_ATTR_GET_TCP_NODELAY:
3614 if (buffer && buflen && (*buflen >= sizeof (int)))
3615 {
3616 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003617 *(int *) buffer = vcl_session_has_attr (session,
3618 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003619 *buflen = sizeof (int);
3620
Florin Coras5e062572019-03-14 19:07:51 -07003621 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3622 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003623 }
3624 else
3625 rv = VPPCOM_EINVAL;
3626 break;
3627
3628 case VPPCOM_ATTR_SET_TCP_NODELAY:
3629 if (buffer && buflen && (*buflen == sizeof (int)))
3630 {
3631 /* VPP-TBD */
3632 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003633 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003634 else
Florin Corasac422d62020-10-19 20:51:36 -07003635 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003636
Florin Coras5e062572019-03-14 19:07:51 -07003637 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003638 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003639 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003640 }
3641 else
3642 rv = VPPCOM_EINVAL;
3643 break;
3644
3645 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3646 if (buffer && buflen && (*buflen >= sizeof (int)))
3647 {
3648 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003649 *(int *) buffer = vcl_session_has_attr (session,
3650 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003651 *buflen = sizeof (int);
3652
Florin Coras5e062572019-03-14 19:07:51 -07003653 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3654 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003655 }
3656 else
3657 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003658 break;
3659
3660 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003661 if (buffer && buflen && (*buflen == sizeof (int)))
3662 {
3663 /* VPP-TBD */
3664 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003665 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003666 else
Florin Corasac422d62020-10-19 20:51:36 -07003667 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003668
Florin Coras5e062572019-03-14 19:07:51 -07003669 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003670 vcl_session_has_attr (session,
3671 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003672 }
3673 else
3674 rv = VPPCOM_EINVAL;
3675 break;
3676
3677 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3678 if (buffer && buflen && (*buflen >= sizeof (int)))
3679 {
3680 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003681 *(int *) buffer = vcl_session_has_attr (session,
3682 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003683 *buflen = sizeof (int);
3684
Florin Coras5e062572019-03-14 19:07:51 -07003685 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3686 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003687 }
3688 else
3689 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003690 break;
3691
3692 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003693 if (buffer && buflen && (*buflen == sizeof (int)))
3694 {
3695 /* VPP-TBD */
3696 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003697 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003698 else
Florin Corasac422d62020-10-19 20:51:36 -07003699 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003700
Florin Coras5e062572019-03-14 19:07:51 -07003701 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003702 vcl_session_has_attr (session,
3703 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003704 }
3705 else
3706 rv = VPPCOM_EINVAL;
3707 break;
3708
3709 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003710 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003711 {
Florin Coras04ae8272021-04-12 19:55:37 -07003712 rv = VPPCOM_EINVAL;
3713 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003714 }
Florin Coras04ae8272021-04-12 19:55:37 -07003715
3716 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3717 tea.mss = *(u32 *) buffer;
3718 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3719 rv = VPPCOM_ENOPROTOOPT;
3720
3721 if (!rv)
3722 {
3723 *(u32 *) buffer = tea.mss;
3724 *buflen = sizeof (int);
3725 }
3726
3727 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3728 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003729 break;
3730
3731 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003732 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003733 {
Florin Coras04ae8272021-04-12 19:55:37 -07003734 rv = VPPCOM_EINVAL;
3735 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003736 }
Florin Coras04ae8272021-04-12 19:55:37 -07003737
3738 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3739 tea.mss = *(u32 *) buffer;
3740 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3741 rv = VPPCOM_ENOPROTOOPT;
3742
3743 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3744 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003745 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003746
Florin Coras7baeb712019-01-04 17:05:43 -08003747 case VPPCOM_ATTR_SET_SHUT:
3748 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003749 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003750 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003751 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003752 break;
3753
3754 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003755 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003756 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003757 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003758 tmp_flags |= 2;
3759 if (tmp_flags == 1)
3760 *(int *) buffer = SHUT_RD;
3761 else if (tmp_flags == 2)
3762 *(int *) buffer = SHUT_WR;
3763 else if (tmp_flags == 3)
3764 *(int *) buffer = SHUT_RDWR;
3765 *buflen = sizeof (int);
3766 break;
Florin Coras1e966172020-05-16 18:18:14 +00003767
3768 case VPPCOM_ATTR_SET_CONNECTED:
3769 session->flags |= VCL_SESSION_F_CONNECTED;
3770 break;
3771
Florin Corasa5a9efd2021-01-05 17:03:29 -08003772 case VPPCOM_ATTR_SET_CKPAIR:
3773 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3774 !vcl_session_has_crypto (session))
3775 {
3776 rv = VPPCOM_EINVAL;
3777 break;
3778 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003779 if (!session->ext_config)
3780 {
Florin Coras87f63892021-05-01 16:01:40 -07003781 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3782 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003783 }
3784 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3785 {
3786 rv = VPPCOM_EINVAL;
3787 break;
3788 }
3789
3790 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003791 break;
3792
Florin Coras6a6555a2021-01-28 11:39:27 -08003793 case VPPCOM_ATTR_SET_VRF:
3794 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3795 {
3796 rv = VPPCOM_EINVAL;
3797 break;
3798 }
3799 session->vrf = *(u32 *) buffer;
3800 break;
3801
3802 case VPPCOM_ATTR_GET_VRF:
3803 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3804 {
3805 rv = VPPCOM_EINVAL;
3806 break;
3807 }
3808 *(u32 *) buffer = session->vrf;
3809 *buflen = sizeof (u32);
3810 break;
3811
wanghanlin0674f852021-02-22 10:38:36 +08003812 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003813 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003814 {
Florin Corasd77325c2021-02-23 12:03:03 -08003815 rv = VPPCOM_EINVAL;
3816 break;
wanghanlin0674f852021-02-22 10:38:36 +08003817 }
Florin Corasd77325c2021-02-23 12:03:03 -08003818
3819 if (session->transport.is_ip4)
3820 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003821 else
Florin Corasd77325c2021-02-23 12:03:03 -08003822 *(int *) buffer = AF_INET6;
3823 *buflen = sizeof (int);
3824
wanghanlin0674f852021-02-22 10:38:36 +08003825 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3826 *buflen);
3827 break;
3828
Florin Coras87f63892021-05-01 16:01:40 -07003829 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3830 if (!(buffer && buflen && (*buflen > 0)))
3831 {
3832 rv = VPPCOM_EINVAL;
3833 break;
3834 }
3835 if (session->ext_config)
3836 {
3837 rv = VPPCOM_EINVAL;
3838 break;
3839 }
3840 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3841 *buflen + sizeof (u32));
3842 clib_memcpy (session->ext_config->data, buffer, *buflen);
3843 session->ext_config->len = *buflen;
3844 break;
3845
Dave Wallacee22aa742017-10-20 12:30:38 -04003846 default:
3847 rv = VPPCOM_EINVAL;
3848 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003849 }
3850
Dave Wallace35830af2017-10-09 01:43:42 -04003851 return rv;
3852}
3853
Stevenac1f96d2017-10-24 16:03:58 -07003854int
Florin Coras134a9962018-08-28 11:32:04 -07003855vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003856 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3857{
Florin Coras134a9962018-08-28 11:32:04 -07003858 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003859 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003860 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003861
3862 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003863 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003864 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003865 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003866 else
3867 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003868 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003869 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003870 }
3871
Florin Coras0a1e1832020-03-29 18:54:04 +00003872 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003873 {
Florin Coras5da10c42020-04-01 04:31:21 +00003874 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003875 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003876 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3877 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003878 else
Dave Barach178cf492018-11-13 16:34:13 -05003879 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3880 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003881 ep->is_ip4 = session->transport.is_ip4;
3882 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003883 }
Florin Coras460dce62018-07-27 05:45:06 -07003884
Stevenac1f96d2017-10-24 16:03:58 -07003885 return rv;
3886}
3887
3888int
Florin Coras134a9962018-08-28 11:32:04 -07003889vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003890 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3891{
Florin Coras7a2abce2020-04-05 19:25:44 +00003892 vcl_worker_t *wrk = vcl_worker_get_current ();
3893 vcl_session_t *s;
3894
3895 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras0b0d28e2021-06-04 17:31:53 -07003896 if (PREDICT_FALSE (!s))
Florin Coras7a2abce2020-04-05 19:25:44 +00003897 return VPPCOM_EBADFD;
3898
Dave Wallace617dffa2017-10-26 14:47:06 -04003899 if (ep)
3900 {
Florin Coras5824cc52020-10-20 18:44:41 -07003901 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003902 return VPPCOM_EINVAL;
3903
3904 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003905 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003906 {
Florin Coras5824cc52020-10-20 18:44:41 -07003907 u32 session_index = s->session_index;
3908 f64 timeout = vcm->cfg.session_timeout;
3909 int rv;
3910
Florin Coras5da10c42020-04-01 04:31:21 +00003911 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003912 rv = vppcom_wait_for_session_state_change (session_index,
3913 VCL_STATE_READY,
3914 timeout);
3915 if (rv < 0)
3916 return rv;
3917 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003918 }
Florin Coras5824cc52020-10-20 18:44:41 -07003919
3920 s->transport.is_ip4 = ep->is_ip4;
3921 s->transport.rmt_port = ep->port;
3922 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003923 }
3924
3925 if (flags)
3926 {
3927 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003928 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003929 }
3930
Florin Coras7a2abce2020-04-05 19:25:44 +00003931 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3932 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003933}
3934
Dave Wallace048b1d62018-01-03 22:24:41 -05003935int
3936vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3937{
Florin Coras134a9962018-08-28 11:32:04 -07003938 vcl_worker_t *wrk = vcl_worker_get_current ();
3939 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003940 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003941 svm_msg_q_msg_t msg;
3942 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003943 int rv, num_ev = 0;
3944
Florin Coras5e062572019-03-14 19:07:51 -07003945 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003946
3947 if (!vp)
3948 return VPPCOM_EFAULT;
3949
3950 do
3951 {
Florin Coras7e12d942018-06-27 14:32:43 -07003952 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003953
Florin Coras6917b942018-11-13 22:44:54 -08003954 /* Dequeue all events and drop all unhandled io events */
3955 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3956 {
3957 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3958 vcl_handle_mq_event (wrk, e);
3959 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3960 }
3961 vec_reset_length (wrk->unhandled_evts_vector);
3962
Dave Wallace048b1d62018-01-03 22:24:41 -05003963 for (i = 0; i < n_sids; i++)
3964 {
Florin Coras7baeb712019-01-04 17:05:43 -08003965 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003966 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003967 {
3968 vp[i].revents = POLLHUP;
3969 num_ev++;
3970 continue;
3971 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003972
Florin Coras6917b942018-11-13 22:44:54 -08003973 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003974
3975 if (POLLIN & vp[i].events)
3976 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003977 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003978 if (rv > 0)
3979 {
Florin Coras6917b942018-11-13 22:44:54 -08003980 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003981 num_ev++;
3982 }
3983 else if (rv < 0)
3984 {
3985 switch (rv)
3986 {
3987 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003988 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003989 break;
3990
3991 default:
Florin Coras6917b942018-11-13 22:44:54 -08003992 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003993 break;
3994 }
3995 num_ev++;
3996 }
3997 }
3998
3999 if (POLLOUT & vp[i].events)
4000 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004001 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004002 if (rv > 0)
4003 {
Florin Coras6917b942018-11-13 22:44:54 -08004004 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004005 num_ev++;
4006 }
4007 else if (rv < 0)
4008 {
4009 switch (rv)
4010 {
4011 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004012 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004013 break;
4014
4015 default:
Florin Coras6917b942018-11-13 22:44:54 -08004016 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004017 break;
4018 }
4019 num_ev++;
4020 }
4021 }
4022
Dave Wallace7e607a72018-06-18 18:41:32 -04004023 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004024 {
Florin Coras6917b942018-11-13 22:44:54 -08004025 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004026 num_ev++;
4027 }
4028 }
4029 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004030 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004031 }
4032 while ((num_ev == 0) && keep_trying);
4033
Dave Wallace048b1d62018-01-03 22:24:41 -05004034 return num_ev;
4035}
4036
Florin Coras99368312018-08-02 10:45:44 -07004037int
4038vppcom_mq_epoll_fd (void)
4039{
Florin Coras134a9962018-08-28 11:32:04 -07004040 vcl_worker_t *wrk = vcl_worker_get_current ();
4041 return wrk->mqs_epfd;
4042}
4043
4044int
Florin Coras30e79c22019-01-02 19:31:22 -08004045vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004046{
4047 return session_handle & 0xFFFFFF;
4048}
4049
4050int
Florin Coras30e79c22019-01-02 19:31:22 -08004051vppcom_session_worker (vcl_session_handle_t session_handle)
4052{
4053 return session_handle >> 24;
4054}
4055
4056int
Florin Coras134a9962018-08-28 11:32:04 -07004057vppcom_worker_register (void)
4058{
Florin Coras47c40e22018-11-26 17:01:36 -08004059 if (!vcl_worker_alloc_and_init ())
4060 return VPPCOM_EEXIST;
4061
Florin Coras47c40e22018-11-26 17:01:36 -08004062 if (vcl_worker_register_with_vpp ())
4063 return VPPCOM_EEXIST;
4064
4065 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004066}
4067
Florin Coras369db832019-07-08 12:34:45 -07004068void
4069vppcom_worker_unregister (void)
4070{
4071 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4072 vcl_set_worker_index (~0);
4073}
4074
Pivo6017ff02020-05-04 17:57:33 +02004075void
4076vppcom_worker_index_set (int index)
4077{
4078 vcl_set_worker_index (index);
4079}
4080
Florin Corasdfe4cf42018-11-28 22:13:45 -08004081int
4082vppcom_worker_index (void)
4083{
4084 return vcl_get_worker_index ();
4085}
4086
Florin Corase0982e52019-01-25 13:19:56 -08004087int
4088vppcom_worker_mqs_epfd (void)
4089{
4090 vcl_worker_t *wrk = vcl_worker_get_current ();
4091 if (!vcm->cfg.use_mq_eventfd)
4092 return -1;
4093 return wrk->mqs_epfd;
4094}
4095
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004096int
4097vppcom_session_is_connectable_listener (uint32_t session_handle)
4098{
4099 vcl_session_t *session;
4100 vcl_worker_t *wrk = vcl_worker_get_current ();
4101 session = vcl_session_get_w_handle (wrk, session_handle);
4102 if (!session)
4103 return VPPCOM_EBADFD;
4104 return vcl_session_is_connectable_listener (wrk, session);
4105}
4106
4107int
4108vppcom_session_listener (uint32_t session_handle)
4109{
4110 vcl_worker_t *wrk = vcl_worker_get_current ();
4111 vcl_session_t *listen_session, *session;
4112 session = vcl_session_get_w_handle (wrk, session_handle);
4113 if (!session)
4114 return VPPCOM_EBADFD;
4115 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4116 return VPPCOM_EBADFD;
4117 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4118 if (!listen_session)
4119 return VPPCOM_EBADFD;
4120 return vcl_session_handle (listen_session);
4121}
4122
4123int
4124vppcom_session_n_accepted (uint32_t session_handle)
4125{
4126 vcl_worker_t *wrk = vcl_worker_get_current ();
4127 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4128 if (!session)
4129 return VPPCOM_EBADFD;
4130 return session->n_accepted_sessions;
4131}
4132
Florin Coras66ec4672020-06-15 07:59:40 -07004133const char *
4134vppcom_proto_str (vppcom_proto_t proto)
4135{
4136 char const *proto_str;
4137
4138 switch (proto)
4139 {
4140 case VPPCOM_PROTO_TCP:
4141 proto_str = "TCP";
4142 break;
4143 case VPPCOM_PROTO_UDP:
4144 proto_str = "UDP";
4145 break;
4146 case VPPCOM_PROTO_TLS:
4147 proto_str = "TLS";
4148 break;
4149 case VPPCOM_PROTO_QUIC:
4150 proto_str = "QUIC";
4151 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004152 case VPPCOM_PROTO_DTLS:
4153 proto_str = "DTLS";
4154 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004155 case VPPCOM_PROTO_SRTP:
4156 proto_str = "SRTP";
4157 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004158 default:
4159 proto_str = "UNKNOWN";
4160 break;
4161 }
4162 return proto_str;
4163}
4164
4165const char *
4166vppcom_retval_str (int retval)
4167{
4168 char const *st;
4169
4170 switch (retval)
4171 {
4172 case VPPCOM_OK:
4173 st = "VPPCOM_OK";
4174 break;
4175
4176 case VPPCOM_EAGAIN:
4177 st = "VPPCOM_EAGAIN";
4178 break;
4179
4180 case VPPCOM_EFAULT:
4181 st = "VPPCOM_EFAULT";
4182 break;
4183
4184 case VPPCOM_ENOMEM:
4185 st = "VPPCOM_ENOMEM";
4186 break;
4187
4188 case VPPCOM_EINVAL:
4189 st = "VPPCOM_EINVAL";
4190 break;
4191
4192 case VPPCOM_EBADFD:
4193 st = "VPPCOM_EBADFD";
4194 break;
4195
4196 case VPPCOM_EAFNOSUPPORT:
4197 st = "VPPCOM_EAFNOSUPPORT";
4198 break;
4199
4200 case VPPCOM_ECONNABORTED:
4201 st = "VPPCOM_ECONNABORTED";
4202 break;
4203
4204 case VPPCOM_ECONNRESET:
4205 st = "VPPCOM_ECONNRESET";
4206 break;
4207
4208 case VPPCOM_ENOTCONN:
4209 st = "VPPCOM_ENOTCONN";
4210 break;
4211
4212 case VPPCOM_ECONNREFUSED:
4213 st = "VPPCOM_ECONNREFUSED";
4214 break;
4215
4216 case VPPCOM_ETIMEDOUT:
4217 st = "VPPCOM_ETIMEDOUT";
4218 break;
4219
4220 default:
4221 st = "UNKNOWN_STATE";
4222 break;
4223 }
4224
4225 return st;
4226}
4227
Florin Corasa5a9efd2021-01-05 17:03:29 -08004228int
4229vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4230{
4231 if (vcm->cfg.vpp_app_socket_api)
4232 {
4233 clib_warning ("not supported");
4234 return VPPCOM_EINVAL;
4235 }
4236 return vcl_bapi_add_cert_key_pair (ckpair);
4237}
4238
4239int
4240vppcom_del_cert_key_pair (uint32_t ckpair_index)
4241{
4242 if (vcm->cfg.vpp_app_socket_api)
4243 {
4244 clib_warning ("not supported");
4245 return VPPCOM_EINVAL;
4246 }
4247 return vcl_bapi_del_cert_key_pair (ckpair_index);
4248}
4249
Dave Wallacee22aa742017-10-20 12:30:38 -04004250/*
4251 * fd.io coding-style-patch-verification: ON
4252 *
4253 * Local Variables:
4254 * eval: (c-set-style "gnu")
4255 * End:
4256 */