+/**
+ * Lookup session with ip6 and transport layer information
+ *
+ * Lookup logic is identical to that of @ref session_lookup_connection_wt6 but
+ * this returns a session as opposed to a transport connection;
+ */
+stream_session_t *
+session_lookup6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
+ u16 lcl_port, u16 rmt_port, u8 proto)
+{
+ session_table_t *st;
+ session_kv6_t kv6;
+ stream_session_t *s;
+ int rv;
+
+ st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
+ if (PREDICT_FALSE (!st))
+ return 0;
+
+ make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
+ rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
+ if (rv == 0)
+ return session_get_from_handle (kv6.value);
+
+ /* If nothing is found, check if any listener is available */
+ if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
+ return s;
+
+ /* Finally, try half-open connections */
+ rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
+ if (rv == 0)
+ return session_get_from_handle (kv6.value);
+ return 0;
+}
+
+u64
+session_lookup_local_listener_make_handle (session_endpoint_t * sep)
+{
+ return ((u64) SESSION_LOCAL_TABLE_PREFIX << 32
+ | (u32) sep->port << 16 | (u32) sep->transport_proto << 8
+ | (u32) sep->is_ip4);
+}
+
+u8
+session_lookup_local_is_handle (u64 handle)
+{
+ if (handle >> 32 == SESSION_LOCAL_TABLE_PREFIX)
+ return 1;
+ return 0;
+}
+
+int
+session_lookup_local_listener_parse_handle (u64 handle,
+ session_endpoint_t * sep)
+{
+ u32 local_table_handle;
+ if (handle >> 32 != SESSION_LOCAL_TABLE_PREFIX)
+ return -1;
+ local_table_handle = handle & 0xFFFFFFFFULL;
+ sep->is_ip4 = local_table_handle & 0xff;
+ local_table_handle >>= 8;
+ sep->transport_proto = local_table_handle & 0xff;
+ sep->port = local_table_handle >> 8;
+ return 0;
+}
+
+u8 *
+format_ip4_session_lookup_kvp (u8 * s, va_list * args)
+{
+ clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
+ u32 is_local = va_arg (*args, u32);
+ u8 *app_name, *str = 0;
+ stream_session_t *session;
+ v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
+
+ char *proto = key->proto == TRANSPORT_PROTO_TCP ? "T" : "U";
+ if (!is_local)
+ {
+ session = session_get_from_handle (kvp->value);
+ app_name = application_name_from_index (session->app_index);
+ str = format (0, "[%s] %U:%d->%U:%d", proto, format_ip4_address,
+ &key->src, clib_net_to_host_u16 (key->src_port),
+ format_ip4_address, &key->dst,
+ clib_net_to_host_u16 (key->dst_port));
+ s = format (s, "%-40v%-30v", str, app_name);
+ }
+ else
+ {
+ app_name = application_name_from_index (kvp->value);
+ str = format (0, "[%s] %U:%d", proto, format_ip4_address,
+ &key->src, clib_net_to_host_u16 (key->src_port));
+ s = format (s, "%-30v%-30v", str, app_name);
+ }
+ vec_free (app_name);
+ return s;
+}
+
+typedef struct _ip4_session_table_show_ctx_t
+{
+ vlib_main_t *vm;
+ u8 is_local;
+} ip4_session_table_show_ctx_t;
+
+static int
+ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
+{
+ ip4_session_table_show_ctx_t *ctx = arg;
+ vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
+ ctx->is_local);
+ return 1;
+}
+
+void
+session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
+ u8 type, u8 is_local)
+{
+ ip4_session_table_show_ctx_t ctx = {
+ .vm = vm,
+ .is_local = is_local,
+ };
+ if (!is_local)
+ vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
+ else
+ vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
+ switch (type)
+ {
+ /* main table v4 */
+ case 0:
+ ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
+ &ctx);
+ break;
+ default:
+ clib_warning ("not supported");
+ }
+}
+