session: improve show session cli

Change-Id: If30cea80058752cedcc6ecc38e90bef8732c83d3
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/session_cli.c b/src/vnet/session/session_cli.c
index 9da7f5b..99baf44 100755
--- a/src/vnet/session/session_cli.c
+++ b/src/vnet/session/session_cli.c
@@ -61,10 +61,22 @@
   u32 tp = session_get_transport_proto (ss);
   u8 *str = 0;
 
-  if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
-    str = format (0, "%-10u%-10u",
-		  svm_fifo_max_dequeue (ss->server_rx_fifo),
-		  svm_fifo_max_dequeue (ss->server_tx_fifo));
+  if (ss->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
+    {
+      s = format (s, "[%u:%u] CLOSED", ss->thread_index, ss->session_index);
+      return s;
+    }
+
+  if (verbose == 1)
+    {
+      u8 post_accept = ss->session_state >= SESSION_STATE_ACCEPTING;
+      u8 hasf = post_accept | session_tx_is_dgram (ss);
+      u32 rxf, txf;
+
+      rxf = hasf ? svm_fifo_max_dequeue (ss->server_rx_fifo) : 0;
+      txf = hasf ? svm_fifo_max_dequeue (ss->server_tx_fifo) : 0;
+      str = format (0, "%-10u%-10u", rxf, txf);
+    }
 
   if (ss->session_state >= SESSION_STATE_ACCEPTING)
     {
@@ -77,8 +89,8 @@
     }
   else if (ss->session_state == SESSION_STATE_LISTENING)
     {
-      s = format (s, "%-40U%v", format_transport_listen_connection,
-		  tp, ss->connection_index, str);
+      s = format (s, "%U%v", format_transport_listen_connection,
+		  tp, ss->connection_index, verbose, str);
       if (verbose > 1)
 	s = format (s, "\n%U", format_session_fifos, ss, verbose);
     }
@@ -219,7 +231,7 @@
 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 			 vlib_cli_command_t * cmd)
 {
-  u8 *str = 0, one_session = 0, do_listeners = 0, sst, do_elog = 0;
+  u8 one_session = 0, do_listeners = 0, sst, do_elog = 0;
   session_manager_main_t *smm = &session_manager_main;
   u32 transport_proto = ~0, track_index;
   stream_session_t *pool, *s;
@@ -255,7 +267,7 @@
 
   if (one_session)
     {
-      str = format (0, "%U", format_stream_session, s, 3);
+      u8 *str = format (0, "%U", format_stream_session, s, 3);
       if (do_elog && s->session_state != SESSION_STATE_LISTENING)
 	{
 	  elog_main_t *em = &vm->elog_main;
@@ -270,13 +282,14 @@
 			  dt, track_index);
 	}
       vlib_cli_output (vm, "%v", str);
+      vec_free (str);
       return 0;
     }
 
   if (do_listeners)
     {
       sst = session_type_from_proto_and_ip (transport_proto, 1);
-      vlib_cli_output (vm, "%-40s%-24s", "Listener", "App");
+      vlib_cli_output (vm, "%-50s%-24s", "Listener", "App");
       /* *INDENT-OFF* */
       pool_foreach (s, smm->wrk[0].sessions, ({
 	if (s->session_state != SESSION_STATE_LISTENING
@@ -284,7 +297,7 @@
 	  continue;
 	app_wrk = app_worker_get (s->app_wrk_index);
 	app_name = application_name_from_index (app_wrk->app_index);
-	vlib_cli_output (vm, "%U%-25v%", format_stream_session, s, 1,
+	vlib_cli_output (vm, "%U%-25v%", format_stream_session, s, 0,
 			 app_name);
       }));
       /* *INDENT-ON* */
@@ -293,42 +306,46 @@
 
   for (i = 0; i < vec_len (smm->wrk); i++)
     {
-      u32 once_per_pool;
+      u32 once_per_pool = 1, n_closed = 0;
+
       pool = smm->wrk[i].sessions;
-
-      once_per_pool = 1;
-
-      if (pool_elts (pool))
+      if (!pool_elts (pool))
 	{
-
-	  vlib_cli_output (vm, "Thread %d: %d active sessions",
-			   i, pool_elts (pool));
-	  if (verbose)
-	    {
-	      if (once_per_pool && verbose == 1)
-		{
-		  str = format (str, "%-50s%-15s%-10s%-10s",
-				"Connection", "State", "Rx-f", "Tx-f");
-		  vlib_cli_output (vm, "%v", str);
-		  vec_reset_length (str);
-		  once_per_pool = 0;
-		}
-
-              /* *INDENT-OFF* */
-              pool_foreach (s, pool,
-              ({
-        	vec_reset_length (str);
-                str = format (str, "%U", format_stream_session, s, verbose);
-                vlib_cli_output (vm, "%v", str);
-              }));
-              /* *INDENT-ON* */
-	    }
+	  vlib_cli_output (vm, "Thread %d: no sessions", i);
+	  continue;
 	}
+
+      if (!verbose)
+	{
+	  vlib_cli_output (vm, "Thread %d: %d sessions", i, pool_elts (pool));
+	  continue;
+	}
+
+      if (once_per_pool && verbose == 1)
+	{
+	  vlib_cli_output (vm, "%s%-50s%-15s%-10s%-10s", i ? "\n" : "",
+			   "Connection", "State", "Rx-f", "Tx-f");
+	  once_per_pool = 0;
+	}
+
+      /* *INDENT-OFF* */
+      pool_foreach (s, pool, ({
+        if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
+          {
+            n_closed += 1;
+            continue;
+          }
+        vlib_cli_output (vm, "%U", format_stream_session, s, verbose);
+      }));
+      /* *INDENT-ON* */
+
+      if (!n_closed)
+	vlib_cli_output (vm, "Thread %d: active sessions %u", i,
+			 pool_elts (pool) - n_closed);
       else
-	vlib_cli_output (vm, "Thread %d: no active sessions", i);
-      vec_reset_length (str);
+	vlib_cli_output (vm, "Thread %d: active sessions %u closed %u", i,
+			 pool_elts (pool) - n_closed, n_closed);
     }
-  vec_free (str);
 
   return 0;
 }
@@ -337,7 +354,8 @@
 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
 {
   .path = "show session",
-  .short_help = "show session [verbose [nnn]]",
+  .short_help = "show session [verbose [n]] [listeners <proto>] "
+		"[<session-id> [elog]]",
   .function = show_session_command_fn,
 };
 /* *INDENT-ON* */
diff --git a/src/vnet/session/transport.c b/src/vnet/session/transport.c
index c2d21bf..0d4ccb5 100644
--- a/src/vnet/session/transport.c
+++ b/src/vnet/session/transport.c
@@ -125,14 +125,13 @@
 format_transport_listen_connection (u8 * s, va_list * args)
 {
   u32 transport_proto = va_arg (*args, u32);
-  u32 listen_index = va_arg (*args, u32);
   transport_proto_vft_t *tp_vft;
 
   tp_vft = transport_protocol_get_vft (transport_proto);
   if (!tp_vft)
     return s;
 
-  s = format (s, "%U", tp_vft->format_listener, listen_index);
+  s = (tp_vft->format_listener) (s, args);
   return s;
 }
 
diff --git a/src/vnet/tcp/tcp.c b/src/vnet/tcp/tcp.c
index 037abdc..99b015b 100644
--- a/src/vnet/tcp/tcp.c
+++ b/src/vnet/tcp/tcp.c
@@ -920,8 +920,12 @@
 format_tcp_listener_session (u8 * s, va_list * args)
 {
   u32 tci = va_arg (*args, u32);
+  u32 verbose = va_arg (*args, u32);
   tcp_connection_t *tc = tcp_listener_get (tci);
-  return format (s, "%U", format_tcp_connection_id, tc);
+  s = format (s, "%-50U", format_tcp_connection_id, tc);
+  if (verbose)
+    s = format (s, "%-15U", format_tcp_state, tc->state);
+  return s;
 }
 
 static u8 *