session: add unix socket api for app attachment
[vpp.git] / src / vnet / session / session_api.c
index 153c528..15c5812 100644 (file)
@@ -22,6 +22,8 @@
 #include <vnet/session/session_table.h>
 #include <vnet/session/session.h>
 
+#include <vnet/ip/ip_types_api.h>
+
 #include <vnet/vnet_msg_enum.h>
 
 #define vl_typedefs            /* define message structures */
 #include <vlibapi/api_helper_macros.h>
 
 #define foreach_session_api_msg                                         \
-_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply)                 \
-_(APPLICATION_ATTACH, application_attach)                              \
 _(APP_ATTACH, app_attach)                                              \
 _(APPLICATION_DETACH, application_detach)                              \
-_(BIND_URI, bind_uri)                                                   \
-_(UNBIND_URI, unbind_uri)                                               \
-_(CONNECT_URI, connect_uri)                                             \
-_(DISCONNECT_SESSION, disconnect_session)                               \
-_(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
-_(BIND_SOCK, bind_sock)                                                        \
-_(UNBIND_SOCK, unbind_sock)                                             \
-_(CONNECT_SOCK, connect_sock)                                                  \
 _(SESSION_ENABLE_DISABLE, session_enable_disable)                      \
 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)                                \
 _(SESSION_RULE_ADD_DEL, session_rule_add_del)                          \
@@ -63,94 +55,54 @@ _(APP_ADD_CERT_KEY_PAIR, app_add_cert_key_pair)                             \
 _(APP_DEL_CERT_KEY_PAIR, app_del_cert_key_pair)                                \
 _(APP_WORKER_ADD_DEL, app_worker_add_del)                              \
 
-static int
-session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
+static transport_proto_t
+api_session_transport_proto_decode (const vl_api_transport_proto_t * api_tp)
 {
-  clib_error_t *error;
-  if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
-    {
-      clib_warning ("can't send memfd fd");
-      return -1;
-    }
-  error = vl_api_send_fd_msg (reg, fds, n_fds);
-  if (error)
-    {
-      clib_error_report (error);
-      return -1;
+  switch (*api_tp)
+    {
+    case TRANSPORT_PROTO_API_TCP:
+      return TRANSPORT_PROTO_TCP;
+    case TRANSPORT_PROTO_API_UDP:
+      return TRANSPORT_PROTO_UDP;
+    case TRANSPORT_PROTO_API_TLS:
+      return TRANSPORT_PROTO_TLS;
+    case TRANSPORT_PROTO_API_QUIC:
+      return TRANSPORT_PROTO_QUIC;
+    default:
+      return TRANSPORT_PROTO_NONE;
     }
-  return 0;
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
-static int
-send_add_segment_callback (u32 api_client_index, u64 segment_handle)
+static vl_api_transport_proto_t
+api_session_transport_proto_encode (const transport_proto_t tp)
 {
-  int fds[SESSION_N_FD_TYPE], n_fds = 0;
-  vl_api_map_another_segment_t *mp;
-  vl_api_registration_t *reg;
-  fifo_segment_t *fs;
-  ssvm_private_t *sp;
-  u8 fd_flags = 0;
-
-  reg = vl_mem_api_client_index_to_registration (api_client_index);
-  if (!reg)
-    {
-      clib_warning ("no api registration for client: %u", api_client_index);
-      return -1;
-    }
-
-  fs = segment_manager_get_segment_w_handle (segment_handle);
-  sp = &fs->ssvm;
-  if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
-    {
-      if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
-       {
-         clib_warning ("can't send memfd fd");
-         return -1;
-       }
-
-      fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
-      fds[n_fds] = sp->fd;
-      n_fds += 1;
+  switch (tp)
+    {
+    case TRANSPORT_PROTO_TCP:
+      return TRANSPORT_PROTO_API_TCP;
+    case TRANSPORT_PROTO_UDP:
+      return TRANSPORT_PROTO_API_UDP;
+    case TRANSPORT_PROTO_TLS:
+      return TRANSPORT_PROTO_API_TLS;
+    case TRANSPORT_PROTO_QUIC:
+      return TRANSPORT_PROTO_API_QUIC;
+    default:
+      return TRANSPORT_PROTO_API_NONE;
     }
-
-  mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
-  clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
-  mp->segment_size = sp->ssvm_size;
-  mp->fd_flags = fd_flags;
-  mp->segment_handle = clib_host_to_net_u64 (segment_handle);
-  strncpy ((char *) mp->segment_name, (char *) sp->name,
-          sizeof (mp->segment_name) - 1);
-
-  vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
-
-  if (n_fds)
-    return session_send_fds (reg, fds, n_fds);
-
-  return 0;
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static int
-send_del_segment_callback (u32 api_client_index, u64 segment_handle)
+session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
 {
-  vl_api_unmap_segment_t *mp;
-  vl_api_registration_t *reg;
-
-  reg = vl_mem_api_client_index_to_registration (api_client_index);
-  if (!reg)
+  clib_error_t *error;
+  if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
+    return SESSION_E_BAPI_NO_FD;
+  error = vl_api_send_fd_msg (reg, fds, n_fds);
+  if (error)
     {
-      clib_warning ("no registration: %u", api_client_index);
-      return -1;
+      clib_error_report (error);
+      return SESSION_E_BAPI_SEND_FD;
     }
-
-  mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
-  clib_memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
-  mp->segment_handle = clib_host_to_net_u64 (segment_handle);
-  vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
-
   return 0;
 }
 
@@ -187,7 +139,7 @@ mq_send_session_accepted_cb (session_t * s)
   app = application_get (app_wrk->app_index);
   app_mq = app_wrk->event_queue;
   if (mq_try_lock_and_alloc_msg (app_mq, msg))
-    return -1;
+    return SESSION_E_MQ_MSG_ALLOC;
 
   evt = svm_msg_q_msg_data (app_mq, msg);
   clib_memset (evt, 0, sizeof (*evt));
@@ -306,7 +258,7 @@ mq_send_session_reset_cb (session_t * s)
 
 int
 mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
-                             session_t * s, u8 is_fail)
+                             session_t * s, session_error_t err)
 {
   svm_msg_q_msg_t _msg, *msg = &_msg;
   session_connected_msg_t *mp;
@@ -325,7 +277,7 @@ mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
     }
 
   if (mq_try_lock_and_alloc_msg (app_mq, msg))
-    return -1;
+    return SESSION_E_MQ_MSG_ALLOC;
 
   evt = svm_msg_q_msg_data (app_mq, msg);
   clib_memset (evt, 0, sizeof (*evt));
@@ -334,7 +286,7 @@ mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
   clib_memset (mp, 0, sizeof (*mp));
   mp->context = api_context;
 
-  if (is_fail)
+  if (err)
     goto done;
 
   if (session_has_transport (s))
@@ -342,7 +294,8 @@ mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
       tc = session_get_transport (s);
       if (!tc)
        {
-         is_fail = 1;
+         clib_warning ("failed to retrieve transport!");
+         err = SESSION_E_REFUSED;
          goto done;
        }
 
@@ -377,8 +330,7 @@ mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
     }
 
 done:
-  mp->retval = is_fail ?
-    clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
+  mp->retval = err;
 
   svm_msg_q_add_and_unlock (app_mq, msg);
   return 0;
@@ -406,7 +358,7 @@ mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
     }
 
   if (mq_try_lock_and_alloc_msg (app_mq, msg))
-    return -1;
+    return SESSION_E_MQ_MSG_ALLOC;
 
   evt = svm_msg_q_msg_data (app_mq, msg);
   clib_memset (evt, 0, sizeof (*evt));
@@ -470,7 +422,28 @@ mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
 static void
 mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
 {
-  clib_warning ("not supported");
+  svm_msg_q_msg_t _msg, *msg = &_msg;
+  session_migrated_msg_t *mp;
+  svm_msg_q_t *vpp_evt_q;
+  app_worker_t *app_wrk;
+  session_event_t *evt;
+  svm_msg_q_t *app_mq;
+
+  app_wrk = app_worker_get (s->app_wrk_index);
+  app_mq = app_wrk->event_queue;
+  if (mq_try_lock_and_alloc_msg (app_mq, msg))
+    return;
+
+  evt = svm_msg_q_msg_data (app_mq, msg);
+  clib_memset (evt, 0, sizeof (*evt));
+  evt->event_type = SESSION_CTRL_EVT_MIGRATED;
+  mp = (session_migrated_msg_t *) evt->data;
+  mp->handle = session_handle (s);
+  mp->new_handle = new_sh;
+  mp->vpp_thread_index = session_thread_from_handle (new_sh);
+  vpp_evt_q = session_main_get_vpp_event_queue (mp->vpp_thread_index);
+  mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
+  svm_msg_q_add_and_unlock (app_mq, msg);
 }
 
 static int
@@ -568,16 +541,36 @@ mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
   return 0;
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
-static session_cb_vft_t session_mq_cb_vft_old = {
-  .session_accept_callback = mq_send_session_accepted_cb,
-  .session_disconnect_callback = mq_send_session_disconnected_cb,
-  .session_connected_callback = mq_send_session_connected_cb,
-  .session_reset_callback = mq_send_session_reset_cb,
-  .session_migrate_callback = mq_send_session_migrate_cb,
-  .add_segment_callback = send_add_segment_callback,
-  .del_segment_callback = send_del_segment_callback,
-};
+static void
+mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
+{
+  svm_msg_q_msg_t _msg, *msg = &_msg;
+  session_cleanup_msg_t *mp;
+  svm_msg_q_t *app_mq;
+  session_event_t *evt;
+  app_worker_t *app_wrk;
+
+  /* Propagate transport cleanup notifications only if app didn't close */
+  if (ntf == SESSION_CLEANUP_TRANSPORT
+      && s->session_state != SESSION_STATE_TRANSPORT_DELETED)
+    return;
+
+  app_wrk = app_worker_get_if_valid (s->app_wrk_index);
+  if (!app_wrk)
+    return;
+
+  app_mq = app_wrk->event_queue;
+  if (mq_try_lock_and_alloc_msg (app_mq, msg))
+    return;
+
+  evt = svm_msg_q_msg_data (app_mq, msg);
+  clib_memset (evt, 0, sizeof (*evt));
+  evt->event_type = SESSION_CTRL_EVT_CLEANUP;
+  mp = (session_cleanup_msg_t *) evt->data;
+  mp->handle = session_handle (s);
+  mp->type = ntf;
+  svm_msg_q_add_and_unlock (app_mq, msg);
+}
 
 static session_cb_vft_t session_mq_cb_vft = {
   .session_accept_callback = mq_send_session_accepted_cb,
@@ -585,6 +578,7 @@ static session_cb_vft_t session_mq_cb_vft = {
   .session_connected_callback = mq_send_session_connected_cb,
   .session_reset_callback = mq_send_session_reset_cb,
   .session_migrate_callback = mq_send_session_migrate_cb,
+  .session_cleanup_callback = mq_send_session_cleanup_cb,
   .add_segment_callback = mq_send_add_segment_cb,
   .del_segment_callback = mq_send_del_segment_cb,
 };
@@ -600,22 +594,22 @@ vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
   REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
+vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
 {
   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
-  vl_api_application_attach_reply_t *rmp;
+  vl_api_app_attach_reply_t *rmp;
   ssvm_private_t *segp, *evt_q_segment;
   vnet_app_attach_args_t _a, *a = &_a;
+  u8 fd_flags = 0, ctrl_thread;
   vl_api_registration_t *reg;
-  u8 fd_flags = 0;
+  svm_msg_q_t *ctrl_mq;
 
   reg = vl_api_client_index_to_registration (mp->client_index);
   if (!reg)
     return;
 
-  if (session_main_is_enabled () == 0)
+  if (!session_main_is_enabled () || appns_sapi_enabled ())
     {
       rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
@@ -628,19 +622,9 @@ vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
   clib_memset (a, 0, sizeof (*a));
   a->api_client_index = mp->client_index;
   a->options = mp->options;
-  a->session_cb_vft = &session_mq_cb_vft_old;
-  if (mp->namespace_id_len > 64)
-    {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto done;
-    }
+  a->session_cb_vft = &session_mq_cb_vft;
 
-  if (mp->namespace_id_len)
-    {
-      vec_validate (a->namespace_id, mp->namespace_id_len - 1);
-      clib_memcpy_fast (a->namespace_id, mp->namespace_id,
-                       mp->namespace_id_len);
-    }
+  a->namespace_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
 
   if ((rv = vnet_application_attach (a)))
     {
@@ -672,23 +656,24 @@ vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
     }
 
 done:
-
   /* *INDENT-OFF* */
-  REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
+  REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
     if (!rv)
       {
+       ctrl_thread = vlib_num_workers () ? 1 : 0;
+       ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
        segp = a->segment;
        rmp->app_index = clib_host_to_net_u32 (a->app_index);
-       rmp->segment_name_length = 0;
-       rmp->segment_size = segp->ssvm_size;
+       rmp->app_mq = pointer_to_uword (a->app_evt_q);
+       rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
+       rmp->vpp_ctrl_mq_thread = ctrl_thread;
+       rmp->n_fds = n_fds;
+       rmp->fd_flags = fd_flags;
        if (vec_len (segp->name))
          {
-           memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
-           rmp->segment_name_length = vec_len (segp->name);
+           vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
          }
-       rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
-       rmp->n_fds = n_fds;
-       rmp->fd_flags = fd_flags;
+       rmp->segment_size = segp->ssvm_size;
        rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
       }
   }));
@@ -699,97 +684,76 @@ done:
 }
 
 static void
-vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
+vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
 {
   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
-  vl_api_app_attach_reply_t *rmp;
-  ssvm_private_t *segp, *evt_q_segment;
-  vnet_app_attach_args_t _a, *a = &_a;
-  u8 fd_flags = 0, ctrl_thread;
+  vl_api_app_worker_add_del_reply_t *rmp;
   vl_api_registration_t *reg;
-  svm_msg_q_t *ctrl_mq;
-
-  reg = vl_api_client_index_to_registration (mp->client_index);
-  if (!reg)
-    return;
+  application_t *app;
+  u8 fd_flags = 0;
 
-  if (session_main_is_enabled () == 0)
+  if (!session_main_is_enabled () || appns_sapi_enabled ())
     {
       rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
     }
 
-  STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
-                sizeof (mp->options),
-                "Out of options, fix api message definition");
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
 
-  clib_memset (a, 0, sizeof (*a));
-  a->api_client_index = mp->client_index;
-  a->options = mp->options;
-  a->session_cb_vft = &session_mq_cb_vft;
-  if (mp->namespace_id_len > 64)
+  app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
+  if (!app)
     {
       rv = VNET_API_ERROR_INVALID_VALUE;
       goto done;
     }
 
-  if (mp->namespace_id_len)
-    {
-      vec_validate (a->namespace_id, mp->namespace_id_len - 1);
-      clib_memcpy_fast (a->namespace_id, mp->namespace_id,
-                       mp->namespace_id_len);
-    }
-
-  if ((rv = vnet_application_attach (a)))
+  vnet_app_worker_add_del_args_t args = {
+    .app_index = app->app_index,
+    .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
+    .api_client_index = mp->client_index,
+    .is_add = mp->is_add
+  };
+  rv = vnet_app_worker_add_del (&args);
+  if (rv)
     {
-      clib_warning ("attach returned: %d", rv);
-      vec_free (a->namespace_id);
+      clib_warning ("app worker add/del returned: %d", rv);
       goto done;
     }
-  vec_free (a->namespace_id);
 
-  /* Send event queues segment */
-  if ((evt_q_segment = session_main_get_evt_q_segment ()))
-    {
-      fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
-      fds[n_fds] = evt_q_segment->fd;
-      n_fds += 1;
-    }
+  if (!mp->is_add)
+    goto done;
+
   /* Send fifo segment fd if needed */
-  if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
+  if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
     {
       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
-      fds[n_fds] = a->segment->fd;
+      fds[n_fds] = args.segment->fd;
       n_fds += 1;
     }
-  if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
+  if (application_segment_manager_properties (app)->use_mq_eventfd)
     {
       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
-      fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
+      fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
       n_fds += 1;
     }
 
-done:
   /* *INDENT-OFF* */
-  REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
-    if (!rv)
+done:
+  REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
+    rmp->is_add = mp->is_add;
+    rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
+    rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
+    if (!rv && mp->is_add)
       {
-       ctrl_thread = vlib_num_workers () ? 1 : 0;
-       ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
-       segp = a->segment;
-       rmp->app_index = clib_host_to_net_u32 (a->app_index);
-       rmp->app_mq = pointer_to_uword (a->app_evt_q);
-       rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
-       rmp->vpp_ctrl_mq_thread = ctrl_thread;
-       rmp->n_fds = n_fds;
-       rmp->fd_flags = fd_flags;
-       if (vec_len (segp->name))
+       if (vec_len (args.segment->name))
          {
-           memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
-           rmp->segment_name_length = vec_len (segp->name);
+           vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
          }
-       rmp->segment_size = segp->ssvm_size;
-       rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
+       rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
+       rmp->n_fds = n_fds;
+       rmp->fd_flags = fd_flags;
       }
   }));
   /* *INDENT-ON* */
@@ -798,7 +762,6 @@ done:
     session_send_fds (reg, fds, n_fds);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
 {
@@ -807,7 +770,7 @@ vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
   vnet_app_detach_args_t _a, *a = &_a;
   application_t *app;
 
-  if (session_main_is_enabled () == 0)
+  if (!session_main_is_enabled () || appns_sapi_enabled ())
     {
       rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
@@ -825,853 +788,926 @@ done:
   REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
+vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
 {
-  vl_api_bind_uri_reply_t *rmp;
-  vnet_listen_args_t _a, *a = &_a;
-  application_t *app = 0;
-  app_worker_t *app_wrk;
-  int rv;
-
+  vl_api_app_namespace_add_del_reply_t *rmp;
+  u32 appns_index = 0;
+  u8 *ns_id = 0;
+  int rv = 0;
   if (session_main_is_enabled () == 0)
     {
       rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
     }
 
-  app = application_lookup (mp->client_index);
-  if (app)
-    {
-      clib_memset (a, 0, sizeof (*a));
-      a->uri = (char *) mp->uri;
-      a->app_index = app->app_index;
-      rv = vnet_bind_uri (a);
-    }
-  else
-    {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
-    }
-
-done:
-
-  REPLY_MACRO (VL_API_BIND_URI_REPLY);
+  ns_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
 
-  if (app)
+  vnet_app_namespace_add_del_args_t args = {
+    .ns_id = ns_id,
+    .secret = clib_net_to_host_u64 (mp->secret),
+    .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
+    .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
+    .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
+    .is_add = 1
+  };
+  rv = vnet_app_namespace_add_del (&args);
+  if (!rv)
     {
-      app_wrk = application_get_worker (app, 0);
-      mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
-                               rv);
+      appns_index = app_namespace_index_from_id (ns_id);
+      if (appns_index == APP_NAMESPACE_INVALID_INDEX)
+       {
+         clib_warning ("app ns lookup failed");
+         rv = VNET_API_ERROR_UNSPECIFIED;
+       }
     }
+  vec_free (ns_id);
+
+  /* *INDENT-OFF* */
+done:
+  REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
+    if (!rv)
+      rmp->appns_index = clib_host_to_net_u32 (appns_index);
+  }));
+  /* *INDENT-ON* */
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
+vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
 {
-  vl_api_unbind_uri_reply_t *rmp;
-  application_t *app;
-  vnet_unlisten_args_t _a, *a = &_a;
-  int rv;
+  vl_api_session_rule_add_del_reply_t *rmp;
+  session_rule_add_del_args_t args;
+  session_rule_table_add_del_args_t *table_args = &args.table_args;
+  int rv = 0;
 
-  if (session_main_is_enabled () == 0)
-    {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
-      goto done;
-    }
+  clib_memset (&args, 0, sizeof (args));
 
-  app = application_lookup (mp->client_index);
-  if (app)
-    {
-      a->uri = (char *) mp->uri;
-      a->app_index = app->app_index;
-      a->wrk_map_index = 0;
-      rv = vnet_unbind_uri (a);
-    }
-  else
-    {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
-    }
+  ip_prefix_decode (&mp->lcl, &table_args->lcl);
+  ip_prefix_decode (&mp->rmt, &table_args->rmt);
 
-done:
-  REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
+  table_args->lcl_port = mp->lcl_port;
+  table_args->rmt_port = mp->rmt_port;
+  table_args->action_index = clib_net_to_host_u32 (mp->action_index);
+  table_args->is_add = mp->is_add;
+  mp->tag[sizeof (mp->tag) - 1] = 0;
+  table_args->tag = format (0, "%s", mp->tag);
+  args.appns_index = clib_net_to_host_u32 (mp->appns_index);
+  args.scope = mp->scope;
+  args.transport_proto =
+    api_session_transport_proto_decode (&mp->transport_proto) ==
+    TRANSPORT_PROTO_UDP ? 1 : 0;
+
+  rv = vnet_session_rule_add_del (&args);
+  if (rv)
+    clib_warning ("rule add del returned: %d", rv);
+  vec_free (table_args->tag);
+  REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
+send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
+                           u8 transport_proto, u32 appns_index, u8 * tag,
+                           vl_api_registration_t * reg, u32 context)
 {
-  vl_api_connect_uri_reply_t *rmp;
-  vnet_connect_args_t _a, *a = &_a;
-  application_t *app;
-  int rv = 0;
+  vl_api_session_rules_details_t *rmp = 0;
+  session_mask_or_match_4_t *match =
+    (session_mask_or_match_4_t *) & rule->match;
+  session_mask_or_match_4_t *mask =
+    (session_mask_or_match_4_t *) & rule->mask;
+  fib_prefix_t lcl, rmt;
 
-  if (session_main_is_enabled () == 0)
-    {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
-      goto done;
-    }
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  clib_memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
+  rmp->context = context;
 
-  app = application_lookup (mp->client_index);
-  if (app)
-    {
-      clib_memset (a, 0, sizeof (*a));
-      a->uri = (char *) mp->uri;
-      a->api_context = mp->context;
-      a->app_index = app->app_index;
-      if ((rv = vnet_connect_uri (a)))
-       clib_warning ("connect_uri returned: %d", rv);
-    }
-  else
+  clib_memset (&lcl, 0, sizeof (lcl));
+  clib_memset (&rmt, 0, sizeof (rmt));
+  ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
+  ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
+  lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
+  rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
+
+  ip_prefix_encode (&lcl, &rmp->lcl);
+  ip_prefix_encode (&rmt, &rmp->rmt);
+  rmp->lcl_port = match->lcl_port;
+  rmp->rmt_port = match->rmt_port;
+  rmp->action_index = clib_host_to_net_u32 (rule->action_index);
+  rmp->scope =
+    is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
+  rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
+  rmp->appns_index = clib_host_to_net_u32 (appns_index);
+  if (tag)
     {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
+      rmp->tag[vec_len (tag)] = 0;
     }
 
-  /*
-   * Don't reply to stream (tcp) connects. The reply will come once
-   * the connection is established. In case of the redirects, the reply
-   * will come from the server app.
-   */
-  if (rv == 0)
-    return;
-
-done:
-  REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
+  vl_api_send_msg (reg, (u8 *) rmp);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
+send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
+                           u8 transport_proto, u32 appns_index, u8 * tag,
+                           vl_api_registration_t * reg, u32 context)
 {
-  vl_api_disconnect_session_reply_t *rmp;
-  vnet_disconnect_args_t _a, *a = &_a;
-  application_t *app;
-  int rv = 0;
+  vl_api_session_rules_details_t *rmp = 0;
+  session_mask_or_match_6_t *match =
+    (session_mask_or_match_6_t *) & rule->match;
+  session_mask_or_match_6_t *mask =
+    (session_mask_or_match_6_t *) & rule->mask;
+  fib_prefix_t lcl, rmt;
 
-  if (session_main_is_enabled () == 0)
-    {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
-      goto done;
-    }
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  clib_memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
+  rmp->context = context;
 
-  app = application_lookup (mp->client_index);
-  if (app)
-    {
-      a->handle = mp->handle;
-      a->app_index = app->app_index;
-      rv = vnet_disconnect_session (a);
-    }
-  else
+  clib_memset (&lcl, 0, sizeof (lcl));
+  clib_memset (&rmt, 0, sizeof (rmt));
+  ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
+  ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
+  lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
+  rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
+
+  ip_prefix_encode (&lcl, &rmp->lcl);
+  ip_prefix_encode (&rmt, &rmp->rmt);
+  rmp->lcl_port = match->lcl_port;
+  rmp->rmt_port = match->rmt_port;
+  rmp->action_index = clib_host_to_net_u32 (rule->action_index);
+  rmp->scope =
+    is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
+  rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
+  rmp->appns_index = clib_host_to_net_u32 (appns_index);
+  if (tag)
     {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
+      rmp->tag[vec_len (tag)] = 0;
     }
 
-done:
-  REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
+  vl_api_send_msg (reg, (u8 *) rmp);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
-                                          mp)
+send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
+                                 u8 tp, u8 is_local, u32 appns_index,
+                                 vl_api_registration_t * reg, u32 context)
 {
-  vnet_disconnect_args_t _a, *a = &_a;
-  application_t *app;
+  mma_rule_16_t *rule16;
+  mma_rule_40_t *rule40;
+  mma_rules_table_16_t *srt16;
+  mma_rules_table_40_t *srt40;
+  u32 ri;
 
-  /* Client objected to disconnecting the session, log and continue */
-  if (mp->retval)
+  if (is_local || fib_proto == FIB_PROTOCOL_IP4)
     {
-      clib_warning ("client retval %d", mp->retval);
-      return;
+      u8 *tag = 0;
+      /* *INDENT-OFF* */
+      srt16 = &srt->session_rules_tables_16;
+      pool_foreach (rule16, srt16->rules, ({
+       ri = mma_rules_table_rule_index_16 (srt16, rule16);
+       tag = session_rules_table_rule_tag (srt, ri, 1);
+        send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
+                                    reg, context);
+      }));
+      /* *INDENT-ON* */
     }
-
-  /* Disconnect has been confirmed. Confirm close to transport */
-  app = application_lookup (mp->context);
-  if (app)
+  if (is_local || fib_proto == FIB_PROTOCOL_IP6)
     {
-      a->handle = mp->handle;
-      a->app_index = app->app_index;
-      vnet_disconnect_session (a);
+      u8 *tag = 0;
+      /* *INDENT-OFF* */
+      srt40 = &srt->session_rules_tables_40;
+      pool_foreach (rule40, srt40->rules, ({
+       ri = mma_rules_table_rule_index_40 (srt40, rule40);
+       tag = session_rules_table_rule_tag (srt, ri, 1);
+        send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
+                                    reg, context);
+      }));
+      /* *INDENT-ON* */
     }
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
-                                           * mp)
+vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
 {
-  clib_warning ("not implemented");
+  vl_api_registration_t *reg;
+  session_table_t *st;
+  u8 tp;
+
+  reg = vl_api_client_index_to_registration (mp->client_index);
+  if (!reg)
+    return;
+
+  /* *INDENT-OFF* */
+  session_table_foreach (st, ({
+    for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
+      {
+        send_session_rules_table_details (&st->session_rules[tp],
+                                          st->active_fib_proto, tp,
+                                          st->is_local, st->appns_index, reg,
+                                          mp->context);
+      }
+  }));
+  /* *INDENT-ON* */
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
+vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
 {
-  vnet_listen_args_t _a, *a = &_a;
-  vl_api_bind_sock_reply_t *rmp;
-  application_t *app = 0;
-  app_worker_t *app_wrk;
-  ip46_address_t *ip46;
+  vl_api_app_add_cert_key_pair_reply_t *rmp;
+  vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
+  u32 certkey_len, key_len, cert_len;
   int rv = 0;
-
   if (session_main_is_enabled () == 0)
     {
       rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
     }
 
-  app = application_lookup (mp->client_index);
-  if (!app)
+  cert_len = clib_net_to_host_u16 (mp->cert_len);
+  if (cert_len > 10000)
     {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto done;
+    }
+
+  certkey_len = clib_net_to_host_u16 (mp->certkey_len);
+  if (certkey_len < cert_len)
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto done;
+    }
+
+  key_len = certkey_len - cert_len;
+  if (key_len > 10000)
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
       goto done;
     }
 
-  ip46 = (ip46_address_t *) mp->ip;
   clib_memset (a, 0, sizeof (*a));
-  a->sep.is_ip4 = mp->is_ip4;
-  a->sep.ip = *ip46;
-  a->sep.port = mp->port;
-  a->sep.fib_index = mp->vrf;
-  a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
-  a->sep.transport_proto = mp->proto;
-  a->app_index = app->app_index;
-  a->wrk_map_index = mp->wrk_index;
-
-  if ((rv = vnet_listen (a)))
-    clib_warning ("listen returned: %d", rv);
+  vec_validate (a->cert, cert_len);
+  vec_validate (a->key, key_len);
+  clib_memcpy_fast (a->cert, mp->certkey, cert_len);
+  clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
+  rv = vnet_app_add_cert_key_pair (a);
+  vec_free (a->cert);
+  vec_free (a->key);
 
 done:
-  /* Actual reply sent only over mq */
-  REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
+  /* *INDENT-OFF* */
+  REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
+    if (!rv)
+      rmp->index = clib_host_to_net_u32 (a->index);
+  }));
+  /* *INDENT-ON* */
+}
 
-  if (app)
+static void
+vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
+{
+  vl_api_app_del_cert_key_pair_reply_t *rmp;
+  u32 ckpair_index;
+  int rv = 0;
+  if (session_main_is_enabled () == 0)
     {
-      app_wrk = application_get_worker (app, mp->wrk_index);
-      mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
-                               rv);
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
     }
+  ckpair_index = clib_net_to_host_u32 (mp->index);
+  rv = vnet_app_del_cert_key_pair (ckpair_index);
+
+done:
+  REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
 }
 
 /* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
+vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
+                                          mp)
 {
-  vl_api_unbind_sock_reply_t *rmp;
-  vnet_unlisten_args_t _a, *a = &_a;
-  app_worker_t *app_wrk;
-  application_t *app = 0;
+  vl_api_application_tls_cert_add_reply_t *rmp;
+  app_cert_key_pair_t *ckpair;
+  application_t *app;
+  u32 cert_len;
   int rv = 0;
-
   if (session_main_is_enabled () == 0)
     {
       rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
     }
-
-  app = application_lookup (mp->client_index);
-  if (app)
+  if (!(app = application_lookup (mp->client_index)))
     {
-      a->app_index = app->app_index;
-      a->handle = mp->handle;
-      a->wrk_map_index = mp->wrk_index;
-      if ((rv = vnet_unlisten (a)))
-       clib_warning ("unlisten returned: %d", rv);
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      goto done;
     }
-  else
+  cert_len = clib_net_to_host_u16 (mp->cert_len);
+  if (cert_len > 10000)
     {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto done;
     }
+  ckpair = app_cert_key_pair_get_default ();
+  vec_validate (ckpair->cert, cert_len);
+  clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
 
 done:
-  REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
-
-  if (!app)
-    return;
-
-  app_wrk = application_get_worker (app, a->wrk_map_index);
-  if (!app_wrk)
-    return;
-
-  mq_send_unlisten_reply (app_wrk, mp->handle, mp->context, rv);
+  REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
 }
 
 /* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
+vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
+                                         mp)
 {
-  vl_api_connect_sock_reply_t *rmp;
-  vnet_connect_args_t _a, *a = &_a;
-  application_t *app = 0;
+  vl_api_application_tls_key_add_reply_t *rmp;
+  app_cert_key_pair_t *ckpair;
+  application_t *app;
+  u32 key_len;
   int rv = 0;
-
   if (session_main_is_enabled () == 0)
     {
       rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
     }
-
-  app = application_lookup (mp->client_index);
-  if (app)
-    {
-      svm_queue_t *client_q;
-      ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
-
-      clib_memset (a, 0, sizeof (*a));
-      client_q = vl_api_client_index_to_input_queue (mp->client_index);
-      mp->client_queue_address = pointer_to_uword (client_q);
-      a->sep.is_ip4 = mp->is_ip4;
-      a->sep.ip = *ip46;
-      a->sep.port = mp->port;
-      a->sep.transport_proto = mp->proto;
-      a->sep.peer.fib_index = mp->vrf;
-      a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
-      a->sep_ext.parent_handle = mp->parent_handle;
-      if (mp->hostname_len)
-       {
-         vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
-         clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
-                           mp->hostname_len);
-       }
-      a->api_context = mp->context;
-      a->app_index = app->app_index;
-      a->wrk_map_index = mp->wrk_index;
-      if ((rv = vnet_connect (a)))
-       clib_warning ("connect returned: %U", format_vnet_api_errno, rv);
-      vec_free (a->sep_ext.hostname);
-    }
-  else
+  if (!(app = application_lookup (mp->client_index)))
     {
       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
-    }
-
-  if (rv == 0)
-    return;
-
-  /* Got some error, relay it */
-
-done:
-  REPLY_MACRO (VL_API_CONNECT_SOCK_REPLY);
-
-  if (app)
-    {
-      app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
-      mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
-    }
-}
-
-static void
-vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
-{
-  int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
-  vl_api_app_worker_add_del_reply_t *rmp;
-  vl_api_registration_t *reg;
-  application_t *app;
-  u8 fd_flags = 0;
-
-  if (session_main_is_enabled () == 0)
-    {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
       goto done;
     }
-
-  reg = vl_api_client_index_to_registration (mp->client_index);
-  if (!reg)
-    return;
-
-  app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
-  if (!app)
+  key_len = clib_net_to_host_u16 (mp->key_len);
+  if (key_len > 10000)
     {
       rv = VNET_API_ERROR_INVALID_VALUE;
       goto done;
     }
-
-  vnet_app_worker_add_del_args_t args = {
-    .app_index = app->app_index,
-    .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
-    .api_client_index = mp->client_index,
-    .is_add = mp->is_add
-  };
-  rv = vnet_app_worker_add_del (&args);
-  if (rv)
-    {
-      clib_warning ("app worker add/del returned: %d", rv);
-      goto done;
-    }
-
-  if (!mp->is_add)
-    goto done;
-
-  /* Send fifo segment fd if needed */
-  if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
-    {
-      fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
-      fds[n_fds] = args.segment->fd;
-      n_fds += 1;
-    }
-  if (application_segment_manager_properties (app)->use_mq_eventfd)
-    {
-      fd_flags |= SESSION_FD_F_MQ_EVENTFD;
-      fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
-      n_fds += 1;
-    }
-
-  /* *INDENT-OFF* */
+  ckpair = app_cert_key_pair_get_default ();
+  vec_validate (ckpair->key, key_len);
+  clib_memcpy_fast (ckpair->key, mp->key, key_len);
 done:
-  REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
-    rmp->is_add = mp->is_add;
-    rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
-    rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
-    if (!rv && mp->is_add)
-      {
-       if (vec_len (args.segment->name))
-         {
-           memcpy (rmp->segment_name, args.segment->name,
-                   vec_len (args.segment->name));
-           rmp->segment_name_length = vec_len (args.segment->name);
-         }
-       rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
-       rmp->n_fds = n_fds;
-       rmp->fd_flags = fd_flags;
-      }
-  }));
-  /* *INDENT-ON* */
-
-  if (n_fds)
-    session_send_fds (reg, fds, n_fds);
+  REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
 }
 
-static void
-vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
+static clib_error_t *
+application_reaper_cb (u32 client_index)
 {
-  vl_api_app_namespace_add_del_reply_t *rmp;
-  u32 appns_index = 0;
-  u8 *ns_id = 0;
-  int rv = 0;
-  if (session_main_is_enabled () == 0)
+  application_t *app = application_lookup (client_index);
+  vnet_app_detach_args_t _a, *a = &_a;
+  if (app)
     {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
-      goto done;
+      a->app_index = app->app_index;
+      a->api_client_index = client_index;
+      vnet_application_detach (a);
     }
+  return 0;
+}
 
-  if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
-    {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto done;
-    }
+VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
 
-  vec_validate (ns_id, mp->namespace_id_len - 1);
-  clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
-  vnet_app_namespace_add_del_args_t args = {
-    .ns_id = ns_id,
-    .secret = clib_net_to_host_u64 (mp->secret),
-    .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
-    .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
-    .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
-    .is_add = 1
-  };
-  rv = vnet_app_namespace_add_del (&args);
-  if (!rv)
-    {
-      appns_index = app_namespace_index_from_id (ns_id);
-      if (appns_index == APP_NAMESPACE_INVALID_INDEX)
-       {
-         clib_warning ("app ns lookup failed");
-         rv = VNET_API_ERROR_UNSPECIFIED;
-       }
-    }
-  vec_free (ns_id);
+#define vl_msg_name_crc_list
+#include <vnet/vnet_all_api_h.h>
+#undef vl_msg_name_crc_list
 
-  /* *INDENT-OFF* */
-done:
-  REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
-    if (!rv)
-      rmp->appns_index = clib_host_to_net_u32 (appns_index);
-  }));
-  /* *INDENT-ON* */
+static void
+setup_message_id_table (api_main_t * am)
+{
+#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
+  foreach_vl_msg_name_crc_session;
+#undef _
 }
 
-static void
-vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
+/*
+ * session_api_hookup
+ * Add uri's API message handlers to the table.
+ * vlib has already mapped shared memory and
+ * added the client registration handlers.
+ * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
+ */
+static clib_error_t *
+session_api_hookup (vlib_main_t * vm)
 {
-  vl_api_session_rule_add_del_reply_t *rmp;
-  session_rule_add_del_args_t args;
-  session_rule_table_add_del_args_t *table_args = &args.table_args;
-  u8 fib_proto;
-  int rv = 0;
+  api_main_t *am = vlibapi_get_main ();
 
-  clib_memset (&args, 0, sizeof (args));
-  fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
+#define _(N,n)                                                  \
+    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
+                           vl_api_##n##_t_handler,              \
+                           vl_noop_handler,                     \
+                           vl_api_##n##_t_endian,               \
+                           vl_api_##n##_t_print,                \
+                           sizeof(vl_api_##n##_t), 1);
+  foreach_session_api_msg;
+#undef _
 
-  table_args->lcl.fp_len = mp->lcl_plen;
-  table_args->lcl.fp_proto = fib_proto;
-  table_args->rmt.fp_len = mp->rmt_plen;
-  table_args->rmt.fp_proto = fib_proto;
-  table_args->lcl_port = mp->lcl_port;
-  table_args->rmt_port = mp->rmt_port;
-  table_args->action_index = clib_net_to_host_u32 (mp->action_index);
-  table_args->is_add = mp->is_add;
-  mp->tag[sizeof (mp->tag) - 1] = 0;
-  table_args->tag = format (0, "%s", mp->tag);
-  args.appns_index = clib_net_to_host_u32 (mp->appns_index);
-  args.scope = mp->scope;
-  args.transport_proto = mp->transport_proto;
+  /*
+   * Set up the (msg_name, crc, message-id) table
+   */
+  setup_message_id_table (am);
 
-  clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
-  clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
-  ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
-  ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
-  rv = vnet_session_rule_add_del (&args);
-  if (rv)
-    clib_warning ("rule add del returned: %d", rv);
-  vec_free (table_args->tag);
-  REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
+  return 0;
 }
 
+VLIB_API_INIT_FUNCTION (session_api_hookup);
+
+/*
+ * Socket api functions
+ */
+
 static void
-send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
-                           u8 transport_proto, u32 appns_index, u8 * tag,
-                           vl_api_registration_t * reg, u32 context)
+sapi_send_fds (app_worker_t * app_wrk, int *fds, int n_fds)
 {
-  vl_api_session_rules_details_t *rmp = 0;
-  session_mask_or_match_4_t *match =
-    (session_mask_or_match_4_t *) & rule->match;
-  session_mask_or_match_4_t *mask =
-    (session_mask_or_match_4_t *) & rule->mask;
-
-  rmp = vl_msg_api_alloc (sizeof (*rmp));
-  clib_memset (rmp, 0, sizeof (*rmp));
-  rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
-  rmp->context = context;
+  app_sapi_msg_t smsg = { 0 };
+  app_namespace_t *app_ns;
+  application_t *app;
+  clib_socket_t *cs;
+  u32 cs_index;
 
-  rmp->is_ip4 = 1;
-  clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
-  clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
-  rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
-  rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
-  rmp->lcl_port = match->lcl_port;
-  rmp->rmt_port = match->rmt_port;
-  rmp->action_index = clib_host_to_net_u32 (rule->action_index);
-  rmp->scope =
-    is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
-  rmp->transport_proto = transport_proto;
-  rmp->appns_index = clib_host_to_net_u32 (appns_index);
-  if (tag)
-    {
-      clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
-      rmp->tag[vec_len (tag)] = 0;
-    }
+  app = application_get (app_wrk->app_index);
+  app_ns = app_namespace_get (app->ns_index);
+  cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
+  cs = appns_sapi_get_socket (app_ns, cs_index);
 
-  vl_api_send_msg (reg, (u8 *) rmp);
+  /* There's no payload for the message only the type */
+  smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
+  clib_socket_sendmsg (cs, &smsg, sizeof (smsg), fds, n_fds);
 }
 
-static void
-send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
-                           u8 transport_proto, u32 appns_index, u8 * tag,
-                           vl_api_registration_t * reg, u32 context)
+static int
+mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
 {
-  vl_api_session_rules_details_t *rmp = 0;
-  session_mask_or_match_6_t *match =
-    (session_mask_or_match_6_t *) & rule->match;
-  session_mask_or_match_6_t *mask =
-    (session_mask_or_match_6_t *) & rule->mask;
+  int fds[SESSION_N_FD_TYPE], n_fds = 0;
+  svm_msg_q_msg_t _msg, *msg = &_msg;
+  session_app_add_segment_msg_t *mp;
+  app_worker_t *app_wrk;
+  session_event_t *evt;
+  svm_msg_q_t *app_mq;
+  fifo_segment_t *fs;
+  ssvm_private_t *sp;
+  u8 fd_flags = 0;
 
-  rmp = vl_msg_api_alloc (sizeof (*rmp));
-  clib_memset (rmp, 0, sizeof (*rmp));
-  rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
-  rmp->context = context;
+  app_wrk = app_worker_get (app_wrk_index);
 
-  rmp->is_ip4 = 0;
-  clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
-  clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
-  rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
-  rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
-  rmp->lcl_port = match->lcl_port;
-  rmp->rmt_port = match->rmt_port;
-  rmp->action_index = clib_host_to_net_u32 (rule->action_index);
-  rmp->scope =
-    is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
-  rmp->transport_proto = transport_proto;
-  rmp->appns_index = clib_host_to_net_u32 (appns_index);
-  if (tag)
-    {
-      clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
-      rmp->tag[vec_len (tag)] = 0;
-    }
+  fs = segment_manager_get_segment_w_handle (segment_handle);
+  sp = &fs->ssvm;
+  ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD);
 
-  vl_api_send_msg (reg, (u8 *) rmp);
-}
+  fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
+  fds[n_fds] = sp->fd;
+  n_fds += 1;
 
-static void
-send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
-                                 u8 tp, u8 is_local, u32 appns_index,
-                                 vl_api_registration_t * reg, u32 context)
-{
-  mma_rule_16_t *rule16;
-  mma_rule_40_t *rule40;
-  mma_rules_table_16_t *srt16;
-  mma_rules_table_40_t *srt40;
-  u32 ri;
+  app_mq = app_wrk->event_queue;
+  if (mq_try_lock_and_alloc_msg (app_mq, msg))
+    return -1;
 
-  if (is_local || fib_proto == FIB_PROTOCOL_IP4)
-    {
-      u8 *tag = 0;
-      /* *INDENT-OFF* */
-      srt16 = &srt->session_rules_tables_16;
-      pool_foreach (rule16, srt16->rules, ({
-       ri = mma_rules_table_rule_index_16 (srt16, rule16);
-       tag = session_rules_table_rule_tag (srt, ri, 1);
-        send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
-                                    reg, context);
-      }));
-      /* *INDENT-ON* */
-    }
-  if (is_local || fib_proto == FIB_PROTOCOL_IP6)
-    {
-      u8 *tag = 0;
-      /* *INDENT-OFF* */
-      srt40 = &srt->session_rules_tables_40;
-      pool_foreach (rule40, srt40->rules, ({
-       ri = mma_rules_table_rule_index_40 (srt40, rule40);
-       tag = session_rules_table_rule_tag (srt, ri, 1);
-        send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
-                                    reg, context);
-      }));
-      /* *INDENT-ON* */
-    }
+  /*
+   * Send the fd over api socket
+   */
+  sapi_send_fds (app_wrk, fds, n_fds);
+
+  /*
+   * Send the actual message over mq
+   */
+  evt = svm_msg_q_msg_data (app_mq, msg);
+  clib_memset (evt, 0, sizeof (*evt));
+  evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
+  mp = (session_app_add_segment_msg_t *) evt->data;
+  clib_memset (mp, 0, sizeof (*mp));
+  mp->segment_size = sp->ssvm_size;
+  mp->fd_flags = fd_flags;
+  mp->segment_handle = segment_handle;
+  strncpy ((char *) mp->segment_name, (char *) sp->name,
+          sizeof (mp->segment_name) - 1);
+
+  svm_msg_q_add_and_unlock (app_mq, msg);
+
+  return 0;
 }
 
-static void
-vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
+static int
+mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
 {
-  vl_api_registration_t *reg;
-  session_table_t *st;
-  u8 tp;
+  svm_msg_q_msg_t _msg, *msg = &_msg;
+  session_app_del_segment_msg_t *mp;
+  app_worker_t *app_wrk;
+  session_event_t *evt;
+  svm_msg_q_t *app_mq;
 
-  reg = vl_api_client_index_to_registration (mp->client_index);
-  if (!reg)
-    return;
+  app_wrk = app_worker_get (app_wrk_index);
 
-  /* *INDENT-OFF* */
-  session_table_foreach (st, ({
-    for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
-      {
-        send_session_rules_table_details (&st->session_rules[tp],
-                                          st->active_fib_proto, tp,
-                                          st->is_local, st->appns_index, reg,
-                                          mp->context);
-      }
-  }));
-  /* *INDENT-ON* */
+  app_mq = app_wrk->event_queue;
+  if (mq_try_lock_and_alloc_msg (app_mq, msg))
+    return -1;
+
+  evt = svm_msg_q_msg_data (app_mq, msg);
+  clib_memset (evt, 0, sizeof (*evt));
+  evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
+  mp = (session_app_del_segment_msg_t *) evt->data;
+  clib_memset (mp, 0, sizeof (*mp));
+  mp->segment_handle = segment_handle;
+  svm_msg_q_add_and_unlock (app_mq, msg);
+
+  return 0;
 }
 
+static session_cb_vft_t session_mq_sapi_cb_vft = {
+  .session_accept_callback = mq_send_session_accepted_cb,
+  .session_disconnect_callback = mq_send_session_disconnected_cb,
+  .session_connected_callback = mq_send_session_connected_cb,
+  .session_reset_callback = mq_send_session_reset_cb,
+  .session_migrate_callback = mq_send_session_migrate_cb,
+  .session_cleanup_callback = mq_send_session_cleanup_cb,
+  .add_segment_callback = mq_send_add_segment_sapi_cb,
+  .del_segment_callback = mq_send_del_segment_sapi_cb,
+};
+
 static void
-vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
+session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs,
+                           app_sapi_attach_msg_t * mp)
 {
-  vl_api_app_add_cert_key_pair_reply_t *rmp;
-  vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
-  u32 certkey_len, key_len, cert_len;
-  int rv = 0;
-  if (session_main_is_enabled () == 0)
+  int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
+  vnet_app_attach_args_t _a, *a = &_a;
+  app_sapi_attach_reply_msg_t *rmp;
+  ssvm_private_t *evt_q_segment;
+  u8 fd_flags = 0, ctrl_thread;
+  app_ns_api_handle_t *handle;
+  app_sapi_msg_t msg = { 0 };
+  app_worker_t *app_wrk;
+  svm_msg_q_t *ctrl_mq;
+  application_t *app;
+
+  /* Make sure name is null terminated */
+  mp->name[63] = 0;
+
+  clib_memset (a, 0, sizeof (*a));
+  a->api_client_index = appns_sapi_socket_handle (app_ns, cs);
+  a->name = format (0, "%s", (char *) mp->name);
+  a->options = mp->options;
+  a->session_cb_vft = &session_mq_sapi_cb_vft;
+  a->use_sock_api = 1;
+  a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns);
+
+  if ((rv = vnet_application_attach (a)))
     {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      clib_warning ("attach returned: %d", rv);
       goto done;
     }
 
-  cert_len = clib_net_to_host_u16 (mp->cert_len);
-  if (cert_len > 10000)
+  /* Send event queues segment */
+  if ((evt_q_segment = session_main_get_evt_q_segment ()))
     {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto done;
+      fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
+      fds[n_fds] = evt_q_segment->fd;
+      n_fds += 1;
     }
-
-  certkey_len = clib_net_to_host_u16 (mp->certkey_len);
-  if (certkey_len < cert_len)
+  /* Send fifo segment fd if needed */
+  if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
     {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto done;
+      fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
+      fds[n_fds] = a->segment->fd;
+      n_fds += 1;
     }
-
-  key_len = certkey_len - cert_len;
-  if (key_len > 10000)
+  if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
     {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto done;
+      fd_flags |= SESSION_FD_F_MQ_EVENTFD;
+      fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
+      n_fds += 1;
     }
 
-  clib_memset (a, 0, sizeof (*a));
-  vec_validate (a->cert, cert_len);
-  vec_validate (a->key, key_len);
-  clib_memcpy_fast (a->cert, mp->certkey, cert_len);
-  clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
-  rv = vnet_app_add_cert_key_pair (a);
-  vec_free (a->cert);
-  vec_free (a->key);
-
 done:
-  /* *INDENT-OFF* */
-  REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
-    if (!rv)
-      rmp->index = clib_host_to_net_u32 (a->index);
-  }));
-  /* *INDENT-ON* */
+
+  msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY;
+  rmp = &msg.attach_reply;
+  rmp->retval = rv;
+  if (!rv)
+    {
+      ctrl_thread = vlib_num_workers ()? 1 : 0;
+      ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
+      rmp->app_index = a->app_index;
+      rmp->app_mq = pointer_to_uword (a->app_evt_q);
+      rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
+      rmp->vpp_ctrl_mq_thread = ctrl_thread;
+      rmp->n_fds = n_fds;
+      rmp->fd_flags = fd_flags;
+      /* No segment name and size since we only support memfds
+       * in this configuration */
+      rmp->segment_handle = a->segment_handle;
+      rmp->api_client_handle = a->api_client_index;
+
+      /* Update app index for socket */
+      handle = (app_ns_api_handle_t *) & cs->private_data;
+      app = application_get (a->app_index);
+      app_wrk = application_get_worker (app, 0);
+      handle->aah_app_wrk_index = app_wrk->wrk_index;
+    }
+
+  clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
+  vec_free (a->name);
 }
 
 static void
-vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
+sapi_socket_close_w_handle (u32 api_handle)
 {
-  vl_api_app_del_cert_key_pair_reply_t *rmp;
-  u32 ckpair_index;
-  int rv = 0;
-  if (session_main_is_enabled () == 0)
-    {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
-      goto done;
-    }
-  ckpair_index = clib_net_to_host_u32 (mp->index);
-  rv = vnet_app_del_cert_key_pair (ckpair_index);
+  app_namespace_t *app_ns = app_namespace_get (api_handle >> 16);
+  u16 sock_index = api_handle & 0xffff;
+  app_ns_api_handle_t *handle;
+  clib_socket_t *cs;
+  clib_file_t *cf;
+
+  cs = appns_sapi_get_socket (app_ns, sock_index);
+  if (!cs)
+    return;
 
-done:
-  REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
+  handle = (app_ns_api_handle_t *) & cs->private_data;
+  cf = clib_file_get (&file_main, handle->aah_file_index);
+  clib_file_del (&file_main, cf);
+
+  clib_socket_close (cs);
+  appns_sapi_free_socket (app_ns, cs);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
-                                          mp)
+sapi_add_del_worker_handler (app_namespace_t * app_ns,
+                            clib_socket_t * cs,
+                            app_sapi_worker_add_del_msg_t * mp)
 {
-  vl_api_application_tls_cert_add_reply_t *rmp;
-  app_cert_key_pair_t *ckpair;
+  int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
+  app_sapi_worker_add_del_reply_msg_t *rmp;
+  app_ns_api_handle_t *handle;
+  app_sapi_msg_t msg = { 0 };
+  app_worker_t *app_wrk;
   application_t *app;
-  u32 cert_len;
-  int rv = 0;
-  if (session_main_is_enabled () == 0)
+  u32 sapi_handle;
+  u8 fd_flags = 0;
+
+  app = application_get_if_valid (mp->app_index);
+  if (!app)
     {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      rv = VNET_API_ERROR_INVALID_VALUE;
       goto done;
     }
-  if (!(app = application_lookup (mp->client_index)))
+
+  sapi_handle = appns_sapi_socket_handle (app_ns, cs);
+
+  vnet_app_worker_add_del_args_t args = {
+    .app_index = app->app_index,
+    .wrk_map_index = mp->wrk_index,
+    .api_client_index = sapi_handle,
+    .is_add = mp->is_add
+  };
+  rv = vnet_app_worker_add_del (&args);
+  if (rv)
     {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+      clib_warning ("app worker add/del returned: %d", rv);
       goto done;
     }
-  cert_len = clib_net_to_host_u16 (mp->cert_len);
-  if (cert_len > 10000)
+
+  if (!mp->is_add)
     {
-      rv = VNET_API_ERROR_INVALID_VALUE;
+      sapi_socket_close_w_handle (sapi_handle);
       goto done;
     }
-  ckpair = app_cert_key_pair_get_default ();
-  vec_validate (ckpair->cert, cert_len);
-  clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
+
+  /* Send fifo segment fd if needed */
+  if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
+    {
+      fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
+      fds[n_fds] = args.segment->fd;
+      n_fds += 1;
+    }
+  if (application_segment_manager_properties (app)->use_mq_eventfd)
+    {
+      fd_flags |= SESSION_FD_F_MQ_EVENTFD;
+      fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
+      n_fds += 1;
+    }
 
 done:
-  REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
+
+  msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY;
+  rmp = &msg.worker_add_del_reply;
+  rmp->retval = rv;
+  rmp->is_add = mp->is_add;
+  rmp->wrk_index = args.wrk_map_index;
+  rmp->segment_handle = args.segment_handle;
+  if (!rv && mp->is_add)
+    {
+      /* No segment name and size. This supports only memfds */
+      rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
+      rmp->n_fds = n_fds;
+      rmp->fd_flags = fd_flags;
+
+      /* Update app index for socket */
+      handle = (app_ns_api_handle_t *) & cs->private_data;
+      app_wrk = application_get_worker (app, args.wrk_map_index);
+      handle->aah_app_wrk_index = app_wrk->wrk_index;
+    }
+
+  clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
 }
 
-/* ### WILL BE DEPRECATED POST 20.01 ### */
 static void
-vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
-                                         mp)
+sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs)
 {
-  vl_api_application_tls_key_add_reply_t *rmp;
-  app_cert_key_pair_t *ckpair;
-  application_t *app;
-  u32 key_len;
-  int rv = 0;
-  if (session_main_is_enabled () == 0)
-    {
-      rv = VNET_API_ERROR_FEATURE_DISABLED;
-      goto done;
-    }
-  if (!(app = application_lookup (mp->client_index)))
+  vnet_app_detach_args_t _a = { 0 }, *a = &_a;
+  app_ns_api_handle_t *handle;
+  app_worker_t *app_wrk;
+  u32 api_client_handle;
+
+  api_client_handle = appns_sapi_socket_handle (app_ns, cs);
+  sapi_socket_close_w_handle (api_client_handle);
+
+  /* Cleanup everything because app closed socket or crashed */
+  handle = (app_ns_api_handle_t *) & cs->private_data;
+  app_wrk = app_worker_get (handle->aah_app_wrk_index);
+  a->app_index = app_wrk->app_index;
+  a->api_client_index = api_client_handle;
+  vnet_application_detach (a);
+}
+
+static clib_error_t *
+sapi_sock_read_ready (clib_file_t * cf)
+{
+  app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
+  app_sapi_msg_t msg = { 0 };
+  app_namespace_t *app_ns;
+  clib_error_t *err = 0;
+  clib_socket_t *cs;
+
+  app_ns = app_namespace_get (handle->aah_app_ns_index);
+  cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
+  if (!cs)
+    goto error;
+
+  err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0);
+  if (err)
     {
-      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
-      goto done;
+      clib_error_free (err);
+      sapi_socket_detach (app_ns, cs);
+      goto error;
     }
-  key_len = clib_net_to_host_u16 (mp->key_len);
-  if (key_len > 10000)
+
+  handle = (app_ns_api_handle_t *) & cs->private_data;
+
+  switch (msg.type)
     {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto done;
+    case APP_SAPI_MSG_TYPE_ATTACH:
+      session_api_attach_handler (app_ns, cs, &msg.attach);
+      break;
+    case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER:
+      sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del);
+      break;
+    default:
+      clib_warning ("app wrk %u unknown message type: %u",
+                   handle->aah_app_wrk_index, msg.type);
+      break;
     }
-  ckpair = app_cert_key_pair_get_default ();
-  vec_validate (ckpair->key, key_len);
-  clib_memcpy_fast (ckpair->key, mp->key, key_len);
-done:
-  REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
+
+error:
+  return 0;
 }
 
 static clib_error_t *
-application_reaper_cb (u32 client_index)
+sapi_sock_write_ready (clib_file_t * cf)
 {
-  application_t *app = application_lookup (client_index);
-  vnet_app_detach_args_t _a, *a = &_a;
-  if (app)
-    {
-      a->app_index = app->app_index;
-      a->api_client_index = client_index;
-      vnet_application_detach (a);
-    }
+  app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
+  clib_warning ("called for app ns %u", handle->aah_app_ns_index);
   return 0;
 }
 
-VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
+static clib_error_t *
+sapi_sock_error (clib_file_t * cf)
+{
+  app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
+  app_namespace_t *app_ns;
+  clib_socket_t *cs;
 
-#define vl_msg_name_crc_list
-#include <vnet/vnet_all_api_h.h>
-#undef vl_msg_name_crc_list
+  app_ns = app_namespace_get (handle->aah_app_ns_index);
+  cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
+  if (!cs)
+    return 0;
 
-static void
-setup_message_id_table (api_main_t * am)
-{
-#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
-  foreach_vl_msg_name_crc_session;
-#undef _
+  sapi_socket_detach (app_ns, cs);
+  return 0;
 }
 
-/*
- * session_api_hookup
- * Add uri's API message handlers to the table.
- * vlib has already mapped shared memory and
- * added the client registration handlers.
- * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
- */
 static clib_error_t *
-session_api_hookup (vlib_main_t * vm)
+sapi_sock_accept_ready (clib_file_t * scf)
 {
-  api_main_t *am = vlibapi_get_main ();
+  app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data;
+  app_namespace_t *app_ns;
+  clib_file_t cf = { 0 };
+  clib_error_t *err = 0;
+  clib_socket_t *ccs, *scs;
 
-#define _(N,n)                                                  \
-    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
-                           vl_api_##n##_t_handler,              \
-                           vl_noop_handler,                     \
-                           vl_api_##n##_t_endian,               \
-                           vl_api_##n##_t_print,                \
-                           sizeof(vl_api_##n##_t), 1);
-  foreach_session_api_msg;
-#undef _
+  /* Listener files point to namespace */
+  app_ns = app_namespace_get (handle.aah_app_ns_index);
 
   /*
-   * Messages which bounce off the data-plane to
-   * an API client. Simply tells the message handling infra not
-   * to free the message.
-   *
-   * Bounced message handlers MUST NOT block the data plane
+   * Initialize client socket
    */
-  am->message_bounce[VL_API_CONNECT_URI] = 1;
-  am->message_bounce[VL_API_CONNECT_SOCK] = 1;
+  ccs = appns_sapi_alloc_socket (app_ns);
+
+  /* Grab server socket after client is initialized  */
+  scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index);
+  if (!scs)
+    goto error;
+
+  err = clib_socket_accept (scs, ccs);
+  if (err)
+    {
+      clib_error_report (err);
+      goto error;
+    }
+
+  cf.read_function = sapi_sock_read_ready;
+  cf.write_function = sapi_sock_write_ready;
+  cf.error_function = sapi_sock_error;
+  cf.file_descriptor = ccs->fd;
+  /* File points to app namespace and socket */
+  handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs);
+  cf.private_data = handle.as_uword;
+  cf.description = format (0, "app sock conn fd: %d", ccs->fd);
+
+  /* Poll until we get an attach message. Socket points to file and
+   * application that owns the socket */
+  handle.aah_app_wrk_index = APP_INVALID_INDEX;
+  handle.aah_file_index = clib_file_add (&file_main, &cf);
+  ccs->private_data = handle.as_uword;
+
+  return err;
+
+error:
+  appns_sapi_free_socket (app_ns, ccs);
+  return err;
+}
+
+int
+appns_sapi_add_ns_socket (app_namespace_t * app_ns)
+{
+  char *subdir = "/app_ns_sockets/";
+  app_ns_api_handle_t *handle;
+  clib_file_t cf = { 0 };
+  struct stat file_stat;
+  clib_error_t *err;
+  clib_socket_t *cs;
+  u8 *dir = 0;
+  int rv = 0;
+
+  vec_add (dir, vlib_unix_get_runtime_dir (),
+          strlen (vlib_unix_get_runtime_dir ()));
+  vec_add (dir, (u8 *) subdir, strlen (subdir));
+
+  err = vlib_unix_recursive_mkdir ((char *) dir);
+  if (err)
+    {
+      clib_error_report (err);
+      rv = -1;
+      goto error;
+    }
+
+  app_ns->sock_name = format (0, "%v%v%c", dir, app_ns->ns_id, 0);
 
   /*
-   * Set up the (msg_name, crc, message-id) table
+   * Create and initialize socket to listen on
    */
-  setup_message_id_table (am);
+  cs = appns_sapi_alloc_socket (app_ns);
+  cs->config = (char *) app_ns->sock_name;
+  cs->flags = CLIB_SOCKET_F_IS_SERVER |
+    CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
+    CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
 
-  return 0;
-}
+  if ((err = clib_socket_init (cs)))
+    {
+      clib_error_report (err);
+      rv = -1;
+      goto error;
+    }
 
-VLIB_API_INIT_FUNCTION (session_api_hookup);
+  if (stat ((char *) app_ns->sock_name, &file_stat) == -1)
+    {
+      rv = -1;
+      goto error;
+    }
+
+  /*
+   * Start polling it
+   */
+  cf.read_function = sapi_sock_accept_ready;
+  cf.file_descriptor = cs->fd;
+  /* File points to namespace */
+  handle = (app_ns_api_handle_t *) & cf.private_data;
+  handle->aah_app_ns_index = app_namespace_index (app_ns);
+  handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs);
+  cf.description = format (0, "app sock listener: %s", app_ns->sock_name);
+
+  /* Socket points to clib file index */
+  handle = (app_ns_api_handle_t *) & cs->private_data;
+  handle->aah_file_index = clib_file_add (&file_main, &cf);
+  handle->aah_app_wrk_index = APP_INVALID_INDEX;
+
+error:
+  vec_free (dir);
+  return rv;
+}
 
 /*
  * fd.io coding-style-patch-verification: ON