vcl: remove session locks
[vpp.git] / src / vcl / vppcom.c
index d1c4413..6079bd1 100644 (file)
@@ -33,9 +33,15 @@ static void
 vcl_wait_for_memory (void *mem)
 {
   u8 __clib_unused test;
+  if (vcm->mounting_segment)
+    {
+      while (vcm->mounting_segment)
+       ;
+      return;
+    }
   if (1 || vcm->debug)
     {
-      sleep (1);
+      usleep (1e5);
       return;
     }
   if (signal (SIGSEGV, sigsegv_signal))
@@ -242,55 +248,52 @@ vppcom_wait_for_app_state_change (app_state_t app_state)
   return VPPCOM_ETIMEDOUT;
 }
 
-static u32
-vcl_ct_registration_add (svm_msg_q_t * mq, u32 sid)
+static svm_msg_q_t *
+vcl_session_vpp_evt_q (vcl_session_t * s)
 {
-  vcl_cut_through_registration_t *cr;
-  pool_get (vcm->cut_through_registrations, cr);
-  cr->mq = mq;
-  cr->sid = sid;
-  return (cr - vcm->cut_through_registrations);
+  if (vcl_session_is_ct (s))
+    return vcm->vpp_event_queues[0];
+  else
+    return vcm->vpp_event_queues[s->tx_fifo->master_thread_index];
 }
 
 static void
-vcl_ct_registration_del (u32 ct_index)
+vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
+                                session_handle_t handle, int retval)
 {
-  pool_put_index (vcm->cut_through_registrations, ct_index);
+  app_session_evt_t _app_evt, *app_evt = &_app_evt;
+  session_accepted_reply_msg_t *rmp;
+  app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
+  rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
+  rmp->handle = handle;
+  rmp->context = context;
+  rmp->retval = retval;
+  app_send_ctrl_evt_to_vpp (mq, app_evt);
 }
 
-static vcl_session_t *
-vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
+static void
+vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
+                                    session_handle_t handle, int retval)
 {
-  vcl_session_t *s;
-  s = vcl_session_get (f->client_session_index);
-  if (s)
-    {
-      /* rx fifo */
-      if (type == 0 && s->rx_fifo == f)
-       return s;
-      /* tx fifo */
-      if (type == 1 && s->tx_fifo == f)
-       return s;
-    }
-  s = vcl_session_get (f->master_session_index);
-  if (s)
-    {
-      if (type == 0 && s->rx_fifo == f)
-       return s;
-      if (type == 1 && s->tx_fifo == f)
-       return s;
-    }
-  return 0;
+  app_session_evt_t _app_evt, *app_evt = &_app_evt;
+  session_disconnected_reply_msg_t *rmp;
+  app_alloc_ctrl_evt_to_vpp (mq, app_evt,
+                            SESSION_CTRL_EVT_DISCONNECTED_REPLY);
+  rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
+  rmp->handle = handle;
+  rmp->context = context;
+  rmp->retval = retval;
+  app_send_ctrl_evt_to_vpp (mq, app_evt);
 }
 
 static void
-vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
-                                session_handle_t handle, int retval)
+vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
+                             session_handle_t handle, int retval)
 {
   app_session_evt_t _app_evt, *app_evt = &_app_evt;
-  session_accepted_reply_msg_t *rmp;
-  app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
-  rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
+  session_reset_reply_msg_t *rmp;
+  app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
+  rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
   rmp->handle = handle;
   rmp->context = context;
   rmp->retval = retval;
@@ -302,9 +305,11 @@ vcl_session_accepted_handler (session_accepted_msg_t * mp)
 {
   vcl_session_t *session, *listen_session;
   svm_fifo_t *rx_fifo, *tx_fifo;
-  u32 session_index;
+  u32 session_index, vpp_wrk_index;
+  svm_msg_q_t *evt_q;
 
-  VCL_SESSION_LOCK ();
+  session = vcl_session_alloc ();
+  session_index = vcl_session_index (session);
 
   listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
   if (!listen_session)
@@ -316,13 +321,10 @@ vcl_session_accepted_handler (session_accepted_msg_t * mp)
                    getpid (), mp->listener_handle);
       vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
                                       VNET_API_ERROR_INVALID_ARGUMENT);
+      vcl_session_free (session);
       return VCL_INVALID_SESSION_INDEX;
     }
 
-  pool_get (vcm->sessions, session);
-  memset (session, 0, sizeof (*session));
-  session_index = (u32) (session - vcm->sessions);
-
   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
 
@@ -333,10 +335,11 @@ vcl_session_accepted_handler (session_accepted_msg_t * mp)
       session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
                                             svm_msg_q_t *);
       vcl_wait_for_memory (session->vpp_evt_q);
-      session->ct_registration = vcl_ct_registration_add (session->our_evt_q,
-                                                         session_index);
       rx_fifo->master_session_index = session_index;
       tx_fifo->master_session_index = session_index;
+      vec_validate (vcm->vpp_event_queues, 0);
+      evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
+      vcm->vpp_event_queues[0] = evt_q;
     }
   else
     {
@@ -344,6 +347,10 @@ vcl_session_accepted_handler (session_accepted_msg_t * mp)
                                             svm_msg_q_t *);
       rx_fifo->client_session_index = session_index;
       tx_fifo->client_session_index = session_index;
+
+      vpp_wrk_index = tx_fifo->master_thread_index;
+      vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
+      vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
     }
 
   session->vpp_handle = mp->handle;
@@ -359,6 +366,8 @@ vcl_session_accepted_handler (session_accepted_msg_t * mp)
   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
   session->transport.lcl_port = listen_session->transport.lcl_port;
   session->transport.lcl_ip = listen_session->transport.lcl_ip;
+  session->session_type = listen_session->session_type;
+  session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
 
   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
        " address %U port %d queue %p!", getpid (), mp->handle, session_index,
@@ -367,43 +376,41 @@ vcl_session_accepted_handler (session_accepted_msg_t * mp)
        clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
   vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
 
-  VCL_SESSION_UNLOCK ();
   return session_index;
 }
 
 static u32
 vcl_session_connected_handler (session_connected_msg_t * mp)
 {
-  vcl_session_t *session = 0;
-  u32 session_index;
+  u32 session_index, vpp_wrk_index;
   svm_fifo_t *rx_fifo, *tx_fifo;
-  int rv = VPPCOM_OK;
+  vcl_session_t *session = 0;
+  svm_msg_q_t *evt_q;
 
   session_index = mp->context;
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
-done:
+  session = vcl_session_get (session_index);
+  if (!session)
+    {
+      clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
+                   "Invalid session index (%u)!",
+                   getpid (), mp->handle, session_index);
+      return VCL_INVALID_SESSION_INDEX;
+    }
   if (mp->retval)
     {
-      clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
-                   "connect failed! %U",
-                   getpid (), mp->handle, session_index,
-                   format_api_error, ntohl (mp->retval));
-      if (session)
-       {
-         session->session_state = STATE_FAILED;
-         session->vpp_handle = mp->handle;
-       }
-      else
-       {
-         clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
-                       "Invalid session index (%u)!",
-                       getpid (), mp->handle, session_index);
-       }
-      goto done_unlock;
+      clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
+                   mp->handle, session_index, format_api_error,
+                   ntohl (mp->retval));
+      session->session_state = STATE_FAILED;
+      session->vpp_handle = mp->handle;
+      return session_index;
     }
 
-  if (rv)
-    goto done_unlock;
+  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
+  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
+  vcl_wait_for_memory (rx_fifo);
+  rx_fifo->client_session_index = session_index;
+  tx_fifo->client_session_index = session_index;
 
   if (mp->client_event_queue_address)
     {
@@ -411,18 +418,19 @@ done:
                                             svm_msg_q_t *);
       session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
                                             svm_msg_q_t *);
-      vcl_wait_for_memory (session->vpp_evt_q);
-      session->ct_registration = vcl_ct_registration_add (session->our_evt_q,
-                                                         session_index);
+
+      vec_validate (vcm->vpp_event_queues, 0);
+      evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
+      vcm->vpp_event_queues[0] = evt_q;
     }
   else
-    session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
-                                          svm_msg_q_t *);
-
-  rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
-  rx_fifo->client_session_index = session_index;
-  tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
-  tx_fifo->client_session_index = session_index;
+    {
+      session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
+                                            svm_msg_q_t *);
+      vpp_wrk_index = tx_fifo->master_thread_index;
+      vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
+      vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
+    }
 
   session->rx_fifo = rx_fifo;
   session->tx_fifo = tx_fifo;
@@ -440,11 +448,81 @@ done:
        "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
        getpid (), mp->handle, session_index, session->rx_fifo,
        session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
-done_unlock:
-  VCL_SESSION_UNLOCK ();
+
   return session_index;
 }
 
+static u32
+vcl_session_reset_handler (session_reset_msg_t * reset_msg)
+{
+  vcl_session_t *session;
+  u32 sid;
+
+  sid = vcl_session_get_index_from_handle (reset_msg->handle);
+  session = vcl_session_get (sid);
+  if (!session)
+    {
+      VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
+      return VCL_INVALID_SESSION_INDEX;
+    }
+  session->session_state = STATE_CLOSE_ON_EMPTY;
+  VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
+  vcl_send_session_reset_reply (vcl_session_vpp_evt_q (session),
+                               vcm->my_client_index, reset_msg->handle, 0);
+  return sid;
+}
+
+static u32
+vcl_session_bound_handler (session_bound_msg_t * mp)
+{
+  vcl_session_t *session;
+  u32 sid = mp->context;
+
+  session = vcl_session_get (sid);
+  if (mp->retval)
+    {
+      VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
+           getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
+      if (session)
+       {
+         session->session_state = STATE_FAILED;
+         session->vpp_handle = mp->handle;
+         return sid;
+       }
+      else
+       {
+         clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
+                       "Invalid session index (%u)!",
+                       getpid (), mp->handle, sid);
+         return VCL_INVALID_SESSION_INDEX;
+       }
+    }
+
+  session->vpp_handle = mp->handle;
+  session->transport.is_ip4 = mp->lcl_is_ip4;
+  clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
+              sizeof (ip46_address_t));
+  session->transport.lcl_port = mp->lcl_port;
+  vppcom_session_table_add_listener (mp->handle, sid);
+  session->session_state = STATE_LISTEN;
+
+  if (session->is_dgram)
+    {
+      svm_fifo_t *rx_fifo, *tx_fifo;
+      session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
+      rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
+      rx_fifo->client_session_index = sid;
+      tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
+      tx_fifo->client_session_index = sid;
+      session->rx_fifo = rx_fifo;
+      session->tx_fifo = tx_fifo;
+    }
+
+  VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
+       getpid (), mp->handle, sid);
+  return sid;
+}
+
 int
 vcl_handle_mq_ctrl_event (session_event_t * e)
 {
@@ -483,8 +561,21 @@ vcl_handle_mq_ctrl_event (session_event_t * e)
       disconnected_msg = (session_disconnected_msg_t *) e->data;
       sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
       session = vcl_session_get (sid);
+      if (!session)
+       {
+         VDBG (0, "request to disconnect unknown handle 0x%llx",
+               disconnected_msg->handle);
+         break;
+       }
       session->session_state = STATE_DISCONNECT;
-      VDBG (0, "disconnected %u", sid);
+      VDBG (0, "disconnected handle 0xllx, sid %u", disconnected_msg->handle,
+           sid);
+      break;
+    case SESSION_CTRL_EVT_RESET:
+      vcl_session_reset_handler ((session_reset_msg_t *) e->data);
+      break;
+    case SESSION_CTRL_EVT_BOUND:
+      vcl_session_bound_handler ((session_bound_msg_t *) e->data);
       break;
     default:
       clib_warning ("unhandled %u", e->event_type);
@@ -501,28 +592,22 @@ vppcom_wait_for_session_state_change (u32 session_index,
   vcl_session_t *volatile session;
   svm_msg_q_msg_t msg;
   session_event_t *e;
-  int rv;
 
   do
     {
-      VCL_SESSION_LOCK ();
-      rv = vppcom_session_at_index (session_index, &session);
-      if (PREDICT_FALSE (rv))
+      session = vcl_session_get (session_index);
+      if (PREDICT_FALSE (!session))
        {
-         VCL_SESSION_UNLOCK ();
-         return rv;
+         return VPPCOM_EBADFD;
        }
       if (session->session_state & state)
        {
-         VCL_SESSION_UNLOCK ();
          return VPPCOM_OK;
        }
       if (session->session_state & STATE_FAILED)
        {
-         VCL_SESSION_UNLOCK ();
          return VPPCOM_ECONNREFUSED;
        }
-      VCL_SESSION_UNLOCK ();
 
       if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
        continue;
@@ -579,41 +664,37 @@ static int
 vppcom_session_unbind (u32 session_index)
 {
   vcl_session_t *session = 0;
-  int rv;
   u64 vpp_handle;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
+  session = vcl_session_get (session_index);
+  if (!session)
+    return VPPCOM_EBADFD;
 
   vpp_handle = session->vpp_handle;
   vppcom_session_table_del_listener (vpp_handle);
   session->vpp_handle = ~0;
   session->session_state = STATE_DISCONNECT;
 
-  VCL_SESSION_UNLOCK ();
-
   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
        " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
        vppcom_session_state_str (STATE_DISCONNECT));
   vcl_evt (VCL_EVT_UNBIND, session);
   vppcom_send_unbind_sock (vpp_handle);
 
-done:
-  return rv;
+  return VPPCOM_OK;
 }
 
 static int
 vppcom_session_disconnect (u32 session_index)
 {
-  int rv;
+  svm_msg_q_t *vpp_evt_q;
   vcl_session_t *session;
-  u64 vpp_handle;
   session_state_t state;
+  u64 vpp_handle;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
-
+  session = vcl_session_get (session_index);
   vpp_handle = session->vpp_handle;
   state = session->session_state;
-  VCL_SESSION_UNLOCK ();
 
   VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
        vpp_handle, session_index, state, vppcom_session_state_str (state));
@@ -623,14 +704,14 @@ vppcom_session_disconnect (u32 session_index)
       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
                    "Cannot disconnect a listen socket!",
                    getpid (), vpp_handle, session_index);
-      rv = VPPCOM_EBADFD;
-      goto done;
+      return VPPCOM_EBADFD;
     }
 
   if (state & STATE_CLOSE_ON_EMPTY)
     {
-      vppcom_send_disconnect_session_reply (vpp_handle, session_index,
-                                           0 /* rv */ );
+      vpp_evt_q = vcl_session_vpp_evt_q (session);
+      vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
+                                          vpp_handle, 0);
       VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
            "REPLY...", getpid (), vpp_handle, session_index);
     }
@@ -641,8 +722,7 @@ vppcom_session_disconnect (u32 session_index)
       vppcom_send_disconnect_session (vpp_handle, session_index);
     }
 
-done:
-  return rv;
+  return VPPCOM_OK;
 }
 
 /*
@@ -658,21 +738,24 @@ vppcom_app_create (char *app_name)
     {
       vcm->init = 1;
       vppcom_cfg (&vcm->cfg);
+      vcl_cfg = &vcm->cfg;
 
-      clib_spinlock_init (&vcm->session_fifo_lockp);
-      clib_fifo_validate (vcm->client_session_index_fifo,
-                         vcm->cfg.listen_queue_size);
-      clib_spinlock_init (&vcm->sessions_lockp);
-
+      vcm->mqs_epfd = -1;
+      if (vcl_cfg->use_mq_eventfd)
+       vcm->mqs_epfd = epoll_create (1);
 
       vcm->main_cpu = os_get_thread_index ();
-
       vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
+      vcm->ct_registration_by_mq = hash_create (0, sizeof (uword));
+      clib_spinlock_init (&vcm->ct_registration_lock);
 
       clib_time_init (&vcm->clib_time);
       vppcom_init_error_string_table ();
       svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
                                  20 /* timeout in secs */ );
+      vec_validate (vcm->mq_events, 64);
+      vec_validate (vcm->mq_msg_vector, 128);
+      vec_reset_length (vcm->mq_msg_vector);
     }
 
   if (vcm->my_client_index == ~0)
@@ -689,12 +772,7 @@ vppcom_app_create (char *app_name)
          return rv;
        }
 
-      /* State event handling thread */
-
-      rv = vce_start_event_thread (&(vcm->event_thread), 20);
-
       VDBG (0, "VCL<%d>: sending session enable", getpid ());
-
       rv = vppcom_app_session_enable ();
       if (rv)
        {
@@ -704,7 +782,6 @@ vppcom_app_create (char *app_name)
        }
 
       VDBG (0, "VCL<%d>: sending app attach", getpid ());
-
       rv = vppcom_app_attach ();
       if (rv)
        {
@@ -754,7 +831,6 @@ vppcom_session_create (u8 proto, u8 is_nonblocking)
   vcl_session_t *session;
   u32 session_index;
 
-  VCL_SESSION_LOCK ();
   pool_get (vcm->sessions, session);
   memset (session, 0, sizeof (*session));
   session_index = session - vcm->sessions;
@@ -762,17 +838,14 @@ vppcom_session_create (u8 proto, u8 is_nonblocking)
   session->session_type = proto;
   session->session_state = STATE_START;
   session->vpp_handle = ~0;
+  session->is_dgram = proto == VPPCOM_PROTO_UDP;
 
   if (is_nonblocking)
     VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
-  else
-    VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
 
   vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
           is_nonblocking, session_index);
 
-  VCL_SESSION_UNLOCK ();
-
   VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
 
   return (int) session_index;
@@ -782,7 +855,7 @@ int
 vppcom_session_close (uint32_t session_index)
 {
   vcl_session_t *session = 0;
-  int rv;
+  int rv = VPPCOM_OK;
   u8 is_vep;
   u8 is_vep_session;
   u32 next_sid;
@@ -791,14 +864,16 @@ vppcom_session_close (uint32_t session_index)
   uword *p;
   session_state_t state;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
+  session = vcl_session_get (session_index);
+  if (!session)
+    return VPPCOM_EBADFD;
+
   is_vep = session->is_vep;
   is_vep_session = session->is_vep_session;
   next_sid = session->vep.next_sid;
   vep_idx = session->vep.vep_idx;
   state = session->session_state;
   vpp_handle = session->vpp_handle;
-  VCL_SESSION_UNLOCK ();
 
   if (VPPCOM_DEBUG > 0)
     {
@@ -823,9 +898,7 @@ vppcom_session_close (uint32_t session_index)
                  getpid (), vpp_handle, next_sid, vep_idx,
                  rv, vppcom_retval_str (rv));
 
-         VCL_SESSION_LOCK_AND_GET (session_index, &session);
          next_sid = session->vep.next_sid;
-         VCL_SESSION_UNLOCK ();
        }
     }
   else
@@ -849,7 +922,7 @@ vppcom_session_close (uint32_t session_index)
                  getpid (), vpp_handle, session_index,
                  rv, vppcom_retval_str (rv));
        }
-      else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
+      else if (state & STATE_OPEN)
        {
          rv = vppcom_session_disconnect (session_index);
          if (PREDICT_FALSE (rv < 0))
@@ -860,20 +933,29 @@ vppcom_session_close (uint32_t session_index)
        }
     }
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
-  if (session->our_evt_q)
-    vcl_ct_registration_del (session->ct_registration);
+  if (vcl_session_is_ct (session))
+    {
+      vcl_cut_through_registration_t *ctr;
+      uword mq_addr;
+
+      mq_addr = pointer_to_uword (session->our_evt_q);
+      ctr = vcl_ct_registration_lock_and_lookup (mq_addr);
+      ASSERT (ctr);
+      if (ctr->epoll_evt_conn_index != ~0)
+       vcl_mq_epoll_del_evfd (ctr->epoll_evt_conn_index);
+      VDBG (0, "Removing ct registration %u",
+           vcl_ct_registration_index (ctr));
+      vcl_ct_registration_del (ctr);
+      vcl_ct_registration_unlock ();
+    }
 
-  vpp_handle = session->vpp_handle;
   if (vpp_handle != ~0)
     {
       p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
       if (p)
        hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
     }
-  pool_put_index (vcm->sessions, session_index);
-
-  VCL_SESSION_UNLOCK ();
+  vcl_session_free (session);
 
   if (VPPCOM_DEBUG > 0)
     {
@@ -884,7 +966,6 @@ vppcom_session_close (uint32_t session_index)
        clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
                      getpid (), vpp_handle, session_index);
     }
-done:
 
   vcl_evt (VCL_EVT_CLOSE, session, rv);
 
@@ -895,20 +976,19 @@ int
 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
 {
   vcl_session_t *session = 0;
-  int rv;
 
   if (!ep || !ep->ip)
     return VPPCOM_EINVAL;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
+  session = vcl_session_get (session_index);
+  if (!session)
+    return VPPCOM_EBADFD;
 
   if (session->is_vep)
     {
-      VCL_SESSION_UNLOCK ();
       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
                    "bind to an epoll session!", getpid (), session_index);
-      rv = VPPCOM_EBADFD;
-      goto done;
+      return VPPCOM_EBADFD;
     }
 
   session->transport.is_ip4 = ep->is_ip4;
@@ -928,9 +1008,11 @@ vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
        clib_net_to_host_u16 (session->transport.lcl_port),
        session->session_type ? "UDP" : "TCP");
   vcl_evt (VCL_EVT_BIND, session);
-  VCL_SESSION_UNLOCK ();
-done:
-  return rv;
+
+  if (session->session_type == VPPCOM_PROTO_UDP)
+    vppcom_session_listen (session_index, 10);
+
+  return VPPCOM_OK;
 }
 
 int
@@ -938,56 +1020,51 @@ vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
 {
   vcl_session_t *listen_session = 0;
   u64 listen_vpp_handle;
-  int rv, retval;
+  int rv;
+
+  listen_session = vcl_session_get (listen_session_index);
+  if (!listen_session)
+    return VPPCOM_EBADFD;
 
   if (q_len == 0 || q_len == ~0)
     q_len = vcm->cfg.listen_queue_size;
 
-  VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
-
   if (listen_session->is_vep)
     {
-      VCL_SESSION_UNLOCK ();
       clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
                    "epoll session!", getpid (), listen_session_index);
-      rv = VPPCOM_EBADFD;
-      goto done;
+      return VPPCOM_EBADFD;
     }
 
   listen_vpp_handle = listen_session->vpp_handle;
   if (listen_session->session_state & STATE_LISTEN)
     {
-      VCL_SESSION_UNLOCK ();
       VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
            getpid (), listen_vpp_handle, listen_session_index);
-      rv = VPPCOM_OK;
-      goto done;
+      return VPPCOM_OK;
     }
 
   VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
        "request...", getpid (), listen_vpp_handle, listen_session_index);
 
+  /*
+   * Send listen request to vpp and wait for reply
+   */
   vppcom_send_bind_sock (listen_session, listen_session_index);
-  VCL_SESSION_UNLOCK ();
-  retval = vppcom_wait_for_session_state_change (listen_session_index,
-                                                STATE_LISTEN,
-                                                vcm->cfg.session_timeout);
+  rv =
+    vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
+                                         vcm->cfg.session_timeout);
 
-  VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
-  if (PREDICT_FALSE (retval))
+  if (PREDICT_FALSE (rv))
     {
+      listen_session = vcl_session_get (listen_session_index);
       VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
            "returning %d (%s)", getpid (), listen_session->vpp_handle,
-           listen_session_index, retval, vppcom_retval_str (retval));
-      VCL_SESSION_UNLOCK ();
-      rv = retval;
-      goto done;
+           listen_session_index, rv, vppcom_retval_str (rv));
+      return rv;
     }
 
-  VCL_SESSION_UNLOCK ();
-
-done:
-  return rv;
+  return VPPCOM_OK;
 }
 
 int
@@ -1031,15 +1108,12 @@ vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
   u8 is_nonblocking;
   int rv;
 
-  VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
-
-  if (validate_args_session_accept_ (listen_session))
-    {
-      VCL_SESSION_UNLOCK ();
-      goto done;
-    }
+  listen_session = vcl_session_get (listen_session_index);
+  if (!listen_session)
+    return VPPCOM_EBADFD;
 
-  VCL_SESSION_UNLOCK ();
+  if ((rv = validate_args_session_accept_ (listen_session)))
+    return rv;
 
   if (clib_fifo_elts (listen_session->accept_evts_fifo))
     {
@@ -1073,8 +1147,8 @@ vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
 handle:
 
   client_session_index = vcl_session_accepted_handler (&accepted_msg);
-  VCL_SESSION_LOCK_AND_GET (client_session_index, &client_session);
-  rv = client_session_index;
+  listen_session = vcl_session_get (listen_session_index);
+  client_session = vcl_session_get (client_session_index);
 
   if (flags & O_NONBLOCK)
     VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
@@ -1104,6 +1178,7 @@ handle:
                                  svm_msg_q_t *);
   else
     vpp_evt_q = client_session->vpp_evt_q;
+
   vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
                                   client_session->vpp_handle, 0);
 
@@ -1122,10 +1197,8 @@ handle:
        clib_net_to_host_u16 (client_session->transport.lcl_port));
   vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
           client_session_index);
-  VCL_SESSION_UNLOCK ();
 
-done:
-  return rv;
+  return client_session_index;
 }
 
 int
@@ -1133,17 +1206,17 @@ vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
 {
   vcl_session_t *session = 0;
   u64 vpp_handle = 0;
-  int rv, retval = VPPCOM_OK;
+  int rv;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
+  session = vcl_session_get (session_index);
+  if (!session)
+    return VPPCOM_EBADFD;
 
   if (PREDICT_FALSE (session->is_vep))
     {
-      VCL_SESSION_UNLOCK ();
       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
                    "connect on an epoll session!", getpid (), session_index);
-      rv = VPPCOM_EBADFD;
-      goto done;
+      return VPPCOM_EBADFD;
     }
 
   if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
@@ -1158,9 +1231,7 @@ vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
            clib_net_to_host_u16 (session->transport.rmt_port),
            session->session_type ? "UDP" : "TCP", session->session_state,
            vppcom_session_state_str (session->session_state));
-
-      VCL_SESSION_UNLOCK ();
-      goto done;
+      return VPPCOM_OK;
     }
 
   session->transport.is_ip4 = server_ep->is_ip4;
@@ -1182,20 +1253,18 @@ vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
        clib_net_to_host_u16 (session->transport.rmt_port),
        session->session_type ? "UDP" : "TCP");
 
+  /*
+   * Send connect request and wait for reply from vpp
+   */
   vppcom_send_connect_sock (session, session_index);
-  VCL_SESSION_UNLOCK ();
-
-  retval = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
-                                                vcm->cfg.session_timeout);
+  rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
+                                            vcm->cfg.session_timeout);
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
+  session = vcl_session_get (session_index);
   vpp_handle = session->vpp_handle;
-  VCL_SESSION_UNLOCK ();
 
-done:
-  if (PREDICT_FALSE (retval))
+  if (PREDICT_FALSE (rv))
     {
-      rv = retval;
       if (VPPCOM_DEBUG > 0)
        {
          if (session)
@@ -1225,111 +1294,116 @@ vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
     return (e->event_type == SESSION_IO_EVT_CT_TX);
 }
 
+static inline u8
+vcl_session_is_readable (vcl_session_t * s)
+{
+  return ((s->session_state & STATE_OPEN)
+         || (s->session_state == STATE_LISTEN
+             && s->session_type == VPPCOM_PROTO_UDP));
+}
+
 static inline int
 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
                              u8 peek)
 {
   int n_read = 0, rv, is_nonblocking;
-  vcl_session_t *session = 0;
+  vcl_session_t *s = 0;
   svm_fifo_t *rx_fifo;
   svm_msg_q_msg_t msg;
   session_event_t *e;
   svm_msg_q_t *mq;
   u8 is_full;
 
-  ASSERT (buf);
+  if (PREDICT_FALSE (!buf))
+    return VPPCOM_EINVAL;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
+  s = vcl_session_get (session_index);
+  if (PREDICT_FALSE (!s))
+    return VPPCOM_EBADFD;
 
-  if (PREDICT_FALSE (session->is_vep))
+  if (PREDICT_FALSE (s->is_vep))
     {
-      VCL_SESSION_UNLOCK ();
       clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
                    "read from an epoll session!", getpid (), session_index);
-      rv = VPPCOM_EBADFD;
-      goto done;
+      return VPPCOM_EBADFD;
     }
 
-  is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
-  rx_fifo = session->rx_fifo;
+  is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
+  rx_fifo = s->rx_fifo;
 
-  if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
+  if (PREDICT_FALSE (!vcl_session_is_readable (s)))
     {
-      session_state_t state = session->session_state;
-      VCL_SESSION_UNLOCK ();
+      session_state_t state = s->session_state;
       rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
 
       VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
            "state 0x%x (%s), returning %d (%s)",
-           getpid (), session->vpp_handle, session_index, state,
+           getpid (), s->vpp_handle, session_index, state,
            vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
-      goto done;
+      return rv;
     }
 
-  VCL_SESSION_UNLOCK ();
-  mq = session->our_evt_q ? session->our_evt_q : vcm->app_event_queue;
+  mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
+  svm_fifo_unset_event (rx_fifo);
   is_full = svm_fifo_is_full (rx_fifo);
 
   if (svm_fifo_is_empty (rx_fifo))
     {
-      svm_fifo_unset_event (rx_fifo);
       if (is_nonblocking)
        {
-         rv = VPPCOM_OK;
-         goto done;
+         return VPPCOM_OK;
        }
-      svm_msg_q_lock (mq);
       while (1)
        {
+         svm_msg_q_lock (mq);
          if (svm_msg_q_is_empty (mq))
            svm_msg_q_wait (mq);
+
          svm_msg_q_sub_w_lock (mq, &msg);
          e = svm_msg_q_msg_data (mq, &msg);
+         svm_msg_q_unlock (mq);
          if (!vcl_is_rx_evt_for_session (e, session_index,
-                                         session->our_evt_q != 0))
+                                         s->our_evt_q != 0))
            {
              vcl_handle_mq_ctrl_event (e);
              svm_msg_q_free_msg (mq, &msg);
              continue;
            }
-         if (svm_fifo_is_empty (rx_fifo))
-           {
-             svm_msg_q_free_msg (mq, &msg);
-             continue;
-           }
+         svm_fifo_unset_event (rx_fifo);
          svm_msg_q_free_msg (mq, &msg);
-         svm_msg_q_unlock (mq);
+         if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
+           return 0;
+         if (svm_fifo_is_empty (rx_fifo))
+           continue;
          break;
        }
     }
 
-  if (peek)
-    n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
+  if (s->is_dgram)
+    n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
   else
-    n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
-  ASSERT (n_read > 0);
-  svm_fifo_unset_event (rx_fifo);
-
-  if (session->our_evt_q && is_full)
-    app_send_io_evt_to_vpp (session->vpp_evt_q, rx_fifo, SESSION_IO_EVT_CT_RX,
-                           SVM_Q_WAIT);
+    n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
 
+  if (vcl_session_is_ct (s) && is_full)
+    {
+      /* If the peer is not polling send notification */
+      if (!svm_fifo_has_event (s->rx_fifo))
+       app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
+                               SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
+    }
 
   if (VPPCOM_DEBUG > 2)
     {
       if (n_read > 0)
        clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
-                     "from (%p)", getpid (), session->vpp_handle,
+                     "from (%p)", getpid (), s->vpp_handle,
                      session_index, n_read, rx_fifo);
       else
        clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
-                     "returning %d (%s)", getpid (), session->vpp_handle,
-                     session_index, rv, vppcom_retval_str (rv));
+                     "returning %d (%s)", getpid (), s->vpp_handle,
+                     session_index, n_read, vppcom_retval_str (n_read));
     }
   return n_read;
-
-done:
-  return rv;
 }
 
 int
@@ -1389,112 +1463,114 @@ int
 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
 {
   int rv, n_write, is_nonblocking;
-  vcl_session_t *session = 0;
+  vcl_session_t *s = 0;
   svm_fifo_t *tx_fifo = 0;
+  session_evt_type_t et;
   svm_msg_q_msg_t msg;
   session_event_t *e;
   svm_msg_q_t *mq;
 
-  ASSERT (buf);
+  if (PREDICT_FALSE (!buf))
+    return VPPCOM_EINVAL;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
+  s = vcl_session_get (session_index);
+  if (PREDICT_FALSE (!s))
+    return VPPCOM_EBADFD;
 
-  tx_fifo = session->tx_fifo;
-  is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
+  tx_fifo = s->tx_fifo;
+  is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
 
-  if (PREDICT_FALSE (session->is_vep))
+  if (PREDICT_FALSE (s->is_vep))
     {
-      VCL_SESSION_UNLOCK ();
       clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
                    "cannot write to an epoll session!",
-                   getpid (), session->vpp_handle, session_index);
+                   getpid (), s->vpp_handle, session_index);
 
-      rv = VPPCOM_EBADFD;
-      goto done;
+      return VPPCOM_EBADFD;
     }
 
-  if (!(session->session_state & STATE_OPEN))
+  if (!(s->session_state & STATE_OPEN))
     {
-      session_state_t state = session->session_state;
+      session_state_t state = s->session_state;
       rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
-      VCL_SESSION_UNLOCK ();
       VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
            "state 0x%x (%s)",
-           getpid (), session->vpp_handle, session_index,
+           getpid (), s->vpp_handle, session_index,
            state, vppcom_session_state_str (state));
-      goto done;
+      return rv;
     }
 
-  VCL_SESSION_UNLOCK ();
-
-  mq = session->our_evt_q ? session->our_evt_q : vcm->app_event_queue;
+  mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
   if (svm_fifo_is_full (tx_fifo))
     {
       if (is_nonblocking)
        {
-         rv = VPPCOM_EWOULDBLOCK;
-         goto done;
+         return VPPCOM_EWOULDBLOCK;
        }
-      svm_msg_q_lock (mq);
-      while (1)
+      while (svm_fifo_is_full (tx_fifo))
        {
-         if (!svm_fifo_is_full (tx_fifo))
-           {
-             svm_msg_q_unlock (mq);
-             break;
-           }
-         if (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
-           continue;
+         svm_msg_q_lock (mq);
+         while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
+           ;
          svm_msg_q_sub_w_lock (mq, &msg);
          e = svm_msg_q_msg_data (mq, &msg);
+         svm_msg_q_unlock (mq);
+
          if (!vcl_is_tx_evt_for_session (e, session_index,
-                                         session->our_evt_q != 0))
-           {
-             vcl_handle_mq_ctrl_event (e);
-             svm_msg_q_free_msg (mq, &msg);
-             continue;
-           }
-         if (svm_fifo_is_full (tx_fifo))
-           {
-             svm_msg_q_free_msg (mq, &msg);
-             continue;
-           }
+                                         s->our_evt_q != 0))
+           vcl_handle_mq_ctrl_event (e);
          svm_msg_q_free_msg (mq, &msg);
-         svm_msg_q_unlock (mq);
-         break;
        }
     }
 
-  n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
-  ASSERT (n_write > 0);
+  ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
+  et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
+  if (s->is_dgram)
+    n_write = app_send_dgram_raw (tx_fifo, &s->transport,
+                                 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
+  else
+    n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
+                                  SVM_Q_WAIT);
 
-  if (svm_fifo_set_event (tx_fifo))
-    {
-      session_evt_type_t et;
-      VCL_SESSION_LOCK_AND_GET (session_index, &session);
-      et = session->our_evt_q ? SESSION_IO_EVT_CT_TX : FIFO_EVENT_APP_TX;
-      app_send_io_evt_to_vpp (session->vpp_evt_q, tx_fifo, et, SVM_Q_WAIT);
-      VCL_SESSION_UNLOCK ();
-      VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: added FIFO_EVENT_APP_TX "
-           "to vpp_event_q %p, n_write %d", getpid (),
-           session->vpp_handle, session_index, session->vpp_evt_q, n_write);
-    }
+  ASSERT (n_write > 0);
 
   if (VPPCOM_DEBUG > 2)
     {
       if (n_write <= 0)
        clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
-                     "FIFO-FULL (%p)", getpid (), session->vpp_handle,
+                     "FIFO-FULL (%p)", getpid (), s->vpp_handle,
                      session_index, tx_fifo);
       else
        clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
                      "wrote %d bytes tx-fifo: (%p)", getpid (),
-                     session->vpp_handle, session_index, n_write, tx_fifo);
+                     s->vpp_handle, session_index, n_write, tx_fifo);
     }
   return n_write;
+}
 
-done:
-  return rv;
+static vcl_session_t *
+vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
+{
+  vcl_session_t *s;
+  s = vcl_session_get (f->client_session_index);
+  if (s)
+    {
+      /* rx fifo */
+      if (type == 0 && s->rx_fifo == f)
+       return s;
+      /* tx fifo */
+      if (type == 1 && s->tx_fifo == f)
+       return s;
+    }
+  s = vcl_session_get (f->master_session_index);
+  if (s)
+    {
+      if (type == 0 && s->rx_fifo == f)
+       return s;
+      if (type == 1 && s->tx_fifo == f)
+       return s;
+    }
+  return 0;
 }
 
 static inline int
@@ -1539,6 +1615,22 @@ vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
   return svm_fifo_max_enqueue (session->tx_fifo);
 }
 
+static inline int
+vcl_mq_dequeue_batch (svm_msg_q_t * mq)
+{
+  svm_msg_q_msg_t *msg;
+  u32 n_msgs;
+  int i;
+
+  n_msgs = svm_msg_q_size (mq);
+  for (i = 0; i < n_msgs; i++)
+    {
+      vec_add2 (vcm->mq_msg_vector, msg, 1);
+      svm_msg_q_sub_w_lock (mq, msg);
+    }
+  return n_msgs;
+}
+
 static int
 vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
                      unsigned long *read_map, unsigned long *write_map,
@@ -1546,12 +1638,13 @@ vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
                      u32 * bits_set)
 {
   session_disconnected_msg_t *disconnected_msg;
+  session_connected_msg_t *connected_msg;
   session_accepted_msg_t *accepted_msg;
   vcl_session_msg_t *vcl_msg;
   vcl_session_t *session;
-  svm_msg_q_msg_t msg;
+  svm_msg_q_msg_t *msg;
   session_event_t *e;
-  u32 n_msgs, i, sid;
+  u32 i, sid;
   u64 handle;
 
   svm_msg_q_lock (mq);
@@ -1581,24 +1674,18 @@ vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
            }
        }
     }
+  vcl_mq_dequeue_batch (mq);
   svm_msg_q_unlock (mq);
 
-  n_msgs = svm_msg_q_size (mq);
-  for (i = 0; i < n_msgs; i++)
+  for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
     {
-      if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0))
-       {
-         clib_warning ("message queue returned");
-         continue;
-       }
-      e = svm_msg_q_msg_data (mq, &msg);
+      msg = vec_elt_at_index (vcm->mq_msg_vector, i);
+      e = svm_msg_q_msg_data (mq, msg);
       switch (e->event_type)
        {
        case FIFO_EVENT_APP_RX:
          sid = e->fifo->client_session_index;
          session = vcl_session_get (sid);
-         if (!session || svm_fifo_is_empty (session->rx_fifo))
-           break;
          if (sid < n_bits && read_map)
            {
              clib_bitmap_set_no_check (read_map, sid, 1);
@@ -1608,7 +1695,7 @@ vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
        case FIFO_EVENT_APP_TX:
          sid = e->fifo->client_session_index;
          session = vcl_session_get (sid);
-         if (!session || svm_fifo_is_full (session->tx_fifo))
+         if (!session)
            break;
          if (sid < n_bits && write_map)
            {
@@ -1619,8 +1706,6 @@ vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
        case SESSION_IO_EVT_CT_TX:
          session = vcl_ct_session_get_from_fifo (e->fifo, 0);
          sid = vcl_session_index (session);
-         if (!session || svm_fifo_is_empty (session->rx_fifo))
-           break;
          if (sid < n_bits && read_map)
            {
              clib_bitmap_set_no_check (read_map, sid, 1);
@@ -1631,7 +1716,7 @@ vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
        case SESSION_IO_EVT_CT_RX:
          session = vcl_ct_session_get_from_fifo (e->fifo, 1);
          sid = vcl_session_index (session);
-         if (!session || svm_fifo_is_full (session->tx_fifo))
+         if (!session)
            break;
          if (sid < n_bits && write_map)
            {
@@ -1659,6 +1744,10 @@ vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
              *bits_set += 1;
            }
          break;
+       case SESSION_CTRL_EVT_CONNECTED:
+         connected_msg = (session_connected_msg_t *) e->data;
+         vcl_session_connected_handler (connected_msg);
+         break;
        case SESSION_CTRL_EVT_DISCONNECTED:
          disconnected_msg = (session_disconnected_msg_t *) e->data;
          sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
@@ -1668,24 +1757,85 @@ vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
              *bits_set += 1;
            }
          break;
+       case SESSION_CTRL_EVT_RESET:
+         sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data);
+         if (sid < n_bits && except_map)
+           {
+             clib_bitmap_set_no_check (except_map, sid, 1);
+             *bits_set += 1;
+           }
+         break;
        default:
          clib_warning ("unhandled: %u", e->event_type);
          break;
        }
-      svm_msg_q_free_msg (mq, &msg);
+      svm_msg_q_free_msg (mq, msg);
     }
 
+  vec_reset_length (vcm->mq_msg_vector);
   return *bits_set;
 }
 
+static int
+vppcom_select_condvar (unsigned long n_bits, unsigned long *read_map,
+                      unsigned long *write_map, unsigned long *except_map,
+                      double time_to_wait, u32 * bits_set)
+{
+  double total_wait = 0, wait_slice;
+  vcl_cut_through_registration_t *cr;
+
+  time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
+  wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
+  do
+    {
+      /* *INDENT-OFF* */
+      pool_foreach (cr, vcm->cut_through_registrations, ({
+       vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
+                             0, bits_set);
+      }));
+      /* *INDENT-ON* */
+
+      vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
+                           except_map, time_to_wait, bits_set);
+      total_wait += wait_slice;
+      if (*bits_set)
+       return *bits_set;
+    }
+  while (total_wait < time_to_wait);
+
+  return 0;
+}
+
+static int
+vppcom_select_eventfd (unsigned long n_bits, unsigned long *read_map,
+                      unsigned long *write_map, unsigned long *except_map,
+                      double time_to_wait, u32 * bits_set)
+{
+  vcl_mq_evt_conn_t *mqc;
+  int __clib_unused n_read;
+  int n_mq_evts, i;
+  u64 buf;
+
+  vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
+  n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
+                         vec_len (vcm->mq_events), time_to_wait);
+  for (i = 0; i < n_mq_evts; i++)
+    {
+      mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
+      n_read = read (mqc->mq_fd, &buf, sizeof (buf));
+      vcl_select_handle_mq (mqc->mq, n_bits, read_map, write_map,
+                           except_map, 0, bits_set);
+    }
+
+  return (n_mq_evts > 0 ? (int) *bits_set : 0);
+}
+
 int
 vppcom_select (unsigned long n_bits, unsigned long *read_map,
               unsigned long *write_map, unsigned long *except_map,
               double time_to_wait)
 {
   u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
-  vcl_cut_through_registration_t *cr;
-  double total_wait = 0, wait_slice;
   vcl_session_t *session = 0;
   int rv;
 
@@ -1723,17 +1873,14 @@ vppcom_select (unsigned long n_bits, unsigned long *read_map,
 
   /* *INDENT-OFF* */
   clib_bitmap_foreach (sid, vcm->wr_bitmap, ({
-    VCL_SESSION_LOCK();
     if (!(session = vcl_session_get (sid)))
       {
-        VCL_SESSION_UNLOCK();
         VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
               getpid (), sid);
         return VPPCOM_EBADFD;
       }
 
     rv = svm_fifo_is_full (session->tx_fifo);
-    VCL_SESSION_UNLOCK();
     if (!rv)
       {
         clib_bitmap_set_no_check (write_map, sid, 1);
@@ -1744,18 +1891,16 @@ vppcom_select (unsigned long n_bits, unsigned long *read_map,
 check_rd:
   if (!read_map)
     goto check_mq;
+
   clib_bitmap_foreach (sid, vcm->rd_bitmap, ({
-    VCL_SESSION_LOCK();
     if (!(session = vcl_session_get (sid)))
       {
-        VCL_SESSION_UNLOCK();
         VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
               getpid (), sid);
         return VPPCOM_EBADFD;
       }
 
     rv = vppcom_session_read_ready (session);
-    VCL_SESSION_UNLOCK();
     if (rv)
       {
         clib_bitmap_set_no_check (read_map, sid, 1);
@@ -1765,23 +1910,13 @@ check_rd:
   /* *INDENT-ON* */
 
 check_mq:
-  wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
-  do
-    {
-      /* *INDENT-OFF* */
-      pool_foreach (cr, vcm->cut_through_registrations, ({
-       vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
-                             0, &bits_set);
-      }));
-      /* *INDENT-ON* */
 
-      vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
-                           except_map, time_to_wait, &bits_set);
-      total_wait += wait_slice;
-      if (bits_set)
-       return bits_set;
-    }
-  while (total_wait < time_to_wait);
+  if (vcm->cfg.use_mq_eventfd)
+    vppcom_select_eventfd (n_bits, read_map, write_map, except_map,
+                          time_to_wait, &bits_set);
+  else
+    vppcom_select_condvar (n_bits, read_map, write_map, except_map,
+                          time_to_wait, &bits_set);
 
   return (bits_set);
 }
@@ -1791,15 +1926,14 @@ vep_verify_epoll_chain (u32 vep_idx)
 {
   vcl_session_t *session;
   vppcom_epoll_t *vep;
-  int rv;
   u32 sid = vep_idx;
 
   if (VPPCOM_DEBUG <= 1)
     return;
 
   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
-  rv = vppcom_session_at_index (vep_idx, &session);
-  if (PREDICT_FALSE (rv))
+  session = vcl_session_get (vep_idx);
+  if (PREDICT_FALSE (!session))
     {
       clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
                    getpid (), vep_idx);
@@ -1825,8 +1959,8 @@ vep_verify_epoll_chain (u32 vep_idx)
 
   for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
     {
-      rv = vppcom_session_at_index (sid, &session);
-      if (PREDICT_FALSE (rv))
+      session = vcl_session_get (sid);
+      if (PREDICT_FALSE (!session))
        {
          clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
          goto done;
@@ -1875,7 +2009,6 @@ vppcom_epoll_create (void)
   vcl_session_t *vep_session;
   u32 vep_idx;
 
-  VCL_SESSION_LOCK ();
   pool_get (vcm->sessions, vep_session);
   memset (vep_session, 0, sizeof (*vep_session));
   vep_idx = vep_session - vcm->sessions;
@@ -1889,8 +2022,6 @@ vppcom_epoll_create (void)
   vep_session->poll_reg = 0;
 
   vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
-  VCL_SESSION_UNLOCK ();
-
   VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
        getpid (), vep_idx, vep_idx);
 
@@ -1903,7 +2034,7 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
 {
   vcl_session_t *vep_session;
   vcl_session_t *session;
-  int rv;
+  int rv = VPPCOM_OK;
 
   if (vep_idx == session_index)
     {
@@ -1912,36 +2043,33 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
       return VPPCOM_EINVAL;
     }
 
-  VCL_SESSION_LOCK ();
-  rv = vppcom_session_at_index (vep_idx, &vep_session);
-  if (PREDICT_FALSE (rv))
+  vep_session = vcl_session_get (vep_idx);
+  if (PREDICT_FALSE (!vep_session))
     {
       clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
-      goto done;
+      return VPPCOM_EBADFD;
     }
   if (PREDICT_FALSE (!vep_session->is_vep))
     {
       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
                    getpid (), vep_idx);
-      rv = VPPCOM_EINVAL;
-      goto done;
+      return VPPCOM_EINVAL;
     }
 
   ASSERT (vep_session->vep.vep_idx == ~0);
   ASSERT (vep_session->vep.prev_sid == ~0);
 
-  rv = vppcom_session_at_index (session_index, &session);
-  if (PREDICT_FALSE (rv))
+  session = vcl_session_get (session_index);
+  if (PREDICT_FALSE (!session))
     {
       VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
            getpid (), session_index);
-      goto done;
+      return VPPCOM_EBADFD;
     }
   if (PREDICT_FALSE (session->is_vep))
     {
       clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
-      rv = VPPCOM_EINVAL;
-      goto done;
+      return VPPCOM_EINVAL;
     }
 
   switch (op)
@@ -1951,20 +2079,18 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
        {
          clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
                        "epoll_event structure!", getpid ());
-         rv = VPPCOM_EINVAL;
-         goto done;
+         return VPPCOM_EINVAL;
        }
       if (vep_session->vep.next_sid != ~0)
        {
          vcl_session_t *next_session;
-         rv = vppcom_session_at_index (vep_session->vep.next_sid,
-                                       &next_session);
-         if (PREDICT_FALSE (rv))
+         next_session = vcl_session_get (vep_session->vep.next_sid);
+         if (PREDICT_FALSE (!next_session))
            {
              clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
                            "vep.next_sid (%u) on vep_idx (%u)!",
                            getpid (), vep_session->vep.next_sid, vep_idx);
-             goto done;
+             return VPPCOM_EBADFD;
            }
          ASSERT (next_session->vep.prev_sid == vep_idx);
          next_session->vep.prev_sid = session_index;
@@ -1978,22 +2104,9 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
       session->is_vep_session = 1;
       vep_session->vep.next_sid = session_index;
 
-      /* VCL Event Register handler */
-      if (session->session_state & STATE_LISTEN)
-       {
-         /* Register handler for connect_request event on listen_session_index */
-         vce_event_key_t evk;
-         evk.session_index = session_index;
-         evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
-         vep_session->poll_reg =
-           vce_register_handler (&vcm->event_thread, &evk,
-                                 vce_poll_wait_connect_request_handler_fn,
-                                 0 /* No callback args */ );
-       }
-      VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
-           "sid %u, events 0x%x, data 0x%llx!",
-           getpid (), vep_idx, session_index,
-           event->events, event->data.u64);
+      VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
+           "data 0x%llx!", getpid (), vep_idx, session_index, event->events,
+           event->data.u64);
       vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
       break;
 
@@ -2046,13 +2159,6 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
          goto done;
        }
 
-      /* VCL Event Un-register handler */
-      if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
-       {
-         (void) vce_unregister_handler (&vcm->event_thread,
-                                        vep_session->poll_reg);
-       }
-
       vep_session->wait_cont_idx =
        (vep_session->wait_cont_idx == session_index) ?
        session->vep.next_sid : vep_session->wait_cont_idx;
@@ -2062,13 +2168,13 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
       else
        {
          vcl_session_t *prev_session;
-         rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
-         if (PREDICT_FALSE (rv))
+         prev_session = vcl_session_get (session->vep.prev_sid);
+         if (PREDICT_FALSE (!prev_session))
            {
              clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
                            "vep.prev_sid (%u) on sid (%u)!",
                            getpid (), session->vep.prev_sid, session_index);
-             goto done;
+             return VPPCOM_EBADFD;
            }
          ASSERT (prev_session->vep.next_sid == session_index);
          prev_session->vep.next_sid = session->vep.next_sid;
@@ -2076,13 +2182,13 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
       if (session->vep.next_sid != ~0)
        {
          vcl_session_t *next_session;
-         rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
-         if (PREDICT_FALSE (rv))
+         next_session = vcl_session_get (session->vep.next_sid);
+         if (PREDICT_FALSE (!next_session))
            {
              clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
                            "vep.next_sid (%u) on sid (%u)!",
                            getpid (), session->vep.next_sid, session_index);
-             goto done;
+             return VPPCOM_EBADFD;
            }
          ASSERT (next_session->vep.prev_sid == session_index);
          next_session->vep.prev_sid = session->vep.prev_sid;
@@ -2106,7 +2212,6 @@ vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
   vep_verify_epoll_chain (vep_idx);
 
 done:
-  VCL_SESSION_UNLOCK ();
   return rv;
 }
 
@@ -2117,11 +2222,11 @@ vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
   session_disconnected_msg_t *disconnected_msg;
   session_connected_msg_t *connected_msg;
   session_accepted_msg_t *accepted_msg;
-  u32 sid = ~0, session_events, n_msgs;
   u64 session_evt_data = ~0, handle;
+  u32 sid = ~0, session_events;
   vcl_session_msg_t *vcl_msg;
   vcl_session_t *session;
-  svm_msg_q_msg_t msg;
+  svm_msg_q_msg_t *msg;
   session_event_t *e;
   u8 add_event;
   int i;
@@ -2147,71 +2252,55 @@ vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
            }
        }
     }
+  vcl_mq_dequeue_batch (mq);
   svm_msg_q_unlock (mq);
 
-  n_msgs = svm_msg_q_size (mq);
-  for (i = 0; i < n_msgs; i++)
+  for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
     {
-      if (svm_msg_q_sub (mq, &msg, SVM_Q_WAIT, 0))
-       {
-         clib_warning ("message queue returned");
-         continue;
-       }
-      e = svm_msg_q_msg_data (mq, &msg);
+      msg = vec_elt_at_index (vcm->mq_msg_vector, i);
+      e = svm_msg_q_msg_data (mq, msg);
       add_event = 0;
       switch (e->event_type)
        {
        case FIFO_EVENT_APP_RX:
          sid = e->fifo->client_session_index;
-         clib_spinlock_lock (&vcm->sessions_lockp);
          session = vcl_session_get (sid);
          session_events = session->vep.ev.events;
-         if ((EPOLLIN & session->vep.ev.events)
-             && !svm_fifo_is_empty (session->rx_fifo))
-           {
-             add_event = 1;
-             events[*num_ev].events |= EPOLLIN;
-             session_evt_data = session->vep.ev.data.u64;
-           }
-         clib_spinlock_unlock (&vcm->sessions_lockp);
+         if (!(EPOLLIN & session->vep.ev.events))
+           break;
+         add_event = 1;
+         events[*num_ev].events |= EPOLLIN;
+         session_evt_data = session->vep.ev.data.u64;
          break;
        case FIFO_EVENT_APP_TX:
          sid = e->fifo->client_session_index;
-         clib_spinlock_lock (&vcm->sessions_lockp);
          session = vcl_session_get (sid);
          session_events = session->vep.ev.events;
-         if ((EPOLLOUT & session_events)
-             && !svm_fifo_is_full (session->tx_fifo))
-           {
-             add_event = 1;
-             events[*num_ev].events |= EPOLLOUT;
-             session_evt_data = session->vep.ev.data.u64;
-           }
-         clib_spinlock_unlock (&vcm->sessions_lockp);
+         if (!(EPOLLOUT & session_events))
+           break;
+         add_event = 1;
+         events[*num_ev].events |= EPOLLOUT;
+         session_evt_data = session->vep.ev.data.u64;
          break;
        case SESSION_IO_EVT_CT_TX:
          session = vcl_ct_session_get_from_fifo (e->fifo, 0);
          sid = vcl_session_index (session);
          session_events = session->vep.ev.events;
-         if ((EPOLLIN & session->vep.ev.events)
-             && !svm_fifo_is_empty (session->rx_fifo))
-           {
-             add_event = 1;
-             events[*num_ev].events |= EPOLLIN;
-             session_evt_data = session->vep.ev.data.u64;
-           }
+         if (!(EPOLLIN & session->vep.ev.events))
+           break;
+         add_event = 1;
+         events[*num_ev].events |= EPOLLIN;
+         session_evt_data = session->vep.ev.data.u64;
          break;
        case SESSION_IO_EVT_CT_RX:
          session = vcl_ct_session_get_from_fifo (e->fifo, 1);
          sid = vcl_session_index (session);
          session_events = session->vep.ev.events;
-         if ((EPOLLOUT & session_events)
-             && !svm_fifo_is_full (session->tx_fifo))
-           {
-             add_event = 1;
-             events[*num_ev].events |= EPOLLOUT;
-             session_evt_data = session->vep.ev.data.u64;
-           }
+         if (!(EPOLLOUT & session_events))
+           break;
+         add_event = 1;
+         events[*num_ev].events |= EPOLLOUT;
+         session_evt_data = session->vep.ev.data.u64;
          break;
        case SESSION_CTRL_EVT_ACCEPTED:
          accepted_msg = (session_accepted_msg_t *) e->data;
@@ -2239,7 +2328,6 @@ vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
          vcl_session_connected_handler (connected_msg);
          /* Generate EPOLLOUT because there's no connected event */
          sid = vcl_session_get_index_from_handle (connected_msg->handle);
-         clib_spinlock_lock (&vcm->sessions_lockp);
          session = vcl_session_get (sid);
          session_events = session->vep.ev.events;
          if (EPOLLOUT & session_events)
@@ -2248,53 +2336,110 @@ vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
              events[*num_ev].events |= EPOLLOUT;
              session_evt_data = session->vep.ev.data.u64;
            }
-         clib_spinlock_unlock (&vcm->sessions_lockp);
          break;
        case SESSION_CTRL_EVT_DISCONNECTED:
          disconnected_msg = (session_disconnected_msg_t *) e->data;
          sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
-         clib_spinlock_lock (&vcm->sessions_lockp);
-         session = vcl_session_get (sid);
+         if (!(session = vcl_session_get (sid)))
+           break;
+         add_event = 1;
+         events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
+         session_evt_data = session->vep.ev.data.u64;
+         session_events = session->vep.ev.events;
+         break;
+       case SESSION_CTRL_EVT_RESET:
+         sid = vcl_session_reset_handler ((session_reset_msg_t *) e->data);
+         if (!(session = vcl_session_get (sid)))
+           break;
          add_event = 1;
          events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
          session_evt_data = session->vep.ev.data.u64;
          session_events = session->vep.ev.events;
-         clib_spinlock_unlock (&vcm->sessions_lockp);
          break;
        default:
          clib_warning ("unhandled: %u", e->event_type);
-         svm_msg_q_free_msg (mq, &msg);
+         svm_msg_q_free_msg (mq, msg);
          continue;
        }
-
-      svm_msg_q_free_msg (mq, &msg);
+      svm_msg_q_free_msg (mq, msg);
 
       if (add_event)
        {
          events[*num_ev].data.u64 = session_evt_data;
          if (EPOLLONESHOT & session_events)
            {
-             clib_spinlock_lock (&vcm->sessions_lockp);
              session = vcl_session_get (sid);
              session->vep.ev.events = 0;
-             clib_spinlock_unlock (&vcm->sessions_lockp);
            }
          *num_ev += 1;
          if (*num_ev == maxevents)
            break;
        }
     }
+
+  vec_reset_length (vcm->mq_msg_vector);
   return *num_ev;
 }
 
+static int
+vppcom_epoll_wait_condvar (struct epoll_event *events, int maxevents,
+                          double wait_for_time)
+{
+  vcl_cut_through_registration_t *cr;
+  double total_wait = 0, wait_slice;
+  u32 num_ev = 0;
+  int rv;
+
+  wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
+  wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
+
+  do
+    {
+      /* *INDENT-OFF* */
+      pool_foreach (cr, vcm->cut_through_registrations, ({
+        vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
+      }));
+      /* *INDENT-ON* */
+
+      rv = vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
+                                    num_ev ? 0 : wait_slice, &num_ev);
+      if (rv)
+       total_wait += wait_slice;
+      if (num_ev)
+       return num_ev;
+    }
+  while (total_wait < wait_for_time);
+  return (int) num_ev;
+}
+
+static int
+vppcom_epoll_wait_eventfd (struct epoll_event *events, int maxevents,
+                          double wait_for_time)
+{
+  vcl_mq_evt_conn_t *mqc;
+  int __clib_unused n_read;
+  int n_mq_evts, i;
+  u32 n_evts = 0;
+  u64 buf;
+
+  vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
+  n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
+                         vec_len (vcm->mq_events), wait_for_time);
+  for (i = 0; i < n_mq_evts; i++)
+    {
+      mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
+      n_read = read (mqc->mq_fd, &buf, sizeof (buf));
+      vcl_epoll_wait_handle_mq (mqc->mq, events, maxevents, 0, &n_evts);
+    }
+
+  return (int) n_evts;
+}
+
 int
 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
                   int maxevents, double wait_for_time)
 {
-  vcl_cut_through_registration_t *cr;
   vcl_session_t *vep_session;
-  double total_wait = 0, wait_slice;
-  u32 num_ev = 0;
 
   if (PREDICT_FALSE (maxevents <= 0))
     {
@@ -2303,37 +2448,20 @@ vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
       return VPPCOM_EINVAL;
     }
 
-  clib_spinlock_lock (&vcm->sessions_lockp);
   vep_session = vcl_session_get (vep_idx);
   if (PREDICT_FALSE (!vep_session->is_vep))
     {
       clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
                    getpid (), vep_idx);
-      clib_spinlock_unlock (&vcm->sessions_lockp);
       return VPPCOM_EINVAL;
     }
-  clib_spinlock_unlock (&vcm->sessions_lockp);
 
   memset (events, 0, sizeof (*events) * maxevents);
-  wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
-
-  do
-    {
-      /* *INDENT-OFF* */
-      pool_foreach (cr, vcm->cut_through_registrations, ({
-        vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
-      }));
-      /* *INDENT-ON* */
 
-      vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
-                               num_ev ? 0 : wait_slice, &num_ev);
-      total_wait += wait_slice;
-      if (num_ev)
-       return num_ev;
-    }
-  while (total_wait < wait_for_time);
+  if (vcm->cfg.use_mq_eventfd)
+    return vppcom_epoll_wait_eventfd (events, maxevents, wait_for_time);
 
-  return num_ev;
+  return vppcom_epoll_wait_condvar (events, maxevents, wait_for_time);
 }
 
 int
@@ -2345,9 +2473,9 @@ vppcom_session_attr (uint32_t session_index, uint32_t op,
   u32 *flags = buffer;
   vppcom_endpt_t *ep = buffer;
 
-  VCL_SESSION_LOCK_AND_GET (session_index, &session);
-
-  ASSERT (session);
+  session = vcl_session_get (session_index);
+  if (!session)
+    return VPPCOM_EBADFD;
 
   switch (op)
     {
@@ -2864,8 +2992,6 @@ vppcom_session_attr (uint32_t session_index, uint32_t op,
       break;
     }
 
-done:
-  VCL_SESSION_UNLOCK ();
   return rv;
 }
 
@@ -2878,26 +3004,15 @@ vppcom_session_recvfrom (uint32_t session_index, void *buffer,
 
   if (ep)
     {
-      VCL_SESSION_LOCK ();
-      rv = vppcom_session_at_index (session_index, &session);
-      if (PREDICT_FALSE (rv))
+      session = vcl_session_get (session_index);
+      if (PREDICT_FALSE (!session))
        {
-         VCL_SESSION_UNLOCK ();
          VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
                getpid (), session_index);
-         rv = VPPCOM_EBADFD;
-         VCL_SESSION_UNLOCK ();
-         goto done;
+         return VPPCOM_EBADFD;
        }
       ep->is_ip4 = session->transport.is_ip4;
       ep->port = session->transport.rmt_port;
-      if (session->transport.is_ip4)
-       clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
-                    sizeof (ip4_address_t));
-      else
-       clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
-                    sizeof (ip6_address_t));
-      VCL_SESSION_UNLOCK ();
     }
 
   if (flags == 0)
@@ -2908,10 +3023,19 @@ vppcom_session_recvfrom (uint32_t session_index, void *buffer,
     {
       clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
                    getpid (), flags);
-      rv = VPPCOM_EAFNOSUPPORT;
+      return VPPCOM_EAFNOSUPPORT;
+    }
+
+  if (ep)
+    {
+      if (session->transport.is_ip4)
+       clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
+                    sizeof (ip4_address_t));
+      else
+       clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
+                    sizeof (ip6_address_t));
     }
 
-done:
   return rv;
 }
 
@@ -2959,17 +3083,16 @@ vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
        {
          ASSERT (vp[i].revents);
 
-         VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
-         VCL_SESSION_UNLOCK ();
+         session = vcl_session_get (vp[i].sid);
+         if (!session)
+           continue;
 
          if (*vp[i].revents)
            *vp[i].revents = 0;
 
          if (POLLIN & vp[i].events)
            {
-             VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
              rv = vppcom_session_read_ready (session);
-             VCL_SESSION_UNLOCK ();
              if (rv > 0)
                {
                  *vp[i].revents |= POLLIN;
@@ -2993,9 +3116,7 @@ vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
 
          if (POLLOUT & vp[i].events)
            {
-             VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
              rv = vppcom_session_write_ready (session, vp[i].sid);
-             VCL_SESSION_UNLOCK ();
              if (rv > 0)
                {
                  *vp[i].revents |= POLLOUT;
@@ -3019,7 +3140,6 @@ vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
 
          if (0)                // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
            {
-           done:
              *vp[i].revents = POLLNVAL;
              num_ev++;
            }
@@ -3042,6 +3162,12 @@ vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
   return num_ev;
 }
 
+int
+vppcom_mq_epoll_fd (void)
+{
+  return vcm->mqs_epfd;
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *