- /* unique segment name counter */
- u32 unique_segment_index;
-
- /* For deadman timers */
- clib_time_t clib_time;
-
- /* State of the connection, shared between msg RX thread and main thread */
- volatile app_state_t app_state;
-
- vppcom_cfg_t cfg;
-
- /* Event thread */
- vce_event_thread_t event_thread;
-
- /* IO thread */
- vppcom_session_io_thread_t session_io_thread;
-
- /* VPP Event-logger */
- elog_main_t elog_main;
- elog_track_t elog_track;
-
- /* VNET_API_ERROR_FOO -> "Foo" hash table */
- uword *error_string_by_error_number;
-} vppcom_main_t;
-
-/* NOTE: _vppcom_main is only used until the heap is allocated.
- * Do not access it directly -- use vcm which will point to
- * the heap allocated copy after init.
- */
-static vppcom_main_t _vppcom_main = {
- .debug = VPPCOM_DEBUG_INIT,
- .my_client_index = ~0
-};
-
-static vppcom_main_t *vcm = &_vppcom_main;
-
-#define VCL_SESSION_LOCK_AND_GET(I, S) \
-do { \
- clib_spinlock_lock (&vcm->sessions_lockp); \
- rv = vppcom_session_at_index (I, S); \
- if (PREDICT_FALSE (rv)) \
- { \
- clib_spinlock_unlock (&vcm->sessions_lockp); \
- clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
- getpid (), I); \
- goto done; \
- } \
-} while (0)
-
-#define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
-#define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
-
-#define VCL_IO_SESSIONS_LOCK() \
- clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
-#define VCL_IO_SESSIONS_UNLOCK() \
- clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
-
-#define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
-#define VCL_ACCEPT_FIFO_UNLOCK() \
- clib_spinlock_unlock (&(vcm->session_fifo_lockp))
-
-#define VCL_EVENTS_LOCK() \
- clib_spinlock_lock (&(vcm->event_thread.events_lockp))
-#define VCL_EVENTS_UNLOCK() \
- clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
-
-static const char *
-vppcom_app_state_str (app_state_t state)
-{
- char *st;
-
- switch (state)
- {
- case STATE_APP_START:
- st = "STATE_APP_START";
- break;
-
- case STATE_APP_CONN_VPP:
- st = "STATE_APP_CONN_VPP";
- break;
-
- case STATE_APP_ENABLED:
- st = "STATE_APP_ENABLED";
- break;
-
- case STATE_APP_ATTACHED:
- st = "STATE_APP_ATTACHED";
- break;
-
- default:
- st = "UNKNOWN_APP_STATE";
- break;
- }
-
- return st;
-}
-
-static const char *
-vppcom_session_state_str (session_state_t state)
-{
- char *st;
-
- switch (state)
- {
- case STATE_START:
- st = "STATE_START";
- break;
-
- case STATE_CONNECT:
- st = "STATE_CONNECT";
- break;
-
- case STATE_LISTEN:
- st = "STATE_LISTEN";
- break;
-
- case STATE_ACCEPT:
- st = "STATE_ACCEPT";
- break;
-
- case STATE_CLOSE_ON_EMPTY:
- st = "STATE_CLOSE_ON_EMPTY";
- break;
-
- case STATE_DISCONNECT:
- st = "STATE_DISCONNECT";
- break;
-
- case STATE_FAILED:
- st = "STATE_FAILED";
- break;
-
- default:
- st = "UNKNOWN_STATE";
- break;
- }
-
- return st;
-}
-
-
-/*
- * VPPCOM Utility Functions
- */
-
-static inline int
-vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
-{
- /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
- if (PREDICT_FALSE ((session_index == ~0) ||
- pool_is_free_index (vcm->sessions, session_index)))
- {
- clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
- getpid (), session_index);
- return VPPCOM_EBADFD;
- }
- *sess = pool_elt_at_index (vcm->sessions, session_index);
- return VPPCOM_OK;
-}
-
-static inline void
-vppcom_session_table_add_listener (u64 listener_handle, u32 value)
-{
- /* Session and listener handles have different formats. The latter has
- * the thread index in the upper 32 bits while the former has the session
- * type. Knowing that, for listeners we just flip the MSB to 1 */
- listener_handle |= 1ULL << 63;
- hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
-}
-
-static inline session_t *
-vppcom_session_table_lookup_listener (u64 listener_handle)
-{
- uword *p;
- u64 handle = listener_handle | (1ULL << 63);
- session_t *session;
-
- p = hash_get (vcm->session_index_by_vpp_handles, handle);
- if (!p)
- {
- clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
- "listener handle %llx", getpid (), listener_handle);
- return 0;
- }
- if (pool_is_free_index (vcm->sessions, p[0]))
- {
- if (VPPCOM_DEBUG > 1)
- clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
- getpid (), p[0]);
- return 0;
- }
-
- session = pool_elt_at_index (vcm->sessions, p[0]);
- ASSERT (session->state & STATE_LISTEN);
- return session;
-}
-
-static inline void
-vppcom_session_table_del_listener (u64 listener_handle)
-{
- listener_handle |= 1ULL << 63;
- hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
-}
-
-static void
-write_elog (void)
-{
- elog_main_t *em = &vcm->elog_main;
- char *chroot_file;
- clib_error_t *error = 0;
-
- chroot_file =
- (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
- vcm->my_client_index, getpid (), 0);
- error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
- if (error)
- {
- clib_error_report (error);
- }
- if (VPPCOM_DEBUG > 0)
- clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
-
-}
-
-static inline void
-vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
-{
- vl_api_accept_session_reply_t *rmp;
-
- rmp = vl_msg_api_alloc (sizeof (*rmp));
- memset (rmp, 0, sizeof (*rmp));
- rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
- rmp->retval = htonl (retval);
- rmp->context = context;
- rmp->handle = handle;
- vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
-}
-
-static int
-vppcom_connect_to_vpp (char *app_name)
-{
- api_main_t *am = &api_main;
- vppcom_cfg_t *vcl_cfg = &vcm->cfg;
- int rv = VPPCOM_OK;
-
- if (!vcl_cfg->vpp_api_filename)
- vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: app (%s) connecting to VPP api (%s)...",
- getpid (), app_name, vcl_cfg->vpp_api_filename);
-
- if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
- vcm->cfg.vpp_api_q_length) < 0)
- {
- clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
- rv = VPPCOM_ECONNREFUSED;
- }
- else
- {
- vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
- vcm->my_client_index = (u32) am->my_client_index;
- vcm->app_state = STATE_APP_CONN_VPP;
-
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
- getpid (), app_name);
- }
-
- if (VPPCOM_DEBUG > 0)
- {
- vcm->elog_main.lock =
- clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
- vcm->elog_main.lock[0] = 0;
- vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
- elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
- elog_enable_disable (&vcm->elog_main, 1);
-
- vcm->elog_track.name =
- (char *) format (0, "P:%d:C:%d%c", getpid (),
- vcm->my_client_index, 0);
- elog_track_register (&vcm->elog_main, &vcm->elog_track);
-
- /* *INDENT-OFF* */
- ELOG_TYPE_DECLARE (e) =
- {
- .format = "connect_vpp:rv:%d",
- .format_args = "i4",
- };
- struct
- {
- u32 data;
- } *ed;
- ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
- ed->data = (u32) rv;
- /* *INDENT-ON* */
- }
- return rv;
-}
-
-static u8 *
-format_api_error (u8 * s, va_list * args)
-{
- i32 error = va_arg (*args, u32);
- uword *p;
-
- p = hash_get (vcm->error_string_by_error_number, -error);
-
- if (p)
- s = format (s, "%s (%d)", p[0], error);
- else
- s = format (s, "%d", error);
- return s;
-}
-
-static void
-vppcom_init_error_string_table (void)
-{
- vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
-
-#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
- foreach_vnet_api_error;
-#undef _
-
- hash_set (vcm->error_string_by_error_number, 99, "Misc");
-}
-
-static inline int
-vppcom_wait_for_app_state_change (app_state_t app_state)
-{
- f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
-
- while (clib_time_now (&vcm->clib_time) < timeout)
- {
- if (vcm->app_state == app_state)
- return VPPCOM_OK;
- }
- if (VPPCOM_DEBUG > 0)
- clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
- vppcom_app_state_str (app_state), app_state);
-
- if (VPPCOM_DEBUG > 0)
- {
- /* *INDENT-OFF* */
- ELOG_TYPE_DECLARE (e) =
- {
- .format = "ERR: timeout state:%d",
- .format_args = "i4",
- };
- struct
- {
- u32 data;
- } *ed;
-
- ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
-
- ed->data = app_state;
- /* *INDENT-ON* */
- }
-
- return VPPCOM_ETIMEDOUT;
-}