blob: ca20ffcdb3090da766f03ecd9f9ae39841216afe [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_CONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040067 st = "STATE_CONNECT";
68 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070069 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040070 st = "STATE_LISTEN";
71 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070072 case VCL_STATE_ACCEPT:
Dave Wallace543852a2017-08-03 02:11:34 -040073 st = "STATE_ACCEPT";
74 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070075 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080076 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050077 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070078 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040079 st = "STATE_DISCONNECT";
80 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070081 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000082 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040083 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070084 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080085 st = "STATE_UPDATED";
86 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070087 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080088 st = "STATE_LISTEN_NO_MQ";
89 break;
Dave Wallace543852a2017-08-03 02:11:34 -040090 default:
91 st = "UNKNOWN_STATE";
92 break;
93 }
94
95 return st;
96}
97
Dave Wallace543852a2017-08-03 02:11:34 -040098u8 *
99format_ip4_address (u8 * s, va_list * args)
100{
101 u8 *a = va_arg (*args, u8 *);
102 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
103}
104
105u8 *
106format_ip6_address (u8 * s, va_list * args)
107{
108 ip6_address_t *a = va_arg (*args, ip6_address_t *);
109 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
110
111 i_max_n_zero = ARRAY_LEN (a->as_u16);
112 max_n_zeros = 0;
113 i_first_zero = i_max_n_zero;
114 n_zeros = 0;
115 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
116 {
117 u32 is_zero = a->as_u16[i] == 0;
118 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
119 {
120 i_first_zero = i;
121 n_zeros = 0;
122 }
123 n_zeros += is_zero;
124 if ((!is_zero && n_zeros > max_n_zeros)
125 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
126 {
127 i_max_n_zero = i_first_zero;
128 max_n_zeros = n_zeros;
129 i_first_zero = ARRAY_LEN (a->as_u16);
130 n_zeros = 0;
131 }
132 }
133
134 last_double_colon = 0;
135 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
136 {
137 if (i == i_max_n_zero && max_n_zeros > 1)
138 {
139 s = format (s, "::");
140 i += max_n_zeros - 1;
141 last_double_colon = 1;
142 }
143 else
144 {
145 s = format (s, "%s%x",
146 (last_double_colon || i == 0) ? "" : ":",
147 clib_net_to_host_u16 (a->as_u16[i]));
148 last_double_colon = 0;
149 }
150 }
151
152 return s;
153}
154
155/* Format an IP46 address. */
156u8 *
157format_ip46_address (u8 * s, va_list * args)
158{
159 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
160 ip46_type_t type = va_arg (*args, ip46_type_t);
161 int is_ip4 = 1;
162
163 switch (type)
164 {
165 case IP46_TYPE_ANY:
166 is_ip4 = ip46_address_is_ip4 (ip46);
167 break;
168 case IP46_TYPE_IP4:
169 is_ip4 = 1;
170 break;
171 case IP46_TYPE_IP6:
172 is_ip4 = 0;
173 break;
174 }
175
176 return is_ip4 ?
177 format (s, "%U", format_ip4_address, &ip46->ip4) :
178 format (s, "%U", format_ip6_address, &ip46->ip6);
179}
180
Florin Coras697faea2018-06-27 17:10:49 -0700181/*
182 * VPPCOM Utility Functions
183 */
184
Florin Coras458089b2019-08-21 16:20:44 -0700185static void
186vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
187{
188 app_session_evt_t _app_evt, *app_evt = &_app_evt;
189 session_listen_msg_t *mp;
190 svm_msg_q_t *mq;
191
192 mq = vcl_worker_ctrl_mq (wrk);
193 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
194 mp = (session_listen_msg_t *) app_evt->evt->data;
195 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700196 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700197 mp->context = s->session_index;
198 mp->wrk_index = wrk->vpp_wrk_index;
199 mp->is_ip4 = s->transport.is_ip4;
200 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
201 mp->port = s->transport.lcl_port;
202 mp->proto = s->session_type;
Florin Coras1e966172020-05-16 18:18:14 +0000203 if (s->flags & VCL_SESSION_F_CONNECTED)
204 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700205 app_send_ctrl_evt_to_vpp (mq, app_evt);
206}
207
208static void
209vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
210{
211 app_session_evt_t _app_evt, *app_evt = &_app_evt;
212 session_connect_msg_t *mp;
213 svm_msg_q_t *mq;
214
215 mq = vcl_worker_ctrl_mq (wrk);
216 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
217 mp = (session_connect_msg_t *) app_evt->evt->data;
218 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700219 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700220 mp->context = s->session_index;
221 mp->wrk_index = wrk->vpp_wrk_index;
222 mp->is_ip4 = s->transport.is_ip4;
223 mp->parent_handle = s->parent_handle;
224 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700225 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700226 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000227 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700228 mp->proto = s->session_type;
Florin Coras0a1e1832020-03-29 18:54:04 +0000229 if (s->flags & VCL_SESSION_F_CONNECTED)
230 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700231 app_send_ctrl_evt_to_vpp (mq, app_evt);
232}
233
234void
235vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
236{
237 app_session_evt_t _app_evt, *app_evt = &_app_evt;
238 session_unlisten_msg_t *mp;
239 svm_msg_q_t *mq;
240
241 mq = vcl_worker_ctrl_mq (wrk);
242 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
243 mp = (session_unlisten_msg_t *) app_evt->evt->data;
244 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700245 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700246 mp->wrk_index = wrk->vpp_wrk_index;
247 mp->handle = s->vpp_handle;
248 mp->context = wrk->wrk_index;
249 app_send_ctrl_evt_to_vpp (mq, app_evt);
250}
251
252static void
253vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
254{
255 app_session_evt_t _app_evt, *app_evt = &_app_evt;
256 session_disconnect_msg_t *mp;
257 svm_msg_q_t *mq;
258
259 /* Send to thread that owns the session */
260 mq = s->vpp_evt_q;
261 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
262 mp = (session_disconnect_msg_t *) app_evt->evt->data;
263 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700264 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700265 mp->handle = s->vpp_handle;
266 app_send_ctrl_evt_to_vpp (mq, app_evt);
267}
268
269static void
270vcl_send_app_detach (vcl_worker_t * wrk)
271{
272 app_session_evt_t _app_evt, *app_evt = &_app_evt;
273 session_app_detach_msg_t *mp;
274 svm_msg_q_t *mq;
275
276 mq = vcl_worker_ctrl_mq (wrk);
277 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
278 mp = (session_app_detach_msg_t *) app_evt->evt->data;
279 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700280 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700281 app_send_ctrl_evt_to_vpp (mq, app_evt);
282}
Florin Coras697faea2018-06-27 17:10:49 -0700283
Florin Coras54693d22018-07-17 10:46:29 -0700284static void
285vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
286 session_handle_t handle, int retval)
287{
288 app_session_evt_t _app_evt, *app_evt = &_app_evt;
289 session_accepted_reply_msg_t *rmp;
290 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
291 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
292 rmp->handle = handle;
293 rmp->context = context;
294 rmp->retval = retval;
295 app_send_ctrl_evt_to_vpp (mq, app_evt);
296}
297
Florin Coras99368312018-08-02 10:45:44 -0700298static void
299vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
300 session_handle_t handle, int retval)
301{
302 app_session_evt_t _app_evt, *app_evt = &_app_evt;
303 session_disconnected_reply_msg_t *rmp;
304 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
305 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
306 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
307 rmp->handle = handle;
308 rmp->context = context;
309 rmp->retval = retval;
310 app_send_ctrl_evt_to_vpp (mq, app_evt);
311}
312
Florin Corasc9fbd662018-08-24 12:59:56 -0700313static void
314vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
315 session_handle_t handle, int retval)
316{
317 app_session_evt_t _app_evt, *app_evt = &_app_evt;
318 session_reset_reply_msg_t *rmp;
319 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
320 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
321 rmp->handle = handle;
322 rmp->context = context;
323 rmp->retval = retval;
324 app_send_ctrl_evt_to_vpp (mq, app_evt);
325}
326
Florin Coras30e79c22019-01-02 19:31:22 -0800327void
328vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
329 u32 wrk_index)
330{
331 app_session_evt_t _app_evt, *app_evt = &_app_evt;
332 session_worker_update_msg_t *mp;
333 svm_msg_q_t *mq;
334
335 mq = vcl_session_vpp_evt_q (wrk, s);
336 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
337 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700338 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800339 mp->handle = s->vpp_handle;
340 mp->req_wrk_index = wrk->vpp_wrk_index;
341 mp->wrk_index = wrk_index;
342 app_send_ctrl_evt_to_vpp (mq, app_evt);
343}
344
hanlina3a48962020-07-13 11:09:15 +0800345int
Florin Coras40c07ce2020-07-16 20:46:17 -0700346vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
347{
348 app_session_evt_t _app_evt, *app_evt = &_app_evt;
349 session_app_wrk_rpc_msg_t *mp;
350 vcl_worker_t *dst_wrk, *wrk;
351 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800352 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700353
354 if (data_len > sizeof (mp->data))
355 goto done;
356
357 clib_spinlock_lock (&vcm->workers_lock);
358
359 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
360 if (!dst_wrk)
361 goto done;
362
363 wrk = vcl_worker_get_current ();
364 mq = vcl_worker_ctrl_mq (wrk);
365 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
366 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700367 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700368 mp->wrk_index = dst_wrk->vpp_wrk_index;
369 clib_memcpy (mp->data, data, data_len);
370 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800371 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700372
373done:
374 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800375 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700376}
377
Florin Coras54693d22018-07-17 10:46:29 -0700378static u32
Florin Coras00cca802019-06-06 09:38:44 -0700379vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
380 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700381{
382 vcl_session_t *session, *listen_session;
383 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700384 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700385 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700386
Florin Coras134a9962018-08-28 11:32:04 -0700387 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700388
Florin Coras00cca802019-06-06 09:38:44 -0700389 listen_session = vcl_session_get (wrk, ls_index);
390 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700391 {
Florin Coras00cca802019-06-06 09:38:44 -0700392 VDBG (0, "ERROR: listener handle %lu does not match session %u",
393 mp->listener_handle, ls_index);
394 goto error;
395 }
396
Florin Corasc4c4cf52019-08-24 18:17:34 -0700397 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Coras00cca802019-06-06 09:38:44 -0700398 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700399 VDBG (0, "ERROR: segment for session %u is not mounted!",
Florin Coras00cca802019-06-06 09:38:44 -0700400 session->session_index);
401 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700402 }
403
Florin Coras54693d22018-07-17 10:46:29 -0700404 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
405 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Coras653e43f2019-03-04 10:56:23 -0800406 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
407 svm_msg_q_t *);
408 rx_fifo->client_session_index = session->session_index;
409 tx_fifo->client_session_index = session->session_index;
410 rx_fifo->client_thread_index = vcl_get_worker_index ();
411 tx_fifo->client_thread_index = vcl_get_worker_index ();
412 vpp_wrk_index = tx_fifo->master_thread_index;
413 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
414 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700415
416 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800417 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700418 session->rx_fifo = rx_fifo;
419 session->tx_fifo = tx_fifo;
420
Florin Corasc127d5a2020-10-14 16:35:58 -0700421 session->session_state = VCL_STATE_ACCEPT;
Florin Coras09d18c22019-04-24 11:10:02 -0700422 session->transport.rmt_port = mp->rmt.port;
423 session->transport.is_ip4 = mp->rmt.is_ip4;
424 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500425 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700426
Florin Coras134a9962018-08-28 11:32:04 -0700427 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700428 session->transport.lcl_port = listen_session->transport.lcl_port;
429 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700430 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200431 session->is_dgram = vcl_proto_is_dgram (session->session_type);
432 session->listener_index = listen_session->session_index;
433 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700434
Florin Coras5e062572019-03-14 19:07:51 -0700435 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
436 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700437 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
438 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
439 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700440 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
441
Florin Coras00cca802019-06-06 09:38:44 -0700442 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
443 session->vpp_handle, 0);
444
Florin Coras134a9962018-08-28 11:32:04 -0700445 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700446
447error:
448 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
449 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
450 VNET_API_ERROR_INVALID_ARGUMENT);
451 vcl_session_free (wrk, session);
452 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700453}
454
455static u32
Florin Coras134a9962018-08-28 11:32:04 -0700456vcl_session_connected_handler (vcl_worker_t * wrk,
457 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700458{
Florin Coras99368312018-08-02 10:45:44 -0700459 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700460 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700461 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700462
463 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700464 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700465 if (!session)
466 {
Florin Coras5e062572019-03-14 19:07:51 -0700467 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
468 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700469 return VCL_INVALID_SESSION_INDEX;
470 }
Florin Coras54693d22018-07-17 10:46:29 -0700471 if (mp->retval)
472 {
Florin Coras5e062572019-03-14 19:07:51 -0700473 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700474 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700475 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700476 session->vpp_handle = mp->handle;
477 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700478 }
479
Florin Corasdbc9c592019-09-25 16:37:43 -0700480 session->vpp_handle = mp->handle;
481 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
482 svm_msg_q_t *);
Florin Coras460dce62018-07-27 05:45:06 -0700483 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
484 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700485 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Corasd85de682018-11-29 17:02:29 -0800486 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700487 VDBG (0, "segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800488 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700489 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700490 vcl_send_session_disconnect (wrk, session);
491 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800492 }
493
Florin Coras460dce62018-07-27 05:45:06 -0700494 rx_fifo->client_session_index = session_index;
495 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700496 rx_fifo->client_thread_index = vcl_get_worker_index ();
497 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700498
Florin Coras653e43f2019-03-04 10:56:23 -0800499 vpp_wrk_index = tx_fifo->master_thread_index;
500 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
501 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700502
Florin Coras653e43f2019-03-04 10:56:23 -0800503 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700504 {
Florin Coras653e43f2019-03-04 10:56:23 -0800505 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
506 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700507 if (vcl_segment_is_not_mounted (wrk, mp->ct_segment_handle))
Florin Coras653e43f2019-03-04 10:56:23 -0800508 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700509 VDBG (0, "ct segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800510 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700511 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700512 vcl_send_session_disconnect (wrk, session);
513 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800514 }
Florin Coras99368312018-08-02 10:45:44 -0700515 }
Florin Coras54693d22018-07-17 10:46:29 -0700516
Florin Coras54693d22018-07-17 10:46:29 -0700517 session->rx_fifo = rx_fifo;
518 session->tx_fifo = tx_fifo;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800519 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras09d18c22019-04-24 11:10:02 -0700520 session->transport.is_ip4 = mp->lcl.is_ip4;
521 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500522 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700523 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700524
525 /* Application closed session before connect reply */
526 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700527 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700528 vcl_send_session_disconnect (wrk, session);
529 else
Florin Corasc127d5a2020-10-14 16:35:58 -0700530 session->session_state = VCL_STATE_CONNECT;
Florin Coras54693d22018-07-17 10:46:29 -0700531
532 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800533 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700534
Florin Coras5e062572019-03-14 19:07:51 -0700535 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
536 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700537 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700538
Florin Coras54693d22018-07-17 10:46:29 -0700539 return session_index;
540}
541
Florin Coras3c7d4f92018-12-14 11:28:43 -0800542static int
543vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
544{
545 vcl_session_msg_t *accepted_msg;
546 int i;
547
548 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
549 {
550 accepted_msg = &session->accept_evts_fifo[i];
551 if (accepted_msg->accepted_msg.handle == handle)
552 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800553 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800554 return 1;
555 }
556 }
557 return 0;
558}
559
Florin Corasc9fbd662018-08-24 12:59:56 -0700560static u32
Florin Coras134a9962018-08-28 11:32:04 -0700561vcl_session_reset_handler (vcl_worker_t * wrk,
562 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700563{
564 vcl_session_t *session;
565 u32 sid;
566
Florin Coras134a9962018-08-28 11:32:04 -0700567 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
568 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700569 if (!session)
570 {
571 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
572 return VCL_INVALID_SESSION_INDEX;
573 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800574
575 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700576 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800577 {
578
579 if (!vcl_flag_accepted_session (session, reset_msg->handle,
580 VCL_ACCEPTED_F_RESET))
581 VDBG (0, "session was not accepted!");
582 return VCL_INVALID_SESSION_INDEX;
583 }
584
Florin Corasc127d5a2020-10-14 16:35:58 -0700585 if (session->session_state != VCL_STATE_CLOSED)
586 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800587 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700588 return sid;
589}
590
Florin Coras60116992018-08-27 09:52:18 -0700591static u32
Florin Coras134a9962018-08-28 11:32:04 -0700592vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700593{
594 vcl_session_t *session;
595 u32 sid = mp->context;
596
Florin Coras134a9962018-08-28 11:32:04 -0700597 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700598 if (mp->retval)
599 {
Florin Coras5e062572019-03-14 19:07:51 -0700600 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700601 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700602 if (session)
603 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700604 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700605 session->vpp_handle = mp->handle;
606 return sid;
607 }
608 else
609 {
Florin Coras5e062572019-03-14 19:07:51 -0700610 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
611 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700612 return VCL_INVALID_SESSION_INDEX;
613 }
614 }
615
616 session->vpp_handle = mp->handle;
617 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500618 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
619 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700620 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700621 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700622 session->session_state = VCL_STATE_LISTEN;
Florin Coras60116992018-08-27 09:52:18 -0700623
Florin Coras5f45e012019-01-23 09:21:30 -0800624 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
625 vec_validate (wrk->vpp_event_queues, 0);
626 wrk->vpp_event_queues[0] = session->vpp_evt_q;
627
Florin Coras6e3c1f82020-01-15 01:30:46 +0000628 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700629 {
630 svm_fifo_t *rx_fifo, *tx_fifo;
631 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
632 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
633 rx_fifo->client_session_index = sid;
634 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
635 tx_fifo->client_session_index = sid;
636 session->rx_fifo = rx_fifo;
637 session->tx_fifo = tx_fifo;
638 }
639
Florin Coras05ecfcc2018-12-12 18:19:39 -0800640 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700641 return sid;
642}
643
Florin Corasdfae9f92019-02-20 19:48:31 -0800644static void
645vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
646{
647 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
648 vcl_session_t *s;
649
650 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000651 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800652 {
653 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
654 return;
655 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700656 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000657 {
658 /* Connected udp listener */
659 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700660 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000661 return;
662
663 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
664 return;
665 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800666
667 if (mp->retval)
668 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700669 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800670
671 if (mp->context != wrk->wrk_index)
672 VDBG (0, "wrong context");
673
674 vcl_session_table_del_vpp_handle (wrk, mp->handle);
675 vcl_session_free (wrk, s);
676}
677
Florin Coras68b7e582020-01-21 18:33:23 -0800678static void
679vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
680{
681 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
682 vcl_session_t *s;
683
684 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
685 if (!s)
686 {
687 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
688 return;
689 }
690
691 s->vpp_thread_index = mp->vpp_thread_index;
Florin Coras57660d92020-04-04 22:45:34 +0000692 s->vpp_handle = mp->new_handle;
Florin Coras68b7e582020-01-21 18:33:23 -0800693 s->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
694
695 vec_validate (wrk->vpp_event_queues, s->vpp_thread_index);
696 wrk->vpp_event_queues[s->vpp_thread_index] = s->vpp_evt_q;
697
698 vcl_session_table_del_vpp_handle (wrk, mp->handle);
699 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
700
701 /* Generate new tx event if we have outstanding data */
702 if (svm_fifo_has_event (s->tx_fifo))
703 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
704 SESSION_IO_EVT_TX, SVM_Q_WAIT);
705
Florin Coras57660d92020-04-04 22:45:34 +0000706 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
707 s->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800708}
709
Florin Coras3c7d4f92018-12-14 11:28:43 -0800710static vcl_session_t *
711vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
712{
713 vcl_session_msg_t *vcl_msg;
714 vcl_session_t *session;
715
716 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
717 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800718 VWRN ("session overlap handle %lu state %u!", msg->handle,
719 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800720
721 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
722 if (!session)
723 {
724 VERR ("couldn't find listen session: listener handle %llx",
725 msg->listener_handle);
726 return 0;
727 }
728
729 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000730 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800731 vcl_msg->accepted_msg = *msg;
732 /* Session handle points to listener until fully accepted by app */
733 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
734
735 return session;
736}
737
738static vcl_session_t *
739vcl_session_disconnected_handler (vcl_worker_t * wrk,
740 session_disconnected_msg_t * msg)
741{
742 vcl_session_t *session;
743
744 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
745 if (!session)
746 {
747 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
748 return 0;
749 }
750
Florin Corasadcfb152020-02-12 08:50:29 +0000751 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700752 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000753 return 0;
754
Florin Coras3c7d4f92018-12-14 11:28:43 -0800755 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700756 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800757 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800758 if (!vcl_flag_accepted_session (session, msg->handle,
759 VCL_ACCEPTED_F_CLOSED))
760 VDBG (0, "session was not accepted!");
761 return 0;
762 }
763
Florin Corasadcfb152020-02-12 08:50:29 +0000764 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700765 if (session->session_state != VCL_STATE_DISCONNECT)
766 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000767
Florin Coras3c7d4f92018-12-14 11:28:43 -0800768 return session;
769}
770
Florin Coras30e79c22019-01-02 19:31:22 -0800771static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700772vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
773{
774 session_cleanup_msg_t *msg;
775 vcl_session_t *session;
776
777 msg = (session_cleanup_msg_t *) data;
778 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
779 if (!session)
780 {
781 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
782 return;
783 }
784
Florin Coras36d49392020-04-24 23:00:11 +0000785 if (msg->type == SESSION_CLEANUP_TRANSPORT)
786 {
787 /* Transport was cleaned up before we confirmed close. Probably the
788 * app is still waiting for some data that cannot be delivered.
789 * Confirm close to make sure everything is cleaned up */
Florin Corasc127d5a2020-10-14 16:35:58 -0700790 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras36d49392020-04-24 23:00:11 +0000791 vcl_session_cleanup (wrk, session, vcl_session_handle (session),
792 1 /* do_disconnect */ );
793 return;
794 }
795
Florin Coras9ace36d2019-10-28 13:14:17 -0700796 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000797 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700798 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000799 {
800 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700801 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000802 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
803 return;
804 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700805 vcl_session_free (wrk, session);
806}
807
808static void
Florin Coras30e79c22019-01-02 19:31:22 -0800809vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
810{
811 session_req_worker_update_msg_t *msg;
812 vcl_session_t *s;
813
814 msg = (session_req_worker_update_msg_t *) data;
815 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
816 if (!s)
817 return;
818
819 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
820}
821
822static void
823vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
824{
825 session_worker_update_reply_msg_t *msg;
826 vcl_session_t *s;
827
828 msg = (session_worker_update_reply_msg_t *) data;
829 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
830 if (!s)
831 {
832 VDBG (0, "unknown handle 0x%llx", msg->handle);
833 return;
834 }
Florin Corasc4c4cf52019-08-24 18:17:34 -0700835 if (vcl_segment_is_not_mounted (wrk, msg->segment_handle))
Florin Coras30e79c22019-01-02 19:31:22 -0800836 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700837 clib_warning ("segment for session %u is not mounted!",
Florin Coras30e79c22019-01-02 19:31:22 -0800838 s->session_index);
839 return;
840 }
Florin Coras30e79c22019-01-02 19:31:22 -0800841
Florin Coras5f45e012019-01-23 09:21:30 -0800842 if (s->rx_fifo)
843 {
844 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
845 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
846 s->rx_fifo->client_session_index = s->session_index;
847 s->tx_fifo->client_session_index = s->session_index;
848 s->rx_fifo->client_thread_index = wrk->wrk_index;
849 s->tx_fifo->client_thread_index = wrk->wrk_index;
850 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700851 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800852
Florin Coras30e79c22019-01-02 19:31:22 -0800853 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
854 s->vpp_handle, wrk->wrk_index);
855}
856
Florin Coras935ce752020-09-08 22:43:47 -0700857static int
858vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
859{
860
861 if (vcm->cfg.vpp_app_socket_api)
862 return vcl_sapi_recv_fds (wrk, fds, n_fds);
863
864 return vcl_bapi_recv_fds (wrk, fds, n_fds);
865}
866
Florin Corasc4c4cf52019-08-24 18:17:34 -0700867static void
868vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
869{
870 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
871 session_app_add_segment_msg_t *msg;
872 u64 segment_handle;
873 int fd = -1;
874
875 msg = (session_app_add_segment_msg_t *) data;
876
877 if (msg->fd_flags)
878 {
Florin Coras935ce752020-09-08 22:43:47 -0700879 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700880 seg_type = SSVM_SEGMENT_MEMFD;
881 }
882
883 segment_handle = msg->segment_handle;
884 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
885 {
886 clib_warning ("invalid segment handle");
887 return;
888 }
889
890 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
891 seg_type, fd))
892 {
893 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
894 return;
895 }
896
897 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
898 msg->segment_size);
899}
900
901static void
902vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
903{
904 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
905 vcl_segment_detach (msg->segment_handle);
906 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
907}
908
Florin Coras40c07ce2020-07-16 20:46:17 -0700909static void
910vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
911{
912 if (!vcm->wrk_rpc_fn)
913 return;
914
hanlina3a48962020-07-13 11:09:15 +0800915 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700916}
917
Florin Coras86f04502018-09-12 16:08:01 -0700918static int
919vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700920{
Florin Coras54693d22018-07-17 10:46:29 -0700921 session_disconnected_msg_t *disconnected_msg;
Florin Corasc127d5a2020-10-14 16:35:58 -0700922 vcl_session_t *s;
Florin Coras54693d22018-07-17 10:46:29 -0700923
924 switch (e->event_type)
925 {
Florin Coras653e43f2019-03-04 10:56:23 -0800926 case SESSION_IO_EVT_RX:
927 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700928 s = vcl_session_get (wrk, e->session_index);
929 if (!s || !(vcl_session_is_ready (s)))
Florin Coras653e43f2019-03-04 10:56:23 -0800930 break;
Florin Coras86f04502018-09-12 16:08:01 -0700931 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700932 break;
933 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800934 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700935 break;
936 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700937 vcl_session_connected_handler (wrk,
938 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700939 break;
940 case SESSION_CTRL_EVT_DISCONNECTED:
941 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasc127d5a2020-10-14 16:35:58 -0700942 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
943 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800944 break;
Florin Corasc127d5a2020-10-14 16:35:58 -0700945 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
946 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700947 break;
948 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700949 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700950 break;
951 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700952 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700953 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800954 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
955 vcl_session_unlisten_reply_handler (wrk, e->data);
956 break;
Florin Coras68b7e582020-01-21 18:33:23 -0800957 case SESSION_CTRL_EVT_MIGRATED:
958 vcl_session_migrated_handler (wrk, e->data);
959 break;
Florin Coras9ace36d2019-10-28 13:14:17 -0700960 case SESSION_CTRL_EVT_CLEANUP:
961 vcl_session_cleanup_handler (wrk, e->data);
962 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800963 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
964 vcl_session_req_worker_update_handler (wrk, e->data);
965 break;
966 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
967 vcl_session_worker_update_reply_handler (wrk, e->data);
968 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700969 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
970 vcl_session_app_add_segment_handler (wrk, e->data);
971 break;
972 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
973 vcl_session_app_del_segment_handler (wrk, e->data);
974 break;
hanlina3a48962020-07-13 11:09:15 +0800975 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -0700976 vcl_worker_rpc_handler (wrk, e->data);
977 break;
Florin Coras54693d22018-07-17 10:46:29 -0700978 default:
979 clib_warning ("unhandled %u", e->event_type);
980 }
981 return VPPCOM_OK;
982}
983
Florin Coras30e79c22019-01-02 19:31:22 -0800984static int
Florin Coras697faea2018-06-27 17:10:49 -0700985vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800986 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700987 f64 wait_for_time)
988{
Florin Coras134a9962018-08-28 11:32:04 -0700989 vcl_worker_t *wrk = vcl_worker_get_current ();
990 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700991 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700992 svm_msg_q_msg_t msg;
993 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400994
Florin Coras697faea2018-06-27 17:10:49 -0700995 do
Dave Wallace543852a2017-08-03 02:11:34 -0400996 {
Florin Coras134a9962018-08-28 11:32:04 -0700997 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700998 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700999 {
Florin Coras070453d2018-08-24 17:04:27 -07001000 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001001 }
1002 if (session->session_state & state)
1003 {
Florin Coras697faea2018-06-27 17:10:49 -07001004 return VPPCOM_OK;
1005 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001006 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001007 {
Florin Coras697faea2018-06-27 17:10:49 -07001008 return VPPCOM_ECONNREFUSED;
1009 }
Florin Coras54693d22018-07-17 10:46:29 -07001010
Florin Coras134a9962018-08-28 11:32:04 -07001011 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001012 {
1013 usleep (100);
1014 continue;
1015 }
Florin Coras134a9962018-08-28 11:32:04 -07001016 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001017 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001018 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001019 }
Florin Coras134a9962018-08-28 11:32:04 -07001020 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001021
Florin Coras05ecfcc2018-12-12 18:19:39 -08001022 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001023 vppcom_session_state_str (state));
1024 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001025
Florin Coras697faea2018-06-27 17:10:49 -07001026 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001027}
1028
Florin Coras30e79c22019-01-02 19:31:22 -08001029static void
1030vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1031{
Florin Coras288eaab2019-02-03 15:26:14 -08001032 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001033 vcl_session_t *s;
1034 u32 *sip;
1035
1036 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1037 return;
1038
1039 vec_foreach (sip, wrk->pending_session_wrk_updates)
1040 {
1041 s = vcl_session_get (wrk, *sip);
1042 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1043 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001044 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1045 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001046 s->session_state = state;
1047 }
1048 vec_reset_length (wrk->pending_session_wrk_updates);
1049}
1050
Florin Corasf9240dc2019-01-15 08:03:17 -08001051void
Florin Coras30e79c22019-01-02 19:31:22 -08001052vcl_flush_mq_events (void)
1053{
1054 vcl_worker_t *wrk = vcl_worker_get_current ();
1055 svm_msg_q_msg_t *msg;
1056 session_event_t *e;
1057 svm_msg_q_t *mq;
1058 int i;
1059
1060 mq = wrk->app_event_queue;
1061 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -07001062 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001063 svm_msg_q_unlock (mq);
1064
1065 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1066 {
1067 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1068 e = svm_msg_q_msg_data (mq, msg);
1069 vcl_handle_mq_event (wrk, e);
1070 svm_msg_q_free_msg (mq, msg);
1071 }
1072 vec_reset_length (wrk->mq_msg_vector);
1073 vcl_handle_pending_wrk_updates (wrk);
1074}
1075
Florin Coras697faea2018-06-27 17:10:49 -07001076static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001077vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001078{
Florin Coras134a9962018-08-28 11:32:04 -07001079 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001080 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001081 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001082 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001083
Florin Corasab2f6db2018-08-31 14:31:41 -07001084 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001085 if (!session)
1086 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001087
Florin Corasaaff5ee2019-07-08 13:00:00 -07001088 /* Flush pending accept events, if any */
1089 while (clib_fifo_elts (session->accept_evts_fifo))
1090 {
1091 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1092 accepted_msg = &evt->accepted_msg;
1093 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1094 vcl_send_session_accepted_reply (session->vpp_evt_q,
1095 accepted_msg->context,
1096 session->vpp_handle, -1);
1097 }
1098 clib_fifo_free (session->accept_evts_fifo);
1099
Florin Coras458089b2019-08-21 16:20:44 -07001100 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001101
Florin Coras5e062572019-03-14 19:07:51 -07001102 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001103 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001104 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001105
1106 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001107 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001108
Florin Coras070453d2018-08-24 17:04:27 -07001109 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001110}
1111
Florin Coras697faea2018-06-27 17:10:49 -07001112static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001113vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001114{
Florin Coras134a9962018-08-28 11:32:04 -07001115 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -07001116 svm_msg_q_t *vpp_evt_q;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001117 vcl_session_t *session, *listen_session;
Florin Coras288eaab2019-02-03 15:26:14 -08001118 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -07001119 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001120
Florin Corasab2f6db2018-08-31 14:31:41 -07001121 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -07001122 if (!session)
1123 return VPPCOM_EBADFD;
1124
Dave Wallace4878cbe2017-11-21 03:45:09 -05001125 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001126 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001127
Florin Coras5e062572019-03-14 19:07:51 -07001128 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
1129 vpp_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001130
Florin Corasc127d5a2020-10-14 16:35:58 -07001131 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001132 {
Florin Coras5e062572019-03-14 19:07:51 -07001133 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
Florin Coras070453d2018-08-24 17:04:27 -07001134 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001135 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001136
Florin Corasc127d5a2020-10-14 16:35:58 -07001137 if (state == VCL_STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001138 {
Florin Coras134a9962018-08-28 11:32:04 -07001139 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Corascc7c88e2020-09-15 15:56:51 -07001140 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->api_client_handle,
Florin Coras99368312018-08-02 10:45:44 -07001141 vpp_handle, 0);
Florin Coras5e062572019-03-14 19:07:51 -07001142 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
1143 session->session_index, vpp_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001144 }
1145 else
Dave Wallace227867f2017-11-13 21:21:53 -05001146 {
Florin Corasa5ea8212020-08-24 21:23:51 -07001147 /* Session doesn't have an event queue yet. Probably a non-blocking
1148 * connect. Wait for the reply */
1149 if (PREDICT_FALSE (!session->vpp_evt_q))
1150 return VPPCOM_OK;
1151
Florin Coras5e062572019-03-14 19:07:51 -07001152 VDBG (1, "session %u [0x%llx]: sending disconnect...",
1153 session->session_index, vpp_handle);
Florin Coras458089b2019-08-21 16:20:44 -07001154 vcl_send_session_disconnect (wrk, session);
Dave Wallace227867f2017-11-13 21:21:53 -05001155 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001156
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001157 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
1158 {
1159 listen_session = vcl_session_get (wrk, session->listener_index);
1160 listen_session->n_accepted_sessions--;
1161 }
1162
Florin Coras070453d2018-08-24 17:04:27 -07001163 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001164}
1165
Florin Coras940f78f2018-11-30 12:11:20 -08001166/**
1167 * Handle app exit
1168 *
1169 * Notify vpp of the disconnect and mark the worker as free. If we're the
1170 * last worker, do a full cleanup otherwise, since we're probably a forked
1171 * child, avoid syscalls as much as possible. We might've lost privileges.
1172 */
1173void
1174vppcom_app_exit (void)
1175{
1176 if (!pool_elts (vcm->workers))
1177 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001178 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1179 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001180 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001181}
1182
Florin Coras935ce752020-09-08 22:43:47 -07001183static int
1184vcl_api_attach (void)
1185{
1186 if (vcm->cfg.vpp_app_socket_api)
1187 return vcl_sapi_attach ();
1188
1189 return vcl_bapi_attach ();
1190}
1191
1192static void
1193vcl_api_detach (vcl_worker_t * wrk)
1194{
1195 vcl_send_app_detach (wrk);
1196
1197 if (vcm->cfg.vpp_app_socket_api)
1198 return vcl_sapi_detach (wrk);
1199
1200 return vcl_bapi_disconnect_from_vpp ();
1201}
1202
Dave Wallace543852a2017-08-03 02:11:34 -04001203/*
1204 * VPPCOM Public API functions
1205 */
1206int
Florin Coras66ec4672020-06-15 07:59:40 -07001207vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001208{
Dave Wallace543852a2017-08-03 02:11:34 -04001209 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001210 int rv;
1211
Florin Coras47c40e22018-11-26 17:01:36 -08001212 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001213 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001214 VDBG (1, "already initialized");
1215 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001216 }
1217
Florin Coras47c40e22018-11-26 17:01:36 -08001218 vcm->is_init = 1;
1219 vppcom_cfg (&vcm->cfg);
1220 vcl_cfg = &vcm->cfg;
1221
1222 vcm->main_cpu = pthread_self ();
1223 vcm->main_pid = getpid ();
1224 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001225 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1226 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001227 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1228 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001229 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001230 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001231 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001232
1233 /* Allocate default worker */
1234 vcl_worker_alloc_and_init ();
1235
Florin Coras935ce752020-09-08 22:43:47 -07001236 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001237 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001238
1239 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001240 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001241
1242 return VPPCOM_OK;
1243}
1244
1245void
1246vppcom_app_destroy (void)
1247{
Florin Corasdc0ded72020-04-30 02:59:55 +00001248 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001249 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001250
Florin Coras940f78f2018-11-30 12:11:20 -08001251 if (!pool_elts (vcm->workers))
1252 return;
1253
Florin Coras0d427d82018-06-27 03:24:07 -07001254 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001255
Florin Corasdc0ded72020-04-30 02:59:55 +00001256 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001257
Florin Corasce815de2020-04-16 18:47:27 +00001258 /* *INDENT-OFF* */
1259 pool_foreach (wrk, vcm->workers, ({
Florin Corasdc0ded72020-04-30 02:59:55 +00001260 if (current_wrk != wrk)
1261 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Florin Corasce815de2020-04-16 18:47:27 +00001262 }));
1263 /* *INDENT-ON* */
1264
Florin Coras935ce752020-09-08 22:43:47 -07001265 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001266 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1267
Florin Coras0d427d82018-06-27 03:24:07 -07001268 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001269
1270 /*
1271 * Free the heap and fix vcm
1272 */
1273 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001274 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001275
1276 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001277 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001278}
1279
1280int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001281vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001282{
Florin Coras134a9962018-08-28 11:32:04 -07001283 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001284 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001285
Florin Coras134a9962018-08-28 11:32:04 -07001286 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001287
Florin Coras7e12d942018-06-27 14:32:43 -07001288 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001289 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001290 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001291 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001292
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001293 if (is_nonblocking)
1294 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001295
Florin Coras7e12d942018-06-27 14:32:43 -07001296 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1297 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001298
Florin Coras5e062572019-03-14 19:07:51 -07001299 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001300
Florin Coras134a9962018-08-28 11:32:04 -07001301 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001302}
1303
1304int
Florin Corasc127d5a2020-10-14 16:35:58 -07001305vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001306 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001307{
Florin Coras288eaab2019-02-03 15:26:14 -08001308 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001309 u32 next_sh, vep_sh;
1310 int rv = VPPCOM_OK;
1311 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001312 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001313
Florin Corasc127d5a2020-10-14 16:35:58 -07001314 is_vep = s->is_vep;
1315 next_sh = s->vep.next_sh;
1316 vep_sh = s->vep.vep_sh;
1317 state = s->session_state;
1318 vpp_handle = s->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001319
Florin Corasc127d5a2020-10-14 16:35:58 -07001320 VDBG (1, "session %u [0x%llx] closing", s->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001321
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001322 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001323 {
Florin Coras134a9962018-08-28 11:32:04 -07001324 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001325 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001326 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001327 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001328 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras47c40e22018-11-26 17:01:36 -08001329 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1330 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001331
Florin Corasc127d5a2020-10-14 16:35:58 -07001332 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001333 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001334 goto cleanup;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001335 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001336
Florin Corasc127d5a2020-10-14 16:35:58 -07001337 if (s->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001338 {
Florin Coras9ace36d2019-10-28 13:14:17 -07001339 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1340 if (rv < 0)
1341 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Corasc127d5a2020-10-14 16:35:58 -07001342 "failed! rv %d (%s)", s->session_index, vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001343 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001344 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001345
Florin Coras9ace36d2019-10-28 13:14:17 -07001346 if (!do_disconnect)
1347 {
1348 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Corasc127d5a2020-10-14 16:35:58 -07001349 s->session_index, vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001350 goto cleanup;
1351 }
1352
Florin Corasc127d5a2020-10-14 16:35:58 -07001353 if (state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001354 {
1355 rv = vppcom_session_unbind (sh);
1356 if (PREDICT_FALSE (rv < 0))
1357 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Corasc127d5a2020-10-14 16:35:58 -07001358 "rv %d (%s)", s->session_index, vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001359 vppcom_retval_str (rv));
1360 return rv;
1361 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001362 else if ((vcl_session_is_ready (s))
1363 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001364 {
1365 rv = vppcom_session_disconnect (sh);
1366 if (PREDICT_FALSE (rv < 0))
1367 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Corasc127d5a2020-10-14 16:35:58 -07001368 " rv %d (%s)", s->session_index, vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001369 rv, vppcom_retval_str (rv));
1370 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001371 else if (state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001372 {
Florin Corasc127d5a2020-10-14 16:35:58 -07001373 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, s);
Florin Corascc7c88e2020-09-15 15:56:51 -07001374 vcl_send_session_reset_reply (mq, wrk->api_client_handle,
Florin Corasc127d5a2020-10-14 16:35:58 -07001375 s->vpp_handle, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001376 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001377 else if (state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001378 {
1379 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001380 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001381 goto free_session;
1382 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001383
Florin Corasc127d5a2020-10-14 16:35:58 -07001384 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001385
Florin Coras9ace36d2019-10-28 13:14:17 -07001386 /* Session is removed only after vpp confirms the disconnect */
1387 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001388
Florin Corasf9240dc2019-01-15 08:03:17 -08001389cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001390 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001391free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001392 vcl_session_free (wrk, s);
1393 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001394
Dave Wallace543852a2017-08-03 02:11:34 -04001395 return rv;
1396}
1397
1398int
Florin Corasf9240dc2019-01-15 08:03:17 -08001399vppcom_session_close (uint32_t session_handle)
1400{
1401 vcl_worker_t *wrk = vcl_worker_get_current ();
1402 vcl_session_t *session;
1403
1404 session = vcl_session_get_w_handle (wrk, session_handle);
1405 if (!session)
1406 return VPPCOM_EBADFD;
1407 return vcl_session_cleanup (wrk, session, session_handle,
1408 1 /* do_disconnect */ );
1409}
1410
1411int
Florin Coras134a9962018-08-28 11:32:04 -07001412vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001413{
Florin Coras134a9962018-08-28 11:32:04 -07001414 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001415 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001416
1417 if (!ep || !ep->ip)
1418 return VPPCOM_EINVAL;
1419
Florin Coras134a9962018-08-28 11:32:04 -07001420 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001421 if (!session)
1422 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001423
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001424 if (session->is_vep)
1425 {
Florin Coras5e062572019-03-14 19:07:51 -07001426 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1427 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001428 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001429 }
1430
Florin Coras7e12d942018-06-27 14:32:43 -07001431 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001432 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001433 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1434 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001435 else
Dave Barach178cf492018-11-13 16:34:13 -05001436 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1437 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001438 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001439
Florin Coras5e062572019-03-14 19:07:51 -07001440 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1441 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001442 session->transport.is_ip4 ? "IPv4" : "IPv6",
1443 format_ip46_address, &session->transport.lcl_ip,
1444 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1445 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001446 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001447 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001448
1449 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001450 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001451
Florin Coras070453d2018-08-24 17:04:27 -07001452 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001453}
1454
1455int
Florin Coras134a9962018-08-28 11:32:04 -07001456vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001457{
Florin Coras134a9962018-08-28 11:32:04 -07001458 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001459 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001460 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001461 int rv;
1462
Florin Coras134a9962018-08-28 11:32:04 -07001463 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001464 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001465 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001466
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001467 if (q_len == 0 || q_len == ~0)
1468 q_len = vcm->cfg.listen_queue_size;
1469
Dave Wallaceee45d412017-11-24 21:44:06 -05001470 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001471 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001472 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001473 VDBG (0, "session %u [0x%llx]: already in listen state!",
1474 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001475 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001476 }
1477
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001478 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001479
Florin Coras070453d2018-08-24 17:04:27 -07001480 /*
1481 * Send listen request to vpp and wait for reply
1482 */
Florin Coras458089b2019-08-21 16:20:44 -07001483 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001484 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001485 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001486 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001487
Florin Coras070453d2018-08-24 17:04:27 -07001488 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001489 {
Florin Coras134a9962018-08-28 11:32:04 -07001490 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001491 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1492 listen_sh, listen_session->vpp_handle, rv,
1493 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001494 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001495 }
1496
Florin Coras070453d2018-08-24 17:04:27 -07001497 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001498}
1499
Florin Coras134a9962018-08-28 11:32:04 -07001500static int
Florin Coras5e062572019-03-14 19:07:51 -07001501validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001502{
Florin Coras5e062572019-03-14 19:07:51 -07001503 if (ls->is_vep)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001504 {
Florin Coras5e062572019-03-14 19:07:51 -07001505 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1506 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001507 return VPPCOM_EBADFD;
1508 }
1509
Florin Corasc127d5a2020-10-14 16:35:58 -07001510 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001511 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001512 {
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001513 VDBG (0,
1514 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1515 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001516 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001517 return VPPCOM_EBADFD;
1518 }
1519 return VPPCOM_OK;
1520}
1521
1522int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001523vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1524{
1525 if (!strcmp (proto_str, "TCP"))
1526 *proto = VPPCOM_PROTO_TCP;
1527 else if (!strcmp (proto_str, "tcp"))
1528 *proto = VPPCOM_PROTO_TCP;
1529 else if (!strcmp (proto_str, "UDP"))
1530 *proto = VPPCOM_PROTO_UDP;
1531 else if (!strcmp (proto_str, "udp"))
1532 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001533 else if (!strcmp (proto_str, "TLS"))
1534 *proto = VPPCOM_PROTO_TLS;
1535 else if (!strcmp (proto_str, "tls"))
1536 *proto = VPPCOM_PROTO_TLS;
1537 else if (!strcmp (proto_str, "QUIC"))
1538 *proto = VPPCOM_PROTO_QUIC;
1539 else if (!strcmp (proto_str, "quic"))
1540 *proto = VPPCOM_PROTO_QUIC;
1541 else
1542 return 1;
1543 return 0;
1544}
1545
1546int
Florin Coras134a9962018-08-28 11:32:04 -07001547vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001548 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001549{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001550 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001551 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001552 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001553 vcl_session_t *listen_session = 0;
1554 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001555 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001556 svm_msg_q_msg_t msg;
1557 session_event_t *e;
1558 u8 is_nonblocking;
1559 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001560
Florin Coras134a9962018-08-28 11:32:04 -07001561 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001562 if (!listen_session)
1563 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001564
Florin Coras134a9962018-08-28 11:32:04 -07001565 listen_session_index = listen_session->session_index;
1566 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001567 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001568
Florin Coras54693d22018-07-17 10:46:29 -07001569 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001570 {
Florin Coras54693d22018-07-17 10:46:29 -07001571 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001572 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001573 accepted_msg = evt->accepted_msg;
1574 goto handle;
1575 }
1576
1577 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1578 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001579 while (1)
1580 {
Carl Smith592a9092019-11-12 14:57:37 +13001581 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1582 return VPPCOM_EAGAIN;
1583
Florin Coras134a9962018-08-28 11:32:04 -07001584 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001585 return VPPCOM_EAGAIN;
1586
Florin Coras134a9962018-08-28 11:32:04 -07001587 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001588 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001589 {
wanghanlin96453fd2019-12-16 19:14:39 +08001590 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001591 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001592 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001593 }
Dave Barach178cf492018-11-13 16:34:13 -05001594 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001595 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001596 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001597 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001598
Florin Coras54693d22018-07-17 10:46:29 -07001599handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001600
Florin Coras00cca802019-06-06 09:38:44 -07001601 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1602 listen_session_index);
1603 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1604 return VPPCOM_ECONNABORTED;
1605
Florin Coras134a9962018-08-28 11:32:04 -07001606 listen_session = vcl_session_get (wrk, listen_session_index);
1607 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001608
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001609 if (flags & O_NONBLOCK)
1610 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001611
Florin Coras5e062572019-03-14 19:07:51 -07001612 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1613 " flags %d, is_nonblocking %u", listen_session->session_index,
1614 listen_session->vpp_handle, client_session_index,
1615 client_session->vpp_handle, flags,
1616 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001617
Dave Wallace048b1d62018-01-03 22:24:41 -05001618 if (ep)
1619 {
Florin Coras7e12d942018-06-27 14:32:43 -07001620 ep->is_ip4 = client_session->transport.is_ip4;
1621 ep->port = client_session->transport.rmt_port;
1622 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001623 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1624 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001625 else
Dave Barach178cf492018-11-13 16:34:13 -05001626 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1627 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001628 }
Dave Wallace60caa062017-11-10 17:07:13 -05001629
Florin Coras05ecfcc2018-12-12 18:19:39 -08001630 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001631 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001632 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001633 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001634 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001635 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001636 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001637 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001638 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001639 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1640 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001641
Florin Coras3c7d4f92018-12-14 11:28:43 -08001642 /*
1643 * Session might have been closed already
1644 */
1645 if (accept_flags)
1646 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001647 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001648 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001649 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001650 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001651 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001652 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001653}
1654
1655int
Florin Coras134a9962018-08-28 11:32:04 -07001656vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001657{
Florin Coras134a9962018-08-28 11:32:04 -07001658 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001659 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001660 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001661 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001662
Florin Coras134a9962018-08-28 11:32:04 -07001663 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001664 if (!session)
1665 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001666 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001667
1668 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001669 {
Florin Coras5e062572019-03-14 19:07:51 -07001670 VDBG (0, "ERROR: cannot connect epoll session %u!",
1671 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001672 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001673 }
1674
Florin Corasc127d5a2020-10-14 16:35:58 -07001675 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001676 {
Florin Coras7baeb712019-01-04 17:05:43 -08001677 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001678 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001679 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001680 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001681 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001682 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001683 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001684 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001685 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001686 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001687 }
1688
Florin Coras0a1e1832020-03-29 18:54:04 +00001689 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001690 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001691 {
1692 if (session->session_type != VPPCOM_PROTO_UDP)
1693 return VPPCOM_EINVAL;
1694 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001695 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001696 }
1697
Florin Coras7e12d942018-06-27 14:32:43 -07001698 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001699 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001700 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001701 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001702 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001703
Florin Coras0a1e1832020-03-29 18:54:04 +00001704 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001705 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001706 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001707 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001708 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001709 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001710 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001711 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001712 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001713
Florin Coras458089b2019-08-21 16:20:44 -07001714 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001715
1716 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001717 {
fanyfa49d59d2020-10-13 17:07:16 +08001718 /* State set to STATE_UPDATED to ensure the session is not assumed
1719 * to be open and to also allow the app to close it prior to vpp's
1720 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001721 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001722 return VPPCOM_EINPROGRESS;
1723 }
Florin Coras57c88932019-08-29 12:03:17 -07001724
1725 /*
1726 * Wait for reply from vpp if blocking
1727 */
Florin Corasc127d5a2020-10-14 16:35:58 -07001728 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_CONNECT,
Florin Coras070453d2018-08-24 17:04:27 -07001729 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001730
Florin Coras134a9962018-08-28 11:32:04 -07001731 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001732 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1733 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001734
Dave Wallace4878cbe2017-11-21 03:45:09 -05001735 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001736}
1737
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001738int
1739vppcom_session_stream_connect (uint32_t session_handle,
1740 uint32_t parent_session_handle)
1741{
1742 vcl_worker_t *wrk = vcl_worker_get_current ();
1743 vcl_session_t *session, *parent_session;
1744 u32 session_index, parent_session_index;
1745 int rv;
1746
1747 session = vcl_session_get_w_handle (wrk, session_handle);
1748 if (!session)
1749 return VPPCOM_EBADFD;
1750 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1751 if (!parent_session)
1752 return VPPCOM_EBADFD;
1753
1754 session_index = session->session_index;
1755 parent_session_index = parent_session->session_index;
1756 if (PREDICT_FALSE (session->is_vep))
1757 {
1758 VDBG (0, "ERROR: cannot connect epoll session %u!",
1759 session->session_index);
1760 return VPPCOM_EBADFD;
1761 }
1762
Florin Corasc127d5a2020-10-14 16:35:58 -07001763 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001764 {
1765 VDBG (0, "session handle %u [0x%llx]: session already "
1766 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1767 session_handle, session->vpp_handle,
1768 parent_session_handle, parent_session->vpp_handle,
1769 vppcom_proto_str (session->session_type), session->session_state,
1770 vppcom_session_state_str (session->session_state));
1771 return VPPCOM_OK;
1772 }
1773
1774 /* Connect to quic session specifics */
1775 session->transport.is_ip4 = parent_session->transport.is_ip4;
1776 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1777 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001778 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001779
1780 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1781 session_handle, parent_session_handle, parent_session->vpp_handle);
1782
1783 /*
1784 * Send connect request and wait for reply from vpp
1785 */
Florin Coras458089b2019-08-21 16:20:44 -07001786 vcl_send_session_connect (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001787 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_CONNECT,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001788 vcm->cfg.session_timeout);
1789
1790 session->listener_index = parent_session_index;
1791 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001792 if (parent_session)
1793 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001794
1795 session = vcl_session_get (wrk, session_index);
1796 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1797 session->vpp_handle, rv ? "failed" : "succeeded");
1798
1799 return rv;
1800}
1801
Florin Coras54693d22018-07-17 10:46:29 -07001802static u8
1803vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1804{
Florin Corasc0737e92019-03-04 14:19:39 -08001805 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001806}
1807
Steven58f464e2017-10-25 12:33:12 -07001808static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001809vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001810 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001811{
Florin Coras134a9962018-08-28 11:32:04 -07001812 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001813 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001814 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001815 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001816 svm_msg_q_msg_t msg;
1817 session_event_t *e;
1818 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001819 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001820
Florin Coras070453d2018-08-24 17:04:27 -07001821 if (PREDICT_FALSE (!buf))
1822 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001823
Florin Coras134a9962018-08-28 11:32:04 -07001824 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001825 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001826 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001827
Florin Coras6d0106e2019-01-29 20:11:58 -08001828 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001829 {
Florin Coras5e062572019-03-14 19:07:51 -07001830 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001831 s->session_index, s->vpp_handle, s->session_state,
1832 vppcom_session_state_str (s->session_state));
1833 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001834 }
1835
Florin Coras2cba8532018-09-11 16:33:36 -07001836 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001837 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001838 mq = wrk->app_event_queue;
1839 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001840 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001841
Sirshak Das28aa5392019-02-05 01:33:33 -06001842 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001843 {
Florin Coras54693d22018-07-17 10:46:29 -07001844 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001845 {
Yu Pingb2955352019-12-04 06:49:04 +08001846 if (vcl_session_is_closing (s))
1847 return vcl_session_closing_error (s);
Florin Corasc0737e92019-03-04 14:19:39 -08001848 svm_fifo_unset_event (s->rx_fifo);
1849 return VPPCOM_EWOULDBLOCK;
1850 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001851 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001852 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001853 if (vcl_session_is_closing (s))
1854 return vcl_session_closing_error (s);
1855
Florin Corasc0737e92019-03-04 14:19:39 -08001856 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001857 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001858 if (svm_msg_q_is_empty (mq))
1859 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001860
Florin Coras54693d22018-07-17 10:46:29 -07001861 svm_msg_q_sub_w_lock (mq, &msg);
1862 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001863 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001864 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001865 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001866 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001867 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001868 }
Florin Coras54693d22018-07-17 10:46:29 -07001869
Florin Coras4a2c7942020-09-10 12:27:14 -07001870read_again:
1871
Florin Coras460dce62018-07-27 05:45:06 -07001872 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001873 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001874 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001875 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1876
1877 ASSERT (rv >= 0);
1878 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001879
Sirshak Das28aa5392019-02-05 01:33:33 -06001880 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001881 {
1882 svm_fifo_unset_event (s->rx_fifo);
1883 if (!svm_fifo_is_empty_cons (rx_fifo)
1884 && svm_fifo_set_event (s->rx_fifo) && is_nonblocking)
1885 {
Florin Corasc34118b2020-08-12 18:58:25 -07001886 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1887 e->event_type = SESSION_IO_EVT_RX;
1888 e->session_index = s->session_index;
1889 }
1890 }
Florin Coras4a2c7942020-09-10 12:27:14 -07001891 else if (PREDICT_FALSE (rv < n))
1892 {
1893 /* More data enqueued while reading. Try to drain it
1894 * or fill the buffer */
1895 buf += rv;
1896 n -= rv;
1897 goto read_again;
1898 }
Florin Coras41c9e042018-09-11 00:10:41 -07001899
Florin Coras17ec5772020-08-12 20:46:29 -07001900 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001901 {
Florin Coras17ec5772020-08-12 20:46:29 -07001902 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001903 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1904 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001905 }
1906
Florin Coras5e062572019-03-14 19:07:51 -07001907 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1908 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001909
Florin Coras54693d22018-07-17 10:46:29 -07001910 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001911}
1912
Steven58f464e2017-10-25 12:33:12 -07001913int
Florin Coras134a9962018-08-28 11:32:04 -07001914vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001915{
Florin Coras134a9962018-08-28 11:32:04 -07001916 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001917}
1918
1919static int
Florin Coras134a9962018-08-28 11:32:04 -07001920vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001921{
Florin Coras134a9962018-08-28 11:32:04 -07001922 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001923}
1924
Florin Coras2cba8532018-09-11 16:33:36 -07001925int
1926vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001927 vppcom_data_segment_t * ds, uint32_t n_segments,
1928 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001929{
1930 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001931 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001932 vcl_session_t *s = 0;
1933 svm_fifo_t *rx_fifo;
1934 svm_msg_q_msg_t msg;
1935 session_event_t *e;
1936 svm_msg_q_t *mq;
1937 u8 is_ct;
1938
1939 s = vcl_session_get_w_handle (wrk, session_handle);
1940 if (PREDICT_FALSE (!s || s->is_vep))
1941 return VPPCOM_EBADFD;
1942
Florin Coras6d0106e2019-01-29 20:11:58 -08001943 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1944 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001945
1946 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1947 is_ct = vcl_session_is_ct (s);
1948 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1949 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001950 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001951
Florin Coras653e43f2019-03-04 10:56:23 -08001952 if (is_ct)
1953 svm_fifo_unset_event (s->rx_fifo);
1954
Sirshak Das28aa5392019-02-05 01:33:33 -06001955 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001956 {
1957 if (is_nonblocking)
1958 {
1959 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001960 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001961 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001962 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001963 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001964 if (vcl_session_is_closing (s))
1965 return vcl_session_closing_error (s);
1966
Florin Coras2cba8532018-09-11 16:33:36 -07001967 svm_fifo_unset_event (rx_fifo);
1968 svm_msg_q_lock (mq);
1969 if (svm_msg_q_is_empty (mq))
1970 svm_msg_q_wait (mq);
1971
1972 svm_msg_q_sub_w_lock (mq, &msg);
1973 e = svm_msg_q_msg_data (mq, &msg);
1974 svm_msg_q_unlock (mq);
1975 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001976 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001977 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001978 }
1979 }
1980
Florin Corasd1cc38d2020-10-11 11:05:04 -07001981 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
1982 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
1983 if (n_read < 0)
1984 return VPPCOM_EAGAIN;
1985
1986 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
1987 {
1988 svm_fifo_unset_event (s->rx_fifo);
1989 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
1990 && svm_fifo_set_event (s->rx_fifo)
1991 && VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK))
1992 {
1993 session_event_t *e;
1994 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1995 e->event_type = SESSION_IO_EVT_RX;
1996 e->session_index = s->session_index;
1997 }
1998 }
1999
2000 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002001 return n_read;
2002}
2003
2004void
Florin Corasd68faf82020-09-25 15:18:13 -07002005vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002006{
2007 vcl_worker_t *wrk = vcl_worker_get_current ();
2008 vcl_session_t *s;
2009
2010 s = vcl_session_get_w_handle (wrk, session_handle);
2011 if (PREDICT_FALSE (!s || s->is_vep))
2012 return;
2013
Florin Corasd68faf82020-09-25 15:18:13 -07002014 svm_fifo_dequeue_drop (s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002015
2016 ASSERT (s->rx_bytes_pending < n_bytes);
2017 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002018}
2019
Florin Coras54693d22018-07-17 10:46:29 -07002020static u8
2021vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2022{
Florin Corasc0737e92019-03-04 14:19:39 -08002023 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002024}
2025
Florin Coras7a2abce2020-04-05 19:25:44 +00002026always_inline u8
2027vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002028{
Florin Coras7a2abce2020-04-05 19:25:44 +00002029 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2030 if (is_dgram)
2031 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2032 else
2033 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002034}
2035
2036always_inline int
2037vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2038 size_t n, u8 is_flush, u8 is_dgram)
2039{
Florin Coras6d0106e2019-01-29 20:11:58 -08002040 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002041 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002042 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002043 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002044 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002045 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002046 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002047
jiangxiaomingff31ac62019-11-21 23:34:56 +08002048 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002049 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002050
Florin Coras460dce62018-07-27 05:45:06 -07002051 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04002052 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002053 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2054 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002055 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002056 }
2057
Florin Coras6d0106e2019-01-29 20:11:58 -08002058 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002059 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002060 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2061 s->session_index, s->vpp_handle, s->session_state,
2062 vppcom_session_state_str (s->session_state));
2063 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002064 }
2065
Florin Coras0e88e852018-09-17 22:09:02 -07002066 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002067 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07002068 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002069
Florin Coras653e43f2019-03-04 10:56:23 -08002070 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002071 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002072 {
Florin Coras54693d22018-07-17 10:46:29 -07002073 if (is_nonblocking)
2074 {
Florin Coras070453d2018-08-24 17:04:27 -07002075 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002076 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002077 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002078 {
Florin Coras2d379d82019-06-28 12:45:12 -07002079 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002080 if (vcl_session_is_closing (s))
2081 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002082 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002083 if (svm_msg_q_is_empty (mq))
2084 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002085
Florin Coras54693d22018-07-17 10:46:29 -07002086 svm_msg_q_sub_w_lock (mq, &msg);
2087 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002088 svm_msg_q_unlock (mq);
2089
Florin Coras0e88e852018-09-17 22:09:02 -07002090 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002091 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002092 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002093 }
Dave Wallace543852a2017-08-03 02:11:34 -04002094 }
Dave Wallace543852a2017-08-03 02:11:34 -04002095
Florin Coras653e43f2019-03-04 10:56:23 -08002096 et = SESSION_IO_EVT_TX;
2097 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002098 et = SESSION_IO_EVT_TX_FLUSH;
2099
Florin Coras7a2abce2020-04-05 19:25:44 +00002100 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002101 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002102 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002103 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002104 else
2105 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002106 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002107
Florin Coras14ed6df2019-03-06 21:13:42 -08002108 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08002109 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
2110 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002111
Florin Coras56230092020-09-02 20:52:58 -07002112 /* The underlying fifo segment can run out of memory */
2113 if (PREDICT_FALSE (n_write < 0))
2114 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002115
Florin Coras6d0106e2019-01-29 20:11:58 -08002116 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2117 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002118
Florin Coras54693d22018-07-17 10:46:29 -07002119 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002120}
2121
Florin Coras42ceddb2018-12-12 10:56:01 -08002122int
2123vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2124{
Florin Coras7a2abce2020-04-05 19:25:44 +00002125 vcl_worker_t *wrk = vcl_worker_get_current ();
2126 vcl_session_t *s;
2127
2128 s = vcl_session_get_w_handle (wrk, session_handle);
2129 if (PREDICT_FALSE (!s))
2130 return VPPCOM_EBADFD;
2131
2132 return vppcom_session_write_inline (wrk, s, buf, n,
2133 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002134}
2135
Florin Corasb0f662f2018-12-27 14:51:46 -08002136int
2137vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2138{
Florin Coras7a2abce2020-04-05 19:25:44 +00002139 vcl_worker_t *wrk = vcl_worker_get_current ();
2140 vcl_session_t *s;
2141
2142 s = vcl_session_get_w_handle (wrk, session_handle);
2143 if (PREDICT_FALSE (!s))
2144 return VPPCOM_EBADFD;
2145
2146 return vppcom_session_write_inline (wrk, s, buf, n,
2147 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002148}
2149
Florin Corasc0737e92019-03-04 14:19:39 -08002150#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002151if (PREDICT_FALSE (!_s->rx_fifo)) \
2152 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002153if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2154 { \
2155 if (!vcl_session_is_ct (_s)) \
2156 { \
2157 svm_fifo_unset_event (_s->rx_fifo); \
2158 if (svm_fifo_is_empty (_s->rx_fifo)) \
2159 break; \
2160 } \
2161 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2162 { \
Florin Coras2f630182020-08-13 00:17:51 -07002163 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002164 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2165 break; \
2166 } \
2167 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002168
Florin Coras86f04502018-09-12 16:08:01 -07002169static void
2170vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2171 unsigned long n_bits, unsigned long *read_map,
2172 unsigned long *write_map,
2173 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002174{
2175 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002176 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07002177 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002178 u32 sid;
2179
2180 switch (e->event_type)
2181 {
Florin Corasc0737e92019-03-04 14:19:39 -08002182 case SESSION_IO_EVT_RX:
2183 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002184 session = vcl_session_get (wrk, sid);
Florin Corasf49cf472020-04-19 23:12:08 +00002185 if (!session || !vcl_session_is_open (session))
Florin Coras86f04502018-09-12 16:08:01 -07002186 break;
Florin Corasfff68f72019-03-13 16:01:38 -07002187 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002188 if (sid < n_bits && read_map)
2189 {
David Johnsond9818dd2018-12-14 14:53:41 -05002190 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002191 *bits_set += 1;
2192 }
2193 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002194 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002195 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002196 session = vcl_session_get (wrk, sid);
Florin Corasf49cf472020-04-19 23:12:08 +00002197 if (!session || !vcl_session_is_open (session))
Florin Coras86f04502018-09-12 16:08:01 -07002198 break;
2199 if (sid < n_bits && write_map)
2200 {
David Johnsond9818dd2018-12-14 14:53:41 -05002201 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002202 *bits_set += 1;
2203 }
2204 break;
Florin Coras86f04502018-09-12 16:08:01 -07002205 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002206 session = vcl_session_accepted (wrk,
2207 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002208 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002209 break;
Florin Coras86f04502018-09-12 16:08:01 -07002210 sid = session->session_index;
2211 if (sid < n_bits && read_map)
2212 {
David Johnsond9818dd2018-12-14 14:53:41 -05002213 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002214 *bits_set += 1;
2215 }
2216 break;
2217 case SESSION_CTRL_EVT_CONNECTED:
2218 connected_msg = (session_connected_msg_t *) e->data;
Florin Coras57c88932019-08-29 12:03:17 -07002219 sid = vcl_session_connected_handler (wrk, connected_msg);
2220 if (sid == VCL_INVALID_SESSION_INDEX)
2221 break;
2222 if (sid < n_bits && write_map)
2223 {
2224 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2225 *bits_set += 1;
2226 }
Florin Coras86f04502018-09-12 16:08:01 -07002227 break;
2228 case SESSION_CTRL_EVT_DISCONNECTED:
2229 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002230 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2231 if (!session)
2232 break;
2233 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002234 if (sid < n_bits && except_map)
2235 {
David Johnsond9818dd2018-12-14 14:53:41 -05002236 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002237 *bits_set += 1;
2238 }
2239 break;
2240 case SESSION_CTRL_EVT_RESET:
2241 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2242 if (sid < n_bits && except_map)
2243 {
David Johnsond9818dd2018-12-14 14:53:41 -05002244 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002245 *bits_set += 1;
2246 }
2247 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002248 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2249 vcl_session_unlisten_reply_handler (wrk, e->data);
2250 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002251 case SESSION_CTRL_EVT_MIGRATED:
2252 vcl_session_migrated_handler (wrk, e->data);
2253 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002254 case SESSION_CTRL_EVT_CLEANUP:
2255 vcl_session_cleanup_handler (wrk, e->data);
2256 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002257 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2258 vcl_session_worker_update_reply_handler (wrk, e->data);
2259 break;
2260 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2261 vcl_session_req_worker_update_handler (wrk, e->data);
2262 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002263 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2264 vcl_session_app_add_segment_handler (wrk, e->data);
2265 break;
2266 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2267 vcl_session_app_del_segment_handler (wrk, e->data);
2268 break;
hanlina3a48962020-07-13 11:09:15 +08002269 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002270 vcl_worker_rpc_handler (wrk, e->data);
2271 break;
Florin Coras86f04502018-09-12 16:08:01 -07002272 default:
2273 clib_warning ("unhandled: %u", e->event_type);
2274 break;
2275 }
2276}
2277
2278static int
2279vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2280 unsigned long n_bits, unsigned long *read_map,
2281 unsigned long *write_map, unsigned long *except_map,
2282 double time_to_wait, u32 * bits_set)
2283{
Florin Coras99368312018-08-02 10:45:44 -07002284 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002285 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002286 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002287
2288 svm_msg_q_lock (mq);
2289 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002290 {
Florin Coras54693d22018-07-17 10:46:29 -07002291 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002292 {
Florin Coras54693d22018-07-17 10:46:29 -07002293 svm_msg_q_unlock (mq);
2294 return 0;
2295 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002296
Florin Coras54693d22018-07-17 10:46:29 -07002297 if (!time_to_wait)
2298 {
2299 svm_msg_q_unlock (mq);
2300 return 0;
2301 }
2302 else if (time_to_wait < 0)
2303 {
2304 svm_msg_q_wait (mq);
2305 }
2306 else
2307 {
2308 if (svm_msg_q_timedwait (mq, time_to_wait))
2309 {
2310 svm_msg_q_unlock (mq);
2311 return 0;
2312 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002313 }
2314 }
Florin Corase003a1b2019-06-05 10:47:16 -07002315 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002316 svm_msg_q_unlock (mq);
2317
Florin Coras134a9962018-08-28 11:32:04 -07002318 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002319 {
Florin Coras134a9962018-08-28 11:32:04 -07002320 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002321 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002322 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2323 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002324 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002325 }
Florin Coras134a9962018-08-28 11:32:04 -07002326 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002327 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002328 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002329}
2330
Florin Coras99368312018-08-02 10:45:44 -07002331static int
Florin Coras294afe22019-01-07 17:49:17 -08002332vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2333 vcl_si_set * read_map, vcl_si_set * write_map,
2334 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002335 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002336{
Florin Coras14ed6df2019-03-06 21:13:42 -08002337 double wait = 0, start = 0;
2338
2339 if (!*bits_set)
2340 {
2341 wait = time_to_wait;
2342 start = clib_time_now (&wrk->clib_time);
2343 }
2344
2345 do
2346 {
2347 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2348 write_map, except_map, wait, bits_set);
2349 if (*bits_set)
2350 return *bits_set;
2351 if (wait == -1)
2352 continue;
2353
2354 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2355 }
2356 while (wait > 0);
2357
2358 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002359}
2360
2361static int
Florin Coras294afe22019-01-07 17:49:17 -08002362vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2363 vcl_si_set * read_map, vcl_si_set * write_map,
2364 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002365 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002366{
2367 vcl_mq_evt_conn_t *mqc;
2368 int __clib_unused n_read;
2369 int n_mq_evts, i;
2370 u64 buf;
2371
Florin Coras134a9962018-08-28 11:32:04 -07002372 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2373 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2374 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002375 for (i = 0; i < n_mq_evts; i++)
2376 {
Florin Coras134a9962018-08-28 11:32:04 -07002377 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002378 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002379 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002380 except_map, 0, bits_set);
2381 }
2382
2383 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2384}
2385
Dave Wallace543852a2017-08-03 02:11:34 -04002386int
Florin Coras294afe22019-01-07 17:49:17 -08002387vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2388 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002389{
Florin Coras54693d22018-07-17 10:46:29 -07002390 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002391 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002392 vcl_session_t *session = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002393 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002394
Dave Wallace7876d392017-10-19 03:53:57 -04002395 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002396 {
Florin Coras134a9962018-08-28 11:32:04 -07002397 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002398 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002399 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2400 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002401 }
Dave Wallace7876d392017-10-19 03:53:57 -04002402 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002403 {
Florin Coras134a9962018-08-28 11:32:04 -07002404 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002405 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002406 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2407 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002408 }
Dave Wallace7876d392017-10-19 03:53:57 -04002409 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002410 {
Florin Coras134a9962018-08-28 11:32:04 -07002411 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002412 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002413 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2414 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002415 }
2416
Florin Coras54693d22018-07-17 10:46:29 -07002417 if (!n_bits)
2418 return 0;
2419
2420 if (!write_map)
2421 goto check_rd;
2422
2423 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002424 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2425 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002426 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002427 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2428 bits_set++;
2429 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002430 }
2431
Florin Coras5e6222a2020-04-24 17:09:25 +00002432 if (vcl_session_write_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002433 {
David Johnsond9818dd2018-12-14 14:53:41 -05002434 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002435 bits_set++;
2436 }
Florin Coras294afe22019-01-07 17:49:17 -08002437 else
Florin Coras2d379d82019-06-28 12:45:12 -07002438 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002439 }));
2440
2441check_rd:
2442 if (!read_map)
2443 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002444
Florin Coras134a9962018-08-28 11:32:04 -07002445 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2446 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002447 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002448 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2449 bits_set++;
2450 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002451 }
2452
Florin Coras5e6222a2020-04-24 17:09:25 +00002453 if (vcl_session_read_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002454 {
David Johnsond9818dd2018-12-14 14:53:41 -05002455 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002456 bits_set++;
2457 }
2458 }));
2459 /* *INDENT-ON* */
2460
2461check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002462
Florin Coras86f04502018-09-12 16:08:01 -07002463 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2464 {
2465 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2466 read_map, write_map, except_map, &bits_set);
2467 }
2468 vec_reset_length (wrk->unhandled_evts_vector);
2469
Florin Coras99368312018-08-02 10:45:44 -07002470 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002471 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002472 time_to_wait, &bits_set);
2473 else
Florin Coras134a9962018-08-28 11:32:04 -07002474 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002475 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002476
Dave Wallace543852a2017-08-03 02:11:34 -04002477 return (bits_set);
2478}
2479
Dave Wallacef7f809c2017-10-03 01:48:42 -04002480static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002481vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002482{
Florin Coras7e12d942018-06-27 14:32:43 -07002483 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002484 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002485 u32 sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002486
Florin Corasc0737e92019-03-04 14:19:39 -08002487 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002488 return;
2489
Florin Coras7e5e62b2019-07-30 14:08:23 -07002490 session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002491 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002492 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002493 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002494 goto done;
2495 }
2496 if (PREDICT_FALSE (!session->is_vep))
2497 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002498 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002499 goto done;
2500 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002501 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002502 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002503 "{\n"
2504 " is_vep = %u\n"
2505 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002506 " next_sh = 0x%x (%u)\n"
2507 "}\n", vep_handle, session->is_vep, session->is_vep_session,
Florin Coras5e062572019-03-14 19:07:51 -07002508 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002509
Florin Coras7e5e62b2019-07-30 14:08:23 -07002510 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002511 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002512 session = vcl_session_get_w_handle (wrk, sh);
Florin Coras070453d2018-08-24 17:04:27 -07002513 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002514 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002515 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002516 goto done;
2517 }
2518 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002519 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002520 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002521 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002522 else if (PREDICT_FALSE (!session->is_vep_session))
2523 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002524 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002525 goto done;
2526 }
2527 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002528 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2529 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
2530 sh, session->vep.vep_sh, vep_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002531 if (session->is_vep_session)
2532 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002533 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002534 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002535 " next_sh = 0x%x (%u)\n"
2536 " prev_sh = 0x%x (%u)\n"
2537 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002538 " ev.events = 0x%x\n"
2539 " ev.data.u64 = 0x%llx\n"
2540 " et_mask = 0x%x\n"
2541 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002542 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002543 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2544 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002545 }
2546 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002547
2548done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002549 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002550}
2551
2552int
2553vppcom_epoll_create (void)
2554{
Florin Coras134a9962018-08-28 11:32:04 -07002555 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002556 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002557
Florin Coras134a9962018-08-28 11:32:04 -07002558 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002559
2560 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002561 vep_session->vep.vep_sh = ~0;
2562 vep_session->vep.next_sh = ~0;
2563 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002564 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002565
Florin Corasa7a1a222018-12-30 17:11:31 -08002566 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2567 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002568
Florin Corasab2f6db2018-08-31 14:31:41 -07002569 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002570}
2571
2572int
Florin Coras134a9962018-08-28 11:32:04 -07002573vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002574 struct epoll_event *event)
2575{
Florin Coras134a9962018-08-28 11:32:04 -07002576 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002577 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002578 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002579 vcl_session_t *s;
2580 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002581
Florin Coras134a9962018-08-28 11:32:04 -07002582 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002583 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002584 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002585 return VPPCOM_EINVAL;
2586 }
2587
Florin Coras134a9962018-08-28 11:32:04 -07002588 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002589 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002590 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002591 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002592 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002593 }
2594 if (PREDICT_FALSE (!vep_session->is_vep))
2595 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002596 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002597 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002598 }
2599
Florin Coras134a9962018-08-28 11:32:04 -07002600 ASSERT (vep_session->vep.vep_sh == ~0);
2601 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002602
Florin Coras17ec5772020-08-12 20:46:29 -07002603 s = vcl_session_get_w_handle (wrk, session_handle);
2604 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002605 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002606 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002607 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002608 }
Florin Coras17ec5772020-08-12 20:46:29 -07002609 if (PREDICT_FALSE (s->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002610 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002611 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002612 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002613 }
2614
2615 switch (op)
2616 {
2617 case EPOLL_CTL_ADD:
2618 if (PREDICT_FALSE (!event))
2619 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002620 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002621 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002622 }
Florin Coras134a9962018-08-28 11:32:04 -07002623 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002624 {
Florin Coras7e12d942018-06-27 14:32:43 -07002625 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002626 next_session = vcl_session_get_w_handle (wrk,
2627 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002628 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002629 {
Florin Coras5e062572019-03-14 19:07:51 -07002630 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002631 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002632 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002633 }
Florin Coras134a9962018-08-28 11:32:04 -07002634 ASSERT (next_session->vep.prev_sh == vep_handle);
2635 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002636 }
Florin Coras17ec5772020-08-12 20:46:29 -07002637 s->vep.next_sh = vep_session->vep.next_sh;
2638 s->vep.prev_sh = vep_handle;
2639 s->vep.vep_sh = vep_handle;
2640 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2641 s->vep.ev = *event;
2642 s->is_vep = 0;
2643 s->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002644 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002645
Florin Coras17ec5772020-08-12 20:46:29 -07002646 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2647 if (txf && (event->events & EPOLLOUT))
2648 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002649
Florin Coras6e3c1f82020-01-15 01:30:46 +00002650 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002651 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002652 {
2653 session_event_t e = { 0 };
2654 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002655 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002656 vec_add1 (wrk->unhandled_evts_vector, e);
2657 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002658 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002659 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002660 {
2661 session_event_t e = { 0 };
2662 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002663 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002664 vec_add1 (wrk->unhandled_evts_vector, e);
2665 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002666 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2667 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002668 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002669 break;
2670
2671 case EPOLL_CTL_MOD:
2672 if (PREDICT_FALSE (!event))
2673 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002674 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675 rv = VPPCOM_EINVAL;
2676 goto done;
2677 }
Florin Coras17ec5772020-08-12 20:46:29 -07002678 else if (PREDICT_FALSE (!s->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002679 {
Florin Coras5e062572019-03-14 19:07:51 -07002680 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002681 rv = VPPCOM_EINVAL;
2682 goto done;
2683 }
Florin Coras17ec5772020-08-12 20:46:29 -07002684 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002685 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002686 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002687 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002688 rv = VPPCOM_EINVAL;
2689 goto done;
2690 }
hanlin475c9d72019-12-26 11:44:28 +08002691
2692 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2693 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002694 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002695 {
2696 session_event_t e = { 0 };
2697 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002698 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002699 vec_add1 (wrk->unhandled_evts_vector, e);
2700 }
Florin Coras17ec5772020-08-12 20:46:29 -07002701 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2702 s->vep.ev = *event;
2703 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2704 if (event->events & EPOLLOUT)
2705 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2706 else
2707 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002708 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2709 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002710 break;
2711
2712 case EPOLL_CTL_DEL:
Florin Coras17ec5772020-08-12 20:46:29 -07002713 if (PREDICT_FALSE (!s->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002714 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002715 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002716 rv = VPPCOM_EINVAL;
2717 goto done;
2718 }
Florin Coras17ec5772020-08-12 20:46:29 -07002719 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002720 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002721 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002722 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002723 rv = VPPCOM_EINVAL;
2724 goto done;
2725 }
2726
Florin Coras17ec5772020-08-12 20:46:29 -07002727 if (s->vep.prev_sh == vep_handle)
2728 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002729 else
2730 {
Florin Coras7e12d942018-06-27 14:32:43 -07002731 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002732 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002733 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002734 {
Florin Coras5e062572019-03-14 19:07:51 -07002735 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002736 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002737 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002738 }
Florin Coras134a9962018-08-28 11:32:04 -07002739 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002740 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002741 }
Florin Coras17ec5772020-08-12 20:46:29 -07002742 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743 {
Florin Coras7e12d942018-06-27 14:32:43 -07002744 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002745 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002746 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002747 {
Florin Coras5e062572019-03-14 19:07:51 -07002748 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002749 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002750 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751 }
Florin Coras134a9962018-08-28 11:32:04 -07002752 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002753 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002754 }
2755
Florin Coras17ec5772020-08-12 20:46:29 -07002756 memset (&s->vep, 0, sizeof (s->vep));
2757 s->vep.next_sh = ~0;
2758 s->vep.prev_sh = ~0;
2759 s->vep.vep_sh = ~0;
2760 s->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002761
Florin Coras17ec5772020-08-12 20:46:29 -07002762 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2763 if (txf)
2764 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002765
Florin Coras5e062572019-03-14 19:07:51 -07002766 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002767 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002768 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 break;
2770
2771 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002772 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002773 rv = VPPCOM_EINVAL;
2774 }
2775
Florin Coras134a9962018-08-28 11:32:04 -07002776 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002777
2778done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002779 return rv;
2780}
2781
Florin Coras86f04502018-09-12 16:08:01 -07002782static inline void
2783vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2784 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002785{
2786 session_disconnected_msg_t *disconnected_msg;
2787 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002788 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002789 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002790 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002791 u8 add_event = 0;
2792
2793 switch (e->event_type)
2794 {
Florin Coras653e43f2019-03-04 10:56:23 -08002795 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002796 sid = e->session_index;
Florin Coras080aa502020-05-26 00:47:52 +00002797 session = vcl_session_get (wrk, sid);
2798 if (vcl_session_is_closed (session))
Florin Corasfa915f82018-12-26 16:29:06 -08002799 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002800 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002801 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002802 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002803 break;
2804 add_event = 1;
2805 events[*num_ev].events |= EPOLLIN;
2806 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002807 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002808 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002809 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002810 sid = e->session_index;
Florin Coras080aa502020-05-26 00:47:52 +00002811 session = vcl_session_get (wrk, sid);
2812 if (vcl_session_is_closed (session))
Florin Corasfa915f82018-12-26 16:29:06 -08002813 break;
Florin Coras86f04502018-09-12 16:08:01 -07002814 session_events = session->vep.ev.events;
2815 if (!(EPOLLOUT & session_events))
2816 break;
2817 add_event = 1;
2818 events[*num_ev].events |= EPOLLOUT;
2819 session_evt_data = session->vep.ev.data.u64;
Florin Coras17ec5772020-08-12 20:46:29 -07002820 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (session) ?
2821 session->ct_tx_fifo : session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002822 break;
Florin Coras86f04502018-09-12 16:08:01 -07002823 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002824 session = vcl_session_accepted (wrk,
2825 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002826 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002827 break;
Florin Coras86f04502018-09-12 16:08:01 -07002828
Florin Coras86f04502018-09-12 16:08:01 -07002829 session_events = session->vep.ev.events;
2830 if (!(EPOLLIN & session_events))
2831 break;
2832
2833 add_event = 1;
2834 events[*num_ev].events |= EPOLLIN;
2835 session_evt_data = session->vep.ev.data.u64;
2836 break;
2837 case SESSION_CTRL_EVT_CONNECTED:
2838 connected_msg = (session_connected_msg_t *) e->data;
Florin Corasf1653e62019-11-06 15:41:37 -08002839 sid = vcl_session_connected_handler (wrk, connected_msg);
Florin Coras86f04502018-09-12 16:08:01 -07002840 /* Generate EPOLLOUT because there's no connected event */
Florin Coras080aa502020-05-26 00:47:52 +00002841 session = vcl_session_get (wrk, sid);
2842 if (vcl_session_is_closed (session))
Florin Corasfa915f82018-12-26 16:29:06 -08002843 break;
Florin Coras86f04502018-09-12 16:08:01 -07002844 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002845 if (!(EPOLLOUT & session_events))
2846 break;
2847 add_event = 1;
2848 events[*num_ev].events |= EPOLLOUT;
2849 session_evt_data = session->vep.ev.data.u64;
Florin Corasc127d5a2020-10-14 16:35:58 -07002850 if (session->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002851 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002852 break;
2853 case SESSION_CTRL_EVT_DISCONNECTED:
2854 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002855 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Coras080aa502020-05-26 00:47:52 +00002856 if (vcl_session_is_closed (session))
Florin Coras86f04502018-09-12 16:08:01 -07002857 break;
Florin Coras72f77822019-01-22 19:05:52 -08002858 session_events = session->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002859 add_event = 1;
2860 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2861 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002862 break;
2863 case SESSION_CTRL_EVT_RESET:
2864 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras080aa502020-05-26 00:47:52 +00002865 session = vcl_session_get (wrk, sid);
2866 if (vcl_session_is_closed (session))
Florin Coras86f04502018-09-12 16:08:01 -07002867 break;
Florin Coras72f77822019-01-22 19:05:52 -08002868 session_events = session->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002869 add_event = 1;
2870 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2871 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002872 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002873 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2874 vcl_session_unlisten_reply_handler (wrk, e->data);
2875 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002876 case SESSION_CTRL_EVT_MIGRATED:
2877 vcl_session_migrated_handler (wrk, e->data);
2878 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002879 case SESSION_CTRL_EVT_CLEANUP:
2880 vcl_session_cleanup_handler (wrk, e->data);
2881 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002882 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2883 vcl_session_req_worker_update_handler (wrk, e->data);
2884 break;
2885 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2886 vcl_session_worker_update_reply_handler (wrk, e->data);
2887 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002888 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2889 vcl_session_app_add_segment_handler (wrk, e->data);
2890 break;
2891 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2892 vcl_session_app_del_segment_handler (wrk, e->data);
2893 break;
hanlina3a48962020-07-13 11:09:15 +08002894 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002895 vcl_worker_rpc_handler (wrk, e->data);
2896 break;
Florin Coras86f04502018-09-12 16:08:01 -07002897 default:
2898 VDBG (0, "unhandled: %u", e->event_type);
2899 break;
2900 }
2901
2902 if (add_event)
2903 {
2904 events[*num_ev].data.u64 = session_evt_data;
2905 if (EPOLLONESHOT & session_events)
2906 {
2907 session = vcl_session_get (wrk, sid);
2908 session->vep.ev.events = 0;
2909 }
2910 *num_ev += 1;
2911 }
2912}
2913
2914static int
2915vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2916 struct epoll_event *events, u32 maxevents,
2917 double wait_for_time, u32 * num_ev)
2918{
Florin Coras99368312018-08-02 10:45:44 -07002919 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002920 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002921 int i;
2922
Florin Coras539663c2018-09-28 14:59:37 -07002923 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2924 goto handle_dequeued;
2925
Florin Coras54693d22018-07-17 10:46:29 -07002926 svm_msg_q_lock (mq);
2927 if (svm_msg_q_is_empty (mq))
2928 {
2929 if (!wait_for_time)
2930 {
2931 svm_msg_q_unlock (mq);
2932 return 0;
2933 }
2934 else if (wait_for_time < 0)
2935 {
2936 svm_msg_q_wait (mq);
2937 }
2938 else
2939 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002940 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002941 {
2942 svm_msg_q_unlock (mq);
2943 return 0;
2944 }
2945 }
2946 }
Florin Corase003a1b2019-06-05 10:47:16 -07002947 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002948 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002949 svm_msg_q_unlock (mq);
2950
Florin Coras539663c2018-09-28 14:59:37 -07002951handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002952 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002953 {
Florin Coras134a9962018-08-28 11:32:04 -07002954 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002955 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002956 if (*num_ev < maxevents)
2957 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2958 else
2959 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07002960 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002961 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002962 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002963 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002964 return *num_ev;
2965}
2966
Florin Coras99368312018-08-02 10:45:44 -07002967static int
Florin Coras134a9962018-08-28 11:32:04 -07002968vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002969 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002970{
Florin Corase003a1b2019-06-05 10:47:16 -07002971 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002972
2973 if (!n_evts)
2974 {
2975 wait = wait_for_time;
2976 start = clib_time_now (&wrk->clib_time);
2977 }
2978
2979 do
2980 {
2981 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2982 wait, &n_evts);
2983 if (n_evts)
2984 return n_evts;
2985 if (wait == -1)
2986 continue;
2987
Florin Corase003a1b2019-06-05 10:47:16 -07002988 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07002989 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07002990 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002991 }
2992 while (wait > 0);
2993
2994 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002995}
2996
2997static int
Florin Coras134a9962018-08-28 11:32:04 -07002998vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002999 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003000{
3001 vcl_mq_evt_conn_t *mqc;
3002 int __clib_unused n_read;
3003 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003004 u64 buf;
3005
Florin Coras134a9962018-08-28 11:32:04 -07003006 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07003007again:
Florin Coras134a9962018-08-28 11:32:04 -07003008 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3009 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07003010 for (i = 0; i < n_mq_evts; i++)
3011 {
Florin Coras134a9962018-08-28 11:32:04 -07003012 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07003013 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07003014 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07003015 }
Florin Corasa4878ed2018-10-12 13:09:36 -07003016 if (!n_evts && n_mq_evts > 0)
3017 goto again;
Florin Coras99368312018-08-02 10:45:44 -07003018
3019 return (int) n_evts;
3020}
3021
Dave Wallacef7f809c2017-10-03 01:48:42 -04003022int
Florin Coras134a9962018-08-28 11:32:04 -07003023vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003024 int maxevents, double wait_for_time)
3025{
Florin Coras134a9962018-08-28 11:32:04 -07003026 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003027 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003028 u32 n_evts = 0;
3029 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003030
3031 if (PREDICT_FALSE (maxevents <= 0))
3032 {
Florin Coras5e062572019-03-14 19:07:51 -07003033 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003034 return VPPCOM_EINVAL;
3035 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003036
Florin Coras134a9962018-08-28 11:32:04 -07003037 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003038 if (!vep_session)
3039 return VPPCOM_EBADFD;
3040
Florin Coras54693d22018-07-17 10:46:29 -07003041 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003042 {
Florin Coras5e062572019-03-14 19:07:51 -07003043 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003044 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003045 }
Florin Coras54693d22018-07-17 10:46:29 -07003046
3047 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003048
Florin Coras86f04502018-09-12 16:08:01 -07003049 if (vec_len (wrk->unhandled_evts_vector))
3050 {
3051 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3052 {
3053 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3054 events, &n_evts);
3055 if (n_evts == maxevents)
3056 {
Florin Corase003a1b2019-06-05 10:47:16 -07003057 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3058 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003059 }
3060 }
Florin Corase003a1b2019-06-05 10:47:16 -07003061 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003062 }
3063
3064 if (vcm->cfg.use_mq_eventfd)
3065 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3066 wait_for_time);
3067
3068 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3069 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003070}
3071
Dave Wallace35830af2017-10-09 01:43:42 -04003072int
Florin Coras134a9962018-08-28 11:32:04 -07003073vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003074 void *buffer, uint32_t * buflen)
3075{
Florin Coras134a9962018-08-28 11:32:04 -07003076 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003077 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04003078 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08003079 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003080 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003081
Florin Coras134a9962018-08-28 11:32:04 -07003082 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003083 if (!session)
3084 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003085
Dave Wallace35830af2017-10-09 01:43:42 -04003086 switch (op)
3087 {
3088 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003089 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003090 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3091 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003092 break;
3093
Dave Wallace227867f2017-11-13 21:21:53 -05003094 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003095 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003096 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3097 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003098 break;
3099
3100 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003101 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003102 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003103 *flags =
3104 O_RDWR |
3105 (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK) ?
3106 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003107 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003108 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3109 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07003110 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003111 }
3112 else
3113 rv = VPPCOM_EINVAL;
3114 break;
3115
3116 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003117 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003118 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003119 if (*flags & O_NONBLOCK)
3120 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
3121 else
3122 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
3123
Florin Coras5e062572019-03-14 19:07:51 -07003124 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3125 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07003126 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003127 }
3128 else
3129 rv = VPPCOM_EINVAL;
3130 break;
3131
3132 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003133 if (PREDICT_TRUE (buffer && buflen &&
3134 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003135 {
Florin Coras7e12d942018-06-27 14:32:43 -07003136 ep->is_ip4 = session->transport.is_ip4;
3137 ep->port = session->transport.rmt_port;
3138 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003139 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3140 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003141 else
Dave Barach178cf492018-11-13 16:34:13 -05003142 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3143 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003144 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003145 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3146 "addr = %U, port %u", session_handle, ep->is_ip4,
3147 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003148 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3149 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003150 }
3151 else
3152 rv = VPPCOM_EINVAL;
3153 break;
3154
3155 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003156 if (PREDICT_TRUE (buffer && buflen &&
3157 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003158 {
Florin Coras7e12d942018-06-27 14:32:43 -07003159 ep->is_ip4 = session->transport.is_ip4;
3160 ep->port = session->transport.lcl_port;
3161 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003162 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3163 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003164 else
Dave Barach178cf492018-11-13 16:34:13 -05003165 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3166 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003167 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003168 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3169 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003170 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003171 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3172 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003173 }
3174 else
3175 rv = VPPCOM_EINVAL;
3176 break;
Stevenb5a11602017-10-11 09:59:30 -07003177
Florin Corasef7cbf62019-10-17 09:56:27 -07003178 case VPPCOM_ATTR_SET_LCL_ADDR:
3179 if (PREDICT_TRUE (buffer && buflen &&
3180 (*buflen >= sizeof (*ep)) && ep->ip))
3181 {
3182 session->transport.is_ip4 = ep->is_ip4;
3183 session->transport.lcl_port = ep->port;
3184 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3185 *buflen = sizeof (*ep);
3186 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3187 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3188 &session->transport.lcl_ip,
3189 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3190 clib_net_to_host_u16 (ep->port));
3191 }
3192 else
3193 rv = VPPCOM_EINVAL;
3194 break;
3195
Dave Wallace048b1d62018-01-03 22:24:41 -05003196 case VPPCOM_ATTR_GET_LIBC_EPFD:
3197 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003198 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003199 break;
3200
3201 case VPPCOM_ATTR_SET_LIBC_EPFD:
3202 if (PREDICT_TRUE (buffer && buflen &&
3203 (*buflen == sizeof (session->libc_epfd))))
3204 {
3205 session->libc_epfd = *(int *) buffer;
3206 *buflen = sizeof (session->libc_epfd);
3207
Florin Coras5e062572019-03-14 19:07:51 -07003208 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3209 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003210 }
3211 else
3212 rv = VPPCOM_EINVAL;
3213 break;
3214
3215 case VPPCOM_ATTR_GET_PROTOCOL:
3216 if (buffer && buflen && (*buflen >= sizeof (int)))
3217 {
Florin Coras7e12d942018-06-27 14:32:43 -07003218 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003219 *buflen = sizeof (int);
3220
Florin Coras5e062572019-03-14 19:07:51 -07003221 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3222 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003223 }
3224 else
3225 rv = VPPCOM_EINVAL;
3226 break;
3227
3228 case VPPCOM_ATTR_GET_LISTEN:
3229 if (buffer && buflen && (*buflen >= sizeof (int)))
3230 {
3231 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3232 VCL_SESS_ATTR_LISTEN);
3233 *buflen = sizeof (int);
3234
Florin Coras5e062572019-03-14 19:07:51 -07003235 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3236 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003237 }
3238 else
3239 rv = VPPCOM_EINVAL;
3240 break;
3241
3242 case VPPCOM_ATTR_GET_ERROR:
3243 if (buffer && buflen && (*buflen >= sizeof (int)))
3244 {
3245 *(int *) buffer = 0;
3246 *buflen = sizeof (int);
3247
Florin Coras5e062572019-03-14 19:07:51 -07003248 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3249 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003250 }
3251 else
3252 rv = VPPCOM_EINVAL;
3253 break;
3254
3255 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3256 if (buffer && buflen && (*buflen >= sizeof (u32)))
3257 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003258
3259 /* VPP-TBD */
3260 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003261 session->tx_fifo ?
3262 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003263 vcm->cfg.tx_fifo_size);
3264 *buflen = sizeof (u32);
3265
Florin Coras5e062572019-03-14 19:07:51 -07003266 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3267 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3268 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003269 }
3270 else
3271 rv = VPPCOM_EINVAL;
3272 break;
3273
3274 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3275 if (buffer && buflen && (*buflen == sizeof (u32)))
3276 {
3277 /* VPP-TBD */
3278 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003279 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3280 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3281 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003282 }
3283 else
3284 rv = VPPCOM_EINVAL;
3285 break;
3286
3287 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3288 if (buffer && buflen && (*buflen >= sizeof (u32)))
3289 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003290
3291 /* VPP-TBD */
3292 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003293 session->rx_fifo ?
3294 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003295 vcm->cfg.rx_fifo_size);
3296 *buflen = sizeof (u32);
3297
Florin Coras5e062572019-03-14 19:07:51 -07003298 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3299 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003300 }
3301 else
3302 rv = VPPCOM_EINVAL;
3303 break;
3304
3305 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3306 if (buffer && buflen && (*buflen == sizeof (u32)))
3307 {
3308 /* VPP-TBD */
3309 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003310 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3311 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3312 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003313 }
3314 else
3315 rv = VPPCOM_EINVAL;
3316 break;
3317
3318 case VPPCOM_ATTR_GET_REUSEADDR:
3319 if (buffer && buflen && (*buflen >= sizeof (int)))
3320 {
3321 /* VPP-TBD */
3322 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3323 VCL_SESS_ATTR_REUSEADDR);
3324 *buflen = sizeof (int);
3325
Florin Coras5e062572019-03-14 19:07:51 -07003326 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3327 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003328 }
3329 else
3330 rv = VPPCOM_EINVAL;
3331 break;
3332
Stevenb5a11602017-10-11 09:59:30 -07003333 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003334 if (buffer && buflen && (*buflen == sizeof (int)) &&
3335 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3336 {
3337 /* VPP-TBD */
3338 if (*(int *) buffer)
3339 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3340 else
3341 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3342
Florin Coras5e062572019-03-14 19:07:51 -07003343 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3344 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
3345 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003346 }
3347 else
3348 rv = VPPCOM_EINVAL;
3349 break;
3350
3351 case VPPCOM_ATTR_GET_REUSEPORT:
3352 if (buffer && buflen && (*buflen >= sizeof (int)))
3353 {
3354 /* VPP-TBD */
3355 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3356 VCL_SESS_ATTR_REUSEPORT);
3357 *buflen = sizeof (int);
3358
Florin Coras5e062572019-03-14 19:07:51 -07003359 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3360 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003361 }
3362 else
3363 rv = VPPCOM_EINVAL;
3364 break;
3365
3366 case VPPCOM_ATTR_SET_REUSEPORT:
3367 if (buffer && buflen && (*buflen == sizeof (int)) &&
3368 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3369 {
3370 /* VPP-TBD */
3371 if (*(int *) buffer)
3372 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3373 else
3374 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3375
Florin Coras5e062572019-03-14 19:07:51 -07003376 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3377 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3378 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003379 }
3380 else
3381 rv = VPPCOM_EINVAL;
3382 break;
3383
3384 case VPPCOM_ATTR_GET_BROADCAST:
3385 if (buffer && buflen && (*buflen >= sizeof (int)))
3386 {
3387 /* VPP-TBD */
3388 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3389 VCL_SESS_ATTR_BROADCAST);
3390 *buflen = sizeof (int);
3391
Florin Coras5e062572019-03-14 19:07:51 -07003392 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3393 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003394 }
3395 else
3396 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003397 break;
3398
3399 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003400 if (buffer && buflen && (*buflen == sizeof (int)))
3401 {
3402 /* VPP-TBD */
3403 if (*(int *) buffer)
3404 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3405 else
3406 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3407
Florin Coras5e062572019-03-14 19:07:51 -07003408 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3409 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3410 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 }
3412 else
3413 rv = VPPCOM_EINVAL;
3414 break;
3415
3416 case VPPCOM_ATTR_GET_V6ONLY:
3417 if (buffer && buflen && (*buflen >= sizeof (int)))
3418 {
3419 /* VPP-TBD */
3420 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3421 VCL_SESS_ATTR_V6ONLY);
3422 *buflen = sizeof (int);
3423
Florin Coras5e062572019-03-14 19:07:51 -07003424 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3425 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003426 }
3427 else
3428 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003429 break;
3430
3431 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003432 if (buffer && buflen && (*buflen == sizeof (int)))
3433 {
3434 /* VPP-TBD */
3435 if (*(int *) buffer)
3436 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3437 else
3438 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3439
Florin Coras5e062572019-03-14 19:07:51 -07003440 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3441 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3442 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003443 }
3444 else
3445 rv = VPPCOM_EINVAL;
3446 break;
3447
3448 case VPPCOM_ATTR_GET_KEEPALIVE:
3449 if (buffer && buflen && (*buflen >= sizeof (int)))
3450 {
3451 /* VPP-TBD */
3452 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3453 VCL_SESS_ATTR_KEEPALIVE);
3454 *buflen = sizeof (int);
3455
Florin Coras5e062572019-03-14 19:07:51 -07003456 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3457 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003458 }
3459 else
3460 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003461 break;
Stevenbccd3392017-10-12 20:42:21 -07003462
3463 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003464 if (buffer && buflen && (*buflen == sizeof (int)))
3465 {
3466 /* VPP-TBD */
3467 if (*(int *) buffer)
3468 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3469 else
3470 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3471
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3473 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3474 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 }
3476 else
3477 rv = VPPCOM_EINVAL;
3478 break;
3479
3480 case VPPCOM_ATTR_GET_TCP_NODELAY:
3481 if (buffer && buflen && (*buflen >= sizeof (int)))
3482 {
3483 /* VPP-TBD */
3484 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3485 VCL_SESS_ATTR_TCP_NODELAY);
3486 *buflen = sizeof (int);
3487
Florin Coras5e062572019-03-14 19:07:51 -07003488 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3489 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 }
3491 else
3492 rv = VPPCOM_EINVAL;
3493 break;
3494
3495 case VPPCOM_ATTR_SET_TCP_NODELAY:
3496 if (buffer && buflen && (*buflen == sizeof (int)))
3497 {
3498 /* VPP-TBD */
3499 if (*(int *) buffer)
3500 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3501 else
3502 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3503
Florin Coras5e062572019-03-14 19:07:51 -07003504 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3505 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3506 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 }
3508 else
3509 rv = VPPCOM_EINVAL;
3510 break;
3511
3512 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3513 if (buffer && buflen && (*buflen >= sizeof (int)))
3514 {
3515 /* VPP-TBD */
3516 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3517 VCL_SESS_ATTR_TCP_KEEPIDLE);
3518 *buflen = sizeof (int);
3519
Florin Coras5e062572019-03-14 19:07:51 -07003520 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3521 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003522 }
3523 else
3524 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003525 break;
3526
3527 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 if (buffer && buflen && (*buflen == sizeof (int)))
3529 {
3530 /* VPP-TBD */
3531 if (*(int *) buffer)
3532 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3533 else
3534 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3535
Florin Coras5e062572019-03-14 19:07:51 -07003536 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003537 VCL_SESS_ATTR_TEST (session->attr,
3538 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 }
3540 else
3541 rv = VPPCOM_EINVAL;
3542 break;
3543
3544 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3545 if (buffer && buflen && (*buflen >= sizeof (int)))
3546 {
3547 /* VPP-TBD */
3548 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3549 VCL_SESS_ATTR_TCP_KEEPINTVL);
3550 *buflen = sizeof (int);
3551
Florin Coras5e062572019-03-14 19:07:51 -07003552 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3553 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003554 }
3555 else
3556 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003557 break;
3558
3559 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003560 if (buffer && buflen && (*buflen == sizeof (int)))
3561 {
3562 /* VPP-TBD */
3563 if (*(int *) buffer)
3564 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3565 else
3566 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3567
Florin Coras5e062572019-03-14 19:07:51 -07003568 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003569 VCL_SESS_ATTR_TEST (session->attr,
3570 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003571 }
3572 else
3573 rv = VPPCOM_EINVAL;
3574 break;
3575
3576 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3577 if (buffer && buflen && (*buflen >= sizeof (u32)))
3578 {
3579 /* VPP-TBD */
3580 *(u32 *) buffer = session->user_mss;
3581 *buflen = sizeof (int);
3582
Florin Coras5e062572019-03-14 19:07:51 -07003583 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3584 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003585 }
3586 else
3587 rv = VPPCOM_EINVAL;
3588 break;
3589
3590 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3591 if (buffer && buflen && (*buflen == sizeof (u32)))
3592 {
3593 /* VPP-TBD */
3594 session->user_mss = *(u32 *) buffer;
3595
Florin Coras5e062572019-03-14 19:07:51 -07003596 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3597 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 }
3599 else
3600 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003601 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003602
Florin Coras7baeb712019-01-04 17:05:43 -08003603 case VPPCOM_ATTR_SET_SHUT:
3604 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3605 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3606 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3607 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3608 break;
3609
3610 case VPPCOM_ATTR_GET_SHUT:
3611 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3612 tmp_flags = 1;
3613 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3614 tmp_flags |= 2;
3615 if (tmp_flags == 1)
3616 *(int *) buffer = SHUT_RD;
3617 else if (tmp_flags == 2)
3618 *(int *) buffer = SHUT_WR;
3619 else if (tmp_flags == 3)
3620 *(int *) buffer = SHUT_RDWR;
3621 *buflen = sizeof (int);
3622 break;
Florin Coras1e966172020-05-16 18:18:14 +00003623
3624 case VPPCOM_ATTR_SET_CONNECTED:
3625 session->flags |= VCL_SESSION_F_CONNECTED;
3626 break;
3627
Dave Wallacee22aa742017-10-20 12:30:38 -04003628 default:
3629 rv = VPPCOM_EINVAL;
3630 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003631 }
3632
Dave Wallace35830af2017-10-09 01:43:42 -04003633 return rv;
3634}
3635
Stevenac1f96d2017-10-24 16:03:58 -07003636int
Florin Coras134a9962018-08-28 11:32:04 -07003637vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003638 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3639{
Florin Coras134a9962018-08-28 11:32:04 -07003640 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003641 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003642 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003643
3644 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003645 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003646 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003647 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003648 else
3649 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003650 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003651 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003652 }
3653
Florin Coras0a1e1832020-03-29 18:54:04 +00003654 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003655 {
Florin Coras5da10c42020-04-01 04:31:21 +00003656 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003657 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003658 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3659 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003660 else
Dave Barach178cf492018-11-13 16:34:13 -05003661 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3662 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003663 ep->is_ip4 = session->transport.is_ip4;
3664 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003665 }
Florin Coras460dce62018-07-27 05:45:06 -07003666
Stevenac1f96d2017-10-24 16:03:58 -07003667 return rv;
3668}
3669
3670int
Florin Coras134a9962018-08-28 11:32:04 -07003671vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003672 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3673{
Florin Coras7a2abce2020-04-05 19:25:44 +00003674 vcl_worker_t *wrk = vcl_worker_get_current ();
3675 vcl_session_t *s;
3676
3677 s = vcl_session_get_w_handle (wrk, session_handle);
3678 if (!s)
3679 return VPPCOM_EBADFD;
3680
Dave Wallace617dffa2017-10-26 14:47:06 -04003681 if (!buffer)
3682 return VPPCOM_EINVAL;
3683
3684 if (ep)
3685 {
Florin Coras0a1e1832020-03-29 18:54:04 +00003686 if (s->session_type != VPPCOM_PROTO_UDP
3687 || (s->flags & VCL_SESSION_F_CONNECTED))
Florin Coras5da10c42020-04-01 04:31:21 +00003688 return VPPCOM_EINVAL;
3689
3690 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003691 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003692 {
3693 vcl_send_session_connect (wrk, s);
3694 }
3695 else
3696 {
3697 s->transport.is_ip4 = ep->is_ip4;
3698 s->transport.rmt_port = ep->port;
3699 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
3700 }
Dave Wallace617dffa2017-10-26 14:47:06 -04003701 }
3702
3703 if (flags)
3704 {
3705 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003706 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003707 }
3708
Florin Coras7a2abce2020-04-05 19:25:44 +00003709 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3710 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003711}
3712
Dave Wallace048b1d62018-01-03 22:24:41 -05003713int
3714vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3715{
Florin Coras134a9962018-08-28 11:32:04 -07003716 vcl_worker_t *wrk = vcl_worker_get_current ();
3717 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003718 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003719 svm_msg_q_msg_t msg;
3720 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003721 int rv, num_ev = 0;
3722
Florin Coras5e062572019-03-14 19:07:51 -07003723 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003724
3725 if (!vp)
3726 return VPPCOM_EFAULT;
3727
3728 do
3729 {
Florin Coras7e12d942018-06-27 14:32:43 -07003730 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003731
Florin Coras6917b942018-11-13 22:44:54 -08003732 /* Dequeue all events and drop all unhandled io events */
3733 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3734 {
3735 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3736 vcl_handle_mq_event (wrk, e);
3737 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3738 }
3739 vec_reset_length (wrk->unhandled_evts_vector);
3740
Dave Wallace048b1d62018-01-03 22:24:41 -05003741 for (i = 0; i < n_sids; i++)
3742 {
Florin Coras7baeb712019-01-04 17:05:43 -08003743 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003744 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003745 {
3746 vp[i].revents = POLLHUP;
3747 num_ev++;
3748 continue;
3749 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003750
Florin Coras6917b942018-11-13 22:44:54 -08003751 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003752
3753 if (POLLIN & vp[i].events)
3754 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003755 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003756 if (rv > 0)
3757 {
Florin Coras6917b942018-11-13 22:44:54 -08003758 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003759 num_ev++;
3760 }
3761 else if (rv < 0)
3762 {
3763 switch (rv)
3764 {
3765 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003766 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003767 break;
3768
3769 default:
Florin Coras6917b942018-11-13 22:44:54 -08003770 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003771 break;
3772 }
3773 num_ev++;
3774 }
3775 }
3776
3777 if (POLLOUT & vp[i].events)
3778 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003779 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003780 if (rv > 0)
3781 {
Florin Coras6917b942018-11-13 22:44:54 -08003782 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003783 num_ev++;
3784 }
3785 else if (rv < 0)
3786 {
3787 switch (rv)
3788 {
3789 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003790 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003791 break;
3792
3793 default:
Florin Coras6917b942018-11-13 22:44:54 -08003794 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003795 break;
3796 }
3797 num_ev++;
3798 }
3799 }
3800
Dave Wallace7e607a72018-06-18 18:41:32 -04003801 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003802 {
Florin Coras6917b942018-11-13 22:44:54 -08003803 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003804 num_ev++;
3805 }
3806 }
3807 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003808 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003809 }
3810 while ((num_ev == 0) && keep_trying);
3811
Dave Wallace048b1d62018-01-03 22:24:41 -05003812 return num_ev;
3813}
3814
Florin Coras99368312018-08-02 10:45:44 -07003815int
3816vppcom_mq_epoll_fd (void)
3817{
Florin Coras134a9962018-08-28 11:32:04 -07003818 vcl_worker_t *wrk = vcl_worker_get_current ();
3819 return wrk->mqs_epfd;
3820}
3821
3822int
Florin Coras30e79c22019-01-02 19:31:22 -08003823vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003824{
3825 return session_handle & 0xFFFFFF;
3826}
3827
3828int
Florin Coras30e79c22019-01-02 19:31:22 -08003829vppcom_session_worker (vcl_session_handle_t session_handle)
3830{
3831 return session_handle >> 24;
3832}
3833
3834int
Florin Coras134a9962018-08-28 11:32:04 -07003835vppcom_worker_register (void)
3836{
Florin Coras47c40e22018-11-26 17:01:36 -08003837 if (!vcl_worker_alloc_and_init ())
3838 return VPPCOM_EEXIST;
3839
Florin Coras47c40e22018-11-26 17:01:36 -08003840 if (vcl_worker_register_with_vpp ())
3841 return VPPCOM_EEXIST;
3842
3843 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003844}
3845
Florin Coras369db832019-07-08 12:34:45 -07003846void
3847vppcom_worker_unregister (void)
3848{
3849 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3850 vcl_set_worker_index (~0);
3851}
3852
Pivo6017ff02020-05-04 17:57:33 +02003853void
3854vppcom_worker_index_set (int index)
3855{
3856 vcl_set_worker_index (index);
3857}
3858
Florin Corasdfe4cf42018-11-28 22:13:45 -08003859int
3860vppcom_worker_index (void)
3861{
3862 return vcl_get_worker_index ();
3863}
3864
Florin Corase0982e52019-01-25 13:19:56 -08003865int
3866vppcom_worker_mqs_epfd (void)
3867{
3868 vcl_worker_t *wrk = vcl_worker_get_current ();
3869 if (!vcm->cfg.use_mq_eventfd)
3870 return -1;
3871 return wrk->mqs_epfd;
3872}
3873
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003874int
3875vppcom_session_is_connectable_listener (uint32_t session_handle)
3876{
3877 vcl_session_t *session;
3878 vcl_worker_t *wrk = vcl_worker_get_current ();
3879 session = vcl_session_get_w_handle (wrk, session_handle);
3880 if (!session)
3881 return VPPCOM_EBADFD;
3882 return vcl_session_is_connectable_listener (wrk, session);
3883}
3884
3885int
3886vppcom_session_listener (uint32_t session_handle)
3887{
3888 vcl_worker_t *wrk = vcl_worker_get_current ();
3889 vcl_session_t *listen_session, *session;
3890 session = vcl_session_get_w_handle (wrk, session_handle);
3891 if (!session)
3892 return VPPCOM_EBADFD;
3893 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3894 return VPPCOM_EBADFD;
3895 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3896 if (!listen_session)
3897 return VPPCOM_EBADFD;
3898 return vcl_session_handle (listen_session);
3899}
3900
3901int
3902vppcom_session_n_accepted (uint32_t session_handle)
3903{
3904 vcl_worker_t *wrk = vcl_worker_get_current ();
3905 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3906 if (!session)
3907 return VPPCOM_EBADFD;
3908 return session->n_accepted_sessions;
3909}
3910
Florin Coras66ec4672020-06-15 07:59:40 -07003911const char *
3912vppcom_proto_str (vppcom_proto_t proto)
3913{
3914 char const *proto_str;
3915
3916 switch (proto)
3917 {
3918 case VPPCOM_PROTO_TCP:
3919 proto_str = "TCP";
3920 break;
3921 case VPPCOM_PROTO_UDP:
3922 proto_str = "UDP";
3923 break;
3924 case VPPCOM_PROTO_TLS:
3925 proto_str = "TLS";
3926 break;
3927 case VPPCOM_PROTO_QUIC:
3928 proto_str = "QUIC";
3929 break;
3930 default:
3931 proto_str = "UNKNOWN";
3932 break;
3933 }
3934 return proto_str;
3935}
3936
3937const char *
3938vppcom_retval_str (int retval)
3939{
3940 char const *st;
3941
3942 switch (retval)
3943 {
3944 case VPPCOM_OK:
3945 st = "VPPCOM_OK";
3946 break;
3947
3948 case VPPCOM_EAGAIN:
3949 st = "VPPCOM_EAGAIN";
3950 break;
3951
3952 case VPPCOM_EFAULT:
3953 st = "VPPCOM_EFAULT";
3954 break;
3955
3956 case VPPCOM_ENOMEM:
3957 st = "VPPCOM_ENOMEM";
3958 break;
3959
3960 case VPPCOM_EINVAL:
3961 st = "VPPCOM_EINVAL";
3962 break;
3963
3964 case VPPCOM_EBADFD:
3965 st = "VPPCOM_EBADFD";
3966 break;
3967
3968 case VPPCOM_EAFNOSUPPORT:
3969 st = "VPPCOM_EAFNOSUPPORT";
3970 break;
3971
3972 case VPPCOM_ECONNABORTED:
3973 st = "VPPCOM_ECONNABORTED";
3974 break;
3975
3976 case VPPCOM_ECONNRESET:
3977 st = "VPPCOM_ECONNRESET";
3978 break;
3979
3980 case VPPCOM_ENOTCONN:
3981 st = "VPPCOM_ENOTCONN";
3982 break;
3983
3984 case VPPCOM_ECONNREFUSED:
3985 st = "VPPCOM_ECONNREFUSED";
3986 break;
3987
3988 case VPPCOM_ETIMEDOUT:
3989 st = "VPPCOM_ETIMEDOUT";
3990 break;
3991
3992 default:
3993 st = "UNKNOWN_STATE";
3994 break;
3995 }
3996
3997 return st;
3998}
3999
Dave Wallacee22aa742017-10-20 12:30:38 -04004000/*
4001 * fd.io coding-style-patch-verification: ON
4002 *
4003 * Local Variables:
4004 * eval: (c-set-style "gnu")
4005 * End:
4006 */