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>
25 vppcom_app_state_str (app_state_t state)
32 st = "STATE_APP_START";
35 case STATE_APP_CONN_VPP:
36 st = "STATE_APP_CONN_VPP";
39 case STATE_APP_ENABLED:
40 st = "STATE_APP_ENABLED";
43 case STATE_APP_ATTACHED:
44 st = "STATE_APP_ATTACHED";
48 st = "UNKNOWN_APP_STATE";
56 vppcom_session_state_str (session_state_t state)
78 case STATE_CLOSE_ON_EMPTY:
79 st = "STATE_CLOSE_ON_EMPTY";
82 case STATE_DISCONNECT:
83 st = "STATE_DISCONNECT";
99 format_ip4_address (u8 * s, va_list * args)
101 u8 *a = va_arg (*args, u8 *);
102 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
106 format_ip6_address (u8 * s, va_list * args)
108 ip6_address_t *a = va_arg (*args, ip6_address_t *);
109 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
111 i_max_n_zero = ARRAY_LEN (a->as_u16);
113 i_first_zero = i_max_n_zero;
115 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
117 u32 is_zero = a->as_u16[i] == 0;
118 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
124 if ((!is_zero && n_zeros > max_n_zeros)
125 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
127 i_max_n_zero = i_first_zero;
128 max_n_zeros = n_zeros;
129 i_first_zero = ARRAY_LEN (a->as_u16);
134 last_double_colon = 0;
135 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
137 if (i == i_max_n_zero && max_n_zeros > 1)
139 s = format (s, "::");
140 i += max_n_zeros - 1;
141 last_double_colon = 1;
145 s = format (s, "%s%x",
146 (last_double_colon || i == 0) ? "" : ":",
147 clib_net_to_host_u16 (a->as_u16[i]));
148 last_double_colon = 0;
155 /* Format an IP46 address. */
157 format_ip46_address (u8 * s, va_list * args)
159 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
160 ip46_type_t type = va_arg (*args, ip46_type_t);
166 is_ip4 = ip46_address_is_ip4 (ip46);
177 format (s, "%U", format_ip4_address, &ip46->ip4) :
178 format (s, "%U", format_ip6_address, &ip46->ip6);
182 * VPPCOM Utility Functions
186 vppcom_session_table_del_listener (u64 listener_handle)
188 listener_handle |= 1ULL << 63;
189 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
193 vppcom_wait_for_app_state_change (app_state_t app_state)
195 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
197 while (clib_time_now (&vcm->clib_time) < timeout)
199 if (vcm->app_state == app_state)
202 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
203 vppcom_app_state_str (app_state), app_state);
204 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
206 return VPPCOM_ETIMEDOUT;
210 vppcom_wait_for_session_state_change (u32 session_index,
211 session_state_t state,
214 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
215 vcl_session_t *volatile session;
221 rv = vppcom_session_at_index (session_index, &session);
222 if (PREDICT_FALSE (rv))
224 VCL_SESSION_UNLOCK ();
227 if (session->session_state & state)
229 VCL_SESSION_UNLOCK ();
232 if (session->session_state & STATE_FAILED)
234 VCL_SESSION_UNLOCK ();
235 return VPPCOM_ECONNREFUSED;
238 VCL_SESSION_UNLOCK ();
240 while (clib_time_now (&vcm->clib_time) < timeout);
242 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
243 vppcom_session_state_str (state));
244 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
246 return VPPCOM_ETIMEDOUT;
250 vppcom_app_session_enable (void)
254 if (vcm->app_state != STATE_APP_ENABLED)
256 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
257 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
258 if (PREDICT_FALSE (rv))
260 VDBG (0, "VCL<%d>: application session enable timed out! "
261 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
269 vppcom_app_attach (void)
273 vppcom_app_send_attach ();
274 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
275 if (PREDICT_FALSE (rv))
277 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
278 getpid (), rv, vppcom_retval_str (rv));
286 vppcom_session_unbind (u32 session_index)
288 vcl_session_t *session = 0;
292 VCL_SESSION_LOCK_AND_GET (session_index, &session);
294 vpp_handle = session->vpp_handle;
295 vppcom_session_table_del_listener (vpp_handle);
296 session->vpp_handle = ~0;
297 session->session_state = STATE_DISCONNECT;
299 VCL_SESSION_UNLOCK ();
301 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
302 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
303 vppcom_session_state_str (STATE_DISCONNECT));
304 vcl_evt (VCL_EVT_UNBIND, session);
305 vppcom_send_unbind_sock (vpp_handle);
312 vppcom_session_disconnect (u32 session_index)
315 vcl_session_t *session;
317 session_state_t state;
319 VCL_SESSION_LOCK_AND_GET (session_index, &session);
321 vpp_handle = session->vpp_handle;
322 state = session->session_state;
323 VCL_SESSION_UNLOCK ();
325 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
326 vpp_handle, session_index, state, vppcom_session_state_str (state));
328 if (PREDICT_FALSE (state & STATE_LISTEN))
330 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
331 "Cannot disconnect a listen socket!",
332 getpid (), vpp_handle, session_index);
337 /* The peer has already initiated the close,
338 * so send the disconnect session reply.
340 if (state & STATE_CLOSE_ON_EMPTY)
342 //XXX alagalah - Check and drain here?
343 vppcom_send_disconnect_session_reply (vpp_handle,
344 session_index, 0 /* rv */ );
345 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
346 "REPLY...", getpid (), vpp_handle, session_index);
349 /* Otherwise, send a disconnect session msg...
353 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
354 getpid (), vpp_handle, session_index);
356 vppcom_send_disconnect_session (vpp_handle, session_index);
364 * VPPCOM Public API functions
367 vppcom_app_create (char *app_name)
369 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
377 clib_spinlock_init (&vcm->session_fifo_lockp);
378 clib_fifo_validate (vcm->client_session_index_fifo,
379 vcm->cfg.listen_queue_size);
380 clib_spinlock_init (&vcm->sessions_lockp);
382 vppcom_cfg (&vcm->cfg);
384 vcm->main_cpu = os_get_thread_index ();
385 heap = clib_mem_get_per_cpu_heap ();
386 h = mheap_header (heap);
387 /* make the main heap thread-safe */
388 h->flags |= MHEAP_FLAG_THREAD_SAFE;
390 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
392 clib_time_init (&vcm->clib_time);
393 vppcom_init_error_string_table ();
394 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
395 20 /* timeout in secs */ );
398 if (vcm->my_client_index == ~0)
400 /* API hookup and connect to VPP */
401 vppcom_api_hookup ();
403 vcm->app_state = STATE_APP_START;
404 rv = vppcom_connect_to_vpp (app_name);
407 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
412 /* State event handling thread */
414 rv = vce_start_event_thread (&(vcm->event_thread), 20);
416 VDBG (0, "VCL<%d>: sending session enable", getpid ());
418 rv = vppcom_app_session_enable ();
421 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
422 "failed!", getpid ());
426 VDBG (0, "VCL<%d>: sending app attach", getpid ());
428 rv = vppcom_app_attach ();
431 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
436 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
437 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
444 vppcom_app_destroy (void)
447 f64 orig_app_timeout;
449 if (vcm->my_client_index == ~0)
452 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
453 getpid (), vcm->my_client_index, vcm->my_client_index);
454 vcl_evt (VCL_EVT_DETACH, vcm);
456 vppcom_app_send_detach ();
457 orig_app_timeout = vcm->cfg.app_timeout;
458 vcm->cfg.app_timeout = 2.0;
459 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
460 vcm->cfg.app_timeout = orig_app_timeout;
461 if (PREDICT_FALSE (rv))
462 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
463 getpid (), rv, vppcom_retval_str (rv));
466 vl_client_disconnect_from_vlib ();
467 vcm->my_client_index = ~0;
468 vcm->app_state = STATE_APP_START;
472 vppcom_session_create (u8 proto, u8 is_nonblocking)
474 vcl_session_t *session;
478 pool_get (vcm->sessions, session);
479 memset (session, 0, sizeof (*session));
480 session_index = session - vcm->sessions;
482 session->session_type = proto;
483 session->session_state = STATE_START;
484 session->vpp_handle = ~0;
487 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
489 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
491 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
492 is_nonblocking, session_index);
494 VCL_SESSION_UNLOCK ();
496 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
498 return (int) session_index;
502 vppcom_session_close (uint32_t session_index)
504 vcl_session_t *session = 0;
512 session_state_t state;
514 VCL_SESSION_LOCK_AND_GET (session_index, &session);
515 is_vep = session->is_vep;
516 is_vep_session = session->is_vep_session;
517 next_sid = session->vep.next_sid;
518 vep_idx = session->vep.vep_idx;
519 state = session->session_state;
520 vpp_handle = session->vpp_handle;
521 VCL_SESSION_UNLOCK ();
523 if (VPPCOM_DEBUG > 0)
526 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
527 "closing epoll session...",
528 getpid (), session_index, session_index);
530 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
531 "closing session...",
532 getpid (), vpp_handle, session_index);
537 while (next_sid != ~0)
539 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
540 if (PREDICT_FALSE (rv < 0))
541 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
542 "vep_idx %u failed! rv %d (%s)",
543 getpid (), vpp_handle, next_sid, vep_idx,
544 rv, vppcom_retval_str (rv));
546 VCL_SESSION_LOCK_AND_GET (session_index, &session);
547 next_sid = session->vep.next_sid;
548 VCL_SESSION_UNLOCK ();
555 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
557 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
558 "vep_idx %u failed! rv %d (%s)",
559 getpid (), vpp_handle, session_index,
560 vep_idx, rv, vppcom_retval_str (rv));
563 if (state & STATE_LISTEN)
565 rv = vppcom_session_unbind (session_index);
566 if (PREDICT_FALSE (rv < 0))
567 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
568 "failed! rv %d (%s)",
569 getpid (), vpp_handle, session_index,
570 rv, vppcom_retval_str (rv));
573 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
575 rv = vppcom_session_disconnect (session_index);
576 if (PREDICT_FALSE (rv < 0))
577 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
578 "session disconnect failed! rv %d (%s)",
579 getpid (), vpp_handle, session_index,
580 rv, vppcom_retval_str (rv));
584 VCL_SESSION_LOCK_AND_GET (session_index, &session);
585 vpp_handle = session->vpp_handle;
586 if (vpp_handle != ~0)
588 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
590 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
592 pool_put_index (vcm->sessions, session_index);
594 VCL_SESSION_UNLOCK ();
596 if (VPPCOM_DEBUG > 0)
599 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
600 getpid (), session_index, session_index);
602 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
603 getpid (), vpp_handle, session_index);
607 vcl_evt (VCL_EVT_CLOSE, session, rv);
613 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
615 vcl_session_t *session = 0;
619 return VPPCOM_EINVAL;
621 VCL_SESSION_LOCK_AND_GET (session_index, &session);
625 VCL_SESSION_UNLOCK ();
626 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
627 "bind to an epoll session!", getpid (), session_index);
632 session->transport.is_ip4 = ep->is_ip4;
633 session->transport.lcl_ip = to_ip46 (ep->is_ip4 ? IP46_TYPE_IP4 :
634 IP46_TYPE_IP6, ep->ip);
635 session->transport.lcl_port = ep->port;
637 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
638 "proto %s", getpid (), session_index,
639 session->transport.is_ip4 ? "IPv4" : "IPv6",
640 format_ip46_address, &session->transport.lcl_ip,
641 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
642 clib_net_to_host_u16 (session->transport.lcl_port),
643 session->session_type ? "UDP" : "TCP");
644 vcl_evt (VCL_EVT_BIND, session);
645 VCL_SESSION_UNLOCK ();
651 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
653 vcl_session_t *listen_session = 0;
654 u64 listen_vpp_handle;
657 if (q_len == 0 || q_len == ~0)
658 q_len = vcm->cfg.listen_queue_size;
660 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
662 if (listen_session->is_vep)
664 VCL_SESSION_UNLOCK ();
665 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
666 "epoll session!", getpid (), listen_session_index);
671 listen_vpp_handle = listen_session->vpp_handle;
672 if (listen_session->session_state & STATE_LISTEN)
674 VCL_SESSION_UNLOCK ();
675 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
676 getpid (), listen_vpp_handle, listen_session_index);
681 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
682 "request...", getpid (), listen_vpp_handle, listen_session_index);
684 vppcom_send_bind_sock (listen_session, listen_session_index);
685 VCL_SESSION_UNLOCK ();
687 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
688 vcm->cfg.session_timeout);
690 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
691 if (PREDICT_FALSE (retval))
693 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
694 "returning %d (%s)", getpid (), listen_session->vpp_handle,
695 listen_session_index, retval, vppcom_retval_str (retval));
696 VCL_SESSION_UNLOCK ();
701 VCL_ACCEPT_FIFO_LOCK ();
702 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
703 VCL_ACCEPT_FIFO_UNLOCK ();
705 VCL_SESSION_UNLOCK ();
712 validate_args_session_accept_ (vcl_session_t * listen_session)
714 u32 listen_session_index = listen_session - vcm->sessions;
716 /* Input validation - expects spinlock on sessions_lockp */
717 if (listen_session->is_vep)
719 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
720 "epoll session!", getpid (), listen_session_index);
721 return VPPCOM_EBADFD;
724 if (listen_session->session_state != STATE_LISTEN)
726 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
727 "not in listen state! state 0x%x (%s)", getpid (),
728 listen_session->vpp_handle, listen_session_index,
729 listen_session->session_state,
730 vppcom_session_state_str (listen_session->session_state));
731 return VPPCOM_EBADFD;
737 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
740 vcl_session_t *listen_session = 0;
741 vcl_session_t *client_session = 0;
742 u32 client_session_index = ~0;
744 u64 listen_vpp_handle;
745 vce_event_handler_reg_t *reg;
747 vce_event_connect_request_t *result;
750 int millisecond_timeout = 1;
751 int hours_timeout = 20 * 60 * 60;
753 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
754 listen_vpp_handle = listen_session->vpp_handle; // For debugging
756 rv = validate_args_session_accept_ (listen_session);
759 VCL_SESSION_UNLOCK ();
763 /* Using an aggressive timer of 1ms and a generous timer of
764 * 20 hours, we can implement a blocking and non-blocking listener
765 * as both event and time driven */
766 gettimeofday (&tv, NULL);
767 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
768 ts.tv_sec = tv.tv_sec;
770 /* Predict that the Listener is blocking more often than not */
771 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
772 VCL_SESS_ATTR_NONBLOCK)))
773 ts.tv_sec += hours_timeout;
775 VCL_SESSION_UNLOCK ();
777 /* Register handler for connect_request event on listen_session_index */
779 evk.session_index = listen_session_index;
780 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
781 reg = vce_register_handler (&vcm->event_thread, &evk,
782 vce_connect_request_handler_fn, 0);
784 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
785 pthread_mutex_lock (®->handler_lock);
788 VCL_EVENTS_UNLOCK ();
789 rv = pthread_cond_timedwait (®->handler_cond,
790 ®->handler_lock, &ts);
797 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
799 result = vce_get_event_data (ev, sizeof (*result));
800 client_session_index = result->accepted_session_index;
801 VCL_EVENTS_UNLOCK ();
803 /* Remove from the FIFO used to service epoll */
804 VCL_ACCEPT_FIFO_LOCK ();
805 if (clib_fifo_elts (vcm->client_session_index_fifo))
807 u32 tmp_client_session_index;
808 clib_fifo_sub1 (vcm->client_session_index_fifo,
809 tmp_client_session_index);
810 /* It wasn't ours... put it back ... */
811 if (tmp_client_session_index != client_session_index)
812 clib_fifo_add1 (vcm->client_session_index_fifo,
813 tmp_client_session_index);
815 VCL_ACCEPT_FIFO_UNLOCK ();
819 rv = vppcom_session_at_index (client_session_index, &client_session);
820 if (PREDICT_FALSE (rv))
822 rv = VPPCOM_ECONNABORTED;
823 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
824 "lookup failed! returning %d (%s)", getpid (),
825 listen_vpp_handle, listen_session_index,
826 client_session_index, rv, vppcom_retval_str (rv));
830 if (flags & O_NONBLOCK)
831 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
833 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
835 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
836 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
837 getpid (), listen_vpp_handle, listen_session_index,
838 client_session->vpp_handle, client_session_index,
839 flags, VCL_SESS_ATTR_TEST (client_session->attr,
840 VCL_SESS_ATTR_NONBLOCK));
844 ep->is_ip4 = client_session->transport.is_ip4;
845 ep->port = client_session->transport.rmt_port;
846 if (client_session->transport.is_ip4)
847 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
848 sizeof (ip4_address_t));
850 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
851 sizeof (ip6_address_t));
854 vppcom_send_accept_session_reply (client_session->vpp_handle,
855 client_session->client_context,
858 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx,"
859 " sid %u connection from peer %s address %U port %u to local %s address"
861 getpid (), listen_vpp_handle,
862 listen_session_index, client_session->vpp_handle,
863 client_session_index,
864 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
865 format_ip46_address, &client_session->transport.rmt_ip,
866 client_session->transport.is_ip4 ?
867 IP46_TYPE_IP4 : IP46_TYPE_IP6,
868 clib_net_to_host_u16 (client_session->transport.rmt_port),
869 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
870 format_ip46_address, &client_session->transport.lcl_ip,
871 client_session->transport.is_ip4 ?
872 IP46_TYPE_IP4 : IP46_TYPE_IP6,
873 clib_net_to_host_u16 (client_session->transport.lcl_port));
874 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
875 client_session_index);
876 VCL_SESSION_UNLOCK ();
878 rv = (int) client_session_index;
879 vce_clear_event (&vcm->event_thread, reg->ev_idx);
880 if (vcm->session_io_thread.io_sessions_lockp)
882 /* Throw this new accepted session index into the rx poll thread pool */
883 VCL_IO_SESSIONS_LOCK ();
884 u32 *active_session_index;
885 pool_get (vcm->session_io_thread.active_session_indexes,
886 active_session_index);
887 *active_session_index = client_session_index;
888 VCL_IO_SESSIONS_UNLOCK ();
891 vce_unregister_handler (&vcm->event_thread, reg);
892 pthread_mutex_unlock (®->handler_lock);
899 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
901 vcl_session_t *session = 0;
903 int rv, retval = VPPCOM_OK;
905 VCL_SESSION_LOCK_AND_GET (session_index, &session);
907 if (PREDICT_FALSE (session->is_vep))
909 VCL_SESSION_UNLOCK ();
910 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
911 "connect on an epoll session!", getpid (), session_index);
916 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
918 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
919 "connected to %s %U port %d proto %s, state 0x%x (%s)",
920 getpid (), session->vpp_handle, session_index,
921 session->transport.is_ip4 ? "IPv4" : "IPv6",
923 &session->transport.rmt_ip, session->transport.is_ip4 ?
924 IP46_TYPE_IP4 : IP46_TYPE_IP6,
925 clib_net_to_host_u16 (session->transport.rmt_port),
926 session->session_type ? "UDP" : "TCP", session->session_state,
927 vppcom_session_state_str (session->session_state));
929 VCL_SESSION_UNLOCK ();
933 session->transport.is_ip4 = server_ep->is_ip4;
934 if (session->transport.is_ip4)
935 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
936 sizeof (ip4_address_t));
938 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
939 sizeof (ip6_address_t));
940 session->transport.rmt_port = server_ep->port;
942 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
944 getpid (), session->vpp_handle, session_index,
945 session->transport.is_ip4 ? "IPv4" : "IPv6",
947 &session->transport.rmt_ip, session->transport.is_ip4 ?
948 IP46_TYPE_IP4 : IP46_TYPE_IP6,
949 clib_net_to_host_u16 (session->transport.rmt_port),
950 session->session_type ? "UDP" : "TCP");
952 vppcom_send_connect_sock (session, session_index);
953 VCL_SESSION_UNLOCK ();
956 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
957 vcm->cfg.session_timeout);
959 VCL_SESSION_LOCK_AND_GET (session_index, &session);
960 vpp_handle = session->vpp_handle;
961 VCL_SESSION_UNLOCK ();
964 if (PREDICT_FALSE (retval))
967 if (VPPCOM_DEBUG > 0)
970 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
971 "failed! returning %d (%s)", getpid (), vpp_handle,
972 session_index, rv, vppcom_retval_str (rv));
974 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
975 "returning %d (%s)", getpid (),
976 session_index, rv, vppcom_retval_str (rv));
980 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
981 getpid (), vpp_handle, session_index);
987 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
990 vcl_session_t *session = 0;
998 session_state_t state;
1002 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1004 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
1005 rx_fifo = session->rx_fifo;
1006 state = session->session_state;
1007 vpp_handle = session->vpp_handle;
1009 if (PREDICT_FALSE (session->is_vep))
1011 VCL_SESSION_UNLOCK ();
1012 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1013 "read from an epoll session!", getpid (), session_index);
1018 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
1020 VCL_SESSION_UNLOCK ();
1021 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1023 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1024 "state 0x%x (%s), returning %d (%s)",
1025 getpid (), vpp_handle, session_index, state,
1026 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1030 VCL_SESSION_UNLOCK ();
1035 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
1037 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
1039 while (!is_nonblocking && (n_read <= 0));
1043 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1045 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
1046 (EPOLLET | EPOLLIN));
1048 session->vep.et_mask |= EPOLLIN;
1050 if (state & STATE_CLOSE_ON_EMPTY)
1052 rv = VPPCOM_ECONNRESET;
1054 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1055 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1056 "returning %d (%s)",
1057 getpid (), session->vpp_handle, session_index,
1058 state, vppcom_session_state_str (state),
1060 vppcom_session_state_str (STATE_DISCONNECT), rv,
1061 vppcom_retval_str (rv));
1063 session->session_state = STATE_DISCONNECT;
1068 VCL_SESSION_UNLOCK ();
1073 if (VPPCOM_DEBUG > 2)
1076 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
1077 "from (%p)", getpid (), vpp_handle,
1078 session_index, n_read, rx_fifo);
1080 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
1081 "returning %d (%s)", getpid (), vpp_handle,
1082 session_index, rv, vppcom_retval_str (rv));
1089 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
1091 return (vppcom_session_read_internal (session_index, buf, n, 0));
1095 vppcom_session_peek (uint32_t session_index, void *buf, int n)
1097 return (vppcom_session_read_internal (session_index, buf, n, 1));
1101 vppcom_session_read_ready (vcl_session_t * session, u32 session_index)
1106 session_state_t state = session->session_state;
1107 u64 vpp_handle = session->vpp_handle;
1109 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1110 if (PREDICT_FALSE (session->is_vep))
1112 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
1113 "epoll session!", getpid (), session_index);
1118 if (session->session_state & STATE_LISTEN)
1120 VCL_ACCEPT_FIFO_LOCK ();
1121 ready = clib_fifo_elts (vcm->client_session_index_fifo);
1122 VCL_ACCEPT_FIFO_UNLOCK ();
1126 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
1128 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
1131 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1132 " state 0x%x (%s), returning %d (%s)",
1133 getpid (), vpp_handle, session_index,
1134 state, vppcom_session_state_str (state),
1135 rv, vppcom_retval_str (rv));
1139 ready = svm_fifo_max_dequeue (session->rx_fifo);
1145 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
1147 session->vep.et_mask |= EPOLLIN;
1149 if (state & STATE_CLOSE_ON_EMPTY)
1151 rv = VPPCOM_ECONNRESET;
1153 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1154 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1155 "returning %d (%s)",
1156 getpid (), session_index, vpp_handle,
1157 state, vppcom_session_state_str (state),
1159 vppcom_session_state_str (STATE_DISCONNECT), rv,
1160 vppcom_retval_str (rv));
1161 session->session_state = STATE_DISCONNECT;
1167 if (!svm_msg_q_is_empty (vcm->app_event_queue) &&
1168 !pthread_mutex_trylock (&vcm->app_event_queue->q->mutex))
1170 u32 i, n_to_dequeue = vcm->app_event_queue->q->cursize;
1171 svm_msg_q_msg_t msg;
1173 for (i = 0; i < n_to_dequeue; i++)
1175 svm_queue_sub_raw (vcm->app_event_queue->q, (u8 *) & msg);
1176 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1179 pthread_mutex_unlock (&vcm->app_event_queue->q->mutex);
1186 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
1188 vcl_session_t *session = 0;
1189 svm_fifo_t *tx_fifo = 0;
1191 session_state_t state;
1192 int rv, n_write, is_nonblocking;
1198 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1200 tx_fifo = session->tx_fifo;
1201 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
1202 vpp_handle = session->vpp_handle;
1203 state = session->session_state;
1205 if (PREDICT_FALSE (session->is_vep))
1207 VCL_SESSION_UNLOCK ();
1208 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1209 "cannot write to an epoll session!",
1210 getpid (), vpp_handle, session_index);
1216 if (!(session->session_state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
1219 ((session->session_state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
1222 VCL_SESSION_UNLOCK ();
1223 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1225 getpid (), vpp_handle, session_index,
1226 state, vppcom_session_state_str (state));
1230 VCL_SESSION_UNLOCK ();
1234 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
1236 while (!is_nonblocking && (n_write <= 0));
1238 /* If event wasn't set, add one
1240 * To reduce context switching, can check if an
1241 * event is already there for this event_key, but for now
1242 * this will suffice. */
1244 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
1246 /* Send TX event to vpp */
1247 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1248 mq = session->vpp_evt_q;
1250 app_send_io_evt_to_vpp (mq, tx_fifo, FIFO_EVENT_APP_TX, SVM_Q_WAIT);
1251 VCL_SESSION_UNLOCK ();
1252 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: added FIFO_EVENT_APP_TX "
1253 "to vpp_event_q %p, n_write %d", getpid (),
1254 vpp_handle, session_index, mq, n_write);
1259 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1261 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
1262 (EPOLLET | EPOLLOUT));
1264 session->vep.et_mask |= EPOLLOUT;
1266 if (session->session_state & STATE_CLOSE_ON_EMPTY)
1268 rv = VPPCOM_ECONNRESET;
1270 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1271 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1272 "returning %d (%s)",
1273 getpid (), session->vpp_handle, session_index,
1274 session->session_state,
1275 vppcom_session_state_str (session->session_state),
1277 vppcom_session_state_str (STATE_DISCONNECT), rv,
1278 vppcom_retval_str (rv));
1280 session->session_state = STATE_DISCONNECT;
1285 VCL_SESSION_UNLOCK ();
1290 if (VPPCOM_DEBUG > 2)
1293 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1294 "FIFO-FULL (%p)", getpid (), vpp_handle,
1295 session_index, tx_fifo);
1297 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1298 "wrote %d bytes tx-fifo: (%p)", getpid (),
1299 vpp_handle, session_index, n_write, tx_fifo);
1306 vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
1314 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1315 if (PREDICT_FALSE (session->is_vep))
1317 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1318 "cannot write to an epoll session!",
1319 getpid (), session->vpp_handle, session_index);
1324 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
1326 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1327 "cannot write to a listen session!",
1328 getpid (), session->vpp_handle, session_index);
1333 if (!(session->session_state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
1335 session_state_t state = session->session_state;
1337 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1339 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1340 "session is not open! state 0x%x (%s), "
1341 "returning %d (%s)", getpid (), session->vpp_handle,
1343 state, vppcom_session_state_str (state),
1344 rv, vppcom_retval_str (rv));
1348 ready = svm_fifo_max_enqueue (session->tx_fifo);
1350 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1351 getpid (), session->vpp_handle, session_index, session->tx_fifo,
1356 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
1357 (EPOLLET | EPOLLOUT));
1359 session->vep.et_mask |= EPOLLOUT;
1361 if (session->session_state & STATE_CLOSE_ON_EMPTY)
1363 rv = VPPCOM_ECONNRESET;
1365 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1366 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1367 "returning %d (%s)", getpid (),
1368 session->vpp_handle, session_index,
1369 session->session_state,
1370 vppcom_session_state_str (session->session_state),
1372 vppcom_session_state_str (STATE_DISCONNECT), rv,
1373 vppcom_retval_str (rv));
1374 session->session_state = STATE_DISCONNECT;
1384 vppcom_select (unsigned long n_bits, unsigned long *read_map,
1385 unsigned long *write_map, unsigned long *except_map,
1386 double time_to_wait)
1389 vcl_session_t *session = 0;
1390 int rv, bits_set = 0;
1391 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
1392 u32 minbits = clib_max (n_bits, BITS (uword));
1394 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1396 if (n_bits && read_map)
1398 clib_bitmap_validate (vcm->rd_bitmap, minbits);
1399 clib_memcpy (vcm->rd_bitmap, read_map,
1400 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1401 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1403 if (n_bits && write_map)
1405 clib_bitmap_validate (vcm->wr_bitmap, minbits);
1406 clib_memcpy (vcm->wr_bitmap, write_map,
1407 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1408 memset (write_map, 0,
1409 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1411 if (n_bits && except_map)
1413 clib_bitmap_validate (vcm->ex_bitmap, minbits);
1414 clib_memcpy (vcm->ex_bitmap, except_map,
1415 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1416 memset (except_map, 0,
1417 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1427 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
1430 rv = vppcom_session_at_index (session_index, &session);
1433 VCL_SESSION_UNLOCK();
1434 VDBG (1, "VCL<%d>: session %d specified in read_map is"
1435 " closed.", getpid (),
1437 bits_set = VPPCOM_EBADFD;
1440 if (session->session_state & STATE_LISTEN)
1442 vce_event_handler_reg_t *reg = 0;
1443 vce_event_key_t evk;
1445 /* Check if handler already registered for this
1447 * If not, register handler for connect_request event
1448 * on listen_session_index
1450 evk.session_index = session_index;
1451 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1452 reg = vce_get_event_handler (&vcm->event_thread, &evk);
1454 reg = vce_register_handler (&vcm->event_thread, &evk,
1455 vce_poll_wait_connect_request_handler_fn,
1456 0 /* No callback args */);
1457 rv = vppcom_session_read_ready (session, session_index);
1460 vce_unregister_handler (&vcm->event_thread, reg);
1464 rv = vppcom_session_read_ready (session, session_index);
1465 VCL_SESSION_UNLOCK();
1466 if (except_map && vcm->ex_bitmap &&
1467 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
1470 clib_bitmap_set_no_check (except_map, session_index, 1);
1475 clib_bitmap_set_no_check (read_map, session_index, 1);
1483 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
1486 rv = vppcom_session_at_index (session_index, &session);
1489 VCL_SESSION_UNLOCK();
1490 VDBG (0, "VCL<%d>: session %d specified in "
1491 "write_map is closed.", getpid (),
1493 bits_set = VPPCOM_EBADFD;
1497 rv = vppcom_session_write_ready (session, session_index);
1498 VCL_SESSION_UNLOCK();
1499 if (write_map && (rv > 0))
1501 clib_bitmap_set_no_check (write_map, session_index, 1);
1509 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
1512 rv = vppcom_session_at_index (session_index, &session);
1515 VCL_SESSION_UNLOCK();
1516 VDBG (1, "VCL<%d>: session %d specified in except_map "
1517 "is closed.", getpid (),
1519 bits_set = VPPCOM_EBADFD;
1523 rv = vppcom_session_read_ready (session, session_index);
1524 VCL_SESSION_UNLOCK();
1527 clib_bitmap_set_no_check (except_map, session_index, 1);
1535 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
1542 vep_verify_epoll_chain (u32 vep_idx)
1544 vcl_session_t *session;
1545 vppcom_epoll_t *vep;
1549 if (VPPCOM_DEBUG <= 1)
1552 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1553 rv = vppcom_session_at_index (vep_idx, &session);
1554 if (PREDICT_FALSE (rv))
1556 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1557 getpid (), vep_idx);
1560 if (PREDICT_FALSE (!session->is_vep))
1562 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1563 getpid (), vep_idx);
1566 vep = &session->vep;
1567 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
1570 " is_vep_session = %u\n"
1571 " next_sid = 0x%x (%u)\n"
1572 " wait_cont_idx = 0x%x (%u)\n"
1573 "}\n", getpid (), vep_idx,
1574 session->is_vep, session->is_vep_session,
1575 vep->next_sid, vep->next_sid,
1576 session->wait_cont_idx, session->wait_cont_idx);
1578 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
1580 rv = vppcom_session_at_index (sid, &session);
1581 if (PREDICT_FALSE (rv))
1583 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
1586 if (PREDICT_FALSE (session->is_vep))
1587 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1588 getpid (), vep_idx);
1589 else if (PREDICT_FALSE (!session->is_vep_session))
1591 clib_warning ("VCL<%d>: ERROR: session (%u) "
1592 "is not a vep session!", getpid (), sid);
1595 vep = &session->vep;
1596 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
1597 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
1598 "vep_idx (%u)!", getpid (),
1599 sid, session->vep.vep_idx, vep_idx);
1600 if (session->is_vep_session)
1602 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
1604 " next_sid = 0x%x (%u)\n"
1605 " prev_sid = 0x%x (%u)\n"
1606 " vep_idx = 0x%x (%u)\n"
1607 " ev.events = 0x%x\n"
1608 " ev.data.u64 = 0x%llx\n"
1612 vep->next_sid, vep->next_sid,
1613 vep->prev_sid, vep->prev_sid,
1614 vep->vep_idx, vep->vep_idx,
1615 vep->ev.events, vep->ev.data.u64, vep->et_mask);
1620 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
1621 getpid (), vep_idx);
1625 vppcom_epoll_create (void)
1627 vcl_session_t *vep_session;
1630 VCL_SESSION_LOCK ();
1631 pool_get (vcm->sessions, vep_session);
1632 memset (vep_session, 0, sizeof (*vep_session));
1633 vep_idx = vep_session - vcm->sessions;
1635 vep_session->is_vep = 1;
1636 vep_session->vep.vep_idx = ~0;
1637 vep_session->vep.next_sid = ~0;
1638 vep_session->vep.prev_sid = ~0;
1639 vep_session->wait_cont_idx = ~0;
1640 vep_session->vpp_handle = ~0;
1641 vep_session->poll_reg = 0;
1643 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
1644 VCL_SESSION_UNLOCK ();
1646 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
1647 getpid (), vep_idx, vep_idx);
1653 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
1654 struct epoll_event *event)
1656 vcl_session_t *vep_session;
1657 vcl_session_t *session;
1660 if (vep_idx == session_index)
1662 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
1663 getpid (), vep_idx);
1664 return VPPCOM_EINVAL;
1667 VCL_SESSION_LOCK ();
1668 rv = vppcom_session_at_index (vep_idx, &vep_session);
1669 if (PREDICT_FALSE (rv))
1671 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
1674 if (PREDICT_FALSE (!vep_session->is_vep))
1676 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1677 getpid (), vep_idx);
1682 ASSERT (vep_session->vep.vep_idx == ~0);
1683 ASSERT (vep_session->vep.prev_sid == ~0);
1685 rv = vppcom_session_at_index (session_index, &session);
1686 if (PREDICT_FALSE (rv))
1688 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
1689 getpid (), session_index);
1692 if (PREDICT_FALSE (session->is_vep))
1694 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
1702 if (PREDICT_FALSE (!event))
1704 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
1705 "epoll_event structure!", getpid ());
1709 if (vep_session->vep.next_sid != ~0)
1711 vcl_session_t *next_session;
1712 rv = vppcom_session_at_index (vep_session->vep.next_sid,
1714 if (PREDICT_FALSE (rv))
1716 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
1717 "vep.next_sid (%u) on vep_idx (%u)!",
1718 getpid (), vep_session->vep.next_sid, vep_idx);
1721 ASSERT (next_session->vep.prev_sid == vep_idx);
1722 next_session->vep.prev_sid = session_index;
1724 session->vep.next_sid = vep_session->vep.next_sid;
1725 session->vep.prev_sid = vep_idx;
1726 session->vep.vep_idx = vep_idx;
1727 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
1728 session->vep.ev = *event;
1729 session->is_vep = 0;
1730 session->is_vep_session = 1;
1731 vep_session->vep.next_sid = session_index;
1733 /* VCL Event Register handler */
1734 if (session->session_state & STATE_LISTEN)
1736 /* Register handler for connect_request event on listen_session_index */
1737 vce_event_key_t evk;
1738 evk.session_index = session_index;
1739 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1740 vep_session->poll_reg =
1741 vce_register_handler (&vcm->event_thread, &evk,
1742 vce_poll_wait_connect_request_handler_fn,
1743 0 /* No callback args */ );
1745 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
1746 "sid %u, events 0x%x, data 0x%llx!",
1747 getpid (), vep_idx, session_index,
1748 event->events, event->data.u64);
1749 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
1753 if (PREDICT_FALSE (!event))
1755 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
1756 "epoll_event structure!", getpid ());
1760 else if (PREDICT_FALSE (!session->is_vep_session))
1762 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
1763 "not a vep session!", getpid (), session_index);
1767 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
1769 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
1770 "vep_idx (%u) != vep_idx (%u)!",
1771 getpid (), session_index,
1772 session->vep.vep_idx, vep_idx);
1776 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
1777 session->vep.ev = *event;
1778 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
1779 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
1784 if (PREDICT_FALSE (!session->is_vep_session))
1786 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
1787 "not a vep session!", getpid (), session_index);
1791 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
1793 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
1794 "vep_idx (%u) != vep_idx (%u)!",
1795 getpid (), session_index,
1796 session->vep.vep_idx, vep_idx);
1801 /* VCL Event Un-register handler */
1802 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
1804 (void) vce_unregister_handler (&vcm->event_thread,
1805 vep_session->poll_reg);
1808 vep_session->wait_cont_idx =
1809 (vep_session->wait_cont_idx == session_index) ?
1810 session->vep.next_sid : vep_session->wait_cont_idx;
1812 if (session->vep.prev_sid == vep_idx)
1813 vep_session->vep.next_sid = session->vep.next_sid;
1816 vcl_session_t *prev_session;
1817 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
1818 if (PREDICT_FALSE (rv))
1820 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
1821 "vep.prev_sid (%u) on sid (%u)!",
1822 getpid (), session->vep.prev_sid, session_index);
1825 ASSERT (prev_session->vep.next_sid == session_index);
1826 prev_session->vep.next_sid = session->vep.next_sid;
1828 if (session->vep.next_sid != ~0)
1830 vcl_session_t *next_session;
1831 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
1832 if (PREDICT_FALSE (rv))
1834 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
1835 "vep.next_sid (%u) on sid (%u)!",
1836 getpid (), session->vep.next_sid, session_index);
1839 ASSERT (next_session->vep.prev_sid == session_index);
1840 next_session->vep.prev_sid = session->vep.prev_sid;
1843 memset (&session->vep, 0, sizeof (session->vep));
1844 session->vep.next_sid = ~0;
1845 session->vep.prev_sid = ~0;
1846 session->vep.vep_idx = ~0;
1847 session->is_vep_session = 0;
1848 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
1849 getpid (), vep_idx, session_index);
1850 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
1854 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
1858 vep_verify_epoll_chain (vep_idx);
1861 VCL_SESSION_UNLOCK ();
1866 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
1867 int maxevents, double wait_for_time)
1869 vcl_session_t *vep_session;
1871 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
1872 u32 keep_trying = 1;
1874 u32 vep_next_sid, wait_cont_idx;
1877 if (PREDICT_FALSE (maxevents <= 0))
1879 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
1880 getpid (), maxevents);
1881 return VPPCOM_EINVAL;
1883 memset (events, 0, sizeof (*events) * maxevents);
1885 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
1886 vep_next_sid = vep_session->vep.next_sid;
1887 is_vep = vep_session->is_vep;
1888 wait_cont_idx = vep_session->wait_cont_idx;
1889 VCL_SESSION_UNLOCK ();
1891 if (PREDICT_FALSE (!is_vep))
1893 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1894 getpid (), vep_idx);
1898 if (PREDICT_FALSE (vep_next_sid == ~0))
1900 VDBG (1, "VCL<%d>: WARNING: vep_idx (%u) is empty!",
1901 getpid (), vep_idx);
1909 vcl_session_t *session;
1911 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
1912 sid != ~0; sid = next_sid)
1914 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
1915 u8 add_event, is_vep_session;
1917 u64 session_ev_data;
1919 VCL_SESSION_LOCK_AND_GET (sid, &session);
1920 next_sid = session->vep.next_sid;
1921 session_events = session->vep.ev.events;
1922 et_mask = session->vep.et_mask;
1923 is_vep = session->is_vep;
1924 is_vep_session = session->is_vep_session;
1925 session_vep_idx = session->vep.vep_idx;
1926 session_ev_data = session->vep.ev.data.u64;
1928 VCL_SESSION_UNLOCK ();
1930 if (PREDICT_FALSE (is_vep))
1932 VDBG (0, "VCL<%d>: ERROR: sid (%u) is a vep!",
1933 getpid (), vep_idx);
1937 if (PREDICT_FALSE (!is_vep_session))
1939 VDBG (0, "VCL<%d>: ERROR: session (%u) is not "
1940 "a vep session!", getpid (), sid);
1944 if (PREDICT_FALSE (session_vep_idx != vep_idx))
1946 clib_warning ("VCL<%d>: ERROR: session (%u) "
1947 "vep_idx (%u) != vep_idx (%u)!",
1948 getpid (), sid, session_vep_idx, vep_idx);
1953 add_event = clear_et_mask = 0;
1955 if (EPOLLIN & session_events)
1957 VCL_SESSION_LOCK_AND_GET (sid, &session);
1958 ready = vppcom_session_read_ready (session, sid);
1959 VCL_SESSION_UNLOCK ();
1960 if ((ready > 0) && (EPOLLIN & et_mask))
1963 events[num_ev].events |= EPOLLIN;
1964 if (((EPOLLET | EPOLLIN) & session_events) ==
1965 (EPOLLET | EPOLLIN))
1966 clear_et_mask |= EPOLLIN;
1973 case VPPCOM_ECONNRESET:
1974 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
1978 events[num_ev].events |= EPOLLERR;
1984 if (EPOLLOUT & session_events)
1986 VCL_SESSION_LOCK_AND_GET (sid, &session);
1987 ready = vppcom_session_write_ready (session, sid);
1988 VCL_SESSION_UNLOCK ();
1989 if ((ready > 0) && (EPOLLOUT & et_mask))
1992 events[num_ev].events |= EPOLLOUT;
1993 if (((EPOLLET | EPOLLOUT) & session_events) ==
1994 (EPOLLET | EPOLLOUT))
1995 clear_et_mask |= EPOLLOUT;
2002 case VPPCOM_ECONNRESET:
2003 events[num_ev].events |= EPOLLHUP;
2007 events[num_ev].events |= EPOLLERR;
2015 events[num_ev].data.u64 = session_ev_data;
2016 if (EPOLLONESHOT & session_events)
2018 VCL_SESSION_LOCK_AND_GET (sid, &session);
2019 session->vep.ev.events = 0;
2020 VCL_SESSION_UNLOCK ();
2023 if (num_ev == maxevents)
2025 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
2026 vep_session->wait_cont_idx = next_sid;
2027 VCL_SESSION_UNLOCK ();
2031 if (wait_cont_idx != ~0)
2034 next_sid = vep_next_sid;
2035 else if (next_sid == wait_cont_idx)
2039 if (wait_for_time != -1)
2040 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
2042 while ((num_ev == 0) && keep_trying);
2044 if (wait_cont_idx != ~0)
2046 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
2047 vep_session->wait_cont_idx = ~0;
2048 VCL_SESSION_UNLOCK ();
2051 return (rv != VPPCOM_OK) ? rv : num_ev;
2055 vppcom_session_attr (uint32_t session_index, uint32_t op,
2056 void *buffer, uint32_t * buflen)
2058 vcl_session_t *session;
2060 u32 *flags = buffer;
2061 vppcom_endpt_t *ep = buffer;
2063 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2069 case VPPCOM_ATTR_GET_NREAD:
2070 rv = vppcom_session_read_ready (session, session_index);
2071 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2075 case VPPCOM_ATTR_GET_NWRITE:
2076 rv = vppcom_session_write_ready (session, session_index);
2077 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2078 getpid (), session_index, rv);
2081 case VPPCOM_ATTR_GET_FLAGS:
2082 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
2084 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2085 VCL_SESS_ATTR_NONBLOCK));
2086 *buflen = sizeof (*flags);
2087 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2088 "is_nonblocking = %u", getpid (),
2089 session_index, *flags,
2090 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2096 case VPPCOM_ATTR_SET_FLAGS:
2097 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
2099 if (*flags & O_NONBLOCK)
2100 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2102 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2104 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2105 " is_nonblocking = %u",
2106 getpid (), session_index, *flags,
2107 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
2113 case VPPCOM_ATTR_GET_PEER_ADDR:
2114 if (PREDICT_TRUE (buffer && buflen &&
2115 (*buflen >= sizeof (*ep)) && ep->ip))
2117 ep->is_ip4 = session->transport.is_ip4;
2118 ep->port = session->transport.rmt_port;
2119 if (session->transport.is_ip4)
2120 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2121 sizeof (ip4_address_t));
2123 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2124 sizeof (ip6_address_t));
2125 *buflen = sizeof (*ep);
2126 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2127 "addr = %U, port %u", getpid (),
2128 session_index, ep->is_ip4, format_ip46_address,
2129 &session->transport.rmt_ip,
2130 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2131 clib_net_to_host_u16 (ep->port));
2137 case VPPCOM_ATTR_GET_LCL_ADDR:
2138 if (PREDICT_TRUE (buffer && buflen &&
2139 (*buflen >= sizeof (*ep)) && ep->ip))
2141 ep->is_ip4 = session->transport.is_ip4;
2142 ep->port = session->transport.lcl_port;
2143 if (session->transport.is_ip4)
2144 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
2145 sizeof (ip4_address_t));
2147 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
2148 sizeof (ip6_address_t));
2149 *buflen = sizeof (*ep);
2150 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2151 " addr = %U port %d", getpid (),
2152 session_index, ep->is_ip4, format_ip46_address,
2153 &session->transport.lcl_ip,
2154 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2155 clib_net_to_host_u16 (ep->port));
2161 case VPPCOM_ATTR_GET_LIBC_EPFD:
2162 rv = session->libc_epfd;
2163 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2167 case VPPCOM_ATTR_SET_LIBC_EPFD:
2168 if (PREDICT_TRUE (buffer && buflen &&
2169 (*buflen == sizeof (session->libc_epfd))))
2171 session->libc_epfd = *(int *) buffer;
2172 *buflen = sizeof (session->libc_epfd);
2174 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2175 "buflen %d", getpid (), session->libc_epfd, *buflen);
2181 case VPPCOM_ATTR_GET_PROTOCOL:
2182 if (buffer && buflen && (*buflen >= sizeof (int)))
2184 *(int *) buffer = session->session_type;
2185 *buflen = sizeof (int);
2187 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2188 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2195 case VPPCOM_ATTR_GET_LISTEN:
2196 if (buffer && buflen && (*buflen >= sizeof (int)))
2198 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2199 VCL_SESS_ATTR_LISTEN);
2200 *buflen = sizeof (int);
2202 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2203 getpid (), *(int *) buffer, *buflen);
2209 case VPPCOM_ATTR_GET_ERROR:
2210 if (buffer && buflen && (*buflen >= sizeof (int)))
2212 *(int *) buffer = 0;
2213 *buflen = sizeof (int);
2215 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2216 getpid (), *(int *) buffer, *buflen);
2222 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2223 if (buffer && buflen && (*buflen >= sizeof (u32)))
2227 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
2228 session->tx_fifo ? session->tx_fifo->nitems :
2229 vcm->cfg.tx_fifo_size);
2230 *buflen = sizeof (u32);
2232 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2233 "buflen %d, #VPP-TBD#", getpid (),
2234 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2240 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2241 if (buffer && buflen && (*buflen == sizeof (u32)))
2244 session->sndbuf_size = *(u32 *) buffer;
2245 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2246 "buflen %d, #VPP-TBD#", getpid (),
2247 session->sndbuf_size, session->sndbuf_size, *buflen);
2253 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2254 if (buffer && buflen && (*buflen >= sizeof (u32)))
2258 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
2259 session->rx_fifo ? session->rx_fifo->nitems :
2260 vcm->cfg.rx_fifo_size);
2261 *buflen = sizeof (u32);
2263 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2264 "buflen %d, #VPP-TBD#", getpid (),
2265 *(size_t *) buffer, *(size_t *) buffer, *buflen);
2271 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2272 if (buffer && buflen && (*buflen == sizeof (u32)))
2275 session->rcvbuf_size = *(u32 *) buffer;
2276 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2277 "buflen %d, #VPP-TBD#", getpid (),
2278 session->sndbuf_size, session->sndbuf_size, *buflen);
2284 case VPPCOM_ATTR_GET_REUSEADDR:
2285 if (buffer && buflen && (*buflen >= sizeof (int)))
2288 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2289 VCL_SESS_ATTR_REUSEADDR);
2290 *buflen = sizeof (int);
2292 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2293 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2299 case VPPCOM_ATTR_SET_REUSEADDR:
2300 if (buffer && buflen && (*buflen == sizeof (int)) &&
2301 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2304 if (*(int *) buffer)
2305 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2307 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2309 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2310 " #VPP-TBD#", getpid (),
2311 VCL_SESS_ATTR_TEST (session->attr,
2312 VCL_SESS_ATTR_REUSEADDR), *buflen);
2318 case VPPCOM_ATTR_GET_REUSEPORT:
2319 if (buffer && buflen && (*buflen >= sizeof (int)))
2322 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2323 VCL_SESS_ATTR_REUSEPORT);
2324 *buflen = sizeof (int);
2326 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2327 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2333 case VPPCOM_ATTR_SET_REUSEPORT:
2334 if (buffer && buflen && (*buflen == sizeof (int)) &&
2335 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2338 if (*(int *) buffer)
2339 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2341 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2343 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2344 " #VPP-TBD#", getpid (),
2345 VCL_SESS_ATTR_TEST (session->attr,
2346 VCL_SESS_ATTR_REUSEPORT), *buflen);
2352 case VPPCOM_ATTR_GET_BROADCAST:
2353 if (buffer && buflen && (*buflen >= sizeof (int)))
2356 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2357 VCL_SESS_ATTR_BROADCAST);
2358 *buflen = sizeof (int);
2360 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2361 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2367 case VPPCOM_ATTR_SET_BROADCAST:
2368 if (buffer && buflen && (*buflen == sizeof (int)))
2371 if (*(int *) buffer)
2372 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2374 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2376 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2377 "#VPP-TBD#", getpid (),
2378 VCL_SESS_ATTR_TEST (session->attr,
2379 VCL_SESS_ATTR_BROADCAST), *buflen);
2385 case VPPCOM_ATTR_GET_V6ONLY:
2386 if (buffer && buflen && (*buflen >= sizeof (int)))
2389 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2390 VCL_SESS_ATTR_V6ONLY);
2391 *buflen = sizeof (int);
2393 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2394 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2400 case VPPCOM_ATTR_SET_V6ONLY:
2401 if (buffer && buflen && (*buflen == sizeof (int)))
2404 if (*(int *) buffer)
2405 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2407 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2409 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2410 "#VPP-TBD#", getpid (),
2411 VCL_SESS_ATTR_TEST (session->attr,
2412 VCL_SESS_ATTR_V6ONLY), *buflen);
2418 case VPPCOM_ATTR_GET_KEEPALIVE:
2419 if (buffer && buflen && (*buflen >= sizeof (int)))
2422 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2423 VCL_SESS_ATTR_KEEPALIVE);
2424 *buflen = sizeof (int);
2426 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2427 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2433 case VPPCOM_ATTR_SET_KEEPALIVE:
2434 if (buffer && buflen && (*buflen == sizeof (int)))
2437 if (*(int *) buffer)
2438 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2440 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2442 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2443 "#VPP-TBD#", getpid (),
2444 VCL_SESS_ATTR_TEST (session->attr,
2445 VCL_SESS_ATTR_KEEPALIVE), *buflen);
2451 case VPPCOM_ATTR_GET_TCP_NODELAY:
2452 if (buffer && buflen && (*buflen >= sizeof (int)))
2455 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2456 VCL_SESS_ATTR_TCP_NODELAY);
2457 *buflen = sizeof (int);
2459 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2460 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2466 case VPPCOM_ATTR_SET_TCP_NODELAY:
2467 if (buffer && buflen && (*buflen == sizeof (int)))
2470 if (*(int *) buffer)
2471 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2473 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2475 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2476 "#VPP-TBD#", getpid (),
2477 VCL_SESS_ATTR_TEST (session->attr,
2478 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
2484 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2485 if (buffer && buflen && (*buflen >= sizeof (int)))
2488 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2489 VCL_SESS_ATTR_TCP_KEEPIDLE);
2490 *buflen = sizeof (int);
2492 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2493 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2499 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
2500 if (buffer && buflen && (*buflen == sizeof (int)))
2503 if (*(int *) buffer)
2504 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2506 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2508 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2509 "#VPP-TBD#", getpid (),
2510 VCL_SESS_ATTR_TEST (session->attr,
2511 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
2517 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2518 if (buffer && buflen && (*buflen >= sizeof (int)))
2521 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2522 VCL_SESS_ATTR_TCP_KEEPINTVL);
2523 *buflen = sizeof (int);
2525 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
2526 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2532 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
2533 if (buffer && buflen && (*buflen == sizeof (int)))
2536 if (*(int *) buffer)
2537 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2539 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2541 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
2542 "#VPP-TBD#", getpid (),
2543 VCL_SESS_ATTR_TEST (session->attr,
2544 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
2550 case VPPCOM_ATTR_GET_TCP_USER_MSS:
2551 if (buffer && buflen && (*buflen >= sizeof (u32)))
2554 *(u32 *) buffer = session->user_mss;
2555 *buflen = sizeof (int);
2557 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
2558 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
2564 case VPPCOM_ATTR_SET_TCP_USER_MSS:
2565 if (buffer && buflen && (*buflen == sizeof (u32)))
2568 session->user_mss = *(u32 *) buffer;
2570 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
2571 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
2583 VCL_SESSION_UNLOCK ();
2588 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
2589 uint32_t buflen, int flags, vppcom_endpt_t * ep)
2592 vcl_session_t *session = 0;
2596 VCL_SESSION_LOCK ();
2597 rv = vppcom_session_at_index (session_index, &session);
2598 if (PREDICT_FALSE (rv))
2600 VCL_SESSION_UNLOCK ();
2601 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
2602 getpid (), session_index);
2604 VCL_SESSION_UNLOCK ();
2607 ep->is_ip4 = session->transport.is_ip4;
2608 ep->port = session->transport.rmt_port;
2609 if (session->transport.is_ip4)
2610 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
2611 sizeof (ip4_address_t));
2613 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
2614 sizeof (ip6_address_t));
2615 VCL_SESSION_UNLOCK ();
2619 rv = vppcom_session_read (session_index, buffer, buflen);
2620 else if (flags & MSG_PEEK)
2621 rv = vppcom_session_peek (session_index, buffer, buflen);
2624 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
2626 rv = VPPCOM_EAFNOSUPPORT;
2634 vppcom_session_sendto (uint32_t session_index, void *buffer,
2635 uint32_t buflen, int flags, vppcom_endpt_t * ep)
2638 return VPPCOM_EINVAL;
2643 return VPPCOM_EINVAL;
2648 // TBD check the flags and do the right thing
2649 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
2650 getpid (), flags, flags);
2653 return (vppcom_session_write (session_index, buffer, buflen));
2657 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
2659 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
2660 u32 i, keep_trying = 1;
2663 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
2664 getpid (), vp, n_sids, wait_for_time);
2667 return VPPCOM_EFAULT;
2671 vcl_session_t *session;
2673 for (i = 0; i < n_sids; i++)
2675 ASSERT (vp[i].revents);
2677 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
2678 VCL_SESSION_UNLOCK ();
2683 if (POLLIN & vp[i].events)
2685 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
2686 rv = vppcom_session_read_ready (session, vp[i].sid);
2687 VCL_SESSION_UNLOCK ();
2690 *vp[i].revents |= POLLIN;
2697 case VPPCOM_ECONNRESET:
2698 *vp[i].revents = POLLHUP;
2702 *vp[i].revents = POLLERR;
2709 if (POLLOUT & vp[i].events)
2711 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
2712 rv = vppcom_session_write_ready (session, vp[i].sid);
2713 VCL_SESSION_UNLOCK ();
2716 *vp[i].revents |= POLLOUT;
2723 case VPPCOM_ECONNRESET:
2724 *vp[i].revents = POLLHUP;
2728 *vp[i].revents = POLLERR;
2735 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
2738 *vp[i].revents = POLLNVAL;
2742 if (wait_for_time != -1)
2743 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
2745 while ((num_ev == 0) && keep_trying);
2747 if (VPPCOM_DEBUG > 3)
2749 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
2750 for (i = 0; i < n_sids; i++)
2752 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
2753 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
2754 vp[i].events, *vp[i].revents);
2761 * fd.io coding-style-patch-verification: ON
2764 * eval: (c-set-style "gnu")