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/callback.h>
43 #include <vppinfra/cpu.h>
44 #include <vppinfra/elog.h>
48 /** \file src/vlib/cli.c Debug CLI Implementation
51 int vl_api_set_elog_trace_api_messages (int enable);
52 int vl_api_get_elog_trace_api_messages (void);
54 static void *current_traced_heap;
56 /* Root of all show commands. */
58 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
60 .short_help = "Show commands",
64 /* Root of all clear commands. */
66 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
68 .short_help = "Clear commands",
72 /* Root of all set commands. */
74 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
76 .short_help = "Set commands",
80 /* Root of all test commands. */
82 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
84 .short_help = "Test commands",
88 /* Returns bitmap of commands which match key. */
90 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
94 vlib_cli_parse_position_t *p;
96 unformat_skip_white_space (input);
102 k = unformat_get_input (input);
116 case UNFORMAT_END_OF_INPUT:
117 /* White space or end of input removes any non-white
118 matches that were before possible. */
119 if (i < vec_len (c->sub_command_positions)
120 && clib_bitmap_count_set_bits (match) > 1)
122 p = vec_elt_at_index (c->sub_command_positions, i);
123 for (n = 0; n < vec_len (p->bitmaps); n++)
124 match = clib_bitmap_andnot (match, p->bitmaps[n]);
129 unformat_put_input (input);
133 if (i >= vec_len (c->sub_command_positions))
136 clib_bitmap_free (match);
140 p = vec_elt_at_index (c->sub_command_positions, i);
141 if (vec_len (p->bitmaps) == 0)
145 if (n < 0 || n >= vec_len (p->bitmaps))
149 match = clib_bitmap_dup (p->bitmaps[n]);
151 match = clib_bitmap_and (match, p->bitmaps[n]);
153 if (clib_bitmap_is_zero (match))
161 /* Looks for string based sub-input formatted { SUB-INPUT }. */
163 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
165 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
171 c = unformat_get_input (i);
183 /* Put back paren. */
184 if (c != UNFORMAT_END_OF_INPUT)
185 unformat_put_input (i);
187 if (c == '{' && unformat (i, "%v", &s))
189 unformat_init_vector (sub_input, s);
198 static vlib_cli_command_t *
199 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
201 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
202 return vec_elt_at_index (cm->commands, s->index);
206 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
208 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
209 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
210 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
211 vlib_cli_main_t *cm = &vm->cli_main;
212 uword *match_bitmap, is_unique, index;
214 match_bitmap = vlib_cli_sub_command_match (c, i);
215 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
219 index = clib_bitmap_first_set (match_bitmap);
220 *result = get_sub_command (cm, c, index);
222 clib_bitmap_free (match_bitmap);
228 vlib_cli_cmp_strings (void *a1, void *a2)
230 u8 *c1 = *(u8 **) a1;
231 u8 *c2 = *(u8 **) a2;
233 return vec_cmp (c1, c2);
237 vlib_cli_get_possible_completions (u8 * str)
239 vlib_cli_command_t *c;
240 vlib_cli_sub_command_t *sc;
241 vlib_main_t *vm = vlib_get_main ();
242 vlib_cli_main_t *vcm = &vm->cli_main;
243 uword *match_bitmap = 0;
244 uword index, is_unique, help_next_level;
246 unformat_input_t input;
247 unformat_init_vector (&input, vec_dup (str));
248 c = vec_elt_at_index (vcm->commands, 0);
250 /* remove trailing whitespace, except for one of them */
251 while (vec_len (input.buffer) >= 2 &&
252 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
253 isspace (input.buffer[vec_len (input.buffer) - 2]))
255 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
258 /* if input is empty, directly return list of root commands */
259 if (vec_len (input.buffer) == 0 ||
260 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
262 vec_foreach (sc, c->sub_commands)
264 vec_add1 (result, (u8 *) sc->name);
269 /* add a trailing '?' so that vlib_cli_sub_command_match can find
270 * all commands starting with the input string */
271 vec_add1 (input.buffer, '?');
275 match_bitmap = vlib_cli_sub_command_match (c, &input);
276 /* no match: return no result */
277 if (match_bitmap == 0)
281 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
282 /* unique match: try to step one subcommand level further */
285 /* stop if no more input */
286 if (input.index >= vec_len (input.buffer) - 1)
291 index = clib_bitmap_first_set (match_bitmap);
292 c = get_sub_command (vcm, c, index);
293 clib_bitmap_free (match_bitmap);
296 /* multiple matches: stop here, return all matches */
300 /* remove trailing '?' */
301 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
303 /* if we have a space at the end of input, and a unique match,
304 * autocomplete the next level of subcommands */
305 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
307 clib_bitmap_foreach(index, match_bitmap, {
308 if (help_next_level && is_unique) {
309 c = get_sub_command (vcm, c, index);
310 vec_foreach (sc, c->sub_commands) {
311 vec_add1 (result, (u8*) sc->name);
313 goto done; /* break doesn't work in this macro-loop */
315 sc = &c->sub_commands[index];
316 vec_add1(result, (u8*) sc->name);
321 clib_bitmap_free (match_bitmap);
322 unformat_free (&input);
325 vec_sort_with_function (result, vlib_cli_cmp_strings);
330 format_vlib_cli_command_help (u8 * s, va_list * args)
332 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
333 int is_long = va_arg (*args, int);
334 if (is_long && c->long_help)
335 s = format (s, "%s", c->long_help);
336 else if (c->short_help)
337 s = format (s, "%s", c->short_help);
339 s = format (s, "%v commands", c->path);
344 format_vlib_cli_path (u8 * s, va_list * args)
346 u8 *path = va_arg (*args, u8 *);
348 s = format (s, "%v", path);
353 static vlib_cli_command_t *
354 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
356 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
357 vlib_cli_sub_command_t *sc;
360 vec_add1 (subs, c[0]);
362 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
368 vlib_cli_cmp_rule (void *a1, void *a2)
370 vlib_cli_sub_rule_t *r1 = a1;
371 vlib_cli_sub_rule_t *r2 = a2;
373 return vec_cmp (r1->name, r2->name);
377 vlib_cli_cmp_command (void *a1, void *a2)
379 vlib_cli_command_t *c1 = a1;
380 vlib_cli_command_t *c2 = a2;
382 return vec_cmp (c1->path, c2->path);
385 static clib_error_t *
386 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
387 vlib_cli_main_t * cm,
388 unformat_input_t * input,
389 uword parent_command_index)
391 vlib_cli_command_t *parent, *c;
392 clib_error_t *error = 0;
393 unformat_input_t sub_input;
395 uword is_main_dispatch = cm == &vm->cli_main;
397 parent = vec_elt_at_index (cm->commands, parent_command_index);
398 if (is_main_dispatch && unformat (input, "help"))
400 uword help_at_end_of_line, i;
402 help_at_end_of_line =
403 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
408 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
411 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
418 /* help SUB-COMMAND => long format help.
419 "help" at end of line: show all commands. */
420 if (!help_at_end_of_line)
421 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
424 else if (vec_len (c->sub_commands) == 0)
425 vlib_cli_output (vm, "%v: no sub-commands", c->path);
429 vlib_cli_sub_rule_t *sr, *subs = 0;
430 vlib_cli_sub_command_t *sc;
432 vec_foreach (sc, c->sub_commands)
434 vec_add2 (subs, sr, 1);
436 sr->command_index = sc->index;
440 vec_sort_with_function (subs, vlib_cli_cmp_rule);
442 for (i = 0; i < vec_len (subs); i++)
444 vlib_cli_command_t *d;
446 d = vec_elt_at_index (cm->commands, subs[i].command_index);
448 (vm, " %-30v %U", subs[i].name,
449 format_vlib_cli_command_help, d, /* is_long */ 0);
456 else if (is_main_dispatch
457 && (unformat (input, "choices") || unformat (input, "?")))
459 vlib_cli_command_t *sub, *subs;
461 subs = all_subs (cm, 0, parent_command_index);
462 vec_sort_with_function (subs, vlib_cli_cmp_command);
463 vec_foreach (sub, subs)
464 vlib_cli_output (vm, " %-40U %U",
465 format_vlib_cli_path, sub->path,
466 format_vlib_cli_command_help, sub, /* is_long */ 0);
470 else if (unformat (input, "comment %v", &string))
475 else if (unformat (input, "uncomment %U",
476 unformat_vlib_cli_sub_input, &sub_input))
479 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
480 parent_command_index);
481 unformat_free (&sub_input);
483 else if (unformat (input, "leak-check %U",
484 unformat_vlib_cli_sub_input, &sub_input))
487 if (current_traced_heap)
490 oldheap = clib_mem_set_heap (current_traced_heap);
492 clib_mem_set_heap (oldheap);
493 current_traced_heap = 0;
497 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
498 parent_command_index);
499 unformat_free (&sub_input);
501 /* Otherwise, the clib_error_t shows up as a leak... */
504 vlib_cli_output (vm, "%v", error->what);
505 clib_error_free (error);
509 (void) clib_mem_trace_enable_disable (0);
510 leak_report = format (0, "%U", format_clib_mem_heap, 0,
511 1 /* verbose, i.e. print leaks */ );
513 vlib_cli_output (vm, "%v", leak_report);
514 vec_free (leak_report);
518 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
520 unformat_input_t *si;
521 uword has_sub_commands =
522 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
525 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
528 if (has_sub_commands)
529 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
531 if (has_sub_commands && !error)
532 /* Found valid sub-command. */ ;
534 else if (c->function)
536 clib_error_t *c_error;
538 /* Skip white space for benefit of called function. */
539 unformat_skip_white_space (si);
541 if (unformat (si, "?"))
543 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
548 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
551 ELOG_TYPE_DECLARE (e) =
553 .format = "cli-cmd: %s",
561 ed = ELOG_DATA (&vm->elog_main, e);
562 ed->c = elog_string (&vm->elog_main, "%v", c->path);
566 vlib_worker_thread_barrier_sync (vm);
567 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
568 clib_call_callbacks (cm->perf_counter_cbs, cm,
569 c - cm->commands, 0 /* before */ );
572 c_error = c->function (vm, si, c);
574 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
575 clib_call_callbacks (cm->perf_counter_cbs, cm,
576 c - cm->commands, 1 /* after */ );
578 vlib_worker_thread_barrier_release (vm);
580 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
583 ELOG_TYPE_DECLARE (e) =
585 .format = "cli-cmd: %s %s",
586 .format_args = "T4T4",
593 ed = ELOG_DATA (&vm->elog_main, e);
594 ed->c = elog_string (&vm->elog_main, "%v", c->path);
597 vec_add1 (c_error->what, 0);
599 elog_string (&vm->elog_main, (char *) c_error->what);
600 _vec_len (c_error->what) -= 1;
603 ed->err = elog_string (&vm->elog_main, "OK");
609 clib_error_return (0, "%v: %v", c->path, c_error->what);
610 clib_error_free (c_error);
611 /* Free sub input. */
619 /* Free any previous error. */
620 clib_error_free (error);
624 error = clib_error_return (0, "%v: no sub-commands", c->path);
626 /* Free sub input. */
638 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
639 format_unformat_error, input);
641 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
646 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
647 __attribute__ ((weak));
650 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
654 /* Process CLI input. */
656 vlib_cli_input (vlib_main_t * vm,
657 unformat_input_t * input,
658 vlib_cli_output_function_t * function, uword function_arg)
660 vlib_process_t *cp = vlib_get_current_process (vm);
662 vlib_cli_output_function_t *save_function;
663 uword save_function_arg;
666 save_function = cp->output_function;
667 save_function_arg = cp->output_function_arg;
669 cp->output_function = function;
670 cp->output_function_arg = function_arg;
674 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
677 while (!error && !unformat (input, "%U", unformat_eof));
681 vlib_cli_output (vm, "%v", error->what);
682 vlib_unix_error_report (vm, error);
683 /* clib_error_return is unfortunately often called with a '0'
685 rv = error->code != 0 ? error->code : -1;
686 clib_error_free (error);
689 cp->output_function = save_function;
690 cp->output_function_arg = save_function_arg;
694 /* Output to current CLI connection. */
696 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
698 vlib_process_t *cp = vlib_get_current_process (vm);
703 s = va_format (0, fmt, &va);
706 /* Terminate with \n if not present. */
707 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
710 if ((!cp) || (!cp->output_function))
711 fformat (stdout, "%v", s);
713 cp->output_function (cp->output_function_arg, s, vec_len (s));
718 void *vl_msg_push_heap (void) __attribute__ ((weak));
720 vl_msg_push_heap (void)
725 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
727 vl_msg_pop_heap (void *oldheap)
731 void *vlib_stats_push_heap (void *) __attribute__ ((weak));
733 vlib_stats_push_heap (void *notused)
738 static clib_error_t *
739 show_memory_usage (vlib_main_t * vm,
740 unformat_input_t * input, vlib_cli_command_t * cmd)
742 clib_mem_main_t *mm = &clib_mem_main;
743 int verbose __attribute__ ((unused)) = 0;
744 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
749 uword clib_mem_trace_enable_disable (uword enable);
753 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
755 if (unformat (input, "verbose"))
757 else if (unformat (input, "api-segment"))
759 else if (unformat (input, "stats-segment"))
761 else if (unformat (input, "main-heap"))
763 else if (unformat (input, "numa-heaps"))
765 else if (unformat (input, "map"))
769 error = clib_error_return (0, "unknown input `%U'",
770 format_unformat_error, input);
775 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
776 return clib_error_return
777 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
782 void *oldheap = vl_msg_push_heap ();
783 was_enabled = clib_mem_trace_enable_disable (0);
784 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
785 vl_msg_pop_heap (oldheap);
786 u8 *s = vec_dup (s_in_svm);
788 oldheap = vl_msg_push_heap ();
790 clib_mem_trace_enable_disable (was_enabled);
791 vl_msg_pop_heap (oldheap);
792 vlib_cli_output (vm, "API segment");
793 vlib_cli_output (vm, "%v", s);
798 void *oldheap = vlib_stats_push_heap (0);
799 was_enabled = clib_mem_trace_enable_disable (0);
800 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
802 clib_mem_set_heap (oldheap);
803 u8 *s = vec_dup (s_in_svm);
805 oldheap = vlib_stats_push_heap (0);
809 clib_mem_trace_enable_disable (was_enabled);
810 clib_mem_set_heap (oldheap);
812 vlib_cli_output (vm, "Stats segment");
813 vlib_cli_output (vm, "%v", s);
822 * Note: the foreach_vlib_main causes allocator traffic,
823 * so shut off tracing before we go there...
825 was_enabled = clib_mem_trace_enable_disable (0);
830 void *heap = mm->per_cpu_mheaps[index];
832 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
833 vlib_worker_threads[index].name);
834 vlib_cli_output (vm, " %U\n", format_page_map,
835 pointer_to_uword (clib_mem_get_heap_base(heap)),
836 clib_mem_get_heap_size (heap));
837 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
838 mm->per_cpu_mheaps[index],
844 /* Restore the trace flag */
845 clib_mem_trace_enable_disable (was_enabled);
851 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
853 if (mm->per_numa_mheaps[i] == 0)
855 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
857 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
860 was_enabled = clib_mem_trace_enable_disable (0);
861 heap = mm->per_numa_mheaps[i];
863 vlib_cli_output (vm, "Numa %d:", i);
864 vlib_cli_output (vm, " %U\n", format_page_map,
865 pointer_to_uword (clib_mem_get_heap_base (heap)),
866 clib_mem_get_heap_size (heap));
867 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
868 mm->per_numa_mheaps[index], verbose);
873 clib_mem_page_stats_t stats = { };
874 clib_mem_vm_map_hdr_t *hdr = 0;
878 s = format (s, "\n%-16s%7s%5s%7s%7s",
879 "StartAddr", "size", "FD", "PageSz", "Pages");
880 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
881 s = format (s, " Numa%u", numa);
882 s = format (s, " NotMap");
883 s = format (s, " Name");
884 vlib_cli_output (vm, "%v", s);
885 vec_reset_length (s);
887 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
889 clib_mem_get_page_stats ((void *) hdr->base_addr,
890 hdr->log2_page_sz, hdr->num_pages,
892 s = format (s, "%016lx%7U",
893 hdr->base_addr, format_memory_size,
894 hdr->num_pages << hdr->log2_page_sz);
897 s = format (s, "%5d", hdr->fd);
899 s = format (s, "%5s", " ");
901 s = format (s, "%7U%7lu",
902 format_log2_page_size, hdr->log2_page_sz,
904 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
905 s = format (s, "%6lu", stats.per_numa[numa]);
906 s = format (s, "%7lu", stats.not_mapped);
907 s = format (s, " %s", hdr->name);
908 vlib_cli_output (vm, "%v", s);
909 vec_reset_length (s);
918 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
919 .path = "show memory",
920 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
921 " [numa-heaps][map]",
922 .function = show_memory_usage,
926 static clib_error_t *
927 show_cpu (vlib_main_t * vm, unformat_input_t * input,
928 vlib_cli_command_t * cmd)
930 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
931 _("Model name", "%U", format_cpu_model_name);
932 _("Microarch model (family)", "%U", format_cpu_uarch);
933 _("Flags", "%U", format_cpu_flags);
934 _("Base frequency", "%.2f GHz",
935 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
941 * Displays various information about the CPU.
944 * @cliexstart{show cpu}
945 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
946 * Microarchitecture: Broadwell (Broadwell-EP/EX)
947 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
948 * Base Frequency: 3.20 GHz
952 VLIB_CLI_COMMAND (show_cpu_command, static) = {
954 .short_help = "Show cpu information",
955 .function = show_cpu,
959 static clib_error_t *
960 enable_disable_memory_trace (vlib_main_t * vm,
961 unformat_input_t * input,
962 vlib_cli_command_t * cmd)
964 clib_mem_main_t *mm = &clib_mem_main;
965 unformat_input_t _line_input, *line_input = &_line_input;
968 int stats_segment = 0;
973 if (!unformat_user (input, unformat_line_input, line_input))
976 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
978 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
980 else if (unformat (line_input, "api-segment"))
982 else if (unformat (line_input, "stats-segment"))
984 else if (unformat (line_input, "main-heap"))
986 else if (unformat (line_input, "numa-heap %d", &numa_id))
990 unformat_free (line_input);
991 return clib_error_return (0, "invalid input");
994 unformat_free (line_input);
996 if ((api_segment + stats_segment + main_heap + (enable == 0)
997 + (numa_id != ~0)) == 0)
999 return clib_error_return
1000 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1001 "numa-heap <nn> or disable");
1004 /* Turn off current trace, if any */
1005 if (current_traced_heap)
1008 oldheap = clib_mem_set_heap (current_traced_heap);
1010 clib_mem_set_heap (oldheap);
1011 current_traced_heap = 0;
1020 oldheap = vl_msg_push_heap ();
1021 current_traced_heap = clib_mem_get_heap ();
1023 vl_msg_pop_heap (oldheap);
1030 oldheap = vlib_stats_push_heap (0);
1031 current_traced_heap = clib_mem_get_heap ();
1032 clib_mem_trace (stats_segment);
1033 /* We don't want to call vlib_stats_pop_heap... */
1035 clib_mem_set_heap (oldheap);
1041 current_traced_heap = clib_mem_get_heap ();
1042 clib_mem_trace (main_heap);
1047 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
1048 return clib_error_return (0, "Numa %d out of range", numa_id);
1049 if (mm->per_numa_mheaps[numa_id] == 0)
1050 return clib_error_return (0, "Numa %d heap not active", numa_id);
1052 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
1053 return clib_error_return (0, "Numa %d uses the main heap...",
1055 current_traced_heap = mm->per_numa_mheaps[numa_id];
1056 oldheap = clib_mem_set_heap (current_traced_heap);
1058 clib_mem_set_heap (oldheap);
1066 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1067 .path = "memory-trace",
1068 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1069 " [numa-heap <numa-id>]\n",
1070 .function = enable_disable_memory_trace,
1074 static clib_error_t *
1075 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1076 vlib_cli_command_t * cmd)
1078 clib_file_main_t *fm = &file_main;
1081 /* environ(7) does not indicate a header for this */
1082 extern char **environ;
1084 /* Close all known open files */
1086 pool_foreach(f, fm->file_pool,
1088 if (f->file_descriptor > 2)
1089 close(f->file_descriptor);
1094 execve (vm->name, (char **) vm->argv, environ);
1100 VLIB_CLI_COMMAND (restart_cmd,static) = {
1102 .short_help = "restart process",
1103 .function = restart_cmd_fn,
1109 * A trivial test harness to verify the per-process output_function
1110 * is working correcty.
1113 static clib_error_t *
1114 sleep_ten_seconds (vlib_main_t * vm,
1115 unformat_input_t * input, vlib_cli_command_t * cmd)
1118 u16 my_id = rand ();
1120 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1122 for (i = 0; i < 10; i++)
1124 vlib_process_wait_for_event_or_clock (vm, 1.0);
1125 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1127 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1132 VLIB_CLI_COMMAND (ping_command, static) = {
1133 .path = "test sleep",
1134 .function = sleep_ten_seconds,
1135 .short_help = "Sleep for 10 seconds",
1138 #endif /* ifdef TEST_CODE */
1141 vlib_cli_normalize_path (char *input, char **result)
1146 uword index_of_last_space = ~0;
1151 /* Multiple white space -> single space. */
1158 if (l > 0 && s[l - 1] != ' ')
1166 if (l > 0 && s[l - 1] == ' ')
1167 index_of_last_space = vec_len (s);
1174 /* Remove any extra space at end. */
1175 if (l > 0 && s[l - 1] == ' ')
1179 return index_of_last_space;
1183 parent_path_len (char *path)
1186 for (i = vec_len (path) - 1; i >= 0; i--)
1195 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1197 vlib_cli_command_t *p, *c;
1198 vlib_cli_sub_command_t *sub_c;
1202 p = vec_elt_at_index (cm->commands, parent_index);
1203 c = vec_elt_at_index (cm->commands, child_index);
1205 l = parent_path_len (c->path);
1207 sub_name = vec_dup ((u8 *) c->path);
1210 ASSERT (l + 1 < vec_len (c->path));
1212 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1215 if (sub_name[0] == '%')
1218 vlib_cli_sub_rule_t *sr;
1221 vec_delete (sub_name, 1, 0);
1223 if (!p->sub_rule_index_by_name)
1224 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1225 sizeof (sub_name[0]),
1227 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1230 sr = vec_elt_at_index (p->sub_rules, q[0]);
1231 ASSERT (sr->command_index == child_index);
1235 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1236 vec_len (p->sub_rules));
1237 vec_add2 (p->sub_rules, sr, 1);
1238 sr->name = sub_name;
1239 sr->rule_index = sr - p->sub_rules;
1240 sr->command_index = child_index;
1244 if (!p->sub_command_index_by_name)
1245 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1246 sizeof (c->path[0]),
1249 /* Check if sub-command has already been created. */
1250 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1252 vec_free (sub_name);
1256 vec_add2 (p->sub_commands, sub_c, 1);
1257 sub_c->index = child_index;
1258 sub_c->name = sub_name;
1259 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1260 sub_c - p->sub_commands);
1262 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1263 for (i = 0; i < vec_len (sub_c->name); i++)
1266 vlib_cli_parse_position_t *pos;
1268 pos = vec_elt_at_index (p->sub_command_positions, i);
1271 pos->min_char = sub_c->name[i];
1273 n = sub_c->name[i] - pos->min_char;
1276 pos->min_char = sub_c->name[i];
1277 vec_insert (pos->bitmaps, -n, 0);
1281 vec_validate (pos->bitmaps, n);
1283 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1288 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1290 uword p_len, pi, *p;
1292 vlib_cli_command_t *c, *parent;
1294 /* Root command (index 0) should have already been added. */
1295 ASSERT (vec_len (cm->commands) > 0);
1297 c = vec_elt_at_index (cm->commands, ci);
1298 p_len = parent_path_len (c->path);
1300 /* No space? Parent is root command. */
1303 add_sub_command (cm, 0, ci);
1308 vec_add (p_path, c->path, p_len);
1310 p = hash_get_mem (cm->command_index_by_path, p_path);
1312 /* Parent exists? */
1315 /* Parent does not exist; create it. */
1316 vec_add2 (cm->commands, parent, 1);
1317 parent->path = p_path;
1318 hash_set_mem (cm->command_index_by_path, parent->path,
1319 parent - cm->commands);
1320 pi = parent - cm->commands;
1328 add_sub_command (cm, pi, ci);
1330 /* Create parent's parent. */
1332 vlib_cli_make_parent (cm, pi);
1336 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1338 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1342 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1344 vlib_cli_main_t *cm = &vm->cli_main;
1345 clib_error_t *error = 0;
1347 char *normalized_path;
1349 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1352 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1354 if (!cm->command_index_by_path)
1355 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1356 sizeof (c->path[0]),
1359 /* See if command already exists with given path. */
1360 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1363 vlib_cli_command_t *d;
1366 d = vec_elt_at_index (cm->commands, ci);
1368 /* If existing command was created via vlib_cli_make_parent
1369 replaced it with callers data. */
1370 if (vlib_cli_command_is_empty (d))
1372 vlib_cli_command_t save = d[0];
1374 ASSERT (!vlib_cli_command_is_empty (c));
1376 /* Copy callers fields. */
1379 /* Save internal fields. */
1380 d->path = save.path;
1381 d->sub_commands = save.sub_commands;
1382 d->sub_command_index_by_name = save.sub_command_index_by_name;
1383 d->sub_command_positions = save.sub_command_positions;
1384 d->sub_rules = save.sub_rules;
1388 clib_error_return (0, "duplicate command name with path %v",
1391 vec_free (normalized_path);
1397 /* Command does not exist: create it. */
1399 /* Add root command (index 0). */
1400 if (vec_len (cm->commands) == 0)
1402 /* Create command with index 0; path is empty string. */
1403 vec_resize (cm->commands, 1);
1406 ci = vec_len (cm->commands);
1407 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1408 vec_add1 (cm->commands, c[0]);
1410 c = vec_elt_at_index (cm->commands, ci);
1411 c->path = normalized_path;
1413 /* Don't inherit from registration. */
1414 c->sub_commands = 0;
1415 c->sub_command_index_by_name = 0;
1416 c->sub_command_positions = 0;
1419 vlib_cli_make_parent (cm, ci);
1424 /* $$$ turn back on again someday, maybe */
1426 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1428 vlib_cli_main_t *cm = &vm->cli_main;
1429 vlib_cli_parse_rule_t *r;
1430 clib_error_t *error = 0;
1434 if (!cm->parse_rule_index_by_name)
1435 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1436 sizeof (r->name[0]),
1439 /* Make vector copy of name. */
1440 r_name = format (0, "%s", r_reg->name);
1442 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1445 return clib_error_return (0, "duplicate parse rule name `%s'",
1449 vec_add2 (cm->parse_rules, r, 1);
1451 r->name = (char *) r_name;
1452 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1457 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1458 vlib_cli_parse_rule_t *
1460 vlib_cli_parse_rule_t *
1462 __attribute__ ((unused))
1464 clib_error_t *error = 0;
1465 vlib_cli_parse_rule_t *r;
1467 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1469 if (!r->name || strlen (r->name) == 0)
1471 error = clib_error_return (0, "parse rule with no name");
1475 error = vlib_cli_register_parse_rule (vm, r);
1485 static clib_error_t *
1486 elog_trace_command_fn (vlib_main_t * vm,
1487 unformat_input_t * input, vlib_cli_command_t * cmd)
1489 unformat_input_t _line_input, *line_input = &_line_input;
1491 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1492 u32 circuit_node_index;
1494 if (!unformat_user (input, unformat_line_input, line_input))
1497 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1499 if (unformat (line_input, "api"))
1501 else if (unformat (line_input, "dispatch"))
1503 else if (unformat (line_input, "circuit-node %U",
1504 unformat_vlib_node, vm, &circuit_node_index))
1506 else if (unformat (line_input, "cli"))
1508 else if (unformat (line_input, "barrier"))
1510 else if (unformat (line_input, "disable"))
1512 else if (unformat (line_input, "enable"))
1517 unformat_free (line_input);
1519 vl_api_set_elog_trace_api_messages
1520 (api ? enable : vl_api_get_elog_trace_api_messages ());
1521 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1522 vm->elog_trace_graph_dispatch = dispatch ?
1523 enable : vm->elog_trace_graph_dispatch;
1524 vm->elog_trace_graph_circuit = circuit ?
1525 enable : vm->elog_trace_graph_circuit;
1526 vlib_worker_threads->barrier_elog_enabled =
1527 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1528 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1531 * Set up start-of-buffer logic-analyzer trigger
1532 * for main loop event logs, which are fairly heavyweight.
1533 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1534 * will fully disable the scheme when the elog buffer fills.
1536 if (dispatch || circuit)
1538 elog_main_t *em = &vm->elog_main;
1540 em->n_total_events_disable_limit =
1541 em->n_total_events + vec_len (em->event_ring);
1546 vlib_cli_output (vm, "Current status:");
1549 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1550 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1551 vm->elog_trace_cli_commands ? "on" : "off");
1553 (vm, " Barrier sync trace: %s",
1554 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1556 (vm, " Graph Dispatch: %s",
1557 vm->elog_trace_graph_dispatch ? "on" : "off");
1559 (vm, " Graph Circuit: %s",
1560 vm->elog_trace_graph_circuit ? "on" : "off");
1561 if (vm->elog_trace_graph_circuit)
1564 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1570 * Control event logging of api, cli, and thread barrier events
1571 * With no arguments, displays the current trace status.
1572 * Name the event groups you wish to trace or stop tracing.
1576 * elog trace api cli barrier
1577 * elog trace api cli barrier disable
1578 * elog trace dispatch
1579 * elog trace circuit-node ethernet-input
1582 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1585 VLIB_CLI_COMMAND (elog_trace_command, static) =
1587 .path = "elog trace",
1588 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1589 "[circuit-node <name> e.g. ethernet-input][disable]",
1590 .function = elog_trace_command_fn,
1594 static clib_error_t *
1595 suspend_command_fn (vlib_main_t * vm,
1596 unformat_input_t * input, vlib_cli_command_t * cmd)
1598 vlib_process_suspend (vm, 30e-3);
1603 VLIB_CLI_COMMAND (suspend_command, static) =
1606 .short_help = "suspend debug CLI for 30ms",
1607 .function = suspend_command_fn,
1614 sort_cmds_by_path (void *a1, void *a2)
1618 vlib_main_t *vm = vlib_get_main ();
1619 vlib_cli_main_t *cm = &vm->cli_main;
1620 vlib_cli_command_t *c1, *c2;
1623 c1 = vec_elt_at_index (cm->commands, *index1);
1624 c2 = vec_elt_at_index (cm->commands, *index2);
1626 lmin = vec_len (c1->path);
1627 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1629 for (i = 0; i < lmin; i++)
1631 if (c1->path[i] < c2->path[i])
1633 else if (c1->path[i] > c2->path[i])
1642 vlib_cli_main_t *cm;
1643 u32 parent_command_index;
1645 int show_not_mp_safe;
1648 } vlib_cli_walk_args_t;
1651 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1653 vlib_cli_command_t *parent;
1654 vlib_cli_sub_command_t *sub;
1655 vlib_cli_walk_args_t _a, *a = &_a;
1656 vlib_cli_main_t *cm;
1659 /* Copy args into this stack frame */
1663 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1665 if (parent->function)
1667 if (((a->show_mp_safe && parent->is_mp_safe)
1668 || (a->show_not_mp_safe && !parent->is_mp_safe))
1669 && (a->show_hit == 0 || parent->hit_counter))
1671 vec_add1 (cm->sort_vector, a->parent_command_index);
1675 parent->hit_counter = 0;
1678 for (i = 0; i < vec_len (parent->sub_commands); i++)
1680 sub = vec_elt_at_index (parent->sub_commands, i);
1681 a->parent_command_index = sub->index;
1682 cli_recursive_walk (a);
1687 format_mp_safe (u8 * s, va_list * args)
1689 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1690 int show_mp_safe = va_arg (*args, int);
1691 int show_not_mp_safe = va_arg (*args, int);
1692 int show_hit = va_arg (*args, int);
1693 int clear_hit = va_arg (*args, int);
1694 vlib_cli_command_t *c;
1695 vlib_cli_walk_args_t _a, *a = &_a;
1697 char *format_string = "\n%v";
1700 format_string = "\n%v: %u";
1702 vec_reset_length (cm->sort_vector);
1705 a->parent_command_index = 0;
1706 a->show_mp_safe = show_mp_safe;
1707 a->show_not_mp_safe = show_not_mp_safe;
1708 a->show_hit = show_hit;
1709 a->clear_hit = clear_hit;
1711 cli_recursive_walk (a);
1713 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1715 for (i = 0; i < vec_len (cm->sort_vector); i++)
1717 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1718 s = format (s, format_string, c->path, c->hit_counter);
1725 static clib_error_t *
1726 show_cli_command_fn (vlib_main_t * vm,
1727 unformat_input_t * input, vlib_cli_command_t * cmd)
1729 int show_mp_safe = 0;
1730 int show_not_mp_safe = 0;
1734 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1736 if (unformat (input, "mp-safe"))
1738 if (unformat (input, "not-mp-safe"))
1739 show_not_mp_safe = 1;
1740 else if (unformat (input, "hit"))
1742 else if (unformat (input, "clear-hit"))
1748 /* default set: all cli commands */
1749 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1750 show_mp_safe = show_not_mp_safe = 1;
1752 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1753 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1755 vlib_cli_output (vm, "hit counters cleared...");
1761 * Displays debug cli command information
1764 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1766 * "show cli" displays the entire debug cli:
1770 * adjacency counters
1776 * "show cli mp-safe" displays mp-safe debug CLI commands:
1790 * "show cli not-mp-safe" displays debug CLI commands
1791 * which cause worker thread barrier synchronization
1793 * "show cli hit" displays commands which have been executed. Qualify
1794 * as desired with "mp-safe" or "not-mp-safe".
1796 * "show cli clear-hit" clears the per-command hit counters.
1801 VLIB_CLI_COMMAND (show_cli_command, static) =
1804 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1805 .function = show_cli_command_fn,
1810 static clib_error_t *
1811 vlib_cli_init (vlib_main_t * vm)
1813 vlib_cli_main_t *cm = &vm->cli_main;
1814 clib_error_t *error = 0;
1815 vlib_cli_command_t *cmd;
1817 cmd = cm->cli_command_registrations;
1821 error = vlib_cli_register (vm, cmd);
1824 cmd = cmd->next_cli_command;
1829 VLIB_INIT_FUNCTION (vlib_cli_init);
1832 * fd.io coding-style-patch-verification: ON
1835 * eval: (c-set-style "gnu")