blob: 229a251846f10f3c1dbb54bb7d9a83324cb6f22e [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 Coras30e79c22019-01-02 19:31:22 -0800768static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700769vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
770{
771 session_cleanup_msg_t *msg;
772 vcl_session_t *session;
773
774 msg = (session_cleanup_msg_t *) data;
775 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
776 if (!session)
777 {
778 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
779 return;
780 }
781
Florin Coras36d49392020-04-24 23:00:11 +0000782 if (msg->type == SESSION_CLEANUP_TRANSPORT)
783 {
784 /* Transport was cleaned up before we confirmed close. Probably the
785 * app is still waiting for some data that cannot be delivered.
786 * Confirm close to make sure everything is cleaned up */
Florin Coras0ff7eec2020-08-03 18:55:40 -0700787 if (session->session_state == VCL_STATE_VPP_CLOSING
788 || session->session_state == VCL_STATE_DISCONNECT)
789 {
790 vcl_session_cleanup (wrk, session, vcl_session_handle (session),
791 1 /* do_disconnect */ );
792 /* Move to undetermined state to ensure that the session is not
793 * removed before both vpp and the app cleanup.
794 * - If the app closes first, the session is moved to CLOSED state
795 * and the session cleanup notification from vpp removes the
796 * session.
797 * - If vpp cleans up the session first, the session is moved to
798 * DETACHED state lower and subsequently the close from the app
799 * frees the session
800 */
801 session->session_state = VCL_STATE_UPDATED;
802 }
Florin Coras36d49392020-04-24 23:00:11 +0000803 return;
804 }
805
Florin Coras9ace36d2019-10-28 13:14:17 -0700806 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000807 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700808 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000809 {
810 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700811 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000812 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
813 return;
814 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700815 vcl_session_free (wrk, session);
816}
817
818static void
Florin Coras30e79c22019-01-02 19:31:22 -0800819vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
820{
821 session_req_worker_update_msg_t *msg;
822 vcl_session_t *s;
823
824 msg = (session_req_worker_update_msg_t *) data;
825 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
826 if (!s)
827 return;
828
829 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
830}
831
832static void
833vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
834{
835 session_worker_update_reply_msg_t *msg;
836 vcl_session_t *s;
837
838 msg = (session_worker_update_reply_msg_t *) data;
839 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
840 if (!s)
841 {
842 VDBG (0, "unknown handle 0x%llx", msg->handle);
843 return;
844 }
Florin Corasc4c4cf52019-08-24 18:17:34 -0700845 if (vcl_segment_is_not_mounted (wrk, msg->segment_handle))
Florin Coras30e79c22019-01-02 19:31:22 -0800846 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700847 clib_warning ("segment for session %u is not mounted!",
Florin Coras30e79c22019-01-02 19:31:22 -0800848 s->session_index);
849 return;
850 }
Florin Coras30e79c22019-01-02 19:31:22 -0800851
Florin Coras5f45e012019-01-23 09:21:30 -0800852 if (s->rx_fifo)
853 {
854 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
855 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
856 s->rx_fifo->client_session_index = s->session_index;
857 s->tx_fifo->client_session_index = s->session_index;
858 s->rx_fifo->client_thread_index = wrk->wrk_index;
859 s->tx_fifo->client_thread_index = wrk->wrk_index;
860 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700861 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800862
Florin Coras30e79c22019-01-02 19:31:22 -0800863 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
864 s->vpp_handle, wrk->wrk_index);
865}
866
Florin Coras935ce752020-09-08 22:43:47 -0700867static int
868vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
869{
870
871 if (vcm->cfg.vpp_app_socket_api)
872 return vcl_sapi_recv_fds (wrk, fds, n_fds);
873
874 return vcl_bapi_recv_fds (wrk, fds, n_fds);
875}
876
Florin Corasc4c4cf52019-08-24 18:17:34 -0700877static void
878vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
879{
880 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
881 session_app_add_segment_msg_t *msg;
882 u64 segment_handle;
883 int fd = -1;
884
885 msg = (session_app_add_segment_msg_t *) data;
886
887 if (msg->fd_flags)
888 {
Florin Coras935ce752020-09-08 22:43:47 -0700889 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700890 seg_type = SSVM_SEGMENT_MEMFD;
891 }
892
893 segment_handle = msg->segment_handle;
894 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
895 {
896 clib_warning ("invalid segment handle");
897 return;
898 }
899
900 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
901 seg_type, fd))
902 {
903 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
904 return;
905 }
906
907 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
908 msg->segment_size);
909}
910
911static void
912vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
913{
914 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
915 vcl_segment_detach (msg->segment_handle);
916 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
917}
918
Florin Coras40c07ce2020-07-16 20:46:17 -0700919static void
920vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
921{
922 if (!vcm->wrk_rpc_fn)
923 return;
924
hanlina3a48962020-07-13 11:09:15 +0800925 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700926}
927
Florin Coras86f04502018-09-12 16:08:01 -0700928static int
929vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700930{
Florin Coras54693d22018-07-17 10:46:29 -0700931 session_disconnected_msg_t *disconnected_msg;
Florin Corasc127d5a2020-10-14 16:35:58 -0700932 vcl_session_t *s;
Florin Coras54693d22018-07-17 10:46:29 -0700933
934 switch (e->event_type)
935 {
Florin Coras653e43f2019-03-04 10:56:23 -0800936 case SESSION_IO_EVT_RX:
937 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700938 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -0700939 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -0800940 break;
Florin Coras86f04502018-09-12 16:08:01 -0700941 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700942 break;
943 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800944 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700945 break;
946 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700947 vcl_session_connected_handler (wrk,
948 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700949 break;
950 case SESSION_CTRL_EVT_DISCONNECTED:
951 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasc127d5a2020-10-14 16:35:58 -0700952 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
953 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800954 break;
Florin Corasc127d5a2020-10-14 16:35:58 -0700955 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
956 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700957 break;
958 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700959 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700960 break;
961 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700962 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700963 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800964 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
965 vcl_session_unlisten_reply_handler (wrk, e->data);
966 break;
Florin Coras68b7e582020-01-21 18:33:23 -0800967 case SESSION_CTRL_EVT_MIGRATED:
968 vcl_session_migrated_handler (wrk, e->data);
969 break;
Florin Coras9ace36d2019-10-28 13:14:17 -0700970 case SESSION_CTRL_EVT_CLEANUP:
971 vcl_session_cleanup_handler (wrk, e->data);
972 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800973 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
974 vcl_session_req_worker_update_handler (wrk, e->data);
975 break;
976 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
977 vcl_session_worker_update_reply_handler (wrk, e->data);
978 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700979 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
980 vcl_session_app_add_segment_handler (wrk, e->data);
981 break;
982 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
983 vcl_session_app_del_segment_handler (wrk, e->data);
984 break;
hanlina3a48962020-07-13 11:09:15 +0800985 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -0700986 vcl_worker_rpc_handler (wrk, e->data);
987 break;
Florin Coras54693d22018-07-17 10:46:29 -0700988 default:
989 clib_warning ("unhandled %u", e->event_type);
990 }
991 return VPPCOM_OK;
992}
993
Florin Coras30e79c22019-01-02 19:31:22 -0800994static int
Florin Coras697faea2018-06-27 17:10:49 -0700995vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800996 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700997 f64 wait_for_time)
998{
Florin Coras134a9962018-08-28 11:32:04 -0700999 vcl_worker_t *wrk = vcl_worker_get_current ();
1000 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001001 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001002 svm_msg_q_msg_t msg;
1003 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001004
Florin Coras697faea2018-06-27 17:10:49 -07001005 do
Dave Wallace543852a2017-08-03 02:11:34 -04001006 {
Florin Coras134a9962018-08-28 11:32:04 -07001007 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001008 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001009 {
Florin Coras070453d2018-08-24 17:04:27 -07001010 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001011 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001012 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001013 {
Florin Coras697faea2018-06-27 17:10:49 -07001014 return VPPCOM_OK;
1015 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001016 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001017 {
Florin Coras697faea2018-06-27 17:10:49 -07001018 return VPPCOM_ECONNREFUSED;
1019 }
Florin Coras54693d22018-07-17 10:46:29 -07001020
Florin Coras134a9962018-08-28 11:32:04 -07001021 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001022 {
1023 usleep (100);
1024 continue;
1025 }
Florin Coras134a9962018-08-28 11:32:04 -07001026 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001027 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001028 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001029 }
Florin Coras134a9962018-08-28 11:32:04 -07001030 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001031
Florin Coras05ecfcc2018-12-12 18:19:39 -08001032 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001033 vppcom_session_state_str (state));
1034 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001035
Florin Coras697faea2018-06-27 17:10:49 -07001036 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001037}
1038
Florin Coras30e79c22019-01-02 19:31:22 -08001039static void
1040vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1041{
Florin Coras288eaab2019-02-03 15:26:14 -08001042 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001043 vcl_session_t *s;
1044 u32 *sip;
1045
1046 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1047 return;
1048
1049 vec_foreach (sip, wrk->pending_session_wrk_updates)
1050 {
1051 s = vcl_session_get (wrk, *sip);
1052 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1053 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001054 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1055 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001056 s->session_state = state;
1057 }
1058 vec_reset_length (wrk->pending_session_wrk_updates);
1059}
1060
Florin Corasf9240dc2019-01-15 08:03:17 -08001061void
Florin Coras30e79c22019-01-02 19:31:22 -08001062vcl_flush_mq_events (void)
1063{
1064 vcl_worker_t *wrk = vcl_worker_get_current ();
1065 svm_msg_q_msg_t *msg;
1066 session_event_t *e;
1067 svm_msg_q_t *mq;
1068 int i;
1069
1070 mq = wrk->app_event_queue;
1071 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -07001072 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001073 svm_msg_q_unlock (mq);
1074
1075 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1076 {
1077 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1078 e = svm_msg_q_msg_data (mq, msg);
1079 vcl_handle_mq_event (wrk, e);
1080 svm_msg_q_free_msg (mq, msg);
1081 }
1082 vec_reset_length (wrk->mq_msg_vector);
1083 vcl_handle_pending_wrk_updates (wrk);
1084}
1085
Florin Coras697faea2018-06-27 17:10:49 -07001086static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001087vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001088{
Florin Coras134a9962018-08-28 11:32:04 -07001089 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001090 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001091 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001092 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001093
Florin Corasab2f6db2018-08-31 14:31:41 -07001094 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001095 if (!session)
1096 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001097
Florin Corasaaff5ee2019-07-08 13:00:00 -07001098 /* Flush pending accept events, if any */
1099 while (clib_fifo_elts (session->accept_evts_fifo))
1100 {
1101 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1102 accepted_msg = &evt->accepted_msg;
1103 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1104 vcl_send_session_accepted_reply (session->vpp_evt_q,
1105 accepted_msg->context,
1106 session->vpp_handle, -1);
1107 }
1108 clib_fifo_free (session->accept_evts_fifo);
1109
Florin Coras458089b2019-08-21 16:20:44 -07001110 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001111
Florin Coras5e062572019-03-14 19:07:51 -07001112 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001113 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001114 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001115
1116 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001117 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001118
Florin Coras070453d2018-08-24 17:04:27 -07001119 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001120}
1121
Florin Coras697faea2018-06-27 17:10:49 -07001122static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001123vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001124{
Florin Coras134a9962018-08-28 11:32:04 -07001125 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -07001126 svm_msg_q_t *vpp_evt_q;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001127 vcl_session_t *session, *listen_session;
Florin Coras288eaab2019-02-03 15:26:14 -08001128 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -07001129 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001130
Florin Corasab2f6db2018-08-31 14:31:41 -07001131 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -07001132 if (!session)
1133 return VPPCOM_EBADFD;
1134
Dave Wallace4878cbe2017-11-21 03:45:09 -05001135 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001136 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001137
Florin Coras5e062572019-03-14 19:07:51 -07001138 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
1139 vpp_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001140
Florin Corasc127d5a2020-10-14 16:35:58 -07001141 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001142 {
Florin Coras5e062572019-03-14 19:07:51 -07001143 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
Florin Coras070453d2018-08-24 17:04:27 -07001144 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001145 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001146
Florin Corasc127d5a2020-10-14 16:35:58 -07001147 if (state == VCL_STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001148 {
Florin Coras134a9962018-08-28 11:32:04 -07001149 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Corascc7c88e2020-09-15 15:56:51 -07001150 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->api_client_handle,
Florin Coras99368312018-08-02 10:45:44 -07001151 vpp_handle, 0);
Florin Coras5e062572019-03-14 19:07:51 -07001152 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
1153 session->session_index, vpp_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001154 }
1155 else
Dave Wallace227867f2017-11-13 21:21:53 -05001156 {
Florin Corasa5ea8212020-08-24 21:23:51 -07001157 /* Session doesn't have an event queue yet. Probably a non-blocking
1158 * connect. Wait for the reply */
1159 if (PREDICT_FALSE (!session->vpp_evt_q))
1160 return VPPCOM_OK;
1161
Florin Coras5e062572019-03-14 19:07:51 -07001162 VDBG (1, "session %u [0x%llx]: sending disconnect...",
1163 session->session_index, vpp_handle);
Florin Coras458089b2019-08-21 16:20:44 -07001164 vcl_send_session_disconnect (wrk, session);
Dave Wallace227867f2017-11-13 21:21:53 -05001165 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001166
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001167 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
1168 {
1169 listen_session = vcl_session_get (wrk, session->listener_index);
1170 listen_session->n_accepted_sessions--;
1171 }
1172
Florin Coras070453d2018-08-24 17:04:27 -07001173 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001174}
1175
Florin Coras940f78f2018-11-30 12:11:20 -08001176/**
1177 * Handle app exit
1178 *
1179 * Notify vpp of the disconnect and mark the worker as free. If we're the
1180 * last worker, do a full cleanup otherwise, since we're probably a forked
1181 * child, avoid syscalls as much as possible. We might've lost privileges.
1182 */
1183void
1184vppcom_app_exit (void)
1185{
1186 if (!pool_elts (vcm->workers))
1187 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001188 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1189 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001190 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001191}
1192
Florin Coras935ce752020-09-08 22:43:47 -07001193static int
1194vcl_api_attach (void)
1195{
1196 if (vcm->cfg.vpp_app_socket_api)
1197 return vcl_sapi_attach ();
1198
1199 return vcl_bapi_attach ();
1200}
1201
1202static void
1203vcl_api_detach (vcl_worker_t * wrk)
1204{
1205 vcl_send_app_detach (wrk);
1206
1207 if (vcm->cfg.vpp_app_socket_api)
1208 return vcl_sapi_detach (wrk);
1209
1210 return vcl_bapi_disconnect_from_vpp ();
1211}
1212
Dave Wallace543852a2017-08-03 02:11:34 -04001213/*
1214 * VPPCOM Public API functions
1215 */
1216int
Florin Coras66ec4672020-06-15 07:59:40 -07001217vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001218{
Dave Wallace543852a2017-08-03 02:11:34 -04001219 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001220 int rv;
1221
Florin Coras47c40e22018-11-26 17:01:36 -08001222 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001223 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001224 VDBG (1, "already initialized");
1225 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001226 }
1227
Florin Coras47c40e22018-11-26 17:01:36 -08001228 vcm->is_init = 1;
1229 vppcom_cfg (&vcm->cfg);
1230 vcl_cfg = &vcm->cfg;
1231
1232 vcm->main_cpu = pthread_self ();
1233 vcm->main_pid = getpid ();
1234 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001235 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1236 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001237 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1238 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001239 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001240 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001241 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001242
1243 /* Allocate default worker */
1244 vcl_worker_alloc_and_init ();
1245
Florin Coras935ce752020-09-08 22:43:47 -07001246 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001247 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001248
1249 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001250 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001251
1252 return VPPCOM_OK;
1253}
1254
1255void
1256vppcom_app_destroy (void)
1257{
Florin Corasdc0ded72020-04-30 02:59:55 +00001258 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001259 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001260
Florin Coras940f78f2018-11-30 12:11:20 -08001261 if (!pool_elts (vcm->workers))
1262 return;
1263
Florin Coras0d427d82018-06-27 03:24:07 -07001264 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001265
Florin Corasdc0ded72020-04-30 02:59:55 +00001266 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001267
Florin Corasce815de2020-04-16 18:47:27 +00001268 /* *INDENT-OFF* */
1269 pool_foreach (wrk, vcm->workers, ({
Florin Corasdc0ded72020-04-30 02:59:55 +00001270 if (current_wrk != wrk)
1271 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Florin Corasce815de2020-04-16 18:47:27 +00001272 }));
1273 /* *INDENT-ON* */
1274
Florin Coras935ce752020-09-08 22:43:47 -07001275 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001276 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1277
Florin Coras0d427d82018-06-27 03:24:07 -07001278 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001279
1280 /*
1281 * Free the heap and fix vcm
1282 */
1283 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001284 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001285
1286 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001287 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001288}
1289
1290int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001291vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001292{
Florin Coras134a9962018-08-28 11:32:04 -07001293 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001294 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001295
Florin Coras134a9962018-08-28 11:32:04 -07001296 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001297
Florin Coras7e12d942018-06-27 14:32:43 -07001298 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001299 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001300 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001301 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001302
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001303 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001304 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001305
Florin Coras7e12d942018-06-27 14:32:43 -07001306 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1307 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001308
Florin Coras5e062572019-03-14 19:07:51 -07001309 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001310
Florin Coras134a9962018-08-28 11:32:04 -07001311 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001312}
1313
1314int
Florin Corasc127d5a2020-10-14 16:35:58 -07001315vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001316 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001317{
Florin Coras134a9962018-08-28 11:32:04 -07001318 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001319
Florin Coras6c3b2182020-10-19 18:36:48 -07001320 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001321
Florin Coras6c3b2182020-10-19 18:36:48 -07001322 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001323 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001324 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001325 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001326 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001327 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001328 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001329 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001330 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1331 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001332 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001333 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001334 goto cleanup;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001335 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001336
Florin Coras6c3b2182020-10-19 18:36:48 -07001337 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001338 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001339 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001340 if (rv < 0)
1341 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001342 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1343 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001344 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001345
Florin Coras9ace36d2019-10-28 13:14:17 -07001346 if (!do_disconnect)
1347 {
1348 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001349 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001350 goto cleanup;
1351 }
1352
Florin Coras6c3b2182020-10-19 18:36:48 -07001353 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001354 {
1355 rv = vppcom_session_unbind (sh);
1356 if (PREDICT_FALSE (rv < 0))
1357 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001358 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001359 vppcom_retval_str (rv));
1360 return rv;
1361 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001362 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001363 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001364 {
1365 rv = vppcom_session_disconnect (sh);
1366 if (PREDICT_FALSE (rv < 0))
1367 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001368 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001369 rv, vppcom_retval_str (rv));
1370 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001371 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001372 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001373 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, s);
Florin Corascc7c88e2020-09-15 15:56:51 -07001374 vcl_send_session_reset_reply (mq, wrk->api_client_handle,
Florin Corasc127d5a2020-10-14 16:35:58 -07001375 s->vpp_handle, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001376 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001377 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001378 {
1379 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001380 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001381 goto free_session;
1382 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001383
Florin Corasc127d5a2020-10-14 16:35:58 -07001384 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001385
Florin Coras9ace36d2019-10-28 13:14:17 -07001386 /* Session is removed only after vpp confirms the disconnect */
1387 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001388
Florin Corasf9240dc2019-01-15 08:03:17 -08001389cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001390 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001391free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001392 vcl_session_free (wrk, s);
1393 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001394
Dave Wallace543852a2017-08-03 02:11:34 -04001395 return rv;
1396}
1397
1398int
Florin Corasf9240dc2019-01-15 08:03:17 -08001399vppcom_session_close (uint32_t session_handle)
1400{
1401 vcl_worker_t *wrk = vcl_worker_get_current ();
1402 vcl_session_t *session;
1403
1404 session = vcl_session_get_w_handle (wrk, session_handle);
1405 if (!session)
1406 return VPPCOM_EBADFD;
1407 return vcl_session_cleanup (wrk, session, session_handle,
1408 1 /* do_disconnect */ );
1409}
1410
1411int
Florin Coras134a9962018-08-28 11:32:04 -07001412vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001413{
Florin Coras134a9962018-08-28 11:32:04 -07001414 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001415 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001416
1417 if (!ep || !ep->ip)
1418 return VPPCOM_EINVAL;
1419
Florin Coras134a9962018-08-28 11:32:04 -07001420 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001421 if (!session)
1422 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001423
Florin Coras6c3b2182020-10-19 18:36:48 -07001424 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001425 {
Florin Coras5e062572019-03-14 19:07:51 -07001426 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1427 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001428 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001429 }
1430
Florin Coras7e12d942018-06-27 14:32:43 -07001431 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001432 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001433 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1434 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001435 else
Dave Barach178cf492018-11-13 16:34:13 -05001436 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1437 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001438 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001439
Florin Coras5e062572019-03-14 19:07:51 -07001440 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1441 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001442 session->transport.is_ip4 ? "IPv4" : "IPv6",
1443 format_ip46_address, &session->transport.lcl_ip,
1444 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1445 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001446 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001447 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001448
1449 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001450 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001451
Florin Coras070453d2018-08-24 17:04:27 -07001452 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001453}
1454
1455int
Florin Coras134a9962018-08-28 11:32:04 -07001456vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001457{
Florin Coras134a9962018-08-28 11:32:04 -07001458 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001459 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001460 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001461 int rv;
1462
Florin Coras134a9962018-08-28 11:32:04 -07001463 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001464 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001465 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001466
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001467 if (q_len == 0 || q_len == ~0)
1468 q_len = vcm->cfg.listen_queue_size;
1469
Dave Wallaceee45d412017-11-24 21:44:06 -05001470 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001471 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001472 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001473 VDBG (0, "session %u [0x%llx]: already in listen state!",
1474 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001475 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001476 }
1477
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001478 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001479
Florin Coras070453d2018-08-24 17:04:27 -07001480 /*
1481 * Send listen request to vpp and wait for reply
1482 */
Florin Coras458089b2019-08-21 16:20:44 -07001483 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001484 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001485 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001486 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001487
Florin Coras070453d2018-08-24 17:04:27 -07001488 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001489 {
Florin Coras134a9962018-08-28 11:32:04 -07001490 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001491 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1492 listen_sh, listen_session->vpp_handle, rv,
1493 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001494 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001495 }
1496
Florin Coras070453d2018-08-24 17:04:27 -07001497 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001498}
1499
Florin Coras134a9962018-08-28 11:32:04 -07001500static int
Florin Coras5e062572019-03-14 19:07:51 -07001501validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001502{
Florin Coras6c3b2182020-10-19 18:36:48 -07001503 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001504 {
Florin Coras5e062572019-03-14 19:07:51 -07001505 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1506 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001507 return VPPCOM_EBADFD;
1508 }
1509
Florin Corasc127d5a2020-10-14 16:35:58 -07001510 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001511 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001512 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001513 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001514 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001515 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001516 return VPPCOM_EBADFD;
1517 }
1518 return VPPCOM_OK;
1519}
1520
1521int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001522vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1523{
1524 if (!strcmp (proto_str, "TCP"))
1525 *proto = VPPCOM_PROTO_TCP;
1526 else if (!strcmp (proto_str, "tcp"))
1527 *proto = VPPCOM_PROTO_TCP;
1528 else if (!strcmp (proto_str, "UDP"))
1529 *proto = VPPCOM_PROTO_UDP;
1530 else if (!strcmp (proto_str, "udp"))
1531 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001532 else if (!strcmp (proto_str, "TLS"))
1533 *proto = VPPCOM_PROTO_TLS;
1534 else if (!strcmp (proto_str, "tls"))
1535 *proto = VPPCOM_PROTO_TLS;
1536 else if (!strcmp (proto_str, "QUIC"))
1537 *proto = VPPCOM_PROTO_QUIC;
1538 else if (!strcmp (proto_str, "quic"))
1539 *proto = VPPCOM_PROTO_QUIC;
1540 else
1541 return 1;
1542 return 0;
1543}
1544
1545int
Florin Coras134a9962018-08-28 11:32:04 -07001546vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001547 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001548{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001549 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001550 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001551 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001552 vcl_session_t *listen_session = 0;
1553 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001554 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001555 svm_msg_q_msg_t msg;
1556 session_event_t *e;
1557 u8 is_nonblocking;
1558 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001559
Florin Coras134a9962018-08-28 11:32:04 -07001560 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001561 if (!listen_session)
1562 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001563
Florin Coras134a9962018-08-28 11:32:04 -07001564 listen_session_index = listen_session->session_index;
1565 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001566 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001567
Florin Coras54693d22018-07-17 10:46:29 -07001568 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001569 {
Florin Coras54693d22018-07-17 10:46:29 -07001570 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001571 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001572 accepted_msg = evt->accepted_msg;
1573 goto handle;
1574 }
1575
Florin Corasac422d62020-10-19 20:51:36 -07001576 is_nonblocking = vcl_session_has_attr (listen_session,
1577 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001578 while (1)
1579 {
Carl Smith592a9092019-11-12 14:57:37 +13001580 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1581 return VPPCOM_EAGAIN;
1582
Florin Coras134a9962018-08-28 11:32:04 -07001583 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001584 return VPPCOM_EAGAIN;
1585
Florin Coras134a9962018-08-28 11:32:04 -07001586 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001587 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001588 {
wanghanlin96453fd2019-12-16 19:14:39 +08001589 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001590 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001591 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001592 }
Dave Barach178cf492018-11-13 16:34:13 -05001593 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001594 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001595 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001596 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001597
Florin Coras54693d22018-07-17 10:46:29 -07001598handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001599
Florin Coras00cca802019-06-06 09:38:44 -07001600 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1601 listen_session_index);
1602 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1603 return VPPCOM_ECONNABORTED;
1604
Florin Coras134a9962018-08-28 11:32:04 -07001605 listen_session = vcl_session_get (wrk, listen_session_index);
1606 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001607
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001608 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001609 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001610
Florin Coras5e062572019-03-14 19:07:51 -07001611 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1612 " flags %d, is_nonblocking %u", listen_session->session_index,
1613 listen_session->vpp_handle, client_session_index,
1614 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001615 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001616
Dave Wallace048b1d62018-01-03 22:24:41 -05001617 if (ep)
1618 {
Florin Coras7e12d942018-06-27 14:32:43 -07001619 ep->is_ip4 = client_session->transport.is_ip4;
1620 ep->port = client_session->transport.rmt_port;
1621 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001622 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1623 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001624 else
Dave Barach178cf492018-11-13 16:34:13 -05001625 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1626 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001627 }
Dave Wallace60caa062017-11-10 17:07:13 -05001628
Florin Coras05ecfcc2018-12-12 18:19:39 -08001629 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001630 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001631 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001632 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001633 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001634 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001635 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001636 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001637 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001638 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1639 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001640
Florin Coras3c7d4f92018-12-14 11:28:43 -08001641 /*
1642 * Session might have been closed already
1643 */
1644 if (accept_flags)
1645 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001646 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001647 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001648 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001649 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001650 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001651 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001652}
1653
1654int
Florin Coras134a9962018-08-28 11:32:04 -07001655vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001656{
Florin Coras134a9962018-08-28 11:32:04 -07001657 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001658 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001659 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001660 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001661
Florin Coras134a9962018-08-28 11:32:04 -07001662 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001663 if (!session)
1664 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001665 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001666
Florin Coras6c3b2182020-10-19 18:36:48 -07001667 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001668 {
Florin Coras5e062572019-03-14 19:07:51 -07001669 VDBG (0, "ERROR: cannot connect epoll session %u!",
1670 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001671 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001672 }
1673
Florin Corasc127d5a2020-10-14 16:35:58 -07001674 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001675 {
Florin Coras7baeb712019-01-04 17:05:43 -08001676 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001677 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001678 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001679 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001680 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001681 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001682 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001683 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001684 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001685 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001686 }
1687
Florin Coras0a1e1832020-03-29 18:54:04 +00001688 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001689 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001690 {
1691 if (session->session_type != VPPCOM_PROTO_UDP)
1692 return VPPCOM_EINVAL;
1693 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001694 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001695 }
1696
Florin Coras7e12d942018-06-27 14:32:43 -07001697 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001698 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001699 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001700 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001701 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001702
Florin Coras0a1e1832020-03-29 18:54:04 +00001703 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001704 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001705 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001706 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001707 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001708 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001709 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001710 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001711 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001712
Florin Coras458089b2019-08-21 16:20:44 -07001713 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001714
Florin Corasac422d62020-10-19 20:51:36 -07001715 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001716 {
fanyfa49d59d2020-10-13 17:07:16 +08001717 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001718 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001719 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001720 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001721 return VPPCOM_EINPROGRESS;
1722 }
Florin Coras57c88932019-08-29 12:03:17 -07001723
1724 /*
1725 * Wait for reply from vpp if blocking
1726 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001727 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001728 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001729
Florin Coras134a9962018-08-28 11:32:04 -07001730 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001731 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1732 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001733
Dave Wallace4878cbe2017-11-21 03:45:09 -05001734 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001735}
1736
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001737int
1738vppcom_session_stream_connect (uint32_t session_handle,
1739 uint32_t parent_session_handle)
1740{
1741 vcl_worker_t *wrk = vcl_worker_get_current ();
1742 vcl_session_t *session, *parent_session;
1743 u32 session_index, parent_session_index;
1744 int rv;
1745
1746 session = vcl_session_get_w_handle (wrk, session_handle);
1747 if (!session)
1748 return VPPCOM_EBADFD;
1749 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1750 if (!parent_session)
1751 return VPPCOM_EBADFD;
1752
1753 session_index = session->session_index;
1754 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001755 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001756 {
1757 VDBG (0, "ERROR: cannot connect epoll session %u!",
1758 session->session_index);
1759 return VPPCOM_EBADFD;
1760 }
1761
Florin Corasc127d5a2020-10-14 16:35:58 -07001762 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001763 {
1764 VDBG (0, "session handle %u [0x%llx]: session already "
1765 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1766 session_handle, session->vpp_handle,
1767 parent_session_handle, parent_session->vpp_handle,
1768 vppcom_proto_str (session->session_type), session->session_state,
1769 vppcom_session_state_str (session->session_state));
1770 return VPPCOM_OK;
1771 }
1772
1773 /* Connect to quic session specifics */
1774 session->transport.is_ip4 = parent_session->transport.is_ip4;
1775 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1776 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001777 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001778
1779 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1780 session_handle, parent_session_handle, parent_session->vpp_handle);
1781
1782 /*
1783 * Send connect request and wait for reply from vpp
1784 */
Florin Coras458089b2019-08-21 16:20:44 -07001785 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001786 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001787 vcm->cfg.session_timeout);
1788
1789 session->listener_index = parent_session_index;
1790 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001791 if (parent_session)
1792 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001793
1794 session = vcl_session_get (wrk, session_index);
1795 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1796 session->vpp_handle, rv ? "failed" : "succeeded");
1797
1798 return rv;
1799}
1800
Florin Coras54693d22018-07-17 10:46:29 -07001801static u8
1802vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1803{
Florin Corasc0737e92019-03-04 14:19:39 -08001804 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001805}
1806
Steven58f464e2017-10-25 12:33:12 -07001807static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001808vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001809 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001810{
Florin Coras134a9962018-08-28 11:32:04 -07001811 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001812 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001813 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001814 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001815 svm_msg_q_msg_t msg;
1816 session_event_t *e;
1817 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001818 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001819
Florin Coras070453d2018-08-24 17:04:27 -07001820 if (PREDICT_FALSE (!buf))
1821 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001822
Florin Coras134a9962018-08-28 11:32:04 -07001823 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001824 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001825 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001826
Florin Coras6d0106e2019-01-29 20:11:58 -08001827 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001828 {
Florin Coras5e062572019-03-14 19:07:51 -07001829 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001830 s->session_index, s->vpp_handle, s->session_state,
1831 vppcom_session_state_str (s->session_state));
1832 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001833 }
1834
Florin Corasac422d62020-10-19 20:51:36 -07001835 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001836 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001837 mq = wrk->app_event_queue;
1838 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001839 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001840
Sirshak Das28aa5392019-02-05 01:33:33 -06001841 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001842 {
Florin Coras54693d22018-07-17 10:46:29 -07001843 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001844 {
Yu Pingb2955352019-12-04 06:49:04 +08001845 if (vcl_session_is_closing (s))
1846 return vcl_session_closing_error (s);
Florin Corasc0737e92019-03-04 14:19:39 -08001847 svm_fifo_unset_event (s->rx_fifo);
1848 return VPPCOM_EWOULDBLOCK;
1849 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001850 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001851 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001852 if (vcl_session_is_closing (s))
1853 return vcl_session_closing_error (s);
1854
Florin Corasc0737e92019-03-04 14:19:39 -08001855 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001856 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001857 if (svm_msg_q_is_empty (mq))
1858 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001859
Florin Coras54693d22018-07-17 10:46:29 -07001860 svm_msg_q_sub_w_lock (mq, &msg);
1861 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001862 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001863 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001864 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001865 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001866 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001867 }
Florin Coras54693d22018-07-17 10:46:29 -07001868
Florin Coras4a2c7942020-09-10 12:27:14 -07001869read_again:
1870
Florin Coras460dce62018-07-27 05:45:06 -07001871 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001872 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001873 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001874 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1875
1876 ASSERT (rv >= 0);
1877 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001878
Sirshak Das28aa5392019-02-05 01:33:33 -06001879 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001880 {
1881 svm_fifo_unset_event (s->rx_fifo);
1882 if (!svm_fifo_is_empty_cons (rx_fifo)
1883 && svm_fifo_set_event (s->rx_fifo) && is_nonblocking)
1884 {
Florin Corasc34118b2020-08-12 18:58:25 -07001885 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1886 e->event_type = SESSION_IO_EVT_RX;
1887 e->session_index = s->session_index;
1888 }
1889 }
Florin Coras4a2c7942020-09-10 12:27:14 -07001890 else if (PREDICT_FALSE (rv < n))
1891 {
1892 /* More data enqueued while reading. Try to drain it
1893 * or fill the buffer */
1894 buf += rv;
1895 n -= rv;
1896 goto read_again;
1897 }
Florin Coras41c9e042018-09-11 00:10:41 -07001898
Florin Coras17ec5772020-08-12 20:46:29 -07001899 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001900 {
Florin Coras17ec5772020-08-12 20:46:29 -07001901 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001902 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1903 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001904 }
1905
Florin Coras5e062572019-03-14 19:07:51 -07001906 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1907 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001908
Florin Coras54693d22018-07-17 10:46:29 -07001909 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001910}
1911
Steven58f464e2017-10-25 12:33:12 -07001912int
Florin Coras134a9962018-08-28 11:32:04 -07001913vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001914{
Florin Coras134a9962018-08-28 11:32:04 -07001915 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001916}
1917
1918static int
Florin Coras134a9962018-08-28 11:32:04 -07001919vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001920{
Florin Coras134a9962018-08-28 11:32:04 -07001921 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001922}
1923
Florin Coras2cba8532018-09-11 16:33:36 -07001924int
1925vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001926 vppcom_data_segment_t * ds, uint32_t n_segments,
1927 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001928{
1929 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001930 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001931 vcl_session_t *s = 0;
1932 svm_fifo_t *rx_fifo;
1933 svm_msg_q_msg_t msg;
1934 session_event_t *e;
1935 svm_msg_q_t *mq;
1936 u8 is_ct;
1937
1938 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001939 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001940 return VPPCOM_EBADFD;
1941
Florin Coras6d0106e2019-01-29 20:11:58 -08001942 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1943 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001944
Florin Corasac422d62020-10-19 20:51:36 -07001945 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07001946 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07001947 mq = wrk->app_event_queue;
Florin Coras2cba8532018-09-11 16:33:36 -07001948 rx_fifo = s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001949 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07001950
Florin Coras653e43f2019-03-04 10:56:23 -08001951 if (is_ct)
1952 svm_fifo_unset_event (s->rx_fifo);
1953
Sirshak Das28aa5392019-02-05 01:33:33 -06001954 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001955 {
1956 if (is_nonblocking)
1957 {
1958 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001959 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001960 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001961 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001962 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001963 if (vcl_session_is_closing (s))
1964 return vcl_session_closing_error (s);
1965
Florin Coras2cba8532018-09-11 16:33:36 -07001966 svm_fifo_unset_event (rx_fifo);
1967 svm_msg_q_lock (mq);
1968 if (svm_msg_q_is_empty (mq))
1969 svm_msg_q_wait (mq);
1970
1971 svm_msg_q_sub_w_lock (mq, &msg);
1972 e = svm_msg_q_msg_data (mq, &msg);
1973 svm_msg_q_unlock (mq);
1974 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001975 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001976 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001977 }
1978 }
1979
Florin Corasd1cc38d2020-10-11 11:05:04 -07001980 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
1981 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
1982 if (n_read < 0)
1983 return VPPCOM_EAGAIN;
1984
1985 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
1986 {
1987 svm_fifo_unset_event (s->rx_fifo);
1988 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
1989 && svm_fifo_set_event (s->rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07001990 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07001991 {
1992 session_event_t *e;
1993 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1994 e->event_type = SESSION_IO_EVT_RX;
1995 e->session_index = s->session_index;
1996 }
1997 }
1998
1999 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002000 return n_read;
2001}
2002
2003void
Florin Corasd68faf82020-09-25 15:18:13 -07002004vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002005{
2006 vcl_worker_t *wrk = vcl_worker_get_current ();
2007 vcl_session_t *s;
2008
2009 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002010 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002011 return;
2012
Florin Corasd68faf82020-09-25 15:18:13 -07002013 svm_fifo_dequeue_drop (s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002014
2015 ASSERT (s->rx_bytes_pending < n_bytes);
2016 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002017}
2018
Florin Coras54693d22018-07-17 10:46:29 -07002019static u8
2020vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2021{
Florin Corasc0737e92019-03-04 14:19:39 -08002022 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002023}
2024
Florin Coras7a2abce2020-04-05 19:25:44 +00002025always_inline u8
2026vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002027{
Florin Coras7a2abce2020-04-05 19:25:44 +00002028 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2029 if (is_dgram)
2030 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2031 else
2032 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002033}
2034
2035always_inline int
2036vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2037 size_t n, u8 is_flush, u8 is_dgram)
2038{
Florin Coras6d0106e2019-01-29 20:11:58 -08002039 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002040 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002041 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002042 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002043 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002044 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002045 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002046
jiangxiaomingff31ac62019-11-21 23:34:56 +08002047 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002048 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002049
Florin Coras6c3b2182020-10-19 18:36:48 -07002050 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002051 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002052 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2053 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002054 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002055 }
2056
Florin Coras6d0106e2019-01-29 20:11:58 -08002057 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002058 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002059 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2060 s->session_index, s->vpp_handle, s->session_state,
2061 vppcom_session_state_str (s->session_state));
2062 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002063 }
2064
Florin Coras0e88e852018-09-17 22:09:02 -07002065 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002066 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002067 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002068
Florin Coras653e43f2019-03-04 10:56:23 -08002069 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002070 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002071 {
Florin Coras54693d22018-07-17 10:46:29 -07002072 if (is_nonblocking)
2073 {
Florin Coras070453d2018-08-24 17:04:27 -07002074 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002075 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002076 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002077 {
Florin Coras2d379d82019-06-28 12:45:12 -07002078 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002079 if (vcl_session_is_closing (s))
2080 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002081 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002082 if (svm_msg_q_is_empty (mq))
2083 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002084
Florin Coras54693d22018-07-17 10:46:29 -07002085 svm_msg_q_sub_w_lock (mq, &msg);
2086 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002087 svm_msg_q_unlock (mq);
2088
Florin Coras0e88e852018-09-17 22:09:02 -07002089 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002090 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002091 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002092 }
Dave Wallace543852a2017-08-03 02:11:34 -04002093 }
Dave Wallace543852a2017-08-03 02:11:34 -04002094
Florin Coras653e43f2019-03-04 10:56:23 -08002095 et = SESSION_IO_EVT_TX;
2096 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002097 et = SESSION_IO_EVT_TX_FLUSH;
2098
Florin Coras7a2abce2020-04-05 19:25:44 +00002099 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002100 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002101 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002102 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002103 else
2104 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002105 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002106
Florin Coras14ed6df2019-03-06 21:13:42 -08002107 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08002108 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
2109 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002110
Florin Coras56230092020-09-02 20:52:58 -07002111 /* The underlying fifo segment can run out of memory */
2112 if (PREDICT_FALSE (n_write < 0))
2113 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002114
Florin Coras6d0106e2019-01-29 20:11:58 -08002115 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2116 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002117
Florin Coras54693d22018-07-17 10:46:29 -07002118 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002119}
2120
Florin Coras42ceddb2018-12-12 10:56:01 -08002121int
2122vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2123{
Florin Coras7a2abce2020-04-05 19:25:44 +00002124 vcl_worker_t *wrk = vcl_worker_get_current ();
2125 vcl_session_t *s;
2126
2127 s = vcl_session_get_w_handle (wrk, session_handle);
2128 if (PREDICT_FALSE (!s))
2129 return VPPCOM_EBADFD;
2130
2131 return vppcom_session_write_inline (wrk, s, buf, n,
2132 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002133}
2134
Florin Corasb0f662f2018-12-27 14:51:46 -08002135int
2136vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2137{
Florin Coras7a2abce2020-04-05 19:25:44 +00002138 vcl_worker_t *wrk = vcl_worker_get_current ();
2139 vcl_session_t *s;
2140
2141 s = vcl_session_get_w_handle (wrk, session_handle);
2142 if (PREDICT_FALSE (!s))
2143 return VPPCOM_EBADFD;
2144
2145 return vppcom_session_write_inline (wrk, s, buf, n,
2146 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002147}
2148
Florin Corasc0737e92019-03-04 14:19:39 -08002149#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002150if (PREDICT_FALSE (!_s->rx_fifo)) \
2151 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002152if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2153 { \
2154 if (!vcl_session_is_ct (_s)) \
2155 { \
2156 svm_fifo_unset_event (_s->rx_fifo); \
2157 if (svm_fifo_is_empty (_s->rx_fifo)) \
2158 break; \
2159 } \
2160 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2161 { \
Florin Coras2f630182020-08-13 00:17:51 -07002162 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002163 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2164 break; \
2165 } \
2166 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002167
Florin Coras86f04502018-09-12 16:08:01 -07002168static void
2169vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2170 unsigned long n_bits, unsigned long *read_map,
2171 unsigned long *write_map,
2172 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002173{
2174 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002175 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07002176 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002177 u32 sid;
2178
2179 switch (e->event_type)
2180 {
Florin Corasc0737e92019-03-04 14:19:39 -08002181 case SESSION_IO_EVT_RX:
2182 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002183 session = vcl_session_get (wrk, sid);
Florin Corasf49cf472020-04-19 23:12:08 +00002184 if (!session || !vcl_session_is_open (session))
Florin Coras86f04502018-09-12 16:08:01 -07002185 break;
Florin Corasfff68f72019-03-13 16:01:38 -07002186 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002187 if (sid < n_bits && read_map)
2188 {
David Johnsond9818dd2018-12-14 14:53:41 -05002189 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002190 *bits_set += 1;
2191 }
2192 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002193 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002194 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002195 session = vcl_session_get (wrk, sid);
Florin Corasf49cf472020-04-19 23:12:08 +00002196 if (!session || !vcl_session_is_open (session))
Florin Coras86f04502018-09-12 16:08:01 -07002197 break;
2198 if (sid < n_bits && write_map)
2199 {
David Johnsond9818dd2018-12-14 14:53:41 -05002200 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002201 *bits_set += 1;
2202 }
2203 break;
Florin Coras86f04502018-09-12 16:08:01 -07002204 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002205 session = vcl_session_accepted (wrk,
2206 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002207 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002208 break;
Florin Coras86f04502018-09-12 16:08:01 -07002209 sid = session->session_index;
2210 if (sid < n_bits && read_map)
2211 {
David Johnsond9818dd2018-12-14 14:53:41 -05002212 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002213 *bits_set += 1;
2214 }
2215 break;
2216 case SESSION_CTRL_EVT_CONNECTED:
2217 connected_msg = (session_connected_msg_t *) e->data;
Florin Coras57c88932019-08-29 12:03:17 -07002218 sid = vcl_session_connected_handler (wrk, connected_msg);
2219 if (sid == VCL_INVALID_SESSION_INDEX)
2220 break;
2221 if (sid < n_bits && write_map)
2222 {
2223 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2224 *bits_set += 1;
2225 }
Florin Coras86f04502018-09-12 16:08:01 -07002226 break;
2227 case SESSION_CTRL_EVT_DISCONNECTED:
2228 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002229 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2230 if (!session)
2231 break;
2232 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002233 if (sid < n_bits && except_map)
2234 {
David Johnsond9818dd2018-12-14 14:53:41 -05002235 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002236 *bits_set += 1;
2237 }
2238 break;
2239 case SESSION_CTRL_EVT_RESET:
2240 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2241 if (sid < n_bits && except_map)
2242 {
David Johnsond9818dd2018-12-14 14:53:41 -05002243 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002244 *bits_set += 1;
2245 }
2246 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002247 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2248 vcl_session_unlisten_reply_handler (wrk, e->data);
2249 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002250 case SESSION_CTRL_EVT_MIGRATED:
2251 vcl_session_migrated_handler (wrk, e->data);
2252 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002253 case SESSION_CTRL_EVT_CLEANUP:
2254 vcl_session_cleanup_handler (wrk, e->data);
2255 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002256 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2257 vcl_session_worker_update_reply_handler (wrk, e->data);
2258 break;
2259 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2260 vcl_session_req_worker_update_handler (wrk, e->data);
2261 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002262 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2263 vcl_session_app_add_segment_handler (wrk, e->data);
2264 break;
2265 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2266 vcl_session_app_del_segment_handler (wrk, e->data);
2267 break;
hanlina3a48962020-07-13 11:09:15 +08002268 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002269 vcl_worker_rpc_handler (wrk, e->data);
2270 break;
Florin Coras86f04502018-09-12 16:08:01 -07002271 default:
2272 clib_warning ("unhandled: %u", e->event_type);
2273 break;
2274 }
2275}
2276
2277static int
2278vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2279 unsigned long n_bits, unsigned long *read_map,
2280 unsigned long *write_map, unsigned long *except_map,
2281 double time_to_wait, u32 * bits_set)
2282{
Florin Coras99368312018-08-02 10:45:44 -07002283 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002284 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002285 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002286
2287 svm_msg_q_lock (mq);
2288 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002289 {
Florin Coras54693d22018-07-17 10:46:29 -07002290 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002291 {
Florin Coras54693d22018-07-17 10:46:29 -07002292 svm_msg_q_unlock (mq);
2293 return 0;
2294 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002295
Florin Coras54693d22018-07-17 10:46:29 -07002296 if (!time_to_wait)
2297 {
2298 svm_msg_q_unlock (mq);
2299 return 0;
2300 }
2301 else if (time_to_wait < 0)
2302 {
2303 svm_msg_q_wait (mq);
2304 }
2305 else
2306 {
2307 if (svm_msg_q_timedwait (mq, time_to_wait))
2308 {
2309 svm_msg_q_unlock (mq);
2310 return 0;
2311 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002312 }
2313 }
Florin Corase003a1b2019-06-05 10:47:16 -07002314 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002315 svm_msg_q_unlock (mq);
2316
Florin Coras134a9962018-08-28 11:32:04 -07002317 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002318 {
Florin Coras134a9962018-08-28 11:32:04 -07002319 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002320 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002321 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2322 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002323 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002324 }
Florin Coras134a9962018-08-28 11:32:04 -07002325 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002326 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002327 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002328}
2329
Florin Coras99368312018-08-02 10:45:44 -07002330static int
Florin Coras294afe22019-01-07 17:49:17 -08002331vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2332 vcl_si_set * read_map, vcl_si_set * write_map,
2333 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002334 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002335{
Florin Coras14ed6df2019-03-06 21:13:42 -08002336 double wait = 0, start = 0;
2337
2338 if (!*bits_set)
2339 {
2340 wait = time_to_wait;
2341 start = clib_time_now (&wrk->clib_time);
2342 }
2343
2344 do
2345 {
2346 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2347 write_map, except_map, wait, bits_set);
2348 if (*bits_set)
2349 return *bits_set;
2350 if (wait == -1)
2351 continue;
2352
2353 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2354 }
2355 while (wait > 0);
2356
2357 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002358}
2359
2360static int
Florin Coras294afe22019-01-07 17:49:17 -08002361vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2362 vcl_si_set * read_map, vcl_si_set * write_map,
2363 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002364 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002365{
2366 vcl_mq_evt_conn_t *mqc;
2367 int __clib_unused n_read;
2368 int n_mq_evts, i;
2369 u64 buf;
2370
Florin Coras134a9962018-08-28 11:32:04 -07002371 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2372 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2373 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002374 for (i = 0; i < n_mq_evts; i++)
2375 {
Florin Coras134a9962018-08-28 11:32:04 -07002376 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002377 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002378 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002379 except_map, 0, bits_set);
2380 }
2381
2382 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2383}
2384
Dave Wallace543852a2017-08-03 02:11:34 -04002385int
Florin Coras294afe22019-01-07 17:49:17 -08002386vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2387 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002388{
Florin Coras54693d22018-07-17 10:46:29 -07002389 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002390 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002391 vcl_session_t *session = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002392 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002393
Dave Wallace7876d392017-10-19 03:53:57 -04002394 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002395 {
Florin Coras134a9962018-08-28 11:32:04 -07002396 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002397 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002398 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2399 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002400 }
Dave Wallace7876d392017-10-19 03:53:57 -04002401 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002402 {
Florin Coras134a9962018-08-28 11:32:04 -07002403 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002404 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002405 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2406 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002407 }
Dave Wallace7876d392017-10-19 03:53:57 -04002408 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002409 {
Florin Coras134a9962018-08-28 11:32:04 -07002410 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002411 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002412 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2413 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002414 }
2415
Florin Coras54693d22018-07-17 10:46:29 -07002416 if (!n_bits)
2417 return 0;
2418
2419 if (!write_map)
2420 goto check_rd;
2421
2422 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002423 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2424 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002425 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002426 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2427 bits_set++;
2428 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002429 }
2430
Florin Coras5e6222a2020-04-24 17:09:25 +00002431 if (vcl_session_write_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002432 {
David Johnsond9818dd2018-12-14 14:53:41 -05002433 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002434 bits_set++;
2435 }
Florin Coras294afe22019-01-07 17:49:17 -08002436 else
Florin Coras2d379d82019-06-28 12:45:12 -07002437 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002438 }));
2439
2440check_rd:
2441 if (!read_map)
2442 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002443
Florin Coras134a9962018-08-28 11:32:04 -07002444 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2445 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002446 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002447 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2448 bits_set++;
2449 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002450 }
2451
Florin Coras5e6222a2020-04-24 17:09:25 +00002452 if (vcl_session_read_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002453 {
David Johnsond9818dd2018-12-14 14:53:41 -05002454 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002455 bits_set++;
2456 }
2457 }));
2458 /* *INDENT-ON* */
2459
2460check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002461
Florin Coras86f04502018-09-12 16:08:01 -07002462 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2463 {
2464 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2465 read_map, write_map, except_map, &bits_set);
2466 }
2467 vec_reset_length (wrk->unhandled_evts_vector);
2468
Florin Coras99368312018-08-02 10:45:44 -07002469 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002470 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002471 time_to_wait, &bits_set);
2472 else
Florin Coras134a9962018-08-28 11:32:04 -07002473 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002474 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002475
Dave Wallace543852a2017-08-03 02:11:34 -04002476 return (bits_set);
2477}
2478
Dave Wallacef7f809c2017-10-03 01:48:42 -04002479static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002480vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002481{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002482 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002483 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002484 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002485
Florin Corasc0737e92019-03-04 14:19:39 -08002486 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002487 return;
2488
Florin Coras6c3b2182020-10-19 18:36:48 -07002489 s = vcl_session_get_w_handle (wrk, vep_handle);
2490 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002491 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002492 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002493 goto done;
2494 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002495 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002496 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002497 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002498 goto done;
2499 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002500 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002501 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002502 "{\n"
2503 " is_vep = %u\n"
2504 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002505 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002506 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2507 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002508
Florin Coras7e5e62b2019-07-30 14:08:23 -07002509 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002510 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002511 s = vcl_session_get_w_handle (wrk, sh);
2512 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002513 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002514 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002515 goto done;
2516 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002517 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002518 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002519 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002520 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002521 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002522 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002523 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002524 goto done;
2525 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002526 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002527 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2528 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002529 sh, s->vep.vep_sh, vep_handle);
2530 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002531 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002532 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002533 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002534 " next_sh = 0x%x (%u)\n"
2535 " prev_sh = 0x%x (%u)\n"
2536 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002537 " ev.events = 0x%x\n"
2538 " ev.data.u64 = 0x%llx\n"
2539 " et_mask = 0x%x\n"
2540 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002541 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002542 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2543 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002544 }
2545 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002546
2547done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002548 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002549}
2550
2551int
2552vppcom_epoll_create (void)
2553{
Florin Coras134a9962018-08-28 11:32:04 -07002554 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002555 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002556
Florin Coras134a9962018-08-28 11:32:04 -07002557 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002558
Florin Coras6c3b2182020-10-19 18:36:48 -07002559 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002560 vep_session->vep.vep_sh = ~0;
2561 vep_session->vep.next_sh = ~0;
2562 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002563 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002564
Florin Corasa7a1a222018-12-30 17:11:31 -08002565 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2566 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002567
Florin Corasab2f6db2018-08-31 14:31:41 -07002568 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002569}
2570
2571int
Florin Coras134a9962018-08-28 11:32:04 -07002572vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002573 struct epoll_event *event)
2574{
Florin Coras134a9962018-08-28 11:32:04 -07002575 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002576 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002577 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002578 vcl_session_t *s;
2579 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002580
Florin Coras134a9962018-08-28 11:32:04 -07002581 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002582 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002583 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002584 return VPPCOM_EINVAL;
2585 }
2586
Florin Coras134a9962018-08-28 11:32:04 -07002587 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002588 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002589 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002590 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002591 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002592 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002593 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002594 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002595 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002596 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002597 }
2598
Florin Coras134a9962018-08-28 11:32:04 -07002599 ASSERT (vep_session->vep.vep_sh == ~0);
2600 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002601
Florin Coras17ec5772020-08-12 20:46:29 -07002602 s = vcl_session_get_w_handle (wrk, session_handle);
2603 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002604 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002605 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002606 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002607 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002608 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002609 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002610 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002611 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002612 }
2613
2614 switch (op)
2615 {
2616 case EPOLL_CTL_ADD:
2617 if (PREDICT_FALSE (!event))
2618 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002619 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002620 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002621 }
Florin Coras134a9962018-08-28 11:32:04 -07002622 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002623 {
Florin Coras7e12d942018-06-27 14:32:43 -07002624 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002625 next_session = vcl_session_get_w_handle (wrk,
2626 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002627 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002628 {
Florin Coras5e062572019-03-14 19:07:51 -07002629 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002630 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002631 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002632 }
Florin Coras134a9962018-08-28 11:32:04 -07002633 ASSERT (next_session->vep.prev_sh == vep_handle);
2634 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002635 }
Florin Coras17ec5772020-08-12 20:46:29 -07002636 s->vep.next_sh = vep_session->vep.next_sh;
2637 s->vep.prev_sh = vep_handle;
2638 s->vep.vep_sh = vep_handle;
2639 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2640 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002641 s->flags &= ~VCL_SESSION_F_IS_VEP;
2642 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002643 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002644
Florin Coras17ec5772020-08-12 20:46:29 -07002645 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2646 if (txf && (event->events & EPOLLOUT))
2647 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002648
Florin Coras6e3c1f82020-01-15 01:30:46 +00002649 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002650 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002651 {
2652 session_event_t e = { 0 };
2653 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002654 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002655 vec_add1 (wrk->unhandled_evts_vector, e);
2656 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002657 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002658 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002659 {
2660 session_event_t e = { 0 };
2661 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002662 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002663 vec_add1 (wrk->unhandled_evts_vector, e);
2664 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002665 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2666 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002667 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002668 break;
2669
2670 case EPOLL_CTL_MOD:
2671 if (PREDICT_FALSE (!event))
2672 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002673 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002674 rv = VPPCOM_EINVAL;
2675 goto done;
2676 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002677 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002678 {
Florin Coras5e062572019-03-14 19:07:51 -07002679 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002680 rv = VPPCOM_EINVAL;
2681 goto done;
2682 }
Florin Coras17ec5772020-08-12 20:46:29 -07002683 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002684 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002685 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002686 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687 rv = VPPCOM_EINVAL;
2688 goto done;
2689 }
hanlin475c9d72019-12-26 11:44:28 +08002690
2691 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2692 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002693 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002694 {
2695 session_event_t e = { 0 };
2696 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002697 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002698 vec_add1 (wrk->unhandled_evts_vector, e);
2699 }
Florin Coras17ec5772020-08-12 20:46:29 -07002700 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2701 s->vep.ev = *event;
2702 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2703 if (event->events & EPOLLOUT)
2704 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2705 else
2706 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002707 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2708 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002709 break;
2710
2711 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002712 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002713 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002714 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002715 rv = VPPCOM_EINVAL;
2716 goto done;
2717 }
Florin Coras17ec5772020-08-12 20:46:29 -07002718 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002719 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002720 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002721 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002722 rv = VPPCOM_EINVAL;
2723 goto done;
2724 }
2725
Florin Coras17ec5772020-08-12 20:46:29 -07002726 if (s->vep.prev_sh == vep_handle)
2727 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002728 else
2729 {
Florin Coras7e12d942018-06-27 14:32:43 -07002730 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002731 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002732 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002733 {
Florin Coras5e062572019-03-14 19:07:51 -07002734 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002735 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002736 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002737 }
Florin Coras134a9962018-08-28 11:32:04 -07002738 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002739 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002740 }
Florin Coras17ec5772020-08-12 20:46:29 -07002741 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002742 {
Florin Coras7e12d942018-06-27 14:32:43 -07002743 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002744 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002745 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746 {
Florin Coras5e062572019-03-14 19:07:51 -07002747 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002748 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002749 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002750 }
Florin Coras134a9962018-08-28 11:32:04 -07002751 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002752 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002753 }
2754
Florin Coras17ec5772020-08-12 20:46:29 -07002755 memset (&s->vep, 0, sizeof (s->vep));
2756 s->vep.next_sh = ~0;
2757 s->vep.prev_sh = ~0;
2758 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002759 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002760
Florin Coras17ec5772020-08-12 20:46:29 -07002761 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2762 if (txf)
2763 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002764
Florin Coras5e062572019-03-14 19:07:51 -07002765 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002766 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002767 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002768 break;
2769
2770 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002771 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002772 rv = VPPCOM_EINVAL;
2773 }
2774
Florin Coras134a9962018-08-28 11:32:04 -07002775 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776
2777done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002778 return rv;
2779}
2780
Florin Coras86f04502018-09-12 16:08:01 -07002781static inline void
2782vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2783 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002784{
2785 session_disconnected_msg_t *disconnected_msg;
2786 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002787 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002788 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002789 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002790 u8 add_event = 0;
2791
2792 switch (e->event_type)
2793 {
Florin Coras653e43f2019-03-04 10:56:23 -08002794 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002795 sid = e->session_index;
Florin Coras080aa502020-05-26 00:47:52 +00002796 session = vcl_session_get (wrk, sid);
2797 if (vcl_session_is_closed (session))
Florin Corasfa915f82018-12-26 16:29:06 -08002798 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002799 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002800 session_events = session->vep.ev.events;
Florin Corasac422d62020-10-19 20:51:36 -07002801 if (!(EPOLLIN & session->vep.ev.events)
2802 || (session->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002803 break;
2804 add_event = 1;
2805 events[*num_ev].events |= EPOLLIN;
2806 session_evt_data = session->vep.ev.data.u64;
Florin Corasac422d62020-10-19 20:51:36 -07002807 session->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002808 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002809 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002810 sid = e->session_index;
Florin Coras080aa502020-05-26 00:47:52 +00002811 session = vcl_session_get (wrk, sid);
2812 if (vcl_session_is_closed (session))
Florin Corasfa915f82018-12-26 16:29:06 -08002813 break;
Florin Coras86f04502018-09-12 16:08:01 -07002814 session_events = session->vep.ev.events;
2815 if (!(EPOLLOUT & session_events))
2816 break;
2817 add_event = 1;
2818 events[*num_ev].events |= EPOLLOUT;
2819 session_evt_data = session->vep.ev.data.u64;
Florin Coras17ec5772020-08-12 20:46:29 -07002820 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (session) ?
2821 session->ct_tx_fifo : session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002822 break;
Florin Coras86f04502018-09-12 16:08:01 -07002823 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002824 session = vcl_session_accepted (wrk,
2825 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002826 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002827 break;
Florin Coras86f04502018-09-12 16:08:01 -07002828
Florin Coras86f04502018-09-12 16:08:01 -07002829 session_events = session->vep.ev.events;
2830 if (!(EPOLLIN & session_events))
2831 break;
2832
2833 add_event = 1;
2834 events[*num_ev].events |= EPOLLIN;
2835 session_evt_data = session->vep.ev.data.u64;
2836 break;
2837 case SESSION_CTRL_EVT_CONNECTED:
2838 connected_msg = (session_connected_msg_t *) e->data;
Florin Corasf1653e62019-11-06 15:41:37 -08002839 sid = vcl_session_connected_handler (wrk, connected_msg);
Florin Coras86f04502018-09-12 16:08:01 -07002840 /* Generate EPOLLOUT because there's no connected event */
Florin Coras080aa502020-05-26 00:47:52 +00002841 session = vcl_session_get (wrk, sid);
2842 if (vcl_session_is_closed (session))
Florin Corasfa915f82018-12-26 16:29:06 -08002843 break;
Florin Coras86f04502018-09-12 16:08:01 -07002844 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002845 if (!(EPOLLOUT & session_events))
2846 break;
2847 add_event = 1;
2848 events[*num_ev].events |= EPOLLOUT;
2849 session_evt_data = session->vep.ev.data.u64;
Florin Corasc127d5a2020-10-14 16:35:58 -07002850 if (session->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002851 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002852 break;
2853 case SESSION_CTRL_EVT_DISCONNECTED:
2854 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002855 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Coras080aa502020-05-26 00:47:52 +00002856 if (vcl_session_is_closed (session))
Florin Coras86f04502018-09-12 16:08:01 -07002857 break;
Florin Coras72f77822019-01-22 19:05:52 -08002858 session_events = session->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002859 add_event = 1;
2860 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2861 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002862 break;
2863 case SESSION_CTRL_EVT_RESET:
2864 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras080aa502020-05-26 00:47:52 +00002865 session = vcl_session_get (wrk, sid);
2866 if (vcl_session_is_closed (session))
Florin Coras86f04502018-09-12 16:08:01 -07002867 break;
Florin Coras72f77822019-01-22 19:05:52 -08002868 session_events = session->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002869 add_event = 1;
2870 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2871 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002872 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002873 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2874 vcl_session_unlisten_reply_handler (wrk, e->data);
2875 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002876 case SESSION_CTRL_EVT_MIGRATED:
2877 vcl_session_migrated_handler (wrk, e->data);
2878 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002879 case SESSION_CTRL_EVT_CLEANUP:
2880 vcl_session_cleanup_handler (wrk, e->data);
2881 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002882 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2883 vcl_session_req_worker_update_handler (wrk, e->data);
2884 break;
2885 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2886 vcl_session_worker_update_reply_handler (wrk, e->data);
2887 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002888 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2889 vcl_session_app_add_segment_handler (wrk, e->data);
2890 break;
2891 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2892 vcl_session_app_del_segment_handler (wrk, e->data);
2893 break;
hanlina3a48962020-07-13 11:09:15 +08002894 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002895 vcl_worker_rpc_handler (wrk, e->data);
2896 break;
Florin Coras86f04502018-09-12 16:08:01 -07002897 default:
2898 VDBG (0, "unhandled: %u", e->event_type);
2899 break;
2900 }
2901
2902 if (add_event)
2903 {
2904 events[*num_ev].data.u64 = session_evt_data;
2905 if (EPOLLONESHOT & session_events)
2906 {
2907 session = vcl_session_get (wrk, sid);
2908 session->vep.ev.events = 0;
2909 }
2910 *num_ev += 1;
2911 }
2912}
2913
2914static int
2915vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2916 struct epoll_event *events, u32 maxevents,
2917 double wait_for_time, u32 * num_ev)
2918{
Florin Coras99368312018-08-02 10:45:44 -07002919 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002920 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002921 int i;
2922
Florin Coras539663c2018-09-28 14:59:37 -07002923 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2924 goto handle_dequeued;
2925
Florin Coras54693d22018-07-17 10:46:29 -07002926 svm_msg_q_lock (mq);
2927 if (svm_msg_q_is_empty (mq))
2928 {
2929 if (!wait_for_time)
2930 {
2931 svm_msg_q_unlock (mq);
2932 return 0;
2933 }
2934 else if (wait_for_time < 0)
2935 {
2936 svm_msg_q_wait (mq);
2937 }
2938 else
2939 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002940 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002941 {
2942 svm_msg_q_unlock (mq);
2943 return 0;
2944 }
2945 }
2946 }
Florin Corase003a1b2019-06-05 10:47:16 -07002947 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002948 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002949 svm_msg_q_unlock (mq);
2950
Florin Coras539663c2018-09-28 14:59:37 -07002951handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002952 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002953 {
Florin Coras134a9962018-08-28 11:32:04 -07002954 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002955 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002956 if (*num_ev < maxevents)
2957 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2958 else
2959 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07002960 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002961 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002962 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002963 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002964 return *num_ev;
2965}
2966
Florin Coras99368312018-08-02 10:45:44 -07002967static int
Florin Coras134a9962018-08-28 11:32:04 -07002968vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002969 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002970{
Florin Corase003a1b2019-06-05 10:47:16 -07002971 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002972
2973 if (!n_evts)
2974 {
2975 wait = wait_for_time;
2976 start = clib_time_now (&wrk->clib_time);
2977 }
2978
2979 do
2980 {
2981 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2982 wait, &n_evts);
2983 if (n_evts)
2984 return n_evts;
2985 if (wait == -1)
2986 continue;
2987
Florin Corase003a1b2019-06-05 10:47:16 -07002988 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07002989 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07002990 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002991 }
2992 while (wait > 0);
2993
2994 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002995}
2996
2997static int
Florin Coras134a9962018-08-28 11:32:04 -07002998vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002999 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003000{
3001 vcl_mq_evt_conn_t *mqc;
3002 int __clib_unused n_read;
3003 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003004 u64 buf;
3005
Florin Coras134a9962018-08-28 11:32:04 -07003006 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07003007again:
Florin Coras134a9962018-08-28 11:32:04 -07003008 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3009 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07003010 for (i = 0; i < n_mq_evts; i++)
3011 {
Florin Coras134a9962018-08-28 11:32:04 -07003012 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07003013 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07003014 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07003015 }
Florin Corasa4878ed2018-10-12 13:09:36 -07003016 if (!n_evts && n_mq_evts > 0)
3017 goto again;
Florin Coras99368312018-08-02 10:45:44 -07003018
3019 return (int) n_evts;
3020}
3021
Dave Wallacef7f809c2017-10-03 01:48:42 -04003022int
Florin Coras134a9962018-08-28 11:32:04 -07003023vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003024 int maxevents, double wait_for_time)
3025{
Florin Coras134a9962018-08-28 11:32:04 -07003026 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003027 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003028 u32 n_evts = 0;
3029 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003030
3031 if (PREDICT_FALSE (maxevents <= 0))
3032 {
Florin Coras5e062572019-03-14 19:07:51 -07003033 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003034 return VPPCOM_EINVAL;
3035 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003036
Florin Coras134a9962018-08-28 11:32:04 -07003037 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003038 if (!vep_session)
3039 return VPPCOM_EBADFD;
3040
Florin Coras6c3b2182020-10-19 18:36:48 -07003041 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003042 {
Florin Coras5e062572019-03-14 19:07:51 -07003043 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003044 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003045 }
Florin Coras54693d22018-07-17 10:46:29 -07003046
3047 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003048
Florin Coras86f04502018-09-12 16:08:01 -07003049 if (vec_len (wrk->unhandled_evts_vector))
3050 {
3051 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3052 {
3053 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3054 events, &n_evts);
3055 if (n_evts == maxevents)
3056 {
Florin Corase003a1b2019-06-05 10:47:16 -07003057 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3058 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003059 }
3060 }
Florin Corase003a1b2019-06-05 10:47:16 -07003061 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003062 }
3063
3064 if (vcm->cfg.use_mq_eventfd)
3065 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3066 wait_for_time);
3067
3068 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3069 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003070}
3071
Dave Wallace35830af2017-10-09 01:43:42 -04003072int
Florin Coras134a9962018-08-28 11:32:04 -07003073vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003074 void *buffer, uint32_t * buflen)
3075{
Florin Coras134a9962018-08-28 11:32:04 -07003076 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003077 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04003078 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08003079 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003080 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003081
Florin Coras134a9962018-08-28 11:32:04 -07003082 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003083 if (!session)
3084 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003085
Dave Wallace35830af2017-10-09 01:43:42 -04003086 switch (op)
3087 {
3088 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003089 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003090 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3091 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003092 break;
3093
Dave Wallace227867f2017-11-13 21:21:53 -05003094 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003095 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003096 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3097 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003098 break;
3099
3100 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003101 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003102 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003103 *flags =
3104 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003105 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003106 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003107 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003108 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3109 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003110 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003111 }
3112 else
3113 rv = VPPCOM_EINVAL;
3114 break;
3115
3116 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003117 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003118 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003119 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003120 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003121 else
Florin Corasac422d62020-10-19 20:51:36 -07003122 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003123
Florin Coras5e062572019-03-14 19:07:51 -07003124 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3125 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003126 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003127 }
3128 else
3129 rv = VPPCOM_EINVAL;
3130 break;
3131
3132 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003133 if (PREDICT_TRUE (buffer && buflen &&
3134 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003135 {
Florin Coras7e12d942018-06-27 14:32:43 -07003136 ep->is_ip4 = session->transport.is_ip4;
3137 ep->port = session->transport.rmt_port;
3138 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003139 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3140 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003141 else
Dave Barach178cf492018-11-13 16:34:13 -05003142 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3143 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003144 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003145 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3146 "addr = %U, port %u", session_handle, ep->is_ip4,
3147 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003148 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3149 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003150 }
3151 else
3152 rv = VPPCOM_EINVAL;
3153 break;
3154
3155 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003156 if (PREDICT_TRUE (buffer && buflen &&
3157 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003158 {
Florin Coras7e12d942018-06-27 14:32:43 -07003159 ep->is_ip4 = session->transport.is_ip4;
3160 ep->port = session->transport.lcl_port;
3161 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003162 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3163 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003164 else
Dave Barach178cf492018-11-13 16:34:13 -05003165 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3166 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003167 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003168 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3169 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003170 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003171 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3172 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003173 }
3174 else
3175 rv = VPPCOM_EINVAL;
3176 break;
Stevenb5a11602017-10-11 09:59:30 -07003177
Florin Corasef7cbf62019-10-17 09:56:27 -07003178 case VPPCOM_ATTR_SET_LCL_ADDR:
3179 if (PREDICT_TRUE (buffer && buflen &&
3180 (*buflen >= sizeof (*ep)) && ep->ip))
3181 {
3182 session->transport.is_ip4 = ep->is_ip4;
3183 session->transport.lcl_port = ep->port;
3184 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3185 *buflen = sizeof (*ep);
3186 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3187 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3188 &session->transport.lcl_ip,
3189 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3190 clib_net_to_host_u16 (ep->port));
3191 }
3192 else
3193 rv = VPPCOM_EINVAL;
3194 break;
3195
Dave Wallace048b1d62018-01-03 22:24:41 -05003196 case VPPCOM_ATTR_GET_LIBC_EPFD:
3197 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003198 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003199 break;
3200
3201 case VPPCOM_ATTR_SET_LIBC_EPFD:
3202 if (PREDICT_TRUE (buffer && buflen &&
3203 (*buflen == sizeof (session->libc_epfd))))
3204 {
3205 session->libc_epfd = *(int *) buffer;
3206 *buflen = sizeof (session->libc_epfd);
3207
Florin Coras5e062572019-03-14 19:07:51 -07003208 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3209 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003210 }
3211 else
3212 rv = VPPCOM_EINVAL;
3213 break;
3214
3215 case VPPCOM_ATTR_GET_PROTOCOL:
3216 if (buffer && buflen && (*buflen >= sizeof (int)))
3217 {
Florin Coras7e12d942018-06-27 14:32:43 -07003218 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003219 *buflen = sizeof (int);
3220
Florin Coras5e062572019-03-14 19:07:51 -07003221 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3222 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003223 }
3224 else
3225 rv = VPPCOM_EINVAL;
3226 break;
3227
3228 case VPPCOM_ATTR_GET_LISTEN:
3229 if (buffer && buflen && (*buflen >= sizeof (int)))
3230 {
Florin Corasac422d62020-10-19 20:51:36 -07003231 *(int *) buffer = vcl_session_has_attr (session,
3232 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003233 *buflen = sizeof (int);
3234
Florin Coras5e062572019-03-14 19:07:51 -07003235 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3236 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003237 }
3238 else
3239 rv = VPPCOM_EINVAL;
3240 break;
3241
3242 case VPPCOM_ATTR_GET_ERROR:
3243 if (buffer && buflen && (*buflen >= sizeof (int)))
3244 {
3245 *(int *) buffer = 0;
3246 *buflen = sizeof (int);
3247
Florin Coras5e062572019-03-14 19:07:51 -07003248 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3249 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003250 }
3251 else
3252 rv = VPPCOM_EINVAL;
3253 break;
3254
3255 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3256 if (buffer && buflen && (*buflen >= sizeof (u32)))
3257 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003258
3259 /* VPP-TBD */
3260 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003261 session->tx_fifo ?
3262 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003263 vcm->cfg.tx_fifo_size);
3264 *buflen = sizeof (u32);
3265
Florin Coras5e062572019-03-14 19:07:51 -07003266 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3267 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3268 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003269 }
3270 else
3271 rv = VPPCOM_EINVAL;
3272 break;
3273
3274 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3275 if (buffer && buflen && (*buflen == sizeof (u32)))
3276 {
3277 /* VPP-TBD */
3278 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003279 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3280 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3281 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003282 }
3283 else
3284 rv = VPPCOM_EINVAL;
3285 break;
3286
3287 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3288 if (buffer && buflen && (*buflen >= sizeof (u32)))
3289 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003290
3291 /* VPP-TBD */
3292 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003293 session->rx_fifo ?
3294 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003295 vcm->cfg.rx_fifo_size);
3296 *buflen = sizeof (u32);
3297
Florin Coras5e062572019-03-14 19:07:51 -07003298 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3299 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003300 }
3301 else
3302 rv = VPPCOM_EINVAL;
3303 break;
3304
3305 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3306 if (buffer && buflen && (*buflen == sizeof (u32)))
3307 {
3308 /* VPP-TBD */
3309 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003310 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3311 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3312 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003313 }
3314 else
3315 rv = VPPCOM_EINVAL;
3316 break;
3317
3318 case VPPCOM_ATTR_GET_REUSEADDR:
3319 if (buffer && buflen && (*buflen >= sizeof (int)))
3320 {
3321 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003322 *(int *) buffer = vcl_session_has_attr (session,
3323 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003324 *buflen = sizeof (int);
3325
Florin Coras5e062572019-03-14 19:07:51 -07003326 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3327 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003328 }
3329 else
3330 rv = VPPCOM_EINVAL;
3331 break;
3332
Stevenb5a11602017-10-11 09:59:30 -07003333 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003334 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003335 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003336 {
3337 /* VPP-TBD */
3338 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003339 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003340 else
Florin Corasac422d62020-10-19 20:51:36 -07003341 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003342
Florin Coras5e062572019-03-14 19:07:51 -07003343 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003344 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003345 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003346 }
3347 else
3348 rv = VPPCOM_EINVAL;
3349 break;
3350
3351 case VPPCOM_ATTR_GET_REUSEPORT:
3352 if (buffer && buflen && (*buflen >= sizeof (int)))
3353 {
3354 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003355 *(int *) buffer = vcl_session_has_attr (session,
3356 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003357 *buflen = sizeof (int);
3358
Florin Coras5e062572019-03-14 19:07:51 -07003359 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3360 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003361 }
3362 else
3363 rv = VPPCOM_EINVAL;
3364 break;
3365
3366 case VPPCOM_ATTR_SET_REUSEPORT:
3367 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003368 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003369 {
3370 /* VPP-TBD */
3371 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003372 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003373 else
Florin Corasac422d62020-10-19 20:51:36 -07003374 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003375
Florin Coras5e062572019-03-14 19:07:51 -07003376 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003377 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003378 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003379 }
3380 else
3381 rv = VPPCOM_EINVAL;
3382 break;
3383
3384 case VPPCOM_ATTR_GET_BROADCAST:
3385 if (buffer && buflen && (*buflen >= sizeof (int)))
3386 {
3387 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003388 *(int *) buffer = vcl_session_has_attr (session,
3389 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003390 *buflen = sizeof (int);
3391
Florin Coras5e062572019-03-14 19:07:51 -07003392 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3393 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003394 }
3395 else
3396 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003397 break;
3398
3399 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003400 if (buffer && buflen && (*buflen == sizeof (int)))
3401 {
3402 /* VPP-TBD */
3403 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003404 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003405 else
Florin Corasac422d62020-10-19 20:51:36 -07003406 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003407
Florin Coras5e062572019-03-14 19:07:51 -07003408 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003409 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003410 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 }
3412 else
3413 rv = VPPCOM_EINVAL;
3414 break;
3415
3416 case VPPCOM_ATTR_GET_V6ONLY:
3417 if (buffer && buflen && (*buflen >= sizeof (int)))
3418 {
3419 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003420 *(int *) buffer = vcl_session_has_attr (session,
3421 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003422 *buflen = sizeof (int);
3423
Florin Coras5e062572019-03-14 19:07:51 -07003424 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3425 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003426 }
3427 else
3428 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003429 break;
3430
3431 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003432 if (buffer && buflen && (*buflen == sizeof (int)))
3433 {
3434 /* VPP-TBD */
3435 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003436 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003437 else
Florin Corasac422d62020-10-19 20:51:36 -07003438 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003439
Florin Coras5e062572019-03-14 19:07:51 -07003440 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003441 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003442 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003443 }
3444 else
3445 rv = VPPCOM_EINVAL;
3446 break;
3447
3448 case VPPCOM_ATTR_GET_KEEPALIVE:
3449 if (buffer && buflen && (*buflen >= sizeof (int)))
3450 {
3451 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003452 *(int *) buffer = vcl_session_has_attr (session,
3453 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003454 *buflen = sizeof (int);
3455
Florin Coras5e062572019-03-14 19:07:51 -07003456 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3457 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003458 }
3459 else
3460 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003461 break;
Stevenbccd3392017-10-12 20:42:21 -07003462
3463 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003464 if (buffer && buflen && (*buflen == sizeof (int)))
3465 {
3466 /* VPP-TBD */
3467 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003468 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003469 else
Florin Corasac422d62020-10-19 20:51:36 -07003470 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003471
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003473 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003474 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 }
3476 else
3477 rv = VPPCOM_EINVAL;
3478 break;
3479
3480 case VPPCOM_ATTR_GET_TCP_NODELAY:
3481 if (buffer && buflen && (*buflen >= sizeof (int)))
3482 {
3483 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003484 *(int *) buffer = vcl_session_has_attr (session,
3485 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003486 *buflen = sizeof (int);
3487
Florin Coras5e062572019-03-14 19:07:51 -07003488 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3489 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 }
3491 else
3492 rv = VPPCOM_EINVAL;
3493 break;
3494
3495 case VPPCOM_ATTR_SET_TCP_NODELAY:
3496 if (buffer && buflen && (*buflen == sizeof (int)))
3497 {
3498 /* VPP-TBD */
3499 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003500 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003501 else
Florin Corasac422d62020-10-19 20:51:36 -07003502 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003503
Florin Coras5e062572019-03-14 19:07:51 -07003504 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003505 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003506 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 }
3508 else
3509 rv = VPPCOM_EINVAL;
3510 break;
3511
3512 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3513 if (buffer && buflen && (*buflen >= sizeof (int)))
3514 {
3515 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003516 *(int *) buffer = vcl_session_has_attr (session,
3517 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003518 *buflen = sizeof (int);
3519
Florin Coras5e062572019-03-14 19:07:51 -07003520 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3521 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003522 }
3523 else
3524 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003525 break;
3526
3527 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 if (buffer && buflen && (*buflen == sizeof (int)))
3529 {
3530 /* VPP-TBD */
3531 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003532 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003533 else
Florin Corasac422d62020-10-19 20:51:36 -07003534 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003535
Florin Coras5e062572019-03-14 19:07:51 -07003536 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003537 vcl_session_has_attr (session,
3538 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 }
3540 else
3541 rv = VPPCOM_EINVAL;
3542 break;
3543
3544 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3545 if (buffer && buflen && (*buflen >= sizeof (int)))
3546 {
3547 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003548 *(int *) buffer = vcl_session_has_attr (session,
3549 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003550 *buflen = sizeof (int);
3551
Florin Coras5e062572019-03-14 19:07:51 -07003552 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3553 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003554 }
3555 else
3556 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003557 break;
3558
3559 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003560 if (buffer && buflen && (*buflen == sizeof (int)))
3561 {
3562 /* VPP-TBD */
3563 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003564 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003565 else
Florin Corasac422d62020-10-19 20:51:36 -07003566 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003567
Florin Coras5e062572019-03-14 19:07:51 -07003568 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003569 vcl_session_has_attr (session,
3570 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003571 }
3572 else
3573 rv = VPPCOM_EINVAL;
3574 break;
3575
3576 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3577 if (buffer && buflen && (*buflen >= sizeof (u32)))
3578 {
3579 /* VPP-TBD */
3580 *(u32 *) buffer = session->user_mss;
3581 *buflen = sizeof (int);
3582
Florin Coras5e062572019-03-14 19:07:51 -07003583 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3584 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003585 }
3586 else
3587 rv = VPPCOM_EINVAL;
3588 break;
3589
3590 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3591 if (buffer && buflen && (*buflen == sizeof (u32)))
3592 {
3593 /* VPP-TBD */
3594 session->user_mss = *(u32 *) buffer;
3595
Florin Coras5e062572019-03-14 19:07:51 -07003596 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3597 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 }
3599 else
3600 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003601 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003602
Florin Coras7baeb712019-01-04 17:05:43 -08003603 case VPPCOM_ATTR_SET_SHUT:
3604 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003605 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003606 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003607 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003608 break;
3609
3610 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003611 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003612 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003613 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003614 tmp_flags |= 2;
3615 if (tmp_flags == 1)
3616 *(int *) buffer = SHUT_RD;
3617 else if (tmp_flags == 2)
3618 *(int *) buffer = SHUT_WR;
3619 else if (tmp_flags == 3)
3620 *(int *) buffer = SHUT_RDWR;
3621 *buflen = sizeof (int);
3622 break;
Florin Coras1e966172020-05-16 18:18:14 +00003623
3624 case VPPCOM_ATTR_SET_CONNECTED:
3625 session->flags |= VCL_SESSION_F_CONNECTED;
3626 break;
3627
Dave Wallacee22aa742017-10-20 12:30:38 -04003628 default:
3629 rv = VPPCOM_EINVAL;
3630 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003631 }
3632
Dave Wallace35830af2017-10-09 01:43:42 -04003633 return rv;
3634}
3635
Stevenac1f96d2017-10-24 16:03:58 -07003636int
Florin Coras134a9962018-08-28 11:32:04 -07003637vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003638 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3639{
Florin Coras134a9962018-08-28 11:32:04 -07003640 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003641 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003642 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003643
3644 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003645 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003646 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003647 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003648 else
3649 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003650 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003651 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003652 }
3653
Florin Coras0a1e1832020-03-29 18:54:04 +00003654 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003655 {
Florin Coras5da10c42020-04-01 04:31:21 +00003656 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003657 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003658 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3659 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003660 else
Dave Barach178cf492018-11-13 16:34:13 -05003661 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3662 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003663 ep->is_ip4 = session->transport.is_ip4;
3664 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003665 }
Florin Coras460dce62018-07-27 05:45:06 -07003666
Stevenac1f96d2017-10-24 16:03:58 -07003667 return rv;
3668}
3669
3670int
Florin Coras134a9962018-08-28 11:32:04 -07003671vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003672 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3673{
Florin Coras7a2abce2020-04-05 19:25:44 +00003674 vcl_worker_t *wrk = vcl_worker_get_current ();
3675 vcl_session_t *s;
3676
3677 s = vcl_session_get_w_handle (wrk, session_handle);
3678 if (!s)
3679 return VPPCOM_EBADFD;
3680
Dave Wallace617dffa2017-10-26 14:47:06 -04003681 if (!buffer)
3682 return VPPCOM_EINVAL;
3683
3684 if (ep)
3685 {
Florin Coras5824cc52020-10-20 18:44:41 -07003686 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003687 return VPPCOM_EINVAL;
3688
3689 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003690 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003691 {
Florin Coras5824cc52020-10-20 18:44:41 -07003692 u32 session_index = s->session_index;
3693 f64 timeout = vcm->cfg.session_timeout;
3694 int rv;
3695
Florin Coras5da10c42020-04-01 04:31:21 +00003696 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003697 rv = vppcom_wait_for_session_state_change (session_index,
3698 VCL_STATE_READY,
3699 timeout);
3700 if (rv < 0)
3701 return rv;
3702 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003703 }
Florin Coras5824cc52020-10-20 18:44:41 -07003704
3705 s->transport.is_ip4 = ep->is_ip4;
3706 s->transport.rmt_port = ep->port;
3707 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003708 }
3709
3710 if (flags)
3711 {
3712 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003713 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003714 }
3715
Florin Coras7a2abce2020-04-05 19:25:44 +00003716 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3717 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003718}
3719
Dave Wallace048b1d62018-01-03 22:24:41 -05003720int
3721vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3722{
Florin Coras134a9962018-08-28 11:32:04 -07003723 vcl_worker_t *wrk = vcl_worker_get_current ();
3724 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003725 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003726 svm_msg_q_msg_t msg;
3727 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003728 int rv, num_ev = 0;
3729
Florin Coras5e062572019-03-14 19:07:51 -07003730 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003731
3732 if (!vp)
3733 return VPPCOM_EFAULT;
3734
3735 do
3736 {
Florin Coras7e12d942018-06-27 14:32:43 -07003737 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003738
Florin Coras6917b942018-11-13 22:44:54 -08003739 /* Dequeue all events and drop all unhandled io events */
3740 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3741 {
3742 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3743 vcl_handle_mq_event (wrk, e);
3744 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3745 }
3746 vec_reset_length (wrk->unhandled_evts_vector);
3747
Dave Wallace048b1d62018-01-03 22:24:41 -05003748 for (i = 0; i < n_sids; i++)
3749 {
Florin Coras7baeb712019-01-04 17:05:43 -08003750 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003751 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003752 {
3753 vp[i].revents = POLLHUP;
3754 num_ev++;
3755 continue;
3756 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003757
Florin Coras6917b942018-11-13 22:44:54 -08003758 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003759
3760 if (POLLIN & vp[i].events)
3761 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003762 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003763 if (rv > 0)
3764 {
Florin Coras6917b942018-11-13 22:44:54 -08003765 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003766 num_ev++;
3767 }
3768 else if (rv < 0)
3769 {
3770 switch (rv)
3771 {
3772 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003773 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003774 break;
3775
3776 default:
Florin Coras6917b942018-11-13 22:44:54 -08003777 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003778 break;
3779 }
3780 num_ev++;
3781 }
3782 }
3783
3784 if (POLLOUT & vp[i].events)
3785 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003786 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003787 if (rv > 0)
3788 {
Florin Coras6917b942018-11-13 22:44:54 -08003789 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003790 num_ev++;
3791 }
3792 else if (rv < 0)
3793 {
3794 switch (rv)
3795 {
3796 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003797 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003798 break;
3799
3800 default:
Florin Coras6917b942018-11-13 22:44:54 -08003801 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003802 break;
3803 }
3804 num_ev++;
3805 }
3806 }
3807
Dave Wallace7e607a72018-06-18 18:41:32 -04003808 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003809 {
Florin Coras6917b942018-11-13 22:44:54 -08003810 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 num_ev++;
3812 }
3813 }
3814 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003815 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003816 }
3817 while ((num_ev == 0) && keep_trying);
3818
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 return num_ev;
3820}
3821
Florin Coras99368312018-08-02 10:45:44 -07003822int
3823vppcom_mq_epoll_fd (void)
3824{
Florin Coras134a9962018-08-28 11:32:04 -07003825 vcl_worker_t *wrk = vcl_worker_get_current ();
3826 return wrk->mqs_epfd;
3827}
3828
3829int
Florin Coras30e79c22019-01-02 19:31:22 -08003830vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003831{
3832 return session_handle & 0xFFFFFF;
3833}
3834
3835int
Florin Coras30e79c22019-01-02 19:31:22 -08003836vppcom_session_worker (vcl_session_handle_t session_handle)
3837{
3838 return session_handle >> 24;
3839}
3840
3841int
Florin Coras134a9962018-08-28 11:32:04 -07003842vppcom_worker_register (void)
3843{
Florin Coras47c40e22018-11-26 17:01:36 -08003844 if (!vcl_worker_alloc_and_init ())
3845 return VPPCOM_EEXIST;
3846
Florin Coras47c40e22018-11-26 17:01:36 -08003847 if (vcl_worker_register_with_vpp ())
3848 return VPPCOM_EEXIST;
3849
3850 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003851}
3852
Florin Coras369db832019-07-08 12:34:45 -07003853void
3854vppcom_worker_unregister (void)
3855{
3856 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3857 vcl_set_worker_index (~0);
3858}
3859
Pivo6017ff02020-05-04 17:57:33 +02003860void
3861vppcom_worker_index_set (int index)
3862{
3863 vcl_set_worker_index (index);
3864}
3865
Florin Corasdfe4cf42018-11-28 22:13:45 -08003866int
3867vppcom_worker_index (void)
3868{
3869 return vcl_get_worker_index ();
3870}
3871
Florin Corase0982e52019-01-25 13:19:56 -08003872int
3873vppcom_worker_mqs_epfd (void)
3874{
3875 vcl_worker_t *wrk = vcl_worker_get_current ();
3876 if (!vcm->cfg.use_mq_eventfd)
3877 return -1;
3878 return wrk->mqs_epfd;
3879}
3880
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003881int
3882vppcom_session_is_connectable_listener (uint32_t session_handle)
3883{
3884 vcl_session_t *session;
3885 vcl_worker_t *wrk = vcl_worker_get_current ();
3886 session = vcl_session_get_w_handle (wrk, session_handle);
3887 if (!session)
3888 return VPPCOM_EBADFD;
3889 return vcl_session_is_connectable_listener (wrk, session);
3890}
3891
3892int
3893vppcom_session_listener (uint32_t session_handle)
3894{
3895 vcl_worker_t *wrk = vcl_worker_get_current ();
3896 vcl_session_t *listen_session, *session;
3897 session = vcl_session_get_w_handle (wrk, session_handle);
3898 if (!session)
3899 return VPPCOM_EBADFD;
3900 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3901 return VPPCOM_EBADFD;
3902 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3903 if (!listen_session)
3904 return VPPCOM_EBADFD;
3905 return vcl_session_handle (listen_session);
3906}
3907
3908int
3909vppcom_session_n_accepted (uint32_t session_handle)
3910{
3911 vcl_worker_t *wrk = vcl_worker_get_current ();
3912 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3913 if (!session)
3914 return VPPCOM_EBADFD;
3915 return session->n_accepted_sessions;
3916}
3917
Florin Coras66ec4672020-06-15 07:59:40 -07003918const char *
3919vppcom_proto_str (vppcom_proto_t proto)
3920{
3921 char const *proto_str;
3922
3923 switch (proto)
3924 {
3925 case VPPCOM_PROTO_TCP:
3926 proto_str = "TCP";
3927 break;
3928 case VPPCOM_PROTO_UDP:
3929 proto_str = "UDP";
3930 break;
3931 case VPPCOM_PROTO_TLS:
3932 proto_str = "TLS";
3933 break;
3934 case VPPCOM_PROTO_QUIC:
3935 proto_str = "QUIC";
3936 break;
3937 default:
3938 proto_str = "UNKNOWN";
3939 break;
3940 }
3941 return proto_str;
3942}
3943
3944const char *
3945vppcom_retval_str (int retval)
3946{
3947 char const *st;
3948
3949 switch (retval)
3950 {
3951 case VPPCOM_OK:
3952 st = "VPPCOM_OK";
3953 break;
3954
3955 case VPPCOM_EAGAIN:
3956 st = "VPPCOM_EAGAIN";
3957 break;
3958
3959 case VPPCOM_EFAULT:
3960 st = "VPPCOM_EFAULT";
3961 break;
3962
3963 case VPPCOM_ENOMEM:
3964 st = "VPPCOM_ENOMEM";
3965 break;
3966
3967 case VPPCOM_EINVAL:
3968 st = "VPPCOM_EINVAL";
3969 break;
3970
3971 case VPPCOM_EBADFD:
3972 st = "VPPCOM_EBADFD";
3973 break;
3974
3975 case VPPCOM_EAFNOSUPPORT:
3976 st = "VPPCOM_EAFNOSUPPORT";
3977 break;
3978
3979 case VPPCOM_ECONNABORTED:
3980 st = "VPPCOM_ECONNABORTED";
3981 break;
3982
3983 case VPPCOM_ECONNRESET:
3984 st = "VPPCOM_ECONNRESET";
3985 break;
3986
3987 case VPPCOM_ENOTCONN:
3988 st = "VPPCOM_ENOTCONN";
3989 break;
3990
3991 case VPPCOM_ECONNREFUSED:
3992 st = "VPPCOM_ECONNREFUSED";
3993 break;
3994
3995 case VPPCOM_ETIMEDOUT:
3996 st = "VPPCOM_ETIMEDOUT";
3997 break;
3998
3999 default:
4000 st = "UNKNOWN_STATE";
4001 break;
4002 }
4003
4004 return st;
4005}
4006
Dave Wallacee22aa742017-10-20 12:30:38 -04004007/*
4008 * fd.io coding-style-patch-verification: ON
4009 *
4010 * Local Variables:
4011 * eval: (c-set-style "gnu")
4012 * End:
4013 */