session: use session handles as app listener handles

Type: refactor

Change-Id: I421750147a8a821bd0b522daf6c2b7239e551f12
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
index 3095787..0b8c8d4 100644
--- a/src/vnet/session/application.c
+++ b/src/vnet/session/application.c
@@ -38,6 +38,7 @@
   app_listener->app_index = app->app_index;
   app_listener->session_index = SESSION_INVALID_INDEX;
   app_listener->local_index = SESSION_INVALID_INDEX;
+  app_listener->ls_handle = SESSION_INVALID_HANDLE;
   return app_listener;
 }
 
@@ -47,14 +48,6 @@
   return pool_elt_at_index (app->listeners, app_listener_index);
 }
 
-static app_listener_t *
-app_listener_get_if_valid (application_t * app, u32 app_listener_index)
-{
-  if (pool_is_free_index (app->listeners, app_listener_index))
-    return 0;
-  return pool_elt_at_index (app->listeners, app_listener_index);
-}
-
 static void
 app_listener_free (application_t * app, app_listener_t * app_listener)
 {
@@ -64,45 +57,10 @@
     clib_memset (app_listener, 0xfa, sizeof (*app_listener));
 }
 
-static u32
-app_listener_id (app_listener_t * al)
-{
-  ASSERT (al->app_index < 1 << 16 && al->al_index < 1 << 16);
-  return (al->app_index << 16 | al->al_index);
-}
-
 session_handle_t
 app_listener_handle (app_listener_t * al)
 {
-  return ((u64) SESSION_LISTENER_PREFIX << 32 | (u64) app_listener_id (al));
-}
-
-static void
-app_listener_id_parse (u32 listener_id, u32 * app_index,
-		       u32 * app_listener_index)
-{
-  *app_index = listener_id >> 16;
-  *app_listener_index = listener_id & 0xFFFF;
-}
-
-void
-app_listener_handle_parse (session_handle_t handle, u32 * app_index,
-			   u32 * app_listener_index)
-{
-  app_listener_id_parse (handle & 0xFFFFFFFF, app_index, app_listener_index);
-}
-
-static app_listener_t *
-app_listener_get_w_id (u32 listener_id)
-{
-  u32 app_index, app_listener_index;
-  application_t *app;
-
-  app_listener_id_parse (listener_id, &app_index, &app_listener_index);
-  app = application_get_if_valid (app_index);
-  if (!app)
-    return 0;
-  return app_listener_get_if_valid (app, app_listener_index);
+  return al->ls_handle;
 }
 
 app_listener_t *
@@ -116,14 +74,24 @@
   return app_listener_get (app, ls->al_index);
 }
 
+session_handle_t
+app_listen_session_handle (session_t * ls)
+{
+  app_listener_t *al;
+  al = app_listener_get_w_session (ls);
+  if (!al)
+    return listen_session_get_handle (ls);
+  return al->ls_handle;
+}
+
 app_listener_t *
 app_listener_get_w_handle (session_handle_t handle)
 {
-
-  if (handle >> 32 != SESSION_LISTENER_PREFIX)
+  session_t *ls;
+  ls = session_get_from_handle_if_valid (handle);
+  if (!ls)
     return 0;
-
-  return app_listener_get_w_id (handle & 0xFFFFFFFF);
+  return app_listener_get_w_session (ls);
 }
 
 app_listener_t *
@@ -202,6 +170,7 @@
       ls = session_get_from_handle (lh);
       app_listener = app_listener_get (app, al_index);
       app_listener->local_index = ls->session_index;
+      app_listener->ls_handle = lh;
       ls->al_index = al_index;
 
       table_index = application_local_session_table (app);
@@ -234,6 +203,7 @@
       ls = listen_session_get_from_handle (lh);
       app_listener = app_listener_get (app, al_index);
       app_listener->session_index = ls->session_index;
+      app_listener->ls_handle = lh;
       ls->al_index = al_index;
 
       /* Add to the global lookup table after transport was initialized.
diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h
index cb83f65..a17270c 100644
--- a/src/vnet/session/application.h
+++ b/src/vnet/session/application.h
@@ -74,10 +74,13 @@
 {
   clib_bitmap_t *workers;	/**< workers accepting connections */
   u32 accept_rotor;		/**< last worker to accept a connection */
-  u32 al_index;
-  u32 app_index;
-  u32 local_index;
-  u32 session_index;
+  u32 al_index;			/**< app listener index in app pool */
+  u32 app_index;		/**< owning app index */
+  u32 local_index;		/**< local listening session index */
+  u32 session_index;		/**< global listening session index */
+  session_handle_t ls_handle;	/**< session handle of the local or global
+				     listening session that also identifies
+				     the app listener */
 } app_listener_t;
 
 typedef struct application_
@@ -173,6 +176,28 @@
 session_handle_t app_listener_handle (app_listener_t * app_listener);
 app_listener_t *app_listener_lookup (application_t * app,
 				     session_endpoint_cfg_t * sep);
+
+/**
+ * Get app listener handle for listening session
+ *
+ * For a given listening session, this can return either the session
+ * handle of the app listener associated to the listening session or,
+ * if no such app listener exists, the session's handle
+ *
+ * @param ls		listening session
+ * @return		app listener or listening session handle
+ */
+session_handle_t app_listen_session_handle (session_t * ls);
+/**
+ * Get app listener for listener session handle
+ *
+ * Should only be called on handles that have an app listener, i.e.,
+ * were obtained at the end of a @ref vnet_listen call.
+ *
+ * @param handle	handle of the app listener. This is the handle of
+ * 			either the global or local listener
+ * @return		pointer to app listener or 0
+ */
 app_listener_t *app_listener_get_w_handle (session_handle_t handle);
 app_listener_t *app_listener_get_w_session (session_t * ls);
 session_t *app_listener_get_session (app_listener_t * al);
diff --git a/src/vnet/session/application_worker.c b/src/vnet/session/application_worker.c
index a30191c..6f137e9 100644
--- a/src/vnet/session/application_worker.c
+++ b/src/vnet/session/application_worker.c
@@ -58,10 +58,9 @@
   vnet_unlisten_args_t _a, *a = &_a;
   u64 handle, *handles = 0;
   segment_manager_t *sm;
+  session_t *ls;
   u32 sm_index;
   int i;
-  app_listener_t *al;
-  session_t *ls;
 
   /*
    *  Listener cleanup
@@ -70,8 +69,7 @@
   /* *INDENT-OFF* */
   hash_foreach (handle, sm_index, app_wrk->listeners_table, ({
     ls = listen_session_get_from_handle (handle);
-    al = app_listener_get (app, ls->al_index);
-    vec_add1 (handles, app_listener_handle (al));
+    vec_add1 (handles, app_listen_session_handle (ls));
     sm = segment_manager_get (sm_index);
     sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
   }));
diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c
index ed71667..da62c85 100755
--- a/src/vnet/session/session_api.c
+++ b/src/vnet/session/session_api.c
@@ -178,7 +178,6 @@
   session_accepted_msg_t *mp;
   session_event_t *evt;
   application_t *app;
-  app_listener_t *al;
 
   app = application_get (app_wrk->app_index);
   app_mq = app_wrk->event_queue;
@@ -198,8 +197,7 @@
   if (session_has_transport (s))
     {
       listener = listen_session_get (s->listener_index);
-      al = app_listener_get (app, listener->al_index);
-      mp->listener_handle = app_listener_handle (al);
+      mp->listener_handle = app_listen_session_handle (listener);
       if (application_is_proxy (app))
 	{
 	  listener =
@@ -220,8 +218,7 @@
 
       ct = (ct_connection_t *) session_get_transport (s);
       listener = listen_session_get (s->listener_index);
-      al = app_listener_get (app, listener->al_index);
-      mp->listener_handle = app_listener_handle (al);
+      mp->listener_handle = app_listen_session_handle (listener);
       mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
       mp->rmt.port = ct->c_rmt_port;
       mp->handle = session_handle (s);
diff --git a/src/vnet/session/session_types.h b/src/vnet/session/session_types.h
index b392439..4a8e97e 100644
--- a/src/vnet/session/session_types.h
+++ b/src/vnet/session/session_types.h
@@ -19,8 +19,6 @@
 #include <svm/svm_fifo.h>
 #include <vnet/session/transport_types.h>
 
-#define SESSION_LISTENER_PREFIX		0x5FFFFFFF
-
 #define foreach_session_endpoint_fields				\
   foreach_transport_endpoint_cfg_fields				\
   _(u8, transport_proto)					\