+static int
+vppcom_session_unbind (u32 session_handle)
+{
+ vcl_worker_t *wrk = vcl_worker_get_current ();
+ session_accepted_msg_t *accepted_msg;
+ vcl_session_t *session = 0;
+ vcl_session_msg_t *evt;
+
+ session = vcl_session_get_w_handle (wrk, session_handle);
+ if (!session)
+ return VPPCOM_EBADFD;
+
+ /* Flush pending accept events, if any */
+ while (clib_fifo_elts (session->accept_evts_fifo))
+ {
+ clib_fifo_sub2 (session->accept_evts_fifo, evt);
+ accepted_msg = &evt->accepted_msg;
+ vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
+ vcl_send_session_accepted_reply (session->vpp_evt_q,
+ accepted_msg->context,
+ session->vpp_handle, -1);
+ }
+ clib_fifo_free (session->accept_evts_fifo);
+
+ vcl_send_session_unlisten (wrk, session);
+
+ VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
+ session->vpp_handle);
+ vcl_evt (VCL_EVT_UNBIND, session);
+
+ session->vpp_handle = ~0;
+ session->session_state = STATE_DISCONNECT;
+
+ return VPPCOM_OK;
+}
+
+static int
+vppcom_session_disconnect (u32 session_handle)
+{
+ vcl_worker_t *wrk = vcl_worker_get_current ();
+ svm_msg_q_t *vpp_evt_q;
+ vcl_session_t *session, *listen_session;
+ vcl_session_state_t state;
+ u64 vpp_handle;
+
+ session = vcl_session_get_w_handle (wrk, session_handle);
+ if (!session)
+ return VPPCOM_EBADFD;
+
+ vpp_handle = session->vpp_handle;
+ state = session->session_state;
+
+ VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
+ vpp_handle, state, vppcom_session_state_str (state));
+
+ if (PREDICT_FALSE (state & STATE_LISTEN))
+ {
+ VDBG (0, "ERROR: Cannot disconnect a listen socket!");
+ return VPPCOM_EBADFD;
+ }
+
+ if (state & STATE_VPP_CLOSING)
+ {
+ vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
+ vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
+ vpp_handle, 0);
+ VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
+ session->session_index, vpp_handle);
+ }
+ else
+ {
+ VDBG (1, "session %u [0x%llx]: sending disconnect...",
+ session->session_index, vpp_handle);
+ vcl_send_session_disconnect (wrk, session);
+ }
+
+ if (session->listener_index != VCL_INVALID_SESSION_INDEX)
+ {
+ listen_session = vcl_session_get (wrk, session->listener_index);
+ listen_session->n_accepted_sessions--;
+ }
+
+ return VPPCOM_OK;
+}
+
+/**
+ * Handle app exit
+ *
+ * Notify vpp of the disconnect and mark the worker as free. If we're the
+ * last worker, do a full cleanup otherwise, since we're probably a forked
+ * child, avoid syscalls as much as possible. We might've lost privileges.
+ */
+void
+vppcom_app_exit (void)
+{
+ if (!pool_elts (vcm->workers))
+ return;
+ vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
+ vcl_set_worker_index (~0);
+ vcl_elog_stop (vcm);
+}
+
+/*
+ * VPPCOM Public API functions
+ */
+int
+vppcom_app_create (char *app_name)
+{
+ vppcom_cfg_t *vcl_cfg = &vcm->cfg;
+ int rv;
+
+ if (vcm->is_init)
+ {
+ VDBG (1, "already initialized");
+ return VPPCOM_EEXIST;
+ }
+
+ vcm->is_init = 1;
+ vppcom_cfg (&vcm->cfg);
+ vcl_cfg = &vcm->cfg;
+
+ vcm->main_cpu = pthread_self ();
+ vcm->main_pid = getpid ();
+ vcm->app_name = format (0, "%s", app_name);
+ vppcom_init_error_string_table ();
+ fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
+ 20 /* timeout in secs */ );
+ pool_alloc (vcm->workers, vcl_cfg->max_workers);
+ clib_spinlock_init (&vcm->workers_lock);
+ clib_rwlock_init (&vcm->segment_table_lock);
+ atexit (vppcom_app_exit);
+
+ /* Allocate default worker */
+ vcl_worker_alloc_and_init ();
+
+ /* API hookup and connect to VPP */
+ vcl_elog_init (vcm);
+ vcm->app_state = STATE_APP_START;
+ rv = vppcom_connect_to_vpp (app_name);
+ if (rv)
+ {
+ VERR ("couldn't connect to VPP!");
+ return rv;
+ }
+ VDBG (0, "sending session enable");
+ rv = vppcom_app_session_enable ();
+ if (rv)
+ {
+ VERR ("vppcom_app_session_enable() failed!");
+ return rv;
+ }
+
+ VDBG (0, "sending app attach");
+ rv = vppcom_app_attach ();
+ if (rv)
+ {
+ VERR ("vppcom_app_attach() failed!");
+ return rv;
+ }
+
+ VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
+ vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
+
+ return VPPCOM_OK;
+}
+