blob: 21892438c376f2f46b5924fbe907ca9252d7942d [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
167vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
168{
169 app_session_evt_t _app_evt, *app_evt = &_app_evt;
170 session_listen_msg_t *mp;
171 svm_msg_q_t *mq;
172
173 mq = vcl_worker_ctrl_mq (wrk);
174 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
175 mp = (session_listen_msg_t *) app_evt->evt->data;
176 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700177 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700178 mp->context = s->session_index;
179 mp->wrk_index = wrk->vpp_wrk_index;
180 mp->is_ip4 = s->transport.is_ip4;
181 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
182 mp->port = s->transport.lcl_port;
183 mp->proto = s->session_type;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800184 mp->ckpair_index = s->ckpair_index;
Florin Coras6a6555a2021-01-28 11:39:27 -0800185 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +0000186 if (s->flags & VCL_SESSION_F_CONNECTED)
187 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700188 app_send_ctrl_evt_to_vpp (mq, app_evt);
189}
190
191static void
192vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
193{
194 app_session_evt_t _app_evt, *app_evt = &_app_evt;
195 session_connect_msg_t *mp;
196 svm_msg_q_t *mq;
197
198 mq = vcl_worker_ctrl_mq (wrk);
199 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
200 mp = (session_connect_msg_t *) app_evt->evt->data;
201 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700202 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700203 mp->context = s->session_index;
204 mp->wrk_index = wrk->vpp_wrk_index;
205 mp->is_ip4 = s->transport.is_ip4;
206 mp->parent_handle = s->parent_handle;
207 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700208 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700209 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000210 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700211 mp->proto = s->session_type;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800212 mp->ckpair_index = s->ckpair_index;
Florin Coras6a6555a2021-01-28 11:39:27 -0800213 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000214 if (s->flags & VCL_SESSION_F_CONNECTED)
215 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700216 app_send_ctrl_evt_to_vpp (mq, app_evt);
217}
218
219void
220vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
221{
222 app_session_evt_t _app_evt, *app_evt = &_app_evt;
223 session_unlisten_msg_t *mp;
224 svm_msg_q_t *mq;
225
226 mq = vcl_worker_ctrl_mq (wrk);
227 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
228 mp = (session_unlisten_msg_t *) app_evt->evt->data;
229 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700230 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700231 mp->wrk_index = wrk->vpp_wrk_index;
232 mp->handle = s->vpp_handle;
233 mp->context = wrk->wrk_index;
234 app_send_ctrl_evt_to_vpp (mq, app_evt);
235}
236
237static void
238vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
239{
240 app_session_evt_t _app_evt, *app_evt = &_app_evt;
241 session_disconnect_msg_t *mp;
242 svm_msg_q_t *mq;
243
244 /* Send to thread that owns the session */
245 mq = s->vpp_evt_q;
246 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
247 mp = (session_disconnect_msg_t *) app_evt->evt->data;
248 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700249 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700250 mp->handle = s->vpp_handle;
251 app_send_ctrl_evt_to_vpp (mq, app_evt);
252}
253
254static void
255vcl_send_app_detach (vcl_worker_t * wrk)
256{
257 app_session_evt_t _app_evt, *app_evt = &_app_evt;
258 session_app_detach_msg_t *mp;
259 svm_msg_q_t *mq;
260
261 mq = vcl_worker_ctrl_mq (wrk);
262 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
263 mp = (session_app_detach_msg_t *) app_evt->evt->data;
264 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700265 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700266 app_send_ctrl_evt_to_vpp (mq, app_evt);
267}
Florin Coras697faea2018-06-27 17:10:49 -0700268
Florin Coras54693d22018-07-17 10:46:29 -0700269static void
270vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
271 session_handle_t handle, int retval)
272{
273 app_session_evt_t _app_evt, *app_evt = &_app_evt;
274 session_accepted_reply_msg_t *rmp;
275 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
276 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
277 rmp->handle = handle;
278 rmp->context = context;
279 rmp->retval = retval;
280 app_send_ctrl_evt_to_vpp (mq, app_evt);
281}
282
Florin Coras99368312018-08-02 10:45:44 -0700283static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800284vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
285 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700286{
287 app_session_evt_t _app_evt, *app_evt = &_app_evt;
288 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800289 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700290 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
291 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800292 rmp->handle = s->vpp_handle;
293 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700294 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800295 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700296}
297
Florin Corasc9fbd662018-08-24 12:59:56 -0700298static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800299vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
300 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700301{
302 app_session_evt_t _app_evt, *app_evt = &_app_evt;
303 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800304 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
305 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700306 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800307 rmp->handle = s->vpp_handle;
308 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700309 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800310 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700311}
312
Florin Coras30e79c22019-01-02 19:31:22 -0800313void
314vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
315 u32 wrk_index)
316{
317 app_session_evt_t _app_evt, *app_evt = &_app_evt;
318 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800319
Florin Coras52dd29f2020-11-18 19:02:17 -0800320 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
321 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800322 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700323 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800324 mp->handle = s->vpp_handle;
325 mp->req_wrk_index = wrk->vpp_wrk_index;
326 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800327 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800328}
329
hanlina3a48962020-07-13 11:09:15 +0800330int
Florin Coras40c07ce2020-07-16 20:46:17 -0700331vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
332{
333 app_session_evt_t _app_evt, *app_evt = &_app_evt;
334 session_app_wrk_rpc_msg_t *mp;
335 vcl_worker_t *dst_wrk, *wrk;
336 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800337 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700338
339 if (data_len > sizeof (mp->data))
340 goto done;
341
342 clib_spinlock_lock (&vcm->workers_lock);
343
344 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
345 if (!dst_wrk)
346 goto done;
347
348 wrk = vcl_worker_get_current ();
349 mq = vcl_worker_ctrl_mq (wrk);
350 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
351 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700352 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700353 mp->wrk_index = dst_wrk->vpp_wrk_index;
354 clib_memcpy (mp->data, data, data_len);
355 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800356 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700357
358done:
359 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800360 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700361}
362
Florin Coras54693d22018-07-17 10:46:29 -0700363static u32
Florin Coras00cca802019-06-06 09:38:44 -0700364vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
365 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700366{
367 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700368 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700369
Florin Coras134a9962018-08-28 11:32:04 -0700370 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700371
Florin Coras00cca802019-06-06 09:38:44 -0700372 listen_session = vcl_session_get (wrk, ls_index);
373 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700374 {
Florin Coras00cca802019-06-06 09:38:44 -0700375 VDBG (0, "ERROR: listener handle %lu does not match session %u",
376 mp->listener_handle, ls_index);
377 goto error;
378 }
379
Florin Corasc547e912020-12-08 17:50:45 -0800380 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800381 mp->server_tx_fifo,
382 mp->vpp_event_queue_address, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700383 {
Florin Corasc547e912020-12-08 17:50:45 -0800384 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700385 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700386 }
387
Florin Coras54693d22018-07-17 10:46:29 -0700388 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700389 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700390 session->transport.rmt_port = mp->rmt.port;
391 session->transport.is_ip4 = mp->rmt.is_ip4;
392 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500393 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700394
Florin Coras134a9962018-08-28 11:32:04 -0700395 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700396 session->transport.lcl_port = listen_session->transport.lcl_port;
397 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700398 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200399 session->is_dgram = vcl_proto_is_dgram (session->session_type);
400 session->listener_index = listen_session->session_index;
401 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700402
Florin Coras5e062572019-03-14 19:07:51 -0700403 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
404 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700405 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
406 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
407 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700408 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
409
Florin Coras00cca802019-06-06 09:38:44 -0700410 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
411 session->vpp_handle, 0);
412
Florin Coras134a9962018-08-28 11:32:04 -0700413 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700414
415error:
Florin Corasb4624182020-12-11 13:58:12 -0800416 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
417 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700418 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
419 VNET_API_ERROR_INVALID_ARGUMENT);
420 vcl_session_free (wrk, session);
421 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700422}
423
424static u32
Florin Coras134a9962018-08-28 11:32:04 -0700425vcl_session_connected_handler (vcl_worker_t * wrk,
426 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700427{
Florin Coras99368312018-08-02 10:45:44 -0700428 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800429 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700430
431 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700432 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700433 if (!session)
434 {
Florin Coras5e062572019-03-14 19:07:51 -0700435 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
436 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700437 return VCL_INVALID_SESSION_INDEX;
438 }
Florin Coras54693d22018-07-17 10:46:29 -0700439 if (mp->retval)
440 {
Florin Coras5e062572019-03-14 19:07:51 -0700441 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700442 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700443 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700444 session->vpp_handle = mp->handle;
445 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700446 }
447
Florin Corasdbc9c592019-09-25 16:37:43 -0700448 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800449
450 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800451 mp->server_tx_fifo,
452 mp->vpp_event_queue_address, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800453 {
Florin Corasc547e912020-12-08 17:50:45 -0800454 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700455 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700456 vcl_send_session_disconnect (wrk, session);
457 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800458 }
459
Florin Coras653e43f2019-03-04 10:56:23 -0800460 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700461 {
Florin Corasc547e912020-12-08 17:50:45 -0800462 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800463 mp->ct_tx_fifo, (uword) ~0, 1, session))
Florin Coras653e43f2019-03-04 10:56:23 -0800464 {
Florin Corasc547e912020-12-08 17:50:45 -0800465 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700466 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700467 vcl_send_session_disconnect (wrk, session);
468 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800469 }
Florin Coras99368312018-08-02 10:45:44 -0700470 }
Florin Coras54693d22018-07-17 10:46:29 -0700471
Florin Coras09d18c22019-04-24 11:10:02 -0700472 session->transport.is_ip4 = mp->lcl.is_ip4;
473 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500474 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700475 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700476
477 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700478 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700479 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700480 vcl_send_session_disconnect (wrk, session);
481 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700482 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700483
484 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800485 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700486
Florin Coras5e062572019-03-14 19:07:51 -0700487 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
488 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700489 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700490
Florin Coras54693d22018-07-17 10:46:29 -0700491 return session_index;
492}
493
Florin Coras3c7d4f92018-12-14 11:28:43 -0800494static int
495vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
496{
497 vcl_session_msg_t *accepted_msg;
498 int i;
499
500 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
501 {
502 accepted_msg = &session->accept_evts_fifo[i];
503 if (accepted_msg->accepted_msg.handle == handle)
504 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800505 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800506 return 1;
507 }
508 }
509 return 0;
510}
511
Florin Corasc9fbd662018-08-24 12:59:56 -0700512static u32
Florin Coras134a9962018-08-28 11:32:04 -0700513vcl_session_reset_handler (vcl_worker_t * wrk,
514 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700515{
516 vcl_session_t *session;
517 u32 sid;
518
Florin Coras134a9962018-08-28 11:32:04 -0700519 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
520 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700521 if (!session)
522 {
523 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
524 return VCL_INVALID_SESSION_INDEX;
525 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800526
527 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700528 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800529 {
530
531 if (!vcl_flag_accepted_session (session, reset_msg->handle,
532 VCL_ACCEPTED_F_RESET))
533 VDBG (0, "session was not accepted!");
534 return VCL_INVALID_SESSION_INDEX;
535 }
536
Florin Corasc127d5a2020-10-14 16:35:58 -0700537 if (session->session_state != VCL_STATE_CLOSED)
538 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800539 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700540 return sid;
541}
542
Florin Coras60116992018-08-27 09:52:18 -0700543static u32
Florin Coras134a9962018-08-28 11:32:04 -0700544vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700545{
546 vcl_session_t *session;
547 u32 sid = mp->context;
548
Florin Coras134a9962018-08-28 11:32:04 -0700549 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700550 if (mp->retval)
551 {
Florin Coras5e062572019-03-14 19:07:51 -0700552 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700553 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700554 if (session)
555 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700556 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700557 session->vpp_handle = mp->handle;
558 return sid;
559 }
560 else
561 {
Florin Coras5e062572019-03-14 19:07:51 -0700562 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
563 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700564 return VCL_INVALID_SESSION_INDEX;
565 }
566 }
567
568 session->vpp_handle = mp->handle;
569 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500570 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
571 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700572 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700573 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700574 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800575
Florin Coras6e3c1f82020-01-15 01:30:46 +0000576 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700577 {
Florin Corasc547e912020-12-08 17:50:45 -0800578 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800579 mp->tx_fifo, mp->vpp_evt_q, 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800580 {
581 VDBG (0, "failed to attach fifos for %u", session->session_index);
582 session->session_state = VCL_STATE_DETACHED;
583 return VCL_INVALID_SESSION_INDEX;
584 }
Florin Coras60116992018-08-27 09:52:18 -0700585 }
586
Florin Coras05ecfcc2018-12-12 18:19:39 -0800587 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700588 return sid;
589}
590
Florin Corasdfae9f92019-02-20 19:48:31 -0800591static void
592vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
593{
594 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
595 vcl_session_t *s;
596
597 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000598 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800599 {
600 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
601 return;
602 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700603 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000604 {
605 /* Connected udp listener */
606 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700607 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000608 return;
609
610 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
611 return;
612 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800613
614 if (mp->retval)
615 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700616 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800617
618 if (mp->context != wrk->wrk_index)
619 VDBG (0, "wrong context");
620
621 vcl_session_table_del_vpp_handle (wrk, mp->handle);
622 vcl_session_free (wrk, s);
623}
624
Florin Coras68b7e582020-01-21 18:33:23 -0800625static void
626vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
627{
628 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
629 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800630 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800631
632 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
633 if (!s)
634 {
635 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
636 return;
637 }
638
Florin Corasc547e912020-12-08 17:50:45 -0800639 fs_index = vcl_segment_table_lookup (mp->segment_handle);
640 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
641 {
642 VDBG (0, "segment for session %u is not mounted!", s->session_index);
643 s->session_state = VCL_STATE_DETACHED;
644 return;
645 }
646
Florin Coras57660d92020-04-04 22:45:34 +0000647 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800648
649 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
650 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800651
Florin Coras68b7e582020-01-21 18:33:23 -0800652 vcl_session_table_del_vpp_handle (wrk, mp->handle);
653 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
654
655 /* Generate new tx event if we have outstanding data */
656 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800657 app_send_io_evt_to_vpp (s->vpp_evt_q,
658 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800659 SESSION_IO_EVT_TX, SVM_Q_WAIT);
660
Florin Coras57660d92020-04-04 22:45:34 +0000661 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800662 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800663}
664
Florin Coras3c7d4f92018-12-14 11:28:43 -0800665static vcl_session_t *
666vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
667{
668 vcl_session_msg_t *vcl_msg;
669 vcl_session_t *session;
670
671 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
672 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800673 VWRN ("session overlap handle %lu state %u!", msg->handle,
674 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800675
676 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
677 if (!session)
678 {
679 VERR ("couldn't find listen session: listener handle %llx",
680 msg->listener_handle);
681 return 0;
682 }
683
684 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000685 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800686 vcl_msg->accepted_msg = *msg;
687 /* Session handle points to listener until fully accepted by app */
688 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
689
690 return session;
691}
692
693static vcl_session_t *
694vcl_session_disconnected_handler (vcl_worker_t * wrk,
695 session_disconnected_msg_t * msg)
696{
697 vcl_session_t *session;
698
699 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
700 if (!session)
701 {
702 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
703 return 0;
704 }
705
Florin Corasadcfb152020-02-12 08:50:29 +0000706 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700707 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000708 return 0;
709
Florin Coras3c7d4f92018-12-14 11:28:43 -0800710 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700711 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800712 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800713 if (!vcl_flag_accepted_session (session, msg->handle,
714 VCL_ACCEPTED_F_CLOSED))
715 VDBG (0, "session was not accepted!");
716 return 0;
717 }
718
Florin Corasadcfb152020-02-12 08:50:29 +0000719 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700720 if (session->session_state != VCL_STATE_DISCONNECT)
721 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000722
Florin Coras3c7d4f92018-12-14 11:28:43 -0800723 return session;
724}
725
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700726static int
727vppcom_session_disconnect (u32 session_handle)
728{
729 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700730 vcl_session_t *session, *listen_session;
731 vcl_session_state_t state;
732 u64 vpp_handle;
733
734 session = vcl_session_get_w_handle (wrk, session_handle);
735 if (!session)
736 return VPPCOM_EBADFD;
737
738 vpp_handle = session->vpp_handle;
739 state = session->session_state;
740
741 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
742 vpp_handle, state, vppcom_session_state_str (state));
743
744 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
745 {
746 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
747 return VPPCOM_EBADFD;
748 }
749
750 if (state == VCL_STATE_VPP_CLOSING)
751 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800752 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700753 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
754 session->session_index, vpp_handle);
755 }
756 else
757 {
758 /* Session doesn't have an event queue yet. Probably a non-blocking
759 * connect. Wait for the reply */
760 if (PREDICT_FALSE (!session->vpp_evt_q))
761 return VPPCOM_OK;
762
763 VDBG (1, "session %u [0x%llx]: sending disconnect...",
764 session->session_index, vpp_handle);
765 vcl_send_session_disconnect (wrk, session);
766 }
767
768 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
769 {
770 listen_session = vcl_session_get (wrk, session->listener_index);
771 listen_session->n_accepted_sessions--;
772 }
773
774 return VPPCOM_OK;
775}
776
Florin Coras30e79c22019-01-02 19:31:22 -0800777static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700778vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
779{
780 session_cleanup_msg_t *msg;
781 vcl_session_t *session;
782
783 msg = (session_cleanup_msg_t *) data;
784 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
785 if (!session)
786 {
787 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
788 return;
789 }
790
Florin Coras36d49392020-04-24 23:00:11 +0000791 if (msg->type == SESSION_CLEANUP_TRANSPORT)
792 {
793 /* Transport was cleaned up before we confirmed close. Probably the
794 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700795 * Confirm close to make sure everything is cleaned up.
796 * Move to undetermined state to ensure that the session is not
797 * removed before both vpp and the app cleanup.
798 * - If the app closes first, the session is moved to CLOSED state
799 * and the session cleanup notification from vpp removes the
800 * session.
801 * - If vpp cleans up the session first, the session is moved to
802 * DETACHED state lower and subsequently the close from the app
803 * frees the session
804 */
805 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700806 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700807 vppcom_session_disconnect (vcl_session_handle (session));
808 session->session_state = VCL_STATE_UPDATED;
809 }
810 else if (session->session_state == VCL_STATE_DISCONNECT)
811 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800812 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700813 session->session_state = VCL_STATE_UPDATED;
814 }
Florin Coras36d49392020-04-24 23:00:11 +0000815 return;
816 }
817
Florin Coras9ace36d2019-10-28 13:14:17 -0700818 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000819 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700820 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000821 {
822 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700823 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000824 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
825 return;
826 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700827 vcl_session_free (wrk, session);
828}
829
830static void
Florin Coras30e79c22019-01-02 19:31:22 -0800831vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
832{
833 session_req_worker_update_msg_t *msg;
834 vcl_session_t *s;
835
836 msg = (session_req_worker_update_msg_t *) data;
837 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
838 if (!s)
839 return;
840
841 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
842}
843
844static void
845vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
846{
847 session_worker_update_reply_msg_t *msg;
848 vcl_session_t *s;
849
850 msg = (session_worker_update_reply_msg_t *) data;
851 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
852 if (!s)
853 {
854 VDBG (0, "unknown handle 0x%llx", msg->handle);
855 return;
856 }
Florin Coras30e79c22019-01-02 19:31:22 -0800857
Florin Coras5f45e012019-01-23 09:21:30 -0800858 if (s->rx_fifo)
859 {
Florin Corasc547e912020-12-08 17:50:45 -0800860 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800861 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800862 {
863 VDBG (0, "failed to attach fifos for %u", s->session_index);
864 return;
865 }
Florin Coras5f45e012019-01-23 09:21:30 -0800866 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700867 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800868
Florin Coras30e79c22019-01-02 19:31:22 -0800869 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
870 s->vpp_handle, wrk->wrk_index);
871}
872
Florin Coras935ce752020-09-08 22:43:47 -0700873static int
874vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
875{
876
877 if (vcm->cfg.vpp_app_socket_api)
878 return vcl_sapi_recv_fds (wrk, fds, n_fds);
879
880 return vcl_bapi_recv_fds (wrk, fds, n_fds);
881}
882
Florin Corasc4c4cf52019-08-24 18:17:34 -0700883static void
884vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
885{
886 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
887 session_app_add_segment_msg_t *msg;
888 u64 segment_handle;
889 int fd = -1;
890
891 msg = (session_app_add_segment_msg_t *) data;
892
893 if (msg->fd_flags)
894 {
Florin Coras935ce752020-09-08 22:43:47 -0700895 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700896 seg_type = SSVM_SEGMENT_MEMFD;
897 }
898
899 segment_handle = msg->segment_handle;
900 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
901 {
902 clib_warning ("invalid segment handle");
903 return;
904 }
905
906 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
907 seg_type, fd))
908 {
909 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
910 return;
911 }
912
913 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
914 msg->segment_size);
915}
916
917static void
918vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
919{
920 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
921 vcl_segment_detach (msg->segment_handle);
922 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
923}
924
Florin Coras40c07ce2020-07-16 20:46:17 -0700925static void
926vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
927{
928 if (!vcm->wrk_rpc_fn)
929 return;
930
hanlina3a48962020-07-13 11:09:15 +0800931 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700932}
933
Florin Coras86f04502018-09-12 16:08:01 -0700934static int
935vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700936{
Florin Coras54693d22018-07-17 10:46:29 -0700937 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700938 session_connected_msg_t *connected_msg;
939 session_reset_msg_t *reset_msg;
940 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700941 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700942 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700943
944 switch (e->event_type)
945 {
Florin Coras653e43f2019-03-04 10:56:23 -0800946 case SESSION_IO_EVT_RX:
947 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700948 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -0700949 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -0800950 break;
Florin Coras86f04502018-09-12 16:08:01 -0700951 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700952 break;
Florin Corasb242d312020-10-26 15:35:40 -0700953 case SESSION_CTRL_EVT_BOUND:
954 /* We can only wait for only one listen so not postponed */
955 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
956 break;
Florin Coras54693d22018-07-17 10:46:29 -0700957 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -0700958 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
959 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
960 {
961 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
962 *ecpy = *e;
963 ecpy->postponed = 1;
964 ecpy->session_index = s->session_index;
965 }
Florin Coras54693d22018-07-17 10:46:29 -0700966 break;
967 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -0700968 connected_msg = (session_connected_msg_t *) e->data;
969 sid = vcl_session_connected_handler (wrk, connected_msg);
970 if (!(s = vcl_session_get (wrk, sid)))
971 break;
972 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
973 {
974 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
975 *ecpy = *e;
976 ecpy->postponed = 1;
977 ecpy->session_index = s->session_index;
978 }
Florin Coras54693d22018-07-17 10:46:29 -0700979 break;
980 case SESSION_CTRL_EVT_DISCONNECTED:
981 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -0700982 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
983 break;
984 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
985 {
986 vec_add1 (wrk->unhandled_evts_vector, *e);
987 break;
988 }
989 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -0800990 break;
Florin Corasc127d5a2020-10-14 16:35:58 -0700991 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
992 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700993 break;
994 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -0700995 reset_msg = (session_reset_msg_t *) e->data;
996 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
997 break;
998 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
999 {
1000 vec_add1 (wrk->unhandled_evts_vector, *e);
1001 break;
1002 }
Florin Coras134a9962018-08-28 11:32:04 -07001003 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001004 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001005 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1006 vcl_session_unlisten_reply_handler (wrk, e->data);
1007 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001008 case SESSION_CTRL_EVT_MIGRATED:
1009 vcl_session_migrated_handler (wrk, e->data);
1010 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001011 case SESSION_CTRL_EVT_CLEANUP:
1012 vcl_session_cleanup_handler (wrk, e->data);
1013 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001014 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1015 vcl_session_req_worker_update_handler (wrk, e->data);
1016 break;
1017 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1018 vcl_session_worker_update_reply_handler (wrk, e->data);
1019 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001020 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1021 vcl_session_app_add_segment_handler (wrk, e->data);
1022 break;
1023 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1024 vcl_session_app_del_segment_handler (wrk, e->data);
1025 break;
hanlina3a48962020-07-13 11:09:15 +08001026 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001027 vcl_worker_rpc_handler (wrk, e->data);
1028 break;
Florin Coras54693d22018-07-17 10:46:29 -07001029 default:
1030 clib_warning ("unhandled %u", e->event_type);
1031 }
1032 return VPPCOM_OK;
1033}
1034
Florin Coras30e79c22019-01-02 19:31:22 -08001035static int
Florin Coras697faea2018-06-27 17:10:49 -07001036vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001037 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001038 f64 wait_for_time)
1039{
Florin Coras134a9962018-08-28 11:32:04 -07001040 vcl_worker_t *wrk = vcl_worker_get_current ();
1041 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001042 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001043 svm_msg_q_msg_t msg;
1044 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001045
Florin Coras697faea2018-06-27 17:10:49 -07001046 do
Dave Wallace543852a2017-08-03 02:11:34 -04001047 {
Florin Coras134a9962018-08-28 11:32:04 -07001048 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001049 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001050 {
Florin Coras070453d2018-08-24 17:04:27 -07001051 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001052 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001053 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001054 {
Florin Coras697faea2018-06-27 17:10:49 -07001055 return VPPCOM_OK;
1056 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001057 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001058 {
Florin Coras697faea2018-06-27 17:10:49 -07001059 return VPPCOM_ECONNREFUSED;
1060 }
Florin Coras54693d22018-07-17 10:46:29 -07001061
Florin Coras134a9962018-08-28 11:32:04 -07001062 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001063 {
1064 usleep (100);
1065 continue;
1066 }
Florin Coras134a9962018-08-28 11:32:04 -07001067 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001068 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001069 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001070 }
Florin Coras134a9962018-08-28 11:32:04 -07001071 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001072
Florin Coras05ecfcc2018-12-12 18:19:39 -08001073 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001074 vppcom_session_state_str (state));
1075 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001076
Florin Coras697faea2018-06-27 17:10:49 -07001077 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001078}
1079
Florin Coras30e79c22019-01-02 19:31:22 -08001080static void
1081vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1082{
Florin Coras288eaab2019-02-03 15:26:14 -08001083 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001084 vcl_session_t *s;
1085 u32 *sip;
1086
1087 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1088 return;
1089
1090 vec_foreach (sip, wrk->pending_session_wrk_updates)
1091 {
1092 s = vcl_session_get (wrk, *sip);
1093 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1094 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001095 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1096 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001097 s->session_state = state;
1098 }
1099 vec_reset_length (wrk->pending_session_wrk_updates);
1100}
1101
Florin Corasf9240dc2019-01-15 08:03:17 -08001102void
Florin Coras5398dfb2021-01-25 20:31:27 -08001103vcl_worker_flush_mq_events (vcl_worker_t *wrk)
Florin Coras30e79c22019-01-02 19:31:22 -08001104{
Florin Coras30e79c22019-01-02 19:31:22 -08001105 svm_msg_q_msg_t *msg;
1106 session_event_t *e;
1107 svm_msg_q_t *mq;
1108 int i;
1109
1110 mq = wrk->app_event_queue;
Florin Corase003a1b2019-06-05 10:47:16 -07001111 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001112
1113 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1114 {
1115 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1116 e = svm_msg_q_msg_data (mq, msg);
1117 vcl_handle_mq_event (wrk, e);
1118 svm_msg_q_free_msg (mq, msg);
1119 }
1120 vec_reset_length (wrk->mq_msg_vector);
1121 vcl_handle_pending_wrk_updates (wrk);
1122}
1123
Florin Coras5398dfb2021-01-25 20:31:27 -08001124void
1125vcl_flush_mq_events (void)
1126{
1127 vcl_worker_flush_mq_events (vcl_worker_get_current ());
1128}
1129
Florin Coras697faea2018-06-27 17:10:49 -07001130static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001131vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001132{
Florin Coras134a9962018-08-28 11:32:04 -07001133 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001134 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001135 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001136 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001137
Florin Corasab2f6db2018-08-31 14:31:41 -07001138 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001139 if (!session)
1140 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001141
Florin Corasaaff5ee2019-07-08 13:00:00 -07001142 /* Flush pending accept events, if any */
1143 while (clib_fifo_elts (session->accept_evts_fifo))
1144 {
1145 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1146 accepted_msg = &evt->accepted_msg;
1147 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1148 vcl_send_session_accepted_reply (session->vpp_evt_q,
1149 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001150 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001151 }
1152 clib_fifo_free (session->accept_evts_fifo);
1153
Florin Coras458089b2019-08-21 16:20:44 -07001154 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001155
Florin Coras5e062572019-03-14 19:07:51 -07001156 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001157 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001158 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001159
1160 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001161 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001162
Florin Coras070453d2018-08-24 17:04:27 -07001163 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001164}
1165
Florin Coras940f78f2018-11-30 12:11:20 -08001166/**
1167 * Handle app exit
1168 *
1169 * Notify vpp of the disconnect and mark the worker as free. If we're the
1170 * last worker, do a full cleanup otherwise, since we're probably a forked
1171 * child, avoid syscalls as much as possible. We might've lost privileges.
1172 */
1173void
1174vppcom_app_exit (void)
1175{
1176 if (!pool_elts (vcm->workers))
1177 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001178 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1179 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001180 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001181}
1182
Florin Coras935ce752020-09-08 22:43:47 -07001183static int
1184vcl_api_attach (void)
1185{
1186 if (vcm->cfg.vpp_app_socket_api)
1187 return vcl_sapi_attach ();
1188
1189 return vcl_bapi_attach ();
1190}
1191
1192static void
1193vcl_api_detach (vcl_worker_t * wrk)
1194{
1195 vcl_send_app_detach (wrk);
1196
1197 if (vcm->cfg.vpp_app_socket_api)
1198 return vcl_sapi_detach (wrk);
1199
1200 return vcl_bapi_disconnect_from_vpp ();
1201}
1202
Dave Wallace543852a2017-08-03 02:11:34 -04001203/*
1204 * VPPCOM Public API functions
1205 */
1206int
Florin Coras66ec4672020-06-15 07:59:40 -07001207vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001208{
Dave Wallace543852a2017-08-03 02:11:34 -04001209 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001210 int rv;
1211
Florin Coras47c40e22018-11-26 17:01:36 -08001212 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001213 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001214 VDBG (1, "already initialized");
1215 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001216 }
1217
Florin Coras47c40e22018-11-26 17:01:36 -08001218 vcm->is_init = 1;
1219 vppcom_cfg (&vcm->cfg);
1220 vcl_cfg = &vcm->cfg;
1221
1222 vcm->main_cpu = pthread_self ();
1223 vcm->main_pid = getpid ();
1224 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001225 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1226 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001227 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1228 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001229 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001230 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001231 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001232
1233 /* Allocate default worker */
1234 vcl_worker_alloc_and_init ();
1235
Florin Coras935ce752020-09-08 22:43:47 -07001236 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001237 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001238
1239 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001240 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001241
1242 return VPPCOM_OK;
1243}
1244
1245void
1246vppcom_app_destroy (void)
1247{
Florin Corasdc0ded72020-04-30 02:59:55 +00001248 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001249 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001250
Florin Coras940f78f2018-11-30 12:11:20 -08001251 if (!pool_elts (vcm->workers))
1252 return;
1253
Florin Coras0d427d82018-06-27 03:24:07 -07001254 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001255
Florin Corasdc0ded72020-04-30 02:59:55 +00001256 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001257
Florin Corasce815de2020-04-16 18:47:27 +00001258 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001259 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001260 if (current_wrk != wrk)
1261 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001262 }
Florin Corasce815de2020-04-16 18:47:27 +00001263 /* *INDENT-ON* */
1264
Florin Coras935ce752020-09-08 22:43:47 -07001265 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001266 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1267
Florin Coras0d427d82018-06-27 03:24:07 -07001268 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001269
1270 /*
1271 * Free the heap and fix vcm
1272 */
1273 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001274 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001275
1276 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001277 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001278}
1279
1280int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001281vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001282{
Florin Coras134a9962018-08-28 11:32:04 -07001283 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001284 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001285
Florin Coras134a9962018-08-28 11:32:04 -07001286 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001287
Florin Coras7e12d942018-06-27 14:32:43 -07001288 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001289 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001290 session->vpp_handle = ~0;
Florin Corasa5a9efd2021-01-05 17:03:29 -08001291 session->ckpair_index = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001292 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001293
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001294 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001295 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001296
Florin Coras7e12d942018-06-27 14:32:43 -07001297 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1298 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001299
Florin Coras5e062572019-03-14 19:07:51 -07001300 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001301
Florin Coras134a9962018-08-28 11:32:04 -07001302 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001303}
1304
1305int
Florin Corasc127d5a2020-10-14 16:35:58 -07001306vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001307 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001308{
Florin Coras134a9962018-08-28 11:32:04 -07001309 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001310
Florin Coras6c3b2182020-10-19 18:36:48 -07001311 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001312
Florin Coras6c3b2182020-10-19 18:36:48 -07001313 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001314 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001315 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001316 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001317 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001318 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001319 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001320 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001321 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1322 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001323 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001324 }
Florin Corase4306b62020-10-28 12:51:10 -07001325 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001326 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001327
Florin Coras6c3b2182020-10-19 18:36:48 -07001328 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001329 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001330 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001331 if (rv < 0)
1332 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001333 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1334 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001335 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001336
Florin Coras9ace36d2019-10-28 13:14:17 -07001337 if (!do_disconnect)
1338 {
1339 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001340 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001341 goto cleanup;
1342 }
1343
Florin Coras6c3b2182020-10-19 18:36:48 -07001344 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001345 {
1346 rv = vppcom_session_unbind (sh);
1347 if (PREDICT_FALSE (rv < 0))
1348 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001349 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001350 vppcom_retval_str (rv));
1351 return rv;
1352 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001353 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001354 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001355 {
1356 rv = vppcom_session_disconnect (sh);
1357 if (PREDICT_FALSE (rv < 0))
1358 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001359 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001360 rv, vppcom_retval_str (rv));
1361 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001362 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001363 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001364 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001365 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001366 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001367 {
1368 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001369 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001370 goto free_session;
1371 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001372
Florin Corasc127d5a2020-10-14 16:35:58 -07001373 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001374
Florin Coras9ace36d2019-10-28 13:14:17 -07001375 /* Session is removed only after vpp confirms the disconnect */
1376 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001377
Florin Corasf9240dc2019-01-15 08:03:17 -08001378cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001379 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001380free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001381 vcl_session_free (wrk, s);
1382 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001383
Dave Wallace543852a2017-08-03 02:11:34 -04001384 return rv;
1385}
1386
1387int
Florin Corasf9240dc2019-01-15 08:03:17 -08001388vppcom_session_close (uint32_t session_handle)
1389{
1390 vcl_worker_t *wrk = vcl_worker_get_current ();
1391 vcl_session_t *session;
1392
1393 session = vcl_session_get_w_handle (wrk, session_handle);
1394 if (!session)
1395 return VPPCOM_EBADFD;
1396 return vcl_session_cleanup (wrk, session, session_handle,
1397 1 /* do_disconnect */ );
1398}
1399
1400int
Florin Coras134a9962018-08-28 11:32:04 -07001401vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001402{
Florin Coras134a9962018-08-28 11:32:04 -07001403 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001404 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001405
1406 if (!ep || !ep->ip)
1407 return VPPCOM_EINVAL;
1408
Florin Coras134a9962018-08-28 11:32:04 -07001409 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001410 if (!session)
1411 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001412
Florin Coras6c3b2182020-10-19 18:36:48 -07001413 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001414 {
Florin Coras5e062572019-03-14 19:07:51 -07001415 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1416 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001417 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001418 }
1419
Florin Coras7e12d942018-06-27 14:32:43 -07001420 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001421 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001422 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1423 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001424 else
Dave Barach178cf492018-11-13 16:34:13 -05001425 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1426 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001427 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001428
Florin Coras5e062572019-03-14 19:07:51 -07001429 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1430 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001431 session->transport.is_ip4 ? "IPv4" : "IPv6",
1432 format_ip46_address, &session->transport.lcl_ip,
1433 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1434 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001435 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001436 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001437
1438 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001439 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001440
Florin Coras070453d2018-08-24 17:04:27 -07001441 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001442}
1443
1444int
Florin Coras134a9962018-08-28 11:32:04 -07001445vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001446{
Florin Coras134a9962018-08-28 11:32:04 -07001447 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001448 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001449 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001450 int rv;
1451
Florin Coras134a9962018-08-28 11:32:04 -07001452 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001453 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001454 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001455
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001456 if (q_len == 0 || q_len == ~0)
1457 q_len = vcm->cfg.listen_queue_size;
1458
Dave Wallaceee45d412017-11-24 21:44:06 -05001459 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001460 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001461 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001462 VDBG (0, "session %u [0x%llx]: already in listen state!",
1463 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001464 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001465 }
1466
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001467 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001468
Florin Coras070453d2018-08-24 17:04:27 -07001469 /*
1470 * Send listen request to vpp and wait for reply
1471 */
Florin Coras458089b2019-08-21 16:20:44 -07001472 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001473 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001474 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001475 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001476
Florin Coras070453d2018-08-24 17:04:27 -07001477 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001478 {
Florin Coras134a9962018-08-28 11:32:04 -07001479 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001480 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1481 listen_sh, listen_session->vpp_handle, rv,
1482 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001483 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001484 }
1485
Florin Coras070453d2018-08-24 17:04:27 -07001486 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001487}
1488
Florin Coras134a9962018-08-28 11:32:04 -07001489static int
Florin Coras5e062572019-03-14 19:07:51 -07001490validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001491{
Florin Coras6c3b2182020-10-19 18:36:48 -07001492 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001493 {
Florin Coras5e062572019-03-14 19:07:51 -07001494 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1495 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001496 return VPPCOM_EBADFD;
1497 }
1498
Florin Corasc127d5a2020-10-14 16:35:58 -07001499 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001500 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001501 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001502 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001503 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001504 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001505 return VPPCOM_EBADFD;
1506 }
1507 return VPPCOM_OK;
1508}
1509
1510int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001511vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1512{
1513 if (!strcmp (proto_str, "TCP"))
1514 *proto = VPPCOM_PROTO_TCP;
1515 else if (!strcmp (proto_str, "tcp"))
1516 *proto = VPPCOM_PROTO_TCP;
1517 else if (!strcmp (proto_str, "UDP"))
1518 *proto = VPPCOM_PROTO_UDP;
1519 else if (!strcmp (proto_str, "udp"))
1520 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001521 else if (!strcmp (proto_str, "TLS"))
1522 *proto = VPPCOM_PROTO_TLS;
1523 else if (!strcmp (proto_str, "tls"))
1524 *proto = VPPCOM_PROTO_TLS;
1525 else if (!strcmp (proto_str, "QUIC"))
1526 *proto = VPPCOM_PROTO_QUIC;
1527 else if (!strcmp (proto_str, "quic"))
1528 *proto = VPPCOM_PROTO_QUIC;
1529 else
1530 return 1;
1531 return 0;
1532}
1533
1534int
Florin Coras134a9962018-08-28 11:32:04 -07001535vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001536 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001537{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001538 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001539 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001540 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001541 vcl_session_t *listen_session = 0;
1542 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001543 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001544 u8 is_nonblocking;
1545 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001546
Florin Coras5398dfb2021-01-25 20:31:27 -08001547again:
1548
Florin Coras134a9962018-08-28 11:32:04 -07001549 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001550 if (!listen_session)
1551 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001552
Florin Coras134a9962018-08-28 11:32:04 -07001553 listen_session_index = listen_session->session_index;
1554 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001555 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001556
Florin Coras54693d22018-07-17 10:46:29 -07001557 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001558 {
Florin Coras54693d22018-07-17 10:46:29 -07001559 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001560 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001561 accepted_msg = evt->accepted_msg;
1562 goto handle;
1563 }
1564
Florin Corasac422d62020-10-19 20:51:36 -07001565 is_nonblocking = vcl_session_has_attr (listen_session,
1566 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001567 while (1)
1568 {
Carl Smith592a9092019-11-12 14:57:37 +13001569 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1570 return VPPCOM_EAGAIN;
1571
Florin Coras5398dfb2021-01-25 20:31:27 -08001572 svm_msg_q_wait (wrk->app_event_queue, SVM_MQ_WAIT_EMPTY);
1573 vcl_worker_flush_mq_events (wrk);
1574 goto again;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001575 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001576
Florin Coras54693d22018-07-17 10:46:29 -07001577handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001578
Florin Coras00cca802019-06-06 09:38:44 -07001579 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1580 listen_session_index);
1581 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1582 return VPPCOM_ECONNABORTED;
1583
Florin Coras134a9962018-08-28 11:32:04 -07001584 listen_session = vcl_session_get (wrk, listen_session_index);
1585 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001586
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001587 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001588 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001589
Florin Coras5e062572019-03-14 19:07:51 -07001590 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1591 " flags %d, is_nonblocking %u", listen_session->session_index,
1592 listen_session->vpp_handle, client_session_index,
1593 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001594 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001595
Dave Wallace048b1d62018-01-03 22:24:41 -05001596 if (ep)
1597 {
Florin Coras7e12d942018-06-27 14:32:43 -07001598 ep->is_ip4 = client_session->transport.is_ip4;
1599 ep->port = client_session->transport.rmt_port;
1600 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001601 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1602 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001603 else
Dave Barach178cf492018-11-13 16:34:13 -05001604 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1605 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001606 }
Dave Wallace60caa062017-11-10 17:07:13 -05001607
Florin Coras05ecfcc2018-12-12 18:19:39 -08001608 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001609 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001610 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001611 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001612 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001613 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001614 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001615 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001616 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001617 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1618 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001619
Florin Coras3c7d4f92018-12-14 11:28:43 -08001620 /*
1621 * Session might have been closed already
1622 */
1623 if (accept_flags)
1624 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001625 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001626 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001627 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001628 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001629 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001630 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001631}
1632
1633int
Florin Coras134a9962018-08-28 11:32:04 -07001634vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001635{
Florin Coras134a9962018-08-28 11:32:04 -07001636 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001637 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001638 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001639 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001640
Florin Coras134a9962018-08-28 11:32:04 -07001641 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001642 if (!session)
1643 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001644 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001645
Florin Coras6c3b2182020-10-19 18:36:48 -07001646 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001647 {
Florin Coras5e062572019-03-14 19:07:51 -07001648 VDBG (0, "ERROR: cannot connect epoll session %u!",
1649 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001650 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001651 }
1652
Florin Corasc127d5a2020-10-14 16:35:58 -07001653 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001654 {
Florin Coras7baeb712019-01-04 17:05:43 -08001655 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001656 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001657 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001658 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001659 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001660 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001661 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001662 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001663 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001664 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001665 }
1666
Florin Coras0a1e1832020-03-29 18:54:04 +00001667 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001668 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001669 {
1670 if (session->session_type != VPPCOM_PROTO_UDP)
1671 return VPPCOM_EINVAL;
1672 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001673 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001674 }
1675
Florin Coras7e12d942018-06-27 14:32:43 -07001676 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001677 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001678 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001679 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001680 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001681
Florin Coras0a1e1832020-03-29 18:54:04 +00001682 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001683 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001684 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001685 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001686 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001687 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001688 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001689 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001690 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001691
Florin Coras458089b2019-08-21 16:20:44 -07001692 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001693
Florin Corasac422d62020-10-19 20:51:36 -07001694 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001695 {
fanyfa49d59d2020-10-13 17:07:16 +08001696 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001697 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001698 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001699 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001700 return VPPCOM_EINPROGRESS;
1701 }
Florin Coras57c88932019-08-29 12:03:17 -07001702
1703 /*
1704 * Wait for reply from vpp if blocking
1705 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001706 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001707 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001708
Florin Coras134a9962018-08-28 11:32:04 -07001709 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001710 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1711 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001712
Dave Wallace4878cbe2017-11-21 03:45:09 -05001713 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001714}
1715
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001716int
1717vppcom_session_stream_connect (uint32_t session_handle,
1718 uint32_t parent_session_handle)
1719{
1720 vcl_worker_t *wrk = vcl_worker_get_current ();
1721 vcl_session_t *session, *parent_session;
1722 u32 session_index, parent_session_index;
1723 int rv;
1724
1725 session = vcl_session_get_w_handle (wrk, session_handle);
1726 if (!session)
1727 return VPPCOM_EBADFD;
1728 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1729 if (!parent_session)
1730 return VPPCOM_EBADFD;
1731
1732 session_index = session->session_index;
1733 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001734 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001735 {
1736 VDBG (0, "ERROR: cannot connect epoll session %u!",
1737 session->session_index);
1738 return VPPCOM_EBADFD;
1739 }
1740
Florin Corasc127d5a2020-10-14 16:35:58 -07001741 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001742 {
1743 VDBG (0, "session handle %u [0x%llx]: session already "
1744 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1745 session_handle, session->vpp_handle,
1746 parent_session_handle, parent_session->vpp_handle,
1747 vppcom_proto_str (session->session_type), session->session_state,
1748 vppcom_session_state_str (session->session_state));
1749 return VPPCOM_OK;
1750 }
1751
1752 /* Connect to quic session specifics */
1753 session->transport.is_ip4 = parent_session->transport.is_ip4;
1754 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1755 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001756 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001757
1758 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1759 session_handle, parent_session_handle, parent_session->vpp_handle);
1760
1761 /*
1762 * Send connect request and wait for reply from vpp
1763 */
Florin Coras458089b2019-08-21 16:20:44 -07001764 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001765 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001766 vcm->cfg.session_timeout);
1767
1768 session->listener_index = parent_session_index;
1769 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001770 if (parent_session)
1771 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001772
1773 session = vcl_session_get (wrk, session_index);
1774 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1775 session->vpp_handle, rv ? "failed" : "succeeded");
1776
1777 return rv;
1778}
1779
Steven58f464e2017-10-25 12:33:12 -07001780static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001781vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001782 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001783{
Florin Coras134a9962018-08-28 11:32:04 -07001784 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001785 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001786 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001787 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001788 session_event_t *e;
1789 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001790 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001791
Florin Coras070453d2018-08-24 17:04:27 -07001792 if (PREDICT_FALSE (!buf))
1793 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001794
Florin Coras134a9962018-08-28 11:32:04 -07001795 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001796 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001797 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001798
Florin Coras6d0106e2019-01-29 20:11:58 -08001799 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001800 {
Florin Coras5e062572019-03-14 19:07:51 -07001801 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001802 s->session_index, s->vpp_handle, s->session_state,
1803 vppcom_session_state_str (s->session_state));
1804 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001805 }
1806
Florin Corasac422d62020-10-19 20:51:36 -07001807 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001808 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001809 mq = wrk->app_event_queue;
1810 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001811 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001812
Sirshak Das28aa5392019-02-05 01:33:33 -06001813 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001814 {
Florin Coras54693d22018-07-17 10:46:29 -07001815 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001816 {
Yu Pingb2955352019-12-04 06:49:04 +08001817 if (vcl_session_is_closing (s))
1818 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001819 if (is_ct)
1820 svm_fifo_unset_event (s->rx_fifo);
1821 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001822 return VPPCOM_EWOULDBLOCK;
1823 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001824 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001825 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001826 if (vcl_session_is_closing (s))
1827 return vcl_session_closing_error (s);
1828
Florin Corasd6894562020-10-28 00:37:15 -07001829 if (is_ct)
1830 svm_fifo_unset_event (s->rx_fifo);
1831 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001832
Florin Coras5398dfb2021-01-25 20:31:27 -08001833 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1834 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001835 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001836 }
Florin Coras54693d22018-07-17 10:46:29 -07001837
Florin Coras4a2c7942020-09-10 12:27:14 -07001838read_again:
1839
Florin Coras460dce62018-07-27 05:45:06 -07001840 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001841 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001842 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001843 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1844
1845 ASSERT (rv >= 0);
1846 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001847
Sirshak Das28aa5392019-02-05 01:33:33 -06001848 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001849 {
Florin Corasd6894562020-10-28 00:37:15 -07001850 if (is_ct)
1851 svm_fifo_unset_event (s->rx_fifo);
1852 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001853 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001854 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001855 {
Florin Corasc34118b2020-08-12 18:58:25 -07001856 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1857 e->event_type = SESSION_IO_EVT_RX;
1858 e->session_index = s->session_index;
1859 }
1860 }
Florin Coras54fe32c2020-11-25 20:26:32 -08001861 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07001862 {
1863 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08001864 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07001865 buf += rv;
1866 n -= rv;
1867 goto read_again;
1868 }
Florin Coras41c9e042018-09-11 00:10:41 -07001869
Florin Coras17ec5772020-08-12 20:46:29 -07001870 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001871 {
Florin Coras17ec5772020-08-12 20:46:29 -07001872 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08001873 app_send_io_evt_to_vpp (s->vpp_evt_q,
1874 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07001875 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001876 }
1877
Florin Coras5e062572019-03-14 19:07:51 -07001878 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1879 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001880
Florin Coras54693d22018-07-17 10:46:29 -07001881 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001882}
1883
Steven58f464e2017-10-25 12:33:12 -07001884int
Florin Coras134a9962018-08-28 11:32:04 -07001885vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001886{
Florin Coras134a9962018-08-28 11:32:04 -07001887 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001888}
1889
1890static int
Florin Coras134a9962018-08-28 11:32:04 -07001891vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001892{
Florin Coras134a9962018-08-28 11:32:04 -07001893 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001894}
1895
Florin Coras2cba8532018-09-11 16:33:36 -07001896int
1897vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001898 vppcom_data_segment_t * ds, uint32_t n_segments,
1899 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001900{
1901 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001902 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001903 vcl_session_t *s = 0;
1904 svm_fifo_t *rx_fifo;
Florin Coras2cba8532018-09-11 16:33:36 -07001905 svm_msg_q_t *mq;
1906 u8 is_ct;
1907
1908 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001909 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001910 return VPPCOM_EBADFD;
1911
Florin Coras6d0106e2019-01-29 20:11:58 -08001912 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1913 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001914
Florin Corasac422d62020-10-19 20:51:36 -07001915 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07001916 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07001917 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07001918 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001919 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07001920
Sirshak Das28aa5392019-02-05 01:33:33 -06001921 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001922 {
1923 if (is_nonblocking)
1924 {
Florin Coras62877022020-10-28 21:22:04 -07001925 if (is_ct)
1926 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001927 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001928 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001929 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001930 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001931 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001932 if (vcl_session_is_closing (s))
1933 return vcl_session_closing_error (s);
1934
Florin Coras62877022020-10-28 21:22:04 -07001935 if (is_ct)
1936 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001937 svm_fifo_unset_event (rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001938
Florin Coras5398dfb2021-01-25 20:31:27 -08001939 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
1940 vcl_worker_flush_mq_events (wrk);
Florin Coras2cba8532018-09-11 16:33:36 -07001941 }
1942 }
1943
Florin Corasd1cc38d2020-10-11 11:05:04 -07001944 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
1945 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
1946 if (n_read < 0)
1947 return VPPCOM_EAGAIN;
1948
1949 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
1950 {
Florin Coras62877022020-10-28 21:22:04 -07001951 if (is_ct)
1952 svm_fifo_unset_event (s->rx_fifo);
1953 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07001954 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07001955 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07001956 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07001957 {
1958 session_event_t *e;
1959 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1960 e->event_type = SESSION_IO_EVT_RX;
1961 e->session_index = s->session_index;
1962 }
1963 }
1964
1965 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07001966 return n_read;
1967}
1968
1969void
Florin Corasd68faf82020-09-25 15:18:13 -07001970vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001971{
1972 vcl_worker_t *wrk = vcl_worker_get_current ();
1973 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07001974 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07001975
1976 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001977 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001978 return;
1979
Florin Coras62877022020-10-28 21:22:04 -07001980 is_ct = vcl_session_is_ct (s);
1981 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07001982
1983 ASSERT (s->rx_bytes_pending < n_bytes);
1984 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07001985}
1986
Florin Coras7a2abce2020-04-05 19:25:44 +00001987always_inline u8
1988vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04001989{
Florin Coras7a2abce2020-04-05 19:25:44 +00001990 u32 max_enq = svm_fifo_max_enqueue_prod (f);
1991 if (is_dgram)
1992 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
1993 else
1994 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00001995}
1996
1997always_inline int
1998vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
1999 size_t n, u8 is_flush, u8 is_dgram)
2000{
Florin Coras6d0106e2019-01-29 20:11:58 -08002001 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002002 session_evt_type_t et;
Florin Coras14ed6df2019-03-06 21:13:42 -08002003 svm_fifo_t *tx_fifo;
Florin Coras3c2fed52018-07-04 04:15:05 -07002004 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002005 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002006
jiangxiaomingff31ac62019-11-21 23:34:56 +08002007 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002008 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002009
Florin Coras6c3b2182020-10-19 18:36:48 -07002010 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002011 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002012 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2013 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002014 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002015 }
2016
Florin Coras6d0106e2019-01-29 20:11:58 -08002017 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002018 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002019 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2020 s->session_index, s->vpp_handle, s->session_state,
2021 vppcom_session_state_str (s->session_state));
2022 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002023 }
2024
Florin Coras0e88e852018-09-17 22:09:02 -07002025 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002026 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002027 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002028
Florin Coras653e43f2019-03-04 10:56:23 -08002029 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002030 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002031 {
Florin Coras54693d22018-07-17 10:46:29 -07002032 if (is_nonblocking)
2033 {
Florin Coras070453d2018-08-24 17:04:27 -07002034 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002035 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002036 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002037 {
Florin Coras2d379d82019-06-28 12:45:12 -07002038 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002039 if (vcl_session_is_closing (s))
2040 return vcl_session_closing_error (s);
Florin Coras0e88e852018-09-17 22:09:02 -07002041
Florin Coras5398dfb2021-01-25 20:31:27 -08002042 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
2043 vcl_worker_flush_mq_events (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002044 }
Dave Wallace543852a2017-08-03 02:11:34 -04002045 }
Dave Wallace543852a2017-08-03 02:11:34 -04002046
Florin Coras653e43f2019-03-04 10:56:23 -08002047 et = SESSION_IO_EVT_TX;
2048 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002049 et = SESSION_IO_EVT_TX_FLUSH;
2050
Florin Coras7a2abce2020-04-05 19:25:44 +00002051 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002052 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002053 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002054 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002055 else
2056 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002057 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002058
Florin Coras14ed6df2019-03-06 21:13:42 -08002059 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002060 app_send_io_evt_to_vpp (
2061 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002062
Florin Coras56230092020-09-02 20:52:58 -07002063 /* The underlying fifo segment can run out of memory */
2064 if (PREDICT_FALSE (n_write < 0))
2065 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002066
Florin Coras6d0106e2019-01-29 20:11:58 -08002067 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2068 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002069
Florin Coras54693d22018-07-17 10:46:29 -07002070 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002071}
2072
Florin Coras42ceddb2018-12-12 10:56:01 -08002073int
2074vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2075{
Florin Coras7a2abce2020-04-05 19:25:44 +00002076 vcl_worker_t *wrk = vcl_worker_get_current ();
2077 vcl_session_t *s;
2078
2079 s = vcl_session_get_w_handle (wrk, session_handle);
2080 if (PREDICT_FALSE (!s))
2081 return VPPCOM_EBADFD;
2082
2083 return vppcom_session_write_inline (wrk, s, buf, n,
2084 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002085}
2086
Florin Corasb0f662f2018-12-27 14:51:46 -08002087int
2088vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2089{
Florin Coras7a2abce2020-04-05 19:25:44 +00002090 vcl_worker_t *wrk = vcl_worker_get_current ();
2091 vcl_session_t *s;
2092
2093 s = vcl_session_get_w_handle (wrk, session_handle);
2094 if (PREDICT_FALSE (!s))
2095 return VPPCOM_EBADFD;
2096
2097 return vppcom_session_write_inline (wrk, s, buf, n,
2098 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002099}
2100
Florin Corasc0737e92019-03-04 14:19:39 -08002101#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002102if (PREDICT_FALSE (!_s->rx_fifo)) \
2103 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002104if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2105 { \
2106 if (!vcl_session_is_ct (_s)) \
2107 { \
2108 svm_fifo_unset_event (_s->rx_fifo); \
2109 if (svm_fifo_is_empty (_s->rx_fifo)) \
2110 break; \
2111 } \
2112 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2113 { \
Florin Coras2f630182020-08-13 00:17:51 -07002114 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002115 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2116 break; \
2117 } \
2118 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002119
Florin Coras86f04502018-09-12 16:08:01 -07002120static void
2121vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2122 unsigned long n_bits, unsigned long *read_map,
2123 unsigned long *write_map,
2124 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002125{
2126 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002127 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002128 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002129 u32 sid;
2130
2131 switch (e->event_type)
2132 {
Florin Corasc0737e92019-03-04 14:19:39 -08002133 case SESSION_IO_EVT_RX:
2134 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002135 s = vcl_session_get (wrk, sid);
2136 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002137 break;
Florin Corasb242d312020-10-26 15:35:40 -07002138 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002139 if (sid < n_bits && read_map)
2140 {
David Johnsond9818dd2018-12-14 14:53:41 -05002141 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002142 *bits_set += 1;
2143 }
2144 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002145 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002146 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002147 s = vcl_session_get (wrk, sid);
2148 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002149 break;
2150 if (sid < n_bits && write_map)
2151 {
David Johnsond9818dd2018-12-14 14:53:41 -05002152 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002153 *bits_set += 1;
2154 }
2155 break;
Florin Coras86f04502018-09-12 16:08:01 -07002156 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002157 if (!e->postponed)
2158 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2159 else
2160 s = vcl_session_get (wrk, e->session_index);
2161 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002162 break;
Florin Corasb242d312020-10-26 15:35:40 -07002163 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002164 if (sid < n_bits && read_map)
2165 {
David Johnsond9818dd2018-12-14 14:53:41 -05002166 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002167 *bits_set += 1;
2168 }
2169 break;
2170 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002171 if (!e->postponed)
2172 {
2173 connected_msg = (session_connected_msg_t *) e->data;
2174 sid = vcl_session_connected_handler (wrk, connected_msg);
2175 }
2176 else
2177 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002178 if (sid == VCL_INVALID_SESSION_INDEX)
2179 break;
2180 if (sid < n_bits && write_map)
2181 {
2182 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2183 *bits_set += 1;
2184 }
Florin Coras86f04502018-09-12 16:08:01 -07002185 break;
2186 case SESSION_CTRL_EVT_DISCONNECTED:
2187 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002188 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2189 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002190 break;
Florin Corasb242d312020-10-26 15:35:40 -07002191 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002192 if (sid < n_bits && except_map)
2193 {
David Johnsond9818dd2018-12-14 14:53:41 -05002194 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002195 *bits_set += 1;
2196 }
2197 break;
2198 case SESSION_CTRL_EVT_RESET:
2199 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2200 if (sid < n_bits && except_map)
2201 {
David Johnsond9818dd2018-12-14 14:53:41 -05002202 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002203 *bits_set += 1;
2204 }
2205 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002206 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2207 vcl_session_unlisten_reply_handler (wrk, e->data);
2208 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002209 case SESSION_CTRL_EVT_MIGRATED:
2210 vcl_session_migrated_handler (wrk, e->data);
2211 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002212 case SESSION_CTRL_EVT_CLEANUP:
2213 vcl_session_cleanup_handler (wrk, e->data);
2214 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002215 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2216 vcl_session_worker_update_reply_handler (wrk, e->data);
2217 break;
2218 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2219 vcl_session_req_worker_update_handler (wrk, e->data);
2220 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002221 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2222 vcl_session_app_add_segment_handler (wrk, e->data);
2223 break;
2224 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2225 vcl_session_app_del_segment_handler (wrk, e->data);
2226 break;
hanlina3a48962020-07-13 11:09:15 +08002227 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002228 vcl_worker_rpc_handler (wrk, e->data);
2229 break;
Florin Coras86f04502018-09-12 16:08:01 -07002230 default:
2231 clib_warning ("unhandled: %u", e->event_type);
2232 break;
2233 }
2234}
2235
2236static int
2237vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2238 unsigned long n_bits, unsigned long *read_map,
2239 unsigned long *write_map, unsigned long *except_map,
2240 double time_to_wait, u32 * bits_set)
2241{
Florin Coras99368312018-08-02 10:45:44 -07002242 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002243 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002244 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002245
Florin Coras54693d22018-07-17 10:46:29 -07002246 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002247 {
Florin Coras54693d22018-07-17 10:46:29 -07002248 if (*bits_set)
Florin Coras5398dfb2021-01-25 20:31:27 -08002249 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002250
Florin Coras54693d22018-07-17 10:46:29 -07002251 if (!time_to_wait)
Florin Coras5398dfb2021-01-25 20:31:27 -08002252 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002253 else if (time_to_wait < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002254 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002255 else
2256 {
2257 if (svm_msg_q_timedwait (mq, time_to_wait))
Florin Coras5398dfb2021-01-25 20:31:27 -08002258 return 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002259 }
2260 }
Florin Corase003a1b2019-06-05 10:47:16 -07002261 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002262
Florin Coras134a9962018-08-28 11:32:04 -07002263 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002264 {
Florin Coras134a9962018-08-28 11:32:04 -07002265 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002266 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002267 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2268 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002269 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002270 }
Florin Coras134a9962018-08-28 11:32:04 -07002271 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002272 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002273 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002274}
2275
Florin Coras99368312018-08-02 10:45:44 -07002276static int
Florin Coras294afe22019-01-07 17:49:17 -08002277vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2278 vcl_si_set * read_map, vcl_si_set * write_map,
2279 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002280 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002281{
Florin Coras14ed6df2019-03-06 21:13:42 -08002282 double wait = 0, start = 0;
2283
2284 if (!*bits_set)
2285 {
2286 wait = time_to_wait;
2287 start = clib_time_now (&wrk->clib_time);
2288 }
2289
2290 do
2291 {
2292 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2293 write_map, except_map, wait, bits_set);
2294 if (*bits_set)
2295 return *bits_set;
2296 if (wait == -1)
2297 continue;
2298
2299 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2300 }
2301 while (wait > 0);
2302
2303 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002304}
2305
2306static int
Florin Coras294afe22019-01-07 17:49:17 -08002307vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2308 vcl_si_set * read_map, vcl_si_set * write_map,
2309 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002310 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002311{
2312 vcl_mq_evt_conn_t *mqc;
2313 int __clib_unused n_read;
2314 int n_mq_evts, i;
2315 u64 buf;
2316
Florin Coras134a9962018-08-28 11:32:04 -07002317 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2318 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2319 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002320 for (i = 0; i < n_mq_evts; i++)
2321 {
Florin Coras134a9962018-08-28 11:32:04 -07002322 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002323 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002324 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002325 except_map, 0, bits_set);
2326 }
2327
2328 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2329}
2330
Dave Wallace543852a2017-08-03 02:11:34 -04002331int
Florin Coras294afe22019-01-07 17:49:17 -08002332vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2333 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002334{
Florin Coras54693d22018-07-17 10:46:29 -07002335 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002336 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002337 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002338 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002339
Dave Wallace7876d392017-10-19 03:53:57 -04002340 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002341 {
Florin Coras134a9962018-08-28 11:32:04 -07002342 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002343 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002344 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2345 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002346 }
Dave Wallace7876d392017-10-19 03:53:57 -04002347 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002348 {
Florin Coras134a9962018-08-28 11:32:04 -07002349 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002350 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002351 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2352 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002353 }
Dave Wallace7876d392017-10-19 03:53:57 -04002354 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002355 {
Florin Coras134a9962018-08-28 11:32:04 -07002356 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002357 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002358 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2359 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002360 }
2361
Florin Coras54693d22018-07-17 10:46:29 -07002362 if (!n_bits)
2363 return 0;
2364
2365 if (!write_map)
2366 goto check_rd;
2367
Florin Coras096a1d52021-01-27 15:33:51 -08002368 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2369 {
2370 if (!(s = vcl_session_get (wrk, sid)))
2371 {
2372 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2373 bits_set++;
2374 continue;
2375 }
Florin Coras54693d22018-07-17 10:46:29 -07002376
Florin Coras096a1d52021-01-27 15:33:51 -08002377 if (vcl_session_write_ready (s))
2378 {
2379 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2380 bits_set++;
2381 }
2382 else
2383 {
2384 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2385 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2386 }
2387 }
Florin Coras54693d22018-07-17 10:46:29 -07002388
2389check_rd:
2390 if (!read_map)
2391 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002392
Florin Coras096a1d52021-01-27 15:33:51 -08002393 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2394 {
2395 if (!(s = vcl_session_get (wrk, sid)))
2396 {
2397 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2398 bits_set++;
2399 continue;
2400 }
Florin Coras54693d22018-07-17 10:46:29 -07002401
Florin Coras096a1d52021-01-27 15:33:51 -08002402 if (vcl_session_read_ready (s))
2403 {
2404 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2405 bits_set++;
2406 }
2407 }
Florin Coras54693d22018-07-17 10:46:29 -07002408
2409check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002410
Florin Coras86f04502018-09-12 16:08:01 -07002411 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2412 {
2413 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2414 read_map, write_map, except_map, &bits_set);
2415 }
2416 vec_reset_length (wrk->unhandled_evts_vector);
2417
Florin Coras99368312018-08-02 10:45:44 -07002418 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002419 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002420 time_to_wait, &bits_set);
2421 else
Florin Coras134a9962018-08-28 11:32:04 -07002422 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002423 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002424
Dave Wallace543852a2017-08-03 02:11:34 -04002425 return (bits_set);
2426}
2427
Dave Wallacef7f809c2017-10-03 01:48:42 -04002428static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002429vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002430{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002431 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002432 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002433 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002434
Florin Corasc0737e92019-03-04 14:19:39 -08002435 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002436 return;
2437
Florin Coras6c3b2182020-10-19 18:36:48 -07002438 s = vcl_session_get_w_handle (wrk, vep_handle);
2439 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002440 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002441 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002442 goto done;
2443 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002444 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002445 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002446 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002447 goto done;
2448 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002449 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002450 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002451 "{\n"
2452 " is_vep = %u\n"
2453 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002454 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002455 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2456 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002457
Florin Coras7e5e62b2019-07-30 14:08:23 -07002458 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002459 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002460 s = vcl_session_get_w_handle (wrk, sh);
2461 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002462 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002463 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002464 goto done;
2465 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002466 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002467 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002468 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002469 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002470 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002471 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002472 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002473 goto done;
2474 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002475 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002476 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2477 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002478 sh, s->vep.vep_sh, vep_handle);
2479 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002480 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002481 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002482 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002483 " next_sh = 0x%x (%u)\n"
2484 " prev_sh = 0x%x (%u)\n"
2485 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002486 " ev.events = 0x%x\n"
2487 " ev.data.u64 = 0x%llx\n"
2488 " et_mask = 0x%x\n"
2489 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002490 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002491 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2492 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002493 }
2494 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002495
2496done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002497 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002498}
2499
2500int
2501vppcom_epoll_create (void)
2502{
Florin Coras134a9962018-08-28 11:32:04 -07002503 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002504 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002505
Florin Coras134a9962018-08-28 11:32:04 -07002506 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002507
Florin Coras6c3b2182020-10-19 18:36:48 -07002508 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002509 vep_session->vep.vep_sh = ~0;
2510 vep_session->vep.next_sh = ~0;
2511 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002512 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002513
Florin Corasa7a1a222018-12-30 17:11:31 -08002514 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2515 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002516
Florin Corasab2f6db2018-08-31 14:31:41 -07002517 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002518}
2519
2520int
Florin Coras134a9962018-08-28 11:32:04 -07002521vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002522 struct epoll_event *event)
2523{
Florin Coras134a9962018-08-28 11:32:04 -07002524 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002525 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002526 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002527 vcl_session_t *s;
2528 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002529
Florin Coras134a9962018-08-28 11:32:04 -07002530 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002531 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002532 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002533 return VPPCOM_EINVAL;
2534 }
2535
Florin Coras134a9962018-08-28 11:32:04 -07002536 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002537 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002538 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002539 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002540 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002541 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002542 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002543 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002544 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002545 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002546 }
2547
Florin Coras134a9962018-08-28 11:32:04 -07002548 ASSERT (vep_session->vep.vep_sh == ~0);
2549 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002550
Florin Coras17ec5772020-08-12 20:46:29 -07002551 s = vcl_session_get_w_handle (wrk, session_handle);
2552 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002553 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002554 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002555 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002556 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002557 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002558 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002559 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002560 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002561 }
2562
2563 switch (op)
2564 {
2565 case EPOLL_CTL_ADD:
2566 if (PREDICT_FALSE (!event))
2567 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002568 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002569 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002570 }
Florin Coras134a9962018-08-28 11:32:04 -07002571 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002572 {
Florin Coras7e12d942018-06-27 14:32:43 -07002573 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002574 next_session = vcl_session_get_w_handle (wrk,
2575 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002576 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002577 {
Florin Coras5e062572019-03-14 19:07:51 -07002578 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002579 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002580 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002581 }
Florin Coras134a9962018-08-28 11:32:04 -07002582 ASSERT (next_session->vep.prev_sh == vep_handle);
2583 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002584 }
Florin Coras17ec5772020-08-12 20:46:29 -07002585 s->vep.next_sh = vep_session->vep.next_sh;
2586 s->vep.prev_sh = vep_handle;
2587 s->vep.vep_sh = vep_handle;
2588 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2589 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002590 s->flags &= ~VCL_SESSION_F_IS_VEP;
2591 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002592 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002593
Florin Coras17ec5772020-08-12 20:46:29 -07002594 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2595 if (txf && (event->events & EPOLLOUT))
2596 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002597
Florin Coras6e3c1f82020-01-15 01:30:46 +00002598 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002599 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002600 {
2601 session_event_t e = { 0 };
2602 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002603 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002604 vec_add1 (wrk->unhandled_evts_vector, e);
2605 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002606 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002607 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002608 {
2609 session_event_t e = { 0 };
2610 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002611 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002612 vec_add1 (wrk->unhandled_evts_vector, e);
2613 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002614 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2615 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002616 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002617 break;
2618
2619 case EPOLL_CTL_MOD:
2620 if (PREDICT_FALSE (!event))
2621 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002622 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002623 rv = VPPCOM_EINVAL;
2624 goto done;
2625 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002626 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002627 {
Florin Coras5e062572019-03-14 19:07:51 -07002628 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002629 rv = VPPCOM_EINVAL;
2630 goto done;
2631 }
Florin Coras17ec5772020-08-12 20:46:29 -07002632 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002633 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002634 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002635 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002636 rv = VPPCOM_EINVAL;
2637 goto done;
2638 }
hanlin475c9d72019-12-26 11:44:28 +08002639
2640 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2641 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002642 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002643 {
2644 session_event_t e = { 0 };
2645 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002646 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002647 vec_add1 (wrk->unhandled_evts_vector, e);
2648 }
Florin Coras17ec5772020-08-12 20:46:29 -07002649 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2650 s->vep.ev = *event;
2651 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2652 if (event->events & EPOLLOUT)
2653 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2654 else
2655 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002656 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2657 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002658 break;
2659
2660 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002661 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002662 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002663 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002664 rv = VPPCOM_EINVAL;
2665 goto done;
2666 }
Florin Coras17ec5772020-08-12 20:46:29 -07002667 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002668 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002669 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002670 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002671 rv = VPPCOM_EINVAL;
2672 goto done;
2673 }
2674
Florin Coras17ec5772020-08-12 20:46:29 -07002675 if (s->vep.prev_sh == vep_handle)
2676 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002677 else
2678 {
Florin Coras7e12d942018-06-27 14:32:43 -07002679 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002680 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002681 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682 {
Florin Coras5e062572019-03-14 19:07:51 -07002683 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002684 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002685 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686 }
Florin Coras134a9962018-08-28 11:32:04 -07002687 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002688 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002689 }
Florin Coras17ec5772020-08-12 20:46:29 -07002690 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691 {
Florin Coras7e12d942018-06-27 14:32:43 -07002692 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002693 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002694 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002695 {
Florin Coras5e062572019-03-14 19:07:51 -07002696 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002697 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002698 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002699 }
Florin Coras134a9962018-08-28 11:32:04 -07002700 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002701 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002702 }
2703
Florin Coras17ec5772020-08-12 20:46:29 -07002704 memset (&s->vep, 0, sizeof (s->vep));
2705 s->vep.next_sh = ~0;
2706 s->vep.prev_sh = ~0;
2707 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002708 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002709
Florin Coras17ec5772020-08-12 20:46:29 -07002710 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2711 if (txf)
2712 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002713
Florin Coras5e062572019-03-14 19:07:51 -07002714 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002715 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002716 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002717 break;
2718
2719 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002720 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002721 rv = VPPCOM_EINVAL;
2722 }
2723
Florin Coras134a9962018-08-28 11:32:04 -07002724 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002725
2726done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002727 return rv;
2728}
2729
Florin Coras86f04502018-09-12 16:08:01 -07002730static inline void
2731vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2732 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002733{
2734 session_disconnected_msg_t *disconnected_msg;
2735 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002736 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002737 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002738 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002739 u8 add_event = 0;
2740
2741 switch (e->event_type)
2742 {
Florin Coras653e43f2019-03-04 10:56:23 -08002743 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002744 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002745 s = vcl_session_get (wrk, sid);
2746 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002747 break;
Florin Corasb242d312020-10-26 15:35:40 -07002748 vcl_fifo_rx_evt_valid_or_break (s);
2749 session_events = s->vep.ev.events;
2750 if (!(EPOLLIN & s->vep.ev.events)
2751 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002752 break;
2753 add_event = 1;
2754 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002755 session_evt_data = s->vep.ev.data.u64;
2756 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002757 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002758 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002759 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002760 s = vcl_session_get (wrk, sid);
2761 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002762 break;
Florin Corasb242d312020-10-26 15:35:40 -07002763 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002764 if (!(EPOLLOUT & session_events))
2765 break;
2766 add_event = 1;
2767 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002768 session_evt_data = s->vep.ev.data.u64;
2769 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2770 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002771 break;
Florin Coras86f04502018-09-12 16:08:01 -07002772 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002773 if (!e->postponed)
2774 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2775 else
2776 s = vcl_session_get (wrk, e->session_index);
2777 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002778 break;
Florin Corasb242d312020-10-26 15:35:40 -07002779 session_events = s->vep.ev.events;
2780 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002781 if (!(EPOLLIN & session_events))
2782 break;
Florin Coras86f04502018-09-12 16:08:01 -07002783 add_event = 1;
2784 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002785 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002786 break;
2787 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002788 if (!e->postponed)
2789 {
2790 connected_msg = (session_connected_msg_t *) e->data;
2791 sid = vcl_session_connected_handler (wrk, connected_msg);
2792 }
2793 else
2794 sid = e->session_index;
2795 s = vcl_session_get (wrk, sid);
2796 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002797 break;
Florin Corasb242d312020-10-26 15:35:40 -07002798 session_events = s->vep.ev.events;
2799 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002800 if (!(EPOLLOUT & session_events))
2801 break;
2802 add_event = 1;
2803 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002804 session_evt_data = s->vep.ev.data.u64;
2805 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002806 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002807 break;
2808 case SESSION_CTRL_EVT_DISCONNECTED:
2809 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002810 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2811 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002812 break;
Florin Corasb242d312020-10-26 15:35:40 -07002813 sid = s->session_index;
2814 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002815 add_event = 1;
2816 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002817 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002818 break;
2819 case SESSION_CTRL_EVT_RESET:
2820 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002821 s = vcl_session_get (wrk, sid);
2822 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002823 break;
Florin Corasb242d312020-10-26 15:35:40 -07002824 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002825 add_event = 1;
2826 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002827 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002828 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002829 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2830 vcl_session_unlisten_reply_handler (wrk, e->data);
2831 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002832 case SESSION_CTRL_EVT_MIGRATED:
2833 vcl_session_migrated_handler (wrk, e->data);
2834 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002835 case SESSION_CTRL_EVT_CLEANUP:
2836 vcl_session_cleanup_handler (wrk, e->data);
2837 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002838 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2839 vcl_session_req_worker_update_handler (wrk, e->data);
2840 break;
2841 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2842 vcl_session_worker_update_reply_handler (wrk, e->data);
2843 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002844 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2845 vcl_session_app_add_segment_handler (wrk, e->data);
2846 break;
2847 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2848 vcl_session_app_del_segment_handler (wrk, e->data);
2849 break;
hanlina3a48962020-07-13 11:09:15 +08002850 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002851 vcl_worker_rpc_handler (wrk, e->data);
2852 break;
Florin Coras86f04502018-09-12 16:08:01 -07002853 default:
2854 VDBG (0, "unhandled: %u", e->event_type);
2855 break;
2856 }
2857
2858 if (add_event)
2859 {
2860 events[*num_ev].data.u64 = session_evt_data;
2861 if (EPOLLONESHOT & session_events)
2862 {
Florin Corasb242d312020-10-26 15:35:40 -07002863 s = vcl_session_get (wrk, sid);
2864 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002865 }
2866 *num_ev += 1;
2867 }
2868}
2869
2870static int
2871vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2872 struct epoll_event *events, u32 maxevents,
2873 double wait_for_time, u32 * num_ev)
2874{
Florin Coras99368312018-08-02 10:45:44 -07002875 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002876 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002877 int i;
2878
Florin Coras539663c2018-09-28 14:59:37 -07002879 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2880 goto handle_dequeued;
2881
Florin Coras54693d22018-07-17 10:46:29 -07002882 if (svm_msg_q_is_empty (mq))
2883 {
2884 if (!wait_for_time)
Florin Coras5398dfb2021-01-25 20:31:27 -08002885 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002886 else if (wait_for_time < 0)
Florin Coras5398dfb2021-01-25 20:31:27 -08002887 svm_msg_q_wait (mq, SVM_MQ_WAIT_EMPTY);
Florin Coras54693d22018-07-17 10:46:29 -07002888 else
2889 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002890 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras5398dfb2021-01-25 20:31:27 -08002891 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07002892 }
2893 }
Florin Corase003a1b2019-06-05 10:47:16 -07002894 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002895 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002896
Florin Coras539663c2018-09-28 14:59:37 -07002897handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002898 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002899 {
Florin Coras134a9962018-08-28 11:32:04 -07002900 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002901 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002902 if (*num_ev < maxevents)
2903 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2904 else
2905 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07002906 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002907 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002908 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002909 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002910 return *num_ev;
2911}
2912
Florin Coras99368312018-08-02 10:45:44 -07002913static int
Florin Coras134a9962018-08-28 11:32:04 -07002914vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002915 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002916{
Florin Corase003a1b2019-06-05 10:47:16 -07002917 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002918
2919 if (!n_evts)
2920 {
2921 wait = wait_for_time;
2922 start = clib_time_now (&wrk->clib_time);
2923 }
2924
2925 do
2926 {
2927 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2928 wait, &n_evts);
2929 if (n_evts)
2930 return n_evts;
2931 if (wait == -1)
2932 continue;
2933
Florin Corase003a1b2019-06-05 10:47:16 -07002934 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07002935 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07002936 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002937 }
2938 while (wait > 0);
2939
2940 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002941}
2942
2943static int
Florin Coras134a9962018-08-28 11:32:04 -07002944vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002945 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002946{
Florin Corasb13ba132021-01-27 16:05:24 -08002947 double wait = 0, start = 0, now;
Florin Coras99368312018-08-02 10:45:44 -07002948 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08002949 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07002950 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002951 u64 buf;
2952
Florin Coras134a9962018-08-28 11:32:04 -07002953 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08002954 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07002955 {
Florin Corasb13ba132021-01-27 16:05:24 -08002956 wait = wait_for_time;
2957 start = clib_time_now (&wrk->clib_time);
Florin Coras99368312018-08-02 10:45:44 -07002958 }
2959
Florin Corasb13ba132021-01-27 16:05:24 -08002960 do
2961 {
2962 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2963 vec_len (wrk->mq_events), wait);
2964 if (n_mq_evts < 0)
2965 {
2966 VDBG (0, "epoll_wait error %u", errno);
2967 return n_evts;
2968 }
2969
2970 for (i = 0; i < n_mq_evts; i++)
2971 {
2972 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2973 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2974 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
2975 &n_evts);
2976 }
2977
2978 if (n_evts)
2979 return n_evts;
2980 if (wait == -1)
2981 continue;
2982
2983 now = clib_time_now (&wrk->clib_time);
2984 wait -= (now - start) * 1e3;
2985 start = now;
2986 }
2987 while (wait > 0);
2988
2989 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002990}
2991
Dave Wallacef7f809c2017-10-03 01:48:42 -04002992int
Florin Coras134a9962018-08-28 11:32:04 -07002993vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002994 int maxevents, double wait_for_time)
2995{
Florin Coras134a9962018-08-28 11:32:04 -07002996 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002997 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002998 u32 n_evts = 0;
2999 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003000
3001 if (PREDICT_FALSE (maxevents <= 0))
3002 {
Florin Coras5e062572019-03-14 19:07:51 -07003003 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003004 return VPPCOM_EINVAL;
3005 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003006
Florin Coras134a9962018-08-28 11:32:04 -07003007 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003008 if (!vep_session)
3009 return VPPCOM_EBADFD;
3010
Florin Coras6c3b2182020-10-19 18:36:48 -07003011 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003012 {
Florin Coras5e062572019-03-14 19:07:51 -07003013 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003014 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003015 }
Florin Coras54693d22018-07-17 10:46:29 -07003016
3017 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003018
Florin Coras86f04502018-09-12 16:08:01 -07003019 if (vec_len (wrk->unhandled_evts_vector))
3020 {
3021 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3022 {
3023 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3024 events, &n_evts);
3025 if (n_evts == maxevents)
3026 {
Florin Corase003a1b2019-06-05 10:47:16 -07003027 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3028 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003029 }
3030 }
Florin Corase003a1b2019-06-05 10:47:16 -07003031 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003032 }
3033
3034 if (vcm->cfg.use_mq_eventfd)
3035 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3036 wait_for_time);
3037
3038 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3039 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003040}
3041
Dave Wallace35830af2017-10-09 01:43:42 -04003042int
Florin Coras134a9962018-08-28 11:32:04 -07003043vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003044 void *buffer, uint32_t * buflen)
3045{
Florin Coras134a9962018-08-28 11:32:04 -07003046 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003047 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003048 vppcom_endpt_t *ep = buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003049 vcl_session_t *session;
3050 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003051
Florin Coras134a9962018-08-28 11:32:04 -07003052 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003053 if (!session)
3054 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003055
Dave Wallace35830af2017-10-09 01:43:42 -04003056 switch (op)
3057 {
3058 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003059 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003060 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3061 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003062 break;
3063
Dave Wallace227867f2017-11-13 21:21:53 -05003064 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003065 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003066 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3067 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003068 break;
3069
3070 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003071 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003072 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003073 *flags =
3074 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003075 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003076 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003077 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003078 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3079 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003080 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003081 }
3082 else
3083 rv = VPPCOM_EINVAL;
3084 break;
3085
3086 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003087 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003088 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003089 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003090 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003091 else
Florin Corasac422d62020-10-19 20:51:36 -07003092 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003093
Florin Coras5e062572019-03-14 19:07:51 -07003094 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3095 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003096 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003097 }
3098 else
3099 rv = VPPCOM_EINVAL;
3100 break;
3101
3102 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003103 if (PREDICT_TRUE (buffer && buflen &&
3104 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003105 {
Florin Coras7e12d942018-06-27 14:32:43 -07003106 ep->is_ip4 = session->transport.is_ip4;
3107 ep->port = session->transport.rmt_port;
3108 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003109 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3110 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003111 else
Dave Barach178cf492018-11-13 16:34:13 -05003112 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3113 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003114 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003115 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3116 "addr = %U, port %u", session_handle, ep->is_ip4,
3117 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003118 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3119 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003120 }
3121 else
3122 rv = VPPCOM_EINVAL;
3123 break;
3124
3125 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003126 if (PREDICT_TRUE (buffer && buflen &&
3127 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003128 {
Florin Coras7e12d942018-06-27 14:32:43 -07003129 ep->is_ip4 = session->transport.is_ip4;
3130 ep->port = session->transport.lcl_port;
3131 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003132 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3133 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003134 else
Dave Barach178cf492018-11-13 16:34:13 -05003135 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3136 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003137 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003138 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3139 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003140 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003141 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3142 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003143 }
3144 else
3145 rv = VPPCOM_EINVAL;
3146 break;
Stevenb5a11602017-10-11 09:59:30 -07003147
Florin Corasef7cbf62019-10-17 09:56:27 -07003148 case VPPCOM_ATTR_SET_LCL_ADDR:
3149 if (PREDICT_TRUE (buffer && buflen &&
3150 (*buflen >= sizeof (*ep)) && ep->ip))
3151 {
3152 session->transport.is_ip4 = ep->is_ip4;
3153 session->transport.lcl_port = ep->port;
3154 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3155 *buflen = sizeof (*ep);
3156 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3157 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3158 &session->transport.lcl_ip,
3159 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3160 clib_net_to_host_u16 (ep->port));
3161 }
3162 else
3163 rv = VPPCOM_EINVAL;
3164 break;
3165
Dave Wallace048b1d62018-01-03 22:24:41 -05003166 case VPPCOM_ATTR_GET_LIBC_EPFD:
3167 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003168 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003169 break;
3170
3171 case VPPCOM_ATTR_SET_LIBC_EPFD:
3172 if (PREDICT_TRUE (buffer && buflen &&
3173 (*buflen == sizeof (session->libc_epfd))))
3174 {
3175 session->libc_epfd = *(int *) buffer;
3176 *buflen = sizeof (session->libc_epfd);
3177
Florin Coras5e062572019-03-14 19:07:51 -07003178 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3179 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003180 }
3181 else
3182 rv = VPPCOM_EINVAL;
3183 break;
3184
3185 case VPPCOM_ATTR_GET_PROTOCOL:
3186 if (buffer && buflen && (*buflen >= sizeof (int)))
3187 {
Florin Coras7e12d942018-06-27 14:32:43 -07003188 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003189 *buflen = sizeof (int);
3190
Florin Coras5e062572019-03-14 19:07:51 -07003191 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3192 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003193 }
3194 else
3195 rv = VPPCOM_EINVAL;
3196 break;
3197
3198 case VPPCOM_ATTR_GET_LISTEN:
3199 if (buffer && buflen && (*buflen >= sizeof (int)))
3200 {
Florin Corasac422d62020-10-19 20:51:36 -07003201 *(int *) buffer = vcl_session_has_attr (session,
3202 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003203 *buflen = sizeof (int);
3204
Florin Coras5e062572019-03-14 19:07:51 -07003205 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3206 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003207 }
3208 else
3209 rv = VPPCOM_EINVAL;
3210 break;
3211
3212 case VPPCOM_ATTR_GET_ERROR:
3213 if (buffer && buflen && (*buflen >= sizeof (int)))
3214 {
3215 *(int *) buffer = 0;
3216 *buflen = sizeof (int);
3217
Florin Coras5e062572019-03-14 19:07:51 -07003218 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3219 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003220 }
3221 else
3222 rv = VPPCOM_EINVAL;
3223 break;
3224
3225 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3226 if (buffer && buflen && (*buflen >= sizeof (u32)))
3227 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003228
3229 /* VPP-TBD */
3230 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003231 session->tx_fifo ?
3232 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003233 vcm->cfg.tx_fifo_size);
3234 *buflen = sizeof (u32);
3235
Florin Coras5e062572019-03-14 19:07:51 -07003236 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3237 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3238 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003239 }
3240 else
3241 rv = VPPCOM_EINVAL;
3242 break;
3243
3244 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3245 if (buffer && buflen && (*buflen == sizeof (u32)))
3246 {
3247 /* VPP-TBD */
3248 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003249 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3250 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3251 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003252 }
3253 else
3254 rv = VPPCOM_EINVAL;
3255 break;
3256
3257 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3258 if (buffer && buflen && (*buflen >= sizeof (u32)))
3259 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003260
3261 /* VPP-TBD */
3262 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003263 session->rx_fifo ?
3264 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003265 vcm->cfg.rx_fifo_size);
3266 *buflen = sizeof (u32);
3267
Florin Coras5e062572019-03-14 19:07:51 -07003268 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3269 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003270 }
3271 else
3272 rv = VPPCOM_EINVAL;
3273 break;
3274
3275 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3276 if (buffer && buflen && (*buflen == sizeof (u32)))
3277 {
3278 /* VPP-TBD */
3279 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003280 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3281 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3282 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003283 }
3284 else
3285 rv = VPPCOM_EINVAL;
3286 break;
3287
3288 case VPPCOM_ATTR_GET_REUSEADDR:
3289 if (buffer && buflen && (*buflen >= sizeof (int)))
3290 {
3291 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003292 *(int *) buffer = vcl_session_has_attr (session,
3293 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003294 *buflen = sizeof (int);
3295
Florin Coras5e062572019-03-14 19:07:51 -07003296 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3297 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003298 }
3299 else
3300 rv = VPPCOM_EINVAL;
3301 break;
3302
Stevenb5a11602017-10-11 09:59:30 -07003303 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003304 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003305 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003306 {
3307 /* VPP-TBD */
3308 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003309 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003310 else
Florin Corasac422d62020-10-19 20:51:36 -07003311 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003312
Florin Coras5e062572019-03-14 19:07:51 -07003313 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003314 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003315 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003316 }
3317 else
3318 rv = VPPCOM_EINVAL;
3319 break;
3320
3321 case VPPCOM_ATTR_GET_REUSEPORT:
3322 if (buffer && buflen && (*buflen >= sizeof (int)))
3323 {
3324 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003325 *(int *) buffer = vcl_session_has_attr (session,
3326 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003327 *buflen = sizeof (int);
3328
Florin Coras5e062572019-03-14 19:07:51 -07003329 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3330 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003331 }
3332 else
3333 rv = VPPCOM_EINVAL;
3334 break;
3335
3336 case VPPCOM_ATTR_SET_REUSEPORT:
3337 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003338 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003339 {
3340 /* VPP-TBD */
3341 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003342 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003343 else
Florin Corasac422d62020-10-19 20:51:36 -07003344 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003345
Florin Coras5e062572019-03-14 19:07:51 -07003346 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003347 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003348 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003349 }
3350 else
3351 rv = VPPCOM_EINVAL;
3352 break;
3353
3354 case VPPCOM_ATTR_GET_BROADCAST:
3355 if (buffer && buflen && (*buflen >= sizeof (int)))
3356 {
3357 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003358 *(int *) buffer = vcl_session_has_attr (session,
3359 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003360 *buflen = sizeof (int);
3361
Florin Coras5e062572019-03-14 19:07:51 -07003362 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3363 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003364 }
3365 else
3366 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003367 break;
3368
3369 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003370 if (buffer && buflen && (*buflen == sizeof (int)))
3371 {
3372 /* VPP-TBD */
3373 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003374 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003375 else
Florin Corasac422d62020-10-19 20:51:36 -07003376 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003377
Florin Coras5e062572019-03-14 19:07:51 -07003378 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003379 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003380 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003381 }
3382 else
3383 rv = VPPCOM_EINVAL;
3384 break;
3385
3386 case VPPCOM_ATTR_GET_V6ONLY:
3387 if (buffer && buflen && (*buflen >= sizeof (int)))
3388 {
3389 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003390 *(int *) buffer = vcl_session_has_attr (session,
3391 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003392 *buflen = sizeof (int);
3393
Florin Coras5e062572019-03-14 19:07:51 -07003394 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3395 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003396 }
3397 else
3398 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003399 break;
3400
3401 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003402 if (buffer && buflen && (*buflen == sizeof (int)))
3403 {
3404 /* VPP-TBD */
3405 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003406 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003407 else
Florin Corasac422d62020-10-19 20:51:36 -07003408 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003409
Florin Coras5e062572019-03-14 19:07:51 -07003410 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003411 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003412 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003413 }
3414 else
3415 rv = VPPCOM_EINVAL;
3416 break;
3417
3418 case VPPCOM_ATTR_GET_KEEPALIVE:
3419 if (buffer && buflen && (*buflen >= sizeof (int)))
3420 {
3421 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003422 *(int *) buffer = vcl_session_has_attr (session,
3423 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003424 *buflen = sizeof (int);
3425
Florin Coras5e062572019-03-14 19:07:51 -07003426 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3427 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003428 }
3429 else
3430 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003431 break;
Stevenbccd3392017-10-12 20:42:21 -07003432
3433 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 if (buffer && buflen && (*buflen == sizeof (int)))
3435 {
3436 /* VPP-TBD */
3437 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003438 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003439 else
Florin Corasac422d62020-10-19 20:51:36 -07003440 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003441
Florin Coras5e062572019-03-14 19:07:51 -07003442 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003443 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003444 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003445 }
3446 else
3447 rv = VPPCOM_EINVAL;
3448 break;
3449
3450 case VPPCOM_ATTR_GET_TCP_NODELAY:
3451 if (buffer && buflen && (*buflen >= sizeof (int)))
3452 {
3453 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003454 *(int *) buffer = vcl_session_has_attr (session,
3455 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003456 *buflen = sizeof (int);
3457
Florin Coras5e062572019-03-14 19:07:51 -07003458 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3459 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003460 }
3461 else
3462 rv = VPPCOM_EINVAL;
3463 break;
3464
3465 case VPPCOM_ATTR_SET_TCP_NODELAY:
3466 if (buffer && buflen && (*buflen == sizeof (int)))
3467 {
3468 /* VPP-TBD */
3469 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003470 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003471 else
Florin Corasac422d62020-10-19 20:51:36 -07003472 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003473
Florin Coras5e062572019-03-14 19:07:51 -07003474 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003475 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003476 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003477 }
3478 else
3479 rv = VPPCOM_EINVAL;
3480 break;
3481
3482 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3483 if (buffer && buflen && (*buflen >= sizeof (int)))
3484 {
3485 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003486 *(int *) buffer = vcl_session_has_attr (session,
3487 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003488 *buflen = sizeof (int);
3489
Florin Coras5e062572019-03-14 19:07:51 -07003490 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3491 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003492 }
3493 else
3494 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003495 break;
3496
3497 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003498 if (buffer && buflen && (*buflen == sizeof (int)))
3499 {
3500 /* VPP-TBD */
3501 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003502 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003503 else
Florin Corasac422d62020-10-19 20:51:36 -07003504 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003505
Florin Coras5e062572019-03-14 19:07:51 -07003506 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003507 vcl_session_has_attr (session,
3508 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003509 }
3510 else
3511 rv = VPPCOM_EINVAL;
3512 break;
3513
3514 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3515 if (buffer && buflen && (*buflen >= sizeof (int)))
3516 {
3517 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003518 *(int *) buffer = vcl_session_has_attr (session,
3519 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003520 *buflen = sizeof (int);
3521
Florin Coras5e062572019-03-14 19:07:51 -07003522 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3523 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003524 }
3525 else
3526 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003527 break;
3528
3529 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003530 if (buffer && buflen && (*buflen == sizeof (int)))
3531 {
3532 /* VPP-TBD */
3533 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003534 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003535 else
Florin Corasac422d62020-10-19 20:51:36 -07003536 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003537
Florin Coras5e062572019-03-14 19:07:51 -07003538 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003539 vcl_session_has_attr (session,
3540 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003541 }
3542 else
3543 rv = VPPCOM_EINVAL;
3544 break;
3545
3546 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3547 if (buffer && buflen && (*buflen >= sizeof (u32)))
3548 {
3549 /* VPP-TBD */
3550 *(u32 *) buffer = session->user_mss;
3551 *buflen = sizeof (int);
3552
Florin Coras5e062572019-03-14 19:07:51 -07003553 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3554 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003555 }
3556 else
3557 rv = VPPCOM_EINVAL;
3558 break;
3559
3560 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3561 if (buffer && buflen && (*buflen == sizeof (u32)))
3562 {
3563 /* VPP-TBD */
3564 session->user_mss = *(u32 *) buffer;
3565
Florin Coras5e062572019-03-14 19:07:51 -07003566 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3567 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003568 }
3569 else
3570 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003571 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003572
Florin Coras7baeb712019-01-04 17:05:43 -08003573 case VPPCOM_ATTR_SET_SHUT:
3574 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003575 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003576 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003577 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003578 break;
3579
3580 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003581 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003582 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003583 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003584 tmp_flags |= 2;
3585 if (tmp_flags == 1)
3586 *(int *) buffer = SHUT_RD;
3587 else if (tmp_flags == 2)
3588 *(int *) buffer = SHUT_WR;
3589 else if (tmp_flags == 3)
3590 *(int *) buffer = SHUT_RDWR;
3591 *buflen = sizeof (int);
3592 break;
Florin Coras1e966172020-05-16 18:18:14 +00003593
3594 case VPPCOM_ATTR_SET_CONNECTED:
3595 session->flags |= VCL_SESSION_F_CONNECTED;
3596 break;
3597
Florin Corasa5a9efd2021-01-05 17:03:29 -08003598 case VPPCOM_ATTR_SET_CKPAIR:
3599 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3600 !vcl_session_has_crypto (session))
3601 {
3602 rv = VPPCOM_EINVAL;
3603 break;
3604 }
3605 session->ckpair_index = *(uint32_t *) buffer;
3606 break;
3607
Florin Coras6a6555a2021-01-28 11:39:27 -08003608 case VPPCOM_ATTR_SET_VRF:
3609 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3610 {
3611 rv = VPPCOM_EINVAL;
3612 break;
3613 }
3614 session->vrf = *(u32 *) buffer;
3615 break;
3616
3617 case VPPCOM_ATTR_GET_VRF:
3618 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3619 {
3620 rv = VPPCOM_EINVAL;
3621 break;
3622 }
3623 *(u32 *) buffer = session->vrf;
3624 *buflen = sizeof (u32);
3625 break;
3626
Dave Wallacee22aa742017-10-20 12:30:38 -04003627 default:
3628 rv = VPPCOM_EINVAL;
3629 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003630 }
3631
Dave Wallace35830af2017-10-09 01:43:42 -04003632 return rv;
3633}
3634
Stevenac1f96d2017-10-24 16:03:58 -07003635int
Florin Coras134a9962018-08-28 11:32:04 -07003636vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003637 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3638{
Florin Coras134a9962018-08-28 11:32:04 -07003639 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003640 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003641 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003642
3643 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003644 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003645 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003646 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003647 else
3648 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003649 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003650 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003651 }
3652
Florin Coras0a1e1832020-03-29 18:54:04 +00003653 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003654 {
Florin Coras5da10c42020-04-01 04:31:21 +00003655 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003656 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003657 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3658 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003659 else
Dave Barach178cf492018-11-13 16:34:13 -05003660 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3661 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003662 ep->is_ip4 = session->transport.is_ip4;
3663 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003664 }
Florin Coras460dce62018-07-27 05:45:06 -07003665
Stevenac1f96d2017-10-24 16:03:58 -07003666 return rv;
3667}
3668
3669int
Florin Coras134a9962018-08-28 11:32:04 -07003670vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003671 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3672{
Florin Coras7a2abce2020-04-05 19:25:44 +00003673 vcl_worker_t *wrk = vcl_worker_get_current ();
3674 vcl_session_t *s;
3675
3676 s = vcl_session_get_w_handle (wrk, session_handle);
3677 if (!s)
3678 return VPPCOM_EBADFD;
3679
Dave Wallace617dffa2017-10-26 14:47:06 -04003680 if (!buffer)
3681 return VPPCOM_EINVAL;
3682
3683 if (ep)
3684 {
Florin Coras5824cc52020-10-20 18:44:41 -07003685 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003686 return VPPCOM_EINVAL;
3687
3688 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003689 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003690 {
Florin Coras5824cc52020-10-20 18:44:41 -07003691 u32 session_index = s->session_index;
3692 f64 timeout = vcm->cfg.session_timeout;
3693 int rv;
3694
Florin Coras5da10c42020-04-01 04:31:21 +00003695 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003696 rv = vppcom_wait_for_session_state_change (session_index,
3697 VCL_STATE_READY,
3698 timeout);
3699 if (rv < 0)
3700 return rv;
3701 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003702 }
Florin Coras5824cc52020-10-20 18:44:41 -07003703
3704 s->transport.is_ip4 = ep->is_ip4;
3705 s->transport.rmt_port = ep->port;
3706 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003707 }
3708
3709 if (flags)
3710 {
3711 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003712 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003713 }
3714
Florin Coras7a2abce2020-04-05 19:25:44 +00003715 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3716 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003717}
3718
Dave Wallace048b1d62018-01-03 22:24:41 -05003719int
3720vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3721{
Florin Coras134a9962018-08-28 11:32:04 -07003722 vcl_worker_t *wrk = vcl_worker_get_current ();
3723 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003724 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003725 svm_msg_q_msg_t msg;
3726 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003727 int rv, num_ev = 0;
3728
Florin Coras5e062572019-03-14 19:07:51 -07003729 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003730
3731 if (!vp)
3732 return VPPCOM_EFAULT;
3733
3734 do
3735 {
Florin Coras7e12d942018-06-27 14:32:43 -07003736 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003737
Florin Coras6917b942018-11-13 22:44:54 -08003738 /* Dequeue all events and drop all unhandled io events */
3739 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3740 {
3741 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3742 vcl_handle_mq_event (wrk, e);
3743 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3744 }
3745 vec_reset_length (wrk->unhandled_evts_vector);
3746
Dave Wallace048b1d62018-01-03 22:24:41 -05003747 for (i = 0; i < n_sids; i++)
3748 {
Florin Coras7baeb712019-01-04 17:05:43 -08003749 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003750 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003751 {
3752 vp[i].revents = POLLHUP;
3753 num_ev++;
3754 continue;
3755 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003756
Florin Coras6917b942018-11-13 22:44:54 -08003757 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003758
3759 if (POLLIN & vp[i].events)
3760 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003761 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003762 if (rv > 0)
3763 {
Florin Coras6917b942018-11-13 22:44:54 -08003764 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003765 num_ev++;
3766 }
3767 else if (rv < 0)
3768 {
3769 switch (rv)
3770 {
3771 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003772 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003773 break;
3774
3775 default:
Florin Coras6917b942018-11-13 22:44:54 -08003776 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003777 break;
3778 }
3779 num_ev++;
3780 }
3781 }
3782
3783 if (POLLOUT & vp[i].events)
3784 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003785 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003786 if (rv > 0)
3787 {
Florin Coras6917b942018-11-13 22:44:54 -08003788 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003789 num_ev++;
3790 }
3791 else if (rv < 0)
3792 {
3793 switch (rv)
3794 {
3795 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003796 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003797 break;
3798
3799 default:
Florin Coras6917b942018-11-13 22:44:54 -08003800 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003801 break;
3802 }
3803 num_ev++;
3804 }
3805 }
3806
Dave Wallace7e607a72018-06-18 18:41:32 -04003807 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003808 {
Florin Coras6917b942018-11-13 22:44:54 -08003809 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003810 num_ev++;
3811 }
3812 }
3813 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003814 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003815 }
3816 while ((num_ev == 0) && keep_trying);
3817
Dave Wallace048b1d62018-01-03 22:24:41 -05003818 return num_ev;
3819}
3820
Florin Coras99368312018-08-02 10:45:44 -07003821int
3822vppcom_mq_epoll_fd (void)
3823{
Florin Coras134a9962018-08-28 11:32:04 -07003824 vcl_worker_t *wrk = vcl_worker_get_current ();
3825 return wrk->mqs_epfd;
3826}
3827
3828int
Florin Coras30e79c22019-01-02 19:31:22 -08003829vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003830{
3831 return session_handle & 0xFFFFFF;
3832}
3833
3834int
Florin Coras30e79c22019-01-02 19:31:22 -08003835vppcom_session_worker (vcl_session_handle_t session_handle)
3836{
3837 return session_handle >> 24;
3838}
3839
3840int
Florin Coras134a9962018-08-28 11:32:04 -07003841vppcom_worker_register (void)
3842{
Florin Coras47c40e22018-11-26 17:01:36 -08003843 if (!vcl_worker_alloc_and_init ())
3844 return VPPCOM_EEXIST;
3845
Florin Coras47c40e22018-11-26 17:01:36 -08003846 if (vcl_worker_register_with_vpp ())
3847 return VPPCOM_EEXIST;
3848
3849 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003850}
3851
Florin Coras369db832019-07-08 12:34:45 -07003852void
3853vppcom_worker_unregister (void)
3854{
3855 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3856 vcl_set_worker_index (~0);
3857}
3858
Pivo6017ff02020-05-04 17:57:33 +02003859void
3860vppcom_worker_index_set (int index)
3861{
3862 vcl_set_worker_index (index);
3863}
3864
Florin Corasdfe4cf42018-11-28 22:13:45 -08003865int
3866vppcom_worker_index (void)
3867{
3868 return vcl_get_worker_index ();
3869}
3870
Florin Corase0982e52019-01-25 13:19:56 -08003871int
3872vppcom_worker_mqs_epfd (void)
3873{
3874 vcl_worker_t *wrk = vcl_worker_get_current ();
3875 if (!vcm->cfg.use_mq_eventfd)
3876 return -1;
3877 return wrk->mqs_epfd;
3878}
3879
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003880int
3881vppcom_session_is_connectable_listener (uint32_t session_handle)
3882{
3883 vcl_session_t *session;
3884 vcl_worker_t *wrk = vcl_worker_get_current ();
3885 session = vcl_session_get_w_handle (wrk, session_handle);
3886 if (!session)
3887 return VPPCOM_EBADFD;
3888 return vcl_session_is_connectable_listener (wrk, session);
3889}
3890
3891int
3892vppcom_session_listener (uint32_t session_handle)
3893{
3894 vcl_worker_t *wrk = vcl_worker_get_current ();
3895 vcl_session_t *listen_session, *session;
3896 session = vcl_session_get_w_handle (wrk, session_handle);
3897 if (!session)
3898 return VPPCOM_EBADFD;
3899 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3900 return VPPCOM_EBADFD;
3901 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3902 if (!listen_session)
3903 return VPPCOM_EBADFD;
3904 return vcl_session_handle (listen_session);
3905}
3906
3907int
3908vppcom_session_n_accepted (uint32_t session_handle)
3909{
3910 vcl_worker_t *wrk = vcl_worker_get_current ();
3911 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3912 if (!session)
3913 return VPPCOM_EBADFD;
3914 return session->n_accepted_sessions;
3915}
3916
Florin Coras66ec4672020-06-15 07:59:40 -07003917const char *
3918vppcom_proto_str (vppcom_proto_t proto)
3919{
3920 char const *proto_str;
3921
3922 switch (proto)
3923 {
3924 case VPPCOM_PROTO_TCP:
3925 proto_str = "TCP";
3926 break;
3927 case VPPCOM_PROTO_UDP:
3928 proto_str = "UDP";
3929 break;
3930 case VPPCOM_PROTO_TLS:
3931 proto_str = "TLS";
3932 break;
3933 case VPPCOM_PROTO_QUIC:
3934 proto_str = "QUIC";
3935 break;
3936 default:
3937 proto_str = "UNKNOWN";
3938 break;
3939 }
3940 return proto_str;
3941}
3942
3943const char *
3944vppcom_retval_str (int retval)
3945{
3946 char const *st;
3947
3948 switch (retval)
3949 {
3950 case VPPCOM_OK:
3951 st = "VPPCOM_OK";
3952 break;
3953
3954 case VPPCOM_EAGAIN:
3955 st = "VPPCOM_EAGAIN";
3956 break;
3957
3958 case VPPCOM_EFAULT:
3959 st = "VPPCOM_EFAULT";
3960 break;
3961
3962 case VPPCOM_ENOMEM:
3963 st = "VPPCOM_ENOMEM";
3964 break;
3965
3966 case VPPCOM_EINVAL:
3967 st = "VPPCOM_EINVAL";
3968 break;
3969
3970 case VPPCOM_EBADFD:
3971 st = "VPPCOM_EBADFD";
3972 break;
3973
3974 case VPPCOM_EAFNOSUPPORT:
3975 st = "VPPCOM_EAFNOSUPPORT";
3976 break;
3977
3978 case VPPCOM_ECONNABORTED:
3979 st = "VPPCOM_ECONNABORTED";
3980 break;
3981
3982 case VPPCOM_ECONNRESET:
3983 st = "VPPCOM_ECONNRESET";
3984 break;
3985
3986 case VPPCOM_ENOTCONN:
3987 st = "VPPCOM_ENOTCONN";
3988 break;
3989
3990 case VPPCOM_ECONNREFUSED:
3991 st = "VPPCOM_ECONNREFUSED";
3992 break;
3993
3994 case VPPCOM_ETIMEDOUT:
3995 st = "VPPCOM_ETIMEDOUT";
3996 break;
3997
3998 default:
3999 st = "UNKNOWN_STATE";
4000 break;
4001 }
4002
4003 return st;
4004}
4005
Florin Corasa5a9efd2021-01-05 17:03:29 -08004006int
4007vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4008{
4009 if (vcm->cfg.vpp_app_socket_api)
4010 {
4011 clib_warning ("not supported");
4012 return VPPCOM_EINVAL;
4013 }
4014 return vcl_bapi_add_cert_key_pair (ckpair);
4015}
4016
4017int
4018vppcom_del_cert_key_pair (uint32_t ckpair_index)
4019{
4020 if (vcm->cfg.vpp_app_socket_api)
4021 {
4022 clib_warning ("not supported");
4023 return VPPCOM_EINVAL;
4024 }
4025 return vcl_bapi_del_cert_key_pair (ckpair_index);
4026}
4027
Dave Wallacee22aa742017-10-20 12:30:38 -04004028/*
4029 * fd.io coding-style-patch-verification: ON
4030 *
4031 * Local Variables:
4032 * eval: (c-set-style "gnu")
4033 * End:
4034 */