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/stats/stats.h>
42 #include <vlib/unix/unix.h>
43 #include <vppinfra/callback.h>
44 #include <vppinfra/cpu.h>
45 #include <vppinfra/elog.h>
49 /** \file src/vlib/cli.c Debug CLI Implementation
52 int vl_api_set_elog_trace_api_messages (int enable);
53 int vl_api_get_elog_trace_api_messages (void);
55 static void *current_traced_heap;
57 /* Root of all show commands. */
59 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
61 .short_help = "Show commands",
65 /* Root of all clear commands. */
67 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
69 .short_help = "Clear commands",
73 /* Root of all set commands. */
75 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
77 .short_help = "Set commands",
81 /* Root of all test commands. */
83 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
85 .short_help = "Test commands",
89 /* Returns bitmap of commands which match key. */
91 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
95 vlib_cli_parse_position_t *p;
97 unformat_skip_white_space (input);
103 k = unformat_get_input (input);
117 case UNFORMAT_END_OF_INPUT:
118 /* White space or end of input removes any non-white
119 matches that were before possible. */
120 if (i < vec_len (c->sub_command_positions)
121 && clib_bitmap_count_set_bits (match) > 1)
123 p = vec_elt_at_index (c->sub_command_positions, i);
124 for (n = 0; n < vec_len (p->bitmaps); n++)
125 match = clib_bitmap_andnot (match, p->bitmaps[n]);
130 unformat_put_input (input);
134 if (i >= vec_len (c->sub_command_positions))
137 clib_bitmap_free (match);
141 p = vec_elt_at_index (c->sub_command_positions, i);
142 if (vec_len (p->bitmaps) == 0)
146 if (n < 0 || n >= vec_len (p->bitmaps))
150 match = clib_bitmap_dup (p->bitmaps[n]);
152 match = clib_bitmap_and (match, p->bitmaps[n]);
154 if (clib_bitmap_is_zero (match))
163 unformat_vlib_cli_line (unformat_input_t *i, va_list *va)
165 unformat_input_t *result = va_arg (*va, unformat_input_t *);
173 /* skip leading whitespace if any */
174 unformat_skip_white_space (i);
176 if (unformat_is_eof (i))
179 while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT)
183 c = unformat_get_input (i);
188 vec_add1 (line, '\n');
194 vec_add1 (line, '\\');
196 if (c == UNFORMAT_END_OF_INPUT)
216 unformat_init_vector (result, line);
220 /* Looks for string based sub-input formatted { SUB-INPUT }. */
222 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
224 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
230 c = unformat_get_input (i);
242 /* Put back paren. */
243 if (c != UNFORMAT_END_OF_INPUT)
244 unformat_put_input (i);
246 if (c == '{' && unformat (i, "%v", &s))
248 unformat_init_vector (sub_input, s);
257 static vlib_cli_command_t *
258 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
260 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
261 return vec_elt_at_index (cm->commands, s->index);
265 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
267 vlib_main_t __clib_unused *vm = va_arg (*args, vlib_main_t *);
268 vlib_global_main_t *vgm = vlib_get_global_main ();
269 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
270 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
271 vlib_cli_main_t *cm = &vgm->cli_main;
272 uword *match_bitmap, is_unique, index;
274 match_bitmap = vlib_cli_sub_command_match (c, i);
275 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
279 index = clib_bitmap_first_set (match_bitmap);
280 *result = get_sub_command (cm, c, index);
282 clib_bitmap_free (match_bitmap);
288 vlib_cli_cmp_strings (void *a1, void *a2)
290 u8 *c1 = *(u8 **) a1;
291 u8 *c2 = *(u8 **) a2;
293 return vec_cmp (c1, c2);
297 vlib_cli_get_possible_completions (u8 * str)
299 vlib_cli_command_t *c;
300 vlib_cli_sub_command_t *sc;
301 vlib_global_main_t *vgm = vlib_get_global_main ();
302 vlib_cli_main_t *vcm = &vgm->cli_main;
303 uword *match_bitmap = 0;
304 uword index, is_unique, help_next_level;
306 unformat_input_t input;
307 unformat_init_vector (&input, vec_dup (str));
308 c = vec_elt_at_index (vcm->commands, 0);
310 /* remove trailing whitespace, except for one of them */
311 while (vec_len (input.buffer) >= 2 &&
312 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
313 isspace (input.buffer[vec_len (input.buffer) - 2]))
315 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
318 /* if input is empty, directly return list of root commands */
319 if (vec_len (input.buffer) == 0 ||
320 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
322 vec_foreach (sc, c->sub_commands)
324 vec_add1 (result, (u8 *) sc->name);
329 /* add a trailing '?' so that vlib_cli_sub_command_match can find
330 * all commands starting with the input string */
331 vec_add1 (input.buffer, '?');
335 match_bitmap = vlib_cli_sub_command_match (c, &input);
336 /* no match: return no result */
337 if (match_bitmap == 0)
341 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
342 /* unique match: try to step one subcommand level further */
345 /* stop if no more input */
346 if (input.index >= vec_len (input.buffer) - 1)
351 index = clib_bitmap_first_set (match_bitmap);
352 c = get_sub_command (vcm, c, index);
353 clib_bitmap_free (match_bitmap);
356 /* multiple matches: stop here, return all matches */
360 /* remove trailing '?' */
361 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
363 /* if we have a space at the end of input, and a unique match,
364 * autocomplete the next level of subcommands */
365 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
367 clib_bitmap_foreach (index, match_bitmap) {
368 if (help_next_level && is_unique) {
369 c = get_sub_command (vcm, c, index);
370 vec_foreach (sc, c->sub_commands) {
371 vec_add1 (result, (u8*) sc->name);
373 goto done; /* break doesn't work in this macro-loop */
375 sc = &c->sub_commands[index];
376 vec_add1(result, (u8*) sc->name);
381 clib_bitmap_free (match_bitmap);
382 unformat_free (&input);
385 vec_sort_with_function (result, vlib_cli_cmp_strings);
390 format_vlib_cli_command_help (u8 * s, va_list * args)
392 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
393 int is_long = va_arg (*args, int);
394 if (is_long && c->long_help)
395 s = format (s, "%s", c->long_help);
396 else if (c->short_help)
397 s = format (s, "%s", c->short_help);
399 s = format (s, "%v commands", c->path);
404 format_vlib_cli_path (u8 * s, va_list * args)
406 u8 *path = va_arg (*args, u8 *);
408 s = format (s, "%v", path);
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;
420 vec_add1 (subs, c[0]);
422 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
428 vlib_cli_cmp_rule (void *a1, void *a2)
430 vlib_cli_sub_rule_t *r1 = a1;
431 vlib_cli_sub_rule_t *r2 = a2;
433 return vec_cmp (r1->name, r2->name);
437 vlib_cli_cmp_command (void *a1, void *a2)
439 vlib_cli_command_t *c1 = a1;
440 vlib_cli_command_t *c2 = a2;
442 return vec_cmp (c1->path, c2->path);
445 static clib_error_t *
446 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
447 vlib_cli_main_t * cm,
448 unformat_input_t * input,
449 uword parent_command_index)
451 vlib_global_main_t *vgm = vlib_get_global_main ();
452 vlib_cli_command_t *parent, *c;
453 clib_error_t *error = 0;
454 unformat_input_t sub_input;
456 uword is_main_dispatch = cm == &vgm->cli_main;
458 parent = vec_elt_at_index (cm->commands, parent_command_index);
459 if (is_main_dispatch && unformat (input, "help"))
461 uword help_at_end_of_line, i;
463 help_at_end_of_line =
464 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
469 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
472 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
479 /* help SUB-COMMAND => long format help.
480 "help" at end of line: show all commands. */
481 if (!help_at_end_of_line)
482 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
485 else if (vec_len (c->sub_commands) == 0)
486 vlib_cli_output (vm, "%v: no sub-commands", c->path);
490 vlib_cli_sub_rule_t *sr, *subs = 0;
491 vlib_cli_sub_command_t *sc;
493 vec_foreach (sc, c->sub_commands)
495 vec_add2 (subs, sr, 1);
497 sr->command_index = sc->index;
501 vec_sort_with_function (subs, vlib_cli_cmp_rule);
503 for (i = 0; i < vec_len (subs); i++)
505 vlib_cli_command_t *d;
507 d = vec_elt_at_index (cm->commands, subs[i].command_index);
509 (vm, " %-30v %U", subs[i].name,
510 format_vlib_cli_command_help, d, /* is_long */ 0);
517 else if (is_main_dispatch
518 && (unformat (input, "choices") || unformat (input, "?")))
520 vlib_cli_command_t *sub, *subs;
522 subs = all_subs (cm, 0, parent_command_index);
523 vec_sort_with_function (subs, vlib_cli_cmp_command);
524 vec_foreach (sub, subs)
525 vlib_cli_output (vm, " %-40U %U",
526 format_vlib_cli_path, sub->path,
527 format_vlib_cli_command_help, sub, /* is_long */ 0);
531 else if (unformat (input, "comment %v", &string))
536 else if (unformat (input, "vpplog %v", &string))
540 * Delete leading whitespace, so "vpplog { this and that }"
541 * and "vpplog this" line up nicely.
543 for (i = 0; i < vec_len (string); i++)
544 if (string[i] != ' ')
547 vec_delete (string, i, 0);
549 vlib_log_notice (cm->log, "CLI: %v", string);
553 else if (unformat (input, "uncomment %U",
554 unformat_vlib_cli_sub_input, &sub_input))
557 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
558 parent_command_index);
559 unformat_free (&sub_input);
561 else if (unformat (input, "leak-check %U",
562 unformat_vlib_cli_sub_input, &sub_input))
565 if (current_traced_heap)
568 oldheap = clib_mem_set_heap (current_traced_heap);
570 clib_mem_set_heap (oldheap);
571 current_traced_heap = 0;
575 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
576 parent_command_index);
577 unformat_free (&sub_input);
579 /* Otherwise, the clib_error_t shows up as a leak... */
582 vlib_cli_output (vm, "%v", error->what);
583 clib_error_free (error);
587 (void) clib_mem_trace_enable_disable (0);
588 leak_report = format (0, "%U", format_clib_mem_heap, 0,
589 1 /* verbose, i.e. print leaks */ );
591 vlib_cli_output (vm, "%v", leak_report);
592 vec_free (leak_report);
596 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
598 unformat_input_t *si;
599 uword has_sub_commands =
600 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
603 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
606 if (has_sub_commands)
607 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
609 if (has_sub_commands && !error)
610 /* Found valid sub-command. */ ;
612 else if (c->function)
614 clib_error_t *c_error;
616 /* Skip white space for benefit of called function. */
617 unformat_skip_white_space (si);
619 if (unformat (si, "?"))
621 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
626 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
629 ELOG_TYPE_DECLARE (e) =
631 .format = "cli-cmd: %s",
639 ed = ELOG_DATA (vlib_get_elog_main (), e);
640 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
644 vlib_worker_thread_barrier_sync (vm);
645 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
646 clib_call_callbacks (cm->perf_counter_cbs, cm,
647 c - cm->commands, 0 /* before */ );
650 c_error = c->function (vm, si, c);
652 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
653 clib_call_callbacks (cm->perf_counter_cbs, cm,
654 c - cm->commands, 1 /* after */ );
656 vlib_worker_thread_barrier_release (vm);
658 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
661 ELOG_TYPE_DECLARE (e) =
663 .format = "cli-cmd: %s %s",
664 .format_args = "T4T4",
671 ed = ELOG_DATA (vlib_get_elog_main (), e);
672 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
675 vec_add1 (c_error->what, 0);
676 ed->err = elog_string (vlib_get_elog_main (),
677 (char *) c_error->what);
678 vec_dec_len (c_error->what, 1);
681 ed->err = elog_string (vlib_get_elog_main (), "OK");
687 clib_error_return (0, "%v: %v", c->path, c_error->what);
688 clib_error_free (c_error);
689 /* Free sub input. */
697 /* Free any previous error. */
698 clib_error_free (error);
702 error = clib_error_return (0, "%v: no sub-commands", c->path);
704 /* Free sub input. */
716 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
717 format_unformat_error, input);
719 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
724 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
725 __attribute__ ((weak));
728 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
732 /* Process CLI input. */
734 vlib_cli_input (vlib_main_t * vm,
735 unformat_input_t * input,
736 vlib_cli_output_function_t * function, uword function_arg)
738 vlib_global_main_t *vgm = vlib_get_global_main ();
739 vlib_process_t *cp = vlib_get_current_process (vm);
741 vlib_cli_output_function_t *save_function;
742 uword save_function_arg;
745 save_function = cp->output_function;
746 save_function_arg = cp->output_function_arg;
748 cp->output_function = function;
749 cp->output_function_arg = function_arg;
753 error = vlib_cli_dispatch_sub_commands (vm, &vgm->cli_main, input,
756 while (!error && !unformat (input, "%U", unformat_eof));
760 vlib_cli_output (vm, "%v", error->what);
761 vlib_unix_error_report (vm, error);
762 /* clib_error_return is unfortunately often called with a '0'
764 rv = error->code != 0 ? error->code : -1;
765 clib_error_free (error);
768 cp->output_function = save_function;
769 cp->output_function_arg = save_function_arg;
773 /* Output to current CLI connection. */
775 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
777 vlib_process_t *cp = vlib_get_current_process (vm);
782 s = va_format (0, fmt, &va);
785 /* some format functions might return 0
786 * e.g. show int addr */
790 /* Terminate with \n if not present. */
791 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
794 if ((!cp) || (!cp->output_function))
795 fformat (stdout, "%v", s);
797 cp->output_function (cp->output_function_arg, s, vec_len (s));
802 void *vl_msg_push_heap (void) __attribute__ ((weak));
804 vl_msg_push_heap (void)
809 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
811 vl_msg_pop_heap (void *oldheap)
815 static clib_error_t *
816 show_memory_usage (vlib_main_t * vm,
817 unformat_input_t * input, vlib_cli_command_t * cmd)
819 clib_mem_main_t *mm = &clib_mem_main;
820 int verbose __attribute__ ((unused)) = 0;
821 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
826 uword clib_mem_trace_enable_disable (uword enable);
830 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
832 if (unformat (input, "verbose"))
834 else if (unformat (input, "api-segment"))
836 else if (unformat (input, "stats-segment"))
838 else if (unformat (input, "main-heap"))
840 else if (unformat (input, "numa-heaps"))
842 else if (unformat (input, "map"))
846 error = clib_error_return (0, "unknown input `%U'",
847 format_unformat_error, input);
852 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
853 return clib_error_return
854 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
859 void *oldheap = vl_msg_push_heap ();
860 was_enabled = clib_mem_trace_enable_disable (0);
861 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
862 vl_msg_pop_heap (oldheap);
863 u8 *s = vec_dup (s_in_svm);
865 oldheap = vl_msg_push_heap ();
867 clib_mem_trace_enable_disable (was_enabled);
868 vl_msg_pop_heap (oldheap);
869 vlib_cli_output (vm, "API segment");
870 vlib_cli_output (vm, "%v", s);
875 void *oldheap = vlib_stats_set_heap ();
876 was_enabled = clib_mem_trace_enable_disable (0);
877 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
879 clib_mem_set_heap (oldheap);
880 u8 *s = vec_dup (s_in_svm);
882 oldheap = vlib_stats_set_heap ();
886 clib_mem_trace_enable_disable (was_enabled);
887 clib_mem_set_heap (oldheap);
889 vlib_cli_output (vm, "Stats segment");
890 vlib_cli_output (vm, "%v", s);
899 * Note: the foreach_vlib_main causes allocator traffic,
900 * so shut off tracing before we go there...
902 was_enabled = clib_mem_trace_enable_disable (0);
906 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n" : "", index,
907 vlib_worker_threads[index].name);
908 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
909 mm->per_cpu_mheaps[index], verbose);
913 /* Restore the trace flag */
914 clib_mem_trace_enable_disable (was_enabled);
918 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
920 if (mm->per_numa_mheaps[i] == 0)
922 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
924 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
927 was_enabled = clib_mem_trace_enable_disable (0);
929 vlib_cli_output (vm, "Numa %d:", i);
930 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
931 mm->per_numa_mheaps[index], verbose);
936 clib_mem_page_stats_t stats = { };
937 clib_mem_vm_map_hdr_t *hdr = 0;
941 s = format (s, "\n%-16s%7s%5s%7s%7s",
942 "StartAddr", "size", "FD", "PageSz", "Pages");
943 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
944 s = format (s, " Numa%u", numa);
945 s = format (s, " NotMap");
946 s = format (s, " Name");
947 vlib_cli_output (vm, "%v", s);
948 vec_reset_length (s);
950 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
952 clib_mem_get_page_stats ((void *) hdr->base_addr,
953 hdr->log2_page_sz, hdr->num_pages,
955 s = format (s, "%016lx%7U",
956 hdr->base_addr, format_memory_size,
957 hdr->num_pages << hdr->log2_page_sz);
960 s = format (s, "%5d", hdr->fd);
962 s = format (s, "%5s", " ");
964 s = format (s, "%7U%7lu",
965 format_log2_page_size, hdr->log2_page_sz,
967 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
968 s = format (s, "%6lu", stats.per_numa[numa]);
969 s = format (s, "%7lu", stats.not_mapped);
970 s = format (s, " %s", hdr->name);
971 vlib_cli_output (vm, "%v", s);
972 vec_reset_length (s);
981 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
982 .path = "show memory",
983 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
984 " [numa-heaps][map][main-heap]",
985 .function = show_memory_usage,
989 static clib_error_t *
990 show_cpu (vlib_main_t * vm, unformat_input_t * input,
991 vlib_cli_command_t * cmd)
993 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
994 _("Model name", "%U", format_cpu_model_name);
995 _("Microarch model (family)", "%U", format_cpu_uarch);
996 _("Flags", "%U", format_cpu_flags);
997 _("Base frequency", "%.2f GHz",
998 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
1004 * Displays various information about the CPU.
1007 * @cliexstart{show cpu}
1008 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
1009 * Microarchitecture: Broadwell (Broadwell-EP/EX)
1010 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
1011 * Base Frequency: 3.20 GHz
1015 VLIB_CLI_COMMAND (show_cpu_command, static) = {
1017 .short_help = "Show cpu information",
1018 .function = show_cpu,
1022 static clib_error_t *
1023 enable_disable_memory_trace (vlib_main_t * vm,
1024 unformat_input_t * input,
1025 vlib_cli_command_t * cmd)
1027 clib_mem_main_t *mm = &clib_mem_main;
1028 unformat_input_t _line_input, *line_input = &_line_input;
1030 int api_segment = 0;
1031 int stats_segment = 0;
1036 if (!unformat_user (input, unformat_line_input, line_input))
1039 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1041 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
1043 else if (unformat (line_input, "api-segment"))
1045 else if (unformat (line_input, "stats-segment"))
1047 else if (unformat (line_input, "main-heap"))
1049 else if (unformat (line_input, "numa-heap %d", &numa_id))
1053 unformat_free (line_input);
1054 return clib_error_return (0, "invalid input");
1057 unformat_free (line_input);
1059 if ((api_segment + stats_segment + main_heap + (enable == 0)
1060 + (numa_id != ~0)) == 0)
1062 return clib_error_return
1063 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1064 "numa-heap <nn> or disable");
1067 /* Turn off current trace, if any */
1068 if (current_traced_heap)
1071 oldheap = clib_mem_set_heap (current_traced_heap);
1073 clib_mem_set_heap (oldheap);
1074 current_traced_heap = 0;
1083 oldheap = vl_msg_push_heap ();
1084 current_traced_heap = clib_mem_get_heap ();
1086 vl_msg_pop_heap (oldheap);
1093 oldheap = vlib_stats_set_heap ();
1094 current_traced_heap = clib_mem_get_heap ();
1095 clib_mem_trace (stats_segment);
1096 /* We don't want to call vlib_stats_pop_heap... */
1098 clib_mem_set_heap (oldheap);
1104 current_traced_heap = clib_mem_get_heap ();
1105 clib_mem_trace (main_heap);
1110 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
1111 return clib_error_return (0, "Numa %d out of range", numa_id);
1112 if (mm->per_numa_mheaps[numa_id] == 0)
1113 return clib_error_return (0, "Numa %d heap not active", numa_id);
1115 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
1116 return clib_error_return (0, "Numa %d uses the main heap...",
1118 current_traced_heap = mm->per_numa_mheaps[numa_id];
1119 oldheap = clib_mem_set_heap (current_traced_heap);
1121 clib_mem_set_heap (oldheap);
1129 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1130 .path = "memory-trace",
1131 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1132 " [numa-heap <numa-id>]\n",
1133 .function = enable_disable_memory_trace,
1137 static clib_error_t *
1138 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1139 vlib_cli_command_t * cmd)
1141 vlib_global_main_t *vgm = vlib_get_global_main ();
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 (vgm->name, (char **) vgm->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 /* "Can't happen," check mainly to shut up coverity */
1280 ALWAYS_ASSERT (sub_name != 0);
1282 if (sub_name[0] == '%')
1285 vlib_cli_sub_rule_t *sr;
1288 vec_delete (sub_name, 1, 0);
1290 if (!p->sub_rule_index_by_name)
1291 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1292 sizeof (sub_name[0]),
1294 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1297 sr = vec_elt_at_index (p->sub_rules, q[0]);
1298 ASSERT (sr->command_index == child_index);
1302 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1303 vec_len (p->sub_rules));
1304 vec_add2 (p->sub_rules, sr, 1);
1305 sr->name = sub_name;
1306 sr->rule_index = sr - p->sub_rules;
1307 sr->command_index = child_index;
1311 if (!p->sub_command_index_by_name)
1312 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1313 sizeof (c->path[0]),
1316 /* Check if sub-command has already been created. */
1317 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1319 vec_free (sub_name);
1323 vec_add2 (p->sub_commands, sub_c, 1);
1324 sub_c->index = child_index;
1325 sub_c->name = sub_name;
1326 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1327 sub_c - p->sub_commands);
1329 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1330 for (i = 0; i < vec_len (sub_c->name); i++)
1333 vlib_cli_parse_position_t *pos;
1335 pos = vec_elt_at_index (p->sub_command_positions, i);
1338 pos->min_char = sub_c->name[i];
1340 n = sub_c->name[i] - pos->min_char;
1343 pos->min_char = sub_c->name[i];
1344 vec_insert (pos->bitmaps, -n, 0);
1348 vec_validate (pos->bitmaps, n);
1350 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1355 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1357 uword p_len, pi, *p;
1359 vlib_cli_command_t *c, *parent;
1361 /* Root command (index 0) should have already been added. */
1362 ASSERT (vec_len (cm->commands) > 0);
1364 c = vec_elt_at_index (cm->commands, ci);
1365 p_len = parent_path_len (c->path);
1367 /* No space? Parent is root command. */
1370 add_sub_command (cm, 0, ci);
1375 vec_add (p_path, c->path, p_len);
1377 p = hash_get_mem (cm->command_index_by_path, p_path);
1379 /* Parent exists? */
1382 /* Parent does not exist; create it. */
1383 vec_add2 (cm->commands, parent, 1);
1384 parent->path = p_path;
1385 hash_set_mem (cm->command_index_by_path, parent->path,
1386 parent - cm->commands);
1387 pi = parent - cm->commands;
1395 add_sub_command (cm, pi, ci);
1397 /* Create parent's parent. */
1399 vlib_cli_make_parent (cm, pi);
1403 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1405 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1409 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1411 vlib_global_main_t *vgm = vlib_get_global_main ();
1412 vlib_cli_main_t *cm = &vgm->cli_main;
1413 clib_error_t *error = 0;
1415 char *normalized_path;
1417 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1420 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1422 if (!cm->command_index_by_path)
1423 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1424 sizeof (c->path[0]),
1427 /* See if command already exists with given path. */
1428 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1431 vlib_cli_command_t *d;
1434 d = vec_elt_at_index (cm->commands, ci);
1436 /* If existing command was created via vlib_cli_make_parent
1437 replaced it with callers data. */
1438 if (vlib_cli_command_is_empty (d))
1440 vlib_cli_command_t save = d[0];
1442 ASSERT (!vlib_cli_command_is_empty (c));
1444 /* Copy callers fields. */
1447 /* Save internal fields. */
1448 d->path = save.path;
1449 d->sub_commands = save.sub_commands;
1450 d->sub_command_index_by_name = save.sub_command_index_by_name;
1451 d->sub_command_positions = save.sub_command_positions;
1452 d->sub_rules = save.sub_rules;
1456 clib_error_return (0, "duplicate command name with path %v",
1459 vec_free (normalized_path);
1465 /* Command does not exist: create it. */
1467 /* Add root command (index 0). */
1468 if (vec_len (cm->commands) == 0)
1470 /* Create command with index 0; path is empty string. */
1471 vec_resize (cm->commands, 1);
1474 ci = vec_len (cm->commands);
1475 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1476 vec_add1 (cm->commands, c[0]);
1478 c = vec_elt_at_index (cm->commands, ci);
1479 c->path = normalized_path;
1481 /* Don't inherit from registration. */
1482 c->sub_commands = 0;
1483 c->sub_command_index_by_name = 0;
1484 c->sub_command_positions = 0;
1487 vlib_cli_make_parent (cm, ci);
1492 /* $$$ turn back on again someday, maybe */
1494 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1496 vlib_cli_main_t *cm = &vm->cli_main;
1497 vlib_cli_parse_rule_t *r;
1498 clib_error_t *error = 0;
1502 if (!cm->parse_rule_index_by_name)
1503 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1504 sizeof (r->name[0]),
1507 /* Make vector copy of name. */
1508 r_name = format (0, "%s", r_reg->name);
1510 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1513 return clib_error_return (0, "duplicate parse rule name `%s'",
1517 vec_add2 (cm->parse_rules, r, 1);
1519 r->name = (char *) r_name;
1520 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1525 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1526 vlib_cli_parse_rule_t *
1528 vlib_cli_parse_rule_t *
1530 __attribute__ ((unused))
1532 clib_error_t *error = 0;
1533 vlib_cli_parse_rule_t *r;
1535 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1537 if (!r->name || strlen (r->name) == 0)
1539 error = clib_error_return (0, "parse rule with no name");
1543 error = vlib_cli_register_parse_rule (vm, r);
1553 static clib_error_t *
1554 event_logger_trace_command_fn (vlib_main_t * vm,
1555 unformat_input_t * input,
1556 vlib_cli_command_t * cmd)
1558 unformat_input_t _line_input, *line_input = &_line_input;
1560 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1561 u32 circuit_node_index;
1563 if (!unformat_user (input, unformat_line_input, line_input))
1566 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1568 if (unformat (line_input, "api"))
1570 else if (unformat (line_input, "dispatch"))
1572 else if (unformat (line_input, "circuit-node %U",
1573 unformat_vlib_node, vm, &circuit_node_index))
1575 else if (unformat (line_input, "cli"))
1577 else if (unformat (line_input, "barrier"))
1579 else if (unformat (line_input, "disable"))
1581 else if (unformat (line_input, "enable"))
1586 unformat_free (line_input);
1588 vl_api_set_elog_trace_api_messages
1589 (api ? enable : vl_api_get_elog_trace_api_messages ());
1590 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1591 vm->elog_trace_graph_dispatch = dispatch ?
1592 enable : vm->elog_trace_graph_dispatch;
1593 vm->elog_trace_graph_circuit = circuit ?
1594 enable : vm->elog_trace_graph_circuit;
1595 vlib_worker_threads->barrier_elog_enabled =
1596 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1597 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1600 * Set up start-of-buffer logic-analyzer trigger
1601 * for main loop event logs, which are fairly heavyweight.
1602 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1603 * will fully disable the scheme when the elog buffer fills.
1605 if (dispatch || circuit)
1607 elog_main_t *em = &vlib_global_main.elog_main;
1609 em->n_total_events_disable_limit =
1610 em->n_total_events + vec_len (em->event_ring);
1615 vlib_cli_output (vm, "Current status:");
1618 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1619 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1620 vm->elog_trace_cli_commands ? "on" : "off");
1622 (vm, " Barrier sync trace: %s",
1623 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1625 (vm, " Graph Dispatch: %s",
1626 vm->elog_trace_graph_dispatch ? "on" : "off");
1628 (vm, " Graph Circuit: %s",
1629 vm->elog_trace_graph_circuit ? "on" : "off");
1630 if (vm->elog_trace_graph_circuit)
1633 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1639 * Control event logging of api, cli, and thread barrier events
1640 * With no arguments, displays the current trace status.
1641 * Name the event groups you wish to trace or stop tracing.
1645 * event-logger trace api cli barrier
1646 * event-logger trace api cli barrier disable
1647 * event-logger trace dispatch
1648 * event-logger trace circuit-node ethernet-input
1650 * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
1653 VLIB_CLI_COMMAND (event_logger_trace_command, static) =
1655 .path = "event-logger trace",
1656 .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
1657 "[circuit-node <name> e.g. ethernet-input][disable]",
1658 .function = event_logger_trace_command_fn,
1662 static clib_error_t *
1663 suspend_command_fn (vlib_main_t * vm,
1664 unformat_input_t * input, vlib_cli_command_t * cmd)
1666 vlib_process_suspend (vm, 30e-3);
1671 VLIB_CLI_COMMAND (suspend_command, static) =
1674 .short_help = "suspend debug CLI for 30ms",
1675 .function = suspend_command_fn,
1682 sort_cmds_by_path (void *a1, void *a2)
1686 vlib_global_main_t *vgm = vlib_get_global_main ();
1687 vlib_cli_main_t *cm = &vgm->cli_main;
1688 vlib_cli_command_t *c1, *c2;
1691 c1 = vec_elt_at_index (cm->commands, *index1);
1692 c2 = vec_elt_at_index (cm->commands, *index2);
1694 lmin = vec_len (c1->path);
1695 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1697 for (i = 0; i < lmin; i++)
1699 if (c1->path[i] < c2->path[i])
1701 else if (c1->path[i] > c2->path[i])
1710 vlib_cli_main_t *cm;
1711 u32 parent_command_index;
1713 int show_not_mp_safe;
1716 } vlib_cli_walk_args_t;
1719 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1721 vlib_cli_command_t *parent;
1722 vlib_cli_sub_command_t *sub;
1723 vlib_cli_walk_args_t _a, *a = &_a;
1724 vlib_cli_main_t *cm;
1727 /* Copy args into this stack frame */
1731 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1733 if (parent->function)
1735 if (((a->show_mp_safe && parent->is_mp_safe)
1736 || (a->show_not_mp_safe && !parent->is_mp_safe))
1737 && (a->show_hit == 0 || parent->hit_counter))
1739 vec_add1 (cm->sort_vector, a->parent_command_index);
1743 parent->hit_counter = 0;
1746 for (i = 0; i < vec_len (parent->sub_commands); i++)
1748 sub = vec_elt_at_index (parent->sub_commands, i);
1749 a->parent_command_index = sub->index;
1750 cli_recursive_walk (a);
1755 format_mp_safe (u8 * s, va_list * args)
1757 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1758 int show_mp_safe = va_arg (*args, int);
1759 int show_not_mp_safe = va_arg (*args, int);
1760 int show_hit = va_arg (*args, int);
1761 int clear_hit = va_arg (*args, int);
1762 vlib_cli_command_t *c;
1763 vlib_cli_walk_args_t _a, *a = &_a;
1765 char *format_string = "\n%v";
1768 format_string = "\n%v: %u";
1770 vec_reset_length (cm->sort_vector);
1773 a->parent_command_index = 0;
1774 a->show_mp_safe = show_mp_safe;
1775 a->show_not_mp_safe = show_not_mp_safe;
1776 a->show_hit = show_hit;
1777 a->clear_hit = clear_hit;
1779 cli_recursive_walk (a);
1781 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1783 for (i = 0; i < vec_len (cm->sort_vector); i++)
1785 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1786 s = format (s, format_string, c->path, c->hit_counter);
1793 static clib_error_t *
1794 show_cli_command_fn (vlib_main_t * vm,
1795 unformat_input_t * input, vlib_cli_command_t * cmd)
1797 vlib_global_main_t *vgm = vlib_get_global_main ();
1798 int show_mp_safe = 0;
1799 int show_not_mp_safe = 0;
1803 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1805 if (unformat (input, "mp-safe"))
1807 if (unformat (input, "not-mp-safe"))
1808 show_not_mp_safe = 1;
1809 else if (unformat (input, "hit"))
1811 else if (unformat (input, "clear-hit"))
1817 /* default set: all cli commands */
1818 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1819 show_mp_safe = show_not_mp_safe = 1;
1821 vlib_cli_output (vm, "%U", format_mp_safe, &vgm->cli_main, show_mp_safe,
1822 show_not_mp_safe, show_hit, clear_hit);
1824 vlib_cli_output (vm, "hit counters cleared...");
1830 * Displays debug cli command information
1833 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1835 * "show cli" displays the entire debug cli:
1839 * adjacency counters
1845 * "show cli mp-safe" displays mp-safe debug CLI commands:
1859 * "show cli not-mp-safe" displays debug CLI commands
1860 * which cause worker thread barrier synchronization
1862 * "show cli hit" displays commands which have been executed. Qualify
1863 * as desired with "mp-safe" or "not-mp-safe".
1865 * "show cli clear-hit" clears the per-command hit counters.
1870 VLIB_CLI_COMMAND (show_cli_command, static) =
1873 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1874 .function = show_cli_command_fn,
1879 static clib_error_t *
1880 vlib_cli_init (vlib_main_t * vm)
1882 vlib_global_main_t *vgm = vlib_get_global_main ();
1883 vlib_cli_main_t *cm = &vgm->cli_main;
1884 clib_error_t *error = 0;
1885 vlib_cli_command_t *cmd;
1887 cmd = cm->cli_command_registrations;
1891 error = vlib_cli_register (vm, cmd);
1894 cmd = cmd->next_cli_command;
1897 cm->log = vlib_log_register_class_rate_limit (
1898 "cli", "log", 0x7FFFFFFF /* aka no rate limit */);
1902 VLIB_INIT_FUNCTION (vlib_cli_init);
1905 * fd.io coding-style-patch-verification: ON
1908 * eval: (c-set-style "gnu")