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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * cli.c: command line interface
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 #include <vppinfra/cpu.h>
43 #include <vppinfra/elog.h>
47 /** \file src/vlib/cli.c Debug CLI Implementation
50 int vl_api_set_elog_trace_api_messages (int enable);
51 int vl_api_get_elog_trace_api_messages (void);
53 static void *current_traced_heap;
55 /* Root of all show commands. */
57 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
59 .short_help = "Show commands",
63 /* Root of all clear commands. */
65 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
67 .short_help = "Clear commands",
71 /* Root of all set commands. */
73 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
75 .short_help = "Set commands",
79 /* Root of all test commands. */
81 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
83 .short_help = "Test commands",
87 /* Returns bitmap of commands which match key. */
89 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
93 vlib_cli_parse_position_t *p;
95 unformat_skip_white_space (input);
101 k = unformat_get_input (input);
115 case UNFORMAT_END_OF_INPUT:
116 /* White space or end of input removes any non-white
117 matches that were before possible. */
118 if (i < vec_len (c->sub_command_positions)
119 && clib_bitmap_count_set_bits (match) > 1)
121 p = vec_elt_at_index (c->sub_command_positions, i);
122 for (n = 0; n < vec_len (p->bitmaps); n++)
123 match = clib_bitmap_andnot (match, p->bitmaps[n]);
128 unformat_put_input (input);
132 if (i >= vec_len (c->sub_command_positions))
135 clib_bitmap_free (match);
139 p = vec_elt_at_index (c->sub_command_positions, i);
140 if (vec_len (p->bitmaps) == 0)
144 if (n < 0 || n >= vec_len (p->bitmaps))
148 match = clib_bitmap_dup (p->bitmaps[n]);
150 match = clib_bitmap_and (match, p->bitmaps[n]);
152 if (clib_bitmap_is_zero (match))
160 /* Looks for string based sub-input formatted { SUB-INPUT }. */
162 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
164 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
170 c = unformat_get_input (i);
182 /* Put back paren. */
183 if (c != UNFORMAT_END_OF_INPUT)
184 unformat_put_input (i);
186 if (c == '{' && unformat (i, "%v", &s))
188 unformat_init_vector (sub_input, s);
197 static vlib_cli_command_t *
198 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
200 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
201 return vec_elt_at_index (cm->commands, s->index);
205 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
207 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
208 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
209 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
210 vlib_cli_main_t *cm = &vm->cli_main;
211 uword *match_bitmap, is_unique, index;
214 vlib_cli_sub_rule_t *sr;
215 vlib_cli_parse_rule_t *r;
216 vec_foreach (sr, c->sub_rules)
219 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
220 vec_add2 (cm->parse_rule_data, d, 1);
221 vec_reset_length (d[0]);
223 d[0] = _vec_resize (d[0],
224 /* length increment */ 1,
226 /* header_bytes */ 0,
227 /* data align */ sizeof (uword));
228 if (unformat_user (i, r->unformat_function, vm, d[0]))
230 *result = vec_elt_at_index (cm->commands, sr->command_index);
236 match_bitmap = vlib_cli_sub_command_match (c, i);
237 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
241 index = clib_bitmap_first_set (match_bitmap);
242 *result = get_sub_command (cm, c, index);
244 clib_bitmap_free (match_bitmap);
250 vlib_cli_cmp_strings (void *a1, void *a2)
252 u8 *c1 = *(u8 **) a1;
253 u8 *c2 = *(u8 **) a2;
255 return vec_cmp (c1, c2);
259 vlib_cli_get_possible_completions (u8 * str)
261 vlib_cli_command_t *c;
262 vlib_cli_sub_command_t *sc;
263 vlib_main_t *vm = vlib_get_main ();
264 vlib_cli_main_t *vcm = &vm->cli_main;
265 uword *match_bitmap = 0;
266 uword index, is_unique, help_next_level;
268 unformat_input_t input;
269 unformat_init_vector (&input, vec_dup (str));
270 c = vec_elt_at_index (vcm->commands, 0);
272 /* remove trailing whitespace, except for one of them */
273 while (vec_len (input.buffer) >= 2 &&
274 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
275 isspace (input.buffer[vec_len (input.buffer) - 2]))
277 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
280 /* if input is empty, directly return list of root commands */
281 if (vec_len (input.buffer) == 0 ||
282 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
284 vec_foreach (sc, c->sub_commands)
286 vec_add1 (result, (u8 *) sc->name);
291 /* add a trailing '?' so that vlib_cli_sub_command_match can find
292 * all commands starting with the input string */
293 vec_add1 (input.buffer, '?');
297 match_bitmap = vlib_cli_sub_command_match (c, &input);
298 /* no match: return no result */
299 if (match_bitmap == 0)
303 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
304 /* unique match: try to step one subcommand level further */
307 /* stop if no more input */
308 if (input.index >= vec_len (input.buffer) - 1)
313 index = clib_bitmap_first_set (match_bitmap);
314 c = get_sub_command (vcm, c, index);
315 clib_bitmap_free (match_bitmap);
318 /* multiple matches: stop here, return all matches */
322 /* remove trailing '?' */
323 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
325 /* if we have a space at the end of input, and a unique match,
326 * autocomplete the next level of subcommands */
327 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
329 clib_bitmap_foreach(index, match_bitmap, {
330 if (help_next_level && is_unique) {
331 c = get_sub_command (vcm, c, index);
332 vec_foreach (sc, c->sub_commands) {
333 vec_add1 (result, (u8*) sc->name);
335 goto done; /* break doesn't work in this macro-loop */
337 sc = &c->sub_commands[index];
338 vec_add1(result, (u8*) sc->name);
343 clib_bitmap_free (match_bitmap);
344 unformat_free (&input);
347 vec_sort_with_function (result, vlib_cli_cmp_strings);
352 format_vlib_cli_command_help (u8 * s, va_list * args)
354 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
355 int is_long = va_arg (*args, int);
356 if (is_long && c->long_help)
357 s = format (s, "%s", c->long_help);
358 else if (c->short_help)
359 s = format (s, "%s", c->short_help);
361 s = format (s, "%v commands", c->path);
366 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
368 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
369 return format (s, "<%U>", format_c_identifier, r->name);
373 format_vlib_cli_path (u8 * s, va_list * args)
375 u8 *path = va_arg (*args, u8 *);
378 for (i = 0; i < vec_len (path); i++)
384 vec_add1 (s, '<'); /* start of <RULE> */
388 /* _ -> space in rules. */
389 vec_add1 (s, in_rule ? ' ' : '_');
395 vec_add1 (s, '>'); /* end of <RULE> */
402 vec_add1 (s, path[i]);
408 vec_add1 (s, '>'); /* terminate <RULE> */
413 static vlib_cli_command_t *
414 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
416 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
417 vlib_cli_sub_command_t *sc;
418 vlib_cli_sub_rule_t *sr;
421 vec_add1 (subs, c[0]);
423 vec_foreach (sr, c->sub_rules)
424 subs = all_subs (cm, subs, sr->command_index);
425 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
431 vlib_cli_cmp_rule (void *a1, void *a2)
433 vlib_cli_sub_rule_t *r1 = a1;
434 vlib_cli_sub_rule_t *r2 = a2;
436 return vec_cmp (r1->name, r2->name);
440 vlib_cli_cmp_command (void *a1, void *a2)
442 vlib_cli_command_t *c1 = a1;
443 vlib_cli_command_t *c2 = a2;
445 return vec_cmp (c1->path, c2->path);
448 static clib_error_t *
449 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
450 vlib_cli_main_t * cm,
451 unformat_input_t * input,
452 uword parent_command_index)
454 vlib_cli_command_t *parent, *c;
455 clib_error_t *error = 0;
456 unformat_input_t sub_input;
458 uword is_main_dispatch = cm == &vm->cli_main;
460 parent = vec_elt_at_index (cm->commands, parent_command_index);
461 if (is_main_dispatch && unformat (input, "help"))
463 uword help_at_end_of_line, i;
465 help_at_end_of_line =
466 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
471 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
474 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
481 /* help SUB-COMMAND => long format help.
482 "help" at end of line: show all commands. */
483 if (!help_at_end_of_line)
484 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
487 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
488 vlib_cli_output (vm, "%v: no sub-commands", c->path);
492 vlib_cli_sub_command_t *sc;
493 vlib_cli_sub_rule_t *sr, *subs;
495 subs = vec_dup (c->sub_rules);
497 /* Add in rules if any. */
498 vec_foreach (sc, c->sub_commands)
500 vec_add2 (subs, sr, 1);
502 sr->command_index = sc->index;
506 vec_sort_with_function (subs, vlib_cli_cmp_rule);
508 for (i = 0; i < vec_len (subs); i++)
510 vlib_cli_command_t *d;
511 vlib_cli_parse_rule_t *r;
513 d = vec_elt_at_index (cm->commands, subs[i].command_index);
515 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
523 format_vlib_cli_parse_rule_name, r,
524 format_vlib_cli_command_help, d, /* is_long */ 0);
529 format_vlib_cli_command_help, d, /* is_long */ 0);
536 else if (is_main_dispatch
537 && (unformat (input, "choices") || unformat (input, "?")))
539 vlib_cli_command_t *sub, *subs;
541 subs = all_subs (cm, 0, parent_command_index);
542 vec_sort_with_function (subs, vlib_cli_cmp_command);
543 vec_foreach (sub, subs)
544 vlib_cli_output (vm, " %-40U %U",
545 format_vlib_cli_path, sub->path,
546 format_vlib_cli_command_help, sub, /* is_long */ 0);
550 else if (unformat (input, "comment %v", &string))
555 else if (unformat (input, "uncomment %U",
556 unformat_vlib_cli_sub_input, &sub_input))
559 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
560 parent_command_index);
561 unformat_free (&sub_input);
563 else if (unformat (input, "leak-check %U",
564 unformat_vlib_cli_sub_input, &sub_input))
567 if (current_traced_heap)
570 oldheap = clib_mem_set_heap (current_traced_heap);
572 clib_mem_set_heap (oldheap);
573 current_traced_heap = 0;
577 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
578 parent_command_index);
579 unformat_free (&sub_input);
581 /* Otherwise, the clib_error_t shows up as a leak... */
584 vlib_cli_output (vm, "%v", error->what);
585 clib_error_free (error);
589 (void) clib_mem_trace_enable_disable (0);
590 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
591 1 /* verbose, i.e. print leaks */ );
593 vlib_cli_output (vm, "%v", leak_report);
594 vec_free (leak_report);
598 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
600 unformat_input_t *si;
601 uword has_sub_commands =
602 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
605 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
608 if (has_sub_commands)
609 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
611 if (has_sub_commands && !error)
612 /* Found valid sub-command. */ ;
614 else if (c->function)
616 clib_error_t *c_error;
618 /* Skip white space for benefit of called function. */
619 unformat_skip_white_space (si);
621 if (unformat (si, "?"))
623 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
628 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
631 ELOG_TYPE_DECLARE (e) =
633 .format = "cli-cmd: %s",
641 ed = ELOG_DATA (&vm->elog_main, e);
642 ed->c = elog_string (&vm->elog_main, c->path);
646 vlib_worker_thread_barrier_sync (vm);
649 c_error = c->function (vm, si, c);
652 vlib_worker_thread_barrier_release (vm);
654 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
657 ELOG_TYPE_DECLARE (e) =
659 .format = "cli-cmd: %s %s",
660 .format_args = "T4T4",
667 ed = ELOG_DATA (&vm->elog_main, e);
668 ed->c = elog_string (&vm->elog_main, c->path);
671 vec_add1 (c_error->what, 0);
672 ed->err = elog_string (&vm->elog_main,
673 (char *) c_error->what);
674 _vec_len (c_error->what) -= 1;
677 ed->err = elog_string (&vm->elog_main, "OK");
683 clib_error_return (0, "%v: %v", c->path, c_error->what);
684 clib_error_free (c_error);
685 /* Free sub input. */
693 /* Free any previous error. */
694 clib_error_free (error);
698 error = clib_error_return (0, "%v: no sub-commands", c->path);
700 /* Free sub input. */
712 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
713 format_unformat_error, input);
715 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
720 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
721 __attribute__ ((weak));
724 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
728 /* Process CLI input. */
730 vlib_cli_input (vlib_main_t * vm,
731 unformat_input_t * input,
732 vlib_cli_output_function_t * function, uword function_arg)
734 vlib_process_t *cp = vlib_get_current_process (vm);
735 vlib_cli_main_t *cm = &vm->cli_main;
737 vlib_cli_output_function_t *save_function;
738 uword save_function_arg;
741 save_function = cp->output_function;
742 save_function_arg = cp->output_function_arg;
744 cp->output_function = function;
745 cp->output_function_arg = function_arg;
749 vec_reset_length (cm->parse_rule_data);
750 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
753 while (!error && !unformat (input, "%U", unformat_eof));
757 vlib_cli_output (vm, "%v", error->what);
758 vlib_unix_error_report (vm, error);
759 /* clib_error_return is unfortunately often called with a '0'
761 rv = error->code != 0 ? error->code : -1;
762 clib_error_free (error);
765 cp->output_function = save_function;
766 cp->output_function_arg = save_function_arg;
770 /* Output to current CLI connection. */
772 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
774 vlib_process_t *cp = vlib_get_current_process (vm);
779 s = va_format (0, fmt, &va);
782 /* Terminate with \n if not present. */
783 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
786 if ((!cp) || (!cp->output_function))
787 fformat (stdout, "%v", s);
789 cp->output_function (cp->output_function_arg, s, vec_len (s));
794 void *vl_msg_push_heap (void) __attribute__ ((weak));
796 vl_msg_push_heap (void)
801 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
803 vl_msg_pop_heap (void *oldheap)
807 void *vlib_stats_push_heap (void *) __attribute__ ((weak));
809 vlib_stats_push_heap (void *notused)
814 static clib_error_t *
815 show_memory_usage (vlib_main_t * vm,
816 unformat_input_t * input, vlib_cli_command_t * cmd)
818 int verbose __attribute__ ((unused)) = 0;
819 int api_segment = 0, stats_segment = 0, main_heap = 0;
822 uword clib_mem_trace_enable_disable (uword enable);
826 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
828 if (unformat (input, "verbose"))
830 else if (unformat (input, "api-segment"))
832 else if (unformat (input, "stats-segment"))
834 else if (unformat (input, "main-heap"))
838 error = clib_error_return (0, "unknown input `%U'",
839 format_unformat_error, input);
844 if ((api_segment + stats_segment + main_heap) == 0)
845 return clib_error_return
846 (0, "Please supply one of api-segment, stats-segment or main-heap");
850 void *oldheap = vl_msg_push_heap ();
851 was_enabled = clib_mem_trace_enable_disable (0);
853 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
854 vl_msg_pop_heap (oldheap);
855 u8 *s = vec_dup (s_in_svm);
857 oldheap = vl_msg_push_heap ();
859 clib_mem_trace_enable_disable (was_enabled);
860 vl_msg_pop_heap (oldheap);
861 vlib_cli_output (vm, "API segment");
862 vlib_cli_output (vm, "%v", s);
867 void *oldheap = vlib_stats_push_heap (0);
868 was_enabled = clib_mem_trace_enable_disable (0);
870 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
872 clib_mem_set_heap (oldheap);
873 u8 *s = vec_dup (s_in_svm);
875 oldheap = vlib_stats_push_heap (0);
879 clib_mem_trace_enable_disable (was_enabled);
880 clib_mem_set_heap (oldheap);
882 vlib_cli_output (vm, "Stats segment");
883 vlib_cli_output (vm, "%v", s);
887 #if USE_DLMALLOC == 0
891 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
892 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
893 vlib_worker_threads[index].name);
894 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
895 h->vm_alloc_offset_from_header,
897 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
907 * Note: the foreach_vlib_main causes allocator traffic,
908 * so shut off tracing before we go there...
910 was_enabled = clib_mem_trace_enable_disable (0);
915 struct dlmallinfo mi;
917 mspace = clib_per_cpu_mheaps[index];
919 mi = mspace_mallinfo (mspace);
920 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
921 vlib_worker_threads[index].name);
922 vlib_cli_output (vm, " %U\n", format_page_map,
923 pointer_to_uword (mspace_least_addr(mspace)),
925 vlib_cli_output (vm, " %U\n", format_mheap,
926 clib_per_cpu_mheaps[index],
932 /* Restore the trace flag */
933 clib_mem_trace_enable_disable (was_enabled);
936 #endif /* USE_DLMALLOC */
941 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
942 .path = "show memory",
943 .short_help = "show memory [api-segment][stats-segment][verbose]",
944 .function = show_memory_usage,
948 static clib_error_t *
949 show_cpu (vlib_main_t * vm, unformat_input_t * input,
950 vlib_cli_command_t * cmd)
952 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
953 _("Model name", "%U", format_cpu_model_name);
954 _("Microarch model (family)", "%U", format_cpu_uarch);
955 _("Flags", "%U", format_cpu_flags);
956 _("Base frequency", "%.2f GHz",
957 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
963 * Displays various information about the CPU.
966 * @cliexstart{show cpu}
967 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
968 * Microarchitecture: Broadwell (Broadwell-EP/EX)
969 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
970 * Base Frequency: 3.20 GHz
974 VLIB_CLI_COMMAND (show_cpu_command, static) = {
976 .short_help = "Show cpu information",
977 .function = show_cpu,
981 static clib_error_t *
982 enable_disable_memory_trace (vlib_main_t * vm,
983 unformat_input_t * input,
984 vlib_cli_command_t * cmd)
986 unformat_input_t _line_input, *line_input = &_line_input;
989 int stats_segment = 0;
993 if (!unformat_user (input, unformat_line_input, line_input))
996 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
998 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
1000 else if (unformat (line_input, "api-segment"))
1002 else if (unformat (line_input, "stats-segment"))
1004 else if (unformat (line_input, "main-heap"))
1008 unformat_free (line_input);
1009 return clib_error_return (0, "invalid input");
1012 unformat_free (line_input);
1014 if ((api_segment + stats_segment + main_heap + (enable == 0)) == 0)
1016 return clib_error_return
1017 (0, "Need one of main-heap, stats-segment or api-segment");
1020 /* Turn off current trace, if any */
1021 if (current_traced_heap)
1024 oldheap = clib_mem_set_heap (current_traced_heap);
1026 clib_mem_set_heap (oldheap);
1027 current_traced_heap = 0;
1036 oldheap = vl_msg_push_heap ();
1037 current_traced_heap = clib_mem_get_heap ();
1039 vl_msg_pop_heap (oldheap);
1046 oldheap = vlib_stats_push_heap (0);
1047 current_traced_heap = clib_mem_get_heap ();
1048 clib_mem_trace (stats_segment);
1049 /* We don't want to call vlib_stats_pop_heap... */
1051 clib_mem_set_heap (oldheap);
1057 current_traced_heap = clib_mem_get_heap ();
1058 clib_mem_trace (main_heap);
1065 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1066 .path = "memory-trace",
1067 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n",
1068 .function = enable_disable_memory_trace,
1073 static clib_error_t *
1074 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
1075 vlib_cli_command_t * cmd)
1077 #if USE_DLMALLOC == 0
1078 clib_error_t *error = 0;
1082 if (unformat (input, "on"))
1086 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1087 mheap = mheap_header(heap);
1088 mheap->flags |= MHEAP_FLAG_VALIDATE;
1089 // Turn off small object cache because it delays detection of errors
1090 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
1095 else if (unformat (input, "off"))
1099 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1100 mheap = mheap_header(heap);
1101 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
1102 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
1106 else if (unformat (input, "now"))
1110 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1111 mheap = mheap_header(heap);
1112 mheap_validate(heap);
1115 vlib_cli_output (vm, "heap validation complete");
1120 return clib_error_return (0, "unknown input `%U'",
1121 format_unformat_error, input);
1126 return clib_error_return (0, "unimplemented...");
1127 #endif /* USE_DLMALLOC */
1131 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1132 .path = "test heap-validate",
1133 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1134 .function = test_heap_validate,
1138 static clib_error_t *
1139 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1140 vlib_cli_command_t * cmd)
1142 clib_file_main_t *fm = &file_main;
1145 /* environ(7) does not indicate a header for this */
1146 extern char **environ;
1148 /* Close all known open files */
1150 pool_foreach(f, fm->file_pool,
1152 if (f->file_descriptor > 2)
1153 close(f->file_descriptor);
1158 execve (vm->name, (char **) vm->argv, environ);
1164 VLIB_CLI_COMMAND (restart_cmd,static) = {
1166 .short_help = "restart process",
1167 .function = restart_cmd_fn,
1173 * A trivial test harness to verify the per-process output_function
1174 * is working correcty.
1177 static clib_error_t *
1178 sleep_ten_seconds (vlib_main_t * vm,
1179 unformat_input_t * input, vlib_cli_command_t * cmd)
1182 u16 my_id = rand ();
1184 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1186 for (i = 0; i < 10; i++)
1188 vlib_process_wait_for_event_or_clock (vm, 1.0);
1189 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1191 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1196 VLIB_CLI_COMMAND (ping_command, static) = {
1197 .path = "test sleep",
1198 .function = sleep_ten_seconds,
1199 .short_help = "Sleep for 10 seconds",
1202 #endif /* ifdef TEST_CODE */
1205 vlib_cli_normalize_path (char *input, char **result)
1210 uword index_of_last_space = ~0;
1215 /* Multiple white space -> single space. */
1222 if (l > 0 && s[l - 1] != ' ')
1230 if (l > 0 && s[l - 1] == ' ')
1231 index_of_last_space = vec_len (s);
1238 /* Remove any extra space at end. */
1239 if (l > 0 && s[l - 1] == ' ')
1243 return index_of_last_space;
1247 parent_path_len (char *path)
1250 for (i = vec_len (path) - 1; i >= 0; i--)
1259 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1261 vlib_cli_command_t *p, *c;
1262 vlib_cli_sub_command_t *sub_c;
1266 p = vec_elt_at_index (cm->commands, parent_index);
1267 c = vec_elt_at_index (cm->commands, child_index);
1269 l = parent_path_len (c->path);
1271 sub_name = vec_dup ((u8 *) c->path);
1274 ASSERT (l + 1 < vec_len (c->path));
1276 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1279 if (sub_name[0] == '%')
1282 vlib_cli_sub_rule_t *sr;
1285 vec_delete (sub_name, 1, 0);
1287 if (!p->sub_rule_index_by_name)
1288 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1289 sizeof (sub_name[0]),
1291 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1294 sr = vec_elt_at_index (p->sub_rules, q[0]);
1295 ASSERT (sr->command_index == child_index);
1299 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
1302 clib_error ("reference to unknown rule `%%%v' in path `%v'",
1307 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1308 vec_len (p->sub_rules));
1309 vec_add2 (p->sub_rules, sr, 1);
1310 sr->name = sub_name;
1311 sr->rule_index = q[0];
1312 sr->command_index = child_index;
1316 if (!p->sub_command_index_by_name)
1317 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1318 sizeof (c->path[0]),
1321 /* Check if sub-command has already been created. */
1322 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1324 vec_free (sub_name);
1328 vec_add2 (p->sub_commands, sub_c, 1);
1329 sub_c->index = child_index;
1330 sub_c->name = sub_name;
1331 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1332 sub_c - p->sub_commands);
1334 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1335 for (i = 0; i < vec_len (sub_c->name); i++)
1338 vlib_cli_parse_position_t *pos;
1340 pos = vec_elt_at_index (p->sub_command_positions, i);
1343 pos->min_char = sub_c->name[i];
1345 n = sub_c->name[i] - pos->min_char;
1348 pos->min_char = sub_c->name[i];
1349 vec_insert (pos->bitmaps, -n, 0);
1353 vec_validate (pos->bitmaps, n);
1355 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1360 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1362 uword p_len, pi, *p;
1364 vlib_cli_command_t *c, *parent;
1366 /* Root command (index 0) should have already been added. */
1367 ASSERT (vec_len (cm->commands) > 0);
1369 c = vec_elt_at_index (cm->commands, ci);
1370 p_len = parent_path_len (c->path);
1372 /* No space? Parent is root command. */
1375 add_sub_command (cm, 0, ci);
1380 vec_add (p_path, c->path, p_len);
1382 p = hash_get_mem (cm->command_index_by_path, p_path);
1384 /* Parent exists? */
1387 /* Parent does not exist; create it. */
1388 vec_add2 (cm->commands, parent, 1);
1389 parent->path = p_path;
1390 hash_set_mem (cm->command_index_by_path, parent->path,
1391 parent - cm->commands);
1392 pi = parent - cm->commands;
1400 add_sub_command (cm, pi, ci);
1402 /* Create parent's parent. */
1404 vlib_cli_make_parent (cm, pi);
1408 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1410 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1414 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1416 vlib_cli_main_t *cm = &vm->cli_main;
1417 clib_error_t *error = 0;
1419 char *normalized_path;
1421 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1424 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1426 if (!cm->command_index_by_path)
1427 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1428 sizeof (c->path[0]),
1431 /* See if command already exists with given path. */
1432 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1435 vlib_cli_command_t *d;
1438 d = vec_elt_at_index (cm->commands, ci);
1440 /* If existing command was created via vlib_cli_make_parent
1441 replaced it with callers data. */
1442 if (vlib_cli_command_is_empty (d))
1444 vlib_cli_command_t save = d[0];
1446 ASSERT (!vlib_cli_command_is_empty (c));
1448 /* Copy callers fields. */
1451 /* Save internal fields. */
1452 d->path = save.path;
1453 d->sub_commands = save.sub_commands;
1454 d->sub_command_index_by_name = save.sub_command_index_by_name;
1455 d->sub_command_positions = save.sub_command_positions;
1456 d->sub_rules = save.sub_rules;
1460 clib_error_return (0, "duplicate command name with path %v",
1463 vec_free (normalized_path);
1469 /* Command does not exist: create it. */
1471 /* Add root command (index 0). */
1472 if (vec_len (cm->commands) == 0)
1474 /* Create command with index 0; path is empty string. */
1475 vec_resize (cm->commands, 1);
1478 ci = vec_len (cm->commands);
1479 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1480 vec_add1 (cm->commands, c[0]);
1482 c = vec_elt_at_index (cm->commands, ci);
1483 c->path = normalized_path;
1485 /* Don't inherit from registration. */
1486 c->sub_commands = 0;
1487 c->sub_command_index_by_name = 0;
1488 c->sub_command_positions = 0;
1491 vlib_cli_make_parent (cm, ci);
1496 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1498 vlib_cli_main_t *cm = &vm->cli_main;
1499 vlib_cli_parse_rule_t *r;
1500 clib_error_t *error = 0;
1504 if (!cm->parse_rule_index_by_name)
1505 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1506 sizeof (r->name[0]),
1509 /* Make vector copy of name. */
1510 r_name = format (0, "%s", r_reg->name);
1512 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1515 return clib_error_return (0, "duplicate parse rule name `%s'",
1519 vec_add2 (cm->parse_rules, r, 1);
1521 r->name = (char *) r_name;
1522 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1528 /* $$$ turn back on again someday, maybe */
1529 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1530 vlib_cli_parse_rule_t *
1532 vlib_cli_parse_rule_t *
1534 __attribute__ ((unused))
1536 clib_error_t *error = 0;
1537 vlib_cli_parse_rule_t *r;
1539 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1541 if (!r->name || strlen (r->name) == 0)
1543 error = clib_error_return (0, "parse rule with no name");
1547 error = vlib_cli_register_parse_rule (vm, r);
1557 static clib_error_t *
1558 elog_trace_command_fn (vlib_main_t * vm,
1559 unformat_input_t * input, vlib_cli_command_t * cmd)
1561 unformat_input_t _line_input, *line_input = &_line_input;
1563 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1564 u32 circuit_node_index;
1566 if (!unformat_user (input, unformat_line_input, line_input))
1569 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1571 if (unformat (line_input, "api"))
1573 else if (unformat (line_input, "dispatch"))
1575 else if (unformat (line_input, "circuit-node %U",
1576 unformat_vlib_node, vm, &circuit_node_index))
1578 else if (unformat (line_input, "cli"))
1580 else if (unformat (line_input, "barrier"))
1582 else if (unformat (line_input, "disable"))
1584 else if (unformat (line_input, "enable"))
1589 unformat_free (line_input);
1591 vl_api_set_elog_trace_api_messages
1592 (api ? enable : vl_api_get_elog_trace_api_messages ());
1593 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1594 vm->elog_trace_graph_dispatch = dispatch ?
1595 enable : vm->elog_trace_graph_dispatch;
1596 vm->elog_trace_graph_circuit = circuit ?
1597 enable : vm->elog_trace_graph_circuit;
1598 vlib_worker_threads->barrier_elog_enabled =
1599 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1600 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1603 * Set up start-of-buffer logic-analyzer trigger
1604 * for main loop event logs, which are fairly heavyweight.
1605 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1606 * will fully disable the scheme when the elog buffer fills.
1608 if (dispatch || circuit)
1610 elog_main_t *em = &vm->elog_main;
1612 em->n_total_events_disable_limit =
1613 em->n_total_events + vec_len (em->event_ring);
1618 vlib_cli_output (vm, "Current status:");
1621 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1622 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1623 vm->elog_trace_cli_commands ? "on" : "off");
1625 (vm, " Barrier sync trace: %s",
1626 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1628 (vm, " Graph Dispatch: %s",
1629 vm->elog_trace_graph_dispatch ? "on" : "off");
1631 (vm, " Graph Circuit: %s",
1632 vm->elog_trace_graph_circuit ? "on" : "off");
1633 if (vm->elog_trace_graph_circuit)
1636 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1642 * Control event logging of api, cli, and thread barrier events
1643 * With no arguments, displays the current trace status.
1644 * Name the event groups you wish to trace or stop tracing.
1648 * elog trace api cli barrier
1649 * elog trace api cli barrier disable
1650 * elog trace dispatch
1651 * elog trace circuit-node ethernet-input
1654 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1657 VLIB_CLI_COMMAND (elog_trace_command, static) =
1659 .path = "elog trace",
1660 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1661 "[circuit-node <name> e.g. ethernet-input][disable]",
1662 .function = elog_trace_command_fn,
1666 static clib_error_t *
1667 suspend_command_fn (vlib_main_t * vm,
1668 unformat_input_t * input, vlib_cli_command_t * cmd)
1670 vlib_process_suspend (vm, 30e-3);
1675 VLIB_CLI_COMMAND (suspend_command, static) =
1678 .short_help = "suspend debug CLI for 30ms",
1679 .function = suspend_command_fn,
1686 sort_cmds_by_path (void *a1, void *a2)
1690 vlib_main_t *vm = vlib_get_main ();
1691 vlib_cli_main_t *cm = &vm->cli_main;
1692 vlib_cli_command_t *c1, *c2;
1695 c1 = vec_elt_at_index (cm->commands, *index1);
1696 c2 = vec_elt_at_index (cm->commands, *index2);
1698 lmin = vec_len (c1->path);
1699 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1701 for (i = 0; i < lmin; i++)
1703 if (c1->path[i] < c2->path[i])
1705 else if (c1->path[i] > c2->path[i])
1714 vlib_cli_main_t *cm;
1715 u32 parent_command_index;
1717 int show_not_mp_safe;
1720 } vlib_cli_walk_args_t;
1723 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1725 vlib_cli_command_t *parent;
1726 vlib_cli_sub_command_t *sub;
1727 vlib_cli_walk_args_t _a, *a = &_a;
1728 vlib_cli_main_t *cm;
1731 /* Copy args into this stack frame */
1735 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1737 if (parent->function)
1739 if (((a->show_mp_safe && parent->is_mp_safe)
1740 || (a->show_not_mp_safe && !parent->is_mp_safe))
1741 && (a->show_hit == 0 || parent->hit_counter))
1743 vec_add1 (cm->sort_vector, a->parent_command_index);
1747 parent->hit_counter = 0;
1750 for (i = 0; i < vec_len (parent->sub_commands); i++)
1752 sub = vec_elt_at_index (parent->sub_commands, i);
1753 a->parent_command_index = sub->index;
1754 cli_recursive_walk (a);
1759 format_mp_safe (u8 * s, va_list * args)
1761 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1762 int show_mp_safe = va_arg (*args, int);
1763 int show_not_mp_safe = va_arg (*args, int);
1764 int show_hit = va_arg (*args, int);
1765 int clear_hit = va_arg (*args, int);
1766 vlib_cli_command_t *c;
1767 vlib_cli_walk_args_t _a, *a = &_a;
1769 char *format_string = "\n%v";
1772 format_string = "\n%v: %u";
1774 vec_reset_length (cm->sort_vector);
1777 a->parent_command_index = 0;
1778 a->show_mp_safe = show_mp_safe;
1779 a->show_not_mp_safe = show_not_mp_safe;
1780 a->show_hit = show_hit;
1781 a->clear_hit = clear_hit;
1783 cli_recursive_walk (a);
1785 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1787 for (i = 0; i < vec_len (cm->sort_vector); i++)
1789 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1790 s = format (s, format_string, c->path, c->hit_counter);
1797 static clib_error_t *
1798 show_cli_command_fn (vlib_main_t * vm,
1799 unformat_input_t * input, vlib_cli_command_t * cmd)
1801 int show_mp_safe = 0;
1802 int show_not_mp_safe = 0;
1806 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1808 if (unformat (input, "mp-safe"))
1810 if (unformat (input, "not-mp-safe"))
1811 show_not_mp_safe = 1;
1812 else if (unformat (input, "hit"))
1814 else if (unformat (input, "clear-hit"))
1820 /* default set: all cli commands */
1821 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1822 show_mp_safe = show_not_mp_safe = 1;
1824 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1825 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1827 vlib_cli_output (vm, "hit counters cleared...");
1833 * Displays debug cli command information
1836 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1838 * "show cli" displays the entire debug cli:
1842 * adjacency counters
1848 * "show cli mp-safe" displays mp-safe debug CLI commands:
1862 * "show cli not-mp-safe" displays debug CLI commands
1863 * which cause worker thread barrier synchronization
1865 * "show cli hit" displays commands which have been executed. Qualify
1866 * as desired with "mp-safe" or "not-mp-safe".
1868 * "show cli clear-hit" clears the per-command hit counters.
1873 VLIB_CLI_COMMAND (show_cli_command, static) =
1876 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1877 .function = show_cli_command_fn,
1882 static clib_error_t *
1883 vlib_cli_init (vlib_main_t * vm)
1885 vlib_cli_main_t *cm = &vm->cli_main;
1886 clib_error_t *error = 0;
1887 vlib_cli_command_t *cmd;
1889 cmd = cm->cli_command_registrations;
1893 error = vlib_cli_register (vm, cmd);
1896 cmd = cmd->next_cli_command;
1901 VLIB_INIT_FUNCTION (vlib_cli_init);
1904 * fd.io coding-style-patch-verification: ON
1907 * eval: (c-set-style "gnu")