vlib: add verbose sh node option
[vpp.git] / src / vlib / node_cli.c
index 05d0f0b..0738bc9 100644 (file)
@@ -37,6 +37,9 @@
  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <vlib/vlib.h>
 #include <vlib/threads.h>
 
@@ -88,6 +91,127 @@ VLIB_CLI_COMMAND (show_node_graph_command, static) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+show_node_graphviz (vlib_main_t * vm,
+                   unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  clib_error_t *error = 0;
+  vlib_node_main_t *nm = &vm->node_main;
+  u8 *chroot_filename = 0;
+  int fd;
+  vlib_node_t **nodes = 0;
+  uword i, j;
+
+  if (!unformat_user (input, unformat_vlib_tmpfile, &chroot_filename))
+    {
+      fd = -1;
+    }
+  else
+    {
+      fd =
+       open ((char *) chroot_filename, O_CREAT | O_TRUNC | O_WRONLY, 0664);
+    }
+
+#define format__(vm__, fd__, ...) \
+  if ((fd) < 0) \
+    { \
+      vlib_cli_output((vm__), ## __VA_ARGS__); \
+    } \
+  else \
+    { \
+      fdformat((fd__), ## __VA_ARGS__); \
+    }
+
+  format__ (vm, fd, "%s", "digraph {\n");
+
+  nodes = vec_dup (nm->nodes);
+  vec_sort_with_function (nodes, node_cmp);
+
+  for (i = 0; i < vec_len (nodes); i++)
+    {
+      for (j = 0; j < vec_len (nodes[i]->next_nodes); j++)
+       {
+         vlib_node_t *x;
+
+         if (nodes[i]->next_nodes[j] == VLIB_INVALID_NODE_INDEX)
+           continue;
+
+         x = vec_elt (nm->nodes, nodes[i]->next_nodes[j]);
+         format__ (vm, fd, "  \"%v\" -> \"%v\"\n", nodes[i]->name, x->name);
+       }
+    }
+
+  format__ (vm, fd, "%s", "}");
+
+  if (fd >= 0)
+    {
+      vlib_cli_output (vm,
+                      "vlib graph dumped into `%s'. Run eg. `fdp -Tsvg -O %s'.",
+                      chroot_filename, chroot_filename);
+    }
+
+  vec_free (nodes);
+  vec_free (chroot_filename);
+  vec_free (nodes);
+  if (fd >= 0)
+    close (fd);
+  return error;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_node_graphviz_command, static) = {
+  .path = "show vlib graphviz",
+  .short_help = "Dump packet processing node graph as a graphviz dotfile",
+  .function = show_node_graphviz,
+};
+/* *INDENT-ON* */
+
+static u8 *
+format_vlib_node_state (u8 * s, va_list * va)
+{
+  vlib_main_t *vm = va_arg (*va, vlib_main_t *);
+  vlib_node_t *n = va_arg (*va, vlib_node_t *);
+  char *state;
+
+  state = "active";
+  if (n->type == VLIB_NODE_TYPE_PROCESS)
+    {
+      vlib_process_t *p = vlib_get_process_from_node (vm, n);
+
+      switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
+                         | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
+       {
+       default:
+         if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
+           state = "done";
+         break;
+
+       case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
+         state = "time wait";
+         break;
+
+       case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
+         state = "event wait";
+         break;
+
+       case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
+         state =
+           "any wait";
+         break;
+       }
+    }
+  else if (n->type != VLIB_NODE_TYPE_INTERNAL)
+    {
+      state = "polling";
+      if (n->state == VLIB_NODE_STATE_DISABLED)
+       state = "disabled";
+      else if (n->state == VLIB_NODE_STATE_INTERRUPT)
+       state = "interrupt wait";
+    }
+
+  return format (s, "%s", state);
+}
+
 static u8 *
 format_vlib_node_stats (u8 * s, va_list * va)
 {
@@ -95,27 +219,27 @@ format_vlib_node_stats (u8 * s, va_list * va)
   vlib_node_t *n = va_arg (*va, vlib_node_t *);
   int max = va_arg (*va, int);
   f64 v;
-  char *state;
   u8 *ns;
   u8 *misc_info = 0;
   u64 c, p, l, d;
   f64 x;
   f64 maxc, maxcn;
   u32 maxn;
-  uword indent;
+  u32 indent;
 
   if (!n)
     {
       if (max)
-       return format (s,
-                      "%=30s%=17s%=16s%=16s%=16s%=16s",
-                      "Name", "Max Node Clocks", "Vectors at Max",
-                      "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
+       s = format (s,
+                   "%=30s%=17s%=16s%=16s%=16s%=16s",
+                   "Name", "Max Node Clocks", "Vectors at Max",
+                   "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
       else
-       return format (s,
-                      "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
-                      "Name", "State", "Calls", "Vectors", "Suspends",
-                      "Clocks", "Vectors/Call");
+       s = format (s,
+                   "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
+                   "Name", "State", "Calls", "Vectors", "Suspends",
+                   "Clocks", "Vectors/Call");
+      return s;
     }
 
   indent = format_get_indent (s);
@@ -145,7 +269,6 @@ format_vlib_node_stats (u8 * s, va_list * va)
   else
     v = 0;
 
-  state = "active";
   if (n->type == VLIB_NODE_TYPE_PROCESS)
     {
       vlib_process_t *p = vlib_get_process_from_node (vm, n);
@@ -154,46 +277,15 @@ format_vlib_node_stats (u8 * s, va_list * va)
          being handled. */
       if (!clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
        misc_info = format (misc_info, "events pending, ");
-
-      switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
-                         | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
-       {
-       default:
-         if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
-           state = "done";
-         break;
-
-       case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
-         state = "time wait";
-         break;
-
-       case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
-         state = "event wait";
-         break;
-
-       case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
-         state =
-           "any wait";
-         break;
-       }
-    }
-  else if (n->type != VLIB_NODE_TYPE_INTERNAL)
-    {
-      state = "polling";
-      if (n->state == VLIB_NODE_STATE_DISABLED)
-       state = "disabled";
-      else if (n->state == VLIB_NODE_STATE_INTERRUPT)
-       state = "interrupt wait";
     }
-
   ns = n->name;
 
   if (max)
     s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
                ns, maxc, maxn, maxcn, x, v);
   else
-    s = format (s, "%-30v%=12s%16Ld%16Ld%16Ld%16.2e%16.2f", ns, state,
-               c, p, d, x, v);
+    s = format (s, "%-30v%=12U%16Ld%16Ld%16Ld%16.2e%16.2f", ns,
+               format_vlib_node_state, vm, n, c, p, d, x, v);
 
   if (ns != n->name)
     vec_free (ns);
@@ -207,6 +299,13 @@ format_vlib_node_stats (u8 * s, va_list * va)
   return s;
 }
 
+f64 vlib_get_stat_segment_update_rate (void) __attribute__ ((weak));
+f64
+vlib_get_stat_segment_update_rate (void)
+{
+  return 1e70;
+}
+
 static clib_error_t *
 show_node_runtime (vlib_main_t * vm,
                   unformat_input_t * input, vlib_cli_command_t * cmd)
@@ -216,8 +315,7 @@ show_node_runtime (vlib_main_t * vm,
   f64 time_now;
   u32 node_index;
   vlib_node_t ***node_dups = 0;
-  f64 *vectors_per_main_loop = 0;
-  f64 *last_vector_length_per_node = 0;
+  f64 *internal_node_vector_rates = 0;
 
   time_now = vlib_time_now (vm);
 
@@ -237,6 +335,7 @@ show_node_runtime (vlib_main_t * vm,
       u64 n_internal_vectors, n_internal_calls;
       u64 n_clocks, l, v, c, d;
       int brief = 1;
+      int summary = 0;
       int max = 0;
       vlib_main_t **stat_vms = 0, *stat_vm;
 
@@ -247,17 +346,15 @@ show_node_runtime (vlib_main_t * vm,
        brief = 0;
       if (unformat (input, "max") || unformat (input, "m"))
        max = 1;
+      if (unformat (input, "summary") || unformat (input, "sum")
+         || unformat (input, "su"))
+       summary = 1;
 
-      if (vec_len (vlib_mains) == 0)
-       vec_add1 (stat_vms, vm);
-      else
+      for (i = 0; i < vec_len (vlib_mains); i++)
        {
-         for (i = 0; i < vec_len (vlib_mains); i++)
-           {
-             stat_vm = vlib_mains[i];
-             if (stat_vm)
-               vec_add1 (stat_vms, stat_vm);
-           }
+         stat_vm = vlib_mains[i];
+         if (stat_vm)
+           vec_add1 (stat_vms, stat_vm);
        }
 
       /*
@@ -280,10 +377,8 @@ show_node_runtime (vlib_main_t * vm,
          nodes = vec_dup (nm->nodes);
 
          vec_add1 (node_dups, nodes);
-         vec_add1 (vectors_per_main_loop,
-                   vlib_last_vectors_per_main_loop_as_f64 (stat_vm));
-         vec_add1 (last_vector_length_per_node,
-                   vlib_last_vector_length_per_node (stat_vm));
+         vec_add1 (internal_node_vector_rates,
+                   vlib_internal_node_vector_rate (stat_vm));
        }
       vlib_worker_thread_barrier_release (vm);
 
@@ -331,15 +426,15 @@ show_node_runtime (vlib_main_t * vm,
                }
            }
 
-         if (vec_len (vlib_mains))
+         if (vec_len (vlib_mains) > 1)
            {
              vlib_worker_thread_t *w = vlib_worker_threads + j;
              if (j > 0)
                vlib_cli_output (vm, "---------------");
 
-             if (w->lcore_id > -1)
+             if (w->cpu_id > -1)
                vlib_cli_output (vm, "Thread %d %s (lcore %u)", j, w->name,
-                                w->lcore_id);
+                                w->cpu_id);
              else
                vlib_cli_output (vm, "Thread %d %s", j, w->name);
            }
@@ -347,39 +442,39 @@ show_node_runtime (vlib_main_t * vm,
          dt = time_now - nm->time_last_runtime_stats_clear;
          vlib_cli_output
            (vm,
-            "Time %.1f, average vectors/node %.2f, last %d main loops %.2f per node %.2f"
-            "\n  vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
+            "Time %.1f, %f sec internal node vector rate %.2f loops/sec %.2f\n"
+            "  vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
             dt,
-            (n_internal_calls > 0
-             ? (f64) n_internal_vectors / (f64) n_internal_calls
-             : 0),
-            1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE,
-            vectors_per_main_loop[j],
-            last_vector_length_per_node[j],
+            vlib_get_stat_segment_update_rate (),
+            internal_node_vector_rates[j],
+            stat_vm->loops_per_second,
             (f64) n_input / dt,
             (f64) n_output / dt, (f64) n_drop / dt, (f64) n_punt / dt);
 
-         vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm, 0, max);
-         for (i = 0; i < vec_len (nodes); i++)
+         if (summary == 0)
            {
-             c =
-               nodes[i]->stats_total.calls -
-               nodes[i]->stats_last_clear.calls;
-             d =
-               nodes[i]->stats_total.suspends -
-               nodes[i]->stats_last_clear.suspends;
-             if (c || d || !brief)
+             vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
+                              0, max);
+             for (i = 0; i < vec_len (nodes); i++)
                {
-                 vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
-                                  nodes[i], max);
+                 c =
+                   nodes[i]->stats_total.calls -
+                   nodes[i]->stats_last_clear.calls;
+                 d =
+                   nodes[i]->stats_total.suspends -
+                   nodes[i]->stats_last_clear.suspends;
+                 if (c || d || !brief)
+                   {
+                     vlib_cli_output (vm, "%U", format_vlib_node_stats,
+                                      stat_vm, nodes[i], max);
+                   }
                }
            }
          vec_free (nodes);
        }
       vec_free (stat_vms);
       vec_free (node_dups);
-      vec_free (vectors_per_main_loop);
-      vec_free (last_vector_length_per_node);
+      vec_free (internal_node_vector_rates);
     }
 
   return 0;
@@ -404,16 +499,11 @@ clear_node_runtime (vlib_main_t * vm,
   vlib_main_t **stat_vms = 0, *stat_vm;
   vlib_node_runtime_t *r;
 
-  if (vec_len (vlib_mains) == 0)
-    vec_add1 (stat_vms, vm);
-  else
+  for (i = 0; i < vec_len (vlib_mains); i++)
     {
-      for (i = 0; i < vec_len (vlib_mains); i++)
-       {
-         stat_vm = vlib_mains[i];
-         if (stat_vm)
-           vec_add1 (stat_vms, stat_vm);
-       }
+      stat_vm = vlib_mains[i];
+      if (stat_vm)
+       vec_add1 (stat_vms, stat_vm);
     }
 
   vlib_worker_thread_barrier_sync (vm);
@@ -451,6 +541,244 @@ VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+show_node (vlib_main_t * vm, unformat_input_t * input,
+          vlib_cli_command_t * cmd)
+{
+  unformat_input_t _line_input, *line_input = &_line_input;
+  clib_error_t *error = 0;
+  vlib_node_main_t *nm = &vm->node_main;
+  vlib_node_t *n;
+  u8 *s = 0, *s2 = 0;
+  u32 i, node_index = ~0, verbose = 0;
+  char *type_str;
+  u8 valid_node_name = 0;
+  u64 cl, ca, v;
+
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (line_input, "index %u", &node_index))
+       ;
+      else if (unformat (line_input, "verbose"))
+       verbose = 1;
+      else
+       if (unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
+       valid_node_name = 1;
+      else if (!valid_node_name)
+       error = clib_error_return (0, "unknown node name: '%U'",
+                                  format_unformat_error, line_input);
+      else
+       error = clib_error_return (0, "unknown input '%U'",
+                                  format_unformat_error, line_input);
+
+      if (error)
+       break;
+    }
+
+  unformat_free (line_input);
+
+  if (error)
+    return error;
+
+  if (node_index >= vec_len (vm->node_main.nodes))
+    return clib_error_return (0, "please specify valid node");
+
+  n = vlib_get_node (vm, node_index);
+  vlib_node_sync_stats (vm, n);
+
+  switch (n->type)
+    {
+    case VLIB_NODE_TYPE_INTERNAL:
+      type_str = "internal";
+      break;
+    case VLIB_NODE_TYPE_INPUT:
+      type_str = "input";
+      break;
+    case VLIB_NODE_TYPE_PRE_INPUT:
+      type_str = "pre-input";
+      break;
+    case VLIB_NODE_TYPE_PROCESS:
+      type_str = "process";
+      break;
+    default:
+      type_str = "unknown";
+    }
+
+  if (n->sibling_of)
+    s = format (s, ", sibling-of %s", n->sibling_of);
+
+  vlib_cli_output (vm, "node %v, type %s, state %U, index %d%v\n",
+                  n->name, type_str, format_vlib_node_state, vm, n,
+                  n->index, s);
+  vec_reset_length (s);
+
+  if (n->node_fn_registrations)
+    {
+      vlib_node_fn_registration_t *fnr = n->node_fn_registrations;
+      while (fnr)
+       {
+         if (vec_len (s) == 0)
+           s = format (s, "\n    %-15s  %=8s  %6s",
+                       "Name", "Priority", "Active");
+         s = format (s, "\n    %-15s  %=8u  %=6s", fnr->name, fnr->priority,
+                     fnr->function == n->function ? "yes" : "");
+         fnr = fnr->next_registration;
+       }
+    }
+  else
+    s = format (s, "\n    default only");
+  vlib_cli_output (vm, "  node function variants:%v\n", s);
+  vec_reset_length (s);
+
+  for (i = 0; i < vec_len (n->next_nodes); i++)
+    {
+      vlib_node_t *pn;
+      if (n->next_nodes[i] == VLIB_INVALID_NODE_INDEX)
+       continue;
+
+      pn = vec_elt (nm->nodes, n->next_nodes[i]);
+
+      if (vec_len (s) == 0)
+       s = format (s, "\n    %10s  %10s  %=30s %8s",
+                   "next-index", "node-index", "Node", "Vectors");
+
+      s = format (s, "\n    %=10u  %=10u  %=30v %=8llu", i, n->next_nodes[i],
+                 pn->name, vec_elt (n->n_vectors_by_next_node, i));
+    }
+
+  if (vec_len (s) == 0)
+    s = format (s, "\n    none");
+  vlib_cli_output (vm, "\n  next nodes:%v\n", s);
+  vec_reset_length (s);
+
+  if (n->type == VLIB_NODE_TYPE_INTERNAL)
+    {
+      int j = 0;
+      /* *INDENT-OFF* */
+      clib_bitmap_foreach (i, n->prev_node_bitmap, ({
+           vlib_node_t *pn = vlib_get_node (vm, i);
+           if (j++ % 3 == 0)
+             s = format (s, "\n    ");
+           s2 = format (s2, "%v (%u)", pn->name, i);
+           s = format (s, "%-35v", s2);
+           vec_reset_length (s2);
+         }));
+      /* *INDENT-ON* */
+
+      if (vec_len (s) == 0)
+       s = format (s, "\n    none");
+      vlib_cli_output (vm, "\n  known previous nodes:%v\n", s);
+      vec_reset_length (s);
+      vec_free (s2);
+    }
+
+  if (!verbose)
+    goto done;
+
+  s = format (s, "\n%8s %=12s %=12s %=12s %=12s %=12s\n", "Thread", "Calls",
+             "Clocks", "Vectors", "Max Clock", "Max Vectors");
+  for (i = 0; i < vec_len (vlib_mains); i++)
+    {
+      n = vlib_get_node (vlib_mains[i], node_index);
+      vlib_node_sync_stats (vlib_mains[i], n);
+
+      cl = n->stats_total.clocks - n->stats_last_clear.clocks;
+      ca = n->stats_total.calls - n->stats_last_clear.calls;
+      v = n->stats_total.vectors - n->stats_last_clear.vectors;
+
+      s = format (s, "%=8u %=12lu %=12lu %=12lu %=12u %=12u\n", i, ca, cl, v,
+                 n->stats_total.max_clock, n->stats_total.max_clock_n);
+    }
+
+  vlib_cli_output (vm, "%v", s);
+
+done:
+
+  vec_free (s);
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_node_command, static) = {
+  .path = "show node",
+  .short_help = "show node [index] <node-name | node-index>",
+  .function = show_node,
+};
+
+static clib_error_t *
+set_node_fn(vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  unformat_input_t _line_input, *line_input = &_line_input;
+  u32 node_index;
+  vlib_node_t *n;
+  clib_error_t *err = 0;
+  vlib_node_fn_registration_t *fnr;
+  u8 *variant = 0;
+
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  if (!unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
+    {
+      err = clib_error_return (0, "please specify valid node name");
+      goto done;
+    }
+
+  if (!unformat (line_input, "%s", &variant))
+    {
+      err = clib_error_return (0, "please specify node functional variant");
+      goto done;
+    }
+
+  n = vlib_get_node (vm, node_index);
+
+  if (n->node_fn_registrations == 0)
+    {
+      err = clib_error_return (0, "node doesn't have functional variants");
+      goto done;
+    }
+
+  fnr = n->node_fn_registrations;
+  vec_add1 (variant, 0);
+
+  while (fnr)
+    {
+      if (!strncmp (fnr->name, (char *) variant, vec_len (variant) - 1))
+       {
+         int i;
+
+         n->function = fnr->function;
+
+         for (i = 0; i < vec_len (vlib_mains); i++)
+           {
+             vlib_node_runtime_t *nrt;
+             nrt = vlib_node_get_runtime (vlib_mains[i], n->index);
+             nrt->function = fnr->function;
+           }
+         goto done;
+       }
+      fnr = fnr->next_registration;
+    }
+
+  err = clib_error_return (0, "node functional variant '%s' not found", variant);
+
+done:
+  vec_free (variant);
+  unformat_free (line_input);
+  return err;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (set_node_fn_command, static) = {
+  .path = "set node function",
+  .short_help = "set node function <node-name> <variant-name>",
+  .function = set_node_fn,
+};
+/* *INDENT-ON* */
+
 /* Dummy function to get us linked in. */
 void
 vlib_node_cli_reference (void)