vppinfra: Improve code portability
[vpp.git] / src / vnet / session / session_cli.c
index fabff11..569a77b 100644 (file)
@@ -145,8 +145,11 @@ format_session (u8 * s, va_list * args)
   else if (ss->session_state == SESSION_STATE_CONNECTING)
     {
       if (ss->flags & SESSION_F_HALF_OPEN)
-       s = format (s, "%U%v", format_transport_half_open_connection, tp,
-                   ss->connection_index, ss->thread_index, verbose, str);
+       {
+         s = format (s, "%U", format_transport_half_open_connection, tp,
+                     ss->connection_index, ss->thread_index, verbose);
+         s = format (s, "%v", str);
+       }
       else
        s = format (s, "%U", format_transport_connection, tp,
                    ss->connection_index, ss->thread_index, verbose);
@@ -339,7 +342,6 @@ session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
 
       n_closed = 0;
 
-      /* *INDENT-OFF* */
       pool_foreach (s, pool)  {
         if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED)
           {
@@ -348,7 +350,6 @@ session_cli_show_all_sessions (vlib_main_t * vm, int verbose)
           }
         vlib_cli_output (vm, "%U", format_session, s, verbose);
       }
-      /* *INDENT-ON* */
 
       if (!n_closed)
        vlib_cli_output (vm, "Thread %d: active sessions %u", thread_index,
@@ -487,7 +488,6 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 {
   u8 one_session = 0, do_listeners = 0, sst, do_elog = 0, do_filter = 0;
   u32 track_index, thread_index = 0, start = 0, end = ~0, session_index;
-  unformat_input_t _cli_args, *cli_args = &_cli_args;
   transport_proto_t transport_proto = TRANSPORT_PROTO_INVALID;
   session_state_t state = SESSION_N_STATES, *states = 0;
   session_main_t *smm = &session_main;
@@ -501,26 +501,20 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 
   session_cli_return_if_not_enabled ();
 
-  if (!unformat_user (input, unformat_vlib_cli_args, cli_args))
-    {
-      session_cli_show_all_sessions (vm, 0);
-      return 0;
-    }
-
-  while (unformat_check_input (cli_args) != UNFORMAT_END_OF_INPUT)
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (cli_args, "verbose %d", &verbose))
+      if (unformat (input, "verbose %d", &verbose))
        ;
-      else if (unformat (cli_args, "verbose"))
+      else if (unformat (input, "verbose"))
        verbose = 1;
-      else if (unformat (cli_args, "listeners %U", unformat_transport_proto,
+      else if (unformat (input, "listeners %U", unformat_transport_proto,
                         &transport_proto))
        do_listeners = 1;
-      else if (unformat (cli_args, "%U", unformat_session, &s))
+      else if (unformat (input, "%U", unformat_session, &s))
        {
          one_session = 1;
        }
-      else if (unformat (cli_args, "thread %u index %u", &thread_index,
+      else if (unformat (input, "thread %u index %u", &thread_index,
                         &session_index))
        {
          s = session_get_if_valid (session_index, thread_index);
@@ -531,18 +525,17 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
            }
          one_session = 1;
        }
-      else if (unformat (cli_args, "thread %u", &thread_index))
+      else if (unformat (input, "thread %u", &thread_index))
        {
          do_filter = 1;
        }
-      else if (unformat (cli_args, "state %U", unformat_session_state, &state))
+      else if (unformat (input, "state %U", unformat_session_state, &state))
        {
          vec_add1 (states, state);
          do_filter = 1;
        }
-      else if (unformat (cli_args, "proto %U index %u",
-                        unformat_transport_proto, &transport_proto,
-                        &transport_index))
+      else if (unformat (input, "proto %U index %u", unformat_transport_proto,
+                        &transport_proto, &transport_index))
        {
          transport_connection_t *tc;
          tc = transport_get_connection (transport_proto, transport_index,
@@ -563,34 +556,34 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
            }
          one_session = 1;
        }
-      else if (unformat (cli_args, "proto %U", unformat_transport_proto,
+      else if (unformat (input, "proto %U", unformat_transport_proto,
                         &transport_proto))
        do_filter = 1;
-      else if (unformat (cli_args, "range %u %u", &start, &end))
+      else if (unformat (input, "range %u %u", &start, &end))
        do_filter = 1;
-      else if (unformat (cli_args, "range %u", &start))
+      else if (unformat (input, "range %u", &start))
        {
          end = start + 50;
          do_filter = 1;
        }
-      else if (unformat (cli_args, "elog"))
+      else if (unformat (input, "elog"))
        do_elog = 1;
-      else if (unformat (cli_args, "protos"))
+      else if (unformat (input, "protos"))
        {
          vlib_cli_output (vm, "%U", format_transport_protos);
          goto done;
        }
-      else if (unformat (cli_args, "states"))
+      else if (unformat (input, "states"))
        {
          session_cli_print_session_states (vm);
          goto done;
        }
-      else if (unformat (cli_args, "events"))
+      else if (unformat (input, "events"))
        do_events = 1;
       else
        {
          error = clib_error_return (0, "unknown input `%U'",
-                                    format_unformat_error, cli_args);
+                                    format_unformat_error, input);
          goto done;
        }
     }
@@ -623,7 +616,6 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
       vlib_cli_output (vm, "%-" SESSION_CLI_ID_LEN "s%-24s", "Listener",
                       "App");
 
-      /* *INDENT-OFF* */
       pool_foreach (s, smm->wrk[0].sessions)  {
        if (s->session_state != SESSION_STATE_LISTENING
            || s->session_type != sst)
@@ -633,7 +625,6 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
        vlib_cli_output (vm, "%U%-25v%", format_session, s, 0,
                         app_name);
       }
-      /* *INDENT-ON* */
       goto done;
     }
 
@@ -659,12 +650,10 @@ show_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
   session_cli_show_all_sessions (vm, verbose);
 
 done:
-  unformat_free (cli_args);
   vec_free (states);
   return error;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
 {
   .path = "show session",
@@ -674,7 +663,6 @@ VLIB_CLI_COMMAND (vlib_cli_show_session_command) =
                "[protos] [states] ",
   .function = show_session_command_fn,
 };
-/* *INDENT-ON* */
 
 static int
 clear_session (session_t * s)
@@ -726,27 +714,23 @@ clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input,
 
   if (clear_all)
     {
-      /* *INDENT-OFF* */
       vec_foreach (wrk, smm->wrk)
        {
          pool_foreach (session, wrk->sessions)  {
            clear_session (session);
          }
        };
-      /* *INDENT-ON* */
     }
 
   return 0;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (clear_session_command, static) =
 {
   .path = "clear session",
   .short_help = "clear session thread <thread> session <index>",
   .function = clear_session_command_fn,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 show_session_fifo_trace_command_fn (vlib_main_t * vm,
@@ -789,14 +773,12 @@ show_session_fifo_trace_command_fn (vlib_main_t * vm,
   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,
@@ -836,53 +818,98 @@ session_replay_fifo_command_fn (vlib_main_t * vm, unformat_input_t * input,
   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 _cli_args, *cli_args = &_cli_args;
-  u8 is_en = 1;
-  clib_error_t *error;
-
-  if (!unformat_user (input, unformat_vlib_cli_args, cli_args))
-    return clib_error_return (0, "expected enable | disable");
+  u8 is_en = 2;
 
-  while (unformat_check_input (cli_args) != UNFORMAT_END_OF_INPUT)
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (cli_args, "enable"))
+      if (unformat (input, "enable"))
        is_en = 1;
-      else if (unformat (cli_args, "disable"))
+      else if (unformat (input, "disable"))
        is_en = 0;
       else
-       {
-         error = clib_error_return (0, "unknown input `%U'",
-                                    format_unformat_error, cli_args);
-         unformat_free (cli_args);
-         return error;
-       }
+       return clib_error_return (0, "unknown input `%U'",
+                                 format_unformat_error, input);
     }
 
-  unformat_free (cli_args);
+  if (is_en > 1)
+    return clib_error_return (0, "expected enable | disable");
+
   return vnet_session_enable_disable (vm, is_en);
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (session_enable_disable_command, static) =
 {
   .path = "session",
   .short_help = "session [enable|disable]",
   .function = session_enable_disable_fn,
 };
-/* *INDENT-ON* */
+
+static clib_error_t *
+show_session_stats_fn (vlib_main_t *vm, unformat_input_t *input,
+                      vlib_cli_command_t *cmd)
+{
+  session_main_t *smm = &session_main;
+  session_worker_t *wrk;
+  unsigned int *e;
+
+  if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
+                             input);
+
+  vec_foreach (wrk, smm->wrk)
+    {
+      vlib_cli_output (vm, "Thread %u:\n", wrk - smm->wrk);
+      e = wrk->stats.errors;
+#define _(name, str)                                                          \
+  if (e[SESSION_EP_##name])                                                   \
+    vlib_cli_output (vm, " %lu %s", e[SESSION_EP_##name], str);
+      foreach_session_error
+#undef _
+    }
+  return 0;
+}
+
+VLIB_CLI_COMMAND (show_session_stats_command, static) = {
+  .path = "show session stats",
+  .short_help = "show session stats",
+  .function = show_session_stats_fn,
+};
+
+static clib_error_t *
+clear_session_stats_fn (vlib_main_t *vm, unformat_input_t *input,
+                       vlib_cli_command_t *cmd)
+{
+  session_main_t *smm = &session_main;
+  session_worker_t *wrk;
+
+  if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
+                             input);
+
+  vec_foreach (wrk, smm->wrk)
+    {
+      clib_memset (&wrk->stats, 0, sizeof (wrk->stats));
+    }
+
+  return 0;
+}
+
+VLIB_CLI_COMMAND (clear_session_stats_command, static) = {
+  .path = "clear session stats",
+  .short_help = "clear session stats",
+  .function = clear_session_stats_fn,
+};
 
 /*
  * fd.io coding-style-patch-verification: ON