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>
27 sigsegv_signal (int signum)
33 vcl_wait_for_memory (void *mem)
35 u8 __clib_unused test;
36 if (vcm->mounting_segment)
38 while (vcm->mounting_segment)
47 if (signal (SIGSEGV, sigsegv_signal))
63 signal (SIGSEGV, SIG_DFL);
67 vppcom_app_state_str (app_state_t state)
74 st = "STATE_APP_START";
77 case STATE_APP_CONN_VPP:
78 st = "STATE_APP_CONN_VPP";
81 case STATE_APP_ENABLED:
82 st = "STATE_APP_ENABLED";
85 case STATE_APP_ATTACHED:
86 st = "STATE_APP_ATTACHED";
90 st = "UNKNOWN_APP_STATE";
98 vppcom_session_state_str (session_state_t state)
109 st = "STATE_CONNECT";
120 case STATE_CLOSE_ON_EMPTY:
121 st = "STATE_CLOSE_ON_EMPTY";
124 case STATE_DISCONNECT:
125 st = "STATE_DISCONNECT";
133 st = "UNKNOWN_STATE";
141 format_ip4_address (u8 * s, va_list * args)
143 u8 *a = va_arg (*args, u8 *);
144 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
148 format_ip6_address (u8 * s, va_list * args)
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;
153 i_max_n_zero = ARRAY_LEN (a->as_u16);
155 i_first_zero = i_max_n_zero;
157 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
159 u32 is_zero = a->as_u16[i] == 0;
160 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
166 if ((!is_zero && n_zeros > max_n_zeros)
167 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
169 i_max_n_zero = i_first_zero;
170 max_n_zeros = n_zeros;
171 i_first_zero = ARRAY_LEN (a->as_u16);
176 last_double_colon = 0;
177 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
179 if (i == i_max_n_zero && max_n_zeros > 1)
181 s = format (s, "::");
182 i += max_n_zeros - 1;
183 last_double_colon = 1;
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;
197 /* Format an IP46 address. */
199 format_ip46_address (u8 * s, va_list * args)
201 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
202 ip46_type_t type = va_arg (*args, ip46_type_t);
208 is_ip4 = ip46_address_is_ip4 (ip46);
219 format (s, "%U", format_ip4_address, &ip46->ip4) :
220 format (s, "%U", format_ip6_address, &ip46->ip6);
224 * VPPCOM Utility Functions
228 vppcom_session_table_del_listener (u64 listener_handle)
230 listener_handle |= 1ULL << 63;
231 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
235 vppcom_wait_for_app_state_change (app_state_t app_state)
237 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
239 while (clib_time_now (&vcm->clib_time) < timeout)
241 if (vcm->app_state == app_state)
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);
248 return VPPCOM_ETIMEDOUT;
252 vcl_session_vpp_evt_q (vcl_session_t * s)
254 if (vcl_session_is_ct (s))
255 return vcm->vpp_event_queues[0];
257 return vcm->vpp_event_queues[s->tx_fifo->master_thread_index];
261 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
262 session_handle_t handle, int retval)
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);
275 vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
276 session_handle_t handle, int retval)
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);
290 vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
291 session_handle_t handle, int retval)
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);
304 vcl_session_accepted_handler (session_accepted_msg_t * mp)
306 vcl_session_t *session, *listen_session;
307 svm_fifo_t *rx_fifo, *tx_fifo;
308 u32 session_index, vpp_wrk_index;
311 session = vcl_session_alloc ();
312 session_index = vcl_session_index (session);
314 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
318 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
319 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
320 "unknown vpp listener handle %llx",
321 getpid (), mp->listener_handle);
322 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
323 VNET_API_ERROR_INVALID_ARGUMENT);
324 vcl_session_free (session);
325 return VCL_INVALID_SESSION_INDEX;
328 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
329 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
331 if (mp->server_event_queue_address)
333 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
335 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
337 vcl_wait_for_memory (session->vpp_evt_q);
338 rx_fifo->master_session_index = session_index;
339 tx_fifo->master_session_index = session_index;
340 vec_validate (vcm->vpp_event_queues, 0);
341 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
342 vcm->vpp_event_queues[0] = evt_q;
346 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
348 rx_fifo->client_session_index = session_index;
349 tx_fifo->client_session_index = session_index;
351 vpp_wrk_index = tx_fifo->master_thread_index;
352 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
353 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
356 session->vpp_handle = mp->handle;
357 session->client_context = mp->context;
358 session->rx_fifo = rx_fifo;
359 session->tx_fifo = tx_fifo;
361 session->session_state = STATE_ACCEPT;
362 session->transport.rmt_port = mp->port;
363 session->transport.is_ip4 = mp->is_ip4;
364 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
366 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
367 session->transport.lcl_port = listen_session->transport.lcl_port;
368 session->transport.lcl_ip = listen_session->transport.lcl_ip;
369 session->session_type = listen_session->session_type;
370 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
372 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
373 " address %U port %d queue %p!", getpid (), mp->handle, session_index,
374 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
375 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
376 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
377 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
379 return session_index;
383 vcl_session_connected_handler (session_connected_msg_t * mp)
385 u32 session_index, vpp_wrk_index;
386 svm_fifo_t *rx_fifo, *tx_fifo;
387 vcl_session_t *session = 0;
390 session_index = mp->context;
391 session = vcl_session_get (session_index);
394 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
395 "Invalid session index (%u)!",
396 getpid (), mp->handle, session_index);
397 return VCL_INVALID_SESSION_INDEX;
401 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
402 mp->handle, session_index, format_api_error,
404 session->session_state = STATE_FAILED;
405 session->vpp_handle = mp->handle;
406 return session_index;
409 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
410 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
411 vcl_wait_for_memory (rx_fifo);
412 rx_fifo->client_session_index = session_index;
413 tx_fifo->client_session_index = session_index;
415 if (mp->client_event_queue_address)
417 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
419 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
422 vec_validate (vcm->vpp_event_queues, 0);
423 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
424 vcm->vpp_event_queues[0] = evt_q;
428 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
430 vpp_wrk_index = tx_fifo->master_thread_index;
431 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
432 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
435 session->rx_fifo = rx_fifo;
436 session->tx_fifo = tx_fifo;
437 session->vpp_handle = mp->handle;
438 session->transport.is_ip4 = mp->is_ip4;
439 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
440 sizeof (session->transport.lcl_ip));
441 session->transport.lcl_port = mp->lcl_port;
442 session->session_state = STATE_CONNECT;
444 /* Add it to lookup table */
445 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
447 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
448 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
449 getpid (), mp->handle, session_index, session->rx_fifo,
450 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
452 return session_index;
456 vcl_session_reset_handler (session_reset_msg_t * reset_msg)
458 vcl_session_t *session;
461 sid = vcl_session_get_index_from_handle (reset_msg->handle);
462 session = vcl_session_get (sid);
465 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
466 return VCL_INVALID_SESSION_INDEX;
468 session->session_state = STATE_CLOSE_ON_EMPTY;
469 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
470 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (session),
471 vcm->my_client_index, reset_msg->handle, 0);
476 vcl_session_bound_handler (session_bound_msg_t * mp)
478 vcl_session_t *session;
479 u32 sid = mp->context;
481 session = vcl_session_get (sid);
484 VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
485 getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
488 session->session_state = STATE_FAILED;
489 session->vpp_handle = mp->handle;
494 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
495 "Invalid session index (%u)!",
496 getpid (), mp->handle, sid);
497 return VCL_INVALID_SESSION_INDEX;
501 session->vpp_handle = mp->handle;
502 session->transport.is_ip4 = mp->lcl_is_ip4;
503 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
504 sizeof (ip46_address_t));
505 session->transport.lcl_port = mp->lcl_port;
506 vppcom_session_table_add_listener (mp->handle, sid);
507 session->session_state = STATE_LISTEN;
509 if (session->is_dgram)
511 svm_fifo_t *rx_fifo, *tx_fifo;
512 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
513 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
514 rx_fifo->client_session_index = sid;
515 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
516 tx_fifo->client_session_index = sid;
517 session->rx_fifo = rx_fifo;
518 session->tx_fifo = tx_fifo;
521 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
522 getpid (), mp->handle, sid);
527 vcl_handle_mq_ctrl_event (session_event_t * e)
529 session_accepted_msg_t *accepted_msg;
530 session_disconnected_msg_t *disconnected_msg;
531 vcl_session_msg_t *vcl_msg;
532 vcl_session_t *session;
536 switch (e->event_type)
538 case FIFO_EVENT_APP_RX:
539 clib_warning ("unhandled rx: sid %u (0x%x)",
540 e->fifo->client_session_index,
541 e->fifo->client_session_index);
543 case SESSION_CTRL_EVT_ACCEPTED:
544 accepted_msg = (session_accepted_msg_t *) e->data;
545 handle = accepted_msg->listener_handle;
546 session = vppcom_session_table_lookup_listener (handle);
549 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
550 "listener handle %llx", getpid (), handle);
554 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
555 vcl_msg->accepted_msg = *accepted_msg;
557 case SESSION_CTRL_EVT_CONNECTED:
558 vcl_session_connected_handler ((session_connected_msg_t *) e->data);
560 case SESSION_CTRL_EVT_DISCONNECTED:
561 disconnected_msg = (session_disconnected_msg_t *) e->data;
562 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
563 session = vcl_session_get (sid);
566 VDBG (0, "request to disconnect unknown handle 0x%llx",
567 disconnected_msg->handle);
570 session->session_state = STATE_DISCONNECT;
571 VDBG (0, "disconnected handle 0xllx, sid %u", disconnected_msg->handle,
574 case SESSION_CTRL_EVT_RESET:
575 vcl_session_reset_handler ((session_reset_msg_t *) e->data);
577 case SESSION_CTRL_EVT_BOUND:
578 vcl_session_bound_handler ((session_bound_msg_t *) e->data);
581 clib_warning ("unhandled %u", e->event_type);
587 vppcom_wait_for_session_state_change (u32 session_index,
588 session_state_t state,
591 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
592 vcl_session_t *volatile session;
598 session = vcl_session_get (session_index);
599 if (PREDICT_FALSE (!session))
601 return VPPCOM_EBADFD;
603 if (session->session_state & state)
607 if (session->session_state & STATE_FAILED)
609 return VPPCOM_ECONNREFUSED;
612 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
614 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
615 vcl_handle_mq_ctrl_event (e);
616 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
618 while (clib_time_now (&vcm->clib_time) < timeout);
620 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
621 vppcom_session_state_str (state));
622 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
624 return VPPCOM_ETIMEDOUT;
628 vppcom_app_session_enable (void)
632 if (vcm->app_state != STATE_APP_ENABLED)
634 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
635 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
636 if (PREDICT_FALSE (rv))
638 VDBG (0, "VCL<%d>: application session enable timed out! "
639 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
647 vppcom_app_attach (void)
651 vppcom_app_send_attach ();
652 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
653 if (PREDICT_FALSE (rv))
655 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
656 getpid (), rv, vppcom_retval_str (rv));
664 vppcom_session_unbind (u32 session_index)
666 vcl_session_t *session = 0;
669 session = vcl_session_get (session_index);
671 return VPPCOM_EBADFD;
673 vpp_handle = session->vpp_handle;
674 vppcom_session_table_del_listener (vpp_handle);
675 session->vpp_handle = ~0;
676 session->session_state = STATE_DISCONNECT;
678 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
679 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
680 vppcom_session_state_str (STATE_DISCONNECT));
681 vcl_evt (VCL_EVT_UNBIND, session);
682 vppcom_send_unbind_sock (vpp_handle);
688 vppcom_session_disconnect (u32 session_index)
690 svm_msg_q_t *vpp_evt_q;
691 vcl_session_t *session;
692 session_state_t state;
695 session = vcl_session_get (session_index);
696 vpp_handle = session->vpp_handle;
697 state = session->session_state;
699 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
700 vpp_handle, session_index, state, vppcom_session_state_str (state));
702 if (PREDICT_FALSE (state & STATE_LISTEN))
704 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
705 "Cannot disconnect a listen socket!",
706 getpid (), vpp_handle, session_index);
707 return VPPCOM_EBADFD;
710 if (state & STATE_CLOSE_ON_EMPTY)
712 vpp_evt_q = vcl_session_vpp_evt_q (session);
713 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
715 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
716 "REPLY...", getpid (), vpp_handle, session_index);
720 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
721 getpid (), vpp_handle, session_index);
722 vppcom_send_disconnect_session (vpp_handle, session_index);
729 * VPPCOM Public API functions
732 vppcom_app_create (char *app_name)
734 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
740 vppcom_cfg (&vcm->cfg);
744 if (vcl_cfg->use_mq_eventfd)
745 vcm->mqs_epfd = epoll_create (1);
747 vcm->main_cpu = os_get_thread_index ();
748 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
749 vcm->ct_registration_by_mq = hash_create (0, sizeof (uword));
750 clib_spinlock_init (&vcm->ct_registration_lock);
752 clib_time_init (&vcm->clib_time);
753 vppcom_init_error_string_table ();
754 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
755 20 /* timeout in secs */ );
756 vec_validate (vcm->mq_events, 64);
757 vec_validate (vcm->mq_msg_vector, 128);
758 vec_reset_length (vcm->mq_msg_vector);
761 if (vcm->my_client_index == ~0)
763 /* API hookup and connect to VPP */
764 vppcom_api_hookup ();
766 vcm->app_state = STATE_APP_START;
767 rv = vppcom_connect_to_vpp (app_name);
770 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
775 VDBG (0, "VCL<%d>: sending session enable", getpid ());
776 rv = vppcom_app_session_enable ();
779 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
780 "failed!", getpid ());
784 VDBG (0, "VCL<%d>: sending app attach", getpid ());
785 rv = vppcom_app_attach ();
788 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
793 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
794 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
801 vppcom_app_destroy (void)
804 f64 orig_app_timeout;
806 if (vcm->my_client_index == ~0)
809 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
810 getpid (), vcm->my_client_index, vcm->my_client_index);
811 vcl_evt (VCL_EVT_DETACH, vcm);
813 vppcom_app_send_detach ();
814 orig_app_timeout = vcm->cfg.app_timeout;
815 vcm->cfg.app_timeout = 2.0;
816 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
817 vcm->cfg.app_timeout = orig_app_timeout;
818 if (PREDICT_FALSE (rv))
819 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
820 getpid (), rv, vppcom_retval_str (rv));
823 vl_client_disconnect_from_vlib ();
824 vcm->my_client_index = ~0;
825 vcm->app_state = STATE_APP_START;
829 vppcom_session_create (u8 proto, u8 is_nonblocking)
831 vcl_session_t *session;
834 pool_get (vcm->sessions, session);
835 memset (session, 0, sizeof (*session));
836 session_index = session - vcm->sessions;
838 session->session_type = proto;
839 session->session_state = STATE_START;
840 session->vpp_handle = ~0;
841 session->is_dgram = proto == VPPCOM_PROTO_UDP;
844 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
846 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
847 is_nonblocking, session_index);
849 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
851 return (int) session_index;
855 vppcom_session_close (uint32_t session_index)
857 vcl_session_t *session = 0;
865 session_state_t state;
867 session = vcl_session_get (session_index);
869 return VPPCOM_EBADFD;
871 is_vep = session->is_vep;
872 is_vep_session = session->is_vep_session;
873 next_sid = session->vep.next_sid;
874 vep_idx = session->vep.vep_idx;
875 state = session->session_state;
876 vpp_handle = session->vpp_handle;
878 if (VPPCOM_DEBUG > 0)
881 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
882 "closing epoll session...",
883 getpid (), session_index, session_index);
885 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
886 "closing session...",
887 getpid (), vpp_handle, session_index);
892 while (next_sid != ~0)
894 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
895 if (PREDICT_FALSE (rv < 0))
896 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
897 "vep_idx %u failed! rv %d (%s)",
898 getpid (), vpp_handle, next_sid, vep_idx,
899 rv, vppcom_retval_str (rv));
901 next_sid = session->vep.next_sid;
908 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
910 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
911 "vep_idx %u failed! rv %d (%s)",
912 getpid (), vpp_handle, session_index,
913 vep_idx, rv, vppcom_retval_str (rv));
916 if (state & STATE_LISTEN)
918 rv = vppcom_session_unbind (session_index);
919 if (PREDICT_FALSE (rv < 0))
920 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
921 "failed! rv %d (%s)",
922 getpid (), vpp_handle, session_index,
923 rv, vppcom_retval_str (rv));
925 else if (state & STATE_OPEN)
927 rv = vppcom_session_disconnect (session_index);
928 if (PREDICT_FALSE (rv < 0))
929 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
930 "session disconnect failed! rv %d (%s)",
931 getpid (), vpp_handle, session_index,
932 rv, vppcom_retval_str (rv));
936 if (vcl_session_is_ct (session))
938 vcl_cut_through_registration_t *ctr;
941 mq_addr = pointer_to_uword (session->our_evt_q);
942 ctr = vcl_ct_registration_lock_and_lookup (mq_addr);
944 if (ctr->epoll_evt_conn_index != ~0)
945 vcl_mq_epoll_del_evfd (ctr->epoll_evt_conn_index);
946 VDBG (0, "Removing ct registration %u",
947 vcl_ct_registration_index (ctr));
948 vcl_ct_registration_del (ctr);
949 vcl_ct_registration_unlock ();
952 if (vpp_handle != ~0)
954 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
956 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
958 vcl_session_free (session);
960 if (VPPCOM_DEBUG > 0)
963 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
964 getpid (), session_index, session_index);
966 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
967 getpid (), vpp_handle, session_index);
970 vcl_evt (VCL_EVT_CLOSE, session, rv);
976 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
978 vcl_session_t *session = 0;
981 return VPPCOM_EINVAL;
983 session = vcl_session_get (session_index);
985 return VPPCOM_EBADFD;
989 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
990 "bind to an epoll session!", getpid (), session_index);
991 return VPPCOM_EBADFD;
994 session->transport.is_ip4 = ep->is_ip4;
996 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
997 sizeof (ip4_address_t));
999 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
1000 sizeof (ip6_address_t));
1001 session->transport.lcl_port = ep->port;
1003 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
1004 "proto %s", getpid (), session_index,
1005 session->transport.is_ip4 ? "IPv4" : "IPv6",
1006 format_ip46_address, &session->transport.lcl_ip,
1007 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1008 clib_net_to_host_u16 (session->transport.lcl_port),
1009 session->session_type ? "UDP" : "TCP");
1010 vcl_evt (VCL_EVT_BIND, session);
1012 if (session->session_type == VPPCOM_PROTO_UDP)
1013 vppcom_session_listen (session_index, 10);
1019 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
1021 vcl_session_t *listen_session = 0;
1022 u64 listen_vpp_handle;
1025 listen_session = vcl_session_get (listen_session_index);
1026 if (!listen_session)
1027 return VPPCOM_EBADFD;
1029 if (q_len == 0 || q_len == ~0)
1030 q_len = vcm->cfg.listen_queue_size;
1032 if (listen_session->is_vep)
1034 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
1035 "epoll session!", getpid (), listen_session_index);
1036 return VPPCOM_EBADFD;
1039 listen_vpp_handle = listen_session->vpp_handle;
1040 if (listen_session->session_state & STATE_LISTEN)
1042 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
1043 getpid (), listen_vpp_handle, listen_session_index);
1047 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
1048 "request...", getpid (), listen_vpp_handle, listen_session_index);
1051 * Send listen request to vpp and wait for reply
1053 vppcom_send_bind_sock (listen_session, listen_session_index);
1055 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
1056 vcm->cfg.session_timeout);
1058 if (PREDICT_FALSE (rv))
1060 listen_session = vcl_session_get (listen_session_index);
1061 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1062 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1063 listen_session_index, rv, vppcom_retval_str (rv));
1071 validate_args_session_accept_ (vcl_session_t * listen_session)
1073 u32 listen_session_index = listen_session - vcm->sessions;
1075 /* Input validation - expects spinlock on sessions_lockp */
1076 if (listen_session->is_vep)
1078 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1079 "epoll session!", getpid (), listen_session_index);
1080 return VPPCOM_EBADFD;
1083 if (listen_session->session_state != STATE_LISTEN)
1085 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1086 "not in listen state! state 0x%x (%s)", getpid (),
1087 listen_session->vpp_handle, listen_session_index,
1088 listen_session->session_state,
1089 vppcom_session_state_str (listen_session->session_state));
1090 return VPPCOM_EBADFD;
1096 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
1099 session_accepted_msg_t accepted_msg;
1100 vcl_session_t *listen_session = 0;
1101 vcl_session_t *client_session = 0;
1102 u32 client_session_index = ~0;
1103 svm_msg_q_t *vpp_evt_q;
1104 vcl_session_msg_t *evt;
1105 u64 listen_vpp_handle;
1106 svm_msg_q_msg_t msg;
1111 listen_session = vcl_session_get (listen_session_index);
1112 if (!listen_session)
1113 return VPPCOM_EBADFD;
1115 if ((rv = validate_args_session_accept_ (listen_session)))
1118 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1120 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1121 accepted_msg = evt->accepted_msg;
1125 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1126 VCL_SESS_ATTR_NONBLOCK);
1127 if (svm_msg_q_is_empty (vcm->app_event_queue) && is_nonblocking)
1128 return VPPCOM_EAGAIN;
1132 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_WAIT, 0))
1133 return VPPCOM_EAGAIN;
1135 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
1136 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1138 clib_warning ("discarded event: %u", e->event_type);
1139 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1142 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1143 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1149 client_session_index = vcl_session_accepted_handler (&accepted_msg);
1150 listen_session = vcl_session_get (listen_session_index);
1151 client_session = vcl_session_get (client_session_index);
1153 if (flags & O_NONBLOCK)
1154 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1156 listen_vpp_handle = listen_session->vpp_handle;
1157 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1158 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1159 getpid (), listen_vpp_handle, listen_session_index,
1160 client_session->vpp_handle, client_session_index,
1161 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1162 VCL_SESS_ATTR_NONBLOCK));
1166 ep->is_ip4 = client_session->transport.is_ip4;
1167 ep->port = client_session->transport.rmt_port;
1168 if (client_session->transport.is_ip4)
1169 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
1170 sizeof (ip4_address_t));
1172 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
1173 sizeof (ip6_address_t));
1176 if (accepted_msg.server_event_queue_address)
1177 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1180 vpp_evt_q = client_session->vpp_evt_q;
1182 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1183 client_session->vpp_handle, 0);
1185 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1186 "sid %u connection from peer %s address %U port %u to local %s "
1187 "address %U port %u", getpid (), listen_vpp_handle,
1188 listen_session_index, client_session->vpp_handle,
1189 client_session_index,
1190 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1191 format_ip46_address, &client_session->transport.rmt_ip,
1192 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1193 clib_net_to_host_u16 (client_session->transport.rmt_port),
1194 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1195 format_ip46_address, &client_session->transport.lcl_ip,
1196 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1197 clib_net_to_host_u16 (client_session->transport.lcl_port));
1198 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1199 client_session_index);
1201 return client_session_index;
1205 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
1207 vcl_session_t *session = 0;
1211 session = vcl_session_get (session_index);
1213 return VPPCOM_EBADFD;
1215 if (PREDICT_FALSE (session->is_vep))
1217 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1218 "connect on an epoll session!", getpid (), session_index);
1219 return VPPCOM_EBADFD;
1222 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1224 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1225 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1226 getpid (), session->vpp_handle, session_index,
1227 session->transport.is_ip4 ? "IPv4" : "IPv6",
1228 format_ip46_address,
1229 &session->transport.rmt_ip, session->transport.is_ip4 ?
1230 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1231 clib_net_to_host_u16 (session->transport.rmt_port),
1232 session->session_type ? "UDP" : "TCP", session->session_state,
1233 vppcom_session_state_str (session->session_state));
1237 session->transport.is_ip4 = server_ep->is_ip4;
1238 if (session->transport.is_ip4)
1239 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
1240 sizeof (ip4_address_t));
1242 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
1243 sizeof (ip6_address_t));
1244 session->transport.rmt_port = server_ep->port;
1246 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1248 getpid (), session->vpp_handle, session_index,
1249 session->transport.is_ip4 ? "IPv4" : "IPv6",
1250 format_ip46_address,
1251 &session->transport.rmt_ip, session->transport.is_ip4 ?
1252 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1253 clib_net_to_host_u16 (session->transport.rmt_port),
1254 session->session_type ? "UDP" : "TCP");
1257 * Send connect request and wait for reply from vpp
1259 vppcom_send_connect_sock (session, session_index);
1260 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1261 vcm->cfg.session_timeout);
1263 session = vcl_session_get (session_index);
1264 vpp_handle = session->vpp_handle;
1266 if (PREDICT_FALSE (rv))
1268 if (VPPCOM_DEBUG > 0)
1271 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1272 "failed! returning %d (%s)", getpid (), vpp_handle,
1273 session_index, rv, vppcom_retval_str (rv));
1275 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1276 "returning %d (%s)", getpid (),
1277 session_index, rv, vppcom_retval_str (rv));
1281 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1282 getpid (), vpp_handle, session_index);
1288 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1291 return (e->event_type == FIFO_EVENT_APP_RX
1292 && e->fifo->client_session_index == sid);
1294 return (e->event_type == SESSION_IO_EVT_CT_TX);
1298 vcl_session_is_readable (vcl_session_t * s)
1300 return ((s->session_state & STATE_OPEN)
1301 || (s->session_state == STATE_LISTEN
1302 && s->session_type == VPPCOM_PROTO_UDP));
1306 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
1309 int n_read = 0, rv, is_nonblocking;
1310 vcl_session_t *s = 0;
1311 svm_fifo_t *rx_fifo;
1312 svm_msg_q_msg_t msg;
1317 if (PREDICT_FALSE (!buf))
1318 return VPPCOM_EINVAL;
1320 s = vcl_session_get (session_index);
1321 if (PREDICT_FALSE (!s))
1322 return VPPCOM_EBADFD;
1324 if (PREDICT_FALSE (s->is_vep))
1326 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1327 "read from an epoll session!", getpid (), session_index);
1328 return VPPCOM_EBADFD;
1331 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1332 rx_fifo = s->rx_fifo;
1334 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1336 session_state_t state = s->session_state;
1337 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1339 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1340 "state 0x%x (%s), returning %d (%s)",
1341 getpid (), s->vpp_handle, session_index, state,
1342 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1346 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1347 svm_fifo_unset_event (rx_fifo);
1348 is_full = svm_fifo_is_full (rx_fifo);
1350 if (svm_fifo_is_empty (rx_fifo))
1358 svm_msg_q_lock (mq);
1359 if (svm_msg_q_is_empty (mq))
1360 svm_msg_q_wait (mq);
1362 svm_msg_q_sub_w_lock (mq, &msg);
1363 e = svm_msg_q_msg_data (mq, &msg);
1364 svm_msg_q_unlock (mq);
1365 if (!vcl_is_rx_evt_for_session (e, session_index,
1368 vcl_handle_mq_ctrl_event (e);
1369 svm_msg_q_free_msg (mq, &msg);
1372 svm_fifo_unset_event (rx_fifo);
1373 svm_msg_q_free_msg (mq, &msg);
1374 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1376 if (svm_fifo_is_empty (rx_fifo))
1383 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
1385 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1387 if (vcl_session_is_ct (s) && is_full)
1389 /* If the peer is not polling send notification */
1390 if (!svm_fifo_has_event (s->rx_fifo))
1391 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1392 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1395 if (VPPCOM_DEBUG > 2)
1398 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
1399 "from (%p)", getpid (), s->vpp_handle,
1400 session_index, n_read, rx_fifo);
1402 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
1403 "returning %d (%s)", getpid (), s->vpp_handle,
1404 session_index, n_read, vppcom_retval_str (n_read));
1410 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
1412 return (vppcom_session_read_internal (session_index, buf, n, 0));
1416 vppcom_session_peek (uint32_t session_index, void *buf, int n)
1418 return (vppcom_session_read_internal (session_index, buf, n, 1));
1422 vppcom_session_read_ready (vcl_session_t * session)
1424 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1425 if (PREDICT_FALSE (session->is_vep))
1427 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1428 "epoll session!", getpid (), vcl_session_index (session));
1429 return VPPCOM_EBADFD;
1432 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1434 session_state_t state = session->session_state;
1437 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1439 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1440 " state 0x%x (%s), returning %d (%s)", getpid (),
1441 session->vpp_handle, vcl_session_index (session), state,
1442 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1446 if (session->session_state & STATE_LISTEN)
1447 return clib_fifo_elts (session->accept_evts_fifo);
1449 return svm_fifo_max_dequeue (session->rx_fifo);
1453 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1456 return (e->event_type == FIFO_EVENT_APP_TX
1457 && e->fifo->client_session_index == sid);
1459 return (e->event_type == SESSION_IO_EVT_CT_RX);
1463 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
1465 int rv, n_write, is_nonblocking;
1466 vcl_session_t *s = 0;
1467 svm_fifo_t *tx_fifo = 0;
1468 session_evt_type_t et;
1469 svm_msg_q_msg_t msg;
1473 if (PREDICT_FALSE (!buf))
1474 return VPPCOM_EINVAL;
1476 s = vcl_session_get (session_index);
1477 if (PREDICT_FALSE (!s))
1478 return VPPCOM_EBADFD;
1480 tx_fifo = s->tx_fifo;
1481 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1483 if (PREDICT_FALSE (s->is_vep))
1485 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1486 "cannot write to an epoll session!",
1487 getpid (), s->vpp_handle, session_index);
1489 return VPPCOM_EBADFD;
1492 if (!(s->session_state & STATE_OPEN))
1494 session_state_t state = s->session_state;
1495 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1496 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1498 getpid (), s->vpp_handle, session_index,
1499 state, vppcom_session_state_str (state));
1503 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1504 if (svm_fifo_is_full (tx_fifo))
1508 return VPPCOM_EWOULDBLOCK;
1510 while (svm_fifo_is_full (tx_fifo))
1512 svm_msg_q_lock (mq);
1513 while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1515 svm_msg_q_sub_w_lock (mq, &msg);
1516 e = svm_msg_q_msg_data (mq, &msg);
1517 svm_msg_q_unlock (mq);
1519 if (!vcl_is_tx_evt_for_session (e, session_index,
1521 vcl_handle_mq_ctrl_event (e);
1522 svm_msg_q_free_msg (mq, &msg);
1526 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1527 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1529 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1530 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1532 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1535 ASSERT (n_write > 0);
1537 if (VPPCOM_DEBUG > 2)
1540 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1541 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
1542 session_index, tx_fifo);
1544 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1545 "wrote %d bytes tx-fifo: (%p)", getpid (),
1546 s->vpp_handle, session_index, n_write, tx_fifo);
1551 static vcl_session_t *
1552 vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
1555 s = vcl_session_get (f->client_session_index);
1559 if (type == 0 && s->rx_fifo == f)
1562 if (type == 1 && s->tx_fifo == f)
1565 s = vcl_session_get (f->master_session_index);
1568 if (type == 0 && s->rx_fifo == f)
1570 if (type == 1 && s->tx_fifo == f)
1577 vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
1579 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1580 if (PREDICT_FALSE (session->is_vep))
1582 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1583 "cannot write to an epoll session!",
1584 getpid (), session->vpp_handle, session_index);
1585 return VPPCOM_EBADFD;
1588 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1590 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1591 "cannot write to a listen session!",
1592 getpid (), session->vpp_handle, session_index);
1593 return VPPCOM_EBADFD;
1596 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1598 session_state_t state = session->session_state;
1601 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1602 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1603 "session is not open! state 0x%x (%s), "
1604 "returning %d (%s)", getpid (), session->vpp_handle,
1606 state, vppcom_session_state_str (state),
1607 rv, vppcom_retval_str (rv));
1611 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1612 getpid (), session->vpp_handle, session_index, session->tx_fifo,
1613 svm_fifo_max_enqueue (session->tx_fifo));
1615 return svm_fifo_max_enqueue (session->tx_fifo);
1619 vcl_mq_dequeue_batch (svm_msg_q_t * mq)
1621 svm_msg_q_msg_t *msg;
1625 n_msgs = svm_msg_q_size (mq);
1626 for (i = 0; i < n_msgs; i++)
1628 vec_add2 (vcm->mq_msg_vector, msg, 1);
1629 svm_msg_q_sub_w_lock (mq, msg);
1635 vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
1636 unsigned long *read_map, unsigned long *write_map,
1637 unsigned long *except_map, double time_to_wait,
1640 session_disconnected_msg_t *disconnected_msg;
1641 session_connected_msg_t *connected_msg;
1642 session_accepted_msg_t *accepted_msg;
1643 vcl_session_msg_t *vcl_msg;
1644 vcl_session_t *session;
1645 svm_msg_q_msg_t *msg;
1650 svm_msg_q_lock (mq);
1651 if (svm_msg_q_is_empty (mq))
1655 svm_msg_q_unlock (mq);
1661 svm_msg_q_unlock (mq);
1664 else if (time_to_wait < 0)
1666 svm_msg_q_wait (mq);
1670 if (svm_msg_q_timedwait (mq, time_to_wait))
1672 svm_msg_q_unlock (mq);
1677 vcl_mq_dequeue_batch (mq);
1678 svm_msg_q_unlock (mq);
1680 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
1682 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
1683 e = svm_msg_q_msg_data (mq, msg);
1684 switch (e->event_type)
1686 case FIFO_EVENT_APP_RX:
1687 sid = e->fifo->client_session_index;
1688 session = vcl_session_get (sid);
1689 if (sid < n_bits && read_map)
1691 clib_bitmap_set_no_check (read_map, sid, 1);
1695 case FIFO_EVENT_APP_TX:
1696 sid = e->fifo->client_session_index;
1697 session = vcl_session_get (sid);
1700 if (sid < n_bits && write_map)
1702 clib_bitmap_set_no_check (write_map, sid, 1);
1706 case SESSION_IO_EVT_CT_TX:
1707 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
1708 sid = vcl_session_index (session);
1709 if (sid < n_bits && read_map)
1711 clib_bitmap_set_no_check (read_map, sid, 1);
1716 case SESSION_IO_EVT_CT_RX:
1717 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
1718 sid = vcl_session_index (session);
1721 if (sid < n_bits && write_map)
1723 clib_bitmap_set_no_check (write_map, sid, 1);
1727 case SESSION_CTRL_EVT_ACCEPTED:
1728 accepted_msg = (session_accepted_msg_t *) e->data;
1729 handle = accepted_msg->listener_handle;
1730 session = vppcom_session_table_lookup_listener (handle);
1733 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1734 "listener handle %llx", getpid (), handle);
1738 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1739 vcl_msg->accepted_msg = *accepted_msg;
1740 sid = session - vcm->sessions;
1741 if (sid < n_bits && read_map)
1743 clib_bitmap_set_no_check (read_map, sid, 1);
1747 case SESSION_CTRL_EVT_CONNECTED:
1748 connected_msg = (session_connected_msg_t *) e->data;
1749 vcl_session_connected_handler (connected_msg);
1751 case SESSION_CTRL_EVT_DISCONNECTED:
1752 disconnected_msg = (session_disconnected_msg_t *) e->data;
1753 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
1754 if (sid < n_bits && except_map)
1756 clib_bitmap_set_no_check (except_map, sid, 1);
1760 case SESSION_CTRL_EVT_RESET:
1761 sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data);
1762 if (sid < n_bits && except_map)
1764 clib_bitmap_set_no_check (except_map, sid, 1);
1769 clib_warning ("unhandled: %u", e->event_type);
1772 svm_msg_q_free_msg (mq, msg);
1775 vec_reset_length (vcm->mq_msg_vector);
1780 vppcom_select_condvar (unsigned long n_bits, unsigned long *read_map,
1781 unsigned long *write_map, unsigned long *except_map,
1782 double time_to_wait, u32 * bits_set)
1784 double total_wait = 0, wait_slice;
1785 vcl_cut_through_registration_t *cr;
1787 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1788 wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
1792 pool_foreach (cr, vcm->cut_through_registrations, ({
1793 vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
1798 vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
1799 except_map, time_to_wait, bits_set);
1800 total_wait += wait_slice;
1804 while (total_wait < time_to_wait);
1810 vppcom_select_eventfd (unsigned long n_bits, unsigned long *read_map,
1811 unsigned long *write_map, unsigned long *except_map,
1812 double time_to_wait, u32 * bits_set)
1814 vcl_mq_evt_conn_t *mqc;
1815 int __clib_unused n_read;
1819 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
1820 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
1821 vec_len (vcm->mq_events), time_to_wait);
1822 for (i = 0; i < n_mq_evts; i++)
1824 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
1825 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1826 vcl_select_handle_mq (mqc->mq, n_bits, read_map, write_map,
1827 except_map, 0, bits_set);
1830 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1834 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1835 unsigned long *write_map, unsigned long *except_map,
1836 double time_to_wait)
1838 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1839 vcl_session_t *session = 0;
1842 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1844 if (n_bits && read_map)
1846 clib_bitmap_validate (vcm->rd_bitmap, minbits);
1847 clib_memcpy (vcm->rd_bitmap, read_map,
1848 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1849 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1851 if (n_bits && write_map)
1853 clib_bitmap_validate (vcm->wr_bitmap, minbits);
1854 clib_memcpy (vcm->wr_bitmap, write_map,
1855 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1856 memset (write_map, 0,
1857 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1859 if (n_bits && except_map)
1861 clib_bitmap_validate (vcm->ex_bitmap, minbits);
1862 clib_memcpy (vcm->ex_bitmap, except_map,
1863 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1864 memset (except_map, 0,
1865 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1875 clib_bitmap_foreach (sid, vcm->wr_bitmap, ({
1876 if (!(session = vcl_session_get (sid)))
1878 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1880 return VPPCOM_EBADFD;
1883 rv = svm_fifo_is_full (session->tx_fifo);
1886 clib_bitmap_set_no_check (write_map, sid, 1);
1895 clib_bitmap_foreach (sid, vcm->rd_bitmap, ({
1896 if (!(session = vcl_session_get (sid)))
1898 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1900 return VPPCOM_EBADFD;
1903 rv = vppcom_session_read_ready (session);
1906 clib_bitmap_set_no_check (read_map, sid, 1);
1914 if (vcm->cfg.use_mq_eventfd)
1915 vppcom_select_eventfd (n_bits, read_map, write_map, except_map,
1916 time_to_wait, &bits_set);
1918 vppcom_select_condvar (n_bits, read_map, write_map, except_map,
1919 time_to_wait, &bits_set);
1925 vep_verify_epoll_chain (u32 vep_idx)
1927 vcl_session_t *session;
1928 vppcom_epoll_t *vep;
1931 if (VPPCOM_DEBUG <= 1)
1934 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1935 session = vcl_session_get (vep_idx);
1936 if (PREDICT_FALSE (!session))
1938 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1939 getpid (), vep_idx);
1942 if (PREDICT_FALSE (!session->is_vep))
1944 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1945 getpid (), vep_idx);
1948 vep = &session->vep;
1949 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
1952 " is_vep_session = %u\n"
1953 " next_sid = 0x%x (%u)\n"
1954 " wait_cont_idx = 0x%x (%u)\n"
1955 "}\n", getpid (), vep_idx,
1956 session->is_vep, session->is_vep_session,
1957 vep->next_sid, vep->next_sid,
1958 session->wait_cont_idx, session->wait_cont_idx);
1960 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
1962 session = vcl_session_get (sid);
1963 if (PREDICT_FALSE (!session))
1965 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
1968 if (PREDICT_FALSE (session->is_vep))
1969 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1970 getpid (), vep_idx);
1971 else if (PREDICT_FALSE (!session->is_vep_session))
1973 clib_warning ("VCL<%d>: ERROR: session (%u) "
1974 "is not a vep session!", getpid (), sid);
1977 vep = &session->vep;
1978 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
1979 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
1980 "vep_idx (%u)!", getpid (),
1981 sid, session->vep.vep_idx, vep_idx);
1982 if (session->is_vep_session)
1984 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
1986 " next_sid = 0x%x (%u)\n"
1987 " prev_sid = 0x%x (%u)\n"
1988 " vep_idx = 0x%x (%u)\n"
1989 " ev.events = 0x%x\n"
1990 " ev.data.u64 = 0x%llx\n"
1994 vep->next_sid, vep->next_sid,
1995 vep->prev_sid, vep->prev_sid,
1996 vep->vep_idx, vep->vep_idx,
1997 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2002 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2003 getpid (), vep_idx);
2007 vppcom_epoll_create (void)
2009 vcl_session_t *vep_session;
2012 pool_get (vcm->sessions, vep_session);
2013 memset (vep_session, 0, sizeof (*vep_session));
2014 vep_idx = vep_session - vcm->sessions;
2016 vep_session->is_vep = 1;
2017 vep_session->vep.vep_idx = ~0;
2018 vep_session->vep.next_sid = ~0;
2019 vep_session->vep.prev_sid = ~0;
2020 vep_session->wait_cont_idx = ~0;
2021 vep_session->vpp_handle = ~0;
2022 vep_session->poll_reg = 0;
2024 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
2025 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2026 getpid (), vep_idx, vep_idx);
2032 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2033 struct epoll_event *event)
2035 vcl_session_t *vep_session;
2036 vcl_session_t *session;
2039 if (vep_idx == session_index)
2041 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
2042 getpid (), vep_idx);
2043 return VPPCOM_EINVAL;
2046 vep_session = vcl_session_get (vep_idx);
2047 if (PREDICT_FALSE (!vep_session))
2049 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
2050 return VPPCOM_EBADFD;
2052 if (PREDICT_FALSE (!vep_session->is_vep))
2054 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2055 getpid (), vep_idx);
2056 return VPPCOM_EINVAL;
2059 ASSERT (vep_session->vep.vep_idx == ~0);
2060 ASSERT (vep_session->vep.prev_sid == ~0);
2062 session = vcl_session_get (session_index);
2063 if (PREDICT_FALSE (!session))
2065 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
2066 getpid (), session_index);
2067 return VPPCOM_EBADFD;
2069 if (PREDICT_FALSE (session->is_vep))
2071 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2072 return VPPCOM_EINVAL;
2078 if (PREDICT_FALSE (!event))
2080 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
2081 "epoll_event structure!", getpid ());
2082 return VPPCOM_EINVAL;
2084 if (vep_session->vep.next_sid != ~0)
2086 vcl_session_t *next_session;
2087 next_session = vcl_session_get (vep_session->vep.next_sid);
2088 if (PREDICT_FALSE (!next_session))
2090 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
2091 "vep.next_sid (%u) on vep_idx (%u)!",
2092 getpid (), vep_session->vep.next_sid, vep_idx);
2093 return VPPCOM_EBADFD;
2095 ASSERT (next_session->vep.prev_sid == vep_idx);
2096 next_session->vep.prev_sid = session_index;
2098 session->vep.next_sid = vep_session->vep.next_sid;
2099 session->vep.prev_sid = vep_idx;
2100 session->vep.vep_idx = vep_idx;
2101 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2102 session->vep.ev = *event;
2103 session->is_vep = 0;
2104 session->is_vep_session = 1;
2105 vep_session->vep.next_sid = session_index;
2107 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
2108 "data 0x%llx!", getpid (), vep_idx, session_index, event->events,
2110 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2114 if (PREDICT_FALSE (!event))
2116 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2117 "epoll_event structure!", getpid ());
2121 else if (PREDICT_FALSE (!session->is_vep_session))
2123 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2124 "not a vep session!", getpid (), session_index);
2128 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2130 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2131 "vep_idx (%u) != vep_idx (%u)!",
2132 getpid (), session_index,
2133 session->vep.vep_idx, vep_idx);
2137 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2138 session->vep.ev = *event;
2139 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2140 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
2145 if (PREDICT_FALSE (!session->is_vep_session))
2147 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2148 "not a vep session!", getpid (), session_index);
2152 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2154 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2155 "vep_idx (%u) != vep_idx (%u)!",
2156 getpid (), session_index,
2157 session->vep.vep_idx, vep_idx);
2162 vep_session->wait_cont_idx =
2163 (vep_session->wait_cont_idx == session_index) ?
2164 session->vep.next_sid : vep_session->wait_cont_idx;
2166 if (session->vep.prev_sid == vep_idx)
2167 vep_session->vep.next_sid = session->vep.next_sid;
2170 vcl_session_t *prev_session;
2171 prev_session = vcl_session_get (session->vep.prev_sid);
2172 if (PREDICT_FALSE (!prev_session))
2174 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2175 "vep.prev_sid (%u) on sid (%u)!",
2176 getpid (), session->vep.prev_sid, session_index);
2177 return VPPCOM_EBADFD;
2179 ASSERT (prev_session->vep.next_sid == session_index);
2180 prev_session->vep.next_sid = session->vep.next_sid;
2182 if (session->vep.next_sid != ~0)
2184 vcl_session_t *next_session;
2185 next_session = vcl_session_get (session->vep.next_sid);
2186 if (PREDICT_FALSE (!next_session))
2188 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2189 "vep.next_sid (%u) on sid (%u)!",
2190 getpid (), session->vep.next_sid, session_index);
2191 return VPPCOM_EBADFD;
2193 ASSERT (next_session->vep.prev_sid == session_index);
2194 next_session->vep.prev_sid = session->vep.prev_sid;
2197 memset (&session->vep, 0, sizeof (session->vep));
2198 session->vep.next_sid = ~0;
2199 session->vep.prev_sid = ~0;
2200 session->vep.vep_idx = ~0;
2201 session->is_vep_session = 0;
2202 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2203 getpid (), vep_idx, session_index);
2204 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
2208 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2212 vep_verify_epoll_chain (vep_idx);
2219 vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
2220 u32 maxevents, double wait_for_time, u32 * num_ev)
2222 session_disconnected_msg_t *disconnected_msg;
2223 session_connected_msg_t *connected_msg;
2224 session_accepted_msg_t *accepted_msg;
2225 u64 session_evt_data = ~0, handle;
2226 u32 sid = ~0, session_events;
2227 vcl_session_msg_t *vcl_msg;
2228 vcl_session_t *session;
2229 svm_msg_q_msg_t *msg;
2234 svm_msg_q_lock (mq);
2235 if (svm_msg_q_is_empty (mq))
2239 svm_msg_q_unlock (mq);
2242 else if (wait_for_time < 0)
2244 svm_msg_q_wait (mq);
2248 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2250 svm_msg_q_unlock (mq);
2255 vcl_mq_dequeue_batch (mq);
2256 svm_msg_q_unlock (mq);
2258 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
2260 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
2261 e = svm_msg_q_msg_data (mq, msg);
2263 switch (e->event_type)
2265 case FIFO_EVENT_APP_RX:
2266 sid = e->fifo->client_session_index;
2267 session = vcl_session_get (sid);
2268 session_events = session->vep.ev.events;
2269 if (!(EPOLLIN & session->vep.ev.events))
2272 events[*num_ev].events |= EPOLLIN;
2273 session_evt_data = session->vep.ev.data.u64;
2275 case FIFO_EVENT_APP_TX:
2276 sid = e->fifo->client_session_index;
2277 session = vcl_session_get (sid);
2278 session_events = session->vep.ev.events;
2279 if (!(EPOLLOUT & session_events))
2282 events[*num_ev].events |= EPOLLOUT;
2283 session_evt_data = session->vep.ev.data.u64;
2285 case SESSION_IO_EVT_CT_TX:
2286 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
2287 sid = vcl_session_index (session);
2288 session_events = session->vep.ev.events;
2289 if (!(EPOLLIN & session->vep.ev.events))
2292 events[*num_ev].events |= EPOLLIN;
2293 session_evt_data = session->vep.ev.data.u64;
2295 case SESSION_IO_EVT_CT_RX:
2296 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
2297 sid = vcl_session_index (session);
2298 session_events = session->vep.ev.events;
2299 if (!(EPOLLOUT & session_events))
2302 events[*num_ev].events |= EPOLLOUT;
2303 session_evt_data = session->vep.ev.data.u64;
2305 case SESSION_CTRL_EVT_ACCEPTED:
2306 accepted_msg = (session_accepted_msg_t *) e->data;
2307 handle = accepted_msg->listener_handle;
2308 session = vppcom_session_table_lookup_listener (handle);
2311 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2312 "listener handle %llx", getpid (), handle);
2316 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2317 vcl_msg->accepted_msg = *accepted_msg;
2318 session_events = session->vep.ev.events;
2319 if (!(EPOLLIN & session_events))
2323 events[*num_ev].events |= EPOLLIN;
2324 session_evt_data = session->vep.ev.data.u64;
2326 case SESSION_CTRL_EVT_CONNECTED:
2327 connected_msg = (session_connected_msg_t *) e->data;
2328 vcl_session_connected_handler (connected_msg);
2329 /* Generate EPOLLOUT because there's no connected event */
2330 sid = vcl_session_get_index_from_handle (connected_msg->handle);
2331 session = vcl_session_get (sid);
2332 session_events = session->vep.ev.events;
2333 if (EPOLLOUT & session_events)
2336 events[*num_ev].events |= EPOLLOUT;
2337 session_evt_data = session->vep.ev.data.u64;
2340 case SESSION_CTRL_EVT_DISCONNECTED:
2341 disconnected_msg = (session_disconnected_msg_t *) e->data;
2342 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
2343 if (!(session = vcl_session_get (sid)))
2346 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2347 session_evt_data = session->vep.ev.data.u64;
2348 session_events = session->vep.ev.events;
2350 case SESSION_CTRL_EVT_RESET:
2351 sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data);
2352 if (!(session = vcl_session_get (sid)))
2355 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2356 session_evt_data = session->vep.ev.data.u64;
2357 session_events = session->vep.ev.events;
2360 clib_warning ("unhandled: %u", e->event_type);
2361 svm_msg_q_free_msg (mq, msg);
2364 svm_msg_q_free_msg (mq, msg);
2368 events[*num_ev].data.u64 = session_evt_data;
2369 if (EPOLLONESHOT & session_events)
2371 session = vcl_session_get (sid);
2372 session->vep.ev.events = 0;
2375 if (*num_ev == maxevents)
2380 vec_reset_length (vcm->mq_msg_vector);
2385 vppcom_epoll_wait_condvar (struct epoll_event *events, int maxevents,
2386 double wait_for_time)
2388 vcl_cut_through_registration_t *cr;
2389 double total_wait = 0, wait_slice;
2393 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2394 wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
2399 pool_foreach (cr, vcm->cut_through_registrations, ({
2400 vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
2404 rv = vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
2405 num_ev ? 0 : wait_slice, &num_ev);
2407 total_wait += wait_slice;
2411 while (total_wait < wait_for_time);
2412 return (int) num_ev;
2416 vppcom_epoll_wait_eventfd (struct epoll_event *events, int maxevents,
2417 double wait_for_time)
2419 vcl_mq_evt_conn_t *mqc;
2420 int __clib_unused n_read;
2425 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
2426 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
2427 vec_len (vcm->mq_events), wait_for_time);
2428 for (i = 0; i < n_mq_evts; i++)
2430 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
2431 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2432 vcl_epoll_wait_handle_mq (mqc->mq, events, maxevents, 0, &n_evts);
2435 return (int) n_evts;
2439 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2440 int maxevents, double wait_for_time)
2442 vcl_session_t *vep_session;
2444 if (PREDICT_FALSE (maxevents <= 0))
2446 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2447 getpid (), maxevents);
2448 return VPPCOM_EINVAL;
2451 vep_session = vcl_session_get (vep_idx);
2452 if (PREDICT_FALSE (!vep_session->is_vep))
2454 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2455 getpid (), vep_idx);
2456 return VPPCOM_EINVAL;
2459 memset (events, 0, sizeof (*events) * maxevents);
2461 if (vcm->cfg.use_mq_eventfd)
2462 return vppcom_epoll_wait_eventfd (events, maxevents, wait_for_time);
2464 return vppcom_epoll_wait_condvar (events, maxevents, wait_for_time);
2468 vppcom_session_attr (uint32_t session_index, uint32_t op,
2469 void *buffer, uint32_t * buflen)
2471 vcl_session_t *session;
2473 u32 *flags = buffer;
2474 vppcom_endpt_t *ep = buffer;
2476 session = vcl_session_get (session_index);
2478 return VPPCOM_EBADFD;
2482 case VPPCOM_ATTR_GET_NREAD:
2483 rv = vppcom_session_read_ready (session);
2484 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2488 case VPPCOM_ATTR_GET_NWRITE:
2489 rv = vppcom_session_write_ready (session, session_index);
2490 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2491 getpid (), session_index, rv);
2494 case VPPCOM_ATTR_GET_FLAGS:
2495 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2497 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2498 VCL_SESS_ATTR_NONBLOCK));
2499 *buflen = sizeof (*flags);
2500 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2501 "is_nonblocking = %u", getpid (),
2502 session_index, *flags,
2503 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2509 case VPPCOM_ATTR_SET_FLAGS:
2510 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2512 if (*flags & O_NONBLOCK)
2513 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2515 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2517 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2518 " is_nonblocking = %u",
2519 getpid (), session_index, *flags,
2520 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2526 case VPPCOM_ATTR_GET_PEER_ADDR:
2527 if (PREDICT_TRUE (buffer && buflen &&
2528 (*buflen >= sizeof (*ep)) && ep->ip))
2530 ep->is_ip4 = session->transport.is_ip4;
2531 ep->port = session->transport.rmt_port;
2532 if (session->transport.is_ip4)
2533 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2534 sizeof (ip4_address_t));
2536 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2537 sizeof (ip6_address_t));
2538 *buflen = sizeof (*ep);
2539 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2540 "addr = %U, port %u", getpid (),
2541 session_index, ep->is_ip4, format_ip46_address,
2542 &session->transport.rmt_ip,
2543 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2544 clib_net_to_host_u16 (ep->port));
2550 case VPPCOM_ATTR_GET_LCL_ADDR:
2551 if (PREDICT_TRUE (buffer && buflen &&
2552 (*buflen >= sizeof (*ep)) && ep->ip))
2554 ep->is_ip4 = session->transport.is_ip4;
2555 ep->port = session->transport.lcl_port;
2556 if (session->transport.is_ip4)
2557 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2558 sizeof (ip4_address_t));
2560 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2561 sizeof (ip6_address_t));
2562 *buflen = sizeof (*ep);
2563 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2564 " addr = %U port %d", getpid (),
2565 session_index, ep->is_ip4, format_ip46_address,
2566 &session->transport.lcl_ip,
2567 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2568 clib_net_to_host_u16 (ep->port));
2574 case VPPCOM_ATTR_GET_LIBC_EPFD:
2575 rv = session->libc_epfd;
2576 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2580 case VPPCOM_ATTR_SET_LIBC_EPFD:
2581 if (PREDICT_TRUE (buffer && buflen &&
2582 (*buflen == sizeof (session->libc_epfd))))
2584 session->libc_epfd = *(int *) buffer;
2585 *buflen = sizeof (session->libc_epfd);
2587 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2588 "buflen %d", getpid (), session->libc_epfd, *buflen);
2594 case VPPCOM_ATTR_GET_PROTOCOL:
2595 if (buffer && buflen && (*buflen >= sizeof (int)))
2597 *(int *) buffer = session->session_type;
2598 *buflen = sizeof (int);
2600 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2601 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2608 case VPPCOM_ATTR_GET_LISTEN:
2609 if (buffer && buflen && (*buflen >= sizeof (int)))
2611 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2612 VCL_SESS_ATTR_LISTEN);
2613 *buflen = sizeof (int);
2615 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2616 getpid (), *(int *) buffer, *buflen);
2622 case VPPCOM_ATTR_GET_ERROR:
2623 if (buffer && buflen && (*buflen >= sizeof (int)))
2625 *(int *) buffer = 0;
2626 *buflen = sizeof (int);
2628 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2629 getpid (), *(int *) buffer, *buflen);
2635 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2636 if (buffer && buflen && (*buflen >= sizeof (u32)))
2640 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2641 session->tx_fifo ? session->tx_fifo->nitems :
2642 vcm->cfg.tx_fifo_size);
2643 *buflen = sizeof (u32);
2645 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2646 "buflen %d, #VPP-TBD#", getpid (),
2647 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2653 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2654 if (buffer && buflen && (*buflen == sizeof (u32)))
2657 session->sndbuf_size = *(u32 *) buffer;
2658 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2659 "buflen %d, #VPP-TBD#", getpid (),
2660 session->sndbuf_size, session->sndbuf_size, *buflen);
2666 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2667 if (buffer && buflen && (*buflen >= sizeof (u32)))
2671 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2672 session->rx_fifo ? session->rx_fifo->nitems :
2673 vcm->cfg.rx_fifo_size);
2674 *buflen = sizeof (u32);
2676 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2677 "buflen %d, #VPP-TBD#", getpid (),
2678 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2684 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2685 if (buffer && buflen && (*buflen == sizeof (u32)))
2688 session->rcvbuf_size = *(u32 *) buffer;
2689 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2690 "buflen %d, #VPP-TBD#", getpid (),
2691 session->sndbuf_size, session->sndbuf_size, *buflen);
2697 case VPPCOM_ATTR_GET_REUSEADDR:
2698 if (buffer && buflen && (*buflen >= sizeof (int)))
2701 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2702 VCL_SESS_ATTR_REUSEADDR);
2703 *buflen = sizeof (int);
2705 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2706 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2712 case VPPCOM_ATTR_SET_REUSEADDR:
2713 if (buffer && buflen && (*buflen == sizeof (int)) &&
2714 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2717 if (*(int *) buffer)
2718 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2720 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2722 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2723 " #VPP-TBD#", getpid (),
2724 VCL_SESS_ATTR_TEST (session->attr,
2725 VCL_SESS_ATTR_REUSEADDR), *buflen);
2731 case VPPCOM_ATTR_GET_REUSEPORT:
2732 if (buffer && buflen && (*buflen >= sizeof (int)))
2735 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2736 VCL_SESS_ATTR_REUSEPORT);
2737 *buflen = sizeof (int);
2739 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2740 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2746 case VPPCOM_ATTR_SET_REUSEPORT:
2747 if (buffer && buflen && (*buflen == sizeof (int)) &&
2748 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2751 if (*(int *) buffer)
2752 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2754 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2756 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2757 " #VPP-TBD#", getpid (),
2758 VCL_SESS_ATTR_TEST (session->attr,
2759 VCL_SESS_ATTR_REUSEPORT), *buflen);
2765 case VPPCOM_ATTR_GET_BROADCAST:
2766 if (buffer && buflen && (*buflen >= sizeof (int)))
2769 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2770 VCL_SESS_ATTR_BROADCAST);
2771 *buflen = sizeof (int);
2773 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2774 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2780 case VPPCOM_ATTR_SET_BROADCAST:
2781 if (buffer && buflen && (*buflen == sizeof (int)))
2784 if (*(int *) buffer)
2785 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2787 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2789 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2790 "#VPP-TBD#", getpid (),
2791 VCL_SESS_ATTR_TEST (session->attr,
2792 VCL_SESS_ATTR_BROADCAST), *buflen);
2798 case VPPCOM_ATTR_GET_V6ONLY:
2799 if (buffer && buflen && (*buflen >= sizeof (int)))
2802 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2803 VCL_SESS_ATTR_V6ONLY);
2804 *buflen = sizeof (int);
2806 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2807 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2813 case VPPCOM_ATTR_SET_V6ONLY:
2814 if (buffer && buflen && (*buflen == sizeof (int)))
2817 if (*(int *) buffer)
2818 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2820 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2822 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2823 "#VPP-TBD#", getpid (),
2824 VCL_SESS_ATTR_TEST (session->attr,
2825 VCL_SESS_ATTR_V6ONLY), *buflen);
2831 case VPPCOM_ATTR_GET_KEEPALIVE:
2832 if (buffer && buflen && (*buflen >= sizeof (int)))
2835 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2836 VCL_SESS_ATTR_KEEPALIVE);
2837 *buflen = sizeof (int);
2839 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2840 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2846 case VPPCOM_ATTR_SET_KEEPALIVE:
2847 if (buffer && buflen && (*buflen == sizeof (int)))
2850 if (*(int *) buffer)
2851 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2853 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2855 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2856 "#VPP-TBD#", getpid (),
2857 VCL_SESS_ATTR_TEST (session->attr,
2858 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2864 case VPPCOM_ATTR_GET_TCP_NODELAY:
2865 if (buffer && buflen && (*buflen >= sizeof (int)))
2868 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2869 VCL_SESS_ATTR_TCP_NODELAY);
2870 *buflen = sizeof (int);
2872 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2873 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2879 case VPPCOM_ATTR_SET_TCP_NODELAY:
2880 if (buffer && buflen && (*buflen == sizeof (int)))
2883 if (*(int *) buffer)
2884 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2886 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2888 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2889 "#VPP-TBD#", getpid (),
2890 VCL_SESS_ATTR_TEST (session->attr,
2891 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
2897 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2898 if (buffer && buflen && (*buflen >= sizeof (int)))
2901 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2902 VCL_SESS_ATTR_TCP_KEEPIDLE);
2903 *buflen = sizeof (int);
2905 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2906 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2912 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
2913 if (buffer && buflen && (*buflen == sizeof (int)))
2916 if (*(int *) buffer)
2917 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2919 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2921 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2922 "#VPP-TBD#", getpid (),
2923 VCL_SESS_ATTR_TEST (session->attr,
2924 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
2930 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2931 if (buffer && buflen && (*buflen >= sizeof (int)))
2934 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2935 VCL_SESS_ATTR_TCP_KEEPINTVL);
2936 *buflen = sizeof (int);
2938 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
2939 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2945 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
2946 if (buffer && buflen && (*buflen == sizeof (int)))
2949 if (*(int *) buffer)
2950 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2952 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2954 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
2955 "#VPP-TBD#", getpid (),
2956 VCL_SESS_ATTR_TEST (session->attr,
2957 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
2963 case VPPCOM_ATTR_GET_TCP_USER_MSS:
2964 if (buffer && buflen && (*buflen >= sizeof (u32)))
2967 *(u32 *) buffer = session->user_mss;
2968 *buflen = sizeof (int);
2970 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
2971 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2977 case VPPCOM_ATTR_SET_TCP_USER_MSS:
2978 if (buffer && buflen && (*buflen == sizeof (u32)))
2981 session->user_mss = *(u32 *) buffer;
2983 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
2984 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
2999 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3000 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3003 vcl_session_t *session = 0;
3007 session = vcl_session_get (session_index);
3008 if (PREDICT_FALSE (!session))
3010 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3011 getpid (), session_index);
3012 return VPPCOM_EBADFD;
3014 ep->is_ip4 = session->transport.is_ip4;
3015 ep->port = session->transport.rmt_port;
3019 rv = vppcom_session_read (session_index, buffer, buflen);
3020 else if (flags & MSG_PEEK)
3021 rv = vppcom_session_peek (session_index, buffer, buflen);
3024 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3026 return VPPCOM_EAFNOSUPPORT;
3031 if (session->transport.is_ip4)
3032 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3033 sizeof (ip4_address_t));
3035 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3036 sizeof (ip6_address_t));
3043 vppcom_session_sendto (uint32_t session_index, void *buffer,
3044 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3047 return VPPCOM_EINVAL;
3052 return VPPCOM_EINVAL;
3057 // TBD check the flags and do the right thing
3058 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3059 getpid (), flags, flags);
3062 return (vppcom_session_write (session_index, buffer, buflen));
3066 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3068 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3069 u32 i, keep_trying = 1;
3072 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3073 getpid (), vp, n_sids, wait_for_time);
3076 return VPPCOM_EFAULT;
3080 vcl_session_t *session;
3082 for (i = 0; i < n_sids; i++)
3084 ASSERT (vp[i].revents);
3086 session = vcl_session_get (vp[i].sid);
3093 if (POLLIN & vp[i].events)
3095 rv = vppcom_session_read_ready (session);
3098 *vp[i].revents |= POLLIN;
3105 case VPPCOM_ECONNRESET:
3106 *vp[i].revents = POLLHUP;
3110 *vp[i].revents = POLLERR;
3117 if (POLLOUT & vp[i].events)
3119 rv = vppcom_session_write_ready (session, vp[i].sid);
3122 *vp[i].revents |= POLLOUT;
3129 case VPPCOM_ECONNRESET:
3130 *vp[i].revents = POLLHUP;
3134 *vp[i].revents = POLLERR;
3141 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3143 *vp[i].revents = POLLNVAL;
3147 if (wait_for_time != -1)
3148 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3150 while ((num_ev == 0) && keep_trying);
3152 if (VPPCOM_DEBUG > 3)
3154 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3155 for (i = 0; i < n_sids; i++)
3157 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3158 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3159 vp[i].events, *vp[i].revents);
3166 vppcom_mq_epoll_fd (void)
3168 return vcm->mqs_epfd;
3172 * fd.io coding-style-patch-verification: ON
3175 * eval: (c-set-style "gnu")