blob: 4c77b0bc43b017d5d1631533eafdf55612d2c0e3 [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
jiangxiaomingff31ac62019-11-21 23:34:56 +08002155 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002156 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002157
Florin Coras6c3b2182020-10-19 18:36:48 -07002158 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002159 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002160 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2161 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002162 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002163 }
2164
liuyacan534468e2021-05-09 03:50:40 +00002165 if (PREDICT_FALSE (!vcl_session_is_open (s) ||
2166 s->flags & VCL_SESSION_F_SHUTDOWN))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002167 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002168 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2169 s->session_index, s->vpp_handle, s->session_state,
2170 vppcom_session_state_str (s->session_state));
2171 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002172 }
2173
Florin Coras0e88e852018-09-17 22:09:02 -07002174 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002175 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002176 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002177
Florin Coras653e43f2019-03-04 10:56:23 -08002178 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002179 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002180 {
Florin Coras54693d22018-07-17 10:46:29 -07002181 if (is_nonblocking)
2182 {
Florin Coras070453d2018-08-24 17:04:27 -07002183 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002184 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002185 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002186 {
Florin Coras2d379d82019-06-28 12:45:12 -07002187 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002188 if (vcl_session_is_closing (s))
2189 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002190
Florin Coras5398dfb2021-01-25 20:31:27 -08002191 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2192 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002193 }
Dave Wallace543852a2017-08-03 02:11:34 -04002194 }
Dave Wallace543852a2017-08-03 02:11:34 -04002195
Florin Coras653e43f2019-03-04 10:56:23 -08002196 et = SESSION_IO_EVT_TX;
2197 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002198 et = SESSION_IO_EVT_TX_FLUSH;
2199
Florin Coras7a2abce2020-04-05 19:25:44 +00002200 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002201 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002202 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002203 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002204 else
2205 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002206 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002207
Florin Coras14ed6df2019-03-06 21:13:42 -08002208 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002209 app_send_io_evt_to_vpp (
2210 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002211
Florin Coras56230092020-09-02 20:52:58 -07002212 /* The underlying fifo segment can run out of memory */
2213 if (PREDICT_FALSE (n_write < 0))
2214 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002215
Florin Coras6d0106e2019-01-29 20:11:58 -08002216 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2217 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002218
Florin Coras54693d22018-07-17 10:46:29 -07002219 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002220}
2221
Florin Coras42ceddb2018-12-12 10:56:01 -08002222int
2223vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2224{
Florin Coras7a2abce2020-04-05 19:25:44 +00002225 vcl_worker_t *wrk = vcl_worker_get_current ();
2226 vcl_session_t *s;
2227
2228 s = vcl_session_get_w_handle (wrk, session_handle);
2229 if (PREDICT_FALSE (!s))
2230 return VPPCOM_EBADFD;
2231
2232 return vppcom_session_write_inline (wrk, s, buf, n,
2233 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002234}
2235
Florin Corasb0f662f2018-12-27 14:51:46 -08002236int
2237vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2238{
Florin Coras7a2abce2020-04-05 19:25:44 +00002239 vcl_worker_t *wrk = vcl_worker_get_current ();
2240 vcl_session_t *s;
2241
2242 s = vcl_session_get_w_handle (wrk, session_handle);
2243 if (PREDICT_FALSE (!s))
2244 return VPPCOM_EBADFD;
2245
2246 return vppcom_session_write_inline (wrk, s, buf, n,
2247 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002248}
2249
Florin Corasc0737e92019-03-04 14:19:39 -08002250#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002251if (PREDICT_FALSE (!_s->rx_fifo)) \
2252 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002253if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2254 { \
2255 if (!vcl_session_is_ct (_s)) \
2256 { \
2257 svm_fifo_unset_event (_s->rx_fifo); \
2258 if (svm_fifo_is_empty (_s->rx_fifo)) \
2259 break; \
2260 } \
2261 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2262 { \
Florin Coras2f630182020-08-13 00:17:51 -07002263 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002264 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2265 break; \
2266 } \
2267 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002268
Florin Coras86f04502018-09-12 16:08:01 -07002269static void
2270vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2271 unsigned long n_bits, unsigned long *read_map,
2272 unsigned long *write_map,
2273 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002274{
2275 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002276 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002277 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002278 u32 sid;
2279
2280 switch (e->event_type)
2281 {
Florin Corasc0737e92019-03-04 14:19:39 -08002282 case SESSION_IO_EVT_RX:
2283 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002284 s = vcl_session_get (wrk, sid);
2285 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002286 break;
Florin Corasb242d312020-10-26 15:35:40 -07002287 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002288 if (sid < n_bits && read_map)
2289 {
David Johnsond9818dd2018-12-14 14:53:41 -05002290 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002291 *bits_set += 1;
2292 }
2293 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002294 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002295 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002296 s = vcl_session_get (wrk, sid);
2297 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002298 break;
2299 if (sid < n_bits && write_map)
2300 {
David Johnsond9818dd2018-12-14 14:53:41 -05002301 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002302 *bits_set += 1;
2303 }
2304 break;
Florin Coras86f04502018-09-12 16:08:01 -07002305 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002306 if (!e->postponed)
2307 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2308 else
2309 s = vcl_session_get (wrk, e->session_index);
2310 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002311 break;
Florin Corasb242d312020-10-26 15:35:40 -07002312 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002313 if (sid < n_bits && read_map)
2314 {
David Johnsond9818dd2018-12-14 14:53:41 -05002315 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002316 *bits_set += 1;
2317 }
2318 break;
2319 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002320 if (!e->postponed)
2321 {
2322 connected_msg = (session_connected_msg_t *) e->data;
2323 sid = vcl_session_connected_handler (wrk, connected_msg);
2324 }
2325 else
2326 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002327 if (sid == VCL_INVALID_SESSION_INDEX)
2328 break;
2329 if (sid < n_bits && write_map)
2330 {
2331 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2332 *bits_set += 1;
2333 }
Florin Coras86f04502018-09-12 16:08:01 -07002334 break;
2335 case SESSION_CTRL_EVT_DISCONNECTED:
2336 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002337 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2338 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002339 break;
Florin Corasb242d312020-10-26 15:35:40 -07002340 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002341 if (sid < n_bits && except_map)
2342 {
David Johnsond9818dd2018-12-14 14:53:41 -05002343 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002344 *bits_set += 1;
2345 }
2346 break;
2347 case SESSION_CTRL_EVT_RESET:
2348 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2349 if (sid < n_bits && except_map)
2350 {
David Johnsond9818dd2018-12-14 14:53:41 -05002351 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002352 *bits_set += 1;
2353 }
2354 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002355 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2356 vcl_session_unlisten_reply_handler (wrk, e->data);
2357 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002358 case SESSION_CTRL_EVT_MIGRATED:
2359 vcl_session_migrated_handler (wrk, e->data);
2360 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002361 case SESSION_CTRL_EVT_CLEANUP:
2362 vcl_session_cleanup_handler (wrk, e->data);
2363 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002364 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2365 vcl_session_worker_update_reply_handler (wrk, e->data);
2366 break;
2367 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2368 vcl_session_req_worker_update_handler (wrk, e->data);
2369 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002370 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2371 vcl_session_app_add_segment_handler (wrk, e->data);
2372 break;
2373 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2374 vcl_session_app_del_segment_handler (wrk, e->data);
2375 break;
hanlina3a48962020-07-13 11:09:15 +08002376 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002377 vcl_worker_rpc_handler (wrk, e->data);
2378 break;
Florin Coras86f04502018-09-12 16:08:01 -07002379 default:
2380 clib_warning ("unhandled: %u", e->event_type);
2381 break;
2382 }
2383}
2384
2385static int
2386vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2387 unsigned long n_bits, unsigned long *read_map,
2388 unsigned long *write_map, unsigned long *except_map,
2389 double time_to_wait, u32 * bits_set)
2390{
Florin Coras99368312018-08-02 10:45:44 -07002391 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002392 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002393 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002394
Florin Coras54693d22018-07-17 10:46:29 -07002395 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002396 {
Florin Coras54693d22018-07-17 10:46:29 -07002397 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002398 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002399
Florin Coras54693d22018-07-17 10:46:29 -07002400 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002401 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002402 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002403 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002404 else
2405 {
2406 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002407 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002408 }
2409 }
Florin Corase003a1b2019-06-05 10:47:16 -07002410 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002411
Florin Coras134a9962018-08-28 11:32:04 -07002412 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002413 {
Florin Coras134a9962018-08-28 11:32:04 -07002414 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002415 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002416 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2417 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002418 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002419 }
Florin Coras134a9962018-08-28 11:32:04 -07002420 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002421 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002422 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002423}
2424
Florin Coras99368312018-08-02 10:45:44 -07002425static int
Florin Coras294afe22019-01-07 17:49:17 -08002426vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2427 vcl_si_set * read_map, vcl_si_set * write_map,
2428 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002429 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002430{
Florin Coras14ed6df2019-03-06 21:13:42 -08002431 double wait = 0, start = 0;
2432
2433 if (!*bits_set)
2434 {
2435 wait = time_to_wait;
2436 start = clib_time_now (&wrk->clib_time);
2437 }
2438
2439 do
2440 {
2441 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2442 write_map, except_map, wait, bits_set);
2443 if (*bits_set)
2444 return *bits_set;
2445 if (wait == -1)
2446 continue;
2447
2448 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2449 }
2450 while (wait > 0);
2451
2452 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002453}
2454
2455static int
Florin Coras294afe22019-01-07 17:49:17 -08002456vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2457 vcl_si_set * read_map, vcl_si_set * write_map,
2458 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002459 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002460{
2461 vcl_mq_evt_conn_t *mqc;
2462 int __clib_unused n_read;
2463 int n_mq_evts, i;
2464 u64 buf;
2465
Florin Coras134a9962018-08-28 11:32:04 -07002466 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2467 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2468 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002469 for (i = 0; i < n_mq_evts; i++)
2470 {
Florin Coras134a9962018-08-28 11:32:04 -07002471 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002472 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002473 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002474 except_map, 0, bits_set);
2475 }
2476
2477 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2478}
2479
Dave Wallace543852a2017-08-03 02:11:34 -04002480int
Florin Coras294afe22019-01-07 17:49:17 -08002481vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2482 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002483{
Florin Coras54693d22018-07-17 10:46:29 -07002484 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002485 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002486 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002487 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002488
Dave Wallace7876d392017-10-19 03:53:57 -04002489 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002490 {
Florin Coras134a9962018-08-28 11:32:04 -07002491 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002492 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002493 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2494 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002495 }
Dave Wallace7876d392017-10-19 03:53:57 -04002496 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002497 {
Florin Coras134a9962018-08-28 11:32:04 -07002498 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002499 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002500 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2501 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002502 }
Dave Wallace7876d392017-10-19 03:53:57 -04002503 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002504 {
Florin Coras134a9962018-08-28 11:32:04 -07002505 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002506 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002507 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2508 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002509 }
2510
Florin Coras54693d22018-07-17 10:46:29 -07002511 if (!n_bits)
2512 return 0;
2513
2514 if (!write_map)
2515 goto check_rd;
2516
Florin Coras096a1d52021-01-27 15:33:51 -08002517 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2518 {
2519 if (!(s = vcl_session_get (wrk, sid)))
2520 {
2521 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2522 bits_set++;
2523 continue;
2524 }
Florin Coras54693d22018-07-17 10:46:29 -07002525
Florin Coras096a1d52021-01-27 15:33:51 -08002526 if (vcl_session_write_ready (s))
2527 {
2528 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2529 bits_set++;
2530 }
2531 else
2532 {
2533 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2534 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2535 }
2536 }
Florin Coras54693d22018-07-17 10:46:29 -07002537
2538check_rd:
2539 if (!read_map)
2540 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002541
Florin Coras096a1d52021-01-27 15:33:51 -08002542 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2543 {
2544 if (!(s = vcl_session_get (wrk, sid)))
2545 {
2546 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2547 bits_set++;
2548 continue;
2549 }
Florin Coras54693d22018-07-17 10:46:29 -07002550
Florin Coras096a1d52021-01-27 15:33:51 -08002551 if (vcl_session_read_ready (s))
2552 {
2553 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2554 bits_set++;
2555 }
2556 }
Florin Coras54693d22018-07-17 10:46:29 -07002557
2558check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002559
Florin Coras86f04502018-09-12 16:08:01 -07002560 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2561 {
2562 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2563 read_map, write_map, except_map, &bits_set);
2564 }
2565 vec_reset_length (wrk->unhandled_evts_vector);
2566
Florin Coras99368312018-08-02 10:45:44 -07002567 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002568 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002569 time_to_wait, &bits_set);
2570 else
Florin Coras134a9962018-08-28 11:32:04 -07002571 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002572 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002573
Dave Wallace543852a2017-08-03 02:11:34 -04002574 return (bits_set);
2575}
2576
Dave Wallacef7f809c2017-10-03 01:48:42 -04002577static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002578vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002579{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002580 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002581 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002582 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002583
Florin Corasc0737e92019-03-04 14:19:39 -08002584 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002585 return;
2586
Florin Coras6c3b2182020-10-19 18:36:48 -07002587 s = vcl_session_get_w_handle (wrk, vep_handle);
2588 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002589 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002590 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002591 goto done;
2592 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002593 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002594 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002595 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002596 goto done;
2597 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002598 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002599 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002600 "{\n"
2601 " is_vep = %u\n"
2602 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002603 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002604 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2605 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002606
Florin Coras7e5e62b2019-07-30 14:08:23 -07002607 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002608 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002609 s = vcl_session_get_w_handle (wrk, sh);
2610 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002611 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002612 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002613 goto done;
2614 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002615 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002616 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002617 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002618 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002619 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002620 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002621 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002622 goto done;
2623 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002624 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002625 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2626 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002627 sh, s->vep.vep_sh, vep_handle);
2628 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002629 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002630 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002631 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002632 " next_sh = 0x%x (%u)\n"
2633 " prev_sh = 0x%x (%u)\n"
2634 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002635 " ev.events = 0x%x\n"
2636 " ev.data.u64 = 0x%llx\n"
2637 " et_mask = 0x%x\n"
2638 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002639 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002640 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2641 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002642 }
2643 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002644
2645done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002646 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002647}
2648
2649int
2650vppcom_epoll_create (void)
2651{
Florin Coras134a9962018-08-28 11:32:04 -07002652 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002653 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002654
Florin Coras134a9962018-08-28 11:32:04 -07002655 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002656
Florin Coras6c3b2182020-10-19 18:36:48 -07002657 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002658 vep_session->vep.vep_sh = ~0;
2659 vep_session->vep.next_sh = ~0;
2660 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002661 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002662
Florin Corasa7a1a222018-12-30 17:11:31 -08002663 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2664 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002665
Florin Corasab2f6db2018-08-31 14:31:41 -07002666 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002667}
2668
2669int
Florin Coras134a9962018-08-28 11:32:04 -07002670vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002671 struct epoll_event *event)
2672{
Florin Coras134a9962018-08-28 11:32:04 -07002673 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002674 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002675 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002676 vcl_session_t *s;
2677 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002678
Florin Coras134a9962018-08-28 11:32:04 -07002679 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002680 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002681 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682 return VPPCOM_EINVAL;
2683 }
2684
Florin Coras134a9962018-08-28 11:32:04 -07002685 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002686 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002688 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002689 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002691 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002692 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002693 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002694 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002695 }
2696
Florin Coras134a9962018-08-28 11:32:04 -07002697 ASSERT (vep_session->vep.vep_sh == ~0);
2698 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002699
Florin Coras17ec5772020-08-12 20:46:29 -07002700 s = vcl_session_get_w_handle (wrk, session_handle);
2701 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002702 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002703 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002704 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002705 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002706 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002707 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002708 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002709 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002710 }
2711
2712 switch (op)
2713 {
2714 case EPOLL_CTL_ADD:
2715 if (PREDICT_FALSE (!event))
2716 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002717 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002718 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002719 }
Florin Coras2645f682021-06-04 15:59:41 -07002720 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
2721 {
2722 VDBG (0, "EPOLL_CTL_ADD: %u already epolled!", s->session_index);
2723 rv = VPPCOM_EEXIST;
2724 goto done;
2725 }
Florin Coras134a9962018-08-28 11:32:04 -07002726 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002727 {
Florin Coras7e12d942018-06-27 14:32:43 -07002728 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002729 next_session = vcl_session_get_w_handle (wrk,
2730 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002731 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002732 {
Florin Coras5e062572019-03-14 19:07:51 -07002733 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002734 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002735 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002736 }
Florin Coras134a9962018-08-28 11:32:04 -07002737 ASSERT (next_session->vep.prev_sh == vep_handle);
2738 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002739 }
Florin Coras17ec5772020-08-12 20:46:29 -07002740 s->vep.next_sh = vep_session->vep.next_sh;
2741 s->vep.prev_sh = vep_handle;
2742 s->vep.vep_sh = vep_handle;
2743 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2744 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002745 s->flags &= ~VCL_SESSION_F_IS_VEP;
2746 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002747 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002748
Florin Coras17ec5772020-08-12 20:46:29 -07002749 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2750 if (txf && (event->events & EPOLLOUT))
2751 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002752
Florin Coras6e3c1f82020-01-15 01:30:46 +00002753 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002754 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002755 {
2756 session_event_t e = { 0 };
2757 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002758 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002759 vec_add1 (wrk->unhandled_evts_vector, e);
2760 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002761 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002762 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002763 {
2764 session_event_t e = { 0 };
2765 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002766 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002767 vec_add1 (wrk->unhandled_evts_vector, e);
2768 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002769 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2770 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002771 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002772 break;
2773
2774 case EPOLL_CTL_MOD:
2775 if (PREDICT_FALSE (!event))
2776 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002777 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002778 rv = VPPCOM_EINVAL;
2779 goto done;
2780 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002781 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002782 {
Florin Coras5e062572019-03-14 19:07:51 -07002783 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002784 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002785 goto done;
2786 }
Florin Coras17ec5772020-08-12 20:46:29 -07002787 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002788 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002789 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002790 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791 rv = VPPCOM_EINVAL;
2792 goto done;
2793 }
hanlin475c9d72019-12-26 11:44:28 +08002794
Florin Coras2645f682021-06-04 15:59:41 -07002795 /* Generate EPOLLOUT if session write ready nd event was not on */
2796 if ((event->events & EPOLLOUT) && !(s->vep.ev.events & EPOLLOUT) &&
2797 (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002798 {
2799 session_event_t e = { 0 };
2800 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002801 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002802 vec_add1 (wrk->unhandled_evts_vector, e);
2803 }
Florin Coras2645f682021-06-04 15:59:41 -07002804 /* Generate EPOLLIN if session read ready and event was not on */
2805 if ((event->events & EPOLLIN) && !(s->vep.ev.events & EPOLLIN) &&
2806 (vcl_session_read_ready (s) > 0))
2807 {
2808 session_event_t e = { 0 };
2809 e.event_type = SESSION_IO_EVT_RX;
2810 e.session_index = s->session_index;
2811 vec_add1 (wrk->unhandled_evts_vector, e);
2812 }
Florin Coras17ec5772020-08-12 20:46:29 -07002813 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2814 s->vep.ev = *event;
2815 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras8fb22fd2021-02-17 17:35:32 -08002816 if (txf)
2817 {
2818 if (event->events & EPOLLOUT)
2819 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2820 else
2821 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2822 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002823 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2824 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002825 break;
2826
2827 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002828 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002829 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002830 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Florin Coras2645f682021-06-04 15:59:41 -07002831 rv = VPPCOM_ENOENT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002832 goto done;
2833 }
Florin Coras17ec5772020-08-12 20:46:29 -07002834 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002835 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002836 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002837 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002838 rv = VPPCOM_EINVAL;
2839 goto done;
2840 }
2841
Florin Coras17ec5772020-08-12 20:46:29 -07002842 if (s->vep.prev_sh == vep_handle)
2843 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002844 else
2845 {
Florin Coras7e12d942018-06-27 14:32:43 -07002846 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002847 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002848 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002849 {
Florin Coras5e062572019-03-14 19:07:51 -07002850 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002851 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002852 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002853 }
Florin Coras134a9962018-08-28 11:32:04 -07002854 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002855 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002856 }
Florin Coras17ec5772020-08-12 20:46:29 -07002857 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002858 {
Florin Coras7e12d942018-06-27 14:32:43 -07002859 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002860 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002861 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002862 {
Florin Coras5e062572019-03-14 19:07:51 -07002863 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002864 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002865 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002866 }
Florin Coras134a9962018-08-28 11:32:04 -07002867 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002868 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002869 }
2870
Florin Coras17ec5772020-08-12 20:46:29 -07002871 memset (&s->vep, 0, sizeof (s->vep));
2872 s->vep.next_sh = ~0;
2873 s->vep.prev_sh = ~0;
2874 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002875 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002876
Florin Coras17ec5772020-08-12 20:46:29 -07002877 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2878 if (txf)
2879 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002880
Florin Coras5e062572019-03-14 19:07:51 -07002881 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002882 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002883 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002884 break;
2885
2886 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002887 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002888 rv = VPPCOM_EINVAL;
2889 }
2890
Florin Coras134a9962018-08-28 11:32:04 -07002891 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002892
2893done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002894 return rv;
2895}
2896
Florin Coras86f04502018-09-12 16:08:01 -07002897static inline void
2898vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2899 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002900{
2901 session_disconnected_msg_t *disconnected_msg;
2902 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002903 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002904 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002905 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002906 u8 add_event = 0;
2907
2908 switch (e->event_type)
2909 {
Florin Coras653e43f2019-03-04 10:56:23 -08002910 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002911 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002912 s = vcl_session_get (wrk, sid);
2913 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002914 break;
Florin Corasb242d312020-10-26 15:35:40 -07002915 vcl_fifo_rx_evt_valid_or_break (s);
2916 session_events = s->vep.ev.events;
2917 if (!(EPOLLIN & s->vep.ev.events)
2918 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002919 break;
2920 add_event = 1;
2921 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002922 session_evt_data = s->vep.ev.data.u64;
2923 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002924 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002925 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002926 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002927 s = vcl_session_get (wrk, sid);
2928 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002929 break;
Florin Corasb242d312020-10-26 15:35:40 -07002930 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002931 if (!(EPOLLOUT & session_events))
2932 break;
2933 add_event = 1;
2934 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002935 session_evt_data = s->vep.ev.data.u64;
2936 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2937 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002938 break;
Florin Coras86f04502018-09-12 16:08:01 -07002939 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002940 if (!e->postponed)
2941 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2942 else
2943 s = vcl_session_get (wrk, e->session_index);
2944 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002945 break;
Florin Corasb242d312020-10-26 15:35:40 -07002946 session_events = s->vep.ev.events;
2947 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002948 if (!(EPOLLIN & session_events))
2949 break;
Florin Coras86f04502018-09-12 16:08:01 -07002950 add_event = 1;
2951 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002952 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002953 break;
2954 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002955 if (!e->postponed)
2956 {
2957 connected_msg = (session_connected_msg_t *) e->data;
2958 sid = vcl_session_connected_handler (wrk, connected_msg);
2959 }
2960 else
2961 sid = e->session_index;
2962 s = vcl_session_get (wrk, sid);
2963 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002964 break;
Florin Corasb242d312020-10-26 15:35:40 -07002965 session_events = s->vep.ev.events;
2966 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002967 if (!(EPOLLOUT & session_events))
2968 break;
2969 add_event = 1;
2970 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002971 session_evt_data = s->vep.ev.data.u64;
2972 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002973 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002974 break;
2975 case SESSION_CTRL_EVT_DISCONNECTED:
2976 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002977 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2978 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002979 break;
Florin Corasb242d312020-10-26 15:35:40 -07002980 sid = s->session_index;
2981 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002982 add_event = 1;
2983 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002984 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002985 break;
2986 case SESSION_CTRL_EVT_RESET:
2987 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002988 s = vcl_session_get (wrk, sid);
2989 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002990 break;
Florin Corasb242d312020-10-26 15:35:40 -07002991 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002992 add_event = 1;
2993 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002994 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002995 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002996 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2997 vcl_session_unlisten_reply_handler (wrk, e->data);
2998 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002999 case SESSION_CTRL_EVT_MIGRATED:
3000 vcl_session_migrated_handler (wrk, e->data);
3001 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07003002 case SESSION_CTRL_EVT_CLEANUP:
3003 vcl_session_cleanup_handler (wrk, e->data);
3004 break;
Florin Coras30e79c22019-01-02 19:31:22 -08003005 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
3006 vcl_session_req_worker_update_handler (wrk, e->data);
3007 break;
3008 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
3009 vcl_session_worker_update_reply_handler (wrk, e->data);
3010 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07003011 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
3012 vcl_session_app_add_segment_handler (wrk, e->data);
3013 break;
3014 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
3015 vcl_session_app_del_segment_handler (wrk, e->data);
3016 break;
hanlina3a48962020-07-13 11:09:15 +08003017 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07003018 vcl_worker_rpc_handler (wrk, e->data);
3019 break;
Florin Coras86f04502018-09-12 16:08:01 -07003020 default:
3021 VDBG (0, "unhandled: %u", e->event_type);
3022 break;
3023 }
3024
3025 if (add_event)
3026 {
3027 events[*num_ev].data.u64 = session_evt_data;
3028 if (EPOLLONESHOT & session_events)
3029 {
Florin Corasb242d312020-10-26 15:35:40 -07003030 s = vcl_session_get (wrk, sid);
3031 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07003032 }
3033 *num_ev += 1;
3034 }
3035}
3036
3037static int
3038vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
3039 struct epoll_event *events, u32 maxevents,
3040 double wait_for_time, u32 * num_ev)
3041{
Florin Coras99368312018-08-02 10:45:44 -07003042 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07003043 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07003044 int i;
3045
Florin Coras539663c2018-09-28 14:59:37 -07003046 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
3047 goto handle_dequeued;
3048
Florin Coras54693d22018-07-17 10:46:29 -07003049 if (svm_msg_q_is_empty (mq))
3050 {
3051 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08003052 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003053 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08003054 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07003055 else
3056 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003057 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08003058 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07003059 }
3060 }
Florin Corase003a1b2019-06-05 10:47:16 -07003061 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003062 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003063
Florin Coras539663c2018-09-28 14:59:37 -07003064handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003065 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003066 {
Florin Coras134a9962018-08-28 11:32:04 -07003067 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003068 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003069 if (*num_ev < maxevents)
3070 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3071 else
3072 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003073 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003074 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003075 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003076 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003077 return *num_ev;
3078}
3079
Florin Coras99368312018-08-02 10:45:44 -07003080static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003081vppcom_epoll_wait_condvar (vcl_worker_t *wrk, struct epoll_event *events,
3082 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003083{
Florin Corasccdb8b82021-04-28 13:01:06 -07003084 double end = -1;
Florin Coras14ed6df2019-03-06 21:13:42 -08003085
3086 if (!n_evts)
3087 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003088 if (timeout_ms > 0)
3089 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras14ed6df2019-03-06 21:13:42 -08003090 }
3091
3092 do
3093 {
3094 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
Florin Corasccdb8b82021-04-28 13:01:06 -07003095 timeout_ms, &n_evts);
3096 if (n_evts || !timeout_ms)
Florin Coras14ed6df2019-03-06 21:13:42 -08003097 return n_evts;
Florin Coras14ed6df2019-03-06 21:13:42 -08003098 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003099 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Coras14ed6df2019-03-06 21:13:42 -08003100
3101 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003102}
3103
3104static int
Florin Corasccdb8b82021-04-28 13:01:06 -07003105vppcom_epoll_wait_eventfd (vcl_worker_t *wrk, struct epoll_event *events,
3106 int maxevents, u32 n_evts, double timeout_ms)
Florin Coras99368312018-08-02 10:45:44 -07003107{
Florin Coras99368312018-08-02 10:45:44 -07003108 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003109 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003110 int n_mq_evts, i;
Florin Corasccdb8b82021-04-28 13:01:06 -07003111 double end = -1;
Florin Coras99368312018-08-02 10:45:44 -07003112 u64 buf;
3113
Florin Coras134a9962018-08-28 11:32:04 -07003114 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003115 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003116 {
Florin Corasccdb8b82021-04-28 13:01:06 -07003117 if (timeout_ms > 0)
3118 end = clib_time_now (&wrk->clib_time) + (timeout_ms / 1e3);
Florin Coras99368312018-08-02 10:45:44 -07003119 }
3120
Florin Corasb13ba132021-01-27 16:05:24 -08003121 do
3122 {
3123 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
Florin Corasccdb8b82021-04-28 13:01:06 -07003124 vec_len (wrk->mq_events), timeout_ms);
Florin Corasb13ba132021-01-27 16:05:24 -08003125 if (n_mq_evts < 0)
3126 {
3127 VDBG (0, "epoll_wait error %u", errno);
3128 return n_evts;
3129 }
3130
3131 for (i = 0; i < n_mq_evts; i++)
3132 {
3133 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3134 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3135 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3136 &n_evts);
3137 }
3138
Florin Corasccdb8b82021-04-28 13:01:06 -07003139 if (n_evts || !timeout_ms)
Florin Corasb13ba132021-01-27 16:05:24 -08003140 return n_evts;
Florin Corasb13ba132021-01-27 16:05:24 -08003141 }
Florin Corasccdb8b82021-04-28 13:01:06 -07003142 while (end == -1 || clib_time_now (&wrk->clib_time) < end);
Florin Corasb13ba132021-01-27 16:05:24 -08003143
3144 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003145}
3146
Dave Wallacef7f809c2017-10-03 01:48:42 -04003147int
Florin Coras134a9962018-08-28 11:32:04 -07003148vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003149 int maxevents, double wait_for_time)
3150{
Florin Coras134a9962018-08-28 11:32:04 -07003151 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003152 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003153 u32 n_evts = 0;
3154 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003155
3156 if (PREDICT_FALSE (maxevents <= 0))
3157 {
Florin Coras5e062572019-03-14 19:07:51 -07003158 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003159 return VPPCOM_EINVAL;
3160 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003161
Florin Coras134a9962018-08-28 11:32:04 -07003162 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003163 if (!vep_session)
3164 return VPPCOM_EBADFD;
3165
Florin Coras6c3b2182020-10-19 18:36:48 -07003166 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003167 {
Florin Coras5e062572019-03-14 19:07:51 -07003168 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003169 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003170 }
Florin Coras54693d22018-07-17 10:46:29 -07003171
3172 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003173
Florin Coras86f04502018-09-12 16:08:01 -07003174 if (vec_len (wrk->unhandled_evts_vector))
3175 {
3176 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3177 {
3178 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3179 events, &n_evts);
3180 if (n_evts == maxevents)
3181 {
Florin Corase003a1b2019-06-05 10:47:16 -07003182 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3183 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003184 }
3185 }
Florin Corase003a1b2019-06-05 10:47:16 -07003186 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003187 }
wanghanlin8919fec2021-03-18 20:00:41 +08003188 /* Request to only drain unhandled */
3189 if ((int) wait_for_time == -2)
3190 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003191
3192 if (vcm->cfg.use_mq_eventfd)
3193 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3194 wait_for_time);
3195
3196 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3197 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003198}
3199
Dave Wallace35830af2017-10-09 01:43:42 -04003200int
Florin Coras134a9962018-08-28 11:32:04 -07003201vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003202 void *buffer, uint32_t * buflen)
3203{
Florin Coras134a9962018-08-28 11:32:04 -07003204 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003205 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003206 vppcom_endpt_t *ep = buffer;
Florin Coras04ae8272021-04-12 19:55:37 -07003207 transport_endpt_attr_t tea;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003208 vcl_session_t *session;
3209 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003210
Florin Coras134a9962018-08-28 11:32:04 -07003211 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003212 if (!session)
3213 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003214
Dave Wallace35830af2017-10-09 01:43:42 -04003215 switch (op)
3216 {
3217 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003218 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003219 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3220 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003221 break;
3222
Dave Wallace227867f2017-11-13 21:21:53 -05003223 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003224 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003225 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3226 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003227 break;
3228
3229 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003230 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003231 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003232 *flags =
3233 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003234 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003235 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003236 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003237 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3238 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003239 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003240 }
3241 else
3242 rv = VPPCOM_EINVAL;
3243 break;
3244
3245 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003246 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003247 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003248 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003249 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003250 else
Florin Corasac422d62020-10-19 20:51:36 -07003251 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003252
Florin Coras5e062572019-03-14 19:07:51 -07003253 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3254 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003255 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003256 }
3257 else
3258 rv = VPPCOM_EINVAL;
3259 break;
3260
3261 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003262 if (PREDICT_TRUE (buffer && buflen &&
3263 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003264 {
Florin Coras7e12d942018-06-27 14:32:43 -07003265 ep->is_ip4 = session->transport.is_ip4;
3266 ep->port = session->transport.rmt_port;
3267 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003268 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3269 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003270 else
Dave Barach178cf492018-11-13 16:34:13 -05003271 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3272 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003273 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003274 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3275 "addr = %U, port %u", session_handle, ep->is_ip4,
3276 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003277 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3278 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003279 }
3280 else
3281 rv = VPPCOM_EINVAL;
3282 break;
3283
3284 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003285 if (PREDICT_TRUE (buffer && buflen &&
3286 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003287 {
Florin Coras7e12d942018-06-27 14:32:43 -07003288 ep->is_ip4 = session->transport.is_ip4;
3289 ep->port = session->transport.lcl_port;
3290 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003291 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3292 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003293 else
Dave Barach178cf492018-11-13 16:34:13 -05003294 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3295 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003296 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003297 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3298 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003299 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003300 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3301 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003302 }
3303 else
3304 rv = VPPCOM_EINVAL;
3305 break;
Stevenb5a11602017-10-11 09:59:30 -07003306
Florin Corasef7cbf62019-10-17 09:56:27 -07003307 case VPPCOM_ATTR_SET_LCL_ADDR:
3308 if (PREDICT_TRUE (buffer && buflen &&
3309 (*buflen >= sizeof (*ep)) && ep->ip))
3310 {
3311 session->transport.is_ip4 = ep->is_ip4;
3312 session->transport.lcl_port = ep->port;
3313 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3314 *buflen = sizeof (*ep);
3315 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3316 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3317 &session->transport.lcl_ip,
3318 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3319 clib_net_to_host_u16 (ep->port));
3320 }
3321 else
3322 rv = VPPCOM_EINVAL;
3323 break;
3324
Dave Wallace048b1d62018-01-03 22:24:41 -05003325 case VPPCOM_ATTR_GET_LIBC_EPFD:
3326 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003327 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003328 break;
3329
3330 case VPPCOM_ATTR_SET_LIBC_EPFD:
3331 if (PREDICT_TRUE (buffer && buflen &&
3332 (*buflen == sizeof (session->libc_epfd))))
3333 {
3334 session->libc_epfd = *(int *) buffer;
3335 *buflen = sizeof (session->libc_epfd);
3336
Florin Coras5e062572019-03-14 19:07:51 -07003337 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3338 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003339 }
3340 else
3341 rv = VPPCOM_EINVAL;
3342 break;
3343
3344 case VPPCOM_ATTR_GET_PROTOCOL:
3345 if (buffer && buflen && (*buflen >= sizeof (int)))
3346 {
Florin Coras7e12d942018-06-27 14:32:43 -07003347 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003348 *buflen = sizeof (int);
3349
Florin Coras5e062572019-03-14 19:07:51 -07003350 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3351 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003352 }
3353 else
3354 rv = VPPCOM_EINVAL;
3355 break;
3356
3357 case VPPCOM_ATTR_GET_LISTEN:
3358 if (buffer && buflen && (*buflen >= sizeof (int)))
3359 {
Florin Corasac422d62020-10-19 20:51:36 -07003360 *(int *) buffer = vcl_session_has_attr (session,
3361 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003362 *buflen = sizeof (int);
3363
Florin Coras5e062572019-03-14 19:07:51 -07003364 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3365 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003366 }
3367 else
3368 rv = VPPCOM_EINVAL;
3369 break;
3370
3371 case VPPCOM_ATTR_GET_ERROR:
3372 if (buffer && buflen && (*buflen >= sizeof (int)))
3373 {
3374 *(int *) buffer = 0;
3375 *buflen = sizeof (int);
3376
Florin Coras5e062572019-03-14 19:07:51 -07003377 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3378 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003379 }
3380 else
3381 rv = VPPCOM_EINVAL;
3382 break;
3383
3384 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3385 if (buffer && buflen && (*buflen >= sizeof (u32)))
3386 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003387
3388 /* VPP-TBD */
3389 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003390 session->tx_fifo ?
3391 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003392 vcm->cfg.tx_fifo_size);
3393 *buflen = sizeof (u32);
3394
Florin Coras5e062572019-03-14 19:07:51 -07003395 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3396 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3397 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003398 }
3399 else
3400 rv = VPPCOM_EINVAL;
3401 break;
3402
3403 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3404 if (buffer && buflen && (*buflen == sizeof (u32)))
3405 {
3406 /* VPP-TBD */
3407 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003408 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3409 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3410 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 }
3412 else
3413 rv = VPPCOM_EINVAL;
3414 break;
3415
3416 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3417 if (buffer && buflen && (*buflen >= sizeof (u32)))
3418 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003419
3420 /* VPP-TBD */
3421 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003422 session->rx_fifo ?
3423 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003424 vcm->cfg.rx_fifo_size);
3425 *buflen = sizeof (u32);
3426
Florin Coras5e062572019-03-14 19:07:51 -07003427 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3428 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003429 }
3430 else
3431 rv = VPPCOM_EINVAL;
3432 break;
3433
3434 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3435 if (buffer && buflen && (*buflen == sizeof (u32)))
3436 {
3437 /* VPP-TBD */
3438 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003439 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3440 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3441 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003442 }
3443 else
3444 rv = VPPCOM_EINVAL;
3445 break;
3446
3447 case VPPCOM_ATTR_GET_REUSEADDR:
3448 if (buffer && buflen && (*buflen >= sizeof (int)))
3449 {
3450 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003451 *(int *) buffer = vcl_session_has_attr (session,
3452 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003453 *buflen = sizeof (int);
3454
Florin Coras5e062572019-03-14 19:07:51 -07003455 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3456 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003457 }
3458 else
3459 rv = VPPCOM_EINVAL;
3460 break;
3461
Stevenb5a11602017-10-11 09:59:30 -07003462 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003463 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003464 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003465 {
3466 /* VPP-TBD */
3467 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003468 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003469 else
Florin Corasac422d62020-10-19 20:51:36 -07003470 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003471
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003473 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003474 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 }
3476 else
3477 rv = VPPCOM_EINVAL;
3478 break;
3479
3480 case VPPCOM_ATTR_GET_REUSEPORT:
3481 if (buffer && buflen && (*buflen >= sizeof (int)))
3482 {
3483 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003484 *(int *) buffer = vcl_session_has_attr (session,
3485 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003486 *buflen = sizeof (int);
3487
Florin Coras5e062572019-03-14 19:07:51 -07003488 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3489 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 }
3491 else
3492 rv = VPPCOM_EINVAL;
3493 break;
3494
3495 case VPPCOM_ATTR_SET_REUSEPORT:
3496 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003497 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003498 {
3499 /* VPP-TBD */
3500 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003501 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003502 else
Florin Corasac422d62020-10-19 20:51:36 -07003503 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003504
Florin Coras5e062572019-03-14 19:07:51 -07003505 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003506 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003507 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003508 }
3509 else
3510 rv = VPPCOM_EINVAL;
3511 break;
3512
3513 case VPPCOM_ATTR_GET_BROADCAST:
3514 if (buffer && buflen && (*buflen >= sizeof (int)))
3515 {
3516 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003517 *(int *) buffer = vcl_session_has_attr (session,
3518 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003519 *buflen = sizeof (int);
3520
Florin Coras5e062572019-03-14 19:07:51 -07003521 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3522 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003523 }
3524 else
3525 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003526 break;
3527
3528 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003529 if (buffer && buflen && (*buflen == sizeof (int)))
3530 {
3531 /* VPP-TBD */
3532 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003533 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003534 else
Florin Corasac422d62020-10-19 20:51:36 -07003535 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003536
Florin Coras5e062572019-03-14 19:07:51 -07003537 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003538 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003539 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003540 }
3541 else
3542 rv = VPPCOM_EINVAL;
3543 break;
3544
3545 case VPPCOM_ATTR_GET_V6ONLY:
3546 if (buffer && buflen && (*buflen >= sizeof (int)))
3547 {
3548 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003549 *(int *) buffer = vcl_session_has_attr (session,
3550 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003551 *buflen = sizeof (int);
3552
Florin Coras5e062572019-03-14 19:07:51 -07003553 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3554 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003555 }
3556 else
3557 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003558 break;
3559
3560 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003561 if (buffer && buflen && (*buflen == sizeof (int)))
3562 {
3563 /* VPP-TBD */
3564 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003565 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003566 else
Florin Corasac422d62020-10-19 20:51:36 -07003567 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003568
Florin Coras5e062572019-03-14 19:07:51 -07003569 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003570 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003571 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003572 }
3573 else
3574 rv = VPPCOM_EINVAL;
3575 break;
3576
3577 case VPPCOM_ATTR_GET_KEEPALIVE:
3578 if (buffer && buflen && (*buflen >= sizeof (int)))
3579 {
3580 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003581 *(int *) buffer = vcl_session_has_attr (session,
3582 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003583 *buflen = sizeof (int);
3584
Florin Coras5e062572019-03-14 19:07:51 -07003585 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3586 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003587 }
3588 else
3589 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003590 break;
Stevenbccd3392017-10-12 20:42:21 -07003591
3592 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003593 if (buffer && buflen && (*buflen == sizeof (int)))
3594 {
3595 /* VPP-TBD */
3596 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003597 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 else
Florin Corasac422d62020-10-19 20:51:36 -07003599 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003600
Florin Coras5e062572019-03-14 19:07:51 -07003601 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003602 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003603 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003604 }
3605 else
3606 rv = VPPCOM_EINVAL;
3607 break;
3608
3609 case VPPCOM_ATTR_GET_TCP_NODELAY:
3610 if (buffer && buflen && (*buflen >= sizeof (int)))
3611 {
3612 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003613 *(int *) buffer = vcl_session_has_attr (session,
3614 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003615 *buflen = sizeof (int);
3616
Florin Coras5e062572019-03-14 19:07:51 -07003617 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3618 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003619 }
3620 else
3621 rv = VPPCOM_EINVAL;
3622 break;
3623
3624 case VPPCOM_ATTR_SET_TCP_NODELAY:
3625 if (buffer && buflen && (*buflen == sizeof (int)))
3626 {
3627 /* VPP-TBD */
3628 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003629 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003630 else
Florin Corasac422d62020-10-19 20:51:36 -07003631 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003632
Florin Coras5e062572019-03-14 19:07:51 -07003633 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003634 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003635 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003636 }
3637 else
3638 rv = VPPCOM_EINVAL;
3639 break;
3640
3641 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3642 if (buffer && buflen && (*buflen >= sizeof (int)))
3643 {
3644 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003645 *(int *) buffer = vcl_session_has_attr (session,
3646 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003647 *buflen = sizeof (int);
3648
Florin Coras5e062572019-03-14 19:07:51 -07003649 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3650 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003651 }
3652 else
3653 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003654 break;
3655
3656 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003657 if (buffer && buflen && (*buflen == sizeof (int)))
3658 {
3659 /* VPP-TBD */
3660 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003661 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003662 else
Florin Corasac422d62020-10-19 20:51:36 -07003663 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003664
Florin Coras5e062572019-03-14 19:07:51 -07003665 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003666 vcl_session_has_attr (session,
3667 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003668 }
3669 else
3670 rv = VPPCOM_EINVAL;
3671 break;
3672
3673 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3674 if (buffer && buflen && (*buflen >= sizeof (int)))
3675 {
3676 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003677 *(int *) buffer = vcl_session_has_attr (session,
3678 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003679 *buflen = sizeof (int);
3680
Florin Coras5e062572019-03-14 19:07:51 -07003681 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3682 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003683 }
3684 else
3685 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003686 break;
3687
3688 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003689 if (buffer && buflen && (*buflen == sizeof (int)))
3690 {
3691 /* VPP-TBD */
3692 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003693 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003694 else
Florin Corasac422d62020-10-19 20:51:36 -07003695 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003696
Florin Coras5e062572019-03-14 19:07:51 -07003697 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003698 vcl_session_has_attr (session,
3699 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003700 }
3701 else
3702 rv = VPPCOM_EINVAL;
3703 break;
3704
3705 case VPPCOM_ATTR_GET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003706 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003707 {
Florin Coras04ae8272021-04-12 19:55:37 -07003708 rv = VPPCOM_EINVAL;
3709 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003710 }
Florin Coras04ae8272021-04-12 19:55:37 -07003711
3712 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3713 tea.mss = *(u32 *) buffer;
3714 if (vcl_session_transport_attr (wrk, session, 1 /* is_get */, &tea))
3715 rv = VPPCOM_ENOPROTOOPT;
3716
3717 if (!rv)
3718 {
3719 *(u32 *) buffer = tea.mss;
3720 *buflen = sizeof (int);
3721 }
3722
3723 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d", *(int *) buffer,
3724 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003725 break;
3726
3727 case VPPCOM_ATTR_SET_TCP_USER_MSS:
Florin Coras04ae8272021-04-12 19:55:37 -07003728 if (!(buffer && buflen && (*buflen == sizeof (u32))))
Dave Wallace048b1d62018-01-03 22:24:41 -05003729 {
Florin Coras04ae8272021-04-12 19:55:37 -07003730 rv = VPPCOM_EINVAL;
3731 break;
Dave Wallace048b1d62018-01-03 22:24:41 -05003732 }
Florin Coras04ae8272021-04-12 19:55:37 -07003733
3734 tea.type = TRANSPORT_ENDPT_ATTR_MSS;
3735 tea.mss = *(u32 *) buffer;
3736 if (vcl_session_transport_attr (wrk, session, 0 /* is_get */, &tea))
3737 rv = VPPCOM_ENOPROTOOPT;
3738
3739 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d", tea.mss,
3740 *buflen);
Stevenbccd3392017-10-12 20:42:21 -07003741 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003742
Florin Coras7baeb712019-01-04 17:05:43 -08003743 case VPPCOM_ATTR_SET_SHUT:
3744 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003745 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003746 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003747 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003748 break;
3749
3750 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003751 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003752 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003753 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003754 tmp_flags |= 2;
3755 if (tmp_flags == 1)
3756 *(int *) buffer = SHUT_RD;
3757 else if (tmp_flags == 2)
3758 *(int *) buffer = SHUT_WR;
3759 else if (tmp_flags == 3)
3760 *(int *) buffer = SHUT_RDWR;
3761 *buflen = sizeof (int);
3762 break;
Florin Coras1e966172020-05-16 18:18:14 +00003763
3764 case VPPCOM_ATTR_SET_CONNECTED:
3765 session->flags |= VCL_SESSION_F_CONNECTED;
3766 break;
3767
Florin Corasa5a9efd2021-01-05 17:03:29 -08003768 case VPPCOM_ATTR_SET_CKPAIR:
3769 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3770 !vcl_session_has_crypto (session))
3771 {
3772 rv = VPPCOM_EINVAL;
3773 break;
3774 }
Florin Corasa54b62d2021-04-21 09:05:56 -07003775 if (!session->ext_config)
3776 {
Florin Coras87f63892021-05-01 16:01:40 -07003777 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_CRYPTO,
3778 sizeof (transport_endpt_ext_cfg_t));
Florin Corasa54b62d2021-04-21 09:05:56 -07003779 }
3780 else if (session->ext_config->type != TRANSPORT_ENDPT_EXT_CFG_CRYPTO)
3781 {
3782 rv = VPPCOM_EINVAL;
3783 break;
3784 }
3785
3786 session->ext_config->crypto.ckpair_index = *(uint32_t *) buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003787 break;
3788
Florin Coras6a6555a2021-01-28 11:39:27 -08003789 case VPPCOM_ATTR_SET_VRF:
3790 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3791 {
3792 rv = VPPCOM_EINVAL;
3793 break;
3794 }
3795 session->vrf = *(u32 *) buffer;
3796 break;
3797
3798 case VPPCOM_ATTR_GET_VRF:
3799 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3800 {
3801 rv = VPPCOM_EINVAL;
3802 break;
3803 }
3804 *(u32 *) buffer = session->vrf;
3805 *buflen = sizeof (u32);
3806 break;
3807
wanghanlin0674f852021-02-22 10:38:36 +08003808 case VPPCOM_ATTR_GET_DOMAIN:
Florin Corasd77325c2021-02-23 12:03:03 -08003809 if (!(buffer && buflen && (*buflen >= sizeof (int))))
wanghanlin0674f852021-02-22 10:38:36 +08003810 {
Florin Corasd77325c2021-02-23 12:03:03 -08003811 rv = VPPCOM_EINVAL;
3812 break;
wanghanlin0674f852021-02-22 10:38:36 +08003813 }
Florin Corasd77325c2021-02-23 12:03:03 -08003814
3815 if (session->transport.is_ip4)
3816 *(int *) buffer = AF_INET;
wanghanlin0674f852021-02-22 10:38:36 +08003817 else
Florin Corasd77325c2021-02-23 12:03:03 -08003818 *(int *) buffer = AF_INET6;
3819 *buflen = sizeof (int);
3820
wanghanlin0674f852021-02-22 10:38:36 +08003821 VDBG (2, "VPPCOM_ATTR_GET_DOMAIN: %d, buflen %u", *(int *) buffer,
3822 *buflen);
3823 break;
3824
Florin Coras87f63892021-05-01 16:01:40 -07003825 case VPPCOM_ATTR_SET_ENDPT_EXT_CFG:
3826 if (!(buffer && buflen && (*buflen > 0)))
3827 {
3828 rv = VPPCOM_EINVAL;
3829 break;
3830 }
3831 if (session->ext_config)
3832 {
3833 rv = VPPCOM_EINVAL;
3834 break;
3835 }
3836 vcl_session_alloc_ext_cfg (session, TRANSPORT_ENDPT_EXT_CFG_NONE,
3837 *buflen + sizeof (u32));
3838 clib_memcpy (session->ext_config->data, buffer, *buflen);
3839 session->ext_config->len = *buflen;
3840 break;
3841
Dave Wallacee22aa742017-10-20 12:30:38 -04003842 default:
3843 rv = VPPCOM_EINVAL;
3844 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003845 }
3846
Dave Wallace35830af2017-10-09 01:43:42 -04003847 return rv;
3848}
3849
Stevenac1f96d2017-10-24 16:03:58 -07003850int
Florin Coras134a9962018-08-28 11:32:04 -07003851vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003852 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3853{
Florin Coras134a9962018-08-28 11:32:04 -07003854 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003855 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003856 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003857
3858 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003859 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003860 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003861 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003862 else
3863 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003864 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003865 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003866 }
3867
Florin Coras0a1e1832020-03-29 18:54:04 +00003868 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003869 {
Florin Coras5da10c42020-04-01 04:31:21 +00003870 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003871 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003872 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3873 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003874 else
Dave Barach178cf492018-11-13 16:34:13 -05003875 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3876 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003877 ep->is_ip4 = session->transport.is_ip4;
3878 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003879 }
Florin Coras460dce62018-07-27 05:45:06 -07003880
Stevenac1f96d2017-10-24 16:03:58 -07003881 return rv;
3882}
3883
3884int
Florin Coras134a9962018-08-28 11:32:04 -07003885vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003886 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3887{
Florin Coras7a2abce2020-04-05 19:25:44 +00003888 vcl_worker_t *wrk = vcl_worker_get_current ();
3889 vcl_session_t *s;
3890
3891 s = vcl_session_get_w_handle (wrk, session_handle);
3892 if (!s)
3893 return VPPCOM_EBADFD;
3894
Dave Wallace617dffa2017-10-26 14:47:06 -04003895 if (!buffer)
3896 return VPPCOM_EINVAL;
3897
3898 if (ep)
3899 {
Florin Coras5824cc52020-10-20 18:44:41 -07003900 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003901 return VPPCOM_EINVAL;
3902
3903 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003904 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003905 {
Florin Coras5824cc52020-10-20 18:44:41 -07003906 u32 session_index = s->session_index;
3907 f64 timeout = vcm->cfg.session_timeout;
3908 int rv;
3909
Florin Coras5da10c42020-04-01 04:31:21 +00003910 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003911 rv = vppcom_wait_for_session_state_change (session_index,
3912 VCL_STATE_READY,
3913 timeout);
3914 if (rv < 0)
3915 return rv;
3916 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003917 }
Florin Coras5824cc52020-10-20 18:44:41 -07003918
3919 s->transport.is_ip4 = ep->is_ip4;
3920 s->transport.rmt_port = ep->port;
3921 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003922 }
3923
3924 if (flags)
3925 {
3926 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003927 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003928 }
3929
Florin Coras7a2abce2020-04-05 19:25:44 +00003930 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3931 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003932}
3933
Dave Wallace048b1d62018-01-03 22:24:41 -05003934int
3935vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3936{
Florin Coras134a9962018-08-28 11:32:04 -07003937 vcl_worker_t *wrk = vcl_worker_get_current ();
3938 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003939 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003940 svm_msg_q_msg_t msg;
3941 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003942 int rv, num_ev = 0;
3943
Florin Coras5e062572019-03-14 19:07:51 -07003944 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003945
3946 if (!vp)
3947 return VPPCOM_EFAULT;
3948
3949 do
3950 {
Florin Coras7e12d942018-06-27 14:32:43 -07003951 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003952
Florin Coras6917b942018-11-13 22:44:54 -08003953 /* Dequeue all events and drop all unhandled io events */
3954 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3955 {
3956 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3957 vcl_handle_mq_event (wrk, e);
3958 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3959 }
3960 vec_reset_length (wrk->unhandled_evts_vector);
3961
Dave Wallace048b1d62018-01-03 22:24:41 -05003962 for (i = 0; i < n_sids; i++)
3963 {
Florin Coras7baeb712019-01-04 17:05:43 -08003964 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003965 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003966 {
3967 vp[i].revents = POLLHUP;
3968 num_ev++;
3969 continue;
3970 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003971
Florin Coras6917b942018-11-13 22:44:54 -08003972 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003973
3974 if (POLLIN & vp[i].events)
3975 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003976 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003977 if (rv > 0)
3978 {
Florin Coras6917b942018-11-13 22:44:54 -08003979 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003980 num_ev++;
3981 }
3982 else if (rv < 0)
3983 {
3984 switch (rv)
3985 {
3986 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003987 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003988 break;
3989
3990 default:
Florin Coras6917b942018-11-13 22:44:54 -08003991 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003992 break;
3993 }
3994 num_ev++;
3995 }
3996 }
3997
3998 if (POLLOUT & vp[i].events)
3999 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08004000 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004001 if (rv > 0)
4002 {
Florin Coras6917b942018-11-13 22:44:54 -08004003 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05004004 num_ev++;
4005 }
4006 else if (rv < 0)
4007 {
4008 switch (rv)
4009 {
4010 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08004011 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05004012 break;
4013
4014 default:
Florin Coras6917b942018-11-13 22:44:54 -08004015 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05004016 break;
4017 }
4018 num_ev++;
4019 }
4020 }
4021
Dave Wallace7e607a72018-06-18 18:41:32 -04004022 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004023 {
Florin Coras6917b942018-11-13 22:44:54 -08004024 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05004025 num_ev++;
4026 }
4027 }
4028 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07004029 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05004030 }
4031 while ((num_ev == 0) && keep_trying);
4032
Dave Wallace048b1d62018-01-03 22:24:41 -05004033 return num_ev;
4034}
4035
Florin Coras99368312018-08-02 10:45:44 -07004036int
4037vppcom_mq_epoll_fd (void)
4038{
Florin Coras134a9962018-08-28 11:32:04 -07004039 vcl_worker_t *wrk = vcl_worker_get_current ();
4040 return wrk->mqs_epfd;
4041}
4042
4043int
Florin Coras30e79c22019-01-02 19:31:22 -08004044vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07004045{
4046 return session_handle & 0xFFFFFF;
4047}
4048
4049int
Florin Coras30e79c22019-01-02 19:31:22 -08004050vppcom_session_worker (vcl_session_handle_t session_handle)
4051{
4052 return session_handle >> 24;
4053}
4054
4055int
Florin Coras134a9962018-08-28 11:32:04 -07004056vppcom_worker_register (void)
4057{
Florin Coras47c40e22018-11-26 17:01:36 -08004058 if (!vcl_worker_alloc_and_init ())
4059 return VPPCOM_EEXIST;
4060
Florin Coras47c40e22018-11-26 17:01:36 -08004061 if (vcl_worker_register_with_vpp ())
4062 return VPPCOM_EEXIST;
4063
4064 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07004065}
4066
Florin Coras369db832019-07-08 12:34:45 -07004067void
4068vppcom_worker_unregister (void)
4069{
4070 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
4071 vcl_set_worker_index (~0);
4072}
4073
Pivo6017ff02020-05-04 17:57:33 +02004074void
4075vppcom_worker_index_set (int index)
4076{
4077 vcl_set_worker_index (index);
4078}
4079
Florin Corasdfe4cf42018-11-28 22:13:45 -08004080int
4081vppcom_worker_index (void)
4082{
4083 return vcl_get_worker_index ();
4084}
4085
Florin Corase0982e52019-01-25 13:19:56 -08004086int
4087vppcom_worker_mqs_epfd (void)
4088{
4089 vcl_worker_t *wrk = vcl_worker_get_current ();
4090 if (!vcm->cfg.use_mq_eventfd)
4091 return -1;
4092 return wrk->mqs_epfd;
4093}
4094
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02004095int
4096vppcom_session_is_connectable_listener (uint32_t session_handle)
4097{
4098 vcl_session_t *session;
4099 vcl_worker_t *wrk = vcl_worker_get_current ();
4100 session = vcl_session_get_w_handle (wrk, session_handle);
4101 if (!session)
4102 return VPPCOM_EBADFD;
4103 return vcl_session_is_connectable_listener (wrk, session);
4104}
4105
4106int
4107vppcom_session_listener (uint32_t session_handle)
4108{
4109 vcl_worker_t *wrk = vcl_worker_get_current ();
4110 vcl_session_t *listen_session, *session;
4111 session = vcl_session_get_w_handle (wrk, session_handle);
4112 if (!session)
4113 return VPPCOM_EBADFD;
4114 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
4115 return VPPCOM_EBADFD;
4116 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
4117 if (!listen_session)
4118 return VPPCOM_EBADFD;
4119 return vcl_session_handle (listen_session);
4120}
4121
4122int
4123vppcom_session_n_accepted (uint32_t session_handle)
4124{
4125 vcl_worker_t *wrk = vcl_worker_get_current ();
4126 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
4127 if (!session)
4128 return VPPCOM_EBADFD;
4129 return session->n_accepted_sessions;
4130}
4131
Florin Coras66ec4672020-06-15 07:59:40 -07004132const char *
4133vppcom_proto_str (vppcom_proto_t proto)
4134{
4135 char const *proto_str;
4136
4137 switch (proto)
4138 {
4139 case VPPCOM_PROTO_TCP:
4140 proto_str = "TCP";
4141 break;
4142 case VPPCOM_PROTO_UDP:
4143 proto_str = "UDP";
4144 break;
4145 case VPPCOM_PROTO_TLS:
4146 proto_str = "TLS";
4147 break;
4148 case VPPCOM_PROTO_QUIC:
4149 proto_str = "QUIC";
4150 break;
Florin Coras4b47ee22020-11-19 13:38:26 -08004151 case VPPCOM_PROTO_DTLS:
4152 proto_str = "DTLS";
4153 break;
Florin Coras6621abf2021-01-06 17:35:17 -08004154 case VPPCOM_PROTO_SRTP:
4155 proto_str = "SRTP";
4156 break;
Florin Coras66ec4672020-06-15 07:59:40 -07004157 default:
4158 proto_str = "UNKNOWN";
4159 break;
4160 }
4161 return proto_str;
4162}
4163
4164const char *
4165vppcom_retval_str (int retval)
4166{
4167 char const *st;
4168
4169 switch (retval)
4170 {
4171 case VPPCOM_OK:
4172 st = "VPPCOM_OK";
4173 break;
4174
4175 case VPPCOM_EAGAIN:
4176 st = "VPPCOM_EAGAIN";
4177 break;
4178
4179 case VPPCOM_EFAULT:
4180 st = "VPPCOM_EFAULT";
4181 break;
4182
4183 case VPPCOM_ENOMEM:
4184 st = "VPPCOM_ENOMEM";
4185 break;
4186
4187 case VPPCOM_EINVAL:
4188 st = "VPPCOM_EINVAL";
4189 break;
4190
4191 case VPPCOM_EBADFD:
4192 st = "VPPCOM_EBADFD";
4193 break;
4194
4195 case VPPCOM_EAFNOSUPPORT:
4196 st = "VPPCOM_EAFNOSUPPORT";
4197 break;
4198
4199 case VPPCOM_ECONNABORTED:
4200 st = "VPPCOM_ECONNABORTED";
4201 break;
4202
4203 case VPPCOM_ECONNRESET:
4204 st = "VPPCOM_ECONNRESET";
4205 break;
4206
4207 case VPPCOM_ENOTCONN:
4208 st = "VPPCOM_ENOTCONN";
4209 break;
4210
4211 case VPPCOM_ECONNREFUSED:
4212 st = "VPPCOM_ECONNREFUSED";
4213 break;
4214
4215 case VPPCOM_ETIMEDOUT:
4216 st = "VPPCOM_ETIMEDOUT";
4217 break;
4218
4219 default:
4220 st = "UNKNOWN_STATE";
4221 break;
4222 }
4223
4224 return st;
4225}
4226
Florin Corasa5a9efd2021-01-05 17:03:29 -08004227int
4228vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4229{
4230 if (vcm->cfg.vpp_app_socket_api)
4231 {
4232 clib_warning ("not supported");
4233 return VPPCOM_EINVAL;
4234 }
4235 return vcl_bapi_add_cert_key_pair (ckpair);
4236}
4237
4238int
4239vppcom_del_cert_key_pair (uint32_t ckpair_index)
4240{
4241 if (vcm->cfg.vpp_app_socket_api)
4242 {
4243 clib_warning ("not supported");
4244 return VPPCOM_EINVAL;
4245 }
4246 return vcl_bapi_del_cert_key_pair (ckpair_index);
4247}
4248
Dave Wallacee22aa742017-10-20 12:30:38 -04004249/*
4250 * fd.io coding-style-patch-verification: ON
4251 *
4252 * Local Variables:
4253 * eval: (c-set-style "gnu")
4254 * End:
4255 */