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 /* Looks for string based sub-input formatted { SUB-INPUT }. */
164 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
166 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
172 c = unformat_get_input (i);
184 /* Put back paren. */
185 if (c != UNFORMAT_END_OF_INPUT)
186 unformat_put_input (i);
188 if (c == '{' && unformat (i, "%v", &s))
190 unformat_init_vector (sub_input, s);
199 static vlib_cli_command_t *
200 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
202 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
203 return vec_elt_at_index (cm->commands, s->index);
207 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
209 vlib_main_t __clib_unused *vm = va_arg (*args, vlib_main_t *);
210 vlib_global_main_t *vgm = vlib_get_global_main ();
211 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
212 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
213 vlib_cli_main_t *cm = &vgm->cli_main;
214 uword *match_bitmap, is_unique, index;
216 match_bitmap = vlib_cli_sub_command_match (c, i);
217 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
221 index = clib_bitmap_first_set (match_bitmap);
222 *result = get_sub_command (cm, c, index);
224 clib_bitmap_free (match_bitmap);
230 vlib_cli_cmp_strings (void *a1, void *a2)
232 u8 *c1 = *(u8 **) a1;
233 u8 *c2 = *(u8 **) a2;
235 return vec_cmp (c1, c2);
239 vlib_cli_get_possible_completions (u8 * str)
241 vlib_cli_command_t *c;
242 vlib_cli_sub_command_t *sc;
243 vlib_global_main_t *vgm = vlib_get_global_main ();
244 vlib_cli_main_t *vcm = &vgm->cli_main;
245 uword *match_bitmap = 0;
246 uword index, is_unique, help_next_level;
248 unformat_input_t input;
249 unformat_init_vector (&input, vec_dup (str));
250 c = vec_elt_at_index (vcm->commands, 0);
252 /* remove trailing whitespace, except for one of them */
253 while (vec_len (input.buffer) >= 2 &&
254 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
255 isspace (input.buffer[vec_len (input.buffer) - 2]))
257 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
260 /* if input is empty, directly return list of root commands */
261 if (vec_len (input.buffer) == 0 ||
262 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
264 vec_foreach (sc, c->sub_commands)
266 vec_add1 (result, (u8 *) sc->name);
271 /* add a trailing '?' so that vlib_cli_sub_command_match can find
272 * all commands starting with the input string */
273 vec_add1 (input.buffer, '?');
277 match_bitmap = vlib_cli_sub_command_match (c, &input);
278 /* no match: return no result */
279 if (match_bitmap == 0)
283 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
284 /* unique match: try to step one subcommand level further */
287 /* stop if no more input */
288 if (input.index >= vec_len (input.buffer) - 1)
293 index = clib_bitmap_first_set (match_bitmap);
294 c = get_sub_command (vcm, c, index);
295 clib_bitmap_free (match_bitmap);
298 /* multiple matches: stop here, return all matches */
302 /* remove trailing '?' */
303 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
305 /* if we have a space at the end of input, and a unique match,
306 * autocomplete the next level of subcommands */
307 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
309 clib_bitmap_foreach (index, match_bitmap) {
310 if (help_next_level && is_unique) {
311 c = get_sub_command (vcm, c, index);
312 vec_foreach (sc, c->sub_commands) {
313 vec_add1 (result, (u8*) sc->name);
315 goto done; /* break doesn't work in this macro-loop */
317 sc = &c->sub_commands[index];
318 vec_add1(result, (u8*) sc->name);
323 clib_bitmap_free (match_bitmap);
324 unformat_free (&input);
327 vec_sort_with_function (result, vlib_cli_cmp_strings);
332 format_vlib_cli_command_help (u8 * s, va_list * args)
334 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
335 int is_long = va_arg (*args, int);
336 if (is_long && c->long_help)
337 s = format (s, "%s", c->long_help);
338 else if (c->short_help)
339 s = format (s, "%s", c->short_help);
341 s = format (s, "%v commands", c->path);
346 format_vlib_cli_path (u8 * s, va_list * args)
348 u8 *path = va_arg (*args, u8 *);
350 s = format (s, "%v", path);
355 static vlib_cli_command_t *
356 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
358 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
359 vlib_cli_sub_command_t *sc;
362 vec_add1 (subs, c[0]);
364 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
370 vlib_cli_cmp_rule (void *a1, void *a2)
372 vlib_cli_sub_rule_t *r1 = a1;
373 vlib_cli_sub_rule_t *r2 = a2;
375 return vec_cmp (r1->name, r2->name);
379 vlib_cli_cmp_command (void *a1, void *a2)
381 vlib_cli_command_t *c1 = a1;
382 vlib_cli_command_t *c2 = a2;
384 return vec_cmp (c1->path, c2->path);
387 static clib_error_t *
388 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
389 vlib_cli_main_t * cm,
390 unformat_input_t * input,
391 uword parent_command_index)
393 vlib_global_main_t *vgm = vlib_get_global_main ();
394 vlib_cli_command_t *parent, *c;
395 clib_error_t *error = 0;
396 unformat_input_t sub_input;
398 uword is_main_dispatch = cm == &vgm->cli_main;
400 parent = vec_elt_at_index (cm->commands, parent_command_index);
401 if (is_main_dispatch && unformat (input, "help"))
403 uword help_at_end_of_line, i;
405 help_at_end_of_line =
406 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
411 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
414 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
421 /* help SUB-COMMAND => long format help.
422 "help" at end of line: show all commands. */
423 if (!help_at_end_of_line)
424 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
427 else if (vec_len (c->sub_commands) == 0)
428 vlib_cli_output (vm, "%v: no sub-commands", c->path);
432 vlib_cli_sub_rule_t *sr, *subs = 0;
433 vlib_cli_sub_command_t *sc;
435 vec_foreach (sc, c->sub_commands)
437 vec_add2 (subs, sr, 1);
439 sr->command_index = sc->index;
443 vec_sort_with_function (subs, vlib_cli_cmp_rule);
445 for (i = 0; i < vec_len (subs); i++)
447 vlib_cli_command_t *d;
449 d = vec_elt_at_index (cm->commands, subs[i].command_index);
451 (vm, " %-30v %U", subs[i].name,
452 format_vlib_cli_command_help, d, /* is_long */ 0);
459 else if (is_main_dispatch
460 && (unformat (input, "choices") || unformat (input, "?")))
462 vlib_cli_command_t *sub, *subs;
464 subs = all_subs (cm, 0, parent_command_index);
465 vec_sort_with_function (subs, vlib_cli_cmp_command);
466 vec_foreach (sub, subs)
467 vlib_cli_output (vm, " %-40U %U",
468 format_vlib_cli_path, sub->path,
469 format_vlib_cli_command_help, sub, /* is_long */ 0);
473 else if (unformat (input, "comment %v", &string))
478 else if (unformat (input, "vpplog %v", &string))
482 * Delete leading whitespace, so "vpplog { this and that }"
483 * and "vpplog this" line up nicely.
485 for (i = 0; i < vec_len (string); i++)
486 if (string[i] != ' ')
489 vec_delete (string, i, 0);
491 vlib_log_notice (cm->log, "CLI: %v", string);
495 else if (unformat (input, "uncomment %U",
496 unformat_vlib_cli_sub_input, &sub_input))
499 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
500 parent_command_index);
501 unformat_free (&sub_input);
503 else if (unformat (input, "leak-check %U",
504 unformat_vlib_cli_sub_input, &sub_input))
507 if (current_traced_heap)
510 oldheap = clib_mem_set_heap (current_traced_heap);
512 clib_mem_set_heap (oldheap);
513 current_traced_heap = 0;
517 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
518 parent_command_index);
519 unformat_free (&sub_input);
521 /* Otherwise, the clib_error_t shows up as a leak... */
524 vlib_cli_output (vm, "%v", error->what);
525 clib_error_free (error);
529 (void) clib_mem_trace_enable_disable (0);
530 leak_report = format (0, "%U", format_clib_mem_heap, 0,
531 1 /* verbose, i.e. print leaks */ );
533 vlib_cli_output (vm, "%v", leak_report);
534 vec_free (leak_report);
538 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
540 unformat_input_t *si;
541 uword has_sub_commands =
542 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
545 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
548 if (has_sub_commands)
549 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
551 if (has_sub_commands && !error)
552 /* Found valid sub-command. */ ;
554 else if (c->function)
556 clib_error_t *c_error;
558 /* Skip white space for benefit of called function. */
559 unformat_skip_white_space (si);
561 if (unformat (si, "?"))
563 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
568 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
571 ELOG_TYPE_DECLARE (e) =
573 .format = "cli-cmd: %s",
581 ed = ELOG_DATA (vlib_get_elog_main (), e);
582 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
586 vlib_worker_thread_barrier_sync (vm);
587 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
588 clib_call_callbacks (cm->perf_counter_cbs, cm,
589 c - cm->commands, 0 /* before */ );
592 c_error = c->function (vm, si, c);
594 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
595 clib_call_callbacks (cm->perf_counter_cbs, cm,
596 c - cm->commands, 1 /* after */ );
598 vlib_worker_thread_barrier_release (vm);
600 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
603 ELOG_TYPE_DECLARE (e) =
605 .format = "cli-cmd: %s %s",
606 .format_args = "T4T4",
613 ed = ELOG_DATA (vlib_get_elog_main (), e);
614 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
617 vec_add1 (c_error->what, 0);
618 ed->err = elog_string (vlib_get_elog_main (),
619 (char *) c_error->what);
620 _vec_len (c_error->what) -= 1;
623 ed->err = elog_string (vlib_get_elog_main (), "OK");
629 clib_error_return (0, "%v: %v", c->path, c_error->what);
630 clib_error_free (c_error);
631 /* Free sub input. */
639 /* Free any previous error. */
640 clib_error_free (error);
644 error = clib_error_return (0, "%v: no sub-commands", c->path);
646 /* Free sub input. */
658 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
659 format_unformat_error, input);
661 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
666 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
667 __attribute__ ((weak));
670 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
674 /* Process CLI input. */
676 vlib_cli_input (vlib_main_t * vm,
677 unformat_input_t * input,
678 vlib_cli_output_function_t * function, uword function_arg)
680 vlib_global_main_t *vgm = vlib_get_global_main ();
681 vlib_process_t *cp = vlib_get_current_process (vm);
683 vlib_cli_output_function_t *save_function;
684 uword save_function_arg;
687 save_function = cp->output_function;
688 save_function_arg = cp->output_function_arg;
690 cp->output_function = function;
691 cp->output_function_arg = function_arg;
695 error = vlib_cli_dispatch_sub_commands (vm, &vgm->cli_main, input,
698 while (!error && !unformat (input, "%U", unformat_eof));
702 vlib_cli_output (vm, "%v", error->what);
703 vlib_unix_error_report (vm, error);
704 /* clib_error_return is unfortunately often called with a '0'
706 rv = error->code != 0 ? error->code : -1;
707 clib_error_free (error);
710 cp->output_function = save_function;
711 cp->output_function_arg = save_function_arg;
715 /* Output to current CLI connection. */
717 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
719 vlib_process_t *cp = vlib_get_current_process (vm);
724 s = va_format (0, fmt, &va);
727 /* some format functions might return 0
728 * e.g. show int addr */
732 /* Terminate with \n if not present. */
733 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
736 if ((!cp) || (!cp->output_function))
737 fformat (stdout, "%v", s);
739 cp->output_function (cp->output_function_arg, s, vec_len (s));
744 void *vl_msg_push_heap (void) __attribute__ ((weak));
746 vl_msg_push_heap (void)
751 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
753 vl_msg_pop_heap (void *oldheap)
757 static clib_error_t *
758 show_memory_usage (vlib_main_t * vm,
759 unformat_input_t * input, vlib_cli_command_t * cmd)
761 clib_mem_main_t *mm = &clib_mem_main;
762 int verbose __attribute__ ((unused)) = 0;
763 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
768 uword clib_mem_trace_enable_disable (uword enable);
772 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
774 if (unformat (input, "verbose"))
776 else if (unformat (input, "api-segment"))
778 else if (unformat (input, "stats-segment"))
780 else if (unformat (input, "main-heap"))
782 else if (unformat (input, "numa-heaps"))
784 else if (unformat (input, "map"))
788 error = clib_error_return (0, "unknown input `%U'",
789 format_unformat_error, input);
794 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
795 return clib_error_return
796 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
801 void *oldheap = vl_msg_push_heap ();
802 was_enabled = clib_mem_trace_enable_disable (0);
803 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
804 vl_msg_pop_heap (oldheap);
805 u8 *s = vec_dup (s_in_svm);
807 oldheap = vl_msg_push_heap ();
809 clib_mem_trace_enable_disable (was_enabled);
810 vl_msg_pop_heap (oldheap);
811 vlib_cli_output (vm, "API segment");
812 vlib_cli_output (vm, "%v", s);
817 void *oldheap = vlib_stats_set_heap (0);
818 was_enabled = clib_mem_trace_enable_disable (0);
819 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
821 clib_mem_set_heap (oldheap);
822 u8 *s = vec_dup (s_in_svm);
824 oldheap = vlib_stats_set_heap (0);
828 clib_mem_trace_enable_disable (was_enabled);
829 clib_mem_set_heap (oldheap);
831 vlib_cli_output (vm, "Stats segment");
832 vlib_cli_output (vm, "%v", s);
841 * Note: the foreach_vlib_main causes allocator traffic,
842 * so shut off tracing before we go there...
844 was_enabled = clib_mem_trace_enable_disable (0);
848 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n" : "", index,
849 vlib_worker_threads[index].name);
850 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
851 mm->per_cpu_mheaps[index], verbose);
855 /* Restore the trace flag */
856 clib_mem_trace_enable_disable (was_enabled);
860 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
862 if (mm->per_numa_mheaps[i] == 0)
864 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
866 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
869 was_enabled = clib_mem_trace_enable_disable (0);
871 vlib_cli_output (vm, "Numa %d:", i);
872 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
873 mm->per_numa_mheaps[index], verbose);
878 clib_mem_page_stats_t stats = { };
879 clib_mem_vm_map_hdr_t *hdr = 0;
883 s = format (s, "\n%-16s%7s%5s%7s%7s",
884 "StartAddr", "size", "FD", "PageSz", "Pages");
885 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
886 s = format (s, " Numa%u", numa);
887 s = format (s, " NotMap");
888 s = format (s, " Name");
889 vlib_cli_output (vm, "%v", s);
890 vec_reset_length (s);
892 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
894 clib_mem_get_page_stats ((void *) hdr->base_addr,
895 hdr->log2_page_sz, hdr->num_pages,
897 s = format (s, "%016lx%7U",
898 hdr->base_addr, format_memory_size,
899 hdr->num_pages << hdr->log2_page_sz);
902 s = format (s, "%5d", hdr->fd);
904 s = format (s, "%5s", " ");
906 s = format (s, "%7U%7lu",
907 format_log2_page_size, hdr->log2_page_sz,
909 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
910 s = format (s, "%6lu", stats.per_numa[numa]);
911 s = format (s, "%7lu", stats.not_mapped);
912 s = format (s, " %s", hdr->name);
913 vlib_cli_output (vm, "%v", s);
914 vec_reset_length (s);
923 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
924 .path = "show memory",
925 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
926 " [numa-heaps][map]",
927 .function = show_memory_usage,
931 static clib_error_t *
932 show_cpu (vlib_main_t * vm, unformat_input_t * input,
933 vlib_cli_command_t * cmd)
935 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
936 _("Model name", "%U", format_cpu_model_name);
937 _("Microarch model (family)", "%U", format_cpu_uarch);
938 _("Flags", "%U", format_cpu_flags);
939 _("Base frequency", "%.2f GHz",
940 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
946 * Displays various information about the CPU.
949 * @cliexstart{show cpu}
950 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
951 * Microarchitecture: Broadwell (Broadwell-EP/EX)
952 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
953 * Base Frequency: 3.20 GHz
957 VLIB_CLI_COMMAND (show_cpu_command, static) = {
959 .short_help = "Show cpu information",
960 .function = show_cpu,
964 static clib_error_t *
965 enable_disable_memory_trace (vlib_main_t * vm,
966 unformat_input_t * input,
967 vlib_cli_command_t * cmd)
969 clib_mem_main_t *mm = &clib_mem_main;
970 unformat_input_t _line_input, *line_input = &_line_input;
973 int stats_segment = 0;
978 if (!unformat_user (input, unformat_line_input, line_input))
981 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
983 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
985 else if (unformat (line_input, "api-segment"))
987 else if (unformat (line_input, "stats-segment"))
989 else if (unformat (line_input, "main-heap"))
991 else if (unformat (line_input, "numa-heap %d", &numa_id))
995 unformat_free (line_input);
996 return clib_error_return (0, "invalid input");
999 unformat_free (line_input);
1001 if ((api_segment + stats_segment + main_heap + (enable == 0)
1002 + (numa_id != ~0)) == 0)
1004 return clib_error_return
1005 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1006 "numa-heap <nn> or disable");
1009 /* Turn off current trace, if any */
1010 if (current_traced_heap)
1013 oldheap = clib_mem_set_heap (current_traced_heap);
1015 clib_mem_set_heap (oldheap);
1016 current_traced_heap = 0;
1025 oldheap = vl_msg_push_heap ();
1026 current_traced_heap = clib_mem_get_heap ();
1028 vl_msg_pop_heap (oldheap);
1035 oldheap = vlib_stats_set_heap ();
1036 current_traced_heap = clib_mem_get_heap ();
1037 clib_mem_trace (stats_segment);
1038 /* We don't want to call vlib_stats_pop_heap... */
1040 clib_mem_set_heap (oldheap);
1046 current_traced_heap = clib_mem_get_heap ();
1047 clib_mem_trace (main_heap);
1052 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
1053 return clib_error_return (0, "Numa %d out of range", numa_id);
1054 if (mm->per_numa_mheaps[numa_id] == 0)
1055 return clib_error_return (0, "Numa %d heap not active", numa_id);
1057 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
1058 return clib_error_return (0, "Numa %d uses the main heap...",
1060 current_traced_heap = mm->per_numa_mheaps[numa_id];
1061 oldheap = clib_mem_set_heap (current_traced_heap);
1063 clib_mem_set_heap (oldheap);
1071 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1072 .path = "memory-trace",
1073 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1074 " [numa-heap <numa-id>]\n",
1075 .function = enable_disable_memory_trace,
1079 static clib_error_t *
1080 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1081 vlib_cli_command_t * cmd)
1083 vlib_global_main_t *vgm = vlib_get_global_main ();
1084 clib_file_main_t *fm = &file_main;
1087 /* environ(7) does not indicate a header for this */
1088 extern char **environ;
1090 /* Close all known open files */
1092 pool_foreach (f, fm->file_pool)
1094 if (f->file_descriptor > 2)
1095 close(f->file_descriptor);
1100 execve (vgm->name, (char **) vgm->argv, environ);
1106 VLIB_CLI_COMMAND (restart_cmd,static) = {
1108 .short_help = "restart process",
1109 .function = restart_cmd_fn,
1115 * A trivial test harness to verify the per-process output_function
1116 * is working correcty.
1119 static clib_error_t *
1120 sleep_ten_seconds (vlib_main_t * vm,
1121 unformat_input_t * input, vlib_cli_command_t * cmd)
1124 u16 my_id = rand ();
1126 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1128 for (i = 0; i < 10; i++)
1130 vlib_process_wait_for_event_or_clock (vm, 1.0);
1131 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1133 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1138 VLIB_CLI_COMMAND (ping_command, static) = {
1139 .path = "test sleep",
1140 .function = sleep_ten_seconds,
1141 .short_help = "Sleep for 10 seconds",
1144 #endif /* ifdef TEST_CODE */
1147 vlib_cli_normalize_path (char *input, char **result)
1152 uword index_of_last_space = ~0;
1157 /* Multiple white space -> single space. */
1164 if (l > 0 && s[l - 1] != ' ')
1172 if (l > 0 && s[l - 1] == ' ')
1173 index_of_last_space = vec_len (s);
1180 /* Remove any extra space at end. */
1181 if (l > 0 && s[l - 1] == ' ')
1185 return index_of_last_space;
1189 parent_path_len (char *path)
1192 for (i = vec_len (path) - 1; i >= 0; i--)
1201 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1203 vlib_cli_command_t *p, *c;
1204 vlib_cli_sub_command_t *sub_c;
1208 p = vec_elt_at_index (cm->commands, parent_index);
1209 c = vec_elt_at_index (cm->commands, child_index);
1211 l = parent_path_len (c->path);
1213 sub_name = vec_dup ((u8 *) c->path);
1216 ASSERT (l + 1 < vec_len (c->path));
1218 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1221 /* "Can't happen," check mainly to shut up coverity */
1222 ALWAYS_ASSERT (sub_name != 0);
1224 if (sub_name[0] == '%')
1227 vlib_cli_sub_rule_t *sr;
1230 vec_delete (sub_name, 1, 0);
1232 if (!p->sub_rule_index_by_name)
1233 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1234 sizeof (sub_name[0]),
1236 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1239 sr = vec_elt_at_index (p->sub_rules, q[0]);
1240 ASSERT (sr->command_index == child_index);
1244 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1245 vec_len (p->sub_rules));
1246 vec_add2 (p->sub_rules, sr, 1);
1247 sr->name = sub_name;
1248 sr->rule_index = sr - p->sub_rules;
1249 sr->command_index = child_index;
1253 if (!p->sub_command_index_by_name)
1254 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1255 sizeof (c->path[0]),
1258 /* Check if sub-command has already been created. */
1259 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1261 vec_free (sub_name);
1265 vec_add2 (p->sub_commands, sub_c, 1);
1266 sub_c->index = child_index;
1267 sub_c->name = sub_name;
1268 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1269 sub_c - p->sub_commands);
1271 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1272 for (i = 0; i < vec_len (sub_c->name); i++)
1275 vlib_cli_parse_position_t *pos;
1277 pos = vec_elt_at_index (p->sub_command_positions, i);
1280 pos->min_char = sub_c->name[i];
1282 n = sub_c->name[i] - pos->min_char;
1285 pos->min_char = sub_c->name[i];
1286 vec_insert (pos->bitmaps, -n, 0);
1290 vec_validate (pos->bitmaps, n);
1292 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1297 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1299 uword p_len, pi, *p;
1301 vlib_cli_command_t *c, *parent;
1303 /* Root command (index 0) should have already been added. */
1304 ASSERT (vec_len (cm->commands) > 0);
1306 c = vec_elt_at_index (cm->commands, ci);
1307 p_len = parent_path_len (c->path);
1309 /* No space? Parent is root command. */
1312 add_sub_command (cm, 0, ci);
1317 vec_add (p_path, c->path, p_len);
1319 p = hash_get_mem (cm->command_index_by_path, p_path);
1321 /* Parent exists? */
1324 /* Parent does not exist; create it. */
1325 vec_add2 (cm->commands, parent, 1);
1326 parent->path = p_path;
1327 hash_set_mem (cm->command_index_by_path, parent->path,
1328 parent - cm->commands);
1329 pi = parent - cm->commands;
1337 add_sub_command (cm, pi, ci);
1339 /* Create parent's parent. */
1341 vlib_cli_make_parent (cm, pi);
1345 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1347 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1351 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1353 vlib_global_main_t *vgm = vlib_get_global_main ();
1354 vlib_cli_main_t *cm = &vgm->cli_main;
1355 clib_error_t *error = 0;
1357 char *normalized_path;
1359 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1362 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1364 if (!cm->command_index_by_path)
1365 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1366 sizeof (c->path[0]),
1369 /* See if command already exists with given path. */
1370 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1373 vlib_cli_command_t *d;
1376 d = vec_elt_at_index (cm->commands, ci);
1378 /* If existing command was created via vlib_cli_make_parent
1379 replaced it with callers data. */
1380 if (vlib_cli_command_is_empty (d))
1382 vlib_cli_command_t save = d[0];
1384 ASSERT (!vlib_cli_command_is_empty (c));
1386 /* Copy callers fields. */
1389 /* Save internal fields. */
1390 d->path = save.path;
1391 d->sub_commands = save.sub_commands;
1392 d->sub_command_index_by_name = save.sub_command_index_by_name;
1393 d->sub_command_positions = save.sub_command_positions;
1394 d->sub_rules = save.sub_rules;
1398 clib_error_return (0, "duplicate command name with path %v",
1401 vec_free (normalized_path);
1407 /* Command does not exist: create it. */
1409 /* Add root command (index 0). */
1410 if (vec_len (cm->commands) == 0)
1412 /* Create command with index 0; path is empty string. */
1413 vec_resize (cm->commands, 1);
1416 ci = vec_len (cm->commands);
1417 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1418 vec_add1 (cm->commands, c[0]);
1420 c = vec_elt_at_index (cm->commands, ci);
1421 c->path = normalized_path;
1423 /* Don't inherit from registration. */
1424 c->sub_commands = 0;
1425 c->sub_command_index_by_name = 0;
1426 c->sub_command_positions = 0;
1429 vlib_cli_make_parent (cm, ci);
1434 /* $$$ turn back on again someday, maybe */
1436 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1438 vlib_cli_main_t *cm = &vm->cli_main;
1439 vlib_cli_parse_rule_t *r;
1440 clib_error_t *error = 0;
1444 if (!cm->parse_rule_index_by_name)
1445 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1446 sizeof (r->name[0]),
1449 /* Make vector copy of name. */
1450 r_name = format (0, "%s", r_reg->name);
1452 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1455 return clib_error_return (0, "duplicate parse rule name `%s'",
1459 vec_add2 (cm->parse_rules, r, 1);
1461 r->name = (char *) r_name;
1462 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1467 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1468 vlib_cli_parse_rule_t *
1470 vlib_cli_parse_rule_t *
1472 __attribute__ ((unused))
1474 clib_error_t *error = 0;
1475 vlib_cli_parse_rule_t *r;
1477 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1479 if (!r->name || strlen (r->name) == 0)
1481 error = clib_error_return (0, "parse rule with no name");
1485 error = vlib_cli_register_parse_rule (vm, r);
1495 static clib_error_t *
1496 event_logger_trace_command_fn (vlib_main_t * vm,
1497 unformat_input_t * input,
1498 vlib_cli_command_t * cmd)
1500 unformat_input_t _line_input, *line_input = &_line_input;
1502 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1503 u32 circuit_node_index;
1505 if (!unformat_user (input, unformat_line_input, line_input))
1508 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1510 if (unformat (line_input, "api"))
1512 else if (unformat (line_input, "dispatch"))
1514 else if (unformat (line_input, "circuit-node %U",
1515 unformat_vlib_node, vm, &circuit_node_index))
1517 else if (unformat (line_input, "cli"))
1519 else if (unformat (line_input, "barrier"))
1521 else if (unformat (line_input, "disable"))
1523 else if (unformat (line_input, "enable"))
1528 unformat_free (line_input);
1530 vl_api_set_elog_trace_api_messages
1531 (api ? enable : vl_api_get_elog_trace_api_messages ());
1532 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1533 vm->elog_trace_graph_dispatch = dispatch ?
1534 enable : vm->elog_trace_graph_dispatch;
1535 vm->elog_trace_graph_circuit = circuit ?
1536 enable : vm->elog_trace_graph_circuit;
1537 vlib_worker_threads->barrier_elog_enabled =
1538 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1539 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1542 * Set up start-of-buffer logic-analyzer trigger
1543 * for main loop event logs, which are fairly heavyweight.
1544 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1545 * will fully disable the scheme when the elog buffer fills.
1547 if (dispatch || circuit)
1549 elog_main_t *em = &vlib_global_main.elog_main;
1551 em->n_total_events_disable_limit =
1552 em->n_total_events + vec_len (em->event_ring);
1557 vlib_cli_output (vm, "Current status:");
1560 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1561 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1562 vm->elog_trace_cli_commands ? "on" : "off");
1564 (vm, " Barrier sync trace: %s",
1565 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1567 (vm, " Graph Dispatch: %s",
1568 vm->elog_trace_graph_dispatch ? "on" : "off");
1570 (vm, " Graph Circuit: %s",
1571 vm->elog_trace_graph_circuit ? "on" : "off");
1572 if (vm->elog_trace_graph_circuit)
1575 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1581 * Control event logging of api, cli, and thread barrier events
1582 * With no arguments, displays the current trace status.
1583 * Name the event groups you wish to trace or stop tracing.
1587 * event-logger trace api cli barrier
1588 * event-logger trace api cli barrier disable
1589 * event-logger trace dispatch
1590 * event-logger trace circuit-node ethernet-input
1592 * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
1595 VLIB_CLI_COMMAND (event_logger_trace_command, static) =
1597 .path = "event-logger trace",
1598 .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
1599 "[circuit-node <name> e.g. ethernet-input][disable]",
1600 .function = event_logger_trace_command_fn,
1604 static clib_error_t *
1605 suspend_command_fn (vlib_main_t * vm,
1606 unformat_input_t * input, vlib_cli_command_t * cmd)
1608 vlib_process_suspend (vm, 30e-3);
1613 VLIB_CLI_COMMAND (suspend_command, static) =
1616 .short_help = "suspend debug CLI for 30ms",
1617 .function = suspend_command_fn,
1624 sort_cmds_by_path (void *a1, void *a2)
1628 vlib_global_main_t *vgm = vlib_get_global_main ();
1629 vlib_cli_main_t *cm = &vgm->cli_main;
1630 vlib_cli_command_t *c1, *c2;
1633 c1 = vec_elt_at_index (cm->commands, *index1);
1634 c2 = vec_elt_at_index (cm->commands, *index2);
1636 lmin = vec_len (c1->path);
1637 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1639 for (i = 0; i < lmin; i++)
1641 if (c1->path[i] < c2->path[i])
1643 else if (c1->path[i] > c2->path[i])
1652 vlib_cli_main_t *cm;
1653 u32 parent_command_index;
1655 int show_not_mp_safe;
1658 } vlib_cli_walk_args_t;
1661 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1663 vlib_cli_command_t *parent;
1664 vlib_cli_sub_command_t *sub;
1665 vlib_cli_walk_args_t _a, *a = &_a;
1666 vlib_cli_main_t *cm;
1669 /* Copy args into this stack frame */
1673 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1675 if (parent->function)
1677 if (((a->show_mp_safe && parent->is_mp_safe)
1678 || (a->show_not_mp_safe && !parent->is_mp_safe))
1679 && (a->show_hit == 0 || parent->hit_counter))
1681 vec_add1 (cm->sort_vector, a->parent_command_index);
1685 parent->hit_counter = 0;
1688 for (i = 0; i < vec_len (parent->sub_commands); i++)
1690 sub = vec_elt_at_index (parent->sub_commands, i);
1691 a->parent_command_index = sub->index;
1692 cli_recursive_walk (a);
1697 format_mp_safe (u8 * s, va_list * args)
1699 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1700 int show_mp_safe = va_arg (*args, int);
1701 int show_not_mp_safe = va_arg (*args, int);
1702 int show_hit = va_arg (*args, int);
1703 int clear_hit = va_arg (*args, int);
1704 vlib_cli_command_t *c;
1705 vlib_cli_walk_args_t _a, *a = &_a;
1707 char *format_string = "\n%v";
1710 format_string = "\n%v: %u";
1712 vec_reset_length (cm->sort_vector);
1715 a->parent_command_index = 0;
1716 a->show_mp_safe = show_mp_safe;
1717 a->show_not_mp_safe = show_not_mp_safe;
1718 a->show_hit = show_hit;
1719 a->clear_hit = clear_hit;
1721 cli_recursive_walk (a);
1723 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1725 for (i = 0; i < vec_len (cm->sort_vector); i++)
1727 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1728 s = format (s, format_string, c->path, c->hit_counter);
1735 static clib_error_t *
1736 show_cli_command_fn (vlib_main_t * vm,
1737 unformat_input_t * input, vlib_cli_command_t * cmd)
1739 vlib_global_main_t *vgm = vlib_get_global_main ();
1740 int show_mp_safe = 0;
1741 int show_not_mp_safe = 0;
1745 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1747 if (unformat (input, "mp-safe"))
1749 if (unformat (input, "not-mp-safe"))
1750 show_not_mp_safe = 1;
1751 else if (unformat (input, "hit"))
1753 else if (unformat (input, "clear-hit"))
1759 /* default set: all cli commands */
1760 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1761 show_mp_safe = show_not_mp_safe = 1;
1763 vlib_cli_output (vm, "%U", format_mp_safe, &vgm->cli_main, show_mp_safe,
1764 show_not_mp_safe, show_hit, clear_hit);
1766 vlib_cli_output (vm, "hit counters cleared...");
1772 * Displays debug cli command information
1775 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1777 * "show cli" displays the entire debug cli:
1781 * adjacency counters
1787 * "show cli mp-safe" displays mp-safe debug CLI commands:
1801 * "show cli not-mp-safe" displays debug CLI commands
1802 * which cause worker thread barrier synchronization
1804 * "show cli hit" displays commands which have been executed. Qualify
1805 * as desired with "mp-safe" or "not-mp-safe".
1807 * "show cli clear-hit" clears the per-command hit counters.
1812 VLIB_CLI_COMMAND (show_cli_command, static) =
1815 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1816 .function = show_cli_command_fn,
1821 static clib_error_t *
1822 vlib_cli_init (vlib_main_t * vm)
1824 vlib_global_main_t *vgm = vlib_get_global_main ();
1825 vlib_cli_main_t *cm = &vgm->cli_main;
1826 clib_error_t *error = 0;
1827 vlib_cli_command_t *cmd;
1829 cmd = cm->cli_command_registrations;
1833 error = vlib_cli_register (vm, cmd);
1836 cmd = cmd->next_cli_command;
1839 cm->log = vlib_log_register_class_rate_limit (
1840 "cli", "log", 0x7FFFFFFF /* aka no rate limit */);
1844 VLIB_INIT_FUNCTION (vlib_cli_init);
1847 * fd.io coding-style-patch-verification: ON
1850 * eval: (c-set-style "gnu")