vcl: wait for segments with segment handle
[vpp.git] / src / vnet / session / application_interface.c
index ad4c28b..d254b9b 100644 (file)
     VPP's application/session API bind/unbind/connect/disconnect calls
 */
 
+/*
+ * TLS server cert and keys to be used for testing only
+ */
+const char test_srv_crt_rsa[] =
+  "-----BEGIN CERTIFICATE-----\r\n"
+  "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
+  "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
+  "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
+  "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
+  "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
+  "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
+  "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
+  "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
+  "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
+  "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
+  "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
+  "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
+  "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
+  "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
+  "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
+  "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
+  "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
+  "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
+  "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
+  "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
+  "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
+  "-----END CERTIFICATE-----\r\n";
+const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
+
+const char test_srv_key_rsa[] =
+  "-----BEGIN PRIVATE KEY-----\r\n"
+  "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
+  "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
+  "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
+  "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
+  "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
+  "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
+  "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
+  "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
+  "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
+  "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
+  "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
+  "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
+  "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
+  "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
+  "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
+  "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
+  "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
+  "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
+  "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
+  "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
+  "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
+  "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
+  "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
+  "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
+  "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
+  "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
+const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
+
+#define app_interface_check_thread_and_barrier(_fn, _arg)              \
+  if (PREDICT_FALSE (!vlib_thread_is_main_w_barrier ()))               \
+    {                                                                  \
+      vlib_rpc_call_main_thread (_fn, (u8 *) _arg, sizeof(*_arg));     \
+      return 0;                                                                \
+    }
+
 static u8
 session_endpoint_is_local (session_endpoint_t * sep)
 {
@@ -38,8 +104,8 @@ session_endpoint_is_zero (session_endpoint_t * sep)
 u8
 session_endpoint_in_ns (session_endpoint_t * sep)
 {
-  u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4);
-  if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX
+  u8 is_lep = session_endpoint_is_local (sep);
+  if (!is_lep && sep->sw_if_index != ENDPOINT_INVALID_INDEX
       && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
     {
       clib_warning ("sw_if_index %u not configured with ip %U",
@@ -47,7 +113,7 @@ session_endpoint_in_ns (session_endpoint_t * sep)
                    sep->is_ip4);
       return 0;
     }
-  return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
+  return (is_lep || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
 }
 
 int
@@ -59,10 +125,10 @@ api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
   *thread_index = handle & 0xFFFFFFFF;
   *session_index = handle >> 32;
 
-  if (*thread_index >= vec_len (smm->sessions))
+  if (*thread_index >= vec_len (smm->wrk))
     return VNET_API_ERROR_INVALID_VALUE;
 
-  pool = smm->sessions[*thread_index];
+  pool = smm->wrk[*thread_index].sessions;
 
   if (pool_is_free_index (pool, *session_index))
     return VNET_API_ERROR_INVALID_VALUE_2;
@@ -71,176 +137,201 @@ api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
 }
 
 static void
-session_endpoint_update_for_app (session_endpoint_t * sep,
-                                application_t * app)
+session_endpoint_update_for_app (session_endpoint_cfg_t * sep,
+                                application_t * app, u8 is_connect)
 {
   app_namespace_t *app_ns;
-  app_ns = app_namespace_get (app->ns_index);
-  if (app_ns)
+  u32 ns_index, fib_index;
+
+  ns_index = app->ns_index;
+
+  /* App is a transport proto, so fetch the calling app's ns */
+  if (app->flags & APP_OPTIONS_FLAGS_IS_TRANSPORT_APP)
+    {
+      app_worker_t *owner_wrk;
+      application_t *owner_app;
+
+      owner_wrk = app_worker_get (sep->app_wrk_index);
+      owner_app = application_get (owner_wrk->app_index);
+      ns_index = owner_app->ns_index;
+    }
+  app_ns = app_namespace_get (ns_index);
+  if (!app_ns)
+    return;
+
+  /* Ask transport and network to bind to/connect using local interface
+   * that "supports" app's namespace. This will fix our local connection
+   * endpoint.
+   */
+
+  /* If in default namespace and user requested a fib index use it */
+  if (ns_index == 0 && sep->fib_index != ENDPOINT_INVALID_INDEX)
+    fib_index = sep->fib_index;
+  else
+    fib_index = sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
+  sep->peer.fib_index = fib_index;
+  sep->fib_index = fib_index;
+
+  if (!is_connect)
     {
-      /* Ask transport and network to bind to/connect using local interface
-       * that "supports" app's namespace. This will fix our local connection
-       * endpoint.
-       */
       sep->sw_if_index = app_ns->sw_if_index;
-      sep->fib_index =
-       sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
+    }
+  else
+    {
+      if (app_ns->sw_if_index != APP_NAMESPACE_INVALID_INDEX
+         && sep->peer.sw_if_index != ENDPOINT_INVALID_INDEX
+         && sep->peer.sw_if_index != app_ns->sw_if_index)
+       clib_warning ("Local sw_if_index different from app ns sw_if_index");
+
+      sep->peer.sw_if_index = app_ns->sw_if_index;
     }
 }
 
-static int
-vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
+static inline int
+vnet_bind_inline (vnet_bind_args_t * a)
 {
+  u64 ll_handle = SESSION_INVALID_HANDLE;
+  app_worker_t *app_wrk;
   application_t *app;
-  u32 table_index;
-  u64 listener;
-  int rv, have_local = 0;
+  int rv;
 
-  app = application_get_if_valid (app_index);
+  app = application_get_if_valid (a->app_index);
   if (!app)
     {
       SESSION_DBG ("app not attached");
       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
     }
+  app_wrk = application_get_worker (app, a->wrk_map_index);
+  a->sep_ext.app_wrk_index = app_wrk->wrk_index;
 
-  session_endpoint_update_for_app (sep, app);
-  if (!session_endpoint_in_ns (sep))
+  session_endpoint_update_for_app (&a->sep_ext, app, 0 /* is_connect */ );
+  if (!session_endpoint_in_ns (&a->sep))
     return VNET_API_ERROR_INVALID_VALUE_2;
 
-  table_index = application_session_table (app,
-                                          session_endpoint_fib_proto (sep));
-  listener = session_lookup_endpoint_listener (table_index, sep, 1);
-  if (listener != SESSION_INVALID_HANDLE)
-    return VNET_API_ERROR_ADDRESS_IN_USE;
-
   /*
    * Add session endpoint to local session table. Only binds to "inaddr_any"
    * (i.e., zero address) are added to local scope table.
    */
-  if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
+  if (application_has_local_scope (app)
+      && session_endpoint_is_local (&a->sep))
     {
-      table_index = application_local_session_table (app);
-      listener = session_lookup_endpoint_listener (table_index, sep, 1);
-      if (listener != SESSION_INVALID_HANDLE)
-       return VNET_API_ERROR_ADDRESS_IN_USE;
-      session_lookup_add_session_endpoint (table_index, sep, app->index);
-      *handle = session_lookup_local_listener_make_handle (sep);
-      have_local = 1;
+      if ((rv = application_start_local_listen (app, &a->sep_ext,
+                                               &a->handle)))
+       return rv;
+      ll_handle = a->handle;
     }
 
   if (!application_has_global_scope (app))
-    return (have_local - 1);
+    return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
 
   /*
    * Add session endpoint to global session table
    */
 
   /* Setup listen path down to transport */
-  rv = application_start_listen (app, sep, handle);
-  if (rv && have_local)
-    session_lookup_del_session_endpoint (table_index, sep);
+  rv = application_start_listen (app, &a->sep_ext, &a->handle);
+  if (rv && ll_handle != SESSION_INVALID_HANDLE)
+    {
+      application_stop_local_listen (a->app_index, a->wrk_map_index,
+                                    ll_handle);
+      return rv;
+    }
+
+  /*
+   * Store in local table listener the index of the transport layer
+   * listener. We'll need if if local listeners are hit and we need to
+   * return global handle
+   */
+  if (ll_handle != SESSION_INVALID_HANDLE)
+    {
+      local_session_t *ll;
+      stream_session_t *tl;
+      ll = application_get_local_listener_w_handle (ll_handle);
+      tl = listen_session_get_from_handle (a->handle);
+      if (ll->transport_listener_index == ~0)
+       ll->transport_listener_index = tl->session_index;
+    }
   return rv;
 }
 
-int
-vnet_unbind_i (u32 app_index, u64 handle)
+static inline int
+vnet_unbind_inline (vnet_unbind_args_t * a)
 {
-  application_t *app = application_get_if_valid (app_index);
-  stream_session_t *listener = 0;
-  u32 table_index;
+  application_t *app;
+  int rv;
 
-  if (!app)
+  if (!(app = application_get_if_valid (a->app_index)))
     {
-      SESSION_DBG ("app (%d) not attached", app_index);
+      SESSION_DBG ("app (%d) not attached", wrk_map_index);
       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
     }
 
-  /*
-   * Clean up local session table. If we have a listener session use it to
-   * find the port and proto. If not, the handle must be a local table handle
-   * so parse it.
-   */
-
   if (application_has_local_scope (app))
     {
-      session_endpoint_t sep = SESSION_ENDPOINT_NULL;
-      if (!session_lookup_local_is_handle (handle))
-       listener = listen_session_get_from_handle (handle);
-      if (listener)
-       {
-         if (listen_session_get_local_session_endpoint (listener, &sep))
-           {
-             clib_warning ("broken listener");
-             return -1;
-           }
-       }
-      else
-       {
-         if (session_lookup_local_listener_parse_handle (handle, &sep))
-           {
-             clib_warning ("can't parse handle");
-             return -1;
-           }
-       }
-      table_index = application_local_session_table (app);
-      session_lookup_del_session_endpoint (table_index, &sep);
+      if ((rv = application_stop_local_listen (a->app_index,
+                                              a->wrk_map_index, a->handle)))
+       return rv;
     }
 
   /*
    * Clear the global scope table of the listener
    */
   if (application_has_global_scope (app))
-    return application_stop_listen (app, handle);
+    return application_stop_listen (a->app_index, a->wrk_map_index,
+                                   a->handle);
   return 0;
 }
 
 static int
-app_connect_redirect (application_t * server, void *mp)
-{
-  return server->cb_fns.redirect_connect_callback (server->api_client_index,
-                                                  mp);
-}
-
-int
-vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
-               void *mp)
+application_connect (vnet_connect_args_t * a)
 {
-  application_t *server, *app;
-  u32 table_index, server_index;
+  app_worker_t *server_wrk, *client_wrk;
+  u32 table_index, server_index, li;
   stream_session_t *listener;
+  application_t *client, *server;
+  local_session_t *ll;
+  u8 fib_proto;
+  u64 lh;
 
-  if (session_endpoint_is_zero (sep))
+  if (session_endpoint_is_zero (&a->sep))
     return VNET_API_ERROR_INVALID_VALUE;
 
-  app = application_get (app_index);
-  session_endpoint_update_for_app (sep, app);
+  client = application_get (a->app_index);
+  session_endpoint_update_for_app (&a->sep_ext, client, 1 /* is_connect */ );
+  client_wrk = application_get_worker (client, a->wrk_map_index);
 
   /*
-   * First check the the local scope for locally attached destinations.
+   * First check the local scope for locally attached destinations.
    * If we have local scope, we pass *all* connects through it since we may
    * have special policy rules even for non-local destinations, think proxy.
    */
-  if (application_has_local_scope (app))
+  if (application_has_local_scope (client))
     {
-      table_index = application_local_session_table (app);
-      server_index = session_lookup_local_endpoint (table_index, sep);
-      if (server_index == APP_DROP_INDEX)
+      table_index = application_local_session_table (client);
+      lh = session_lookup_local_endpoint (table_index, &a->sep);
+      if (lh == SESSION_DROP_HANDLE)
        return VNET_API_ERROR_APP_CONNECT_FILTERED;
 
+      if (lh == SESSION_INVALID_HANDLE)
+       goto global_scope;
+
+      local_session_parse_handle (lh, &server_index, &li);
+
       /*
        * Break loop if rule in local table points to connecting app. This
        * can happen if client is a generic proxy. Route connect through
        * global table instead.
        */
-      if (server_index != app_index)
+      if (server_index != a->app_index)
        {
          server = application_get (server_index);
-         /*
-          * Server is willing to have a direct fifo connection created
-          * instead of going through the state machine, etc.
-          */
-         if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
-           return app_connect_redirect (server, mp);
+         ll = application_get_local_listen_session (server, li);
+         listener = (stream_session_t *) ll;
+         server_wrk = application_listener_select_worker (listener,
+                                                          1 /* is_local */ );
+         return application_local_session_connect (client_wrk,
+                                                   server_wrk, ll,
+                                                   a->api_context);
        }
     }
 
@@ -248,26 +339,30 @@ vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
    * If nothing found, check the global scope for locally attached
    * destinations. Make sure first that we're allowed to.
    */
-  if (session_endpoint_is_local (sep))
+
+global_scope:
+  if (session_endpoint_is_local (&a->sep))
     return VNET_API_ERROR_SESSION_CONNECT;
 
-  if (!application_has_global_scope (app))
+  if (!application_has_global_scope (client))
     return VNET_API_ERROR_APP_CONNECT_SCOPE;
 
-  table_index = application_session_table (app,
-                                          session_endpoint_fib_proto (sep));
-  listener = session_lookup_listener (table_index, sep);
+  fib_proto = session_endpoint_fib_proto (&a->sep);
+  table_index = application_session_table (client, fib_proto);
+  listener = session_lookup_listener (table_index, &a->sep);
   if (listener)
     {
-      server = application_get (listener->app_index);
-      if (server && (server->flags & APP_OPTIONS_FLAGS_ACCEPT_REDIRECT))
-       return app_connect_redirect (server, mp);
+      server_wrk = application_listener_select_worker (listener,
+                                                      0 /* is_local */ );
+      ll = (local_session_t *) listener;
+      return application_local_session_connect (client_wrk, server_wrk, ll,
+                                               a->api_context);
     }
 
   /*
    * Not connecting to a local server, propagate to transport
    */
-  if (application_open_session (app, sep, api_context))
+  if (app_worker_open_session (client_wrk, &a->sep, a->api_context))
     return VNET_API_ERROR_SESSION_CONNECT;
   return 0;
 }
@@ -275,8 +370,9 @@ vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
 /**
  * unformat a vnet URI
  *
- * transport-proto://ip46-addr:port
- * eg. tcp://ip46-addr:port
+ * transport-proto://[hostname]ip46-addr:port
+ * eg.         tcp://ip46-addr:port
+ *     tls://[testtsl.fd.io]ip46-addr:port
  *
  * u8 ip46_address[16];
  * u16  port_in_host_byte_order;
@@ -291,23 +387,41 @@ vnet_connect_i (u32 app_index, u32 api_context, session_endpoint_t * sep,
 uword
 unformat_vnet_uri (unformat_input_t * input, va_list * args)
 {
-  session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
-  u32 transport_proto = 0;
+  session_endpoint_cfg_t *sep = va_arg (*args, session_endpoint_cfg_t *);
+  u32 transport_proto = 0, port;
+
   if (unformat (input, "%U://%U/%d", unformat_transport_proto,
-               &transport_proto, unformat_ip4_address, &sep->ip.ip4,
-               &sep->port))
+               &transport_proto, unformat_ip4_address, &sep->ip.ip4, &port))
     {
       sep->transport_proto = transport_proto;
-      sep->port = clib_host_to_net_u16 (sep->port);
+      sep->port = clib_host_to_net_u16 (port);
       sep->is_ip4 = 1;
       return 1;
     }
-  if (unformat (input, "%U://%U/%d", unformat_transport_proto,
-               &transport_proto, unformat_ip6_address, &sep->ip.ip6,
-               &sep->port))
+  else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
+                    &transport_proto, &sep->hostname, unformat_ip4_address,
+                    &sep->ip.ip4, &port))
+    {
+      sep->transport_proto = transport_proto;
+      sep->port = clib_host_to_net_u16 (port);
+      sep->is_ip4 = 1;
+      return 1;
+    }
+  else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
+                    &transport_proto, unformat_ip6_address, &sep->ip.ip6,
+                    &port))
     {
       sep->transport_proto = transport_proto;
-      sep->port = clib_host_to_net_u16 (sep->port);
+      sep->port = clib_host_to_net_u16 (port);
+      sep->is_ip4 = 0;
+      return 1;
+    }
+  else if (unformat (input, "%U://[%s]%U/%d", unformat_transport_proto,
+                    &transport_proto, &sep->hostname, unformat_ip6_address,
+                    &sep->ip.ip6, &port))
+    {
+      sep->transport_proto = transport_proto;
+      sep->port = clib_host_to_net_u16 (port);
       sep->is_ip4 = 0;
       return 1;
     }
@@ -315,10 +429,10 @@ unformat_vnet_uri (unformat_input_t * input, va_list * args)
 }
 
 static u8 *cache_uri;
-static session_endpoint_t *cache_sep;
+static session_endpoint_cfg_t *cache_sep;
 
 int
-parse_uri (char *uri, session_endpoint_t * sep)
+parse_uri (char *uri, session_endpoint_cfg_t * sep)
 {
   unformat_input_t _input, *input = &_input;
 
@@ -351,7 +465,7 @@ parse_uri (char *uri, session_endpoint_t * sep)
 }
 
 static int
-session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
+app_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
 {
   app_namespace_t *app_ns;
   if (vec_len (namespace_id) == 0)
@@ -372,6 +486,19 @@ session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
   return 0;
 }
 
+static u8 *
+app_name_from_api_index (u32 api_client_index)
+{
+  vl_api_registration_t *regp;
+  regp = vl_api_client_index_to_registration (api_client_index);
+  if (regp)
+    return format (0, "%s%c", regp->name, 0);
+
+  clib_warning ("api client index %u does not have an api registration!",
+               api_client_index);
+  return format (0, "unknown%c", 0);
+}
+
 /**
  * Attach application to vpp
  *
@@ -384,37 +511,57 @@ vnet_application_attach (vnet_app_attach_args_t * a)
 {
   svm_fifo_segment_private_t *fs;
   application_t *app = 0;
+  app_worker_t *app_wrk;
   segment_manager_t *sm;
   u32 app_ns_index = 0;
+  u8 *app_name = 0;
   u64 secret;
   int rv;
 
-  app = application_lookup (a->api_client_index);
+  if (a->api_client_index != APP_INVALID_INDEX)
+    app = application_lookup (a->api_client_index);
+  else if (a->name)
+    app = application_lookup_name (a->name);
+  else
+    return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
+                                  "api index or name must be provided");
+
   if (app)
-    return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED,
-                                  0, "app already attached");
+    return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED, 0,
+                                  "app already attached");
+
+  if (a->api_client_index != APP_INVALID_INDEX)
+    {
+      app_name = app_name_from_api_index (a->api_client_index);
+      a->name = app_name;
+    }
 
   secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
-  if ((rv = session_validate_namespace (a->namespace_id, secret,
-                                       &app_ns_index)))
+  if ((rv = app_validate_namespace (a->namespace_id, secret, &app_ns_index)))
     return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
   a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
-  app = application_new ();
-  if ((rv = application_init (app, a->api_client_index, a->options,
-                             a->session_cb_vft)))
+
+  if ((rv = application_alloc_and_init ((app_init_args_t *) a)))
     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
 
-  a->app_event_queue_address = pointer_to_uword (app->event_queue);
-  sm = segment_manager_get (app->first_segment_manager);
-  fs = segment_manager_get_segment (sm->segment_indices[0]);
+  app = application_get (a->app_index);
+  if ((rv = app_worker_alloc_and_init (app, &app_wrk)))
+    return clib_error_return_code (0, rv, 0, "app default wrk init: %d", rv);
+
+  a->app_evt_q = app_wrk->event_queue;
+  app_wrk->api_client_index = a->api_client_index;
+  sm = segment_manager_get (app_wrk->first_segment_manager);
+  fs = segment_manager_get_segment_w_lock (sm, 0);
 
   if (application_is_proxy (app))
     application_setup_proxy (app);
 
   ASSERT (vec_len (fs->ssvm.name) <= 128);
   a->segment = &fs->ssvm;
-  a->app_index = app->index;
+  a->segment_handle = segment_manager_segment_handle (sm, fs);
 
+  segment_manager_segment_reader_unlock (sm);
+  vec_free (app_name);
   return 0;
 }
 
@@ -425,62 +572,69 @@ int
 vnet_application_detach (vnet_app_detach_args_t * a)
 {
   application_t *app;
-  app = application_get_if_valid (a->app_index);
 
+  app = application_get_if_valid (a->app_index);
   if (!app)
     {
       clib_warning ("app not attached");
       return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
     }
 
-  application_del (app);
+  app_interface_check_thread_and_barrier (vnet_application_detach, a);
+  application_detach_process (app, a->api_client_index);
   return 0;
 }
 
 int
 vnet_bind_uri (vnet_bind_args_t * a)
 {
-  session_endpoint_t sep = SESSION_ENDPOINT_NULL;
+  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
   int rv;
 
   rv = parse_uri (a->uri, &sep);
   if (rv)
     return rv;
-
-  return vnet_bind_i (a->app_index, &sep, &a->handle);
+  sep.app_wrk_index = 0;
+  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
+  return vnet_bind_inline (a);
 }
 
 int
 vnet_unbind_uri (vnet_unbind_args_t * a)
 {
+  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
   stream_session_t *listener;
-  session_endpoint_t sep = SESSION_ENDPOINT_NULL;
+  u32 table_index;
   int rv;
 
   rv = parse_uri (a->uri, &sep);
   if (rv)
     return rv;
 
-  /* NOTE: only default table supported for uri */
-  listener = session_lookup_listener (0, &sep);
+  /* NOTE: only default fib tables supported for uri apis */
+  table_index = session_lookup_get_index_for_fib (fib_ip_proto (!sep.is_ip4),
+                                                 0);
+  listener = session_lookup_listener (table_index,
+                                     (session_endpoint_t *) & sep);
   if (!listener)
     return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
-
-  return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
+  a->handle = listen_session_get_handle (listener);
+  return vnet_unbind_inline (a);
 }
 
 clib_error_t *
 vnet_connect_uri (vnet_connect_args_t * a)
 {
-  session_endpoint_t sep_null = SESSION_ENDPOINT_NULL;
+  session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
   int rv;
 
   /* Parse uri */
-  a->sep = sep_null;
-  rv = parse_uri (a->uri, &a->sep);
+  rv = parse_uri (a->uri, &sep);
   if (rv)
     return clib_error_return_code (0, rv, 0, "app init: %d", rv);
-  if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
+
+  clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
+  if ((rv = application_connect (a)))
     return clib_error_return_code (0, rv, 0, "connect failed");
   return 0;
 }
@@ -488,19 +642,35 @@ vnet_connect_uri (vnet_connect_args_t * a)
 int
 vnet_disconnect_session (vnet_disconnect_args_t * a)
 {
-  u32 index, thread_index;
-  stream_session_t *s;
+  if (session_handle_is_local (a->handle))
+    {
+      local_session_t *ls;
 
-  session_parse_handle (a->handle, &index, &thread_index);
-  s = session_get_if_valid (index, thread_index);
+      /* Disconnect reply came to worker 1 not main thread */
+      app_interface_check_thread_and_barrier (vnet_disconnect_session, a);
 
-  if (!s || s->app_index != a->app_index)
-    return VNET_API_ERROR_INVALID_VALUE;
+      if (!(ls = application_get_local_session_from_handle (a->handle)))
+       return 0;
 
-  /* We're peeking into another's thread pool. Make sure */
-  ASSERT (s->session_index == index);
+      return application_local_session_disconnect (a->app_index, ls);
+    }
+  else
+    {
+      app_worker_t *app_wrk;
+      stream_session_t *s;
 
-  stream_session_disconnect (s);
+      s = session_get_from_handle_if_valid (a->handle);
+      if (!s)
+       return VNET_API_ERROR_INVALID_VALUE;
+      app_wrk = app_worker_get (s->app_wrk_index);
+      if (app_wrk->app_index != a->app_index)
+       return VNET_API_ERROR_INVALID_VALUE;
+
+      /* We're peeking into another's thread pool. Make sure */
+      ASSERT (s->session_index == session_index_from_handle (a->handle));
+
+      stream_session_disconnect (s);
+    }
   return 0;
 }
 
@@ -508,8 +678,8 @@ clib_error_t *
 vnet_bind (vnet_bind_args_t * a)
 {
   int rv;
-  if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle)))
-    return clib_error_return_code (0, rv, 0, "bind failed");
+  if ((rv = vnet_bind_inline (a)))
+    return clib_error_return_code (0, rv, 0, "bind failed: %d", rv);
   return 0;
 }
 
@@ -517,8 +687,8 @@ clib_error_t *
 vnet_unbind (vnet_unbind_args_t * a)
 {
   int rv;
-  if ((rv = vnet_unbind_i (a->app_index, a->handle)))
-    return clib_error_return_code (0, rv, 0, "unbind failed");
+  if ((rv = vnet_unbind_inline (a)))
+    return clib_error_return_code (0, rv, 0, "unbind failed: %d", rv);
   return 0;
 }
 
@@ -526,8 +696,9 @@ clib_error_t *
 vnet_connect (vnet_connect_args_t * a)
 {
   int rv;
-  if ((rv = vnet_connect_i (a->app_index, a->api_context, &a->sep, a->mp)))
-    return clib_error_return_code (0, rv, 0, "connect failed");
+
+  if ((rv = application_connect (a)))
+    return clib_error_return_code (0, rv, 0, "connect failed: %d", rv);
   return 0;
 }