Add API calls for packet generator
[vpp.git] / vlib / vlib / cli.c
index e5163f2..79db99f 100644 (file)
 #include <vlib/vlib.h>
 
 /* Root of all show commands. */
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
   .path = "show",
   .short_help = "Show commands",
 };
+/* *INDENT-ON* */
 
 /* Root of all clear commands. */
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
   .path = "clear",
   .short_help = "Clear commands",
 };
+/* *INDENT-ON* */
 
 /* Root of all set commands. */
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
   .path = "set",
   .short_help = "Set commands",
 };
+/* *INDENT-ON* */
 
 /* Root of all test commands. */
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
   .path = "test",
   .short_help = "Test commands",
 };
+/* *INDENT-ON* */
 
 /* Returns bitmap of commands which match key. */
 static uword *
 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
 {
   int i, n;
-  uword * match = 0;
-  vlib_cli_parse_position_t * p;
+  uword *match = 0;
+  vlib_cli_parse_position_t *p;
 
   unformat_skip_white_space (input);
 
-  for (i = 0; ; i++)
+  for (i = 0;; i++)
     {
       uword k;
 
@@ -83,10 +91,14 @@ vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
        case 'a' ... 'z':
        case 'A' ... 'Z':
        case '0' ... '9':
-       case '-': case '_':
+       case '-':
+       case '_':
          break;
 
-       case ' ': case '\t': case '\r': case '\n':
+       case ' ':
+       case '\t':
+       case '\r':
+       case '\n':
        case UNFORMAT_END_OF_INPUT:
          /* White space or end of input removes any non-white
             matches that were before possible. */
@@ -128,15 +140,16 @@ vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
        goto no_match;
     }
 
- done:
+done:
   return match;
 }
 
 /* Looks for string based sub-input formatted { SUB-INPUT }. */
-static uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
+uword
+unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
 {
-  unformat_input_t * sub_input = va_arg (*args, unformat_input_t *);
-  u8 * s;
+  unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
+  u8 *s;
   uword c;
 
   while (1)
@@ -144,8 +157,10 @@ static uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
       c = unformat_get_input (i);
       switch (c)
        {
-       case ' ': case '\t':
-       case '\n': case '\r':
+       case ' ':
+       case '\t':
+       case '\n':
+       case '\r':
        case '\f':
          break;
 
@@ -169,39 +184,40 @@ static uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
 static vlib_cli_command_t *
 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
 {
-  vlib_cli_sub_command_t * s = vec_elt_at_index (parent->sub_commands, si);
+  vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
   return vec_elt_at_index (cm->commands, s->index);
 }
 
-static uword unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
+static uword
+unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
 {
-  vlib_main_t * vm = va_arg (*args, vlib_main_t *);
-  vlib_cli_command_t * c = va_arg (*args, vlib_cli_command_t *);
-  vlib_cli_command_t ** result = va_arg (*args, vlib_cli_command_t **);
-  vlib_cli_main_t * cm = &vm->cli_main;
-  uword * match_bitmap, is_unique, index;
+  vlib_main_t *vm = va_arg (*args, vlib_main_t *);
+  vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
+  vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
+  vlib_cli_main_t *cm = &vm->cli_main;
+  uword *match_bitmap, is_unique, index;
 
   {
-    vlib_cli_sub_rule_t * sr;
-    vlib_cli_parse_rule_t * r;
+    vlib_cli_sub_rule_t *sr;
+    vlib_cli_parse_rule_t *r;
     vec_foreach (sr, c->sub_rules)
-      {
-       void ** d;
-       r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
-       vec_add2 (cm->parse_rule_data, d, 1);
-       vec_reset_length (d[0]);
-       if (r->data_size)
-         d[0] = _vec_resize (d[0],
-                             /* length increment */ 1,
-                             r->data_size,
-                             /* header_bytes */ 0,
-                             /* data align */ sizeof (uword));
-       if (unformat_user (i, r->unformat_function, vm, d[0]))
-         {
-           *result = vec_elt_at_index (cm->commands, sr->command_index);
-           return 1;
-         }
-      }
+    {
+      void **d;
+      r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
+      vec_add2 (cm->parse_rule_data, d, 1);
+      vec_reset_length (d[0]);
+      if (r->data_size)
+       d[0] = _vec_resize (d[0],
+                           /* length increment */ 1,
+                           r->data_size,
+                           /* header_bytes */ 0,
+                           /* data align */ sizeof (uword));
+      if (unformat_user (i, r->unformat_function, vm, d[0]))
+       {
+         *result = vec_elt_at_index (cm->commands, sr->command_index);
+         return 1;
+       }
+    }
   }
 
   match_bitmap = vlib_cli_sub_command_match (c, i);
@@ -217,9 +233,10 @@ static uword unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args
   return is_unique;
 }
 
-static u8 * format_vlib_cli_command_help (u8 * s, va_list * args)
+static u8 *
+format_vlib_cli_command_help (u8 * s, va_list * args)
 {
-  vlib_cli_command_t * c = va_arg (*args, vlib_cli_command_t *);
+  vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
   int is_long = va_arg (*args, int);
   if (is_long && c->long_help)
     s = format (s, "%s", c->long_help);
@@ -230,15 +247,17 @@ static u8 * format_vlib_cli_command_help (u8 * s, va_list * args)
   return s;
 }
 
-static u8 * format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
+static u8 *
+format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
 {
-  vlib_cli_parse_rule_t * r  = va_arg (*args, vlib_cli_parse_rule_t *);
+  vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
   return format (s, "<%U>", format_c_identifier, r->name);
 }
 
-static u8 * format_vlib_cli_path (u8 * s, va_list * args)
+static u8 *
+format_vlib_cli_path (u8 * s, va_list * args)
 {
-  u8 * path = va_arg (*args, u8 *);
+  u8 *path = va_arg (*args, u8 *);
   int i, in_rule;
   in_rule = 0;
   for (i = 0; i < vec_len (path); i++)
@@ -258,7 +277,7 @@ static u8 * format_vlib_cli_path (u8 * s, va_list * args)
        case ' ':
          if (in_rule)
            {
-             vec_add1 (s, '>'); /* end of <RULE> */
+             vec_add1 (s, '>');        /* end of <RULE> */
              in_rule = 0;
            }
          vec_add1 (s, ' ');
@@ -277,35 +296,50 @@ static u8 * format_vlib_cli_path (u8 * s, va_list * args)
 }
 
 static vlib_cli_command_t *
-all_subs (vlib_cli_main_t * cm,
-         vlib_cli_command_t * subs,
-         u32 command_index)
+all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
 {
-  vlib_cli_command_t * c = vec_elt_at_index (cm->commands, command_index);
-  vlib_cli_sub_command_t * sc;
-  vlib_cli_sub_rule_t * sr;
+  vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
+  vlib_cli_sub_command_t *sc;
+  vlib_cli_sub_rule_t *sr;
 
   if (c->function)
     vec_add1 (subs, c[0]);
 
   vec_foreach (sr, c->sub_rules)
     subs = all_subs (cm, subs, sr->command_index);
-  vec_foreach (sc, c->sub_commands)
-    subs = all_subs (cm, subs, sc->index);
+  vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
 
   return subs;
 }
 
+static int
+vlib_cli_cmp_rule (void *a1, void *a2)
+{
+  vlib_cli_sub_rule_t *r1 = a1;
+  vlib_cli_sub_rule_t *r2 = a2;
+
+  return vec_cmp (r1->name, r2->name);
+}
+
+static int
+vlib_cli_cmp_command (void *a1, void *a2)
+{
+  vlib_cli_command_t *c1 = a1;
+  vlib_cli_command_t *c2 = a2;
+
+  return vec_cmp (c1->path, c2->path);
+}
+
 static clib_error_t *
 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
                                vlib_cli_main_t * cm,
                                unformat_input_t * input,
                                uword parent_command_index)
 {
-  vlib_cli_command_t * parent, * c;
-  clib_error_t * error = 0;
+  vlib_cli_command_t *parent, *c;
+  clib_error_t *error = 0;
   unformat_input_t sub_input;
-  u8 * string;
+  u8 *string;
   uword is_main_dispatch = cm == &vm->cli_main;
 
   parent = vec_elt_at_index (cm->commands, parent_command_index);
@@ -313,53 +347,60 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
     {
       uword help_at_end_of_line, i;
 
-      help_at_end_of_line = unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
+      help_at_end_of_line =
+       unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
       while (1)
-        {
+       {
          c = parent;
-          if (unformat_user (input, unformat_vlib_cli_sub_command, vm, c, &parent))
+         if (unformat_user
+             (input, unformat_vlib_cli_sub_command, vm, c, &parent))
            ;
 
-         else if (! unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
+         else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
            goto unknown;
 
          else
            break;
-        }
-      
+       }
+
       /* help SUB-COMMAND => long format help.
-        "help" at end of line: show all commands. */
-      if (! help_at_end_of_line)
-       vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c, /* is_long */ 1);
+         "help" at end of line: show all commands. */
+      if (!help_at_end_of_line)
+       vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
+                        /* is_long */ 1);
 
       else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
        vlib_cli_output (vm, "%v: no sub-commands", c->path);
 
       else
        {
-         vlib_cli_sub_command_t * sc;
-         vlib_cli_sub_rule_t * sr, * subs;
+         vlib_cli_sub_command_t *sc;
+         vlib_cli_sub_rule_t *sr, *subs;
 
          subs = vec_dup (c->sub_rules);
 
          /* Add in rules if any. */
          vec_foreach (sc, c->sub_commands)
-           {
-             vec_add2 (subs, sr, 1);
-             sr->name = sc->name;
-             sr->command_index = sc->index;
-             sr->rule_index = ~0;
-           }
+         {
+           vec_add2 (subs, sr, 1);
+           sr->name = sc->name;
+           sr->command_index = sc->index;
+           sr->rule_index = ~0;
+         }
 
-         vec_sort (subs, c1, c2, vec_cmp (c1->name, c2->name));
+         vec_sort_with_function (subs, vlib_cli_cmp_rule);
 
-         for (i = 0; i < vec_len (subs); i++) 
+         for (i = 0; i < vec_len (subs); i++)
            {
-             vlib_cli_command_t * d;
-             vlib_cli_parse_rule_t * r;
+             vlib_cli_command_t *d;
+             vlib_cli_parse_rule_t *r;
 
              d = vec_elt_at_index (cm->commands, subs[i].command_index);
-             r = subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules, subs[i].rule_index) : 0;
+             r =
+               subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
+                                                            subs
+                                                            [i].rule_index) :
+               0;
 
              if (r)
                vlib_cli_output
@@ -376,13 +417,14 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
          vec_free (subs);
        }
     }
-  
-  else if (is_main_dispatch && (unformat (input, "choices") || unformat (input, "?")))
+
+  else if (is_main_dispatch
+          && (unformat (input, "choices") || unformat (input, "?")))
     {
-      vlib_cli_command_t * sub, * subs;
+      vlib_cli_command_t *sub, *subs;
 
       subs = all_subs (cm, 0, parent_command_index);
-      vec_sort (subs, c1, c2, vec_cmp (c1->path, c2->path));
+      vec_sort_with_function (subs, vlib_cli_cmp_command);
       vec_foreach (sub, subs)
        vlib_cli_output (vm, "  %-40U %U",
                         format_vlib_cli_path, sub->path,
@@ -394,55 +436,59 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
     {
       vec_free (string);
     }
-  
+
   else if (unformat (input, "uncomment %U",
                     unformat_vlib_cli_sub_input, &sub_input))
     {
-      error = vlib_cli_dispatch_sub_commands (vm, cm, &sub_input, parent_command_index);
+      error =
+       vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
+                                       parent_command_index);
       unformat_free (&sub_input);
     }
-  
-  else if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
+
+  else
+    if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
     {
-      unformat_input_t * si;
-      uword has_sub_commands = vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
-      
+      unformat_input_t *si;
+      uword has_sub_commands =
+       vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
+
       si = input;
       if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
        si = &sub_input;
-      
+
       if (has_sub_commands)
        error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
 
-      if (has_sub_commands && ! error)
-       /* Found valid sub-command. */;
+      if (has_sub_commands && !error)
+       /* Found valid sub-command. */ ;
 
       else if (c->function)
        {
-         clib_error_t * c_error;
+         clib_error_t *c_error;
 
          /* Skip white space for benefit of called function. */
          unformat_skip_white_space (si);
 
          if (unformat (si, "?"))
            {
-             vlib_cli_output (vm, "  %-40U %U",
-                              format_vlib_cli_path, c->path,
-                              format_vlib_cli_command_help, c, /* is_long */ 0);
+             vlib_cli_output (vm, "  %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c,        /* is_long */
+                              0);
            }
          else
            {
-              if (!c->is_mp_safe)
-                vlib_worker_thread_barrier_sync(vm);
+             if (!c->is_mp_safe)
+               vlib_worker_thread_barrier_sync (vm);
 
              c_error = c->function (vm, si, c);
 
-              if (!c->is_mp_safe)
-                vlib_worker_thread_barrier_release(vm);
+             if (!c->is_mp_safe)
+               vlib_worker_thread_barrier_release (vm);
 
              if (c_error)
                {
-                 error = clib_error_return (0, "%v: %v", c->path, c_error->what);
+                 error =
+                   clib_error_return (0, "%v: %v", c->path, c_error->what);
                  clib_error_free (c_error);
                  /* Free sub input. */
                  if (si != input)
@@ -456,7 +502,7 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
          clib_error_free (error);
        }
 
-      else if (! error)
+      else if (!error)
        error = clib_error_return (0, "%v: no sub-commands", c->path);
 
       /* Free sub input. */
@@ -469,40 +515,49 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
 
   return error;
 
- unknown:
+unknown:
   if (parent->path)
-    return clib_error_return (0, "%v: unknown input `%U'", parent->path, format_unformat_error, input);
+    return clib_error_return (0, "%v: unknown input `%U'", parent->path,
+                             format_unformat_error, input);
   else
-    return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
+    return clib_error_return (0, "unknown input `%U'", format_unformat_error,
+                             input);
 }
 
 
-void vlib_unix_error_report (vlib_main_t *, clib_error_t *) 
-    __attribute__ ((weak));
+void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
+  __attribute__ ((weak));
 
-void vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error) { }
+void
+vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
+{
+}
 
 /* Process CLI input. */
-void vlib_cli_input (vlib_main_t * vm,
-                    unformat_input_t * input,
-                    vlib_cli_output_function_t * function,
-                    uword function_arg)
+void
+vlib_cli_input (vlib_main_t * vm,
+               unformat_input_t * input,
+               vlib_cli_output_function_t * function, uword function_arg)
 {
-  vlib_cli_main_t * cm = &vm->cli_main;
-  clib_error_t * error;
-  vlib_cli_output_function_t * save_function;
+  vlib_process_t *cp = vlib_get_current_process (vm);
+  vlib_cli_main_t *cm = &vm->cli_main;
+  clib_error_t *error;
+  vlib_cli_output_function_t *save_function;
   uword save_function_arg;
 
-  save_function = cm->output_function;
-  save_function_arg = cm->output_function_arg;
+  save_function = cp->output_function;
+  save_function_arg = cp->output_function_arg;
 
-  cm->output_function = function;
-  cm->output_function_arg = function_arg;
+  cp->output_function = function;
+  cp->output_function_arg = function_arg;
 
-  do {
-    vec_reset_length (cm->parse_rule_data);
-    error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */ 0);
-  } while (! error && ! unformat (input, "%U", unformat_eof));
+  do
+    {
+      vec_reset_length (cm->parse_rule_data);
+      error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,        /* parent */
+                                             0);
+    }
+  while (!error && !unformat (input, "%U", unformat_eof));
 
   if (error)
     {
@@ -511,77 +566,82 @@ void vlib_cli_input (vlib_main_t * vm,
       clib_error_free (error);
     }
 
-  cm->output_function = save_function;
-  cm->output_function_arg = save_function_arg;
+  cp->output_function = save_function;
+  cp->output_function_arg = save_function_arg;
 }
 
 /* Output to current CLI connection. */
-void vlib_cli_output (vlib_main_t * vm, char * fmt, ...)
+void
+vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
 {
-  vlib_cli_main_t * cm = &vm->cli_main;
+  vlib_process_t *cp = vlib_get_current_process (vm);
   va_list va;
-  u8 * s;
+  u8 *s;
 
   va_start (va, fmt);
   s = va_format (0, fmt, &va);
   va_end (va);
 
   /* Terminate with \n if not present. */
-  if (vec_len (s) > 0 && s[vec_len (s)-1] != '\n')
+  if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
     vec_add1 (s, '\n');
 
-  if (! cm->output_function)
+  if ((!cp) || (!cp->output_function))
     fformat (stdout, "%v", s);
   else
-    cm->output_function (cm->output_function_arg, s, vec_len (s));
+    cp->output_function (cp->output_function_arg, s, vec_len (s));
 
   vec_free (s);
 }
 
 static clib_error_t *
 show_memory_usage (vlib_main_t * vm,
-                  unformat_input_t * input,
-                  vlib_cli_command_t * cmd)
+                  unformat_input_t * input, vlib_cli_command_t * cmd)
 {
   int verbose = 0;
-  clib_error_t * error;
+  clib_error_t *error;
   u32 index = 0;
 
-  while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) 
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "verbose")) 
-        verbose = 1;
-      else {
-        error = clib_error_return (0, "unknown input `%U'", 
-                                   format_unformat_error, input);
-        return error;
-      }
-  }
+      if (unformat (input, "verbose"))
+       verbose = 1;
+      else
+       {
+         error = clib_error_return (0, "unknown input `%U'",
+                                    format_unformat_error, input);
+         return error;
+       }
+    }
 
+  /* *INDENT-OFF* */
   foreach_vlib_main (
   ({
       vlib_cli_output (vm, "Thread %d %v\n", index, vlib_worker_threads[index].name);
       vlib_cli_output (vm, "%U\n", format_mheap, clib_per_cpu_mheaps[index], verbose);
       index++;
   }));
+  /* *INDENT-ON* */
   return 0;
 }
 
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
   .path = "show memory",
   .short_help = "Show current memory usage",
   .function = show_memory_usage,
 };
+/* *INDENT-ON* */
 
 static clib_error_t *
 enable_disable_memory_trace (vlib_main_t * vm,
                             unformat_input_t * input,
                             vlib_cli_command_t * cmd)
 {
-  clib_error_t * error = 0;
+  clib_error_t *error = 0;
   int enable;
 
-  if (! unformat_user (input, unformat_vlib_enable_disable, &enable))
+  if (!unformat_user (input, unformat_vlib_enable_disable, &enable))
     {
       error = clib_error_return (0, "expecting enable/on or disable/off");
       goto done;
@@ -589,26 +649,30 @@ enable_disable_memory_trace (vlib_main_t * vm,
 
   clib_mem_trace (enable);
 
- done:
+done:
   return error;
 }
 
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
   .path = "memory-trace",
   .short_help = "Enable/disable memory allocation trace",
   .function = enable_disable_memory_trace,
 };
+/* *INDENT-ON* */
 
 
 static clib_error_t *
 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
-                        vlib_cli_command_t * cmd)
+                   vlib_cli_command_t * cmd)
 {
-    clib_error_t * error = 0;
-    void * heap;
-    mheap_t *mheap;
+  clib_error_t *error = 0;
+  void *heap;
+  mheap_t *mheap;
 
-    if (unformat(input, "on")) {
+  if (unformat (input, "on"))
+    {
+        /* *INDENT-OFF* */
         foreach_vlib_main({
           heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
           mheap = mheap_header(heap);
@@ -616,42 +680,87 @@ test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
           // Turn off small object cache because it delays detection of errors
           mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
         });
+        /* *INDENT-ON* */
 
-    } else if (unformat(input, "off")) {
+    }
+  else if (unformat (input, "off"))
+    {
+        /* *INDENT-OFF* */
         foreach_vlib_main({
           heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
           mheap = mheap_header(heap);
           mheap->flags &= ~MHEAP_FLAG_VALIDATE;
           mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
         });
-
-    } else if (unformat(input, "now")) {
+        /* *INDENT-ON* */
+    }
+  else if (unformat (input, "now"))
+    {
+        /* *INDENT-OFF* */
         foreach_vlib_main({
           heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
           mheap = mheap_header(heap);
           mheap_validate(heap);
         });
-        vlib_cli_output(vm, "heap validation complete");
+        /* *INDENT-ON* */
+      vlib_cli_output (vm, "heap validation complete");
 
-    } else {
-        return clib_error_return(0, "unknown input `%U'",
-                                  format_unformat_error, input);
+    }
+  else
+    {
+      return clib_error_return (0, "unknown input `%U'",
+                               format_unformat_error, input);
     }
 
-    return error;
+  return error;
 }
 
+/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
     .path = "test heap-validate",
     .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
     .function = test_heap_validate,
 };
+/* *INDENT-ON* */
+
+#ifdef TEST_CODE
+/*
+ * A trivial test harness to verify the per-process output_function
+ * is working correcty.
+ */
+
+static clib_error_t *
+sleep_ten_seconds (vlib_main_t * vm,
+                  unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  u16 i;
+  u16 my_id = rand ();
+
+  vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
+
+  for (i = 0; i < 10; i++)
+    {
+      vlib_process_wait_for_event_or_clock (vm, 1.0);
+      vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
+    }
+  vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
+  return 0;
+}
 
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (ping_command, static) = {
+  .path = "test sleep",
+  .function = sleep_ten_seconds,
+  .short_help = "Sleep for 10 seconds",
+};
+/* *INDENT-ON* */
+#endif /* ifdef TEST_CODE */
 
-static uword vlib_cli_normalize_path (char * input, char ** result)
+static uword
+vlib_cli_normalize_path (char *input, char **result)
 {
-  char * i = input;
-  char * s = 0;
+  char *i = input;
+  char *s = 0;
   uword l = 0;
   uword index_of_last_space = ~0;
 
@@ -665,7 +774,7 @@ static uword vlib_cli_normalize_path (char * input, char ** result)
        case '\t':
        case '\n':
        case '\r':
-         if (l > 0 && s[l-1] != ' ')
+         if (l > 0 && s[l - 1] != ' ')
            {
              vec_add1 (s, ' ');
              l++;
@@ -673,7 +782,7 @@ static uword vlib_cli_normalize_path (char * input, char ** result)
          break;
 
        default:
-         if (l > 0 && s[l-1] == ' ')
+         if (l > 0 && s[l - 1] == ' ')
            index_of_last_space = vec_len (s);
          vec_add1 (s, c);
          l++;
@@ -682,7 +791,7 @@ static uword vlib_cli_normalize_path (char * input, char ** result)
     }
 
   /* Remove any extra space at end. */
-  if (l > 0 && s[l-1] == ' ')
+  if (l > 0 && s[l - 1] == ' ')
     _vec_len (s) -= 1;
 
   *result = s;
@@ -690,7 +799,7 @@ static uword vlib_cli_normalize_path (char * input, char ** result)
 }
 
 always_inline uword
-parent_path_len (char * path)
+parent_path_len (char *path)
 {
   word i;
   for (i = vec_len (path) - 1; i >= 0; i--)
@@ -701,13 +810,12 @@ parent_path_len (char * path)
   return ~0;
 }
 
-static void add_sub_command (vlib_cli_main_t * cm,
-                            uword parent_index,
-                            uword child_index)
+static void
+add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
 {
-  vlib_cli_command_t * p, * c;
-  vlib_cli_sub_command_t * sub_c;
-  u8 * sub_name;
+  vlib_cli_command_t *p, *c;
+  vlib_cli_sub_command_t *sub_c;
+  u8 *sub_name;
   word i, l;
 
   p = vec_elt_at_index (cm->commands, parent_index);
@@ -725,17 +833,16 @@ static void add_sub_command (vlib_cli_main_t * cm,
 
   if (sub_name[0] == '%')
     {
-      uword * q;
-      vlib_cli_sub_rule_t * sr;
+      uword *q;
+      vlib_cli_sub_rule_t *sr;
 
       /* Remove %. */
       vec_delete (sub_name, 1, 0);
 
-      if (! p->sub_rule_index_by_name)
-       p->sub_rule_index_by_name
-         = hash_create_vec (/* initial length */ 32,
-                            sizeof (sub_name[0]),
-                            sizeof (uword));
+      if (!p->sub_rule_index_by_name)
+       p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
+                                                    sizeof (sub_name[0]),
+                                                    sizeof (uword));
       q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
       if (q)
        {
@@ -745,11 +852,12 @@ static void add_sub_command (vlib_cli_main_t * cm,
        }
 
       q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
-      if (! q)
+      if (!q)
        clib_error ("reference to unknown rule `%%%v' in path `%v'",
                    sub_name, c->path);
 
-      hash_set_mem (p->sub_rule_index_by_name, sub_name, vec_len (p->sub_rules));
+      hash_set_mem (p->sub_rule_index_by_name, sub_name,
+                   vec_len (p->sub_rules));
       vec_add2 (p->sub_rules, sr, 1);
       sr->name = sub_name;
       sr->rule_index = q[0];
@@ -757,11 +865,10 @@ static void add_sub_command (vlib_cli_main_t * cm,
       return;
     }
 
-  if (! p->sub_command_index_by_name)
-    p->sub_command_index_by_name
-      = hash_create_vec (/* initial length */ 32,
-                        sizeof (c->path[0]),
-                        sizeof (uword));
+  if (!p->sub_command_index_by_name)
+    p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
+                                                   sizeof (c->path[0]),
+                                                   sizeof (uword));
 
   /* Check if sub-command has already been created. */
   if (hash_get_mem (p->sub_command_index_by_name, sub_name))
@@ -773,17 +880,18 @@ static void add_sub_command (vlib_cli_main_t * cm,
   vec_add2 (p->sub_commands, sub_c, 1);
   sub_c->index = child_index;
   sub_c->name = sub_name;
-  hash_set_mem (p->sub_command_index_by_name, sub_c->name, sub_c - p->sub_commands);
+  hash_set_mem (p->sub_command_index_by_name, sub_c->name,
+               sub_c - p->sub_commands);
 
   vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
   for (i = 0; i < vec_len (sub_c->name); i++)
     {
       int n;
-      vlib_cli_parse_position_t * pos;
+      vlib_cli_parse_position_t *pos;
 
       pos = vec_elt_at_index (p->sub_command_positions, i);
 
-      if (! pos->bitmaps)
+      if (!pos->bitmaps)
        pos->min_char = sub_c->name[i];
 
       n = sub_c->name[i] - pos->min_char;
@@ -795,16 +903,17 @@ static void add_sub_command (vlib_cli_main_t * cm,
        }
 
       vec_validate (pos->bitmaps, n);
-      pos->bitmaps[n] = clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
+      pos->bitmaps[n] =
+       clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
     }
 }
 
 static void
 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
 {
-  uword p_len, pi, * p;
-  char * p_path;
-  vlib_cli_command_t * c, * parent;
+  uword p_len, pi, *p;
+  char *p_path;
+  vlib_cli_command_t *c, *parent;
 
   /* Root command (index 0) should have already been added. */
   ASSERT (vec_len (cm->commands) > 0);
@@ -812,7 +921,7 @@ vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
   c = vec_elt_at_index (cm->commands, ci);
   p_len = parent_path_len (c->path);
 
-  /* No space?  Parent is root command. */ 
+  /* No space?  Parent is root command. */
   if (p_len == ~0)
     {
       add_sub_command (cm, 0, ci);
@@ -825,12 +934,13 @@ vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
   p = hash_get_mem (cm->command_index_by_path, p_path);
 
   /* Parent exists? */
-  if (! p)
+  if (!p)
     {
       /* Parent does not exist; create it. */
       vec_add2 (cm->commands, parent, 1);
       parent->path = p_path;
-      hash_set_mem (cm->command_index_by_path, parent->path, parent - cm->commands);
+      hash_set_mem (cm->command_index_by_path, parent->path,
+                   parent - cm->commands);
       pi = parent - cm->commands;
     }
   else
@@ -842,32 +952,31 @@ vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
   add_sub_command (cm, pi, ci);
 
   /* Create parent's parent. */
-  if (! p)
+  if (!p)
     vlib_cli_make_parent (cm, pi);
 }
 
 always_inline uword
 vlib_cli_command_is_empty (vlib_cli_command_t * c)
 {
-  return (c->long_help == 0
-         && c->short_help == 0
-         && c->function == 0);
+  return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
 }
 
-clib_error_t * vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
+clib_error_t *
+vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
 {
-  vlib_cli_main_t * cm = &vm->cli_main;
-  clib_error_t * error = 0;
-  uword ci, * p;
-  char * normalized_path;
+  vlib_cli_main_t *cm = &vm->cli_main;
+  clib_error_t *error = 0;
+  uword ci, *p;
+  char *normalized_path;
 
   if ((error = vlib_call_init_function (vm, vlib_cli_init)))
     return error;
 
   (void) vlib_cli_normalize_path (c->path, &normalized_path);
 
-  if (! cm->command_index_by_path)
-    cm->command_index_by_path = hash_create_vec (/* initial length */ 32,
+  if (!cm->command_index_by_path)
+    cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
                                                 sizeof (c->path[0]),
                                                 sizeof (uword));
 
@@ -875,18 +984,18 @@ clib_error_t * vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
   p = hash_get_mem (cm->command_index_by_path, normalized_path);
   if (p)
     {
-      vlib_cli_command_t * d;
+      vlib_cli_command_t *d;
 
       ci = p[0];
       d = vec_elt_at_index (cm->commands, ci);
 
       /* If existing command was created via vlib_cli_make_parent
-        replaced it with callers data. */
+         replaced it with callers data. */
       if (vlib_cli_command_is_empty (d))
        {
          vlib_cli_command_t save = d[0];
 
-         ASSERT (! vlib_cli_command_is_empty (c));
+         ASSERT (!vlib_cli_command_is_empty (c));
 
          /* Copy callers fields. */
          d[0] = c[0];
@@ -899,7 +1008,9 @@ clib_error_t * vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
          d->sub_rules = save.sub_rules;
        }
       else
-       error = clib_error_return (0, "duplicate command name with path %v", normalized_path);
+       error =
+         clib_error_return (0, "duplicate command name with path %v",
+                            normalized_path);
 
       vec_free (normalized_path);
       if (error)
@@ -936,14 +1047,14 @@ clib_error_t * vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
 clib_error_t *
 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
 {
-  vlib_cli_main_t * cm = &vm->cli_main;
-  vlib_cli_parse_rule_t * r;
-  clib_error_t * error = 0;
-  u8 * r_name;
-  uword * p;
-
-  if (! cm->parse_rule_index_by_name)
-    cm->parse_rule_index_by_name = hash_create_vec (/* initial length */ 32,
+  vlib_cli_main_t *cm = &vm->cli_main;
+  vlib_cli_parse_rule_t *r;
+  clib_error_t *error = 0;
+  u8 *r_name;
+  uword *p;
+
+  if (!cm->parse_rule_index_by_name)
+    cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
                                                    sizeof (r->name[0]),
                                                    sizeof (uword));
 
@@ -953,7 +1064,8 @@ vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
   if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
     {
       vec_free (r_name);
-      return clib_error_return (0, "duplicate parse rule name `%s'", r_reg->name);
+      return clib_error_return (0, "duplicate parse rule name `%s'",
+                               r_reg->name);
     }
 
   vec_add2 (cm->parse_rules, r, 1);
@@ -966,19 +1078,19 @@ vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
 
 #if 0
 /* $$$ turn back on again someday, maybe */
-static clib_error_t *
-vlib_cli_register_parse_rules (vlib_main_t * vm,
-                              vlib_cli_parse_rule_t * lo,
-                              vlib_cli_parse_rule_t * hi)
-
-    __attribute__((unused))
+static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
+                                                   vlib_cli_parse_rule_t *
+                                                   lo,
+                                                   vlib_cli_parse_rule_t *
+                                                   hi)
+  __attribute__ ((unused))
 {
-  clib_error_t * error = 0;
-  vlib_cli_parse_rule_t * r;
+  clib_error_t *error = 0;
+  vlib_cli_parse_rule_t *r;
 
   for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
     {
-      if (! r->name || strlen (r->name) == 0)
+      if (!r->name || strlen (r->name) == 0)
        {
          error = clib_error_return (0, "parse rule with no name");
          goto done;
@@ -989,16 +1101,17 @@ vlib_cli_register_parse_rules (vlib_main_t * vm,
        goto done;
     }
 
- done:
+done:
   return error;
 }
 #endif
 
-static clib_error_t * vlib_cli_init (vlib_main_t * vm)
+static clib_error_t *
+vlib_cli_init (vlib_main_t * vm)
 {
-  vlib_cli_main_t * cm = &vm->cli_main;
-  clib_error_t * error = 0;
-  vlib_cli_command_t * cmd;
+  vlib_cli_main_t *cm = &vm->cli_main;
+  clib_error_t *error = 0;
+  vlib_cli_command_t *cmd;
 
   cmd = cm->cli_command_registrations;
 
@@ -1006,10 +1119,18 @@ static clib_error_t * vlib_cli_init (vlib_main_t * vm)
     {
       error = vlib_cli_register (vm, cmd);
       if (error)
-        return error;
+       return error;
       cmd = cmd->next_cli_command;
     }
   return error;
 }
 
 VLIB_INIT_FUNCTION (vlib_cli_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */