blob: fd866155b4d4fcfa37d16aa68b5275e3d824bee4 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Corasd85de682018-11-29 17:02:29 -080025static int
Florin Corasc4c4cf52019-08-24 18:17:34 -070026vcl_segment_is_not_mounted (vcl_worker_t * wrk, u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070027{
Florin Corasc4c4cf52019-08-24 18:17:34 -070028 u32 segment_index;
Florin Coras54693d22018-07-17 10:46:29 -070029
Florin Corasd85de682018-11-29 17:02:29 -080030 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
Florin Coras5f45e012019-01-23 09:21:30 -080031 return 0;
Florin Coras54693d22018-07-17 10:46:29 -070032
Florin Corasc4c4cf52019-08-24 18:17:34 -070033 segment_index = vcl_segment_table_lookup (segment_handle);
34 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
35 return 0;
36
Florin Corasd85de682018-11-29 17:02:29 -080037 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070038}
39
Florin Coras30e79c22019-01-02 19:31:22 -080040static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070041vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080042{
43 svm_msg_q_msg_t *msg;
44 u32 n_msgs;
45 int i;
46
Florin Corase003a1b2019-06-05 10:47:16 -070047 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
Florin Coras30e79c22019-01-02 19:31:22 -080048 for (i = 0; i < n_msgs; i++)
49 {
50 vec_add2 (wrk->mq_msg_vector, msg, 1);
51 svm_msg_q_sub_w_lock (mq, msg);
52 }
53 return n_msgs;
54}
55
Florin Coras697faea2018-06-27 17:10:49 -070056const char *
Florin Coras288eaab2019-02-03 15:26:14 -080057vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040058{
59 char *st;
60
61 switch (state)
62 {
Florin Corasc127d5a2020-10-14 16:35:58 -070063 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000064 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040065 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070066 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040067 st = "STATE_LISTEN";
68 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070069 case VCL_STATE_READY:
70 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040071 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070072 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080073 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050074 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070075 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040076 st = "STATE_DISCONNECT";
77 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070078 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000079 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040080 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070081 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080082 st = "STATE_UPDATED";
83 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070084 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080085 st = "STATE_LISTEN_NO_MQ";
86 break;
Dave Wallace543852a2017-08-03 02:11:34 -040087 default:
88 st = "UNKNOWN_STATE";
89 break;
90 }
91
92 return st;
93}
94
Dave Wallace543852a2017-08-03 02:11:34 -040095u8 *
96format_ip4_address (u8 * s, va_list * args)
97{
98 u8 *a = va_arg (*args, u8 *);
99 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
100}
101
102u8 *
103format_ip6_address (u8 * s, va_list * args)
104{
105 ip6_address_t *a = va_arg (*args, ip6_address_t *);
106 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
107
108 i_max_n_zero = ARRAY_LEN (a->as_u16);
109 max_n_zeros = 0;
110 i_first_zero = i_max_n_zero;
111 n_zeros = 0;
112 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
113 {
114 u32 is_zero = a->as_u16[i] == 0;
115 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
116 {
117 i_first_zero = i;
118 n_zeros = 0;
119 }
120 n_zeros += is_zero;
121 if ((!is_zero && n_zeros > max_n_zeros)
122 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
123 {
124 i_max_n_zero = i_first_zero;
125 max_n_zeros = n_zeros;
126 i_first_zero = ARRAY_LEN (a->as_u16);
127 n_zeros = 0;
128 }
129 }
130
131 last_double_colon = 0;
132 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
133 {
134 if (i == i_max_n_zero && max_n_zeros > 1)
135 {
136 s = format (s, "::");
137 i += max_n_zeros - 1;
138 last_double_colon = 1;
139 }
140 else
141 {
142 s = format (s, "%s%x",
143 (last_double_colon || i == 0) ? "" : ":",
144 clib_net_to_host_u16 (a->as_u16[i]));
145 last_double_colon = 0;
146 }
147 }
148
149 return s;
150}
151
152/* Format an IP46 address. */
153u8 *
154format_ip46_address (u8 * s, va_list * args)
155{
156 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
157 ip46_type_t type = va_arg (*args, ip46_type_t);
158 int is_ip4 = 1;
159
160 switch (type)
161 {
162 case IP46_TYPE_ANY:
163 is_ip4 = ip46_address_is_ip4 (ip46);
164 break;
165 case IP46_TYPE_IP4:
166 is_ip4 = 1;
167 break;
168 case IP46_TYPE_IP6:
169 is_ip4 = 0;
170 break;
171 }
172
173 return is_ip4 ?
174 format (s, "%U", format_ip4_address, &ip46->ip4) :
175 format (s, "%U", format_ip6_address, &ip46->ip6);
176}
177
Florin Coras697faea2018-06-27 17:10:49 -0700178/*
179 * VPPCOM Utility Functions
180 */
181
Florin Coras458089b2019-08-21 16:20:44 -0700182static void
183vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
184{
185 app_session_evt_t _app_evt, *app_evt = &_app_evt;
186 session_listen_msg_t *mp;
187 svm_msg_q_t *mq;
188
189 mq = vcl_worker_ctrl_mq (wrk);
190 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
191 mp = (session_listen_msg_t *) app_evt->evt->data;
192 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700193 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700194 mp->context = s->session_index;
195 mp->wrk_index = wrk->vpp_wrk_index;
196 mp->is_ip4 = s->transport.is_ip4;
197 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
198 mp->port = s->transport.lcl_port;
199 mp->proto = s->session_type;
Florin Coras1e966172020-05-16 18:18:14 +0000200 if (s->flags & VCL_SESSION_F_CONNECTED)
201 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700202 app_send_ctrl_evt_to_vpp (mq, app_evt);
203}
204
205static void
206vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
207{
208 app_session_evt_t _app_evt, *app_evt = &_app_evt;
209 session_connect_msg_t *mp;
210 svm_msg_q_t *mq;
211
212 mq = vcl_worker_ctrl_mq (wrk);
213 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
214 mp = (session_connect_msg_t *) app_evt->evt->data;
215 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700216 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700217 mp->context = s->session_index;
218 mp->wrk_index = wrk->vpp_wrk_index;
219 mp->is_ip4 = s->transport.is_ip4;
220 mp->parent_handle = s->parent_handle;
221 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700222 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700223 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000224 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700225 mp->proto = s->session_type;
Florin Coras0a1e1832020-03-29 18:54:04 +0000226 if (s->flags & VCL_SESSION_F_CONNECTED)
227 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700228 app_send_ctrl_evt_to_vpp (mq, app_evt);
229}
230
231void
232vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
233{
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_unlisten_msg_t *mp;
236 svm_msg_q_t *mq;
237
238 mq = vcl_worker_ctrl_mq (wrk);
239 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
240 mp = (session_unlisten_msg_t *) app_evt->evt->data;
241 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700242 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700243 mp->wrk_index = wrk->vpp_wrk_index;
244 mp->handle = s->vpp_handle;
245 mp->context = wrk->wrk_index;
246 app_send_ctrl_evt_to_vpp (mq, app_evt);
247}
248
249static void
250vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
251{
252 app_session_evt_t _app_evt, *app_evt = &_app_evt;
253 session_disconnect_msg_t *mp;
254 svm_msg_q_t *mq;
255
256 /* Send to thread that owns the session */
257 mq = s->vpp_evt_q;
258 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
259 mp = (session_disconnect_msg_t *) app_evt->evt->data;
260 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700261 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700262 mp->handle = s->vpp_handle;
263 app_send_ctrl_evt_to_vpp (mq, app_evt);
264}
265
266static void
267vcl_send_app_detach (vcl_worker_t * wrk)
268{
269 app_session_evt_t _app_evt, *app_evt = &_app_evt;
270 session_app_detach_msg_t *mp;
271 svm_msg_q_t *mq;
272
273 mq = vcl_worker_ctrl_mq (wrk);
274 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
275 mp = (session_app_detach_msg_t *) app_evt->evt->data;
276 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700277 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700278 app_send_ctrl_evt_to_vpp (mq, app_evt);
279}
Florin Coras697faea2018-06-27 17:10:49 -0700280
Florin Coras54693d22018-07-17 10:46:29 -0700281static void
282vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
283 session_handle_t handle, int retval)
284{
285 app_session_evt_t _app_evt, *app_evt = &_app_evt;
286 session_accepted_reply_msg_t *rmp;
287 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
288 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
289 rmp->handle = handle;
290 rmp->context = context;
291 rmp->retval = retval;
292 app_send_ctrl_evt_to_vpp (mq, app_evt);
293}
294
Florin Coras99368312018-08-02 10:45:44 -0700295static void
296vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
297 session_handle_t handle, int retval)
298{
299 app_session_evt_t _app_evt, *app_evt = &_app_evt;
300 session_disconnected_reply_msg_t *rmp;
301 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
302 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
303 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
304 rmp->handle = handle;
305 rmp->context = context;
306 rmp->retval = retval;
307 app_send_ctrl_evt_to_vpp (mq, app_evt);
308}
309
Florin Corasc9fbd662018-08-24 12:59:56 -0700310static void
311vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
312 session_handle_t handle, int retval)
313{
314 app_session_evt_t _app_evt, *app_evt = &_app_evt;
315 session_reset_reply_msg_t *rmp;
316 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
317 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
318 rmp->handle = handle;
319 rmp->context = context;
320 rmp->retval = retval;
321 app_send_ctrl_evt_to_vpp (mq, app_evt);
322}
323
Florin Coras30e79c22019-01-02 19:31:22 -0800324void
325vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
326 u32 wrk_index)
327{
328 app_session_evt_t _app_evt, *app_evt = &_app_evt;
329 session_worker_update_msg_t *mp;
330 svm_msg_q_t *mq;
331
332 mq = vcl_session_vpp_evt_q (wrk, s);
333 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
334 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700335 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800336 mp->handle = s->vpp_handle;
337 mp->req_wrk_index = wrk->vpp_wrk_index;
338 mp->wrk_index = wrk_index;
339 app_send_ctrl_evt_to_vpp (mq, app_evt);
340}
341
hanlina3a48962020-07-13 11:09:15 +0800342int
Florin Coras40c07ce2020-07-16 20:46:17 -0700343vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
344{
345 app_session_evt_t _app_evt, *app_evt = &_app_evt;
346 session_app_wrk_rpc_msg_t *mp;
347 vcl_worker_t *dst_wrk, *wrk;
348 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800349 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700350
351 if (data_len > sizeof (mp->data))
352 goto done;
353
354 clib_spinlock_lock (&vcm->workers_lock);
355
356 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
357 if (!dst_wrk)
358 goto done;
359
360 wrk = vcl_worker_get_current ();
361 mq = vcl_worker_ctrl_mq (wrk);
362 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
363 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700364 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700365 mp->wrk_index = dst_wrk->vpp_wrk_index;
366 clib_memcpy (mp->data, data, data_len);
367 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800368 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700369
370done:
371 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800372 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700373}
374
Florin Coras54693d22018-07-17 10:46:29 -0700375static u32
Florin Coras00cca802019-06-06 09:38:44 -0700376vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
377 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700378{
379 vcl_session_t *session, *listen_session;
380 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700381 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700382 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700383
Florin Coras134a9962018-08-28 11:32:04 -0700384 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700385
Florin Coras00cca802019-06-06 09:38:44 -0700386 listen_session = vcl_session_get (wrk, ls_index);
387 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700388 {
Florin Coras00cca802019-06-06 09:38:44 -0700389 VDBG (0, "ERROR: listener handle %lu does not match session %u",
390 mp->listener_handle, ls_index);
391 goto error;
392 }
393
Florin Corasc4c4cf52019-08-24 18:17:34 -0700394 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Coras00cca802019-06-06 09:38:44 -0700395 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700396 VDBG (0, "ERROR: segment for session %u is not mounted!",
Florin Coras00cca802019-06-06 09:38:44 -0700397 session->session_index);
398 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700399 }
400
Florin Coras54693d22018-07-17 10:46:29 -0700401 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
402 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Coras653e43f2019-03-04 10:56:23 -0800403 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
404 svm_msg_q_t *);
405 rx_fifo->client_session_index = session->session_index;
406 tx_fifo->client_session_index = session->session_index;
407 rx_fifo->client_thread_index = vcl_get_worker_index ();
408 tx_fifo->client_thread_index = vcl_get_worker_index ();
409 vpp_wrk_index = tx_fifo->master_thread_index;
410 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
411 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700412
413 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800414 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700415 session->rx_fifo = rx_fifo;
416 session->tx_fifo = tx_fifo;
417
Florin Corasdfffdd72020-10-15 10:54:47 -0700418 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700419 session->transport.rmt_port = mp->rmt.port;
420 session->transport.is_ip4 = mp->rmt.is_ip4;
421 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500422 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700423
Florin Coras134a9962018-08-28 11:32:04 -0700424 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700425 session->transport.lcl_port = listen_session->transport.lcl_port;
426 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700427 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200428 session->is_dgram = vcl_proto_is_dgram (session->session_type);
429 session->listener_index = listen_session->session_index;
430 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700431
Florin Coras5e062572019-03-14 19:07:51 -0700432 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
433 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700434 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
435 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
436 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700437 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
438
Florin Coras00cca802019-06-06 09:38:44 -0700439 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
440 session->vpp_handle, 0);
441
Florin Coras134a9962018-08-28 11:32:04 -0700442 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700443
444error:
445 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
446 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
447 VNET_API_ERROR_INVALID_ARGUMENT);
448 vcl_session_free (wrk, session);
449 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700450}
451
452static u32
Florin Coras134a9962018-08-28 11:32:04 -0700453vcl_session_connected_handler (vcl_worker_t * wrk,
454 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700455{
Florin Coras99368312018-08-02 10:45:44 -0700456 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700457 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700458 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700459
460 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700461 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700462 if (!session)
463 {
Florin Coras5e062572019-03-14 19:07:51 -0700464 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
465 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700466 return VCL_INVALID_SESSION_INDEX;
467 }
Florin Coras54693d22018-07-17 10:46:29 -0700468 if (mp->retval)
469 {
Florin Coras5e062572019-03-14 19:07:51 -0700470 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700471 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700472 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700473 session->vpp_handle = mp->handle;
474 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700475 }
476
Florin Corasdbc9c592019-09-25 16:37:43 -0700477 session->vpp_handle = mp->handle;
478 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
479 svm_msg_q_t *);
Florin Coras460dce62018-07-27 05:45:06 -0700480 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
481 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700482 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Corasd85de682018-11-29 17:02:29 -0800483 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700484 VDBG (0, "segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800485 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700486 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700487 vcl_send_session_disconnect (wrk, session);
488 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800489 }
490
Florin Coras460dce62018-07-27 05:45:06 -0700491 rx_fifo->client_session_index = session_index;
492 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700493 rx_fifo->client_thread_index = vcl_get_worker_index ();
494 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700495
Florin Coras653e43f2019-03-04 10:56:23 -0800496 vpp_wrk_index = tx_fifo->master_thread_index;
497 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
498 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700499
Florin Coras653e43f2019-03-04 10:56:23 -0800500 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700501 {
Florin Coras653e43f2019-03-04 10:56:23 -0800502 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
503 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700504 if (vcl_segment_is_not_mounted (wrk, mp->ct_segment_handle))
Florin Coras653e43f2019-03-04 10:56:23 -0800505 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700506 VDBG (0, "ct segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800507 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700508 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700509 vcl_send_session_disconnect (wrk, session);
510 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800511 }
Florin Coras99368312018-08-02 10:45:44 -0700512 }
Florin Coras54693d22018-07-17 10:46:29 -0700513
Florin Coras54693d22018-07-17 10:46:29 -0700514 session->rx_fifo = rx_fifo;
515 session->tx_fifo = tx_fifo;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800516 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras09d18c22019-04-24 11:10:02 -0700517 session->transport.is_ip4 = mp->lcl.is_ip4;
518 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500519 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700520 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700521
522 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700523 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700524 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700525 vcl_send_session_disconnect (wrk, session);
526 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700527 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700528
529 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800530 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700531
Florin Coras5e062572019-03-14 19:07:51 -0700532 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
533 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700534 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700535
Florin Coras54693d22018-07-17 10:46:29 -0700536 return session_index;
537}
538
Florin Coras3c7d4f92018-12-14 11:28:43 -0800539static int
540vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
541{
542 vcl_session_msg_t *accepted_msg;
543 int i;
544
545 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
546 {
547 accepted_msg = &session->accept_evts_fifo[i];
548 if (accepted_msg->accepted_msg.handle == handle)
549 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800550 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800551 return 1;
552 }
553 }
554 return 0;
555}
556
Florin Corasc9fbd662018-08-24 12:59:56 -0700557static u32
Florin Coras134a9962018-08-28 11:32:04 -0700558vcl_session_reset_handler (vcl_worker_t * wrk,
559 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700560{
561 vcl_session_t *session;
562 u32 sid;
563
Florin Coras134a9962018-08-28 11:32:04 -0700564 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
565 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700566 if (!session)
567 {
568 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
569 return VCL_INVALID_SESSION_INDEX;
570 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800571
572 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700573 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800574 {
575
576 if (!vcl_flag_accepted_session (session, reset_msg->handle,
577 VCL_ACCEPTED_F_RESET))
578 VDBG (0, "session was not accepted!");
579 return VCL_INVALID_SESSION_INDEX;
580 }
581
Florin Corasc127d5a2020-10-14 16:35:58 -0700582 if (session->session_state != VCL_STATE_CLOSED)
583 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800584 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700585 return sid;
586}
587
Florin Coras60116992018-08-27 09:52:18 -0700588static u32
Florin Coras134a9962018-08-28 11:32:04 -0700589vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700590{
591 vcl_session_t *session;
592 u32 sid = mp->context;
593
Florin Coras134a9962018-08-28 11:32:04 -0700594 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700595 if (mp->retval)
596 {
Florin Coras5e062572019-03-14 19:07:51 -0700597 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700598 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700599 if (session)
600 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700601 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700602 session->vpp_handle = mp->handle;
603 return sid;
604 }
605 else
606 {
Florin Coras5e062572019-03-14 19:07:51 -0700607 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
608 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700609 return VCL_INVALID_SESSION_INDEX;
610 }
611 }
612
613 session->vpp_handle = mp->handle;
614 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500615 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
616 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700617 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700618 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700619 session->session_state = VCL_STATE_LISTEN;
Florin Coras60116992018-08-27 09:52:18 -0700620
Florin Coras5f45e012019-01-23 09:21:30 -0800621 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
622 vec_validate (wrk->vpp_event_queues, 0);
623 wrk->vpp_event_queues[0] = session->vpp_evt_q;
624
Florin Coras6e3c1f82020-01-15 01:30:46 +0000625 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700626 {
627 svm_fifo_t *rx_fifo, *tx_fifo;
628 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
629 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
630 rx_fifo->client_session_index = sid;
631 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
632 tx_fifo->client_session_index = sid;
633 session->rx_fifo = rx_fifo;
634 session->tx_fifo = tx_fifo;
635 }
636
Florin Coras05ecfcc2018-12-12 18:19:39 -0800637 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700638 return sid;
639}
640
Florin Corasdfae9f92019-02-20 19:48:31 -0800641static void
642vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
643{
644 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
645 vcl_session_t *s;
646
647 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000648 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800649 {
650 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
651 return;
652 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700653 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000654 {
655 /* Connected udp listener */
656 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700657 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000658 return;
659
660 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
661 return;
662 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800663
664 if (mp->retval)
665 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700666 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800667
668 if (mp->context != wrk->wrk_index)
669 VDBG (0, "wrong context");
670
671 vcl_session_table_del_vpp_handle (wrk, mp->handle);
672 vcl_session_free (wrk, s);
673}
674
Florin Coras68b7e582020-01-21 18:33:23 -0800675static void
676vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
677{
678 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
679 vcl_session_t *s;
680
681 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
682 if (!s)
683 {
684 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
685 return;
686 }
687
688 s->vpp_thread_index = mp->vpp_thread_index;
Florin Coras57660d92020-04-04 22:45:34 +0000689 s->vpp_handle = mp->new_handle;
Florin Coras68b7e582020-01-21 18:33:23 -0800690 s->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
691
692 vec_validate (wrk->vpp_event_queues, s->vpp_thread_index);
693 wrk->vpp_event_queues[s->vpp_thread_index] = s->vpp_evt_q;
694
695 vcl_session_table_del_vpp_handle (wrk, mp->handle);
696 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
697
698 /* Generate new tx event if we have outstanding data */
699 if (svm_fifo_has_event (s->tx_fifo))
700 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
701 SESSION_IO_EVT_TX, SVM_Q_WAIT);
702
Florin Coras57660d92020-04-04 22:45:34 +0000703 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
704 s->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800705}
706
Florin Coras3c7d4f92018-12-14 11:28:43 -0800707static vcl_session_t *
708vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
709{
710 vcl_session_msg_t *vcl_msg;
711 vcl_session_t *session;
712
713 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
714 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800715 VWRN ("session overlap handle %lu state %u!", msg->handle,
716 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800717
718 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
719 if (!session)
720 {
721 VERR ("couldn't find listen session: listener handle %llx",
722 msg->listener_handle);
723 return 0;
724 }
725
726 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000727 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800728 vcl_msg->accepted_msg = *msg;
729 /* Session handle points to listener until fully accepted by app */
730 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
731
732 return session;
733}
734
735static vcl_session_t *
736vcl_session_disconnected_handler (vcl_worker_t * wrk,
737 session_disconnected_msg_t * msg)
738{
739 vcl_session_t *session;
740
741 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
742 if (!session)
743 {
744 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
745 return 0;
746 }
747
Florin Corasadcfb152020-02-12 08:50:29 +0000748 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700749 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000750 return 0;
751
Florin Coras3c7d4f92018-12-14 11:28:43 -0800752 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700753 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800754 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800755 if (!vcl_flag_accepted_session (session, msg->handle,
756 VCL_ACCEPTED_F_CLOSED))
757 VDBG (0, "session was not accepted!");
758 return 0;
759 }
760
Florin Corasadcfb152020-02-12 08:50:29 +0000761 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700762 if (session->session_state != VCL_STATE_DISCONNECT)
763 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000764
Florin Coras3c7d4f92018-12-14 11:28:43 -0800765 return session;
766}
767
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700768static int
769vppcom_session_disconnect (u32 session_handle)
770{
771 vcl_worker_t *wrk = vcl_worker_get_current ();
772 svm_msg_q_t *vpp_evt_q;
773 vcl_session_t *session, *listen_session;
774 vcl_session_state_t state;
775 u64 vpp_handle;
776
777 session = vcl_session_get_w_handle (wrk, session_handle);
778 if (!session)
779 return VPPCOM_EBADFD;
780
781 vpp_handle = session->vpp_handle;
782 state = session->session_state;
783
784 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
785 vpp_handle, state, vppcom_session_state_str (state));
786
787 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
788 {
789 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
790 return VPPCOM_EBADFD;
791 }
792
793 if (state == VCL_STATE_VPP_CLOSING)
794 {
795 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
796 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->api_client_handle,
797 vpp_handle, 0);
798 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
799 session->session_index, vpp_handle);
800 }
801 else
802 {
803 /* Session doesn't have an event queue yet. Probably a non-blocking
804 * connect. Wait for the reply */
805 if (PREDICT_FALSE (!session->vpp_evt_q))
806 return VPPCOM_OK;
807
808 VDBG (1, "session %u [0x%llx]: sending disconnect...",
809 session->session_index, vpp_handle);
810 vcl_send_session_disconnect (wrk, session);
811 }
812
813 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
814 {
815 listen_session = vcl_session_get (wrk, session->listener_index);
816 listen_session->n_accepted_sessions--;
817 }
818
819 return VPPCOM_OK;
820}
821
Florin Coras30e79c22019-01-02 19:31:22 -0800822static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700823vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
824{
825 session_cleanup_msg_t *msg;
826 vcl_session_t *session;
827
828 msg = (session_cleanup_msg_t *) data;
829 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
830 if (!session)
831 {
832 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
833 return;
834 }
835
Florin Coras36d49392020-04-24 23:00:11 +0000836 if (msg->type == SESSION_CLEANUP_TRANSPORT)
837 {
838 /* Transport was cleaned up before we confirmed close. Probably the
839 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700840 * Confirm close to make sure everything is cleaned up.
841 * Move to undetermined state to ensure that the session is not
842 * removed before both vpp and the app cleanup.
843 * - If the app closes first, the session is moved to CLOSED state
844 * and the session cleanup notification from vpp removes the
845 * session.
846 * - If vpp cleans up the session first, the session is moved to
847 * DETACHED state lower and subsequently the close from the app
848 * frees the session
849 */
850 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700851 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700852 vppcom_session_disconnect (vcl_session_handle (session));
853 session->session_state = VCL_STATE_UPDATED;
854 }
855 else if (session->session_state == VCL_STATE_DISCONNECT)
856 {
857 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
858 wrk->api_client_handle,
859 session->vpp_handle, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700860 session->session_state = VCL_STATE_UPDATED;
861 }
Florin Coras36d49392020-04-24 23:00:11 +0000862 return;
863 }
864
Florin Coras9ace36d2019-10-28 13:14:17 -0700865 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000866 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700867 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000868 {
869 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700870 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000871 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
872 return;
873 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700874 vcl_session_free (wrk, session);
875}
876
877static void
Florin Coras30e79c22019-01-02 19:31:22 -0800878vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
879{
880 session_req_worker_update_msg_t *msg;
881 vcl_session_t *s;
882
883 msg = (session_req_worker_update_msg_t *) data;
884 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
885 if (!s)
886 return;
887
888 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
889}
890
891static void
892vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
893{
894 session_worker_update_reply_msg_t *msg;
895 vcl_session_t *s;
896
897 msg = (session_worker_update_reply_msg_t *) data;
898 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
899 if (!s)
900 {
901 VDBG (0, "unknown handle 0x%llx", msg->handle);
902 return;
903 }
Florin Corasc4c4cf52019-08-24 18:17:34 -0700904 if (vcl_segment_is_not_mounted (wrk, msg->segment_handle))
Florin Coras30e79c22019-01-02 19:31:22 -0800905 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700906 clib_warning ("segment for session %u is not mounted!",
Florin Coras30e79c22019-01-02 19:31:22 -0800907 s->session_index);
908 return;
909 }
Florin Coras30e79c22019-01-02 19:31:22 -0800910
Florin Coras5f45e012019-01-23 09:21:30 -0800911 if (s->rx_fifo)
912 {
913 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
914 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
915 s->rx_fifo->client_session_index = s->session_index;
916 s->tx_fifo->client_session_index = s->session_index;
917 s->rx_fifo->client_thread_index = wrk->wrk_index;
918 s->tx_fifo->client_thread_index = wrk->wrk_index;
919 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700920 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800921
Florin Coras30e79c22019-01-02 19:31:22 -0800922 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
923 s->vpp_handle, wrk->wrk_index);
924}
925
Florin Coras935ce752020-09-08 22:43:47 -0700926static int
927vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
928{
929
930 if (vcm->cfg.vpp_app_socket_api)
931 return vcl_sapi_recv_fds (wrk, fds, n_fds);
932
933 return vcl_bapi_recv_fds (wrk, fds, n_fds);
934}
935
Florin Corasc4c4cf52019-08-24 18:17:34 -0700936static void
937vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
938{
939 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
940 session_app_add_segment_msg_t *msg;
941 u64 segment_handle;
942 int fd = -1;
943
944 msg = (session_app_add_segment_msg_t *) data;
945
946 if (msg->fd_flags)
947 {
Florin Coras935ce752020-09-08 22:43:47 -0700948 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700949 seg_type = SSVM_SEGMENT_MEMFD;
950 }
951
952 segment_handle = msg->segment_handle;
953 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
954 {
955 clib_warning ("invalid segment handle");
956 return;
957 }
958
959 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
960 seg_type, fd))
961 {
962 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
963 return;
964 }
965
966 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
967 msg->segment_size);
968}
969
970static void
971vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
972{
973 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
974 vcl_segment_detach (msg->segment_handle);
975 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
976}
977
Florin Coras40c07ce2020-07-16 20:46:17 -0700978static void
979vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
980{
981 if (!vcm->wrk_rpc_fn)
982 return;
983
hanlina3a48962020-07-13 11:09:15 +0800984 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700985}
986
Florin Coras86f04502018-09-12 16:08:01 -0700987static int
988vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700989{
Florin Coras54693d22018-07-17 10:46:29 -0700990 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700991 session_connected_msg_t *connected_msg;
992 session_reset_msg_t *reset_msg;
993 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700994 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700995 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700996
997 switch (e->event_type)
998 {
Florin Coras653e43f2019-03-04 10:56:23 -0800999 case SESSION_IO_EVT_RX:
1000 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -07001001 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -07001002 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -08001003 break;
Florin Coras86f04502018-09-12 16:08:01 -07001004 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -07001005 break;
Florin Corasb242d312020-10-26 15:35:40 -07001006 case SESSION_CTRL_EVT_BOUND:
1007 /* We can only wait for only one listen so not postponed */
1008 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
1009 break;
Florin Coras54693d22018-07-17 10:46:29 -07001010 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07001011 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
1012 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1013 {
1014 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1015 *ecpy = *e;
1016 ecpy->postponed = 1;
1017 ecpy->session_index = s->session_index;
1018 }
Florin Coras54693d22018-07-17 10:46:29 -07001019 break;
1020 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07001021 connected_msg = (session_connected_msg_t *) e->data;
1022 sid = vcl_session_connected_handler (wrk, connected_msg);
1023 if (!(s = vcl_session_get (wrk, sid)))
1024 break;
1025 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1026 {
1027 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1028 *ecpy = *e;
1029 ecpy->postponed = 1;
1030 ecpy->session_index = s->session_index;
1031 }
Florin Coras54693d22018-07-17 10:46:29 -07001032 break;
1033 case SESSION_CTRL_EVT_DISCONNECTED:
1034 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001035 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1036 break;
1037 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1038 {
1039 vec_add1 (wrk->unhandled_evts_vector, *e);
1040 break;
1041 }
1042 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001043 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001044 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1045 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001046 break;
1047 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001048 reset_msg = (session_reset_msg_t *) e->data;
1049 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1050 break;
1051 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1052 {
1053 vec_add1 (wrk->unhandled_evts_vector, *e);
1054 break;
1055 }
Florin Coras134a9962018-08-28 11:32:04 -07001056 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001057 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001058 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1059 vcl_session_unlisten_reply_handler (wrk, e->data);
1060 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001061 case SESSION_CTRL_EVT_MIGRATED:
1062 vcl_session_migrated_handler (wrk, e->data);
1063 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001064 case SESSION_CTRL_EVT_CLEANUP:
1065 vcl_session_cleanup_handler (wrk, e->data);
1066 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001067 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1068 vcl_session_req_worker_update_handler (wrk, e->data);
1069 break;
1070 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1071 vcl_session_worker_update_reply_handler (wrk, e->data);
1072 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001073 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1074 vcl_session_app_add_segment_handler (wrk, e->data);
1075 break;
1076 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1077 vcl_session_app_del_segment_handler (wrk, e->data);
1078 break;
hanlina3a48962020-07-13 11:09:15 +08001079 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001080 vcl_worker_rpc_handler (wrk, e->data);
1081 break;
Florin Coras54693d22018-07-17 10:46:29 -07001082 default:
1083 clib_warning ("unhandled %u", e->event_type);
1084 }
1085 return VPPCOM_OK;
1086}
1087
Florin Coras30e79c22019-01-02 19:31:22 -08001088static int
Florin Coras697faea2018-06-27 17:10:49 -07001089vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001090 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001091 f64 wait_for_time)
1092{
Florin Coras134a9962018-08-28 11:32:04 -07001093 vcl_worker_t *wrk = vcl_worker_get_current ();
1094 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001095 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001096 svm_msg_q_msg_t msg;
1097 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001098
Florin Coras697faea2018-06-27 17:10:49 -07001099 do
Dave Wallace543852a2017-08-03 02:11:34 -04001100 {
Florin Coras134a9962018-08-28 11:32:04 -07001101 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001102 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001103 {
Florin Coras070453d2018-08-24 17:04:27 -07001104 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001105 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001106 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001107 {
Florin Coras697faea2018-06-27 17:10:49 -07001108 return VPPCOM_OK;
1109 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001110 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001111 {
Florin Coras697faea2018-06-27 17:10:49 -07001112 return VPPCOM_ECONNREFUSED;
1113 }
Florin Coras54693d22018-07-17 10:46:29 -07001114
Florin Coras134a9962018-08-28 11:32:04 -07001115 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001116 {
1117 usleep (100);
1118 continue;
1119 }
Florin Coras134a9962018-08-28 11:32:04 -07001120 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001121 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001122 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001123 }
Florin Coras134a9962018-08-28 11:32:04 -07001124 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001125
Florin Coras05ecfcc2018-12-12 18:19:39 -08001126 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001127 vppcom_session_state_str (state));
1128 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001129
Florin Coras697faea2018-06-27 17:10:49 -07001130 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001131}
1132
Florin Coras30e79c22019-01-02 19:31:22 -08001133static void
1134vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1135{
Florin Coras288eaab2019-02-03 15:26:14 -08001136 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001137 vcl_session_t *s;
1138 u32 *sip;
1139
1140 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1141 return;
1142
1143 vec_foreach (sip, wrk->pending_session_wrk_updates)
1144 {
1145 s = vcl_session_get (wrk, *sip);
1146 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1147 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001148 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1149 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001150 s->session_state = state;
1151 }
1152 vec_reset_length (wrk->pending_session_wrk_updates);
1153}
1154
Florin Corasf9240dc2019-01-15 08:03:17 -08001155void
Florin Coras30e79c22019-01-02 19:31:22 -08001156vcl_flush_mq_events (void)
1157{
1158 vcl_worker_t *wrk = vcl_worker_get_current ();
1159 svm_msg_q_msg_t *msg;
1160 session_event_t *e;
1161 svm_msg_q_t *mq;
1162 int i;
1163
1164 mq = wrk->app_event_queue;
1165 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -07001166 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001167 svm_msg_q_unlock (mq);
1168
1169 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1170 {
1171 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1172 e = svm_msg_q_msg_data (mq, msg);
1173 vcl_handle_mq_event (wrk, e);
1174 svm_msg_q_free_msg (mq, msg);
1175 }
1176 vec_reset_length (wrk->mq_msg_vector);
1177 vcl_handle_pending_wrk_updates (wrk);
1178}
1179
Florin Coras697faea2018-06-27 17:10:49 -07001180static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001181vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001182{
Florin Coras134a9962018-08-28 11:32:04 -07001183 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001184 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001185 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001186 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001187
Florin Corasab2f6db2018-08-31 14:31:41 -07001188 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001189 if (!session)
1190 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001191
Florin Corasaaff5ee2019-07-08 13:00:00 -07001192 /* Flush pending accept events, if any */
1193 while (clib_fifo_elts (session->accept_evts_fifo))
1194 {
1195 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1196 accepted_msg = &evt->accepted_msg;
1197 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1198 vcl_send_session_accepted_reply (session->vpp_evt_q,
1199 accepted_msg->context,
1200 session->vpp_handle, -1);
1201 }
1202 clib_fifo_free (session->accept_evts_fifo);
1203
Florin Coras458089b2019-08-21 16:20:44 -07001204 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001205
Florin Coras5e062572019-03-14 19:07:51 -07001206 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001207 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001208 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001209
1210 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001211 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001212
Florin Coras070453d2018-08-24 17:04:27 -07001213 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001214}
1215
Florin Coras940f78f2018-11-30 12:11:20 -08001216/**
1217 * Handle app exit
1218 *
1219 * Notify vpp of the disconnect and mark the worker as free. If we're the
1220 * last worker, do a full cleanup otherwise, since we're probably a forked
1221 * child, avoid syscalls as much as possible. We might've lost privileges.
1222 */
1223void
1224vppcom_app_exit (void)
1225{
1226 if (!pool_elts (vcm->workers))
1227 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001228 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1229 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001230 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001231}
1232
Florin Coras935ce752020-09-08 22:43:47 -07001233static int
1234vcl_api_attach (void)
1235{
1236 if (vcm->cfg.vpp_app_socket_api)
1237 return vcl_sapi_attach ();
1238
1239 return vcl_bapi_attach ();
1240}
1241
1242static void
1243vcl_api_detach (vcl_worker_t * wrk)
1244{
1245 vcl_send_app_detach (wrk);
1246
1247 if (vcm->cfg.vpp_app_socket_api)
1248 return vcl_sapi_detach (wrk);
1249
1250 return vcl_bapi_disconnect_from_vpp ();
1251}
1252
Dave Wallace543852a2017-08-03 02:11:34 -04001253/*
1254 * VPPCOM Public API functions
1255 */
1256int
Florin Coras66ec4672020-06-15 07:59:40 -07001257vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001258{
Dave Wallace543852a2017-08-03 02:11:34 -04001259 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001260 int rv;
1261
Florin Coras47c40e22018-11-26 17:01:36 -08001262 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001263 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001264 VDBG (1, "already initialized");
1265 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001266 }
1267
Florin Coras47c40e22018-11-26 17:01:36 -08001268 vcm->is_init = 1;
1269 vppcom_cfg (&vcm->cfg);
1270 vcl_cfg = &vcm->cfg;
1271
1272 vcm->main_cpu = pthread_self ();
1273 vcm->main_pid = getpid ();
1274 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001275 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1276 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001277 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1278 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001279 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001280 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001281 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001282
1283 /* Allocate default worker */
1284 vcl_worker_alloc_and_init ();
1285
Florin Coras935ce752020-09-08 22:43:47 -07001286 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001287 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001288
1289 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001290 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001291
1292 return VPPCOM_OK;
1293}
1294
1295void
1296vppcom_app_destroy (void)
1297{
Florin Corasdc0ded72020-04-30 02:59:55 +00001298 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001299 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001300
Florin Coras940f78f2018-11-30 12:11:20 -08001301 if (!pool_elts (vcm->workers))
1302 return;
1303
Florin Coras0d427d82018-06-27 03:24:07 -07001304 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001305
Florin Corasdc0ded72020-04-30 02:59:55 +00001306 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001307
Florin Corasce815de2020-04-16 18:47:27 +00001308 /* *INDENT-OFF* */
1309 pool_foreach (wrk, vcm->workers, ({
Florin Corasdc0ded72020-04-30 02:59:55 +00001310 if (current_wrk != wrk)
1311 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Florin Corasce815de2020-04-16 18:47:27 +00001312 }));
1313 /* *INDENT-ON* */
1314
Florin Coras935ce752020-09-08 22:43:47 -07001315 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001316 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1317
Florin Coras0d427d82018-06-27 03:24:07 -07001318 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001319
1320 /*
1321 * Free the heap and fix vcm
1322 */
1323 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001324 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001325
1326 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001327 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001328}
1329
1330int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001331vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001332{
Florin Coras134a9962018-08-28 11:32:04 -07001333 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001334 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001335
Florin Coras134a9962018-08-28 11:32:04 -07001336 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001337
Florin Coras7e12d942018-06-27 14:32:43 -07001338 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001339 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001340 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001341 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001342
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001343 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001344 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001345
Florin Coras7e12d942018-06-27 14:32:43 -07001346 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1347 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001348
Florin Coras5e062572019-03-14 19:07:51 -07001349 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001350
Florin Coras134a9962018-08-28 11:32:04 -07001351 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001352}
1353
1354int
Florin Corasc127d5a2020-10-14 16:35:58 -07001355vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001356 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001357{
Florin Coras134a9962018-08-28 11:32:04 -07001358 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001359
Florin Coras6c3b2182020-10-19 18:36:48 -07001360 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001361
Florin Coras6c3b2182020-10-19 18:36:48 -07001362 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001363 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001364 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001365 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001366 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001367 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001368 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001369 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001370 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1371 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001372 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001373 }
Florin Corase4306b62020-10-28 12:51:10 -07001374 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001375 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001376
Florin Coras6c3b2182020-10-19 18:36:48 -07001377 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001378 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001379 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001380 if (rv < 0)
1381 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001382 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1383 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001384 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001385
Florin Coras9ace36d2019-10-28 13:14:17 -07001386 if (!do_disconnect)
1387 {
1388 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001389 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001390 goto cleanup;
1391 }
1392
Florin Coras6c3b2182020-10-19 18:36:48 -07001393 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001394 {
1395 rv = vppcom_session_unbind (sh);
1396 if (PREDICT_FALSE (rv < 0))
1397 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001398 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001399 vppcom_retval_str (rv));
1400 return rv;
1401 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001402 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001403 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001404 {
1405 rv = vppcom_session_disconnect (sh);
1406 if (PREDICT_FALSE (rv < 0))
1407 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001408 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001409 rv, vppcom_retval_str (rv));
1410 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001411 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001412 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001413 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, s);
Florin Corascc7c88e2020-09-15 15:56:51 -07001414 vcl_send_session_reset_reply (mq, wrk->api_client_handle,
Florin Corasc127d5a2020-10-14 16:35:58 -07001415 s->vpp_handle, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001416 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001417 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001418 {
1419 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001420 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001421 goto free_session;
1422 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001423
Florin Corasc127d5a2020-10-14 16:35:58 -07001424 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001425
Florin Coras9ace36d2019-10-28 13:14:17 -07001426 /* Session is removed only after vpp confirms the disconnect */
1427 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001428
Florin Corasf9240dc2019-01-15 08:03:17 -08001429cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001430 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001431free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001432 vcl_session_free (wrk, s);
1433 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001434
Dave Wallace543852a2017-08-03 02:11:34 -04001435 return rv;
1436}
1437
1438int
Florin Corasf9240dc2019-01-15 08:03:17 -08001439vppcom_session_close (uint32_t session_handle)
1440{
1441 vcl_worker_t *wrk = vcl_worker_get_current ();
1442 vcl_session_t *session;
1443
1444 session = vcl_session_get_w_handle (wrk, session_handle);
1445 if (!session)
1446 return VPPCOM_EBADFD;
1447 return vcl_session_cleanup (wrk, session, session_handle,
1448 1 /* do_disconnect */ );
1449}
1450
1451int
Florin Coras134a9962018-08-28 11:32:04 -07001452vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001453{
Florin Coras134a9962018-08-28 11:32:04 -07001454 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001455 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001456
1457 if (!ep || !ep->ip)
1458 return VPPCOM_EINVAL;
1459
Florin Coras134a9962018-08-28 11:32:04 -07001460 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001461 if (!session)
1462 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001463
Florin Coras6c3b2182020-10-19 18:36:48 -07001464 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001465 {
Florin Coras5e062572019-03-14 19:07:51 -07001466 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1467 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001468 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001469 }
1470
Florin Coras7e12d942018-06-27 14:32:43 -07001471 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001472 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001473 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1474 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001475 else
Dave Barach178cf492018-11-13 16:34:13 -05001476 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1477 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001478 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001479
Florin Coras5e062572019-03-14 19:07:51 -07001480 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1481 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001482 session->transport.is_ip4 ? "IPv4" : "IPv6",
1483 format_ip46_address, &session->transport.lcl_ip,
1484 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1485 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001486 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001487 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001488
1489 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001490 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001491
Florin Coras070453d2018-08-24 17:04:27 -07001492 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001493}
1494
1495int
Florin Coras134a9962018-08-28 11:32:04 -07001496vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001497{
Florin Coras134a9962018-08-28 11:32:04 -07001498 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001499 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001500 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001501 int rv;
1502
Florin Coras134a9962018-08-28 11:32:04 -07001503 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001504 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001505 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001506
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001507 if (q_len == 0 || q_len == ~0)
1508 q_len = vcm->cfg.listen_queue_size;
1509
Dave Wallaceee45d412017-11-24 21:44:06 -05001510 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001511 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001512 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001513 VDBG (0, "session %u [0x%llx]: already in listen state!",
1514 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001515 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001516 }
1517
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001518 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001519
Florin Coras070453d2018-08-24 17:04:27 -07001520 /*
1521 * Send listen request to vpp and wait for reply
1522 */
Florin Coras458089b2019-08-21 16:20:44 -07001523 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001524 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001525 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001526 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001527
Florin Coras070453d2018-08-24 17:04:27 -07001528 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001529 {
Florin Coras134a9962018-08-28 11:32:04 -07001530 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001531 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1532 listen_sh, listen_session->vpp_handle, rv,
1533 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001534 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001535 }
1536
Florin Coras070453d2018-08-24 17:04:27 -07001537 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001538}
1539
Florin Coras134a9962018-08-28 11:32:04 -07001540static int
Florin Coras5e062572019-03-14 19:07:51 -07001541validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001542{
Florin Coras6c3b2182020-10-19 18:36:48 -07001543 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001544 {
Florin Coras5e062572019-03-14 19:07:51 -07001545 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1546 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001547 return VPPCOM_EBADFD;
1548 }
1549
Florin Corasc127d5a2020-10-14 16:35:58 -07001550 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001551 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001552 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001553 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001554 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001555 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001556 return VPPCOM_EBADFD;
1557 }
1558 return VPPCOM_OK;
1559}
1560
1561int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001562vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1563{
1564 if (!strcmp (proto_str, "TCP"))
1565 *proto = VPPCOM_PROTO_TCP;
1566 else if (!strcmp (proto_str, "tcp"))
1567 *proto = VPPCOM_PROTO_TCP;
1568 else if (!strcmp (proto_str, "UDP"))
1569 *proto = VPPCOM_PROTO_UDP;
1570 else if (!strcmp (proto_str, "udp"))
1571 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001572 else if (!strcmp (proto_str, "TLS"))
1573 *proto = VPPCOM_PROTO_TLS;
1574 else if (!strcmp (proto_str, "tls"))
1575 *proto = VPPCOM_PROTO_TLS;
1576 else if (!strcmp (proto_str, "QUIC"))
1577 *proto = VPPCOM_PROTO_QUIC;
1578 else if (!strcmp (proto_str, "quic"))
1579 *proto = VPPCOM_PROTO_QUIC;
1580 else
1581 return 1;
1582 return 0;
1583}
1584
1585int
Florin Coras134a9962018-08-28 11:32:04 -07001586vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001587 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001588{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001589 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001590 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001591 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001592 vcl_session_t *listen_session = 0;
1593 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001594 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001595 svm_msg_q_msg_t msg;
1596 session_event_t *e;
1597 u8 is_nonblocking;
1598 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001599
Florin Coras134a9962018-08-28 11:32:04 -07001600 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001601 if (!listen_session)
1602 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001603
Florin Coras134a9962018-08-28 11:32:04 -07001604 listen_session_index = listen_session->session_index;
1605 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001606 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001607
Florin Coras54693d22018-07-17 10:46:29 -07001608 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001609 {
Florin Coras54693d22018-07-17 10:46:29 -07001610 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001611 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001612 accepted_msg = evt->accepted_msg;
1613 goto handle;
1614 }
1615
Florin Corasac422d62020-10-19 20:51:36 -07001616 is_nonblocking = vcl_session_has_attr (listen_session,
1617 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001618 while (1)
1619 {
Carl Smith592a9092019-11-12 14:57:37 +13001620 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1621 return VPPCOM_EAGAIN;
1622
Florin Coras134a9962018-08-28 11:32:04 -07001623 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001624 return VPPCOM_EAGAIN;
1625
Florin Coras134a9962018-08-28 11:32:04 -07001626 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001627 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001628 {
wanghanlin96453fd2019-12-16 19:14:39 +08001629 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001630 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001631 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001632 }
Dave Barach178cf492018-11-13 16:34:13 -05001633 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001634 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001635 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001636 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001637
Florin Coras54693d22018-07-17 10:46:29 -07001638handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001639
Florin Coras00cca802019-06-06 09:38:44 -07001640 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1641 listen_session_index);
1642 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1643 return VPPCOM_ECONNABORTED;
1644
Florin Coras134a9962018-08-28 11:32:04 -07001645 listen_session = vcl_session_get (wrk, listen_session_index);
1646 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001647
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001648 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001649 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001650
Florin Coras5e062572019-03-14 19:07:51 -07001651 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1652 " flags %d, is_nonblocking %u", listen_session->session_index,
1653 listen_session->vpp_handle, client_session_index,
1654 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001655 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001656
Dave Wallace048b1d62018-01-03 22:24:41 -05001657 if (ep)
1658 {
Florin Coras7e12d942018-06-27 14:32:43 -07001659 ep->is_ip4 = client_session->transport.is_ip4;
1660 ep->port = client_session->transport.rmt_port;
1661 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001662 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1663 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001664 else
Dave Barach178cf492018-11-13 16:34:13 -05001665 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1666 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001667 }
Dave Wallace60caa062017-11-10 17:07:13 -05001668
Florin Coras05ecfcc2018-12-12 18:19:39 -08001669 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001670 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001671 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001672 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001673 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001674 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001675 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001676 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001677 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001678 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1679 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001680
Florin Coras3c7d4f92018-12-14 11:28:43 -08001681 /*
1682 * Session might have been closed already
1683 */
1684 if (accept_flags)
1685 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001686 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001687 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001688 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001689 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001690 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001691 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001692}
1693
1694int
Florin Coras134a9962018-08-28 11:32:04 -07001695vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001696{
Florin Coras134a9962018-08-28 11:32:04 -07001697 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001698 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001699 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001700 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001701
Florin Coras134a9962018-08-28 11:32:04 -07001702 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001703 if (!session)
1704 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001705 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001706
Florin Coras6c3b2182020-10-19 18:36:48 -07001707 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001708 {
Florin Coras5e062572019-03-14 19:07:51 -07001709 VDBG (0, "ERROR: cannot connect epoll session %u!",
1710 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001711 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001712 }
1713
Florin Corasc127d5a2020-10-14 16:35:58 -07001714 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001715 {
Florin Coras7baeb712019-01-04 17:05:43 -08001716 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001717 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001718 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001719 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001720 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001721 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001722 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001723 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001724 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001725 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001726 }
1727
Florin Coras0a1e1832020-03-29 18:54:04 +00001728 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001729 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001730 {
1731 if (session->session_type != VPPCOM_PROTO_UDP)
1732 return VPPCOM_EINVAL;
1733 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001734 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001735 }
1736
Florin Coras7e12d942018-06-27 14:32:43 -07001737 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001738 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001739 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001740 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001741 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001742
Florin Coras0a1e1832020-03-29 18:54:04 +00001743 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001744 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001745 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001746 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001747 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001748 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001749 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001750 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001751 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001752
Florin Coras458089b2019-08-21 16:20:44 -07001753 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001754
Florin Corasac422d62020-10-19 20:51:36 -07001755 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001756 {
fanyfa49d59d2020-10-13 17:07:16 +08001757 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001758 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001759 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001760 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001761 return VPPCOM_EINPROGRESS;
1762 }
Florin Coras57c88932019-08-29 12:03:17 -07001763
1764 /*
1765 * Wait for reply from vpp if blocking
1766 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001767 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001768 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001769
Florin Coras134a9962018-08-28 11:32:04 -07001770 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001771 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1772 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001773
Dave Wallace4878cbe2017-11-21 03:45:09 -05001774 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001775}
1776
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001777int
1778vppcom_session_stream_connect (uint32_t session_handle,
1779 uint32_t parent_session_handle)
1780{
1781 vcl_worker_t *wrk = vcl_worker_get_current ();
1782 vcl_session_t *session, *parent_session;
1783 u32 session_index, parent_session_index;
1784 int rv;
1785
1786 session = vcl_session_get_w_handle (wrk, session_handle);
1787 if (!session)
1788 return VPPCOM_EBADFD;
1789 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1790 if (!parent_session)
1791 return VPPCOM_EBADFD;
1792
1793 session_index = session->session_index;
1794 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001795 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001796 {
1797 VDBG (0, "ERROR: cannot connect epoll session %u!",
1798 session->session_index);
1799 return VPPCOM_EBADFD;
1800 }
1801
Florin Corasc127d5a2020-10-14 16:35:58 -07001802 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001803 {
1804 VDBG (0, "session handle %u [0x%llx]: session already "
1805 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1806 session_handle, session->vpp_handle,
1807 parent_session_handle, parent_session->vpp_handle,
1808 vppcom_proto_str (session->session_type), session->session_state,
1809 vppcom_session_state_str (session->session_state));
1810 return VPPCOM_OK;
1811 }
1812
1813 /* Connect to quic session specifics */
1814 session->transport.is_ip4 = parent_session->transport.is_ip4;
1815 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1816 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001817 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001818
1819 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1820 session_handle, parent_session_handle, parent_session->vpp_handle);
1821
1822 /*
1823 * Send connect request and wait for reply from vpp
1824 */
Florin Coras458089b2019-08-21 16:20:44 -07001825 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001826 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001827 vcm->cfg.session_timeout);
1828
1829 session->listener_index = parent_session_index;
1830 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001831 if (parent_session)
1832 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001833
1834 session = vcl_session_get (wrk, session_index);
1835 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1836 session->vpp_handle, rv ? "failed" : "succeeded");
1837
1838 return rv;
1839}
1840
Florin Coras54693d22018-07-17 10:46:29 -07001841static u8
1842vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1843{
Florin Corasc0737e92019-03-04 14:19:39 -08001844 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001845}
1846
Steven58f464e2017-10-25 12:33:12 -07001847static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001848vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001849 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001850{
Florin Coras134a9962018-08-28 11:32:04 -07001851 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001852 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001853 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001854 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001855 svm_msg_q_msg_t msg;
1856 session_event_t *e;
1857 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001858 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001859
Florin Coras070453d2018-08-24 17:04:27 -07001860 if (PREDICT_FALSE (!buf))
1861 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001862
Florin Coras134a9962018-08-28 11:32:04 -07001863 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001864 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001865 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001866
Florin Coras6d0106e2019-01-29 20:11:58 -08001867 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001868 {
Florin Coras5e062572019-03-14 19:07:51 -07001869 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001870 s->session_index, s->vpp_handle, s->session_state,
1871 vppcom_session_state_str (s->session_state));
1872 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001873 }
1874
Florin Corasac422d62020-10-19 20:51:36 -07001875 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001876 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001877 mq = wrk->app_event_queue;
1878 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001879 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001880
Sirshak Das28aa5392019-02-05 01:33:33 -06001881 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001882 {
Florin Coras54693d22018-07-17 10:46:29 -07001883 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001884 {
Yu Pingb2955352019-12-04 06:49:04 +08001885 if (vcl_session_is_closing (s))
1886 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001887 if (is_ct)
1888 svm_fifo_unset_event (s->rx_fifo);
1889 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001890 return VPPCOM_EWOULDBLOCK;
1891 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001892 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001893 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001894 if (vcl_session_is_closing (s))
1895 return vcl_session_closing_error (s);
1896
Florin Corasd6894562020-10-28 00:37:15 -07001897 if (is_ct)
1898 svm_fifo_unset_event (s->rx_fifo);
1899 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001900 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001901 if (svm_msg_q_is_empty (mq))
1902 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001903
Florin Coras54693d22018-07-17 10:46:29 -07001904 svm_msg_q_sub_w_lock (mq, &msg);
1905 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001906 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001907 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001908 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001909 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001910 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001911 }
Florin Coras54693d22018-07-17 10:46:29 -07001912
Florin Coras4a2c7942020-09-10 12:27:14 -07001913read_again:
1914
Florin Coras460dce62018-07-27 05:45:06 -07001915 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001916 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001917 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001918 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1919
1920 ASSERT (rv >= 0);
1921 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001922
Sirshak Das28aa5392019-02-05 01:33:33 -06001923 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001924 {
Florin Corasd6894562020-10-28 00:37:15 -07001925 if (is_ct)
1926 svm_fifo_unset_event (s->rx_fifo);
1927 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001928 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001929 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001930 {
Florin Corasc34118b2020-08-12 18:58:25 -07001931 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1932 e->event_type = SESSION_IO_EVT_RX;
1933 e->session_index = s->session_index;
1934 }
1935 }
Florin Coras4a2c7942020-09-10 12:27:14 -07001936 else if (PREDICT_FALSE (rv < n))
1937 {
1938 /* More data enqueued while reading. Try to drain it
1939 * or fill the buffer */
1940 buf += rv;
1941 n -= rv;
1942 goto read_again;
1943 }
Florin Coras41c9e042018-09-11 00:10:41 -07001944
Florin Coras17ec5772020-08-12 20:46:29 -07001945 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001946 {
Florin Coras17ec5772020-08-12 20:46:29 -07001947 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001948 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1949 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001950 }
1951
Florin Coras5e062572019-03-14 19:07:51 -07001952 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1953 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001954
Florin Coras54693d22018-07-17 10:46:29 -07001955 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001956}
1957
Steven58f464e2017-10-25 12:33:12 -07001958int
Florin Coras134a9962018-08-28 11:32:04 -07001959vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001960{
Florin Coras134a9962018-08-28 11:32:04 -07001961 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001962}
1963
1964static int
Florin Coras134a9962018-08-28 11:32:04 -07001965vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001966{
Florin Coras134a9962018-08-28 11:32:04 -07001967 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001968}
1969
Florin Coras2cba8532018-09-11 16:33:36 -07001970int
1971vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001972 vppcom_data_segment_t * ds, uint32_t n_segments,
1973 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001974{
1975 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001976 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001977 vcl_session_t *s = 0;
1978 svm_fifo_t *rx_fifo;
1979 svm_msg_q_msg_t msg;
1980 session_event_t *e;
1981 svm_msg_q_t *mq;
1982 u8 is_ct;
1983
1984 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001985 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001986 return VPPCOM_EBADFD;
1987
Florin Coras6d0106e2019-01-29 20:11:58 -08001988 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1989 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001990
Florin Corasac422d62020-10-19 20:51:36 -07001991 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07001992 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07001993 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07001994 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001995 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07001996
Sirshak Das28aa5392019-02-05 01:33:33 -06001997 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001998 {
1999 if (is_nonblocking)
2000 {
Florin Coras62877022020-10-28 21:22:04 -07002001 if (is_ct)
2002 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002003 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07002004 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07002005 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002006 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07002007 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002008 if (vcl_session_is_closing (s))
2009 return vcl_session_closing_error (s);
2010
Florin Coras62877022020-10-28 21:22:04 -07002011 if (is_ct)
2012 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07002013 svm_fifo_unset_event (rx_fifo);
2014 svm_msg_q_lock (mq);
2015 if (svm_msg_q_is_empty (mq))
2016 svm_msg_q_wait (mq);
2017
2018 svm_msg_q_sub_w_lock (mq, &msg);
2019 e = svm_msg_q_msg_data (mq, &msg);
2020 svm_msg_q_unlock (mq);
2021 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08002022 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07002023 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07002024 }
2025 }
2026
Florin Corasd1cc38d2020-10-11 11:05:04 -07002027 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2028 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2029 if (n_read < 0)
2030 return VPPCOM_EAGAIN;
2031
2032 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2033 {
Florin Coras62877022020-10-28 21:22:04 -07002034 if (is_ct)
2035 svm_fifo_unset_event (s->rx_fifo);
2036 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002037 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002038 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002039 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002040 {
2041 session_event_t *e;
2042 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2043 e->event_type = SESSION_IO_EVT_RX;
2044 e->session_index = s->session_index;
2045 }
2046 }
2047
2048 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002049 return n_read;
2050}
2051
2052void
Florin Corasd68faf82020-09-25 15:18:13 -07002053vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002054{
2055 vcl_worker_t *wrk = vcl_worker_get_current ();
2056 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002057 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002058
2059 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002060 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002061 return;
2062
Florin Coras62877022020-10-28 21:22:04 -07002063 is_ct = vcl_session_is_ct (s);
2064 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002065
2066 ASSERT (s->rx_bytes_pending < n_bytes);
2067 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002068}
2069
Florin Coras54693d22018-07-17 10:46:29 -07002070static u8
2071vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2072{
Florin Corasc0737e92019-03-04 14:19:39 -08002073 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002074}
2075
Florin Coras7a2abce2020-04-05 19:25:44 +00002076always_inline u8
2077vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002078{
Florin Coras7a2abce2020-04-05 19:25:44 +00002079 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2080 if (is_dgram)
2081 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2082 else
2083 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002084}
2085
2086always_inline int
2087vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2088 size_t n, u8 is_flush, u8 is_dgram)
2089{
Florin Coras6d0106e2019-01-29 20:11:58 -08002090 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002091 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002092 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002093 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002094 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002095 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002096 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002097
jiangxiaomingff31ac62019-11-21 23:34:56 +08002098 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002099 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002100
Florin Coras6c3b2182020-10-19 18:36:48 -07002101 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002102 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002103 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2104 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002105 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002106 }
2107
Florin Coras6d0106e2019-01-29 20:11:58 -08002108 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002109 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002110 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2111 s->session_index, s->vpp_handle, s->session_state,
2112 vppcom_session_state_str (s->session_state));
2113 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002114 }
2115
Florin Coras0e88e852018-09-17 22:09:02 -07002116 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002117 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002118 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002119
Florin Coras653e43f2019-03-04 10:56:23 -08002120 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002121 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002122 {
Florin Coras54693d22018-07-17 10:46:29 -07002123 if (is_nonblocking)
2124 {
Florin Coras070453d2018-08-24 17:04:27 -07002125 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002126 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002127 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002128 {
Florin Coras2d379d82019-06-28 12:45:12 -07002129 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002130 if (vcl_session_is_closing (s))
2131 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002132 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002133 if (svm_msg_q_is_empty (mq))
2134 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002135
Florin Coras54693d22018-07-17 10:46:29 -07002136 svm_msg_q_sub_w_lock (mq, &msg);
2137 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002138 svm_msg_q_unlock (mq);
2139
Florin Coras0e88e852018-09-17 22:09:02 -07002140 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002141 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002142 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002143 }
Dave Wallace543852a2017-08-03 02:11:34 -04002144 }
Dave Wallace543852a2017-08-03 02:11:34 -04002145
Florin Coras653e43f2019-03-04 10:56:23 -08002146 et = SESSION_IO_EVT_TX;
2147 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002148 et = SESSION_IO_EVT_TX_FLUSH;
2149
Florin Coras7a2abce2020-04-05 19:25:44 +00002150 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002151 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002152 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002153 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002154 else
2155 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002156 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002157
Florin Coras14ed6df2019-03-06 21:13:42 -08002158 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08002159 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
2160 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002161
Florin Coras56230092020-09-02 20:52:58 -07002162 /* The underlying fifo segment can run out of memory */
2163 if (PREDICT_FALSE (n_write < 0))
2164 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002165
Florin Coras6d0106e2019-01-29 20:11:58 -08002166 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2167 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002168
Florin Coras54693d22018-07-17 10:46:29 -07002169 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002170}
2171
Florin Coras42ceddb2018-12-12 10:56:01 -08002172int
2173vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2174{
Florin Coras7a2abce2020-04-05 19:25:44 +00002175 vcl_worker_t *wrk = vcl_worker_get_current ();
2176 vcl_session_t *s;
2177
2178 s = vcl_session_get_w_handle (wrk, session_handle);
2179 if (PREDICT_FALSE (!s))
2180 return VPPCOM_EBADFD;
2181
2182 return vppcom_session_write_inline (wrk, s, buf, n,
2183 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002184}
2185
Florin Corasb0f662f2018-12-27 14:51:46 -08002186int
2187vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2188{
Florin Coras7a2abce2020-04-05 19:25:44 +00002189 vcl_worker_t *wrk = vcl_worker_get_current ();
2190 vcl_session_t *s;
2191
2192 s = vcl_session_get_w_handle (wrk, session_handle);
2193 if (PREDICT_FALSE (!s))
2194 return VPPCOM_EBADFD;
2195
2196 return vppcom_session_write_inline (wrk, s, buf, n,
2197 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002198}
2199
Florin Corasc0737e92019-03-04 14:19:39 -08002200#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002201if (PREDICT_FALSE (!_s->rx_fifo)) \
2202 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002203if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2204 { \
2205 if (!vcl_session_is_ct (_s)) \
2206 { \
2207 svm_fifo_unset_event (_s->rx_fifo); \
2208 if (svm_fifo_is_empty (_s->rx_fifo)) \
2209 break; \
2210 } \
2211 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2212 { \
Florin Coras2f630182020-08-13 00:17:51 -07002213 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002214 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2215 break; \
2216 } \
2217 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002218
Florin Coras86f04502018-09-12 16:08:01 -07002219static void
2220vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2221 unsigned long n_bits, unsigned long *read_map,
2222 unsigned long *write_map,
2223 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002224{
2225 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002226 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002227 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002228 u32 sid;
2229
2230 switch (e->event_type)
2231 {
Florin Corasc0737e92019-03-04 14:19:39 -08002232 case SESSION_IO_EVT_RX:
2233 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002234 s = vcl_session_get (wrk, sid);
2235 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002236 break;
Florin Corasb242d312020-10-26 15:35:40 -07002237 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002238 if (sid < n_bits && read_map)
2239 {
David Johnsond9818dd2018-12-14 14:53:41 -05002240 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002241 *bits_set += 1;
2242 }
2243 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002244 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002245 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002246 s = vcl_session_get (wrk, sid);
2247 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002248 break;
2249 if (sid < n_bits && write_map)
2250 {
David Johnsond9818dd2018-12-14 14:53:41 -05002251 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002252 *bits_set += 1;
2253 }
2254 break;
Florin Coras86f04502018-09-12 16:08:01 -07002255 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002256 if (!e->postponed)
2257 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2258 else
2259 s = vcl_session_get (wrk, e->session_index);
2260 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002261 break;
Florin Corasb242d312020-10-26 15:35:40 -07002262 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002263 if (sid < n_bits && read_map)
2264 {
David Johnsond9818dd2018-12-14 14:53:41 -05002265 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002266 *bits_set += 1;
2267 }
2268 break;
2269 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002270 if (!e->postponed)
2271 {
2272 connected_msg = (session_connected_msg_t *) e->data;
2273 sid = vcl_session_connected_handler (wrk, connected_msg);
2274 }
2275 else
2276 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002277 if (sid == VCL_INVALID_SESSION_INDEX)
2278 break;
2279 if (sid < n_bits && write_map)
2280 {
2281 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2282 *bits_set += 1;
2283 }
Florin Coras86f04502018-09-12 16:08:01 -07002284 break;
2285 case SESSION_CTRL_EVT_DISCONNECTED:
2286 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002287 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2288 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002289 break;
Florin Corasb242d312020-10-26 15:35:40 -07002290 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002291 if (sid < n_bits && except_map)
2292 {
David Johnsond9818dd2018-12-14 14:53:41 -05002293 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002294 *bits_set += 1;
2295 }
2296 break;
2297 case SESSION_CTRL_EVT_RESET:
2298 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2299 if (sid < n_bits && except_map)
2300 {
David Johnsond9818dd2018-12-14 14:53:41 -05002301 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002302 *bits_set += 1;
2303 }
2304 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002305 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2306 vcl_session_unlisten_reply_handler (wrk, e->data);
2307 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002308 case SESSION_CTRL_EVT_MIGRATED:
2309 vcl_session_migrated_handler (wrk, e->data);
2310 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002311 case SESSION_CTRL_EVT_CLEANUP:
2312 vcl_session_cleanup_handler (wrk, e->data);
2313 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002314 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2315 vcl_session_worker_update_reply_handler (wrk, e->data);
2316 break;
2317 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2318 vcl_session_req_worker_update_handler (wrk, e->data);
2319 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002320 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2321 vcl_session_app_add_segment_handler (wrk, e->data);
2322 break;
2323 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2324 vcl_session_app_del_segment_handler (wrk, e->data);
2325 break;
hanlina3a48962020-07-13 11:09:15 +08002326 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002327 vcl_worker_rpc_handler (wrk, e->data);
2328 break;
Florin Coras86f04502018-09-12 16:08:01 -07002329 default:
2330 clib_warning ("unhandled: %u", e->event_type);
2331 break;
2332 }
2333}
2334
2335static int
2336vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2337 unsigned long n_bits, unsigned long *read_map,
2338 unsigned long *write_map, unsigned long *except_map,
2339 double time_to_wait, u32 * bits_set)
2340{
Florin Coras99368312018-08-02 10:45:44 -07002341 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002342 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002343 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002344
2345 svm_msg_q_lock (mq);
2346 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002347 {
Florin Coras54693d22018-07-17 10:46:29 -07002348 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002349 {
Florin Coras54693d22018-07-17 10:46:29 -07002350 svm_msg_q_unlock (mq);
2351 return 0;
2352 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002353
Florin Coras54693d22018-07-17 10:46:29 -07002354 if (!time_to_wait)
2355 {
2356 svm_msg_q_unlock (mq);
2357 return 0;
2358 }
2359 else if (time_to_wait < 0)
2360 {
2361 svm_msg_q_wait (mq);
2362 }
2363 else
2364 {
2365 if (svm_msg_q_timedwait (mq, time_to_wait))
2366 {
2367 svm_msg_q_unlock (mq);
2368 return 0;
2369 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002370 }
2371 }
Florin Corase003a1b2019-06-05 10:47:16 -07002372 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002373 svm_msg_q_unlock (mq);
2374
Florin Coras134a9962018-08-28 11:32:04 -07002375 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002376 {
Florin Coras134a9962018-08-28 11:32:04 -07002377 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002378 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002379 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2380 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002381 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002382 }
Florin Coras134a9962018-08-28 11:32:04 -07002383 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002384 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002385 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002386}
2387
Florin Coras99368312018-08-02 10:45:44 -07002388static int
Florin Coras294afe22019-01-07 17:49:17 -08002389vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2390 vcl_si_set * read_map, vcl_si_set * write_map,
2391 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002392 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002393{
Florin Coras14ed6df2019-03-06 21:13:42 -08002394 double wait = 0, start = 0;
2395
2396 if (!*bits_set)
2397 {
2398 wait = time_to_wait;
2399 start = clib_time_now (&wrk->clib_time);
2400 }
2401
2402 do
2403 {
2404 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2405 write_map, except_map, wait, bits_set);
2406 if (*bits_set)
2407 return *bits_set;
2408 if (wait == -1)
2409 continue;
2410
2411 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2412 }
2413 while (wait > 0);
2414
2415 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002416}
2417
2418static int
Florin Coras294afe22019-01-07 17:49:17 -08002419vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2420 vcl_si_set * read_map, vcl_si_set * write_map,
2421 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002422 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002423{
2424 vcl_mq_evt_conn_t *mqc;
2425 int __clib_unused n_read;
2426 int n_mq_evts, i;
2427 u64 buf;
2428
Florin Coras134a9962018-08-28 11:32:04 -07002429 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2430 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2431 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002432 for (i = 0; i < n_mq_evts; i++)
2433 {
Florin Coras134a9962018-08-28 11:32:04 -07002434 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002435 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002436 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002437 except_map, 0, bits_set);
2438 }
2439
2440 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2441}
2442
Dave Wallace543852a2017-08-03 02:11:34 -04002443int
Florin Coras294afe22019-01-07 17:49:17 -08002444vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2445 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002446{
Florin Coras54693d22018-07-17 10:46:29 -07002447 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002448 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002449 vcl_session_t *session = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002450 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002451
Dave Wallace7876d392017-10-19 03:53:57 -04002452 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002453 {
Florin Coras134a9962018-08-28 11:32:04 -07002454 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002455 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002456 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2457 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002458 }
Dave Wallace7876d392017-10-19 03:53:57 -04002459 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002460 {
Florin Coras134a9962018-08-28 11:32:04 -07002461 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002462 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002463 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2464 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002465 }
Dave Wallace7876d392017-10-19 03:53:57 -04002466 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002467 {
Florin Coras134a9962018-08-28 11:32:04 -07002468 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002469 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002470 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2471 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002472 }
2473
Florin Coras54693d22018-07-17 10:46:29 -07002474 if (!n_bits)
2475 return 0;
2476
2477 if (!write_map)
2478 goto check_rd;
2479
2480 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002481 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2482 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002483 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002484 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2485 bits_set++;
2486 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002487 }
2488
Florin Coras5e6222a2020-04-24 17:09:25 +00002489 if (vcl_session_write_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002490 {
David Johnsond9818dd2018-12-14 14:53:41 -05002491 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002492 bits_set++;
2493 }
Florin Coras294afe22019-01-07 17:49:17 -08002494 else
Florin Coras2d379d82019-06-28 12:45:12 -07002495 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002496 }));
2497
2498check_rd:
2499 if (!read_map)
2500 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002501
Florin Coras134a9962018-08-28 11:32:04 -07002502 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2503 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002504 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002505 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2506 bits_set++;
2507 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002508 }
2509
Florin Coras5e6222a2020-04-24 17:09:25 +00002510 if (vcl_session_read_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002511 {
David Johnsond9818dd2018-12-14 14:53:41 -05002512 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002513 bits_set++;
2514 }
2515 }));
2516 /* *INDENT-ON* */
2517
2518check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002519
Florin Coras86f04502018-09-12 16:08:01 -07002520 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2521 {
2522 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2523 read_map, write_map, except_map, &bits_set);
2524 }
2525 vec_reset_length (wrk->unhandled_evts_vector);
2526
Florin Coras99368312018-08-02 10:45:44 -07002527 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002528 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002529 time_to_wait, &bits_set);
2530 else
Florin Coras134a9962018-08-28 11:32:04 -07002531 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002532 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002533
Dave Wallace543852a2017-08-03 02:11:34 -04002534 return (bits_set);
2535}
2536
Dave Wallacef7f809c2017-10-03 01:48:42 -04002537static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002538vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002539{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002540 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002541 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002542 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002543
Florin Corasc0737e92019-03-04 14:19:39 -08002544 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002545 return;
2546
Florin Coras6c3b2182020-10-19 18:36:48 -07002547 s = vcl_session_get_w_handle (wrk, vep_handle);
2548 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002549 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002550 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002551 goto done;
2552 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002553 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002554 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002555 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002556 goto done;
2557 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002558 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002559 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002560 "{\n"
2561 " is_vep = %u\n"
2562 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002563 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002564 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2565 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002566
Florin Coras7e5e62b2019-07-30 14:08:23 -07002567 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002568 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002569 s = vcl_session_get_w_handle (wrk, sh);
2570 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002571 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002572 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002573 goto done;
2574 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002575 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002576 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002577 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002578 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002579 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002580 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002581 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002582 goto done;
2583 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002584 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002585 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2586 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002587 sh, s->vep.vep_sh, vep_handle);
2588 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002589 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002590 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002591 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002592 " next_sh = 0x%x (%u)\n"
2593 " prev_sh = 0x%x (%u)\n"
2594 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002595 " ev.events = 0x%x\n"
2596 " ev.data.u64 = 0x%llx\n"
2597 " et_mask = 0x%x\n"
2598 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002599 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002600 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2601 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002602 }
2603 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002604
2605done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002606 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002607}
2608
2609int
2610vppcom_epoll_create (void)
2611{
Florin Coras134a9962018-08-28 11:32:04 -07002612 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002613 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002614
Florin Coras134a9962018-08-28 11:32:04 -07002615 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616
Florin Coras6c3b2182020-10-19 18:36:48 -07002617 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002618 vep_session->vep.vep_sh = ~0;
2619 vep_session->vep.next_sh = ~0;
2620 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002621 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002622
Florin Corasa7a1a222018-12-30 17:11:31 -08002623 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2624 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002625
Florin Corasab2f6db2018-08-31 14:31:41 -07002626 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002627}
2628
2629int
Florin Coras134a9962018-08-28 11:32:04 -07002630vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002631 struct epoll_event *event)
2632{
Florin Coras134a9962018-08-28 11:32:04 -07002633 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002634 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002635 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002636 vcl_session_t *s;
2637 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002638
Florin Coras134a9962018-08-28 11:32:04 -07002639 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002640 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002641 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002642 return VPPCOM_EINVAL;
2643 }
2644
Florin Coras134a9962018-08-28 11:32:04 -07002645 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002646 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002647 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002648 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002649 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002650 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002651 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002652 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002653 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002654 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655 }
2656
Florin Coras134a9962018-08-28 11:32:04 -07002657 ASSERT (vep_session->vep.vep_sh == ~0);
2658 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002659
Florin Coras17ec5772020-08-12 20:46:29 -07002660 s = vcl_session_get_w_handle (wrk, session_handle);
2661 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002662 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002663 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002664 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002665 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002666 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002667 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002668 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002669 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002670 }
2671
2672 switch (op)
2673 {
2674 case EPOLL_CTL_ADD:
2675 if (PREDICT_FALSE (!event))
2676 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002677 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002678 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002679 }
Florin Coras134a9962018-08-28 11:32:04 -07002680 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002681 {
Florin Coras7e12d942018-06-27 14:32:43 -07002682 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002683 next_session = vcl_session_get_w_handle (wrk,
2684 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002685 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686 {
Florin Coras5e062572019-03-14 19:07:51 -07002687 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002688 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002689 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690 }
Florin Coras134a9962018-08-28 11:32:04 -07002691 ASSERT (next_session->vep.prev_sh == vep_handle);
2692 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002693 }
Florin Coras17ec5772020-08-12 20:46:29 -07002694 s->vep.next_sh = vep_session->vep.next_sh;
2695 s->vep.prev_sh = vep_handle;
2696 s->vep.vep_sh = vep_handle;
2697 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2698 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002699 s->flags &= ~VCL_SESSION_F_IS_VEP;
2700 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002701 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002702
Florin Coras17ec5772020-08-12 20:46:29 -07002703 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2704 if (txf && (event->events & EPOLLOUT))
2705 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002706
Florin Coras6e3c1f82020-01-15 01:30:46 +00002707 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002708 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002709 {
2710 session_event_t e = { 0 };
2711 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002712 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002713 vec_add1 (wrk->unhandled_evts_vector, e);
2714 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002715 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002716 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002717 {
2718 session_event_t e = { 0 };
2719 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002720 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002721 vec_add1 (wrk->unhandled_evts_vector, e);
2722 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002723 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2724 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002725 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002726 break;
2727
2728 case EPOLL_CTL_MOD:
2729 if (PREDICT_FALSE (!event))
2730 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002731 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002732 rv = VPPCOM_EINVAL;
2733 goto done;
2734 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002735 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002736 {
Florin Coras5e062572019-03-14 19:07:51 -07002737 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002738 rv = VPPCOM_EINVAL;
2739 goto done;
2740 }
Florin Coras17ec5772020-08-12 20:46:29 -07002741 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002742 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002743 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002744 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002745 rv = VPPCOM_EINVAL;
2746 goto done;
2747 }
hanlin475c9d72019-12-26 11:44:28 +08002748
2749 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2750 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002751 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002752 {
2753 session_event_t e = { 0 };
2754 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002755 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002756 vec_add1 (wrk->unhandled_evts_vector, e);
2757 }
Florin Coras17ec5772020-08-12 20:46:29 -07002758 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2759 s->vep.ev = *event;
2760 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2761 if (event->events & EPOLLOUT)
2762 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2763 else
2764 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002765 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2766 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002767 break;
2768
2769 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002770 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002772 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002773 rv = VPPCOM_EINVAL;
2774 goto done;
2775 }
Florin Coras17ec5772020-08-12 20:46:29 -07002776 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002777 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002778 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002779 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002780 rv = VPPCOM_EINVAL;
2781 goto done;
2782 }
2783
Florin Coras17ec5772020-08-12 20:46:29 -07002784 if (s->vep.prev_sh == vep_handle)
2785 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002786 else
2787 {
Florin Coras7e12d942018-06-27 14:32:43 -07002788 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002789 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002790 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791 {
Florin Coras5e062572019-03-14 19:07:51 -07002792 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002793 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002794 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002795 }
Florin Coras134a9962018-08-28 11:32:04 -07002796 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002797 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002798 }
Florin Coras17ec5772020-08-12 20:46:29 -07002799 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002800 {
Florin Coras7e12d942018-06-27 14:32:43 -07002801 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002802 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002803 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002804 {
Florin Coras5e062572019-03-14 19:07:51 -07002805 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002806 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002807 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002808 }
Florin Coras134a9962018-08-28 11:32:04 -07002809 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002810 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002811 }
2812
Florin Coras17ec5772020-08-12 20:46:29 -07002813 memset (&s->vep, 0, sizeof (s->vep));
2814 s->vep.next_sh = ~0;
2815 s->vep.prev_sh = ~0;
2816 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002817 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002818
Florin Coras17ec5772020-08-12 20:46:29 -07002819 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2820 if (txf)
2821 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002822
Florin Coras5e062572019-03-14 19:07:51 -07002823 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002824 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002825 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002826 break;
2827
2828 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002829 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002830 rv = VPPCOM_EINVAL;
2831 }
2832
Florin Coras134a9962018-08-28 11:32:04 -07002833 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002834
2835done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002836 return rv;
2837}
2838
Florin Coras86f04502018-09-12 16:08:01 -07002839static inline void
2840vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2841 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002842{
2843 session_disconnected_msg_t *disconnected_msg;
2844 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002845 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002846 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002847 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002848 u8 add_event = 0;
2849
2850 switch (e->event_type)
2851 {
Florin Coras653e43f2019-03-04 10:56:23 -08002852 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002853 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002854 s = vcl_session_get (wrk, sid);
2855 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002856 break;
Florin Corasb242d312020-10-26 15:35:40 -07002857 vcl_fifo_rx_evt_valid_or_break (s);
2858 session_events = s->vep.ev.events;
2859 if (!(EPOLLIN & s->vep.ev.events)
2860 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002861 break;
2862 add_event = 1;
2863 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002864 session_evt_data = s->vep.ev.data.u64;
2865 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002866 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002867 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002868 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002869 s = vcl_session_get (wrk, sid);
2870 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002871 break;
Florin Corasb242d312020-10-26 15:35:40 -07002872 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002873 if (!(EPOLLOUT & session_events))
2874 break;
2875 add_event = 1;
2876 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002877 session_evt_data = s->vep.ev.data.u64;
2878 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2879 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002880 break;
Florin Coras86f04502018-09-12 16:08:01 -07002881 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002882 if (!e->postponed)
2883 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2884 else
2885 s = vcl_session_get (wrk, e->session_index);
2886 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002887 break;
Florin Corasb242d312020-10-26 15:35:40 -07002888 session_events = s->vep.ev.events;
2889 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002890 if (!(EPOLLIN & session_events))
2891 break;
Florin Coras86f04502018-09-12 16:08:01 -07002892 add_event = 1;
2893 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002894 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002895 break;
2896 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002897 if (!e->postponed)
2898 {
2899 connected_msg = (session_connected_msg_t *) e->data;
2900 sid = vcl_session_connected_handler (wrk, connected_msg);
2901 }
2902 else
2903 sid = e->session_index;
2904 s = vcl_session_get (wrk, sid);
2905 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002906 break;
Florin Corasb242d312020-10-26 15:35:40 -07002907 session_events = s->vep.ev.events;
2908 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002909 if (!(EPOLLOUT & session_events))
2910 break;
2911 add_event = 1;
2912 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002913 session_evt_data = s->vep.ev.data.u64;
2914 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002915 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002916 break;
2917 case SESSION_CTRL_EVT_DISCONNECTED:
2918 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002919 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2920 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002921 break;
Florin Corasb242d312020-10-26 15:35:40 -07002922 sid = s->session_index;
2923 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002924 add_event = 1;
2925 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002926 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002927 break;
2928 case SESSION_CTRL_EVT_RESET:
2929 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002930 s = vcl_session_get (wrk, sid);
2931 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002932 break;
Florin Corasb242d312020-10-26 15:35:40 -07002933 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002934 add_event = 1;
2935 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002936 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002937 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002938 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2939 vcl_session_unlisten_reply_handler (wrk, e->data);
2940 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002941 case SESSION_CTRL_EVT_MIGRATED:
2942 vcl_session_migrated_handler (wrk, e->data);
2943 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002944 case SESSION_CTRL_EVT_CLEANUP:
2945 vcl_session_cleanup_handler (wrk, e->data);
2946 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002947 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2948 vcl_session_req_worker_update_handler (wrk, e->data);
2949 break;
2950 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2951 vcl_session_worker_update_reply_handler (wrk, e->data);
2952 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002953 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2954 vcl_session_app_add_segment_handler (wrk, e->data);
2955 break;
2956 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2957 vcl_session_app_del_segment_handler (wrk, e->data);
2958 break;
hanlina3a48962020-07-13 11:09:15 +08002959 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002960 vcl_worker_rpc_handler (wrk, e->data);
2961 break;
Florin Coras86f04502018-09-12 16:08:01 -07002962 default:
2963 VDBG (0, "unhandled: %u", e->event_type);
2964 break;
2965 }
2966
2967 if (add_event)
2968 {
2969 events[*num_ev].data.u64 = session_evt_data;
2970 if (EPOLLONESHOT & session_events)
2971 {
Florin Corasb242d312020-10-26 15:35:40 -07002972 s = vcl_session_get (wrk, sid);
2973 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002974 }
2975 *num_ev += 1;
2976 }
2977}
2978
2979static int
2980vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2981 struct epoll_event *events, u32 maxevents,
2982 double wait_for_time, u32 * num_ev)
2983{
Florin Coras99368312018-08-02 10:45:44 -07002984 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002985 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002986 int i;
2987
Florin Coras539663c2018-09-28 14:59:37 -07002988 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2989 goto handle_dequeued;
2990
Florin Coras54693d22018-07-17 10:46:29 -07002991 svm_msg_q_lock (mq);
2992 if (svm_msg_q_is_empty (mq))
2993 {
2994 if (!wait_for_time)
2995 {
2996 svm_msg_q_unlock (mq);
2997 return 0;
2998 }
2999 else if (wait_for_time < 0)
3000 {
3001 svm_msg_q_wait (mq);
3002 }
3003 else
3004 {
Florin Coras60f1fc12018-08-16 14:57:31 -07003005 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07003006 {
3007 svm_msg_q_unlock (mq);
3008 return 0;
3009 }
3010 }
3011 }
Florin Corase003a1b2019-06-05 10:47:16 -07003012 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08003013 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07003014 svm_msg_q_unlock (mq);
3015
Florin Coras539663c2018-09-28 14:59:37 -07003016handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07003017 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07003018 {
Florin Coras134a9962018-08-28 11:32:04 -07003019 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07003020 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08003021 if (*num_ev < maxevents)
3022 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
3023 else
3024 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003025 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003026 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003027 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003028 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003029 return *num_ev;
3030}
3031
Florin Coras99368312018-08-02 10:45:44 -07003032static int
Florin Coras134a9962018-08-28 11:32:04 -07003033vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003034 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003035{
Florin Corase003a1b2019-06-05 10:47:16 -07003036 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003037
3038 if (!n_evts)
3039 {
3040 wait = wait_for_time;
3041 start = clib_time_now (&wrk->clib_time);
3042 }
3043
3044 do
3045 {
3046 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
3047 wait, &n_evts);
3048 if (n_evts)
3049 return n_evts;
3050 if (wait == -1)
3051 continue;
3052
Florin Corase003a1b2019-06-05 10:47:16 -07003053 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07003054 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07003055 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003056 }
3057 while (wait > 0);
3058
3059 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003060}
3061
3062static int
Florin Coras134a9962018-08-28 11:32:04 -07003063vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003064 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003065{
3066 vcl_mq_evt_conn_t *mqc;
3067 int __clib_unused n_read;
3068 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003069 u64 buf;
3070
Florin Coras134a9962018-08-28 11:32:04 -07003071 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07003072again:
Florin Coras134a9962018-08-28 11:32:04 -07003073 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3074 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07003075 for (i = 0; i < n_mq_evts; i++)
3076 {
Florin Coras134a9962018-08-28 11:32:04 -07003077 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07003078 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07003079 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07003080 }
Florin Corasa4878ed2018-10-12 13:09:36 -07003081 if (!n_evts && n_mq_evts > 0)
3082 goto again;
Florin Coras99368312018-08-02 10:45:44 -07003083
3084 return (int) n_evts;
3085}
3086
Dave Wallacef7f809c2017-10-03 01:48:42 -04003087int
Florin Coras134a9962018-08-28 11:32:04 -07003088vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003089 int maxevents, double wait_for_time)
3090{
Florin Coras134a9962018-08-28 11:32:04 -07003091 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003092 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003093 u32 n_evts = 0;
3094 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003095
3096 if (PREDICT_FALSE (maxevents <= 0))
3097 {
Florin Coras5e062572019-03-14 19:07:51 -07003098 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003099 return VPPCOM_EINVAL;
3100 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003101
Florin Coras134a9962018-08-28 11:32:04 -07003102 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003103 if (!vep_session)
3104 return VPPCOM_EBADFD;
3105
Florin Coras6c3b2182020-10-19 18:36:48 -07003106 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003107 {
Florin Coras5e062572019-03-14 19:07:51 -07003108 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003109 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003110 }
Florin Coras54693d22018-07-17 10:46:29 -07003111
3112 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003113
Florin Coras86f04502018-09-12 16:08:01 -07003114 if (vec_len (wrk->unhandled_evts_vector))
3115 {
3116 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3117 {
3118 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3119 events, &n_evts);
3120 if (n_evts == maxevents)
3121 {
Florin Corase003a1b2019-06-05 10:47:16 -07003122 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3123 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003124 }
3125 }
Florin Corase003a1b2019-06-05 10:47:16 -07003126 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003127 }
3128
3129 if (vcm->cfg.use_mq_eventfd)
3130 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3131 wait_for_time);
3132
3133 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3134 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003135}
3136
Dave Wallace35830af2017-10-09 01:43:42 -04003137int
Florin Coras134a9962018-08-28 11:32:04 -07003138vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003139 void *buffer, uint32_t * buflen)
3140{
Florin Coras134a9962018-08-28 11:32:04 -07003141 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003142 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04003143 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08003144 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003145 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003146
Florin Coras134a9962018-08-28 11:32:04 -07003147 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003148 if (!session)
3149 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003150
Dave Wallace35830af2017-10-09 01:43:42 -04003151 switch (op)
3152 {
3153 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003154 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003155 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3156 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003157 break;
3158
Dave Wallace227867f2017-11-13 21:21:53 -05003159 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003160 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003161 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3162 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003163 break;
3164
3165 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003166 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003167 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003168 *flags =
3169 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003170 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003171 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003172 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003173 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3174 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003175 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003176 }
3177 else
3178 rv = VPPCOM_EINVAL;
3179 break;
3180
3181 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003182 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003183 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003184 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003185 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003186 else
Florin Corasac422d62020-10-19 20:51:36 -07003187 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003188
Florin Coras5e062572019-03-14 19:07:51 -07003189 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3190 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003191 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003192 }
3193 else
3194 rv = VPPCOM_EINVAL;
3195 break;
3196
3197 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003198 if (PREDICT_TRUE (buffer && buflen &&
3199 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003200 {
Florin Coras7e12d942018-06-27 14:32:43 -07003201 ep->is_ip4 = session->transport.is_ip4;
3202 ep->port = session->transport.rmt_port;
3203 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003204 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3205 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003206 else
Dave Barach178cf492018-11-13 16:34:13 -05003207 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3208 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003209 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003210 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3211 "addr = %U, port %u", session_handle, ep->is_ip4,
3212 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003213 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3214 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003215 }
3216 else
3217 rv = VPPCOM_EINVAL;
3218 break;
3219
3220 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003221 if (PREDICT_TRUE (buffer && buflen &&
3222 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003223 {
Florin Coras7e12d942018-06-27 14:32:43 -07003224 ep->is_ip4 = session->transport.is_ip4;
3225 ep->port = session->transport.lcl_port;
3226 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003227 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3228 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003229 else
Dave Barach178cf492018-11-13 16:34:13 -05003230 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3231 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003232 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003233 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3234 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003235 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003236 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3237 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003238 }
3239 else
3240 rv = VPPCOM_EINVAL;
3241 break;
Stevenb5a11602017-10-11 09:59:30 -07003242
Florin Corasef7cbf62019-10-17 09:56:27 -07003243 case VPPCOM_ATTR_SET_LCL_ADDR:
3244 if (PREDICT_TRUE (buffer && buflen &&
3245 (*buflen >= sizeof (*ep)) && ep->ip))
3246 {
3247 session->transport.is_ip4 = ep->is_ip4;
3248 session->transport.lcl_port = ep->port;
3249 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3250 *buflen = sizeof (*ep);
3251 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3252 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3253 &session->transport.lcl_ip,
3254 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3255 clib_net_to_host_u16 (ep->port));
3256 }
3257 else
3258 rv = VPPCOM_EINVAL;
3259 break;
3260
Dave Wallace048b1d62018-01-03 22:24:41 -05003261 case VPPCOM_ATTR_GET_LIBC_EPFD:
3262 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003263 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 break;
3265
3266 case VPPCOM_ATTR_SET_LIBC_EPFD:
3267 if (PREDICT_TRUE (buffer && buflen &&
3268 (*buflen == sizeof (session->libc_epfd))))
3269 {
3270 session->libc_epfd = *(int *) buffer;
3271 *buflen = sizeof (session->libc_epfd);
3272
Florin Coras5e062572019-03-14 19:07:51 -07003273 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3274 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003275 }
3276 else
3277 rv = VPPCOM_EINVAL;
3278 break;
3279
3280 case VPPCOM_ATTR_GET_PROTOCOL:
3281 if (buffer && buflen && (*buflen >= sizeof (int)))
3282 {
Florin Coras7e12d942018-06-27 14:32:43 -07003283 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003284 *buflen = sizeof (int);
3285
Florin Coras5e062572019-03-14 19:07:51 -07003286 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3287 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003288 }
3289 else
3290 rv = VPPCOM_EINVAL;
3291 break;
3292
3293 case VPPCOM_ATTR_GET_LISTEN:
3294 if (buffer && buflen && (*buflen >= sizeof (int)))
3295 {
Florin Corasac422d62020-10-19 20:51:36 -07003296 *(int *) buffer = vcl_session_has_attr (session,
3297 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003298 *buflen = sizeof (int);
3299
Florin Coras5e062572019-03-14 19:07:51 -07003300 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3301 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003302 }
3303 else
3304 rv = VPPCOM_EINVAL;
3305 break;
3306
3307 case VPPCOM_ATTR_GET_ERROR:
3308 if (buffer && buflen && (*buflen >= sizeof (int)))
3309 {
3310 *(int *) buffer = 0;
3311 *buflen = sizeof (int);
3312
Florin Coras5e062572019-03-14 19:07:51 -07003313 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3314 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003315 }
3316 else
3317 rv = VPPCOM_EINVAL;
3318 break;
3319
3320 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3321 if (buffer && buflen && (*buflen >= sizeof (u32)))
3322 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003323
3324 /* VPP-TBD */
3325 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003326 session->tx_fifo ?
3327 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003328 vcm->cfg.tx_fifo_size);
3329 *buflen = sizeof (u32);
3330
Florin Coras5e062572019-03-14 19:07:51 -07003331 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3332 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3333 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003334 }
3335 else
3336 rv = VPPCOM_EINVAL;
3337 break;
3338
3339 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3340 if (buffer && buflen && (*buflen == sizeof (u32)))
3341 {
3342 /* VPP-TBD */
3343 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003344 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3345 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3346 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003347 }
3348 else
3349 rv = VPPCOM_EINVAL;
3350 break;
3351
3352 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3353 if (buffer && buflen && (*buflen >= sizeof (u32)))
3354 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003355
3356 /* VPP-TBD */
3357 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003358 session->rx_fifo ?
3359 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003360 vcm->cfg.rx_fifo_size);
3361 *buflen = sizeof (u32);
3362
Florin Coras5e062572019-03-14 19:07:51 -07003363 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3364 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003365 }
3366 else
3367 rv = VPPCOM_EINVAL;
3368 break;
3369
3370 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3371 if (buffer && buflen && (*buflen == sizeof (u32)))
3372 {
3373 /* VPP-TBD */
3374 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003375 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3376 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3377 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003378 }
3379 else
3380 rv = VPPCOM_EINVAL;
3381 break;
3382
3383 case VPPCOM_ATTR_GET_REUSEADDR:
3384 if (buffer && buflen && (*buflen >= sizeof (int)))
3385 {
3386 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003387 *(int *) buffer = vcl_session_has_attr (session,
3388 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003389 *buflen = sizeof (int);
3390
Florin Coras5e062572019-03-14 19:07:51 -07003391 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3392 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003393 }
3394 else
3395 rv = VPPCOM_EINVAL;
3396 break;
3397
Stevenb5a11602017-10-11 09:59:30 -07003398 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003399 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003400 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003401 {
3402 /* VPP-TBD */
3403 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003404 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
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_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003407
Florin Coras5e062572019-03-14 19:07:51 -07003408 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003409 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
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_REUSEPORT:
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_REUSEPORT);
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_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3425 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003426 }
3427 else
3428 rv = VPPCOM_EINVAL;
3429 break;
3430
3431 case VPPCOM_ATTR_SET_REUSEPORT:
3432 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003433 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 {
3435 /* VPP-TBD */
3436 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003437 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003438 else
Florin Corasac422d62020-10-19 20:51:36 -07003439 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003440
Florin Coras5e062572019-03-14 19:07:51 -07003441 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003442 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003443 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003444 }
3445 else
3446 rv = VPPCOM_EINVAL;
3447 break;
3448
3449 case VPPCOM_ATTR_GET_BROADCAST:
3450 if (buffer && buflen && (*buflen >= sizeof (int)))
3451 {
3452 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003453 *(int *) buffer = vcl_session_has_attr (session,
3454 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003455 *buflen = sizeof (int);
3456
Florin Coras5e062572019-03-14 19:07:51 -07003457 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3458 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003459 }
3460 else
3461 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003462 break;
3463
3464 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003465 if (buffer && buflen && (*buflen == sizeof (int)))
3466 {
3467 /* VPP-TBD */
3468 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003469 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003470 else
Florin Corasac422d62020-10-19 20:51:36 -07003471 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003472
Florin Coras5e062572019-03-14 19:07:51 -07003473 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003474 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003475 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003476 }
3477 else
3478 rv = VPPCOM_EINVAL;
3479 break;
3480
3481 case VPPCOM_ATTR_GET_V6ONLY:
3482 if (buffer && buflen && (*buflen >= sizeof (int)))
3483 {
3484 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003485 *(int *) buffer = vcl_session_has_attr (session,
3486 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003487 *buflen = sizeof (int);
3488
Florin Coras5e062572019-03-14 19:07:51 -07003489 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3490 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003491 }
3492 else
3493 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003494 break;
3495
3496 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003497 if (buffer && buflen && (*buflen == sizeof (int)))
3498 {
3499 /* VPP-TBD */
3500 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003501 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003502 else
Florin Corasac422d62020-10-19 20:51:36 -07003503 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003504
Florin Coras5e062572019-03-14 19:07:51 -07003505 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003506 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003507 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003508 }
3509 else
3510 rv = VPPCOM_EINVAL;
3511 break;
3512
3513 case VPPCOM_ATTR_GET_KEEPALIVE:
3514 if (buffer && buflen && (*buflen >= sizeof (int)))
3515 {
3516 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003517 *(int *) buffer = vcl_session_has_attr (session,
3518 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003519 *buflen = sizeof (int);
3520
Florin Coras5e062572019-03-14 19:07:51 -07003521 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3522 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003523 }
3524 else
3525 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003526 break;
Stevenbccd3392017-10-12 20:42:21 -07003527
3528 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003529 if (buffer && buflen && (*buflen == sizeof (int)))
3530 {
3531 /* VPP-TBD */
3532 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003533 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003534 else
Florin Corasac422d62020-10-19 20:51:36 -07003535 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003536
Florin Coras5e062572019-03-14 19:07:51 -07003537 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003538 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003539 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003540 }
3541 else
3542 rv = VPPCOM_EINVAL;
3543 break;
3544
3545 case VPPCOM_ATTR_GET_TCP_NODELAY:
3546 if (buffer && buflen && (*buflen >= sizeof (int)))
3547 {
3548 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003549 *(int *) buffer = vcl_session_has_attr (session,
3550 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003551 *buflen = sizeof (int);
3552
Florin Coras5e062572019-03-14 19:07:51 -07003553 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3554 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003555 }
3556 else
3557 rv = VPPCOM_EINVAL;
3558 break;
3559
3560 case VPPCOM_ATTR_SET_TCP_NODELAY:
3561 if (buffer && buflen && (*buflen == sizeof (int)))
3562 {
3563 /* VPP-TBD */
3564 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003565 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003566 else
Florin Corasac422d62020-10-19 20:51:36 -07003567 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003568
Florin Coras5e062572019-03-14 19:07:51 -07003569 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003570 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003571 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003572 }
3573 else
3574 rv = VPPCOM_EINVAL;
3575 break;
3576
3577 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3578 if (buffer && buflen && (*buflen >= sizeof (int)))
3579 {
3580 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003581 *(int *) buffer = vcl_session_has_attr (session,
3582 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003583 *buflen = sizeof (int);
3584
Florin Coras5e062572019-03-14 19:07:51 -07003585 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3586 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003587 }
3588 else
3589 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003590 break;
3591
3592 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003593 if (buffer && buflen && (*buflen == sizeof (int)))
3594 {
3595 /* VPP-TBD */
3596 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003597 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 else
Florin Corasac422d62020-10-19 20:51:36 -07003599 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003600
Florin Coras5e062572019-03-14 19:07:51 -07003601 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003602 vcl_session_has_attr (session,
3603 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003604 }
3605 else
3606 rv = VPPCOM_EINVAL;
3607 break;
3608
3609 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3610 if (buffer && buflen && (*buflen >= sizeof (int)))
3611 {
3612 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003613 *(int *) buffer = vcl_session_has_attr (session,
3614 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003615 *buflen = sizeof (int);
3616
Florin Coras5e062572019-03-14 19:07:51 -07003617 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3618 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003619 }
3620 else
3621 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003622 break;
3623
3624 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003625 if (buffer && buflen && (*buflen == sizeof (int)))
3626 {
3627 /* VPP-TBD */
3628 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003629 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003630 else
Florin Corasac422d62020-10-19 20:51:36 -07003631 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003632
Florin Coras5e062572019-03-14 19:07:51 -07003633 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003634 vcl_session_has_attr (session,
3635 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003636 }
3637 else
3638 rv = VPPCOM_EINVAL;
3639 break;
3640
3641 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3642 if (buffer && buflen && (*buflen >= sizeof (u32)))
3643 {
3644 /* VPP-TBD */
3645 *(u32 *) buffer = session->user_mss;
3646 *buflen = sizeof (int);
3647
Florin Coras5e062572019-03-14 19:07:51 -07003648 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3649 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003650 }
3651 else
3652 rv = VPPCOM_EINVAL;
3653 break;
3654
3655 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3656 if (buffer && buflen && (*buflen == sizeof (u32)))
3657 {
3658 /* VPP-TBD */
3659 session->user_mss = *(u32 *) buffer;
3660
Florin Coras5e062572019-03-14 19:07:51 -07003661 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3662 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003663 }
3664 else
3665 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003666 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003667
Florin Coras7baeb712019-01-04 17:05:43 -08003668 case VPPCOM_ATTR_SET_SHUT:
3669 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003670 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003671 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003672 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003673 break;
3674
3675 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003676 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003677 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003678 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003679 tmp_flags |= 2;
3680 if (tmp_flags == 1)
3681 *(int *) buffer = SHUT_RD;
3682 else if (tmp_flags == 2)
3683 *(int *) buffer = SHUT_WR;
3684 else if (tmp_flags == 3)
3685 *(int *) buffer = SHUT_RDWR;
3686 *buflen = sizeof (int);
3687 break;
Florin Coras1e966172020-05-16 18:18:14 +00003688
3689 case VPPCOM_ATTR_SET_CONNECTED:
3690 session->flags |= VCL_SESSION_F_CONNECTED;
3691 break;
3692
Dave Wallacee22aa742017-10-20 12:30:38 -04003693 default:
3694 rv = VPPCOM_EINVAL;
3695 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003696 }
3697
Dave Wallace35830af2017-10-09 01:43:42 -04003698 return rv;
3699}
3700
Stevenac1f96d2017-10-24 16:03:58 -07003701int
Florin Coras134a9962018-08-28 11:32:04 -07003702vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003703 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3704{
Florin Coras134a9962018-08-28 11:32:04 -07003705 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003706 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003707 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003708
3709 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003710 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003711 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003712 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003713 else
3714 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003715 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003716 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003717 }
3718
Florin Coras0a1e1832020-03-29 18:54:04 +00003719 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003720 {
Florin Coras5da10c42020-04-01 04:31:21 +00003721 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003722 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003723 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3724 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003725 else
Dave Barach178cf492018-11-13 16:34:13 -05003726 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3727 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003728 ep->is_ip4 = session->transport.is_ip4;
3729 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003730 }
Florin Coras460dce62018-07-27 05:45:06 -07003731
Stevenac1f96d2017-10-24 16:03:58 -07003732 return rv;
3733}
3734
3735int
Florin Coras134a9962018-08-28 11:32:04 -07003736vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003737 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3738{
Florin Coras7a2abce2020-04-05 19:25:44 +00003739 vcl_worker_t *wrk = vcl_worker_get_current ();
3740 vcl_session_t *s;
3741
3742 s = vcl_session_get_w_handle (wrk, session_handle);
3743 if (!s)
3744 return VPPCOM_EBADFD;
3745
Dave Wallace617dffa2017-10-26 14:47:06 -04003746 if (!buffer)
3747 return VPPCOM_EINVAL;
3748
3749 if (ep)
3750 {
Florin Coras5824cc52020-10-20 18:44:41 -07003751 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003752 return VPPCOM_EINVAL;
3753
3754 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003755 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003756 {
Florin Coras5824cc52020-10-20 18:44:41 -07003757 u32 session_index = s->session_index;
3758 f64 timeout = vcm->cfg.session_timeout;
3759 int rv;
3760
Florin Coras5da10c42020-04-01 04:31:21 +00003761 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003762 rv = vppcom_wait_for_session_state_change (session_index,
3763 VCL_STATE_READY,
3764 timeout);
3765 if (rv < 0)
3766 return rv;
3767 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003768 }
Florin Coras5824cc52020-10-20 18:44:41 -07003769
3770 s->transport.is_ip4 = ep->is_ip4;
3771 s->transport.rmt_port = ep->port;
3772 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003773 }
3774
3775 if (flags)
3776 {
3777 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003778 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003779 }
3780
Florin Coras7a2abce2020-04-05 19:25:44 +00003781 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3782 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003783}
3784
Dave Wallace048b1d62018-01-03 22:24:41 -05003785int
3786vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3787{
Florin Coras134a9962018-08-28 11:32:04 -07003788 vcl_worker_t *wrk = vcl_worker_get_current ();
3789 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003790 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003791 svm_msg_q_msg_t msg;
3792 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003793 int rv, num_ev = 0;
3794
Florin Coras5e062572019-03-14 19:07:51 -07003795 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003796
3797 if (!vp)
3798 return VPPCOM_EFAULT;
3799
3800 do
3801 {
Florin Coras7e12d942018-06-27 14:32:43 -07003802 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003803
Florin Coras6917b942018-11-13 22:44:54 -08003804 /* Dequeue all events and drop all unhandled io events */
3805 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3806 {
3807 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3808 vcl_handle_mq_event (wrk, e);
3809 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3810 }
3811 vec_reset_length (wrk->unhandled_evts_vector);
3812
Dave Wallace048b1d62018-01-03 22:24:41 -05003813 for (i = 0; i < n_sids; i++)
3814 {
Florin Coras7baeb712019-01-04 17:05:43 -08003815 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003816 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003817 {
3818 vp[i].revents = POLLHUP;
3819 num_ev++;
3820 continue;
3821 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003822
Florin Coras6917b942018-11-13 22:44:54 -08003823 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003824
3825 if (POLLIN & vp[i].events)
3826 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003827 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 if (rv > 0)
3829 {
Florin Coras6917b942018-11-13 22:44:54 -08003830 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003831 num_ev++;
3832 }
3833 else if (rv < 0)
3834 {
3835 switch (rv)
3836 {
3837 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003838 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003839 break;
3840
3841 default:
Florin Coras6917b942018-11-13 22:44:54 -08003842 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 break;
3844 }
3845 num_ev++;
3846 }
3847 }
3848
3849 if (POLLOUT & vp[i].events)
3850 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003851 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003852 if (rv > 0)
3853 {
Florin Coras6917b942018-11-13 22:44:54 -08003854 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003855 num_ev++;
3856 }
3857 else if (rv < 0)
3858 {
3859 switch (rv)
3860 {
3861 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003862 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003863 break;
3864
3865 default:
Florin Coras6917b942018-11-13 22:44:54 -08003866 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003867 break;
3868 }
3869 num_ev++;
3870 }
3871 }
3872
Dave Wallace7e607a72018-06-18 18:41:32 -04003873 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003874 {
Florin Coras6917b942018-11-13 22:44:54 -08003875 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003876 num_ev++;
3877 }
3878 }
3879 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003880 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003881 }
3882 while ((num_ev == 0) && keep_trying);
3883
Dave Wallace048b1d62018-01-03 22:24:41 -05003884 return num_ev;
3885}
3886
Florin Coras99368312018-08-02 10:45:44 -07003887int
3888vppcom_mq_epoll_fd (void)
3889{
Florin Coras134a9962018-08-28 11:32:04 -07003890 vcl_worker_t *wrk = vcl_worker_get_current ();
3891 return wrk->mqs_epfd;
3892}
3893
3894int
Florin Coras30e79c22019-01-02 19:31:22 -08003895vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003896{
3897 return session_handle & 0xFFFFFF;
3898}
3899
3900int
Florin Coras30e79c22019-01-02 19:31:22 -08003901vppcom_session_worker (vcl_session_handle_t session_handle)
3902{
3903 return session_handle >> 24;
3904}
3905
3906int
Florin Coras134a9962018-08-28 11:32:04 -07003907vppcom_worker_register (void)
3908{
Florin Coras47c40e22018-11-26 17:01:36 -08003909 if (!vcl_worker_alloc_and_init ())
3910 return VPPCOM_EEXIST;
3911
Florin Coras47c40e22018-11-26 17:01:36 -08003912 if (vcl_worker_register_with_vpp ())
3913 return VPPCOM_EEXIST;
3914
3915 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003916}
3917
Florin Coras369db832019-07-08 12:34:45 -07003918void
3919vppcom_worker_unregister (void)
3920{
3921 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3922 vcl_set_worker_index (~0);
3923}
3924
Pivo6017ff02020-05-04 17:57:33 +02003925void
3926vppcom_worker_index_set (int index)
3927{
3928 vcl_set_worker_index (index);
3929}
3930
Florin Corasdfe4cf42018-11-28 22:13:45 -08003931int
3932vppcom_worker_index (void)
3933{
3934 return vcl_get_worker_index ();
3935}
3936
Florin Corase0982e52019-01-25 13:19:56 -08003937int
3938vppcom_worker_mqs_epfd (void)
3939{
3940 vcl_worker_t *wrk = vcl_worker_get_current ();
3941 if (!vcm->cfg.use_mq_eventfd)
3942 return -1;
3943 return wrk->mqs_epfd;
3944}
3945
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003946int
3947vppcom_session_is_connectable_listener (uint32_t session_handle)
3948{
3949 vcl_session_t *session;
3950 vcl_worker_t *wrk = vcl_worker_get_current ();
3951 session = vcl_session_get_w_handle (wrk, session_handle);
3952 if (!session)
3953 return VPPCOM_EBADFD;
3954 return vcl_session_is_connectable_listener (wrk, session);
3955}
3956
3957int
3958vppcom_session_listener (uint32_t session_handle)
3959{
3960 vcl_worker_t *wrk = vcl_worker_get_current ();
3961 vcl_session_t *listen_session, *session;
3962 session = vcl_session_get_w_handle (wrk, session_handle);
3963 if (!session)
3964 return VPPCOM_EBADFD;
3965 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3966 return VPPCOM_EBADFD;
3967 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3968 if (!listen_session)
3969 return VPPCOM_EBADFD;
3970 return vcl_session_handle (listen_session);
3971}
3972
3973int
3974vppcom_session_n_accepted (uint32_t session_handle)
3975{
3976 vcl_worker_t *wrk = vcl_worker_get_current ();
3977 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3978 if (!session)
3979 return VPPCOM_EBADFD;
3980 return session->n_accepted_sessions;
3981}
3982
Florin Coras66ec4672020-06-15 07:59:40 -07003983const char *
3984vppcom_proto_str (vppcom_proto_t proto)
3985{
3986 char const *proto_str;
3987
3988 switch (proto)
3989 {
3990 case VPPCOM_PROTO_TCP:
3991 proto_str = "TCP";
3992 break;
3993 case VPPCOM_PROTO_UDP:
3994 proto_str = "UDP";
3995 break;
3996 case VPPCOM_PROTO_TLS:
3997 proto_str = "TLS";
3998 break;
3999 case VPPCOM_PROTO_QUIC:
4000 proto_str = "QUIC";
4001 break;
4002 default:
4003 proto_str = "UNKNOWN";
4004 break;
4005 }
4006 return proto_str;
4007}
4008
4009const char *
4010vppcom_retval_str (int retval)
4011{
4012 char const *st;
4013
4014 switch (retval)
4015 {
4016 case VPPCOM_OK:
4017 st = "VPPCOM_OK";
4018 break;
4019
4020 case VPPCOM_EAGAIN:
4021 st = "VPPCOM_EAGAIN";
4022 break;
4023
4024 case VPPCOM_EFAULT:
4025 st = "VPPCOM_EFAULT";
4026 break;
4027
4028 case VPPCOM_ENOMEM:
4029 st = "VPPCOM_ENOMEM";
4030 break;
4031
4032 case VPPCOM_EINVAL:
4033 st = "VPPCOM_EINVAL";
4034 break;
4035
4036 case VPPCOM_EBADFD:
4037 st = "VPPCOM_EBADFD";
4038 break;
4039
4040 case VPPCOM_EAFNOSUPPORT:
4041 st = "VPPCOM_EAFNOSUPPORT";
4042 break;
4043
4044 case VPPCOM_ECONNABORTED:
4045 st = "VPPCOM_ECONNABORTED";
4046 break;
4047
4048 case VPPCOM_ECONNRESET:
4049 st = "VPPCOM_ECONNRESET";
4050 break;
4051
4052 case VPPCOM_ENOTCONN:
4053 st = "VPPCOM_ENOTCONN";
4054 break;
4055
4056 case VPPCOM_ECONNREFUSED:
4057 st = "VPPCOM_ECONNREFUSED";
4058 break;
4059
4060 case VPPCOM_ETIMEDOUT:
4061 st = "VPPCOM_ETIMEDOUT";
4062 break;
4063
4064 default:
4065 st = "UNKNOWN_STATE";
4066 break;
4067 }
4068
4069 return st;
4070}
4071
Dave Wallacee22aa742017-10-20 12:30:38 -04004072/*
4073 * fd.io coding-style-patch-verification: ON
4074 *
4075 * Local Variables:
4076 * eval: (c-set-style "gnu")
4077 * End:
4078 */