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);
202 /* Looks for string based sub-input formatted { SUB-INPUT }. */
204 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
206 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
212 c = unformat_get_input (i);
224 /* Put back paren. */
225 if (c != UNFORMAT_END_OF_INPUT)
226 unformat_put_input (i);
228 if (c == '{' && unformat (i, "%v", &s))
230 unformat_init_vector (sub_input, s);
239 static vlib_cli_command_t *
240 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
242 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
243 return vec_elt_at_index (cm->commands, s->index);
247 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
249 vlib_main_t __clib_unused *vm = va_arg (*args, vlib_main_t *);
250 vlib_global_main_t *vgm = vlib_get_global_main ();
251 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
252 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
253 vlib_cli_main_t *cm = &vgm->cli_main;
254 uword *match_bitmap, is_unique, index;
256 match_bitmap = vlib_cli_sub_command_match (c, i);
257 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
261 index = clib_bitmap_first_set (match_bitmap);
262 *result = get_sub_command (cm, c, index);
264 clib_bitmap_free (match_bitmap);
270 vlib_cli_cmp_strings (void *a1, void *a2)
272 u8 *c1 = *(u8 **) a1;
273 u8 *c2 = *(u8 **) a2;
275 return vec_cmp (c1, c2);
279 vlib_cli_get_possible_completions (u8 * str)
281 vlib_cli_command_t *c;
282 vlib_cli_sub_command_t *sc;
283 vlib_global_main_t *vgm = vlib_get_global_main ();
284 vlib_cli_main_t *vcm = &vgm->cli_main;
285 uword *match_bitmap = 0;
286 uword index, is_unique, help_next_level;
288 unformat_input_t input;
289 unformat_init_vector (&input, vec_dup (str));
290 c = vec_elt_at_index (vcm->commands, 0);
292 /* remove trailing whitespace, except for one of them */
293 while (vec_len (input.buffer) >= 2 &&
294 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
295 isspace (input.buffer[vec_len (input.buffer) - 2]))
297 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
300 /* if input is empty, directly return list of root commands */
301 if (vec_len (input.buffer) == 0 ||
302 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
304 vec_foreach (sc, c->sub_commands)
306 vec_add1 (result, (u8 *) sc->name);
311 /* add a trailing '?' so that vlib_cli_sub_command_match can find
312 * all commands starting with the input string */
313 vec_add1 (input.buffer, '?');
317 match_bitmap = vlib_cli_sub_command_match (c, &input);
318 /* no match: return no result */
319 if (match_bitmap == 0)
323 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
324 /* unique match: try to step one subcommand level further */
327 /* stop if no more input */
328 if (input.index >= vec_len (input.buffer) - 1)
333 index = clib_bitmap_first_set (match_bitmap);
334 c = get_sub_command (vcm, c, index);
335 clib_bitmap_free (match_bitmap);
338 /* multiple matches: stop here, return all matches */
342 /* remove trailing '?' */
343 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
345 /* if we have a space at the end of input, and a unique match,
346 * autocomplete the next level of subcommands */
347 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
349 clib_bitmap_foreach (index, match_bitmap) {
350 if (help_next_level && is_unique) {
351 c = get_sub_command (vcm, c, index);
352 vec_foreach (sc, c->sub_commands) {
353 vec_add1 (result, (u8*) sc->name);
355 goto done; /* break doesn't work in this macro-loop */
357 sc = &c->sub_commands[index];
358 vec_add1(result, (u8*) sc->name);
363 clib_bitmap_free (match_bitmap);
364 unformat_free (&input);
367 vec_sort_with_function (result, vlib_cli_cmp_strings);
372 format_vlib_cli_command_help (u8 * s, va_list * args)
374 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
375 int is_long = va_arg (*args, int);
376 if (is_long && c->long_help)
377 s = format (s, "%s", c->long_help);
378 else if (c->short_help)
379 s = format (s, "%s", c->short_help);
381 s = format (s, "%v commands", c->path);
386 format_vlib_cli_path (u8 * s, va_list * args)
388 u8 *path = va_arg (*args, u8 *);
390 s = format (s, "%v", path);
395 static vlib_cli_command_t *
396 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
398 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
399 vlib_cli_sub_command_t *sc;
402 vec_add1 (subs, c[0]);
404 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
410 vlib_cli_cmp_rule (void *a1, void *a2)
412 vlib_cli_sub_rule_t *r1 = a1;
413 vlib_cli_sub_rule_t *r2 = a2;
415 return vec_cmp (r1->name, r2->name);
419 vlib_cli_cmp_command (void *a1, void *a2)
421 vlib_cli_command_t *c1 = a1;
422 vlib_cli_command_t *c2 = a2;
424 return vec_cmp (c1->path, c2->path);
427 static clib_error_t *
428 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
429 vlib_cli_main_t * cm,
430 unformat_input_t * input,
431 uword parent_command_index)
433 vlib_global_main_t *vgm = vlib_get_global_main ();
434 vlib_cli_command_t *parent, *c;
435 clib_error_t *error = 0;
436 unformat_input_t sub_input;
438 uword is_main_dispatch = cm == &vgm->cli_main;
440 parent = vec_elt_at_index (cm->commands, parent_command_index);
441 if (is_main_dispatch && unformat (input, "help"))
443 uword help_at_end_of_line, i;
445 help_at_end_of_line =
446 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
451 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
454 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
461 /* help SUB-COMMAND => long format help.
462 "help" at end of line: show all commands. */
463 if (!help_at_end_of_line)
464 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
467 else if (vec_len (c->sub_commands) == 0)
468 vlib_cli_output (vm, "%v: no sub-commands", c->path);
472 vlib_cli_sub_rule_t *sr, *subs = 0;
473 vlib_cli_sub_command_t *sc;
475 vec_foreach (sc, c->sub_commands)
477 vec_add2 (subs, sr, 1);
479 sr->command_index = sc->index;
483 vec_sort_with_function (subs, vlib_cli_cmp_rule);
485 for (i = 0; i < vec_len (subs); i++)
487 vlib_cli_command_t *d;
489 d = vec_elt_at_index (cm->commands, subs[i].command_index);
491 (vm, " %-30v %U", subs[i].name,
492 format_vlib_cli_command_help, d, /* is_long */ 0);
499 else if (is_main_dispatch
500 && (unformat (input, "choices") || unformat (input, "?")))
502 vlib_cli_command_t *sub, *subs;
504 subs = all_subs (cm, 0, parent_command_index);
505 vec_sort_with_function (subs, vlib_cli_cmp_command);
506 vec_foreach (sub, subs)
507 vlib_cli_output (vm, " %-40U %U",
508 format_vlib_cli_path, sub->path,
509 format_vlib_cli_command_help, sub, /* is_long */ 0);
513 else if (unformat (input, "comment %v", &string))
518 else if (unformat (input, "vpplog %v", &string))
522 * Delete leading whitespace, so "vpplog { this and that }"
523 * and "vpplog this" line up nicely.
525 for (i = 0; i < vec_len (string); i++)
526 if (string[i] != ' ')
529 vec_delete (string, i, 0);
531 vlib_log_notice (cm->log, "CLI: %v", string);
535 else if (unformat (input, "uncomment %U",
536 unformat_vlib_cli_sub_input, &sub_input))
539 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
540 parent_command_index);
541 unformat_free (&sub_input);
543 else if (unformat (input, "leak-check %U",
544 unformat_vlib_cli_sub_input, &sub_input))
547 if (current_traced_heap)
550 oldheap = clib_mem_set_heap (current_traced_heap);
552 clib_mem_set_heap (oldheap);
553 current_traced_heap = 0;
557 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
558 parent_command_index);
559 unformat_free (&sub_input);
561 /* Otherwise, the clib_error_t shows up as a leak... */
564 vlib_cli_output (vm, "%v", error->what);
565 clib_error_free (error);
569 (void) clib_mem_trace_enable_disable (0);
570 leak_report = format (0, "%U", format_clib_mem_heap, 0,
571 1 /* verbose, i.e. print leaks */ );
573 vlib_cli_output (vm, "%v", leak_report);
574 vec_free (leak_report);
578 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
580 unformat_input_t *si;
581 uword has_sub_commands =
582 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
585 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
588 if (has_sub_commands)
589 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
591 if (has_sub_commands && !error)
592 /* Found valid sub-command. */ ;
594 else if (c->function)
596 clib_error_t *c_error;
598 /* Skip white space for benefit of called function. */
599 unformat_skip_white_space (si);
601 if (unformat (si, "?"))
603 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
608 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
611 ELOG_TYPE_DECLARE (e) =
613 .format = "cli-cmd: %s",
621 ed = ELOG_DATA (vlib_get_elog_main (), e);
622 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
626 vlib_worker_thread_barrier_sync (vm);
627 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
628 clib_call_callbacks (cm->perf_counter_cbs, cm,
629 c - cm->commands, 0 /* before */ );
632 c_error = c->function (vm, si, c);
634 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
635 clib_call_callbacks (cm->perf_counter_cbs, cm,
636 c - cm->commands, 1 /* after */ );
638 vlib_worker_thread_barrier_release (vm);
640 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
643 ELOG_TYPE_DECLARE (e) =
645 .format = "cli-cmd: %s %s",
646 .format_args = "T4T4",
653 ed = ELOG_DATA (vlib_get_elog_main (), e);
654 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
657 vec_add1 (c_error->what, 0);
658 ed->err = elog_string (vlib_get_elog_main (),
659 (char *) c_error->what);
660 vec_dec_len (c_error->what, 1);
663 ed->err = elog_string (vlib_get_elog_main (), "OK");
669 clib_error_return (0, "%v: %v", c->path, c_error->what);
670 clib_error_free (c_error);
671 /* Free sub input. */
679 /* Free any previous error. */
680 clib_error_free (error);
684 error = clib_error_return (0, "%v: no sub-commands", c->path);
686 /* Free sub input. */
698 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
699 format_unformat_error, input);
701 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
706 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
707 __attribute__ ((weak));
710 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
714 /* Process CLI input. */
716 vlib_cli_input (vlib_main_t * vm,
717 unformat_input_t * input,
718 vlib_cli_output_function_t * function, uword function_arg)
720 vlib_global_main_t *vgm = vlib_get_global_main ();
721 vlib_process_t *cp = vlib_get_current_process (vm);
723 vlib_cli_output_function_t *save_function;
724 uword save_function_arg;
727 save_function = cp->output_function;
728 save_function_arg = cp->output_function_arg;
730 cp->output_function = function;
731 cp->output_function_arg = function_arg;
735 error = vlib_cli_dispatch_sub_commands (vm, &vgm->cli_main, input,
738 while (!error && !unformat (input, "%U", unformat_eof));
742 vlib_cli_output (vm, "%v", error->what);
743 vlib_unix_error_report (vm, error);
744 /* clib_error_return is unfortunately often called with a '0'
746 rv = error->code != 0 ? error->code : -1;
747 clib_error_free (error);
750 cp->output_function = save_function;
751 cp->output_function_arg = save_function_arg;
755 /* Output to current CLI connection. */
757 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
759 vlib_process_t *cp = vlib_get_current_process (vm);
764 s = va_format (0, fmt, &va);
767 /* some format functions might return 0
768 * e.g. show int addr */
772 /* Terminate with \n if not present. */
773 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
776 if ((!cp) || (!cp->output_function))
777 fformat (stdout, "%v", s);
779 cp->output_function (cp->output_function_arg, s, vec_len (s));
784 void *vl_msg_push_heap (void) __attribute__ ((weak));
786 vl_msg_push_heap (void)
791 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
793 vl_msg_pop_heap (void *oldheap)
797 static clib_error_t *
798 show_memory_usage (vlib_main_t * vm,
799 unformat_input_t * input, vlib_cli_command_t * cmd)
801 clib_mem_main_t *mm = &clib_mem_main;
802 int verbose __attribute__ ((unused)) = 0;
803 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
808 uword clib_mem_trace_enable_disable (uword enable);
812 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
814 if (unformat (input, "verbose"))
816 else if (unformat (input, "api-segment"))
818 else if (unformat (input, "stats-segment"))
820 else if (unformat (input, "main-heap"))
822 else if (unformat (input, "numa-heaps"))
824 else if (unformat (input, "map"))
828 error = clib_error_return (0, "unknown input `%U'",
829 format_unformat_error, input);
834 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
835 return clib_error_return
836 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
841 void *oldheap = vl_msg_push_heap ();
842 was_enabled = clib_mem_trace_enable_disable (0);
843 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
844 vl_msg_pop_heap (oldheap);
845 u8 *s = vec_dup (s_in_svm);
847 oldheap = vl_msg_push_heap ();
849 clib_mem_trace_enable_disable (was_enabled);
850 vl_msg_pop_heap (oldheap);
851 vlib_cli_output (vm, "API segment");
852 vlib_cli_output (vm, "%v", s);
857 void *oldheap = vlib_stats_set_heap (0);
858 was_enabled = clib_mem_trace_enable_disable (0);
859 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
861 clib_mem_set_heap (oldheap);
862 u8 *s = vec_dup (s_in_svm);
864 oldheap = vlib_stats_set_heap (0);
868 clib_mem_trace_enable_disable (was_enabled);
869 clib_mem_set_heap (oldheap);
871 vlib_cli_output (vm, "Stats segment");
872 vlib_cli_output (vm, "%v", s);
881 * Note: the foreach_vlib_main causes allocator traffic,
882 * so shut off tracing before we go there...
884 was_enabled = clib_mem_trace_enable_disable (0);
888 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n" : "", index,
889 vlib_worker_threads[index].name);
890 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
891 mm->per_cpu_mheaps[index], verbose);
895 /* Restore the trace flag */
896 clib_mem_trace_enable_disable (was_enabled);
900 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
902 if (mm->per_numa_mheaps[i] == 0)
904 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
906 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
909 was_enabled = clib_mem_trace_enable_disable (0);
911 vlib_cli_output (vm, "Numa %d:", i);
912 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
913 mm->per_numa_mheaps[index], verbose);
918 clib_mem_page_stats_t stats = { };
919 clib_mem_vm_map_hdr_t *hdr = 0;
923 s = format (s, "\n%-16s%7s%5s%7s%7s",
924 "StartAddr", "size", "FD", "PageSz", "Pages");
925 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
926 s = format (s, " Numa%u", numa);
927 s = format (s, " NotMap");
928 s = format (s, " Name");
929 vlib_cli_output (vm, "%v", s);
930 vec_reset_length (s);
932 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
934 clib_mem_get_page_stats ((void *) hdr->base_addr,
935 hdr->log2_page_sz, hdr->num_pages,
937 s = format (s, "%016lx%7U",
938 hdr->base_addr, format_memory_size,
939 hdr->num_pages << hdr->log2_page_sz);
942 s = format (s, "%5d", hdr->fd);
944 s = format (s, "%5s", " ");
946 s = format (s, "%7U%7lu",
947 format_log2_page_size, hdr->log2_page_sz,
949 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
950 s = format (s, "%6lu", stats.per_numa[numa]);
951 s = format (s, "%7lu", stats.not_mapped);
952 s = format (s, " %s", hdr->name);
953 vlib_cli_output (vm, "%v", s);
954 vec_reset_length (s);
963 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
964 .path = "show memory",
965 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
966 " [numa-heaps][map]",
967 .function = show_memory_usage,
971 static clib_error_t *
972 show_cpu (vlib_main_t * vm, unformat_input_t * input,
973 vlib_cli_command_t * cmd)
975 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
976 _("Model name", "%U", format_cpu_model_name);
977 _("Microarch model (family)", "%U", format_cpu_uarch);
978 _("Flags", "%U", format_cpu_flags);
979 _("Base frequency", "%.2f GHz",
980 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
986 * Displays various information about the CPU.
989 * @cliexstart{show cpu}
990 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
991 * Microarchitecture: Broadwell (Broadwell-EP/EX)
992 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
993 * Base Frequency: 3.20 GHz
997 VLIB_CLI_COMMAND (show_cpu_command, static) = {
999 .short_help = "Show cpu information",
1000 .function = show_cpu,
1004 static clib_error_t *
1005 enable_disable_memory_trace (vlib_main_t * vm,
1006 unformat_input_t * input,
1007 vlib_cli_command_t * cmd)
1009 clib_mem_main_t *mm = &clib_mem_main;
1010 unformat_input_t _line_input, *line_input = &_line_input;
1012 int api_segment = 0;
1013 int stats_segment = 0;
1018 if (!unformat_user (input, unformat_line_input, line_input))
1021 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1023 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
1025 else if (unformat (line_input, "api-segment"))
1027 else if (unformat (line_input, "stats-segment"))
1029 else if (unformat (line_input, "main-heap"))
1031 else if (unformat (line_input, "numa-heap %d", &numa_id))
1035 unformat_free (line_input);
1036 return clib_error_return (0, "invalid input");
1039 unformat_free (line_input);
1041 if ((api_segment + stats_segment + main_heap + (enable == 0)
1042 + (numa_id != ~0)) == 0)
1044 return clib_error_return
1045 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1046 "numa-heap <nn> or disable");
1049 /* Turn off current trace, if any */
1050 if (current_traced_heap)
1053 oldheap = clib_mem_set_heap (current_traced_heap);
1055 clib_mem_set_heap (oldheap);
1056 current_traced_heap = 0;
1065 oldheap = vl_msg_push_heap ();
1066 current_traced_heap = clib_mem_get_heap ();
1068 vl_msg_pop_heap (oldheap);
1075 oldheap = vlib_stats_set_heap ();
1076 current_traced_heap = clib_mem_get_heap ();
1077 clib_mem_trace (stats_segment);
1078 /* We don't want to call vlib_stats_pop_heap... */
1080 clib_mem_set_heap (oldheap);
1086 current_traced_heap = clib_mem_get_heap ();
1087 clib_mem_trace (main_heap);
1092 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
1093 return clib_error_return (0, "Numa %d out of range", numa_id);
1094 if (mm->per_numa_mheaps[numa_id] == 0)
1095 return clib_error_return (0, "Numa %d heap not active", numa_id);
1097 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
1098 return clib_error_return (0, "Numa %d uses the main heap...",
1100 current_traced_heap = mm->per_numa_mheaps[numa_id];
1101 oldheap = clib_mem_set_heap (current_traced_heap);
1103 clib_mem_set_heap (oldheap);
1111 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1112 .path = "memory-trace",
1113 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1114 " [numa-heap <numa-id>]\n",
1115 .function = enable_disable_memory_trace,
1119 static clib_error_t *
1120 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1121 vlib_cli_command_t * cmd)
1123 vlib_global_main_t *vgm = vlib_get_global_main ();
1124 clib_file_main_t *fm = &file_main;
1127 /* environ(7) does not indicate a header for this */
1128 extern char **environ;
1130 /* Close all known open files */
1132 pool_foreach (f, fm->file_pool)
1134 if (f->file_descriptor > 2)
1135 close(f->file_descriptor);
1140 execve (vgm->name, (char **) vgm->argv, environ);
1146 VLIB_CLI_COMMAND (restart_cmd,static) = {
1148 .short_help = "restart process",
1149 .function = restart_cmd_fn,
1155 * A trivial test harness to verify the per-process output_function
1156 * is working correcty.
1159 static clib_error_t *
1160 sleep_ten_seconds (vlib_main_t * vm,
1161 unformat_input_t * input, vlib_cli_command_t * cmd)
1164 u16 my_id = rand ();
1166 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1168 for (i = 0; i < 10; i++)
1170 vlib_process_wait_for_event_or_clock (vm, 1.0);
1171 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1173 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1178 VLIB_CLI_COMMAND (ping_command, static) = {
1179 .path = "test sleep",
1180 .function = sleep_ten_seconds,
1181 .short_help = "Sleep for 10 seconds",
1184 #endif /* ifdef TEST_CODE */
1187 vlib_cli_normalize_path (char *input, char **result)
1192 uword index_of_last_space = ~0;
1197 /* Multiple white space -> single space. */
1204 if (l > 0 && s[l - 1] != ' ')
1212 if (l > 0 && s[l - 1] == ' ')
1213 index_of_last_space = vec_len (s);
1220 /* Remove any extra space at end. */
1221 if (l > 0 && s[l - 1] == ' ')
1225 return index_of_last_space;
1229 parent_path_len (char *path)
1232 for (i = vec_len (path) - 1; i >= 0; i--)
1241 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1243 vlib_cli_command_t *p, *c;
1244 vlib_cli_sub_command_t *sub_c;
1248 p = vec_elt_at_index (cm->commands, parent_index);
1249 c = vec_elt_at_index (cm->commands, child_index);
1251 l = parent_path_len (c->path);
1253 sub_name = vec_dup ((u8 *) c->path);
1256 ASSERT (l + 1 < vec_len (c->path));
1258 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1261 /* "Can't happen," check mainly to shut up coverity */
1262 ALWAYS_ASSERT (sub_name != 0);
1264 if (sub_name[0] == '%')
1267 vlib_cli_sub_rule_t *sr;
1270 vec_delete (sub_name, 1, 0);
1272 if (!p->sub_rule_index_by_name)
1273 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1274 sizeof (sub_name[0]),
1276 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1279 sr = vec_elt_at_index (p->sub_rules, q[0]);
1280 ASSERT (sr->command_index == child_index);
1284 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1285 vec_len (p->sub_rules));
1286 vec_add2 (p->sub_rules, sr, 1);
1287 sr->name = sub_name;
1288 sr->rule_index = sr - p->sub_rules;
1289 sr->command_index = child_index;
1293 if (!p->sub_command_index_by_name)
1294 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1295 sizeof (c->path[0]),
1298 /* Check if sub-command has already been created. */
1299 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1301 vec_free (sub_name);
1305 vec_add2 (p->sub_commands, sub_c, 1);
1306 sub_c->index = child_index;
1307 sub_c->name = sub_name;
1308 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1309 sub_c - p->sub_commands);
1311 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1312 for (i = 0; i < vec_len (sub_c->name); i++)
1315 vlib_cli_parse_position_t *pos;
1317 pos = vec_elt_at_index (p->sub_command_positions, i);
1320 pos->min_char = sub_c->name[i];
1322 n = sub_c->name[i] - pos->min_char;
1325 pos->min_char = sub_c->name[i];
1326 vec_insert (pos->bitmaps, -n, 0);
1330 vec_validate (pos->bitmaps, n);
1332 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1337 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1339 uword p_len, pi, *p;
1341 vlib_cli_command_t *c, *parent;
1343 /* Root command (index 0) should have already been added. */
1344 ASSERT (vec_len (cm->commands) > 0);
1346 c = vec_elt_at_index (cm->commands, ci);
1347 p_len = parent_path_len (c->path);
1349 /* No space? Parent is root command. */
1352 add_sub_command (cm, 0, ci);
1357 vec_add (p_path, c->path, p_len);
1359 p = hash_get_mem (cm->command_index_by_path, p_path);
1361 /* Parent exists? */
1364 /* Parent does not exist; create it. */
1365 vec_add2 (cm->commands, parent, 1);
1366 parent->path = p_path;
1367 hash_set_mem (cm->command_index_by_path, parent->path,
1368 parent - cm->commands);
1369 pi = parent - cm->commands;
1377 add_sub_command (cm, pi, ci);
1379 /* Create parent's parent. */
1381 vlib_cli_make_parent (cm, pi);
1385 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1387 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1391 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1393 vlib_global_main_t *vgm = vlib_get_global_main ();
1394 vlib_cli_main_t *cm = &vgm->cli_main;
1395 clib_error_t *error = 0;
1397 char *normalized_path;
1399 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1402 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1404 if (!cm->command_index_by_path)
1405 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1406 sizeof (c->path[0]),
1409 /* See if command already exists with given path. */
1410 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1413 vlib_cli_command_t *d;
1416 d = vec_elt_at_index (cm->commands, ci);
1418 /* If existing command was created via vlib_cli_make_parent
1419 replaced it with callers data. */
1420 if (vlib_cli_command_is_empty (d))
1422 vlib_cli_command_t save = d[0];
1424 ASSERT (!vlib_cli_command_is_empty (c));
1426 /* Copy callers fields. */
1429 /* Save internal fields. */
1430 d->path = save.path;
1431 d->sub_commands = save.sub_commands;
1432 d->sub_command_index_by_name = save.sub_command_index_by_name;
1433 d->sub_command_positions = save.sub_command_positions;
1434 d->sub_rules = save.sub_rules;
1438 clib_error_return (0, "duplicate command name with path %v",
1441 vec_free (normalized_path);
1447 /* Command does not exist: create it. */
1449 /* Add root command (index 0). */
1450 if (vec_len (cm->commands) == 0)
1452 /* Create command with index 0; path is empty string. */
1453 vec_resize (cm->commands, 1);
1456 ci = vec_len (cm->commands);
1457 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1458 vec_add1 (cm->commands, c[0]);
1460 c = vec_elt_at_index (cm->commands, ci);
1461 c->path = normalized_path;
1463 /* Don't inherit from registration. */
1464 c->sub_commands = 0;
1465 c->sub_command_index_by_name = 0;
1466 c->sub_command_positions = 0;
1469 vlib_cli_make_parent (cm, ci);
1474 /* $$$ turn back on again someday, maybe */
1476 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1478 vlib_cli_main_t *cm = &vm->cli_main;
1479 vlib_cli_parse_rule_t *r;
1480 clib_error_t *error = 0;
1484 if (!cm->parse_rule_index_by_name)
1485 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1486 sizeof (r->name[0]),
1489 /* Make vector copy of name. */
1490 r_name = format (0, "%s", r_reg->name);
1492 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1495 return clib_error_return (0, "duplicate parse rule name `%s'",
1499 vec_add2 (cm->parse_rules, r, 1);
1501 r->name = (char *) r_name;
1502 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1507 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1508 vlib_cli_parse_rule_t *
1510 vlib_cli_parse_rule_t *
1512 __attribute__ ((unused))
1514 clib_error_t *error = 0;
1515 vlib_cli_parse_rule_t *r;
1517 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1519 if (!r->name || strlen (r->name) == 0)
1521 error = clib_error_return (0, "parse rule with no name");
1525 error = vlib_cli_register_parse_rule (vm, r);
1535 static clib_error_t *
1536 event_logger_trace_command_fn (vlib_main_t * vm,
1537 unformat_input_t * input,
1538 vlib_cli_command_t * cmd)
1540 unformat_input_t _line_input, *line_input = &_line_input;
1542 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1543 u32 circuit_node_index;
1545 if (!unformat_user (input, unformat_line_input, line_input))
1548 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1550 if (unformat (line_input, "api"))
1552 else if (unformat (line_input, "dispatch"))
1554 else if (unformat (line_input, "circuit-node %U",
1555 unformat_vlib_node, vm, &circuit_node_index))
1557 else if (unformat (line_input, "cli"))
1559 else if (unformat (line_input, "barrier"))
1561 else if (unformat (line_input, "disable"))
1563 else if (unformat (line_input, "enable"))
1568 unformat_free (line_input);
1570 vl_api_set_elog_trace_api_messages
1571 (api ? enable : vl_api_get_elog_trace_api_messages ());
1572 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1573 vm->elog_trace_graph_dispatch = dispatch ?
1574 enable : vm->elog_trace_graph_dispatch;
1575 vm->elog_trace_graph_circuit = circuit ?
1576 enable : vm->elog_trace_graph_circuit;
1577 vlib_worker_threads->barrier_elog_enabled =
1578 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1579 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1582 * Set up start-of-buffer logic-analyzer trigger
1583 * for main loop event logs, which are fairly heavyweight.
1584 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1585 * will fully disable the scheme when the elog buffer fills.
1587 if (dispatch || circuit)
1589 elog_main_t *em = &vlib_global_main.elog_main;
1591 em->n_total_events_disable_limit =
1592 em->n_total_events + vec_len (em->event_ring);
1597 vlib_cli_output (vm, "Current status:");
1600 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1601 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1602 vm->elog_trace_cli_commands ? "on" : "off");
1604 (vm, " Barrier sync trace: %s",
1605 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1607 (vm, " Graph Dispatch: %s",
1608 vm->elog_trace_graph_dispatch ? "on" : "off");
1610 (vm, " Graph Circuit: %s",
1611 vm->elog_trace_graph_circuit ? "on" : "off");
1612 if (vm->elog_trace_graph_circuit)
1615 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1621 * Control event logging of api, cli, and thread barrier events
1622 * With no arguments, displays the current trace status.
1623 * Name the event groups you wish to trace or stop tracing.
1627 * event-logger trace api cli barrier
1628 * event-logger trace api cli barrier disable
1629 * event-logger trace dispatch
1630 * event-logger trace circuit-node ethernet-input
1632 * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
1635 VLIB_CLI_COMMAND (event_logger_trace_command, static) =
1637 .path = "event-logger trace",
1638 .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
1639 "[circuit-node <name> e.g. ethernet-input][disable]",
1640 .function = event_logger_trace_command_fn,
1644 static clib_error_t *
1645 suspend_command_fn (vlib_main_t * vm,
1646 unformat_input_t * input, vlib_cli_command_t * cmd)
1648 vlib_process_suspend (vm, 30e-3);
1653 VLIB_CLI_COMMAND (suspend_command, static) =
1656 .short_help = "suspend debug CLI for 30ms",
1657 .function = suspend_command_fn,
1664 sort_cmds_by_path (void *a1, void *a2)
1668 vlib_global_main_t *vgm = vlib_get_global_main ();
1669 vlib_cli_main_t *cm = &vgm->cli_main;
1670 vlib_cli_command_t *c1, *c2;
1673 c1 = vec_elt_at_index (cm->commands, *index1);
1674 c2 = vec_elt_at_index (cm->commands, *index2);
1676 lmin = vec_len (c1->path);
1677 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1679 for (i = 0; i < lmin; i++)
1681 if (c1->path[i] < c2->path[i])
1683 else if (c1->path[i] > c2->path[i])
1692 vlib_cli_main_t *cm;
1693 u32 parent_command_index;
1695 int show_not_mp_safe;
1698 } vlib_cli_walk_args_t;
1701 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1703 vlib_cli_command_t *parent;
1704 vlib_cli_sub_command_t *sub;
1705 vlib_cli_walk_args_t _a, *a = &_a;
1706 vlib_cli_main_t *cm;
1709 /* Copy args into this stack frame */
1713 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1715 if (parent->function)
1717 if (((a->show_mp_safe && parent->is_mp_safe)
1718 || (a->show_not_mp_safe && !parent->is_mp_safe))
1719 && (a->show_hit == 0 || parent->hit_counter))
1721 vec_add1 (cm->sort_vector, a->parent_command_index);
1725 parent->hit_counter = 0;
1728 for (i = 0; i < vec_len (parent->sub_commands); i++)
1730 sub = vec_elt_at_index (parent->sub_commands, i);
1731 a->parent_command_index = sub->index;
1732 cli_recursive_walk (a);
1737 format_mp_safe (u8 * s, va_list * args)
1739 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1740 int show_mp_safe = va_arg (*args, int);
1741 int show_not_mp_safe = va_arg (*args, int);
1742 int show_hit = va_arg (*args, int);
1743 int clear_hit = va_arg (*args, int);
1744 vlib_cli_command_t *c;
1745 vlib_cli_walk_args_t _a, *a = &_a;
1747 char *format_string = "\n%v";
1750 format_string = "\n%v: %u";
1752 vec_reset_length (cm->sort_vector);
1755 a->parent_command_index = 0;
1756 a->show_mp_safe = show_mp_safe;
1757 a->show_not_mp_safe = show_not_mp_safe;
1758 a->show_hit = show_hit;
1759 a->clear_hit = clear_hit;
1761 cli_recursive_walk (a);
1763 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1765 for (i = 0; i < vec_len (cm->sort_vector); i++)
1767 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1768 s = format (s, format_string, c->path, c->hit_counter);
1775 static clib_error_t *
1776 show_cli_command_fn (vlib_main_t * vm,
1777 unformat_input_t * input, vlib_cli_command_t * cmd)
1779 vlib_global_main_t *vgm = vlib_get_global_main ();
1780 int show_mp_safe = 0;
1781 int show_not_mp_safe = 0;
1785 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1787 if (unformat (input, "mp-safe"))
1789 if (unformat (input, "not-mp-safe"))
1790 show_not_mp_safe = 1;
1791 else if (unformat (input, "hit"))
1793 else if (unformat (input, "clear-hit"))
1799 /* default set: all cli commands */
1800 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1801 show_mp_safe = show_not_mp_safe = 1;
1803 vlib_cli_output (vm, "%U", format_mp_safe, &vgm->cli_main, show_mp_safe,
1804 show_not_mp_safe, show_hit, clear_hit);
1806 vlib_cli_output (vm, "hit counters cleared...");
1812 * Displays debug cli command information
1815 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1817 * "show cli" displays the entire debug cli:
1821 * adjacency counters
1827 * "show cli mp-safe" displays mp-safe debug CLI commands:
1841 * "show cli not-mp-safe" displays debug CLI commands
1842 * which cause worker thread barrier synchronization
1844 * "show cli hit" displays commands which have been executed. Qualify
1845 * as desired with "mp-safe" or "not-mp-safe".
1847 * "show cli clear-hit" clears the per-command hit counters.
1852 VLIB_CLI_COMMAND (show_cli_command, static) =
1855 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1856 .function = show_cli_command_fn,
1861 static clib_error_t *
1862 vlib_cli_init (vlib_main_t * vm)
1864 vlib_global_main_t *vgm = vlib_get_global_main ();
1865 vlib_cli_main_t *cm = &vgm->cli_main;
1866 clib_error_t *error = 0;
1867 vlib_cli_command_t *cmd;
1869 cmd = cm->cli_command_registrations;
1873 error = vlib_cli_register (vm, cmd);
1876 cmd = cmd->next_cli_command;
1879 cm->log = vlib_log_register_class_rate_limit (
1880 "cli", "log", 0x7FFFFFFF /* aka no rate limit */);
1884 VLIB_INIT_FUNCTION (vlib_cli_init);
1887 * fd.io coding-style-patch-verification: ON
1890 * eval: (c-set-style "gnu")