blob: cb07f5c764a537cb1718fab029c95efea6dc44da [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 Corasd85de682018-11-29 17:02:29 -080025static int
Florin Corasc4c4cf52019-08-24 18:17:34 -070026vcl_segment_is_not_mounted (vcl_worker_t * wrk, u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070027{
Florin Corasc4c4cf52019-08-24 18:17:34 -070028 u32 segment_index;
Florin Coras54693d22018-07-17 10:46:29 -070029
Florin Corasd85de682018-11-29 17:02:29 -080030 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
Florin Coras5f45e012019-01-23 09:21:30 -080031 return 0;
Florin Coras54693d22018-07-17 10:46:29 -070032
Florin Corasc4c4cf52019-08-24 18:17:34 -070033 segment_index = vcl_segment_table_lookup (segment_handle);
34 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
35 return 0;
36
Florin Corasd85de682018-11-29 17:02:29 -080037 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070038}
39
Florin Coras30e79c22019-01-02 19:31:22 -080040static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070041vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080042{
43 svm_msg_q_msg_t *msg;
44 u32 n_msgs;
45 int i;
46
Florin Corase003a1b2019-06-05 10:47:16 -070047 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
Florin Coras30e79c22019-01-02 19:31:22 -080048 for (i = 0; i < n_msgs; i++)
49 {
50 vec_add2 (wrk->mq_msg_vector, msg, 1);
51 svm_msg_q_sub_w_lock (mq, msg);
52 }
53 return n_msgs;
54}
55
Florin Coras697faea2018-06-27 17:10:49 -070056const char *
Florin Coras288eaab2019-02-03 15:26:14 -080057vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040058{
59 char *st;
60
61 switch (state)
62 {
Florin Corasc127d5a2020-10-14 16:35:58 -070063 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000064 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040065 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070066 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040067 st = "STATE_LISTEN";
68 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070069 case VCL_STATE_READY:
70 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040071 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070072 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080073 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050074 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070075 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040076 st = "STATE_DISCONNECT";
77 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070078 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000079 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040080 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070081 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080082 st = "STATE_UPDATED";
83 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070084 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080085 st = "STATE_LISTEN_NO_MQ";
86 break;
Dave Wallace543852a2017-08-03 02:11:34 -040087 default:
88 st = "UNKNOWN_STATE";
89 break;
90 }
91
92 return st;
93}
94
Dave Wallace543852a2017-08-03 02:11:34 -040095u8 *
96format_ip4_address (u8 * s, va_list * args)
97{
98 u8 *a = va_arg (*args, u8 *);
99 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
100}
101
102u8 *
103format_ip6_address (u8 * s, va_list * args)
104{
105 ip6_address_t *a = va_arg (*args, ip6_address_t *);
106 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
107
108 i_max_n_zero = ARRAY_LEN (a->as_u16);
109 max_n_zeros = 0;
110 i_first_zero = i_max_n_zero;
111 n_zeros = 0;
112 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
113 {
114 u32 is_zero = a->as_u16[i] == 0;
115 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
116 {
117 i_first_zero = i;
118 n_zeros = 0;
119 }
120 n_zeros += is_zero;
121 if ((!is_zero && n_zeros > max_n_zeros)
122 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
123 {
124 i_max_n_zero = i_first_zero;
125 max_n_zeros = n_zeros;
126 i_first_zero = ARRAY_LEN (a->as_u16);
127 n_zeros = 0;
128 }
129 }
130
131 last_double_colon = 0;
132 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
133 {
134 if (i == i_max_n_zero && max_n_zeros > 1)
135 {
136 s = format (s, "::");
137 i += max_n_zeros - 1;
138 last_double_colon = 1;
139 }
140 else
141 {
142 s = format (s, "%s%x",
143 (last_double_colon || i == 0) ? "" : ":",
144 clib_net_to_host_u16 (a->as_u16[i]));
145 last_double_colon = 0;
146 }
147 }
148
149 return s;
150}
151
152/* Format an IP46 address. */
153u8 *
154format_ip46_address (u8 * s, va_list * args)
155{
156 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
157 ip46_type_t type = va_arg (*args, ip46_type_t);
158 int is_ip4 = 1;
159
160 switch (type)
161 {
162 case IP46_TYPE_ANY:
163 is_ip4 = ip46_address_is_ip4 (ip46);
164 break;
165 case IP46_TYPE_IP4:
166 is_ip4 = 1;
167 break;
168 case IP46_TYPE_IP6:
169 is_ip4 = 0;
170 break;
171 }
172
173 return is_ip4 ?
174 format (s, "%U", format_ip4_address, &ip46->ip4) :
175 format (s, "%U", format_ip6_address, &ip46->ip6);
176}
177
Florin Coras697faea2018-06-27 17:10:49 -0700178/*
179 * VPPCOM Utility Functions
180 */
181
Florin Coras458089b2019-08-21 16:20:44 -0700182static void
183vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
184{
185 app_session_evt_t _app_evt, *app_evt = &_app_evt;
186 session_listen_msg_t *mp;
187 svm_msg_q_t *mq;
188
189 mq = vcl_worker_ctrl_mq (wrk);
190 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
191 mp = (session_listen_msg_t *) app_evt->evt->data;
192 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700193 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700194 mp->context = s->session_index;
195 mp->wrk_index = wrk->vpp_wrk_index;
196 mp->is_ip4 = s->transport.is_ip4;
197 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
198 mp->port = s->transport.lcl_port;
199 mp->proto = s->session_type;
Florin Coras1e966172020-05-16 18:18:14 +0000200 if (s->flags & VCL_SESSION_F_CONNECTED)
201 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700202 app_send_ctrl_evt_to_vpp (mq, app_evt);
203}
204
205static void
206vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
207{
208 app_session_evt_t _app_evt, *app_evt = &_app_evt;
209 session_connect_msg_t *mp;
210 svm_msg_q_t *mq;
211
212 mq = vcl_worker_ctrl_mq (wrk);
213 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
214 mp = (session_connect_msg_t *) app_evt->evt->data;
215 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700216 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700217 mp->context = s->session_index;
218 mp->wrk_index = wrk->vpp_wrk_index;
219 mp->is_ip4 = s->transport.is_ip4;
220 mp->parent_handle = s->parent_handle;
221 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700222 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700223 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000224 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700225 mp->proto = s->session_type;
Florin Coras0a1e1832020-03-29 18:54:04 +0000226 if (s->flags & VCL_SESSION_F_CONNECTED)
227 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700228 app_send_ctrl_evt_to_vpp (mq, app_evt);
229}
230
231void
232vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
233{
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_unlisten_msg_t *mp;
236 svm_msg_q_t *mq;
237
238 mq = vcl_worker_ctrl_mq (wrk);
239 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
240 mp = (session_unlisten_msg_t *) app_evt->evt->data;
241 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700242 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700243 mp->wrk_index = wrk->vpp_wrk_index;
244 mp->handle = s->vpp_handle;
245 mp->context = wrk->wrk_index;
246 app_send_ctrl_evt_to_vpp (mq, app_evt);
247}
248
249static void
250vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
251{
252 app_session_evt_t _app_evt, *app_evt = &_app_evt;
253 session_disconnect_msg_t *mp;
254 svm_msg_q_t *mq;
255
256 /* Send to thread that owns the session */
257 mq = s->vpp_evt_q;
258 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
259 mp = (session_disconnect_msg_t *) app_evt->evt->data;
260 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700261 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700262 mp->handle = s->vpp_handle;
263 app_send_ctrl_evt_to_vpp (mq, app_evt);
264}
265
266static void
267vcl_send_app_detach (vcl_worker_t * wrk)
268{
269 app_session_evt_t _app_evt, *app_evt = &_app_evt;
270 session_app_detach_msg_t *mp;
271 svm_msg_q_t *mq;
272
273 mq = vcl_worker_ctrl_mq (wrk);
274 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
275 mp = (session_app_detach_msg_t *) app_evt->evt->data;
276 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700277 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700278 app_send_ctrl_evt_to_vpp (mq, app_evt);
279}
Florin Coras697faea2018-06-27 17:10:49 -0700280
Florin Coras54693d22018-07-17 10:46:29 -0700281static void
282vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
283 session_handle_t handle, int retval)
284{
285 app_session_evt_t _app_evt, *app_evt = &_app_evt;
286 session_accepted_reply_msg_t *rmp;
287 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
288 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
289 rmp->handle = handle;
290 rmp->context = context;
291 rmp->retval = retval;
292 app_send_ctrl_evt_to_vpp (mq, app_evt);
293}
294
Florin Coras99368312018-08-02 10:45:44 -0700295static void
296vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
297 session_handle_t handle, int retval)
298{
299 app_session_evt_t _app_evt, *app_evt = &_app_evt;
300 session_disconnected_reply_msg_t *rmp;
301 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
302 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
303 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
304 rmp->handle = handle;
305 rmp->context = context;
306 rmp->retval = retval;
307 app_send_ctrl_evt_to_vpp (mq, app_evt);
308}
309
Florin Corasc9fbd662018-08-24 12:59:56 -0700310static void
311vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
312 session_handle_t handle, int retval)
313{
314 app_session_evt_t _app_evt, *app_evt = &_app_evt;
315 session_reset_reply_msg_t *rmp;
316 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
317 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
318 rmp->handle = handle;
319 rmp->context = context;
320 rmp->retval = retval;
321 app_send_ctrl_evt_to_vpp (mq, app_evt);
322}
323
Florin Coras30e79c22019-01-02 19:31:22 -0800324void
325vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
326 u32 wrk_index)
327{
328 app_session_evt_t _app_evt, *app_evt = &_app_evt;
329 session_worker_update_msg_t *mp;
330 svm_msg_q_t *mq;
331
332 mq = vcl_session_vpp_evt_q (wrk, s);
333 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
334 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700335 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800336 mp->handle = s->vpp_handle;
337 mp->req_wrk_index = wrk->vpp_wrk_index;
338 mp->wrk_index = wrk_index;
339 app_send_ctrl_evt_to_vpp (mq, app_evt);
340}
341
hanlina3a48962020-07-13 11:09:15 +0800342int
Florin Coras40c07ce2020-07-16 20:46:17 -0700343vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
344{
345 app_session_evt_t _app_evt, *app_evt = &_app_evt;
346 session_app_wrk_rpc_msg_t *mp;
347 vcl_worker_t *dst_wrk, *wrk;
348 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800349 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700350
351 if (data_len > sizeof (mp->data))
352 goto done;
353
354 clib_spinlock_lock (&vcm->workers_lock);
355
356 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
357 if (!dst_wrk)
358 goto done;
359
360 wrk = vcl_worker_get_current ();
361 mq = vcl_worker_ctrl_mq (wrk);
362 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
363 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700364 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700365 mp->wrk_index = dst_wrk->vpp_wrk_index;
366 clib_memcpy (mp->data, data, data_len);
367 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800368 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700369
370done:
371 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800372 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700373}
374
Florin Coras54693d22018-07-17 10:46:29 -0700375static u32
Florin Coras00cca802019-06-06 09:38:44 -0700376vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
377 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700378{
379 vcl_session_t *session, *listen_session;
380 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700381 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700382 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700383
Florin Coras134a9962018-08-28 11:32:04 -0700384 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700385
Florin Coras00cca802019-06-06 09:38:44 -0700386 listen_session = vcl_session_get (wrk, ls_index);
387 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700388 {
Florin Coras00cca802019-06-06 09:38:44 -0700389 VDBG (0, "ERROR: listener handle %lu does not match session %u",
390 mp->listener_handle, ls_index);
391 goto error;
392 }
393
Florin Corasc4c4cf52019-08-24 18:17:34 -0700394 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Coras00cca802019-06-06 09:38:44 -0700395 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700396 VDBG (0, "ERROR: segment for session %u is not mounted!",
Florin Coras00cca802019-06-06 09:38:44 -0700397 session->session_index);
398 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700399 }
400
Florin Coras54693d22018-07-17 10:46:29 -0700401 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
402 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Coras653e43f2019-03-04 10:56:23 -0800403 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
404 svm_msg_q_t *);
405 rx_fifo->client_session_index = session->session_index;
406 tx_fifo->client_session_index = session->session_index;
407 rx_fifo->client_thread_index = vcl_get_worker_index ();
408 tx_fifo->client_thread_index = vcl_get_worker_index ();
409 vpp_wrk_index = tx_fifo->master_thread_index;
410 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
411 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700412
413 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800414 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700415 session->rx_fifo = rx_fifo;
416 session->tx_fifo = tx_fifo;
417
Florin Corasdfffdd72020-10-15 10:54:47 -0700418 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700419 session->transport.rmt_port = mp->rmt.port;
420 session->transport.is_ip4 = mp->rmt.is_ip4;
421 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500422 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700423
Florin Coras134a9962018-08-28 11:32:04 -0700424 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700425 session->transport.lcl_port = listen_session->transport.lcl_port;
426 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700427 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200428 session->is_dgram = vcl_proto_is_dgram (session->session_type);
429 session->listener_index = listen_session->session_index;
430 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700431
Florin Coras5e062572019-03-14 19:07:51 -0700432 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
433 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700434 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
435 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
436 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700437 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
438
Florin Coras00cca802019-06-06 09:38:44 -0700439 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
440 session->vpp_handle, 0);
441
Florin Coras134a9962018-08-28 11:32:04 -0700442 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700443
444error:
445 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
446 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
447 VNET_API_ERROR_INVALID_ARGUMENT);
448 vcl_session_free (wrk, session);
449 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700450}
451
452static u32
Florin Coras134a9962018-08-28 11:32:04 -0700453vcl_session_connected_handler (vcl_worker_t * wrk,
454 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700455{
Florin Coras99368312018-08-02 10:45:44 -0700456 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700457 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700458 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700459
460 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700461 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700462 if (!session)
463 {
Florin Coras5e062572019-03-14 19:07:51 -0700464 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
465 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700466 return VCL_INVALID_SESSION_INDEX;
467 }
Florin Coras54693d22018-07-17 10:46:29 -0700468 if (mp->retval)
469 {
Florin Coras5e062572019-03-14 19:07:51 -0700470 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700471 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700472 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700473 session->vpp_handle = mp->handle;
474 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700475 }
476
Florin Corasdbc9c592019-09-25 16:37:43 -0700477 session->vpp_handle = mp->handle;
478 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
479 svm_msg_q_t *);
Florin Coras460dce62018-07-27 05:45:06 -0700480 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
481 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700482 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Corasd85de682018-11-29 17:02:29 -0800483 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700484 VDBG (0, "segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800485 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700486 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700487 vcl_send_session_disconnect (wrk, session);
488 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800489 }
490
Florin Coras460dce62018-07-27 05:45:06 -0700491 rx_fifo->client_session_index = session_index;
492 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700493 rx_fifo->client_thread_index = vcl_get_worker_index ();
494 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700495
Florin Coras653e43f2019-03-04 10:56:23 -0800496 vpp_wrk_index = tx_fifo->master_thread_index;
497 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
498 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700499
Florin Coras653e43f2019-03-04 10:56:23 -0800500 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700501 {
Florin Coras653e43f2019-03-04 10:56:23 -0800502 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
503 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700504 if (vcl_segment_is_not_mounted (wrk, mp->ct_segment_handle))
Florin Coras653e43f2019-03-04 10:56:23 -0800505 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700506 VDBG (0, "ct segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800507 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700508 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700509 vcl_send_session_disconnect (wrk, session);
510 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800511 }
Florin Coras99368312018-08-02 10:45:44 -0700512 }
Florin Coras54693d22018-07-17 10:46:29 -0700513
Florin Coras54693d22018-07-17 10:46:29 -0700514 session->rx_fifo = rx_fifo;
515 session->tx_fifo = tx_fifo;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800516 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras09d18c22019-04-24 11:10:02 -0700517 session->transport.is_ip4 = mp->lcl.is_ip4;
518 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500519 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700520 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700521
522 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700523 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700524 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700525 vcl_send_session_disconnect (wrk, session);
526 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700527 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700528
529 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800530 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700531
Florin Coras5e062572019-03-14 19:07:51 -0700532 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
533 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700534 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700535
Florin Coras54693d22018-07-17 10:46:29 -0700536 return session_index;
537}
538
Florin Coras3c7d4f92018-12-14 11:28:43 -0800539static int
540vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
541{
542 vcl_session_msg_t *accepted_msg;
543 int i;
544
545 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
546 {
547 accepted_msg = &session->accept_evts_fifo[i];
548 if (accepted_msg->accepted_msg.handle == handle)
549 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800550 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800551 return 1;
552 }
553 }
554 return 0;
555}
556
Florin Corasc9fbd662018-08-24 12:59:56 -0700557static u32
Florin Coras134a9962018-08-28 11:32:04 -0700558vcl_session_reset_handler (vcl_worker_t * wrk,
559 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700560{
561 vcl_session_t *session;
562 u32 sid;
563
Florin Coras134a9962018-08-28 11:32:04 -0700564 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
565 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700566 if (!session)
567 {
568 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
569 return VCL_INVALID_SESSION_INDEX;
570 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800571
572 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700573 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800574 {
575
576 if (!vcl_flag_accepted_session (session, reset_msg->handle,
577 VCL_ACCEPTED_F_RESET))
578 VDBG (0, "session was not accepted!");
579 return VCL_INVALID_SESSION_INDEX;
580 }
581
Florin Corasc127d5a2020-10-14 16:35:58 -0700582 if (session->session_state != VCL_STATE_CLOSED)
583 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800584 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700585 return sid;
586}
587
Florin Coras60116992018-08-27 09:52:18 -0700588static u32
Florin Coras134a9962018-08-28 11:32:04 -0700589vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700590{
591 vcl_session_t *session;
592 u32 sid = mp->context;
593
Florin Coras134a9962018-08-28 11:32:04 -0700594 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700595 if (mp->retval)
596 {
Florin Coras5e062572019-03-14 19:07:51 -0700597 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700598 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700599 if (session)
600 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700601 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700602 session->vpp_handle = mp->handle;
603 return sid;
604 }
605 else
606 {
Florin Coras5e062572019-03-14 19:07:51 -0700607 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
608 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700609 return VCL_INVALID_SESSION_INDEX;
610 }
611 }
612
613 session->vpp_handle = mp->handle;
614 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500615 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
616 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700617 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700618 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700619 session->session_state = VCL_STATE_LISTEN;
Florin Coras60116992018-08-27 09:52:18 -0700620
Florin Coras5f45e012019-01-23 09:21:30 -0800621 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
622 vec_validate (wrk->vpp_event_queues, 0);
623 wrk->vpp_event_queues[0] = session->vpp_evt_q;
624
Florin Coras6e3c1f82020-01-15 01:30:46 +0000625 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700626 {
627 svm_fifo_t *rx_fifo, *tx_fifo;
628 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
629 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
630 rx_fifo->client_session_index = sid;
631 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
632 tx_fifo->client_session_index = sid;
633 session->rx_fifo = rx_fifo;
634 session->tx_fifo = tx_fifo;
635 }
636
Florin Coras05ecfcc2018-12-12 18:19:39 -0800637 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700638 return sid;
639}
640
Florin Corasdfae9f92019-02-20 19:48:31 -0800641static void
642vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
643{
644 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
645 vcl_session_t *s;
646
647 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000648 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800649 {
650 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
651 return;
652 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700653 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000654 {
655 /* Connected udp listener */
656 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700657 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000658 return;
659
660 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
661 return;
662 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800663
664 if (mp->retval)
665 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700666 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800667
668 if (mp->context != wrk->wrk_index)
669 VDBG (0, "wrong context");
670
671 vcl_session_table_del_vpp_handle (wrk, mp->handle);
672 vcl_session_free (wrk, s);
673}
674
Florin Coras68b7e582020-01-21 18:33:23 -0800675static void
676vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
677{
678 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
679 vcl_session_t *s;
680
681 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
682 if (!s)
683 {
684 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
685 return;
686 }
687
688 s->vpp_thread_index = mp->vpp_thread_index;
Florin Coras57660d92020-04-04 22:45:34 +0000689 s->vpp_handle = mp->new_handle;
Florin Coras68b7e582020-01-21 18:33:23 -0800690 s->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
691
692 vec_validate (wrk->vpp_event_queues, s->vpp_thread_index);
693 wrk->vpp_event_queues[s->vpp_thread_index] = s->vpp_evt_q;
694
695 vcl_session_table_del_vpp_handle (wrk, mp->handle);
696 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
697
698 /* Generate new tx event if we have outstanding data */
699 if (svm_fifo_has_event (s->tx_fifo))
700 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
701 SESSION_IO_EVT_TX, SVM_Q_WAIT);
702
Florin Coras57660d92020-04-04 22:45:34 +0000703 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
704 s->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800705}
706
Florin Coras3c7d4f92018-12-14 11:28:43 -0800707static vcl_session_t *
708vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
709{
710 vcl_session_msg_t *vcl_msg;
711 vcl_session_t *session;
712
713 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
714 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800715 VWRN ("session overlap handle %lu state %u!", msg->handle,
716 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800717
718 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
719 if (!session)
720 {
721 VERR ("couldn't find listen session: listener handle %llx",
722 msg->listener_handle);
723 return 0;
724 }
725
726 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000727 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800728 vcl_msg->accepted_msg = *msg;
729 /* Session handle points to listener until fully accepted by app */
730 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
731
732 return session;
733}
734
735static vcl_session_t *
736vcl_session_disconnected_handler (vcl_worker_t * wrk,
737 session_disconnected_msg_t * msg)
738{
739 vcl_session_t *session;
740
741 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
742 if (!session)
743 {
744 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
745 return 0;
746 }
747
Florin Corasadcfb152020-02-12 08:50:29 +0000748 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700749 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000750 return 0;
751
Florin Coras3c7d4f92018-12-14 11:28:43 -0800752 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700753 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800754 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800755 if (!vcl_flag_accepted_session (session, msg->handle,
756 VCL_ACCEPTED_F_CLOSED))
757 VDBG (0, "session was not accepted!");
758 return 0;
759 }
760
Florin Corasadcfb152020-02-12 08:50:29 +0000761 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700762 if (session->session_state != VCL_STATE_DISCONNECT)
763 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000764
Florin Coras3c7d4f92018-12-14 11:28:43 -0800765 return session;
766}
767
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700768static int
769vppcom_session_disconnect (u32 session_handle)
770{
771 vcl_worker_t *wrk = vcl_worker_get_current ();
772 svm_msg_q_t *vpp_evt_q;
773 vcl_session_t *session, *listen_session;
774 vcl_session_state_t state;
775 u64 vpp_handle;
776
777 session = vcl_session_get_w_handle (wrk, session_handle);
778 if (!session)
779 return VPPCOM_EBADFD;
780
781 vpp_handle = session->vpp_handle;
782 state = session->session_state;
783
784 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
785 vpp_handle, state, vppcom_session_state_str (state));
786
787 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
788 {
789 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
790 return VPPCOM_EBADFD;
791 }
792
793 if (state == VCL_STATE_VPP_CLOSING)
794 {
795 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
796 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->api_client_handle,
797 vpp_handle, 0);
798 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
799 session->session_index, vpp_handle);
800 }
801 else
802 {
803 /* Session doesn't have an event queue yet. Probably a non-blocking
804 * connect. Wait for the reply */
805 if (PREDICT_FALSE (!session->vpp_evt_q))
806 return VPPCOM_OK;
807
808 VDBG (1, "session %u [0x%llx]: sending disconnect...",
809 session->session_index, vpp_handle);
810 vcl_send_session_disconnect (wrk, session);
811 }
812
813 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
814 {
815 listen_session = vcl_session_get (wrk, session->listener_index);
816 listen_session->n_accepted_sessions--;
817 }
818
819 return VPPCOM_OK;
820}
821
Florin Coras30e79c22019-01-02 19:31:22 -0800822static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700823vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
824{
825 session_cleanup_msg_t *msg;
826 vcl_session_t *session;
827
828 msg = (session_cleanup_msg_t *) data;
829 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
830 if (!session)
831 {
832 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
833 return;
834 }
835
Florin Coras36d49392020-04-24 23:00:11 +0000836 if (msg->type == SESSION_CLEANUP_TRANSPORT)
837 {
838 /* Transport was cleaned up before we confirmed close. Probably the
839 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700840 * Confirm close to make sure everything is cleaned up.
841 * Move to undetermined state to ensure that the session is not
842 * removed before both vpp and the app cleanup.
843 * - If the app closes first, the session is moved to CLOSED state
844 * and the session cleanup notification from vpp removes the
845 * session.
846 * - If vpp cleans up the session first, the session is moved to
847 * DETACHED state lower and subsequently the close from the app
848 * frees the session
849 */
850 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700851 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700852 vppcom_session_disconnect (vcl_session_handle (session));
853 session->session_state = VCL_STATE_UPDATED;
854 }
855 else if (session->session_state == VCL_STATE_DISCONNECT)
856 {
857 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
858 wrk->api_client_handle,
859 session->vpp_handle, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700860 session->session_state = VCL_STATE_UPDATED;
861 }
Florin Coras36d49392020-04-24 23:00:11 +0000862 return;
863 }
864
Florin Coras9ace36d2019-10-28 13:14:17 -0700865 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000866 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700867 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000868 {
869 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700870 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000871 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
872 return;
873 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700874 vcl_session_free (wrk, session);
875}
876
877static void
Florin Coras30e79c22019-01-02 19:31:22 -0800878vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
879{
880 session_req_worker_update_msg_t *msg;
881 vcl_session_t *s;
882
883 msg = (session_req_worker_update_msg_t *) data;
884 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
885 if (!s)
886 return;
887
888 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
889}
890
891static void
892vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
893{
894 session_worker_update_reply_msg_t *msg;
895 vcl_session_t *s;
896
897 msg = (session_worker_update_reply_msg_t *) data;
898 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
899 if (!s)
900 {
901 VDBG (0, "unknown handle 0x%llx", msg->handle);
902 return;
903 }
Florin Corasc4c4cf52019-08-24 18:17:34 -0700904 if (vcl_segment_is_not_mounted (wrk, msg->segment_handle))
Florin Coras30e79c22019-01-02 19:31:22 -0800905 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700906 clib_warning ("segment for session %u is not mounted!",
Florin Coras30e79c22019-01-02 19:31:22 -0800907 s->session_index);
908 return;
909 }
Florin Coras30e79c22019-01-02 19:31:22 -0800910
Florin Coras5f45e012019-01-23 09:21:30 -0800911 if (s->rx_fifo)
912 {
913 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
914 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
915 s->rx_fifo->client_session_index = s->session_index;
916 s->tx_fifo->client_session_index = s->session_index;
917 s->rx_fifo->client_thread_index = wrk->wrk_index;
918 s->tx_fifo->client_thread_index = wrk->wrk_index;
919 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700920 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800921
Florin Coras30e79c22019-01-02 19:31:22 -0800922 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
923 s->vpp_handle, wrk->wrk_index);
924}
925
Florin Coras935ce752020-09-08 22:43:47 -0700926static int
927vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
928{
929
930 if (vcm->cfg.vpp_app_socket_api)
931 return vcl_sapi_recv_fds (wrk, fds, n_fds);
932
933 return vcl_bapi_recv_fds (wrk, fds, n_fds);
934}
935
Florin Corasc4c4cf52019-08-24 18:17:34 -0700936static void
937vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
938{
939 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
940 session_app_add_segment_msg_t *msg;
941 u64 segment_handle;
942 int fd = -1;
943
944 msg = (session_app_add_segment_msg_t *) data;
945
946 if (msg->fd_flags)
947 {
Florin Coras935ce752020-09-08 22:43:47 -0700948 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700949 seg_type = SSVM_SEGMENT_MEMFD;
950 }
951
952 segment_handle = msg->segment_handle;
953 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
954 {
955 clib_warning ("invalid segment handle");
956 return;
957 }
958
959 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
960 seg_type, fd))
961 {
962 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
963 return;
964 }
965
966 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
967 msg->segment_size);
968}
969
970static void
971vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
972{
973 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
974 vcl_segment_detach (msg->segment_handle);
975 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
976}
977
Florin Coras40c07ce2020-07-16 20:46:17 -0700978static void
979vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
980{
981 if (!vcm->wrk_rpc_fn)
982 return;
983
hanlina3a48962020-07-13 11:09:15 +0800984 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700985}
986
Florin Coras86f04502018-09-12 16:08:01 -0700987static int
988vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700989{
Florin Coras54693d22018-07-17 10:46:29 -0700990 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700991 session_connected_msg_t *connected_msg;
992 session_reset_msg_t *reset_msg;
993 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700994 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700995 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700996
997 switch (e->event_type)
998 {
Florin Coras653e43f2019-03-04 10:56:23 -0800999 case SESSION_IO_EVT_RX:
1000 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001001 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001002 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001003 break;
Florin Coras86f04502018-09-12 16:08:01 -07001004 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001005 break;
Florin Corasb242d312020-10-26 15:35:40 -07001006 case SESSION_CTRL_EVT_BOUND:
1007 /* We can only wait for only one listen so not postponed */
1008 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1009 break;
Florin Coras54693d22018-07-17 10:46:29 -07001010 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001011 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1012 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1013 {
1014 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1015 *ecpy = *e;
1016 ecpy->postponed = 1;
1017 ecpy->session_index = s->session_index;
1018 }
Florin Coras54693d22018-07-17 10:46:29 -07001019 break;
1020 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001021 connected_msg = (session_connected_msg_t *) e->data;
1022 sid = vcl_session_connected_handler (wrk, connected_msg);
1023 if (!(s = vcl_session_get (wrk, sid)))
1024 break;
1025 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1026 {
1027 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1028 *ecpy = *e;
1029 ecpy->postponed = 1;
1030 ecpy->session_index = s->session_index;
1031 }
Florin Coras54693d22018-07-17 10:46:29 -07001032 break;
1033 case SESSION_CTRL_EVT_DISCONNECTED:
1034 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001035 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1036 break;
1037 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1038 {
1039 vec_add1 (wrk->unhandled_evts_vector, *e);
1040 break;
1041 }
1042 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001043 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001044 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1045 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001046 break;
1047 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001048 reset_msg = (session_reset_msg_t *) e->data;
1049 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1050 break;
1051 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1052 {
1053 vec_add1 (wrk->unhandled_evts_vector, *e);
1054 break;
1055 }
Florin Coras134a9962018-08-28 11:32:04 -07001056 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001057 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001058 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1059 vcl_session_unlisten_reply_handler (wrk, e->data);
1060 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001061 case SESSION_CTRL_EVT_MIGRATED:
1062 vcl_session_migrated_handler (wrk, e->data);
1063 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001064 case SESSION_CTRL_EVT_CLEANUP:
1065 vcl_session_cleanup_handler (wrk, e->data);
1066 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001067 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1068 vcl_session_req_worker_update_handler (wrk, e->data);
1069 break;
1070 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1071 vcl_session_worker_update_reply_handler (wrk, e->data);
1072 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001073 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1074 vcl_session_app_add_segment_handler (wrk, e->data);
1075 break;
1076 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1077 vcl_session_app_del_segment_handler (wrk, e->data);
1078 break;
hanlina3a48962020-07-13 11:09:15 +08001079 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001080 vcl_worker_rpc_handler (wrk, e->data);
1081 break;
Florin Coras54693d22018-07-17 10:46:29 -07001082 default:
1083 clib_warning ("unhandled %u", e->event_type);
1084 }
1085 return VPPCOM_OK;
1086}
1087
Florin Coras30e79c22019-01-02 19:31:22 -08001088static int
Florin Coras697faea2018-06-27 17:10:49 -07001089vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001090 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001091 f64 wait_for_time)
1092{
Florin Coras134a9962018-08-28 11:32:04 -07001093 vcl_worker_t *wrk = vcl_worker_get_current ();
1094 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001095 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001096 svm_msg_q_msg_t msg;
1097 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001098
Florin Coras697faea2018-06-27 17:10:49 -07001099 do
Dave Wallace543852a2017-08-03 02:11:34 -04001100 {
Florin Coras134a9962018-08-28 11:32:04 -07001101 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001102 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001103 {
Florin Coras070453d2018-08-24 17:04:27 -07001104 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001105 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001106 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001107 {
Florin Coras697faea2018-06-27 17:10:49 -07001108 return VPPCOM_OK;
1109 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001110 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001111 {
Florin Coras697faea2018-06-27 17:10:49 -07001112 return VPPCOM_ECONNREFUSED;
1113 }
Florin Coras54693d22018-07-17 10:46:29 -07001114
Florin Coras134a9962018-08-28 11:32:04 -07001115 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001116 {
1117 usleep (100);
1118 continue;
1119 }
Florin Coras134a9962018-08-28 11:32:04 -07001120 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001121 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001122 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001123 }
Florin Coras134a9962018-08-28 11:32:04 -07001124 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001125
Florin Coras05ecfcc2018-12-12 18:19:39 -08001126 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001127 vppcom_session_state_str (state));
1128 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001129
Florin Coras697faea2018-06-27 17:10:49 -07001130 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001131}
1132
Florin Coras30e79c22019-01-02 19:31:22 -08001133static void
1134vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1135{
Florin Coras288eaab2019-02-03 15:26:14 -08001136 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001137 vcl_session_t *s;
1138 u32 *sip;
1139
1140 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1141 return;
1142
1143 vec_foreach (sip, wrk->pending_session_wrk_updates)
1144 {
1145 s = vcl_session_get (wrk, *sip);
1146 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1147 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001148 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1149 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001150 s->session_state = state;
1151 }
1152 vec_reset_length (wrk->pending_session_wrk_updates);
1153}
1154
Florin Corasf9240dc2019-01-15 08:03:17 -08001155void
Florin Coras30e79c22019-01-02 19:31:22 -08001156vcl_flush_mq_events (void)
1157{
1158 vcl_worker_t *wrk = vcl_worker_get_current ();
1159 svm_msg_q_msg_t *msg;
1160 session_event_t *e;
1161 svm_msg_q_t *mq;
1162 int i;
1163
1164 mq = wrk->app_event_queue;
1165 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -07001166 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001167 svm_msg_q_unlock (mq);
1168
1169 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1170 {
1171 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1172 e = svm_msg_q_msg_data (mq, msg);
1173 vcl_handle_mq_event (wrk, e);
1174 svm_msg_q_free_msg (mq, msg);
1175 }
1176 vec_reset_length (wrk->mq_msg_vector);
1177 vcl_handle_pending_wrk_updates (wrk);
1178}
1179
Florin Coras697faea2018-06-27 17:10:49 -07001180static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001181vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001182{
Florin Coras134a9962018-08-28 11:32:04 -07001183 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001184 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001185 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001186 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001187
Florin Corasab2f6db2018-08-31 14:31:41 -07001188 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001189 if (!session)
1190 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001191
Florin Corasaaff5ee2019-07-08 13:00:00 -07001192 /* Flush pending accept events, if any */
1193 while (clib_fifo_elts (session->accept_evts_fifo))
1194 {
1195 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1196 accepted_msg = &evt->accepted_msg;
1197 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1198 vcl_send_session_accepted_reply (session->vpp_evt_q,
1199 accepted_msg->context,
1200 session->vpp_handle, -1);
1201 }
1202 clib_fifo_free (session->accept_evts_fifo);
1203
Florin Coras458089b2019-08-21 16:20:44 -07001204 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001205
Florin Coras5e062572019-03-14 19:07:51 -07001206 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001207 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001208 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001209
1210 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001211 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001212
Florin Coras070453d2018-08-24 17:04:27 -07001213 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001214}
1215
Florin Coras940f78f2018-11-30 12:11:20 -08001216/**
1217 * Handle app exit
1218 *
1219 * Notify vpp of the disconnect and mark the worker as free. If we're the
1220 * last worker, do a full cleanup otherwise, since we're probably a forked
1221 * child, avoid syscalls as much as possible. We might've lost privileges.
1222 */
1223void
1224vppcom_app_exit (void)
1225{
1226 if (!pool_elts (vcm->workers))
1227 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001228 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1229 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001230 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001231}
1232
Florin Coras935ce752020-09-08 22:43:47 -07001233static int
1234vcl_api_attach (void)
1235{
1236 if (vcm->cfg.vpp_app_socket_api)
1237 return vcl_sapi_attach ();
1238
1239 return vcl_bapi_attach ();
1240}
1241
1242static void
1243vcl_api_detach (vcl_worker_t * wrk)
1244{
1245 vcl_send_app_detach (wrk);
1246
1247 if (vcm->cfg.vpp_app_socket_api)
1248 return vcl_sapi_detach (wrk);
1249
1250 return vcl_bapi_disconnect_from_vpp ();
1251}
1252
Dave Wallace543852a2017-08-03 02:11:34 -04001253/*
1254 * VPPCOM Public API functions
1255 */
1256int
Florin Coras66ec4672020-06-15 07:59:40 -07001257vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001258{
Dave Wallace543852a2017-08-03 02:11:34 -04001259 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001260 int rv;
1261
Florin Coras47c40e22018-11-26 17:01:36 -08001262 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001263 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001264 VDBG (1, "already initialized");
1265 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001266 }
1267
Florin Coras47c40e22018-11-26 17:01:36 -08001268 vcm->is_init = 1;
1269 vppcom_cfg (&vcm->cfg);
1270 vcl_cfg = &vcm->cfg;
1271
1272 vcm->main_cpu = pthread_self ();
1273 vcm->main_pid = getpid ();
1274 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001275 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1276 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001277 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1278 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001279 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001280 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001281 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001282
1283 /* Allocate default worker */
1284 vcl_worker_alloc_and_init ();
1285
Florin Coras935ce752020-09-08 22:43:47 -07001286 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001287 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001288
1289 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001290 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001291
1292 return VPPCOM_OK;
1293}
1294
1295void
1296vppcom_app_destroy (void)
1297{
Florin Corasdc0ded72020-04-30 02:59:55 +00001298 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001299 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001300
Florin Coras940f78f2018-11-30 12:11:20 -08001301 if (!pool_elts (vcm->workers))
1302 return;
1303
Florin Coras0d427d82018-06-27 03:24:07 -07001304 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001305
Florin Corasdc0ded72020-04-30 02:59:55 +00001306 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001307
Florin Corasce815de2020-04-16 18:47:27 +00001308 /* *INDENT-OFF* */
1309 pool_foreach (wrk, vcm->workers, ({
Florin Corasdc0ded72020-04-30 02:59:55 +00001310 if (current_wrk != wrk)
1311 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Florin Corasce815de2020-04-16 18:47:27 +00001312 }));
1313 /* *INDENT-ON* */
1314
Florin Coras935ce752020-09-08 22:43:47 -07001315 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001316 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1317
Florin Coras0d427d82018-06-27 03:24:07 -07001318 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001319
1320 /*
1321 * Free the heap and fix vcm
1322 */
1323 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001324 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001325
1326 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001327 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001328}
1329
1330int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001331vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001332{
Florin Coras134a9962018-08-28 11:32:04 -07001333 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001334 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001335
Florin Coras134a9962018-08-28 11:32:04 -07001336 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001337
Florin Coras7e12d942018-06-27 14:32:43 -07001338 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001339 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001340 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001341 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001342
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001343 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001344 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001345
Florin Coras7e12d942018-06-27 14:32:43 -07001346 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1347 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001348
Florin Coras5e062572019-03-14 19:07:51 -07001349 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001350
Florin Coras134a9962018-08-28 11:32:04 -07001351 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001352}
1353
1354int
Florin Corasc127d5a2020-10-14 16:35:58 -07001355vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001356 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001357{
Florin Coras134a9962018-08-28 11:32:04 -07001358 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001359
Florin Coras6c3b2182020-10-19 18:36:48 -07001360 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001361
Florin Coras6c3b2182020-10-19 18:36:48 -07001362 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001363 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001364 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001365 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001366 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001367 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001368 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001369 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001370 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1371 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001372 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001373 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001374 goto cleanup;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001375 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001376
Florin Coras6c3b2182020-10-19 18:36:48 -07001377 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001378 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001379 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001380 if (rv < 0)
1381 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001382 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1383 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001384 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001385
Florin Coras9ace36d2019-10-28 13:14:17 -07001386 if (!do_disconnect)
1387 {
1388 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001389 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001390 goto cleanup;
1391 }
1392
Florin Coras6c3b2182020-10-19 18:36:48 -07001393 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001394 {
1395 rv = vppcom_session_unbind (sh);
1396 if (PREDICT_FALSE (rv < 0))
1397 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001398 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001399 vppcom_retval_str (rv));
1400 return rv;
1401 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001402 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001403 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001404 {
1405 rv = vppcom_session_disconnect (sh);
1406 if (PREDICT_FALSE (rv < 0))
1407 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001408 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001409 rv, vppcom_retval_str (rv));
1410 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001411 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001412 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001413 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, s);
Florin Corascc7c88e2020-09-15 15:56:51 -07001414 vcl_send_session_reset_reply (mq, wrk->api_client_handle,
Florin Corasc127d5a2020-10-14 16:35:58 -07001415 s->vpp_handle, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001416 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001417 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001418 {
1419 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001420 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001421 goto free_session;
1422 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001423
Florin Corasc127d5a2020-10-14 16:35:58 -07001424 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001425
Florin Coras9ace36d2019-10-28 13:14:17 -07001426 /* Session is removed only after vpp confirms the disconnect */
1427 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001428
Florin Corasf9240dc2019-01-15 08:03:17 -08001429cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001430 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001431free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001432 vcl_session_free (wrk, s);
1433 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001434
Dave Wallace543852a2017-08-03 02:11:34 -04001435 return rv;
1436}
1437
1438int
Florin Corasf9240dc2019-01-15 08:03:17 -08001439vppcom_session_close (uint32_t session_handle)
1440{
1441 vcl_worker_t *wrk = vcl_worker_get_current ();
1442 vcl_session_t *session;
1443
1444 session = vcl_session_get_w_handle (wrk, session_handle);
1445 if (!session)
1446 return VPPCOM_EBADFD;
1447 return vcl_session_cleanup (wrk, session, session_handle,
1448 1 /* do_disconnect */ );
1449}
1450
1451int
Florin Coras134a9962018-08-28 11:32:04 -07001452vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001453{
Florin Coras134a9962018-08-28 11:32:04 -07001454 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001455 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001456
1457 if (!ep || !ep->ip)
1458 return VPPCOM_EINVAL;
1459
Florin Coras134a9962018-08-28 11:32:04 -07001460 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001461 if (!session)
1462 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001463
Florin Coras6c3b2182020-10-19 18:36:48 -07001464 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001465 {
Florin Coras5e062572019-03-14 19:07:51 -07001466 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1467 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001468 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001469 }
1470
Florin Coras7e12d942018-06-27 14:32:43 -07001471 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001472 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001473 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1474 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001475 else
Dave Barach178cf492018-11-13 16:34:13 -05001476 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1477 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001478 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001479
Florin Coras5e062572019-03-14 19:07:51 -07001480 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1481 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001482 session->transport.is_ip4 ? "IPv4" : "IPv6",
1483 format_ip46_address, &session->transport.lcl_ip,
1484 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1485 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001486 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001487 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001488
1489 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001490 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001491
Florin Coras070453d2018-08-24 17:04:27 -07001492 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001493}
1494
1495int
Florin Coras134a9962018-08-28 11:32:04 -07001496vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001497{
Florin Coras134a9962018-08-28 11:32:04 -07001498 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001499 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001500 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001501 int rv;
1502
Florin Coras134a9962018-08-28 11:32:04 -07001503 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001504 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001505 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001506
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001507 if (q_len == 0 || q_len == ~0)
1508 q_len = vcm->cfg.listen_queue_size;
1509
Dave Wallaceee45d412017-11-24 21:44:06 -05001510 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001511 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001512 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001513 VDBG (0, "session %u [0x%llx]: already in listen state!",
1514 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001515 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001516 }
1517
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001518 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001519
Florin Coras070453d2018-08-24 17:04:27 -07001520 /*
1521 * Send listen request to vpp and wait for reply
1522 */
Florin Coras458089b2019-08-21 16:20:44 -07001523 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001524 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001525 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001526 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001527
Florin Coras070453d2018-08-24 17:04:27 -07001528 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001529 {
Florin Coras134a9962018-08-28 11:32:04 -07001530 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001531 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1532 listen_sh, listen_session->vpp_handle, rv,
1533 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001534 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001535 }
1536
Florin Coras070453d2018-08-24 17:04:27 -07001537 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001538}
1539
Florin Coras134a9962018-08-28 11:32:04 -07001540static int
Florin Coras5e062572019-03-14 19:07:51 -07001541validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001542{
Florin Coras6c3b2182020-10-19 18:36:48 -07001543 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001544 {
Florin Coras5e062572019-03-14 19:07:51 -07001545 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1546 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001547 return VPPCOM_EBADFD;
1548 }
1549
Florin Corasc127d5a2020-10-14 16:35:58 -07001550 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001551 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001552 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001553 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001554 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001555 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001556 return VPPCOM_EBADFD;
1557 }
1558 return VPPCOM_OK;
1559}
1560
1561int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001562vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1563{
1564 if (!strcmp (proto_str, "TCP"))
1565 *proto = VPPCOM_PROTO_TCP;
1566 else if (!strcmp (proto_str, "tcp"))
1567 *proto = VPPCOM_PROTO_TCP;
1568 else if (!strcmp (proto_str, "UDP"))
1569 *proto = VPPCOM_PROTO_UDP;
1570 else if (!strcmp (proto_str, "udp"))
1571 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001572 else if (!strcmp (proto_str, "TLS"))
1573 *proto = VPPCOM_PROTO_TLS;
1574 else if (!strcmp (proto_str, "tls"))
1575 *proto = VPPCOM_PROTO_TLS;
1576 else if (!strcmp (proto_str, "QUIC"))
1577 *proto = VPPCOM_PROTO_QUIC;
1578 else if (!strcmp (proto_str, "quic"))
1579 *proto = VPPCOM_PROTO_QUIC;
1580 else
1581 return 1;
1582 return 0;
1583}
1584
1585int
Florin Coras134a9962018-08-28 11:32:04 -07001586vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001587 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001588{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001589 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001590 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001591 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001592 vcl_session_t *listen_session = 0;
1593 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001594 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001595 svm_msg_q_msg_t msg;
1596 session_event_t *e;
1597 u8 is_nonblocking;
1598 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001599
Florin Coras134a9962018-08-28 11:32:04 -07001600 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001601 if (!listen_session)
1602 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001603
Florin Coras134a9962018-08-28 11:32:04 -07001604 listen_session_index = listen_session->session_index;
1605 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001606 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001607
Florin Coras54693d22018-07-17 10:46:29 -07001608 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001609 {
Florin Coras54693d22018-07-17 10:46:29 -07001610 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001611 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001612 accepted_msg = evt->accepted_msg;
1613 goto handle;
1614 }
1615
Florin Corasac422d62020-10-19 20:51:36 -07001616 is_nonblocking = vcl_session_has_attr (listen_session,
1617 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001618 while (1)
1619 {
Carl Smith592a9092019-11-12 14:57:37 +13001620 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1621 return VPPCOM_EAGAIN;
1622
Florin Coras134a9962018-08-28 11:32:04 -07001623 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001624 return VPPCOM_EAGAIN;
1625
Florin Coras134a9962018-08-28 11:32:04 -07001626 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001627 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001628 {
wanghanlin96453fd2019-12-16 19:14:39 +08001629 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001630 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001631 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001632 }
Dave Barach178cf492018-11-13 16:34:13 -05001633 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001634 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001635 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001636 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001637
Florin Coras54693d22018-07-17 10:46:29 -07001638handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001639
Florin Coras00cca802019-06-06 09:38:44 -07001640 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1641 listen_session_index);
1642 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1643 return VPPCOM_ECONNABORTED;
1644
Florin Coras134a9962018-08-28 11:32:04 -07001645 listen_session = vcl_session_get (wrk, listen_session_index);
1646 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001647
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001648 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001649 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001650
Florin Coras5e062572019-03-14 19:07:51 -07001651 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1652 " flags %d, is_nonblocking %u", listen_session->session_index,
1653 listen_session->vpp_handle, client_session_index,
1654 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001655 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001656
Dave Wallace048b1d62018-01-03 22:24:41 -05001657 if (ep)
1658 {
Florin Coras7e12d942018-06-27 14:32:43 -07001659 ep->is_ip4 = client_session->transport.is_ip4;
1660 ep->port = client_session->transport.rmt_port;
1661 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001662 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1663 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001664 else
Dave Barach178cf492018-11-13 16:34:13 -05001665 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1666 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001667 }
Dave Wallace60caa062017-11-10 17:07:13 -05001668
Florin Coras05ecfcc2018-12-12 18:19:39 -08001669 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001670 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001671 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001672 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001673 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001674 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001675 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001676 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001677 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001678 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1679 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001680
Florin Coras3c7d4f92018-12-14 11:28:43 -08001681 /*
1682 * Session might have been closed already
1683 */
1684 if (accept_flags)
1685 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001686 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001687 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001688 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001689 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001690 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001691 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001692}
1693
1694int
Florin Coras134a9962018-08-28 11:32:04 -07001695vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001696{
Florin Coras134a9962018-08-28 11:32:04 -07001697 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001698 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001699 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001700 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001701
Florin Coras134a9962018-08-28 11:32:04 -07001702 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001703 if (!session)
1704 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001705 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001706
Florin Coras6c3b2182020-10-19 18:36:48 -07001707 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001708 {
Florin Coras5e062572019-03-14 19:07:51 -07001709 VDBG (0, "ERROR: cannot connect epoll session %u!",
1710 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001711 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001712 }
1713
Florin Corasc127d5a2020-10-14 16:35:58 -07001714 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001715 {
Florin Coras7baeb712019-01-04 17:05:43 -08001716 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001717 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001718 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001719 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001720 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001721 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001722 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001723 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001724 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001725 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001726 }
1727
Florin Coras0a1e1832020-03-29 18:54:04 +00001728 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001729 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001730 {
1731 if (session->session_type != VPPCOM_PROTO_UDP)
1732 return VPPCOM_EINVAL;
1733 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001734 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001735 }
1736
Florin Coras7e12d942018-06-27 14:32:43 -07001737 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001738 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001739 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001740 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001741 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001742
Florin Coras0a1e1832020-03-29 18:54:04 +00001743 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001744 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001745 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001746 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001747 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001748 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001749 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001750 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001751 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001752
Florin Coras458089b2019-08-21 16:20:44 -07001753 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001754
Florin Corasac422d62020-10-19 20:51:36 -07001755 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001756 {
fanyfa49d59d2020-10-13 17:07:16 +08001757 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001758 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001759 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001760 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001761 return VPPCOM_EINPROGRESS;
1762 }
Florin Coras57c88932019-08-29 12:03:17 -07001763
1764 /*
1765 * Wait for reply from vpp if blocking
1766 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001767 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001768 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001769
Florin Coras134a9962018-08-28 11:32:04 -07001770 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001771 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1772 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001773
Dave Wallace4878cbe2017-11-21 03:45:09 -05001774 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001775}
1776
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001777int
1778vppcom_session_stream_connect (uint32_t session_handle,
1779 uint32_t parent_session_handle)
1780{
1781 vcl_worker_t *wrk = vcl_worker_get_current ();
1782 vcl_session_t *session, *parent_session;
1783 u32 session_index, parent_session_index;
1784 int rv;
1785
1786 session = vcl_session_get_w_handle (wrk, session_handle);
1787 if (!session)
1788 return VPPCOM_EBADFD;
1789 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1790 if (!parent_session)
1791 return VPPCOM_EBADFD;
1792
1793 session_index = session->session_index;
1794 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001795 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001796 {
1797 VDBG (0, "ERROR: cannot connect epoll session %u!",
1798 session->session_index);
1799 return VPPCOM_EBADFD;
1800 }
1801
Florin Corasc127d5a2020-10-14 16:35:58 -07001802 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001803 {
1804 VDBG (0, "session handle %u [0x%llx]: session already "
1805 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1806 session_handle, session->vpp_handle,
1807 parent_session_handle, parent_session->vpp_handle,
1808 vppcom_proto_str (session->session_type), session->session_state,
1809 vppcom_session_state_str (session->session_state));
1810 return VPPCOM_OK;
1811 }
1812
1813 /* Connect to quic session specifics */
1814 session->transport.is_ip4 = parent_session->transport.is_ip4;
1815 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1816 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001817 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001818
1819 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1820 session_handle, parent_session_handle, parent_session->vpp_handle);
1821
1822 /*
1823 * Send connect request and wait for reply from vpp
1824 */
Florin Coras458089b2019-08-21 16:20:44 -07001825 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001826 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001827 vcm->cfg.session_timeout);
1828
1829 session->listener_index = parent_session_index;
1830 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001831 if (parent_session)
1832 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001833
1834 session = vcl_session_get (wrk, session_index);
1835 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1836 session->vpp_handle, rv ? "failed" : "succeeded");
1837
1838 return rv;
1839}
1840
Florin Coras54693d22018-07-17 10:46:29 -07001841static u8
1842vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1843{
Florin Corasc0737e92019-03-04 14:19:39 -08001844 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001845}
1846
Steven58f464e2017-10-25 12:33:12 -07001847static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001848vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001849 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001850{
Florin Coras134a9962018-08-28 11:32:04 -07001851 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001852 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001853 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001854 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001855 svm_msg_q_msg_t msg;
1856 session_event_t *e;
1857 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001858 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001859
Florin Coras070453d2018-08-24 17:04:27 -07001860 if (PREDICT_FALSE (!buf))
1861 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001862
Florin Coras134a9962018-08-28 11:32:04 -07001863 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001864 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001865 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001866
Florin Coras6d0106e2019-01-29 20:11:58 -08001867 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001868 {
Florin Coras5e062572019-03-14 19:07:51 -07001869 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001870 s->session_index, s->vpp_handle, s->session_state,
1871 vppcom_session_state_str (s->session_state));
1872 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001873 }
1874
Florin Corasac422d62020-10-19 20:51:36 -07001875 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001876 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001877 mq = wrk->app_event_queue;
1878 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001879 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001880
Sirshak Das28aa5392019-02-05 01:33:33 -06001881 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001882 {
Florin Coras54693d22018-07-17 10:46:29 -07001883 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001884 {
Yu Pingb2955352019-12-04 06:49:04 +08001885 if (vcl_session_is_closing (s))
1886 return vcl_session_closing_error (s);
Florin Corasc0737e92019-03-04 14:19:39 -08001887 svm_fifo_unset_event (s->rx_fifo);
1888 return VPPCOM_EWOULDBLOCK;
1889 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001890 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001891 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001892 if (vcl_session_is_closing (s))
1893 return vcl_session_closing_error (s);
1894
Florin Corasc0737e92019-03-04 14:19:39 -08001895 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001896 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001897 if (svm_msg_q_is_empty (mq))
1898 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001899
Florin Coras54693d22018-07-17 10:46:29 -07001900 svm_msg_q_sub_w_lock (mq, &msg);
1901 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001902 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001903 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001904 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001905 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001906 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001907 }
Florin Coras54693d22018-07-17 10:46:29 -07001908
Florin Coras4a2c7942020-09-10 12:27:14 -07001909read_again:
1910
Florin Coras460dce62018-07-27 05:45:06 -07001911 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001912 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001913 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001914 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1915
1916 ASSERT (rv >= 0);
1917 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001918
Sirshak Das28aa5392019-02-05 01:33:33 -06001919 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001920 {
1921 svm_fifo_unset_event (s->rx_fifo);
1922 if (!svm_fifo_is_empty_cons (rx_fifo)
1923 && svm_fifo_set_event (s->rx_fifo) && is_nonblocking)
1924 {
Florin Corasc34118b2020-08-12 18:58:25 -07001925 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1926 e->event_type = SESSION_IO_EVT_RX;
1927 e->session_index = s->session_index;
1928 }
1929 }
Florin Coras4a2c7942020-09-10 12:27:14 -07001930 else if (PREDICT_FALSE (rv < n))
1931 {
1932 /* More data enqueued while reading. Try to drain it
1933 * or fill the buffer */
1934 buf += rv;
1935 n -= rv;
1936 goto read_again;
1937 }
Florin Coras41c9e042018-09-11 00:10:41 -07001938
Florin Coras17ec5772020-08-12 20:46:29 -07001939 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001940 {
Florin Coras17ec5772020-08-12 20:46:29 -07001941 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001942 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1943 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001944 }
1945
Florin Coras5e062572019-03-14 19:07:51 -07001946 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1947 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001948
Florin Coras54693d22018-07-17 10:46:29 -07001949 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001950}
1951
Steven58f464e2017-10-25 12:33:12 -07001952int
Florin Coras134a9962018-08-28 11:32:04 -07001953vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001954{
Florin Coras134a9962018-08-28 11:32:04 -07001955 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001956}
1957
1958static int
Florin Coras134a9962018-08-28 11:32:04 -07001959vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001960{
Florin Coras134a9962018-08-28 11:32:04 -07001961 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001962}
1963
Florin Coras2cba8532018-09-11 16:33:36 -07001964int
1965vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001966 vppcom_data_segment_t * ds, uint32_t n_segments,
1967 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001968{
1969 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001970 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001971 vcl_session_t *s = 0;
1972 svm_fifo_t *rx_fifo;
1973 svm_msg_q_msg_t msg;
1974 session_event_t *e;
1975 svm_msg_q_t *mq;
1976 u8 is_ct;
1977
1978 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001979 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001980 return VPPCOM_EBADFD;
1981
Florin Coras6d0106e2019-01-29 20:11:58 -08001982 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1983 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001984
Florin Corasac422d62020-10-19 20:51:36 -07001985 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07001986 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07001987 mq = wrk->app_event_queue;
Florin Coras2cba8532018-09-11 16:33:36 -07001988 rx_fifo = s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001989 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07001990
Florin Coras653e43f2019-03-04 10:56:23 -08001991 if (is_ct)
1992 svm_fifo_unset_event (s->rx_fifo);
1993
Sirshak Das28aa5392019-02-05 01:33:33 -06001994 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001995 {
1996 if (is_nonblocking)
1997 {
1998 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001999 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002000 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002001 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002002 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002003 if (vcl_session_is_closing (s))
2004 return vcl_session_closing_error (s);
2005
Florin Coras2cba8532018-09-11 16:33:36 -07002006 svm_fifo_unset_event (rx_fifo);
2007 svm_msg_q_lock (mq);
2008 if (svm_msg_q_is_empty (mq))
2009 svm_msg_q_wait (mq);
2010
2011 svm_msg_q_sub_w_lock (mq, &msg);
2012 e = svm_msg_q_msg_data (mq, &msg);
2013 svm_msg_q_unlock (mq);
2014 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08002015 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07002016 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07002017 }
2018 }
2019
Florin Corasd1cc38d2020-10-11 11:05:04 -07002020 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2021 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2022 if (n_read < 0)
2023 return VPPCOM_EAGAIN;
2024
2025 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2026 {
2027 svm_fifo_unset_event (s->rx_fifo);
2028 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
2029 && svm_fifo_set_event (s->rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002030 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002031 {
2032 session_event_t *e;
2033 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2034 e->event_type = SESSION_IO_EVT_RX;
2035 e->session_index = s->session_index;
2036 }
2037 }
2038
2039 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002040 return n_read;
2041}
2042
2043void
Florin Corasd68faf82020-09-25 15:18:13 -07002044vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002045{
2046 vcl_worker_t *wrk = vcl_worker_get_current ();
2047 vcl_session_t *s;
2048
2049 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002050 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002051 return;
2052
Florin Corasd68faf82020-09-25 15:18:13 -07002053 svm_fifo_dequeue_drop (s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002054
2055 ASSERT (s->rx_bytes_pending < n_bytes);
2056 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002057}
2058
Florin Coras54693d22018-07-17 10:46:29 -07002059static u8
2060vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2061{
Florin Corasc0737e92019-03-04 14:19:39 -08002062 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002063}
2064
Florin Coras7a2abce2020-04-05 19:25:44 +00002065always_inline u8
2066vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002067{
Florin Coras7a2abce2020-04-05 19:25:44 +00002068 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2069 if (is_dgram)
2070 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2071 else
2072 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002073}
2074
2075always_inline int
2076vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2077 size_t n, u8 is_flush, u8 is_dgram)
2078{
Florin Coras6d0106e2019-01-29 20:11:58 -08002079 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002080 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002081 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002082 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002083 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002084 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002085 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002086
jiangxiaomingff31ac62019-11-21 23:34:56 +08002087 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002088 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002089
Florin Coras6c3b2182020-10-19 18:36:48 -07002090 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002091 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002092 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2093 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002094 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002095 }
2096
Florin Coras6d0106e2019-01-29 20:11:58 -08002097 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002098 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002099 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2100 s->session_index, s->vpp_handle, s->session_state,
2101 vppcom_session_state_str (s->session_state));
2102 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002103 }
2104
Florin Coras0e88e852018-09-17 22:09:02 -07002105 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002106 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002107 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002108
Florin Coras653e43f2019-03-04 10:56:23 -08002109 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002110 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002111 {
Florin Coras54693d22018-07-17 10:46:29 -07002112 if (is_nonblocking)
2113 {
Florin Coras070453d2018-08-24 17:04:27 -07002114 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002115 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002116 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002117 {
Florin Coras2d379d82019-06-28 12:45:12 -07002118 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002119 if (vcl_session_is_closing (s))
2120 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002121 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002122 if (svm_msg_q_is_empty (mq))
2123 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002124
Florin Coras54693d22018-07-17 10:46:29 -07002125 svm_msg_q_sub_w_lock (mq, &msg);
2126 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002127 svm_msg_q_unlock (mq);
2128
Florin Coras0e88e852018-09-17 22:09:02 -07002129 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002130 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002131 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002132 }
Dave Wallace543852a2017-08-03 02:11:34 -04002133 }
Dave Wallace543852a2017-08-03 02:11:34 -04002134
Florin Coras653e43f2019-03-04 10:56:23 -08002135 et = SESSION_IO_EVT_TX;
2136 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002137 et = SESSION_IO_EVT_TX_FLUSH;
2138
Florin Coras7a2abce2020-04-05 19:25:44 +00002139 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002140 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002141 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002142 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002143 else
2144 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002145 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002146
Florin Coras14ed6df2019-03-06 21:13:42 -08002147 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08002148 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
2149 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002150
Florin Coras56230092020-09-02 20:52:58 -07002151 /* The underlying fifo segment can run out of memory */
2152 if (PREDICT_FALSE (n_write < 0))
2153 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002154
Florin Coras6d0106e2019-01-29 20:11:58 -08002155 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2156 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002157
Florin Coras54693d22018-07-17 10:46:29 -07002158 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002159}
2160
Florin Coras42ceddb2018-12-12 10:56:01 -08002161int
2162vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2163{
Florin Coras7a2abce2020-04-05 19:25:44 +00002164 vcl_worker_t *wrk = vcl_worker_get_current ();
2165 vcl_session_t *s;
2166
2167 s = vcl_session_get_w_handle (wrk, session_handle);
2168 if (PREDICT_FALSE (!s))
2169 return VPPCOM_EBADFD;
2170
2171 return vppcom_session_write_inline (wrk, s, buf, n,
2172 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002173}
2174
Florin Corasb0f662f2018-12-27 14:51:46 -08002175int
2176vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2177{
Florin Coras7a2abce2020-04-05 19:25:44 +00002178 vcl_worker_t *wrk = vcl_worker_get_current ();
2179 vcl_session_t *s;
2180
2181 s = vcl_session_get_w_handle (wrk, session_handle);
2182 if (PREDICT_FALSE (!s))
2183 return VPPCOM_EBADFD;
2184
2185 return vppcom_session_write_inline (wrk, s, buf, n,
2186 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002187}
2188
Florin Corasc0737e92019-03-04 14:19:39 -08002189#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002190if (PREDICT_FALSE (!_s->rx_fifo)) \
2191 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002192if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2193 { \
2194 if (!vcl_session_is_ct (_s)) \
2195 { \
2196 svm_fifo_unset_event (_s->rx_fifo); \
2197 if (svm_fifo_is_empty (_s->rx_fifo)) \
2198 break; \
2199 } \
2200 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2201 { \
Florin Coras2f630182020-08-13 00:17:51 -07002202 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002203 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2204 break; \
2205 } \
2206 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002207
Florin Coras86f04502018-09-12 16:08:01 -07002208static void
2209vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2210 unsigned long n_bits, unsigned long *read_map,
2211 unsigned long *write_map,
2212 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002213{
2214 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002215 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002216 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002217 u32 sid;
2218
2219 switch (e->event_type)
2220 {
Florin Corasc0737e92019-03-04 14:19:39 -08002221 case SESSION_IO_EVT_RX:
2222 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002223 s = vcl_session_get (wrk, sid);
2224 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002225 break;
Florin Corasb242d312020-10-26 15:35:40 -07002226 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002227 if (sid < n_bits && read_map)
2228 {
David Johnsond9818dd2018-12-14 14:53:41 -05002229 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002230 *bits_set += 1;
2231 }
2232 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002233 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002234 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002235 s = vcl_session_get (wrk, sid);
2236 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002237 break;
2238 if (sid < n_bits && write_map)
2239 {
David Johnsond9818dd2018-12-14 14:53:41 -05002240 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002241 *bits_set += 1;
2242 }
2243 break;
Florin Coras86f04502018-09-12 16:08:01 -07002244 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002245 if (!e->postponed)
2246 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2247 else
2248 s = vcl_session_get (wrk, e->session_index);
2249 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002250 break;
Florin Corasb242d312020-10-26 15:35:40 -07002251 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002252 if (sid < n_bits && read_map)
2253 {
David Johnsond9818dd2018-12-14 14:53:41 -05002254 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002255 *bits_set += 1;
2256 }
2257 break;
2258 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002259 if (!e->postponed)
2260 {
2261 connected_msg = (session_connected_msg_t *) e->data;
2262 sid = vcl_session_connected_handler (wrk, connected_msg);
2263 }
2264 else
2265 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002266 if (sid == VCL_INVALID_SESSION_INDEX)
2267 break;
2268 if (sid < n_bits && write_map)
2269 {
2270 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2271 *bits_set += 1;
2272 }
Florin Coras86f04502018-09-12 16:08:01 -07002273 break;
2274 case SESSION_CTRL_EVT_DISCONNECTED:
2275 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002276 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2277 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002278 break;
Florin Corasb242d312020-10-26 15:35:40 -07002279 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002280 if (sid < n_bits && except_map)
2281 {
David Johnsond9818dd2018-12-14 14:53:41 -05002282 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002283 *bits_set += 1;
2284 }
2285 break;
2286 case SESSION_CTRL_EVT_RESET:
2287 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2288 if (sid < n_bits && except_map)
2289 {
David Johnsond9818dd2018-12-14 14:53:41 -05002290 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002291 *bits_set += 1;
2292 }
2293 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002294 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2295 vcl_session_unlisten_reply_handler (wrk, e->data);
2296 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002297 case SESSION_CTRL_EVT_MIGRATED:
2298 vcl_session_migrated_handler (wrk, e->data);
2299 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002300 case SESSION_CTRL_EVT_CLEANUP:
2301 vcl_session_cleanup_handler (wrk, e->data);
2302 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002303 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2304 vcl_session_worker_update_reply_handler (wrk, e->data);
2305 break;
2306 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2307 vcl_session_req_worker_update_handler (wrk, e->data);
2308 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002309 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2310 vcl_session_app_add_segment_handler (wrk, e->data);
2311 break;
2312 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2313 vcl_session_app_del_segment_handler (wrk, e->data);
2314 break;
hanlina3a48962020-07-13 11:09:15 +08002315 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002316 vcl_worker_rpc_handler (wrk, e->data);
2317 break;
Florin Coras86f04502018-09-12 16:08:01 -07002318 default:
2319 clib_warning ("unhandled: %u", e->event_type);
2320 break;
2321 }
2322}
2323
2324static int
2325vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2326 unsigned long n_bits, unsigned long *read_map,
2327 unsigned long *write_map, unsigned long *except_map,
2328 double time_to_wait, u32 * bits_set)
2329{
Florin Coras99368312018-08-02 10:45:44 -07002330 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002331 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002332 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002333
2334 svm_msg_q_lock (mq);
2335 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002336 {
Florin Coras54693d22018-07-17 10:46:29 -07002337 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002338 {
Florin Coras54693d22018-07-17 10:46:29 -07002339 svm_msg_q_unlock (mq);
2340 return 0;
2341 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002342
Florin Coras54693d22018-07-17 10:46:29 -07002343 if (!time_to_wait)
2344 {
2345 svm_msg_q_unlock (mq);
2346 return 0;
2347 }
2348 else if (time_to_wait < 0)
2349 {
2350 svm_msg_q_wait (mq);
2351 }
2352 else
2353 {
2354 if (svm_msg_q_timedwait (mq, time_to_wait))
2355 {
2356 svm_msg_q_unlock (mq);
2357 return 0;
2358 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002359 }
2360 }
Florin Corase003a1b2019-06-05 10:47:16 -07002361 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002362 svm_msg_q_unlock (mq);
2363
Florin Coras134a9962018-08-28 11:32:04 -07002364 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002365 {
Florin Coras134a9962018-08-28 11:32:04 -07002366 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002367 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002368 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2369 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002370 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002371 }
Florin Coras134a9962018-08-28 11:32:04 -07002372 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002373 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002374 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002375}
2376
Florin Coras99368312018-08-02 10:45:44 -07002377static int
Florin Coras294afe22019-01-07 17:49:17 -08002378vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2379 vcl_si_set * read_map, vcl_si_set * write_map,
2380 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002381 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002382{
Florin Coras14ed6df2019-03-06 21:13:42 -08002383 double wait = 0, start = 0;
2384
2385 if (!*bits_set)
2386 {
2387 wait = time_to_wait;
2388 start = clib_time_now (&wrk->clib_time);
2389 }
2390
2391 do
2392 {
2393 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2394 write_map, except_map, wait, bits_set);
2395 if (*bits_set)
2396 return *bits_set;
2397 if (wait == -1)
2398 continue;
2399
2400 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2401 }
2402 while (wait > 0);
2403
2404 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002405}
2406
2407static int
Florin Coras294afe22019-01-07 17:49:17 -08002408vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2409 vcl_si_set * read_map, vcl_si_set * write_map,
2410 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002411 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002412{
2413 vcl_mq_evt_conn_t *mqc;
2414 int __clib_unused n_read;
2415 int n_mq_evts, i;
2416 u64 buf;
2417
Florin Coras134a9962018-08-28 11:32:04 -07002418 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2419 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2420 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002421 for (i = 0; i < n_mq_evts; i++)
2422 {
Florin Coras134a9962018-08-28 11:32:04 -07002423 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002424 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002425 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002426 except_map, 0, bits_set);
2427 }
2428
2429 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2430}
2431
Dave Wallace543852a2017-08-03 02:11:34 -04002432int
Florin Coras294afe22019-01-07 17:49:17 -08002433vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2434 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002435{
Florin Coras54693d22018-07-17 10:46:29 -07002436 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002437 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002438 vcl_session_t *session = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002439 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002440
Dave Wallace7876d392017-10-19 03:53:57 -04002441 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002442 {
Florin Coras134a9962018-08-28 11:32:04 -07002443 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002444 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002445 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2446 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002447 }
Dave Wallace7876d392017-10-19 03:53:57 -04002448 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002449 {
Florin Coras134a9962018-08-28 11:32:04 -07002450 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002451 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002452 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2453 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002454 }
Dave Wallace7876d392017-10-19 03:53:57 -04002455 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002456 {
Florin Coras134a9962018-08-28 11:32:04 -07002457 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002458 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002459 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2460 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002461 }
2462
Florin Coras54693d22018-07-17 10:46:29 -07002463 if (!n_bits)
2464 return 0;
2465
2466 if (!write_map)
2467 goto check_rd;
2468
2469 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002470 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2471 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002472 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002473 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2474 bits_set++;
2475 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002476 }
2477
Florin Coras5e6222a2020-04-24 17:09:25 +00002478 if (vcl_session_write_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002479 {
David Johnsond9818dd2018-12-14 14:53:41 -05002480 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002481 bits_set++;
2482 }
Florin Coras294afe22019-01-07 17:49:17 -08002483 else
Florin Coras2d379d82019-06-28 12:45:12 -07002484 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002485 }));
2486
2487check_rd:
2488 if (!read_map)
2489 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002490
Florin Coras134a9962018-08-28 11:32:04 -07002491 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2492 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002493 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002494 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2495 bits_set++;
2496 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002497 }
2498
Florin Coras5e6222a2020-04-24 17:09:25 +00002499 if (vcl_session_read_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002500 {
David Johnsond9818dd2018-12-14 14:53:41 -05002501 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002502 bits_set++;
2503 }
2504 }));
2505 /* *INDENT-ON* */
2506
2507check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002508
Florin Coras86f04502018-09-12 16:08:01 -07002509 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2510 {
2511 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2512 read_map, write_map, except_map, &bits_set);
2513 }
2514 vec_reset_length (wrk->unhandled_evts_vector);
2515
Florin Coras99368312018-08-02 10:45:44 -07002516 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002517 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002518 time_to_wait, &bits_set);
2519 else
Florin Coras134a9962018-08-28 11:32:04 -07002520 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002521 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002522
Dave Wallace543852a2017-08-03 02:11:34 -04002523 return (bits_set);
2524}
2525
Dave Wallacef7f809c2017-10-03 01:48:42 -04002526static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002527vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002528{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002529 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002530 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002531 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002532
Florin Corasc0737e92019-03-04 14:19:39 -08002533 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002534 return;
2535
Florin Coras6c3b2182020-10-19 18:36:48 -07002536 s = vcl_session_get_w_handle (wrk, vep_handle);
2537 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002538 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002539 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002540 goto done;
2541 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002542 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002543 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002544 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002545 goto done;
2546 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002547 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002548 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002549 "{\n"
2550 " is_vep = %u\n"
2551 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002552 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002553 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2554 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002555
Florin Coras7e5e62b2019-07-30 14:08:23 -07002556 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002557 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002558 s = vcl_session_get_w_handle (wrk, sh);
2559 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002560 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002561 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002562 goto done;
2563 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002564 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002565 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002566 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002567 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002568 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002569 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002570 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002571 goto done;
2572 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002573 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002574 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2575 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002576 sh, s->vep.vep_sh, vep_handle);
2577 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002578 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002579 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002580 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002581 " next_sh = 0x%x (%u)\n"
2582 " prev_sh = 0x%x (%u)\n"
2583 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002584 " ev.events = 0x%x\n"
2585 " ev.data.u64 = 0x%llx\n"
2586 " et_mask = 0x%x\n"
2587 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002588 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002589 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2590 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002591 }
2592 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002593
2594done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002595 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002596}
2597
2598int
2599vppcom_epoll_create (void)
2600{
Florin Coras134a9962018-08-28 11:32:04 -07002601 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002602 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002603
Florin Coras134a9962018-08-28 11:32:04 -07002604 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002605
Florin Coras6c3b2182020-10-19 18:36:48 -07002606 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002607 vep_session->vep.vep_sh = ~0;
2608 vep_session->vep.next_sh = ~0;
2609 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002610 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002611
Florin Corasa7a1a222018-12-30 17:11:31 -08002612 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2613 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002614
Florin Corasab2f6db2018-08-31 14:31:41 -07002615 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616}
2617
2618int
Florin Coras134a9962018-08-28 11:32:04 -07002619vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002620 struct epoll_event *event)
2621{
Florin Coras134a9962018-08-28 11:32:04 -07002622 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002623 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002624 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002625 vcl_session_t *s;
2626 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002627
Florin Coras134a9962018-08-28 11:32:04 -07002628 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002629 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002630 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002631 return VPPCOM_EINVAL;
2632 }
2633
Florin Coras134a9962018-08-28 11:32:04 -07002634 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002635 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002636 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002637 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002638 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002639 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002640 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002641 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002642 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002643 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002644 }
2645
Florin Coras134a9962018-08-28 11:32:04 -07002646 ASSERT (vep_session->vep.vep_sh == ~0);
2647 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002648
Florin Coras17ec5772020-08-12 20:46:29 -07002649 s = vcl_session_get_w_handle (wrk, session_handle);
2650 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002651 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002652 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002653 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002654 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002655 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002656 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002657 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002658 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002659 }
2660
2661 switch (op)
2662 {
2663 case EPOLL_CTL_ADD:
2664 if (PREDICT_FALSE (!event))
2665 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002666 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002667 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002668 }
Florin Coras134a9962018-08-28 11:32:04 -07002669 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002670 {
Florin Coras7e12d942018-06-27 14:32:43 -07002671 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002672 next_session = vcl_session_get_w_handle (wrk,
2673 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002674 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675 {
Florin Coras5e062572019-03-14 19:07:51 -07002676 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002677 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002678 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002679 }
Florin Coras134a9962018-08-28 11:32:04 -07002680 ASSERT (next_session->vep.prev_sh == vep_handle);
2681 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682 }
Florin Coras17ec5772020-08-12 20:46:29 -07002683 s->vep.next_sh = vep_session->vep.next_sh;
2684 s->vep.prev_sh = vep_handle;
2685 s->vep.vep_sh = vep_handle;
2686 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2687 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002688 s->flags &= ~VCL_SESSION_F_IS_VEP;
2689 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002690 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002691
Florin Coras17ec5772020-08-12 20:46:29 -07002692 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2693 if (txf && (event->events & EPOLLOUT))
2694 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002695
Florin Coras6e3c1f82020-01-15 01:30:46 +00002696 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002697 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002698 {
2699 session_event_t e = { 0 };
2700 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002701 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002702 vec_add1 (wrk->unhandled_evts_vector, e);
2703 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002704 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002705 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002706 {
2707 session_event_t e = { 0 };
2708 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002709 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002710 vec_add1 (wrk->unhandled_evts_vector, e);
2711 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002712 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2713 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002714 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002715 break;
2716
2717 case EPOLL_CTL_MOD:
2718 if (PREDICT_FALSE (!event))
2719 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002720 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002721 rv = VPPCOM_EINVAL;
2722 goto done;
2723 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002724 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002725 {
Florin Coras5e062572019-03-14 19:07:51 -07002726 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002727 rv = VPPCOM_EINVAL;
2728 goto done;
2729 }
Florin Coras17ec5772020-08-12 20:46:29 -07002730 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002731 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002732 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002733 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002734 rv = VPPCOM_EINVAL;
2735 goto done;
2736 }
hanlin475c9d72019-12-26 11:44:28 +08002737
2738 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2739 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002740 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002741 {
2742 session_event_t e = { 0 };
2743 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002744 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002745 vec_add1 (wrk->unhandled_evts_vector, e);
2746 }
Florin Coras17ec5772020-08-12 20:46:29 -07002747 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2748 s->vep.ev = *event;
2749 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2750 if (event->events & EPOLLOUT)
2751 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2752 else
2753 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002754 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2755 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002756 break;
2757
2758 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002759 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002760 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002761 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002762 rv = VPPCOM_EINVAL;
2763 goto done;
2764 }
Florin Coras17ec5772020-08-12 20:46:29 -07002765 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002766 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002767 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002768 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 rv = VPPCOM_EINVAL;
2770 goto done;
2771 }
2772
Florin Coras17ec5772020-08-12 20:46:29 -07002773 if (s->vep.prev_sh == vep_handle)
2774 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002775 else
2776 {
Florin Coras7e12d942018-06-27 14:32:43 -07002777 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002778 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002779 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002780 {
Florin Coras5e062572019-03-14 19:07:51 -07002781 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002782 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002783 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002784 }
Florin Coras134a9962018-08-28 11:32:04 -07002785 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002786 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002787 }
Florin Coras17ec5772020-08-12 20:46:29 -07002788 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789 {
Florin Coras7e12d942018-06-27 14:32:43 -07002790 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002791 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002792 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002793 {
Florin Coras5e062572019-03-14 19:07:51 -07002794 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002795 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002796 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002797 }
Florin Coras134a9962018-08-28 11:32:04 -07002798 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002799 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002800 }
2801
Florin Coras17ec5772020-08-12 20:46:29 -07002802 memset (&s->vep, 0, sizeof (s->vep));
2803 s->vep.next_sh = ~0;
2804 s->vep.prev_sh = ~0;
2805 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002806 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002807
Florin Coras17ec5772020-08-12 20:46:29 -07002808 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2809 if (txf)
2810 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002811
Florin Coras5e062572019-03-14 19:07:51 -07002812 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002813 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002814 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002815 break;
2816
2817 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002818 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002819 rv = VPPCOM_EINVAL;
2820 }
2821
Florin Coras134a9962018-08-28 11:32:04 -07002822 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002823
2824done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002825 return rv;
2826}
2827
Florin Coras86f04502018-09-12 16:08:01 -07002828static inline void
2829vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2830 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002831{
2832 session_disconnected_msg_t *disconnected_msg;
2833 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002834 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002835 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002836 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002837 u8 add_event = 0;
2838
2839 switch (e->event_type)
2840 {
Florin Coras653e43f2019-03-04 10:56:23 -08002841 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002842 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002843 s = vcl_session_get (wrk, sid);
2844 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002845 break;
Florin Corasb242d312020-10-26 15:35:40 -07002846 vcl_fifo_rx_evt_valid_or_break (s);
2847 session_events = s->vep.ev.events;
2848 if (!(EPOLLIN & s->vep.ev.events)
2849 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002850 break;
2851 add_event = 1;
2852 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002853 session_evt_data = s->vep.ev.data.u64;
2854 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002855 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002856 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002857 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002858 s = vcl_session_get (wrk, sid);
2859 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002860 break;
Florin Corasb242d312020-10-26 15:35:40 -07002861 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002862 if (!(EPOLLOUT & session_events))
2863 break;
2864 add_event = 1;
2865 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002866 session_evt_data = s->vep.ev.data.u64;
2867 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2868 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002869 break;
Florin Coras86f04502018-09-12 16:08:01 -07002870 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002871 if (!e->postponed)
2872 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2873 else
2874 s = vcl_session_get (wrk, e->session_index);
2875 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002876 break;
Florin Corasb242d312020-10-26 15:35:40 -07002877 session_events = s->vep.ev.events;
2878 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002879 if (!(EPOLLIN & session_events))
2880 break;
Florin Coras86f04502018-09-12 16:08:01 -07002881 add_event = 1;
2882 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002883 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002884 break;
2885 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002886 if (!e->postponed)
2887 {
2888 connected_msg = (session_connected_msg_t *) e->data;
2889 sid = vcl_session_connected_handler (wrk, connected_msg);
2890 }
2891 else
2892 sid = e->session_index;
2893 s = vcl_session_get (wrk, sid);
2894 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002895 break;
Florin Corasb242d312020-10-26 15:35:40 -07002896 session_events = s->vep.ev.events;
2897 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002898 if (!(EPOLLOUT & session_events))
2899 break;
2900 add_event = 1;
2901 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002902 session_evt_data = s->vep.ev.data.u64;
2903 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002904 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002905 break;
2906 case SESSION_CTRL_EVT_DISCONNECTED:
2907 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002908 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2909 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002910 break;
Florin Corasb242d312020-10-26 15:35:40 -07002911 sid = s->session_index;
2912 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002913 add_event = 1;
2914 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002915 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002916 break;
2917 case SESSION_CTRL_EVT_RESET:
2918 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002919 s = vcl_session_get (wrk, sid);
2920 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002921 break;
Florin Corasb242d312020-10-26 15:35:40 -07002922 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002923 add_event = 1;
2924 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002925 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002926 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002927 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2928 vcl_session_unlisten_reply_handler (wrk, e->data);
2929 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002930 case SESSION_CTRL_EVT_MIGRATED:
2931 vcl_session_migrated_handler (wrk, e->data);
2932 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002933 case SESSION_CTRL_EVT_CLEANUP:
2934 vcl_session_cleanup_handler (wrk, e->data);
2935 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002936 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2937 vcl_session_req_worker_update_handler (wrk, e->data);
2938 break;
2939 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2940 vcl_session_worker_update_reply_handler (wrk, e->data);
2941 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002942 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2943 vcl_session_app_add_segment_handler (wrk, e->data);
2944 break;
2945 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2946 vcl_session_app_del_segment_handler (wrk, e->data);
2947 break;
hanlina3a48962020-07-13 11:09:15 +08002948 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002949 vcl_worker_rpc_handler (wrk, e->data);
2950 break;
Florin Coras86f04502018-09-12 16:08:01 -07002951 default:
2952 VDBG (0, "unhandled: %u", e->event_type);
2953 break;
2954 }
2955
2956 if (add_event)
2957 {
2958 events[*num_ev].data.u64 = session_evt_data;
2959 if (EPOLLONESHOT & session_events)
2960 {
Florin Corasb242d312020-10-26 15:35:40 -07002961 s = vcl_session_get (wrk, sid);
2962 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002963 }
2964 *num_ev += 1;
2965 }
2966}
2967
2968static int
2969vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2970 struct epoll_event *events, u32 maxevents,
2971 double wait_for_time, u32 * num_ev)
2972{
Florin Coras99368312018-08-02 10:45:44 -07002973 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002974 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002975 int i;
2976
Florin Coras539663c2018-09-28 14:59:37 -07002977 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2978 goto handle_dequeued;
2979
Florin Coras54693d22018-07-17 10:46:29 -07002980 svm_msg_q_lock (mq);
2981 if (svm_msg_q_is_empty (mq))
2982 {
2983 if (!wait_for_time)
2984 {
2985 svm_msg_q_unlock (mq);
2986 return 0;
2987 }
2988 else if (wait_for_time < 0)
2989 {
2990 svm_msg_q_wait (mq);
2991 }
2992 else
2993 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002994 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002995 {
2996 svm_msg_q_unlock (mq);
2997 return 0;
2998 }
2999 }
3000 }
Florin Corase003a1b2019-06-05 10:47:16 -07003001 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003002 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003003 svm_msg_q_unlock (mq);
3004
Florin Coras539663c2018-09-28 14:59:37 -07003005handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003006 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003007 {
Florin Coras134a9962018-08-28 11:32:04 -07003008 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003009 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003010 if (*num_ev < maxevents)
3011 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3012 else
3013 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003014 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003015 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003016 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003017 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003018 return *num_ev;
3019}
3020
Florin Coras99368312018-08-02 10:45:44 -07003021static int
Florin Coras134a9962018-08-28 11:32:04 -07003022vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003023 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003024{
Florin Corase003a1b2019-06-05 10:47:16 -07003025 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003026
3027 if (!n_evts)
3028 {
3029 wait = wait_for_time;
3030 start = clib_time_now (&wrk->clib_time);
3031 }
3032
3033 do
3034 {
3035 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
3036 wait, &n_evts);
3037 if (n_evts)
3038 return n_evts;
3039 if (wait == -1)
3040 continue;
3041
Florin Corase003a1b2019-06-05 10:47:16 -07003042 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07003043 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07003044 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003045 }
3046 while (wait > 0);
3047
3048 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003049}
3050
3051static int
Florin Coras134a9962018-08-28 11:32:04 -07003052vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003053 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003054{
3055 vcl_mq_evt_conn_t *mqc;
3056 int __clib_unused n_read;
3057 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003058 u64 buf;
3059
Florin Coras134a9962018-08-28 11:32:04 -07003060 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07003061again:
Florin Coras134a9962018-08-28 11:32:04 -07003062 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3063 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07003064 for (i = 0; i < n_mq_evts; i++)
3065 {
Florin Coras134a9962018-08-28 11:32:04 -07003066 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07003067 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07003068 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07003069 }
Florin Corasa4878ed2018-10-12 13:09:36 -07003070 if (!n_evts && n_mq_evts > 0)
3071 goto again;
Florin Coras99368312018-08-02 10:45:44 -07003072
3073 return (int) n_evts;
3074}
3075
Dave Wallacef7f809c2017-10-03 01:48:42 -04003076int
Florin Coras134a9962018-08-28 11:32:04 -07003077vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003078 int maxevents, double wait_for_time)
3079{
Florin Coras134a9962018-08-28 11:32:04 -07003080 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003081 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003082 u32 n_evts = 0;
3083 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003084
3085 if (PREDICT_FALSE (maxevents <= 0))
3086 {
Florin Coras5e062572019-03-14 19:07:51 -07003087 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003088 return VPPCOM_EINVAL;
3089 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003090
Florin Coras134a9962018-08-28 11:32:04 -07003091 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003092 if (!vep_session)
3093 return VPPCOM_EBADFD;
3094
Florin Coras6c3b2182020-10-19 18:36:48 -07003095 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003096 {
Florin Coras5e062572019-03-14 19:07:51 -07003097 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003098 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003099 }
Florin Coras54693d22018-07-17 10:46:29 -07003100
3101 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003102
Florin Coras86f04502018-09-12 16:08:01 -07003103 if (vec_len (wrk->unhandled_evts_vector))
3104 {
3105 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3106 {
3107 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3108 events, &n_evts);
3109 if (n_evts == maxevents)
3110 {
Florin Corase003a1b2019-06-05 10:47:16 -07003111 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3112 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003113 }
3114 }
Florin Corase003a1b2019-06-05 10:47:16 -07003115 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003116 }
3117
3118 if (vcm->cfg.use_mq_eventfd)
3119 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3120 wait_for_time);
3121
3122 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3123 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003124}
3125
Dave Wallace35830af2017-10-09 01:43:42 -04003126int
Florin Coras134a9962018-08-28 11:32:04 -07003127vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003128 void *buffer, uint32_t * buflen)
3129{
Florin Coras134a9962018-08-28 11:32:04 -07003130 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003131 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04003132 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08003133 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003134 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003135
Florin Coras134a9962018-08-28 11:32:04 -07003136 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003137 if (!session)
3138 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003139
Dave Wallace35830af2017-10-09 01:43:42 -04003140 switch (op)
3141 {
3142 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003143 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003144 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3145 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003146 break;
3147
Dave Wallace227867f2017-11-13 21:21:53 -05003148 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003149 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003150 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3151 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003152 break;
3153
3154 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003155 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003156 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003157 *flags =
3158 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003159 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003160 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003161 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003162 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3163 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003164 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003165 }
3166 else
3167 rv = VPPCOM_EINVAL;
3168 break;
3169
3170 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003171 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003172 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003173 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003174 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003175 else
Florin Corasac422d62020-10-19 20:51:36 -07003176 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003177
Florin Coras5e062572019-03-14 19:07:51 -07003178 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3179 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003180 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003181 }
3182 else
3183 rv = VPPCOM_EINVAL;
3184 break;
3185
3186 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003187 if (PREDICT_TRUE (buffer && buflen &&
3188 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003189 {
Florin Coras7e12d942018-06-27 14:32:43 -07003190 ep->is_ip4 = session->transport.is_ip4;
3191 ep->port = session->transport.rmt_port;
3192 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003193 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3194 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003195 else
Dave Barach178cf492018-11-13 16:34:13 -05003196 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3197 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003198 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003199 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3200 "addr = %U, port %u", session_handle, ep->is_ip4,
3201 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003202 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3203 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003204 }
3205 else
3206 rv = VPPCOM_EINVAL;
3207 break;
3208
3209 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003210 if (PREDICT_TRUE (buffer && buflen &&
3211 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003212 {
Florin Coras7e12d942018-06-27 14:32:43 -07003213 ep->is_ip4 = session->transport.is_ip4;
3214 ep->port = session->transport.lcl_port;
3215 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003216 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3217 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003218 else
Dave Barach178cf492018-11-13 16:34:13 -05003219 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3220 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003221 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003222 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3223 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003224 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003225 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3226 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003227 }
3228 else
3229 rv = VPPCOM_EINVAL;
3230 break;
Stevenb5a11602017-10-11 09:59:30 -07003231
Florin Corasef7cbf62019-10-17 09:56:27 -07003232 case VPPCOM_ATTR_SET_LCL_ADDR:
3233 if (PREDICT_TRUE (buffer && buflen &&
3234 (*buflen >= sizeof (*ep)) && ep->ip))
3235 {
3236 session->transport.is_ip4 = ep->is_ip4;
3237 session->transport.lcl_port = ep->port;
3238 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3239 *buflen = sizeof (*ep);
3240 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3241 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3242 &session->transport.lcl_ip,
3243 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3244 clib_net_to_host_u16 (ep->port));
3245 }
3246 else
3247 rv = VPPCOM_EINVAL;
3248 break;
3249
Dave Wallace048b1d62018-01-03 22:24:41 -05003250 case VPPCOM_ATTR_GET_LIBC_EPFD:
3251 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003252 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003253 break;
3254
3255 case VPPCOM_ATTR_SET_LIBC_EPFD:
3256 if (PREDICT_TRUE (buffer && buflen &&
3257 (*buflen == sizeof (session->libc_epfd))))
3258 {
3259 session->libc_epfd = *(int *) buffer;
3260 *buflen = sizeof (session->libc_epfd);
3261
Florin Coras5e062572019-03-14 19:07:51 -07003262 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3263 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 }
3265 else
3266 rv = VPPCOM_EINVAL;
3267 break;
3268
3269 case VPPCOM_ATTR_GET_PROTOCOL:
3270 if (buffer && buflen && (*buflen >= sizeof (int)))
3271 {
Florin Coras7e12d942018-06-27 14:32:43 -07003272 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003273 *buflen = sizeof (int);
3274
Florin Coras5e062572019-03-14 19:07:51 -07003275 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3276 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003277 }
3278 else
3279 rv = VPPCOM_EINVAL;
3280 break;
3281
3282 case VPPCOM_ATTR_GET_LISTEN:
3283 if (buffer && buflen && (*buflen >= sizeof (int)))
3284 {
Florin Corasac422d62020-10-19 20:51:36 -07003285 *(int *) buffer = vcl_session_has_attr (session,
3286 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003287 *buflen = sizeof (int);
3288
Florin Coras5e062572019-03-14 19:07:51 -07003289 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3290 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003291 }
3292 else
3293 rv = VPPCOM_EINVAL;
3294 break;
3295
3296 case VPPCOM_ATTR_GET_ERROR:
3297 if (buffer && buflen && (*buflen >= sizeof (int)))
3298 {
3299 *(int *) buffer = 0;
3300 *buflen = sizeof (int);
3301
Florin Coras5e062572019-03-14 19:07:51 -07003302 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3303 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003304 }
3305 else
3306 rv = VPPCOM_EINVAL;
3307 break;
3308
3309 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3310 if (buffer && buflen && (*buflen >= sizeof (u32)))
3311 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003312
3313 /* VPP-TBD */
3314 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003315 session->tx_fifo ?
3316 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003317 vcm->cfg.tx_fifo_size);
3318 *buflen = sizeof (u32);
3319
Florin Coras5e062572019-03-14 19:07:51 -07003320 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3321 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3322 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003323 }
3324 else
3325 rv = VPPCOM_EINVAL;
3326 break;
3327
3328 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3329 if (buffer && buflen && (*buflen == sizeof (u32)))
3330 {
3331 /* VPP-TBD */
3332 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003333 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3334 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3335 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003336 }
3337 else
3338 rv = VPPCOM_EINVAL;
3339 break;
3340
3341 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3342 if (buffer && buflen && (*buflen >= sizeof (u32)))
3343 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003344
3345 /* VPP-TBD */
3346 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003347 session->rx_fifo ?
3348 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003349 vcm->cfg.rx_fifo_size);
3350 *buflen = sizeof (u32);
3351
Florin Coras5e062572019-03-14 19:07:51 -07003352 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3353 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003354 }
3355 else
3356 rv = VPPCOM_EINVAL;
3357 break;
3358
3359 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3360 if (buffer && buflen && (*buflen == sizeof (u32)))
3361 {
3362 /* VPP-TBD */
3363 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003364 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3365 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3366 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003367 }
3368 else
3369 rv = VPPCOM_EINVAL;
3370 break;
3371
3372 case VPPCOM_ATTR_GET_REUSEADDR:
3373 if (buffer && buflen && (*buflen >= sizeof (int)))
3374 {
3375 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003376 *(int *) buffer = vcl_session_has_attr (session,
3377 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003378 *buflen = sizeof (int);
3379
Florin Coras5e062572019-03-14 19:07:51 -07003380 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3381 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003382 }
3383 else
3384 rv = VPPCOM_EINVAL;
3385 break;
3386
Stevenb5a11602017-10-11 09:59:30 -07003387 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003388 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003389 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003390 {
3391 /* VPP-TBD */
3392 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003393 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003394 else
Florin Corasac422d62020-10-19 20:51:36 -07003395 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003396
Florin Coras5e062572019-03-14 19:07:51 -07003397 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003398 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003399 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003400 }
3401 else
3402 rv = VPPCOM_EINVAL;
3403 break;
3404
3405 case VPPCOM_ATTR_GET_REUSEPORT:
3406 if (buffer && buflen && (*buflen >= sizeof (int)))
3407 {
3408 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003409 *(int *) buffer = vcl_session_has_attr (session,
3410 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 *buflen = sizeof (int);
3412
Florin Coras5e062572019-03-14 19:07:51 -07003413 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3414 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003415 }
3416 else
3417 rv = VPPCOM_EINVAL;
3418 break;
3419
3420 case VPPCOM_ATTR_SET_REUSEPORT:
3421 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003422 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003423 {
3424 /* VPP-TBD */
3425 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003426 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003427 else
Florin Corasac422d62020-10-19 20:51:36 -07003428 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003429
Florin Coras5e062572019-03-14 19:07:51 -07003430 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003431 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003432 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003433 }
3434 else
3435 rv = VPPCOM_EINVAL;
3436 break;
3437
3438 case VPPCOM_ATTR_GET_BROADCAST:
3439 if (buffer && buflen && (*buflen >= sizeof (int)))
3440 {
3441 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003442 *(int *) buffer = vcl_session_has_attr (session,
3443 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003444 *buflen = sizeof (int);
3445
Florin Coras5e062572019-03-14 19:07:51 -07003446 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3447 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003448 }
3449 else
3450 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003451 break;
3452
3453 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003454 if (buffer && buflen && (*buflen == sizeof (int)))
3455 {
3456 /* VPP-TBD */
3457 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003458 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003459 else
Florin Corasac422d62020-10-19 20:51:36 -07003460 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003461
Florin Coras5e062572019-03-14 19:07:51 -07003462 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003463 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003464 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003465 }
3466 else
3467 rv = VPPCOM_EINVAL;
3468 break;
3469
3470 case VPPCOM_ATTR_GET_V6ONLY:
3471 if (buffer && buflen && (*buflen >= sizeof (int)))
3472 {
3473 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003474 *(int *) buffer = vcl_session_has_attr (session,
3475 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003476 *buflen = sizeof (int);
3477
Florin Coras5e062572019-03-14 19:07:51 -07003478 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3479 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003480 }
3481 else
3482 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003483 break;
3484
3485 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003486 if (buffer && buflen && (*buflen == sizeof (int)))
3487 {
3488 /* VPP-TBD */
3489 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003490 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003491 else
Florin Corasac422d62020-10-19 20:51:36 -07003492 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003493
Florin Coras5e062572019-03-14 19:07:51 -07003494 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003495 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003496 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003497 }
3498 else
3499 rv = VPPCOM_EINVAL;
3500 break;
3501
3502 case VPPCOM_ATTR_GET_KEEPALIVE:
3503 if (buffer && buflen && (*buflen >= sizeof (int)))
3504 {
3505 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003506 *(int *) buffer = vcl_session_has_attr (session,
3507 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003508 *buflen = sizeof (int);
3509
Florin Coras5e062572019-03-14 19:07:51 -07003510 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3511 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003512 }
3513 else
3514 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003515 break;
Stevenbccd3392017-10-12 20:42:21 -07003516
3517 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003518 if (buffer && buflen && (*buflen == sizeof (int)))
3519 {
3520 /* VPP-TBD */
3521 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003522 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003523 else
Florin Corasac422d62020-10-19 20:51:36 -07003524 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003525
Florin Coras5e062572019-03-14 19:07:51 -07003526 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003527 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003528 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003529 }
3530 else
3531 rv = VPPCOM_EINVAL;
3532 break;
3533
3534 case VPPCOM_ATTR_GET_TCP_NODELAY:
3535 if (buffer && buflen && (*buflen >= sizeof (int)))
3536 {
3537 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003538 *(int *) buffer = vcl_session_has_attr (session,
3539 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003540 *buflen = sizeof (int);
3541
Florin Coras5e062572019-03-14 19:07:51 -07003542 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3543 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003544 }
3545 else
3546 rv = VPPCOM_EINVAL;
3547 break;
3548
3549 case VPPCOM_ATTR_SET_TCP_NODELAY:
3550 if (buffer && buflen && (*buflen == sizeof (int)))
3551 {
3552 /* VPP-TBD */
3553 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003554 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003555 else
Florin Corasac422d62020-10-19 20:51:36 -07003556 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003557
Florin Coras5e062572019-03-14 19:07:51 -07003558 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003559 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003560 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003561 }
3562 else
3563 rv = VPPCOM_EINVAL;
3564 break;
3565
3566 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3567 if (buffer && buflen && (*buflen >= sizeof (int)))
3568 {
3569 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003570 *(int *) buffer = vcl_session_has_attr (session,
3571 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003572 *buflen = sizeof (int);
3573
Florin Coras5e062572019-03-14 19:07:51 -07003574 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3575 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003576 }
3577 else
3578 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003579 break;
3580
3581 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003582 if (buffer && buflen && (*buflen == sizeof (int)))
3583 {
3584 /* VPP-TBD */
3585 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003586 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003587 else
Florin Corasac422d62020-10-19 20:51:36 -07003588 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003589
Florin Coras5e062572019-03-14 19:07:51 -07003590 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003591 vcl_session_has_attr (session,
3592 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003593 }
3594 else
3595 rv = VPPCOM_EINVAL;
3596 break;
3597
3598 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3599 if (buffer && buflen && (*buflen >= sizeof (int)))
3600 {
3601 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003602 *(int *) buffer = vcl_session_has_attr (session,
3603 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003604 *buflen = sizeof (int);
3605
Florin Coras5e062572019-03-14 19:07:51 -07003606 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3607 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003608 }
3609 else
3610 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003611 break;
3612
3613 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003614 if (buffer && buflen && (*buflen == sizeof (int)))
3615 {
3616 /* VPP-TBD */
3617 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003618 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003619 else
Florin Corasac422d62020-10-19 20:51:36 -07003620 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003621
Florin Coras5e062572019-03-14 19:07:51 -07003622 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003623 vcl_session_has_attr (session,
3624 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003625 }
3626 else
3627 rv = VPPCOM_EINVAL;
3628 break;
3629
3630 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3631 if (buffer && buflen && (*buflen >= sizeof (u32)))
3632 {
3633 /* VPP-TBD */
3634 *(u32 *) buffer = session->user_mss;
3635 *buflen = sizeof (int);
3636
Florin Coras5e062572019-03-14 19:07:51 -07003637 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3638 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003639 }
3640 else
3641 rv = VPPCOM_EINVAL;
3642 break;
3643
3644 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3645 if (buffer && buflen && (*buflen == sizeof (u32)))
3646 {
3647 /* VPP-TBD */
3648 session->user_mss = *(u32 *) buffer;
3649
Florin Coras5e062572019-03-14 19:07:51 -07003650 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3651 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003652 }
3653 else
3654 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003655 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003656
Florin Coras7baeb712019-01-04 17:05:43 -08003657 case VPPCOM_ATTR_SET_SHUT:
3658 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003659 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003660 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003661 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003662 break;
3663
3664 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003665 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003666 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003667 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003668 tmp_flags |= 2;
3669 if (tmp_flags == 1)
3670 *(int *) buffer = SHUT_RD;
3671 else if (tmp_flags == 2)
3672 *(int *) buffer = SHUT_WR;
3673 else if (tmp_flags == 3)
3674 *(int *) buffer = SHUT_RDWR;
3675 *buflen = sizeof (int);
3676 break;
Florin Coras1e966172020-05-16 18:18:14 +00003677
3678 case VPPCOM_ATTR_SET_CONNECTED:
3679 session->flags |= VCL_SESSION_F_CONNECTED;
3680 break;
3681
Dave Wallacee22aa742017-10-20 12:30:38 -04003682 default:
3683 rv = VPPCOM_EINVAL;
3684 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003685 }
3686
Dave Wallace35830af2017-10-09 01:43:42 -04003687 return rv;
3688}
3689
Stevenac1f96d2017-10-24 16:03:58 -07003690int
Florin Coras134a9962018-08-28 11:32:04 -07003691vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003692 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3693{
Florin Coras134a9962018-08-28 11:32:04 -07003694 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003695 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003696 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003697
3698 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003699 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003700 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003701 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003702 else
3703 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003704 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003705 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003706 }
3707
Florin Coras0a1e1832020-03-29 18:54:04 +00003708 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003709 {
Florin Coras5da10c42020-04-01 04:31:21 +00003710 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003711 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003712 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3713 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003714 else
Dave Barach178cf492018-11-13 16:34:13 -05003715 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3716 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003717 ep->is_ip4 = session->transport.is_ip4;
3718 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003719 }
Florin Coras460dce62018-07-27 05:45:06 -07003720
Stevenac1f96d2017-10-24 16:03:58 -07003721 return rv;
3722}
3723
3724int
Florin Coras134a9962018-08-28 11:32:04 -07003725vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003726 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3727{
Florin Coras7a2abce2020-04-05 19:25:44 +00003728 vcl_worker_t *wrk = vcl_worker_get_current ();
3729 vcl_session_t *s;
3730
3731 s = vcl_session_get_w_handle (wrk, session_handle);
3732 if (!s)
3733 return VPPCOM_EBADFD;
3734
Dave Wallace617dffa2017-10-26 14:47:06 -04003735 if (!buffer)
3736 return VPPCOM_EINVAL;
3737
3738 if (ep)
3739 {
Florin Coras5824cc52020-10-20 18:44:41 -07003740 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003741 return VPPCOM_EINVAL;
3742
3743 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003744 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003745 {
Florin Coras5824cc52020-10-20 18:44:41 -07003746 u32 session_index = s->session_index;
3747 f64 timeout = vcm->cfg.session_timeout;
3748 int rv;
3749
Florin Coras5da10c42020-04-01 04:31:21 +00003750 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003751 rv = vppcom_wait_for_session_state_change (session_index,
3752 VCL_STATE_READY,
3753 timeout);
3754 if (rv < 0)
3755 return rv;
3756 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003757 }
Florin Coras5824cc52020-10-20 18:44:41 -07003758
3759 s->transport.is_ip4 = ep->is_ip4;
3760 s->transport.rmt_port = ep->port;
3761 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003762 }
3763
3764 if (flags)
3765 {
3766 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003767 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003768 }
3769
Florin Coras7a2abce2020-04-05 19:25:44 +00003770 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3771 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003772}
3773
Dave Wallace048b1d62018-01-03 22:24:41 -05003774int
3775vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3776{
Florin Coras134a9962018-08-28 11:32:04 -07003777 vcl_worker_t *wrk = vcl_worker_get_current ();
3778 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003779 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003780 svm_msg_q_msg_t msg;
3781 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003782 int rv, num_ev = 0;
3783
Florin Coras5e062572019-03-14 19:07:51 -07003784 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003785
3786 if (!vp)
3787 return VPPCOM_EFAULT;
3788
3789 do
3790 {
Florin Coras7e12d942018-06-27 14:32:43 -07003791 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003792
Florin Coras6917b942018-11-13 22:44:54 -08003793 /* Dequeue all events and drop all unhandled io events */
3794 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3795 {
3796 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3797 vcl_handle_mq_event (wrk, e);
3798 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3799 }
3800 vec_reset_length (wrk->unhandled_evts_vector);
3801
Dave Wallace048b1d62018-01-03 22:24:41 -05003802 for (i = 0; i < n_sids; i++)
3803 {
Florin Coras7baeb712019-01-04 17:05:43 -08003804 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003805 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003806 {
3807 vp[i].revents = POLLHUP;
3808 num_ev++;
3809 continue;
3810 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003811
Florin Coras6917b942018-11-13 22:44:54 -08003812 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003813
3814 if (POLLIN & vp[i].events)
3815 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003816 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 if (rv > 0)
3818 {
Florin Coras6917b942018-11-13 22:44:54 -08003819 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003820 num_ev++;
3821 }
3822 else if (rv < 0)
3823 {
3824 switch (rv)
3825 {
3826 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003827 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 break;
3829
3830 default:
Florin Coras6917b942018-11-13 22:44:54 -08003831 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 break;
3833 }
3834 num_ev++;
3835 }
3836 }
3837
3838 if (POLLOUT & vp[i].events)
3839 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003840 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003841 if (rv > 0)
3842 {
Florin Coras6917b942018-11-13 22:44:54 -08003843 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003844 num_ev++;
3845 }
3846 else if (rv < 0)
3847 {
3848 switch (rv)
3849 {
3850 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003851 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003852 break;
3853
3854 default:
Florin Coras6917b942018-11-13 22:44:54 -08003855 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003856 break;
3857 }
3858 num_ev++;
3859 }
3860 }
3861
Dave Wallace7e607a72018-06-18 18:41:32 -04003862 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003863 {
Florin Coras6917b942018-11-13 22:44:54 -08003864 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003865 num_ev++;
3866 }
3867 }
3868 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003869 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003870 }
3871 while ((num_ev == 0) && keep_trying);
3872
Dave Wallace048b1d62018-01-03 22:24:41 -05003873 return num_ev;
3874}
3875
Florin Coras99368312018-08-02 10:45:44 -07003876int
3877vppcom_mq_epoll_fd (void)
3878{
Florin Coras134a9962018-08-28 11:32:04 -07003879 vcl_worker_t *wrk = vcl_worker_get_current ();
3880 return wrk->mqs_epfd;
3881}
3882
3883int
Florin Coras30e79c22019-01-02 19:31:22 -08003884vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003885{
3886 return session_handle & 0xFFFFFF;
3887}
3888
3889int
Florin Coras30e79c22019-01-02 19:31:22 -08003890vppcom_session_worker (vcl_session_handle_t session_handle)
3891{
3892 return session_handle >> 24;
3893}
3894
3895int
Florin Coras134a9962018-08-28 11:32:04 -07003896vppcom_worker_register (void)
3897{
Florin Coras47c40e22018-11-26 17:01:36 -08003898 if (!vcl_worker_alloc_and_init ())
3899 return VPPCOM_EEXIST;
3900
Florin Coras47c40e22018-11-26 17:01:36 -08003901 if (vcl_worker_register_with_vpp ())
3902 return VPPCOM_EEXIST;
3903
3904 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003905}
3906
Florin Coras369db832019-07-08 12:34:45 -07003907void
3908vppcom_worker_unregister (void)
3909{
3910 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3911 vcl_set_worker_index (~0);
3912}
3913
Pivo6017ff02020-05-04 17:57:33 +02003914void
3915vppcom_worker_index_set (int index)
3916{
3917 vcl_set_worker_index (index);
3918}
3919
Florin Corasdfe4cf42018-11-28 22:13:45 -08003920int
3921vppcom_worker_index (void)
3922{
3923 return vcl_get_worker_index ();
3924}
3925
Florin Corase0982e52019-01-25 13:19:56 -08003926int
3927vppcom_worker_mqs_epfd (void)
3928{
3929 vcl_worker_t *wrk = vcl_worker_get_current ();
3930 if (!vcm->cfg.use_mq_eventfd)
3931 return -1;
3932 return wrk->mqs_epfd;
3933}
3934
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003935int
3936vppcom_session_is_connectable_listener (uint32_t session_handle)
3937{
3938 vcl_session_t *session;
3939 vcl_worker_t *wrk = vcl_worker_get_current ();
3940 session = vcl_session_get_w_handle (wrk, session_handle);
3941 if (!session)
3942 return VPPCOM_EBADFD;
3943 return vcl_session_is_connectable_listener (wrk, session);
3944}
3945
3946int
3947vppcom_session_listener (uint32_t session_handle)
3948{
3949 vcl_worker_t *wrk = vcl_worker_get_current ();
3950 vcl_session_t *listen_session, *session;
3951 session = vcl_session_get_w_handle (wrk, session_handle);
3952 if (!session)
3953 return VPPCOM_EBADFD;
3954 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3955 return VPPCOM_EBADFD;
3956 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3957 if (!listen_session)
3958 return VPPCOM_EBADFD;
3959 return vcl_session_handle (listen_session);
3960}
3961
3962int
3963vppcom_session_n_accepted (uint32_t session_handle)
3964{
3965 vcl_worker_t *wrk = vcl_worker_get_current ();
3966 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3967 if (!session)
3968 return VPPCOM_EBADFD;
3969 return session->n_accepted_sessions;
3970}
3971
Florin Coras66ec4672020-06-15 07:59:40 -07003972const char *
3973vppcom_proto_str (vppcom_proto_t proto)
3974{
3975 char const *proto_str;
3976
3977 switch (proto)
3978 {
3979 case VPPCOM_PROTO_TCP:
3980 proto_str = "TCP";
3981 break;
3982 case VPPCOM_PROTO_UDP:
3983 proto_str = "UDP";
3984 break;
3985 case VPPCOM_PROTO_TLS:
3986 proto_str = "TLS";
3987 break;
3988 case VPPCOM_PROTO_QUIC:
3989 proto_str = "QUIC";
3990 break;
3991 default:
3992 proto_str = "UNKNOWN";
3993 break;
3994 }
3995 return proto_str;
3996}
3997
3998const char *
3999vppcom_retval_str (int retval)
4000{
4001 char const *st;
4002
4003 switch (retval)
4004 {
4005 case VPPCOM_OK:
4006 st = "VPPCOM_OK";
4007 break;
4008
4009 case VPPCOM_EAGAIN:
4010 st = "VPPCOM_EAGAIN";
4011 break;
4012
4013 case VPPCOM_EFAULT:
4014 st = "VPPCOM_EFAULT";
4015 break;
4016
4017 case VPPCOM_ENOMEM:
4018 st = "VPPCOM_ENOMEM";
4019 break;
4020
4021 case VPPCOM_EINVAL:
4022 st = "VPPCOM_EINVAL";
4023 break;
4024
4025 case VPPCOM_EBADFD:
4026 st = "VPPCOM_EBADFD";
4027 break;
4028
4029 case VPPCOM_EAFNOSUPPORT:
4030 st = "VPPCOM_EAFNOSUPPORT";
4031 break;
4032
4033 case VPPCOM_ECONNABORTED:
4034 st = "VPPCOM_ECONNABORTED";
4035 break;
4036
4037 case VPPCOM_ECONNRESET:
4038 st = "VPPCOM_ECONNRESET";
4039 break;
4040
4041 case VPPCOM_ENOTCONN:
4042 st = "VPPCOM_ENOTCONN";
4043 break;
4044
4045 case VPPCOM_ECONNREFUSED:
4046 st = "VPPCOM_ECONNREFUSED";
4047 break;
4048
4049 case VPPCOM_ETIMEDOUT:
4050 st = "VPPCOM_ETIMEDOUT";
4051 break;
4052
4053 default:
4054 st = "UNKNOWN_STATE";
4055 break;
4056 }
4057
4058 return st;
4059}
4060
Dave Wallacee22aa742017-10-20 12:30:38 -04004061/*
4062 * fd.io coding-style-patch-verification: ON
4063 *
4064 * Local Variables:
4065 * eval: (c-set-style "gnu")
4066 * End:
4067 */