d7b2a4686d1cae581c131061cb6cb86dc1305450
[vpp.git] / src / vlib / cli.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * cli.c: command line interface
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 #include <vppinfra/cpu.h>
43 #include <vppinfra/elog.h>
44 #include <unistd.h>
45 #include <ctype.h>
46
47 /* Root of all show commands. */
48 /* *INDENT-OFF* */
49 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
50   .path = "show",
51   .short_help = "Show commands",
52 };
53 /* *INDENT-ON* */
54
55 /* Root of all clear commands. */
56 /* *INDENT-OFF* */
57 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
58   .path = "clear",
59   .short_help = "Clear commands",
60 };
61 /* *INDENT-ON* */
62
63 /* Root of all set commands. */
64 /* *INDENT-OFF* */
65 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
66   .path = "set",
67   .short_help = "Set commands",
68 };
69 /* *INDENT-ON* */
70
71 /* Root of all test commands. */
72 /* *INDENT-OFF* */
73 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
74   .path = "test",
75   .short_help = "Test commands",
76 };
77 /* *INDENT-ON* */
78
79 /* Returns bitmap of commands which match key. */
80 static uword *
81 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
82 {
83   int i, n;
84   uword *match = 0;
85   vlib_cli_parse_position_t *p;
86
87   unformat_skip_white_space (input);
88
89   for (i = 0;; i++)
90     {
91       uword k;
92
93       k = unformat_get_input (input);
94       switch (k)
95         {
96         case 'a' ... 'z':
97         case 'A' ... 'Z':
98         case '0' ... '9':
99         case '-':
100         case '_':
101           break;
102
103         case ' ':
104         case '\t':
105         case '\r':
106         case '\n':
107         case UNFORMAT_END_OF_INPUT:
108           /* White space or end of input removes any non-white
109              matches that were before possible. */
110           if (i < vec_len (c->sub_command_positions)
111               && clib_bitmap_count_set_bits (match) > 1)
112             {
113               p = vec_elt_at_index (c->sub_command_positions, i);
114               for (n = 0; n < vec_len (p->bitmaps); n++)
115                 match = clib_bitmap_andnot (match, p->bitmaps[n]);
116             }
117           goto done;
118
119         default:
120           unformat_put_input (input);
121           goto done;
122         }
123
124       if (i >= vec_len (c->sub_command_positions))
125         {
126         no_match:
127           clib_bitmap_free (match);
128           return 0;
129         }
130
131       p = vec_elt_at_index (c->sub_command_positions, i);
132       if (vec_len (p->bitmaps) == 0)
133         goto no_match;
134
135       n = k - p->min_char;
136       if (n < 0 || n >= vec_len (p->bitmaps))
137         goto no_match;
138
139       if (i == 0)
140         match = clib_bitmap_dup (p->bitmaps[n]);
141       else
142         match = clib_bitmap_and (match, p->bitmaps[n]);
143
144       if (clib_bitmap_is_zero (match))
145         goto no_match;
146     }
147
148 done:
149   return match;
150 }
151
152 /* Looks for string based sub-input formatted { SUB-INPUT }. */
153 uword
154 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
155 {
156   unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
157   u8 *s;
158   uword c;
159
160   while (1)
161     {
162       c = unformat_get_input (i);
163       switch (c)
164         {
165         case ' ':
166         case '\t':
167         case '\n':
168         case '\r':
169         case '\f':
170           break;
171
172         case '{':
173         default:
174           /* Put back paren. */
175           if (c != UNFORMAT_END_OF_INPUT)
176             unformat_put_input (i);
177
178           if (c == '{' && unformat (i, "%v", &s))
179             {
180               unformat_init_vector (sub_input, s);
181               return 1;
182             }
183           return 0;
184         }
185     }
186   return 0;
187 }
188
189 static vlib_cli_command_t *
190 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
191 {
192   vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
193   return vec_elt_at_index (cm->commands, s->index);
194 }
195
196 static uword
197 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
198 {
199   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
200   vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
201   vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
202   vlib_cli_main_t *cm = &vm->cli_main;
203   uword *match_bitmap, is_unique, index;
204
205   {
206     vlib_cli_sub_rule_t *sr;
207     vlib_cli_parse_rule_t *r;
208     vec_foreach (sr, c->sub_rules)
209     {
210       void **d;
211       r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
212       vec_add2 (cm->parse_rule_data, d, 1);
213       vec_reset_length (d[0]);
214       if (r->data_size)
215         d[0] = _vec_resize (d[0],
216                             /* length increment */ 1,
217                             r->data_size,
218                             /* header_bytes */ 0,
219                             /* data align */ sizeof (uword));
220       if (unformat_user (i, r->unformat_function, vm, d[0]))
221         {
222           *result = vec_elt_at_index (cm->commands, sr->command_index);
223           return 1;
224         }
225     }
226   }
227
228   match_bitmap = vlib_cli_sub_command_match (c, i);
229   is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
230   index = ~0;
231   if (is_unique)
232     {
233       index = clib_bitmap_first_set (match_bitmap);
234       *result = get_sub_command (cm, c, index);
235     }
236   clib_bitmap_free (match_bitmap);
237
238   return is_unique;
239 }
240
241 static int
242 vlib_cli_cmp_strings (void *a1, void *a2)
243 {
244   u8 *c1 = *(u8 **) a1;
245   u8 *c2 = *(u8 **) a2;
246
247   return vec_cmp (c1, c2);
248 }
249
250 u8 **
251 vlib_cli_get_possible_completions (u8 * str)
252 {
253   vlib_cli_command_t *c;
254   vlib_cli_sub_command_t *sc;
255   vlib_main_t *vm = vlib_get_main ();
256   vlib_cli_main_t *vcm = &vm->cli_main;
257   uword *match_bitmap = 0;
258   uword index, is_unique, help_next_level;
259   u8 **result = 0;
260   unformat_input_t input;
261   unformat_init_vector (&input, vec_dup (str));
262   c = vec_elt_at_index (vcm->commands, 0);
263
264   /* remove trailing whitespace, except for one of them */
265   while (vec_len (input.buffer) >= 2 &&
266          isspace (input.buffer[vec_len (input.buffer) - 1]) &&
267          isspace (input.buffer[vec_len (input.buffer) - 2]))
268     {
269       vec_del1 (input.buffer, vec_len (input.buffer) - 1);
270     }
271
272   /* if input is empty, directly return list of root commands */
273   if (vec_len (input.buffer) == 0 ||
274       (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
275     {
276       vec_foreach (sc, c->sub_commands)
277       {
278         vec_add1 (result, (u8 *) sc->name);
279       }
280       goto done;
281     }
282
283   /* add a trailing '?' so that vlib_cli_sub_command_match can find
284    * all commands starting with the input string */
285   vec_add1 (input.buffer, '?');
286
287   while (1)
288     {
289       match_bitmap = vlib_cli_sub_command_match (c, &input);
290       /* no match: return no result */
291       if (match_bitmap == 0)
292         {
293           goto done;
294         }
295       is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
296       /* unique match: try to step one subcommand level further */
297       if (is_unique)
298         {
299           /* stop if no more input */
300           if (input.index >= vec_len (input.buffer) - 1)
301             {
302               break;
303             }
304
305           index = clib_bitmap_first_set (match_bitmap);
306           c = get_sub_command (vcm, c, index);
307           clib_bitmap_free (match_bitmap);
308           continue;
309         }
310       /* multiple matches: stop here, return all matches */
311       break;
312     }
313
314   /* remove trailing '?' */
315   vec_del1 (input.buffer, vec_len (input.buffer) - 1);
316
317   /* if we have a space at the end of input, and a unique match,
318    * autocomplete the next level of subcommands */
319   help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
320   /* *INDENT-OFF* */
321   clib_bitmap_foreach(index, match_bitmap, {
322     if (help_next_level && is_unique) {
323         c = get_sub_command (vcm, c, index);
324         vec_foreach (sc, c->sub_commands) {
325           vec_add1 (result, (u8*) sc->name);
326         }
327         goto done; /* break doesn't work in this macro-loop */
328     }
329     sc = &c->sub_commands[index];
330     vec_add1(result, (u8*) sc->name);
331   });
332   /* *INDENT-ON* */
333
334 done:
335   clib_bitmap_free (match_bitmap);
336   unformat_free (&input);
337
338   if (result)
339     vec_sort_with_function (result, vlib_cli_cmp_strings);
340   return result;
341 }
342
343 static u8 *
344 format_vlib_cli_command_help (u8 * s, va_list * args)
345 {
346   vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
347   int is_long = va_arg (*args, int);
348   if (is_long && c->long_help)
349     s = format (s, "%s", c->long_help);
350   else if (c->short_help)
351     s = format (s, "%s", c->short_help);
352   else
353     s = format (s, "%v commands", c->path);
354   return s;
355 }
356
357 static u8 *
358 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
359 {
360   vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
361   return format (s, "<%U>", format_c_identifier, r->name);
362 }
363
364 static u8 *
365 format_vlib_cli_path (u8 * s, va_list * args)
366 {
367   u8 *path = va_arg (*args, u8 *);
368   int i, in_rule;
369   in_rule = 0;
370   for (i = 0; i < vec_len (path); i++)
371     {
372       switch (path[i])
373         {
374         case '%':
375           in_rule = 1;
376           vec_add1 (s, '<');    /* start of <RULE> */
377           break;
378
379         case '_':
380           /* _ -> space in rules. */
381           vec_add1 (s, in_rule ? ' ' : '_');
382           break;
383
384         case ' ':
385           if (in_rule)
386             {
387               vec_add1 (s, '>');        /* end of <RULE> */
388               in_rule = 0;
389             }
390           vec_add1 (s, ' ');
391           break;
392
393         default:
394           vec_add1 (s, path[i]);
395           break;
396         }
397     }
398
399   if (in_rule)
400     vec_add1 (s, '>');          /* terminate <RULE> */
401
402   return s;
403 }
404
405 static vlib_cli_command_t *
406 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
407 {
408   vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
409   vlib_cli_sub_command_t *sc;
410   vlib_cli_sub_rule_t *sr;
411
412   if (c->function)
413     vec_add1 (subs, c[0]);
414
415   vec_foreach (sr, c->sub_rules)
416     subs = all_subs (cm, subs, sr->command_index);
417   vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
418
419   return subs;
420 }
421
422 static int
423 vlib_cli_cmp_rule (void *a1, void *a2)
424 {
425   vlib_cli_sub_rule_t *r1 = a1;
426   vlib_cli_sub_rule_t *r2 = a2;
427
428   return vec_cmp (r1->name, r2->name);
429 }
430
431 static int
432 vlib_cli_cmp_command (void *a1, void *a2)
433 {
434   vlib_cli_command_t *c1 = a1;
435   vlib_cli_command_t *c2 = a2;
436
437   return vec_cmp (c1->path, c2->path);
438 }
439
440 static clib_error_t *
441 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
442                                 vlib_cli_main_t * cm,
443                                 unformat_input_t * input,
444                                 uword parent_command_index)
445 {
446   vlib_cli_command_t *parent, *c;
447   clib_error_t *error = 0;
448   unformat_input_t sub_input;
449   u8 *string;
450   uword is_main_dispatch = cm == &vm->cli_main;
451
452   parent = vec_elt_at_index (cm->commands, parent_command_index);
453   if (is_main_dispatch && unformat (input, "help"))
454     {
455       uword help_at_end_of_line, i;
456
457       help_at_end_of_line =
458         unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
459       while (1)
460         {
461           c = parent;
462           if (unformat_user
463               (input, unformat_vlib_cli_sub_command, vm, c, &parent))
464             ;
465
466           else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
467             goto unknown;
468
469           else
470             break;
471         }
472
473       /* help SUB-COMMAND => long format help.
474          "help" at end of line: show all commands. */
475       if (!help_at_end_of_line)
476         vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
477                          /* is_long */ 1);
478
479       else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
480         vlib_cli_output (vm, "%v: no sub-commands", c->path);
481
482       else
483         {
484           vlib_cli_sub_command_t *sc;
485           vlib_cli_sub_rule_t *sr, *subs;
486
487           subs = vec_dup (c->sub_rules);
488
489           /* Add in rules if any. */
490           vec_foreach (sc, c->sub_commands)
491           {
492             vec_add2 (subs, sr, 1);
493             sr->name = sc->name;
494             sr->command_index = sc->index;
495             sr->rule_index = ~0;
496           }
497
498           vec_sort_with_function (subs, vlib_cli_cmp_rule);
499
500           for (i = 0; i < vec_len (subs); i++)
501             {
502               vlib_cli_command_t *d;
503               vlib_cli_parse_rule_t *r;
504
505               d = vec_elt_at_index (cm->commands, subs[i].command_index);
506               r =
507                 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
508                                                              subs
509                                                              [i].rule_index) :
510                 0;
511
512               if (r)
513                 vlib_cli_output
514                   (vm, "  %-30U %U",
515                    format_vlib_cli_parse_rule_name, r,
516                    format_vlib_cli_command_help, d, /* is_long */ 0);
517               else
518                 vlib_cli_output
519                   (vm, "  %-30v %U",
520                    subs[i].name,
521                    format_vlib_cli_command_help, d, /* is_long */ 0);
522             }
523
524           vec_free (subs);
525         }
526     }
527
528   else if (is_main_dispatch
529            && (unformat (input, "choices") || unformat (input, "?")))
530     {
531       vlib_cli_command_t *sub, *subs;
532
533       subs = all_subs (cm, 0, parent_command_index);
534       vec_sort_with_function (subs, vlib_cli_cmp_command);
535       vec_foreach (sub, subs)
536         vlib_cli_output (vm, "  %-40U %U",
537                          format_vlib_cli_path, sub->path,
538                          format_vlib_cli_command_help, sub, /* is_long */ 0);
539       vec_free (subs);
540     }
541
542   else if (unformat (input, "comment %v", &string))
543     {
544       vec_free (string);
545     }
546
547   else if (unformat (input, "uncomment %U",
548                      unformat_vlib_cli_sub_input, &sub_input))
549     {
550       error =
551         vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
552                                         parent_command_index);
553       unformat_free (&sub_input);
554     }
555
556   else
557     if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
558     {
559       unformat_input_t *si;
560       uword has_sub_commands =
561         vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
562
563       si = input;
564       if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
565         si = &sub_input;
566
567       if (has_sub_commands)
568         error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
569
570       if (has_sub_commands && !error)
571         /* Found valid sub-command. */ ;
572
573       else if (c->function)
574         {
575           clib_error_t *c_error;
576
577           /* Skip white space for benefit of called function. */
578           unformat_skip_white_space (si);
579
580           if (unformat (si, "?"))
581             {
582               vlib_cli_output (vm, "  %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c,        /* is_long */
583                                0);
584             }
585           else
586             {
587               if (PREDICT_FALSE (vm->elog_trace_cli_commands))
588                 {
589                   /* *INDENT-OFF* */
590                   ELOG_TYPE_DECLARE (e) =
591                     {
592                       .format = "cli-cmd: %s",
593                       .format_args = "T4",
594                     };
595                   /* *INDENT-ON* */
596                   struct
597                   {
598                     u32 c;
599                   } *ed;
600                   ed = ELOG_DATA (&vm->elog_main, e);
601                   ed->c = elog_global_id_for_msg_name (c->path);
602                 }
603
604               if (!c->is_mp_safe)
605                 vlib_worker_thread_barrier_sync (vm);
606
607               c_error = c->function (vm, si, c);
608
609               if (!c->is_mp_safe)
610                 vlib_worker_thread_barrier_release (vm);
611
612               if (PREDICT_FALSE (vm->elog_trace_cli_commands))
613                 {
614                   /* *INDENT-OFF* */
615                   ELOG_TYPE_DECLARE (e) =
616                     {
617                       .format = "cli-cmd: %s %s",
618                       .format_args = "T4T4",
619                     };
620                   /* *INDENT-ON* */
621                   struct
622                   {
623                     u32 c, err;
624                   } *ed;
625                   ed = ELOG_DATA (&vm->elog_main, e);
626                   ed->c = elog_global_id_for_msg_name (c->path);
627                   if (c_error)
628                     {
629                       vec_add1 (c_error->what, 0);
630                       ed->err = elog_global_id_for_msg_name
631                         ((const char *) c_error->what);
632                       _vec_len (c_error->what) -= 1;
633                     }
634                   else
635                     ed->err = elog_global_id_for_msg_name ("OK");
636                 }
637
638               if (c_error)
639                 {
640                   error =
641                     clib_error_return (0, "%v: %v", c->path, c_error->what);
642                   clib_error_free (c_error);
643                   /* Free sub input. */
644                   if (si != input)
645                     unformat_free (si);
646
647                   return error;
648                 }
649             }
650
651           /* Free any previous error. */
652           clib_error_free (error);
653         }
654
655       else if (!error)
656         error = clib_error_return (0, "%v: no sub-commands", c->path);
657
658       /* Free sub input. */
659       if (si != input)
660         unformat_free (si);
661     }
662
663   else
664     goto unknown;
665
666   return error;
667
668 unknown:
669   if (parent->path)
670     return clib_error_return (0, "%v: unknown input `%U'", parent->path,
671                               format_unformat_error, input);
672   else
673     return clib_error_return (0, "unknown input `%U'", format_unformat_error,
674                               input);
675 }
676
677
678 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
679   __attribute__ ((weak));
680
681 void
682 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
683 {
684 }
685
686 /* Process CLI input. */
687 void
688 vlib_cli_input (vlib_main_t * vm,
689                 unformat_input_t * input,
690                 vlib_cli_output_function_t * function, uword function_arg)
691 {
692   vlib_process_t *cp = vlib_get_current_process (vm);
693   vlib_cli_main_t *cm = &vm->cli_main;
694   clib_error_t *error;
695   vlib_cli_output_function_t *save_function;
696   uword save_function_arg;
697
698   save_function = cp->output_function;
699   save_function_arg = cp->output_function_arg;
700
701   cp->output_function = function;
702   cp->output_function_arg = function_arg;
703
704   do
705     {
706       vec_reset_length (cm->parse_rule_data);
707       error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
708                                               0);
709     }
710   while (!error && !unformat (input, "%U", unformat_eof));
711
712   if (error)
713     {
714       vlib_cli_output (vm, "%v", error->what);
715       vlib_unix_error_report (vm, error);
716       clib_error_free (error);
717     }
718
719   cp->output_function = save_function;
720   cp->output_function_arg = save_function_arg;
721 }
722
723 /* Output to current CLI connection. */
724 void
725 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
726 {
727   vlib_process_t *cp = vlib_get_current_process (vm);
728   va_list va;
729   u8 *s;
730
731   va_start (va, fmt);
732   s = va_format (0, fmt, &va);
733   va_end (va);
734
735   /* Terminate with \n if not present. */
736   if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
737     vec_add1 (s, '\n');
738
739   if ((!cp) || (!cp->output_function))
740     fformat (stdout, "%v", s);
741   else
742     cp->output_function (cp->output_function_arg, s, vec_len (s));
743
744   vec_free (s);
745 }
746
747 void *vl_msg_push_heap (void) __attribute__ ((weak));
748 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
749
750 static clib_error_t *
751 show_memory_usage (vlib_main_t * vm,
752                    unformat_input_t * input, vlib_cli_command_t * cmd)
753 {
754   int verbose __attribute__ ((unused)) = 0, api_segment = 0;
755   clib_error_t *error;
756   u32 index = 0;
757
758   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
759     {
760       if (unformat (input, "verbose"))
761         verbose = 1;
762       else if (unformat (input, "api-segment"))
763         api_segment = 1;
764       else
765         {
766           error = clib_error_return (0, "unknown input `%U'",
767                                      format_unformat_error, input);
768           return error;
769         }
770     }
771
772   if (api_segment)
773     {
774       void *oldheap = vl_msg_push_heap ();
775       u8 *s_in_svm =
776         format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
777       vl_msg_pop_heap (oldheap);
778       u8 *s = vec_dup (s_in_svm);
779
780       oldheap = vl_msg_push_heap ();
781       vec_free (s_in_svm);
782       vl_msg_pop_heap (oldheap);
783       vlib_cli_output (vm, "API segment start:");
784       vlib_cli_output (vm, "%v", s);
785       vlib_cli_output (vm, "API segment end:");
786       vec_free (s);
787     }
788
789 #if USE_DLMALLOC == 0
790   /* *INDENT-OFF* */
791   foreach_vlib_main (
792   ({
793       mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
794       vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
795                        vlib_worker_threads[index].name);
796       vlib_cli_output (vm, "  %U\n", format_page_map, pointer_to_uword (h) -
797                        h->vm_alloc_offset_from_header,
798                        h->vm_alloc_size);
799       vlib_cli_output (vm, "  %U\n", format_mheap, clib_per_cpu_mheaps[index],
800                        verbose);
801       index++;
802   }));
803   /* *INDENT-ON* */
804 #else
805   {
806     uword clib_mem_trace_enable_disable (uword enable);
807     uword was_enabled;
808
809     /*
810      * Note: the foreach_vlib_main cause allocator traffic,
811      * so shut off tracing before we go there...
812      */
813     was_enabled = clib_mem_trace_enable_disable (0);
814
815     /* *INDENT-OFF* */
816     foreach_vlib_main (
817     ({
818       struct dlmallinfo mi;
819       void *mspace;
820       mspace = clib_per_cpu_mheaps[index];
821
822       mi = mspace_mallinfo (mspace);
823       vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
824                        vlib_worker_threads[index].name);
825       vlib_cli_output (vm, "  %U\n", format_page_map,
826                        pointer_to_uword (mspace_least_addr(mspace)),
827                        mi.arena);
828       vlib_cli_output (vm, "  %U\n", format_mheap, clib_per_cpu_mheaps[index],
829                        verbose);
830       index++;
831     }));
832     /* *INDENT-ON* */
833
834     /* Restore the trace flag */
835     clib_mem_trace_enable_disable (was_enabled);
836   }
837 #endif /* USE_DLMALLOC */
838   return 0;
839 }
840
841 /* *INDENT-OFF* */
842 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
843   .path = "show memory",
844   .short_help = "[verbose | api-segment] Show current memory usage",
845   .function = show_memory_usage,
846 };
847 /* *INDENT-ON* */
848
849 static clib_error_t *
850 show_cpu (vlib_main_t * vm, unformat_input_t * input,
851           vlib_cli_command_t * cmd)
852 {
853 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
854   _("Model name", "%U", format_cpu_model_name);
855   _("Microarchitecture", "%U", format_cpu_uarch);
856   _("Flags", "%U", format_cpu_flags);
857   _("Base frequency", "%.2f GHz",
858     ((f64) vm->clib_time.clocks_per_second) * 1e-9);
859 #undef _
860   return 0;
861 }
862
863 /*?
864  * Displays various information about the CPU.
865  *
866  * @cliexpar
867  * @cliexstart{show cpu}
868  * Model name:               Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
869  * Microarchitecture:        Broadwell (Broadwell-EP/EX)
870  * Flags:                    sse3 ssse3 sse41 sse42 avx avx2 aes
871  * Base Frequency:           3.20 GHz
872  * @cliexend
873 ?*/
874 /* *INDENT-OFF* */
875 VLIB_CLI_COMMAND (show_cpu_command, static) = {
876   .path = "show cpu",
877   .short_help = "Show cpu information",
878   .function = show_cpu,
879 };
880
881 /* *INDENT-ON* */
882
883 static clib_error_t *
884 enable_disable_memory_trace (vlib_main_t * vm,
885                              unformat_input_t * input,
886                              vlib_cli_command_t * cmd)
887 {
888   unformat_input_t _line_input, *line_input = &_line_input;
889   int enable;
890   int api_segment = 0;
891   void *oldheap;
892
893
894   if (!unformat_user (input, unformat_line_input, line_input))
895     return 0;
896
897   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
898     {
899       if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
900         ;
901       else if (unformat (line_input, "api-segment"))
902         api_segment = 1;
903       else
904         {
905           unformat_free (line_input);
906           return clib_error_return (0, "invalid input");
907         }
908     }
909   unformat_free (line_input);
910
911   if (api_segment)
912     oldheap = vl_msg_push_heap ();
913   clib_mem_trace (enable);
914   if (api_segment)
915     vl_msg_pop_heap (oldheap);
916
917   return 0;
918 }
919
920 /* *INDENT-OFF* */
921 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
922   .path = "memory-trace",
923   .short_help = "on|off [api-segment] Enable/disable memory allocation trace",
924   .function = enable_disable_memory_trace,
925 };
926 /* *INDENT-ON* */
927
928
929 static clib_error_t *
930 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
931                     vlib_cli_command_t * cmd)
932 {
933 #if USE_DLMALLOC == 0
934   clib_error_t *error = 0;
935   void *heap;
936   mheap_t *mheap;
937
938   if (unformat (input, "on"))
939     {
940         /* *INDENT-OFF* */
941         foreach_vlib_main({
942           heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
943           mheap = mheap_header(heap);
944           mheap->flags |= MHEAP_FLAG_VALIDATE;
945           // Turn off small object cache because it delays detection of errors
946           mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
947         });
948         /* *INDENT-ON* */
949
950     }
951   else if (unformat (input, "off"))
952     {
953         /* *INDENT-OFF* */
954         foreach_vlib_main({
955           heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
956           mheap = mheap_header(heap);
957           mheap->flags &= ~MHEAP_FLAG_VALIDATE;
958           mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
959         });
960         /* *INDENT-ON* */
961     }
962   else if (unformat (input, "now"))
963     {
964         /* *INDENT-OFF* */
965         foreach_vlib_main({
966           heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
967           mheap = mheap_header(heap);
968           mheap_validate(heap);
969         });
970         /* *INDENT-ON* */
971       vlib_cli_output (vm, "heap validation complete");
972
973     }
974   else
975     {
976       return clib_error_return (0, "unknown input `%U'",
977                                 format_unformat_error, input);
978     }
979
980   return error;
981 #else
982   return clib_error_return (0, "unimplemented...");
983 #endif /* USE_DLMALLOC */
984 }
985
986 /* *INDENT-OFF* */
987 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
988     .path = "test heap-validate",
989     .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
990     .function = test_heap_validate,
991 };
992 /* *INDENT-ON* */
993
994 static clib_error_t *
995 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
996                 vlib_cli_command_t * cmd)
997 {
998   clib_file_main_t *fm = &file_main;
999   clib_file_t *f;
1000
1001   /* environ(7) does not indicate a header for this */
1002   extern char **environ;
1003
1004   /* Close all known open files */
1005   /* *INDENT-OFF* */
1006   pool_foreach(f, fm->file_pool,
1007     ({
1008       if (f->file_descriptor > 2)
1009         close(f->file_descriptor);
1010     }));
1011   /* *INDENT-ON* */
1012
1013   /* Exec ourself */
1014   execve (vm->name, (char **) vm->argv, environ);
1015
1016   return 0;
1017 }
1018
1019 /* *INDENT-OFF* */
1020 VLIB_CLI_COMMAND (restart_cmd,static) = {
1021     .path = "restart",
1022     .short_help = "restart process",
1023     .function = restart_cmd_fn,
1024 };
1025 /* *INDENT-ON* */
1026
1027 #ifdef TEST_CODE
1028 /*
1029  * A trivial test harness to verify the per-process output_function
1030  * is working correcty.
1031  */
1032
1033 static clib_error_t *
1034 sleep_ten_seconds (vlib_main_t * vm,
1035                    unformat_input_t * input, vlib_cli_command_t * cmd)
1036 {
1037   u16 i;
1038   u16 my_id = rand ();
1039
1040   vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1041
1042   for (i = 0; i < 10; i++)
1043     {
1044       vlib_process_wait_for_event_or_clock (vm, 1.0);
1045       vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1046     }
1047   vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1048   return 0;
1049 }
1050
1051 /* *INDENT-OFF* */
1052 VLIB_CLI_COMMAND (ping_command, static) = {
1053   .path = "test sleep",
1054   .function = sleep_ten_seconds,
1055   .short_help = "Sleep for 10 seconds",
1056 };
1057 /* *INDENT-ON* */
1058 #endif /* ifdef TEST_CODE */
1059
1060 static uword
1061 vlib_cli_normalize_path (char *input, char **result)
1062 {
1063   char *i = input;
1064   char *s = 0;
1065   uword l = 0;
1066   uword index_of_last_space = ~0;
1067
1068   while (*i != 0)
1069     {
1070       u8 c = *i++;
1071       /* Multiple white space -> single space. */
1072       switch (c)
1073         {
1074         case ' ':
1075         case '\t':
1076         case '\n':
1077         case '\r':
1078           if (l > 0 && s[l - 1] != ' ')
1079             {
1080               vec_add1 (s, ' ');
1081               l++;
1082             }
1083           break;
1084
1085         default:
1086           if (l > 0 && s[l - 1] == ' ')
1087             index_of_last_space = vec_len (s);
1088           vec_add1 (s, c);
1089           l++;
1090           break;
1091         }
1092     }
1093
1094   /* Remove any extra space at end. */
1095   if (l > 0 && s[l - 1] == ' ')
1096     _vec_len (s) -= 1;
1097
1098   *result = s;
1099   return index_of_last_space;
1100 }
1101
1102 always_inline uword
1103 parent_path_len (char *path)
1104 {
1105   word i;
1106   for (i = vec_len (path) - 1; i >= 0; i--)
1107     {
1108       if (path[i] == ' ')
1109         return i;
1110     }
1111   return ~0;
1112 }
1113
1114 static void
1115 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1116 {
1117   vlib_cli_command_t *p, *c;
1118   vlib_cli_sub_command_t *sub_c;
1119   u8 *sub_name;
1120   word i, l;
1121
1122   p = vec_elt_at_index (cm->commands, parent_index);
1123   c = vec_elt_at_index (cm->commands, child_index);
1124
1125   l = parent_path_len (c->path);
1126   if (l == ~0)
1127     sub_name = vec_dup ((u8 *) c->path);
1128   else
1129     {
1130       ASSERT (l + 1 < vec_len (c->path));
1131       sub_name = 0;
1132       vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1133     }
1134
1135   if (sub_name[0] == '%')
1136     {
1137       uword *q;
1138       vlib_cli_sub_rule_t *sr;
1139
1140       /* Remove %. */
1141       vec_delete (sub_name, 1, 0);
1142
1143       if (!p->sub_rule_index_by_name)
1144         p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1145                                                      sizeof (sub_name[0]),
1146                                                      sizeof (uword));
1147       q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1148       if (q)
1149         {
1150           sr = vec_elt_at_index (p->sub_rules, q[0]);
1151           ASSERT (sr->command_index == child_index);
1152           return;
1153         }
1154
1155       q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
1156       if (!q)
1157         {
1158           clib_error ("reference to unknown rule `%%%v' in path `%v'",
1159                       sub_name, c->path);
1160           return;
1161         }
1162
1163       hash_set_mem (p->sub_rule_index_by_name, sub_name,
1164                     vec_len (p->sub_rules));
1165       vec_add2 (p->sub_rules, sr, 1);
1166       sr->name = sub_name;
1167       sr->rule_index = q[0];
1168       sr->command_index = child_index;
1169       return;
1170     }
1171
1172   if (!p->sub_command_index_by_name)
1173     p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1174                                                     sizeof (c->path[0]),
1175                                                     sizeof (uword));
1176
1177   /* Check if sub-command has already been created. */
1178   if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1179     {
1180       vec_free (sub_name);
1181       return;
1182     }
1183
1184   vec_add2 (p->sub_commands, sub_c, 1);
1185   sub_c->index = child_index;
1186   sub_c->name = sub_name;
1187   hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1188                 sub_c - p->sub_commands);
1189
1190   vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1191   for (i = 0; i < vec_len (sub_c->name); i++)
1192     {
1193       int n;
1194       vlib_cli_parse_position_t *pos;
1195
1196       pos = vec_elt_at_index (p->sub_command_positions, i);
1197
1198       if (!pos->bitmaps)
1199         pos->min_char = sub_c->name[i];
1200
1201       n = sub_c->name[i] - pos->min_char;
1202       if (n < 0)
1203         {
1204           pos->min_char = sub_c->name[i];
1205           vec_insert (pos->bitmaps, -n, 0);
1206           n = 0;
1207         }
1208
1209       vec_validate (pos->bitmaps, n);
1210       pos->bitmaps[n] =
1211         clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1212     }
1213 }
1214
1215 static void
1216 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1217 {
1218   uword p_len, pi, *p;
1219   char *p_path;
1220   vlib_cli_command_t *c, *parent;
1221
1222   /* Root command (index 0) should have already been added. */
1223   ASSERT (vec_len (cm->commands) > 0);
1224
1225   c = vec_elt_at_index (cm->commands, ci);
1226   p_len = parent_path_len (c->path);
1227
1228   /* No space?  Parent is root command. */
1229   if (p_len == ~0)
1230     {
1231       add_sub_command (cm, 0, ci);
1232       return;
1233     }
1234
1235   p_path = 0;
1236   vec_add (p_path, c->path, p_len);
1237
1238   p = hash_get_mem (cm->command_index_by_path, p_path);
1239
1240   /* Parent exists? */
1241   if (!p)
1242     {
1243       /* Parent does not exist; create it. */
1244       vec_add2 (cm->commands, parent, 1);
1245       parent->path = p_path;
1246       hash_set_mem (cm->command_index_by_path, parent->path,
1247                     parent - cm->commands);
1248       pi = parent - cm->commands;
1249     }
1250   else
1251     {
1252       pi = p[0];
1253       vec_free (p_path);
1254     }
1255
1256   add_sub_command (cm, pi, ci);
1257
1258   /* Create parent's parent. */
1259   if (!p)
1260     vlib_cli_make_parent (cm, pi);
1261 }
1262
1263 always_inline uword
1264 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1265 {
1266   return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1267 }
1268
1269 clib_error_t *
1270 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1271 {
1272   vlib_cli_main_t *cm = &vm->cli_main;
1273   clib_error_t *error = 0;
1274   uword ci, *p;
1275   char *normalized_path;
1276
1277   if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1278     return error;
1279
1280   (void) vlib_cli_normalize_path (c->path, &normalized_path);
1281
1282   if (!cm->command_index_by_path)
1283     cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1284                                                  sizeof (c->path[0]),
1285                                                  sizeof (uword));
1286
1287   /* See if command already exists with given path. */
1288   p = hash_get_mem (cm->command_index_by_path, normalized_path);
1289   if (p)
1290     {
1291       vlib_cli_command_t *d;
1292
1293       ci = p[0];
1294       d = vec_elt_at_index (cm->commands, ci);
1295
1296       /* If existing command was created via vlib_cli_make_parent
1297          replaced it with callers data. */
1298       if (vlib_cli_command_is_empty (d))
1299         {
1300           vlib_cli_command_t save = d[0];
1301
1302           ASSERT (!vlib_cli_command_is_empty (c));
1303
1304           /* Copy callers fields. */
1305           d[0] = c[0];
1306
1307           /* Save internal fields. */
1308           d->path = save.path;
1309           d->sub_commands = save.sub_commands;
1310           d->sub_command_index_by_name = save.sub_command_index_by_name;
1311           d->sub_command_positions = save.sub_command_positions;
1312           d->sub_rules = save.sub_rules;
1313         }
1314       else
1315         error =
1316           clib_error_return (0, "duplicate command name with path %v",
1317                              normalized_path);
1318
1319       vec_free (normalized_path);
1320       if (error)
1321         return error;
1322     }
1323   else
1324     {
1325       /* Command does not exist: create it. */
1326
1327       /* Add root command (index 0). */
1328       if (vec_len (cm->commands) == 0)
1329         {
1330           /* Create command with index 0; path is empty string. */
1331           vec_resize (cm->commands, 1);
1332         }
1333
1334       ci = vec_len (cm->commands);
1335       hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1336       vec_add1 (cm->commands, c[0]);
1337
1338       c = vec_elt_at_index (cm->commands, ci);
1339       c->path = normalized_path;
1340
1341       /* Don't inherit from registration. */
1342       c->sub_commands = 0;
1343       c->sub_command_index_by_name = 0;
1344       c->sub_command_positions = 0;
1345     }
1346
1347   vlib_cli_make_parent (cm, ci);
1348   return 0;
1349 }
1350
1351 clib_error_t *
1352 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1353 {
1354   vlib_cli_main_t *cm = &vm->cli_main;
1355   vlib_cli_parse_rule_t *r;
1356   clib_error_t *error = 0;
1357   u8 *r_name;
1358   uword *p;
1359
1360   if (!cm->parse_rule_index_by_name)
1361     cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1362                                                     sizeof (r->name[0]),
1363                                                     sizeof (uword));
1364
1365   /* Make vector copy of name. */
1366   r_name = format (0, "%s", r_reg->name);
1367
1368   if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1369     {
1370       vec_free (r_name);
1371       return clib_error_return (0, "duplicate parse rule name `%s'",
1372                                 r_reg->name);
1373     }
1374
1375   vec_add2 (cm->parse_rules, r, 1);
1376   r[0] = r_reg[0];
1377   r->name = (char *) r_name;
1378   hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1379
1380   return error;
1381 }
1382
1383 #if 0
1384 /* $$$ turn back on again someday, maybe */
1385 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1386                                                     vlib_cli_parse_rule_t *
1387                                                     lo,
1388                                                     vlib_cli_parse_rule_t *
1389                                                     hi)
1390   __attribute__ ((unused))
1391 {
1392   clib_error_t *error = 0;
1393   vlib_cli_parse_rule_t *r;
1394
1395   for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1396     {
1397       if (!r->name || strlen (r->name) == 0)
1398         {
1399           error = clib_error_return (0, "parse rule with no name");
1400           goto done;
1401         }
1402
1403       error = vlib_cli_register_parse_rule (vm, r);
1404       if (error)
1405         goto done;
1406     }
1407
1408 done:
1409   return error;
1410 }
1411 #endif
1412
1413 static int
1414 cli_path_compare (void *a1, void *a2)
1415 {
1416   u8 **s1 = a1;
1417   u8 **s2 = a2;
1418
1419   if ((vec_len (*s1) < vec_len (*s2)) &&
1420       memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0)
1421     return -1;
1422
1423
1424   if ((vec_len (*s1) > vec_len (*s2)) &&
1425       memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0)
1426     return 1;
1427
1428   return vec_cmp (*s1, *s2);
1429 }
1430
1431 static clib_error_t *
1432 show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1433                  vlib_cli_command_t * cmd)
1434 {
1435   vlib_cli_main_t *cm = &vm->cli_main;
1436   vlib_cli_command_t *cli;
1437   u8 **paths = 0, **s;
1438
1439   /* *INDENT-OFF* */
1440   vec_foreach (cli, cm->commands)
1441     if (vec_len (cli->path) > 0)
1442       vec_add1 (paths, (u8 *) cli->path);
1443
1444   vec_sort_with_function (paths, cli_path_compare);
1445
1446   vec_foreach (s, paths)
1447     vlib_cli_output (vm, "%v", *s);
1448   /* *INDENT-ON* */
1449
1450   vec_free (paths);
1451   return 0;
1452 }
1453
1454 /* *INDENT-OFF* */
1455 VLIB_CLI_COMMAND (show_cli_command, static) = {
1456   .path = "show cli",
1457   .short_help = "Show cli commands",
1458   .function = show_cli_cmd_fn,
1459 };
1460 /* *INDENT-ON* */
1461
1462 static clib_error_t *
1463 elog_trace_command_fn (vlib_main_t * vm,
1464                        unformat_input_t * input, vlib_cli_command_t * cmd)
1465 {
1466   unformat_input_t _line_input, *line_input = &_line_input;
1467   int enable = 1;
1468   int api = 0, cli = 0, barrier = 0;
1469
1470   if (!unformat_user (input, unformat_line_input, line_input))
1471     goto print_status;
1472
1473   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1474     {
1475       if (unformat (line_input, "api"))
1476         api = 1;
1477       else if (unformat (line_input, "cli"))
1478         cli = 1;
1479       else if (unformat (line_input, "barrier"))
1480         barrier = 1;
1481       else if (unformat (line_input, "disable"))
1482         enable = 0;
1483       else if (unformat (line_input, "enable"))
1484         enable = 1;
1485       else
1486         break;
1487     }
1488   unformat_free (line_input);
1489
1490   vm->elog_trace_api_messages = api ? enable : vm->elog_trace_api_messages;
1491   vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1492   vlib_worker_threads->barrier_elog_enabled =
1493     barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1494
1495 print_status:
1496   vlib_cli_output (vm, "Current status:");
1497
1498   vlib_cli_output
1499     (vm, "    Event log API message trace: %s\n    CLI command trace: %s",
1500      vm->elog_trace_api_messages ? "on" : "off",
1501      vm->elog_trace_cli_commands ? "on" : "off");
1502   vlib_cli_output
1503     (vm, "    Barrier sync trace: %s",
1504      vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1505
1506   return 0;
1507 }
1508
1509 /*?
1510  * Control event logging of api, cli, and thread barrier events
1511  * With no arguments, displays the current trace status.
1512  * Name the event groups you wish to trace or stop tracing.
1513  *
1514  * @cliexpar
1515  * @clistart
1516  * elog trace api cli barrier
1517  * elog trace api cli barrier disable
1518  * elog trace
1519  * @cliend
1520  * @cliexcmd{elog trace [api][cli][barrier][disable]}
1521 ?*/
1522 /* *INDENT-OFF* */
1523 VLIB_CLI_COMMAND (elog_trace_command, static) =
1524 {
1525   .path = "elog trace",
1526   .short_help = "elog trace [api][cli][barrier][disable]",
1527   .function = elog_trace_command_fn,
1528 };
1529 /* *INDENT-ON* */
1530
1531 static clib_error_t *
1532 vlib_cli_init (vlib_main_t * vm)
1533 {
1534   vlib_cli_main_t *cm = &vm->cli_main;
1535   clib_error_t *error = 0;
1536   vlib_cli_command_t *cmd;
1537
1538   cmd = cm->cli_command_registrations;
1539
1540   while (cmd)
1541     {
1542       error = vlib_cli_register (vm, cmd);
1543       if (error)
1544         return error;
1545       cmd = cmd->next_cli_command;
1546     }
1547   return error;
1548 }
1549
1550 VLIB_INIT_FUNCTION (vlib_cli_init);
1551
1552 /*
1553  * fd.io coding-style-patch-verification: ON
1554  *
1555  * Local Variables:
1556  * eval: (c-set-style "gnu")
1557  * End:
1558  */