session: add rule tags
[vpp.git] / src / vnet / session / session_api.c
old mode 100644 (file)
new mode 100755 (executable)
index 9d06868..fc0a257
 #include <vnet/vnet.h>
 #include <vlibmemory/api.h>
 #include <vnet/session/application.h>
+#include <vnet/session/application_interface.h>
+#include <vnet/session/session_rules_table.h>
+#include <vnet/session/session_table.h>
 
 #include <vnet/vnet_msg_enum.h>
-#include "application_interface.h"
 
 #define vl_typedefs            /* define message structures */
 #include <vnet/vnet_all_api_h.h>
@@ -38,6 +40,8 @@
 
 #define foreach_session_api_msg                                         \
 _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply)                 \
+_(APPLICATION_ATTACH, application_attach)                              \
+_(APPLICATION_DETACH, application_detach)                              \
 _(BIND_URI, bind_uri)                                                   \
 _(UNBIND_URI, unbind_uri)                                               \
 _(CONNECT_URI, connect_uri)                                             \
@@ -45,13 +49,13 @@ _(DISCONNECT_SESSION, disconnect_session)                               \
 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
 _(ACCEPT_SESSION_REPLY, accept_session_reply)                           \
 _(RESET_SESSION_REPLY, reset_session_reply)                            \
-_(BIND_SOCK, bind_sock)                                                \
+_(BIND_SOCK, bind_sock)                                                        \
 _(UNBIND_SOCK, unbind_sock)                                             \
 _(CONNECT_SOCK, connect_sock)                                                  \
-_(DISCONNECT_SOCK, disconnect_sock)                                    \
-_(DISCONNECT_SOCK_REPLY, disconnect_sock_reply)                                \
-_(ACCEPT_SOCK_REPLY, accept_sock_reply)                                \
-_(RESET_SOCK_REPLY, reset_sock_reply)                                  \
+_(SESSION_ENABLE_DISABLE, session_enable_disable)                      \
+_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)                                \
+_(SESSION_RULE_ADD_DEL, session_rule_add_del)                          \
+_(SESSION_RULES_DUMP, session_rules_dump)                              \
 
 static int
 send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
@@ -78,11 +82,14 @@ send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
 }
 
 static int
-send_session_accept_uri_callback (stream_session_t * s)
+send_session_accept_callback (stream_session_t * s)
 {
   vl_api_accept_session_t *mp;
   unix_shared_memory_queue_t *q, *vpp_queue;
   application_t *server = application_get (s->app_index);
+  transport_connection_t *tc;
+  transport_proto_vft_t *tp_vft;
+  stream_session_t *listener;
 
   q = vl_api_client_index_to_input_queue (server->api_client_index);
   vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
@@ -91,24 +98,28 @@ send_session_accept_uri_callback (stream_session_t * s)
     return -1;
 
   mp = vl_msg_api_alloc (sizeof (*mp));
-  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
-
-  /* Note: session_type is the first octet in all types of sessions */
+  memset (mp, 0, sizeof (*mp));
 
-  mp->accept_cookie = server->accept_cookie;
-  mp->server_rx_fifo = (u64) s->server_rx_fifo;
-  mp->server_tx_fifo = (u64) s->server_tx_fifo;
-  mp->session_thread_index = s->thread_index;
-  mp->session_index = s->session_index;
-  mp->session_type = s->session_type;
-  mp->vpp_event_queue_address = (u64) vpp_queue;
+  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
+  mp->context = server->index;
+  listener = listen_session_get (s->session_type, s->listener_index);
+  tp_vft = transport_protocol_get_vft (s->session_type);
+  tc = tp_vft->get_connection (s->connection_index, s->thread_index);
+  mp->listener_handle = listen_session_get_handle (listener);
+  mp->handle = session_handle (s);
+  mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
+  mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
+  mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
+  mp->port = tc->rmt_port;
+  mp->is_ip4 = tc->is_ip4;
+  clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
   vl_msg_api_send_shmem (q, (u8 *) & mp);
 
   return 0;
 }
 
 static void
-send_session_disconnect_uri_callback (stream_session_t * s)
+send_session_disconnect_callback (stream_session_t * s)
 {
   vl_api_disconnect_session_t *mp;
   unix_shared_memory_queue_t *q;
@@ -122,217 +133,73 @@ send_session_disconnect_uri_callback (stream_session_t * s)
   mp = vl_msg_api_alloc (sizeof (*mp));
   memset (mp, 0, sizeof (*mp));
   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
-
-  mp->session_thread_index = s->thread_index;
-  mp->session_index = s->session_index;
+  mp->handle = session_handle (s);
   vl_msg_api_send_shmem (q, (u8 *) & mp);
 }
 
-static int
-send_session_connected_uri_callback (u32 api_client_index,
-                                    stream_session_t * s, u8 is_fail)
+static void
+send_session_reset_callback (stream_session_t * s)
 {
-  vl_api_connect_uri_reply_t *mp;
+  vl_api_reset_session_t *mp;
   unix_shared_memory_queue_t *q;
-  application_t *app = application_lookup (api_client_index);
-  u8 *seg_name;
-  unix_shared_memory_queue_t *vpp_queue;
+  application_t *app = application_get (s->app_index);
 
   q = vl_api_client_index_to_input_queue (app->api_client_index);
 
   if (!q)
-    return -1;
-
-  mp = vl_msg_api_alloc (sizeof (*mp));
-  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_URI_REPLY);
-  mp->context = app->api_context;
-  mp->retval = is_fail;
-  if (!is_fail)
-    {
-      vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
-      mp->server_rx_fifo = (u64) s->server_rx_fifo;
-      mp->server_tx_fifo = (u64) s->server_tx_fifo;
-      mp->session_thread_index = s->thread_index;
-      mp->session_index = s->session_index;
-      mp->session_type = s->session_type;
-      mp->vpp_event_queue_address = (u64) vpp_queue;
-      mp->client_event_queue_address = (u64) app->event_queue;
-
-      session_manager_get_segment_info (s->server_segment_index, &seg_name,
-                                       &mp->segment_size);
-      mp->segment_name_length = vec_len (seg_name);
-      if (mp->segment_name_length)
-       clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
-    }
-
-  vl_msg_api_send_shmem (q, (u8 *) & mp);
-
-  /* Remove client if connect failed */
-  if (is_fail)
-    application_del (app);
-
-  return 0;
-}
-
-/**
- * Redirect a connect_uri message to the indicated server.
- * Only sent if the server has bound the related port with
- * URI_OPTIONS_FLAGS_USE_FIFO
- */
-static int
-redirect_connect_uri_callback (u32 server_api_client_index, void *mp_arg)
-{
-  vl_api_connect_uri_t *mp = mp_arg;
-  unix_shared_memory_queue_t *server_q, *client_q;
-  vlib_main_t *vm = vlib_get_main ();
-  f64 timeout = vlib_time_now (vm) + 0.5;
-  int rv = 0;
-
-  server_q = vl_api_client_index_to_input_queue (server_api_client_index);
-
-  if (!server_q)
-    {
-      rv = VNET_API_ERROR_INVALID_VALUE;
-      goto out;
-    }
-
-  client_q = vl_api_client_index_to_input_queue (mp->client_index);
-  if (!client_q)
-    {
-      rv = VNET_API_ERROR_INVALID_VALUE_2;
-      goto out;
-    }
-
-  /* Tell the server the client's API queue address, so it can reply */
-  mp->client_queue_address = (u64) client_q;
-
-  /*
-   * Bounce message handlers MUST NOT block the data-plane.
-   * Spin waiting for the queue lock, but
-   */
-
-  while (vlib_time_now (vm) < timeout)
-    {
-      rv =
-       unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
-      switch (rv)
-       {
-         /* correctly enqueued */
-       case 0:
-         return VNET_CONNECT_REDIRECTED;
-
-         /* continue spinning, wait for pthread_mutex_trylock to work */
-       case -1:
-         continue;
-
-         /* queue stuffed, drop the msg */
-       case -2:
-         rv = VNET_API_ERROR_QUEUE_FULL;
-         goto out;
-       }
-    }
-out:
-  /* Dispose of the message */
-  vl_msg_api_free (mp);
-  return rv;
-}
-
-static u64
-make_session_handle (stream_session_t * s)
-{
-  return (u64) s->session_index << 32 | (u64) s->thread_index;
-}
-
-static int
-send_session_accept_callback (stream_session_t * s)
-{
-  vl_api_accept_sock_t *mp;
-  unix_shared_memory_queue_t *q, *vpp_queue;
-  application_t *server = application_get (s->app_index);
-
-  q = vl_api_client_index_to_input_queue (server->api_client_index);
-  vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
-
-  if (!q)
-    return -1;
+    return;
 
   mp = vl_msg_api_alloc (sizeof (*mp));
-  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SOCK);
-
-  /* Note: session_type is the first octet in all types of sessions */
-
-  mp->accept_cookie = server->accept_cookie;
-  mp->server_rx_fifo = (u64) s->server_rx_fifo;
-  mp->server_tx_fifo = (u64) s->server_tx_fifo;
-  mp->handle = make_session_handle (s);
-  mp->vpp_event_queue_address = (u64) vpp_queue;
+  memset (mp, 0, sizeof (*mp));
+  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
+  mp->handle = session_handle (s);
   vl_msg_api_send_shmem (q, (u8 *) & mp);
-
-  return 0;
 }
 
-static int
-send_session_connected_callback (u32 api_client_index, stream_session_t * s,
-                                u8 is_fail)
+int
+send_session_connected_callback (u32 app_index, u32 api_context,
+                                stream_session_t * s, u8 is_fail)
 {
-  vl_api_connect_sock_reply_t *mp;
+  vl_api_connect_session_reply_t *mp;
   unix_shared_memory_queue_t *q;
-  application_t *app = application_lookup (api_client_index);
-  u8 *seg_name;
+  application_t *app;
   unix_shared_memory_queue_t *vpp_queue;
+  transport_connection_t *tc;
 
+  app = application_get (app_index);
   q = vl_api_client_index_to_input_queue (app->api_client_index);
 
   if (!q)
     return -1;
 
   mp = vl_msg_api_alloc (sizeof (*mp));
-  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SOCK_REPLY);
-  mp->context = app->api_context;
-  mp->retval = is_fail;
-  if (!is_fail)
-    {
-      vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
-      mp->server_rx_fifo = (u64) s->server_rx_fifo;
-      mp->server_tx_fifo = (u64) s->server_tx_fifo;
-      mp->handle = make_session_handle (s);
-      mp->vpp_event_queue_address = (u64) vpp_queue;
-      mp->client_event_queue_address = (u64) app->event_queue;
-
-      session_manager_get_segment_info (s->server_segment_index, &seg_name,
-                                       &mp->segment_size);
-      mp->segment_name_length = vec_len (seg_name);
-      if (mp->segment_name_length)
-       clib_memcpy (mp->segment_name, seg_name, mp->segment_name_length);
-    }
+  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
+  mp->context = api_context;
 
-  vl_msg_api_send_shmem (q, (u8 *) & mp);
-
-  /* Remove client if connect failed */
   if (is_fail)
-    application_del (app);
-
-  return 0;
-}
-
-static void
-send_session_disconnect_callback (stream_session_t * s)
-{
-  vl_api_disconnect_sock_t *mp;
-  unix_shared_memory_queue_t *q;
-  application_t *app = application_get (s->app_index);
-
-  q = vl_api_client_index_to_input_queue (app->api_client_index);
-
-  if (!q)
-    return;
+    goto done;
 
-  mp = vl_msg_api_alloc (sizeof (*mp));
-  memset (mp, 0, sizeof (*mp));
-  mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SOCK);
+  tc = session_get_transport (s);
+  if (!tc)
+    {
+      is_fail = 1;
+      goto done;
+    }
 
-  mp->handle = make_session_handle (s);
+  vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
+  mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
+  mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
+  mp->handle = session_handle (s);
+  mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
+  clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
+  mp->is_ip4 = tc->is_ip4;
+  mp->lcl_port = tc->lcl_port;
+
+done:
+  mp->retval = is_fail ?
+    clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
   vl_msg_api_send_shmem (q, (u8 *) & mp);
+  return 0;
 }
 
 /**
@@ -347,6 +214,7 @@ redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
   unix_shared_memory_queue_t *server_q, *client_q;
   vlib_main_t *vm = vlib_get_main ();
   f64 timeout = vlib_time_now (vm) + 0.5;
+  application_t *app;
   int rv = 0;
 
   server_q = vl_api_client_index_to_input_queue (server_api_client_index);
@@ -365,7 +233,16 @@ redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
     }
 
   /* Tell the server the client's API queue address, so it can reply */
-  mp->client_queue_address = (u64) client_q;
+  mp->client_queue_address = pointer_to_uword (client_q);
+  app = application_lookup (mp->client_index);
+  if (!app)
+    {
+      clib_warning ("no client application");
+      return -1;
+    }
+
+  mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
+  mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
 
   /*
    * Bounce message handlers MUST NOT block the data-plane.
@@ -380,7 +257,7 @@ redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
        {
          /* correctly enqueued */
        case 0:
-         return VNET_CONNECT_REDIRECTED;
+         return VNET_API_ERROR_SESSION_REDIRECT;
 
          /* continue spinning, wait for pthread_mutex_trylock to work */
        case -1:
@@ -398,123 +275,249 @@ out:
   return rv;
 }
 
-static session_cb_vft_t uri_session_cb_vft = {
-  .session_accept_callback = send_session_accept_uri_callback,
-  .session_disconnect_callback = send_session_disconnect_uri_callback,
-  .session_connected_callback = send_session_connected_uri_callback,
-  .add_segment_callback = send_add_segment_callback,
-  .redirect_connect_callback = redirect_connect_uri_callback
-};
-
 static session_cb_vft_t session_cb_vft = {
   .session_accept_callback = send_session_accept_callback,
   .session_disconnect_callback = send_session_disconnect_callback,
   .session_connected_callback = send_session_connected_callback,
+  .session_reset_callback = send_session_reset_callback,
   .add_segment_callback = send_add_segment_callback,
   .redirect_connect_callback = redirect_connect_callback
 };
 
-static int
-api_session_not_valid (u32 session_index, u32 thread_index)
+static void
+vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
 {
-  session_manager_main_t *smm = vnet_get_session_manager_main ();
-  stream_session_t *pool;
-
-  if (thread_index >= vec_len (smm->sessions))
-    return VNET_API_ERROR_INVALID_VALUE;
-
-  pool = smm->sessions[thread_index];
-
-  if (pool_is_free_index (pool, session_index))
-    return VNET_API_ERROR_INVALID_VALUE_2;
+  vl_api_session_enable_disable_reply_t *rmp;
+  vlib_main_t *vm = vlib_get_main ();
+  int rv = 0;
 
-  return 0;
+  vnet_session_enable_disable (vm, mp->is_enable);
+  REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
 }
 
 static void
-vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
+vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
 {
-  vl_api_bind_uri_reply_t *rmp;
-  vnet_bind_args_t _a, *a = &_a;
-  char segment_name[128];
-  u32 segment_name_length;
-  int rv;
+  vl_api_application_attach_reply_t *rmp;
+  vnet_app_attach_args_t _a, *a = &_a;
+  clib_error_t *error = 0;
+  int rv = 0;
 
-  _Static_assert (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
-                 sizeof (mp->options),
-                 "Out of options, fix api message definition");
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
 
-  segment_name_length = ARRAY_LEN (segment_name);
+  STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
+                sizeof (mp->options),
+                "Out of options, fix api message definition");
 
   memset (a, 0, sizeof (*a));
-
-  a->uri = (char *) mp->uri;
   a->api_client_index = mp->client_index;
   a->options = mp->options;
-  a->segment_name = segment_name;
-  a->segment_name_length = segment_name_length;
-  a->session_cb_vft = &uri_session_cb_vft;
+  a->session_cb_vft = &session_cb_vft;
 
-  a->options[SESSION_OPTIONS_SEGMENT_SIZE] = mp->initial_segment_size;
-  a->options[SESSION_OPTIONS_ACCEPT_COOKIE] = mp->accept_cookie;
-  rv = vnet_bind_uri (a);
+  if (mp->namespace_id_len > 64)
+    {
+      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 (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
+    }
+
+  if ((error = vnet_application_attach (a)))
+    {
+      rv = clib_error_get_code (error);
+      clib_error_report (error);
+    }
+  vec_free (a->namespace_id);
+
+done:
 
   /* *INDENT-OFF* */
-  REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
-    rmp->retval = rv;
+  REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
     if (!rv)
       {
        rmp->segment_name_length = 0;
-       /* $$$$ policy? */
-       rmp->segment_size = mp->initial_segment_size;
-       if (segment_name_length)
+       rmp->segment_size = a->segment_size;
+       if (a->segment_name_length)
          {
-           memcpy (rmp->segment_name, segment_name, segment_name_length);
-           rmp->segment_name_length = segment_name_length;
+           memcpy (rmp->segment_name, a->segment_name,
+                   a->segment_name_length);
+           rmp->segment_name_length = a->segment_name_length;
          }
-       rmp->server_event_queue_address = a->server_event_queue_address;
+       rmp->app_event_queue_address = a->app_event_queue_address;
       }
   }));
   /* *INDENT-ON* */
+}
+
+static void
+vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
+{
+  vl_api_application_detach_reply_t *rmp;
+  int rv = VNET_API_ERROR_INVALID_VALUE_2;
+  vnet_app_detach_args_t _a, *a = &_a;
+  application_t *app;
+
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
+
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      a->app_index = app->index;
+      rv = vnet_application_detach (a);
+    }
+
+done:
+  REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
+}
+
+static void
+vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
+{
+  vl_api_bind_uri_reply_t *rmp;
+  vnet_bind_args_t _a, *a = &_a;
+  application_t *app;
+  int rv;
+
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
+
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      memset (a, 0, sizeof (*a));
+      a->uri = (char *) mp->uri;
+      a->app_index = app->index;
+      rv = vnet_bind_uri (a);
+    }
+  else
+    {
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+    }
 
+done:
+  REPLY_MACRO (VL_API_BIND_URI_REPLY);
 }
 
 static void
 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
 {
   vl_api_unbind_uri_reply_t *rmp;
+  application_t *app;
+  vnet_unbind_args_t _a, *a = &_a;
   int rv;
 
-  rv = vnet_unbind_uri ((char *) mp->uri, mp->client_index);
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
 
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      a->uri = (char *) mp->uri;
+      a->app_index = app->index;
+      rv = vnet_unbind_uri (a);
+    }
+  else
+    {
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+    }
+
+done:
   REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
 }
 
 static void
 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
 {
+  vl_api_connect_session_reply_t *rmp;
   vnet_connect_args_t _a, *a = &_a;
+  application_t *app;
+  clib_error_t *error = 0;
+  int rv = 0;
 
-  a->uri = (char *) mp->uri;
-  a->api_client_index = mp->client_index;
-  a->api_context = mp->context;
-  a->options = mp->options;
-  a->session_cb_vft = &uri_session_cb_vft;
-  a->mp = mp;
-  vnet_connect_uri (a);
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
+
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      a->uri = (char *) mp->uri;
+      a->api_context = mp->context;
+      a->app_index = app->index;
+      a->mp = mp;
+      if ((error = vnet_connect_uri (a)))
+       {
+         rv = clib_error_get_code (error);
+         if (rv != VNET_API_ERROR_SESSION_REDIRECT)
+           clib_error_report (error);
+       }
+    }
+  else
+    {
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+    }
+
+  /*
+   * 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 || rv == VNET_API_ERROR_SESSION_REDIRECT)
+    return;
+
+done:
+  /* *INDENT-OFF* */
+  REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
+  /* *INDENT-ON* */
 }
 
 static void
 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
 {
   vl_api_disconnect_session_reply_t *rmp;
-  int rv;
+  vnet_disconnect_args_t _a, *a = &_a;
+  application_t *app;
+  int rv = 0;
 
-  rv = api_session_not_valid (mp->session_index, mp->session_thread_index);
-  if (!rv)
-    rv = vnet_disconnect_session (mp->client_index, mp->session_index,
-                                 mp->session_thread_index);
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
+
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      a->handle = mp->handle;
+      a->app_index = app->index;
+      rv = vnet_disconnect_session (a);
+    }
+  else
+    {
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+    }
 
+done:
   REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
 }
 
@@ -522,11 +525,8 @@ static void
 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
                                           mp)
 {
-  if (api_session_not_valid (mp->session_index, mp->session_thread_index))
-    {
-      clib_warning ("Invalid session!");
-      return;
-    }
+  vnet_disconnect_args_t _a, *a = &_a;
+  application_t *app;
 
   /* Client objected to disconnecting the session, log and continue */
   if (mp->retval)
@@ -536,16 +536,29 @@ vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
     }
 
   /* Disconnect has been confirmed. Confirm close to transport */
-  vnet_disconnect_session (mp->client_index, mp->session_index,
-                          mp->session_thread_index);
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      a->handle = mp->handle;
+      a->app_index = app->index;
+      vnet_disconnect_session (a);
+    }
 }
 
 static void
 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
 {
+  application_t *app;
   stream_session_t *s;
+  u32 index, thread_index;
+
+  app = application_lookup (mp->client_index);
+  if (!app)
+    return;
 
-  if (api_session_not_valid (mp->session_index, mp->session_thread_index))
+  session_parse_handle (mp->handle, &index, &thread_index);
+  s = session_get_if_valid (index, thread_index);
+  if (s == 0 || app->index != s->app_index)
     {
       clib_warning ("Invalid session!");
       return;
@@ -558,8 +571,6 @@ vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
       return;
     }
 
-  s = stream_session_get (mp->session_index, mp->session_thread_index);
-
   /* This comes as a response to a reset, transport only waiting for
    * confirmation to remove connection state, no need to disconnect */
   stream_session_cleanup (s);
@@ -569,22 +580,32 @@ static void
 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
 {
   stream_session_t *s;
-  int rv;
-
-  if (api_session_not_valid (mp->session_index, mp->session_thread_index))
-    return;
-
-  s = stream_session_get (mp->session_index, mp->session_thread_index);
-  rv = mp->retval;
+  u32 session_index, thread_index;
+  vnet_disconnect_args_t _a, *a = &_a;
 
-  if (rv)
+  /* Server isn't interested, kill the session */
+  if (mp->retval)
     {
-      /* Server isn't interested, kill the session */
-      stream_session_disconnect (s);
-      return;
+      a->app_index = mp->context;
+      a->handle = mp->handle;
+      vnet_disconnect_session (a);
+    }
+  else
+    {
+      session_parse_handle (mp->handle, &session_index, &thread_index);
+      s = session_get_if_valid (session_index, thread_index);
+      if (!s)
+       {
+         clib_warning ("session doesn't exist");
+         return;
+       }
+      if (s->app_index != mp->context)
+       {
+         clib_warning ("app doesn't own session");
+         return;
+       }
+      s->session_state = SESSION_STATE_READY;
     }
-
-  s->session_state = SESSION_STATE_READY;
 }
 
 static void
@@ -599,47 +620,40 @@ vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
 {
   vl_api_bind_sock_reply_t *rmp;
   vnet_bind_args_t _a, *a = &_a;
-  char segment_name[128];
-  u32 segment_name_length;
-  int rv;
-
-  STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
-                sizeof (mp->options),
-                "Out of options, fix api message definition");
-
-  segment_name_length = ARRAY_LEN (segment_name);
-
-  memset (a, 0, sizeof (*a));
-
-  clib_memcpy (&a->tep.ip, mp->ip,
-              (mp->is_ip4 ? sizeof (ip4_address_t) :
-               sizeof (ip6_address_t)));
-  a->tep.is_ip4 = mp->is_ip4;
-  a->tep.port = mp->port;
-  a->tep.vrf = mp->vrf;
-
-  a->api_client_index = mp->client_index;
-  a->options = mp->options;
-  a->segment_name = segment_name;
-  a->segment_name_length = segment_name_length;
-  a->session_cb_vft = &session_cb_vft;
+  int rv = 0;
+  clib_error_t *error;
+  application_t *app;
 
-  rv = vnet_bind_uri (a);
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
 
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
+      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->index;
+
+      if ((error = vnet_bind (a)))
+       {
+         rv = clib_error_get_code (error);
+         clib_error_report (error);
+       }
+    }
+done:
   /* *INDENT-OFF* */
-  REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY, ({
-    rmp->retval = rv;
+  REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
     if (!rv)
-      {
-       rmp->segment_name_length = 0;
-       rmp->segment_size = mp->options[SESSION_OPTIONS_SEGMENT_SIZE];
-       if (segment_name_length)
-         {
-           memcpy(rmp->segment_name, segment_name, segment_name_length);
-           rmp->segment_name_length = segment_name_length;
-         }
-       rmp->server_event_queue_address = a->server_event_queue_address;
-      }
+      rmp->handle = a->handle;
   }));
   /* *INDENT-ON* */
 }
@@ -649,116 +663,317 @@ vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
 {
   vl_api_unbind_sock_reply_t *rmp;
   vnet_unbind_args_t _a, *a = &_a;
-  int rv;
+  application_t *app;
+  clib_error_t *error;
+  int rv = 0;
 
-  a->api_client_index = mp->client_index;
-  a->handle = mp->handle;
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
 
-  rv = vnet_unbind (a);
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      a->app_index = mp->client_index;
+      a->handle = mp->handle;
+      if ((error = vnet_unbind (a)))
+       {
+         rv = clib_error_get_code (error);
+         clib_error_report (error);
+       }
+    }
 
+done:
   REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
 }
 
 static void
 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
 {
+  vl_api_connect_session_reply_t *rmp;
   vnet_connect_args_t _a, *a = &_a;
+  application_t *app;
+  clib_error_t *error = 0;
+  int rv = 0;
 
-  clib_memcpy (&a->tep.ip, mp->ip,
-              (mp->is_ip4 ? sizeof (ip4_address_t) :
-               sizeof (ip6_address_t)));
-  a->tep.is_ip4 = mp->is_ip4;
-  a->tep.port = mp->port;
-  a->tep.vrf = mp->vrf;
-  a->options = mp->options;
-  a->session_cb_vft = &session_cb_vft;
-  a->api_context = mp->context;
-  a->mp = mp;
+  if (session_manager_is_enabled () == 0)
+    {
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
+    }
 
-  vnet_connect (a);
-}
+  app = application_lookup (mp->client_index);
+  if (app)
+    {
+      unix_shared_memory_queue_t *client_q;
+      ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
+
+      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.fib_index = mp->vrf;
+      a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
+      a->api_context = mp->context;
+      a->app_index = app->index;
+      a->mp = mp;
+      if ((error = vnet_connect (a)))
+       {
+         rv = clib_error_get_code (error);
+         if (rv != VNET_API_ERROR_SESSION_REDIRECT)
+           clib_error_report (error);
+       }
+    }
+  else
+    {
+      rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
+    }
 
-static void
-vl_api_disconnect_sock_t_handler (vl_api_disconnect_sock_t * mp)
-{
-  vnet_disconnect_args_t _a, *a = &_a;
-  vl_api_disconnect_sock_reply_t *rmp;
-  int rv;
+  if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
+    return;
 
-  a->api_client_index = mp->client_index;
-  a->handle = mp->handle;
-  rv = vnet_disconnect (a);
+  /* Got some error, relay it */
 
-  REPLY_MACRO (VL_API_DISCONNECT_SOCK_REPLY);
+done:
+  REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
 }
 
 static void
-vl_api_disconnect_sock_reply_t_handler (vl_api_disconnect_sock_reply_t * mp)
+vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
 {
-  vnet_disconnect_args_t _a, *a = &_a;
-
-  /* Client objected to disconnecting the session, log and continue */
-  if (mp->retval)
+  vl_api_app_namespace_add_del_reply_t *rmp;
+  u8 *ns_id = 0;
+  clib_error_t *error = 0;
+  int rv = 0;
+  if (!session_manager_is_enabled ())
     {
-      clib_warning ("client retval %d", mp->retval);
-      return;
+      rv = VNET_API_ERROR_FEATURE_DISABLED;
+      goto done;
     }
 
-  a->api_client_index = mp->client_index;
-  a->handle = mp->handle;
+  if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
+    {
+      rv = VNET_API_ERROR_INVALID_VALUE;
+      goto done;
+    }
 
-  vnet_disconnect (a);
+  vec_validate (ns_id, mp->namespace_id_len - 1);
+  clib_memcpy (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
+  };
+  error = vnet_app_namespace_add_del (&args);
+  if (error)
+    {
+      rv = clib_error_get_code (error);
+      clib_error_report (error);
+    }
+  vec_free (ns_id);
+done:
+  REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY);
 }
 
 static void
-vl_api_reset_sock_reply_t_handler (vl_api_reset_sock_reply_t * mp)
+vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
 {
-  stream_session_t *s;
-  u32 session_index, thread_index;
+  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;
+  clib_error_t *error;
+  u8 fib_proto;
+  int rv = 0;
 
-  /* Client objected to resetting the session, log and continue */
-  if (mp->retval)
+  memset (&args, 0, sizeof (args));
+  fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
+
+  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 = clib_net_to_host_u16 (mp->lcl_port);
+  table_args->rmt_port = clib_net_to_host_u16 (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;
+
+  memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
+  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);
+  error = vnet_session_rule_add_del (&args);
+  if (error)
     {
-      clib_warning ("client retval %d", mp->retval);
-      return;
+      rv = clib_error_get_code (error);
+      clib_error_report (error);
     }
+  vec_free (table_args->tag);
+  REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
+}
 
-  if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
+static void
+send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
+                           u8 transport_proto, u32 appns_index, u8 * tag,
+                           unix_shared_memory_queue_t * q, u32 context)
+{
+  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));
+  memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
+  rmp->context = context;
+
+  rmp->is_ip4 = 1;
+  clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
+  clib_memcpy (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 = clib_host_to_net_u16 (match->lcl_port);
+  rmp->rmt_port = clib_host_to_net_u16 (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_warning ("Invalid handle");
-      return;
+      clib_memcpy (rmp->tag, tag, vec_len (tag));
+      rmp->tag[vec_len (tag)] = 0;
     }
 
-  s = stream_session_get (session_index, thread_index);
+  vl_msg_api_send_shmem (q, (u8 *) & rmp);
+}
 
-  /* This comes as a response to a reset, transport only waiting for
-   * confirmation to remove connection state, no need to disconnect */
-  stream_session_cleanup (s);
+static void
+send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
+                           u8 transport_proto, u32 appns_index, u8 * tag,
+                           unix_shared_memory_queue_t * q, u32 context)
+{
+  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;
+
+  rmp = vl_msg_api_alloc (sizeof (*rmp));
+  memset (rmp, 0, sizeof (*rmp));
+  rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
+  rmp->context = context;
+
+  rmp->is_ip4 = 0;
+  clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
+  clib_memcpy (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 = clib_host_to_net_u16 (match->lcl_port);
+  rmp->rmt_port = clib_host_to_net_u16 (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 (rmp->tag, tag, vec_len (tag));
+      rmp->tag[vec_len (tag)] = 0;
+    }
+
+  vl_msg_api_send_shmem (q, (u8 *) & rmp);
 }
 
 static void
-vl_api_accept_sock_reply_t_handler (vl_api_accept_sock_reply_t * mp)
+send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
+                                 u8 tp, u8 is_local, u32 appns_index,
+                                 unix_shared_memory_queue_t * q, u32 context)
 {
-  stream_session_t *s;
-  u32 session_index, thread_index;
+  mma_rule_16_t *rule16;
+  mma_rule_40_t *rule40;
+  mma_rules_table_16_t *srt16;
+  mma_rules_table_40_t *srt40;
+  u32 ri;
 
-  if (api_parse_session_handle (mp->handle, &session_index, &thread_index))
+  if (is_local || fib_proto == FIB_PROTOCOL_IP4)
     {
-      clib_warning ("Invalid handle");
-      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,
+                                    q, context);
+      }));
+      /* *INDENT-ON* */
     }
-  s = stream_session_get (session_index, thread_index);
-
-  if (mp->retval)
+  if (is_local || fib_proto == FIB_PROTOCOL_IP6)
     {
-      /* Server isn't interested, kill the session */
-      stream_session_disconnect (s);
-      return;
+      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,
+                                    q, context);
+      }));
+      /* *INDENT-ON* */
     }
+}
+
+static void
+vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
+{
+  unix_shared_memory_queue_t *q = NULL;
+  session_table_t *st;
+  u8 tp;
+
+  q = vl_api_client_index_to_input_queue (mp->client_index);
+  if (q == 0)
+    return;
+
+  /* *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, q,
+                                          mp->context);
+      }
+  }));
+  /* *INDENT-ON* */
+}
 
-  s->session_state = SESSION_STATE_READY;
+static clib_error_t *
+application_reaper_cb (u32 client_index)
+{
+  application_t *app = application_lookup (client_index);
+  vnet_app_detach_args_t _a, *a = &_a;
+  if (app)
+    {
+      a->app_index = app->index;
+      vnet_application_detach (a);
+    }
+  return 0;
 }
 
+VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
+
 #define vl_msg_name_crc_list
 #include <vnet/vnet_all_api_h.h>
 #undef vl_msg_name_crc_list
@@ -812,6 +1027,7 @@ session_api_hookup (vlib_main_t * vm)
 }
 
 VLIB_API_INIT_FUNCTION (session_api_hookup);
+
 /*
  * fd.io coding-style-patch-verification: ON
  *