blob: f95e72fc94e973e27e27b6e37311b4e9fc85fccc [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallace33e002b2017-09-06 01:20:02 -04002 * Copyright (c) 2017 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 Wallace543852a2017-08-03 02:11:34 -040018#include <svm/svm_fifo_segment.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040019#include <vcl/vppcom.h>
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080020#include <vcl/vcl_event.h>
Florin Coras0d427d82018-06-27 03:24:07 -070021#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070022#include <vcl/vcl_private.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040023
Florin Coras54693d22018-07-17 10:46:29 -070024static u8 not_ready;
25
26void
27sigsegv_signal (int signum)
28{
29 not_ready = 1;
30}
31
32static void
33vcl_wait_for_memory (void *mem)
34{
35 u8 __clib_unused test;
Florin Coras460dce62018-07-27 05:45:06 -070036 if (vcm->mounting_segment)
37 {
38 while (vcm->mounting_segment)
39 ;
40 return;
41 }
Florin Coras54693d22018-07-17 10:46:29 -070042 if (1 || vcm->debug)
43 {
Florin Coras460dce62018-07-27 05:45:06 -070044 usleep (1e5);
Florin Coras54693d22018-07-17 10:46:29 -070045 return;
46 }
47 if (signal (SIGSEGV, sigsegv_signal))
48 {
49 perror ("signal()");
50 return;
51 }
52 not_ready = 0;
53
54again:
55 test = *(u8 *) mem;
56 if (not_ready)
57 {
58 not_ready = 0;
59 usleep (1);
60 goto again;
61 }
62
63 signal (SIGSEGV, SIG_DFL);
64}
65
Dave Wallace543852a2017-08-03 02:11:34 -040066static const char *
67vppcom_app_state_str (app_state_t state)
68{
69 char *st;
70
71 switch (state)
72 {
73 case STATE_APP_START:
74 st = "STATE_APP_START";
75 break;
76
77 case STATE_APP_CONN_VPP:
78 st = "STATE_APP_CONN_VPP";
79 break;
80
81 case STATE_APP_ENABLED:
82 st = "STATE_APP_ENABLED";
83 break;
84
85 case STATE_APP_ATTACHED:
86 st = "STATE_APP_ATTACHED";
87 break;
88
89 default:
90 st = "UNKNOWN_APP_STATE";
91 break;
92 }
93
94 return st;
95}
96
Florin Coras697faea2018-06-27 17:10:49 -070097const char *
Dave Wallace543852a2017-08-03 02:11:34 -040098vppcom_session_state_str (session_state_t state)
99{
100 char *st;
101
102 switch (state)
103 {
104 case STATE_START:
105 st = "STATE_START";
106 break;
107
108 case STATE_CONNECT:
109 st = "STATE_CONNECT";
110 break;
111
112 case STATE_LISTEN:
113 st = "STATE_LISTEN";
114 break;
115
116 case STATE_ACCEPT:
117 st = "STATE_ACCEPT";
118 break;
119
Dave Wallace4878cbe2017-11-21 03:45:09 -0500120 case STATE_CLOSE_ON_EMPTY:
121 st = "STATE_CLOSE_ON_EMPTY";
122 break;
123
Dave Wallace543852a2017-08-03 02:11:34 -0400124 case STATE_DISCONNECT:
125 st = "STATE_DISCONNECT";
126 break;
127
128 case STATE_FAILED:
129 st = "STATE_FAILED";
130 break;
131
132 default:
133 st = "UNKNOWN_STATE";
134 break;
135 }
136
137 return st;
138}
139
Dave Wallace543852a2017-08-03 02:11:34 -0400140u8 *
141format_ip4_address (u8 * s, va_list * args)
142{
143 u8 *a = va_arg (*args, u8 *);
144 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
145}
146
147u8 *
148format_ip6_address (u8 * s, va_list * args)
149{
150 ip6_address_t *a = va_arg (*args, ip6_address_t *);
151 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
152
153 i_max_n_zero = ARRAY_LEN (a->as_u16);
154 max_n_zeros = 0;
155 i_first_zero = i_max_n_zero;
156 n_zeros = 0;
157 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
158 {
159 u32 is_zero = a->as_u16[i] == 0;
160 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
161 {
162 i_first_zero = i;
163 n_zeros = 0;
164 }
165 n_zeros += is_zero;
166 if ((!is_zero && n_zeros > max_n_zeros)
167 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
168 {
169 i_max_n_zero = i_first_zero;
170 max_n_zeros = n_zeros;
171 i_first_zero = ARRAY_LEN (a->as_u16);
172 n_zeros = 0;
173 }
174 }
175
176 last_double_colon = 0;
177 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
178 {
179 if (i == i_max_n_zero && max_n_zeros > 1)
180 {
181 s = format (s, "::");
182 i += max_n_zeros - 1;
183 last_double_colon = 1;
184 }
185 else
186 {
187 s = format (s, "%s%x",
188 (last_double_colon || i == 0) ? "" : ":",
189 clib_net_to_host_u16 (a->as_u16[i]));
190 last_double_colon = 0;
191 }
192 }
193
194 return s;
195}
196
197/* Format an IP46 address. */
198u8 *
199format_ip46_address (u8 * s, va_list * args)
200{
201 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
202 ip46_type_t type = va_arg (*args, ip46_type_t);
203 int is_ip4 = 1;
204
205 switch (type)
206 {
207 case IP46_TYPE_ANY:
208 is_ip4 = ip46_address_is_ip4 (ip46);
209 break;
210 case IP46_TYPE_IP4:
211 is_ip4 = 1;
212 break;
213 case IP46_TYPE_IP6:
214 is_ip4 = 0;
215 break;
216 }
217
218 return is_ip4 ?
219 format (s, "%U", format_ip4_address, &ip46->ip4) :
220 format (s, "%U", format_ip6_address, &ip46->ip6);
221}
222
Florin Coras697faea2018-06-27 17:10:49 -0700223/*
224 * VPPCOM Utility Functions
225 */
226
227static inline void
228vppcom_session_table_del_listener (u64 listener_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400229{
Florin Coras697faea2018-06-27 17:10:49 -0700230 listener_handle |= 1ULL << 63;
231 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
232}
233
234static inline int
235vppcom_wait_for_app_state_change (app_state_t app_state)
236{
237 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
238
239 while (clib_time_now (&vcm->clib_time) < timeout)
240 {
241 if (vcm->app_state == app_state)
242 return VPPCOM_OK;
243 }
244 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
245 vppcom_app_state_str (app_state), app_state);
246 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
247
248 return VPPCOM_ETIMEDOUT;
249}
250
Florin Corasc9fbd662018-08-24 12:59:56 -0700251static svm_msg_q_t *
252vcl_session_vpp_evt_q (vcl_session_t * s)
253{
254 if (vcl_session_is_ct (s))
255 return vcm->vpp_event_queues[0];
256 else
257 return vcm->vpp_event_queues[s->tx_fifo->master_thread_index];
258}
259
Florin Coras54693d22018-07-17 10:46:29 -0700260static void
261vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
262 session_handle_t handle, int retval)
263{
264 app_session_evt_t _app_evt, *app_evt = &_app_evt;
265 session_accepted_reply_msg_t *rmp;
266 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
267 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
268 rmp->handle = handle;
269 rmp->context = context;
270 rmp->retval = retval;
271 app_send_ctrl_evt_to_vpp (mq, app_evt);
272}
273
Florin Coras99368312018-08-02 10:45:44 -0700274static void
275vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
276 session_handle_t handle, int retval)
277{
278 app_session_evt_t _app_evt, *app_evt = &_app_evt;
279 session_disconnected_reply_msg_t *rmp;
280 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
281 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
282 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
283 rmp->handle = handle;
284 rmp->context = context;
285 rmp->retval = retval;
286 app_send_ctrl_evt_to_vpp (mq, app_evt);
287}
288
Florin Corasc9fbd662018-08-24 12:59:56 -0700289static void
290vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
291 session_handle_t handle, int retval)
292{
293 app_session_evt_t _app_evt, *app_evt = &_app_evt;
294 session_reset_reply_msg_t *rmp;
295 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
296 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
297 rmp->handle = handle;
298 rmp->context = context;
299 rmp->retval = retval;
300 app_send_ctrl_evt_to_vpp (mq, app_evt);
301}
302
Florin Coras54693d22018-07-17 10:46:29 -0700303static u32
304vcl_session_accepted_handler (session_accepted_msg_t * mp)
305{
306 vcl_session_t *session, *listen_session;
307 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700308 u32 session_index, vpp_wrk_index;
309 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700310
311 VCL_SESSION_LOCK ();
312
Florin Coras99368312018-08-02 10:45:44 -0700313 session = vcl_session_alloc ();
314 session_index = vcl_session_index (session);
315
Florin Coras54693d22018-07-17 10:46:29 -0700316 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
317 if (!listen_session)
318 {
319 svm_msg_q_t *evt_q;
320 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
321 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
322 "unknown vpp listener handle %llx",
323 getpid (), mp->listener_handle);
324 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
325 VNET_API_ERROR_INVALID_ARGUMENT);
Florin Coras99368312018-08-02 10:45:44 -0700326 vcl_session_free (session);
327 VCL_SESSION_UNLOCK ();
Florin Coras54693d22018-07-17 10:46:29 -0700328 return VCL_INVALID_SESSION_INDEX;
329 }
330
Florin Coras54693d22018-07-17 10:46:29 -0700331 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
332 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
333
334 if (mp->server_event_queue_address)
335 {
336 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
337 svm_msg_q_t *);
338 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
339 svm_msg_q_t *);
340 vcl_wait_for_memory (session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700341 rx_fifo->master_session_index = session_index;
342 tx_fifo->master_session_index = session_index;
Florin Coras99368312018-08-02 10:45:44 -0700343 vec_validate (vcm->vpp_event_queues, 0);
344 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
345 vcm->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700346 }
347 else
348 {
349 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
350 svm_msg_q_t *);
351 rx_fifo->client_session_index = session_index;
352 tx_fifo->client_session_index = session_index;
Florin Coras99368312018-08-02 10:45:44 -0700353
354 vpp_wrk_index = tx_fifo->master_thread_index;
355 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
356 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700357 }
358
359 session->vpp_handle = mp->handle;
360 session->client_context = mp->context;
361 session->rx_fifo = rx_fifo;
362 session->tx_fifo = tx_fifo;
363
364 session->session_state = STATE_ACCEPT;
365 session->transport.rmt_port = mp->port;
366 session->transport.is_ip4 = mp->is_ip4;
367 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
368
369 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
370 session->transport.lcl_port = listen_session->transport.lcl_port;
371 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700372 session->session_type = listen_session->session_type;
373 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700374
375 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
376 " address %U port %d queue %p!", getpid (), mp->handle, session_index,
377 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
378 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
379 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
380 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
381
382 VCL_SESSION_UNLOCK ();
383 return session_index;
384}
385
386static u32
387vcl_session_connected_handler (session_connected_msg_t * mp)
388{
Florin Coras99368312018-08-02 10:45:44 -0700389 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700390 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700391 vcl_session_t *session = 0;
392 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700393 int rv = VPPCOM_OK;
394
395 session_index = mp->context;
396 VCL_SESSION_LOCK_AND_GET (session_index, &session);
397done:
398 if (mp->retval)
399 {
400 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
401 "connect failed! %U",
402 getpid (), mp->handle, session_index,
403 format_api_error, ntohl (mp->retval));
404 if (session)
405 {
406 session->session_state = STATE_FAILED;
407 session->vpp_handle = mp->handle;
408 }
409 else
410 {
411 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
412 "Invalid session index (%u)!",
413 getpid (), mp->handle, session_index);
414 }
415 goto done_unlock;
416 }
417
418 if (rv)
419 goto done_unlock;
420
Florin Coras460dce62018-07-27 05:45:06 -0700421 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
422 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
423 vcl_wait_for_memory (rx_fifo);
424 rx_fifo->client_session_index = session_index;
425 tx_fifo->client_session_index = session_index;
426
Florin Coras54693d22018-07-17 10:46:29 -0700427 if (mp->client_event_queue_address)
428 {
429 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
430 svm_msg_q_t *);
431 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
432 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700433
434 vec_validate (vcm->vpp_event_queues, 0);
435 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
436 vcm->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700437 }
438 else
Florin Coras99368312018-08-02 10:45:44 -0700439 {
440 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
441 svm_msg_q_t *);
442 vpp_wrk_index = tx_fifo->master_thread_index;
443 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
444 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
445 }
Florin Coras54693d22018-07-17 10:46:29 -0700446
Florin Coras54693d22018-07-17 10:46:29 -0700447 session->rx_fifo = rx_fifo;
448 session->tx_fifo = tx_fifo;
449 session->vpp_handle = mp->handle;
450 session->transport.is_ip4 = mp->is_ip4;
451 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
452 sizeof (session->transport.lcl_ip));
453 session->transport.lcl_port = mp->lcl_port;
454 session->session_state = STATE_CONNECT;
455
456 /* Add it to lookup table */
457 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
458
459 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
460 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
461 getpid (), mp->handle, session_index, session->rx_fifo,
462 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
463done_unlock:
464 VCL_SESSION_UNLOCK ();
465 return session_index;
466}
467
Florin Corasc9fbd662018-08-24 12:59:56 -0700468static u32
469vcl_reset_handler (session_reset_msg_t * reset_msg)
470{
471 vcl_session_t *session;
472 u32 sid;
473
474 sid = vcl_session_get_index_from_handle (reset_msg->handle);
475 session = vcl_session_get (sid);
476 if (!session)
477 {
478 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
479 return VCL_INVALID_SESSION_INDEX;
480 }
481 session->session_state = STATE_CLOSE_ON_EMPTY;
482 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
483 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (session),
484 vcm->my_client_index, reset_msg->handle, 0);
485 return sid;
486}
487
Florin Coras54693d22018-07-17 10:46:29 -0700488int
489vcl_handle_mq_ctrl_event (session_event_t * e)
490{
491 session_accepted_msg_t *accepted_msg;
492 session_disconnected_msg_t *disconnected_msg;
493 vcl_session_msg_t *vcl_msg;
494 vcl_session_t *session;
495 u64 handle;
496 u32 sid;
497
498 switch (e->event_type)
499 {
500 case FIFO_EVENT_APP_RX:
501 clib_warning ("unhandled rx: sid %u (0x%x)",
502 e->fifo->client_session_index,
503 e->fifo->client_session_index);
504 break;
505 case SESSION_CTRL_EVT_ACCEPTED:
506 accepted_msg = (session_accepted_msg_t *) e->data;
507 handle = accepted_msg->listener_handle;
508 session = vppcom_session_table_lookup_listener (handle);
509 if (!session)
510 {
511 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
512 "listener handle %llx", getpid (), handle);
513 break;
514 }
515
516 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
517 vcl_msg->accepted_msg = *accepted_msg;
518 break;
519 case SESSION_CTRL_EVT_CONNECTED:
520 vcl_session_connected_handler ((session_connected_msg_t *) e->data);
521 break;
522 case SESSION_CTRL_EVT_DISCONNECTED:
523 disconnected_msg = (session_disconnected_msg_t *) e->data;
524 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
525 session = vcl_session_get (sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700526 if (!session)
527 {
528 VDBG (0, "request to disconnect unknown handle 0x%llx",
529 disconnected_msg->handle);
530 break;
531 }
Florin Coras54693d22018-07-17 10:46:29 -0700532 session->session_state = STATE_DISCONNECT;
Florin Corasc9fbd662018-08-24 12:59:56 -0700533 VDBG (0, "disconnected handle 0xllx, sid %u", disconnected_msg->handle,
534 sid);
535 break;
536 case SESSION_CTRL_EVT_RESET:
537 vcl_reset_handler ((session_reset_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700538 break;
539 default:
540 clib_warning ("unhandled %u", e->event_type);
541 }
542 return VPPCOM_OK;
543}
544
Florin Coras697faea2018-06-27 17:10:49 -0700545static inline int
546vppcom_wait_for_session_state_change (u32 session_index,
547 session_state_t state,
548 f64 wait_for_time)
549{
550 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
551 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700552 svm_msg_q_msg_t msg;
553 session_event_t *e;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800554 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400555
Florin Coras697faea2018-06-27 17:10:49 -0700556 do
Dave Wallace543852a2017-08-03 02:11:34 -0400557 {
Florin Coras697faea2018-06-27 17:10:49 -0700558 VCL_SESSION_LOCK ();
559 rv = vppcom_session_at_index (session_index, &session);
560 if (PREDICT_FALSE (rv))
561 {
562 VCL_SESSION_UNLOCK ();
563 return rv;
564 }
565 if (session->session_state & state)
566 {
567 VCL_SESSION_UNLOCK ();
568 return VPPCOM_OK;
569 }
570 if (session->session_state & STATE_FAILED)
571 {
572 VCL_SESSION_UNLOCK ();
573 return VPPCOM_ECONNREFUSED;
574 }
Dave Wallace7e607a72018-06-18 18:41:32 -0400575 VCL_SESSION_UNLOCK ();
Florin Coras54693d22018-07-17 10:46:29 -0700576
577 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
578 continue;
579 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
580 vcl_handle_mq_ctrl_event (e);
581 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800582 }
Florin Coras697faea2018-06-27 17:10:49 -0700583 while (clib_time_now (&vcm->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800584
Florin Coras697faea2018-06-27 17:10:49 -0700585 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
586 vppcom_session_state_str (state));
587 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400588
Florin Coras697faea2018-06-27 17:10:49 -0700589 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500590}
591
Florin Coras697faea2018-06-27 17:10:49 -0700592static int
593vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400594{
Florin Coras697faea2018-06-27 17:10:49 -0700595 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400596
Florin Coras697faea2018-06-27 17:10:49 -0700597 if (vcm->app_state != STATE_APP_ENABLED)
598 {
599 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
600 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
601 if (PREDICT_FALSE (rv))
602 {
603 VDBG (0, "VCL<%d>: application session enable timed out! "
604 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
605 return rv;
606 }
607 }
608 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400609}
610
Florin Coras697faea2018-06-27 17:10:49 -0700611static int
612vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400613{
Florin Coras697faea2018-06-27 17:10:49 -0700614 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400615
Florin Coras697faea2018-06-27 17:10:49 -0700616 vppcom_app_send_attach ();
617 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
618 if (PREDICT_FALSE (rv))
619 {
620 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
621 getpid (), rv, vppcom_retval_str (rv));
622 return rv;
623 }
Dave Wallace543852a2017-08-03 02:11:34 -0400624
Florin Coras697faea2018-06-27 17:10:49 -0700625 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400626}
627
628static int
Dave Wallace543852a2017-08-03 02:11:34 -0400629vppcom_session_unbind (u32 session_index)
630{
Florin Coras7e12d942018-06-27 14:32:43 -0700631 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400632 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500633 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400634
Dave Wallace7e607a72018-06-18 18:41:32 -0400635 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500636
637 vpp_handle = session->vpp_handle;
638 vppcom_session_table_del_listener (vpp_handle);
639 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700640 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500641
Dave Wallace7e607a72018-06-18 18:41:32 -0400642 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400643
Florin Coras0d427d82018-06-27 03:24:07 -0700644 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
645 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
646 vppcom_session_state_str (STATE_DISCONNECT));
647 vcl_evt (VCL_EVT_UNBIND, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500648 vppcom_send_unbind_sock (vpp_handle);
649
650done:
651 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400652}
653
Florin Coras697faea2018-06-27 17:10:49 -0700654static int
Dave Wallace543852a2017-08-03 02:11:34 -0400655vppcom_session_disconnect (u32 session_index)
656{
Florin Coras99368312018-08-02 10:45:44 -0700657 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700658 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500659 session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700660 u64 vpp_handle;
661 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400662
Dave Wallace7e607a72018-06-18 18:41:32 -0400663 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500664
665 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700666 state = session->session_state;
Dave Wallace7e607a72018-06-18 18:41:32 -0400667 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500668
Florin Coras0d427d82018-06-27 03:24:07 -0700669 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
670 vpp_handle, session_index, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400671
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800672 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400673 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500674 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500675 "Cannot disconnect a listen socket!",
676 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500677 rv = VPPCOM_EBADFD;
678 goto done;
679 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400680
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800681 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500682 {
Florin Coras99368312018-08-02 10:45:44 -0700683 vpp_evt_q = vcl_session_vpp_evt_q (session);
684 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
685 vpp_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700686 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
687 "REPLY...", getpid (), vpp_handle, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400688 }
689 else
Dave Wallace227867f2017-11-13 21:21:53 -0500690 {
Florin Coras0d427d82018-06-27 03:24:07 -0700691 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
692 getpid (), vpp_handle, session_index);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800693 vppcom_send_disconnect_session (vpp_handle, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -0500694 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400695
Dave Wallace4878cbe2017-11-21 03:45:09 -0500696done:
697 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400698}
699
Dave Wallace543852a2017-08-03 02:11:34 -0400700/*
701 * VPPCOM Public API functions
702 */
703int
704vppcom_app_create (char *app_name)
705{
Dave Wallace543852a2017-08-03 02:11:34 -0400706 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400707 int rv;
708
709 if (!vcm->init)
710 {
Dave Wallace543852a2017-08-03 02:11:34 -0400711 vcm->init = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -0400712 vppcom_cfg (&vcm->cfg);
Florin Coras99368312018-08-02 10:45:44 -0700713 vcl_cfg = &vcm->cfg;
714
715 vcm->mqs_epfd = -1;
716 if (vcl_cfg->use_mq_eventfd)
717 vcm->mqs_epfd = epoll_create (1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400718
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800719 clib_spinlock_init (&vcm->session_fifo_lockp);
Dave Wallace2e005bb2017-11-07 01:21:39 -0500720 clib_fifo_validate (vcm->client_session_index_fifo,
721 vcm->cfg.listen_queue_size);
Florin Coras697faea2018-06-27 17:10:49 -0700722 clib_spinlock_init (&vcm->sessions_lockp);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500723
Dave Wallace8af20542017-10-26 03:29:30 -0400724
Dave Wallace543852a2017-08-03 02:11:34 -0400725 vcm->main_cpu = os_get_thread_index ();
Dave Wallace543852a2017-08-03 02:11:34 -0400726
727 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Coras99368312018-08-02 10:45:44 -0700728 vcm->ct_registration_by_mq = hash_create (0, sizeof (uword));
729 clib_spinlock_init (&vcm->ct_registration_lock);
Dave Wallace543852a2017-08-03 02:11:34 -0400730
731 clib_time_init (&vcm->clib_time);
732 vppcom_init_error_string_table ();
Florin Corasa332c462018-01-31 06:52:17 -0800733 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
734 20 /* timeout in secs */ );
Florin Coras99368312018-08-02 10:45:44 -0700735 vec_validate (vcm->mq_events, 64);
736 vec_validate (vcm->mq_msg_vector, 128);
737 vec_reset_length (vcm->mq_msg_vector);
Dave Wallace543852a2017-08-03 02:11:34 -0400738 }
739
740 if (vcm->my_client_index == ~0)
741 {
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800742 /* API hookup and connect to VPP */
Dave Wallace048b1d62018-01-03 22:24:41 -0500743 vppcom_api_hookup ();
Florin Coras0d427d82018-06-27 03:24:07 -0700744 vcl_elog_init (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400745 vcm->app_state = STATE_APP_START;
746 rv = vppcom_connect_to_vpp (app_name);
747 if (rv)
748 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500749 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500750 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400751 return rv;
752 }
753
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800754 /* State event handling thread */
755
756 rv = vce_start_event_thread (&(vcm->event_thread), 20);
757
Florin Coras0d427d82018-06-27 03:24:07 -0700758 VDBG (0, "VCL<%d>: sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400759
Dave Wallace048b1d62018-01-03 22:24:41 -0500760 rv = vppcom_app_session_enable ();
Dave Wallace543852a2017-08-03 02:11:34 -0400761 if (rv)
762 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500763 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
764 "failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400765 return rv;
766 }
Dave Wallace543852a2017-08-03 02:11:34 -0400767
Florin Coras0d427d82018-06-27 03:24:07 -0700768 VDBG (0, "VCL<%d>: sending app attach", getpid ());
Dave Wallace048b1d62018-01-03 22:24:41 -0500769
770 rv = vppcom_app_attach ();
771 if (rv)
772 {
773 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
774 getpid ());
775 return rv;
776 }
777
Florin Coras0d427d82018-06-27 03:24:07 -0700778 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
779 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400780 }
Dave Wallace543852a2017-08-03 02:11:34 -0400781
782 return VPPCOM_OK;
783}
784
785void
786vppcom_app_destroy (void)
787{
Dave Wallace543852a2017-08-03 02:11:34 -0400788 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400789 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400790
791 if (vcm->my_client_index == ~0)
792 return;
793
Florin Coras0d427d82018-06-27 03:24:07 -0700794 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
795 getpid (), vcm->my_client_index, vcm->my_client_index);
796 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800797
Florin Coras697faea2018-06-27 17:10:49 -0700798 vppcom_app_send_detach ();
Dave Wallacede910062018-03-20 09:22:13 -0400799 orig_app_timeout = vcm->cfg.app_timeout;
800 vcm->cfg.app_timeout = 2.0;
Dave Wallace543852a2017-08-03 02:11:34 -0400801 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
Dave Wallacede910062018-03-20 09:22:13 -0400802 vcm->cfg.app_timeout = orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400803 if (PREDICT_FALSE (rv))
Florin Coras0d427d82018-06-27 03:24:07 -0700804 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
805 getpid (), rv, vppcom_retval_str (rv));
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800806
Florin Coras0d427d82018-06-27 03:24:07 -0700807 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400808 vl_client_disconnect_from_vlib ();
809 vcm->my_client_index = ~0;
810 vcm->app_state = STATE_APP_START;
811}
812
813int
Dave Wallacec04cbf12018-02-07 18:14:02 -0500814vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -0400815{
Florin Coras7e12d942018-06-27 14:32:43 -0700816 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -0400817 u32 session_index;
818
Dave Wallace7e607a72018-06-18 18:41:32 -0400819 VCL_SESSION_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400820 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400821 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -0400822 session_index = session - vcm->sessions;
823
Florin Coras7e12d942018-06-27 14:32:43 -0700824 session->session_type = proto;
825 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500826 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -0700827 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -0400828
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800829 if (is_nonblocking)
830 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -0400831
Florin Coras7e12d942018-06-27 14:32:43 -0700832 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
833 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800834
Dave Wallace7e607a72018-06-18 18:41:32 -0400835 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800836
Florin Coras0d427d82018-06-27 03:24:07 -0700837 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800838
Dave Wallace543852a2017-08-03 02:11:34 -0400839 return (int) session_index;
840}
841
842int
843vppcom_session_close (uint32_t session_index)
844{
Florin Coras7e12d942018-06-27 14:32:43 -0700845 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400846 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400847 u8 is_vep;
848 u8 is_vep_session;
849 u32 next_sid;
850 u32 vep_idx;
Dave Wallaceee45d412017-11-24 21:44:06 -0500851 u64 vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500852 uword *p;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400853 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -0400854
Dave Wallace7e607a72018-06-18 18:41:32 -0400855 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400856 is_vep = session->is_vep;
857 is_vep_session = session->is_vep_session;
858 next_sid = session->vep.next_sid;
859 vep_idx = session->vep.vep_idx;
Florin Coras7e12d942018-06-27 14:32:43 -0700860 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -0500861 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -0400862 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400863
864 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500865 {
866 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500867 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
868 "closing epoll session...",
Dave Wallaceee45d412017-11-24 21:44:06 -0500869 getpid (), session_index, session_index);
870 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500871 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
872 "closing session...",
Dave Wallaceee45d412017-11-24 21:44:06 -0500873 getpid (), vpp_handle, session_index);
874 }
Dave Wallace543852a2017-08-03 02:11:34 -0400875
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400876 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -0400877 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400878 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -0400879 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400880 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700881 if (PREDICT_FALSE (rv < 0))
882 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
883 "vep_idx %u failed! rv %d (%s)",
884 getpid (), vpp_handle, next_sid, vep_idx,
885 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400886
Dave Wallace7e607a72018-06-18 18:41:32 -0400887 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400888 next_sid = session->vep.next_sid;
Dave Wallace7e607a72018-06-18 18:41:32 -0400889 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -0400890 }
891 }
892 else
893 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400894 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400895 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400896 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700897 if (rv < 0)
898 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
899 "vep_idx %u failed! rv %d (%s)",
900 getpid (), vpp_handle, session_index,
901 vep_idx, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400902 }
903
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800904 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400905 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800906 rv = vppcom_session_unbind (session_index);
907 if (PREDICT_FALSE (rv < 0))
Florin Coras0d427d82018-06-27 03:24:07 -0700908 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
909 "failed! rv %d (%s)",
910 getpid (), vpp_handle, session_index,
911 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400912 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500913 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400914 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500915 rv = vppcom_session_disconnect (session_index);
916 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -0500917 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500918 "session disconnect failed! rv %d (%s)",
919 getpid (), vpp_handle, session_index,
920 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400921 }
Dave Wallace19481612017-09-15 18:47:44 -0400922 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500923
Dave Wallace7e607a72018-06-18 18:41:32 -0400924 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Florin Coras99368312018-08-02 10:45:44 -0700925 if (vcl_session_is_ct (session))
926 {
927 vcl_cut_through_registration_t *ctr;
928 uword mq_addr;
929
930 mq_addr = pointer_to_uword (session->our_evt_q);
931 ctr = vcl_ct_registration_lock_and_lookup (mq_addr);
932 ASSERT (ctr);
933 if (ctr->epoll_evt_conn_index != ~0)
934 vcl_mq_epoll_del_evfd (ctr->epoll_evt_conn_index);
935 VDBG (0, "Removing ct registration %u",
936 vcl_ct_registration_index (ctr));
937 vcl_ct_registration_del (ctr);
938 vcl_ct_registration_unlock ();
939 }
Florin Coras54693d22018-07-17 10:46:29 -0700940
Dave Wallaceee45d412017-11-24 21:44:06 -0500941 vpp_handle = session->vpp_handle;
942 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500943 {
Dave Wallaceee45d412017-11-24 21:44:06 -0500944 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500945 if (p)
Dave Wallaceee45d412017-11-24 21:44:06 -0500946 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500947 }
Dave Wallace543852a2017-08-03 02:11:34 -0400948 pool_put_index (vcm->sessions, session_index);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800949
Dave Wallace7e607a72018-06-18 18:41:32 -0400950 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500951
952 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500953 {
954 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500955 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -0500956 getpid (), session_index, session_index);
957 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500958 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -0500959 getpid (), vpp_handle, session_index);
960 }
Dave Wallacef7f809c2017-10-03 01:48:42 -0400961done:
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800962
Florin Coras0d427d82018-06-27 03:24:07 -0700963 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800964
Dave Wallace543852a2017-08-03 02:11:34 -0400965 return rv;
966}
967
968int
969vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
970{
Florin Coras7e12d942018-06-27 14:32:43 -0700971 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400972 int rv;
973
974 if (!ep || !ep->ip)
975 return VPPCOM_EINVAL;
976
Dave Wallace7e607a72018-06-18 18:41:32 -0400977 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -0400978
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400979 if (session->is_vep)
980 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400981 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500982 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
983 "bind to an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500984 rv = VPPCOM_EBADFD;
985 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400986 }
987
Florin Coras7e12d942018-06-27 14:32:43 -0700988 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -0700989 if (ep->is_ip4)
990 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
991 sizeof (ip4_address_t));
992 else
993 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
994 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -0700995 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -0700996
Florin Coras0d427d82018-06-27 03:24:07 -0700997 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
998 "proto %s", getpid (), session_index,
Florin Coras7e12d942018-06-27 14:32:43 -0700999 session->transport.is_ip4 ? "IPv4" : "IPv6",
1000 format_ip46_address, &session->transport.lcl_ip,
1001 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1002 clib_net_to_host_u16 (session->transport.lcl_port),
1003 session->session_type ? "UDP" : "TCP");
Florin Coras0d427d82018-06-27 03:24:07 -07001004 vcl_evt (VCL_EVT_BIND, session);
Dave Wallace7e607a72018-06-18 18:41:32 -04001005 VCL_SESSION_UNLOCK ();
Florin Coras460dce62018-07-27 05:45:06 -07001006
1007 if (session->session_type == VPPCOM_PROTO_UDP)
1008 vppcom_session_listen (session_index, 10);
1009
Dave Wallace4878cbe2017-11-21 03:45:09 -05001010done:
1011 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001012}
1013
1014int
Dave Wallace33e002b2017-09-06 01:20:02 -04001015vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001016{
Florin Coras7e12d942018-06-27 14:32:43 -07001017 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001018 u64 listen_vpp_handle;
1019 int rv, retval;
Dave Wallace543852a2017-08-03 02:11:34 -04001020
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001021 if (q_len == 0 || q_len == ~0)
1022 q_len = vcm->cfg.listen_queue_size;
1023
Dave Wallace7e607a72018-06-18 18:41:32 -04001024 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001025
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001026 if (listen_session->is_vep)
1027 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001028 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001029 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001030 "epoll session!", getpid (), listen_session_index);
1031 rv = VPPCOM_EBADFD;
1032 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001033 }
1034
Dave Wallaceee45d412017-11-24 21:44:06 -05001035 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001036 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001037 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001038 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07001039 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
1040 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001041 rv = VPPCOM_OK;
1042 goto done;
Dave Wallacee695cb42017-11-02 22:04:42 -04001043 }
1044
Florin Coras0d427d82018-06-27 03:24:07 -07001045 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
1046 "request...", getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001047
Dave Wallace4878cbe2017-11-21 03:45:09 -05001048 vppcom_send_bind_sock (listen_session, listen_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001049 VCL_SESSION_UNLOCK ();
Florin Coras54693d22018-07-17 10:46:29 -07001050 retval = vppcom_wait_for_session_state_change (listen_session_index,
1051 STATE_LISTEN,
1052 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001053
Dave Wallace7e607a72018-06-18 18:41:32 -04001054 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05001055 if (PREDICT_FALSE (retval))
1056 {
Florin Coras0d427d82018-06-27 03:24:07 -07001057 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1058 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1059 listen_session_index, retval, vppcom_retval_str (retval));
Dave Wallace7e607a72018-06-18 18:41:32 -04001060 VCL_SESSION_UNLOCK ();
Dave Wallaceee45d412017-11-24 21:44:06 -05001061 rv = retval;
1062 goto done;
1063 }
1064
Dave Wallace7e607a72018-06-18 18:41:32 -04001065 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001066
1067done:
1068 return rv;
1069}
1070
1071int
Florin Coras7e12d942018-06-27 14:32:43 -07001072validate_args_session_accept_ (vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001073{
1074 u32 listen_session_index = listen_session - vcm->sessions;
1075
1076 /* Input validation - expects spinlock on sessions_lockp */
1077 if (listen_session->is_vep)
1078 {
1079 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1080 "epoll session!", getpid (), listen_session_index);
1081 return VPPCOM_EBADFD;
1082 }
1083
Florin Coras7e12d942018-06-27 14:32:43 -07001084 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001085 {
1086 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1087 "not in listen state! state 0x%x (%s)", getpid (),
1088 listen_session->vpp_handle, listen_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001089 listen_session->session_state,
1090 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001091 return VPPCOM_EBADFD;
1092 }
1093 return VPPCOM_OK;
1094}
1095
1096int
Dave Wallace543852a2017-08-03 02:11:34 -04001097vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001098 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001099{
Florin Coras54693d22018-07-17 10:46:29 -07001100 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001101 vcl_session_t *listen_session = 0;
1102 vcl_session_t *client_session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001103 u32 client_session_index = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07001104 svm_msg_q_t *vpp_evt_q;
1105 vcl_session_msg_t *evt;
Dave Wallaceee45d412017-11-24 21:44:06 -05001106 u64 listen_vpp_handle;
Florin Coras54693d22018-07-17 10:46:29 -07001107 svm_msg_q_msg_t msg;
1108 session_event_t *e;
1109 u8 is_nonblocking;
1110 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001111
Dave Wallace7e607a72018-06-18 18:41:32 -04001112 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001113
Florin Coras54693d22018-07-17 10:46:29 -07001114 if (validate_args_session_accept_ (listen_session))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001115 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001116 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05001117 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001118 }
1119
Dave Wallace7e607a72018-06-18 18:41:32 -04001120 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04001121
Florin Coras54693d22018-07-17 10:46:29 -07001122 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001123 {
Florin Coras54693d22018-07-17 10:46:29 -07001124 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1125 accepted_msg = evt->accepted_msg;
1126 goto handle;
1127 }
1128
1129 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1130 VCL_SESS_ATTR_NONBLOCK);
1131 if (svm_msg_q_is_empty (vcm->app_event_queue) && is_nonblocking)
1132 return VPPCOM_EAGAIN;
1133
1134 while (1)
1135 {
1136 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_WAIT, 0))
1137 return VPPCOM_EAGAIN;
1138
1139 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
1140 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001141 {
Florin Coras54693d22018-07-17 10:46:29 -07001142 clib_warning ("discarded event: %u", e->event_type);
1143 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1144 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001145 }
Florin Coras54693d22018-07-17 10:46:29 -07001146 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1147 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1148 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001149 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001150
Florin Coras54693d22018-07-17 10:46:29 -07001151handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001152
Florin Coras54693d22018-07-17 10:46:29 -07001153 client_session_index = vcl_session_accepted_handler (&accepted_msg);
Florin Coras99368312018-08-02 10:45:44 -07001154 listen_session = vcl_session_get (listen_session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001155 VCL_SESSION_LOCK_AND_GET (client_session_index, &client_session);
1156 rv = client_session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001157
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001158 if (flags & O_NONBLOCK)
1159 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001160
Florin Coras54693d22018-07-17 10:46:29 -07001161 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras0d427d82018-06-27 03:24:07 -07001162 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1163 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1164 getpid (), listen_vpp_handle, listen_session_index,
1165 client_session->vpp_handle, client_session_index,
1166 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1167 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001168
Dave Wallace048b1d62018-01-03 22:24:41 -05001169 if (ep)
1170 {
Florin Coras7e12d942018-06-27 14:32:43 -07001171 ep->is_ip4 = client_session->transport.is_ip4;
1172 ep->port = client_session->transport.rmt_port;
1173 if (client_session->transport.is_ip4)
1174 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
Dave Wallace048b1d62018-01-03 22:24:41 -05001175 sizeof (ip4_address_t));
1176 else
Florin Coras7e12d942018-06-27 14:32:43 -07001177 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
Dave Wallace048b1d62018-01-03 22:24:41 -05001178 sizeof (ip6_address_t));
1179 }
Dave Wallace60caa062017-11-10 17:07:13 -05001180
Florin Coras54693d22018-07-17 10:46:29 -07001181 if (accepted_msg.server_event_queue_address)
1182 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1183 svm_msg_q_t *);
1184 else
1185 vpp_evt_q = client_session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -07001186
Florin Coras54693d22018-07-17 10:46:29 -07001187 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1188 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001189
Florin Coras54693d22018-07-17 10:46:29 -07001190 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1191 "sid %u connection from peer %s address %U port %u to local %s "
1192 "address %U port %u", getpid (), listen_vpp_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001193 listen_session_index, client_session->vpp_handle,
1194 client_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001195 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1196 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001197 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001198 clib_net_to_host_u16 (client_session->transport.rmt_port),
1199 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1200 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001201 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001202 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001203 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1204 client_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001205 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001206
Dave Wallace4878cbe2017-11-21 03:45:09 -05001207done:
1208 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001209}
1210
1211int
1212vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
1213{
Florin Coras7e12d942018-06-27 14:32:43 -07001214 vcl_session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001215 u64 vpp_handle = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001216 int rv, retval = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001217
Dave Wallace7e607a72018-06-18 18:41:32 -04001218 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001219
1220 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001221 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001222 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001223 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1224 "connect on an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001225 rv = VPPCOM_EBADFD;
1226 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001227 }
1228
Florin Coras7e12d942018-06-27 14:32:43 -07001229 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001230 {
Florin Coras0d427d82018-06-27 03:24:07 -07001231 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1232 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1233 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001234 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001235 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001236 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001237 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001238 clib_net_to_host_u16 (session->transport.rmt_port),
1239 session->session_type ? "UDP" : "TCP", session->session_state,
1240 vppcom_session_state_str (session->session_state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001241
Dave Wallace7e607a72018-06-18 18:41:32 -04001242 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05001243 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001244 }
1245
Florin Coras7e12d942018-06-27 14:32:43 -07001246 session->transport.is_ip4 = server_ep->is_ip4;
1247 if (session->transport.is_ip4)
1248 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04001249 sizeof (ip4_address_t));
1250 else
Florin Coras7e12d942018-06-27 14:32:43 -07001251 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04001252 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001253 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001254
Florin Coras0d427d82018-06-27 03:24:07 -07001255 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1256 "port %d proto %s",
1257 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001258 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001259 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001260 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001261 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001262 clib_net_to_host_u16 (session->transport.rmt_port),
1263 session->session_type ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04001264
1265 vppcom_send_connect_sock (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001266 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05001267
Florin Coras54693d22018-07-17 10:46:29 -07001268 retval = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1269 vcm->cfg.session_timeout);
Dave Wallaceee45d412017-11-24 21:44:06 -05001270
Dave Wallace7e607a72018-06-18 18:41:32 -04001271 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -05001272 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -04001273 VCL_SESSION_UNLOCK ();
Dave Wallace7876d392017-10-19 03:53:57 -04001274
Dave Wallace4878cbe2017-11-21 03:45:09 -05001275done:
Dave Wallaceee45d412017-11-24 21:44:06 -05001276 if (PREDICT_FALSE (retval))
1277 {
1278 rv = retval;
1279 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001280 {
1281 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07001282 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1283 "failed! returning %d (%s)", getpid (), vpp_handle,
1284 session_index, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001285 else
1286 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1287 "returning %d (%s)", getpid (),
1288 session_index, rv, vppcom_retval_str (rv));
1289 }
Dave Wallaceee45d412017-11-24 21:44:06 -05001290 }
Florin Coras0d427d82018-06-27 03:24:07 -07001291 else
1292 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1293 getpid (), vpp_handle, session_index);
Dave Wallaceee45d412017-11-24 21:44:06 -05001294
Dave Wallace4878cbe2017-11-21 03:45:09 -05001295 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001296}
1297
Florin Coras54693d22018-07-17 10:46:29 -07001298static u8
1299vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1300{
1301 if (!is_ct)
1302 return (e->event_type == FIFO_EVENT_APP_RX
1303 && e->fifo->client_session_index == sid);
1304 else
1305 return (e->event_type == SESSION_IO_EVT_CT_TX);
1306}
1307
Florin Coras460dce62018-07-27 05:45:06 -07001308static inline u8
1309vcl_session_is_readable (vcl_session_t * s)
1310{
1311 return ((s->session_state & STATE_OPEN)
1312 || (s->session_state == STATE_LISTEN
1313 && s->session_type == VPPCOM_PROTO_UDP));
1314}
1315
Steven58f464e2017-10-25 12:33:12 -07001316static inline int
1317vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
1318 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001319{
Florin Coras54693d22018-07-17 10:46:29 -07001320 int n_read = 0, rv, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001321 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001322 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001323 svm_msg_q_msg_t msg;
1324 session_event_t *e;
1325 svm_msg_q_t *mq;
1326 u8 is_full;
Dave Wallace543852a2017-08-03 02:11:34 -04001327
1328 ASSERT (buf);
1329
Florin Coras460dce62018-07-27 05:45:06 -07001330 VCL_SESSION_LOCK_AND_GET (session_index, &s);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001331
Florin Coras460dce62018-07-27 05:45:06 -07001332 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001333 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001334 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001335 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1336 "read from an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001337 rv = VPPCOM_EBADFD;
1338 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001339 }
1340
Florin Coras460dce62018-07-27 05:45:06 -07001341 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1342 rx_fifo = s->rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001343
Florin Coras460dce62018-07-27 05:45:06 -07001344 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001345 {
Florin Coras460dce62018-07-27 05:45:06 -07001346 session_state_t state = s->session_state;
Dave Wallace7e607a72018-06-18 18:41:32 -04001347 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001348 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001349
Florin Coras0d427d82018-06-27 03:24:07 -07001350 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1351 "state 0x%x (%s), returning %d (%s)",
Florin Coras460dce62018-07-27 05:45:06 -07001352 getpid (), s->vpp_handle, session_index, state,
Florin Coras0d427d82018-06-27 03:24:07 -07001353 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001354 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001355 }
1356
Dave Wallace7e607a72018-06-18 18:41:32 -04001357 VCL_SESSION_UNLOCK ();
Florin Coras460dce62018-07-27 05:45:06 -07001358 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
Florin Coras99368312018-08-02 10:45:44 -07001359 svm_fifo_unset_event (rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001360 is_full = svm_fifo_is_full (rx_fifo);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001361
Florin Coras54693d22018-07-17 10:46:29 -07001362 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001363 {
Florin Coras54693d22018-07-17 10:46:29 -07001364 if (is_nonblocking)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001365 {
Florin Coras54693d22018-07-17 10:46:29 -07001366 rv = VPPCOM_OK;
1367 goto done;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001368 }
Florin Coras54693d22018-07-17 10:46:29 -07001369 while (1)
1370 {
Florin Coras99368312018-08-02 10:45:44 -07001371 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001372 if (svm_msg_q_is_empty (mq))
1373 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001374
Florin Coras54693d22018-07-17 10:46:29 -07001375 svm_msg_q_sub_w_lock (mq, &msg);
1376 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001377 svm_msg_q_unlock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001378 if (!vcl_is_rx_evt_for_session (e, session_index,
Florin Coras460dce62018-07-27 05:45:06 -07001379 s->our_evt_q != 0))
Florin Coras54693d22018-07-17 10:46:29 -07001380 {
1381 vcl_handle_mq_ctrl_event (e);
1382 svm_msg_q_free_msg (mq, &msg);
1383 continue;
1384 }
Florin Coras99368312018-08-02 10:45:44 -07001385 svm_fifo_unset_event (rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001386 svm_msg_q_free_msg (mq, &msg);
Florin Coras60f1fc12018-08-16 14:57:31 -07001387 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1388 return 0;
1389 if (svm_fifo_is_empty (rx_fifo))
1390 continue;
Florin Coras54693d22018-07-17 10:46:29 -07001391 break;
1392 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001393 }
Florin Coras54693d22018-07-17 10:46:29 -07001394
Florin Coras460dce62018-07-27 05:45:06 -07001395 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001396 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001397 else
Florin Coras99368312018-08-02 10:45:44 -07001398 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001399
Florin Coras460dce62018-07-27 05:45:06 -07001400 if (vcl_session_is_ct (s) && is_full)
Florin Coras99368312018-08-02 10:45:44 -07001401 {
1402 /* If the peer is not polling send notification */
1403 if (!svm_fifo_has_event (s->rx_fifo))
1404 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1405 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1406 }
Florin Coras54693d22018-07-17 10:46:29 -07001407
Dave Wallace4878cbe2017-11-21 03:45:09 -05001408 if (VPPCOM_DEBUG > 2)
1409 {
Florin Coras54693d22018-07-17 10:46:29 -07001410 if (n_read > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001411 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
Florin Coras460dce62018-07-27 05:45:06 -07001412 "from (%p)", getpid (), s->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001413 session_index, n_read, rx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001414 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001415 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
Florin Coras460dce62018-07-27 05:45:06 -07001416 "returning %d (%s)", getpid (), s->vpp_handle,
Dave Wallaceee45d412017-11-24 21:44:06 -05001417 session_index, rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001418 }
Florin Coras54693d22018-07-17 10:46:29 -07001419 return n_read;
1420
Dave Wallace4878cbe2017-11-21 03:45:09 -05001421done:
1422 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001423}
1424
Steven58f464e2017-10-25 12:33:12 -07001425int
Dave Wallace048b1d62018-01-03 22:24:41 -05001426vppcom_session_read (uint32_t session_index, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001427{
1428 return (vppcom_session_read_internal (session_index, buf, n, 0));
1429}
1430
1431static int
1432vppcom_session_peek (uint32_t session_index, void *buf, int n)
1433{
1434 return (vppcom_session_read_internal (session_index, buf, n, 1));
1435}
1436
Dave Wallace543852a2017-08-03 02:11:34 -04001437static inline int
Florin Coras54693d22018-07-17 10:46:29 -07001438vppcom_session_read_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001439{
Dave Wallace543852a2017-08-03 02:11:34 -04001440 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001441 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001442 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001443 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Florin Coras54693d22018-07-17 10:46:29 -07001444 "epoll session!", getpid (), vcl_session_index (session));
1445 return VPPCOM_EBADFD;
1446 }
1447
1448 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1449 {
1450 session_state_t state = session->session_state;
1451 int rv;
1452
1453 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1454
1455 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1456 " state 0x%x (%s), returning %d (%s)", getpid (),
1457 session->vpp_handle, vcl_session_index (session), state,
1458 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1459 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001460 }
Dave Wallace33e002b2017-09-06 01:20:02 -04001461
Florin Coras7e12d942018-06-27 14:32:43 -07001462 if (session->session_state & STATE_LISTEN)
Florin Coras54693d22018-07-17 10:46:29 -07001463 return clib_fifo_elts (session->accept_evts_fifo);
1464
1465 return svm_fifo_max_dequeue (session->rx_fifo);
1466}
1467
1468static u8
1469vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1470{
1471 if (!is_ct)
1472 return (e->event_type == FIFO_EVENT_APP_TX
1473 && e->fifo->client_session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001474 else
Florin Coras54693d22018-07-17 10:46:29 -07001475 return (e->event_type == SESSION_IO_EVT_CT_RX);
Dave Wallace543852a2017-08-03 02:11:34 -04001476}
1477
1478int
Dave Wallace048b1d62018-01-03 22:24:41 -05001479vppcom_session_write (uint32_t session_index, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04001480{
Florin Coras54693d22018-07-17 10:46:29 -07001481 int rv, n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001482 vcl_session_t *s = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001483 svm_fifo_t *tx_fifo = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001484 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001485 svm_msg_q_msg_t msg;
1486 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001487 svm_msg_q_t *mq;
Dave Wallace543852a2017-08-03 02:11:34 -04001488
1489 ASSERT (buf);
1490
Florin Coras460dce62018-07-27 05:45:06 -07001491 VCL_SESSION_LOCK_AND_GET (session_index, &s);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001492
Florin Coras460dce62018-07-27 05:45:06 -07001493 tx_fifo = s->tx_fifo;
1494 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001495
Florin Coras460dce62018-07-27 05:45:06 -07001496 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001497 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001498 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001499 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001500 "cannot write to an epoll session!",
Florin Coras460dce62018-07-27 05:45:06 -07001501 getpid (), s->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001502
1503 rv = VPPCOM_EBADFD;
1504 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001505 }
1506
Florin Coras460dce62018-07-27 05:45:06 -07001507 if (!(s->session_state & STATE_OPEN))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001508 {
Florin Coras460dce62018-07-27 05:45:06 -07001509 session_state_t state = s->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001510 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace7e607a72018-06-18 18:41:32 -04001511 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07001512 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1513 "state 0x%x (%s)",
Florin Coras460dce62018-07-27 05:45:06 -07001514 getpid (), s->vpp_handle, session_index,
Florin Coras0d427d82018-06-27 03:24:07 -07001515 state, vppcom_session_state_str (state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001516 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001517 }
1518
Dave Wallace7e607a72018-06-18 18:41:32 -04001519 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001520
Florin Coras460dce62018-07-27 05:45:06 -07001521 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001522 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001523 {
Florin Coras54693d22018-07-17 10:46:29 -07001524 if (is_nonblocking)
1525 {
1526 rv = VPPCOM_EWOULDBLOCK;
1527 goto done;
1528 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001529 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001530 {
Florin Coras99368312018-08-02 10:45:44 -07001531 svm_msg_q_lock (mq);
Florin Coras99368312018-08-02 10:45:44 -07001532 while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1533 ;
Florin Coras54693d22018-07-17 10:46:29 -07001534 svm_msg_q_sub_w_lock (mq, &msg);
1535 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001536 svm_msg_q_unlock (mq);
1537
Florin Coras54693d22018-07-17 10:46:29 -07001538 if (!vcl_is_tx_evt_for_session (e, session_index,
Florin Coras460dce62018-07-27 05:45:06 -07001539 s->our_evt_q != 0))
Florin Coras60f1fc12018-08-16 14:57:31 -07001540 vcl_handle_mq_ctrl_event (e);
Florin Coras54693d22018-07-17 10:46:29 -07001541 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001542 }
Dave Wallace543852a2017-08-03 02:11:34 -04001543 }
Dave Wallace543852a2017-08-03 02:11:34 -04001544
Florin Coras460dce62018-07-27 05:45:06 -07001545 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1546 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1547 if (s->is_dgram)
1548 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1549 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1550 else
1551 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1552 SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001553
Florin Coras460dce62018-07-27 05:45:06 -07001554 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001555
1556 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001557 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001558 if (n_write <= 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001559 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Florin Coras460dce62018-07-27 05:45:06 -07001560 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001561 session_index, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001562 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001563 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001564 "wrote %d bytes tx-fifo: (%p)", getpid (),
Florin Coras460dce62018-07-27 05:45:06 -07001565 s->vpp_handle, session_index, n_write, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001566 }
Florin Coras54693d22018-07-17 10:46:29 -07001567 return n_write;
1568
Dave Wallace4878cbe2017-11-21 03:45:09 -05001569done:
1570 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001571}
1572
Florin Coras99368312018-08-02 10:45:44 -07001573static vcl_session_t *
1574vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
1575{
1576 vcl_session_t *s;
1577 s = vcl_session_get (f->client_session_index);
1578 if (s)
1579 {
1580 /* rx fifo */
1581 if (type == 0 && s->rx_fifo == f)
1582 return s;
1583 /* tx fifo */
1584 if (type == 1 && s->tx_fifo == f)
1585 return s;
1586 }
1587 s = vcl_session_get (f->master_session_index);
1588 if (s)
1589 {
1590 if (type == 0 && s->rx_fifo == f)
1591 return s;
1592 if (type == 1 && s->tx_fifo == f)
1593 return s;
1594 }
1595 return 0;
1596}
1597
Dave Wallace543852a2017-08-03 02:11:34 -04001598static inline int
Florin Coras7e12d942018-06-27 14:32:43 -07001599vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001600{
Dave Wallace543852a2017-08-03 02:11:34 -04001601 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001602 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001603 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001604 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001605 "cannot write to an epoll session!",
1606 getpid (), session->vpp_handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001607 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001608 }
1609
Florin Coras7e12d942018-06-27 14:32:43 -07001610 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04001611 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001612 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001613 "cannot write to a listen session!",
1614 getpid (), session->vpp_handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001615 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001616 }
1617
Florin Coras54693d22018-07-17 10:46:29 -07001618 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001619 {
Florin Coras7e12d942018-06-27 14:32:43 -07001620 session_state_t state = session->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001621 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001622
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001623 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace048b1d62018-01-03 22:24:41 -05001624 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001625 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05001626 "returning %d (%s)", getpid (), session->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001627 session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05001628 state, vppcom_session_state_str (state),
1629 rv, vppcom_retval_str (rv));
Florin Coras54693d22018-07-17 10:46:29 -07001630 return rv;
Dave Wallace33e002b2017-09-06 01:20:02 -04001631 }
1632
Florin Coras0d427d82018-06-27 03:24:07 -07001633 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1634 getpid (), session->vpp_handle, session_index, session->tx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -07001635 svm_fifo_max_enqueue (session->tx_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001636
Florin Coras54693d22018-07-17 10:46:29 -07001637 return svm_fifo_max_enqueue (session->tx_fifo);
1638}
1639
Florin Coras99368312018-08-02 10:45:44 -07001640static inline int
1641vcl_mq_dequeue_batch (svm_msg_q_t * mq)
1642{
1643 svm_msg_q_msg_t *msg;
1644 u32 n_msgs;
1645 int i;
1646
1647 n_msgs = svm_msg_q_size (mq);
1648 for (i = 0; i < n_msgs; i++)
1649 {
1650 vec_add2 (vcm->mq_msg_vector, msg, 1);
1651 svm_msg_q_sub_w_lock (mq, msg);
1652 }
1653 return n_msgs;
1654}
1655
Florin Coras54693d22018-07-17 10:46:29 -07001656static int
1657vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
1658 unsigned long *read_map, unsigned long *write_map,
1659 unsigned long *except_map, double time_to_wait,
1660 u32 * bits_set)
1661{
1662 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001663 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001664 session_accepted_msg_t *accepted_msg;
1665 vcl_session_msg_t *vcl_msg;
1666 vcl_session_t *session;
Florin Coras99368312018-08-02 10:45:44 -07001667 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001668 session_event_t *e;
Florin Coras99368312018-08-02 10:45:44 -07001669 u32 i, sid;
Florin Coras54693d22018-07-17 10:46:29 -07001670 u64 handle;
1671
1672 svm_msg_q_lock (mq);
1673 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001674 {
Florin Coras54693d22018-07-17 10:46:29 -07001675 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001676 {
Florin Coras54693d22018-07-17 10:46:29 -07001677 svm_msg_q_unlock (mq);
1678 return 0;
1679 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001680
Florin Coras54693d22018-07-17 10:46:29 -07001681 if (!time_to_wait)
1682 {
1683 svm_msg_q_unlock (mq);
1684 return 0;
1685 }
1686 else if (time_to_wait < 0)
1687 {
1688 svm_msg_q_wait (mq);
1689 }
1690 else
1691 {
1692 if (svm_msg_q_timedwait (mq, time_to_wait))
1693 {
1694 svm_msg_q_unlock (mq);
1695 return 0;
1696 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001697 }
1698 }
Florin Coras99368312018-08-02 10:45:44 -07001699 vcl_mq_dequeue_batch (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001700 svm_msg_q_unlock (mq);
1701
Florin Coras99368312018-08-02 10:45:44 -07001702 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001703 {
Florin Coras99368312018-08-02 10:45:44 -07001704 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
1705 e = svm_msg_q_msg_data (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001706 switch (e->event_type)
1707 {
1708 case FIFO_EVENT_APP_RX:
1709 sid = e->fifo->client_session_index;
1710 session = vcl_session_get (sid);
Florin Coras54693d22018-07-17 10:46:29 -07001711 if (sid < n_bits && read_map)
1712 {
1713 clib_bitmap_set_no_check (read_map, sid, 1);
1714 *bits_set += 1;
1715 }
1716 break;
1717 case FIFO_EVENT_APP_TX:
1718 sid = e->fifo->client_session_index;
1719 session = vcl_session_get (sid);
Florin Coras60f1fc12018-08-16 14:57:31 -07001720 if (!session)
Florin Coras54693d22018-07-17 10:46:29 -07001721 break;
1722 if (sid < n_bits && write_map)
1723 {
1724 clib_bitmap_set_no_check (write_map, sid, 1);
1725 *bits_set += 1;
1726 }
1727 break;
1728 case SESSION_IO_EVT_CT_TX:
1729 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
1730 sid = vcl_session_index (session);
Florin Coras54693d22018-07-17 10:46:29 -07001731 if (sid < n_bits && read_map)
1732 {
1733 clib_bitmap_set_no_check (read_map, sid, 1);
1734 *bits_set += 1;
1735 }
1736 break;
1737 break;
1738 case SESSION_IO_EVT_CT_RX:
1739 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
1740 sid = vcl_session_index (session);
Florin Coras60f1fc12018-08-16 14:57:31 -07001741 if (!session)
Florin Coras54693d22018-07-17 10:46:29 -07001742 break;
1743 if (sid < n_bits && write_map)
1744 {
1745 clib_bitmap_set_no_check (write_map, sid, 1);
1746 *bits_set += 1;
1747 }
1748 break;
1749 case SESSION_CTRL_EVT_ACCEPTED:
1750 accepted_msg = (session_accepted_msg_t *) e->data;
1751 handle = accepted_msg->listener_handle;
1752 session = vppcom_session_table_lookup_listener (handle);
1753 if (!session)
1754 {
1755 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1756 "listener handle %llx", getpid (), handle);
1757 break;
1758 }
1759
1760 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1761 vcl_msg->accepted_msg = *accepted_msg;
1762 sid = session - vcm->sessions;
1763 if (sid < n_bits && read_map)
1764 {
1765 clib_bitmap_set_no_check (read_map, sid, 1);
1766 *bits_set += 1;
1767 }
1768 break;
Florin Coras99368312018-08-02 10:45:44 -07001769 case SESSION_CTRL_EVT_CONNECTED:
1770 connected_msg = (session_connected_msg_t *) e->data;
1771 vcl_session_connected_handler (connected_msg);
1772 break;
Florin Coras54693d22018-07-17 10:46:29 -07001773 case SESSION_CTRL_EVT_DISCONNECTED:
1774 disconnected_msg = (session_disconnected_msg_t *) e->data;
1775 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
1776 if (sid < n_bits && except_map)
1777 {
1778 clib_bitmap_set_no_check (except_map, sid, 1);
1779 *bits_set += 1;
1780 }
1781 break;
Florin Corasc9fbd662018-08-24 12:59:56 -07001782 case SESSION_CTRL_EVT_RESET:
1783 sid = vcl_reset_handler ((session_reset_msg_t *) e->data);
1784 if (sid < n_bits && except_map)
1785 {
1786 clib_bitmap_set_no_check (except_map, sid, 1);
1787 *bits_set += 1;
1788 }
1789 break;
Florin Coras54693d22018-07-17 10:46:29 -07001790 default:
1791 clib_warning ("unhandled: %u", e->event_type);
1792 break;
1793 }
Florin Coras99368312018-08-02 10:45:44 -07001794 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001795 }
1796
Florin Coras99368312018-08-02 10:45:44 -07001797 vec_reset_length (vcm->mq_msg_vector);
Florin Coras54693d22018-07-17 10:46:29 -07001798 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001799}
1800
Florin Coras99368312018-08-02 10:45:44 -07001801static int
1802vppcom_select_condvar (unsigned long n_bits, unsigned long *read_map,
1803 unsigned long *write_map, unsigned long *except_map,
1804 double time_to_wait, u32 * bits_set)
1805{
1806 double total_wait = 0, wait_slice;
1807 vcl_cut_through_registration_t *cr;
1808
1809 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1810 wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
1811 do
1812 {
1813 /* *INDENT-OFF* */
1814 pool_foreach (cr, vcm->cut_through_registrations, ({
1815 vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
1816 0, bits_set);
1817 }));
1818 /* *INDENT-ON* */
1819
1820 vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
1821 except_map, time_to_wait, bits_set);
1822 total_wait += wait_slice;
1823 if (*bits_set)
1824 return *bits_set;
1825 }
1826 while (total_wait < time_to_wait);
1827
1828 return 0;
1829}
1830
1831static int
1832vppcom_select_eventfd (unsigned long n_bits, unsigned long *read_map,
1833 unsigned long *write_map, unsigned long *except_map,
1834 double time_to_wait, u32 * bits_set)
1835{
1836 vcl_mq_evt_conn_t *mqc;
1837 int __clib_unused n_read;
1838 int n_mq_evts, i;
1839 u64 buf;
1840
1841 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
1842 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
1843 vec_len (vcm->mq_events), time_to_wait);
1844 for (i = 0; i < n_mq_evts; i++)
1845 {
1846 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
1847 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1848 vcl_select_handle_mq (mqc->mq, n_bits, read_map, write_map,
1849 except_map, 0, bits_set);
1850 }
1851
1852 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1853}
1854
Dave Wallace543852a2017-08-03 02:11:34 -04001855int
1856vppcom_select (unsigned long n_bits, unsigned long *read_map,
1857 unsigned long *write_map, unsigned long *except_map,
1858 double time_to_wait)
1859{
Florin Coras54693d22018-07-17 10:46:29 -07001860 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07001861 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001862 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001863
1864 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1865
Dave Wallace7876d392017-10-19 03:53:57 -04001866 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001867 {
1868 clib_bitmap_validate (vcm->rd_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001869 clib_memcpy (vcm->rd_bitmap, read_map,
1870 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1871 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001872 }
Dave Wallace7876d392017-10-19 03:53:57 -04001873 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001874 {
1875 clib_bitmap_validate (vcm->wr_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001876 clib_memcpy (vcm->wr_bitmap, write_map,
1877 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1878 memset (write_map, 0,
1879 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001880 }
Dave Wallace7876d392017-10-19 03:53:57 -04001881 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001882 {
1883 clib_bitmap_validate (vcm->ex_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001884 clib_memcpy (vcm->ex_bitmap, except_map,
1885 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1886 memset (except_map, 0,
1887 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001888 }
1889
Florin Coras54693d22018-07-17 10:46:29 -07001890 if (!n_bits)
1891 return 0;
1892
1893 if (!write_map)
1894 goto check_rd;
1895
1896 /* *INDENT-OFF* */
1897 clib_bitmap_foreach (sid, vcm->wr_bitmap, ({
Florin Coras54693d22018-07-17 10:46:29 -07001898 if (!(session = vcl_session_get (sid)))
1899 {
Florin Coras54693d22018-07-17 10:46:29 -07001900 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1901 getpid (), sid);
1902 return VPPCOM_EBADFD;
1903 }
1904
1905 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001906 if (!rv)
1907 {
1908 clib_bitmap_set_no_check (write_map, sid, 1);
1909 bits_set++;
1910 }
1911 }));
1912
1913check_rd:
1914 if (!read_map)
1915 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07001916
Florin Coras54693d22018-07-17 10:46:29 -07001917 clib_bitmap_foreach (sid, vcm->rd_bitmap, ({
Florin Coras54693d22018-07-17 10:46:29 -07001918 if (!(session = vcl_session_get (sid)))
1919 {
Florin Coras54693d22018-07-17 10:46:29 -07001920 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1921 getpid (), sid);
1922 return VPPCOM_EBADFD;
1923 }
1924
1925 rv = vppcom_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07001926 if (rv)
1927 {
1928 clib_bitmap_set_no_check (read_map, sid, 1);
1929 bits_set++;
1930 }
1931 }));
1932 /* *INDENT-ON* */
1933
1934check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04001935
Florin Coras99368312018-08-02 10:45:44 -07001936 if (vcm->cfg.use_mq_eventfd)
1937 vppcom_select_eventfd (n_bits, read_map, write_map, except_map,
1938 time_to_wait, &bits_set);
1939 else
1940 vppcom_select_condvar (n_bits, read_map, write_map, except_map,
1941 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07001942
Dave Wallace543852a2017-08-03 02:11:34 -04001943 return (bits_set);
1944}
1945
Dave Wallacef7f809c2017-10-03 01:48:42 -04001946static inline void
1947vep_verify_epoll_chain (u32 vep_idx)
1948{
Florin Coras7e12d942018-06-27 14:32:43 -07001949 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001950 vppcom_epoll_t *vep;
1951 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05001952 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001953
Dave Wallace498b3a52017-11-09 13:00:34 -05001954 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001955 return;
1956
1957 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1958 rv = vppcom_session_at_index (vep_idx, &session);
1959 if (PREDICT_FALSE (rv))
1960 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001961 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1962 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001963 goto done;
1964 }
1965 if (PREDICT_FALSE (!session->is_vep))
1966 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001967 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1968 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001969 goto done;
1970 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001971 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05001972 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05001973 "{\n"
1974 " is_vep = %u\n"
1975 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05001976 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05001977 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05001978 "}\n", getpid (), vep_idx,
1979 session->is_vep, session->is_vep_session,
1980 vep->next_sid, vep->next_sid,
Dave Wallace498b3a52017-11-09 13:00:34 -05001981 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001982
1983 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001984 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001985 rv = vppcom_session_at_index (sid, &session);
1986 if (PREDICT_FALSE (rv))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001987 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001988 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001989 goto done;
1990 }
1991 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05001992 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1993 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001994 else if (PREDICT_FALSE (!session->is_vep_session))
1995 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001996 clib_warning ("VCL<%d>: ERROR: session (%u) "
1997 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001998 goto done;
1999 }
2000 vep = &session->vep;
2001 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05002002 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002003 "vep_idx (%u)!", getpid (),
2004 sid, session->vep.vep_idx, vep_idx);
2005 if (session->is_vep_session)
2006 {
2007 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2008 "{\n"
2009 " next_sid = 0x%x (%u)\n"
2010 " prev_sid = 0x%x (%u)\n"
2011 " vep_idx = 0x%x (%u)\n"
2012 " ev.events = 0x%x\n"
2013 " ev.data.u64 = 0x%llx\n"
2014 " et_mask = 0x%x\n"
2015 "}\n",
2016 vep_idx, sid, sid,
2017 vep->next_sid, vep->next_sid,
2018 vep->prev_sid, vep->prev_sid,
2019 vep->vep_idx, vep->vep_idx,
2020 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002021 }
2022 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002023
2024done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002025 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2026 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002027}
2028
2029int
2030vppcom_epoll_create (void)
2031{
Florin Coras7e12d942018-06-27 14:32:43 -07002032 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002033 u32 vep_idx;
2034
Dave Wallace7e607a72018-06-18 18:41:32 -04002035 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002036 pool_get (vcm->sessions, vep_session);
2037 memset (vep_session, 0, sizeof (*vep_session));
2038 vep_idx = vep_session - vcm->sessions;
2039
2040 vep_session->is_vep = 1;
2041 vep_session->vep.vep_idx = ~0;
2042 vep_session->vep.next_sid = ~0;
2043 vep_session->vep.prev_sid = ~0;
2044 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002045 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08002046 vep_session->poll_reg = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002047
Florin Coras0d427d82018-06-27 03:24:07 -07002048 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
Dave Wallace7e607a72018-06-18 18:41:32 -04002049 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002050
Florin Coras0d427d82018-06-27 03:24:07 -07002051 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2052 getpid (), vep_idx, vep_idx);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002053
Dave Wallacef7f809c2017-10-03 01:48:42 -04002054 return (vep_idx);
2055}
2056
2057int
2058vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2059 struct epoll_event *event)
2060{
Florin Coras7e12d942018-06-27 14:32:43 -07002061 vcl_session_t *vep_session;
2062 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002063 int rv;
2064
2065 if (vep_idx == session_index)
2066 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002067 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002068 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002069 return VPPCOM_EINVAL;
2070 }
2071
Dave Wallace7e607a72018-06-18 18:41:32 -04002072 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002073 rv = vppcom_session_at_index (vep_idx, &vep_session);
2074 if (PREDICT_FALSE (rv))
2075 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002076 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002077 goto done;
2078 }
2079 if (PREDICT_FALSE (!vep_session->is_vep))
2080 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002081 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002082 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002083 rv = VPPCOM_EINVAL;
2084 goto done;
2085 }
2086
2087 ASSERT (vep_session->vep.vep_idx == ~0);
2088 ASSERT (vep_session->vep.prev_sid == ~0);
2089
2090 rv = vppcom_session_at_index (session_index, &session);
2091 if (PREDICT_FALSE (rv))
2092 {
Florin Coras0d427d82018-06-27 03:24:07 -07002093 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
2094 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002095 goto done;
2096 }
2097 if (PREDICT_FALSE (session->is_vep))
2098 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002099 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002100 rv = VPPCOM_EINVAL;
2101 goto done;
2102 }
2103
2104 switch (op)
2105 {
2106 case EPOLL_CTL_ADD:
2107 if (PREDICT_FALSE (!event))
2108 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002109 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002110 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04002111 rv = VPPCOM_EINVAL;
2112 goto done;
2113 }
2114 if (vep_session->vep.next_sid != ~0)
2115 {
Florin Coras7e12d942018-06-27 14:32:43 -07002116 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002117 rv = vppcom_session_at_index (vep_session->vep.next_sid,
2118 &next_session);
2119 if (PREDICT_FALSE (rv))
2120 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002122 "vep.next_sid (%u) on vep_idx (%u)!",
2123 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002124 goto done;
2125 }
2126 ASSERT (next_session->vep.prev_sid == vep_idx);
2127 next_session->vep.prev_sid = session_index;
2128 }
2129 session->vep.next_sid = vep_session->vep.next_sid;
2130 session->vep.prev_sid = vep_idx;
2131 session->vep.vep_idx = vep_idx;
2132 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2133 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002134 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002135 session->is_vep_session = 1;
2136 vep_session->vep.next_sid = session_index;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002137
2138 /* VCL Event Register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07002139 if (session->session_state & STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002140 {
2141 /* Register handler for connect_request event on listen_session_index */
2142 vce_event_key_t evk;
2143 evk.session_index = session_index;
2144 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08002145 vep_session->poll_reg =
2146 vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08002147 vce_poll_wait_connect_request_handler_fn,
2148 0 /* No callback args */ );
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002149 }
Florin Coras0d427d82018-06-27 03:24:07 -07002150 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
2151 "sid %u, events 0x%x, data 0x%llx!",
2152 getpid (), vep_idx, session_index,
2153 event->events, event->data.u64);
2154 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002155 break;
2156
2157 case EPOLL_CTL_MOD:
2158 if (PREDICT_FALSE (!event))
2159 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002160 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002161 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04002162 rv = VPPCOM_EINVAL;
2163 goto done;
2164 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002165 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002166 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002167 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002168 "not a vep session!", getpid (), session_index);
2169 rv = VPPCOM_EINVAL;
2170 goto done;
2171 }
2172 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2173 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002174 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002175 "vep_idx (%u) != vep_idx (%u)!",
2176 getpid (), session_index,
2177 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002178 rv = VPPCOM_EINVAL;
2179 goto done;
2180 }
2181 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2182 session->vep.ev = *event;
Florin Coras0d427d82018-06-27 03:24:07 -07002183 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2184 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
2185 event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002186 break;
2187
2188 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002189 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002190 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002191 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002192 "not a vep session!", getpid (), session_index);
2193 rv = VPPCOM_EINVAL;
2194 goto done;
2195 }
2196 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2197 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002198 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002199 "vep_idx (%u) != vep_idx (%u)!",
2200 getpid (), session_index,
2201 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002202 rv = VPPCOM_EINVAL;
2203 goto done;
2204 }
2205
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002206 /* VCL Event Un-register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07002207 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002208 {
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002209 (void) vce_unregister_handler (&vcm->event_thread,
2210 vep_session->poll_reg);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002211 }
2212
Dave Wallacef7f809c2017-10-03 01:48:42 -04002213 vep_session->wait_cont_idx =
2214 (vep_session->wait_cont_idx == session_index) ?
2215 session->vep.next_sid : vep_session->wait_cont_idx;
2216
2217 if (session->vep.prev_sid == vep_idx)
2218 vep_session->vep.next_sid = session->vep.next_sid;
2219 else
2220 {
Florin Coras7e12d942018-06-27 14:32:43 -07002221 vcl_session_t *prev_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002222 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2223 if (PREDICT_FALSE (rv))
2224 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002225 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002226 "vep.prev_sid (%u) on sid (%u)!",
2227 getpid (), session->vep.prev_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002228 goto done;
2229 }
2230 ASSERT (prev_session->vep.next_sid == session_index);
2231 prev_session->vep.next_sid = session->vep.next_sid;
2232 }
2233 if (session->vep.next_sid != ~0)
2234 {
Florin Coras7e12d942018-06-27 14:32:43 -07002235 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002236 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2237 if (PREDICT_FALSE (rv))
2238 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002239 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002240 "vep.next_sid (%u) on sid (%u)!",
2241 getpid (), session->vep.next_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002242 goto done;
2243 }
2244 ASSERT (next_session->vep.prev_sid == session_index);
2245 next_session->vep.prev_sid = session->vep.prev_sid;
2246 }
2247
2248 memset (&session->vep, 0, sizeof (session->vep));
2249 session->vep.next_sid = ~0;
2250 session->vep.prev_sid = ~0;
2251 session->vep.vep_idx = ~0;
2252 session->is_vep_session = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07002253 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2254 getpid (), vep_idx, session_index);
2255 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002256 break;
2257
2258 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05002259 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002260 rv = VPPCOM_EINVAL;
2261 }
2262
2263 vep_verify_epoll_chain (vep_idx);
2264
2265done:
Dave Wallace7e607a72018-06-18 18:41:32 -04002266 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002267 return rv;
2268}
2269
Florin Coras54693d22018-07-17 10:46:29 -07002270static int
2271vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
2272 u32 maxevents, double wait_for_time, u32 * num_ev)
2273{
2274 session_disconnected_msg_t *disconnected_msg;
2275 session_connected_msg_t *connected_msg;
2276 session_accepted_msg_t *accepted_msg;
Florin Coras54693d22018-07-17 10:46:29 -07002277 u64 session_evt_data = ~0, handle;
Florin Coras99368312018-08-02 10:45:44 -07002278 u32 sid = ~0, session_events;
Florin Coras54693d22018-07-17 10:46:29 -07002279 vcl_session_msg_t *vcl_msg;
2280 vcl_session_t *session;
Florin Coras99368312018-08-02 10:45:44 -07002281 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002282 session_event_t *e;
2283 u8 add_event;
2284 int i;
2285
2286 svm_msg_q_lock (mq);
2287 if (svm_msg_q_is_empty (mq))
2288 {
2289 if (!wait_for_time)
2290 {
2291 svm_msg_q_unlock (mq);
2292 return 0;
2293 }
2294 else if (wait_for_time < 0)
2295 {
2296 svm_msg_q_wait (mq);
2297 }
2298 else
2299 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002300 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002301 {
2302 svm_msg_q_unlock (mq);
2303 return 0;
2304 }
2305 }
2306 }
Florin Coras99368312018-08-02 10:45:44 -07002307 vcl_mq_dequeue_batch (mq);
Florin Coras54693d22018-07-17 10:46:29 -07002308 svm_msg_q_unlock (mq);
2309
Florin Coras99368312018-08-02 10:45:44 -07002310 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002311 {
Florin Coras99368312018-08-02 10:45:44 -07002312 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
2313 e = svm_msg_q_msg_data (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002314 add_event = 0;
2315 switch (e->event_type)
2316 {
2317 case FIFO_EVENT_APP_RX:
2318 sid = e->fifo->client_session_index;
Florin Coras54693d22018-07-17 10:46:29 -07002319 session = vcl_session_get (sid);
2320 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002321 if (!(EPOLLIN & session->vep.ev.events))
2322 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002323 add_event = 1;
2324 events[*num_ev].events |= EPOLLIN;
2325 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002326 break;
2327 case FIFO_EVENT_APP_TX:
2328 sid = e->fifo->client_session_index;
Florin Coras54693d22018-07-17 10:46:29 -07002329 session = vcl_session_get (sid);
2330 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002331 if (!(EPOLLOUT & session_events))
2332 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002333 add_event = 1;
2334 events[*num_ev].events |= EPOLLOUT;
2335 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002336 break;
2337 case SESSION_IO_EVT_CT_TX:
2338 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
2339 sid = vcl_session_index (session);
2340 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002341 if (!(EPOLLIN & session->vep.ev.events))
2342 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002343 add_event = 1;
2344 events[*num_ev].events |= EPOLLIN;
2345 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002346 break;
2347 case SESSION_IO_EVT_CT_RX:
2348 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
2349 sid = vcl_session_index (session);
2350 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002351 if (!(EPOLLOUT & session_events))
2352 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002353 add_event = 1;
2354 events[*num_ev].events |= EPOLLOUT;
2355 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002356 break;
2357 case SESSION_CTRL_EVT_ACCEPTED:
2358 accepted_msg = (session_accepted_msg_t *) e->data;
2359 handle = accepted_msg->listener_handle;
2360 session = vppcom_session_table_lookup_listener (handle);
2361 if (!session)
2362 {
2363 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2364 "listener handle %llx", getpid (), handle);
2365 break;
2366 }
2367
2368 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2369 vcl_msg->accepted_msg = *accepted_msg;
2370 session_events = session->vep.ev.events;
2371 if (!(EPOLLIN & session_events))
2372 break;
2373
2374 add_event = 1;
2375 events[*num_ev].events |= EPOLLIN;
2376 session_evt_data = session->vep.ev.data.u64;
2377 break;
2378 case SESSION_CTRL_EVT_CONNECTED:
2379 connected_msg = (session_connected_msg_t *) e->data;
2380 vcl_session_connected_handler (connected_msg);
2381 /* Generate EPOLLOUT because there's no connected event */
2382 sid = vcl_session_get_index_from_handle (connected_msg->handle);
2383 clib_spinlock_lock (&vcm->sessions_lockp);
2384 session = vcl_session_get (sid);
2385 session_events = session->vep.ev.events;
2386 if (EPOLLOUT & session_events)
2387 {
2388 add_event = 1;
2389 events[*num_ev].events |= EPOLLOUT;
2390 session_evt_data = session->vep.ev.data.u64;
2391 }
2392 clib_spinlock_unlock (&vcm->sessions_lockp);
2393 break;
2394 case SESSION_CTRL_EVT_DISCONNECTED:
2395 disconnected_msg = (session_disconnected_msg_t *) e->data;
2396 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
2397 clib_spinlock_lock (&vcm->sessions_lockp);
Florin Corasc9fbd662018-08-24 12:59:56 -07002398 if (!(session = vcl_session_get (sid)))
2399 break;
Florin Coras54693d22018-07-17 10:46:29 -07002400 add_event = 1;
2401 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2402 session_evt_data = session->vep.ev.data.u64;
2403 session_events = session->vep.ev.events;
2404 clib_spinlock_unlock (&vcm->sessions_lockp);
2405 break;
Florin Corasc9fbd662018-08-24 12:59:56 -07002406 case SESSION_CTRL_EVT_RESET:
2407 sid = vcl_reset_handler ((session_reset_msg_t *) e->data);
2408 if (!(session = vcl_session_get (sid)))
2409 break;
2410 add_event = 1;
2411 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2412 session_evt_data = session->vep.ev.data.u64;
2413 session_events = session->vep.ev.events;
2414 break;
Florin Coras54693d22018-07-17 10:46:29 -07002415 default:
2416 clib_warning ("unhandled: %u", e->event_type);
Florin Coras99368312018-08-02 10:45:44 -07002417 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002418 continue;
2419 }
Florin Coras99368312018-08-02 10:45:44 -07002420 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002421
2422 if (add_event)
2423 {
2424 events[*num_ev].data.u64 = session_evt_data;
2425 if (EPOLLONESHOT & session_events)
2426 {
2427 clib_spinlock_lock (&vcm->sessions_lockp);
2428 session = vcl_session_get (sid);
2429 session->vep.ev.events = 0;
2430 clib_spinlock_unlock (&vcm->sessions_lockp);
2431 }
2432 *num_ev += 1;
2433 if (*num_ev == maxevents)
2434 break;
2435 }
2436 }
Florin Coras99368312018-08-02 10:45:44 -07002437
2438 vec_reset_length (vcm->mq_msg_vector);
Florin Coras54693d22018-07-17 10:46:29 -07002439 return *num_ev;
2440}
2441
Florin Coras99368312018-08-02 10:45:44 -07002442static int
2443vppcom_epoll_wait_condvar (struct epoll_event *events, int maxevents,
2444 double wait_for_time)
2445{
2446 vcl_cut_through_registration_t *cr;
2447 double total_wait = 0, wait_slice;
2448 u32 num_ev = 0;
2449 int rv;
2450
2451 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2452 wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
2453
2454 do
2455 {
2456 /* *INDENT-OFF* */
2457 pool_foreach (cr, vcm->cut_through_registrations, ({
2458 vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
2459 }));
2460 /* *INDENT-ON* */
2461
2462 rv = vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
2463 num_ev ? 0 : wait_slice, &num_ev);
2464 if (rv)
2465 total_wait += wait_slice;
2466 if (num_ev)
2467 return num_ev;
2468 }
2469 while (total_wait < wait_for_time);
2470 return (int) num_ev;
2471}
2472
2473static int
2474vppcom_epoll_wait_eventfd (struct epoll_event *events, int maxevents,
2475 double wait_for_time)
2476{
2477 vcl_mq_evt_conn_t *mqc;
2478 int __clib_unused n_read;
2479 int n_mq_evts, i;
2480 u32 n_evts = 0;
2481 u64 buf;
2482
2483 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
2484 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
2485 vec_len (vcm->mq_events), wait_for_time);
2486 for (i = 0; i < n_mq_evts; i++)
2487 {
2488 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
2489 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2490 vcl_epoll_wait_handle_mq (mqc->mq, events, maxevents, 0, &n_evts);
2491 }
2492
2493 return (int) n_evts;
2494}
2495
Dave Wallacef7f809c2017-10-03 01:48:42 -04002496int
2497vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2498 int maxevents, double wait_for_time)
2499{
Florin Coras7e12d942018-06-27 14:32:43 -07002500 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002501
2502 if (PREDICT_FALSE (maxevents <= 0))
2503 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002504 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002505 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002506 return VPPCOM_EINVAL;
2507 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002508
Florin Coras54693d22018-07-17 10:46:29 -07002509 clib_spinlock_lock (&vcm->sessions_lockp);
2510 vep_session = vcl_session_get (vep_idx);
2511 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002512 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002513 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002514 getpid (), vep_idx);
Florin Coras54693d22018-07-17 10:46:29 -07002515 clib_spinlock_unlock (&vcm->sessions_lockp);
2516 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002517 }
Florin Coras54693d22018-07-17 10:46:29 -07002518 clib_spinlock_unlock (&vcm->sessions_lockp);
2519
2520 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002521
Florin Coras99368312018-08-02 10:45:44 -07002522 if (vcm->cfg.use_mq_eventfd)
2523 return vppcom_epoll_wait_eventfd (events, maxevents, wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002524
Florin Coras99368312018-08-02 10:45:44 -07002525 return vppcom_epoll_wait_condvar (events, maxevents, wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002526}
2527
Dave Wallace35830af2017-10-09 01:43:42 -04002528int
2529vppcom_session_attr (uint32_t session_index, uint32_t op,
2530 void *buffer, uint32_t * buflen)
2531{
Florin Coras7e12d942018-06-27 14:32:43 -07002532 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002533 int rv = VPPCOM_OK;
2534 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07002535 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002536
Dave Wallace7e607a72018-06-18 18:41:32 -04002537 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002538
2539 ASSERT (session);
2540
Dave Wallace35830af2017-10-09 01:43:42 -04002541 switch (op)
2542 {
2543 case VPPCOM_ATTR_GET_NREAD:
Florin Coras54693d22018-07-17 10:46:29 -07002544 rv = vppcom_session_read_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002545 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2546 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002547 break;
2548
Dave Wallace227867f2017-11-13 21:21:53 -05002549 case VPPCOM_ATTR_GET_NWRITE:
2550 rv = vppcom_session_write_ready (session, session_index);
Florin Coras0d427d82018-06-27 03:24:07 -07002551 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2552 getpid (), session_index, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002553 break;
2554
2555 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002556 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002557 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002558 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2559 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002560 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002561 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2562 "is_nonblocking = %u", getpid (),
2563 session_index, *flags,
2564 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002565 }
2566 else
2567 rv = VPPCOM_EINVAL;
2568 break;
2569
2570 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002571 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002572 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002573 if (*flags & O_NONBLOCK)
2574 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2575 else
2576 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2577
Florin Coras0d427d82018-06-27 03:24:07 -07002578 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2579 " is_nonblocking = %u",
2580 getpid (), session_index, *flags,
2581 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002582 }
2583 else
2584 rv = VPPCOM_EINVAL;
2585 break;
2586
2587 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002588 if (PREDICT_TRUE (buffer && buflen &&
2589 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002590 {
Florin Coras7e12d942018-06-27 14:32:43 -07002591 ep->is_ip4 = session->transport.is_ip4;
2592 ep->port = session->transport.rmt_port;
2593 if (session->transport.is_ip4)
2594 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002595 sizeof (ip4_address_t));
2596 else
Florin Coras7e12d942018-06-27 14:32:43 -07002597 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002598 sizeof (ip6_address_t));
2599 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002600 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2601 "addr = %U, port %u", getpid (),
2602 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002603 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002604 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2605 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002606 }
2607 else
2608 rv = VPPCOM_EINVAL;
2609 break;
2610
2611 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002612 if (PREDICT_TRUE (buffer && buflen &&
2613 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002614 {
Florin Coras7e12d942018-06-27 14:32:43 -07002615 ep->is_ip4 = session->transport.is_ip4;
2616 ep->port = session->transport.lcl_port;
2617 if (session->transport.is_ip4)
2618 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002619 sizeof (ip4_address_t));
2620 else
Florin Coras7e12d942018-06-27 14:32:43 -07002621 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002622 sizeof (ip6_address_t));
2623 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002624 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2625 " addr = %U port %d", getpid (),
2626 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002627 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002628 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2629 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002630 }
2631 else
2632 rv = VPPCOM_EINVAL;
2633 break;
Stevenb5a11602017-10-11 09:59:30 -07002634
Dave Wallace048b1d62018-01-03 22:24:41 -05002635 case VPPCOM_ATTR_GET_LIBC_EPFD:
2636 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002637 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2638 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002639 break;
2640
2641 case VPPCOM_ATTR_SET_LIBC_EPFD:
2642 if (PREDICT_TRUE (buffer && buflen &&
2643 (*buflen == sizeof (session->libc_epfd))))
2644 {
2645 session->libc_epfd = *(int *) buffer;
2646 *buflen = sizeof (session->libc_epfd);
2647
Florin Coras0d427d82018-06-27 03:24:07 -07002648 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2649 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002650 }
2651 else
2652 rv = VPPCOM_EINVAL;
2653 break;
2654
2655 case VPPCOM_ATTR_GET_PROTOCOL:
2656 if (buffer && buflen && (*buflen >= sizeof (int)))
2657 {
Florin Coras7e12d942018-06-27 14:32:43 -07002658 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002659 *buflen = sizeof (int);
2660
Florin Coras0d427d82018-06-27 03:24:07 -07002661 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2662 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2663 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002664 }
2665 else
2666 rv = VPPCOM_EINVAL;
2667 break;
2668
2669 case VPPCOM_ATTR_GET_LISTEN:
2670 if (buffer && buflen && (*buflen >= sizeof (int)))
2671 {
2672 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2673 VCL_SESS_ATTR_LISTEN);
2674 *buflen = sizeof (int);
2675
Florin Coras0d427d82018-06-27 03:24:07 -07002676 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2677 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002678 }
2679 else
2680 rv = VPPCOM_EINVAL;
2681 break;
2682
2683 case VPPCOM_ATTR_GET_ERROR:
2684 if (buffer && buflen && (*buflen >= sizeof (int)))
2685 {
2686 *(int *) buffer = 0;
2687 *buflen = sizeof (int);
2688
Florin Coras0d427d82018-06-27 03:24:07 -07002689 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2690 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002691 }
2692 else
2693 rv = VPPCOM_EINVAL;
2694 break;
2695
2696 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2697 if (buffer && buflen && (*buflen >= sizeof (u32)))
2698 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002699
2700 /* VPP-TBD */
2701 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002702 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002703 vcm->cfg.tx_fifo_size);
2704 *buflen = sizeof (u32);
2705
Florin Coras0d427d82018-06-27 03:24:07 -07002706 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2707 "buflen %d, #VPP-TBD#", getpid (),
2708 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002709 }
2710 else
2711 rv = VPPCOM_EINVAL;
2712 break;
2713
2714 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2715 if (buffer && buflen && (*buflen == sizeof (u32)))
2716 {
2717 /* VPP-TBD */
2718 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002719 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2720 "buflen %d, #VPP-TBD#", getpid (),
2721 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002722 }
2723 else
2724 rv = VPPCOM_EINVAL;
2725 break;
2726
2727 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2728 if (buffer && buflen && (*buflen >= sizeof (u32)))
2729 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002730
2731 /* VPP-TBD */
2732 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002733 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002734 vcm->cfg.rx_fifo_size);
2735 *buflen = sizeof (u32);
2736
Florin Coras0d427d82018-06-27 03:24:07 -07002737 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2738 "buflen %d, #VPP-TBD#", getpid (),
2739 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002740 }
2741 else
2742 rv = VPPCOM_EINVAL;
2743 break;
2744
2745 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2746 if (buffer && buflen && (*buflen == sizeof (u32)))
2747 {
2748 /* VPP-TBD */
2749 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002750 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2751 "buflen %d, #VPP-TBD#", getpid (),
2752 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002753 }
2754 else
2755 rv = VPPCOM_EINVAL;
2756 break;
2757
2758 case VPPCOM_ATTR_GET_REUSEADDR:
2759 if (buffer && buflen && (*buflen >= sizeof (int)))
2760 {
2761 /* VPP-TBD */
2762 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2763 VCL_SESS_ATTR_REUSEADDR);
2764 *buflen = sizeof (int);
2765
Florin Coras0d427d82018-06-27 03:24:07 -07002766 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2767 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002768 }
2769 else
2770 rv = VPPCOM_EINVAL;
2771 break;
2772
Stevenb5a11602017-10-11 09:59:30 -07002773 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002774 if (buffer && buflen && (*buflen == sizeof (int)) &&
2775 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2776 {
2777 /* VPP-TBD */
2778 if (*(int *) buffer)
2779 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2780 else
2781 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2782
Florin Coras0d427d82018-06-27 03:24:07 -07002783 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2784 " #VPP-TBD#", getpid (),
2785 VCL_SESS_ATTR_TEST (session->attr,
2786 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002787 }
2788 else
2789 rv = VPPCOM_EINVAL;
2790 break;
2791
2792 case VPPCOM_ATTR_GET_REUSEPORT:
2793 if (buffer && buflen && (*buflen >= sizeof (int)))
2794 {
2795 /* VPP-TBD */
2796 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2797 VCL_SESS_ATTR_REUSEPORT);
2798 *buflen = sizeof (int);
2799
Florin Coras0d427d82018-06-27 03:24:07 -07002800 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2801 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002802 }
2803 else
2804 rv = VPPCOM_EINVAL;
2805 break;
2806
2807 case VPPCOM_ATTR_SET_REUSEPORT:
2808 if (buffer && buflen && (*buflen == sizeof (int)) &&
2809 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2810 {
2811 /* VPP-TBD */
2812 if (*(int *) buffer)
2813 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2814 else
2815 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2816
Florin Coras0d427d82018-06-27 03:24:07 -07002817 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2818 " #VPP-TBD#", getpid (),
2819 VCL_SESS_ATTR_TEST (session->attr,
2820 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002821 }
2822 else
2823 rv = VPPCOM_EINVAL;
2824 break;
2825
2826 case VPPCOM_ATTR_GET_BROADCAST:
2827 if (buffer && buflen && (*buflen >= sizeof (int)))
2828 {
2829 /* VPP-TBD */
2830 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2831 VCL_SESS_ATTR_BROADCAST);
2832 *buflen = sizeof (int);
2833
Florin Coras0d427d82018-06-27 03:24:07 -07002834 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2835 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002836 }
2837 else
2838 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002839 break;
2840
2841 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002842 if (buffer && buflen && (*buflen == sizeof (int)))
2843 {
2844 /* VPP-TBD */
2845 if (*(int *) buffer)
2846 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2847 else
2848 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2849
Florin Coras0d427d82018-06-27 03:24:07 -07002850 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2851 "#VPP-TBD#", getpid (),
2852 VCL_SESS_ATTR_TEST (session->attr,
2853 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002854 }
2855 else
2856 rv = VPPCOM_EINVAL;
2857 break;
2858
2859 case VPPCOM_ATTR_GET_V6ONLY:
2860 if (buffer && buflen && (*buflen >= sizeof (int)))
2861 {
2862 /* VPP-TBD */
2863 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2864 VCL_SESS_ATTR_V6ONLY);
2865 *buflen = sizeof (int);
2866
Florin Coras0d427d82018-06-27 03:24:07 -07002867 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2868 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002869 }
2870 else
2871 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002872 break;
2873
2874 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002875 if (buffer && buflen && (*buflen == sizeof (int)))
2876 {
2877 /* VPP-TBD */
2878 if (*(int *) buffer)
2879 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2880 else
2881 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2882
Florin Coras0d427d82018-06-27 03:24:07 -07002883 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2884 "#VPP-TBD#", getpid (),
2885 VCL_SESS_ATTR_TEST (session->attr,
2886 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002887 }
2888 else
2889 rv = VPPCOM_EINVAL;
2890 break;
2891
2892 case VPPCOM_ATTR_GET_KEEPALIVE:
2893 if (buffer && buflen && (*buflen >= sizeof (int)))
2894 {
2895 /* VPP-TBD */
2896 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2897 VCL_SESS_ATTR_KEEPALIVE);
2898 *buflen = sizeof (int);
2899
Florin Coras0d427d82018-06-27 03:24:07 -07002900 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2901 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002902 }
2903 else
2904 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002905 break;
Stevenbccd3392017-10-12 20:42:21 -07002906
2907 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002908 if (buffer && buflen && (*buflen == sizeof (int)))
2909 {
2910 /* VPP-TBD */
2911 if (*(int *) buffer)
2912 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2913 else
2914 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2915
Florin Coras0d427d82018-06-27 03:24:07 -07002916 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2917 "#VPP-TBD#", getpid (),
2918 VCL_SESS_ATTR_TEST (session->attr,
2919 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002920 }
2921 else
2922 rv = VPPCOM_EINVAL;
2923 break;
2924
2925 case VPPCOM_ATTR_GET_TCP_NODELAY:
2926 if (buffer && buflen && (*buflen >= sizeof (int)))
2927 {
2928 /* VPP-TBD */
2929 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2930 VCL_SESS_ATTR_TCP_NODELAY);
2931 *buflen = sizeof (int);
2932
Florin Coras0d427d82018-06-27 03:24:07 -07002933 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2934 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002935 }
2936 else
2937 rv = VPPCOM_EINVAL;
2938 break;
2939
2940 case VPPCOM_ATTR_SET_TCP_NODELAY:
2941 if (buffer && buflen && (*buflen == sizeof (int)))
2942 {
2943 /* VPP-TBD */
2944 if (*(int *) buffer)
2945 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2946 else
2947 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2948
Florin Coras0d427d82018-06-27 03:24:07 -07002949 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2950 "#VPP-TBD#", getpid (),
2951 VCL_SESS_ATTR_TEST (session->attr,
2952 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002953 }
2954 else
2955 rv = VPPCOM_EINVAL;
2956 break;
2957
2958 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2959 if (buffer && buflen && (*buflen >= sizeof (int)))
2960 {
2961 /* VPP-TBD */
2962 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2963 VCL_SESS_ATTR_TCP_KEEPIDLE);
2964 *buflen = sizeof (int);
2965
Florin Coras0d427d82018-06-27 03:24:07 -07002966 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2967 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002968 }
2969 else
2970 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07002971 break;
2972
2973 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002974 if (buffer && buflen && (*buflen == sizeof (int)))
2975 {
2976 /* VPP-TBD */
2977 if (*(int *) buffer)
2978 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2979 else
2980 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2981
Florin Coras0d427d82018-06-27 03:24:07 -07002982 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2983 "#VPP-TBD#", getpid (),
2984 VCL_SESS_ATTR_TEST (session->attr,
2985 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002986 }
2987 else
2988 rv = VPPCOM_EINVAL;
2989 break;
2990
2991 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2992 if (buffer && buflen && (*buflen >= sizeof (int)))
2993 {
2994 /* VPP-TBD */
2995 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2996 VCL_SESS_ATTR_TCP_KEEPINTVL);
2997 *buflen = sizeof (int);
2998
Florin Coras0d427d82018-06-27 03:24:07 -07002999 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3000 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003001 }
3002 else
3003 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003004 break;
3005
3006 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003007 if (buffer && buflen && (*buflen == sizeof (int)))
3008 {
3009 /* VPP-TBD */
3010 if (*(int *) buffer)
3011 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3012 else
3013 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3014
Florin Coras0d427d82018-06-27 03:24:07 -07003015 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3016 "#VPP-TBD#", getpid (),
3017 VCL_SESS_ATTR_TEST (session->attr,
3018 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003019 }
3020 else
3021 rv = VPPCOM_EINVAL;
3022 break;
3023
3024 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3025 if (buffer && buflen && (*buflen >= sizeof (u32)))
3026 {
3027 /* VPP-TBD */
3028 *(u32 *) buffer = session->user_mss;
3029 *buflen = sizeof (int);
3030
Florin Coras0d427d82018-06-27 03:24:07 -07003031 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3032 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003033 }
3034 else
3035 rv = VPPCOM_EINVAL;
3036 break;
3037
3038 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3039 if (buffer && buflen && (*buflen == sizeof (u32)))
3040 {
3041 /* VPP-TBD */
3042 session->user_mss = *(u32 *) buffer;
3043
Florin Coras0d427d82018-06-27 03:24:07 -07003044 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3045 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003046 }
3047 else
3048 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003049 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003050
3051 default:
3052 rv = VPPCOM_EINVAL;
3053 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003054 }
3055
3056done:
Dave Wallace7e607a72018-06-18 18:41:32 -04003057 VCL_SESSION_UNLOCK ();
Dave Wallace35830af2017-10-09 01:43:42 -04003058 return rv;
3059}
3060
Stevenac1f96d2017-10-24 16:03:58 -07003061int
3062vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3063 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3064{
Stevenac1f96d2017-10-24 16:03:58 -07003065 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003066 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003067
3068 if (ep)
3069 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003070 VCL_SESSION_LOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07003071 rv = vppcom_session_at_index (session_index, &session);
3072 if (PREDICT_FALSE (rv))
3073 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003074 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07003075 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3076 getpid (), session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04003077 VCL_SESSION_UNLOCK ();
Florin Coras460dce62018-07-27 05:45:06 -07003078 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003079 }
Florin Coras7e12d942018-06-27 14:32:43 -07003080 ep->is_ip4 = session->transport.is_ip4;
3081 ep->port = session->transport.rmt_port;
Dave Wallace7e607a72018-06-18 18:41:32 -04003082 VCL_SESSION_UNLOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07003083 }
Steven58f464e2017-10-25 12:33:12 -07003084
3085 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07003086 rv = vppcom_session_read (session_index, buffer, buflen);
3087 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07003088 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003089 else
3090 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003091 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3092 getpid (), flags);
Florin Coras460dce62018-07-27 05:45:06 -07003093 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003094 }
3095
Florin Coras99368312018-08-02 10:45:44 -07003096 if (ep)
3097 {
3098 if (session->transport.is_ip4)
3099 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3100 sizeof (ip4_address_t));
3101 else
3102 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3103 sizeof (ip6_address_t));
3104 }
Florin Coras460dce62018-07-27 05:45:06 -07003105
Stevenac1f96d2017-10-24 16:03:58 -07003106 return rv;
3107}
3108
3109int
3110vppcom_session_sendto (uint32_t session_index, void *buffer,
3111 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3112{
Dave Wallace617dffa2017-10-26 14:47:06 -04003113 if (!buffer)
3114 return VPPCOM_EINVAL;
3115
3116 if (ep)
3117 {
3118 // TBD
3119 return VPPCOM_EINVAL;
3120 }
3121
3122 if (flags)
3123 {
3124 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003125 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3126 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003127 }
3128
3129 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003130}
3131
Dave Wallace048b1d62018-01-03 22:24:41 -05003132int
3133vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3134{
3135 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3136 u32 i, keep_trying = 1;
3137 int rv, num_ev = 0;
3138
Florin Coras0d427d82018-06-27 03:24:07 -07003139 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3140 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003141
3142 if (!vp)
3143 return VPPCOM_EFAULT;
3144
3145 do
3146 {
Florin Coras7e12d942018-06-27 14:32:43 -07003147 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003148
3149 for (i = 0; i < n_sids; i++)
3150 {
3151 ASSERT (vp[i].revents);
3152
Dave Wallace7e607a72018-06-18 18:41:32 -04003153 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3154 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05003155
3156 if (*vp[i].revents)
3157 *vp[i].revents = 0;
3158
3159 if (POLLIN & vp[i].events)
3160 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003161 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Florin Coras54693d22018-07-17 10:46:29 -07003162 rv = vppcom_session_read_ready (session);
Dave Wallace7e607a72018-06-18 18:41:32 -04003163 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05003164 if (rv > 0)
3165 {
3166 *vp[i].revents |= POLLIN;
3167 num_ev++;
3168 }
3169 else if (rv < 0)
3170 {
3171 switch (rv)
3172 {
3173 case VPPCOM_ECONNRESET:
3174 *vp[i].revents = POLLHUP;
3175 break;
3176
3177 default:
3178 *vp[i].revents = POLLERR;
3179 break;
3180 }
3181 num_ev++;
3182 }
3183 }
3184
3185 if (POLLOUT & vp[i].events)
3186 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003187 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003188 rv = vppcom_session_write_ready (session, vp[i].sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04003189 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05003190 if (rv > 0)
3191 {
3192 *vp[i].revents |= POLLOUT;
3193 num_ev++;
3194 }
3195 else if (rv < 0)
3196 {
3197 switch (rv)
3198 {
3199 case VPPCOM_ECONNRESET:
3200 *vp[i].revents = POLLHUP;
3201 break;
3202
3203 default:
3204 *vp[i].revents = POLLERR;
3205 break;
3206 }
3207 num_ev++;
3208 }
3209 }
3210
Dave Wallace7e607a72018-06-18 18:41:32 -04003211 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003212 {
3213 done:
3214 *vp[i].revents = POLLNVAL;
3215 num_ev++;
3216 }
3217 }
3218 if (wait_for_time != -1)
3219 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3220 }
3221 while ((num_ev == 0) && keep_trying);
3222
3223 if (VPPCOM_DEBUG > 3)
3224 {
3225 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3226 for (i = 0; i < n_sids; i++)
3227 {
3228 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3229 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3230 vp[i].events, *vp[i].revents);
3231 }
3232 }
3233 return num_ev;
3234}
3235
Florin Coras99368312018-08-02 10:45:44 -07003236int
3237vppcom_mq_epoll_fd (void)
3238{
3239 return vcm->mqs_epfd;
3240}
3241
Dave Wallacee22aa742017-10-20 12:30:38 -04003242/*
3243 * fd.io coding-style-patch-verification: ON
3244 *
3245 * Local Variables:
3246 * eval: (c-set-style "gnu")
3247 * End:
3248 */