+app_listener_t *
+app_listener_get_w_session (session_t * ls)
+{
+ application_t *app;
+
+ app = application_get_if_valid (ls->app_index);
+ if (!app)
+ return 0;
+ 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)
+{
+ session_t *ls;
+ ls = session_get_from_handle_if_valid (handle);
+ if (!ls)
+ return 0;
+ return app_listener_get_w_session (ls);
+}
+
+app_listener_t *
+app_listener_lookup (application_t * app, session_endpoint_cfg_t * sep_ext)
+{
+ u32 table_index, fib_proto;
+ session_endpoint_t *sep;
+ session_handle_t handle;
+ session_t *ls;
+
+ sep = (session_endpoint_t *) sep_ext;
+ if (application_has_local_scope (app) && session_endpoint_is_local (sep))
+ {
+ table_index = application_local_session_table (app);
+ handle = session_lookup_endpoint_listener (table_index, sep, 1);
+ if (handle != SESSION_INVALID_HANDLE)
+ {
+ ls = listen_session_get_from_handle (handle);
+ return app_listener_get_w_session (ls);
+ }
+ }
+
+ fib_proto = session_endpoint_fib_proto (sep);
+ table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
+ handle = session_lookup_endpoint_listener (table_index, sep, 1);
+ if (handle != SESSION_INVALID_HANDLE)
+ {
+ ls = listen_session_get_from_handle (handle);
+ return app_listener_get_w_session ((session_t *) ls);
+ }
+
+ return 0;
+}
+
+int
+app_listener_alloc_and_init (application_t * app,
+ session_endpoint_cfg_t * sep,
+ app_listener_t ** listener)
+{
+ app_listener_t *app_listener;
+ transport_connection_t *tc;
+ u32 al_index, table_index;
+ session_handle_t lh;
+ session_type_t st;
+ session_t *ls = 0;
+ int rv;
+
+ app_listener = app_listener_alloc (app);
+ al_index = app_listener->al_index;
+ st = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
+
+ /*
+ * 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_local ((session_endpoint_t *) sep))
+ {
+ session_type_t local_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;
+ app_listener->ls_handle = lh;
+ ls->al_index = al_index;
+
+ table_index = application_local_session_table (app);
+ session_lookup_add_session_endpoint (table_index,
+ (session_endpoint_t *) sep, lh);
+ }
+
+ if (application_has_global_scope (app))
+ {
+ /*
+ * Start listening on local endpoint for requested transport and scope.
+ * Creates a stream session with state LISTENING to be used in session
+ * lookups, prior to establishing connection. Requests transport to
+ * build it's own specific listening connection.
+ */
+ ls = listen_session_alloc (0, st);
+ ls->app_index = app->app_index;
+ ls->app_wrk_index = sep->app_wrk_index;
+
+ /* Listen pool can be reallocated if the transport is
+ * recursive (tls) */
+ lh = listen_session_get_handle (ls);
+
+ if ((rv = session_listen (ls, sep)))
+ {
+ ls = listen_session_get_from_handle (lh);
+ session_free (ls);
+ return rv;
+ }
+ 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.
+ * 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);
+ if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
+ {
+ fib_protocol_t fib_proto;
+ fib_proto = session_endpoint_fib_proto ((session_endpoint_t *) sep);
+ table_index = session_lookup_get_index_for_fib (fib_proto,
+ sep->fib_index);
+ ASSERT (table_index != SESSION_TABLE_INVALID_INDEX);
+ session_lookup_add_session_endpoint (table_index,
+ (session_endpoint_t *) sep,
+ lh);
+ }
+ }
+
+ if (!ls)
+ {
+ app_listener_free (app, app_listener);
+ return -1;
+ }
+
+ *listener = app_listener;
+ return 0;
+}
+
+void
+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)
+ {
+ 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;
+ u32 table_index;
+
+ table_index = application_local_session_table (app);
+ ls = listen_session_get (al->local_index);
+ ct_session_endpoint (ls, &sep);
+ session_lookup_del_session_endpoint (table_index, &sep);
+ session_stop_listen (ls);
+ listen_session_free (ls);
+ }
+ app_listener_free (app, al);
+}
+
+static app_worker_t *
+app_listener_select_worker (application_t * app, app_listener_t * al)
+{
+ u32 wrk_index;
+
+ app = application_get (al->app_index);
+ wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
+ if (wrk_index == ~0)
+ wrk_index = clib_bitmap_first_set (al->workers);
+
+ ASSERT (wrk_index != ~0);
+ al->accept_rotor = wrk_index;
+ return application_get_worker (app, wrk_index);
+}
+
+session_t *
+app_listener_get_session (app_listener_t * al)
+{
+ if (al->session_index == SESSION_INVALID_INDEX)
+ return 0;
+
+ 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)
+{
+ app_worker_map_t *map;
+ pool_get (app->worker_maps, map);
+ clib_memset (map, 0, sizeof (*map));
+ return map;
+}
+
+static u32
+app_worker_map_index (application_t * app, app_worker_map_t * map)
+{
+ return (map - app->worker_maps);
+}
+
+static void
+app_worker_map_free (application_t * app, app_worker_map_t * map)
+{
+ pool_put (app->worker_maps, map);
+}
+
+static app_worker_map_t *
+app_worker_map_get (application_t * app, u32 map_index)
+{
+ if (pool_is_free_index (app->worker_maps, map_index))
+ return 0;
+ return pool_elt_at_index (app->worker_maps, map_index);
+}
+
+static const u8 *