2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <svm/svm_fifo_segment.h>
19 #include <vcl/vppcom.h>
20 #include <vcl/vcl_event.h>
21 #include <vcl/vcl_debug.h>
22 #include <vcl/vcl_private.h>
24 __thread uword __vcl_worker_index = ~0;
29 sigsegv_signal (int signum)
35 vcl_wait_for_memory (void *mem)
37 u8 __clib_unused test;
38 if (vcm->mounting_segment)
40 while (vcm->mounting_segment)
49 if (signal (SIGSEGV, sigsegv_signal))
65 signal (SIGSEGV, SIG_DFL);
69 vppcom_session_state_str (session_state_t state)
91 case STATE_CLOSE_ON_EMPTY:
92 st = "STATE_CLOSE_ON_EMPTY";
95 case STATE_DISCONNECT:
96 st = "STATE_DISCONNECT";
104 st = "UNKNOWN_STATE";
112 format_ip4_address (u8 * s, va_list * args)
114 u8 *a = va_arg (*args, u8 *);
115 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
119 format_ip6_address (u8 * s, va_list * args)
121 ip6_address_t *a = va_arg (*args, ip6_address_t *);
122 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
124 i_max_n_zero = ARRAY_LEN (a->as_u16);
126 i_first_zero = i_max_n_zero;
128 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
130 u32 is_zero = a->as_u16[i] == 0;
131 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
137 if ((!is_zero && n_zeros > max_n_zeros)
138 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
140 i_max_n_zero = i_first_zero;
141 max_n_zeros = n_zeros;
142 i_first_zero = ARRAY_LEN (a->as_u16);
147 last_double_colon = 0;
148 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
150 if (i == i_max_n_zero && max_n_zeros > 1)
152 s = format (s, "::");
153 i += max_n_zeros - 1;
154 last_double_colon = 1;
158 s = format (s, "%s%x",
159 (last_double_colon || i == 0) ? "" : ":",
160 clib_net_to_host_u16 (a->as_u16[i]));
161 last_double_colon = 0;
168 /* Format an IP46 address. */
170 format_ip46_address (u8 * s, va_list * args)
172 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
173 ip46_type_t type = va_arg (*args, ip46_type_t);
179 is_ip4 = ip46_address_is_ip4 (ip46);
190 format (s, "%U", format_ip4_address, &ip46->ip4) :
191 format (s, "%U", format_ip6_address, &ip46->ip6);
195 * VPPCOM Utility Functions
200 vcl_session_vpp_evt_q (vcl_worker_t * wrk, vcl_session_t * s)
202 if (vcl_session_is_ct (s))
203 return wrk->vpp_event_queues[0];
205 return wrk->vpp_event_queues[s->tx_fifo->master_thread_index];
209 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
210 session_handle_t handle, int retval)
212 app_session_evt_t _app_evt, *app_evt = &_app_evt;
213 session_accepted_reply_msg_t *rmp;
214 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
215 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
216 rmp->handle = handle;
217 rmp->context = context;
218 rmp->retval = retval;
219 app_send_ctrl_evt_to_vpp (mq, app_evt);
223 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
224 session_handle_t handle, int retval)
226 app_session_evt_t _app_evt, *app_evt = &_app_evt;
227 session_disconnected_reply_msg_t *rmp;
228 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
229 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
230 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
231 rmp->handle = handle;
232 rmp->context = context;
233 rmp->retval = retval;
234 app_send_ctrl_evt_to_vpp (mq, app_evt);
238 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
239 session_handle_t handle, int retval)
241 app_session_evt_t _app_evt, *app_evt = &_app_evt;
242 session_reset_reply_msg_t *rmp;
243 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
244 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
245 rmp->handle = handle;
246 rmp->context = context;
247 rmp->retval = retval;
248 app_send_ctrl_evt_to_vpp (mq, app_evt);
252 vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
254 vcl_session_t *session, *listen_session;
255 svm_fifo_t *rx_fifo, *tx_fifo;
259 session = vcl_session_alloc (wrk);
261 listen_session = vcl_session_table_lookup_listener (wrk,
262 mp->listener_handle);
266 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
267 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
268 "unknown vpp listener handle %llx",
269 getpid (), mp->listener_handle);
270 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
271 VNET_API_ERROR_INVALID_ARGUMENT);
272 vcl_session_free (wrk, session);
273 return VCL_INVALID_SESSION_INDEX;
276 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
277 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
279 if (mp->server_event_queue_address)
281 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
283 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
285 vcl_wait_for_memory (session->vpp_evt_q);
286 rx_fifo->master_session_index = session->session_index;
287 tx_fifo->master_session_index = session->session_index;
288 vec_validate (wrk->vpp_event_queues, 0);
289 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
290 wrk->vpp_event_queues[0] = evt_q;
294 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
296 rx_fifo->client_session_index = session->session_index;
297 tx_fifo->client_session_index = session->session_index;
299 vpp_wrk_index = tx_fifo->master_thread_index;
300 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
301 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
304 session->vpp_handle = mp->handle;
305 session->client_context = mp->context;
306 session->rx_fifo = rx_fifo;
307 session->tx_fifo = tx_fifo;
309 session->session_state = STATE_ACCEPT;
310 session->transport.rmt_port = mp->port;
311 session->transport.is_ip4 = mp->is_ip4;
312 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
314 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
315 session->transport.lcl_port = listen_session->transport.lcl_port;
316 session->transport.lcl_ip = listen_session->transport.lcl_ip;
317 session->session_type = listen_session->session_type;
318 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
320 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
321 " address %U port %d queue %p!", getpid (), mp->handle,
322 session->session_index,
323 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
324 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
325 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
326 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
328 return session->session_index;
332 vcl_session_connected_handler (vcl_worker_t * wrk,
333 session_connected_msg_t * mp)
335 u32 session_index, vpp_wrk_index;
336 svm_fifo_t *rx_fifo, *tx_fifo;
337 vcl_session_t *session = 0;
340 session_index = mp->context;
341 session = vcl_session_get (wrk, session_index);
344 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
345 "Invalid session index (%u)!",
346 getpid (), mp->handle, session_index);
347 return VCL_INVALID_SESSION_INDEX;
351 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
352 mp->handle, session_index, format_api_error,
354 session->session_state = STATE_FAILED;
355 session->vpp_handle = mp->handle;
356 return session_index;
359 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
360 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
361 vcl_wait_for_memory (rx_fifo);
362 rx_fifo->client_session_index = session_index;
363 tx_fifo->client_session_index = session_index;
365 if (mp->client_event_queue_address)
367 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
369 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
372 vec_validate (wrk->vpp_event_queues, 0);
373 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
374 wrk->vpp_event_queues[0] = evt_q;
378 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
380 vpp_wrk_index = tx_fifo->master_thread_index;
381 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
382 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
385 session->rx_fifo = rx_fifo;
386 session->tx_fifo = tx_fifo;
387 session->vpp_handle = mp->handle;
388 session->transport.is_ip4 = mp->is_ip4;
389 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
390 sizeof (session->transport.lcl_ip));
391 session->transport.lcl_port = mp->lcl_port;
392 session->session_state = STATE_CONNECT;
394 /* Add it to lookup table */
395 hash_set (wrk->session_index_by_vpp_handles, mp->handle, session_index);
397 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
398 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
399 getpid (), mp->handle, session_index, session->rx_fifo,
400 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
402 return session_index;
406 vcl_session_reset_handler (vcl_worker_t * wrk,
407 session_reset_msg_t * reset_msg)
409 vcl_session_t *session;
412 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
413 session = vcl_session_get (wrk, sid);
416 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
417 return VCL_INVALID_SESSION_INDEX;
419 session->session_state = STATE_CLOSE_ON_EMPTY;
420 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
421 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
422 vcm->my_client_index, reset_msg->handle, 0);
427 vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
429 vcl_session_t *session;
430 u32 sid = mp->context;
432 session = vcl_session_get (wrk, sid);
435 VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
436 getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
439 session->session_state = STATE_FAILED;
440 session->vpp_handle = mp->handle;
445 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
446 "Invalid session index (%u)!",
447 getpid (), mp->handle, sid);
448 return VCL_INVALID_SESSION_INDEX;
452 session->vpp_handle = mp->handle;
453 session->transport.is_ip4 = mp->lcl_is_ip4;
454 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
455 sizeof (ip46_address_t));
456 session->transport.lcl_port = mp->lcl_port;
457 vcl_session_table_add_listener (wrk, mp->handle, sid);
458 session->session_state = STATE_LISTEN;
460 if (session->is_dgram)
462 svm_fifo_t *rx_fifo, *tx_fifo;
463 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
464 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
465 rx_fifo->client_session_index = sid;
466 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
467 tx_fifo->client_session_index = sid;
468 session->rx_fifo = rx_fifo;
469 session->tx_fifo = tx_fifo;
472 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
473 getpid (), mp->handle, sid);
478 vcl_handle_mq_ctrl_event (vcl_worker_t * wrk, session_event_t * e)
480 session_accepted_msg_t *accepted_msg;
481 session_disconnected_msg_t *disconnected_msg;
482 vcl_session_msg_t *vcl_msg;
483 vcl_session_t *session;
487 switch (e->event_type)
489 case FIFO_EVENT_APP_RX:
490 clib_warning ("unhandled rx: sid %u (0x%x)",
491 e->fifo->client_session_index,
492 e->fifo->client_session_index);
494 case SESSION_CTRL_EVT_ACCEPTED:
495 accepted_msg = (session_accepted_msg_t *) e->data;
496 handle = accepted_msg->listener_handle;
497 session = vcl_session_table_lookup_listener (wrk, handle);
500 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
501 "listener handle %llx", getpid (), handle);
505 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
506 vcl_msg->accepted_msg = *accepted_msg;
508 case SESSION_CTRL_EVT_CONNECTED:
509 vcl_session_connected_handler (wrk,
510 (session_connected_msg_t *) e->data);
512 case SESSION_CTRL_EVT_DISCONNECTED:
513 disconnected_msg = (session_disconnected_msg_t *) e->data;
514 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
515 session = vcl_session_get (wrk, sid);
518 VDBG (0, "request to disconnect unknown handle 0x%llx",
519 disconnected_msg->handle);
522 session->session_state = STATE_DISCONNECT;
523 VDBG (0, "disconnected handle 0xllx, sid %u", disconnected_msg->handle,
526 case SESSION_CTRL_EVT_RESET:
527 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
529 case SESSION_CTRL_EVT_BOUND:
530 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
533 clib_warning ("unhandled %u", e->event_type);
539 vppcom_wait_for_session_state_change (u32 session_index,
540 session_state_t state,
543 vcl_worker_t *wrk = vcl_worker_get_current ();
544 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
545 vcl_session_t *volatile session;
551 session = vcl_session_get (wrk, session_index);
552 if (PREDICT_FALSE (!session))
554 return VPPCOM_EBADFD;
556 if (session->session_state & state)
560 if (session->session_state & STATE_FAILED)
562 return VPPCOM_ECONNREFUSED;
565 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
567 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
568 vcl_handle_mq_ctrl_event (wrk, e);
569 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
571 while (clib_time_now (&wrk->clib_time) < timeout);
573 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
574 vppcom_session_state_str (state));
575 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
577 return VPPCOM_ETIMEDOUT;
581 vppcom_app_session_enable (void)
585 if (vcm->app_state != STATE_APP_ENABLED)
587 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
588 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
589 if (PREDICT_FALSE (rv))
591 VDBG (0, "VCL<%d>: application session enable timed out! "
592 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
600 vppcom_app_attach (void)
604 vppcom_app_send_attach ();
605 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
606 if (PREDICT_FALSE (rv))
608 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
609 getpid (), rv, vppcom_retval_str (rv));
617 vppcom_session_unbind (u32 session_handle)
619 vcl_worker_t *wrk = vcl_worker_get_current ();
620 vcl_session_t *session = 0;
623 session = vcl_session_get_w_handle (wrk, session_handle);
625 return VPPCOM_EBADFD;
627 vpp_handle = session->vpp_handle;
628 vcl_session_table_del_listener (wrk, vpp_handle);
629 session->vpp_handle = ~0;
630 session->session_state = STATE_DISCONNECT;
632 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
633 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
634 vppcom_session_state_str (STATE_DISCONNECT));
635 vcl_evt (VCL_EVT_UNBIND, session);
636 vppcom_send_unbind_sock (vpp_handle);
642 vppcom_session_disconnect (u32 session_handle)
644 vcl_worker_t *wrk = vcl_worker_get_current ();
645 svm_msg_q_t *vpp_evt_q;
646 vcl_session_t *session;
647 session_state_t state;
650 session = vcl_session_get_w_handle (wrk, session_handle);
651 vpp_handle = session->vpp_handle;
652 state = session->session_state;
654 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
655 vpp_handle, session_handle, state, vppcom_session_state_str (state));
657 if (PREDICT_FALSE (state & STATE_LISTEN))
659 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
660 "Cannot disconnect a listen socket!",
661 getpid (), vpp_handle, session_handle);
662 return VPPCOM_EBADFD;
665 if (state & STATE_CLOSE_ON_EMPTY)
667 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
668 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
670 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
671 "REPLY...", getpid (), vpp_handle, session_handle);
675 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
676 getpid (), vpp_handle, session_handle);
677 vppcom_send_disconnect_session (vpp_handle);
684 * VPPCOM Public API functions
687 vppcom_app_create (char *app_name)
689 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
695 vppcom_cfg (&vcm->cfg);
698 vcm->main_cpu = pthread_self ();
699 vppcom_init_error_string_table ();
700 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
701 20 /* timeout in secs */ );
702 pool_init_fixed (vcm->workers, vcl_cfg->max_workers);
703 vcl_worker_alloc_and_init ();
706 if (vcm->my_client_index == ~0)
708 /* API hookup and connect to VPP */
709 vppcom_api_hookup ();
711 vcm->app_state = STATE_APP_START;
712 rv = vppcom_connect_to_vpp (app_name);
715 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
720 VDBG (0, "VCL<%d>: sending session enable", getpid ());
721 rv = vppcom_app_session_enable ();
724 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
725 "failed!", getpid ());
729 VDBG (0, "VCL<%d>: sending app attach", getpid ());
730 rv = vppcom_app_attach ();
733 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
738 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
739 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
746 vppcom_app_destroy (void)
749 f64 orig_app_timeout;
751 if (vcm->my_client_index == ~0)
754 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
755 getpid (), vcm->my_client_index, vcm->my_client_index);
756 vcl_evt (VCL_EVT_DETACH, vcm);
758 vppcom_app_send_detach ();
759 orig_app_timeout = vcm->cfg.app_timeout;
760 vcm->cfg.app_timeout = 2.0;
761 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
762 vcm->cfg.app_timeout = orig_app_timeout;
763 if (PREDICT_FALSE (rv))
764 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
765 getpid (), rv, vppcom_retval_str (rv));
768 vl_client_disconnect_from_vlib ();
769 vcm->my_client_index = ~0;
770 vcm->app_state = STATE_APP_START;
774 vppcom_session_create (u8 proto, u8 is_nonblocking)
776 vcl_worker_t *wrk = vcl_worker_get_current ();
777 vcl_session_t *session;
779 session = vcl_session_alloc (wrk);
781 session->session_type = proto;
782 session->session_state = STATE_START;
783 session->vpp_handle = ~0;
784 session->is_dgram = proto == VPPCOM_PROTO_UDP;
787 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
789 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
790 is_nonblocking, session_index);
792 VDBG (0, "VCL<%d>: sid %u", getpid (), session->session_index);
794 return vcl_session_handle (session);
798 vppcom_session_close (uint32_t session_handle)
800 vcl_worker_t *wrk = vcl_worker_get_current ();
801 vcl_session_t *session = 0;
802 u8 is_vep, is_vep_session;
803 session_state_t state;
808 session = vcl_session_get_w_handle (wrk, session_handle);
810 return VPPCOM_EBADFD;
812 is_vep = session->is_vep;
813 is_vep_session = session->is_vep_session;
814 next_sh = session->vep.next_sh;
815 vep_sh = session->vep.vep_sh;
816 state = session->session_state;
817 vpp_handle = session->vpp_handle;
819 if (VPPCOM_DEBUG > 0)
822 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
823 "closing epoll session...",
824 getpid (), session_handle, session_handle);
826 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
827 "closing session...",
828 getpid (), vpp_handle, session_handle);
833 while (next_sh != ~0)
835 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
836 if (PREDICT_FALSE (rv < 0))
837 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
838 "vep_idx %u failed! rv %d (%s)",
839 getpid (), vpp_handle, next_sh, vep_sh,
840 rv, vppcom_retval_str (rv));
842 next_sh = session->vep.next_sh;
849 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
851 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
852 "vep_idx %u failed! rv %d (%s)",
853 getpid (), vpp_handle, session_handle,
854 vep_sh, rv, vppcom_retval_str (rv));
857 if (state & STATE_LISTEN)
859 rv = vppcom_session_unbind (session_handle);
860 if (PREDICT_FALSE (rv < 0))
861 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
862 "failed! rv %d (%s)",
863 getpid (), vpp_handle, session_handle,
864 rv, vppcom_retval_str (rv));
866 else if (state & STATE_OPEN)
868 rv = vppcom_session_disconnect (session_handle);
869 if (PREDICT_FALSE (rv < 0))
870 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
871 "session disconnect failed! rv %d (%s)",
872 getpid (), vpp_handle, session_handle,
873 rv, vppcom_retval_str (rv));
877 if (vcl_session_is_ct (session))
879 vcl_cut_through_registration_t *ctr;
882 mq_addr = pointer_to_uword (session->our_evt_q);
883 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
885 if (ctr->epoll_evt_conn_index != ~0)
886 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
887 VDBG (0, "Removing ct registration %u",
888 vcl_ct_registration_index (wrk, ctr));
889 vcl_ct_registration_del (wrk, ctr);
890 vcl_ct_registration_lookup_del (wrk, mq_addr);
891 vcl_ct_registration_unlock (wrk);
894 if (vpp_handle != ~0)
896 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
898 vcl_session_free (wrk, session);
900 if (VPPCOM_DEBUG > 0)
903 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
904 getpid (), session_handle, session_handle);
906 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
907 getpid (), vpp_handle, session_handle);
910 vcl_evt (VCL_EVT_CLOSE, session, rv);
916 vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
918 vcl_worker_t *wrk = vcl_worker_get_current ();
919 vcl_session_t *session = 0;
922 return VPPCOM_EINVAL;
924 session = vcl_session_get_w_handle (wrk, session_handle);
926 return VPPCOM_EBADFD;
930 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
931 "bind to an epoll session!", getpid (), session_handle);
932 return VPPCOM_EBADFD;
935 session->transport.is_ip4 = ep->is_ip4;
937 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
938 sizeof (ip4_address_t));
940 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
941 sizeof (ip6_address_t));
942 session->transport.lcl_port = ep->port;
944 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
945 "proto %s", getpid (), session_handle,
946 session->transport.is_ip4 ? "IPv4" : "IPv6",
947 format_ip46_address, &session->transport.lcl_ip,
948 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
949 clib_net_to_host_u16 (session->transport.lcl_port),
950 session->session_type ? "UDP" : "TCP");
951 vcl_evt (VCL_EVT_BIND, session);
953 if (session->session_type == VPPCOM_PROTO_UDP)
954 vppcom_session_listen (session_handle, 10);
960 vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
962 vcl_worker_t *wrk = vcl_worker_get_current ();
963 vcl_session_t *listen_session = 0;
964 u64 listen_vpp_handle;
967 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
969 return VPPCOM_EBADFD;
971 if (q_len == 0 || q_len == ~0)
972 q_len = vcm->cfg.listen_queue_size;
974 if (listen_session->is_vep)
976 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
977 "epoll session!", getpid (), listen_sh);
978 return VPPCOM_EBADFD;
981 listen_vpp_handle = listen_session->vpp_handle;
982 if (listen_session->session_state & STATE_LISTEN)
984 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
985 getpid (), listen_vpp_handle, listen_sh);
989 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
990 "request...", getpid (), listen_vpp_handle, listen_sh);
993 * Send listen request to vpp and wait for reply
995 vppcom_send_bind_sock (listen_session);
996 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
998 vcm->cfg.session_timeout);
1000 if (PREDICT_FALSE (rv))
1002 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
1003 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1004 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1005 listen_sh, rv, vppcom_retval_str (rv));
1013 validate_args_session_accept_ (vcl_worker_t * wrk,
1014 vcl_session_t * listen_session)
1016 /* Input validation - expects spinlock on sessions_lockp */
1017 if (listen_session->is_vep)
1019 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1020 "epoll session!", getpid (),
1021 listen_session->session_index);
1022 return VPPCOM_EBADFD;
1025 if (listen_session->session_state != STATE_LISTEN)
1027 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1028 "not in listen state! state 0x%x (%s)", getpid (),
1029 listen_session->vpp_handle, listen_session->session_index,
1030 listen_session->session_state,
1031 vppcom_session_state_str (listen_session->session_state));
1032 return VPPCOM_EBADFD;
1038 vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
1041 u32 client_session_index = ~0, listen_session_index;
1042 vcl_worker_t *wrk = vcl_worker_get_current ();
1043 session_accepted_msg_t accepted_msg;
1044 vcl_session_t *listen_session = 0;
1045 vcl_session_t *client_session = 0;
1046 svm_msg_q_t *vpp_evt_q;
1047 vcl_session_msg_t *evt;
1048 u64 listen_vpp_handle;
1049 svm_msg_q_msg_t msg;
1054 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
1055 if (!listen_session)
1056 return VPPCOM_EBADFD;
1058 listen_session_index = listen_session->session_index;
1059 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
1062 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1064 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1065 accepted_msg = evt->accepted_msg;
1069 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1070 VCL_SESS_ATTR_NONBLOCK);
1071 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1072 return VPPCOM_EAGAIN;
1076 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
1077 return VPPCOM_EAGAIN;
1079 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
1080 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1082 clib_warning ("discarded event: %u", e->event_type);
1083 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1086 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1087 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
1093 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1094 listen_session = vcl_session_get (wrk, listen_session_index);
1095 client_session = vcl_session_get (wrk, client_session_index);
1097 if (flags & O_NONBLOCK)
1098 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1100 listen_vpp_handle = listen_session->vpp_handle;
1101 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1102 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1103 getpid (), listen_vpp_handle, listen_session_handle,
1104 client_session->vpp_handle, client_session_index,
1105 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1106 VCL_SESS_ATTR_NONBLOCK));
1110 ep->is_ip4 = client_session->transport.is_ip4;
1111 ep->port = client_session->transport.rmt_port;
1112 if (client_session->transport.is_ip4)
1113 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
1114 sizeof (ip4_address_t));
1116 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
1117 sizeof (ip6_address_t));
1120 if (accepted_msg.server_event_queue_address)
1121 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1124 vpp_evt_q = client_session->vpp_evt_q;
1126 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1127 client_session->vpp_handle, 0);
1129 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1130 "sid %u connection from peer %s address %U port %u to local %s "
1131 "address %U port %u", getpid (), listen_vpp_handle,
1132 listen_session_handle, client_session->vpp_handle,
1133 client_session_index,
1134 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1135 format_ip46_address, &client_session->transport.rmt_ip,
1136 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1137 clib_net_to_host_u16 (client_session->transport.rmt_port),
1138 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1139 format_ip46_address, &client_session->transport.lcl_ip,
1140 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1141 clib_net_to_host_u16 (client_session->transport.lcl_port));
1142 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1143 client_session_index);
1145 return vcl_session_handle (client_session);
1149 vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
1151 vcl_worker_t *wrk = vcl_worker_get_current ();
1152 vcl_session_t *session = 0;
1156 session = vcl_session_get_w_handle (wrk, session_handle);
1158 return VPPCOM_EBADFD;
1159 session_index = session->session_index;
1161 if (PREDICT_FALSE (session->is_vep))
1163 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1164 "connect on an epoll session!", getpid (),
1166 return VPPCOM_EBADFD;
1169 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1171 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1172 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1173 getpid (), session->vpp_handle, session_handle,
1174 session->transport.is_ip4 ? "IPv4" : "IPv6",
1175 format_ip46_address,
1176 &session->transport.rmt_ip, session->transport.is_ip4 ?
1177 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1178 clib_net_to_host_u16 (session->transport.rmt_port),
1179 session->session_type ? "UDP" : "TCP", session->session_state,
1180 vppcom_session_state_str (session->session_state));
1184 session->transport.is_ip4 = server_ep->is_ip4;
1185 if (session->transport.is_ip4)
1186 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
1187 sizeof (ip4_address_t));
1189 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
1190 sizeof (ip6_address_t));
1191 session->transport.rmt_port = server_ep->port;
1193 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1195 getpid (), session->vpp_handle, session_handle,
1196 session->transport.is_ip4 ? "IPv4" : "IPv6",
1197 format_ip46_address,
1198 &session->transport.rmt_ip, session->transport.is_ip4 ?
1199 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1200 clib_net_to_host_u16 (session->transport.rmt_port),
1201 session->session_type ? "UDP" : "TCP");
1204 * Send connect request and wait for reply from vpp
1206 vppcom_send_connect_sock (session);
1207 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1208 vcm->cfg.session_timeout);
1210 session = vcl_session_get (wrk, session_index);
1212 if (PREDICT_FALSE (rv))
1214 if (VPPCOM_DEBUG > 0)
1217 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1218 "failed! returning %d (%s)", getpid (),
1219 session->vpp_handle, session_handle, rv,
1220 vppcom_retval_str (rv));
1222 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1223 "returning %d (%s)", getpid (),
1224 session_handle, rv, vppcom_retval_str (rv));
1228 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1229 getpid (), session->vpp_handle, session_handle);
1235 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1238 return (e->event_type == FIFO_EVENT_APP_RX
1239 && e->fifo->client_session_index == sid);
1241 return (e->event_type == SESSION_IO_EVT_CT_TX);
1245 vcl_session_is_readable (vcl_session_t * s)
1247 return ((s->session_state & STATE_OPEN)
1248 || (s->session_state == STATE_LISTEN
1249 && s->session_type == VPPCOM_PROTO_UDP));
1253 vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
1256 vcl_worker_t *wrk = vcl_worker_get_current ();
1257 int n_read = 0, rv, is_nonblocking;
1258 vcl_session_t *s = 0;
1259 svm_fifo_t *rx_fifo;
1260 svm_msg_q_msg_t msg;
1265 if (PREDICT_FALSE (!buf))
1266 return VPPCOM_EINVAL;
1268 s = vcl_session_get_w_handle (wrk, session_handle);
1269 if (PREDICT_FALSE (!s))
1270 return VPPCOM_EBADFD;
1272 if (PREDICT_FALSE (s->is_vep))
1274 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1275 "read from an epoll session!", getpid (), session_handle);
1276 return VPPCOM_EBADFD;
1279 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1280 rx_fifo = s->rx_fifo;
1282 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1284 session_state_t state = s->session_state;
1285 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1287 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1288 "state 0x%x (%s), returning %d (%s)",
1289 getpid (), s->vpp_handle, session_handle, state,
1290 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1294 mq = vcl_session_is_ct (s) ? s->our_evt_q : wrk->app_event_queue;
1295 svm_fifo_unset_event (rx_fifo);
1296 is_full = svm_fifo_is_full (rx_fifo);
1298 if (svm_fifo_is_empty (rx_fifo))
1306 svm_msg_q_lock (mq);
1307 if (svm_msg_q_is_empty (mq))
1308 svm_msg_q_wait (mq);
1310 svm_msg_q_sub_w_lock (mq, &msg);
1311 e = svm_msg_q_msg_data (mq, &msg);
1312 svm_msg_q_unlock (mq);
1313 if (!vcl_is_rx_evt_for_session (e, s->session_index,
1316 vcl_handle_mq_ctrl_event (wrk, e);
1317 svm_msg_q_free_msg (mq, &msg);
1320 svm_fifo_unset_event (rx_fifo);
1321 svm_msg_q_free_msg (mq, &msg);
1322 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1324 if (svm_fifo_is_empty (rx_fifo))
1331 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1333 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1335 if (vcl_session_is_ct (s) && is_full)
1337 /* If the peer is not polling send notification */
1338 if (!svm_fifo_has_event (s->rx_fifo))
1339 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1340 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1343 if (VPPCOM_DEBUG > 2)
1346 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
1347 "from (%p)", getpid (), s->vpp_handle,
1348 session_handle, n_read, rx_fifo);
1350 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
1351 "returning %d (%s)", getpid (), s->vpp_handle,
1352 session_handle, n_read, vppcom_retval_str (n_read));
1358 vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
1360 return (vppcom_session_read_internal (session_handle, buf, n, 0));
1364 vppcom_session_peek (uint32_t session_handle, void *buf, int n)
1366 return (vppcom_session_read_internal (session_handle, buf, n, 1));
1370 vppcom_session_read_ready (vcl_session_t * session)
1372 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1373 if (PREDICT_FALSE (session->is_vep))
1375 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1376 "epoll session!", getpid (), session->session_index);
1377 return VPPCOM_EBADFD;
1380 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1382 session_state_t state = session->session_state;
1385 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1387 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1388 " state 0x%x (%s), returning %d (%s)", getpid (),
1389 session->vpp_handle, session->session_index, state,
1390 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1394 if (session->session_state & STATE_LISTEN)
1395 return clib_fifo_elts (session->accept_evts_fifo);
1397 return svm_fifo_max_dequeue (session->rx_fifo);
1401 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1404 return (e->event_type == FIFO_EVENT_APP_TX
1405 && e->fifo->client_session_index == sid);
1407 return (e->event_type == SESSION_IO_EVT_CT_RX);
1411 vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1413 vcl_worker_t *wrk = vcl_worker_get_current ();
1414 int rv, n_write, is_nonblocking;
1415 vcl_session_t *s = 0;
1416 svm_fifo_t *tx_fifo = 0;
1417 session_evt_type_t et;
1418 svm_msg_q_msg_t msg;
1422 if (PREDICT_FALSE (!buf))
1423 return VPPCOM_EINVAL;
1425 s = vcl_session_get_w_handle (wrk, session_handle);
1426 if (PREDICT_FALSE (!s))
1427 return VPPCOM_EBADFD;
1429 tx_fifo = s->tx_fifo;
1430 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1432 if (PREDICT_FALSE (s->is_vep))
1434 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1435 "cannot write to an epoll session!",
1436 getpid (), s->vpp_handle, session_handle);
1438 return VPPCOM_EBADFD;
1441 if (!(s->session_state & STATE_OPEN))
1443 session_state_t state = s->session_state;
1444 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1445 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1447 getpid (), s->vpp_handle, session_handle,
1448 state, vppcom_session_state_str (state));
1452 mq = vcl_session_is_ct (s) ? s->our_evt_q : wrk->app_event_queue;
1453 if (svm_fifo_is_full (tx_fifo))
1457 return VPPCOM_EWOULDBLOCK;
1459 while (svm_fifo_is_full (tx_fifo))
1461 svm_msg_q_lock (mq);
1462 while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1464 svm_msg_q_sub_w_lock (mq, &msg);
1465 e = svm_msg_q_msg_data (mq, &msg);
1466 svm_msg_q_unlock (mq);
1468 if (!vcl_is_tx_evt_for_session (e, s->session_index,
1470 vcl_handle_mq_ctrl_event (wrk, e);
1471 svm_msg_q_free_msg (mq, &msg);
1475 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1476 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1478 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1479 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1481 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1484 ASSERT (n_write > 0);
1486 if (VPPCOM_DEBUG > 2)
1489 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1490 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
1491 session_handle, tx_fifo);
1493 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1494 "wrote %d bytes tx-fifo: (%p)", getpid (),
1495 s->vpp_handle, session_handle, n_write, tx_fifo);
1500 static vcl_session_t *
1501 vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
1504 s = vcl_session_get (wrk, f->client_session_index);
1508 if (type == 0 && s->rx_fifo == f)
1511 if (type == 1 && s->tx_fifo == f)
1514 s = vcl_session_get (wrk, f->master_session_index);
1517 if (type == 0 && s->rx_fifo == f)
1519 if (type == 1 && s->tx_fifo == f)
1526 vppcom_session_write_ready (vcl_session_t * session)
1528 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1529 if (PREDICT_FALSE (session->is_vep))
1531 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1532 "cannot write to an epoll session!",
1533 getpid (), session->vpp_handle, session->session_index);
1534 return VPPCOM_EBADFD;
1537 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1539 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1540 "cannot write to a listen session!",
1541 getpid (), session->vpp_handle, session->session_index);
1542 return VPPCOM_EBADFD;
1545 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1547 session_state_t state = session->session_state;
1550 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1551 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1552 "session is not open! state 0x%x (%s), "
1553 "returning %d (%s)", getpid (), session->vpp_handle,
1554 session->session_index,
1555 state, vppcom_session_state_str (state),
1556 rv, vppcom_retval_str (rv));
1560 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1561 getpid (), session->vpp_handle, session->session_index,
1562 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
1564 return svm_fifo_max_enqueue (session->tx_fifo);
1568 vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
1570 svm_msg_q_msg_t *msg;
1574 n_msgs = svm_msg_q_size (mq);
1575 for (i = 0; i < n_msgs; i++)
1577 vec_add2 (wrk->mq_msg_vector, msg, 1);
1578 svm_msg_q_sub_w_lock (mq, msg);
1583 #define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1584 if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1586 svm_fifo_unset_event (_fifo); \
1587 if (svm_fifo_is_empty (_fifo)) \
1592 vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1593 unsigned long n_bits, unsigned long *read_map,
1594 unsigned long *write_map, unsigned long *except_map,
1595 double time_to_wait, u32 * bits_set)
1597 session_disconnected_msg_t *disconnected_msg;
1598 session_connected_msg_t *connected_msg;
1599 session_accepted_msg_t *accepted_msg;
1600 vcl_session_msg_t *vcl_msg;
1601 vcl_session_t *session;
1602 svm_msg_q_msg_t *msg;
1607 svm_msg_q_lock (mq);
1608 if (svm_msg_q_is_empty (mq))
1612 svm_msg_q_unlock (mq);
1618 svm_msg_q_unlock (mq);
1621 else if (time_to_wait < 0)
1623 svm_msg_q_wait (mq);
1627 if (svm_msg_q_timedwait (mq, time_to_wait))
1629 svm_msg_q_unlock (mq);
1634 vcl_mq_dequeue_batch (wrk, mq);
1635 svm_msg_q_unlock (mq);
1637 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1639 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1640 e = svm_msg_q_msg_data (mq, msg);
1641 switch (e->event_type)
1643 case FIFO_EVENT_APP_RX:
1644 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1645 sid = e->fifo->client_session_index;
1646 session = vcl_session_get (wrk, sid);
1649 if (sid < n_bits && read_map)
1651 clib_bitmap_set_no_check (read_map, sid, 1);
1655 case FIFO_EVENT_APP_TX:
1656 sid = e->fifo->client_session_index;
1657 session = vcl_session_get (wrk, sid);
1660 if (sid < n_bits && write_map)
1662 clib_bitmap_set_no_check (write_map, sid, 1);
1666 case SESSION_IO_EVT_CT_TX:
1667 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1668 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1671 sid = session->session_index;
1672 if (sid < n_bits && read_map)
1674 clib_bitmap_set_no_check (read_map, sid, 1);
1678 case SESSION_IO_EVT_CT_RX:
1679 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1682 sid = session->session_index;
1683 if (sid < n_bits && write_map)
1685 clib_bitmap_set_no_check (write_map, sid, 1);
1689 case SESSION_CTRL_EVT_ACCEPTED:
1690 accepted_msg = (session_accepted_msg_t *) e->data;
1691 handle = accepted_msg->listener_handle;
1692 session = vcl_session_table_lookup_listener (wrk, handle);
1695 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1696 "listener handle %llx", getpid (), handle);
1700 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1701 vcl_msg->accepted_msg = *accepted_msg;
1702 sid = session->session_index;
1703 if (sid < n_bits && read_map)
1705 clib_bitmap_set_no_check (read_map, sid, 1);
1709 case SESSION_CTRL_EVT_CONNECTED:
1710 connected_msg = (session_connected_msg_t *) e->data;
1711 vcl_session_connected_handler (wrk, connected_msg);
1713 case SESSION_CTRL_EVT_DISCONNECTED:
1714 disconnected_msg = (session_disconnected_msg_t *) e->data;
1715 sid = vcl_session_index_from_vpp_handle (wrk,
1716 disconnected_msg->handle);
1717 if (sid < n_bits && except_map)
1719 clib_bitmap_set_no_check (except_map, sid, 1);
1723 case SESSION_CTRL_EVT_RESET:
1724 sid = vcl_session_reset_handler (wrk,
1725 (session_reset_msg_t *) e->data);
1726 if (sid < n_bits && except_map)
1728 clib_bitmap_set_no_check (except_map, sid, 1);
1733 clib_warning ("unhandled: %u", e->event_type);
1736 svm_msg_q_free_msg (mq, msg);
1739 vec_reset_length (wrk->mq_msg_vector);
1744 vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
1745 unsigned long *read_map, unsigned long *write_map,
1746 unsigned long *except_map, double time_to_wait,
1749 double total_wait = 0, wait_slice;
1750 vcl_cut_through_registration_t *cr;
1752 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1753 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
1756 vcl_ct_registration_lock (wrk);
1758 pool_foreach (cr, wrk->cut_through_registrations, ({
1759 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
1763 vcl_ct_registration_unlock (wrk);
1765 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1766 write_map, except_map, time_to_wait, bits_set);
1767 total_wait += wait_slice;
1771 while (total_wait < time_to_wait);
1777 vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
1778 unsigned long *read_map, unsigned long *write_map,
1779 unsigned long *except_map, double time_to_wait,
1782 vcl_mq_evt_conn_t *mqc;
1783 int __clib_unused n_read;
1787 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1788 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1789 vec_len (wrk->mq_events), time_to_wait);
1790 for (i = 0; i < n_mq_evts; i++)
1792 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
1793 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1794 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
1795 except_map, 0, bits_set);
1798 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1802 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1803 unsigned long *write_map, unsigned long *except_map,
1804 double time_to_wait)
1806 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1807 vcl_worker_t *wrk = vcl_worker_get_current ();
1808 vcl_session_t *session = 0;
1811 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1813 if (n_bits && read_map)
1815 clib_bitmap_validate (wrk->rd_bitmap, minbits);
1816 clib_memcpy (wrk->rd_bitmap, read_map,
1817 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1818 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1820 if (n_bits && write_map)
1822 clib_bitmap_validate (wrk->wr_bitmap, minbits);
1823 clib_memcpy (wrk->wr_bitmap, write_map,
1824 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1825 memset (write_map, 0,
1826 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
1828 if (n_bits && except_map)
1830 clib_bitmap_validate (wrk->ex_bitmap, minbits);
1831 clib_memcpy (wrk->ex_bitmap, except_map,
1832 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1833 memset (except_map, 0,
1834 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
1844 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
1845 if (!(session = vcl_session_get (wrk, sid)))
1847 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1849 return VPPCOM_EBADFD;
1852 rv = svm_fifo_is_full (session->tx_fifo);
1855 clib_bitmap_set_no_check (write_map, sid, 1);
1864 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
1865 if (!(session = vcl_session_get (wrk, sid)))
1867 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1869 return VPPCOM_EBADFD;
1872 rv = vppcom_session_read_ready (session);
1875 clib_bitmap_set_no_check (read_map, sid, 1);
1883 if (vcm->cfg.use_mq_eventfd)
1884 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
1885 time_to_wait, &bits_set);
1887 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
1888 time_to_wait, &bits_set);
1894 vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
1896 vcl_session_t *session;
1897 vppcom_epoll_t *vep;
1900 if (VPPCOM_DEBUG <= 1)
1903 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1904 session = vcl_session_get (wrk, vep_idx);
1905 if (PREDICT_FALSE (!session))
1907 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1908 getpid (), vep_idx);
1911 if (PREDICT_FALSE (!session->is_vep))
1913 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1914 getpid (), vep_idx);
1917 vep = &session->vep;
1918 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
1921 " is_vep_session = %u\n"
1922 " next_sid = 0x%x (%u)\n"
1923 " wait_cont_idx = 0x%x (%u)\n"
1924 "}\n", getpid (), vep_idx,
1925 session->is_vep, session->is_vep_session,
1926 vep->next_sh, vep->next_sh,
1927 session->wait_cont_idx, session->wait_cont_idx);
1929 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
1931 session = vcl_session_get (wrk, sid);
1932 if (PREDICT_FALSE (!session))
1934 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
1937 if (PREDICT_FALSE (session->is_vep))
1938 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1939 getpid (), vep_idx);
1940 else if (PREDICT_FALSE (!session->is_vep_session))
1942 clib_warning ("VCL<%d>: ERROR: session (%u) "
1943 "is not a vep session!", getpid (), sid);
1946 vep = &session->vep;
1947 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
1948 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
1949 "vep_idx (%u)!", getpid (),
1950 sid, session->vep.vep_sh, vep_idx);
1951 if (session->is_vep_session)
1953 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
1955 " next_sid = 0x%x (%u)\n"
1956 " prev_sid = 0x%x (%u)\n"
1957 " vep_idx = 0x%x (%u)\n"
1958 " ev.events = 0x%x\n"
1959 " ev.data.u64 = 0x%llx\n"
1963 vep->next_sh, vep->next_sh,
1964 vep->prev_sh, vep->prev_sh,
1965 vep->vep_sh, vep->vep_sh,
1966 vep->ev.events, vep->ev.data.u64, vep->et_mask);
1971 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
1972 getpid (), vep_idx);
1976 vppcom_epoll_create (void)
1978 vcl_worker_t *wrk = vcl_worker_get_current ();
1979 vcl_session_t *vep_session;
1981 vep_session = vcl_session_alloc (wrk);
1983 vep_session->is_vep = 1;
1984 vep_session->vep.vep_sh = ~0;
1985 vep_session->vep.next_sh = ~0;
1986 vep_session->vep.prev_sh = ~0;
1987 vep_session->wait_cont_idx = ~0;
1988 vep_session->vpp_handle = ~0;
1990 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
1991 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
1992 getpid (), vep_session->session_index, vep_session->session_index);
1994 return vcl_session_handle (vep_session);
1998 vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
1999 struct epoll_event *event)
2001 vcl_worker_t *wrk = vcl_worker_get_current ();
2002 vcl_session_t *vep_session;
2003 vcl_session_t *session;
2006 if (vep_handle == session_handle)
2008 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2009 getpid (), vep_handle);
2010 return VPPCOM_EINVAL;
2013 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2014 if (PREDICT_FALSE (!vep_session))
2016 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
2017 return VPPCOM_EBADFD;
2019 if (PREDICT_FALSE (!vep_session->is_vep))
2021 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2022 getpid (), vep_handle);
2023 return VPPCOM_EINVAL;
2026 ASSERT (vep_session->vep.vep_sh == ~0);
2027 ASSERT (vep_session->vep.prev_sh == ~0);
2029 session = vcl_session_get_w_handle (wrk, session_handle);
2030 if (PREDICT_FALSE (!session))
2032 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2033 getpid (), session_handle);
2034 return VPPCOM_EBADFD;
2036 if (PREDICT_FALSE (session->is_vep))
2038 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
2039 return VPPCOM_EINVAL;
2045 if (PREDICT_FALSE (!event))
2047 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2048 "epoll_event structure!", getpid ());
2049 return VPPCOM_EINVAL;
2051 if (vep_session->vep.next_sh != ~0)
2053 vcl_session_t *next_session;
2054 next_session = vcl_session_get_w_handle (wrk,
2055 vep_session->vep.next_sh);
2056 if (PREDICT_FALSE (!next_session))
2058 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2059 "vep.next_sid (%u) on vep_idx (%u)!",
2060 getpid (), vep_session->vep.next_sh, vep_handle);
2061 return VPPCOM_EBADFD;
2063 ASSERT (next_session->vep.prev_sh == vep_handle);
2064 next_session->vep.prev_sh = session_handle;
2066 session->vep.next_sh = vep_session->vep.next_sh;
2067 session->vep.prev_sh = vep_handle;
2068 session->vep.vep_sh = vep_handle;
2069 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2070 session->vep.ev = *event;
2071 session->is_vep = 0;
2072 session->is_vep_session = 1;
2073 vep_session->vep.next_sh = session_handle;
2075 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
2076 "data 0x%llx!", getpid (), vep_handle, session_handle,
2077 event->events, event->data.u64);
2078 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2082 if (PREDICT_FALSE (!event))
2084 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2085 "epoll_event structure!", getpid ());
2089 else if (PREDICT_FALSE (!session->is_vep_session))
2091 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2092 "not a vep session!", getpid (), session_handle);
2096 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2098 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2099 "vep_idx (%u) != vep_idx (%u)!",
2100 getpid (), session_handle,
2101 session->vep.vep_sh, vep_handle);
2105 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2106 session->vep.ev = *event;
2107 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2108 " data 0x%llx!", getpid (), vep_handle, session_handle,
2109 event->events, event->data.u64);
2113 if (PREDICT_FALSE (!session->is_vep_session))
2115 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2116 "not a vep session!", getpid (), session_handle);
2120 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
2122 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2123 "vep_idx (%u) != vep_idx (%u)!",
2124 getpid (), session_handle,
2125 session->vep.vep_sh, vep_handle);
2130 vep_session->wait_cont_idx =
2131 (vep_session->wait_cont_idx == session_handle) ?
2132 session->vep.next_sh : vep_session->wait_cont_idx;
2134 if (session->vep.prev_sh == vep_handle)
2135 vep_session->vep.next_sh = session->vep.next_sh;
2138 vcl_session_t *prev_session;
2139 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
2140 if (PREDICT_FALSE (!prev_session))
2142 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2143 "vep.prev_sid (%u) on sid (%u)!",
2144 getpid (), session->vep.prev_sh, session_handle);
2145 return VPPCOM_EBADFD;
2147 ASSERT (prev_session->vep.next_sh == session_handle);
2148 prev_session->vep.next_sh = session->vep.next_sh;
2150 if (session->vep.next_sh != ~0)
2152 vcl_session_t *next_session;
2153 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
2154 if (PREDICT_FALSE (!next_session))
2156 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2157 "vep.next_sid (%u) on sid (%u)!",
2158 getpid (), session->vep.next_sh, session_handle);
2159 return VPPCOM_EBADFD;
2161 ASSERT (next_session->vep.prev_sh == session_handle);
2162 next_session->vep.prev_sh = session->vep.prev_sh;
2165 memset (&session->vep, 0, sizeof (session->vep));
2166 session->vep.next_sh = ~0;
2167 session->vep.prev_sh = ~0;
2168 session->vep.vep_sh = ~0;
2169 session->is_vep_session = 0;
2170 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2171 getpid (), vep_handle, session_handle);
2172 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
2176 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2180 vep_verify_epoll_chain (wrk, vep_handle);
2187 vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2188 struct epoll_event *events, u32 maxevents,
2189 double wait_for_time, u32 * num_ev)
2191 session_disconnected_msg_t *disconnected_msg;
2192 session_connected_msg_t *connected_msg;
2193 session_accepted_msg_t *accepted_msg;
2194 u64 session_evt_data = ~0, handle;
2195 u32 sid = ~0, session_events;
2196 vcl_session_msg_t *vcl_msg;
2197 vcl_session_t *session;
2198 svm_msg_q_msg_t *msg;
2203 svm_msg_q_lock (mq);
2204 if (svm_msg_q_is_empty (mq))
2208 svm_msg_q_unlock (mq);
2211 else if (wait_for_time < 0)
2213 svm_msg_q_wait (mq);
2217 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2219 svm_msg_q_unlock (mq);
2224 vcl_mq_dequeue_batch (wrk, mq);
2225 svm_msg_q_unlock (mq);
2227 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
2229 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
2230 e = svm_msg_q_msg_data (mq, msg);
2232 switch (e->event_type)
2234 case FIFO_EVENT_APP_RX:
2235 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2236 sid = e->fifo->client_session_index;
2237 session = vcl_session_get (wrk, sid);
2238 session_events = session->vep.ev.events;
2239 if (!(EPOLLIN & session->vep.ev.events))
2242 events[*num_ev].events |= EPOLLIN;
2243 session_evt_data = session->vep.ev.data.u64;
2245 case FIFO_EVENT_APP_TX:
2246 sid = e->fifo->client_session_index;
2247 session = vcl_session_get (wrk, sid);
2248 session_events = session->vep.ev.events;
2249 if (!(EPOLLOUT & session_events))
2252 events[*num_ev].events |= EPOLLOUT;
2253 session_evt_data = session->vep.ev.data.u64;
2255 case SESSION_IO_EVT_CT_TX:
2256 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2257 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2258 sid = session->session_index;
2259 session_events = session->vep.ev.events;
2260 if (!(EPOLLIN & session->vep.ev.events))
2263 events[*num_ev].events |= EPOLLIN;
2264 session_evt_data = session->vep.ev.data.u64;
2266 case SESSION_IO_EVT_CT_RX:
2267 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2268 sid = session->session_index;
2269 session_events = session->vep.ev.events;
2270 if (!(EPOLLOUT & session_events))
2273 events[*num_ev].events |= EPOLLOUT;
2274 session_evt_data = session->vep.ev.data.u64;
2276 case SESSION_CTRL_EVT_ACCEPTED:
2277 accepted_msg = (session_accepted_msg_t *) e->data;
2278 handle = accepted_msg->listener_handle;
2279 session = vcl_session_table_lookup_listener (wrk, handle);
2282 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2283 "listener handle %llx", getpid (), handle);
2287 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2288 vcl_msg->accepted_msg = *accepted_msg;
2289 session_events = session->vep.ev.events;
2290 if (!(EPOLLIN & session_events))
2294 events[*num_ev].events |= EPOLLIN;
2295 session_evt_data = session->vep.ev.data.u64;
2297 case SESSION_CTRL_EVT_CONNECTED:
2298 connected_msg = (session_connected_msg_t *) e->data;
2299 vcl_session_connected_handler (wrk, connected_msg);
2300 /* Generate EPOLLOUT because there's no connected event */
2301 sid = vcl_session_index_from_vpp_handle (wrk,
2302 connected_msg->handle);
2303 session = vcl_session_get (wrk, sid);
2304 session_events = session->vep.ev.events;
2305 if (EPOLLOUT & session_events)
2308 events[*num_ev].events |= EPOLLOUT;
2309 session_evt_data = session->vep.ev.data.u64;
2312 case SESSION_CTRL_EVT_DISCONNECTED:
2313 disconnected_msg = (session_disconnected_msg_t *) e->data;
2314 sid = vcl_session_index_from_vpp_handle (wrk,
2315 disconnected_msg->handle);
2316 if (!(session = vcl_session_get (wrk, sid)))
2319 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2320 session_evt_data = session->vep.ev.data.u64;
2321 session_events = session->vep.ev.events;
2323 case SESSION_CTRL_EVT_RESET:
2324 sid = vcl_session_reset_handler (wrk,
2325 (session_reset_msg_t *) e->data);
2326 if (!(session = vcl_session_get (wrk, sid)))
2329 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2330 session_evt_data = session->vep.ev.data.u64;
2331 session_events = session->vep.ev.events;
2334 VDBG (0, "unhandled: %u", e->event_type);
2335 svm_msg_q_free_msg (mq, msg);
2338 svm_msg_q_free_msg (mq, msg);
2342 events[*num_ev].data.u64 = session_evt_data;
2343 if (EPOLLONESHOT & session_events)
2345 session = vcl_session_get (wrk, sid);
2346 session->vep.ev.events = 0;
2349 if (*num_ev == maxevents)
2354 vec_reset_length (wrk->mq_msg_vector);
2359 vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
2360 int maxevents, double wait_for_time)
2362 vcl_cut_through_registration_t *cr;
2363 double total_wait = 0, wait_slice;
2367 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2368 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
2372 vcl_ct_registration_lock (wrk);
2374 pool_foreach (cr, wrk->cut_through_registrations, ({
2375 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &num_ev);
2378 vcl_ct_registration_unlock (wrk);
2380 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2381 maxevents, num_ev ? 0 : wait_slice,
2384 total_wait += wait_slice;
2388 while (total_wait < wait_for_time);
2389 return (int) num_ev;
2393 vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
2394 int maxevents, double wait_for_time)
2396 vcl_mq_evt_conn_t *mqc;
2397 int __clib_unused n_read;
2402 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2403 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2404 vec_len (wrk->mq_events), wait_for_time);
2405 for (i = 0; i < n_mq_evts; i++)
2407 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
2408 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2409 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
2412 return (int) n_evts;
2416 vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
2417 int maxevents, double wait_for_time)
2419 vcl_worker_t *wrk = vcl_worker_get_current ();
2420 vcl_session_t *vep_session;
2422 if (PREDICT_FALSE (maxevents <= 0))
2424 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2425 getpid (), maxevents);
2426 return VPPCOM_EINVAL;
2429 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
2431 return VPPCOM_EBADFD;
2433 if (PREDICT_FALSE (!vep_session->is_vep))
2435 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2436 getpid (), vep_handle);
2437 return VPPCOM_EINVAL;
2440 memset (events, 0, sizeof (*events) * maxevents);
2442 if (vcm->cfg.use_mq_eventfd)
2443 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, wait_for_time);
2445 return vppcom_epoll_wait_condvar (wrk, events, maxevents, wait_for_time);
2449 vppcom_session_attr (uint32_t session_handle, uint32_t op,
2450 void *buffer, uint32_t * buflen)
2452 vcl_worker_t *wrk = vcl_worker_get_current ();
2453 vcl_session_t *session;
2455 u32 *flags = buffer;
2456 vppcom_endpt_t *ep = buffer;
2458 session = vcl_session_get_w_handle (wrk, session_handle);
2460 return VPPCOM_EBADFD;
2464 case VPPCOM_ATTR_GET_NREAD:
2465 rv = vppcom_session_read_ready (session);
2466 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2470 case VPPCOM_ATTR_GET_NWRITE:
2471 rv = vppcom_session_write_ready (session);
2472 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2473 getpid (), session_handle, rv);
2476 case VPPCOM_ATTR_GET_FLAGS:
2477 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2479 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2480 VCL_SESS_ATTR_NONBLOCK));
2481 *buflen = sizeof (*flags);
2482 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2483 "is_nonblocking = %u", getpid (),
2484 session_handle, *flags,
2485 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2491 case VPPCOM_ATTR_SET_FLAGS:
2492 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2494 if (*flags & O_NONBLOCK)
2495 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2497 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2499 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2500 " is_nonblocking = %u",
2501 getpid (), session_handle, *flags,
2502 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2508 case VPPCOM_ATTR_GET_PEER_ADDR:
2509 if (PREDICT_TRUE (buffer && buflen &&
2510 (*buflen >= sizeof (*ep)) && ep->ip))
2512 ep->is_ip4 = session->transport.is_ip4;
2513 ep->port = session->transport.rmt_port;
2514 if (session->transport.is_ip4)
2515 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2516 sizeof (ip4_address_t));
2518 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2519 sizeof (ip6_address_t));
2520 *buflen = sizeof (*ep);
2521 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2522 "addr = %U, port %u", getpid (),
2523 session_handle, ep->is_ip4, format_ip46_address,
2524 &session->transport.rmt_ip,
2525 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2526 clib_net_to_host_u16 (ep->port));
2532 case VPPCOM_ATTR_GET_LCL_ADDR:
2533 if (PREDICT_TRUE (buffer && buflen &&
2534 (*buflen >= sizeof (*ep)) && ep->ip))
2536 ep->is_ip4 = session->transport.is_ip4;
2537 ep->port = session->transport.lcl_port;
2538 if (session->transport.is_ip4)
2539 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2540 sizeof (ip4_address_t));
2542 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2543 sizeof (ip6_address_t));
2544 *buflen = sizeof (*ep);
2545 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2546 " addr = %U port %d", getpid (),
2547 session_handle, ep->is_ip4, format_ip46_address,
2548 &session->transport.lcl_ip,
2549 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2550 clib_net_to_host_u16 (ep->port));
2556 case VPPCOM_ATTR_GET_LIBC_EPFD:
2557 rv = session->libc_epfd;
2558 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2562 case VPPCOM_ATTR_SET_LIBC_EPFD:
2563 if (PREDICT_TRUE (buffer && buflen &&
2564 (*buflen == sizeof (session->libc_epfd))))
2566 session->libc_epfd = *(int *) buffer;
2567 *buflen = sizeof (session->libc_epfd);
2569 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2570 "buflen %d", getpid (), session->libc_epfd, *buflen);
2576 case VPPCOM_ATTR_GET_PROTOCOL:
2577 if (buffer && buflen && (*buflen >= sizeof (int)))
2579 *(int *) buffer = session->session_type;
2580 *buflen = sizeof (int);
2582 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2583 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2590 case VPPCOM_ATTR_GET_LISTEN:
2591 if (buffer && buflen && (*buflen >= sizeof (int)))
2593 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2594 VCL_SESS_ATTR_LISTEN);
2595 *buflen = sizeof (int);
2597 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2598 getpid (), *(int *) buffer, *buflen);
2604 case VPPCOM_ATTR_GET_ERROR:
2605 if (buffer && buflen && (*buflen >= sizeof (int)))
2607 *(int *) buffer = 0;
2608 *buflen = sizeof (int);
2610 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2611 getpid (), *(int *) buffer, *buflen);
2617 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2618 if (buffer && buflen && (*buflen >= sizeof (u32)))
2622 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2623 session->tx_fifo ? session->tx_fifo->nitems :
2624 vcm->cfg.tx_fifo_size);
2625 *buflen = sizeof (u32);
2627 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2628 "buflen %d, #VPP-TBD#", getpid (),
2629 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2635 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2636 if (buffer && buflen && (*buflen == sizeof (u32)))
2639 session->sndbuf_size = *(u32 *) buffer;
2640 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2641 "buflen %d, #VPP-TBD#", getpid (),
2642 session->sndbuf_size, session->sndbuf_size, *buflen);
2648 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2649 if (buffer && buflen && (*buflen >= sizeof (u32)))
2653 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2654 session->rx_fifo ? session->rx_fifo->nitems :
2655 vcm->cfg.rx_fifo_size);
2656 *buflen = sizeof (u32);
2658 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2659 "buflen %d, #VPP-TBD#", getpid (),
2660 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2666 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2667 if (buffer && buflen && (*buflen == sizeof (u32)))
2670 session->rcvbuf_size = *(u32 *) buffer;
2671 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2672 "buflen %d, #VPP-TBD#", getpid (),
2673 session->sndbuf_size, session->sndbuf_size, *buflen);
2679 case VPPCOM_ATTR_GET_REUSEADDR:
2680 if (buffer && buflen && (*buflen >= sizeof (int)))
2683 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2684 VCL_SESS_ATTR_REUSEADDR);
2685 *buflen = sizeof (int);
2687 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2688 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2694 case VPPCOM_ATTR_SET_REUSEADDR:
2695 if (buffer && buflen && (*buflen == sizeof (int)) &&
2696 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2699 if (*(int *) buffer)
2700 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2702 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2704 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2705 " #VPP-TBD#", getpid (),
2706 VCL_SESS_ATTR_TEST (session->attr,
2707 VCL_SESS_ATTR_REUSEADDR), *buflen);
2713 case VPPCOM_ATTR_GET_REUSEPORT:
2714 if (buffer && buflen && (*buflen >= sizeof (int)))
2717 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2718 VCL_SESS_ATTR_REUSEPORT);
2719 *buflen = sizeof (int);
2721 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2722 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2728 case VPPCOM_ATTR_SET_REUSEPORT:
2729 if (buffer && buflen && (*buflen == sizeof (int)) &&
2730 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2733 if (*(int *) buffer)
2734 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2736 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2738 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2739 " #VPP-TBD#", getpid (),
2740 VCL_SESS_ATTR_TEST (session->attr,
2741 VCL_SESS_ATTR_REUSEPORT), *buflen);
2747 case VPPCOM_ATTR_GET_BROADCAST:
2748 if (buffer && buflen && (*buflen >= sizeof (int)))
2751 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2752 VCL_SESS_ATTR_BROADCAST);
2753 *buflen = sizeof (int);
2755 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2756 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2762 case VPPCOM_ATTR_SET_BROADCAST:
2763 if (buffer && buflen && (*buflen == sizeof (int)))
2766 if (*(int *) buffer)
2767 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2769 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2771 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2772 "#VPP-TBD#", getpid (),
2773 VCL_SESS_ATTR_TEST (session->attr,
2774 VCL_SESS_ATTR_BROADCAST), *buflen);
2780 case VPPCOM_ATTR_GET_V6ONLY:
2781 if (buffer && buflen && (*buflen >= sizeof (int)))
2784 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2785 VCL_SESS_ATTR_V6ONLY);
2786 *buflen = sizeof (int);
2788 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2789 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2795 case VPPCOM_ATTR_SET_V6ONLY:
2796 if (buffer && buflen && (*buflen == sizeof (int)))
2799 if (*(int *) buffer)
2800 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2802 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2804 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2805 "#VPP-TBD#", getpid (),
2806 VCL_SESS_ATTR_TEST (session->attr,
2807 VCL_SESS_ATTR_V6ONLY), *buflen);
2813 case VPPCOM_ATTR_GET_KEEPALIVE:
2814 if (buffer && buflen && (*buflen >= sizeof (int)))
2817 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2818 VCL_SESS_ATTR_KEEPALIVE);
2819 *buflen = sizeof (int);
2821 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2822 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2828 case VPPCOM_ATTR_SET_KEEPALIVE:
2829 if (buffer && buflen && (*buflen == sizeof (int)))
2832 if (*(int *) buffer)
2833 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2835 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2837 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2838 "#VPP-TBD#", getpid (),
2839 VCL_SESS_ATTR_TEST (session->attr,
2840 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2846 case VPPCOM_ATTR_GET_TCP_NODELAY:
2847 if (buffer && buflen && (*buflen >= sizeof (int)))
2850 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2851 VCL_SESS_ATTR_TCP_NODELAY);
2852 *buflen = sizeof (int);
2854 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2855 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2861 case VPPCOM_ATTR_SET_TCP_NODELAY:
2862 if (buffer && buflen && (*buflen == sizeof (int)))
2865 if (*(int *) buffer)
2866 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2868 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2870 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2871 "#VPP-TBD#", getpid (),
2872 VCL_SESS_ATTR_TEST (session->attr,
2873 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
2879 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2880 if (buffer && buflen && (*buflen >= sizeof (int)))
2883 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2884 VCL_SESS_ATTR_TCP_KEEPIDLE);
2885 *buflen = sizeof (int);
2887 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2888 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2894 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
2895 if (buffer && buflen && (*buflen == sizeof (int)))
2898 if (*(int *) buffer)
2899 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2901 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2903 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2904 "#VPP-TBD#", getpid (),
2905 VCL_SESS_ATTR_TEST (session->attr,
2906 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
2912 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2913 if (buffer && buflen && (*buflen >= sizeof (int)))
2916 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2917 VCL_SESS_ATTR_TCP_KEEPINTVL);
2918 *buflen = sizeof (int);
2920 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
2921 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2927 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
2928 if (buffer && buflen && (*buflen == sizeof (int)))
2931 if (*(int *) buffer)
2932 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2934 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2936 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
2937 "#VPP-TBD#", getpid (),
2938 VCL_SESS_ATTR_TEST (session->attr,
2939 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
2945 case VPPCOM_ATTR_GET_TCP_USER_MSS:
2946 if (buffer && buflen && (*buflen >= sizeof (u32)))
2949 *(u32 *) buffer = session->user_mss;
2950 *buflen = sizeof (int);
2952 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
2953 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2959 case VPPCOM_ATTR_SET_TCP_USER_MSS:
2960 if (buffer && buflen && (*buflen == sizeof (u32)))
2963 session->user_mss = *(u32 *) buffer;
2965 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
2966 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
2981 vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
2982 uint32_t buflen, int flags, vppcom_endpt_t * ep)
2984 vcl_worker_t *wrk = vcl_worker_get_current ();
2986 vcl_session_t *session = 0;
2990 session = vcl_session_get_w_handle (wrk, session_handle);
2991 if (PREDICT_FALSE (!session))
2993 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
2994 getpid (), session_handle);
2995 return VPPCOM_EBADFD;
2997 ep->is_ip4 = session->transport.is_ip4;
2998 ep->port = session->transport.rmt_port;
3002 rv = vppcom_session_read (session_handle, buffer, buflen);
3003 else if (flags & MSG_PEEK)
3004 rv = vppcom_session_peek (session_handle, buffer, buflen);
3007 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3009 return VPPCOM_EAFNOSUPPORT;
3014 if (session->transport.is_ip4)
3015 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3016 sizeof (ip4_address_t));
3018 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3019 sizeof (ip6_address_t));
3026 vppcom_session_sendto (uint32_t session_handle, void *buffer,
3027 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3030 return VPPCOM_EINVAL;
3035 return VPPCOM_EINVAL;
3040 // TBD check the flags and do the right thing
3041 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3042 getpid (), flags, flags);
3045 return (vppcom_session_write (session_handle, buffer, buflen));
3049 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3051 vcl_worker_t *wrk = vcl_worker_get_current ();
3052 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
3053 u32 i, keep_trying = 1;
3056 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3057 getpid (), vp, n_sids, wait_for_time);
3060 return VPPCOM_EFAULT;
3064 vcl_session_t *session;
3066 for (i = 0; i < n_sids; i++)
3068 ASSERT (vp[i].revents);
3070 session = vcl_session_get (wrk, vp[i].sid);
3077 if (POLLIN & vp[i].events)
3079 rv = vppcom_session_read_ready (session);
3082 *vp[i].revents |= POLLIN;
3089 case VPPCOM_ECONNRESET:
3090 *vp[i].revents = POLLHUP;
3094 *vp[i].revents = POLLERR;
3101 if (POLLOUT & vp[i].events)
3103 rv = vppcom_session_write_ready (session);
3106 *vp[i].revents |= POLLOUT;
3113 case VPPCOM_ECONNRESET:
3114 *vp[i].revents = POLLHUP;
3118 *vp[i].revents = POLLERR;
3125 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3127 *vp[i].revents = POLLNVAL;
3131 if (wait_for_time != -1)
3132 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
3134 while ((num_ev == 0) && keep_trying);
3136 if (VPPCOM_DEBUG > 3)
3138 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3139 for (i = 0; i < n_sids; i++)
3141 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3142 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3143 vp[i].events, *vp[i].revents);
3150 vppcom_mq_epoll_fd (void)
3152 vcl_worker_t *wrk = vcl_worker_get_current ();
3153 return wrk->mqs_epfd;
3157 vppcom_session_index (uint32_t session_handle)
3159 return session_handle & 0xFFFFFF;
3163 vppcom_worker_register (void)
3165 if (vcl_worker_alloc_and_init ())
3167 return VPPCOM_EEXIST;
3171 * fd.io coding-style-patch-verification: ON
3174 * eval: (c-set-style "gnu")