tls: dtls initial implementation
[vpp.git] / src / vnet / session / session.c
index 4eb4f4a..85a802c 100644 (file)
@@ -18,7 +18,6 @@
  */
 
 #include <vnet/session/session.h>
-#include <vnet/session/session_debug.h>
 #include <vnet/session/application.h>
 #include <vnet/dpo/load_balance.h>
 #include <vnet/fib/ip4_fib.h>
@@ -79,7 +78,7 @@ session_send_evt_to_thread (void *data, void *args, u32 thread_index,
 int
 session_send_io_evt_to_thread (svm_fifo_t * f, session_evt_type_t evt_type)
 {
-  return session_send_evt_to_thread (&f->master_session_index, 0,
+  return session_send_evt_to_thread (&f->shr->master_session_index, 0,
                                     f->master_thread_index, evt_type);
 }
 
@@ -130,7 +129,8 @@ session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
   if (!(s->flags & SESSION_F_CUSTOM_TX))
     {
       s->flags |= SESSION_F_CUSTOM_TX;
-      if (svm_fifo_set_event (s->tx_fifo))
+      if (svm_fifo_set_event (s->tx_fifo)
+         || transport_connection_is_descheduled (tc))
        {
          session_worker_t *wrk;
          session_evt_elt_t *elt;
@@ -141,10 +141,24 @@ session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio)
            elt = session_evt_alloc_old (wrk);
          elt->evt.session_index = tc->s_index;
          elt->evt.event_type = SESSION_IO_EVT_TX;
+         tc->flags &= ~TRANSPORT_CONNECTION_F_DESCHED;
        }
     }
 }
 
+void
+sesssion_reschedule_tx (transport_connection_t * tc)
+{
+  session_worker_t *wrk = session_main_get_worker (tc->thread_index);
+  session_evt_elt_t *elt;
+
+  ASSERT (tc->thread_index == vlib_get_thread_index ());
+
+  elt = session_evt_alloc_new (wrk);
+  elt->evt.session_index = tc->s_index;
+  elt->evt.event_type = SESSION_IO_EVT_TX;
+}
+
 static void
 session_program_transport_ctrl_evt (session_t * s, session_evt_type_t evt)
 {
@@ -268,7 +282,21 @@ session_delete (session_t * s)
   session_free_w_fifos (s);
 }
 
-static session_t *
+void
+session_cleanup_half_open (transport_proto_t tp, session_handle_t ho_handle)
+{
+  transport_cleanup_half_open (tp, session_handle_index (ho_handle));
+}
+
+void
+session_half_open_delete_notify (transport_proto_t tp,
+                                session_handle_t ho_handle)
+{
+  app_worker_t *app_wrk = app_worker_get (session_handle_data (ho_handle));
+  app_worker_del_half_open (app_wrk, tp, ho_handle);
+}
+
+session_t *
 session_alloc_for_connection (transport_connection_t * tc)
 {
   session_t *s;
@@ -396,6 +424,24 @@ session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b,
   return 0;
 }
 
+void
+session_fifo_tuning (session_t * s, svm_fifo_t * f,
+                    session_ft_action_t act, u32 len)
+{
+  if (s->flags & SESSION_F_CUSTOM_FIFO_TUNING)
+    {
+      app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
+      app_worker_session_fifo_tuning (app_wrk, s, f, act, len);
+      if (CLIB_ASSERT_ENABLE)
+       {
+         segment_manager_t *sm;
+         sm = segment_manager_get (f->segment_manager);
+         ASSERT (f->shr->size >= 4096);
+         ASSERT (f->shr->size <= sm->max_fifo_size);
+       }
+    }
+}
+
 /*
  * Enqueue data for delivery to session peer. Does not notify peer of enqueue
  * event but on request can queue notification events for later delivery by
@@ -458,6 +504,8 @@ session_enqueue_stream_connection (transport_connection_t * tc,
          s->flags |= SESSION_F_RX_EVT;
          vec_add1 (wrk->session_to_enqueue[tc->proto], s->session_index);
        }
+
+      session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
     }
 
   return enqueued;
@@ -468,22 +516,49 @@ session_enqueue_dgram_connection (session_t * s,
                                  session_dgram_hdr_t * hdr,
                                  vlib_buffer_t * b, u8 proto, u8 queue_event)
 {
-  int enqueued = 0, rv, in_order_off;
+  int rv;
 
   ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo)
          >= b->current_length + sizeof (*hdr));
 
-  svm_fifo_enqueue (s->rx_fifo, sizeof (session_dgram_hdr_t), (u8 *) hdr);
-  enqueued = svm_fifo_enqueue (s->rx_fifo, b->current_length,
-                              vlib_buffer_get_current (b));
-  if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0))
+  if (PREDICT_TRUE (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)))
     {
-      in_order_off = enqueued > b->current_length ? enqueued : 0;
-      rv = session_enqueue_chain_tail (s, b, in_order_off, 1);
-      if (rv > 0)
-       enqueued += rv;
+      /* *INDENT-OFF* */
+      svm_fifo_seg_t segs[2] = {
+         { (u8 *) hdr, sizeof (*hdr) },
+         { vlib_buffer_get_current (b), b->current_length }
+      };
+      /* *INDENT-ON* */
+
+      rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, 2,
+                                     0 /* allow_partial */ );
     }
-  if (queue_event)
+  else
+    {
+      vlib_main_t *vm = vlib_get_main ();
+      svm_fifo_seg_t *segs = 0, *seg;
+      vlib_buffer_t *it = b;
+      u32 n_segs = 1;
+
+      vec_add2 (segs, seg, 1);
+      seg->data = (u8 *) hdr;
+      seg->len = sizeof (*hdr);
+      while (it)
+       {
+         vec_add2 (segs, seg, 1);
+         seg->data = vlib_buffer_get_current (it);
+         seg->len = it->current_length;
+         n_segs++;
+         if (!(it->flags & VLIB_BUFFER_NEXT_PRESENT))
+           break;
+         it = vlib_get_buffer (vm, it->next_buffer);
+       }
+      rv = svm_fifo_enqueue_segments (s->rx_fifo, segs, n_segs,
+                                     0 /* allow partial */ );
+      vec_free (segs);
+    }
+
+  if (queue_event && rv > 0)
     {
       /* Queue RX event on this fifo. Eventually these will need to be flushed
        * by calling stream_server_flush_enqueue_events () */
@@ -495,8 +570,10 @@ session_enqueue_dgram_connection (session_t * s,
          s->flags |= SESSION_F_RX_EVT;
          vec_add1 (wrk->session_to_enqueue[proto], s->session_index);
        }
+
+      session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED, 0);
     }
-  return enqueued;
+  return rv > 0 ? rv : 0;
 }
 
 int
@@ -514,6 +591,7 @@ session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes)
   u32 rv;
 
   rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes);
+  session_fifo_tuning (s, s->tx_fifo, SESSION_FT_ACTION_DEQUEUED, rv);
 
   if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes))
     session_dequeue_notify (s);
@@ -533,9 +611,9 @@ session_notify_subscribers (u32 app_index, session_t * s,
   if (!app)
     return -1;
 
-  for (i = 0; i < f->n_subscribers; i++)
+  for (i = 0; i < f->shr->n_subscribers; i++)
     {
-      app_wrk = application_get_worker (app, f->subscribers[i]);
+      app_wrk = application_get_worker (app, f->shr->subscribers[i]);
       if (!app_wrk)
        continue;
       if (app_worker_lock_and_send_event (app_wrk, s, evt_type))
@@ -573,6 +651,11 @@ session_enqueue_notify_inline (session_t * s)
   SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo));
 
   s->flags &= ~SESSION_F_RX_EVT;
+
+  /* Application didn't confirm accept yet */
+  if (PREDICT_FALSE (s->session_state == SESSION_STATE_ACCEPTING))
+    return 0;
+
   if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s,
                                                     SESSION_IO_EVT_RX)))
     return -1;
@@ -640,7 +723,7 @@ session_dequeue_notify (session_t * s)
                                                     SESSION_IO_EVT_TX)))
     return -1;
 
-  if (PREDICT_FALSE (s->tx_fifo->n_subscribers))
+  if (PREDICT_FALSE (s->tx_fifo->shr->n_subscribers))
     return session_notify_subscribers (app_wrk->app_index, s,
                                       s->tx_fifo, SESSION_IO_EVT_TX);
 
@@ -674,6 +757,9 @@ session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index)
          continue;
        }
 
+      session_fifo_tuning (s, s->rx_fifo, SESSION_FT_ACTION_ENQUEUED,
+                          0 /* TODO/not needed */ );
+
       if (PREDICT_FALSE (session_enqueue_notify_inline (s)))
        errors++;
     }
@@ -694,14 +780,14 @@ session_main_flush_all_enqueue_events (u8 transport_proto)
   return errors;
 }
 
-static inline int
-session_stream_connect_notify_inline (transport_connection_t * tc, u8 is_fail,
-                                     session_state_t opened_state)
+int
+session_stream_connect_notify (transport_connection_t * tc,
+                              session_error_t err)
 {
+  session_handle_t ho_handle, wrk_handle;
   u32 opaque = 0, new_ti, new_si;
   app_worker_t *app_wrk;
   session_t *s = 0;
-  u64 ho_handle;
 
   /*
    * Find connection handle and cleanup half-open table
@@ -717,14 +803,22 @@ session_stream_connect_notify_inline (transport_connection_t * tc, u8 is_fail,
   /* Get the app's index from the handle we stored when opening connection
    * and the opaque (api_context for external apps) from transport session
    * index */
-  app_wrk = app_worker_get_if_valid (ho_handle >> 32);
+  app_wrk = app_worker_get_if_valid (session_handle_data (ho_handle));
   if (!app_wrk)
     return -1;
 
-  opaque = tc->s_index;
+  wrk_handle = app_worker_lookup_half_open (app_wrk, tc->proto, ho_handle);
+  if (wrk_handle == SESSION_INVALID_HANDLE)
+    return -1;
+
+  /* Make sure this is the same half-open index */
+  if (session_handle_index (wrk_handle) != session_handle_index (ho_handle))
+    return -1;
+
+  opaque = session_handle_data (wrk_handle);
 
-  if (is_fail)
-    return app_worker_connect_notify (app_wrk, s, opaque);
+  if (err)
+    return app_worker_connect_notify (app_wrk, s, err, opaque);
 
   s = session_alloc_for_connection (tc);
   s->session_state = SESSION_STATE_CONNECTING;
@@ -732,39 +826,42 @@ session_stream_connect_notify_inline (transport_connection_t * tc, u8 is_fail,
   new_si = s->session_index;
   new_ti = s->thread_index;
 
-  if (app_worker_init_connected (app_wrk, s))
+  if ((err = app_worker_init_connected (app_wrk, s)))
     {
       session_free (s);
-      app_worker_connect_notify (app_wrk, 0, opaque);
+      app_worker_connect_notify (app_wrk, 0, err, opaque);
       return -1;
     }
 
   s = session_get (new_si, new_ti);
-  s->session_state = opened_state;
+  s->session_state = SESSION_STATE_READY;
   session_lookup_add_connection (tc, session_handle (s));
 
-  if (app_worker_connect_notify (app_wrk, s, opaque))
+  if (app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque))
     {
+      session_lookup_del_connection (tc);
+      /* Avoid notifying app about rejected session cleanup */
       s = session_get (new_si, new_ti);
-      session_free_w_fifos (s);
+      segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
+      session_free (s);
       return -1;
     }
 
   return 0;
 }
 
-int
-session_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
+static void
+session_switch_pool_reply (void *arg)
 {
-  return session_stream_connect_notify_inline (tc, is_fail,
-                                              SESSION_STATE_READY);
-}
+  u32 session_index = pointer_to_uword (arg);
+  session_t *s;
 
-int
-session_ho_stream_connect_notify (transport_connection_t * tc, u8 is_fail)
-{
-  return session_stream_connect_notify_inline (tc, is_fail,
-                                              SESSION_STATE_OPENED);
+  s = session_get_if_valid (session_index, vlib_get_thread_index ());
+  if (!s)
+    return;
+
+  /* Notify app that it has data on the new session */
+  session_enqueue_notify (s);
 }
 
 typedef struct _session_switch_pool_args
@@ -782,28 +879,38 @@ static void
 session_switch_pool (void *cb_args)
 {
   session_switch_pool_args_t *args = (session_switch_pool_args_t *) cb_args;
+  session_handle_t new_sh;
+  segment_manager_t *sm;
   app_worker_t *app_wrk;
   session_t *s;
+  void *rargs;
 
   ASSERT (args->thread_index == vlib_get_thread_index ());
   s = session_get (args->session_index, args->thread_index);
-  s->tx_fifo->master_session_index = args->new_session_index;
-  s->tx_fifo->master_thread_index = args->new_thread_index;
+
   transport_cleanup (session_get_transport_proto (s), s->connection_index,
                     s->thread_index);
 
+  new_sh = session_make_handle (args->new_session_index,
+                               args->new_thread_index);
+
   app_wrk = app_worker_get_if_valid (s->app_wrk_index);
   if (app_wrk)
     {
-      session_handle_t new_sh;
-      new_sh = session_make_handle (args->new_session_index,
-                                   args->new_thread_index);
-      app_worker_migrate_notify (app_wrk, s, new_sh);
+      /* Cleanup fifo segment slice state for fifos */
+      sm = app_worker_get_connect_segment_manager (app_wrk);
+      segment_manager_detach_fifo (sm, &s->rx_fifo);
+      segment_manager_detach_fifo (sm, &s->tx_fifo);
 
-      /* Trigger app read on the new thread */
-      session_enqueue_notify_thread (new_sh);
+      /* Notify app, using old session, about the migration event */
+      app_worker_migrate_notify (app_wrk, s, new_sh);
     }
 
+  /* Trigger app read and fifo updates on the new thread */
+  rargs = uword_to_pointer (args->new_session_index, void *);
+  session_send_rpc_evt_to_thread (args->new_thread_index,
+                                 session_switch_pool_reply, rargs);
+
   session_free (s);
   clib_mem_free (cb_args);
 }
@@ -817,17 +924,28 @@ session_dgram_connect_notify (transport_connection_t * tc,
 {
   session_t *new_s;
   session_switch_pool_args_t *rpc_args;
+  segment_manager_t *sm;
+  app_worker_t *app_wrk;
 
   /*
    * Clone half-open session to the right thread.
    */
   new_s = session_clone_safe (tc->s_index, old_thread_index);
   new_s->connection_index = tc->c_index;
-  new_s->rx_fifo->master_session_index = new_s->session_index;
-  new_s->rx_fifo->master_thread_index = new_s->thread_index;
   new_s->session_state = SESSION_STATE_READY;
   new_s->flags |= SESSION_F_IS_MIGRATING;
-  session_lookup_add_connection (tc, session_handle (new_s));
+
+  if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
+    session_lookup_add_connection (tc, session_handle (new_s));
+
+  app_wrk = app_worker_get_if_valid (new_s->app_wrk_index);
+  if (app_wrk)
+    {
+      /* New set of fifos attached to the same shared memory */
+      sm = app_worker_get_connect_segment_manager (app_wrk);
+      segment_manager_attach_fifo (sm, &new_s->rx_fifo, new_s);
+      segment_manager_attach_fifo (sm, &new_s->tx_fifo, new_s);
+    }
 
   /*
    * Ask thread owning the old session to clean it up and make us the tx
@@ -1003,7 +1121,14 @@ session_stream_accept_notify (transport_connection_t * tc)
   if (!app_wrk)
     return -1;
   s->session_state = SESSION_STATE_ACCEPTING;
-  return app_worker_accept_notify (app_wrk, s);
+  if (app_worker_accept_notify (app_wrk, s))
+    {
+      /* On transport delete, no notifications should be sent. Unless, the
+       * accept is retried and successful. */
+      s->session_state = SESSION_STATE_CREATED;
+      return -1;
+    }
+  return 0;
 }
 
 /**
@@ -1021,7 +1146,10 @@ session_stream_accept (transport_connection_t * tc, u32 listener_index,
   s->session_state = SESSION_STATE_CREATED;
 
   if ((rv = app_worker_init_accepted (s)))
-    return rv;
+    {
+      session_free (s);
+      return rv;
+    }
 
   session_lookup_add_connection (tc, session_handle (s));
 
@@ -1029,12 +1157,51 @@ session_stream_accept (transport_connection_t * tc, u32 listener_index,
   if (notify)
     {
       app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
-      return app_worker_accept_notify (app_wrk, s);
+      if ((rv = app_worker_accept_notify (app_wrk, s)))
+       {
+         session_lookup_del_session (s);
+         segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
+         session_free (s);
+         return rv;
+       }
     }
 
   return 0;
 }
 
+int
+session_dgram_accept (transport_connection_t * tc, u32 listener_index,
+                     u32 thread_index)
+{
+  app_worker_t *app_wrk;
+  session_t *s;
+  int rv;
+
+  s = session_alloc_for_connection (tc);
+  s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index;
+
+  if ((rv = app_worker_init_accepted (s)))
+    {
+      session_free (s);
+      return rv;
+    }
+
+  session_lookup_add_connection (tc, session_handle (s));
+
+  app_wrk = app_worker_get (s->app_wrk_index);
+  if ((rv = app_worker_accept_notify (app_wrk, s)))
+    {
+      session_lookup_del_session (s);
+      segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo);
+      session_free (s);
+      return rv;
+    }
+
+  s->session_state = SESSION_STATE_READY;
+
+  return 0;
+}
+
 int
 session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
 {
@@ -1050,7 +1217,7 @@ session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
   if (rv < 0)
     {
       SESSION_DBG ("Transport failed to open connection.");
-      return VNET_API_ERROR_SESSION_CONNECT;
+      return rv;
     }
 
   tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
@@ -1068,7 +1235,7 @@ session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
 
   sh = session_handle (s);
   session_lookup_add_connection (tc, sh);
-  return app_worker_connect_notify (app_wrk, s, opaque);
+  return app_worker_connect_notify (app_wrk, s, SESSION_E_NONE, opaque);
 }
 
 int
@@ -1076,7 +1243,7 @@ session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
 {
   transport_connection_t *tc;
   transport_endpoint_cfg_t *tep;
-  u64 handle;
+  u64 handle, wrk_handle;
   int rv;
 
   tep = session_endpoint_to_transport_cfg (rmt);
@@ -1084,7 +1251,7 @@ session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
   if (rv < 0)
     {
       SESSION_DBG ("Transport failed to open connection.");
-      return VNET_API_ERROR_SESSION_CONNECT;
+      return rv;
     }
 
   tc = transport_get_half_open (rmt->transport_proto, (u32) rv);
@@ -1096,15 +1263,21 @@ session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque)
    * is needed when the connect notify comes and we have to notify the
    * external app
    */
-  handle = (((u64) app_wrk_index) << 32) | (u64) tc->c_index;
+  handle = session_make_handle (tc->c_index, app_wrk_index);
   session_lookup_add_half_open (tc, handle);
 
-  /* Store api_context (opaque) for when the reply comes. Not the nicest
-   * thing but better than allocating a separate half-open pool.
+  /* Store the half-open handle in the connection. Transport will use it
+   * when cleaning up @ref session_half_open_delete_notify
    */
-  tc->s_index = opaque;
-  if (transport_half_open_has_fifos (rmt->transport_proto))
-    return session_ho_stream_connect_notify (tc, 0 /* is_fail */ );
+  tc->s_ho_handle = handle;
+
+  /* Track the half-open connections in case we want to forcefully
+   * clean them up @ref session_cleanup_half_open
+   */
+  wrk_handle = session_make_handle (tc->c_index, opaque);
+  app_worker_add_half_open (app_worker_get (app_wrk_index),
+                           rmt->transport_proto, handle, wrk_handle);
+
   return 0;
 }
 
@@ -1163,7 +1336,8 @@ int
 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
 {
   transport_endpoint_t *tep;
-  u32 tc_index, s_index;
+  int tc_index;
+  u32 s_index;
 
   /* Transport bind/listen */
   tep = session_endpoint_to_transport (sep);
@@ -1171,8 +1345,8 @@ session_listen (session_t * ls, session_endpoint_cfg_t * sep)
   tc_index = transport_start_listen (session_get_transport_proto (ls),
                                     s_index, tep);
 
-  if (tc_index == (u32) ~ 0)
-    return -1;
+  if (tc_index < 0)
+    return tc_index;
 
   /* Attach transport to session. Lookup tables are populated by the app
    * worker because local tables (for ct sessions) are not backed by a fib */
@@ -1194,14 +1368,17 @@ session_stop_listen (session_t * s)
   transport_connection_t *tc;
 
   if (s->session_state != SESSION_STATE_LISTENING)
-    return -1;
+    return SESSION_E_NOLISTEN;
 
   tc = transport_get_listener (tp, s->connection_index);
+
+  /* If no transport, assume everything was cleaned up already */
   if (!tc)
-    return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
+    return SESSION_E_NONE;
 
   if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
     session_lookup_del_connection (tc);
+
   transport_stop_listen (tp, s->connection_index);
   return 0;
 }
@@ -1322,48 +1499,40 @@ session_transport_cleanup (session_t * s)
 /**
  * Allocate event queues in the shared-memory segment
  *
- * That can either be a newly created memfd segment, that will need to be
- * mapped by all stack users, or the binary api's svm region. The latter is
- * assumed to be already mapped. NOTE that this assumption DOES NOT hold if
- * api clients bootstrap shm api over sockets (i.e. use memfd segments) and
- * vpp uses api svm region for event queues.
+ * That can only be a newly created memfd segment, that must be
+ * mapped by all apps/stack users.
  */
 void
 session_vpp_event_queues_allocate (session_main_t * smm)
 {
   u32 evt_q_length = 2048, evt_size = sizeof (session_event_t);
-  ssvm_private_t *eqs = &smm->evt_qs_segment;
-  api_main_t *am = &api_main;
+  fifo_segment_t *eqs = &smm->evt_qs_segment;
   uword eqs_size = 64 << 20;
   pid_t vpp_pid = getpid ();
-  void *oldheap;
   int i;
 
   if (smm->configured_event_queue_length)
     evt_q_length = smm->configured_event_queue_length;
 
-  if (smm->evt_qs_use_memfd_seg)
-    {
-      if (smm->evt_qs_segment_size)
-       eqs_size = smm->evt_qs_segment_size;
+  if (smm->evt_qs_segment_size)
+    eqs_size = smm->evt_qs_segment_size;
 
-      eqs->ssvm_size = eqs_size;
-      eqs->i_am_master = 1;
-      eqs->my_pid = vpp_pid;
-      eqs->name = format (0, "%s%c", "evt-qs-segment", 0);
-      eqs->requested_va = smm->session_baseva;
+  eqs->ssvm.ssvm_size = eqs_size;
+  eqs->ssvm.my_pid = vpp_pid;
+  eqs->ssvm.name = format (0, "%s%c", "session: evt-qs-segment", 0);
+  /* clib_mem_vm_map_shared consumes first page before requested_va */
+  eqs->ssvm.requested_va = smm->session_baseva + clib_mem_get_page_size ();
 
-      if (ssvm_master_init (eqs, SSVM_SEGMENT_MEMFD))
-       {
-         clib_warning ("failed to initialize queue segment");
-         return;
-       }
+  if (ssvm_server_init (&eqs->ssvm, SSVM_SEGMENT_MEMFD))
+    {
+      clib_warning ("failed to initialize queue segment");
+      return;
     }
 
-  if (smm->evt_qs_use_memfd_seg)
-    oldheap = ssvm_push_heap (eqs->sh);
-  else
-    oldheap = svm_push_data_heap (am->vlib_rp);
+  fifo_segment_init (eqs);
+
+  /* Special fifo segment that's filled only with mqs */
+  eqs->h->n_mqs = vec_len (smm->wrk);
 
   for (i = 0; i < vec_len (smm->wrk); i++)
     {
@@ -1377,27 +1546,15 @@ session_vpp_event_queues_allocate (session_main_t * smm)
       cfg->n_rings = 2;
       cfg->q_nitems = evt_q_length;
       cfg->ring_cfgs = rc;
-      smm->wrk[i].vpp_event_queue = svm_msg_q_alloc (cfg);
-      if (smm->evt_qs_use_memfd_seg)
-       {
-         if (svm_msg_q_alloc_consumer_eventfd (smm->wrk[i].vpp_event_queue))
-           clib_warning ("eventfd returned");
-       }
-    }
 
-  if (smm->evt_qs_use_memfd_seg)
-    ssvm_pop_heap (oldheap);
-  else
-    svm_pop_heap (oldheap);
+      smm->wrk[i].vpp_event_queue = fifo_segment_msg_q_alloc (eqs, i, cfg);
+    }
 }
 
-ssvm_private_t *
+fifo_segment_t *
 session_main_get_evt_q_segment (void)
 {
-  session_main_t *smm = &session_main;
-  if (smm->evt_qs_use_memfd_seg)
-    return &smm->evt_qs_segment;
-  return 0;
+  return &session_main.evt_qs_segment;
 }
 
 u64
@@ -1422,12 +1579,6 @@ static session_fifo_rx_fn *session_tx_fns[TRANSPORT_TX_N_FNS] = {
 };
 /* *INDENT-ON* */
 
-/**
- * Initialize session layer for given transport proto and ip version
- *
- * Allocates per session type (transport proto + ip version) data structures
- * and adds arc from session queue node to session type output node.
- */
 void
 session_register_transport (transport_proto_t transport_proto,
                            const transport_proto_vft_t * vft, u8 is_ip4,
@@ -1458,6 +1609,24 @@ session_register_transport (transport_proto_t transport_proto,
     session_tx_fns[vft->transport_options.tx_type];
 }
 
+transport_proto_t
+session_add_transport_proto (void)
+{
+  session_main_t *smm = &session_main;
+  session_worker_t *wrk;
+  u32 thread;
+
+  smm->last_transport_proto_type += 1;
+
+  for (thread = 0; thread < vec_len (smm->wrk); thread++)
+    {
+      wrk = session_main_get_worker (thread);
+      vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
+    }
+
+  return smm->last_transport_proto_type;
+}
+
 transport_connection_t *
 session_get_transport (session_t * s)
 {
@@ -1489,23 +1658,26 @@ listen_session_get_transport (session_t * s)
 }
 
 void
-session_flush_frames_main_thread (vlib_main_t * vm)
+session_queue_run_on_main_thread (vlib_main_t * vm)
 {
   ASSERT (vlib_get_thread_index () == 0);
   vlib_process_signal_event_mt (vm, session_queue_process_node.index,
-                               SESSION_Q_PROCESS_FLUSH_FRAMES, 0);
+                               SESSION_Q_PROCESS_RUN_ON_MAIN, 0);
 }
 
 static clib_error_t *
 session_manager_main_enable (vlib_main_t * vm)
 {
-  segment_manager_main_init_args_t _sm_args = { 0 }, *sm_args = &_sm_args;
   session_main_t *smm = &session_main;
   vlib_thread_main_t *vtm = vlib_get_thread_main ();
   u32 num_threads, preallocated_sessions_per_worker;
   session_worker_t *wrk;
   int i;
 
+  /* We only initialize once and do not de-initialized on disable */
+  if (smm->is_initialized)
+    goto done;
+
   num_threads = 1 /* main thread */  + vtm->n_threads;
 
   if (num_threads < 1)
@@ -1521,8 +1693,9 @@ session_manager_main_enable (vlib_main_t * vm)
       wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list);
       wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list);
       wrk->vm = vlib_mains[i];
-      wrk->last_vlib_time = vlib_time_now (vlib_mains[i]);
+      wrk->last_vlib_time = vlib_time_now (vm);
       wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
+      vec_validate (wrk->session_to_enqueue, smm->last_transport_proto_type);
 
       if (num_threads > 1)
        clib_rwlock_init (&smm->wrk[i].peekers_rw_locks);
@@ -1531,10 +1704,8 @@ session_manager_main_enable (vlib_main_t * vm)
   /* Allocate vpp event queues segment and queue */
   session_vpp_event_queues_allocate (smm);
 
-  /* Initialize fifo segment main baseva and timeout */
-  sm_args->baseva = smm->session_baseva + smm->evt_qs_segment_size;
-  sm_args->size = smm->session_va_space_size;
-  segment_manager_main_init (sm_args);
+  /* Initialize segment manager properties */
+  segment_manager_main_init ();
 
   /* Preallocate sessions */
   if (smm->preallocated_sessions)
@@ -1561,14 +1732,25 @@ session_manager_main_enable (vlib_main_t * vm)
   session_lookup_init ();
   app_namespaces_init ();
   transport_init ();
+  smm->is_initialized = 1;
+
+done:
 
   smm->is_enabled = 1;
 
   /* Enable transports */
   transport_enable_disable (vm, 1);
+  session_debug_init ();
+
   return 0;
 }
 
+static void
+session_manager_main_disable (vlib_main_t * vm)
+{
+  transport_enable_disable (vm, 0 /* is_en */ );
+}
+
 void
 session_node_enable_disable (u8 is_en)
 {
@@ -1580,10 +1762,10 @@ session_node_enable_disable (u8 is_en)
   foreach_vlib_main (({
     if (have_workers && ii == 0)
       {
-       vlib_node_set_state (this_vlib_main, session_queue_process_node.index,
-                            state);
        if (is_en)
          {
+           vlib_node_set_state (this_vlib_main,
+                                session_queue_process_node.index, state);
            vlib_node_t *n = vlib_get_node (this_vlib_main,
                                            session_queue_process_node.index);
            vlib_start_process (this_vlib_main, n->runtime_index);
@@ -1594,8 +1776,8 @@ session_node_enable_disable (u8 is_en)
                                          session_queue_process_node.index,
                                          SESSION_Q_PROCESS_STOP, 0);
          }
-
-       continue;
+       if (!session_main.poll_main)
+         continue;
       }
     vlib_node_set_state (this_vlib_main, session_queue_node.index,
                          state);
@@ -1618,6 +1800,7 @@ vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
   else
     {
       session_main.is_enabled = 0;
+      session_manager_main_disable (vm);
       session_node_enable_disable (is_en);
     }
 
@@ -1625,10 +1808,15 @@ vnet_session_enable_disable (vlib_main_t * vm, u8 is_en)
 }
 
 clib_error_t *
-session_manager_main_init (vlib_main_t * vm)
+session_main_init (vlib_main_t * vm)
 {
   session_main_t *smm = &session_main;
+
+  smm->is_enabled = 0;
+  smm->session_enable_asap = 0;
+  smm->poll_main = 0;
   smm->session_baseva = HIGH_SEGMENT_BASEVA;
+
 #if (HIGH_SEGMENT_BASEVA > (4ULL << 30))
   smm->session_va_space_size = 128ULL << 30;
   smm->evt_qs_segment_size = 64 << 20;
@@ -1636,13 +1824,14 @@ session_manager_main_init (vlib_main_t * vm)
   smm->session_va_space_size = 128 << 20;
   smm->evt_qs_segment_size = 1 << 20;
 #endif
-  smm->is_enabled = 0;
-  smm->session_enable_asap = 0;
+
+  smm->last_transport_proto_type = TRANSPORT_PROTO_DTLS;
+
   return 0;
 }
 
 static clib_error_t *
-session_main_init (vlib_main_t * vm)
+session_main_loop_init (vlib_main_t * vm)
 {
   session_main_t *smm = &session_main;
   if (smm->session_enable_asap)
@@ -1654,8 +1843,8 @@ session_main_init (vlib_main_t * vm)
   return 0;
 }
 
-VLIB_INIT_FUNCTION (session_manager_main_init);
-VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_init);
+VLIB_INIT_FUNCTION (session_main_init);
+VLIB_MAIN_LOOP_ENTER_FUNCTION (session_main_loop_init);
 
 static clib_error_t *
 session_config_fn (vlib_main_t * vm, unformat_input_t * input)
@@ -1731,13 +1920,20 @@ session_config_fn (vlib_main_t * vm, unformat_input_t * input)
       else if (unformat (input, "local-endpoints-table-buckets %d",
                         &smm->local_endpoints_table_buckets))
        ;
+      /* Deprecated but maintained for compatibility */
       else if (unformat (input, "evt_qs_memfd_seg"))
-       smm->evt_qs_use_memfd_seg = 1;
+       ;
       else if (unformat (input, "evt_qs_seg_size %U", unformat_memory_size,
                         &smm->evt_qs_segment_size))
        ;
       else if (unformat (input, "enable"))
        smm->session_enable_asap = 1;
+      else if (unformat (input, "segment-baseva 0x%lx", &smm->session_baseva))
+       ;
+      else if (unformat (input, "use-app-socket-api"))
+       appns_sapi_enable ();
+      else if (unformat (input, "poll-main"))
+       smm->poll_main = 1;
       else
        return clib_error_return (0, "unknown input `%U'",
                                  format_unformat_error, input);