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.
19 #include <svm/svm_fifo_segment.h>
20 #include <vlibmemory/api.h>
21 #include <vpp/api/vpe_msg_enum.h>
22 #include <vnet/session/application_interface.h>
23 #include <vcl/vppcom.h>
24 #include <vcl/vcl_event.h>
25 #include <vcl/vcl_debug.h>
26 #include <vlib/unix/unix.h>
27 #include <vppinfra/vec_bootstrap.h>
29 #define vl_typedefs /* define message structures */
30 #include <vpp/api/vpe_all_api_h.h>
33 /* declare message handlers for each api */
35 #define vl_endianfun /* define message structures */
36 #include <vpp/api/vpe_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...)
42 #include <vpp/api/vpe_all_api_h.h>
46 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
47 * 3 for read/write debug output
49 * export VCL_DEBUG=<#> to set dynamically.
51 #define VPPCOM_DEBUG_INIT 1
53 #define VPPCOM_DEBUG_INIT 0
56 #define VPPCOM_DEBUG vcm->debug
59 * VPPCOM Private definitions and functions.
75 STATE_CLOSE_ON_EMPTY = 0x10,
76 STATE_DISCONNECT = 0x20,
80 #define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
81 #define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
83 typedef struct epoll_event vppcom_epoll_event_t;
90 vppcom_epoll_event_t ev;
91 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
92 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
104 VCL_SESS_ATTR_SERVER,
105 VCL_SESS_ATTR_CUT_THRU,
107 VCL_SESS_ATTR_VEP_SESSION,
108 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
109 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
110 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
111 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
112 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
113 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
114 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
115 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
116 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
117 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
119 } vppcom_session_attr_t;
121 #define VCL_SESS_ATTR_SET(ATTR, VAL) \
123 (ATTR) |= 1 << (VAL); \
126 #define VCL_SESS_ATTR_CLR(ATTR, VAL) \
128 (ATTR) &= ~(1 << (VAL)); \
131 #define VCL_SESS_ATTR_TEST(ATTR, VAL) \
132 ((ATTR) & (1 << (VAL)) ? 1 : 0)
136 volatile session_state_t state;
140 u32 sndbuf_size; // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
141 u32 rcvbuf_size; // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
142 u32 user_mss; // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
147 svm_queue_t *vpp_event_queue;
149 /* Socket configuration state */
156 vppcom_ip46_t lcl_addr;
157 vppcom_ip46_t peer_addr;
158 u16 lcl_port; // network order
159 u16 peer_port; // network order
161 u64 client_queue_address;
163 elog_track_t elog_track;
164 vce_event_handler_reg_t *poll_reg;
167 typedef struct vppcom_cfg_t_
170 u32 vpp_api_q_length;
173 u32 add_segment_size;
174 u32 preallocated_fifo_pairs;
177 u32 event_queue_size;
178 u32 listen_queue_size;
179 u8 app_proxy_transport_tcp;
180 u8 app_proxy_transport_udp;
184 u64 namespace_secret;
189 char *event_log_path;
190 u8 *vpp_api_filename;
193 typedef struct vppcom_main_t_
199 /* FIFO for accepted connections - used in epoll/select */
200 clib_spinlock_t session_fifo_lockp;
201 u32 *client_session_index_fifo;
203 /* vpp input queue */
204 svm_queue_t *vl_input_queue;
206 /* API client handle */
209 clib_spinlock_t sessions_lockp;
212 /* Hash table for disconnect processing */
213 uword *session_index_by_vpp_handles;
216 clib_bitmap_t *rd_bitmap;
217 clib_bitmap_t *wr_bitmap;
218 clib_bitmap_t *ex_bitmap;
220 /* Our event queue */
221 svm_queue_t *app_event_queue;
223 /* unique segment name counter */
224 u32 unique_segment_index;
226 /* For deadman timers */
227 clib_time_t clib_time;
229 /* State of the connection, shared between msg RX thread and main thread */
230 volatile app_state_t app_state;
235 vce_event_thread_t event_thread;
238 vppcom_session_io_thread_t session_io_thread;
241 /* VPP Event-logger */
242 elog_main_t elog_main;
243 elog_track_t elog_track;
246 /* VNET_API_ERROR_FOO -> "Foo" hash table */
247 uword *error_string_by_error_number;
250 /* NOTE: _vppcom_main is only used until the heap is allocated.
251 * Do not access it directly -- use vcm which will point to
252 * the heap allocated copy after init.
254 static vppcom_main_t _vppcom_main = {
255 .debug = VPPCOM_DEBUG_INIT,
256 .my_client_index = ~0
259 static vppcom_main_t *vcm = &_vppcom_main;
261 #define VCL_SESSION_LOCK_AND_GET(I, S) \
263 clib_spinlock_lock (&vcm->sessions_lockp); \
264 rv = vppcom_session_at_index (I, S); \
265 if (PREDICT_FALSE (rv)) \
267 clib_spinlock_unlock (&vcm->sessions_lockp); \
268 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
274 #define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
275 #define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
277 #define VCL_IO_SESSIONS_LOCK() \
278 clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
279 #define VCL_IO_SESSIONS_UNLOCK() \
280 clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
282 #define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
283 #define VCL_ACCEPT_FIFO_UNLOCK() \
284 clib_spinlock_unlock (&(vcm->session_fifo_lockp))
286 #define VCL_EVENTS_LOCK() \
287 clib_spinlock_lock (&(vcm->event_thread.events_lockp))
288 #define VCL_EVENTS_UNLOCK() \
289 clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
292 vppcom_app_state_str (app_state_t state)
298 case STATE_APP_START:
299 st = "STATE_APP_START";
302 case STATE_APP_CONN_VPP:
303 st = "STATE_APP_CONN_VPP";
306 case STATE_APP_ENABLED:
307 st = "STATE_APP_ENABLED";
310 case STATE_APP_ATTACHED:
311 st = "STATE_APP_ATTACHED";
315 st = "UNKNOWN_APP_STATE";
323 vppcom_session_state_str (session_state_t state)
334 st = "STATE_CONNECT";
345 case STATE_CLOSE_ON_EMPTY:
346 st = "STATE_CLOSE_ON_EMPTY";
349 case STATE_DISCONNECT:
350 st = "STATE_DISCONNECT";
358 st = "UNKNOWN_STATE";
367 * VPPCOM Utility Functions
371 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
373 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
374 if (PREDICT_FALSE ((session_index == ~0) ||
375 pool_is_free_index (vcm->sessions, session_index)))
377 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
378 getpid (), session_index);
379 return VPPCOM_EBADFD;
381 *sess = pool_elt_at_index (vcm->sessions, session_index);
386 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
388 /* Session and listener handles have different formats. The latter has
389 * the thread index in the upper 32 bits while the former has the session
390 * type. Knowing that, for listeners we just flip the MSB to 1 */
391 listener_handle |= 1ULL << 63;
392 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
395 static inline session_t *
396 vppcom_session_table_lookup_listener (u64 listener_handle)
399 u64 handle = listener_handle | (1ULL << 63);
402 p = hash_get (vcm->session_index_by_vpp_handles, handle);
405 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
406 "listener handle %llx", getpid (), listener_handle);
409 if (pool_is_free_index (vcm->sessions, p[0]))
411 VDBG (1, "VCL<%d>: invalid listen session, sid (%u)", getpid (), p[0]);
415 session = pool_elt_at_index (vcm->sessions, p[0]);
416 ASSERT (session->state & STATE_LISTEN);
421 vppcom_session_table_del_listener (u64 listener_handle)
423 listener_handle |= 1ULL << 63;
424 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
428 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
430 vl_api_accept_session_reply_t *rmp;
432 rmp = vl_msg_api_alloc (sizeof (*rmp));
433 memset (rmp, 0, sizeof (*rmp));
434 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
435 rmp->retval = htonl (retval);
436 rmp->context = context;
437 rmp->handle = handle;
438 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
442 vppcom_connect_to_vpp (char *app_name)
444 api_main_t *am = &api_main;
445 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
448 if (!vcl_cfg->vpp_api_filename)
449 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
451 VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...",
452 getpid (), app_name, vcl_cfg->vpp_api_filename);
454 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
455 vcm->cfg.vpp_api_q_length) < 0)
457 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
458 rv = VPPCOM_ECONNREFUSED;
462 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
463 vcm->my_client_index = (u32) am->my_client_index;
464 vcm->app_state = STATE_APP_CONN_VPP;
466 VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
469 vcl_evt (VCL_EVT_INIT, vcm);
474 format_api_error (u8 * s, va_list * args)
476 i32 error = va_arg (*args, u32);
479 p = hash_get (vcm->error_string_by_error_number, -error);
482 s = format (s, "%s (%d)", p[0], error);
484 s = format (s, "%d", error);
489 vppcom_init_error_string_table (void)
491 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
493 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
494 foreach_vnet_api_error;
497 hash_set (vcm->error_string_by_error_number, 99, "Misc");
501 vppcom_wait_for_app_state_change (app_state_t app_state)
503 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
505 while (clib_time_now (&vcm->clib_time) < timeout)
507 if (vcm->app_state == app_state)
510 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
511 vppcom_app_state_str (app_state), app_state);
512 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
514 return VPPCOM_ETIMEDOUT;
518 vppcom_wait_for_session_state_change (u32 session_index,
519 session_state_t state,
522 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
523 session_t *volatile session;
529 rv = vppcom_session_at_index (session_index, &session);
530 if (PREDICT_FALSE (rv))
532 VCL_SESSION_UNLOCK ();
535 if (session->state & state)
537 VCL_SESSION_UNLOCK ();
540 if (session->state & STATE_FAILED)
542 VCL_SESSION_UNLOCK ();
543 return VPPCOM_ECONNREFUSED;
546 VCL_SESSION_UNLOCK ();
548 while (clib_time_now (&vcm->clib_time) < timeout);
550 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
551 vppcom_session_state_str (state));
552 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, state);
554 return VPPCOM_ETIMEDOUT;
558 * VPP-API message functions
561 vppcom_send_session_enable_disable (u8 is_enable)
563 vl_api_session_enable_disable_t *bmp;
564 bmp = vl_msg_api_alloc (sizeof (*bmp));
565 memset (bmp, 0, sizeof (*bmp));
567 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
568 bmp->client_index = vcm->my_client_index;
569 bmp->context = htonl (0xfeedface);
570 bmp->is_enable = is_enable;
571 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
575 vppcom_app_session_enable (void)
579 if (vcm->app_state != STATE_APP_ENABLED)
581 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
582 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
583 if (PREDICT_FALSE (rv))
585 VDBG (0, "VCL<%d>: application session enable timed out! "
586 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
594 vl_api_session_enable_disable_reply_t_handler
595 (vl_api_session_enable_disable_reply_t * mp)
599 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
600 format_api_error, ntohl (mp->retval));
603 vcm->app_state = STATE_APP_ENABLED;
607 vppcom_app_send_attach (void)
609 vl_api_application_attach_t *bmp;
610 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
611 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
612 vcm->cfg.app_proxy_transport_udp);
614 bmp = vl_msg_api_alloc (sizeof (*bmp));
615 memset (bmp, 0, sizeof (*bmp));
617 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
618 bmp->client_index = vcm->my_client_index;
619 bmp->context = htonl (0xfeedface);
620 bmp->options[APP_OPTIONS_FLAGS] =
621 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
622 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
623 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
624 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
625 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
626 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
627 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
628 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
629 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
630 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
631 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
632 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
633 vcm->cfg.preallocated_fifo_pairs;
634 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
637 bmp->namespace_id_len = nsid_len;
638 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
639 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
641 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
645 vppcom_app_attach (void)
649 vppcom_app_send_attach ();
650 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
651 if (PREDICT_FALSE (rv))
653 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
654 getpid (), rv, vppcom_retval_str (rv));
662 vppcom_app_detach (void)
664 vl_api_application_detach_t *bmp;
665 bmp = vl_msg_api_alloc (sizeof (*bmp));
666 memset (bmp, 0, sizeof (*bmp));
668 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
669 bmp->client_index = vcm->my_client_index;
670 bmp->context = htonl (0xfeedface);
671 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
675 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
678 static svm_fifo_segment_create_args_t _a;
679 svm_fifo_segment_create_args_t *a = &_a;
682 memset (a, 0, sizeof (*a));
685 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
686 format_api_error, ntohl (mp->retval));
690 if (mp->segment_name_length == 0)
692 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
696 a->segment_name = (char *) mp->segment_name;
697 a->segment_size = mp->segment_size;
699 ASSERT (mp->app_event_queue_address);
701 /* Attach to the segment vpp created */
702 rv = svm_fifo_segment_attach (a);
703 vec_reset_length (a->new_segment_indices);
704 if (PREDICT_FALSE (rv))
706 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
707 getpid (), mp->segment_name);
711 vcm->app_event_queue =
712 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
714 vcm->app_state = STATE_APP_ATTACHED;
718 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
722 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
725 vcm->app_state = STATE_APP_ENABLED;
729 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
733 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
734 getpid (), mp->handle, format_api_error,
739 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
741 static svm_fifo_segment_create_args_t _a;
742 svm_fifo_segment_create_args_t *a = &_a;
745 memset (a, 0, sizeof (*a));
746 a->segment_name = (char *) mp->segment_name;
747 a->segment_size = mp->segment_size;
748 /* Attach to the segment vpp created */
749 rv = svm_fifo_segment_attach (a);
750 vec_reset_length (a->new_segment_indices);
751 if (PREDICT_FALSE (rv))
753 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
754 getpid (), mp->segment_name);
758 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
759 mp->segment_name, mp->segment_size);
763 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
767 * XXX Need segment_name to session_id hash,
768 * XXX - have sessionID by handle hash currently
771 VDBG (1, "Unmapped segment '%s'", mp->segment_name);
775 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
779 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
783 session_t *session = 0;
784 u32 session_index = p[0];
786 VCL_SESSION_LOCK_AND_GET (session_index, &session);
787 session->state = STATE_CLOSE_ON_EMPTY;
789 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: setting state to 0x%x "
790 "(%s)", getpid (), mp->handle, session_index, session->state,
791 vppcom_session_state_str (session->state));
792 VCL_SESSION_UNLOCK ();
796 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session lookup failed!",
797 getpid (), mp->handle, session_index);
800 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
801 "handle failed!", getpid (), mp->handle);
805 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
807 session_t *session = 0;
808 vl_api_reset_session_reply_t *rmp;
812 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
817 rval = vppcom_session_at_index (p[0], &session);
818 if (PREDICT_FALSE (rval))
820 rv = VNET_API_ERROR_INVALID_VALUE_2;
821 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
822 "session lookup failed! returning %d %U",
823 getpid (), mp->handle, p[0],
824 rv, format_api_error, rv);
828 /* TBD: should this disconnect immediately and
831 session->state = STATE_CLOSE_ON_EMPTY;
833 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: state set to %d "
834 "(%s)!", getpid (), mp->handle, p[0], session->state,
835 vppcom_session_state_str (session->state));
837 VCL_SESSION_UNLOCK ();
841 rv = VNET_API_ERROR_INVALID_VALUE;
842 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
843 "failed! returning %d %U",
844 getpid (), mp->handle, rv, format_api_error, rv);
847 rmp = vl_msg_api_alloc (sizeof (*rmp));
848 memset (rmp, 0, sizeof (*rmp));
849 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
850 rmp->retval = htonl (rv);
851 rmp->handle = mp->handle;
852 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
856 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
858 session_t *session = 0;
860 svm_fifo_t *rx_fifo, *tx_fifo;
863 session_index = mp->context;
864 VCL_SESSION_LOCK_AND_GET (session_index, &session);
868 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
869 "connect failed! %U",
870 getpid (), mp->handle, session_index,
871 format_api_error, ntohl (mp->retval));
874 session->state = STATE_FAILED;
875 session->vpp_handle = mp->handle;
879 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
880 "Invalid session index (%u)!",
881 getpid (), mp->handle, session_index);
892 if (vcm->session_io_thread.io_sessions_lockp)
894 // Add this connection to the active io sessions list
895 VCL_IO_SESSIONS_LOCK ();
896 u32 *active_session_index;
897 pool_get (vcm->session_io_thread.active_session_indexes,
898 active_session_index);
899 *active_session_index = session_index;
900 VCL_IO_SESSIONS_UNLOCK ();
902 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
905 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
906 rx_fifo->client_session_index = session_index;
907 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
908 tx_fifo->client_session_index = session_index;
910 session->rx_fifo = rx_fifo;
911 session->tx_fifo = tx_fifo;
912 session->vpp_handle = mp->handle;
913 session->lcl_addr.is_ip4 = mp->is_ip4;
914 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
915 sizeof (session->peer_addr.ip46));
916 session->lcl_port = mp->lcl_port;
917 session->state = STATE_CONNECT;
919 /* Add it to lookup table */
920 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
922 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
923 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
924 getpid (), mp->handle, session_index, session->rx_fifo,
925 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
927 VCL_SESSION_UNLOCK ();
931 vppcom_send_connect_sock (session_t * session, u32 session_index)
933 vl_api_connect_sock_t *cmp;
935 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
936 cmp = vl_msg_api_alloc (sizeof (*cmp));
937 memset (cmp, 0, sizeof (*cmp));
938 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
939 cmp->client_index = vcm->my_client_index;
940 cmp->context = session_index;
942 cmp->is_ip4 = session->peer_addr.is_ip4;
943 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
944 cmp->port = session->peer_port;
945 cmp->proto = session->proto;
946 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
947 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
951 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
954 vl_api_disconnect_session_reply_t *rmp;
956 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
957 getpid (), vpp_handle, session_index);
959 rmp = vl_msg_api_alloc (sizeof (*rmp));
960 memset (rmp, 0, sizeof (*rmp));
962 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
963 rmp->retval = htonl (rv);
964 rmp->handle = vpp_handle;
965 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
969 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
971 vl_api_disconnect_session_t *dmp;
973 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
974 getpid (), vpp_handle, session_index);
976 dmp = vl_msg_api_alloc (sizeof (*dmp));
977 memset (dmp, 0, sizeof (*dmp));
978 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
979 dmp->client_index = vcm->my_client_index;
980 dmp->handle = vpp_handle;
981 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
985 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
987 session_t *session = 0;
988 u32 session_index = mp->context;
991 VCL_SESSION_LOCK_AND_GET (session_index, &session);
995 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
996 "sid %u: bind failed: %U",
997 getpid (), mp->handle, session_index,
998 format_api_error, ntohl (mp->retval));
999 rv = vppcom_session_at_index (session_index, &session);
1000 if (rv == VPPCOM_OK)
1002 session->state = STATE_FAILED;
1003 session->vpp_handle = mp->handle;
1007 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1008 "Invalid session index (%u)!",
1009 getpid (), mp->handle, session_index);
1014 session->vpp_handle = mp->handle;
1015 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1016 session->lcl_addr.ip46 = to_ip46 (mp->lcl_is_ip4 ? IP46_TYPE_IP4 :
1017 IP46_TYPE_IP6, mp->lcl_ip);
1018 session->lcl_port = mp->lcl_port;
1019 vppcom_session_table_add_listener (mp->handle, session_index);
1020 session->state = STATE_LISTEN;
1022 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1023 getpid (), mp->handle, mp->context);
1025 VCL_SESSION_UNLOCK ();
1029 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1032 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1033 getpid (), mp->context, format_api_error,
1034 ntohl (mp->retval));
1037 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
1041 format_ip4_address (u8 * s, va_list * args)
1043 u8 *a = va_arg (*args, u8 *);
1044 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1048 format_ip6_address (u8 * s, va_list * args)
1050 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1051 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1053 i_max_n_zero = ARRAY_LEN (a->as_u16);
1055 i_first_zero = i_max_n_zero;
1057 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1059 u32 is_zero = a->as_u16[i] == 0;
1060 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1066 if ((!is_zero && n_zeros > max_n_zeros)
1067 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1069 i_max_n_zero = i_first_zero;
1070 max_n_zeros = n_zeros;
1071 i_first_zero = ARRAY_LEN (a->as_u16);
1076 last_double_colon = 0;
1077 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1079 if (i == i_max_n_zero && max_n_zeros > 1)
1081 s = format (s, "::");
1082 i += max_n_zeros - 1;
1083 last_double_colon = 1;
1087 s = format (s, "%s%x",
1088 (last_double_colon || i == 0) ? "" : ":",
1089 clib_net_to_host_u16 (a->as_u16[i]));
1090 last_double_colon = 0;
1097 /* Format an IP46 address. */
1099 format_ip46_address (u8 * s, va_list * args)
1101 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1102 ip46_type_t type = va_arg (*args, ip46_type_t);
1108 is_ip4 = ip46_address_is_ip4 (ip46);
1119 format (s, "%U", format_ip4_address, &ip46->ip4) :
1120 format (s, "%U", format_ip6_address, &ip46->ip6);
1124 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1126 svm_fifo_t *rx_fifo, *tx_fifo;
1127 session_t *session, *listen_session;
1129 vce_event_connect_request_t *ecr;
1135 VCL_SESSION_LOCK ();
1137 VCL_ACCEPT_FIFO_LOCK ();
1138 elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
1139 VCL_ACCEPT_FIFO_UNLOCK ();
1143 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1144 vppcom_send_accept_session_reply (mp->handle, mp->context,
1145 VNET_API_ERROR_QUEUE_FULL);
1146 VCL_SESSION_UNLOCK ();
1150 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1151 if (!listen_session)
1153 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1154 "unknown vpp listener handle %llx",
1155 getpid (), mp->listener_handle);
1156 vppcom_send_accept_session_reply (mp->handle, mp->context,
1157 VNET_API_ERROR_INVALID_ARGUMENT);
1158 VCL_SESSION_UNLOCK ();
1162 /* TODO check listener depth and update */
1163 /* TODO on "child" fd close, update listener depth */
1165 /* Allocate local session and set it up */
1166 pool_get (vcm->sessions, session);
1167 memset (session, 0, sizeof (*session));
1168 session_index = (u32) (session - vcm->sessions);
1170 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1171 rx_fifo->client_session_index = session_index;
1172 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1173 tx_fifo->client_session_index = session_index;
1175 session->vpp_handle = mp->handle;
1176 session->client_context = mp->context;
1177 session->rx_fifo = rx_fifo;
1178 session->tx_fifo = tx_fifo;
1179 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1181 session->state = STATE_ACCEPT;
1182 session->peer_port = mp->port;
1183 session->peer_addr.is_ip4 = mp->is_ip4;
1184 session->peer_addr.ip46 = to_ip46 (mp->is_ip4 ? IP46_TYPE_IP4 :
1185 IP46_TYPE_IP6, mp->ip);
1187 /* Add it to lookup table */
1188 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1189 session->lcl_port = listen_session->lcl_port;
1190 session->lcl_addr = listen_session->lcl_addr;
1192 /* Create an event for handlers */
1196 pool_get (vcm->event_thread.vce_events, ev);
1197 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
1198 ecr = vce_get_event_data (ev, sizeof (*ecr));
1199 ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1200 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1201 ev->evk.session_index = (u32) (listen_session - vcm->sessions);
1202 ecr->accepted_session_index = session_index;
1204 VCL_EVENTS_UNLOCK ();
1206 rv = vce_generate_event (&vcm->event_thread, ev_idx);
1209 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
1210 " address %U port %d queue %p!", getpid (), mp->handle, session_index,
1211 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
1212 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1213 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1215 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
1216 VCL_SESSION_UNLOCK ();
1219 /* VPP combines bind and listen as one operation. VCL manages the separation
1220 * of bind and listen locally via vppcom_session_bind() and
1221 * vppcom_session_listen() */
1223 vppcom_send_bind_sock (session_t * session, u32 session_index)
1225 vl_api_bind_sock_t *bmp;
1227 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1228 bmp = vl_msg_api_alloc (sizeof (*bmp));
1229 memset (bmp, 0, sizeof (*bmp));
1231 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1232 bmp->client_index = vcm->my_client_index;
1233 bmp->context = session_index;
1234 bmp->is_ip4 = session->lcl_addr.is_ip4;
1235 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1236 bmp->port = session->lcl_port;
1237 bmp->proto = session->proto;
1238 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1239 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1243 vppcom_send_unbind_sock (u64 vpp_handle)
1245 vl_api_unbind_sock_t *ump;
1247 ump = vl_msg_api_alloc (sizeof (*ump));
1248 memset (ump, 0, sizeof (*ump));
1250 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1251 ump->client_index = vcm->my_client_index;
1252 ump->handle = vpp_handle;
1253 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1257 vppcom_session_unbind (u32 session_index)
1259 session_t *session = 0;
1263 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1265 vpp_handle = session->vpp_handle;
1266 vppcom_session_table_del_listener (vpp_handle);
1267 session->vpp_handle = ~0;
1268 session->state = STATE_DISCONNECT;
1270 VCL_SESSION_UNLOCK ();
1272 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
1273 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
1274 vppcom_session_state_str (STATE_DISCONNECT));
1275 vcl_evt (VCL_EVT_UNBIND, session);
1276 vppcom_send_unbind_sock (vpp_handle);
1283 vppcom_session_disconnect (u32 session_index)
1288 session_state_t state;
1290 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1292 vpp_handle = session->vpp_handle;
1293 state = session->state;
1294 VCL_SESSION_UNLOCK ();
1296 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
1297 vpp_handle, session_index, state, vppcom_session_state_str (state));
1299 if (PREDICT_FALSE (state & STATE_LISTEN))
1301 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1302 "Cannot disconnect a listen socket!",
1303 getpid (), vpp_handle, session_index);
1308 /* The peer has already initiated the close,
1309 * so send the disconnect session reply.
1311 if (state & STATE_CLOSE_ON_EMPTY)
1313 //XXX alagalah - Check and drain here?
1314 vppcom_send_disconnect_session_reply (vpp_handle,
1315 session_index, 0 /* rv */ );
1316 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
1317 "REPLY...", getpid (), vpp_handle, session_index);
1320 /* Otherwise, send a disconnect session msg...
1324 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
1325 getpid (), vpp_handle, session_index);
1327 vppcom_send_disconnect_session (vpp_handle, session_index);
1334 #define foreach_sock_msg \
1335 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1336 _(BIND_SOCK_REPLY, bind_sock_reply) \
1337 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1338 _(ACCEPT_SESSION, accept_session) \
1339 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1340 _(DISCONNECT_SESSION, disconnect_session) \
1341 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1342 _(RESET_SESSION, reset_session) \
1343 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1344 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1345 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1346 _(UNMAP_SEGMENT, unmap_segment)
1349 vppcom_api_hookup (void)
1352 vl_msg_api_set_handlers(VL_API_##N, #n, \
1353 vl_api_##n##_t_handler, \
1355 vl_api_##n##_t_endian, \
1356 vl_api_##n##_t_print, \
1357 sizeof(vl_api_##n##_t), 1);
1363 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1367 vcl_cfg->heapsize = (256ULL << 20);
1368 vcl_cfg->vpp_api_q_length = 1024;
1369 vcl_cfg->segment_baseva = 0x200000000ULL;
1370 vcl_cfg->segment_size = (256 << 20);
1371 vcl_cfg->add_segment_size = (128 << 20);
1372 vcl_cfg->preallocated_fifo_pairs = 8;
1373 vcl_cfg->rx_fifo_size = (1 << 20);
1374 vcl_cfg->tx_fifo_size = (1 << 20);
1375 vcl_cfg->event_queue_size = 2048;
1376 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1377 vcl_cfg->app_timeout = 10 * 60.0;
1378 vcl_cfg->session_timeout = 10 * 60.0;
1379 vcl_cfg->accept_timeout = 60.0;
1380 vcl_cfg->event_ring_size = (128 << 10);
1381 vcl_cfg->event_log_path = "/dev/shm";
1385 vppcom_cfg_heapsize (char *conf_fname)
1387 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1400 fp = fopen (conf_fname, "r");
1403 VDBG (0, "VCL<%d>: using default heapsize %lld (0x%llx)", getpid (),
1404 vcl_cfg->heapsize, vcl_cfg->heapsize);
1408 argv = calloc (1, sizeof (char *));
1411 VDBG (0, "VCL<%d>: calloc failed, using default heapsize %lld (0x%llx)",
1412 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1418 if (fgets (inbuf, 4096, fp) == 0)
1420 p = strtok (inbuf, " \t\n");
1426 char **tmp = realloc (argv, argc * sizeof (char *));
1429 VDBG (0, "VCL<%d>: realloc failed, using default heapsize %lld "
1430 "(0x%llx)", getpid (), vcl_cfg->heapsize,
1435 arg = strndup (p, 1024);
1438 VDBG (0, "VCL<%d>: strndup failed, using default heapsize %lld "
1439 "(0x%llx)", getpid (), vcl_cfg->heapsize,
1443 argv[argc - 1] = arg;
1444 p = strtok (NULL, " \t\n");
1451 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1454 VDBG (0, "VCL<%d>: realloc failed, using default heapsize %lld "
1455 "(0x%llx)", getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1462 * Look for and parse the "heapsize" config parameter.
1463 * Manual since none of the clib infra has been bootstrapped yet.
1465 * Format: heapsize <nn>[mM][gG]
1468 for (i = 1; i < (argc - 1); i++)
1470 if (!strncmp (argv[i], "heapsize", 8))
1472 sizep = (u8 *) argv[i + 1];
1474 while (*sizep >= '0' && *sizep <= '9')
1477 size += *sizep++ - '0';
1481 VDBG (0, "VCL<%d>: parse error '%s %s', using default "
1482 "heapsize %lld (0x%llx)", getpid (), argv[i], argv[i + 1],
1483 vcl_cfg->heapsize, vcl_cfg->heapsize);
1487 if (*sizep == 'g' || *sizep == 'G')
1488 vcl_cfg->heapsize = size << 30;
1489 else if (*sizep == 'm' || *sizep == 'M')
1490 vcl_cfg->heapsize = size << 20;
1493 VDBG (0, "VCL<%d>: parse error '%s %s', using default "
1494 "heapsize %lld (0x%llx)", getpid (), argv[i], argv[i + 1],
1495 vcl_cfg->heapsize, vcl_cfg->heapsize);
1507 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1508 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1509 if (vcl_mem == MAP_FAILED)
1511 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1512 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1514 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1515 ASSERT (vcl_mem != MAP_FAILED);
1518 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1521 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1525 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1528 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1533 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1536 VDBG (0, "VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)", getpid (),
1537 heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1541 vppcom_cfg_read (char *conf_fname)
1543 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1545 unformat_input_t _input, *input = &_input;
1546 unformat_input_t _line_input, *line_input = &_line_input;
1547 u8 vc_cfg_input = 0;
1550 u32 uid, gid, q_len;
1552 fd = open (conf_fname, O_RDONLY);
1555 VDBG (0, "VCL<%d>: using default configuration.", getpid (),
1560 if (fstat (fd, &s) < 0)
1562 VDBG (0, "VCL<%d>: failed to stat `%s', using default configuration",
1563 getpid (), conf_fname);
1567 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1569 VDBG (0, "VCL<%d>: not a regular file `%s', using default "
1570 "configuration", getpid (), conf_fname);
1574 unformat_init_clib_file (input, fd);
1576 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1578 (void) unformat_user (input, unformat_line_input, line_input);
1579 unformat_skip_white_space (line_input);
1581 if (unformat (line_input, "vcl {"))
1589 if (unformat (line_input, "heapsize %s", &chroot_path))
1591 vec_terminate_c_string (chroot_path);
1592 VDBG (0, "VCL<%d>: configured heapsize %s, actual heapsize %lld"
1593 " (0x%llx)", getpid (), chroot_path, vcl_cfg->heapsize,
1595 vec_free (chroot_path);
1597 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1599 vec_terminate_c_string (chroot_path);
1600 if (vcl_cfg->vpp_api_filename)
1601 vec_free (vcl_cfg->vpp_api_filename);
1602 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1604 vl_set_memory_root_path ((char *) chroot_path);
1606 VDBG (0, "VCL<%d>: configured api-prefix (%s) and api filename"
1607 " (%s)", getpid (), chroot_path,
1608 vcl_cfg->vpp_api_filename);
1609 chroot_path = 0; /* Don't vec_free() it! */
1611 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1613 if (q_len < vcl_cfg->vpp_api_q_length)
1615 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1616 "(%u) is too small! Using default: %u ",
1617 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1621 vcl_cfg->vpp_api_q_length = q_len;
1623 VDBG (0, "VCL<%d>: configured vpp-api-q-length %u",
1624 getpid (), vcl_cfg->vpp_api_q_length);
1627 else if (unformat (line_input, "uid %d", &uid))
1629 vl_set_memory_uid (uid);
1630 VDBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
1632 else if (unformat (line_input, "gid %d", &gid))
1634 vl_set_memory_gid (gid);
1635 VDBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
1637 else if (unformat (line_input, "segment-baseva 0x%lx",
1638 &vcl_cfg->segment_baseva))
1640 VDBG (0, "VCL<%d>: configured segment_baseva 0x%lx", getpid (),
1641 vcl_cfg->segment_baseva);
1643 else if (unformat (line_input, "segment-size 0x%lx",
1644 &vcl_cfg->segment_size))
1646 VDBG (0, "VCL<%d>: configured segment_size 0x%lx (%ld)",
1647 getpid (), vcl_cfg->segment_size, vcl_cfg->segment_size);
1649 else if (unformat (line_input, "segment-size %ld",
1650 &vcl_cfg->segment_size))
1652 VDBG (0, "VCL<%d>: configured segment_size %ld (0x%lx)",
1653 getpid (), vcl_cfg->segment_size, vcl_cfg->segment_size);
1655 else if (unformat (line_input, "add-segment-size 0x%lx",
1656 &vcl_cfg->add_segment_size))
1658 VDBG (0, "VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1659 getpid (), vcl_cfg->add_segment_size,
1660 vcl_cfg->add_segment_size);
1662 else if (unformat (line_input, "add-segment-size %ld",
1663 &vcl_cfg->add_segment_size))
1665 VDBG (0, "VCL<%d>: configured add_segment_size %ld (0x%lx)",
1666 getpid (), vcl_cfg->add_segment_size,
1667 vcl_cfg->add_segment_size);
1669 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1670 &vcl_cfg->preallocated_fifo_pairs))
1672 VDBG (0, "VCL<%d>: configured preallocated_fifo_pairs %d "
1673 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
1674 vcl_cfg->preallocated_fifo_pairs);
1676 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1677 &vcl_cfg->rx_fifo_size))
1679 VDBG (0, "VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1680 getpid (), vcl_cfg->rx_fifo_size, vcl_cfg->rx_fifo_size);
1682 else if (unformat (line_input, "rx-fifo-size %ld",
1683 &vcl_cfg->rx_fifo_size))
1685 VDBG (0, "VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1686 getpid (), vcl_cfg->rx_fifo_size, vcl_cfg->rx_fifo_size);
1688 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1689 &vcl_cfg->tx_fifo_size))
1691 VDBG (0, "VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
1692 getpid (), vcl_cfg->tx_fifo_size, vcl_cfg->tx_fifo_size);
1694 else if (unformat (line_input, "tx-fifo-size %ld",
1695 &vcl_cfg->tx_fifo_size))
1697 VDBG (0, "VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
1698 getpid (), vcl_cfg->tx_fifo_size, vcl_cfg->tx_fifo_size);
1700 else if (unformat (line_input, "event-queue-size 0x%lx",
1701 &vcl_cfg->event_queue_size))
1703 VDBG (0, "VCL<%d>: configured event_queue_size 0x%lx (%ld)",
1704 getpid (), vcl_cfg->event_queue_size,
1705 vcl_cfg->event_queue_size);
1707 else if (unformat (line_input, "event-queue-size %ld",
1708 &vcl_cfg->event_queue_size))
1710 VDBG (0, "VCL<%d>: configured event_queue_size %ld (0x%lx)",
1711 getpid (), vcl_cfg->event_queue_size,
1712 vcl_cfg->event_queue_size);
1714 else if (unformat (line_input, "listen-queue-size 0x%lx",
1715 &vcl_cfg->listen_queue_size))
1717 VDBG (0, "VCL<%d>: configured listen_queue_size 0x%lx (%ld)",
1718 getpid (), vcl_cfg->listen_queue_size,
1719 vcl_cfg->listen_queue_size);
1721 else if (unformat (line_input, "listen-queue-size %ld",
1722 &vcl_cfg->listen_queue_size))
1724 VDBG (0, "VCL<%d>: configured listen_queue_size %ld (0x%lx)",
1725 getpid (), vcl_cfg->listen_queue_size,
1726 vcl_cfg->listen_queue_size);
1728 else if (unformat (line_input, "app-timeout %f",
1729 &vcl_cfg->app_timeout))
1731 VDBG (0, "VCL<%d>: configured app_timeout %f",
1732 getpid (), vcl_cfg->app_timeout);
1734 else if (unformat (line_input, "session-timeout %f",
1735 &vcl_cfg->session_timeout))
1737 VDBG (0, "VCL<%d>: configured session_timeout %f",
1738 getpid (), vcl_cfg->session_timeout);
1740 else if (unformat (line_input, "accept-timeout %f",
1741 &vcl_cfg->accept_timeout))
1743 VDBG (0, "VCL<%d>: configured accept_timeout %f",
1744 getpid (), vcl_cfg->accept_timeout);
1746 else if (unformat (line_input, "app-proxy-transport-tcp"))
1748 vcl_cfg->app_proxy_transport_tcp = 1;
1749 VDBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
1750 getpid (), vcl_cfg->app_proxy_transport_tcp);
1752 else if (unformat (line_input, "app-proxy-transport-udp"))
1754 vcl_cfg->app_proxy_transport_udp = 1;
1755 VDBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
1756 getpid (), vcl_cfg->app_proxy_transport_udp);
1758 else if (unformat (line_input, "app-scope-local"))
1760 vcl_cfg->app_scope_local = 1;
1761 VDBG (0, "VCL<%d>: configured app_scope_local (%d)",
1762 getpid (), vcl_cfg->app_scope_local);
1764 else if (unformat (line_input, "app-scope-global"))
1766 vcl_cfg->app_scope_global = 1;
1767 VDBG (0, "VCL<%d>: configured app_scope_global (%d)",
1768 getpid (), vcl_cfg->app_scope_global);
1770 else if (unformat (line_input, "namespace-secret %lu",
1771 &vcl_cfg->namespace_secret))
1773 VDBG (0, "VCL<%d>: configured namespace_secret %lu (0x%lx)",
1774 getpid (), vcl_cfg->namespace_secret,
1775 vcl_cfg->namespace_secret);
1777 else if (unformat (line_input, "namespace-id %v",
1778 &vcl_cfg->namespace_id))
1780 vl_api_application_attach_t *mp;
1781 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1782 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1783 if (nsid_vec_len > max_nsid_vec_len)
1785 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1786 VDBG (0, "VCL<%d>: configured namespace_id is too long,"
1787 " truncated to %d characters!",
1788 getpid (), max_nsid_vec_len);
1791 VDBG (0, "VCL<%d>: configured namespace_id %v",
1792 getpid (), vcl_cfg->namespace_id);
1794 else if (unformat (line_input, "}"))
1797 VDBG (0, "VCL<%d>: completed parsing vppcom config!",
1803 if (line_input->buffer[line_input->index] != '#')
1805 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
1807 &line_input->buffer[line_input->index]);
1814 unformat_free (input);
1822 * VPPCOM Public API functions
1825 vppcom_app_create (char *app_name)
1827 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1838 vppcom_cfg_init (vcl_cfg);
1839 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1843 if (sscanf (env_var_str, "%u", &tmp) != 1)
1844 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
1845 "in the environment variable " VPPCOM_ENV_DEBUG
1846 " (%s)!\n", getpid (), env_var_str);
1850 VDBG (0, "VCL<%d>: configured VCL debug level (%u) from "
1851 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
1854 conf_fname = getenv (VPPCOM_ENV_CONF);
1856 conf_fname = VPPCOM_CONF_DEFAULT;
1857 vppcom_cfg_heapsize (conf_fname);
1858 vcl_cfg = &vcm->cfg;
1859 clib_spinlock_init (&vcm->session_fifo_lockp);
1860 clib_fifo_validate (vcm->client_session_index_fifo,
1861 vcm->cfg.listen_queue_size);
1862 vppcom_cfg_read (conf_fname);
1865 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
1868 if (vcl_cfg->vpp_api_filename)
1869 vec_free (vcl_cfg->vpp_api_filename);
1870 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1872 vl_set_memory_root_path ((char *) env_var_str);
1874 VDBG (0, "VCL<%d>: configured api prefix (%s) and filename (%s) "
1875 "from " VPPCOM_ENV_API_PREFIX "!",
1876 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
1878 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1881 u32 ns_id_vec_len = strlen (env_var_str);
1883 vec_reset_length (vcm->cfg.namespace_id);
1884 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1885 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1887 VDBG (0, "VCL<%d>: configured namespace_id (%v) from "
1888 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1889 vcm->cfg.namespace_id);
1891 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1895 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1896 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
1897 "specified in the environment variable "
1898 VPPCOM_ENV_APP_NAMESPACE_SECRET
1899 " (%s)!\n", getpid (), env_var_str);
1902 vcm->cfg.namespace_secret = tmp;
1903 VDBG (0, "VCL<%d>: configured namespace secret (%lu) from "
1904 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
1905 vcm->cfg.namespace_secret);
1908 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
1910 vcm->cfg.app_proxy_transport_tcp = 1;
1911 VDBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
1912 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
1913 vcm->cfg.app_proxy_transport_tcp);
1915 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
1917 vcm->cfg.app_proxy_transport_udp = 1;
1918 VDBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
1919 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
1920 vcm->cfg.app_proxy_transport_udp);
1922 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1924 vcm->cfg.app_scope_local = 1;
1925 VDBG (0, "VCL<%d>: configured app_scope_local (%u) from "
1926 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
1927 vcm->cfg.app_scope_local);
1929 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1931 vcm->cfg.app_scope_global = 1;
1932 VDBG (0, "VCL<%d>: configured app_scope_global (%u) from "
1933 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
1934 vcm->cfg.app_scope_global);
1937 vcm->main_cpu = os_get_thread_index ();
1938 heap = clib_mem_get_per_cpu_heap ();
1939 h = mheap_header (heap);
1941 /* make the main heap thread-safe */
1942 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1944 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1946 clib_time_init (&vcm->clib_time);
1947 vppcom_init_error_string_table ();
1948 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
1949 20 /* timeout in secs */ );
1950 clib_spinlock_init (&vcm->sessions_lockp);
1953 if (vcm->my_client_index == ~0)
1956 /* API hookup and connect to VPP */
1957 vppcom_api_hookup ();
1958 vcl_elog_init (vcm);
1959 vcm->app_state = STATE_APP_START;
1960 rv = vppcom_connect_to_vpp (app_name);
1963 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
1968 /* State event handling thread */
1970 rv = vce_start_event_thread (&(vcm->event_thread), 20);
1972 VDBG (0, "VCL<%d>: sending session enable", getpid ());
1974 rv = vppcom_app_session_enable ();
1977 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
1978 "failed!", getpid ());
1982 VDBG (0, "VCL<%d>: sending app attach", getpid ());
1984 rv = vppcom_app_attach ();
1987 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
1992 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
1993 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
2000 vppcom_app_destroy (void)
2003 f64 orig_app_timeout;
2005 if (vcm->my_client_index == ~0)
2008 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2009 getpid (), vcm->my_client_index, vcm->my_client_index);
2010 vcl_evt (VCL_EVT_DETACH, vcm);
2012 vppcom_app_detach ();
2013 orig_app_timeout = vcm->cfg.app_timeout;
2014 vcm->cfg.app_timeout = 2.0;
2015 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2016 vcm->cfg.app_timeout = orig_app_timeout;
2017 if (PREDICT_FALSE (rv))
2018 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
2019 getpid (), rv, vppcom_retval_str (rv));
2021 vcl_elog_stop (vcm);
2022 vl_client_disconnect_from_vlib ();
2023 vcm->my_client_index = ~0;
2024 vcm->app_state = STATE_APP_START;
2028 vppcom_session_create (u8 proto, u8 is_nonblocking)
2033 VCL_SESSION_LOCK ();
2034 pool_get (vcm->sessions, session);
2035 memset (session, 0, sizeof (*session));
2036 session_index = session - vcm->sessions;
2038 session->proto = proto;
2039 session->state = STATE_START;
2040 session->vpp_handle = ~0;
2043 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2045 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2047 vcl_evt (VCL_EVT_CREATE, session, proto, session->state, is_nonblocking,
2050 VCL_SESSION_UNLOCK ();
2052 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
2054 return (int) session_index;
2058 vppcom_session_close (uint32_t session_index)
2060 session_t *session = 0;
2068 session_state_t state;
2070 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2071 is_vep = session->is_vep;
2072 is_vep_session = session->is_vep_session;
2073 next_sid = session->vep.next_sid;
2074 vep_idx = session->vep.vep_idx;
2075 state = session->state;
2076 vpp_handle = session->vpp_handle;
2077 VCL_SESSION_UNLOCK ();
2079 if (VPPCOM_DEBUG > 0)
2082 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2083 "closing epoll session...",
2084 getpid (), session_index, session_index);
2086 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2087 "closing session...",
2088 getpid (), vpp_handle, session_index);
2093 while (next_sid != ~0)
2095 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2096 if (PREDICT_FALSE (rv < 0))
2097 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2098 "vep_idx %u failed! rv %d (%s)",
2099 getpid (), vpp_handle, next_sid, vep_idx,
2100 rv, vppcom_retval_str (rv));
2102 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2103 next_sid = session->vep.next_sid;
2104 VCL_SESSION_UNLOCK ();
2111 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2113 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2114 "vep_idx %u failed! rv %d (%s)",
2115 getpid (), vpp_handle, session_index,
2116 vep_idx, rv, vppcom_retval_str (rv));
2119 if (state & STATE_LISTEN)
2121 rv = vppcom_session_unbind (session_index);
2122 if (PREDICT_FALSE (rv < 0))
2123 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
2124 "failed! rv %d (%s)",
2125 getpid (), vpp_handle, session_index,
2126 rv, vppcom_retval_str (rv));
2129 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2131 rv = vppcom_session_disconnect (session_index);
2132 if (PREDICT_FALSE (rv < 0))
2133 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2134 "session disconnect failed! rv %d (%s)",
2135 getpid (), vpp_handle, session_index,
2136 rv, vppcom_retval_str (rv));
2140 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2141 vpp_handle = session->vpp_handle;
2142 if (vpp_handle != ~0)
2144 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2146 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2148 pool_put_index (vcm->sessions, session_index);
2150 VCL_SESSION_UNLOCK ();
2152 if (VPPCOM_DEBUG > 0)
2155 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2156 getpid (), session_index, session_index);
2158 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2159 getpid (), vpp_handle, session_index);
2163 vcl_evt (VCL_EVT_CLOSE, session, rv);
2169 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2171 session_t *session = 0;
2175 return VPPCOM_EINVAL;
2177 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2179 if (session->is_vep)
2181 VCL_SESSION_UNLOCK ();
2182 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2183 "bind to an epoll session!", getpid (), session_index);
2188 session->lcl_addr.is_ip4 = ep->is_ip4;
2189 session->lcl_addr.ip46 = to_ip46 (ep->is_ip4 ? IP46_TYPE_IP4 :
2190 IP46_TYPE_IP6, ep->ip);
2191 session->lcl_port = ep->port;
2193 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
2194 "proto %s", getpid (), session_index,
2195 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2196 format_ip46_address, &session->lcl_addr.ip46,
2197 session->lcl_addr.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2198 clib_net_to_host_u16 (session->lcl_port),
2199 session->proto ? "UDP" : "TCP");
2200 vcl_evt (VCL_EVT_BIND, session);
2201 VCL_SESSION_UNLOCK ();
2207 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2209 session_t *listen_session = 0;
2210 u64 listen_vpp_handle;
2213 if (q_len == 0 || q_len == ~0)
2214 q_len = vcm->cfg.listen_queue_size;
2216 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2218 if (listen_session->is_vep)
2220 VCL_SESSION_UNLOCK ();
2221 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2222 "epoll session!", getpid (), listen_session_index);
2227 listen_vpp_handle = listen_session->vpp_handle;
2228 if (listen_session->state & STATE_LISTEN)
2230 VCL_SESSION_UNLOCK ();
2231 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
2232 getpid (), listen_vpp_handle, listen_session_index);
2237 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
2238 "request...", getpid (), listen_vpp_handle, listen_session_index);
2240 vppcom_send_bind_sock (listen_session, listen_session_index);
2241 VCL_SESSION_UNLOCK ();
2243 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2244 vcm->cfg.session_timeout);
2246 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2247 if (PREDICT_FALSE (retval))
2249 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
2250 "returning %d (%s)", getpid (), listen_session->vpp_handle,
2251 listen_session_index, retval, vppcom_retval_str (retval));
2252 VCL_SESSION_UNLOCK ();
2257 VCL_ACCEPT_FIFO_LOCK ();
2258 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2259 VCL_ACCEPT_FIFO_UNLOCK ();
2261 VCL_SESSION_UNLOCK ();
2268 validate_args_session_accept_ (session_t * listen_session)
2270 u32 listen_session_index = listen_session - vcm->sessions;
2272 /* Input validation - expects spinlock on sessions_lockp */
2273 if (listen_session->is_vep)
2275 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2276 "epoll session!", getpid (), listen_session_index);
2277 return VPPCOM_EBADFD;
2280 if (listen_session->state != STATE_LISTEN)
2282 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2283 "not in listen state! state 0x%x (%s)", getpid (),
2284 listen_session->vpp_handle, listen_session_index,
2285 listen_session->state,
2286 vppcom_session_state_str (listen_session->state));
2287 return VPPCOM_EBADFD;
2293 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2296 session_t *listen_session = 0;
2297 session_t *client_session = 0;
2298 u32 client_session_index = ~0;
2300 u64 listen_vpp_handle;
2301 vce_event_handler_reg_t *reg;
2303 vce_event_connect_request_t *result;
2306 int millisecond_timeout = 1;
2307 int hours_timeout = 20 * 60 * 60;
2309 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2310 listen_vpp_handle = listen_session->vpp_handle; // For debugging
2312 rv = validate_args_session_accept_ (listen_session);
2315 VCL_SESSION_UNLOCK ();
2319 /* Using an aggressive timer of 1ms and a generous timer of
2320 * 20 hours, we can implement a blocking and non-blocking listener
2321 * as both event and time driven */
2322 gettimeofday (&tv, NULL);
2323 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
2324 ts.tv_sec = tv.tv_sec;
2326 /* Predict that the Listener is blocking more often than not */
2327 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
2328 VCL_SESS_ATTR_NONBLOCK)))
2329 ts.tv_sec += hours_timeout;
2331 VCL_SESSION_UNLOCK ();
2333 /* Register handler for connect_request event on listen_session_index */
2334 vce_event_key_t evk;
2335 evk.session_index = listen_session_index;
2336 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2337 reg = vce_register_handler (&vcm->event_thread, &evk,
2338 vce_connect_request_handler_fn, 0);
2340 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2341 pthread_mutex_lock (®->handler_lock);
2344 VCL_EVENTS_UNLOCK ();
2345 rv = pthread_cond_timedwait (®->handler_cond,
2346 ®->handler_lock, &ts);
2347 if (rv == ETIMEDOUT)
2353 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2355 result = vce_get_event_data (ev, sizeof (*result));
2356 client_session_index = result->accepted_session_index;
2357 VCL_EVENTS_UNLOCK ();
2359 /* Remove from the FIFO used to service epoll */
2360 VCL_ACCEPT_FIFO_LOCK ();
2361 if (clib_fifo_elts (vcm->client_session_index_fifo))
2363 u32 tmp_client_session_index;
2364 clib_fifo_sub1 (vcm->client_session_index_fifo,
2365 tmp_client_session_index);
2366 /* It wasn't ours... put it back ... */
2367 if (tmp_client_session_index != client_session_index)
2368 clib_fifo_add1 (vcm->client_session_index_fifo,
2369 tmp_client_session_index);
2371 VCL_ACCEPT_FIFO_UNLOCK ();
2373 VCL_SESSION_LOCK ();
2375 rv = vppcom_session_at_index (client_session_index, &client_session);
2376 if (PREDICT_FALSE (rv))
2378 rv = VPPCOM_ECONNABORTED;
2379 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2380 "lookup failed! returning %d (%s)", getpid (),
2381 listen_vpp_handle, listen_session_index,
2382 client_session_index, rv, vppcom_retval_str (rv));
2386 if (flags & O_NONBLOCK)
2387 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2389 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2391 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2392 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2393 getpid (), listen_vpp_handle, listen_session_index,
2394 client_session->vpp_handle, client_session_index,
2395 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2396 VCL_SESS_ATTR_NONBLOCK));
2400 ep->is_ip4 = client_session->peer_addr.is_ip4;
2401 ep->port = client_session->peer_port;
2402 if (client_session->peer_addr.is_ip4)
2403 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2404 sizeof (ip4_address_t));
2406 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2407 sizeof (ip6_address_t));
2410 vppcom_send_accept_session_reply (client_session->vpp_handle,
2411 client_session->client_context,
2412 0 /* retval OK */ );
2414 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx,"
2415 " sid %u connection from peer %s address %U port %u to local %s address"
2417 getpid (), listen_vpp_handle,
2418 listen_session_index, client_session->vpp_handle,
2419 client_session_index,
2420 client_session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2421 format_ip46_address, &client_session->peer_addr.ip46,
2422 client_session->peer_addr.is_ip4 ?
2423 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2424 clib_net_to_host_u16 (client_session->peer_port),
2425 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2426 format_ip46_address, &client_session->lcl_addr.ip46,
2427 client_session->lcl_addr.is_ip4 ?
2428 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2429 clib_net_to_host_u16 (client_session->lcl_port));
2430 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
2431 client_session_index);
2432 VCL_SESSION_UNLOCK ();
2434 rv = (int) client_session_index;
2435 vce_clear_event (&vcm->event_thread, reg->ev_idx);
2436 if (vcm->session_io_thread.io_sessions_lockp)
2438 /* Throw this new accepted session index into the rx poll thread pool */
2439 VCL_IO_SESSIONS_LOCK ();
2440 u32 *active_session_index;
2441 pool_get (vcm->session_io_thread.active_session_indexes,
2442 active_session_index);
2443 *active_session_index = client_session_index;
2444 VCL_IO_SESSIONS_UNLOCK ();
2447 vce_unregister_handler (&vcm->event_thread, reg);
2448 pthread_mutex_unlock (®->handler_lock);
2455 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2457 session_t *session = 0;
2459 int rv, retval = VPPCOM_OK;
2461 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2463 if (PREDICT_FALSE (session->is_vep))
2465 VCL_SESSION_UNLOCK ();
2466 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2467 "connect on an epoll session!", getpid (), session_index);
2472 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2474 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
2475 "connected to %s %U port %d proto %s, state 0x%x (%s)",
2476 getpid (), session->vpp_handle, session_index,
2477 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2478 format_ip46_address,
2479 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
2480 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2481 clib_net_to_host_u16 (session->peer_port),
2482 session->proto ? "UDP" : "TCP", session->state,
2483 vppcom_session_state_str (session->state));
2485 VCL_SESSION_UNLOCK ();
2489 session->peer_addr.is_ip4 = server_ep->is_ip4;
2490 if (session->peer_addr.is_ip4)
2491 clib_memcpy (&session->peer_addr.ip46.ip4, server_ep->ip,
2492 sizeof (ip4_address_t));
2494 clib_memcpy (&session->peer_addr.ip46.ip6, server_ep->ip,
2495 sizeof (ip6_address_t));
2496 session->peer_port = server_ep->port;
2498 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
2500 getpid (), session->vpp_handle, session_index,
2501 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2502 format_ip46_address,
2503 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
2504 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2505 clib_net_to_host_u16 (session->peer_port),
2506 session->proto ? "UDP" : "TCP");
2508 vppcom_send_connect_sock (session, session_index);
2509 VCL_SESSION_UNLOCK ();
2512 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2513 vcm->cfg.session_timeout);
2515 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2516 vpp_handle = session->vpp_handle;
2517 VCL_SESSION_UNLOCK ();
2520 if (PREDICT_FALSE (retval))
2523 if (VPPCOM_DEBUG > 0)
2526 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
2527 "failed! returning %d (%s)", getpid (), vpp_handle,
2528 session_index, rv, vppcom_retval_str (rv));
2530 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2531 "returning %d (%s)", getpid (),
2532 session_index, rv, vppcom_retval_str (rv));
2536 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
2537 getpid (), vpp_handle, session_index);
2543 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2546 session_t *session = 0;
2547 svm_fifo_t *rx_fifo;
2554 session_state_t state;
2558 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2560 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
2561 rx_fifo = session->rx_fifo;
2562 state = session->state;
2563 vpp_handle = session->vpp_handle;
2565 if (PREDICT_FALSE (session->is_vep))
2567 VCL_SESSION_UNLOCK ();
2568 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2569 "read from an epoll session!", getpid (), session_index);
2574 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
2576 VCL_SESSION_UNLOCK ();
2577 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
2579 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
2580 "state 0x%x (%s), returning %d (%s)",
2581 getpid (), vpp_handle, session_index, state,
2582 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
2586 VCL_SESSION_UNLOCK ();
2591 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2593 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2595 while (!is_nonblocking && (n_read <= 0));
2599 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2601 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
2602 (EPOLLET | EPOLLIN));
2604 session->vep.et_mask |= EPOLLIN;
2606 if (state & STATE_CLOSE_ON_EMPTY)
2608 rv = VPPCOM_ECONNRESET;
2610 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2611 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2612 "returning %d (%s)",
2613 getpid (), session->vpp_handle, session_index,
2614 state, vppcom_session_state_str (state),
2616 vppcom_session_state_str (STATE_DISCONNECT), rv,
2617 vppcom_retval_str (rv));
2619 session->state = STATE_DISCONNECT;
2624 VCL_SESSION_UNLOCK ();
2629 if (VPPCOM_DEBUG > 2)
2632 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
2633 "from (%p)", getpid (), vpp_handle,
2634 session_index, n_read, rx_fifo);
2636 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
2637 "returning %d (%s)", getpid (), vpp_handle,
2638 session_index, rv, vppcom_retval_str (rv));
2645 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
2647 return (vppcom_session_read_internal (session_index, buf, n, 0));
2651 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2653 return (vppcom_session_read_internal (session_index, buf, n, 1));
2657 vppcom_session_read_ready (session_t * session, u32 session_index)
2662 session_state_t state = session->state;
2663 u64 vpp_handle = session->vpp_handle;
2665 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2666 if (PREDICT_FALSE (session->is_vep))
2668 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
2669 "epoll session!", getpid (), session_index);
2674 if (session->state & STATE_LISTEN)
2676 VCL_ACCEPT_FIFO_LOCK ();
2677 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2678 VCL_ACCEPT_FIFO_UNLOCK ();
2682 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
2684 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2687 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
2688 " state 0x%x (%s), returning %d (%s)",
2689 getpid (), vpp_handle, session_index,
2690 state, vppcom_session_state_str (state),
2691 rv, vppcom_retval_str (rv));
2695 ready = svm_fifo_max_dequeue (session->rx_fifo);
2701 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2703 session->vep.et_mask |= EPOLLIN;
2705 if (state & STATE_CLOSE_ON_EMPTY)
2707 rv = VPPCOM_ECONNRESET;
2709 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2710 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2711 "returning %d (%s)",
2712 getpid (), session_index, vpp_handle,
2713 state, vppcom_session_state_str (state),
2715 vppcom_session_state_str (STATE_DISCONNECT), rv,
2716 vppcom_retval_str (rv));
2717 session->state = STATE_DISCONNECT;
2723 if (vcm->app_event_queue->cursize &&
2724 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
2726 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
2727 session_fifo_event_t e;
2729 for (i = 0; i < n_to_dequeue; i++)
2730 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
2732 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
2739 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
2741 session_t *session = 0;
2742 svm_fifo_t *tx_fifo = 0;
2744 session_fifo_event_t evt;
2745 session_state_t state;
2746 int rv, n_write, is_nonblocking;
2752 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2754 tx_fifo = session->tx_fifo;
2755 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
2756 vpp_handle = session->vpp_handle;
2757 state = session->state;
2759 if (PREDICT_FALSE (session->is_vep))
2761 VCL_SESSION_UNLOCK ();
2762 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2763 "cannot write to an epoll session!",
2764 getpid (), vpp_handle, session_index);
2770 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2773 ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
2776 VCL_SESSION_UNLOCK ();
2777 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
2779 getpid (), vpp_handle, session_index,
2780 state, vppcom_session_state_str (state));
2784 VCL_SESSION_UNLOCK ();
2788 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
2790 while (!is_nonblocking && (n_write <= 0));
2792 /* If event wasn't set, add one
2794 * To reduce context switching, can check if an
2795 * event is already there for this event_key, but for now
2796 * this will suffice. */
2798 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
2800 /* Fabricate TX event, send to vpp */
2802 evt.event_type = FIFO_EVENT_APP_TX;
2804 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2805 q = session->vpp_event_queue;
2807 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
2808 VCL_SESSION_UNLOCK ();
2809 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: added FIFO_EVENT_APP_TX "
2810 "to vpp_event_q %p, n_write %d", getpid (),
2811 vpp_handle, session_index, q, n_write);
2816 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2818 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2819 (EPOLLET | EPOLLOUT));
2821 session->vep.et_mask |= EPOLLOUT;
2823 if (session->state & STATE_CLOSE_ON_EMPTY)
2825 rv = VPPCOM_ECONNRESET;
2827 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2828 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2829 "returning %d (%s)",
2830 getpid (), session->vpp_handle, session_index,
2832 vppcom_session_state_str (session->state),
2834 vppcom_session_state_str (STATE_DISCONNECT), rv,
2835 vppcom_retval_str (rv));
2837 session->state = STATE_DISCONNECT;
2842 VCL_SESSION_UNLOCK ();
2847 if (VPPCOM_DEBUG > 2)
2850 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2851 "FIFO-FULL (%p)", getpid (), vpp_handle,
2852 session_index, tx_fifo);
2854 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2855 "wrote %d bytes tx-fifo: (%p)", getpid (),
2856 vpp_handle, session_index, n_write, tx_fifo);
2863 vppcom_session_write_ready (session_t * session, u32 session_index)
2871 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2872 if (PREDICT_FALSE (session->is_vep))
2874 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2875 "cannot write to an epoll session!",
2876 getpid (), session->vpp_handle, session_index);
2881 if (PREDICT_FALSE (session->state & STATE_LISTEN))
2883 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2884 "cannot write to a listen session!",
2885 getpid (), session->vpp_handle, session_index);
2890 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
2892 session_state_t state = session->state;
2894 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
2896 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2897 "session is not open! state 0x%x (%s), "
2898 "returning %d (%s)", getpid (), session->vpp_handle,
2900 state, vppcom_session_state_str (state),
2901 rv, vppcom_retval_str (rv));
2905 ready = svm_fifo_max_enqueue (session->tx_fifo);
2907 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
2908 getpid (), session->vpp_handle, session_index, session->tx_fifo,
2913 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2914 (EPOLLET | EPOLLOUT));
2916 session->vep.et_mask |= EPOLLOUT;
2918 if (session->state & STATE_CLOSE_ON_EMPTY)
2920 rv = VPPCOM_ECONNRESET;
2922 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2923 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2924 "returning %d (%s)", getpid (),
2925 session->vpp_handle, session_index,
2927 vppcom_session_state_str (session->state),
2929 vppcom_session_state_str (STATE_DISCONNECT), rv,
2930 vppcom_retval_str (rv));
2931 session->state = STATE_DISCONNECT;
2941 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2942 unsigned long *write_map, unsigned long *except_map,
2943 double time_to_wait)
2946 session_t *session = 0;
2947 int rv, bits_set = 0;
2948 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2949 u32 minbits = clib_max (n_bits, BITS (uword));
2951 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2953 if (n_bits && read_map)
2955 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2956 clib_memcpy (vcm->rd_bitmap, read_map,
2957 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
2958 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
2960 if (n_bits && write_map)
2962 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2963 clib_memcpy (vcm->wr_bitmap, write_map,
2964 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
2965 memset (write_map, 0,
2966 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
2968 if (n_bits && except_map)
2970 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2971 clib_memcpy (vcm->ex_bitmap, except_map,
2972 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
2973 memset (except_map, 0,
2974 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
2984 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2987 rv = vppcom_session_at_index (session_index, &session);
2990 VCL_SESSION_UNLOCK();
2991 VDBG (1, "VCL<%d>: session %d specified in read_map is"
2992 " closed.", getpid (),
2994 bits_set = VPPCOM_EBADFD;
2997 if (session->state & STATE_LISTEN)
2999 vce_event_handler_reg_t *reg = 0;
3000 vce_event_key_t evk;
3002 /* Check if handler already registered for this
3004 * If not, register handler for connect_request event
3005 * on listen_session_index
3007 evk.session_index = session_index;
3008 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3009 reg = vce_get_event_handler (&vcm->event_thread, &evk);
3011 reg = vce_register_handler (&vcm->event_thread, &evk,
3012 vce_poll_wait_connect_request_handler_fn,
3013 0 /* No callback args */);
3014 rv = vppcom_session_read_ready (session, session_index);
3017 vce_unregister_handler (&vcm->event_thread, reg);
3021 rv = vppcom_session_read_ready (session, session_index);
3022 VCL_SESSION_UNLOCK();
3023 if (except_map && vcm->ex_bitmap &&
3024 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3027 clib_bitmap_set_no_check (except_map, session_index, 1);
3032 clib_bitmap_set_no_check (read_map, session_index, 1);
3040 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3043 rv = vppcom_session_at_index (session_index, &session);
3046 VCL_SESSION_UNLOCK();
3047 VDBG (0, "VCL<%d>: session %d specified in "
3048 "write_map is closed.", getpid (),
3050 bits_set = VPPCOM_EBADFD;
3054 rv = vppcom_session_write_ready (session, session_index);
3055 VCL_SESSION_UNLOCK();
3056 if (write_map && (rv > 0))
3058 clib_bitmap_set_no_check (write_map, session_index, 1);
3066 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3069 rv = vppcom_session_at_index (session_index, &session);
3072 VCL_SESSION_UNLOCK();
3073 VDBG (1, "VCL<%d>: session %d specified in except_map "
3074 "is closed.", getpid (),
3076 bits_set = VPPCOM_EBADFD;
3080 rv = vppcom_session_read_ready (session, session_index);
3081 VCL_SESSION_UNLOCK();
3084 clib_bitmap_set_no_check (except_map, session_index, 1);
3092 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3099 vep_verify_epoll_chain (u32 vep_idx)
3102 vppcom_epoll_t *vep;
3106 if (VPPCOM_DEBUG <= 1)
3109 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3110 rv = vppcom_session_at_index (vep_idx, &session);
3111 if (PREDICT_FALSE (rv))
3113 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3114 getpid (), vep_idx);
3117 if (PREDICT_FALSE (!session->is_vep))
3119 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3120 getpid (), vep_idx);
3123 vep = &session->vep;
3124 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3127 " is_vep_session = %u\n"
3128 " next_sid = 0x%x (%u)\n"
3129 " wait_cont_idx = 0x%x (%u)\n"
3130 "}\n", getpid (), vep_idx,
3131 session->is_vep, session->is_vep_session,
3132 vep->next_sid, vep->next_sid,
3133 session->wait_cont_idx, session->wait_cont_idx);
3135 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3137 rv = vppcom_session_at_index (sid, &session);
3138 if (PREDICT_FALSE (rv))
3140 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3143 if (PREDICT_FALSE (session->is_vep))
3144 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3145 getpid (), vep_idx);
3146 else if (PREDICT_FALSE (!session->is_vep_session))
3148 clib_warning ("VCL<%d>: ERROR: session (%u) "
3149 "is not a vep session!", getpid (), sid);
3152 vep = &session->vep;
3153 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3154 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3155 "vep_idx (%u)!", getpid (),
3156 sid, session->vep.vep_idx, vep_idx);
3157 if (session->is_vep_session)
3159 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3161 " next_sid = 0x%x (%u)\n"
3162 " prev_sid = 0x%x (%u)\n"
3163 " vep_idx = 0x%x (%u)\n"
3164 " ev.events = 0x%x\n"
3165 " ev.data.u64 = 0x%llx\n"
3169 vep->next_sid, vep->next_sid,
3170 vep->prev_sid, vep->prev_sid,
3171 vep->vep_idx, vep->vep_idx,
3172 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3177 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3178 getpid (), vep_idx);
3182 vppcom_epoll_create (void)
3184 session_t *vep_session;
3187 VCL_SESSION_LOCK ();
3188 pool_get (vcm->sessions, vep_session);
3189 memset (vep_session, 0, sizeof (*vep_session));
3190 vep_idx = vep_session - vcm->sessions;
3192 vep_session->is_vep = 1;
3193 vep_session->vep.vep_idx = ~0;
3194 vep_session->vep.next_sid = ~0;
3195 vep_session->vep.prev_sid = ~0;
3196 vep_session->wait_cont_idx = ~0;
3197 vep_session->vpp_handle = ~0;
3198 vep_session->poll_reg = 0;
3200 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
3201 VCL_SESSION_UNLOCK ();
3203 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
3204 getpid (), vep_idx, vep_idx);
3210 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3211 struct epoll_event *event)
3213 session_t *vep_session;
3217 if (vep_idx == session_index)
3219 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3220 getpid (), vep_idx);
3221 return VPPCOM_EINVAL;
3224 VCL_SESSION_LOCK ();
3225 rv = vppcom_session_at_index (vep_idx, &vep_session);
3226 if (PREDICT_FALSE (rv))
3228 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3231 if (PREDICT_FALSE (!vep_session->is_vep))
3233 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3234 getpid (), vep_idx);
3239 ASSERT (vep_session->vep.vep_idx == ~0);
3240 ASSERT (vep_session->vep.prev_sid == ~0);
3242 rv = vppcom_session_at_index (session_index, &session);
3243 if (PREDICT_FALSE (rv))
3245 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
3246 getpid (), session_index);
3249 if (PREDICT_FALSE (session->is_vep))
3251 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3259 if (PREDICT_FALSE (!event))
3261 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3262 "epoll_event structure!", getpid ());
3266 if (vep_session->vep.next_sid != ~0)
3268 session_t *next_session;
3269 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3271 if (PREDICT_FALSE (rv))
3273 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3274 "vep.next_sid (%u) on vep_idx (%u)!",
3275 getpid (), vep_session->vep.next_sid, vep_idx);
3278 ASSERT (next_session->vep.prev_sid == vep_idx);
3279 next_session->vep.prev_sid = session_index;
3281 session->vep.next_sid = vep_session->vep.next_sid;
3282 session->vep.prev_sid = vep_idx;
3283 session->vep.vep_idx = vep_idx;
3284 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3285 session->vep.ev = *event;
3286 session->is_vep = 0;
3287 session->is_vep_session = 1;
3288 vep_session->vep.next_sid = session_index;
3290 /* VCL Event Register handler */
3291 if (session->state & STATE_LISTEN)
3293 /* Register handler for connect_request event on listen_session_index */
3294 vce_event_key_t evk;
3295 evk.session_index = session_index;
3296 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3297 vep_session->poll_reg =
3298 vce_register_handler (&vcm->event_thread, &evk,
3299 vce_poll_wait_connect_request_handler_fn,
3300 0 /* No callback args */ );
3302 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3303 "sid %u, events 0x%x, data 0x%llx!",
3304 getpid (), vep_idx, session_index,
3305 event->events, event->data.u64);
3306 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
3310 if (PREDICT_FALSE (!event))
3312 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3313 "epoll_event structure!", getpid ());
3317 else if (PREDICT_FALSE (!session->is_vep_session))
3319 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3320 "not a vep session!", getpid (), session_index);
3324 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3326 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3327 "vep_idx (%u) != vep_idx (%u)!",
3328 getpid (), session_index,
3329 session->vep.vep_idx, vep_idx);
3333 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3334 session->vep.ev = *event;
3335 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3336 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3341 if (PREDICT_FALSE (!session->is_vep_session))
3343 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3344 "not a vep session!", getpid (), session_index);
3348 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3350 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3351 "vep_idx (%u) != vep_idx (%u)!",
3352 getpid (), session_index,
3353 session->vep.vep_idx, vep_idx);
3358 /* VCL Event Un-register handler */
3359 if ((session->state & STATE_LISTEN) && vep_session->poll_reg)
3361 (void) vce_unregister_handler (&vcm->event_thread,
3362 vep_session->poll_reg);
3365 vep_session->wait_cont_idx =
3366 (vep_session->wait_cont_idx == session_index) ?
3367 session->vep.next_sid : vep_session->wait_cont_idx;
3369 if (session->vep.prev_sid == vep_idx)
3370 vep_session->vep.next_sid = session->vep.next_sid;
3373 session_t *prev_session;
3374 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3375 if (PREDICT_FALSE (rv))
3377 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3378 "vep.prev_sid (%u) on sid (%u)!",
3379 getpid (), session->vep.prev_sid, session_index);
3382 ASSERT (prev_session->vep.next_sid == session_index);
3383 prev_session->vep.next_sid = session->vep.next_sid;
3385 if (session->vep.next_sid != ~0)
3387 session_t *next_session;
3388 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3389 if (PREDICT_FALSE (rv))
3391 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3392 "vep.next_sid (%u) on sid (%u)!",
3393 getpid (), session->vep.next_sid, session_index);
3396 ASSERT (next_session->vep.prev_sid == session_index);
3397 next_session->vep.prev_sid = session->vep.prev_sid;
3400 memset (&session->vep, 0, sizeof (session->vep));
3401 session->vep.next_sid = ~0;
3402 session->vep.prev_sid = ~0;
3403 session->vep.vep_idx = ~0;
3404 session->is_vep_session = 0;
3405 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3406 getpid (), vep_idx, session_index);
3407 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
3411 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
3415 vep_verify_epoll_chain (vep_idx);
3418 VCL_SESSION_UNLOCK ();
3423 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3424 int maxevents, double wait_for_time)
3426 session_t *vep_session;
3428 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3429 u32 keep_trying = 1;
3431 u32 vep_next_sid, wait_cont_idx;
3434 if (PREDICT_FALSE (maxevents <= 0))
3436 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
3437 getpid (), maxevents);
3438 return VPPCOM_EINVAL;
3440 memset (events, 0, sizeof (*events) * maxevents);
3442 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
3443 vep_next_sid = vep_session->vep.next_sid;
3444 is_vep = vep_session->is_vep;
3445 wait_cont_idx = vep_session->wait_cont_idx;
3446 VCL_SESSION_UNLOCK ();
3448 if (PREDICT_FALSE (!is_vep))
3450 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3451 getpid (), vep_idx);
3455 if (PREDICT_FALSE (vep_next_sid == ~0))
3457 VDBG (1, "VCL<%d>: WARNING: vep_idx (%u) is empty!",
3458 getpid (), vep_idx);
3468 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3469 sid != ~0; sid = next_sid)
3471 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3472 u8 add_event, is_vep_session;
3474 u64 session_ev_data;
3476 VCL_SESSION_LOCK_AND_GET (sid, &session);
3477 next_sid = session->vep.next_sid;
3478 session_events = session->vep.ev.events;
3479 et_mask = session->vep.et_mask;
3480 is_vep = session->is_vep;
3481 is_vep_session = session->is_vep_session;
3482 session_vep_idx = session->vep.vep_idx;
3483 session_ev_data = session->vep.ev.data.u64;
3485 VCL_SESSION_UNLOCK ();
3487 if (PREDICT_FALSE (is_vep))
3489 VDBG (0, "VCL<%d>: ERROR: sid (%u) is a vep!",
3490 getpid (), vep_idx);
3494 if (PREDICT_FALSE (!is_vep_session))
3496 VDBG (0, "VCL<%d>: ERROR: session (%u) is not "
3497 "a vep session!", getpid (), sid);
3501 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3503 clib_warning ("VCL<%d>: ERROR: session (%u) "
3504 "vep_idx (%u) != vep_idx (%u)!",
3505 getpid (), sid, session_vep_idx, vep_idx);
3510 add_event = clear_et_mask = 0;
3512 if (EPOLLIN & session_events)
3514 VCL_SESSION_LOCK_AND_GET (sid, &session);
3515 ready = vppcom_session_read_ready (session, sid);
3516 VCL_SESSION_UNLOCK ();
3517 if ((ready > 0) && (EPOLLIN & et_mask))
3520 events[num_ev].events |= EPOLLIN;
3521 if (((EPOLLET | EPOLLIN) & session_events) ==
3522 (EPOLLET | EPOLLIN))
3523 clear_et_mask |= EPOLLIN;
3530 case VPPCOM_ECONNRESET:
3531 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3535 events[num_ev].events |= EPOLLERR;
3541 if (EPOLLOUT & session_events)
3543 VCL_SESSION_LOCK_AND_GET (sid, &session);
3544 ready = vppcom_session_write_ready (session, sid);
3545 VCL_SESSION_UNLOCK ();
3546 if ((ready > 0) && (EPOLLOUT & et_mask))
3549 events[num_ev].events |= EPOLLOUT;
3550 if (((EPOLLET | EPOLLOUT) & session_events) ==
3551 (EPOLLET | EPOLLOUT))
3552 clear_et_mask |= EPOLLOUT;
3559 case VPPCOM_ECONNRESET:
3560 events[num_ev].events |= EPOLLHUP;
3564 events[num_ev].events |= EPOLLERR;
3572 events[num_ev].data.u64 = session_ev_data;
3573 if (EPOLLONESHOT & session_events)
3575 VCL_SESSION_LOCK_AND_GET (sid, &session);
3576 session->vep.ev.events = 0;
3577 VCL_SESSION_UNLOCK ();
3580 if (num_ev == maxevents)
3582 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
3583 vep_session->wait_cont_idx = next_sid;
3584 VCL_SESSION_UNLOCK ();
3588 if (wait_cont_idx != ~0)
3591 next_sid = vep_next_sid;
3592 else if (next_sid == wait_cont_idx)
3596 if (wait_for_time != -1)
3597 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3599 while ((num_ev == 0) && keep_trying);
3601 if (wait_cont_idx != ~0)
3603 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
3604 vep_session->wait_cont_idx = ~0;
3605 VCL_SESSION_UNLOCK ();
3608 return (rv != VPPCOM_OK) ? rv : num_ev;
3612 vppcom_session_attr (uint32_t session_index, uint32_t op,
3613 void *buffer, uint32_t * buflen)
3617 u32 *flags = buffer;
3618 vppcom_endpt_t *ep = buffer;
3620 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3626 case VPPCOM_ATTR_GET_NREAD:
3627 rv = vppcom_session_read_ready (session, session_index);
3628 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
3632 case VPPCOM_ATTR_GET_NWRITE:
3633 rv = vppcom_session_write_ready (session, session_index);
3634 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3635 getpid (), session_index, rv);
3638 case VPPCOM_ATTR_GET_FLAGS:
3639 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
3641 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
3642 VCL_SESS_ATTR_NONBLOCK));
3643 *buflen = sizeof (*flags);
3644 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
3645 "is_nonblocking = %u", getpid (),
3646 session_index, *flags,
3647 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
3653 case VPPCOM_ATTR_SET_FLAGS:
3654 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
3656 if (*flags & O_NONBLOCK)
3657 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
3659 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
3661 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
3662 " is_nonblocking = %u",
3663 getpid (), session_index, *flags,
3664 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
3670 case VPPCOM_ATTR_GET_PEER_ADDR:
3671 if (PREDICT_TRUE (buffer && buflen &&
3672 (*buflen >= sizeof (*ep)) && ep->ip))
3674 ep->is_ip4 = session->peer_addr.is_ip4;
3675 ep->port = session->peer_port;
3676 if (session->peer_addr.is_ip4)
3677 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3678 sizeof (ip4_address_t));
3680 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3681 sizeof (ip6_address_t));
3682 *buflen = sizeof (*ep);
3683 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
3684 "addr = %U, port %u", getpid (),
3685 session_index, ep->is_ip4, format_ip46_address,
3686 &session->peer_addr.ip46,
3687 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3688 clib_net_to_host_u16 (ep->port));
3694 case VPPCOM_ATTR_GET_LCL_ADDR:
3695 if (PREDICT_TRUE (buffer && buflen &&
3696 (*buflen >= sizeof (*ep)) && ep->ip))
3698 ep->is_ip4 = session->lcl_addr.is_ip4;
3699 ep->port = session->lcl_port;
3700 if (session->lcl_addr.is_ip4)
3701 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3702 sizeof (ip4_address_t));
3704 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3705 sizeof (ip6_address_t));
3706 *buflen = sizeof (*ep);
3707 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
3708 " addr = %U port %d", getpid (),
3709 session_index, ep->is_ip4, format_ip46_address,
3710 &session->lcl_addr.ip46,
3711 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3712 clib_net_to_host_u16 (ep->port));
3718 case VPPCOM_ATTR_GET_LIBC_EPFD:
3719 rv = session->libc_epfd;
3720 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
3724 case VPPCOM_ATTR_SET_LIBC_EPFD:
3725 if (PREDICT_TRUE (buffer && buflen &&
3726 (*buflen == sizeof (session->libc_epfd))))
3728 session->libc_epfd = *(int *) buffer;
3729 *buflen = sizeof (session->libc_epfd);
3731 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
3732 "buflen %d", getpid (), session->libc_epfd, *buflen);
3738 case VPPCOM_ATTR_GET_PROTOCOL:
3739 if (buffer && buflen && (*buflen >= sizeof (int)))
3741 *(int *) buffer = session->proto;
3742 *buflen = sizeof (int);
3744 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3745 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
3752 case VPPCOM_ATTR_GET_LISTEN:
3753 if (buffer && buflen && (*buflen >= sizeof (int)))
3755 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3756 VCL_SESS_ATTR_LISTEN);
3757 *buflen = sizeof (int);
3759 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
3760 getpid (), *(int *) buffer, *buflen);
3766 case VPPCOM_ATTR_GET_ERROR:
3767 if (buffer && buflen && (*buflen >= sizeof (int)))
3769 *(int *) buffer = 0;
3770 *buflen = sizeof (int);
3772 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3773 getpid (), *(int *) buffer, *buflen);
3779 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3780 if (buffer && buflen && (*buflen >= sizeof (u32)))
3784 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
3785 session->tx_fifo ? session->tx_fifo->nitems :
3786 vcm->cfg.tx_fifo_size);
3787 *buflen = sizeof (u32);
3789 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
3790 "buflen %d, #VPP-TBD#", getpid (),
3791 *(size_t *) buffer, *(size_t *) buffer, *buflen);
3797 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3798 if (buffer && buflen && (*buflen == sizeof (u32)))
3801 session->sndbuf_size = *(u32 *) buffer;
3802 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
3803 "buflen %d, #VPP-TBD#", getpid (),
3804 session->sndbuf_size, session->sndbuf_size, *buflen);
3810 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3811 if (buffer && buflen && (*buflen >= sizeof (u32)))
3815 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
3816 session->rx_fifo ? session->rx_fifo->nitems :
3817 vcm->cfg.rx_fifo_size);
3818 *buflen = sizeof (u32);
3820 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
3821 "buflen %d, #VPP-TBD#", getpid (),
3822 *(size_t *) buffer, *(size_t *) buffer, *buflen);
3828 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3829 if (buffer && buflen && (*buflen == sizeof (u32)))
3832 session->rcvbuf_size = *(u32 *) buffer;
3833 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
3834 "buflen %d, #VPP-TBD#", getpid (),
3835 session->sndbuf_size, session->sndbuf_size, *buflen);
3841 case VPPCOM_ATTR_GET_REUSEADDR:
3842 if (buffer && buflen && (*buflen >= sizeof (int)))
3845 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3846 VCL_SESS_ATTR_REUSEADDR);
3847 *buflen = sizeof (int);
3849 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
3850 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3856 case VPPCOM_ATTR_SET_REUSEADDR:
3857 if (buffer && buflen && (*buflen == sizeof (int)) &&
3858 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3861 if (*(int *) buffer)
3862 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3864 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3866 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
3867 " #VPP-TBD#", getpid (),
3868 VCL_SESS_ATTR_TEST (session->attr,
3869 VCL_SESS_ATTR_REUSEADDR), *buflen);
3875 case VPPCOM_ATTR_GET_REUSEPORT:
3876 if (buffer && buflen && (*buflen >= sizeof (int)))
3879 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3880 VCL_SESS_ATTR_REUSEPORT);
3881 *buflen = sizeof (int);
3883 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
3884 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3890 case VPPCOM_ATTR_SET_REUSEPORT:
3891 if (buffer && buflen && (*buflen == sizeof (int)) &&
3892 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3895 if (*(int *) buffer)
3896 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3898 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3900 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
3901 " #VPP-TBD#", getpid (),
3902 VCL_SESS_ATTR_TEST (session->attr,
3903 VCL_SESS_ATTR_REUSEPORT), *buflen);
3909 case VPPCOM_ATTR_GET_BROADCAST:
3910 if (buffer && buflen && (*buflen >= sizeof (int)))
3913 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3914 VCL_SESS_ATTR_BROADCAST);
3915 *buflen = sizeof (int);
3917 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
3918 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3924 case VPPCOM_ATTR_SET_BROADCAST:
3925 if (buffer && buflen && (*buflen == sizeof (int)))
3928 if (*(int *) buffer)
3929 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3931 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3933 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
3934 "#VPP-TBD#", getpid (),
3935 VCL_SESS_ATTR_TEST (session->attr,
3936 VCL_SESS_ATTR_BROADCAST), *buflen);
3942 case VPPCOM_ATTR_GET_V6ONLY:
3943 if (buffer && buflen && (*buflen >= sizeof (int)))
3946 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3947 VCL_SESS_ATTR_V6ONLY);
3948 *buflen = sizeof (int);
3950 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
3951 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3957 case VPPCOM_ATTR_SET_V6ONLY:
3958 if (buffer && buflen && (*buflen == sizeof (int)))
3961 if (*(int *) buffer)
3962 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3964 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3966 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3967 "#VPP-TBD#", getpid (),
3968 VCL_SESS_ATTR_TEST (session->attr,
3969 VCL_SESS_ATTR_V6ONLY), *buflen);
3975 case VPPCOM_ATTR_GET_KEEPALIVE:
3976 if (buffer && buflen && (*buflen >= sizeof (int)))
3979 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3980 VCL_SESS_ATTR_KEEPALIVE);
3981 *buflen = sizeof (int);
3983 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3984 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
3990 case VPPCOM_ATTR_SET_KEEPALIVE:
3991 if (buffer && buflen && (*buflen == sizeof (int)))
3994 if (*(int *) buffer)
3995 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3997 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3999 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
4000 "#VPP-TBD#", getpid (),
4001 VCL_SESS_ATTR_TEST (session->attr,
4002 VCL_SESS_ATTR_KEEPALIVE), *buflen);
4008 case VPPCOM_ATTR_GET_TCP_NODELAY:
4009 if (buffer && buflen && (*buflen >= sizeof (int)))
4012 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4013 VCL_SESS_ATTR_TCP_NODELAY);
4014 *buflen = sizeof (int);
4016 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
4017 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
4023 case VPPCOM_ATTR_SET_TCP_NODELAY:
4024 if (buffer && buflen && (*buflen == sizeof (int)))
4027 if (*(int *) buffer)
4028 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
4030 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
4032 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
4033 "#VPP-TBD#", getpid (),
4034 VCL_SESS_ATTR_TEST (session->attr,
4035 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
4041 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
4042 if (buffer && buflen && (*buflen >= sizeof (int)))
4045 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4046 VCL_SESS_ATTR_TCP_KEEPIDLE);
4047 *buflen = sizeof (int);
4049 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
4050 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
4056 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
4057 if (buffer && buflen && (*buflen == sizeof (int)))
4060 if (*(int *) buffer)
4061 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
4063 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
4065 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
4066 "#VPP-TBD#", getpid (),
4067 VCL_SESS_ATTR_TEST (session->attr,
4068 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
4074 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
4075 if (buffer && buflen && (*buflen >= sizeof (int)))
4078 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4079 VCL_SESS_ATTR_TCP_KEEPINTVL);
4080 *buflen = sizeof (int);
4082 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
4083 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
4089 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
4090 if (buffer && buflen && (*buflen == sizeof (int)))
4093 if (*(int *) buffer)
4094 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
4096 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
4098 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
4099 "#VPP-TBD#", getpid (),
4100 VCL_SESS_ATTR_TEST (session->attr,
4101 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
4107 case VPPCOM_ATTR_GET_TCP_USER_MSS:
4108 if (buffer && buflen && (*buflen >= sizeof (u32)))
4111 *(u32 *) buffer = session->user_mss;
4112 *buflen = sizeof (int);
4114 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
4115 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
4121 case VPPCOM_ATTR_SET_TCP_USER_MSS:
4122 if (buffer && buflen && (*buflen == sizeof (u32)))
4125 session->user_mss = *(u32 *) buffer;
4127 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
4128 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
4140 VCL_SESSION_UNLOCK ();
4145 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
4146 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4149 session_t *session = 0;
4153 VCL_SESSION_LOCK ();
4154 rv = vppcom_session_at_index (session_index, &session);
4155 if (PREDICT_FALSE (rv))
4157 VCL_SESSION_UNLOCK ();
4158 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
4159 getpid (), session_index);
4161 VCL_SESSION_UNLOCK ();
4164 ep->is_ip4 = session->peer_addr.is_ip4;
4165 ep->port = session->peer_port;
4166 if (session->peer_addr.is_ip4)
4167 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4168 sizeof (ip4_address_t));
4170 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4171 sizeof (ip6_address_t));
4172 VCL_SESSION_UNLOCK ();
4176 rv = vppcom_session_read (session_index, buffer, buflen);
4177 else if (flags & MSG_PEEK)
4178 rv = vppcom_session_peek (session_index, buffer, buflen);
4181 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
4183 rv = VPPCOM_EAFNOSUPPORT;
4191 vppcom_session_sendto (uint32_t session_index, void *buffer,
4192 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4195 return VPPCOM_EINVAL;
4200 return VPPCOM_EINVAL;
4205 // TBD check the flags and do the right thing
4206 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
4207 getpid (), flags, flags);
4210 return (vppcom_session_write (session_index, buffer, buflen));
4214 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4216 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
4217 u32 i, keep_trying = 1;
4220 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
4221 getpid (), vp, n_sids, wait_for_time);
4224 return VPPCOM_EFAULT;
4230 for (i = 0; i < n_sids; i++)
4232 ASSERT (vp[i].revents);
4234 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
4235 VCL_SESSION_UNLOCK ();
4240 if (POLLIN & vp[i].events)
4242 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
4243 rv = vppcom_session_read_ready (session, vp[i].sid);
4244 VCL_SESSION_UNLOCK ();
4247 *vp[i].revents |= POLLIN;
4254 case VPPCOM_ECONNRESET:
4255 *vp[i].revents = POLLHUP;
4259 *vp[i].revents = POLLERR;
4266 if (POLLOUT & vp[i].events)
4268 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
4269 rv = vppcom_session_write_ready (session, vp[i].sid);
4270 VCL_SESSION_UNLOCK ();
4273 *vp[i].revents |= POLLOUT;
4280 case VPPCOM_ECONNRESET:
4281 *vp[i].revents = POLLHUP;
4285 *vp[i].revents = POLLERR;
4292 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
4295 *vp[i].revents = POLLNVAL;
4299 if (wait_for_time != -1)
4300 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4302 while ((num_ev == 0) && keep_trying);
4304 if (VPPCOM_DEBUG > 3)
4306 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
4307 for (i = 0; i < n_sids; i++)
4309 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
4310 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
4311 vp[i].events, *vp[i].revents);
4318 * VPPCOM Event Functions
4322 vppcom_session_io_thread_fn (void *arg)
4324 vppcom_session_io_thread_t *evt = (vppcom_session_io_thread_t *) arg;
4325 u32 *session_indexes = 0, *session_index;
4332 vec_reset_length (session_indexes);
4333 VCE_IO_SESSIONS_LOCK ();
4334 pool_foreach (session_index, evt->active_session_indexes, (
4341 VCE_IO_SESSIONS_UNLOCK ();
4342 if (session_indexes)
4344 for (i = 0; i < vec_len (session_indexes); ++i)
4346 VCL_SESSION_LOCK_AND_GET (session_indexes[i], &session);
4347 bytes = svm_fifo_max_dequeue (session->rx_fifo);
4348 VCL_SESSION_UNLOCK ();
4352 vppcom_ioevent_t *eio;
4358 pool_get (vcm->event_thread.vce_events, ev);
4359 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
4360 eio = vce_get_event_data (ev, sizeof (*eio));
4361 ev->evk.eid = VCL_EVENT_IOEVENT_RX_FIFO;
4362 ev->evk.session_index = session_indexes[i];
4364 eio->session_index = session_indexes[i];
4366 VCL_EVENTS_UNLOCK ();
4368 rv = vce_generate_event (&vcm->event_thread, ev_idx);
4374 ts.tv_nsec = 1000000; /* 1 millisecond */
4375 nanosleep (&ts, NULL);
4378 VCL_SESSION_UNLOCK ();
4383 vppcom_start_io_event_thread (vppcom_session_io_thread_t * evt,
4386 pthread_cond_init (&(evt->vce_io_cond), NULL);
4387 pthread_mutex_init (&(evt->vce_io_lock), NULL);
4389 clib_spinlock_init (&(evt->io_sessions_lockp));
4391 return pthread_create (&(evt->thread), NULL /* attr */ ,
4392 vppcom_session_io_thread_fn, evt);
4396 vce_registered_ioevent_handler_fn (void *arg)
4398 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
4399 vppcom_ioevent_t *eio;
4401 u32 ioevt_ndx = (u64) (reg->handler_fn_args);
4402 vppcom_session_ioevent_t *ioevent, ioevent_;
4405 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
4406 eio = vce_get_event_data (ev, sizeof (*eio));
4407 VCL_EVENTS_UNLOCK ();
4409 VCL_IO_SESSIONS_LOCK ();
4410 ioevent = pool_elt_at_index (vcm->session_io_thread.ioevents, ioevt_ndx);
4411 ioevent_ = *ioevent;
4412 VCL_IO_SESSIONS_UNLOCK ();
4413 (ioevent_.user_cb) (eio, ioevent_.user_cb_data);
4414 vce_clear_event (&vcm->event_thread, reg->ev_idx);
4417 /*TODO - Unregister check in close for this listener */
4422 vce_registered_listener_connect_handler_fn (void *arg)
4424 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
4425 vce_event_connect_request_t *ecr;
4429 session_t *new_session;
4432 vppcom_session_listener_t *session_listener =
4433 (vppcom_session_listener_t *) reg->handler_fn_args;
4436 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
4437 ecr = vce_get_event_data (ev, sizeof (*ecr));
4438 VCL_EVENTS_UNLOCK ();
4439 VCL_SESSION_LOCK_AND_GET (ecr->accepted_session_index, &new_session);
4441 ep.is_ip4 = new_session->peer_addr.is_ip4;
4442 ep.port = new_session->peer_port;
4443 if (new_session->peer_addr.is_ip4)
4444 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip4,
4445 sizeof (ip4_address_t));
4447 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip6,
4448 sizeof (ip6_address_t));
4450 vppcom_send_accept_session_reply (new_session->vpp_handle,
4451 new_session->client_context,
4452 0 /* retval OK */ );
4453 VCL_SESSION_UNLOCK ();
4455 (session_listener->user_cb) (ecr->accepted_session_index, &ep,
4456 session_listener->user_cb_data);
4458 if (vcm->session_io_thread.io_sessions_lockp)
4460 /* Throw this new accepted session index into the rx poll thread pool */
4461 VCL_IO_SESSIONS_LOCK ();
4462 u32 *active_session_index;
4463 pool_get (vcm->session_io_thread.active_session_indexes,
4464 active_session_index);
4465 *active_session_index = ecr->accepted_session_index;
4466 VCL_IO_SESSIONS_UNLOCK ();
4469 /*TODO - Unregister check in close for this listener */
4473 ASSERT (0); // If we can't get a lock or accepted session fails, lets blow up.
4477 * @brief vce_poll_wait_connect_request_handler_fn
4478 * - used by vppcom_epoll_xxxx() for listener sessions
4479 * - when a vl_api_accept_session_t_handler() generates an event
4480 * this callback is alerted and sets the fields that vppcom_epoll_wait()
4483 * @param arg - void* to be cast to vce_event_handler_reg_t*
4486 vce_poll_wait_connect_request_handler_fn (void *arg)
4488 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
4490 /* Retrieve the VCL_EVENT_CONNECT_REQ_ACCEPTED event */
4491 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
4492 vce_event_connect_request_t *ecr = vce_get_event_data (ev, sizeof (*ecr));
4494 /* Add the accepted_session_index to the FIFO */
4495 VCL_ACCEPT_FIFO_LOCK ();
4496 clib_fifo_add1 (vcm->client_session_index_fifo,
4497 ecr->accepted_session_index);
4498 VCL_ACCEPT_FIFO_UNLOCK ();
4500 /* Recycling the event. */
4503 clib_fifo_add1 (vcm->event_thread.event_index_fifo, reg->ev_idx);
4504 VCL_EVENTS_UNLOCK ();
4508 vppcom_session_register_ioevent_cb (uint32_t session_index,
4509 vppcom_session_ioevent_cb cb,
4510 uint8_t rx, void *ptr)
4513 vce_event_key_t evk;
4514 vppcom_session_ioevent_t *ioevent;
4516 if (!vcm->session_io_thread.io_sessions_lockp)
4517 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
4520 if (rv == VPPCOM_OK)
4524 /* Register handler for ioevent on session_index */
4525 VCL_IO_SESSIONS_LOCK ();
4526 pool_get (vcm->session_io_thread.ioevents, ioevent);
4527 io_evt_ndx = (void *) (ioevent - vcm->session_io_thread.ioevents);
4528 ioevent->user_cb = cb;
4529 ioevent->user_cb_data = ptr;
4530 VCL_IO_SESSIONS_UNLOCK ();
4532 evk.session_index = session_index;
4533 evk.eid = rx ? VCL_EVENT_IOEVENT_RX_FIFO : VCL_EVENT_IOEVENT_TX_FIFO;
4535 (void) vce_register_handler (&vcm->event_thread, &evk,
4536 vce_registered_ioevent_handler_fn,
4543 vppcom_session_register_listener (uint32_t session_index,
4544 vppcom_session_listener_cb cb,
4545 vppcom_session_listener_errcb
4546 errcb, uint8_t flags, int q_len, void *ptr)
4549 vce_event_key_t evk;
4550 vppcom_session_listener_t *listener_args;
4552 if (!vcm->session_io_thread.io_sessions_lockp)
4553 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
4559 rv = vppcom_session_listen (session_index, q_len);
4565 /* Register handler for connect_request event on listen_session_index */
4566 listener_args = clib_mem_alloc (sizeof (vppcom_session_listener_t)); // DAW_TODO: Use a pool instead of thrashing the memory allocator!
4567 listener_args->user_cb = cb;
4568 listener_args->user_cb_data = ptr;
4569 listener_args->user_errcb = errcb;
4571 evk.session_index = session_index;
4572 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
4573 (void) vce_register_handler (&vcm->event_thread, &evk,
4574 vce_registered_listener_connect_handler_fn,
4582 * fd.io coding-style-patch-verification: ON
4585 * eval: (c-set-style "gnu")