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))
162 /* Get current command args */
164 unformat_vlib_cli_args (unformat_input_t *i, va_list *va)
166 unformat_input_t *result = va_arg (*va, unformat_input_t *);
169 u32 index = i->index;
171 if (unformat_is_eof (i))
173 unformat_init (result, 0, 0);
177 /* try to find last non-space character */
181 last_c = i->buffer[--index];
183 while (last_c == ' ');
185 if (last_c == '\t' || last_c == '\n' || last_c == '\r' || last_c == '\f' ||
188 /* current command has no args */
189 unformat_init (result, 0, 0);
193 if (!unformat_user (i, unformat_line, &line))
195 unformat_init (result, 0, 0);
198 unformat_init_vector (result, line);
203 unformat_vlib_cli_line (unformat_input_t *i, va_list *va)
205 unformat_input_t *result = va_arg (*va, unformat_input_t *);
213 /* skip leading whitespace if any */
214 unformat_skip_white_space (i);
216 if (unformat_is_eof (i))
219 while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT)
223 c = unformat_get_input (i);
228 vec_add1 (line, '\n');
234 vec_add1 (line, '\\');
236 if (c == UNFORMAT_END_OF_INPUT)
256 unformat_init_vector (result, line);
260 /* Looks for string based sub-input formatted { SUB-INPUT }. */
262 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
264 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
270 c = unformat_get_input (i);
282 /* Put back paren. */
283 if (c != UNFORMAT_END_OF_INPUT)
284 unformat_put_input (i);
286 if (c == '{' && unformat (i, "%v", &s))
288 unformat_init_vector (sub_input, s);
297 static vlib_cli_command_t *
298 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
300 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
301 return vec_elt_at_index (cm->commands, s->index);
305 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
307 vlib_main_t __clib_unused *vm = va_arg (*args, vlib_main_t *);
308 vlib_global_main_t *vgm = vlib_get_global_main ();
309 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
310 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
311 vlib_cli_main_t *cm = &vgm->cli_main;
312 uword *match_bitmap, is_unique, index;
314 match_bitmap = vlib_cli_sub_command_match (c, i);
315 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
319 index = clib_bitmap_first_set (match_bitmap);
320 *result = get_sub_command (cm, c, index);
322 clib_bitmap_free (match_bitmap);
328 vlib_cli_cmp_strings (void *a1, void *a2)
330 u8 *c1 = *(u8 **) a1;
331 u8 *c2 = *(u8 **) a2;
333 return vec_cmp (c1, c2);
337 vlib_cli_get_possible_completions (u8 * str)
339 vlib_cli_command_t *c;
340 vlib_cli_sub_command_t *sc;
341 vlib_global_main_t *vgm = vlib_get_global_main ();
342 vlib_cli_main_t *vcm = &vgm->cli_main;
343 uword *match_bitmap = 0;
344 uword index, is_unique, help_next_level;
346 unformat_input_t input;
347 unformat_init_vector (&input, vec_dup (str));
348 c = vec_elt_at_index (vcm->commands, 0);
350 /* remove trailing whitespace, except for one of them */
351 while (vec_len (input.buffer) >= 2 &&
352 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
353 isspace (input.buffer[vec_len (input.buffer) - 2]))
355 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
358 /* if input is empty, directly return list of root commands */
359 if (vec_len (input.buffer) == 0 ||
360 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
362 vec_foreach (sc, c->sub_commands)
364 vec_add1 (result, (u8 *) sc->name);
369 /* add a trailing '?' so that vlib_cli_sub_command_match can find
370 * all commands starting with the input string */
371 vec_add1 (input.buffer, '?');
375 match_bitmap = vlib_cli_sub_command_match (c, &input);
376 /* no match: return no result */
377 if (match_bitmap == 0)
381 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
382 /* unique match: try to step one subcommand level further */
385 /* stop if no more input */
386 if (input.index >= vec_len (input.buffer) - 1)
391 index = clib_bitmap_first_set (match_bitmap);
392 c = get_sub_command (vcm, c, index);
393 clib_bitmap_free (match_bitmap);
396 /* multiple matches: stop here, return all matches */
400 /* remove trailing '?' */
401 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
403 /* if we have a space at the end of input, and a unique match,
404 * autocomplete the next level of subcommands */
405 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
407 clib_bitmap_foreach (index, match_bitmap) {
408 if (help_next_level && is_unique) {
409 c = get_sub_command (vcm, c, index);
410 vec_foreach (sc, c->sub_commands) {
411 vec_add1 (result, (u8*) sc->name);
413 goto done; /* break doesn't work in this macro-loop */
415 sc = &c->sub_commands[index];
416 vec_add1(result, (u8*) sc->name);
421 clib_bitmap_free (match_bitmap);
422 unformat_free (&input);
425 vec_sort_with_function (result, vlib_cli_cmp_strings);
430 format_vlib_cli_command_help (u8 * s, va_list * args)
432 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
433 int is_long = va_arg (*args, int);
434 if (is_long && c->long_help)
435 s = format (s, "%s", c->long_help);
436 else if (c->short_help)
437 s = format (s, "%s", c->short_help);
439 s = format (s, "%v commands", c->path);
444 format_vlib_cli_path (u8 * s, va_list * args)
446 u8 *path = va_arg (*args, u8 *);
448 s = format (s, "%v", path);
453 static vlib_cli_command_t *
454 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
456 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
457 vlib_cli_sub_command_t *sc;
460 vec_add1 (subs, c[0]);
462 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
468 vlib_cli_cmp_rule (void *a1, void *a2)
470 vlib_cli_sub_rule_t *r1 = a1;
471 vlib_cli_sub_rule_t *r2 = a2;
473 return vec_cmp (r1->name, r2->name);
477 vlib_cli_cmp_command (void *a1, void *a2)
479 vlib_cli_command_t *c1 = a1;
480 vlib_cli_command_t *c2 = a2;
482 return vec_cmp (c1->path, c2->path);
485 static clib_error_t *
486 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
487 vlib_cli_main_t * cm,
488 unformat_input_t * input,
489 uword parent_command_index)
491 vlib_global_main_t *vgm = vlib_get_global_main ();
492 vlib_cli_command_t *parent, *c;
493 clib_error_t *error = 0;
494 unformat_input_t sub_input;
496 uword is_main_dispatch = cm == &vgm->cli_main;
498 parent = vec_elt_at_index (cm->commands, parent_command_index);
499 if (is_main_dispatch && unformat (input, "help"))
501 uword help_at_end_of_line, i;
503 help_at_end_of_line =
504 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
509 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
512 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
519 /* help SUB-COMMAND => long format help.
520 "help" at end of line: show all commands. */
521 if (!help_at_end_of_line)
522 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
525 else if (vec_len (c->sub_commands) == 0)
526 vlib_cli_output (vm, "%v: no sub-commands", c->path);
530 vlib_cli_sub_rule_t *sr, *subs = 0;
531 vlib_cli_sub_command_t *sc;
533 vec_foreach (sc, c->sub_commands)
535 vec_add2 (subs, sr, 1);
537 sr->command_index = sc->index;
541 vec_sort_with_function (subs, vlib_cli_cmp_rule);
543 for (i = 0; i < vec_len (subs); i++)
545 vlib_cli_command_t *d;
547 d = vec_elt_at_index (cm->commands, subs[i].command_index);
549 (vm, " %-30v %U", subs[i].name,
550 format_vlib_cli_command_help, d, /* is_long */ 0);
557 else if (is_main_dispatch
558 && (unformat (input, "choices") || unformat (input, "?")))
560 vlib_cli_command_t *sub, *subs;
562 subs = all_subs (cm, 0, parent_command_index);
563 vec_sort_with_function (subs, vlib_cli_cmp_command);
564 vec_foreach (sub, subs)
565 vlib_cli_output (vm, " %-40U %U",
566 format_vlib_cli_path, sub->path,
567 format_vlib_cli_command_help, sub, /* is_long */ 0);
571 else if (unformat (input, "comment %v", &string))
576 else if (unformat (input, "vpplog %v", &string))
580 * Delete leading whitespace, so "vpplog { this and that }"
581 * and "vpplog this" line up nicely.
583 for (i = 0; i < vec_len (string); i++)
584 if (string[i] != ' ')
587 vec_delete (string, i, 0);
589 vlib_log_notice (cm->log, "CLI: %v", string);
593 else if (unformat (input, "uncomment %U",
594 unformat_vlib_cli_sub_input, &sub_input))
597 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
598 parent_command_index);
599 unformat_free (&sub_input);
601 else if (unformat (input, "leak-check %U",
602 unformat_vlib_cli_sub_input, &sub_input))
605 if (current_traced_heap)
608 oldheap = clib_mem_set_heap (current_traced_heap);
610 clib_mem_set_heap (oldheap);
611 current_traced_heap = 0;
615 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
616 parent_command_index);
617 unformat_free (&sub_input);
619 /* Otherwise, the clib_error_t shows up as a leak... */
622 vlib_cli_output (vm, "%v", error->what);
623 clib_error_free (error);
627 (void) clib_mem_trace_enable_disable (0);
628 leak_report = format (0, "%U", format_clib_mem_heap, 0,
629 1 /* verbose, i.e. print leaks */ );
631 vlib_cli_output (vm, "%v", leak_report);
632 vec_free (leak_report);
636 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
638 unformat_input_t *si;
639 uword has_sub_commands =
640 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
643 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
646 if (has_sub_commands)
647 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
649 if (has_sub_commands && !error)
650 /* Found valid sub-command. */ ;
652 else if (c->function)
654 clib_error_t *c_error;
656 /* Skip white space for benefit of called function. */
657 unformat_skip_white_space (si);
659 if (unformat (si, "?"))
661 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
666 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
669 ELOG_TYPE_DECLARE (e) =
671 .format = "cli-cmd: %s",
679 ed = ELOG_DATA (vlib_get_elog_main (), e);
680 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
684 vlib_worker_thread_barrier_sync (vm);
685 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
686 clib_call_callbacks (cm->perf_counter_cbs, cm,
687 c - cm->commands, 0 /* before */ );
690 c_error = c->function (vm, si, c);
692 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
693 clib_call_callbacks (cm->perf_counter_cbs, cm,
694 c - cm->commands, 1 /* after */ );
696 vlib_worker_thread_barrier_release (vm);
698 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
701 ELOG_TYPE_DECLARE (e) =
703 .format = "cli-cmd: %s %s",
704 .format_args = "T4T4",
711 ed = ELOG_DATA (vlib_get_elog_main (), e);
712 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
715 vec_add1 (c_error->what, 0);
716 ed->err = elog_string (vlib_get_elog_main (),
717 (char *) c_error->what);
718 vec_dec_len (c_error->what, 1);
721 ed->err = elog_string (vlib_get_elog_main (), "OK");
727 clib_error_return (0, "%v: %v", c->path, c_error->what);
728 clib_error_free (c_error);
729 /* Free sub input. */
737 /* Free any previous error. */
738 clib_error_free (error);
742 error = clib_error_return (0, "%v: no sub-commands", c->path);
744 /* Free sub input. */
756 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
757 format_unformat_error, input);
759 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
764 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
765 __attribute__ ((weak));
768 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
772 /* Process CLI input. */
774 vlib_cli_input (vlib_main_t * vm,
775 unformat_input_t * input,
776 vlib_cli_output_function_t * function, uword function_arg)
778 vlib_global_main_t *vgm = vlib_get_global_main ();
779 vlib_process_t *cp = vlib_get_current_process (vm);
781 vlib_cli_output_function_t *save_function;
782 uword save_function_arg;
785 save_function = cp->output_function;
786 save_function_arg = cp->output_function_arg;
788 cp->output_function = function;
789 cp->output_function_arg = function_arg;
793 error = vlib_cli_dispatch_sub_commands (vm, &vgm->cli_main, input,
796 while (!error && !unformat (input, "%U", unformat_eof));
800 vlib_cli_output (vm, "%v", error->what);
801 vlib_unix_error_report (vm, error);
802 /* clib_error_return is unfortunately often called with a '0'
804 rv = error->code != 0 ? error->code : -1;
805 clib_error_free (error);
808 cp->output_function = save_function;
809 cp->output_function_arg = save_function_arg;
813 /* Output to current CLI connection. */
815 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
817 vlib_process_t *cp = vlib_get_current_process (vm);
822 s = va_format (0, fmt, &va);
825 /* some format functions might return 0
826 * e.g. show int addr */
830 /* Terminate with \n if not present. */
831 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
834 if ((!cp) || (!cp->output_function))
835 fformat (stdout, "%v", s);
837 cp->output_function (cp->output_function_arg, s, vec_len (s));
842 void *vl_msg_push_heap (void) __attribute__ ((weak));
844 vl_msg_push_heap (void)
849 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
851 vl_msg_pop_heap (void *oldheap)
855 static clib_error_t *
856 show_memory_usage (vlib_main_t * vm,
857 unformat_input_t * input, vlib_cli_command_t * cmd)
859 clib_mem_main_t *mm = &clib_mem_main;
860 int verbose __attribute__ ((unused)) = 0;
861 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
866 uword clib_mem_trace_enable_disable (uword enable);
870 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
872 if (unformat (input, "verbose"))
874 else if (unformat (input, "api-segment"))
876 else if (unformat (input, "stats-segment"))
878 else if (unformat (input, "main-heap"))
880 else if (unformat (input, "numa-heaps"))
882 else if (unformat (input, "map"))
886 error = clib_error_return (0, "unknown input `%U'",
887 format_unformat_error, input);
892 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
893 return clib_error_return
894 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
899 void *oldheap = vl_msg_push_heap ();
900 was_enabled = clib_mem_trace_enable_disable (0);
901 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
902 vl_msg_pop_heap (oldheap);
903 u8 *s = vec_dup (s_in_svm);
905 oldheap = vl_msg_push_heap ();
907 clib_mem_trace_enable_disable (was_enabled);
908 vl_msg_pop_heap (oldheap);
909 vlib_cli_output (vm, "API segment");
910 vlib_cli_output (vm, "%v", s);
915 void *oldheap = vlib_stats_set_heap (0);
916 was_enabled = clib_mem_trace_enable_disable (0);
917 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
919 clib_mem_set_heap (oldheap);
920 u8 *s = vec_dup (s_in_svm);
922 oldheap = vlib_stats_set_heap (0);
926 clib_mem_trace_enable_disable (was_enabled);
927 clib_mem_set_heap (oldheap);
929 vlib_cli_output (vm, "Stats segment");
930 vlib_cli_output (vm, "%v", s);
939 * Note: the foreach_vlib_main causes allocator traffic,
940 * so shut off tracing before we go there...
942 was_enabled = clib_mem_trace_enable_disable (0);
946 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n" : "", index,
947 vlib_worker_threads[index].name);
948 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
949 mm->per_cpu_mheaps[index], verbose);
953 /* Restore the trace flag */
954 clib_mem_trace_enable_disable (was_enabled);
958 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
960 if (mm->per_numa_mheaps[i] == 0)
962 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
964 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
967 was_enabled = clib_mem_trace_enable_disable (0);
969 vlib_cli_output (vm, "Numa %d:", i);
970 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
971 mm->per_numa_mheaps[index], verbose);
976 clib_mem_page_stats_t stats = { };
977 clib_mem_vm_map_hdr_t *hdr = 0;
981 s = format (s, "\n%-16s%7s%5s%7s%7s",
982 "StartAddr", "size", "FD", "PageSz", "Pages");
983 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
984 s = format (s, " Numa%u", numa);
985 s = format (s, " NotMap");
986 s = format (s, " Name");
987 vlib_cli_output (vm, "%v", s);
988 vec_reset_length (s);
990 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
992 clib_mem_get_page_stats ((void *) hdr->base_addr,
993 hdr->log2_page_sz, hdr->num_pages,
995 s = format (s, "%016lx%7U",
996 hdr->base_addr, format_memory_size,
997 hdr->num_pages << hdr->log2_page_sz);
1000 s = format (s, "%5d", hdr->fd);
1002 s = format (s, "%5s", " ");
1004 s = format (s, "%7U%7lu",
1005 format_log2_page_size, hdr->log2_page_sz,
1007 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
1008 s = format (s, "%6lu", stats.per_numa[numa]);
1009 s = format (s, "%7lu", stats.not_mapped);
1010 s = format (s, " %s", hdr->name);
1011 vlib_cli_output (vm, "%v", s);
1012 vec_reset_length (s);
1021 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
1022 .path = "show memory",
1023 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
1024 " [numa-heaps][map]",
1025 .function = show_memory_usage,
1029 static clib_error_t *
1030 show_cpu (vlib_main_t * vm, unformat_input_t * input,
1031 vlib_cli_command_t * cmd)
1033 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
1034 _("Model name", "%U", format_cpu_model_name);
1035 _("Microarch model (family)", "%U", format_cpu_uarch);
1036 _("Flags", "%U", format_cpu_flags);
1037 _("Base frequency", "%.2f GHz",
1038 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
1044 * Displays various information about the CPU.
1047 * @cliexstart{show cpu}
1048 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
1049 * Microarchitecture: Broadwell (Broadwell-EP/EX)
1050 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
1051 * Base Frequency: 3.20 GHz
1055 VLIB_CLI_COMMAND (show_cpu_command, static) = {
1057 .short_help = "Show cpu information",
1058 .function = show_cpu,
1062 static clib_error_t *
1063 enable_disable_memory_trace (vlib_main_t * vm,
1064 unformat_input_t * input,
1065 vlib_cli_command_t * cmd)
1067 clib_mem_main_t *mm = &clib_mem_main;
1068 unformat_input_t _line_input, *line_input = &_line_input;
1070 int api_segment = 0;
1071 int stats_segment = 0;
1076 if (!unformat_user (input, unformat_line_input, line_input))
1079 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1081 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
1083 else if (unformat (line_input, "api-segment"))
1085 else if (unformat (line_input, "stats-segment"))
1087 else if (unformat (line_input, "main-heap"))
1089 else if (unformat (line_input, "numa-heap %d", &numa_id))
1093 unformat_free (line_input);
1094 return clib_error_return (0, "invalid input");
1097 unformat_free (line_input);
1099 if ((api_segment + stats_segment + main_heap + (enable == 0)
1100 + (numa_id != ~0)) == 0)
1102 return clib_error_return
1103 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1104 "numa-heap <nn> or disable");
1107 /* Turn off current trace, if any */
1108 if (current_traced_heap)
1111 oldheap = clib_mem_set_heap (current_traced_heap);
1113 clib_mem_set_heap (oldheap);
1114 current_traced_heap = 0;
1123 oldheap = vl_msg_push_heap ();
1124 current_traced_heap = clib_mem_get_heap ();
1126 vl_msg_pop_heap (oldheap);
1133 oldheap = vlib_stats_set_heap ();
1134 current_traced_heap = clib_mem_get_heap ();
1135 clib_mem_trace (stats_segment);
1136 /* We don't want to call vlib_stats_pop_heap... */
1138 clib_mem_set_heap (oldheap);
1144 current_traced_heap = clib_mem_get_heap ();
1145 clib_mem_trace (main_heap);
1150 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
1151 return clib_error_return (0, "Numa %d out of range", numa_id);
1152 if (mm->per_numa_mheaps[numa_id] == 0)
1153 return clib_error_return (0, "Numa %d heap not active", numa_id);
1155 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
1156 return clib_error_return (0, "Numa %d uses the main heap...",
1158 current_traced_heap = mm->per_numa_mheaps[numa_id];
1159 oldheap = clib_mem_set_heap (current_traced_heap);
1161 clib_mem_set_heap (oldheap);
1169 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1170 .path = "memory-trace",
1171 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1172 " [numa-heap <numa-id>]\n",
1173 .function = enable_disable_memory_trace,
1177 static clib_error_t *
1178 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1179 vlib_cli_command_t * cmd)
1181 vlib_global_main_t *vgm = vlib_get_global_main ();
1182 clib_file_main_t *fm = &file_main;
1185 /* environ(7) does not indicate a header for this */
1186 extern char **environ;
1188 /* Close all known open files */
1190 pool_foreach (f, fm->file_pool)
1192 if (f->file_descriptor > 2)
1193 close(f->file_descriptor);
1198 execve (vgm->name, (char **) vgm->argv, environ);
1204 VLIB_CLI_COMMAND (restart_cmd,static) = {
1206 .short_help = "restart process",
1207 .function = restart_cmd_fn,
1213 * A trivial test harness to verify the per-process output_function
1214 * is working correcty.
1217 static clib_error_t *
1218 sleep_ten_seconds (vlib_main_t * vm,
1219 unformat_input_t * input, vlib_cli_command_t * cmd)
1222 u16 my_id = rand ();
1224 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1226 for (i = 0; i < 10; i++)
1228 vlib_process_wait_for_event_or_clock (vm, 1.0);
1229 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1231 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1236 VLIB_CLI_COMMAND (ping_command, static) = {
1237 .path = "test sleep",
1238 .function = sleep_ten_seconds,
1239 .short_help = "Sleep for 10 seconds",
1242 #endif /* ifdef TEST_CODE */
1245 vlib_cli_normalize_path (char *input, char **result)
1250 uword index_of_last_space = ~0;
1255 /* Multiple white space -> single space. */
1262 if (l > 0 && s[l - 1] != ' ')
1270 if (l > 0 && s[l - 1] == ' ')
1271 index_of_last_space = vec_len (s);
1278 /* Remove any extra space at end. */
1279 if (l > 0 && s[l - 1] == ' ')
1283 return index_of_last_space;
1287 parent_path_len (char *path)
1290 for (i = vec_len (path) - 1; i >= 0; i--)
1299 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1301 vlib_cli_command_t *p, *c;
1302 vlib_cli_sub_command_t *sub_c;
1306 p = vec_elt_at_index (cm->commands, parent_index);
1307 c = vec_elt_at_index (cm->commands, child_index);
1309 l = parent_path_len (c->path);
1311 sub_name = vec_dup ((u8 *) c->path);
1314 ASSERT (l + 1 < vec_len (c->path));
1316 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1319 /* "Can't happen," check mainly to shut up coverity */
1320 ALWAYS_ASSERT (sub_name != 0);
1322 if (sub_name[0] == '%')
1325 vlib_cli_sub_rule_t *sr;
1328 vec_delete (sub_name, 1, 0);
1330 if (!p->sub_rule_index_by_name)
1331 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1332 sizeof (sub_name[0]),
1334 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1337 sr = vec_elt_at_index (p->sub_rules, q[0]);
1338 ASSERT (sr->command_index == child_index);
1342 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1343 vec_len (p->sub_rules));
1344 vec_add2 (p->sub_rules, sr, 1);
1345 sr->name = sub_name;
1346 sr->rule_index = sr - p->sub_rules;
1347 sr->command_index = child_index;
1351 if (!p->sub_command_index_by_name)
1352 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1353 sizeof (c->path[0]),
1356 /* Check if sub-command has already been created. */
1357 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1359 vec_free (sub_name);
1363 vec_add2 (p->sub_commands, sub_c, 1);
1364 sub_c->index = child_index;
1365 sub_c->name = sub_name;
1366 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1367 sub_c - p->sub_commands);
1369 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1370 for (i = 0; i < vec_len (sub_c->name); i++)
1373 vlib_cli_parse_position_t *pos;
1375 pos = vec_elt_at_index (p->sub_command_positions, i);
1378 pos->min_char = sub_c->name[i];
1380 n = sub_c->name[i] - pos->min_char;
1383 pos->min_char = sub_c->name[i];
1384 vec_insert (pos->bitmaps, -n, 0);
1388 vec_validate (pos->bitmaps, n);
1390 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1395 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1397 uword p_len, pi, *p;
1399 vlib_cli_command_t *c, *parent;
1401 /* Root command (index 0) should have already been added. */
1402 ASSERT (vec_len (cm->commands) > 0);
1404 c = vec_elt_at_index (cm->commands, ci);
1405 p_len = parent_path_len (c->path);
1407 /* No space? Parent is root command. */
1410 add_sub_command (cm, 0, ci);
1415 vec_add (p_path, c->path, p_len);
1417 p = hash_get_mem (cm->command_index_by_path, p_path);
1419 /* Parent exists? */
1422 /* Parent does not exist; create it. */
1423 vec_add2 (cm->commands, parent, 1);
1424 parent->path = p_path;
1425 hash_set_mem (cm->command_index_by_path, parent->path,
1426 parent - cm->commands);
1427 pi = parent - cm->commands;
1435 add_sub_command (cm, pi, ci);
1437 /* Create parent's parent. */
1439 vlib_cli_make_parent (cm, pi);
1443 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1445 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1449 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1451 vlib_global_main_t *vgm = vlib_get_global_main ();
1452 vlib_cli_main_t *cm = &vgm->cli_main;
1453 clib_error_t *error = 0;
1455 char *normalized_path;
1457 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1460 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1462 if (!cm->command_index_by_path)
1463 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1464 sizeof (c->path[0]),
1467 /* See if command already exists with given path. */
1468 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1471 vlib_cli_command_t *d;
1474 d = vec_elt_at_index (cm->commands, ci);
1476 /* If existing command was created via vlib_cli_make_parent
1477 replaced it with callers data. */
1478 if (vlib_cli_command_is_empty (d))
1480 vlib_cli_command_t save = d[0];
1482 ASSERT (!vlib_cli_command_is_empty (c));
1484 /* Copy callers fields. */
1487 /* Save internal fields. */
1488 d->path = save.path;
1489 d->sub_commands = save.sub_commands;
1490 d->sub_command_index_by_name = save.sub_command_index_by_name;
1491 d->sub_command_positions = save.sub_command_positions;
1492 d->sub_rules = save.sub_rules;
1496 clib_error_return (0, "duplicate command name with path %v",
1499 vec_free (normalized_path);
1505 /* Command does not exist: create it. */
1507 /* Add root command (index 0). */
1508 if (vec_len (cm->commands) == 0)
1510 /* Create command with index 0; path is empty string. */
1511 vec_resize (cm->commands, 1);
1514 ci = vec_len (cm->commands);
1515 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1516 vec_add1 (cm->commands, c[0]);
1518 c = vec_elt_at_index (cm->commands, ci);
1519 c->path = normalized_path;
1521 /* Don't inherit from registration. */
1522 c->sub_commands = 0;
1523 c->sub_command_index_by_name = 0;
1524 c->sub_command_positions = 0;
1527 vlib_cli_make_parent (cm, ci);
1532 /* $$$ turn back on again someday, maybe */
1534 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1536 vlib_cli_main_t *cm = &vm->cli_main;
1537 vlib_cli_parse_rule_t *r;
1538 clib_error_t *error = 0;
1542 if (!cm->parse_rule_index_by_name)
1543 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1544 sizeof (r->name[0]),
1547 /* Make vector copy of name. */
1548 r_name = format (0, "%s", r_reg->name);
1550 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1553 return clib_error_return (0, "duplicate parse rule name `%s'",
1557 vec_add2 (cm->parse_rules, r, 1);
1559 r->name = (char *) r_name;
1560 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1565 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1566 vlib_cli_parse_rule_t *
1568 vlib_cli_parse_rule_t *
1570 __attribute__ ((unused))
1572 clib_error_t *error = 0;
1573 vlib_cli_parse_rule_t *r;
1575 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1577 if (!r->name || strlen (r->name) == 0)
1579 error = clib_error_return (0, "parse rule with no name");
1583 error = vlib_cli_register_parse_rule (vm, r);
1593 static clib_error_t *
1594 event_logger_trace_command_fn (vlib_main_t * vm,
1595 unformat_input_t * input,
1596 vlib_cli_command_t * cmd)
1598 unformat_input_t _line_input, *line_input = &_line_input;
1600 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1601 u32 circuit_node_index;
1603 if (!unformat_user (input, unformat_line_input, line_input))
1606 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1608 if (unformat (line_input, "api"))
1610 else if (unformat (line_input, "dispatch"))
1612 else if (unformat (line_input, "circuit-node %U",
1613 unformat_vlib_node, vm, &circuit_node_index))
1615 else if (unformat (line_input, "cli"))
1617 else if (unformat (line_input, "barrier"))
1619 else if (unformat (line_input, "disable"))
1621 else if (unformat (line_input, "enable"))
1626 unformat_free (line_input);
1628 vl_api_set_elog_trace_api_messages
1629 (api ? enable : vl_api_get_elog_trace_api_messages ());
1630 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1631 vm->elog_trace_graph_dispatch = dispatch ?
1632 enable : vm->elog_trace_graph_dispatch;
1633 vm->elog_trace_graph_circuit = circuit ?
1634 enable : vm->elog_trace_graph_circuit;
1635 vlib_worker_threads->barrier_elog_enabled =
1636 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1637 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1640 * Set up start-of-buffer logic-analyzer trigger
1641 * for main loop event logs, which are fairly heavyweight.
1642 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1643 * will fully disable the scheme when the elog buffer fills.
1645 if (dispatch || circuit)
1647 elog_main_t *em = &vlib_global_main.elog_main;
1649 em->n_total_events_disable_limit =
1650 em->n_total_events + vec_len (em->event_ring);
1655 vlib_cli_output (vm, "Current status:");
1658 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1659 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1660 vm->elog_trace_cli_commands ? "on" : "off");
1662 (vm, " Barrier sync trace: %s",
1663 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1665 (vm, " Graph Dispatch: %s",
1666 vm->elog_trace_graph_dispatch ? "on" : "off");
1668 (vm, " Graph Circuit: %s",
1669 vm->elog_trace_graph_circuit ? "on" : "off");
1670 if (vm->elog_trace_graph_circuit)
1673 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1679 * Control event logging of api, cli, and thread barrier events
1680 * With no arguments, displays the current trace status.
1681 * Name the event groups you wish to trace or stop tracing.
1685 * event-logger trace api cli barrier
1686 * event-logger trace api cli barrier disable
1687 * event-logger trace dispatch
1688 * event-logger trace circuit-node ethernet-input
1690 * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
1693 VLIB_CLI_COMMAND (event_logger_trace_command, static) =
1695 .path = "event-logger trace",
1696 .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
1697 "[circuit-node <name> e.g. ethernet-input][disable]",
1698 .function = event_logger_trace_command_fn,
1702 static clib_error_t *
1703 suspend_command_fn (vlib_main_t * vm,
1704 unformat_input_t * input, vlib_cli_command_t * cmd)
1706 vlib_process_suspend (vm, 30e-3);
1711 VLIB_CLI_COMMAND (suspend_command, static) =
1714 .short_help = "suspend debug CLI for 30ms",
1715 .function = suspend_command_fn,
1722 sort_cmds_by_path (void *a1, void *a2)
1726 vlib_global_main_t *vgm = vlib_get_global_main ();
1727 vlib_cli_main_t *cm = &vgm->cli_main;
1728 vlib_cli_command_t *c1, *c2;
1731 c1 = vec_elt_at_index (cm->commands, *index1);
1732 c2 = vec_elt_at_index (cm->commands, *index2);
1734 lmin = vec_len (c1->path);
1735 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1737 for (i = 0; i < lmin; i++)
1739 if (c1->path[i] < c2->path[i])
1741 else if (c1->path[i] > c2->path[i])
1750 vlib_cli_main_t *cm;
1751 u32 parent_command_index;
1753 int show_not_mp_safe;
1756 } vlib_cli_walk_args_t;
1759 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1761 vlib_cli_command_t *parent;
1762 vlib_cli_sub_command_t *sub;
1763 vlib_cli_walk_args_t _a, *a = &_a;
1764 vlib_cli_main_t *cm;
1767 /* Copy args into this stack frame */
1771 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1773 if (parent->function)
1775 if (((a->show_mp_safe && parent->is_mp_safe)
1776 || (a->show_not_mp_safe && !parent->is_mp_safe))
1777 && (a->show_hit == 0 || parent->hit_counter))
1779 vec_add1 (cm->sort_vector, a->parent_command_index);
1783 parent->hit_counter = 0;
1786 for (i = 0; i < vec_len (parent->sub_commands); i++)
1788 sub = vec_elt_at_index (parent->sub_commands, i);
1789 a->parent_command_index = sub->index;
1790 cli_recursive_walk (a);
1795 format_mp_safe (u8 * s, va_list * args)
1797 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1798 int show_mp_safe = va_arg (*args, int);
1799 int show_not_mp_safe = va_arg (*args, int);
1800 int show_hit = va_arg (*args, int);
1801 int clear_hit = va_arg (*args, int);
1802 vlib_cli_command_t *c;
1803 vlib_cli_walk_args_t _a, *a = &_a;
1805 char *format_string = "\n%v";
1808 format_string = "\n%v: %u";
1810 vec_reset_length (cm->sort_vector);
1813 a->parent_command_index = 0;
1814 a->show_mp_safe = show_mp_safe;
1815 a->show_not_mp_safe = show_not_mp_safe;
1816 a->show_hit = show_hit;
1817 a->clear_hit = clear_hit;
1819 cli_recursive_walk (a);
1821 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1823 for (i = 0; i < vec_len (cm->sort_vector); i++)
1825 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1826 s = format (s, format_string, c->path, c->hit_counter);
1833 static clib_error_t *
1834 show_cli_command_fn (vlib_main_t * vm,
1835 unformat_input_t * input, vlib_cli_command_t * cmd)
1837 vlib_global_main_t *vgm = vlib_get_global_main ();
1838 int show_mp_safe = 0;
1839 int show_not_mp_safe = 0;
1843 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1845 if (unformat (input, "mp-safe"))
1847 if (unformat (input, "not-mp-safe"))
1848 show_not_mp_safe = 1;
1849 else if (unformat (input, "hit"))
1851 else if (unformat (input, "clear-hit"))
1857 /* default set: all cli commands */
1858 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1859 show_mp_safe = show_not_mp_safe = 1;
1861 vlib_cli_output (vm, "%U", format_mp_safe, &vgm->cli_main, show_mp_safe,
1862 show_not_mp_safe, show_hit, clear_hit);
1864 vlib_cli_output (vm, "hit counters cleared...");
1870 * Displays debug cli command information
1873 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1875 * "show cli" displays the entire debug cli:
1879 * adjacency counters
1885 * "show cli mp-safe" displays mp-safe debug CLI commands:
1899 * "show cli not-mp-safe" displays debug CLI commands
1900 * which cause worker thread barrier synchronization
1902 * "show cli hit" displays commands which have been executed. Qualify
1903 * as desired with "mp-safe" or "not-mp-safe".
1905 * "show cli clear-hit" clears the per-command hit counters.
1910 VLIB_CLI_COMMAND (show_cli_command, static) =
1913 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1914 .function = show_cli_command_fn,
1919 static clib_error_t *
1920 vlib_cli_init (vlib_main_t * vm)
1922 vlib_global_main_t *vgm = vlib_get_global_main ();
1923 vlib_cli_main_t *cm = &vgm->cli_main;
1924 clib_error_t *error = 0;
1925 vlib_cli_command_t *cmd;
1927 cmd = cm->cli_command_registrations;
1931 error = vlib_cli_register (vm, cmd);
1934 cmd = cmd->next_cli_command;
1937 cm->log = vlib_log_register_class_rate_limit (
1938 "cli", "log", 0x7FFFFFFF /* aka no rate limit */);
1942 VLIB_INIT_FUNCTION (vlib_cli_init);
1945 * fd.io coding-style-patch-verification: ON
1948 * eval: (c-set-style "gnu")