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_ct_registration_add (svm_msg_q_t * mq, u32 sid)
254 vcl_cut_through_registration_t *cr;
255 pool_get (vcm->cut_through_registrations, cr);
258 return (cr - vcm->cut_through_registrations);
262 vcl_ct_registration_del (u32 ct_index)
264 pool_put_index (vcm->cut_through_registrations, ct_index);
267 static vcl_session_t *
268 vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
271 s = vcl_session_get (f->client_session_index);
275 if (type == 0 && s->rx_fifo == f)
278 if (type == 1 && s->tx_fifo == f)
281 s = vcl_session_get (f->master_session_index);
284 if (type == 0 && s->rx_fifo == f)
286 if (type == 1 && s->tx_fifo == f)
293 vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
294 session_handle_t handle, int retval)
296 app_session_evt_t _app_evt, *app_evt = &_app_evt;
297 session_accepted_reply_msg_t *rmp;
298 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
299 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
300 rmp->handle = handle;
301 rmp->context = context;
302 rmp->retval = retval;
303 app_send_ctrl_evt_to_vpp (mq, app_evt);
307 vcl_session_accepted_handler (session_accepted_msg_t * mp)
309 vcl_session_t *session, *listen_session;
310 svm_fifo_t *rx_fifo, *tx_fifo;
315 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
319 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
320 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
321 "unknown vpp listener handle %llx",
322 getpid (), mp->listener_handle);
323 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
324 VNET_API_ERROR_INVALID_ARGUMENT);
325 return VCL_INVALID_SESSION_INDEX;
328 pool_get (vcm->sessions, session);
329 memset (session, 0, sizeof (*session));
330 session_index = (u32) (session - vcm->sessions);
332 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
333 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
335 if (mp->server_event_queue_address)
337 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
339 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
341 vcl_wait_for_memory (session->vpp_evt_q);
342 session->ct_registration = vcl_ct_registration_add (session->our_evt_q,
344 rx_fifo->master_session_index = session_index;
345 tx_fifo->master_session_index = session_index;
349 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
351 rx_fifo->client_session_index = session_index;
352 tx_fifo->client_session_index = session_index;
355 session->vpp_handle = mp->handle;
356 session->client_context = mp->context;
357 session->rx_fifo = rx_fifo;
358 session->tx_fifo = tx_fifo;
360 session->session_state = STATE_ACCEPT;
361 session->transport.rmt_port = mp->port;
362 session->transport.is_ip4 = mp->is_ip4;
363 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
365 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
366 session->transport.lcl_port = listen_session->transport.lcl_port;
367 session->transport.lcl_ip = listen_session->transport.lcl_ip;
368 session->session_type = listen_session->session_type;
369 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
371 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
372 " address %U port %d queue %p!", getpid (), mp->handle, session_index,
373 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
374 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
375 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
376 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
378 VCL_SESSION_UNLOCK ();
379 return session_index;
383 vcl_session_connected_handler (session_connected_msg_t * mp)
385 vcl_session_t *session = 0;
387 svm_fifo_t *rx_fifo, *tx_fifo;
390 session_index = mp->context;
391 VCL_SESSION_LOCK_AND_GET (session_index, &session);
395 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
396 "connect failed! %U",
397 getpid (), mp->handle, session_index,
398 format_api_error, ntohl (mp->retval));
401 session->session_state = STATE_FAILED;
402 session->vpp_handle = mp->handle;
406 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
407 "Invalid session index (%u)!",
408 getpid (), mp->handle, session_index);
416 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
417 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
418 vcl_wait_for_memory (rx_fifo);
419 rx_fifo->client_session_index = session_index;
420 tx_fifo->client_session_index = session_index;
422 if (mp->client_event_queue_address)
424 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
426 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
428 session->ct_registration = vcl_ct_registration_add (session->our_evt_q,
432 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
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 VCL_SESSION_UNLOCK ();
453 return session_index;
457 vcl_handle_mq_ctrl_event (session_event_t * e)
459 session_accepted_msg_t *accepted_msg;
460 session_disconnected_msg_t *disconnected_msg;
461 vcl_session_msg_t *vcl_msg;
462 vcl_session_t *session;
466 switch (e->event_type)
468 case FIFO_EVENT_APP_RX:
469 clib_warning ("unhandled rx: sid %u (0x%x)",
470 e->fifo->client_session_index,
471 e->fifo->client_session_index);
473 case SESSION_CTRL_EVT_ACCEPTED:
474 accepted_msg = (session_accepted_msg_t *) e->data;
475 handle = accepted_msg->listener_handle;
476 session = vppcom_session_table_lookup_listener (handle);
479 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
480 "listener handle %llx", getpid (), handle);
484 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
485 vcl_msg->accepted_msg = *accepted_msg;
487 case SESSION_CTRL_EVT_CONNECTED:
488 vcl_session_connected_handler ((session_connected_msg_t *) e->data);
490 case SESSION_CTRL_EVT_DISCONNECTED:
491 disconnected_msg = (session_disconnected_msg_t *) e->data;
492 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
493 session = vcl_session_get (sid);
494 session->session_state = STATE_DISCONNECT;
495 VDBG (0, "disconnected %u", sid);
498 clib_warning ("unhandled %u", e->event_type);
504 vppcom_wait_for_session_state_change (u32 session_index,
505 session_state_t state,
508 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
509 vcl_session_t *volatile session;
517 rv = vppcom_session_at_index (session_index, &session);
518 if (PREDICT_FALSE (rv))
520 VCL_SESSION_UNLOCK ();
523 if (session->session_state & state)
525 VCL_SESSION_UNLOCK ();
528 if (session->session_state & STATE_FAILED)
530 VCL_SESSION_UNLOCK ();
531 return VPPCOM_ECONNREFUSED;
533 VCL_SESSION_UNLOCK ();
535 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
537 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
538 vcl_handle_mq_ctrl_event (e);
539 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
541 while (clib_time_now (&vcm->clib_time) < timeout);
543 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
544 vppcom_session_state_str (state));
545 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
547 return VPPCOM_ETIMEDOUT;
551 vppcom_app_session_enable (void)
555 if (vcm->app_state != STATE_APP_ENABLED)
557 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
558 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
559 if (PREDICT_FALSE (rv))
561 VDBG (0, "VCL<%d>: application session enable timed out! "
562 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
570 vppcom_app_attach (void)
574 vppcom_app_send_attach ();
575 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
576 if (PREDICT_FALSE (rv))
578 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
579 getpid (), rv, vppcom_retval_str (rv));
587 vppcom_session_unbind (u32 session_index)
589 vcl_session_t *session = 0;
593 VCL_SESSION_LOCK_AND_GET (session_index, &session);
595 vpp_handle = session->vpp_handle;
596 vppcom_session_table_del_listener (vpp_handle);
597 session->vpp_handle = ~0;
598 session->session_state = STATE_DISCONNECT;
600 VCL_SESSION_UNLOCK ();
602 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
603 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
604 vppcom_session_state_str (STATE_DISCONNECT));
605 vcl_evt (VCL_EVT_UNBIND, session);
606 vppcom_send_unbind_sock (vpp_handle);
613 vppcom_session_disconnect (u32 session_index)
616 vcl_session_t *session;
618 session_state_t state;
620 VCL_SESSION_LOCK_AND_GET (session_index, &session);
622 vpp_handle = session->vpp_handle;
623 state = session->session_state;
624 VCL_SESSION_UNLOCK ();
626 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
627 vpp_handle, session_index, state, vppcom_session_state_str (state));
629 if (PREDICT_FALSE (state & STATE_LISTEN))
631 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
632 "Cannot disconnect a listen socket!",
633 getpid (), vpp_handle, session_index);
638 if (state & STATE_CLOSE_ON_EMPTY)
640 vppcom_send_disconnect_session_reply (vpp_handle, session_index,
642 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
643 "REPLY...", getpid (), vpp_handle, session_index);
647 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
648 getpid (), vpp_handle, session_index);
649 vppcom_send_disconnect_session (vpp_handle, session_index);
657 * VPPCOM Public API functions
660 vppcom_app_create (char *app_name)
662 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
668 vppcom_cfg (&vcm->cfg);
670 clib_spinlock_init (&vcm->session_fifo_lockp);
671 clib_fifo_validate (vcm->client_session_index_fifo,
672 vcm->cfg.listen_queue_size);
673 clib_spinlock_init (&vcm->sessions_lockp);
676 vcm->main_cpu = os_get_thread_index ();
678 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
680 clib_time_init (&vcm->clib_time);
681 vppcom_init_error_string_table ();
682 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
683 20 /* timeout in secs */ );
686 if (vcm->my_client_index == ~0)
688 /* API hookup and connect to VPP */
689 vppcom_api_hookup ();
691 vcm->app_state = STATE_APP_START;
692 rv = vppcom_connect_to_vpp (app_name);
695 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
700 /* State event handling thread */
702 rv = vce_start_event_thread (&(vcm->event_thread), 20);
704 VDBG (0, "VCL<%d>: sending session enable", getpid ());
706 rv = vppcom_app_session_enable ();
709 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
710 "failed!", getpid ());
714 VDBG (0, "VCL<%d>: sending app attach", getpid ());
716 rv = vppcom_app_attach ();
719 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
724 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
725 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
732 vppcom_app_destroy (void)
735 f64 orig_app_timeout;
737 if (vcm->my_client_index == ~0)
740 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
741 getpid (), vcm->my_client_index, vcm->my_client_index);
742 vcl_evt (VCL_EVT_DETACH, vcm);
744 vppcom_app_send_detach ();
745 orig_app_timeout = vcm->cfg.app_timeout;
746 vcm->cfg.app_timeout = 2.0;
747 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
748 vcm->cfg.app_timeout = orig_app_timeout;
749 if (PREDICT_FALSE (rv))
750 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
751 getpid (), rv, vppcom_retval_str (rv));
754 vl_client_disconnect_from_vlib ();
755 vcm->my_client_index = ~0;
756 vcm->app_state = STATE_APP_START;
760 vppcom_session_create (u8 proto, u8 is_nonblocking)
762 vcl_session_t *session;
766 pool_get (vcm->sessions, session);
767 memset (session, 0, sizeof (*session));
768 session_index = session - vcm->sessions;
770 session->session_type = proto;
771 session->session_state = STATE_START;
772 session->vpp_handle = ~0;
773 session->is_dgram = proto == VPPCOM_PROTO_UDP;
776 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
778 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
779 is_nonblocking, session_index);
781 VCL_SESSION_UNLOCK ();
783 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
785 return (int) session_index;
789 vppcom_session_close (uint32_t session_index)
791 vcl_session_t *session = 0;
799 session_state_t state;
801 VCL_SESSION_LOCK_AND_GET (session_index, &session);
802 is_vep = session->is_vep;
803 is_vep_session = session->is_vep_session;
804 next_sid = session->vep.next_sid;
805 vep_idx = session->vep.vep_idx;
806 state = session->session_state;
807 vpp_handle = session->vpp_handle;
808 VCL_SESSION_UNLOCK ();
810 if (VPPCOM_DEBUG > 0)
813 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
814 "closing epoll session...",
815 getpid (), session_index, session_index);
817 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
818 "closing session...",
819 getpid (), vpp_handle, session_index);
824 while (next_sid != ~0)
826 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
827 if (PREDICT_FALSE (rv < 0))
828 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
829 "vep_idx %u failed! rv %d (%s)",
830 getpid (), vpp_handle, next_sid, vep_idx,
831 rv, vppcom_retval_str (rv));
833 VCL_SESSION_LOCK_AND_GET (session_index, &session);
834 next_sid = session->vep.next_sid;
835 VCL_SESSION_UNLOCK ();
842 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
844 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
845 "vep_idx %u failed! rv %d (%s)",
846 getpid (), vpp_handle, session_index,
847 vep_idx, rv, vppcom_retval_str (rv));
850 if (state & STATE_LISTEN)
852 rv = vppcom_session_unbind (session_index);
853 if (PREDICT_FALSE (rv < 0))
854 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
855 "failed! rv %d (%s)",
856 getpid (), vpp_handle, session_index,
857 rv, vppcom_retval_str (rv));
859 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
861 rv = vppcom_session_disconnect (session_index);
862 if (PREDICT_FALSE (rv < 0))
863 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
864 "session disconnect failed! rv %d (%s)",
865 getpid (), vpp_handle, session_index,
866 rv, vppcom_retval_str (rv));
870 VCL_SESSION_LOCK_AND_GET (session_index, &session);
871 if (session->our_evt_q)
872 vcl_ct_registration_del (session->ct_registration);
874 vpp_handle = session->vpp_handle;
875 if (vpp_handle != ~0)
877 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
879 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
881 pool_put_index (vcm->sessions, session_index);
883 VCL_SESSION_UNLOCK ();
885 if (VPPCOM_DEBUG > 0)
888 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
889 getpid (), session_index, session_index);
891 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
892 getpid (), vpp_handle, session_index);
896 vcl_evt (VCL_EVT_CLOSE, session, rv);
902 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
904 vcl_session_t *session = 0;
908 return VPPCOM_EINVAL;
910 VCL_SESSION_LOCK_AND_GET (session_index, &session);
914 VCL_SESSION_UNLOCK ();
915 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
916 "bind to an epoll session!", getpid (), session_index);
921 session->transport.is_ip4 = ep->is_ip4;
923 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
924 sizeof (ip4_address_t));
926 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
927 sizeof (ip6_address_t));
928 session->transport.lcl_port = ep->port;
930 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
931 "proto %s", getpid (), session_index,
932 session->transport.is_ip4 ? "IPv4" : "IPv6",
933 format_ip46_address, &session->transport.lcl_ip,
934 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
935 clib_net_to_host_u16 (session->transport.lcl_port),
936 session->session_type ? "UDP" : "TCP");
937 vcl_evt (VCL_EVT_BIND, session);
938 VCL_SESSION_UNLOCK ();
940 if (session->session_type == VPPCOM_PROTO_UDP)
941 vppcom_session_listen (session_index, 10);
948 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
950 vcl_session_t *listen_session = 0;
951 u64 listen_vpp_handle;
954 if (q_len == 0 || q_len == ~0)
955 q_len = vcm->cfg.listen_queue_size;
957 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
959 if (listen_session->is_vep)
961 VCL_SESSION_UNLOCK ();
962 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
963 "epoll session!", getpid (), listen_session_index);
968 listen_vpp_handle = listen_session->vpp_handle;
969 if (listen_session->session_state & STATE_LISTEN)
971 VCL_SESSION_UNLOCK ();
972 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
973 getpid (), listen_vpp_handle, listen_session_index);
978 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
979 "request...", getpid (), listen_vpp_handle, listen_session_index);
981 vppcom_send_bind_sock (listen_session, listen_session_index);
982 VCL_SESSION_UNLOCK ();
983 retval = vppcom_wait_for_session_state_change (listen_session_index,
985 vcm->cfg.session_timeout);
987 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
988 if (PREDICT_FALSE (retval))
990 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
991 "returning %d (%s)", getpid (), listen_session->vpp_handle,
992 listen_session_index, retval, vppcom_retval_str (retval));
993 VCL_SESSION_UNLOCK ();
998 VCL_SESSION_UNLOCK ();
1005 validate_args_session_accept_ (vcl_session_t * listen_session)
1007 u32 listen_session_index = listen_session - vcm->sessions;
1009 /* Input validation - expects spinlock on sessions_lockp */
1010 if (listen_session->is_vep)
1012 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1013 "epoll session!", getpid (), listen_session_index);
1014 return VPPCOM_EBADFD;
1017 if (listen_session->session_state != STATE_LISTEN)
1019 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1020 "not in listen state! state 0x%x (%s)", getpid (),
1021 listen_session->vpp_handle, listen_session_index,
1022 listen_session->session_state,
1023 vppcom_session_state_str (listen_session->session_state));
1024 return VPPCOM_EBADFD;
1030 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
1033 session_accepted_msg_t accepted_msg;
1034 vcl_session_t *listen_session = 0;
1035 vcl_session_t *client_session = 0;
1036 u32 client_session_index = ~0;
1037 svm_msg_q_t *vpp_evt_q;
1038 vcl_session_msg_t *evt;
1039 u64 listen_vpp_handle;
1040 svm_msg_q_msg_t msg;
1045 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
1047 if (validate_args_session_accept_ (listen_session))
1049 VCL_SESSION_UNLOCK ();
1053 VCL_SESSION_UNLOCK ();
1055 if (clib_fifo_elts (listen_session->accept_evts_fifo))
1057 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1058 accepted_msg = evt->accepted_msg;
1062 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1063 VCL_SESS_ATTR_NONBLOCK);
1064 if (svm_msg_q_is_empty (vcm->app_event_queue) && is_nonblocking)
1065 return VPPCOM_EAGAIN;
1069 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_WAIT, 0))
1070 return VPPCOM_EAGAIN;
1072 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
1073 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
1075 clib_warning ("discarded event: %u", e->event_type);
1076 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1079 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1080 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1086 client_session_index = vcl_session_accepted_handler (&accepted_msg);
1087 VCL_SESSION_LOCK_AND_GET (client_session_index, &client_session);
1088 rv = client_session_index;
1090 if (flags & O_NONBLOCK)
1091 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
1093 listen_vpp_handle = listen_session->vpp_handle;
1094 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1095 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1096 getpid (), listen_vpp_handle, listen_session_index,
1097 client_session->vpp_handle, client_session_index,
1098 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1099 VCL_SESS_ATTR_NONBLOCK));
1103 ep->is_ip4 = client_session->transport.is_ip4;
1104 ep->port = client_session->transport.rmt_port;
1105 if (client_session->transport.is_ip4)
1106 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
1107 sizeof (ip4_address_t));
1109 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
1110 sizeof (ip6_address_t));
1113 if (accepted_msg.server_event_queue_address)
1114 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1117 vpp_evt_q = client_session->vpp_evt_q;
1118 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1119 client_session->vpp_handle, 0);
1121 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1122 "sid %u connection from peer %s address %U port %u to local %s "
1123 "address %U port %u", getpid (), listen_vpp_handle,
1124 listen_session_index, client_session->vpp_handle,
1125 client_session_index,
1126 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1127 format_ip46_address, &client_session->transport.rmt_ip,
1128 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1129 clib_net_to_host_u16 (client_session->transport.rmt_port),
1130 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1131 format_ip46_address, &client_session->transport.lcl_ip,
1132 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1133 clib_net_to_host_u16 (client_session->transport.lcl_port));
1134 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1135 client_session_index);
1136 VCL_SESSION_UNLOCK ();
1143 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
1145 vcl_session_t *session = 0;
1147 int rv, retval = VPPCOM_OK;
1149 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1151 if (PREDICT_FALSE (session->is_vep))
1153 VCL_SESSION_UNLOCK ();
1154 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1155 "connect on an epoll session!", getpid (), session_index);
1160 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1162 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1163 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1164 getpid (), session->vpp_handle, session_index,
1165 session->transport.is_ip4 ? "IPv4" : "IPv6",
1166 format_ip46_address,
1167 &session->transport.rmt_ip, session->transport.is_ip4 ?
1168 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1169 clib_net_to_host_u16 (session->transport.rmt_port),
1170 session->session_type ? "UDP" : "TCP", session->session_state,
1171 vppcom_session_state_str (session->session_state));
1173 VCL_SESSION_UNLOCK ();
1177 session->transport.is_ip4 = server_ep->is_ip4;
1178 if (session->transport.is_ip4)
1179 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
1180 sizeof (ip4_address_t));
1182 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
1183 sizeof (ip6_address_t));
1184 session->transport.rmt_port = server_ep->port;
1186 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1188 getpid (), session->vpp_handle, session_index,
1189 session->transport.is_ip4 ? "IPv4" : "IPv6",
1190 format_ip46_address,
1191 &session->transport.rmt_ip, session->transport.is_ip4 ?
1192 IP46_TYPE_IP4 : IP46_TYPE_IP6,
1193 clib_net_to_host_u16 (session->transport.rmt_port),
1194 session->session_type ? "UDP" : "TCP");
1196 vppcom_send_connect_sock (session, session_index);
1197 VCL_SESSION_UNLOCK ();
1199 retval = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1200 vcm->cfg.session_timeout);
1202 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1203 vpp_handle = session->vpp_handle;
1204 VCL_SESSION_UNLOCK ();
1207 if (PREDICT_FALSE (retval))
1210 if (VPPCOM_DEBUG > 0)
1213 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1214 "failed! returning %d (%s)", getpid (), vpp_handle,
1215 session_index, rv, vppcom_retval_str (rv));
1217 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1218 "returning %d (%s)", getpid (),
1219 session_index, rv, vppcom_retval_str (rv));
1223 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1224 getpid (), vpp_handle, session_index);
1230 vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1233 return (e->event_type == FIFO_EVENT_APP_RX
1234 && e->fifo->client_session_index == sid);
1236 return (e->event_type == SESSION_IO_EVT_CT_TX);
1240 vcl_session_is_readable (vcl_session_t * s)
1242 return ((s->session_state & STATE_OPEN)
1243 || (s->session_state == STATE_LISTEN
1244 && s->session_type == VPPCOM_PROTO_UDP));
1248 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
1251 int n_read = 0, rv, is_nonblocking;
1252 vcl_session_t *s = 0;
1253 svm_fifo_t *rx_fifo;
1254 svm_msg_q_msg_t msg;
1261 VCL_SESSION_LOCK_AND_GET (session_index, &s);
1263 if (PREDICT_FALSE (s->is_vep))
1265 VCL_SESSION_UNLOCK ();
1266 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1267 "read from an epoll session!", getpid (), session_index);
1272 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1273 rx_fifo = s->rx_fifo;
1275 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1277 session_state_t state = s->session_state;
1278 VCL_SESSION_UNLOCK ();
1279 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1281 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1282 "state 0x%x (%s), returning %d (%s)",
1283 getpid (), s->vpp_handle, session_index, state,
1284 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1288 VCL_SESSION_UNLOCK ();
1289 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1290 is_full = svm_fifo_is_full (rx_fifo);
1292 if (svm_fifo_is_empty (rx_fifo))
1294 svm_fifo_unset_event (rx_fifo);
1300 svm_msg_q_lock (mq);
1303 if (svm_msg_q_is_empty (mq))
1304 svm_msg_q_wait (mq);
1305 svm_msg_q_sub_w_lock (mq, &msg);
1306 e = svm_msg_q_msg_data (mq, &msg);
1307 if (!vcl_is_rx_evt_for_session (e, session_index,
1310 vcl_handle_mq_ctrl_event (e);
1311 svm_msg_q_free_msg (mq, &msg);
1314 if (svm_fifo_is_empty (rx_fifo))
1316 svm_msg_q_free_msg (mq, &msg);
1319 svm_msg_q_free_msg (mq, &msg);
1320 svm_msg_q_unlock (mq);
1326 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 1, peek);
1328 n_read = app_recv_stream_raw (rx_fifo, buf, n, 1, peek);
1330 if (vcl_session_is_ct (s) && is_full)
1331 app_send_io_evt_to_vpp (s->vpp_evt_q, rx_fifo, SESSION_IO_EVT_CT_RX,
1334 if (VPPCOM_DEBUG > 2)
1337 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
1338 "from (%p)", getpid (), s->vpp_handle,
1339 session_index, n_read, rx_fifo);
1341 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
1342 "returning %d (%s)", getpid (), s->vpp_handle,
1343 session_index, rv, vppcom_retval_str (rv));
1352 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
1354 return (vppcom_session_read_internal (session_index, buf, n, 0));
1358 vppcom_session_peek (uint32_t session_index, void *buf, int n)
1360 return (vppcom_session_read_internal (session_index, buf, n, 1));
1364 vppcom_session_read_ready (vcl_session_t * session)
1366 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1367 if (PREDICT_FALSE (session->is_vep))
1369 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1370 "epoll session!", getpid (), vcl_session_index (session));
1371 return VPPCOM_EBADFD;
1374 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1376 session_state_t state = session->session_state;
1379 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1381 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1382 " state 0x%x (%s), returning %d (%s)", getpid (),
1383 session->vpp_handle, vcl_session_index (session), state,
1384 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1388 if (session->session_state & STATE_LISTEN)
1389 return clib_fifo_elts (session->accept_evts_fifo);
1391 return svm_fifo_max_dequeue (session->rx_fifo);
1395 vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1398 return (e->event_type == FIFO_EVENT_APP_TX
1399 && e->fifo->client_session_index == sid);
1401 return (e->event_type == SESSION_IO_EVT_CT_RX);
1405 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
1407 int rv, n_write, is_nonblocking;
1408 vcl_session_t *s = 0;
1409 svm_fifo_t *tx_fifo = 0;
1410 session_evt_type_t et;
1411 svm_msg_q_msg_t msg;
1417 VCL_SESSION_LOCK_AND_GET (session_index, &s);
1419 tx_fifo = s->tx_fifo;
1420 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1422 if (PREDICT_FALSE (s->is_vep))
1424 VCL_SESSION_UNLOCK ();
1425 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1426 "cannot write to an epoll session!",
1427 getpid (), s->vpp_handle, session_index);
1433 if (!(s->session_state & STATE_OPEN))
1435 session_state_t state = s->session_state;
1436 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1437 VCL_SESSION_UNLOCK ();
1438 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1440 getpid (), s->vpp_handle, session_index,
1441 state, vppcom_session_state_str (state));
1445 VCL_SESSION_UNLOCK ();
1447 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
1448 if (svm_fifo_is_full (tx_fifo))
1452 rv = VPPCOM_EWOULDBLOCK;
1455 svm_msg_q_lock (mq);
1458 if (!svm_fifo_is_full (tx_fifo))
1460 svm_msg_q_unlock (mq);
1463 if (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1465 svm_msg_q_sub_w_lock (mq, &msg);
1466 e = svm_msg_q_msg_data (mq, &msg);
1467 if (!vcl_is_tx_evt_for_session (e, session_index,
1470 vcl_handle_mq_ctrl_event (e);
1471 svm_msg_q_free_msg (mq, &msg);
1474 if (svm_fifo_is_full (tx_fifo))
1476 svm_msg_q_free_msg (mq, &msg);
1479 svm_msg_q_free_msg (mq, &msg);
1480 svm_msg_q_unlock (mq);
1485 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1486 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1488 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1489 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1491 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1494 ASSERT (n_write > 0);
1496 if (VPPCOM_DEBUG > 2)
1499 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1500 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
1501 session_index, tx_fifo);
1503 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1504 "wrote %d bytes tx-fifo: (%p)", getpid (),
1505 s->vpp_handle, session_index, n_write, tx_fifo);
1514 vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
1516 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1517 if (PREDICT_FALSE (session->is_vep))
1519 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1520 "cannot write to an epoll session!",
1521 getpid (), session->vpp_handle, session_index);
1522 return VPPCOM_EBADFD;
1525 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1527 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1528 "cannot write to a listen session!",
1529 getpid (), session->vpp_handle, session_index);
1530 return VPPCOM_EBADFD;
1533 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
1535 session_state_t state = session->session_state;
1538 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1539 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1540 "session is not open! state 0x%x (%s), "
1541 "returning %d (%s)", getpid (), session->vpp_handle,
1543 state, vppcom_session_state_str (state),
1544 rv, vppcom_retval_str (rv));
1548 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1549 getpid (), session->vpp_handle, session_index, session->tx_fifo,
1550 svm_fifo_max_enqueue (session->tx_fifo));
1552 return svm_fifo_max_enqueue (session->tx_fifo);
1556 vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
1557 unsigned long *read_map, unsigned long *write_map,
1558 unsigned long *except_map, double time_to_wait,
1561 session_disconnected_msg_t *disconnected_msg;
1562 session_accepted_msg_t *accepted_msg;
1563 vcl_session_msg_t *vcl_msg;
1564 vcl_session_t *session;
1565 svm_msg_q_msg_t msg;
1570 svm_msg_q_lock (mq);
1571 if (svm_msg_q_is_empty (mq))
1575 svm_msg_q_unlock (mq);
1581 svm_msg_q_unlock (mq);
1584 else if (time_to_wait < 0)
1586 svm_msg_q_wait (mq);
1590 if (svm_msg_q_timedwait (mq, time_to_wait))
1592 svm_msg_q_unlock (mq);
1597 svm_msg_q_unlock (mq);
1599 n_msgs = svm_msg_q_size (mq);
1600 for (i = 0; i < n_msgs; i++)
1602 if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0))
1604 clib_warning ("message queue returned");
1607 e = svm_msg_q_msg_data (mq, &msg);
1608 switch (e->event_type)
1610 case FIFO_EVENT_APP_RX:
1611 sid = e->fifo->client_session_index;
1612 session = vcl_session_get (sid);
1613 if (!session || svm_fifo_is_empty (session->rx_fifo))
1615 if (sid < n_bits && read_map)
1617 clib_bitmap_set_no_check (read_map, sid, 1);
1621 case FIFO_EVENT_APP_TX:
1622 sid = e->fifo->client_session_index;
1623 session = vcl_session_get (sid);
1624 if (!session || svm_fifo_is_full (session->tx_fifo))
1626 if (sid < n_bits && write_map)
1628 clib_bitmap_set_no_check (write_map, sid, 1);
1632 case SESSION_IO_EVT_CT_TX:
1633 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
1634 sid = vcl_session_index (session);
1635 if (!session || svm_fifo_is_empty (session->rx_fifo))
1637 if (sid < n_bits && read_map)
1639 clib_bitmap_set_no_check (read_map, sid, 1);
1644 case SESSION_IO_EVT_CT_RX:
1645 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
1646 sid = vcl_session_index (session);
1647 if (!session || svm_fifo_is_full (session->tx_fifo))
1649 if (sid < n_bits && write_map)
1651 clib_bitmap_set_no_check (write_map, sid, 1);
1655 case SESSION_CTRL_EVT_ACCEPTED:
1656 accepted_msg = (session_accepted_msg_t *) e->data;
1657 handle = accepted_msg->listener_handle;
1658 session = vppcom_session_table_lookup_listener (handle);
1661 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1662 "listener handle %llx", getpid (), handle);
1666 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1667 vcl_msg->accepted_msg = *accepted_msg;
1668 sid = session - vcm->sessions;
1669 if (sid < n_bits && read_map)
1671 clib_bitmap_set_no_check (read_map, sid, 1);
1675 case SESSION_CTRL_EVT_DISCONNECTED:
1676 disconnected_msg = (session_disconnected_msg_t *) e->data;
1677 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
1678 if (sid < n_bits && except_map)
1680 clib_bitmap_set_no_check (except_map, sid, 1);
1685 clib_warning ("unhandled: %u", e->event_type);
1688 svm_msg_q_free_msg (mq, &msg);
1695 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1696 unsigned long *write_map, unsigned long *except_map,
1697 double time_to_wait)
1699 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
1700 vcl_cut_through_registration_t *cr;
1701 double total_wait = 0, wait_slice;
1702 vcl_session_t *session = 0;
1705 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1707 if (n_bits && read_map)
1709 clib_bitmap_validate (vcm->rd_bitmap, minbits);
1710 clib_memcpy (vcm->rd_bitmap, read_map,
1711 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1712 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1714 if (n_bits && write_map)
1716 clib_bitmap_validate (vcm->wr_bitmap, minbits);
1717 clib_memcpy (vcm->wr_bitmap, write_map,
1718 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1719 memset (write_map, 0,
1720 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1722 if (n_bits && except_map)
1724 clib_bitmap_validate (vcm->ex_bitmap, minbits);
1725 clib_memcpy (vcm->ex_bitmap, except_map,
1726 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1727 memset (except_map, 0,
1728 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1738 clib_bitmap_foreach (sid, vcm->wr_bitmap, ({
1740 if (!(session = vcl_session_get (sid)))
1742 VCL_SESSION_UNLOCK();
1743 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1745 return VPPCOM_EBADFD;
1748 rv = svm_fifo_is_full (session->tx_fifo);
1749 VCL_SESSION_UNLOCK();
1752 clib_bitmap_set_no_check (write_map, sid, 1);
1760 clib_bitmap_foreach (sid, vcm->rd_bitmap, ({
1762 if (!(session = vcl_session_get (sid)))
1764 VCL_SESSION_UNLOCK();
1765 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1767 return VPPCOM_EBADFD;
1770 rv = vppcom_session_read_ready (session);
1771 VCL_SESSION_UNLOCK();
1774 clib_bitmap_set_no_check (read_map, sid, 1);
1781 wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
1785 pool_foreach (cr, vcm->cut_through_registrations, ({
1786 vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
1791 vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
1792 except_map, time_to_wait, &bits_set);
1793 total_wait += wait_slice;
1797 while (total_wait < time_to_wait);
1803 vep_verify_epoll_chain (u32 vep_idx)
1805 vcl_session_t *session;
1806 vppcom_epoll_t *vep;
1810 if (VPPCOM_DEBUG <= 1)
1813 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1814 rv = vppcom_session_at_index (vep_idx, &session);
1815 if (PREDICT_FALSE (rv))
1817 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1818 getpid (), vep_idx);
1821 if (PREDICT_FALSE (!session->is_vep))
1823 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1824 getpid (), vep_idx);
1827 vep = &session->vep;
1828 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
1831 " is_vep_session = %u\n"
1832 " next_sid = 0x%x (%u)\n"
1833 " wait_cont_idx = 0x%x (%u)\n"
1834 "}\n", getpid (), vep_idx,
1835 session->is_vep, session->is_vep_session,
1836 vep->next_sid, vep->next_sid,
1837 session->wait_cont_idx, session->wait_cont_idx);
1839 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
1841 rv = vppcom_session_at_index (sid, &session);
1842 if (PREDICT_FALSE (rv))
1844 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
1847 if (PREDICT_FALSE (session->is_vep))
1848 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1849 getpid (), vep_idx);
1850 else if (PREDICT_FALSE (!session->is_vep_session))
1852 clib_warning ("VCL<%d>: ERROR: session (%u) "
1853 "is not a vep session!", getpid (), sid);
1856 vep = &session->vep;
1857 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
1858 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
1859 "vep_idx (%u)!", getpid (),
1860 sid, session->vep.vep_idx, vep_idx);
1861 if (session->is_vep_session)
1863 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
1865 " next_sid = 0x%x (%u)\n"
1866 " prev_sid = 0x%x (%u)\n"
1867 " vep_idx = 0x%x (%u)\n"
1868 " ev.events = 0x%x\n"
1869 " ev.data.u64 = 0x%llx\n"
1873 vep->next_sid, vep->next_sid,
1874 vep->prev_sid, vep->prev_sid,
1875 vep->vep_idx, vep->vep_idx,
1876 vep->ev.events, vep->ev.data.u64, vep->et_mask);
1881 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
1882 getpid (), vep_idx);
1886 vppcom_epoll_create (void)
1888 vcl_session_t *vep_session;
1891 VCL_SESSION_LOCK ();
1892 pool_get (vcm->sessions, vep_session);
1893 memset (vep_session, 0, sizeof (*vep_session));
1894 vep_idx = vep_session - vcm->sessions;
1896 vep_session->is_vep = 1;
1897 vep_session->vep.vep_idx = ~0;
1898 vep_session->vep.next_sid = ~0;
1899 vep_session->vep.prev_sid = ~0;
1900 vep_session->wait_cont_idx = ~0;
1901 vep_session->vpp_handle = ~0;
1902 vep_session->poll_reg = 0;
1904 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
1905 VCL_SESSION_UNLOCK ();
1907 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
1908 getpid (), vep_idx, vep_idx);
1914 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
1915 struct epoll_event *event)
1917 vcl_session_t *vep_session;
1918 vcl_session_t *session;
1921 if (vep_idx == session_index)
1923 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
1924 getpid (), vep_idx);
1925 return VPPCOM_EINVAL;
1928 VCL_SESSION_LOCK ();
1929 rv = vppcom_session_at_index (vep_idx, &vep_session);
1930 if (PREDICT_FALSE (rv))
1932 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
1935 if (PREDICT_FALSE (!vep_session->is_vep))
1937 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1938 getpid (), vep_idx);
1943 ASSERT (vep_session->vep.vep_idx == ~0);
1944 ASSERT (vep_session->vep.prev_sid == ~0);
1946 rv = vppcom_session_at_index (session_index, &session);
1947 if (PREDICT_FALSE (rv))
1949 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
1950 getpid (), session_index);
1953 if (PREDICT_FALSE (session->is_vep))
1955 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
1963 if (PREDICT_FALSE (!event))
1965 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
1966 "epoll_event structure!", getpid ());
1970 if (vep_session->vep.next_sid != ~0)
1972 vcl_session_t *next_session;
1973 rv = vppcom_session_at_index (vep_session->vep.next_sid,
1975 if (PREDICT_FALSE (rv))
1977 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
1978 "vep.next_sid (%u) on vep_idx (%u)!",
1979 getpid (), vep_session->vep.next_sid, vep_idx);
1982 ASSERT (next_session->vep.prev_sid == vep_idx);
1983 next_session->vep.prev_sid = session_index;
1985 session->vep.next_sid = vep_session->vep.next_sid;
1986 session->vep.prev_sid = vep_idx;
1987 session->vep.vep_idx = vep_idx;
1988 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
1989 session->vep.ev = *event;
1990 session->is_vep = 0;
1991 session->is_vep_session = 1;
1992 vep_session->vep.next_sid = session_index;
1994 /* VCL Event Register handler */
1995 if (session->session_state & STATE_LISTEN)
1997 /* Register handler for connect_request event on listen_session_index */
1998 vce_event_key_t evk;
1999 evk.session_index = session_index;
2000 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2001 vep_session->poll_reg =
2002 vce_register_handler (&vcm->event_thread, &evk,
2003 vce_poll_wait_connect_request_handler_fn,
2004 0 /* No callback args */ );
2006 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
2007 "sid %u, events 0x%x, data 0x%llx!",
2008 getpid (), vep_idx, session_index,
2009 event->events, event->data.u64);
2010 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
2014 if (PREDICT_FALSE (!event))
2016 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
2017 "epoll_event structure!", getpid ());
2021 else if (PREDICT_FALSE (!session->is_vep_session))
2023 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2024 "not a vep session!", getpid (), session_index);
2028 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2030 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
2031 "vep_idx (%u) != vep_idx (%u)!",
2032 getpid (), session_index,
2033 session->vep.vep_idx, vep_idx);
2037 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2038 session->vep.ev = *event;
2039 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2040 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
2045 if (PREDICT_FALSE (!session->is_vep_session))
2047 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2048 "not a vep session!", getpid (), session_index);
2052 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2054 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
2055 "vep_idx (%u) != vep_idx (%u)!",
2056 getpid (), session_index,
2057 session->vep.vep_idx, vep_idx);
2062 /* VCL Event Un-register handler */
2063 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
2065 (void) vce_unregister_handler (&vcm->event_thread,
2066 vep_session->poll_reg);
2069 vep_session->wait_cont_idx =
2070 (vep_session->wait_cont_idx == session_index) ?
2071 session->vep.next_sid : vep_session->wait_cont_idx;
2073 if (session->vep.prev_sid == vep_idx)
2074 vep_session->vep.next_sid = session->vep.next_sid;
2077 vcl_session_t *prev_session;
2078 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2079 if (PREDICT_FALSE (rv))
2081 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2082 "vep.prev_sid (%u) on sid (%u)!",
2083 getpid (), session->vep.prev_sid, session_index);
2086 ASSERT (prev_session->vep.next_sid == session_index);
2087 prev_session->vep.next_sid = session->vep.next_sid;
2089 if (session->vep.next_sid != ~0)
2091 vcl_session_t *next_session;
2092 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2093 if (PREDICT_FALSE (rv))
2095 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
2096 "vep.next_sid (%u) on sid (%u)!",
2097 getpid (), session->vep.next_sid, session_index);
2100 ASSERT (next_session->vep.prev_sid == session_index);
2101 next_session->vep.prev_sid = session->vep.prev_sid;
2104 memset (&session->vep, 0, sizeof (session->vep));
2105 session->vep.next_sid = ~0;
2106 session->vep.prev_sid = ~0;
2107 session->vep.vep_idx = ~0;
2108 session->is_vep_session = 0;
2109 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2110 getpid (), vep_idx, session_index);
2111 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
2115 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
2119 vep_verify_epoll_chain (vep_idx);
2122 VCL_SESSION_UNLOCK ();
2127 vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
2128 u32 maxevents, double wait_for_time, u32 * num_ev)
2130 session_disconnected_msg_t *disconnected_msg;
2131 session_connected_msg_t *connected_msg;
2132 session_accepted_msg_t *accepted_msg;
2133 u32 sid = ~0, session_events, n_msgs;
2134 u64 session_evt_data = ~0, handle;
2135 vcl_session_msg_t *vcl_msg;
2136 vcl_session_t *session;
2137 svm_msg_q_msg_t msg;
2142 svm_msg_q_lock (mq);
2143 if (svm_msg_q_is_empty (mq))
2147 svm_msg_q_unlock (mq);
2150 else if (wait_for_time < 0)
2152 svm_msg_q_wait (mq);
2156 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
2158 svm_msg_q_unlock (mq);
2163 svm_msg_q_unlock (mq);
2165 n_msgs = svm_msg_q_size (mq);
2166 for (i = 0; i < n_msgs; i++)
2168 if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0))
2170 clib_warning ("message queue returned");
2173 e = svm_msg_q_msg_data (mq, &msg);
2175 switch (e->event_type)
2177 case FIFO_EVENT_APP_RX:
2178 sid = e->fifo->client_session_index;
2179 clib_spinlock_lock (&vcm->sessions_lockp);
2180 session = vcl_session_get (sid);
2181 session_events = session->vep.ev.events;
2182 if ((EPOLLIN & session->vep.ev.events)
2183 && !svm_fifo_is_empty (session->rx_fifo))
2186 events[*num_ev].events |= EPOLLIN;
2187 session_evt_data = session->vep.ev.data.u64;
2189 clib_spinlock_unlock (&vcm->sessions_lockp);
2191 case FIFO_EVENT_APP_TX:
2192 sid = e->fifo->client_session_index;
2193 clib_spinlock_lock (&vcm->sessions_lockp);
2194 session = vcl_session_get (sid);
2195 session_events = session->vep.ev.events;
2196 if ((EPOLLOUT & session_events)
2197 && !svm_fifo_is_full (session->tx_fifo))
2200 events[*num_ev].events |= EPOLLOUT;
2201 session_evt_data = session->vep.ev.data.u64;
2203 clib_spinlock_unlock (&vcm->sessions_lockp);
2205 case SESSION_IO_EVT_CT_TX:
2206 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
2207 sid = vcl_session_index (session);
2208 session_events = session->vep.ev.events;
2209 if ((EPOLLIN & session->vep.ev.events)
2210 && !svm_fifo_is_empty (session->rx_fifo))
2213 events[*num_ev].events |= EPOLLIN;
2214 session_evt_data = session->vep.ev.data.u64;
2217 case SESSION_IO_EVT_CT_RX:
2218 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
2219 sid = vcl_session_index (session);
2220 session_events = session->vep.ev.events;
2221 if ((EPOLLOUT & session_events)
2222 && !svm_fifo_is_full (session->tx_fifo))
2225 events[*num_ev].events |= EPOLLOUT;
2226 session_evt_data = session->vep.ev.data.u64;
2229 case SESSION_CTRL_EVT_ACCEPTED:
2230 accepted_msg = (session_accepted_msg_t *) e->data;
2231 handle = accepted_msg->listener_handle;
2232 session = vppcom_session_table_lookup_listener (handle);
2235 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2236 "listener handle %llx", getpid (), handle);
2240 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2241 vcl_msg->accepted_msg = *accepted_msg;
2242 session_events = session->vep.ev.events;
2243 if (!(EPOLLIN & session_events))
2247 events[*num_ev].events |= EPOLLIN;
2248 session_evt_data = session->vep.ev.data.u64;
2250 case SESSION_CTRL_EVT_CONNECTED:
2251 connected_msg = (session_connected_msg_t *) e->data;
2252 vcl_session_connected_handler (connected_msg);
2253 /* Generate EPOLLOUT because there's no connected event */
2254 sid = vcl_session_get_index_from_handle (connected_msg->handle);
2255 clib_spinlock_lock (&vcm->sessions_lockp);
2256 session = vcl_session_get (sid);
2257 session_events = session->vep.ev.events;
2258 if (EPOLLOUT & session_events)
2261 events[*num_ev].events |= EPOLLOUT;
2262 session_evt_data = session->vep.ev.data.u64;
2264 clib_spinlock_unlock (&vcm->sessions_lockp);
2266 case SESSION_CTRL_EVT_DISCONNECTED:
2267 disconnected_msg = (session_disconnected_msg_t *) e->data;
2268 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
2269 clib_spinlock_lock (&vcm->sessions_lockp);
2270 session = vcl_session_get (sid);
2272 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2273 session_evt_data = session->vep.ev.data.u64;
2274 session_events = session->vep.ev.events;
2275 clib_spinlock_unlock (&vcm->sessions_lockp);
2278 clib_warning ("unhandled: %u", e->event_type);
2279 svm_msg_q_free_msg (mq, &msg);
2283 svm_msg_q_free_msg (mq, &msg);
2287 events[*num_ev].data.u64 = session_evt_data;
2288 if (EPOLLONESHOT & session_events)
2290 clib_spinlock_lock (&vcm->sessions_lockp);
2291 session = vcl_session_get (sid);
2292 session->vep.ev.events = 0;
2293 clib_spinlock_unlock (&vcm->sessions_lockp);
2296 if (*num_ev == maxevents)
2304 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2305 int maxevents, double wait_for_time)
2307 vcl_cut_through_registration_t *cr;
2308 vcl_session_t *vep_session;
2309 double total_wait = 0, wait_slice;
2312 if (PREDICT_FALSE (maxevents <= 0))
2314 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
2315 getpid (), maxevents);
2316 return VPPCOM_EINVAL;
2319 clib_spinlock_lock (&vcm->sessions_lockp);
2320 vep_session = vcl_session_get (vep_idx);
2321 if (PREDICT_FALSE (!vep_session->is_vep))
2323 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2324 getpid (), vep_idx);
2325 clib_spinlock_unlock (&vcm->sessions_lockp);
2326 return VPPCOM_EINVAL;
2328 clib_spinlock_unlock (&vcm->sessions_lockp);
2330 memset (events, 0, sizeof (*events) * maxevents);
2331 wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
2336 pool_foreach (cr, vcm->cut_through_registrations, ({
2337 vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
2341 vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
2342 num_ev ? 0 : wait_slice, &num_ev);
2343 total_wait += wait_slice;
2347 while (total_wait < wait_for_time);
2353 vppcom_session_attr (uint32_t session_index, uint32_t op,
2354 void *buffer, uint32_t * buflen)
2356 vcl_session_t *session;
2358 u32 *flags = buffer;
2359 vppcom_endpt_t *ep = buffer;
2361 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2367 case VPPCOM_ATTR_GET_NREAD:
2368 rv = vppcom_session_read_ready (session);
2369 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2373 case VPPCOM_ATTR_GET_NWRITE:
2374 rv = vppcom_session_write_ready (session, session_index);
2375 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2376 getpid (), session_index, rv);
2379 case VPPCOM_ATTR_GET_FLAGS:
2380 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2382 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2383 VCL_SESS_ATTR_NONBLOCK));
2384 *buflen = sizeof (*flags);
2385 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2386 "is_nonblocking = %u", getpid (),
2387 session_index, *flags,
2388 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2394 case VPPCOM_ATTR_SET_FLAGS:
2395 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2397 if (*flags & O_NONBLOCK)
2398 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2400 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2402 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2403 " is_nonblocking = %u",
2404 getpid (), session_index, *flags,
2405 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2411 case VPPCOM_ATTR_GET_PEER_ADDR:
2412 if (PREDICT_TRUE (buffer && buflen &&
2413 (*buflen >= sizeof (*ep)) && ep->ip))
2415 ep->is_ip4 = session->transport.is_ip4;
2416 ep->port = session->transport.rmt_port;
2417 if (session->transport.is_ip4)
2418 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2419 sizeof (ip4_address_t));
2421 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2422 sizeof (ip6_address_t));
2423 *buflen = sizeof (*ep);
2424 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2425 "addr = %U, port %u", getpid (),
2426 session_index, ep->is_ip4, format_ip46_address,
2427 &session->transport.rmt_ip,
2428 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2429 clib_net_to_host_u16 (ep->port));
2435 case VPPCOM_ATTR_GET_LCL_ADDR:
2436 if (PREDICT_TRUE (buffer && buflen &&
2437 (*buflen >= sizeof (*ep)) && ep->ip))
2439 ep->is_ip4 = session->transport.is_ip4;
2440 ep->port = session->transport.lcl_port;
2441 if (session->transport.is_ip4)
2442 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2443 sizeof (ip4_address_t));
2445 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2446 sizeof (ip6_address_t));
2447 *buflen = sizeof (*ep);
2448 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2449 " addr = %U port %d", getpid (),
2450 session_index, ep->is_ip4, format_ip46_address,
2451 &session->transport.lcl_ip,
2452 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2453 clib_net_to_host_u16 (ep->port));
2459 case VPPCOM_ATTR_GET_LIBC_EPFD:
2460 rv = session->libc_epfd;
2461 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2465 case VPPCOM_ATTR_SET_LIBC_EPFD:
2466 if (PREDICT_TRUE (buffer && buflen &&
2467 (*buflen == sizeof (session->libc_epfd))))
2469 session->libc_epfd = *(int *) buffer;
2470 *buflen = sizeof (session->libc_epfd);
2472 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2473 "buflen %d", getpid (), session->libc_epfd, *buflen);
2479 case VPPCOM_ATTR_GET_PROTOCOL:
2480 if (buffer && buflen && (*buflen >= sizeof (int)))
2482 *(int *) buffer = session->session_type;
2483 *buflen = sizeof (int);
2485 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2486 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2493 case VPPCOM_ATTR_GET_LISTEN:
2494 if (buffer && buflen && (*buflen >= sizeof (int)))
2496 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2497 VCL_SESS_ATTR_LISTEN);
2498 *buflen = sizeof (int);
2500 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2501 getpid (), *(int *) buffer, *buflen);
2507 case VPPCOM_ATTR_GET_ERROR:
2508 if (buffer && buflen && (*buflen >= sizeof (int)))
2510 *(int *) buffer = 0;
2511 *buflen = sizeof (int);
2513 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2514 getpid (), *(int *) buffer, *buflen);
2520 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2521 if (buffer && buflen && (*buflen >= sizeof (u32)))
2525 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2526 session->tx_fifo ? session->tx_fifo->nitems :
2527 vcm->cfg.tx_fifo_size);
2528 *buflen = sizeof (u32);
2530 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2531 "buflen %d, #VPP-TBD#", getpid (),
2532 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2538 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2539 if (buffer && buflen && (*buflen == sizeof (u32)))
2542 session->sndbuf_size = *(u32 *) buffer;
2543 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2544 "buflen %d, #VPP-TBD#", getpid (),
2545 session->sndbuf_size, session->sndbuf_size, *buflen);
2551 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2552 if (buffer && buflen && (*buflen >= sizeof (u32)))
2556 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2557 session->rx_fifo ? session->rx_fifo->nitems :
2558 vcm->cfg.rx_fifo_size);
2559 *buflen = sizeof (u32);
2561 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2562 "buflen %d, #VPP-TBD#", getpid (),
2563 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2569 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2570 if (buffer && buflen && (*buflen == sizeof (u32)))
2573 session->rcvbuf_size = *(u32 *) buffer;
2574 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2575 "buflen %d, #VPP-TBD#", getpid (),
2576 session->sndbuf_size, session->sndbuf_size, *buflen);
2582 case VPPCOM_ATTR_GET_REUSEADDR:
2583 if (buffer && buflen && (*buflen >= sizeof (int)))
2586 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2587 VCL_SESS_ATTR_REUSEADDR);
2588 *buflen = sizeof (int);
2590 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2591 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2597 case VPPCOM_ATTR_SET_REUSEADDR:
2598 if (buffer && buflen && (*buflen == sizeof (int)) &&
2599 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2602 if (*(int *) buffer)
2603 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2605 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2607 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2608 " #VPP-TBD#", getpid (),
2609 VCL_SESS_ATTR_TEST (session->attr,
2610 VCL_SESS_ATTR_REUSEADDR), *buflen);
2616 case VPPCOM_ATTR_GET_REUSEPORT:
2617 if (buffer && buflen && (*buflen >= sizeof (int)))
2620 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2621 VCL_SESS_ATTR_REUSEPORT);
2622 *buflen = sizeof (int);
2624 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2625 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2631 case VPPCOM_ATTR_SET_REUSEPORT:
2632 if (buffer && buflen && (*buflen == sizeof (int)) &&
2633 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2636 if (*(int *) buffer)
2637 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2639 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2641 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2642 " #VPP-TBD#", getpid (),
2643 VCL_SESS_ATTR_TEST (session->attr,
2644 VCL_SESS_ATTR_REUSEPORT), *buflen);
2650 case VPPCOM_ATTR_GET_BROADCAST:
2651 if (buffer && buflen && (*buflen >= sizeof (int)))
2654 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2655 VCL_SESS_ATTR_BROADCAST);
2656 *buflen = sizeof (int);
2658 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2659 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2665 case VPPCOM_ATTR_SET_BROADCAST:
2666 if (buffer && buflen && (*buflen == sizeof (int)))
2669 if (*(int *) buffer)
2670 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2672 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2674 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2675 "#VPP-TBD#", getpid (),
2676 VCL_SESS_ATTR_TEST (session->attr,
2677 VCL_SESS_ATTR_BROADCAST), *buflen);
2683 case VPPCOM_ATTR_GET_V6ONLY:
2684 if (buffer && buflen && (*buflen >= sizeof (int)))
2687 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2688 VCL_SESS_ATTR_V6ONLY);
2689 *buflen = sizeof (int);
2691 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2692 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2698 case VPPCOM_ATTR_SET_V6ONLY:
2699 if (buffer && buflen && (*buflen == sizeof (int)))
2702 if (*(int *) buffer)
2703 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2705 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2707 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2708 "#VPP-TBD#", getpid (),
2709 VCL_SESS_ATTR_TEST (session->attr,
2710 VCL_SESS_ATTR_V6ONLY), *buflen);
2716 case VPPCOM_ATTR_GET_KEEPALIVE:
2717 if (buffer && buflen && (*buflen >= sizeof (int)))
2720 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2721 VCL_SESS_ATTR_KEEPALIVE);
2722 *buflen = sizeof (int);
2724 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2725 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2731 case VPPCOM_ATTR_SET_KEEPALIVE:
2732 if (buffer && buflen && (*buflen == sizeof (int)))
2735 if (*(int *) buffer)
2736 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2738 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2740 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2741 "#VPP-TBD#", getpid (),
2742 VCL_SESS_ATTR_TEST (session->attr,
2743 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2749 case VPPCOM_ATTR_GET_TCP_NODELAY:
2750 if (buffer && buflen && (*buflen >= sizeof (int)))
2753 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2754 VCL_SESS_ATTR_TCP_NODELAY);
2755 *buflen = sizeof (int);
2757 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2758 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2764 case VPPCOM_ATTR_SET_TCP_NODELAY:
2765 if (buffer && buflen && (*buflen == sizeof (int)))
2768 if (*(int *) buffer)
2769 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2771 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2773 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2774 "#VPP-TBD#", getpid (),
2775 VCL_SESS_ATTR_TEST (session->attr,
2776 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
2782 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2783 if (buffer && buflen && (*buflen >= sizeof (int)))
2786 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2787 VCL_SESS_ATTR_TCP_KEEPIDLE);
2788 *buflen = sizeof (int);
2790 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2791 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2797 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
2798 if (buffer && buflen && (*buflen == sizeof (int)))
2801 if (*(int *) buffer)
2802 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2804 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2806 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2807 "#VPP-TBD#", getpid (),
2808 VCL_SESS_ATTR_TEST (session->attr,
2809 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
2815 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2816 if (buffer && buflen && (*buflen >= sizeof (int)))
2819 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2820 VCL_SESS_ATTR_TCP_KEEPINTVL);
2821 *buflen = sizeof (int);
2823 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
2824 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2830 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
2831 if (buffer && buflen && (*buflen == sizeof (int)))
2834 if (*(int *) buffer)
2835 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2837 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2839 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
2840 "#VPP-TBD#", getpid (),
2841 VCL_SESS_ATTR_TEST (session->attr,
2842 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
2848 case VPPCOM_ATTR_GET_TCP_USER_MSS:
2849 if (buffer && buflen && (*buflen >= sizeof (u32)))
2852 *(u32 *) buffer = session->user_mss;
2853 *buflen = sizeof (int);
2855 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
2856 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2862 case VPPCOM_ATTR_SET_TCP_USER_MSS:
2863 if (buffer && buflen && (*buflen == sizeof (u32)))
2866 session->user_mss = *(u32 *) buffer;
2868 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
2869 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
2881 VCL_SESSION_UNLOCK ();
2886 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
2887 uint32_t buflen, int flags, vppcom_endpt_t * ep)
2890 vcl_session_t *session = 0;
2894 VCL_SESSION_LOCK ();
2895 rv = vppcom_session_at_index (session_index, &session);
2896 if (PREDICT_FALSE (rv))
2898 VCL_SESSION_UNLOCK ();
2899 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
2900 getpid (), session_index);
2901 VCL_SESSION_UNLOCK ();
2902 return VPPCOM_EBADFD;
2904 ep->is_ip4 = session->transport.is_ip4;
2905 ep->port = session->transport.rmt_port;
2906 VCL_SESSION_UNLOCK ();
2910 rv = vppcom_session_read (session_index, buffer, buflen);
2911 else if (flags & MSG_PEEK)
2912 rv = vppcom_session_peek (session_index, buffer, buflen);
2915 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
2917 return VPPCOM_EAFNOSUPPORT;
2920 if (session->transport.is_ip4)
2921 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2922 sizeof (ip4_address_t));
2924 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2925 sizeof (ip6_address_t));
2931 vppcom_session_sendto (uint32_t session_index, void *buffer,
2932 uint32_t buflen, int flags, vppcom_endpt_t * ep)
2935 return VPPCOM_EINVAL;
2940 return VPPCOM_EINVAL;
2945 // TBD check the flags and do the right thing
2946 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
2947 getpid (), flags, flags);
2950 return (vppcom_session_write (session_index, buffer, buflen));
2954 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
2956 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
2957 u32 i, keep_trying = 1;
2960 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
2961 getpid (), vp, n_sids, wait_for_time);
2964 return VPPCOM_EFAULT;
2968 vcl_session_t *session;
2970 for (i = 0; i < n_sids; i++)
2972 ASSERT (vp[i].revents);
2974 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
2975 VCL_SESSION_UNLOCK ();
2980 if (POLLIN & vp[i].events)
2982 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
2983 rv = vppcom_session_read_ready (session);
2984 VCL_SESSION_UNLOCK ();
2987 *vp[i].revents |= POLLIN;
2994 case VPPCOM_ECONNRESET:
2995 *vp[i].revents = POLLHUP;
2999 *vp[i].revents = POLLERR;
3006 if (POLLOUT & vp[i].events)
3008 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3009 rv = vppcom_session_write_ready (session, vp[i].sid);
3010 VCL_SESSION_UNLOCK ();
3013 *vp[i].revents |= POLLOUT;
3020 case VPPCOM_ECONNRESET:
3021 *vp[i].revents = POLLHUP;
3025 *vp[i].revents = POLLERR;
3032 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
3035 *vp[i].revents = POLLNVAL;
3039 if (wait_for_time != -1)
3040 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3042 while ((num_ev == 0) && keep_trying);
3044 if (VPPCOM_DEBUG > 3)
3046 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3047 for (i = 0; i < n_sids; i++)
3049 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3050 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3051 vp[i].events, *vp[i].revents);
3058 * fd.io coding-style-patch-verification: ON
3061 * eval: (c-set-style "gnu")