session: refactor local/cut-through listens

Introduce a cut-through transport as replacement to local sessions. This
first patch removes the per app local listener pools and switches local
listeners to sessions with cut-through transport.

Change-Id: I98b20c25bc5df5f4110bf66f76c650a973032618
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
index 954b934..4d95426 100644
--- a/src/vnet/session/application.c
+++ b/src/vnet/session/application.c
@@ -124,7 +124,6 @@
   u32 table_index, fib_proto;
   session_endpoint_t *sep;
   session_handle_t handle;
-  local_session_t *ll;
   session_t *ls;
 
   sep = (session_endpoint_t *) sep_ext;
@@ -134,8 +133,8 @@
       handle = session_lookup_endpoint_listener (table_index, sep, 1);
       if (handle != SESSION_INVALID_HANDLE)
 	{
-	  ll = application_get_local_listener_w_handle (handle);
-	  return app_listener_get_w_session ((session_t *) ll);
+	  ls = listen_session_get_from_handle (handle);
+	  return app_listener_get_w_session (ls);
 	}
     }
 
@@ -157,6 +156,7 @@
 			     app_listener_t ** listener)
 {
   app_listener_t *app_listener;
+  transport_connection_t *tc;
   local_session_t *ll = 0;
   session_handle_t lh;
   session_type_t st;
@@ -175,18 +175,31 @@
   if (application_has_local_scope (app)
       && session_endpoint_is_local ((session_endpoint_t *) sep))
     {
+      session_type_t local_st;
       u32 table_index;
 
-      ll = application_local_listen_session_alloc (app);
-      ll->port = sep->port;
-      /* Store the original session type for the unbind */
-      ll->listener_session_type = st;
+      local_st = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
+						 sep->is_ip4);
+      ls = listen_session_alloc (0, local_st);
+      ls->app_index = app->app_index;
+      ls->app_wrk_index = sep->app_wrk_index;
+      lh = session_handle (ls);
+
+      if ((rv = session_listen (ls, sep)))
+	{
+	  ls = session_get_from_handle (lh);
+	  session_free (ls);
+	  return rv;
+	}
+
+      ls = session_get_from_handle (lh);
+      app_listener = app_listener_get (app, al_index);
+      app_listener->local_index = ls->session_index;
+      ls->al_index = al_index;
+
       table_index = application_local_session_table (app);
-      lh = application_local_session_handle (ll);
       session_lookup_add_session_endpoint (table_index,
 					   (session_endpoint_t *) sep, lh);
-      app_listener->local_index = ll->session_index;
-      ll->al_index = app_listener->al_index;
     }
 
   if (application_has_global_scope (app))
@@ -203,18 +216,26 @@
 
       /* Listen pool can be reallocated if the transport is
        * recursive (tls) */
-      lh = session_handle (ls);
+      lh = listen_session_get_handle (ls);
 
       if ((rv = session_listen (ls, sep)))
 	{
-	  ls = session_get_from_handle (lh);
+	  ls = listen_session_get_from_handle (lh);
 	  session_free (ls);
 	  return rv;
 	}
-      ls = session_get_from_handle (lh);
+      ls = listen_session_get_from_handle (lh);
       app_listener = app_listener_get (app, al_index);
       app_listener->session_index = ls->session_index;
       ls->al_index = al_index;
+
+      /* Add to the global lookup table after transport was initialized.
+       * Lookup table needs to be populated only now because sessions
+       * with cut-through transport are are added to app local tables that
+       * are not related to network fibs, i.e., cannot be added as
+       * connections */
+      tc = session_get_transport (ls);
+      session_lookup_add_connection (tc, lh);
     }
 
   if (!ll && !ls)
@@ -231,24 +252,24 @@
 app_listener_cleanup (app_listener_t * al)
 {
   application_t *app = application_get (al->app_index);
+  session_t *ls;
 
   if (al->session_index != SESSION_INVALID_INDEX)
     {
-      session_t *ls = session_get (al->session_index, 0);
+      ls = session_get (al->session_index, 0);
       session_stop_listen (ls);
       listen_session_free (ls);
     }
   if (al->local_index != SESSION_INVALID_INDEX)
     {
       session_endpoint_t sep = SESSION_ENDPOINT_NULL;
-      local_session_t *ll;
       u32 table_index;
 
       table_index = application_local_session_table (app);
-      ll = application_get_local_listen_session (app, al->local_index);
-      application_local_listener_session_endpoint (ll, &sep);
+      ls = listen_session_get (al->local_index);
+      application_local_listener_session_endpoint (ls, &sep);
       session_lookup_del_session_endpoint (table_index, &sep);
-      application_local_listen_session_free (app, ll);
+      listen_session_free (ls);
     }
   app_listener_free (app, al);
 }
@@ -278,6 +299,14 @@
   return listen_session_get (al->session_index);
 }
 
+session_t *
+app_listener_get_local_session (app_listener_t * al)
+{
+  if (al->local_index == SESSION_INVALID_INDEX)
+    return 0;
+  return listen_session_get (al->local_index);
+}
+
 static app_worker_map_t *
 app_worker_map_alloc (application_t * app)
 {
@@ -554,9 +583,6 @@
 {
   app_worker_map_t *wrk_map;
   app_worker_t *app_wrk;
-  u32 table_index;
-  local_session_t *ll;
-  session_endpoint_t sep;
 
   /*
    * The app event queue allocated in first segment is cleared with
@@ -581,21 +607,6 @@
   pool_free (app->worker_maps);
 
   /*
-   * Free local listeners. Global table unbinds stop local listeners
-   * as well, but if we have only local binds, these won't be cleaned up.
-   * Don't bother with local accepted sessions, we clean them when
-   * cleaning up the worker.
-   */
-  table_index = application_local_session_table (app);
-  /* *INDENT-OFF* */
-  pool_foreach (ll, app->local_listen_sessions, ({
-    application_local_listener_session_endpoint (ll, &sep);
-    session_lookup_del_session_endpoint (table_index, &sep);
-  }));
-  /* *INDENT-ON* */
-  pool_free (app->local_listen_sessions);
-
-  /*
    * Cleanup remaining state
    */
   if (application_is_builtin (app))
@@ -1016,12 +1027,11 @@
 {
   app_worker_t *server_wrk, *client_wrk;
   application_t *client;
-  local_session_t *ll;
   app_listener_t *al;
   u32 table_index;
   session_t *ls;
   u8 fib_proto;
-  u64 lh;
+  session_handle_t lh;
 
   if (session_endpoint_is_zero (&a->sep))
     return VNET_API_ERROR_INVALID_VALUE;
@@ -1045,8 +1055,8 @@
       if (lh == SESSION_INVALID_HANDLE)
 	goto global_scope;
 
-      ll = application_get_local_listener_w_handle (lh);
-      al = app_listener_get_w_session ((session_t *) ll);
+      ls = listen_session_get_from_handle (lh);
+      al = app_listener_get_w_session (ls);
 
       /*
        * Break loop if rule in local table points to connecting app. This
@@ -1057,7 +1067,7 @@
 	goto global_scope;
 
       server_wrk = app_listener_select_worker (al);
-      return app_worker_local_session_connect (client_wrk, server_wrk, ll,
+      return app_worker_local_session_connect (client_wrk, server_wrk, ls,
 					       a->api_context);
     }
 
@@ -1080,8 +1090,7 @@
     {
       al = app_listener_get_w_session (ls);
       server_wrk = app_listener_select_worker (al);
-      ll = (local_session_t *) ls;
-      return app_worker_local_session_connect (client_wrk, server_wrk, ll,
+      return app_worker_local_session_connect (client_wrk, server_wrk, ls,
 					       a->api_context);
     }
 
@@ -1471,12 +1480,8 @@
 static void
 application_format_local_sessions (application_t * app, int verbose)
 {
-  vlib_main_t *vm = vlib_get_main ();
   app_worker_map_t *wrk_map;
   app_worker_t *app_wrk;
-  transport_proto_t tp;
-  local_session_t *ls;
-  u8 *conn = 0;
 
   if (!app)
     {
@@ -1485,20 +1490,6 @@
     }
 
   /*
-   * Format local listeners
-   */
-
-  /* *INDENT-OFF* */
-  pool_foreach (ls, app->local_listen_sessions, ({
-    tp = session_type_transport_proto (ls->listener_session_type);
-    conn = format (0, "[L][%U] *:%u", format_transport_proto_short, tp,
-                   ls->port);
-    vlib_cli_output (vm, "%-40v%-15u%-20s", conn, ls->app_wrk_index, "*");
-    vec_reset_length (conn);
-  }));
-  /* *INDENT-ON* */
-
-  /*
    * Format local accepted/connected sessions
    */
   /* *INDENT-OFF* */
diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h
index 209fb25..0590c54 100644
--- a/src/vnet/session/application.h
+++ b/src/vnet/session/application.h
@@ -121,12 +121,6 @@
   /** Pool of listeners for the app */
   app_listener_t *listeners;
 
-  /** Pool of local listeners for app */
-  app_listener_t *local_listeners;
-
-  /** Pool of local listen sessions */
-  local_session_t *local_listen_sessions;
-
   /*
    * TLS Specific
    */
@@ -194,6 +188,7 @@
 app_listener_t *app_listener_get_w_session (session_t * ls);
 app_worker_t *app_listener_select_worker (app_listener_t * al);
 session_t *app_listener_get_session (app_listener_t * al);
+session_t *app_listener_get_local_session (app_listener_t * al);
 
 application_t *application_get (u32 index);
 application_t *application_get_if_valid (u32 index);
diff --git a/src/vnet/session/application_local.c b/src/vnet/session/application_local.c
index 54e53e5..9e874ce 100644
--- a/src/vnet/session/application_local.c
+++ b/src/vnet/session/application_local.c
@@ -16,35 +16,55 @@
 #include <vnet/session/application_local.h>
 #include <vnet/session/session.h>
 
-local_session_t *
-application_local_listen_session_alloc (application_t * app)
+typedef struct ct_connection_
 {
-  local_session_t *ll;
-  pool_get_zero (app->local_listen_sessions, ll);
-  ll->session_index = ll - app->local_listen_sessions;
-  ll->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, 0);
-  ll->app_index = app->app_index;
-  ll->session_state = SESSION_STATE_LISTENING;
-  return ll;
+  transport_connection_t connection;
+  u32 client_wrk;
+  u32 server_wrk;
+  u32 transport_listener_index;
+  transport_proto_t actual_tp;
+} ct_connection_t;
+
+ct_connection_t *connections;
+
+ct_connection_t *
+ct_connection_alloc (void)
+{
+  ct_connection_t *ct;
+
+  pool_get_zero (connections, ct);
+  ct->c_c_index = ct - connections;
+  ct->c_thread_index = 0;
+  ct->client_wrk = ~0;
+  ct->server_wrk = ~0;
+  return ct;
+}
+
+ct_connection_t *
+ct_connection_get (u32 ct_index)
+{
+  if (pool_is_free_index (connections, ct_index))
+    return 0;
+  return pool_elt_at_index (connections, ct_index);
 }
 
 void
-application_local_listen_session_free (application_t * app,
-				       local_session_t * ll)
+ct_connection_free (ct_connection_t * ct)
 {
-  pool_put (app->local_listen_sessions, ll);
   if (CLIB_DEBUG)
-    clib_memset (ll, 0xfb, sizeof (*ll));
+    memset (ct, 0xfc, sizeof (*ct));
+  pool_put (connections, ct);
 }
 
 void
-application_local_listener_session_endpoint (local_session_t * ll,
+application_local_listener_session_endpoint (session_t * ll,
 					     session_endpoint_t * sep)
 {
-  sep->transport_proto =
-    session_type_transport_proto (ll->listener_session_type);
-  sep->port = ll->port;
-  sep->is_ip4 = ll->listener_session_type & 1;
+  ct_connection_t *ct;
+  ct = (ct_connection_t *) session_get_transport (ll);
+  sep->transport_proto = ct->actual_tp;
+  sep->port = ct->c_lcl_port;
+  sep->is_ip4 = ct->c_is_ip4;
 }
 
 local_session_t *
@@ -261,7 +281,7 @@
 int
 app_worker_local_session_connect (app_worker_t * client_wrk,
 				  app_worker_t * server_wrk,
-				  local_session_t * ll, u32 opaque)
+				  session_t * ll, u32 opaque)
 {
   u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
   u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index;
@@ -286,11 +306,13 @@
   round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
   seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
 
-  has_transport = session_has_transport ((session_t *) ll);
+  has_transport = session_has_transport (ll);
   if (!has_transport)
     {
       /* Local sessions don't have backing transport */
-      ls->port = ll->port;
+      transport_connection_t *tc;
+      tc = session_get_transport (ll);
+      ls->port = tc->lcl_port;
       sm = app_worker_get_local_segment_manager (server_wrk);
     }
   else
@@ -492,6 +514,96 @@
   /* *INDENT-ON* */
 }
 
+u32
+ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
+{
+  session_endpoint_cfg_t *sep;
+  ct_connection_t *ct;
+
+  sep = (session_endpoint_cfg_t *) tep;
+  ct = ct_connection_alloc ();
+  ct->server_wrk = sep->app_wrk_index;
+  ct->c_is_ip4 = sep->is_ip4;
+  clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
+  ct->c_lcl_port = sep->port;
+  ct->actual_tp = sep->transport_proto;
+  return ct->c_c_index;
+}
+
+u32
+ct_stop_listen (u32 ct_index)
+{
+  ct_connection_t *ct;
+  ct = ct_connection_get (ct_index);
+  ct_connection_free (ct);
+  return 0;
+}
+
+transport_connection_t *
+ct_listener_get (u32 ct_index)
+{
+  return (transport_connection_t *) ct_connection_get (ct_index);
+}
+
+static u8 *
+format_ct_connection_id (u8 * s, va_list * args)
+{
+  ct_connection_t *ct = va_arg (*args, ct_connection_t *);
+  if (!ct)
+    return s;
+  if (ct->c_is_ip4)
+    {
+      s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
+		  ct->c_s_index, format_transport_proto_short, ct->actual_tp,
+		  format_ip4_address, &ct->c_lcl_ip4,
+		  clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
+		  &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
+    }
+  else
+    {
+      s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
+		  ct->c_s_index, format_transport_proto_short, ct->actual_tp,
+		  format_ip6_address, &ct->c_lcl_ip6,
+		  clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
+		  &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
+    }
+
+  return s;
+}
+
+u8 *
+format_ct_listener (u8 * s, va_list * args)
+{
+  u32 tc_index = va_arg (*args, u32);
+  u32 __clib_unused verbose = va_arg (*args, u32);
+  ct_connection_t *ct = ct_connection_get (tc_index);
+  s = format (s, "%-50U", format_ct_connection_id, ct);
+  if (verbose)
+    s = format (s, "%-15s", "LISTEN");
+  return s;
+}
+
+/* *INDENT-OFF* */
+const static transport_proto_vft_t cut_thru_proto = {
+  .start_listen = ct_start_listen,
+  .stop_listen = ct_stop_listen,
+  .get_listener = ct_listener_get,
+  .tx_type = TRANSPORT_TX_INTERNAL,
+  .service_type = TRANSPORT_SERVICE_APP,
+  .format_listener = format_ct_listener,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+ct_transport_init (vlib_main_t * vm)
+{
+  transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
+			       FIB_PROTOCOL_IP4, ~0);
+  return 0;
+}
+
+VLIB_INIT_FUNCTION (ct_transport_init);
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vnet/session/application_local.h b/src/vnet/session/application_local.h
index 70bb23e..d54b5eb 100644
--- a/src/vnet/session/application_local.h
+++ b/src/vnet/session/application_local.h
@@ -20,9 +20,7 @@
 #include <vnet/session/application.h>
 
 local_session_t *application_local_listen_session_alloc (application_t * app);
-void application_local_listen_session_free (application_t * app,
-					    local_session_t * ll);
-void application_local_listener_session_endpoint (local_session_t * ll,
+void application_local_listener_session_endpoint (session_t * ll,
 						  session_endpoint_t * sep);
 
 local_session_t *app_worker_local_session_alloc (app_worker_t * app_wrk);
@@ -41,7 +39,7 @@
 int app_worker_local_session_connect_notify (local_session_t * ls);
 int app_worker_local_session_connect (app_worker_t * client,
 				      app_worker_t * server,
-				      local_session_t * ls, u32 opaque);
+				      session_t * ls, u32 opaque);
 int app_worker_local_session_disconnect (u32 app_or_wrk,
 					 local_session_t * ls);
 int app_worker_local_session_disconnect_w_index (u32 app_or_wrk,
@@ -53,22 +51,6 @@
 void mq_send_local_session_disconnected_cb (u32 app_or_wrk,
 					    local_session_t * ls);
 
-always_inline local_session_t *
-application_get_local_listen_session (application_t * app, u32 session_index)
-{
-  return pool_elt_at_index (app->local_listen_sessions, session_index);
-}
-
-always_inline local_session_t *
-application_get_local_listener_w_handle (session_handle_t handle)
-{
-  u32 server_index, session_index;
-  application_t *app;
-  local_session_parse_handle (handle, &server_index, &session_index);
-  app = application_get (server_index);
-  return application_get_local_listen_session (app, session_index);
-}
-
 always_inline u8
 application_local_session_listener_has_transport (local_session_t * ls)
 {
diff --git a/src/vnet/session/application_worker.c b/src/vnet/session/application_worker.c
index 43e3db3..51ebe1c 100644
--- a/src/vnet/session/application_worker.c
+++ b/src/vnet/session/application_worker.c
@@ -177,22 +177,9 @@
 }
 
 int
-app_worker_start_listen (app_worker_t * app_wrk,
-			 app_listener_t * app_listener)
+app_worker_init_listener (app_worker_t * app_wrk, session_t * ls)
 {
   segment_manager_t *sm;
-  session_t *ls;
-
-  if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
-    return VNET_API_ERROR_ADDRESS_IN_USE;
-
-  app_listener->workers = clib_bitmap_set (app_listener->workers,
-					   app_wrk->wrk_map_index, 1);
-
-  if (app_listener->session_index == SESSION_INVALID_INDEX)
-    return 0;
-
-  ls = session_get (app_listener->session_index, 0);
 
   /* Allocate segment manager. All sessions derived out of a listen session
    * have fifos allocated by the same segment manager. */
@@ -212,19 +199,47 @@
 }
 
 int
+app_worker_start_listen (app_worker_t * app_wrk,
+			 app_listener_t * app_listener)
+{
+  session_t *ls;
+
+  if (clib_bitmap_get (app_listener->workers, app_wrk->wrk_map_index))
+    return VNET_API_ERROR_ADDRESS_IN_USE;
+
+  app_listener->workers = clib_bitmap_set (app_listener->workers,
+					   app_wrk->wrk_map_index, 1);
+
+  if (app_listener->session_index != SESSION_INVALID_INDEX)
+    {
+      ls = session_get (app_listener->session_index, 0);
+      if (app_worker_init_listener (app_wrk, ls))
+	return -1;
+    }
+
+  if (app_listener->local_index != SESSION_INVALID_INDEX)
+    {
+      ls = session_get (app_listener->local_index, 0);
+      if (app_worker_init_listener (app_wrk, ls))
+	return -1;
+    }
+
+  return 0;
+}
+
+int
 app_worker_stop_listen (app_worker_t * app_wrk, app_listener_t * al)
 {
   session_handle_t handle;
   segment_manager_t *sm;
   uword *sm_indexp;
+  session_t *ls;
 
   if (!clib_bitmap_get (al->workers, app_wrk->wrk_map_index))
     return 0;
 
   if (al->session_index != SESSION_INVALID_INDEX)
     {
-      session_t *ls;
-
       ls = listen_session_get (al->session_index);
       handle = listen_session_get_handle (ls);
 
@@ -251,18 +266,33 @@
 
   if (al->local_index != SESSION_INVALID_INDEX)
     {
-      local_session_t *ll, *ls;
-      application_t *app;
-
-      app = application_get (app_wrk->app_index);
-      ll = application_get_local_listen_session (app, al->local_index);
+      local_session_t *local;
+      ls = listen_session_get (al->local_index);
+      handle = listen_session_get_handle (ls);
 
       /* *INDENT-OFF* */
-      pool_foreach (ls, app_wrk->local_sessions, ({
-        if (ls->listener_index == ll->session_index)
-          app_worker_local_session_disconnect (app_wrk->wrk_index, ls);
+      pool_foreach (local, app_wrk->local_sessions, ({
+        if (local->listener_index == ls->session_index)
+          app_worker_local_session_disconnect (app_wrk->wrk_index, local);
       }));
       /* *INDENT-ON* */
+
+      sm_indexp = hash_get (app_wrk->listeners_table, handle);
+      if (PREDICT_FALSE (!sm_indexp))
+	return -1;
+
+      sm = segment_manager_get (*sm_indexp);
+      if (app_wrk->first_segment_manager == *sm_indexp)
+	{
+	  /* Delete sessions but don't remove segment manager */
+	  app_wrk->first_segment_manager_in_use = 0;
+	  segment_manager_del_sessions (sm);
+	}
+      else
+	{
+	  segment_manager_init_del (sm);
+	}
+      hash_unset (app_wrk->listeners_table, handle);
     }
 
   clib_bitmap_set_no_check (al->workers, app_wrk->wrk_map_index, 0);
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
index 83c889c..110db2c 100644
--- a/src/vnet/session/session.c
+++ b/src/vnet/session/session.c
@@ -1030,25 +1030,23 @@
 int
 session_listen (session_t * ls, session_endpoint_cfg_t * sep)
 {
-  transport_connection_t *tc;
   transport_endpoint_t *tep;
   u32 tc_index, s_index;
 
   /* Transport bind/listen */
   tep = session_endpoint_to_transport (sep);
   s_index = ls->session_index;
-  tc_index = transport_start_listen (sep->transport_proto, s_index, tep);
+  tc_index = transport_start_listen (session_get_transport_proto (ls),
+				     s_index, tep);
 
   if (tc_index == (u32) ~ 0)
     return -1;
 
-  /* Attach transport to session */
+  /* Attach transport to session. Lookup tables are populated by the app
+   * worker because local tables (for ct sessions) are not backed by a fib */
   ls = listen_session_get (s_index);
   ls->connection_index = tc_index;
 
-  /* Add to the main lookup table after transport was initialized */
-  tc = transport_get_listener (sep->transport_proto, tc_index);
-  session_lookup_add_connection (tc, listen_session_get_handle (ls));
   return 0;
 }
 
diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c
index f96274a..cf5a5cd 100755
--- a/src/vnet/session/session_api.c
+++ b/src/vnet/session/session_api.c
@@ -253,22 +253,10 @@
   else
     {
       local_session_t *ls = (local_session_t *) s;
-      local_session_t *ll;
-      if (application_local_session_listener_has_transport (ls))
-	{
-	  listener = listen_session_get (ls->listener_index);
-	  al = app_listener_get (server, listener->al_index);
-	  mp->listener_handle = app_listener_handle (al);
-	  mp->is_ip4 = session_type_is_ip4 (listener->session_type);
-	}
-      else
-	{
-	  ll = application_get_local_listen_session (server,
-						     ls->listener_index);
-	  al = app_listener_get (server, ll->al_index);
-	  mp->listener_handle = app_listener_handle (al);
-	  mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
-	}
+      listener = listen_session_get (ls->listener_index);
+      al = app_listener_get (server, listener->al_index);
+      mp->listener_handle = app_listener_handle (al);
+      mp->is_ip4 = session_type_is_ip4 (listener->session_type);
       mp->handle = application_local_session_handle (ls);
       mp->port = ls->port;
       mp->vpp_event_queue_address = ls->client_evt_q;
@@ -464,7 +452,6 @@
   else
     {
       local_session_t *ls = (local_session_t *) s;
-      local_session_t *ll;
       u8 main_thread = vlib_num_workers ()? 1 : 0;
 
       send_app_cut_through_registration_add (app_wrk->api_client_index,
@@ -472,20 +459,10 @@
 					     ls->server_evt_q,
 					     ls->client_evt_q);
 
-      if (application_local_session_listener_has_transport (ls))
-	{
-	  listener = listen_session_get (ls->listener_index);
-	  al = app_listener_get (app, listener->al_index);
-	  mp->listener_handle = app_listener_handle (al);
-	  mp->is_ip4 = session_type_is_ip4 (listener->session_type);
-	}
-      else
-	{
-	  ll = application_get_local_listen_session (app, ls->listener_index);
-	  al = app_listener_get (app, ll->al_index);
-	  mp->listener_handle = app_listener_handle (al);
-	  mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
-	}
+      listener = listen_session_get (ls->listener_index);
+      al = app_listener_get (app, listener->al_index);
+      mp->listener_handle = app_listener_handle (al);
+      mp->is_ip4 = session_type_is_ip4 (listener->session_type);
       mp->handle = application_local_session_handle (ls);
       mp->port = ls->port;
       vpp_queue = session_manager_get_vpp_event_queue (main_thread);
@@ -671,12 +648,10 @@
   session_bound_msg_t *mp;
   app_worker_t *app_wrk;
   session_event_t *evt;
-  application_t *app;
   app_listener_t *al;
   session_t *ls = 0;
 
   app_wrk = app_worker_get (app_wrk_index);
-  app = application_get (app_wrk->app_index);
   app_mq = app_wrk->event_queue;
   if (!app_mq)
     {
@@ -698,24 +673,15 @@
     goto done;
 
   mp->handle = handle;
-  if (application_has_global_scope (app))
-    {
-      al = app_listener_get_w_handle (handle);
-      ls = app_listener_get_session (al);
-      tc = listen_session_get_transport (ls);
-      mp->lcl_port = tc->lcl_port;
-      mp->lcl_is_ip4 = tc->is_ip4;
-      clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
-    }
+  al = app_listener_get_w_handle (handle);
+  if (al->session_index != SESSION_INVALID_INDEX)
+    ls = app_listener_get_session (al);
   else
-    {
-      local_session_t *local;
-      app_listener_t *al;
-      al = app_listener_get_w_handle (handle);
-      local = application_get_local_listen_session (app, al->local_index);
-      mp->lcl_port = local->port;
-      mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
-    }
+    ls = app_listener_get_local_session (al);
+  tc = listen_session_get_transport (ls);
+  mp->lcl_port = tc->lcl_port;
+  mp->lcl_is_ip4 = tc->is_ip4;
+  clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
 
   vpp_evt_q = session_manager_get_vpp_event_queue (0);
   mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);