VCL: add session namespace support.
[vpp.git] / src / vnet / session / session_cli.c
index e06bc58..95fc48d 100755 (executable)
@@ -19,8 +19,24 @@ u8 *
 format_stream_session_fifos (u8 * s, va_list * args)
 {
   stream_session_t *ss = va_arg (*args, stream_session_t *);
+  int verbose = va_arg (*args, int);
+  session_fifo_event_t _e, *e = &_e;
+  u8 found;
+
   s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1);
+  if (verbose > 2 && ss->server_rx_fifo->has_event)
+    {
+      found = session_node_lookup_fifo_event (ss->server_rx_fifo, e);
+      s = format (s, " session node event: %s\n",
+                 found ? "found" : "not found");
+    }
   s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1);
+  if (verbose > 2 && ss->server_tx_fifo->has_event)
+    {
+      found = session_node_lookup_fifo_event (ss->server_tx_fifo, e);
+      s = format (s, " session node event: %s\n",
+                 found ? "found" : "not found");
+    }
   return s;
 }
 
@@ -39,23 +55,22 @@ format_stream_session (u8 * s, va_list * args)
   int verbose = va_arg (*args, int);
   transport_proto_vft_t *tp_vft;
   u8 *str = 0;
-  tp_vft = session_get_transport_vft (ss->session_type);
+  tp_vft = transport_protocol_get_vft (ss->session_type);
 
-  if (verbose == 1)
+  if (verbose == 1 && ss->session_state >= SESSION_STATE_ACCEPTING)
     str = format (0, "%-10u%-10u%-10lld",
                  svm_fifo_max_dequeue (ss->server_rx_fifo),
                  svm_fifo_max_enqueue (ss->server_tx_fifo),
                  stream_session_get_index (ss));
 
-  if (ss->session_state == SESSION_STATE_READY
-      || ss->session_state == SESSION_STATE_ACCEPTING)
+  if (ss->session_state >= SESSION_STATE_ACCEPTING)
     {
       s = format (s, "%U", tp_vft->format_connection, ss->connection_index,
                  ss->thread_index, verbose);
       if (verbose == 1)
        s = format (s, "%v", str);
       if (verbose > 1)
-       s = format (s, "%U", format_stream_session_fifos, ss);
+       s = format (s, "%U", format_stream_session_fifos, ss, verbose);
     }
   else if (ss->session_state == SESSION_STATE_LISTENING)
     {
@@ -67,26 +82,119 @@ format_stream_session (u8 * s, va_list * args)
       s = format (s, "%-40U%v", tp_vft->format_half_open,
                  ss->connection_index, str);
     }
-  else if (ss->session_state == SESSION_STATE_CLOSED)
-    {
-      s =
-       format (s, "[CL] %U", tp_vft->format_connection, ss->connection_index,
-               ss->thread_index, verbose);
-      if (verbose == 1)
-       s = format (s, "%v", str);
-      if (verbose > 1)
-       s = format (s, "%U", format_stream_session_fifos, ss);
-    }
   else
     {
       clib_warning ("Session in state: %d!", ss->session_state);
     }
-
   vec_free (str);
 
   return s;
 }
 
+uword
+unformat_stream_session_id (unformat_input_t * input, va_list * args)
+{
+  u8 *proto = va_arg (*args, u8 *);
+  ip46_address_t *lcl = va_arg (*args, ip46_address_t *);
+  ip46_address_t *rmt = va_arg (*args, ip46_address_t *);
+  u16 *lcl_port = va_arg (*args, u16 *);
+  u16 *rmt_port = va_arg (*args, u16 *);
+  u8 *is_ip4 = va_arg (*args, u8 *);
+  u8 tuple_is_set = 0;
+
+  memset (lcl, 0, sizeof (*lcl));
+  memset (rmt, 0, sizeof (*rmt));
+
+  if (unformat (input, "tcp"))
+    {
+      *proto = TRANSPORT_PROTO_TCP;
+    }
+  if (unformat (input, "udp"))
+    {
+      *proto = TRANSPORT_PROTO_UDP;
+    }
+  if (unformat (input, "%U:%d->%U:%d", unformat_ip4_address, &lcl->ip4,
+               lcl_port, unformat_ip4_address, &rmt->ip4, rmt_port))
+    {
+      *is_ip4 = 1;
+      tuple_is_set = 1;
+    }
+  else if (unformat (input, "%U:%d->%U:%d", unformat_ip6_address, &lcl->ip6,
+                    lcl_port, unformat_ip6_address, &rmt->ip6, rmt_port))
+    {
+      *is_ip4 = 0;
+      tuple_is_set = 1;
+    }
+
+  return tuple_is_set;
+}
+
+uword
+unformat_stream_session (unformat_input_t * input, va_list * args)
+{
+  stream_session_t **result = va_arg (*args, stream_session_t **);
+  stream_session_t *s;
+  u8 proto = ~0;
+  ip46_address_t lcl, rmt;
+  u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
+  u8 is_ip4 = 0;
+
+  if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
+                &lcl_port, &rmt_port, &is_ip4))
+    return 0;
+
+  if (is_ip4)
+    s = session_lookup_safe4 (fib_index, &lcl.ip4, &rmt.ip4,
+                             clib_host_to_net_u16 (lcl_port),
+                             clib_host_to_net_u16 (rmt_port), proto);
+  else
+    s = session_lookup_safe6 (fib_index, &lcl.ip6, &rmt.ip6,
+                             clib_host_to_net_u16 (lcl_port),
+                             clib_host_to_net_u16 (rmt_port), proto);
+  if (s)
+    {
+      *result = s;
+      session_pool_remove_peeker (s->thread_index);
+      return 1;
+    }
+  return 0;
+}
+
+uword
+unformat_transport_connection (unformat_input_t * input, va_list * args)
+{
+  transport_connection_t **result = va_arg (*args, transport_connection_t **);
+  u32 suggested_proto = va_arg (*args, u32);
+  transport_connection_t *tc;
+  u8 proto = ~0;
+  ip46_address_t lcl, rmt;
+  u32 lcl_port = 0, rmt_port = 0, fib_index = 0;
+  u8 is_ip4 = 0;
+
+  if (!unformat (input, "%U", unformat_stream_session_id, &proto, &lcl, &rmt,
+                &lcl_port, &rmt_port, &is_ip4))
+    return 0;
+
+  proto = (proto == (u8) ~ 0) ? suggested_proto : proto;
+  if (proto == (u8) ~ 0)
+    return 0;
+  if (is_ip4)
+    tc = session_lookup_connection4 (fib_index, &lcl.ip4, &rmt.ip4,
+                                    clib_host_to_net_u16 (lcl_port),
+                                    clib_host_to_net_u16 (rmt_port), proto);
+  else
+    tc = session_lookup_connection6 (fib_index, &lcl.ip6, &rmt.ip6,
+                                    clib_host_to_net_u16 (lcl_port),
+                                    clib_host_to_net_u16 (rmt_port), proto);
+
+  if (tc)
+    {
+      *result = tc;
+      return 1;
+    }
+  return 0;
+}
+
 static clib_error_t *
 show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
                         vlib_cli_command_t * cmd)
@@ -95,13 +203,7 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
   int verbose = 0, i;
   stream_session_t *pool;
   stream_session_t *s;
-  u8 *str = 0, one_session = 0, proto_set = 0, proto = 0;
-  u8 is_ip4 = 0, s_type = 0;
-  ip4_address_t lcl_ip4, rmt_ip4;
-  u32 lcl_port = 0, rmt_port = 0;
-
-  memset (&lcl_ip4, 0, sizeof (lcl_ip4));
-  memset (&rmt_ip4, 0, sizeof (rmt_ip4));
+  u8 *str = 0, one_session = 0;
 
   if (!smm->is_enabled)
     {
@@ -114,40 +216,18 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
        ;
       else if (unformat (input, "verbose"))
        verbose = 1;
-      else if (unformat (input, "tcp"))
-       {
-         proto_set = 1;
-         proto = TRANSPORT_PROTO_TCP;
-       }
-      else if (unformat (input, "%U:%d->%U:%d",
-                        unformat_ip4_address, &lcl_ip4, &lcl_port,
-                        unformat_ip4_address, &rmt_ip4, &rmt_port))
+      else if (unformat (input, "%U", unformat_stream_session, &s))
        {
          one_session = 1;
-         is_ip4 = 1;
        }
-
       else
-       break;
+       return clib_error_return (0, "unknown input `%U'",
+                                 format_unformat_error, input);
     }
 
   if (one_session)
     {
-      if (!proto_set)
-       {
-         vlib_cli_output (vm, "proto not set");
-         return clib_error_return (0, "proto not set");
-       }
-
-      s_type = session_type_from_proto_and_ip (proto, is_ip4);
-      s = stream_session_lookup4 (&lcl_ip4, &rmt_ip4,
-                                 clib_host_to_net_u16 (lcl_port),
-                                 clib_host_to_net_u16 (rmt_port), s_type);
-      if (s)
-       vlib_cli_output (vm, "%U", format_stream_session, s, 2);
-      else
-       vlib_cli_output (vm, "session does not exist");
-
+      vlib_cli_output (vm, "%U", format_stream_session, s, 3);
       return 0;
     }
 
@@ -167,9 +247,9 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
            {
              if (once_per_pool && verbose == 1)
                {
-                 str =
-                   format (str, "%-50s%-15s%-10s%-10s%-10s", "Connection",
-                           "State", "Rx-f", "Tx-f", "S-idx");
+                 str = format (str, "%-50s%-15s%-10s%-10s%-10s",
+                               "Connection", "State", "Rx-f", "Tx-f",
+                               "S-idx");
                  vlib_cli_output (vm, "%v", str);
                  vec_reset_length (str);
                  once_per_pool = 0;
@@ -195,10 +275,10 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 }
 
 /* *INDENT-OFF* */
-VLIB_CLI_COMMAND (show_session_command, static) =
+VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
 {
   .path = "show session",
-  .short_help = "show session [verbose]",
+  .short_help = "show session [verbose [nnn]]",
   .function = show_session_command_fn,
 };
 /* *INDENT-ON* */
@@ -243,7 +323,7 @@ clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 
   if (session_index != ~0)
     {
-      session = stream_session_get_if_valid (session_index, thread_index);
+      session = session_get_if_valid (session_index, thread_index);
       if (!session)
        return clib_error_return (0, "no session %d on thread %d",
                                  session_index, thread_index);
@@ -274,23 +354,130 @@ VLIB_CLI_COMMAND (clear_session_command, static) =
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+show_session_fifo_trace_command_fn (vlib_main_t * vm,
+                                   unformat_input_t * input,
+                                   vlib_cli_command_t * cmd)
+{
+  stream_session_t *s = 0;
+  u8 is_rx = 0, *str = 0;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "%U", unformat_stream_session, &s))
+       ;
+      else if (unformat (input, "rx"))
+       is_rx = 1;
+      else if (unformat (input, "tx"))
+       is_rx = 0;
+      else
+       return clib_error_return (0, "unknown input `%U'",
+                                 format_unformat_error, input);
+    }
+
+  if (!SVM_FIFO_TRACE)
+    {
+      vlib_cli_output (vm, "fifo tracing not enabled");
+      return 0;
+    }
+
+  if (!s)
+    {
+      vlib_cli_output (vm, "could not find session");
+      return 0;
+    }
+
+  str = is_rx ?
+    svm_fifo_dump_trace (str, s->server_rx_fifo) :
+    svm_fifo_dump_trace (str, s->server_tx_fifo);
+
+  vlib_cli_output (vm, "%v", str);
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_session_fifo_trace_command, static) =
+{
+  .path = "show session fifo trace",
+  .short_help = "show session fifo trace <session>",
+  .function = show_session_fifo_trace_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
+                               vlib_cli_command_t * cmd)
+{
+  stream_session_t *s = 0;
+  u8 is_rx = 0, *str = 0;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "%U", unformat_stream_session, &s))
+       ;
+      else if (unformat (input, "rx"))
+       is_rx = 1;
+      else
+       return clib_error_return (0, "unknown input `%U'",
+                                 format_unformat_error, input);
+    }
+
+  if (!SVM_FIFO_TRACE)
+    {
+      vlib_cli_output (vm, "fifo tracing not enabled");
+      return 0;
+    }
+
+  if (!s)
+    {
+      vlib_cli_output (vm, "could not find session");
+      return 0;
+    }
+
+  str = is_rx ?
+    svm_fifo_replay (str, s->server_rx_fifo, 0, 1) :
+    svm_fifo_replay (str, s->server_tx_fifo, 0, 1);
+
+  vlib_cli_output (vm, "%v", str);
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (session_replay_fifo_trace_command, static) =
+{
+  .path = "session replay fifo",
+  .short_help = "session replay fifo <session>",
+  .function = session_replay_fifo_command_fn,
+};
+/* *INDENT-ON* */
+
 static clib_error_t *
 session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input,
                           vlib_cli_command_t * cmd)
 {
+  unformat_input_t _line_input, *line_input = &_line_input;
   u8 is_en = 1;
+  clib_error_t *error;
 
-  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "enable"))
+      if (unformat (line_input, "enable"))
        is_en = 1;
-      else if (unformat (input, "disable"))
+      else if (unformat (line_input, "disable"))
        is_en = 0;
       else
-       return clib_error_return (0, "unknown input `%U'",
-                                 format_unformat_error, input);
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, line_input);
+         unformat_free (line_input);
+         return error;
+       }
     }
 
+  unformat_free (line_input);
   return vnet_session_enable_disable (vm, is_en);
 }