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/cpu.h>
43 #include <vppinfra/elog.h>
47 /** \file src/vlib/cli.c Debug CLI Implementation
50 int vl_api_set_elog_trace_api_messages (int enable);
51 int vl_api_get_elog_trace_api_messages (void);
53 static void *current_traced_heap;
55 /* Root of all show commands. */
57 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
59 .short_help = "Show commands",
63 /* Root of all clear commands. */
65 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
67 .short_help = "Clear commands",
71 /* Root of all set commands. */
73 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
75 .short_help = "Set commands",
79 /* Root of all test commands. */
81 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
83 .short_help = "Test commands",
87 /* Returns bitmap of commands which match key. */
89 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
93 vlib_cli_parse_position_t *p;
95 unformat_skip_white_space (input);
101 k = unformat_get_input (input);
115 case UNFORMAT_END_OF_INPUT:
116 /* White space or end of input removes any non-white
117 matches that were before possible. */
118 if (i < vec_len (c->sub_command_positions)
119 && clib_bitmap_count_set_bits (match) > 1)
121 p = vec_elt_at_index (c->sub_command_positions, i);
122 for (n = 0; n < vec_len (p->bitmaps); n++)
123 match = clib_bitmap_andnot (match, p->bitmaps[n]);
128 unformat_put_input (input);
132 if (i >= vec_len (c->sub_command_positions))
135 clib_bitmap_free (match);
139 p = vec_elt_at_index (c->sub_command_positions, i);
140 if (vec_len (p->bitmaps) == 0)
144 if (n < 0 || n >= vec_len (p->bitmaps))
148 match = clib_bitmap_dup (p->bitmaps[n]);
150 match = clib_bitmap_and (match, p->bitmaps[n]);
152 if (clib_bitmap_is_zero (match))
160 /* Looks for string based sub-input formatted { SUB-INPUT }. */
162 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
164 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
170 c = unformat_get_input (i);
182 /* Put back paren. */
183 if (c != UNFORMAT_END_OF_INPUT)
184 unformat_put_input (i);
186 if (c == '{' && unformat (i, "%v", &s))
188 unformat_init_vector (sub_input, s);
197 static vlib_cli_command_t *
198 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
200 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
201 return vec_elt_at_index (cm->commands, s->index);
205 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
207 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
208 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
209 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
210 vlib_cli_main_t *cm = &vm->cli_main;
211 uword *match_bitmap, is_unique, index;
213 match_bitmap = vlib_cli_sub_command_match (c, i);
214 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
218 index = clib_bitmap_first_set (match_bitmap);
219 *result = get_sub_command (cm, c, index);
221 clib_bitmap_free (match_bitmap);
227 vlib_cli_cmp_strings (void *a1, void *a2)
229 u8 *c1 = *(u8 **) a1;
230 u8 *c2 = *(u8 **) a2;
232 return vec_cmp (c1, c2);
236 vlib_cli_get_possible_completions (u8 * str)
238 vlib_cli_command_t *c;
239 vlib_cli_sub_command_t *sc;
240 vlib_main_t *vm = vlib_get_main ();
241 vlib_cli_main_t *vcm = &vm->cli_main;
242 uword *match_bitmap = 0;
243 uword index, is_unique, help_next_level;
245 unformat_input_t input;
246 unformat_init_vector (&input, vec_dup (str));
247 c = vec_elt_at_index (vcm->commands, 0);
249 /* remove trailing whitespace, except for one of them */
250 while (vec_len (input.buffer) >= 2 &&
251 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
252 isspace (input.buffer[vec_len (input.buffer) - 2]))
254 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
257 /* if input is empty, directly return list of root commands */
258 if (vec_len (input.buffer) == 0 ||
259 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
261 vec_foreach (sc, c->sub_commands)
263 vec_add1 (result, (u8 *) sc->name);
268 /* add a trailing '?' so that vlib_cli_sub_command_match can find
269 * all commands starting with the input string */
270 vec_add1 (input.buffer, '?');
274 match_bitmap = vlib_cli_sub_command_match (c, &input);
275 /* no match: return no result */
276 if (match_bitmap == 0)
280 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
281 /* unique match: try to step one subcommand level further */
284 /* stop if no more input */
285 if (input.index >= vec_len (input.buffer) - 1)
290 index = clib_bitmap_first_set (match_bitmap);
291 c = get_sub_command (vcm, c, index);
292 clib_bitmap_free (match_bitmap);
295 /* multiple matches: stop here, return all matches */
299 /* remove trailing '?' */
300 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
302 /* if we have a space at the end of input, and a unique match,
303 * autocomplete the next level of subcommands */
304 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
306 clib_bitmap_foreach(index, match_bitmap, {
307 if (help_next_level && is_unique) {
308 c = get_sub_command (vcm, c, index);
309 vec_foreach (sc, c->sub_commands) {
310 vec_add1 (result, (u8*) sc->name);
312 goto done; /* break doesn't work in this macro-loop */
314 sc = &c->sub_commands[index];
315 vec_add1(result, (u8*) sc->name);
320 clib_bitmap_free (match_bitmap);
321 unformat_free (&input);
324 vec_sort_with_function (result, vlib_cli_cmp_strings);
329 format_vlib_cli_command_help (u8 * s, va_list * args)
331 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
332 int is_long = va_arg (*args, int);
333 if (is_long && c->long_help)
334 s = format (s, "%s", c->long_help);
335 else if (c->short_help)
336 s = format (s, "%s", c->short_help);
338 s = format (s, "%v commands", c->path);
343 format_vlib_cli_path (u8 * s, va_list * args)
345 u8 *path = va_arg (*args, u8 *);
347 s = format (s, "%v", path);
352 static vlib_cli_command_t *
353 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
355 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
356 vlib_cli_sub_command_t *sc;
359 vec_add1 (subs, c[0]);
361 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
367 vlib_cli_cmp_rule (void *a1, void *a2)
369 vlib_cli_sub_rule_t *r1 = a1;
370 vlib_cli_sub_rule_t *r2 = a2;
372 return vec_cmp (r1->name, r2->name);
376 vlib_cli_cmp_command (void *a1, void *a2)
378 vlib_cli_command_t *c1 = a1;
379 vlib_cli_command_t *c2 = a2;
381 return vec_cmp (c1->path, c2->path);
384 static clib_error_t *
385 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
386 vlib_cli_main_t * cm,
387 unformat_input_t * input,
388 uword parent_command_index)
390 vlib_cli_command_t *parent, *c;
391 clib_error_t *error = 0;
392 unformat_input_t sub_input;
394 uword is_main_dispatch = cm == &vm->cli_main;
398 parent = vec_elt_at_index (cm->commands, parent_command_index);
399 if (is_main_dispatch && unformat (input, "help"))
401 uword help_at_end_of_line, i;
403 help_at_end_of_line =
404 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
409 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
412 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
419 /* help SUB-COMMAND => long format help.
420 "help" at end of line: show all commands. */
421 if (!help_at_end_of_line)
422 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
425 else if (vec_len (c->sub_commands) == 0)
426 vlib_cli_output (vm, "%v: no sub-commands", c->path);
430 vlib_cli_sub_rule_t *sr, *subs = 0;
433 hash_foreach_mem (key, value, c->sub_command_index_by_name,
436 vec_add2 (subs, sr, 1);
437 sr->name = c->sub_commands[value].name;
438 sr->command_index = value;
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, "uncomment %U",
479 unformat_vlib_cli_sub_input, &sub_input))
482 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
483 parent_command_index);
484 unformat_free (&sub_input);
486 else if (unformat (input, "leak-check %U",
487 unformat_vlib_cli_sub_input, &sub_input))
490 if (current_traced_heap)
493 oldheap = clib_mem_set_heap (current_traced_heap);
495 clib_mem_set_heap (oldheap);
496 current_traced_heap = 0;
500 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
501 parent_command_index);
502 unformat_free (&sub_input);
504 /* Otherwise, the clib_error_t shows up as a leak... */
507 vlib_cli_output (vm, "%v", error->what);
508 clib_error_free (error);
512 (void) clib_mem_trace_enable_disable (0);
513 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
514 1 /* verbose, i.e. print leaks */ );
516 vlib_cli_output (vm, "%v", leak_report);
517 vec_free (leak_report);
521 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
523 unformat_input_t *si;
524 uword has_sub_commands =
525 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
528 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
531 if (has_sub_commands)
532 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
534 if (has_sub_commands && !error)
535 /* Found valid sub-command. */ ;
537 else if (c->function)
539 clib_error_t *c_error;
541 /* Skip white space for benefit of called function. */
542 unformat_skip_white_space (si);
544 if (unformat (si, "?"))
546 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
551 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
554 ELOG_TYPE_DECLARE (e) =
556 .format = "cli-cmd: %s",
564 ed = ELOG_DATA (&vm->elog_main, e);
565 ed->c = elog_string (&vm->elog_main, c->path);
569 vlib_worker_thread_barrier_sync (vm);
572 c_error = c->function (vm, si, c);
575 vlib_worker_thread_barrier_release (vm);
577 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
580 ELOG_TYPE_DECLARE (e) =
582 .format = "cli-cmd: %s %s",
583 .format_args = "T4T4",
590 ed = ELOG_DATA (&vm->elog_main, e);
591 ed->c = elog_string (&vm->elog_main, c->path);
594 vec_add1 (c_error->what, 0);
596 elog_string (&vm->elog_main, (char *) c_error->what);
597 _vec_len (c_error->what) -= 1;
600 ed->err = elog_string (&vm->elog_main, "OK");
606 clib_error_return (0, "%v: %v", c->path, c_error->what);
607 clib_error_free (c_error);
608 /* Free sub input. */
616 /* Free any previous error. */
617 clib_error_free (error);
621 error = clib_error_return (0, "%v: no sub-commands", c->path);
623 /* Free sub input. */
635 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
636 format_unformat_error, input);
638 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
643 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
644 __attribute__ ((weak));
647 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
651 /* Process CLI input. */
653 vlib_cli_input (vlib_main_t * vm,
654 unformat_input_t * input,
655 vlib_cli_output_function_t * function, uword function_arg)
657 vlib_process_t *cp = vlib_get_current_process (vm);
659 vlib_cli_output_function_t *save_function;
660 uword save_function_arg;
663 save_function = cp->output_function;
664 save_function_arg = cp->output_function_arg;
666 cp->output_function = function;
667 cp->output_function_arg = function_arg;
671 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
674 while (!error && !unformat (input, "%U", unformat_eof));
678 vlib_cli_output (vm, "%v", error->what);
679 vlib_unix_error_report (vm, error);
680 /* clib_error_return is unfortunately often called with a '0'
682 rv = error->code != 0 ? error->code : -1;
683 clib_error_free (error);
686 cp->output_function = save_function;
687 cp->output_function_arg = save_function_arg;
691 /* Output to current CLI connection. */
693 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
695 vlib_process_t *cp = vlib_get_current_process (vm);
700 s = va_format (0, fmt, &va);
703 /* Terminate with \n if not present. */
704 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
707 if ((!cp) || (!cp->output_function))
708 fformat (stdout, "%v", s);
710 cp->output_function (cp->output_function_arg, s, vec_len (s));
715 void *vl_msg_push_heap (void) __attribute__ ((weak));
717 vl_msg_push_heap (void)
722 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
724 vl_msg_pop_heap (void *oldheap)
728 void *vlib_stats_push_heap (void *) __attribute__ ((weak));
730 vlib_stats_push_heap (void *notused)
735 static clib_error_t *
736 show_memory_usage (vlib_main_t * vm,
737 unformat_input_t * input, vlib_cli_command_t * cmd)
739 int verbose __attribute__ ((unused)) = 0;
740 int api_segment = 0, stats_segment = 0, main_heap = 0;
743 uword clib_mem_trace_enable_disable (uword enable);
747 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
749 if (unformat (input, "verbose"))
751 else if (unformat (input, "api-segment"))
753 else if (unformat (input, "stats-segment"))
755 else if (unformat (input, "main-heap"))
759 error = clib_error_return (0, "unknown input `%U'",
760 format_unformat_error, input);
765 if ((api_segment + stats_segment + main_heap) == 0)
766 return clib_error_return
767 (0, "Please supply one of api-segment, stats-segment or main-heap");
771 void *oldheap = vl_msg_push_heap ();
772 was_enabled = clib_mem_trace_enable_disable (0);
774 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
775 vl_msg_pop_heap (oldheap);
776 u8 *s = vec_dup (s_in_svm);
778 oldheap = vl_msg_push_heap ();
780 clib_mem_trace_enable_disable (was_enabled);
781 vl_msg_pop_heap (oldheap);
782 vlib_cli_output (vm, "API segment");
783 vlib_cli_output (vm, "%v", s);
788 void *oldheap = vlib_stats_push_heap (0);
789 was_enabled = clib_mem_trace_enable_disable (0);
791 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
793 clib_mem_set_heap (oldheap);
794 u8 *s = vec_dup (s_in_svm);
796 oldheap = vlib_stats_push_heap (0);
800 clib_mem_trace_enable_disable (was_enabled);
801 clib_mem_set_heap (oldheap);
803 vlib_cli_output (vm, "Stats segment");
804 vlib_cli_output (vm, "%v", s);
808 #if USE_DLMALLOC == 0
812 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
813 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
814 vlib_worker_threads[index].name);
815 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
816 h->vm_alloc_offset_from_header,
818 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
828 * Note: the foreach_vlib_main causes allocator traffic,
829 * so shut off tracing before we go there...
831 was_enabled = clib_mem_trace_enable_disable (0);
836 struct dlmallinfo mi;
838 mspace = clib_per_cpu_mheaps[index];
840 mi = mspace_mallinfo (mspace);
841 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
842 vlib_worker_threads[index].name);
843 vlib_cli_output (vm, " %U\n", format_page_map,
844 pointer_to_uword (mspace_least_addr(mspace)),
846 vlib_cli_output (vm, " %U\n", format_mheap,
847 clib_per_cpu_mheaps[index],
853 /* Restore the trace flag */
854 clib_mem_trace_enable_disable (was_enabled);
857 #endif /* USE_DLMALLOC */
862 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
863 .path = "show memory",
864 .short_help = "show memory [api-segment][stats-segment][verbose]",
865 .function = show_memory_usage,
869 static clib_error_t *
870 show_cpu (vlib_main_t * vm, unformat_input_t * input,
871 vlib_cli_command_t * cmd)
873 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
874 _("Model name", "%U", format_cpu_model_name);
875 _("Microarch model (family)", "%U", format_cpu_uarch);
876 _("Flags", "%U", format_cpu_flags);
877 _("Base frequency", "%.2f GHz",
878 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
884 * Displays various information about the CPU.
887 * @cliexstart{show cpu}
888 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
889 * Microarchitecture: Broadwell (Broadwell-EP/EX)
890 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
891 * Base Frequency: 3.20 GHz
895 VLIB_CLI_COMMAND (show_cpu_command, static) = {
897 .short_help = "Show cpu information",
898 .function = show_cpu,
902 static clib_error_t *
903 enable_disable_memory_trace (vlib_main_t * vm,
904 unformat_input_t * input,
905 vlib_cli_command_t * cmd)
907 unformat_input_t _line_input, *line_input = &_line_input;
910 int stats_segment = 0;
914 if (!unformat_user (input, unformat_line_input, line_input))
917 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
919 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
921 else if (unformat (line_input, "api-segment"))
923 else if (unformat (line_input, "stats-segment"))
925 else if (unformat (line_input, "main-heap"))
929 unformat_free (line_input);
930 return clib_error_return (0, "invalid input");
933 unformat_free (line_input);
935 if ((api_segment + stats_segment + main_heap + (enable == 0)) == 0)
937 return clib_error_return
938 (0, "Need one of main-heap, stats-segment or api-segment");
941 /* Turn off current trace, if any */
942 if (current_traced_heap)
945 oldheap = clib_mem_set_heap (current_traced_heap);
947 clib_mem_set_heap (oldheap);
948 current_traced_heap = 0;
957 oldheap = vl_msg_push_heap ();
958 current_traced_heap = clib_mem_get_heap ();
960 vl_msg_pop_heap (oldheap);
967 oldheap = vlib_stats_push_heap (0);
968 current_traced_heap = clib_mem_get_heap ();
969 clib_mem_trace (stats_segment);
970 /* We don't want to call vlib_stats_pop_heap... */
972 clib_mem_set_heap (oldheap);
978 current_traced_heap = clib_mem_get_heap ();
979 clib_mem_trace (main_heap);
986 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
987 .path = "memory-trace",
988 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n",
989 .function = enable_disable_memory_trace,
994 static clib_error_t *
995 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
996 vlib_cli_command_t * cmd)
998 #if USE_DLMALLOC == 0
999 clib_error_t *error = 0;
1003 if (unformat (input, "on"))
1007 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1008 mheap = mheap_header(heap);
1009 mheap->flags |= MHEAP_FLAG_VALIDATE;
1010 // Turn off small object cache because it delays detection of errors
1011 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
1016 else if (unformat (input, "off"))
1020 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1021 mheap = mheap_header(heap);
1022 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
1023 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
1027 else if (unformat (input, "now"))
1031 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1032 mheap = mheap_header(heap);
1033 mheap_validate(heap);
1036 vlib_cli_output (vm, "heap validation complete");
1041 return clib_error_return (0, "unknown input `%U'",
1042 format_unformat_error, input);
1047 return clib_error_return (0, "unimplemented...");
1048 #endif /* USE_DLMALLOC */
1052 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1053 .path = "test heap-validate",
1054 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1055 .function = test_heap_validate,
1059 static clib_error_t *
1060 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1061 vlib_cli_command_t * cmd)
1063 clib_file_main_t *fm = &file_main;
1066 /* environ(7) does not indicate a header for this */
1067 extern char **environ;
1069 /* Close all known open files */
1071 pool_foreach(f, fm->file_pool,
1073 if (f->file_descriptor > 2)
1074 close(f->file_descriptor);
1079 execve (vm->name, (char **) vm->argv, environ);
1085 VLIB_CLI_COMMAND (restart_cmd,static) = {
1087 .short_help = "restart process",
1088 .function = restart_cmd_fn,
1094 * A trivial test harness to verify the per-process output_function
1095 * is working correcty.
1098 static clib_error_t *
1099 sleep_ten_seconds (vlib_main_t * vm,
1100 unformat_input_t * input, vlib_cli_command_t * cmd)
1103 u16 my_id = rand ();
1105 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1107 for (i = 0; i < 10; i++)
1109 vlib_process_wait_for_event_or_clock (vm, 1.0);
1110 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1112 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1117 VLIB_CLI_COMMAND (ping_command, static) = {
1118 .path = "test sleep",
1119 .function = sleep_ten_seconds,
1120 .short_help = "Sleep for 10 seconds",
1123 #endif /* ifdef TEST_CODE */
1126 vlib_cli_normalize_path (char *input, char **result)
1131 uword index_of_last_space = ~0;
1136 /* Multiple white space -> single space. */
1143 if (l > 0 && s[l - 1] != ' ')
1151 if (l > 0 && s[l - 1] == ' ')
1152 index_of_last_space = vec_len (s);
1159 /* Remove any extra space at end. */
1160 if (l > 0 && s[l - 1] == ' ')
1164 return index_of_last_space;
1168 parent_path_len (char *path)
1171 for (i = vec_len (path) - 1; i >= 0; i--)
1180 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1182 vlib_cli_command_t *p, *c;
1183 vlib_cli_sub_command_t *sub_c;
1187 p = vec_elt_at_index (cm->commands, parent_index);
1188 c = vec_elt_at_index (cm->commands, child_index);
1190 l = parent_path_len (c->path);
1192 sub_name = vec_dup ((u8 *) c->path);
1195 ASSERT (l + 1 < vec_len (c->path));
1197 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1200 if (sub_name[0] == '%')
1203 vlib_cli_sub_rule_t *sr;
1206 vec_delete (sub_name, 1, 0);
1208 if (!p->sub_rule_index_by_name)
1209 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1210 sizeof (sub_name[0]),
1212 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1215 sr = vec_elt_at_index (p->sub_rules, q[0]);
1216 ASSERT (sr->command_index == child_index);
1220 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1221 vec_len (p->sub_rules));
1222 vec_add2 (p->sub_rules, sr, 1);
1223 sr->name = sub_name;
1224 sr->rule_index = q[0];
1225 sr->command_index = child_index;
1229 if (!p->sub_command_index_by_name)
1230 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1231 sizeof (c->path[0]),
1234 /* Check if sub-command has already been created. */
1235 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1237 vec_free (sub_name);
1241 vec_add2 (p->sub_commands, sub_c, 1);
1242 sub_c->index = child_index;
1243 sub_c->name = sub_name;
1244 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1245 sub_c - p->sub_commands);
1247 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1248 for (i = 0; i < vec_len (sub_c->name); i++)
1251 vlib_cli_parse_position_t *pos;
1253 pos = vec_elt_at_index (p->sub_command_positions, i);
1256 pos->min_char = sub_c->name[i];
1258 n = sub_c->name[i] - pos->min_char;
1261 pos->min_char = sub_c->name[i];
1262 vec_insert (pos->bitmaps, -n, 0);
1266 vec_validate (pos->bitmaps, n);
1268 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1273 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1275 uword p_len, pi, *p;
1277 vlib_cli_command_t *c, *parent;
1279 /* Root command (index 0) should have already been added. */
1280 ASSERT (vec_len (cm->commands) > 0);
1282 c = vec_elt_at_index (cm->commands, ci);
1283 p_len = parent_path_len (c->path);
1285 /* No space? Parent is root command. */
1288 add_sub_command (cm, 0, ci);
1293 vec_add (p_path, c->path, p_len);
1295 p = hash_get_mem (cm->command_index_by_path, p_path);
1297 /* Parent exists? */
1300 /* Parent does not exist; create it. */
1301 vec_add2 (cm->commands, parent, 1);
1302 parent->path = p_path;
1303 hash_set_mem (cm->command_index_by_path, parent->path,
1304 parent - cm->commands);
1305 pi = parent - cm->commands;
1313 add_sub_command (cm, pi, ci);
1315 /* Create parent's parent. */
1317 vlib_cli_make_parent (cm, pi);
1321 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1323 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1327 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1329 vlib_cli_main_t *cm = &vm->cli_main;
1330 clib_error_t *error = 0;
1332 char *normalized_path;
1334 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1337 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1339 if (!cm->command_index_by_path)
1340 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1341 sizeof (c->path[0]),
1344 /* See if command already exists with given path. */
1345 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1348 vlib_cli_command_t *d;
1351 d = vec_elt_at_index (cm->commands, ci);
1353 /* If existing command was created via vlib_cli_make_parent
1354 replaced it with callers data. */
1355 if (vlib_cli_command_is_empty (d))
1357 vlib_cli_command_t save = d[0];
1359 ASSERT (!vlib_cli_command_is_empty (c));
1361 /* Copy callers fields. */
1364 /* Save internal fields. */
1365 d->path = save.path;
1366 d->sub_commands = save.sub_commands;
1367 d->sub_command_index_by_name = save.sub_command_index_by_name;
1368 d->sub_command_positions = save.sub_command_positions;
1369 d->sub_rules = save.sub_rules;
1373 clib_error_return (0, "duplicate command name with path %v",
1376 vec_free (normalized_path);
1382 /* Command does not exist: create it. */
1384 /* Add root command (index 0). */
1385 if (vec_len (cm->commands) == 0)
1387 /* Create command with index 0; path is empty string. */
1388 vec_resize (cm->commands, 1);
1391 ci = vec_len (cm->commands);
1392 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1393 vec_add1 (cm->commands, c[0]);
1395 c = vec_elt_at_index (cm->commands, ci);
1396 c->path = normalized_path;
1398 /* Don't inherit from registration. */
1399 c->sub_commands = 0;
1400 c->sub_command_index_by_name = 0;
1401 c->sub_command_positions = 0;
1404 vlib_cli_make_parent (cm, ci);
1409 /* $$$ turn back on again someday, maybe */
1411 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1413 vlib_cli_main_t *cm = &vm->cli_main;
1414 vlib_cli_parse_rule_t *r;
1415 clib_error_t *error = 0;
1419 if (!cm->parse_rule_index_by_name)
1420 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1421 sizeof (r->name[0]),
1424 /* Make vector copy of name. */
1425 r_name = format (0, "%s", r_reg->name);
1427 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1430 return clib_error_return (0, "duplicate parse rule name `%s'",
1434 vec_add2 (cm->parse_rules, r, 1);
1436 r->name = (char *) r_name;
1437 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1442 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1443 vlib_cli_parse_rule_t *
1445 vlib_cli_parse_rule_t *
1447 __attribute__ ((unused))
1449 clib_error_t *error = 0;
1450 vlib_cli_parse_rule_t *r;
1452 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1454 if (!r->name || strlen (r->name) == 0)
1456 error = clib_error_return (0, "parse rule with no name");
1460 error = vlib_cli_register_parse_rule (vm, r);
1470 static clib_error_t *
1471 elog_trace_command_fn (vlib_main_t * vm,
1472 unformat_input_t * input, vlib_cli_command_t * cmd)
1474 unformat_input_t _line_input, *line_input = &_line_input;
1476 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1477 u32 circuit_node_index;
1479 if (!unformat_user (input, unformat_line_input, line_input))
1482 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1484 if (unformat (line_input, "api"))
1486 else if (unformat (line_input, "dispatch"))
1488 else if (unformat (line_input, "circuit-node %U",
1489 unformat_vlib_node, vm, &circuit_node_index))
1491 else if (unformat (line_input, "cli"))
1493 else if (unformat (line_input, "barrier"))
1495 else if (unformat (line_input, "disable"))
1497 else if (unformat (line_input, "enable"))
1502 unformat_free (line_input);
1504 vl_api_set_elog_trace_api_messages
1505 (api ? enable : vl_api_get_elog_trace_api_messages ());
1506 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1507 vm->elog_trace_graph_dispatch = dispatch ?
1508 enable : vm->elog_trace_graph_dispatch;
1509 vm->elog_trace_graph_circuit = circuit ?
1510 enable : vm->elog_trace_graph_circuit;
1511 vlib_worker_threads->barrier_elog_enabled =
1512 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1513 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1516 * Set up start-of-buffer logic-analyzer trigger
1517 * for main loop event logs, which are fairly heavyweight.
1518 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1519 * will fully disable the scheme when the elog buffer fills.
1521 if (dispatch || circuit)
1523 elog_main_t *em = &vm->elog_main;
1525 em->n_total_events_disable_limit =
1526 em->n_total_events + vec_len (em->event_ring);
1531 vlib_cli_output (vm, "Current status:");
1534 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1535 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1536 vm->elog_trace_cli_commands ? "on" : "off");
1538 (vm, " Barrier sync trace: %s",
1539 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1541 (vm, " Graph Dispatch: %s",
1542 vm->elog_trace_graph_dispatch ? "on" : "off");
1544 (vm, " Graph Circuit: %s",
1545 vm->elog_trace_graph_circuit ? "on" : "off");
1546 if (vm->elog_trace_graph_circuit)
1549 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1555 * Control event logging of api, cli, and thread barrier events
1556 * With no arguments, displays the current trace status.
1557 * Name the event groups you wish to trace or stop tracing.
1561 * elog trace api cli barrier
1562 * elog trace api cli barrier disable
1563 * elog trace dispatch
1564 * elog trace circuit-node ethernet-input
1567 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1570 VLIB_CLI_COMMAND (elog_trace_command, static) =
1572 .path = "elog trace",
1573 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1574 "[circuit-node <name> e.g. ethernet-input][disable]",
1575 .function = elog_trace_command_fn,
1579 static clib_error_t *
1580 suspend_command_fn (vlib_main_t * vm,
1581 unformat_input_t * input, vlib_cli_command_t * cmd)
1583 vlib_process_suspend (vm, 30e-3);
1588 VLIB_CLI_COMMAND (suspend_command, static) =
1591 .short_help = "suspend debug CLI for 30ms",
1592 .function = suspend_command_fn,
1599 sort_cmds_by_path (void *a1, void *a2)
1603 vlib_main_t *vm = vlib_get_main ();
1604 vlib_cli_main_t *cm = &vm->cli_main;
1605 vlib_cli_command_t *c1, *c2;
1608 c1 = vec_elt_at_index (cm->commands, *index1);
1609 c2 = vec_elt_at_index (cm->commands, *index2);
1611 lmin = vec_len (c1->path);
1612 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1614 for (i = 0; i < lmin; i++)
1616 if (c1->path[i] < c2->path[i])
1618 else if (c1->path[i] > c2->path[i])
1627 vlib_cli_main_t *cm;
1628 u32 parent_command_index;
1630 int show_not_mp_safe;
1633 } vlib_cli_walk_args_t;
1636 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1638 vlib_cli_command_t *parent;
1639 vlib_cli_sub_command_t *sub;
1640 vlib_cli_walk_args_t _a, *a = &_a;
1641 vlib_cli_main_t *cm;
1644 /* Copy args into this stack frame */
1648 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1650 if (parent->function)
1652 if (((a->show_mp_safe && parent->is_mp_safe)
1653 || (a->show_not_mp_safe && !parent->is_mp_safe))
1654 && (a->show_hit == 0 || parent->hit_counter))
1656 vec_add1 (cm->sort_vector, a->parent_command_index);
1660 parent->hit_counter = 0;
1663 for (i = 0; i < vec_len (parent->sub_commands); i++)
1665 sub = vec_elt_at_index (parent->sub_commands, i);
1666 a->parent_command_index = sub->index;
1667 cli_recursive_walk (a);
1672 format_mp_safe (u8 * s, va_list * args)
1674 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1675 int show_mp_safe = va_arg (*args, int);
1676 int show_not_mp_safe = va_arg (*args, int);
1677 int show_hit = va_arg (*args, int);
1678 int clear_hit = va_arg (*args, int);
1679 vlib_cli_command_t *c;
1680 vlib_cli_walk_args_t _a, *a = &_a;
1682 char *format_string = "\n%v";
1685 format_string = "\n%v: %u";
1687 vec_reset_length (cm->sort_vector);
1690 a->parent_command_index = 0;
1691 a->show_mp_safe = show_mp_safe;
1692 a->show_not_mp_safe = show_not_mp_safe;
1693 a->show_hit = show_hit;
1694 a->clear_hit = clear_hit;
1696 cli_recursive_walk (a);
1698 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1700 for (i = 0; i < vec_len (cm->sort_vector); i++)
1702 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1703 s = format (s, format_string, c->path, c->hit_counter);
1710 static clib_error_t *
1711 show_cli_command_fn (vlib_main_t * vm,
1712 unformat_input_t * input, vlib_cli_command_t * cmd)
1714 int show_mp_safe = 0;
1715 int show_not_mp_safe = 0;
1719 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1721 if (unformat (input, "mp-safe"))
1723 if (unformat (input, "not-mp-safe"))
1724 show_not_mp_safe = 1;
1725 else if (unformat (input, "hit"))
1727 else if (unformat (input, "clear-hit"))
1733 /* default set: all cli commands */
1734 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1735 show_mp_safe = show_not_mp_safe = 1;
1737 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1738 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1740 vlib_cli_output (vm, "hit counters cleared...");
1746 * Displays debug cli command information
1749 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1751 * "show cli" displays the entire debug cli:
1755 * adjacency counters
1761 * "show cli mp-safe" displays mp-safe debug CLI commands:
1775 * "show cli not-mp-safe" displays debug CLI commands
1776 * which cause worker thread barrier synchronization
1778 * "show cli hit" displays commands which have been executed. Qualify
1779 * as desired with "mp-safe" or "not-mp-safe".
1781 * "show cli clear-hit" clears the per-command hit counters.
1786 VLIB_CLI_COMMAND (show_cli_command, static) =
1789 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1790 .function = show_cli_command_fn,
1795 static clib_error_t *
1796 vlib_cli_init (vlib_main_t * vm)
1798 vlib_cli_main_t *cm = &vm->cli_main;
1799 clib_error_t *error = 0;
1800 vlib_cli_command_t *cmd;
1802 cmd = cm->cli_command_registrations;
1806 error = vlib_cli_register (vm, cmd);
1809 cmd = cmd->next_cli_command;
1814 VLIB_INIT_FUNCTION (vlib_cli_init);
1817 * fd.io coding-style-patch-verification: ON
1820 * eval: (c-set-style "gnu")