blob: 39d665bdb27dc7d5847bd297a627a7b95067b65d [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 Coras54140622020-02-04 19:04:34 +000063 case STATE_CLOSED:
64 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040065 break;
66
67 case STATE_CONNECT:
68 st = "STATE_CONNECT";
69 break;
70
71 case STATE_LISTEN:
72 st = "STATE_LISTEN";
73 break;
74
75 case STATE_ACCEPT:
76 st = "STATE_ACCEPT";
77 break;
78
Florin Coras3c7d4f92018-12-14 11:28:43 -080079 case STATE_VPP_CLOSING:
80 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050081 break;
82
Dave Wallace543852a2017-08-03 02:11:34 -040083 case STATE_DISCONNECT:
84 st = "STATE_DISCONNECT";
85 break;
86
Florin Corasadcfb152020-02-12 08:50:29 +000087 case STATE_DETACHED:
88 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040089 break;
90
Florin Coras2d675d72019-01-28 15:54:27 -080091 case STATE_UPDATED:
92 st = "STATE_UPDATED";
93 break;
94
95 case STATE_LISTEN_NO_MQ:
96 st = "STATE_LISTEN_NO_MQ";
97 break;
98
Dave Wallace543852a2017-08-03 02:11:34 -040099 default:
100 st = "UNKNOWN_STATE";
101 break;
102 }
103
104 return st;
105}
106
Dave Wallace543852a2017-08-03 02:11:34 -0400107u8 *
108format_ip4_address (u8 * s, va_list * args)
109{
110 u8 *a = va_arg (*args, u8 *);
111 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
112}
113
114u8 *
115format_ip6_address (u8 * s, va_list * args)
116{
117 ip6_address_t *a = va_arg (*args, ip6_address_t *);
118 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
119
120 i_max_n_zero = ARRAY_LEN (a->as_u16);
121 max_n_zeros = 0;
122 i_first_zero = i_max_n_zero;
123 n_zeros = 0;
124 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
125 {
126 u32 is_zero = a->as_u16[i] == 0;
127 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
128 {
129 i_first_zero = i;
130 n_zeros = 0;
131 }
132 n_zeros += is_zero;
133 if ((!is_zero && n_zeros > max_n_zeros)
134 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
135 {
136 i_max_n_zero = i_first_zero;
137 max_n_zeros = n_zeros;
138 i_first_zero = ARRAY_LEN (a->as_u16);
139 n_zeros = 0;
140 }
141 }
142
143 last_double_colon = 0;
144 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
145 {
146 if (i == i_max_n_zero && max_n_zeros > 1)
147 {
148 s = format (s, "::");
149 i += max_n_zeros - 1;
150 last_double_colon = 1;
151 }
152 else
153 {
154 s = format (s, "%s%x",
155 (last_double_colon || i == 0) ? "" : ":",
156 clib_net_to_host_u16 (a->as_u16[i]));
157 last_double_colon = 0;
158 }
159 }
160
161 return s;
162}
163
164/* Format an IP46 address. */
165u8 *
166format_ip46_address (u8 * s, va_list * args)
167{
168 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
169 ip46_type_t type = va_arg (*args, ip46_type_t);
170 int is_ip4 = 1;
171
172 switch (type)
173 {
174 case IP46_TYPE_ANY:
175 is_ip4 = ip46_address_is_ip4 (ip46);
176 break;
177 case IP46_TYPE_IP4:
178 is_ip4 = 1;
179 break;
180 case IP46_TYPE_IP6:
181 is_ip4 = 0;
182 break;
183 }
184
185 return is_ip4 ?
186 format (s, "%U", format_ip4_address, &ip46->ip4) :
187 format (s, "%U", format_ip6_address, &ip46->ip6);
188}
189
Florin Coras697faea2018-06-27 17:10:49 -0700190/*
191 * VPPCOM Utility Functions
192 */
193
Florin Coras458089b2019-08-21 16:20:44 -0700194static void
195vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
196{
197 app_session_evt_t _app_evt, *app_evt = &_app_evt;
198 session_listen_msg_t *mp;
199 svm_msg_q_t *mq;
200
201 mq = vcl_worker_ctrl_mq (wrk);
202 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
203 mp = (session_listen_msg_t *) app_evt->evt->data;
204 memset (mp, 0, sizeof (*mp));
205 mp->client_index = wrk->my_client_index;
206 mp->context = s->session_index;
207 mp->wrk_index = wrk->vpp_wrk_index;
208 mp->is_ip4 = s->transport.is_ip4;
209 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
210 mp->port = s->transport.lcl_port;
211 mp->proto = s->session_type;
212 app_send_ctrl_evt_to_vpp (mq, app_evt);
213}
214
215static void
216vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
217{
218 app_session_evt_t _app_evt, *app_evt = &_app_evt;
219 session_connect_msg_t *mp;
220 svm_msg_q_t *mq;
221
222 mq = vcl_worker_ctrl_mq (wrk);
223 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
224 mp = (session_connect_msg_t *) app_evt->evt->data;
225 memset (mp, 0, sizeof (*mp));
226 mp->client_index = wrk->my_client_index;
227 mp->context = s->session_index;
228 mp->wrk_index = wrk->vpp_wrk_index;
229 mp->is_ip4 = s->transport.is_ip4;
230 mp->parent_handle = s->parent_handle;
231 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700232 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700233 mp->port = s->transport.rmt_port;
234 mp->proto = s->session_type;
235 app_send_ctrl_evt_to_vpp (mq, app_evt);
236}
237
238void
239vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
240{
241 app_session_evt_t _app_evt, *app_evt = &_app_evt;
242 session_unlisten_msg_t *mp;
243 svm_msg_q_t *mq;
244
245 mq = vcl_worker_ctrl_mq (wrk);
246 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
247 mp = (session_unlisten_msg_t *) app_evt->evt->data;
248 memset (mp, 0, sizeof (*mp));
249 mp->client_index = wrk->my_client_index;
250 mp->wrk_index = wrk->vpp_wrk_index;
251 mp->handle = s->vpp_handle;
252 mp->context = wrk->wrk_index;
253 app_send_ctrl_evt_to_vpp (mq, app_evt);
254}
255
256static void
257vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
258{
259 app_session_evt_t _app_evt, *app_evt = &_app_evt;
260 session_disconnect_msg_t *mp;
261 svm_msg_q_t *mq;
262
263 /* Send to thread that owns the session */
264 mq = s->vpp_evt_q;
265 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
266 mp = (session_disconnect_msg_t *) app_evt->evt->data;
267 memset (mp, 0, sizeof (*mp));
268 mp->client_index = wrk->my_client_index;
269 mp->handle = s->vpp_handle;
270 app_send_ctrl_evt_to_vpp (mq, app_evt);
271}
272
273static void
274vcl_send_app_detach (vcl_worker_t * wrk)
275{
276 app_session_evt_t _app_evt, *app_evt = &_app_evt;
277 session_app_detach_msg_t *mp;
278 svm_msg_q_t *mq;
279
280 mq = vcl_worker_ctrl_mq (wrk);
281 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
282 mp = (session_app_detach_msg_t *) app_evt->evt->data;
283 memset (mp, 0, sizeof (*mp));
284 mp->client_index = wrk->my_client_index;
285 app_send_ctrl_evt_to_vpp (mq, app_evt);
286}
Florin Coras697faea2018-06-27 17:10:49 -0700287
Florin Coras54693d22018-07-17 10:46:29 -0700288static void
289vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
290 session_handle_t handle, int retval)
291{
292 app_session_evt_t _app_evt, *app_evt = &_app_evt;
293 session_accepted_reply_msg_t *rmp;
294 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
295 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
296 rmp->handle = handle;
297 rmp->context = context;
298 rmp->retval = retval;
299 app_send_ctrl_evt_to_vpp (mq, app_evt);
300}
301
Florin Coras99368312018-08-02 10:45:44 -0700302static void
303vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
304 session_handle_t handle, int retval)
305{
306 app_session_evt_t _app_evt, *app_evt = &_app_evt;
307 session_disconnected_reply_msg_t *rmp;
308 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
309 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
310 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
311 rmp->handle = handle;
312 rmp->context = context;
313 rmp->retval = retval;
314 app_send_ctrl_evt_to_vpp (mq, app_evt);
315}
316
Florin Corasc9fbd662018-08-24 12:59:56 -0700317static void
318vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
319 session_handle_t handle, int retval)
320{
321 app_session_evt_t _app_evt, *app_evt = &_app_evt;
322 session_reset_reply_msg_t *rmp;
323 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
324 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
325 rmp->handle = handle;
326 rmp->context = context;
327 rmp->retval = retval;
328 app_send_ctrl_evt_to_vpp (mq, app_evt);
329}
330
Florin Coras30e79c22019-01-02 19:31:22 -0800331void
332vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
333 u32 wrk_index)
334{
335 app_session_evt_t _app_evt, *app_evt = &_app_evt;
336 session_worker_update_msg_t *mp;
337 svm_msg_q_t *mq;
338
339 mq = vcl_session_vpp_evt_q (wrk, s);
340 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
341 mp = (session_worker_update_msg_t *) app_evt->evt->data;
342 mp->client_index = wrk->my_client_index;
343 mp->handle = s->vpp_handle;
344 mp->req_wrk_index = wrk->vpp_wrk_index;
345 mp->wrk_index = wrk_index;
346 app_send_ctrl_evt_to_vpp (mq, app_evt);
347}
348
Florin Coras54693d22018-07-17 10:46:29 -0700349static u32
Florin Coras00cca802019-06-06 09:38:44 -0700350vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
351 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700352{
353 vcl_session_t *session, *listen_session;
354 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700355 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700356 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700357
Florin Coras134a9962018-08-28 11:32:04 -0700358 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700359
Florin Coras00cca802019-06-06 09:38:44 -0700360 listen_session = vcl_session_get (wrk, ls_index);
361 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700362 {
Florin Coras00cca802019-06-06 09:38:44 -0700363 VDBG (0, "ERROR: listener handle %lu does not match session %u",
364 mp->listener_handle, ls_index);
365 goto error;
366 }
367
Florin Corasc4c4cf52019-08-24 18:17:34 -0700368 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Coras00cca802019-06-06 09:38:44 -0700369 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700370 VDBG (0, "ERROR: segment for session %u is not mounted!",
Florin Coras00cca802019-06-06 09:38:44 -0700371 session->session_index);
372 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700373 }
374
Florin Coras54693d22018-07-17 10:46:29 -0700375 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
376 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Coras653e43f2019-03-04 10:56:23 -0800377 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
378 svm_msg_q_t *);
379 rx_fifo->client_session_index = session->session_index;
380 tx_fifo->client_session_index = session->session_index;
381 rx_fifo->client_thread_index = vcl_get_worker_index ();
382 tx_fifo->client_thread_index = vcl_get_worker_index ();
383 vpp_wrk_index = tx_fifo->master_thread_index;
384 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
385 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700386
387 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800388 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700389 session->rx_fifo = rx_fifo;
390 session->tx_fifo = tx_fifo;
391
392 session->session_state = STATE_ACCEPT;
Florin Coras09d18c22019-04-24 11:10:02 -0700393 session->transport.rmt_port = mp->rmt.port;
394 session->transport.is_ip4 = mp->rmt.is_ip4;
395 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500396 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700397
Florin Coras134a9962018-08-28 11:32:04 -0700398 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700399 session->transport.lcl_port = listen_session->transport.lcl_port;
400 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700401 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200402 session->is_dgram = vcl_proto_is_dgram (session->session_type);
403 session->listener_index = listen_session->session_index;
404 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700405
Florin Coras5e062572019-03-14 19:07:51 -0700406 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
407 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700408 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
409 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
410 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700411 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
412
Florin Coras00cca802019-06-06 09:38:44 -0700413 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
414 session->vpp_handle, 0);
415
Florin Coras134a9962018-08-28 11:32:04 -0700416 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700417
418error:
419 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
420 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
421 VNET_API_ERROR_INVALID_ARGUMENT);
422 vcl_session_free (wrk, session);
423 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700424}
425
426static u32
Florin Coras134a9962018-08-28 11:32:04 -0700427vcl_session_connected_handler (vcl_worker_t * wrk,
428 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700429{
Florin Coras99368312018-08-02 10:45:44 -0700430 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700431 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700432 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700433
434 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700435 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700436 if (!session)
437 {
Florin Coras5e062572019-03-14 19:07:51 -0700438 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
439 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700440 return VCL_INVALID_SESSION_INDEX;
441 }
Florin Coras54693d22018-07-17 10:46:29 -0700442 if (mp->retval)
443 {
Florin Coras5e062572019-03-14 19:07:51 -0700444 VDBG (0, "ERROR: session index %u: connect failed! %U",
445 session_index, format_api_error, ntohl (mp->retval));
Florin Corasadcfb152020-02-12 08:50:29 +0000446 session->session_state = STATE_DETACHED | STATE_DISCONNECT;
Florin Coras070453d2018-08-24 17:04:27 -0700447 session->vpp_handle = mp->handle;
448 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700449 }
450
Florin Corasdbc9c592019-09-25 16:37:43 -0700451 session->vpp_handle = mp->handle;
452 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
453 svm_msg_q_t *);
Florin Coras460dce62018-07-27 05:45:06 -0700454 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
455 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700456 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Corasd85de682018-11-29 17:02:29 -0800457 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700458 VDBG (0, "segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800459 session->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +0000460 session->session_state = STATE_DETACHED | STATE_DISCONNECT;
Florin Corasdbc9c592019-09-25 16:37:43 -0700461 vcl_send_session_disconnect (wrk, session);
462 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800463 }
464
Florin Coras460dce62018-07-27 05:45:06 -0700465 rx_fifo->client_session_index = session_index;
466 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700467 rx_fifo->client_thread_index = vcl_get_worker_index ();
468 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700469
Florin Coras653e43f2019-03-04 10:56:23 -0800470 vpp_wrk_index = tx_fifo->master_thread_index;
471 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
472 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700473
Florin Coras653e43f2019-03-04 10:56:23 -0800474 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700475 {
Florin Coras653e43f2019-03-04 10:56:23 -0800476 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
477 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700478 if (vcl_segment_is_not_mounted (wrk, mp->ct_segment_handle))
Florin Coras653e43f2019-03-04 10:56:23 -0800479 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700480 VDBG (0, "ct segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800481 session->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +0000482 session->session_state = STATE_DETACHED | STATE_DISCONNECT;
Florin Corasdbc9c592019-09-25 16:37:43 -0700483 vcl_send_session_disconnect (wrk, session);
484 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800485 }
Florin Coras99368312018-08-02 10:45:44 -0700486 }
Florin Coras54693d22018-07-17 10:46:29 -0700487
Florin Coras54693d22018-07-17 10:46:29 -0700488 session->rx_fifo = rx_fifo;
489 session->tx_fifo = tx_fifo;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800490 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras09d18c22019-04-24 11:10:02 -0700491 session->transport.is_ip4 = mp->lcl.is_ip4;
492 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500493 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700494 session->transport.lcl_port = mp->lcl.port;
Florin Coras54693d22018-07-17 10:46:29 -0700495 session->session_state = STATE_CONNECT;
496
497 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800498 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700499
Florin Coras5e062572019-03-14 19:07:51 -0700500 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
501 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700502 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700503
Florin Coras54693d22018-07-17 10:46:29 -0700504 return session_index;
505}
506
Florin Coras3c7d4f92018-12-14 11:28:43 -0800507static int
508vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
509{
510 vcl_session_msg_t *accepted_msg;
511 int i;
512
513 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
514 {
515 accepted_msg = &session->accept_evts_fifo[i];
516 if (accepted_msg->accepted_msg.handle == handle)
517 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800518 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800519 return 1;
520 }
521 }
522 return 0;
523}
524
Florin Corasc9fbd662018-08-24 12:59:56 -0700525static u32
Florin Coras134a9962018-08-28 11:32:04 -0700526vcl_session_reset_handler (vcl_worker_t * wrk,
527 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700528{
529 vcl_session_t *session;
530 u32 sid;
531
Florin Coras134a9962018-08-28 11:32:04 -0700532 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
533 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700534 if (!session)
535 {
536 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
537 return VCL_INVALID_SESSION_INDEX;
538 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800539
540 /* Caught a reset before actually accepting the session */
541 if (session->session_state == STATE_LISTEN)
542 {
543
544 if (!vcl_flag_accepted_session (session, reset_msg->handle,
545 VCL_ACCEPTED_F_RESET))
546 VDBG (0, "session was not accepted!");
547 return VCL_INVALID_SESSION_INDEX;
548 }
549
550 session->session_state = STATE_DISCONNECT;
551 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700552 return sid;
553}
554
Florin Coras60116992018-08-27 09:52:18 -0700555static u32
Florin Coras134a9962018-08-28 11:32:04 -0700556vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700557{
558 vcl_session_t *session;
559 u32 sid = mp->context;
560
Florin Coras134a9962018-08-28 11:32:04 -0700561 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700562 if (mp->retval)
563 {
Florin Coras5e062572019-03-14 19:07:51 -0700564 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Corasd85de682018-11-29 17:02:29 -0800565 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700566 if (session)
567 {
Florin Corasadcfb152020-02-12 08:50:29 +0000568 session->session_state = STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700569 session->vpp_handle = mp->handle;
570 return sid;
571 }
572 else
573 {
Florin Coras5e062572019-03-14 19:07:51 -0700574 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
575 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700576 return VCL_INVALID_SESSION_INDEX;
577 }
578 }
579
580 session->vpp_handle = mp->handle;
581 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500582 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
583 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700584 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700585 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700586 session->session_state = STATE_LISTEN;
587
Florin Coras5f45e012019-01-23 09:21:30 -0800588 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
589 vec_validate (wrk->vpp_event_queues, 0);
590 wrk->vpp_event_queues[0] = session->vpp_evt_q;
591
Florin Coras6e3c1f82020-01-15 01:30:46 +0000592 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700593 {
594 svm_fifo_t *rx_fifo, *tx_fifo;
595 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
596 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
597 rx_fifo->client_session_index = sid;
598 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
599 tx_fifo->client_session_index = sid;
600 session->rx_fifo = rx_fifo;
601 session->tx_fifo = tx_fifo;
602 }
603
Florin Coras05ecfcc2018-12-12 18:19:39 -0800604 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700605 return sid;
606}
607
Florin Corasdfae9f92019-02-20 19:48:31 -0800608static void
609vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
610{
611 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
612 vcl_session_t *s;
613
614 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
615 if (!s || s->session_state != STATE_DISCONNECT)
616 {
617 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
618 return;
619 }
620
621 if (mp->retval)
622 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
623 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
624
625 if (mp->context != wrk->wrk_index)
626 VDBG (0, "wrong context");
627
628 vcl_session_table_del_vpp_handle (wrk, mp->handle);
629 vcl_session_free (wrk, s);
630}
631
Florin Coras68b7e582020-01-21 18:33:23 -0800632static void
633vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
634{
635 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
636 vcl_session_t *s;
637
638 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
639 if (!s)
640 {
641 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
642 return;
643 }
644
645 s->vpp_thread_index = mp->vpp_thread_index;
646 s->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
647
648 vec_validate (wrk->vpp_event_queues, s->vpp_thread_index);
649 wrk->vpp_event_queues[s->vpp_thread_index] = s->vpp_evt_q;
650
651 vcl_session_table_del_vpp_handle (wrk, mp->handle);
652 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
653
654 /* Generate new tx event if we have outstanding data */
655 if (svm_fifo_has_event (s->tx_fifo))
656 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
657 SESSION_IO_EVT_TX, SVM_Q_WAIT);
658
659 VDBG (0, "Migrated 0x%x to thread %u", mp->handle, s->vpp_thread_index);
660}
661
Florin Coras3c7d4f92018-12-14 11:28:43 -0800662static vcl_session_t *
663vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
664{
665 vcl_session_msg_t *vcl_msg;
666 vcl_session_t *session;
667
668 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
669 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800670 VWRN ("session overlap handle %lu state %u!", msg->handle,
671 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800672
673 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
674 if (!session)
675 {
676 VERR ("couldn't find listen session: listener handle %llx",
677 msg->listener_handle);
678 return 0;
679 }
680
681 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
682 vcl_msg->accepted_msg = *msg;
683 /* Session handle points to listener until fully accepted by app */
684 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
685
686 return session;
687}
688
689static vcl_session_t *
690vcl_session_disconnected_handler (vcl_worker_t * wrk,
691 session_disconnected_msg_t * msg)
692{
693 vcl_session_t *session;
694
695 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
696 if (!session)
697 {
698 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
699 return 0;
700 }
701
Florin Corasadcfb152020-02-12 08:50:29 +0000702 /* Late disconnect notification on a session that has been closed */
703 if (session->session_state == STATE_CLOSED)
704 return 0;
705
Florin Coras3c7d4f92018-12-14 11:28:43 -0800706 /* Caught a disconnect before actually accepting the session */
707 if (session->session_state == STATE_LISTEN)
708 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800709 if (!vcl_flag_accepted_session (session, msg->handle,
710 VCL_ACCEPTED_F_CLOSED))
711 VDBG (0, "session was not accepted!");
712 return 0;
713 }
714
Florin Corasadcfb152020-02-12 08:50:29 +0000715 /* If not already reset change state */
716 if (session->session_state != STATE_DISCONNECT)
717 session->session_state = STATE_VPP_CLOSING;
718
Florin Coras3c7d4f92018-12-14 11:28:43 -0800719 return session;
720}
721
Florin Coras30e79c22019-01-02 19:31:22 -0800722static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700723vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
724{
725 session_cleanup_msg_t *msg;
726 vcl_session_t *session;
727
728 msg = (session_cleanup_msg_t *) data;
729 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
730 if (!session)
731 {
732 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
733 return;
734 }
735
736 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000737 /* Should not happen. App did not close the connection so don't free it. */
738 if (session->session_state != STATE_CLOSED)
739 {
740 VDBG (0, "app did not close session %d", session->session_index);
741 session->session_state = STATE_DETACHED;
742 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
743 return;
744 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700745 vcl_session_free (wrk, session);
746}
747
748static void
Florin Coras30e79c22019-01-02 19:31:22 -0800749vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
750{
751 session_req_worker_update_msg_t *msg;
752 vcl_session_t *s;
753
754 msg = (session_req_worker_update_msg_t *) data;
755 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
756 if (!s)
757 return;
758
759 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
760}
761
762static void
763vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
764{
765 session_worker_update_reply_msg_t *msg;
766 vcl_session_t *s;
767
768 msg = (session_worker_update_reply_msg_t *) data;
769 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
770 if (!s)
771 {
772 VDBG (0, "unknown handle 0x%llx", msg->handle);
773 return;
774 }
Florin Corasc4c4cf52019-08-24 18:17:34 -0700775 if (vcl_segment_is_not_mounted (wrk, msg->segment_handle))
Florin Coras30e79c22019-01-02 19:31:22 -0800776 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700777 clib_warning ("segment for session %u is not mounted!",
Florin Coras30e79c22019-01-02 19:31:22 -0800778 s->session_index);
779 return;
780 }
Florin Coras30e79c22019-01-02 19:31:22 -0800781
Florin Coras5f45e012019-01-23 09:21:30 -0800782 if (s->rx_fifo)
783 {
784 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
785 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
786 s->rx_fifo->client_session_index = s->session_index;
787 s->tx_fifo->client_session_index = s->session_index;
788 s->rx_fifo->client_thread_index = wrk->wrk_index;
789 s->tx_fifo->client_thread_index = wrk->wrk_index;
790 }
Florin Coras30e79c22019-01-02 19:31:22 -0800791 s->session_state = STATE_UPDATED;
792
Florin Coras30e79c22019-01-02 19:31:22 -0800793 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
794 s->vpp_handle, wrk->wrk_index);
795}
796
Florin Corasc4c4cf52019-08-24 18:17:34 -0700797static void
798vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
799{
800 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
801 session_app_add_segment_msg_t *msg;
802 u64 segment_handle;
803 int fd = -1;
804
805 msg = (session_app_add_segment_msg_t *) data;
806
807 if (msg->fd_flags)
808 {
809 vl_socket_client_recv_fd_msg2 (&wrk->bapi_sock_ctx, &fd, 1, 5);
810 seg_type = SSVM_SEGMENT_MEMFD;
811 }
812
813 segment_handle = msg->segment_handle;
814 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
815 {
816 clib_warning ("invalid segment handle");
817 return;
818 }
819
820 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
821 seg_type, fd))
822 {
823 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
824 return;
825 }
826
827 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
828 msg->segment_size);
829}
830
831static void
832vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
833{
834 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
835 vcl_segment_detach (msg->segment_handle);
836 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
837}
838
Florin Coras86f04502018-09-12 16:08:01 -0700839static int
840vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700841{
Florin Coras54693d22018-07-17 10:46:29 -0700842 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700843 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700844
845 switch (e->event_type)
846 {
Florin Coras653e43f2019-03-04 10:56:23 -0800847 case SESSION_IO_EVT_RX:
848 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -0800849 session = vcl_session_get (wrk, e->session_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800850 if (!session || !(session->session_state & STATE_OPEN))
851 break;
Florin Coras86f04502018-09-12 16:08:01 -0700852 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700853 break;
854 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800855 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700856 break;
857 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700858 vcl_session_connected_handler (wrk,
859 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700860 break;
861 case SESSION_CTRL_EVT_DISCONNECTED:
862 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800863 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700864 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800865 break;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800866 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
867 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700868 break;
869 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700870 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700871 break;
872 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700873 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700874 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800875 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
876 vcl_session_unlisten_reply_handler (wrk, e->data);
877 break;
Florin Coras68b7e582020-01-21 18:33:23 -0800878 case SESSION_CTRL_EVT_MIGRATED:
879 vcl_session_migrated_handler (wrk, e->data);
880 break;
Florin Coras9ace36d2019-10-28 13:14:17 -0700881 case SESSION_CTRL_EVT_CLEANUP:
882 vcl_session_cleanup_handler (wrk, e->data);
883 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800884 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
885 vcl_session_req_worker_update_handler (wrk, e->data);
886 break;
887 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
888 vcl_session_worker_update_reply_handler (wrk, e->data);
889 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700890 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
891 vcl_session_app_add_segment_handler (wrk, e->data);
892 break;
893 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
894 vcl_session_app_del_segment_handler (wrk, e->data);
895 break;
Florin Coras54693d22018-07-17 10:46:29 -0700896 default:
897 clib_warning ("unhandled %u", e->event_type);
898 }
899 return VPPCOM_OK;
900}
901
Florin Coras30e79c22019-01-02 19:31:22 -0800902static int
Florin Coras697faea2018-06-27 17:10:49 -0700903vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800904 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700905 f64 wait_for_time)
906{
Florin Coras134a9962018-08-28 11:32:04 -0700907 vcl_worker_t *wrk = vcl_worker_get_current ();
908 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700909 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700910 svm_msg_q_msg_t msg;
911 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400912
Florin Coras697faea2018-06-27 17:10:49 -0700913 do
Dave Wallace543852a2017-08-03 02:11:34 -0400914 {
Florin Coras134a9962018-08-28 11:32:04 -0700915 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700916 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700917 {
Florin Coras070453d2018-08-24 17:04:27 -0700918 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700919 }
920 if (session->session_state & state)
921 {
Florin Coras697faea2018-06-27 17:10:49 -0700922 return VPPCOM_OK;
923 }
Florin Corasadcfb152020-02-12 08:50:29 +0000924 if (session->session_state & STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -0700925 {
Florin Coras697faea2018-06-27 17:10:49 -0700926 return VPPCOM_ECONNREFUSED;
927 }
Florin Coras54693d22018-07-17 10:46:29 -0700928
Florin Coras134a9962018-08-28 11:32:04 -0700929 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800930 {
931 usleep (100);
932 continue;
933 }
Florin Coras134a9962018-08-28 11:32:04 -0700934 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700935 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700936 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800937 }
Florin Coras134a9962018-08-28 11:32:04 -0700938 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800939
Florin Coras05ecfcc2018-12-12 18:19:39 -0800940 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700941 vppcom_session_state_str (state));
942 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400943
Florin Coras697faea2018-06-27 17:10:49 -0700944 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500945}
946
Florin Coras30e79c22019-01-02 19:31:22 -0800947static void
948vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
949{
Florin Coras288eaab2019-02-03 15:26:14 -0800950 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -0800951 vcl_session_t *s;
952 u32 *sip;
953
954 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
955 return;
956
957 vec_foreach (sip, wrk->pending_session_wrk_updates)
958 {
959 s = vcl_session_get (wrk, *sip);
960 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
961 state = s->session_state;
962 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
963 s->session_state = state;
964 }
965 vec_reset_length (wrk->pending_session_wrk_updates);
966}
967
Florin Corasf9240dc2019-01-15 08:03:17 -0800968void
Florin Coras30e79c22019-01-02 19:31:22 -0800969vcl_flush_mq_events (void)
970{
971 vcl_worker_t *wrk = vcl_worker_get_current ();
972 svm_msg_q_msg_t *msg;
973 session_event_t *e;
974 svm_msg_q_t *mq;
975 int i;
976
977 mq = wrk->app_event_queue;
978 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -0700979 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -0800980 svm_msg_q_unlock (mq);
981
982 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
983 {
984 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
985 e = svm_msg_q_msg_data (mq, msg);
986 vcl_handle_mq_event (wrk, e);
987 svm_msg_q_free_msg (mq, msg);
988 }
989 vec_reset_length (wrk->mq_msg_vector);
990 vcl_handle_pending_wrk_updates (wrk);
991}
992
Florin Coras697faea2018-06-27 17:10:49 -0700993static int
994vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400995{
Florin Coras697faea2018-06-27 17:10:49 -0700996 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400997
Florin Coras697faea2018-06-27 17:10:49 -0700998 if (vcm->app_state != STATE_APP_ENABLED)
999 {
1000 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -07001001 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -07001002 if (PREDICT_FALSE (rv))
1003 {
Florin Coras5e062572019-03-14 19:07:51 -07001004 VDBG (0, "application session enable timed out! returning %d (%s)",
1005 rv, vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -07001006 return rv;
1007 }
1008 }
1009 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001010}
1011
Florin Coras697faea2018-06-27 17:10:49 -07001012static int
1013vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -04001014{
Florin Coras697faea2018-06-27 17:10:49 -07001015 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001016
Florin Coras697faea2018-06-27 17:10:49 -07001017 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -07001018 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -07001019 if (PREDICT_FALSE (rv))
1020 {
Florin Coras5e062572019-03-14 19:07:51 -07001021 VDBG (0, "application attach timed out! returning %d (%s)", rv,
1022 vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -07001023 return rv;
1024 }
Dave Wallace543852a2017-08-03 02:11:34 -04001025
Florin Coras697faea2018-06-27 17:10:49 -07001026 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001027}
1028
1029static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001030vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001031{
Florin Coras134a9962018-08-28 11:32:04 -07001032 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001033 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001034 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001035 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001036
Florin Corasab2f6db2018-08-31 14:31:41 -07001037 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001038 if (!session)
1039 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001040
Florin Corasaaff5ee2019-07-08 13:00:00 -07001041 /* Flush pending accept events, if any */
1042 while (clib_fifo_elts (session->accept_evts_fifo))
1043 {
1044 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1045 accepted_msg = &evt->accepted_msg;
1046 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1047 vcl_send_session_accepted_reply (session->vpp_evt_q,
1048 accepted_msg->context,
1049 session->vpp_handle, -1);
1050 }
1051 clib_fifo_free (session->accept_evts_fifo);
1052
Florin Coras458089b2019-08-21 16:20:44 -07001053 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001054
Florin Coras5e062572019-03-14 19:07:51 -07001055 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001056 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001057 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001058
1059 session->vpp_handle = ~0;
1060 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001061
Florin Coras070453d2018-08-24 17:04:27 -07001062 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001063}
1064
Florin Coras697faea2018-06-27 17:10:49 -07001065static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001066vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001067{
Florin Coras134a9962018-08-28 11:32:04 -07001068 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -07001069 svm_msg_q_t *vpp_evt_q;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001070 vcl_session_t *session, *listen_session;
Florin Coras288eaab2019-02-03 15:26:14 -08001071 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -07001072 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001073
Florin Corasab2f6db2018-08-31 14:31:41 -07001074 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -07001075 if (!session)
1076 return VPPCOM_EBADFD;
1077
Dave Wallace4878cbe2017-11-21 03:45:09 -05001078 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001079 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001080
Florin Coras5e062572019-03-14 19:07:51 -07001081 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
1082 vpp_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001083
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001084 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001085 {
Florin Coras5e062572019-03-14 19:07:51 -07001086 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
Florin Coras070453d2018-08-24 17:04:27 -07001087 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001088 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001089
Florin Coras3c7d4f92018-12-14 11:28:43 -08001090 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001091 {
Florin Coras134a9962018-08-28 11:32:04 -07001092 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -08001093 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -07001094 vpp_handle, 0);
Florin Coras5e062572019-03-14 19:07:51 -07001095 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
1096 session->session_index, vpp_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001097 }
1098 else
Dave Wallace227867f2017-11-13 21:21:53 -05001099 {
Florin Coras5e062572019-03-14 19:07:51 -07001100 VDBG (1, "session %u [0x%llx]: sending disconnect...",
1101 session->session_index, vpp_handle);
Florin Coras458089b2019-08-21 16:20:44 -07001102 vcl_send_session_disconnect (wrk, session);
Dave Wallace227867f2017-11-13 21:21:53 -05001103 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001104
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001105 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
1106 {
1107 listen_session = vcl_session_get (wrk, session->listener_index);
1108 listen_session->n_accepted_sessions--;
1109 }
1110
Florin Coras070453d2018-08-24 17:04:27 -07001111 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001112}
1113
Florin Coras940f78f2018-11-30 12:11:20 -08001114/**
1115 * Handle app exit
1116 *
1117 * Notify vpp of the disconnect and mark the worker as free. If we're the
1118 * last worker, do a full cleanup otherwise, since we're probably a forked
1119 * child, avoid syscalls as much as possible. We might've lost privileges.
1120 */
1121void
1122vppcom_app_exit (void)
1123{
1124 if (!pool_elts (vcm->workers))
1125 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001126 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1127 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001128 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001129}
1130
Dave Wallace543852a2017-08-03 02:11:34 -04001131/*
1132 * VPPCOM Public API functions
1133 */
1134int
1135vppcom_app_create (char *app_name)
1136{
Dave Wallace543852a2017-08-03 02:11:34 -04001137 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001138 int rv;
1139
Florin Coras47c40e22018-11-26 17:01:36 -08001140 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001141 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001142 VDBG (1, "already initialized");
1143 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001144 }
1145
Florin Coras47c40e22018-11-26 17:01:36 -08001146 vcm->is_init = 1;
1147 vppcom_cfg (&vcm->cfg);
1148 vcl_cfg = &vcm->cfg;
1149
1150 vcm->main_cpu = pthread_self ();
1151 vcm->main_pid = getpid ();
1152 vcm->app_name = format (0, "%s", app_name);
1153 vppcom_init_error_string_table ();
Florin Coras88001c62019-04-24 14:44:46 -07001154 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1155 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001156 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1157 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001158 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001159 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -08001160
1161 /* Allocate default worker */
1162 vcl_worker_alloc_and_init ();
1163
1164 /* API hookup and connect to VPP */
Florin Coras47c40e22018-11-26 17:01:36 -08001165 vcl_elog_init (vcm);
1166 vcm->app_state = STATE_APP_START;
1167 rv = vppcom_connect_to_vpp (app_name);
1168 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -04001169 {
Florin Coras47c40e22018-11-26 17:01:36 -08001170 VERR ("couldn't connect to VPP!");
1171 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001172 }
Florin Coras47c40e22018-11-26 17:01:36 -08001173 VDBG (0, "sending session enable");
1174 rv = vppcom_app_session_enable ();
1175 if (rv)
1176 {
1177 VERR ("vppcom_app_session_enable() failed!");
1178 return rv;
1179 }
1180
1181 VDBG (0, "sending app attach");
1182 rv = vppcom_app_attach ();
1183 if (rv)
1184 {
1185 VERR ("vppcom_app_attach() failed!");
1186 return rv;
1187 }
1188
1189 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
1190 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001191
1192 return VPPCOM_OK;
1193}
1194
1195void
1196vppcom_app_destroy (void)
1197{
Dave Wallace543852a2017-08-03 02:11:34 -04001198 int rv;
Dave Wallacede910062018-03-20 09:22:13 -04001199 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04001200
Florin Coras940f78f2018-11-30 12:11:20 -08001201 if (!pool_elts (vcm->workers))
1202 return;
1203
Florin Coras0d427d82018-06-27 03:24:07 -07001204 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001205
Florin Coras940f78f2018-11-30 12:11:20 -08001206 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -08001207 {
Florin Coras458089b2019-08-21 16:20:44 -07001208 vcl_send_app_detach (vcl_worker_get_current ());
Florin Coras47c40e22018-11-26 17:01:36 -08001209 orig_app_timeout = vcm->cfg.app_timeout;
1210 vcm->cfg.app_timeout = 2.0;
1211 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1212 vcm->cfg.app_timeout = orig_app_timeout;
1213 if (PREDICT_FALSE (rv))
1214 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1215 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -08001216 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -08001217 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001218 }
1219 else
1220 {
Florin Coras01f3f892018-12-02 12:45:53 -08001221 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001222 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001223
Florin Coras01f3f892018-12-02 12:45:53 -08001224 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -07001225 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001226 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001227}
1228
1229int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001230vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001231{
Florin Coras134a9962018-08-28 11:32:04 -07001232 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001233 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001234
Florin Coras134a9962018-08-28 11:32:04 -07001235 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001236
Florin Coras7e12d942018-06-27 14:32:43 -07001237 session->session_type = proto;
Florin Coras54140622020-02-04 19:04:34 +00001238 session->session_state = STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001239 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001240 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001241
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001242 if (is_nonblocking)
1243 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001244
Florin Coras7e12d942018-06-27 14:32:43 -07001245 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1246 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001247
Florin Coras5e062572019-03-14 19:07:51 -07001248 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001249
Florin Coras134a9962018-08-28 11:32:04 -07001250 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001251}
1252
1253int
Florin Corasf9240dc2019-01-15 08:03:17 -08001254vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1255 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001256{
Florin Coras288eaab2019-02-03 15:26:14 -08001257 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001258 u32 next_sh, vep_sh;
1259 int rv = VPPCOM_OK;
1260 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001261 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001262
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001263 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001264 next_sh = session->vep.next_sh;
1265 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001266 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001267 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001268
Florin Corasf9240dc2019-01-15 08:03:17 -08001269 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001270
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001271 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001272 {
Florin Coras134a9962018-08-28 11:32:04 -07001273 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001274 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001275 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001276 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001277 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras47c40e22018-11-26 17:01:36 -08001278 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1279 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001280
Florin Coras134a9962018-08-28 11:32:04 -07001281 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001282 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001283 goto cleanup;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001284 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001285
1286 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001287 {
Florin Coras9ace36d2019-10-28 13:14:17 -07001288 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
1289 if (rv < 0)
1290 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1291 "failed! rv %d (%s)", session->session_index, vpp_handle,
1292 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001293 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001294
Florin Coras9ace36d2019-10-28 13:14:17 -07001295 if (!do_disconnect)
1296 {
1297 VDBG (1, "session %u [0x%llx] disconnect skipped",
1298 session->session_index, vpp_handle);
1299 goto cleanup;
1300 }
1301
1302 if (state & STATE_LISTEN)
1303 {
1304 rv = vppcom_session_unbind (sh);
1305 if (PREDICT_FALSE (rv < 0))
1306 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1307 "rv %d (%s)", session->session_index, vpp_handle, rv,
1308 vppcom_retval_str (rv));
1309 return rv;
1310 }
1311 else if ((state & STATE_OPEN)
1312 || (vcl_session_is_connectable_listener (wrk, session)))
1313 {
1314 rv = vppcom_session_disconnect (sh);
1315 if (PREDICT_FALSE (rv < 0))
1316 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1317 " rv %d (%s)", session->session_index, vpp_handle,
1318 rv, vppcom_retval_str (rv));
1319 }
1320 else if (state == STATE_DISCONNECT)
1321 {
1322 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1323 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1324 session->vpp_handle, 0);
1325 }
Florin Corasadcfb152020-02-12 08:50:29 +00001326 else if (state == STATE_DETACHED)
1327 {
1328 /* Should not happen. VPP cleaned up before app confirmed close */
1329 VDBG (0, "vpp freed session %d before close", session->session_index);
1330 goto free_session;
1331 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001332
Florin Coras54140622020-02-04 19:04:34 +00001333 session->session_state = STATE_CLOSED;
1334
Florin Coras9ace36d2019-10-28 13:14:17 -07001335 /* Session is removed only after vpp confirms the disconnect */
1336 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001337
Florin Corasf9240dc2019-01-15 08:03:17 -08001338cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001339 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001340free_session:
Florin Coras134a9962018-08-28 11:32:04 -07001341 vcl_session_free (wrk, session);
Florin Coras0d427d82018-06-27 03:24:07 -07001342 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001343
Dave Wallace543852a2017-08-03 02:11:34 -04001344 return rv;
1345}
1346
1347int
Florin Corasf9240dc2019-01-15 08:03:17 -08001348vppcom_session_close (uint32_t session_handle)
1349{
1350 vcl_worker_t *wrk = vcl_worker_get_current ();
1351 vcl_session_t *session;
1352
1353 session = vcl_session_get_w_handle (wrk, session_handle);
1354 if (!session)
1355 return VPPCOM_EBADFD;
1356 return vcl_session_cleanup (wrk, session, session_handle,
1357 1 /* do_disconnect */ );
1358}
1359
1360int
Florin Coras134a9962018-08-28 11:32:04 -07001361vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001362{
Florin Coras134a9962018-08-28 11:32:04 -07001363 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001364 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001365
1366 if (!ep || !ep->ip)
1367 return VPPCOM_EINVAL;
1368
Florin Coras134a9962018-08-28 11:32:04 -07001369 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001370 if (!session)
1371 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001372
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001373 if (session->is_vep)
1374 {
Florin Coras5e062572019-03-14 19:07:51 -07001375 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1376 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001377 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001378 }
1379
Florin Coras7e12d942018-06-27 14:32:43 -07001380 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001381 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001382 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1383 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001384 else
Dave Barach178cf492018-11-13 16:34:13 -05001385 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1386 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001387 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001388
Florin Coras5e062572019-03-14 19:07:51 -07001389 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1390 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001391 session->transport.is_ip4 ? "IPv4" : "IPv6",
1392 format_ip46_address, &session->transport.lcl_ip,
1393 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1394 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001395 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001396 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001397
1398 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001399 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001400
Florin Coras070453d2018-08-24 17:04:27 -07001401 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001402}
1403
1404int
Florin Coras134a9962018-08-28 11:32:04 -07001405vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001406{
Florin Coras134a9962018-08-28 11:32:04 -07001407 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001408 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001409 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001410 int rv;
1411
Florin Coras134a9962018-08-28 11:32:04 -07001412 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001413 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001414 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001415
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001416 if (q_len == 0 || q_len == ~0)
1417 q_len = vcm->cfg.listen_queue_size;
1418
Dave Wallaceee45d412017-11-24 21:44:06 -05001419 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001420 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001421 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001422 VDBG (0, "session %u [0x%llx]: already in listen state!",
1423 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001424 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001425 }
1426
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001427 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001428
Florin Coras070453d2018-08-24 17:04:27 -07001429 /*
1430 * Send listen request to vpp and wait for reply
1431 */
Florin Coras458089b2019-08-21 16:20:44 -07001432 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001433 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1434 STATE_LISTEN,
1435 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001436
Florin Coras070453d2018-08-24 17:04:27 -07001437 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001438 {
Florin Coras134a9962018-08-28 11:32:04 -07001439 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001440 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1441 listen_sh, listen_session->vpp_handle, rv,
1442 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001443 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001444 }
1445
Florin Coras070453d2018-08-24 17:04:27 -07001446 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001447}
1448
Ping Yu34a3a082018-11-30 19:16:17 -05001449int
1450vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1451 uint32_t cert_len)
1452{
1453
1454 vcl_worker_t *wrk = vcl_worker_get_current ();
1455 vcl_session_t *session = 0;
1456
1457 session = vcl_session_get_w_handle (wrk, session_handle);
1458 if (!session)
1459 return VPPCOM_EBADFD;
1460
1461 if (cert_len == 0 || cert_len == ~0)
1462 return VPPCOM_EBADFD;
1463
1464 /*
1465 * Send listen request to vpp and wait for reply
1466 */
1467 vppcom_send_application_tls_cert_add (session, cert, cert_len);
Florin Coras458089b2019-08-21 16:20:44 -07001468 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1469 vcl_wait_for_app_state_change (STATE_APP_READY);
Ping Yu34a3a082018-11-30 19:16:17 -05001470 return VPPCOM_OK;
1471
1472}
1473
1474int
1475vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1476 uint32_t key_len)
1477{
1478
1479 vcl_worker_t *wrk = vcl_worker_get_current ();
1480 vcl_session_t *session = 0;
1481
1482 session = vcl_session_get_w_handle (wrk, session_handle);
1483 if (!session)
1484 return VPPCOM_EBADFD;
1485
1486 if (key_len == 0 || key_len == ~0)
1487 return VPPCOM_EBADFD;
1488
Ping Yu34a3a082018-11-30 19:16:17 -05001489 vppcom_send_application_tls_key_add (session, key, key_len);
Florin Coras458089b2019-08-21 16:20:44 -07001490 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1491 vcl_wait_for_app_state_change (STATE_APP_READY);
Ping Yu34a3a082018-11-30 19:16:17 -05001492 return VPPCOM_OK;
Ping Yu34a3a082018-11-30 19:16:17 -05001493}
1494
Florin Coras134a9962018-08-28 11:32:04 -07001495static int
Florin Coras5e062572019-03-14 19:07:51 -07001496validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001497{
Florin Coras5e062572019-03-14 19:07:51 -07001498 if (ls->is_vep)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001499 {
Florin Coras5e062572019-03-14 19:07:51 -07001500 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1501 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001502 return VPPCOM_EBADFD;
1503 }
1504
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001505 if ((ls->session_state != STATE_LISTEN)
1506 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001507 {
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001508 VDBG (0,
1509 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1510 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001511 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001512 return VPPCOM_EBADFD;
1513 }
1514 return VPPCOM_OK;
1515}
1516
1517int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001518vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1519{
1520 if (!strcmp (proto_str, "TCP"))
1521 *proto = VPPCOM_PROTO_TCP;
1522 else if (!strcmp (proto_str, "tcp"))
1523 *proto = VPPCOM_PROTO_TCP;
1524 else if (!strcmp (proto_str, "UDP"))
1525 *proto = VPPCOM_PROTO_UDP;
1526 else if (!strcmp (proto_str, "udp"))
1527 *proto = VPPCOM_PROTO_UDP;
1528 else if (!strcmp (proto_str, "UDPC"))
1529 *proto = VPPCOM_PROTO_UDPC;
1530 else if (!strcmp (proto_str, "udpc"))
1531 *proto = VPPCOM_PROTO_UDPC;
1532 else if (!strcmp (proto_str, "SCTP"))
1533 *proto = VPPCOM_PROTO_SCTP;
1534 else if (!strcmp (proto_str, "sctp"))
1535 *proto = VPPCOM_PROTO_SCTP;
1536 else if (!strcmp (proto_str, "TLS"))
1537 *proto = VPPCOM_PROTO_TLS;
1538 else if (!strcmp (proto_str, "tls"))
1539 *proto = VPPCOM_PROTO_TLS;
1540 else if (!strcmp (proto_str, "QUIC"))
1541 *proto = VPPCOM_PROTO_QUIC;
1542 else if (!strcmp (proto_str, "quic"))
1543 *proto = VPPCOM_PROTO_QUIC;
1544 else
1545 return 1;
1546 return 0;
1547}
1548
1549int
Florin Coras134a9962018-08-28 11:32:04 -07001550vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001551 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001552{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001553 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001554 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001555 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001556 vcl_session_t *listen_session = 0;
1557 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001558 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001559 svm_msg_q_msg_t msg;
1560 session_event_t *e;
1561 u8 is_nonblocking;
1562 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001563
Florin Coras134a9962018-08-28 11:32:04 -07001564 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001565 if (!listen_session)
1566 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001567
Florin Coras134a9962018-08-28 11:32:04 -07001568 listen_session_index = listen_session->session_index;
1569 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001570 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001571
Florin Coras54693d22018-07-17 10:46:29 -07001572 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001573 {
Florin Coras54693d22018-07-17 10:46:29 -07001574 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001575 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001576 accepted_msg = evt->accepted_msg;
1577 goto handle;
1578 }
1579
1580 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1581 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001582 while (1)
1583 {
Carl Smith592a9092019-11-12 14:57:37 +13001584 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1585 return VPPCOM_EAGAIN;
1586
Florin Coras134a9962018-08-28 11:32:04 -07001587 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001588 return VPPCOM_EAGAIN;
1589
Florin Coras134a9962018-08-28 11:32:04 -07001590 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001591 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001592 {
wanghanlin96453fd2019-12-16 19:14:39 +08001593 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001594 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001595 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001596 }
Dave Barach178cf492018-11-13 16:34:13 -05001597 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001598 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001599 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001600 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001601
Florin Coras54693d22018-07-17 10:46:29 -07001602handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001603
Florin Coras00cca802019-06-06 09:38:44 -07001604 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1605 listen_session_index);
1606 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1607 return VPPCOM_ECONNABORTED;
1608
Florin Coras134a9962018-08-28 11:32:04 -07001609 listen_session = vcl_session_get (wrk, listen_session_index);
1610 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001611
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001612 if (flags & O_NONBLOCK)
1613 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001614
Florin Coras5e062572019-03-14 19:07:51 -07001615 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1616 " flags %d, is_nonblocking %u", listen_session->session_index,
1617 listen_session->vpp_handle, client_session_index,
1618 client_session->vpp_handle, flags,
1619 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001620
Dave Wallace048b1d62018-01-03 22:24:41 -05001621 if (ep)
1622 {
Florin Coras7e12d942018-06-27 14:32:43 -07001623 ep->is_ip4 = client_session->transport.is_ip4;
1624 ep->port = client_session->transport.rmt_port;
1625 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001626 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1627 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001628 else
Dave Barach178cf492018-11-13 16:34:13 -05001629 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1630 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001631 }
Dave Wallace60caa062017-11-10 17:07:13 -05001632
Florin Coras05ecfcc2018-12-12 18:19:39 -08001633 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001634 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001635 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001636 format_ip46_address, &client_session->transport.rmt_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.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001639 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001640 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001641 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001642 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1643 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001644
Florin Coras3c7d4f92018-12-14 11:28:43 -08001645 /*
1646 * Session might have been closed already
1647 */
1648 if (accept_flags)
1649 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001650 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001651 client_session->session_state = STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001652 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasb0f662f2018-12-27 14:51:46 -08001653 client_session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001654 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001655 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001656}
1657
Florin Corasef7cbf62019-10-17 09:56:27 -07001658static void
1659vcl_ip_copy_from_ep (ip46_address_t * ip, vppcom_endpt_t * ep)
1660{
1661 if (ep->is_ip4)
1662 clib_memcpy_fast (&ip->ip4, ep->ip, sizeof (ip4_address_t));
1663 else
1664 clib_memcpy_fast (&ip->ip6, ep->ip, sizeof (ip6_address_t));
1665}
1666
1667void
1668vcl_ip_copy_to_ep (ip46_address_t * ip, vppcom_endpt_t * ep, u8 is_ip4)
1669{
1670 ep->is_ip4 = is_ip4;
1671 if (is_ip4)
1672 clib_memcpy_fast (ep->ip, &ip->ip4, sizeof (ip4_address_t));
1673 else
1674 clib_memcpy_fast (ep->ip, &ip->ip6, sizeof (ip6_address_t));
1675}
1676
Dave Wallace543852a2017-08-03 02:11:34 -04001677int
Florin Coras134a9962018-08-28 11:32:04 -07001678vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001679{
Florin Coras134a9962018-08-28 11:32:04 -07001680 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001681 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001682 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001683 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001684
Florin Coras134a9962018-08-28 11:32:04 -07001685 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001686 if (!session)
1687 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001688 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001689
1690 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001691 {
Florin Coras5e062572019-03-14 19:07:51 -07001692 VDBG (0, "ERROR: cannot connect epoll session %u!",
1693 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001694 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001695 }
1696
Florin Coras7e12d942018-06-27 14:32:43 -07001697 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001698 {
Florin Coras7baeb712019-01-04 17:05:43 -08001699 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001700 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001701 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001702 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001703 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001704 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001705 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001706 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001707 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001708 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001709 }
1710
Florin Coras7e12d942018-06-27 14:32:43 -07001711 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001712 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001713 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001714 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Dave Wallace543852a2017-08-03 02:11:34 -04001715
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001716 VDBG (0, "session handle %u: connecting to server %s %U "
1717 "port %d proto %s", session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001718 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001719 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001720 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001721 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001722 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001723 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001724
Florin Coras458089b2019-08-21 16:20:44 -07001725 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001726
1727 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK))
1728 return VPPCOM_EINPROGRESS;
1729
1730 /*
1731 * Wait for reply from vpp if blocking
1732 */
Florin Coras070453d2018-08-24 17:04:27 -07001733 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1734 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001735
Florin Coras134a9962018-08-28 11:32:04 -07001736 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001737 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1738 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001739
Dave Wallace4878cbe2017-11-21 03:45:09 -05001740 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001741}
1742
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001743int
1744vppcom_session_stream_connect (uint32_t session_handle,
1745 uint32_t parent_session_handle)
1746{
1747 vcl_worker_t *wrk = vcl_worker_get_current ();
1748 vcl_session_t *session, *parent_session;
1749 u32 session_index, parent_session_index;
1750 int rv;
1751
1752 session = vcl_session_get_w_handle (wrk, session_handle);
1753 if (!session)
1754 return VPPCOM_EBADFD;
1755 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1756 if (!parent_session)
1757 return VPPCOM_EBADFD;
1758
1759 session_index = session->session_index;
1760 parent_session_index = parent_session->session_index;
1761 if (PREDICT_FALSE (session->is_vep))
1762 {
1763 VDBG (0, "ERROR: cannot connect epoll session %u!",
1764 session->session_index);
1765 return VPPCOM_EBADFD;
1766 }
1767
1768 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1769 {
1770 VDBG (0, "session handle %u [0x%llx]: session already "
1771 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1772 session_handle, session->vpp_handle,
1773 parent_session_handle, parent_session->vpp_handle,
1774 vppcom_proto_str (session->session_type), session->session_state,
1775 vppcom_session_state_str (session->session_state));
1776 return VPPCOM_OK;
1777 }
1778
1779 /* Connect to quic session specifics */
1780 session->transport.is_ip4 = parent_session->transport.is_ip4;
1781 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1782 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001783 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001784
1785 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1786 session_handle, parent_session_handle, parent_session->vpp_handle);
1787
1788 /*
1789 * Send connect request and wait for reply from vpp
1790 */
Florin Coras458089b2019-08-21 16:20:44 -07001791 vcl_send_session_connect (wrk, session);
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001792 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1793 vcm->cfg.session_timeout);
1794
1795 session->listener_index = parent_session_index;
1796 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001797 if (parent_session)
1798 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001799
1800 session = vcl_session_get (wrk, session_index);
1801 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1802 session->vpp_handle, rv ? "failed" : "succeeded");
1803
1804 return rv;
1805}
1806
Florin Coras54693d22018-07-17 10:46:29 -07001807static u8
1808vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1809{
Florin Corasc0737e92019-03-04 14:19:39 -08001810 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001811}
1812
Steven58f464e2017-10-25 12:33:12 -07001813static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001814vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001815 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001816{
Florin Coras134a9962018-08-28 11:32:04 -07001817 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001818 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001819 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001820 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001821 svm_msg_q_msg_t msg;
1822 session_event_t *e;
1823 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001824 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001825
Florin Coras070453d2018-08-24 17:04:27 -07001826 if (PREDICT_FALSE (!buf))
1827 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001828
Florin Coras134a9962018-08-28 11:32:04 -07001829 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001830 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001831 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001832
Florin Coras6d0106e2019-01-29 20:11:58 -08001833 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001834 {
Florin Coras5e062572019-03-14 19:07:51 -07001835 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001836 s->session_index, s->vpp_handle, s->session_state,
1837 vppcom_session_state_str (s->session_state));
1838 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001839 }
1840
Florin Coras2cba8532018-09-11 16:33:36 -07001841 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001842 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001843 mq = wrk->app_event_queue;
1844 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001845 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001846
Sirshak Das28aa5392019-02-05 01:33:33 -06001847 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001848 {
Florin Coras54693d22018-07-17 10:46:29 -07001849 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001850 {
Yu Pingb2955352019-12-04 06:49:04 +08001851 if (vcl_session_is_closing (s))
1852 return vcl_session_closing_error (s);
Florin Corasc0737e92019-03-04 14:19:39 -08001853 svm_fifo_unset_event (s->rx_fifo);
1854 return VPPCOM_EWOULDBLOCK;
1855 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001856 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001857 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001858 if (vcl_session_is_closing (s))
1859 return vcl_session_closing_error (s);
1860
Florin Corasc0737e92019-03-04 14:19:39 -08001861 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001862 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001863 if (svm_msg_q_is_empty (mq))
1864 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001865
Florin Coras54693d22018-07-17 10:46:29 -07001866 svm_msg_q_sub_w_lock (mq, &msg);
1867 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001868 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001869 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001870 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001871 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001872 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001873 }
Florin Coras54693d22018-07-17 10:46:29 -07001874
Florin Coras460dce62018-07-27 05:45:06 -07001875 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001876 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001877 else
Florin Coras99368312018-08-02 10:45:44 -07001878 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
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 Corasc0737e92019-03-04 14:19:39 -08001881 svm_fifo_unset_event (s->rx_fifo);
Florin Coras41c9e042018-09-11 00:10:41 -07001882
Florin Coras317b8e02019-04-17 09:57:46 -07001883 /* Cut-through sessions might request tx notifications on rx fifos */
Florin Coras2d379d82019-06-28 12:45:12 -07001884 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
Florin Coras317b8e02019-04-17 09:57:46 -07001885 {
1886 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1887 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras2d379d82019-06-28 12:45:12 -07001888 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001889 }
1890
Florin Coras5e062572019-03-14 19:07:51 -07001891 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1892 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001893
Florin Coras54693d22018-07-17 10:46:29 -07001894 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001895}
1896
Steven58f464e2017-10-25 12:33:12 -07001897int
Florin Coras134a9962018-08-28 11:32:04 -07001898vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001899{
Florin Coras134a9962018-08-28 11:32:04 -07001900 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001901}
1902
1903static int
Florin Coras134a9962018-08-28 11:32:04 -07001904vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001905{
Florin Coras134a9962018-08-28 11:32:04 -07001906 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001907}
1908
Florin Coras2cba8532018-09-11 16:33:36 -07001909int
1910vppcom_session_read_segments (uint32_t session_handle,
1911 vppcom_data_segments_t ds)
1912{
1913 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001914 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001915 vcl_session_t *s = 0;
1916 svm_fifo_t *rx_fifo;
1917 svm_msg_q_msg_t msg;
1918 session_event_t *e;
1919 svm_msg_q_t *mq;
1920 u8 is_ct;
1921
1922 s = vcl_session_get_w_handle (wrk, session_handle);
1923 if (PREDICT_FALSE (!s || s->is_vep))
1924 return VPPCOM_EBADFD;
1925
Florin Coras6d0106e2019-01-29 20:11:58 -08001926 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1927 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001928
1929 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1930 is_ct = vcl_session_is_ct (s);
1931 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1932 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001933 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001934
Florin Coras653e43f2019-03-04 10:56:23 -08001935 if (is_ct)
1936 svm_fifo_unset_event (s->rx_fifo);
1937
Sirshak Das28aa5392019-02-05 01:33:33 -06001938 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001939 {
1940 if (is_nonblocking)
1941 {
1942 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001943 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001944 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001945 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001946 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001947 if (vcl_session_is_closing (s))
1948 return vcl_session_closing_error (s);
1949
Florin Coras2cba8532018-09-11 16:33:36 -07001950 svm_fifo_unset_event (rx_fifo);
1951 svm_msg_q_lock (mq);
1952 if (svm_msg_q_is_empty (mq))
1953 svm_msg_q_wait (mq);
1954
1955 svm_msg_q_sub_w_lock (mq, &msg);
1956 e = svm_msg_q_msg_data (mq, &msg);
1957 svm_msg_q_unlock (mq);
1958 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001959 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001960 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001961 }
1962 }
1963
Florin Coras88001c62019-04-24 14:44:46 -07001964 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001965 svm_fifo_unset_event (rx_fifo);
1966
Florin Coras2cba8532018-09-11 16:33:36 -07001967 return n_read;
1968}
1969
1970void
1971vppcom_session_free_segments (uint32_t session_handle,
1972 vppcom_data_segments_t ds)
1973{
1974 vcl_worker_t *wrk = vcl_worker_get_current ();
1975 vcl_session_t *s;
1976
1977 s = vcl_session_get_w_handle (wrk, session_handle);
1978 if (PREDICT_FALSE (!s || s->is_vep))
1979 return;
1980
Florin Coras88001c62019-04-24 14:44:46 -07001981 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001982}
1983
Florin Coras2cba8532018-09-11 16:33:36 -07001984int
1985vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1986{
1987 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001988 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001989 if (first_copy < max_bytes)
1990 {
Dave Barach178cf492018-11-13 16:34:13 -05001991 clib_memcpy_fast (buf + first_copy, ds[1].data,
1992 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001993 }
1994 return 0;
1995}
1996
Florin Coras54693d22018-07-17 10:46:29 -07001997static u8
1998vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1999{
Florin Corasc0737e92019-03-04 14:19:39 -08002000 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002001}
2002
Florin Coras42ceddb2018-12-12 10:56:01 -08002003static inline int
2004vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
2005 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04002006{
Florin Coras134a9962018-08-28 11:32:04 -07002007 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08002008 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002009 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07002010 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002011 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002012 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002013 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002014 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002015 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002016
jiangxiaomingff31ac62019-11-21 23:34:56 +08002017 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002018 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002019
Florin Coras134a9962018-08-28 11:32:04 -07002020 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002021 if (PREDICT_FALSE (!s))
2022 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002023
Florin Coras460dce62018-07-27 05:45:06 -07002024 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04002025 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002026 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2027 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002028 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002029 }
2030
Florin Coras6d0106e2019-01-29 20:11:58 -08002031 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002032 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002033 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2034 s->session_index, s->vpp_handle, s->session_state,
2035 vppcom_session_state_str (s->session_state));
2036 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002037 }
2038
Florin Coras0e88e852018-09-17 22:09:02 -07002039 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002040 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07002041 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002042
Florin Coras653e43f2019-03-04 10:56:23 -08002043 mq = wrk->app_event_queue;
Sirshak Das28aa5392019-02-05 01:33:33 -06002044 if (svm_fifo_is_full_prod (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04002045 {
Florin Coras54693d22018-07-17 10:46:29 -07002046 if (is_nonblocking)
2047 {
Florin Coras070453d2018-08-24 17:04:27 -07002048 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002049 }
Sirshak Das28aa5392019-02-05 01:33:33 -06002050 while (svm_fifo_is_full_prod (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07002051 {
Florin Coras2d379d82019-06-28 12:45:12 -07002052 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002053 if (vcl_session_is_closing (s))
2054 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002055 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002056 if (svm_msg_q_is_empty (mq))
2057 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002058
Florin Coras54693d22018-07-17 10:46:29 -07002059 svm_msg_q_sub_w_lock (mq, &msg);
2060 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002061 svm_msg_q_unlock (mq);
2062
Florin Coras0e88e852018-09-17 22:09:02 -07002063 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002064 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002065 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002066 }
Dave Wallace543852a2017-08-03 02:11:34 -04002067 }
Dave Wallace543852a2017-08-03 02:11:34 -04002068
Florin Coras653e43f2019-03-04 10:56:23 -08002069 et = SESSION_IO_EVT_TX;
2070 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002071 et = SESSION_IO_EVT_TX_FLUSH;
2072
Florin Coras460dce62018-07-27 05:45:06 -07002073 if (s->is_dgram)
2074 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002075 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002076 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002077 else
2078 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002079 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002080
Florin Coras14ed6df2019-03-06 21:13:42 -08002081 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08002082 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
2083 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002084
Florin Coras460dce62018-07-27 05:45:06 -07002085 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04002086
Florin Coras6d0106e2019-01-29 20:11:58 -08002087 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2088 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002089
Florin Coras54693d22018-07-17 10:46:29 -07002090 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002091}
2092
Florin Coras42ceddb2018-12-12 10:56:01 -08002093int
2094vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2095{
2096 return vppcom_session_write_inline (session_handle, buf, n,
2097 0 /* is_flush */ );
2098}
2099
Florin Corasb0f662f2018-12-27 14:51:46 -08002100int
2101vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2102{
2103 return vppcom_session_write_inline (session_handle, buf, n,
2104 1 /* is_flush */ );
2105}
2106
Florin Corasc0737e92019-03-04 14:19:39 -08002107#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002108if (PREDICT_FALSE (!_s->rx_fifo)) \
2109 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002110if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2111 { \
2112 if (!vcl_session_is_ct (_s)) \
2113 { \
2114 svm_fifo_unset_event (_s->rx_fifo); \
2115 if (svm_fifo_is_empty (_s->rx_fifo)) \
2116 break; \
2117 } \
2118 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2119 { \
2120 svm_fifo_unset_event (_s->ct_rx_fifo); \
2121 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2122 break; \
2123 } \
2124 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002125
Florin Coras86f04502018-09-12 16:08:01 -07002126static void
2127vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2128 unsigned long n_bits, unsigned long *read_map,
2129 unsigned long *write_map,
2130 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002131{
2132 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002133 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07002134 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002135 u32 sid;
2136
2137 switch (e->event_type)
2138 {
Florin Corasc0737e92019-03-04 14:19:39 -08002139 case SESSION_IO_EVT_RX:
2140 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002141 session = vcl_session_get (wrk, sid);
2142 if (!session)
2143 break;
Florin Corasfff68f72019-03-13 16:01:38 -07002144 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002145 if (sid < n_bits && read_map)
2146 {
David Johnsond9818dd2018-12-14 14:53:41 -05002147 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002148 *bits_set += 1;
2149 }
2150 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002151 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002152 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002153 session = vcl_session_get (wrk, sid);
2154 if (!session)
2155 break;
2156 if (sid < n_bits && write_map)
2157 {
David Johnsond9818dd2018-12-14 14:53:41 -05002158 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002159 *bits_set += 1;
2160 }
2161 break;
Florin Coras86f04502018-09-12 16:08:01 -07002162 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002163 session = vcl_session_accepted (wrk,
2164 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002165 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002166 break;
Florin Coras86f04502018-09-12 16:08:01 -07002167 sid = session->session_index;
2168 if (sid < n_bits && read_map)
2169 {
David Johnsond9818dd2018-12-14 14:53:41 -05002170 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002171 *bits_set += 1;
2172 }
2173 break;
2174 case SESSION_CTRL_EVT_CONNECTED:
2175 connected_msg = (session_connected_msg_t *) e->data;
Florin Coras57c88932019-08-29 12:03:17 -07002176 sid = vcl_session_connected_handler (wrk, connected_msg);
2177 if (sid == VCL_INVALID_SESSION_INDEX)
2178 break;
2179 if (sid < n_bits && write_map)
2180 {
2181 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2182 *bits_set += 1;
2183 }
Florin Coras86f04502018-09-12 16:08:01 -07002184 break;
2185 case SESSION_CTRL_EVT_DISCONNECTED:
2186 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002187 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2188 if (!session)
2189 break;
2190 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002191 if (sid < n_bits && except_map)
2192 {
David Johnsond9818dd2018-12-14 14:53:41 -05002193 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002194 *bits_set += 1;
2195 }
2196 break;
2197 case SESSION_CTRL_EVT_RESET:
2198 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2199 if (sid < n_bits && except_map)
2200 {
David Johnsond9818dd2018-12-14 14:53:41 -05002201 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002202 *bits_set += 1;
2203 }
2204 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002205 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2206 vcl_session_unlisten_reply_handler (wrk, e->data);
2207 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002208 case SESSION_CTRL_EVT_MIGRATED:
2209 vcl_session_migrated_handler (wrk, e->data);
2210 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002211 case SESSION_CTRL_EVT_CLEANUP:
2212 vcl_session_cleanup_handler (wrk, e->data);
2213 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002214 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2215 vcl_session_worker_update_reply_handler (wrk, e->data);
2216 break;
2217 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2218 vcl_session_req_worker_update_handler (wrk, e->data);
2219 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002220 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2221 vcl_session_app_add_segment_handler (wrk, e->data);
2222 break;
2223 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2224 vcl_session_app_del_segment_handler (wrk, e->data);
2225 break;
Florin Coras86f04502018-09-12 16:08:01 -07002226 default:
2227 clib_warning ("unhandled: %u", e->event_type);
2228 break;
2229 }
2230}
2231
2232static int
2233vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2234 unsigned long n_bits, unsigned long *read_map,
2235 unsigned long *write_map, unsigned long *except_map,
2236 double time_to_wait, u32 * bits_set)
2237{
Florin Coras99368312018-08-02 10:45:44 -07002238 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002239 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002240 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002241
2242 svm_msg_q_lock (mq);
2243 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002244 {
Florin Coras54693d22018-07-17 10:46:29 -07002245 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002246 {
Florin Coras54693d22018-07-17 10:46:29 -07002247 svm_msg_q_unlock (mq);
2248 return 0;
2249 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002250
Florin Coras54693d22018-07-17 10:46:29 -07002251 if (!time_to_wait)
2252 {
2253 svm_msg_q_unlock (mq);
2254 return 0;
2255 }
2256 else if (time_to_wait < 0)
2257 {
2258 svm_msg_q_wait (mq);
2259 }
2260 else
2261 {
2262 if (svm_msg_q_timedwait (mq, time_to_wait))
2263 {
2264 svm_msg_q_unlock (mq);
2265 return 0;
2266 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002267 }
2268 }
Florin Corase003a1b2019-06-05 10:47:16 -07002269 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002270 svm_msg_q_unlock (mq);
2271
Florin Coras134a9962018-08-28 11:32:04 -07002272 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002273 {
Florin Coras134a9962018-08-28 11:32:04 -07002274 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002275 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002276 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2277 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002278 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002279 }
Florin Coras134a9962018-08-28 11:32:04 -07002280 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002281 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002282 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002283}
2284
Florin Coras99368312018-08-02 10:45:44 -07002285static int
Florin Coras294afe22019-01-07 17:49:17 -08002286vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2287 vcl_si_set * read_map, vcl_si_set * write_map,
2288 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002289 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002290{
Florin Coras14ed6df2019-03-06 21:13:42 -08002291 double wait = 0, start = 0;
2292
2293 if (!*bits_set)
2294 {
2295 wait = time_to_wait;
2296 start = clib_time_now (&wrk->clib_time);
2297 }
2298
2299 do
2300 {
2301 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2302 write_map, except_map, wait, bits_set);
2303 if (*bits_set)
2304 return *bits_set;
2305 if (wait == -1)
2306 continue;
2307
2308 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2309 }
2310 while (wait > 0);
2311
2312 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002313}
2314
2315static int
Florin Coras294afe22019-01-07 17:49:17 -08002316vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2317 vcl_si_set * read_map, vcl_si_set * write_map,
2318 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002319 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002320{
2321 vcl_mq_evt_conn_t *mqc;
2322 int __clib_unused n_read;
2323 int n_mq_evts, i;
2324 u64 buf;
2325
Florin Coras134a9962018-08-28 11:32:04 -07002326 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2327 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2328 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002329 for (i = 0; i < n_mq_evts; i++)
2330 {
Florin Coras134a9962018-08-28 11:32:04 -07002331 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002332 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002333 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002334 except_map, 0, bits_set);
2335 }
2336
2337 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2338}
2339
Dave Wallace543852a2017-08-03 02:11:34 -04002340int
Florin Coras294afe22019-01-07 17:49:17 -08002341vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2342 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002343{
Florin Coras54693d22018-07-17 10:46:29 -07002344 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002345 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002346 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002347 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04002348
Dave Wallace7876d392017-10-19 03:53:57 -04002349 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002350 {
Florin Coras134a9962018-08-28 11:32:04 -07002351 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002352 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002353 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2354 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002355 }
Dave Wallace7876d392017-10-19 03:53:57 -04002356 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002357 {
Florin Coras134a9962018-08-28 11:32:04 -07002358 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002359 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002360 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2361 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002362 }
Dave Wallace7876d392017-10-19 03:53:57 -04002363 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002364 {
Florin Coras134a9962018-08-28 11:32:04 -07002365 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002366 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002367 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2368 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002369 }
2370
Florin Coras54693d22018-07-17 10:46:29 -07002371 if (!n_bits)
2372 return 0;
2373
2374 if (!write_map)
2375 goto check_rd;
2376
2377 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002378 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2379 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002380 {
Florin Coras47c40e22018-11-26 17:01:36 -08002381 if (except_map && sid < minbits)
2382 clib_bitmap_set_no_check (except_map, sid, 1);
2383 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002384 }
2385
Sirshak Das28aa5392019-02-05 01:33:33 -06002386 rv = svm_fifo_is_full_prod (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002387 if (!rv)
2388 {
David Johnsond9818dd2018-12-14 14:53:41 -05002389 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002390 bits_set++;
2391 }
Florin Coras294afe22019-01-07 17:49:17 -08002392 else
Florin Coras2d379d82019-06-28 12:45:12 -07002393 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002394 }));
2395
2396check_rd:
2397 if (!read_map)
2398 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002399
Florin Coras134a9962018-08-28 11:32:04 -07002400 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2401 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002402 {
Florin Coras47c40e22018-11-26 17:01:36 -08002403 if (except_map && sid < minbits)
2404 clib_bitmap_set_no_check (except_map, sid, 1);
2405 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002406 }
2407
Florin Coras0ef8ef22019-01-18 08:37:13 -08002408 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002409 if (rv)
2410 {
David Johnsond9818dd2018-12-14 14:53:41 -05002411 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002412 bits_set++;
2413 }
2414 }));
2415 /* *INDENT-ON* */
2416
2417check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002418
Florin Coras86f04502018-09-12 16:08:01 -07002419 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2420 {
2421 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2422 read_map, write_map, except_map, &bits_set);
2423 }
2424 vec_reset_length (wrk->unhandled_evts_vector);
2425
Florin Coras99368312018-08-02 10:45:44 -07002426 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002427 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002428 time_to_wait, &bits_set);
2429 else
Florin Coras134a9962018-08-28 11:32:04 -07002430 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002431 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002432
Dave Wallace543852a2017-08-03 02:11:34 -04002433 return (bits_set);
2434}
2435
Dave Wallacef7f809c2017-10-03 01:48:42 -04002436static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002437vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002438{
Florin Coras7e12d942018-06-27 14:32:43 -07002439 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002440 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002441 u32 sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002442
Florin Corasc0737e92019-03-04 14:19:39 -08002443 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002444 return;
2445
Florin Coras7e5e62b2019-07-30 14:08:23 -07002446 session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002447 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002448 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002449 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002450 goto done;
2451 }
2452 if (PREDICT_FALSE (!session->is_vep))
2453 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002454 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002455 goto done;
2456 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002457 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002458 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002459 "{\n"
2460 " is_vep = %u\n"
2461 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002462 " next_sh = 0x%x (%u)\n"
2463 "}\n", vep_handle, session->is_vep, session->is_vep_session,
Florin Coras5e062572019-03-14 19:07:51 -07002464 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002465
Florin Coras7e5e62b2019-07-30 14:08:23 -07002466 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002467 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002468 session = vcl_session_get_w_handle (wrk, sh);
Florin Coras070453d2018-08-24 17:04:27 -07002469 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002470 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002471 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002472 goto done;
2473 }
2474 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002475 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002476 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002477 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002478 else if (PREDICT_FALSE (!session->is_vep_session))
2479 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002480 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002481 goto done;
2482 }
2483 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002484 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2485 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
2486 sh, session->vep.vep_sh, vep_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002487 if (session->is_vep_session)
2488 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002489 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002490 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002491 " next_sh = 0x%x (%u)\n"
2492 " prev_sh = 0x%x (%u)\n"
2493 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002494 " ev.events = 0x%x\n"
2495 " ev.data.u64 = 0x%llx\n"
2496 " et_mask = 0x%x\n"
2497 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002498 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002499 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2500 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002501 }
2502 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002503
2504done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002505 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002506}
2507
2508int
2509vppcom_epoll_create (void)
2510{
Florin Coras134a9962018-08-28 11:32:04 -07002511 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002512 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002513
Florin Coras134a9962018-08-28 11:32:04 -07002514 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002515
2516 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002517 vep_session->vep.vep_sh = ~0;
2518 vep_session->vep.next_sh = ~0;
2519 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002520 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002521
Florin Corasa7a1a222018-12-30 17:11:31 -08002522 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2523 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002524
Florin Corasab2f6db2018-08-31 14:31:41 -07002525 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002526}
2527
2528int
Florin Coras134a9962018-08-28 11:32:04 -07002529vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002530 struct epoll_event *event)
2531{
Florin Coras134a9962018-08-28 11:32:04 -07002532 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002533 vcl_session_t *vep_session;
2534 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002535 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002536
Florin Coras134a9962018-08-28 11:32:04 -07002537 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002538 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002539 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002540 return VPPCOM_EINVAL;
2541 }
2542
Florin Coras134a9962018-08-28 11:32:04 -07002543 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002544 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002545 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002546 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002547 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002548 }
2549 if (PREDICT_FALSE (!vep_session->is_vep))
2550 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002551 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002552 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002553 }
2554
Florin Coras134a9962018-08-28 11:32:04 -07002555 ASSERT (vep_session->vep.vep_sh == ~0);
2556 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002557
Florin Coras134a9962018-08-28 11:32:04 -07002558 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002559 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002560 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002561 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002562 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002563 }
2564 if (PREDICT_FALSE (session->is_vep))
2565 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002566 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002567 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002568 }
2569
2570 switch (op)
2571 {
2572 case EPOLL_CTL_ADD:
2573 if (PREDICT_FALSE (!event))
2574 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002575 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002576 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002577 }
Florin Coras134a9962018-08-28 11:32:04 -07002578 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002579 {
Florin Coras7e12d942018-06-27 14:32:43 -07002580 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002581 next_session = vcl_session_get_w_handle (wrk,
2582 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002583 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002584 {
Florin Coras5e062572019-03-14 19:07:51 -07002585 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002586 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002587 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002588 }
Florin Coras134a9962018-08-28 11:32:04 -07002589 ASSERT (next_session->vep.prev_sh == vep_handle);
2590 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002591 }
Florin Coras134a9962018-08-28 11:32:04 -07002592 session->vep.next_sh = vep_session->vep.next_sh;
2593 session->vep.prev_sh = vep_handle;
2594 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002595 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2596 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002597 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002598 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002599 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002600
Florin Coras1bcad5c2019-01-09 20:04:38 -08002601 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002602 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2603 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002604
Florin Coras6e3c1f82020-01-15 01:30:46 +00002605 /* Generate EPOLLOUT if tx fifo not full */
hanlin475c9d72019-12-26 11:44:28 +08002606 if ((event->events & EPOLLOUT) &&
2607 (vcl_session_write_ready (session) > 0))
2608 {
2609 session_event_t e = { 0 };
2610 e.event_type = SESSION_IO_EVT_TX;
2611 e.session_index = session->session_index;
2612 vec_add1 (wrk->unhandled_evts_vector, e);
2613 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002614 /* Generate EPOLLIN if rx fifo has data */
2615 if ((event->events & EPOLLIN) && (vcl_session_read_ready (session) > 0))
2616 {
2617 session_event_t e = { 0 };
2618 e.event_type = SESSION_IO_EVT_RX;
2619 e.session_index = session->session_index;
2620 vec_add1 (wrk->unhandled_evts_vector, e);
2621 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002622 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2623 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002624 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002625 break;
2626
2627 case EPOLL_CTL_MOD:
2628 if (PREDICT_FALSE (!event))
2629 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002630 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002631 rv = VPPCOM_EINVAL;
2632 goto done;
2633 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002634 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002635 {
Florin Coras5e062572019-03-14 19:07:51 -07002636 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002637 rv = VPPCOM_EINVAL;
2638 goto done;
2639 }
Florin Coras134a9962018-08-28 11:32:04 -07002640 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002641 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002642 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2643 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002644 rv = VPPCOM_EINVAL;
2645 goto done;
2646 }
hanlin475c9d72019-12-26 11:44:28 +08002647
2648 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2649 if ((event->events & EPOLLOUT) &&
2650 !(session->vep.ev.events & EPOLLOUT) &&
2651 (vcl_session_write_ready (session) > 0))
2652 {
2653 session_event_t e = { 0 };
2654 e.event_type = SESSION_IO_EVT_TX;
2655 e.session_index = session->session_index;
2656 vec_add1 (wrk->unhandled_evts_vector, e);
2657 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002658 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2659 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002660 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2661 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002662 break;
2663
2664 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002665 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002666 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002667 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002668 rv = VPPCOM_EINVAL;
2669 goto done;
2670 }
Florin Coras134a9962018-08-28 11:32:04 -07002671 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002672 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002673 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2674 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675 rv = VPPCOM_EINVAL;
2676 goto done;
2677 }
2678
Florin Coras134a9962018-08-28 11:32:04 -07002679 if (session->vep.prev_sh == vep_handle)
2680 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002681 else
2682 {
Florin Coras7e12d942018-06-27 14:32:43 -07002683 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002684 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002685 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686 {
Florin Coras5e062572019-03-14 19:07:51 -07002687 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002688 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002689 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690 }
Florin Coras134a9962018-08-28 11:32:04 -07002691 ASSERT (prev_session->vep.next_sh == session_handle);
2692 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002693 }
Florin Coras134a9962018-08-28 11:32:04 -07002694 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002695 {
Florin Coras7e12d942018-06-27 14:32:43 -07002696 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002697 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002698 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002699 {
Florin Coras5e062572019-03-14 19:07:51 -07002700 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002701 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002702 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002703 }
Florin Coras134a9962018-08-28 11:32:04 -07002704 ASSERT (next_session->vep.prev_sh == session_handle);
2705 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002706 }
2707
2708 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002709 session->vep.next_sh = ~0;
2710 session->vep.prev_sh = ~0;
2711 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002712 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002713
2714 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002715 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002716
Florin Coras5e062572019-03-14 19:07:51 -07002717 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002718 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002719 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002720 break;
2721
2722 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002723 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002724 rv = VPPCOM_EINVAL;
2725 }
2726
Florin Coras134a9962018-08-28 11:32:04 -07002727 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002728
2729done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002730 return rv;
2731}
2732
Florin Coras86f04502018-09-12 16:08:01 -07002733static inline void
2734vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2735 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002736{
2737 session_disconnected_msg_t *disconnected_msg;
2738 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002739 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002740 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002741 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002742 u8 add_event = 0;
2743
2744 switch (e->event_type)
2745 {
Florin Coras653e43f2019-03-04 10:56:23 -08002746 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002747 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002748 if (!(session = vcl_session_get (wrk, sid)))
2749 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002750 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002751 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002752 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002753 break;
2754 add_event = 1;
2755 events[*num_ev].events |= EPOLLIN;
2756 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002757 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002758 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002759 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002760 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002761 if (!(session = vcl_session_get (wrk, sid)))
2762 break;
Florin Coras86f04502018-09-12 16:08:01 -07002763 session_events = session->vep.ev.events;
2764 if (!(EPOLLOUT & session_events))
2765 break;
2766 add_event = 1;
2767 events[*num_ev].events |= EPOLLOUT;
2768 session_evt_data = session->vep.ev.data.u64;
Florin Coras2d379d82019-06-28 12:45:12 -07002769 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002770 break;
Florin Coras86f04502018-09-12 16:08:01 -07002771 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002772 session = vcl_session_accepted (wrk,
2773 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002774 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002775 break;
Florin Coras86f04502018-09-12 16:08:01 -07002776
Florin Coras86f04502018-09-12 16:08:01 -07002777 session_events = session->vep.ev.events;
2778 if (!(EPOLLIN & session_events))
2779 break;
2780
2781 add_event = 1;
2782 events[*num_ev].events |= EPOLLIN;
2783 session_evt_data = session->vep.ev.data.u64;
2784 break;
2785 case SESSION_CTRL_EVT_CONNECTED:
2786 connected_msg = (session_connected_msg_t *) e->data;
Florin Corasf1653e62019-11-06 15:41:37 -08002787 sid = vcl_session_connected_handler (wrk, connected_msg);
Florin Coras86f04502018-09-12 16:08:01 -07002788 /* Generate EPOLLOUT because there's no connected event */
Florin Corasfa915f82018-12-26 16:29:06 -08002789 if (!(session = vcl_session_get (wrk, sid)))
2790 break;
Florin Coras86f04502018-09-12 16:08:01 -07002791 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002792 if (!(EPOLLOUT & session_events))
2793 break;
2794 add_event = 1;
2795 events[*num_ev].events |= EPOLLOUT;
2796 session_evt_data = session->vep.ev.data.u64;
Florin Corasadcfb152020-02-12 08:50:29 +00002797 if (session->session_state & STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002798 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002799 break;
2800 case SESSION_CTRL_EVT_DISCONNECTED:
2801 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002802 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2803 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002804 break;
Florin Coras72f77822019-01-22 19:05:52 -08002805 session_events = session->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002806 add_event = 1;
2807 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2808 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002809 break;
2810 case SESSION_CTRL_EVT_RESET:
2811 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2812 if (!(session = vcl_session_get (wrk, sid)))
2813 break;
Florin Coras72f77822019-01-22 19:05:52 -08002814 session_events = session->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002815 add_event = 1;
2816 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2817 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002818 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002819 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2820 vcl_session_unlisten_reply_handler (wrk, e->data);
2821 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002822 case SESSION_CTRL_EVT_MIGRATED:
2823 vcl_session_migrated_handler (wrk, e->data);
2824 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002825 case SESSION_CTRL_EVT_CLEANUP:
2826 vcl_session_cleanup_handler (wrk, e->data);
2827 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002828 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2829 vcl_session_req_worker_update_handler (wrk, e->data);
2830 break;
2831 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2832 vcl_session_worker_update_reply_handler (wrk, e->data);
2833 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002834 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2835 vcl_session_app_add_segment_handler (wrk, e->data);
2836 break;
2837 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2838 vcl_session_app_del_segment_handler (wrk, e->data);
2839 break;
Florin Coras86f04502018-09-12 16:08:01 -07002840 default:
2841 VDBG (0, "unhandled: %u", e->event_type);
2842 break;
2843 }
2844
2845 if (add_event)
2846 {
2847 events[*num_ev].data.u64 = session_evt_data;
2848 if (EPOLLONESHOT & session_events)
2849 {
2850 session = vcl_session_get (wrk, sid);
2851 session->vep.ev.events = 0;
2852 }
2853 *num_ev += 1;
2854 }
2855}
2856
2857static int
2858vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2859 struct epoll_event *events, u32 maxevents,
2860 double wait_for_time, u32 * num_ev)
2861{
Florin Coras99368312018-08-02 10:45:44 -07002862 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002863 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002864 int i;
2865
Florin Coras539663c2018-09-28 14:59:37 -07002866 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2867 goto handle_dequeued;
2868
Florin Coras54693d22018-07-17 10:46:29 -07002869 svm_msg_q_lock (mq);
2870 if (svm_msg_q_is_empty (mq))
2871 {
2872 if (!wait_for_time)
2873 {
2874 svm_msg_q_unlock (mq);
2875 return 0;
2876 }
2877 else if (wait_for_time < 0)
2878 {
2879 svm_msg_q_wait (mq);
2880 }
2881 else
2882 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002883 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002884 {
2885 svm_msg_q_unlock (mq);
2886 return 0;
2887 }
2888 }
2889 }
Florin Corase003a1b2019-06-05 10:47:16 -07002890 ASSERT (maxevents > *num_ev);
2891 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
Florin Coras54693d22018-07-17 10:46:29 -07002892 svm_msg_q_unlock (mq);
2893
Florin Coras539663c2018-09-28 14:59:37 -07002894handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002895 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002896 {
Florin Coras134a9962018-08-28 11:32:04 -07002897 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002898 e = svm_msg_q_msg_data (mq, msg);
Florin Corase003a1b2019-06-05 10:47:16 -07002899 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
Florin Coras99368312018-08-02 10:45:44 -07002900 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002901 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002902 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002903 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002904 return *num_ev;
2905}
2906
Florin Coras99368312018-08-02 10:45:44 -07002907static int
Florin Coras134a9962018-08-28 11:32:04 -07002908vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002909 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002910{
Florin Corase003a1b2019-06-05 10:47:16 -07002911 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002912
2913 if (!n_evts)
2914 {
2915 wait = wait_for_time;
2916 start = clib_time_now (&wrk->clib_time);
2917 }
2918
2919 do
2920 {
2921 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2922 wait, &n_evts);
2923 if (n_evts)
2924 return n_evts;
2925 if (wait == -1)
2926 continue;
2927
Florin Corase003a1b2019-06-05 10:47:16 -07002928 now = clib_time_now (&wrk->clib_time);
2929 wait -= now - start;
2930 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002931 }
2932 while (wait > 0);
2933
2934 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002935}
2936
2937static int
Florin Coras134a9962018-08-28 11:32:04 -07002938vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002939 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002940{
2941 vcl_mq_evt_conn_t *mqc;
2942 int __clib_unused n_read;
2943 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002944 u64 buf;
2945
Florin Coras134a9962018-08-28 11:32:04 -07002946 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002947again:
Florin Coras134a9962018-08-28 11:32:04 -07002948 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2949 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002950 for (i = 0; i < n_mq_evts; i++)
2951 {
Florin Coras134a9962018-08-28 11:32:04 -07002952 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002953 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002954 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002955 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002956 if (!n_evts && n_mq_evts > 0)
2957 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002958
2959 return (int) n_evts;
2960}
2961
Dave Wallacef7f809c2017-10-03 01:48:42 -04002962int
Florin Coras134a9962018-08-28 11:32:04 -07002963vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002964 int maxevents, double wait_for_time)
2965{
Florin Coras134a9962018-08-28 11:32:04 -07002966 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002967 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002968 u32 n_evts = 0;
2969 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002970
2971 if (PREDICT_FALSE (maxevents <= 0))
2972 {
Florin Coras5e062572019-03-14 19:07:51 -07002973 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002974 return VPPCOM_EINVAL;
2975 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002976
Florin Coras134a9962018-08-28 11:32:04 -07002977 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002978 if (!vep_session)
2979 return VPPCOM_EBADFD;
2980
Florin Coras54693d22018-07-17 10:46:29 -07002981 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002982 {
Florin Coras5e062572019-03-14 19:07:51 -07002983 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002984 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002985 }
Florin Coras54693d22018-07-17 10:46:29 -07002986
2987 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002988
Florin Coras86f04502018-09-12 16:08:01 -07002989 if (vec_len (wrk->unhandled_evts_vector))
2990 {
2991 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2992 {
2993 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2994 events, &n_evts);
2995 if (n_evts == maxevents)
2996 {
Florin Corase003a1b2019-06-05 10:47:16 -07002997 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2998 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07002999 }
3000 }
Florin Corase003a1b2019-06-05 10:47:16 -07003001 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003002 }
3003
3004 if (vcm->cfg.use_mq_eventfd)
3005 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3006 wait_for_time);
3007
3008 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3009 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003010}
3011
Dave Wallace35830af2017-10-09 01:43:42 -04003012int
Florin Coras134a9962018-08-28 11:32:04 -07003013vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003014 void *buffer, uint32_t * buflen)
3015{
Florin Coras134a9962018-08-28 11:32:04 -07003016 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003017 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04003018 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08003019 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003020 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003021
Florin Coras134a9962018-08-28 11:32:04 -07003022 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003023 if (!session)
3024 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003025
Dave Wallace35830af2017-10-09 01:43:42 -04003026 switch (op)
3027 {
3028 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003029 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003030 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3031 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003032 break;
3033
Dave Wallace227867f2017-11-13 21:21:53 -05003034 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003035 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003036 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3037 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003038 break;
3039
3040 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003041 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003042 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003043 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
3044 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003045 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003046 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3047 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07003048 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003049 }
3050 else
3051 rv = VPPCOM_EINVAL;
3052 break;
3053
3054 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003055 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003056 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003057 if (*flags & O_NONBLOCK)
3058 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
3059 else
3060 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
3061
Florin Coras5e062572019-03-14 19:07:51 -07003062 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3063 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07003064 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003065 }
3066 else
3067 rv = VPPCOM_EINVAL;
3068 break;
3069
3070 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003071 if (PREDICT_TRUE (buffer && buflen &&
3072 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003073 {
Florin Coras7e12d942018-06-27 14:32:43 -07003074 ep->is_ip4 = session->transport.is_ip4;
3075 ep->port = session->transport.rmt_port;
3076 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003077 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3078 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003079 else
Dave Barach178cf492018-11-13 16:34:13 -05003080 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3081 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003082 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003083 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3084 "addr = %U, port %u", session_handle, ep->is_ip4,
3085 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003086 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3087 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003088 }
3089 else
3090 rv = VPPCOM_EINVAL;
3091 break;
3092
3093 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003094 if (PREDICT_TRUE (buffer && buflen &&
3095 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003096 {
Florin Coras7e12d942018-06-27 14:32:43 -07003097 ep->is_ip4 = session->transport.is_ip4;
3098 ep->port = session->transport.lcl_port;
3099 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003100 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3101 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003102 else
Dave Barach178cf492018-11-13 16:34:13 -05003103 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3104 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003105 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003106 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3107 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003108 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003109 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3110 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003111 }
3112 else
3113 rv = VPPCOM_EINVAL;
3114 break;
Stevenb5a11602017-10-11 09:59:30 -07003115
Florin Corasef7cbf62019-10-17 09:56:27 -07003116 case VPPCOM_ATTR_SET_LCL_ADDR:
3117 if (PREDICT_TRUE (buffer && buflen &&
3118 (*buflen >= sizeof (*ep)) && ep->ip))
3119 {
3120 session->transport.is_ip4 = ep->is_ip4;
3121 session->transport.lcl_port = ep->port;
3122 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3123 *buflen = sizeof (*ep);
3124 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3125 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3126 &session->transport.lcl_ip,
3127 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3128 clib_net_to_host_u16 (ep->port));
3129 }
3130 else
3131 rv = VPPCOM_EINVAL;
3132 break;
3133
Dave Wallace048b1d62018-01-03 22:24:41 -05003134 case VPPCOM_ATTR_GET_LIBC_EPFD:
3135 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003136 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003137 break;
3138
3139 case VPPCOM_ATTR_SET_LIBC_EPFD:
3140 if (PREDICT_TRUE (buffer && buflen &&
3141 (*buflen == sizeof (session->libc_epfd))))
3142 {
3143 session->libc_epfd = *(int *) buffer;
3144 *buflen = sizeof (session->libc_epfd);
3145
Florin Coras5e062572019-03-14 19:07:51 -07003146 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3147 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003148 }
3149 else
3150 rv = VPPCOM_EINVAL;
3151 break;
3152
3153 case VPPCOM_ATTR_GET_PROTOCOL:
3154 if (buffer && buflen && (*buflen >= sizeof (int)))
3155 {
Florin Coras7e12d942018-06-27 14:32:43 -07003156 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003157 *buflen = sizeof (int);
3158
Florin Coras5e062572019-03-14 19:07:51 -07003159 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3160 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003161 }
3162 else
3163 rv = VPPCOM_EINVAL;
3164 break;
3165
3166 case VPPCOM_ATTR_GET_LISTEN:
3167 if (buffer && buflen && (*buflen >= sizeof (int)))
3168 {
3169 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3170 VCL_SESS_ATTR_LISTEN);
3171 *buflen = sizeof (int);
3172
Florin Coras5e062572019-03-14 19:07:51 -07003173 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3174 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003175 }
3176 else
3177 rv = VPPCOM_EINVAL;
3178 break;
3179
3180 case VPPCOM_ATTR_GET_ERROR:
3181 if (buffer && buflen && (*buflen >= sizeof (int)))
3182 {
3183 *(int *) buffer = 0;
3184 *buflen = sizeof (int);
3185
Florin Coras5e062572019-03-14 19:07:51 -07003186 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3187 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003188 }
3189 else
3190 rv = VPPCOM_EINVAL;
3191 break;
3192
3193 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3194 if (buffer && buflen && (*buflen >= sizeof (u32)))
3195 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003196
3197 /* VPP-TBD */
3198 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003199 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05003200 vcm->cfg.tx_fifo_size);
3201 *buflen = sizeof (u32);
3202
Florin Coras5e062572019-03-14 19:07:51 -07003203 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3204 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3205 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003206 }
3207 else
3208 rv = VPPCOM_EINVAL;
3209 break;
3210
3211 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3212 if (buffer && buflen && (*buflen == sizeof (u32)))
3213 {
3214 /* VPP-TBD */
3215 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003216 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3217 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3218 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003219 }
3220 else
3221 rv = VPPCOM_EINVAL;
3222 break;
3223
3224 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3225 if (buffer && buflen && (*buflen >= sizeof (u32)))
3226 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003227
3228 /* VPP-TBD */
3229 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003230 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05003231 vcm->cfg.rx_fifo_size);
3232 *buflen = sizeof (u32);
3233
Florin Coras5e062572019-03-14 19:07:51 -07003234 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3235 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003236 }
3237 else
3238 rv = VPPCOM_EINVAL;
3239 break;
3240
3241 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3242 if (buffer && buflen && (*buflen == sizeof (u32)))
3243 {
3244 /* VPP-TBD */
3245 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003246 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3247 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3248 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003249 }
3250 else
3251 rv = VPPCOM_EINVAL;
3252 break;
3253
3254 case VPPCOM_ATTR_GET_REUSEADDR:
3255 if (buffer && buflen && (*buflen >= sizeof (int)))
3256 {
3257 /* VPP-TBD */
3258 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3259 VCL_SESS_ATTR_REUSEADDR);
3260 *buflen = sizeof (int);
3261
Florin Coras5e062572019-03-14 19:07:51 -07003262 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3263 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 }
3265 else
3266 rv = VPPCOM_EINVAL;
3267 break;
3268
Stevenb5a11602017-10-11 09:59:30 -07003269 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003270 if (buffer && buflen && (*buflen == sizeof (int)) &&
3271 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3272 {
3273 /* VPP-TBD */
3274 if (*(int *) buffer)
3275 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3276 else
3277 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3278
Florin Coras5e062572019-03-14 19:07:51 -07003279 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3280 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
3281 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003282 }
3283 else
3284 rv = VPPCOM_EINVAL;
3285 break;
3286
3287 case VPPCOM_ATTR_GET_REUSEPORT:
3288 if (buffer && buflen && (*buflen >= sizeof (int)))
3289 {
3290 /* VPP-TBD */
3291 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3292 VCL_SESS_ATTR_REUSEPORT);
3293 *buflen = sizeof (int);
3294
Florin Coras5e062572019-03-14 19:07:51 -07003295 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3296 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003297 }
3298 else
3299 rv = VPPCOM_EINVAL;
3300 break;
3301
3302 case VPPCOM_ATTR_SET_REUSEPORT:
3303 if (buffer && buflen && (*buflen == sizeof (int)) &&
3304 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3305 {
3306 /* VPP-TBD */
3307 if (*(int *) buffer)
3308 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3309 else
3310 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3311
Florin Coras5e062572019-03-14 19:07:51 -07003312 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3313 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3314 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003315 }
3316 else
3317 rv = VPPCOM_EINVAL;
3318 break;
3319
3320 case VPPCOM_ATTR_GET_BROADCAST:
3321 if (buffer && buflen && (*buflen >= sizeof (int)))
3322 {
3323 /* VPP-TBD */
3324 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3325 VCL_SESS_ATTR_BROADCAST);
3326 *buflen = sizeof (int);
3327
Florin Coras5e062572019-03-14 19:07:51 -07003328 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3329 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003330 }
3331 else
3332 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003333 break;
3334
3335 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003336 if (buffer && buflen && (*buflen == sizeof (int)))
3337 {
3338 /* VPP-TBD */
3339 if (*(int *) buffer)
3340 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3341 else
3342 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3343
Florin Coras5e062572019-03-14 19:07:51 -07003344 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3345 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3346 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003347 }
3348 else
3349 rv = VPPCOM_EINVAL;
3350 break;
3351
3352 case VPPCOM_ATTR_GET_V6ONLY:
3353 if (buffer && buflen && (*buflen >= sizeof (int)))
3354 {
3355 /* VPP-TBD */
3356 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3357 VCL_SESS_ATTR_V6ONLY);
3358 *buflen = sizeof (int);
3359
Florin Coras5e062572019-03-14 19:07:51 -07003360 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3361 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003362 }
3363 else
3364 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003365 break;
3366
3367 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003368 if (buffer && buflen && (*buflen == sizeof (int)))
3369 {
3370 /* VPP-TBD */
3371 if (*(int *) buffer)
3372 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3373 else
3374 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3375
Florin Coras5e062572019-03-14 19:07:51 -07003376 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3377 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3378 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003379 }
3380 else
3381 rv = VPPCOM_EINVAL;
3382 break;
3383
3384 case VPPCOM_ATTR_GET_KEEPALIVE:
3385 if (buffer && buflen && (*buflen >= sizeof (int)))
3386 {
3387 /* VPP-TBD */
3388 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3389 VCL_SESS_ATTR_KEEPALIVE);
3390 *buflen = sizeof (int);
3391
Florin Coras5e062572019-03-14 19:07:51 -07003392 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %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;
Stevenbccd3392017-10-12 20:42:21 -07003398
3399 case VPPCOM_ATTR_SET_KEEPALIVE:
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_KEEPALIVE);
3405 else
3406 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3407
Florin Coras5e062572019-03-14 19:07:51 -07003408 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3409 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3410 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 }
3412 else
3413 rv = VPPCOM_EINVAL;
3414 break;
3415
3416 case VPPCOM_ATTR_GET_TCP_NODELAY:
3417 if (buffer && buflen && (*buflen >= sizeof (int)))
3418 {
3419 /* VPP-TBD */
3420 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3421 VCL_SESS_ATTR_TCP_NODELAY);
3422 *buflen = sizeof (int);
3423
Florin Coras5e062572019-03-14 19:07:51 -07003424 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3425 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003426 }
3427 else
3428 rv = VPPCOM_EINVAL;
3429 break;
3430
3431 case VPPCOM_ATTR_SET_TCP_NODELAY:
3432 if (buffer && buflen && (*buflen == sizeof (int)))
3433 {
3434 /* VPP-TBD */
3435 if (*(int *) buffer)
3436 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3437 else
3438 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3439
Florin Coras5e062572019-03-14 19:07:51 -07003440 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3441 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3442 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003443 }
3444 else
3445 rv = VPPCOM_EINVAL;
3446 break;
3447
3448 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3449 if (buffer && buflen && (*buflen >= sizeof (int)))
3450 {
3451 /* VPP-TBD */
3452 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3453 VCL_SESS_ATTR_TCP_KEEPIDLE);
3454 *buflen = sizeof (int);
3455
Florin Coras5e062572019-03-14 19:07:51 -07003456 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3457 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003458 }
3459 else
3460 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003461 break;
3462
3463 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
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_TCP_KEEPIDLE);
3469 else
3470 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3471
Florin Coras5e062572019-03-14 19:07:51 -07003472 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003473 VCL_SESS_ATTR_TEST (session->attr,
3474 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 }
3476 else
3477 rv = VPPCOM_EINVAL;
3478 break;
3479
3480 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
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_KEEPINTVL);
3486 *buflen = sizeof (int);
3487
Florin Coras5e062572019-03-14 19:07:51 -07003488 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3489 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 }
3491 else
3492 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003493 break;
3494
3495 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 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_KEEPINTVL);
3501 else
3502 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3503
Florin Coras5e062572019-03-14 19:07:51 -07003504 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003505 VCL_SESS_ATTR_TEST (session->attr,
3506 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 }
3508 else
3509 rv = VPPCOM_EINVAL;
3510 break;
3511
3512 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3513 if (buffer && buflen && (*buflen >= sizeof (u32)))
3514 {
3515 /* VPP-TBD */
3516 *(u32 *) buffer = session->user_mss;
3517 *buflen = sizeof (int);
3518
Florin Coras5e062572019-03-14 19:07:51 -07003519 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3520 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003521 }
3522 else
3523 rv = VPPCOM_EINVAL;
3524 break;
3525
3526 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3527 if (buffer && buflen && (*buflen == sizeof (u32)))
3528 {
3529 /* VPP-TBD */
3530 session->user_mss = *(u32 *) buffer;
3531
Florin Coras5e062572019-03-14 19:07:51 -07003532 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3533 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003534 }
3535 else
3536 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003537 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003538
Florin Coras7baeb712019-01-04 17:05:43 -08003539 case VPPCOM_ATTR_SET_SHUT:
3540 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3541 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3542 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3543 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3544 break;
3545
3546 case VPPCOM_ATTR_GET_SHUT:
3547 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3548 tmp_flags = 1;
3549 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3550 tmp_flags |= 2;
3551 if (tmp_flags == 1)
3552 *(int *) buffer = SHUT_RD;
3553 else if (tmp_flags == 2)
3554 *(int *) buffer = SHUT_WR;
3555 else if (tmp_flags == 3)
3556 *(int *) buffer = SHUT_RDWR;
3557 *buflen = sizeof (int);
3558 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003559 default:
3560 rv = VPPCOM_EINVAL;
3561 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003562 }
3563
Dave Wallace35830af2017-10-09 01:43:42 -04003564 return rv;
3565}
3566
Stevenac1f96d2017-10-24 16:03:58 -07003567int
Florin Coras134a9962018-08-28 11:32:04 -07003568vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003569 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3570{
Florin Coras134a9962018-08-28 11:32:04 -07003571 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003572 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003573 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003574
3575 if (ep)
3576 {
Florin Coras134a9962018-08-28 11:32:04 -07003577 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003578 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003579 {
Florin Coras5e062572019-03-14 19:07:51 -07003580 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003581 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003582 }
Florin Coras7e12d942018-06-27 14:32:43 -07003583 ep->is_ip4 = session->transport.is_ip4;
3584 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003585 }
Steven58f464e2017-10-25 12:33:12 -07003586
3587 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003588 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003589 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003590 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003591 else
3592 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003593 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003594 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003595 }
3596
Florin Coras99368312018-08-02 10:45:44 -07003597 if (ep)
3598 {
3599 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003600 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3601 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003602 else
Dave Barach178cf492018-11-13 16:34:13 -05003603 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3604 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003605 }
Florin Coras460dce62018-07-27 05:45:06 -07003606
Stevenac1f96d2017-10-24 16:03:58 -07003607 return rv;
3608}
3609
3610int
Florin Coras134a9962018-08-28 11:32:04 -07003611vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003612 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3613{
Dave Wallace617dffa2017-10-26 14:47:06 -04003614 if (!buffer)
3615 return VPPCOM_EINVAL;
3616
3617 if (ep)
3618 {
3619 // TBD
3620 return VPPCOM_EINVAL;
3621 }
3622
3623 if (flags)
3624 {
3625 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003626 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003627 }
3628
Florin Coras42ceddb2018-12-12 10:56:01 -08003629 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003630}
3631
Dave Wallace048b1d62018-01-03 22:24:41 -05003632int
3633vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3634{
Florin Coras134a9962018-08-28 11:32:04 -07003635 vcl_worker_t *wrk = vcl_worker_get_current ();
3636 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003637 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003638 svm_msg_q_msg_t msg;
3639 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003640 int rv, num_ev = 0;
3641
Florin Coras5e062572019-03-14 19:07:51 -07003642 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003643
3644 if (!vp)
3645 return VPPCOM_EFAULT;
3646
3647 do
3648 {
Florin Coras7e12d942018-06-27 14:32:43 -07003649 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003650
Florin Coras6917b942018-11-13 22:44:54 -08003651 /* Dequeue all events and drop all unhandled io events */
3652 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3653 {
3654 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3655 vcl_handle_mq_event (wrk, e);
3656 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3657 }
3658 vec_reset_length (wrk->unhandled_evts_vector);
3659
Dave Wallace048b1d62018-01-03 22:24:41 -05003660 for (i = 0; i < n_sids; i++)
3661 {
Florin Coras7baeb712019-01-04 17:05:43 -08003662 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003663 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003664 {
3665 vp[i].revents = POLLHUP;
3666 num_ev++;
3667 continue;
3668 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003669
Florin Coras6917b942018-11-13 22:44:54 -08003670 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003671
3672 if (POLLIN & vp[i].events)
3673 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003674 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003675 if (rv > 0)
3676 {
Florin Coras6917b942018-11-13 22:44:54 -08003677 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003678 num_ev++;
3679 }
3680 else if (rv < 0)
3681 {
3682 switch (rv)
3683 {
3684 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003685 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003686 break;
3687
3688 default:
Florin Coras6917b942018-11-13 22:44:54 -08003689 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003690 break;
3691 }
3692 num_ev++;
3693 }
3694 }
3695
3696 if (POLLOUT & vp[i].events)
3697 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003698 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003699 if (rv > 0)
3700 {
Florin Coras6917b942018-11-13 22:44:54 -08003701 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003702 num_ev++;
3703 }
3704 else if (rv < 0)
3705 {
3706 switch (rv)
3707 {
3708 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003709 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003710 break;
3711
3712 default:
Florin Coras6917b942018-11-13 22:44:54 -08003713 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003714 break;
3715 }
3716 num_ev++;
3717 }
3718 }
3719
Dave Wallace7e607a72018-06-18 18:41:32 -04003720 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003721 {
Florin Coras6917b942018-11-13 22:44:54 -08003722 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003723 num_ev++;
3724 }
3725 }
3726 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003727 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003728 }
3729 while ((num_ev == 0) && keep_trying);
3730
Dave Wallace048b1d62018-01-03 22:24:41 -05003731 return num_ev;
3732}
3733
Florin Coras99368312018-08-02 10:45:44 -07003734int
3735vppcom_mq_epoll_fd (void)
3736{
Florin Coras134a9962018-08-28 11:32:04 -07003737 vcl_worker_t *wrk = vcl_worker_get_current ();
3738 return wrk->mqs_epfd;
3739}
3740
3741int
Florin Coras30e79c22019-01-02 19:31:22 -08003742vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003743{
3744 return session_handle & 0xFFFFFF;
3745}
3746
3747int
Florin Coras30e79c22019-01-02 19:31:22 -08003748vppcom_session_worker (vcl_session_handle_t session_handle)
3749{
3750 return session_handle >> 24;
3751}
3752
3753int
Florin Coras134a9962018-08-28 11:32:04 -07003754vppcom_worker_register (void)
3755{
Florin Corasd4c70922019-11-28 14:21:21 -08003756 vcl_worker_t *wrk;
3757 u8 *wrk_name = 0;
3758 int rv;
3759
Florin Coras47c40e22018-11-26 17:01:36 -08003760 if (!vcl_worker_alloc_and_init ())
3761 return VPPCOM_EEXIST;
3762
Florin Corasd4c70922019-11-28 14:21:21 -08003763 wrk = vcl_worker_get_current ();
3764 wrk_name = format (0, "%s-wrk-%u", vcm->app_name, wrk->wrk_index);
3765
3766 rv = vppcom_connect_to_vpp ((char *) wrk_name);
3767 vec_free (wrk_name);
3768
3769 if (rv)
3770 return VPPCOM_EFAULT;
Florin Coras47c40e22018-11-26 17:01:36 -08003771
3772 if (vcl_worker_register_with_vpp ())
3773 return VPPCOM_EEXIST;
3774
3775 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003776}
3777
Florin Coras369db832019-07-08 12:34:45 -07003778void
3779vppcom_worker_unregister (void)
3780{
3781 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3782 vcl_set_worker_index (~0);
3783}
3784
Florin Corasdfe4cf42018-11-28 22:13:45 -08003785int
3786vppcom_worker_index (void)
3787{
3788 return vcl_get_worker_index ();
3789}
3790
Florin Corase0982e52019-01-25 13:19:56 -08003791int
3792vppcom_worker_mqs_epfd (void)
3793{
3794 vcl_worker_t *wrk = vcl_worker_get_current ();
3795 if (!vcm->cfg.use_mq_eventfd)
3796 return -1;
3797 return wrk->mqs_epfd;
3798}
3799
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003800int
3801vppcom_session_is_connectable_listener (uint32_t session_handle)
3802{
3803 vcl_session_t *session;
3804 vcl_worker_t *wrk = vcl_worker_get_current ();
3805 session = vcl_session_get_w_handle (wrk, session_handle);
3806 if (!session)
3807 return VPPCOM_EBADFD;
3808 return vcl_session_is_connectable_listener (wrk, session);
3809}
3810
3811int
3812vppcom_session_listener (uint32_t session_handle)
3813{
3814 vcl_worker_t *wrk = vcl_worker_get_current ();
3815 vcl_session_t *listen_session, *session;
3816 session = vcl_session_get_w_handle (wrk, session_handle);
3817 if (!session)
3818 return VPPCOM_EBADFD;
3819 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3820 return VPPCOM_EBADFD;
3821 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3822 if (!listen_session)
3823 return VPPCOM_EBADFD;
3824 return vcl_session_handle (listen_session);
3825}
3826
3827int
3828vppcom_session_n_accepted (uint32_t session_handle)
3829{
3830 vcl_worker_t *wrk = vcl_worker_get_current ();
3831 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3832 if (!session)
3833 return VPPCOM_EBADFD;
3834 return session->n_accepted_sessions;
3835}
3836
Dave Wallacee22aa742017-10-20 12:30:38 -04003837/*
3838 * fd.io coding-style-patch-verification: ON
3839 *
3840 * Local Variables:
3841 * eval: (c-set-style "gnu")
3842 * End:
3843 */