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;
396 parent = vec_elt_at_index (cm->commands, parent_command_index);
397 if (is_main_dispatch && unformat (input, "help"))
399 uword help_at_end_of_line, i;
401 help_at_end_of_line =
402 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
407 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
410 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
417 /* help SUB-COMMAND => long format help.
418 "help" at end of line: show all commands. */
419 if (!help_at_end_of_line)
420 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
423 else if (vec_len (c->sub_commands) == 0)
424 vlib_cli_output (vm, "%v: no sub-commands", c->path);
428 vlib_cli_sub_rule_t *sr, *subs = 0;
429 vlib_cli_sub_command_t *sc;
431 vec_foreach (sc, c->sub_commands)
433 vec_add2 (subs, sr, 1);
435 sr->command_index = sc->index;
439 vec_sort_with_function (subs, vlib_cli_cmp_rule);
441 for (i = 0; i < vec_len (subs); i++)
443 vlib_cli_command_t *d;
445 d = vec_elt_at_index (cm->commands, subs[i].command_index);
447 (vm, " %-30v %U", subs[i].name,
448 format_vlib_cli_command_help, d, /* is_long */ 0);
455 else if (is_main_dispatch
456 && (unformat (input, "choices") || unformat (input, "?")))
458 vlib_cli_command_t *sub, *subs;
460 subs = all_subs (cm, 0, parent_command_index);
461 vec_sort_with_function (subs, vlib_cli_cmp_command);
462 vec_foreach (sub, subs)
463 vlib_cli_output (vm, " %-40U %U",
464 format_vlib_cli_path, sub->path,
465 format_vlib_cli_command_help, sub, /* is_long */ 0);
469 else if (unformat (input, "comment %v", &string))
474 else if (unformat (input, "uncomment %U",
475 unformat_vlib_cli_sub_input, &sub_input))
478 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
479 parent_command_index);
480 unformat_free (&sub_input);
482 else if (unformat (input, "leak-check %U",
483 unformat_vlib_cli_sub_input, &sub_input))
486 if (current_traced_heap)
489 oldheap = clib_mem_set_heap (current_traced_heap);
491 clib_mem_set_heap (oldheap);
492 current_traced_heap = 0;
496 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
497 parent_command_index);
498 unformat_free (&sub_input);
500 /* Otherwise, the clib_error_t shows up as a leak... */
503 vlib_cli_output (vm, "%v", error->what);
504 clib_error_free (error);
508 (void) clib_mem_trace_enable_disable (0);
509 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
510 1 /* verbose, i.e. print leaks */ );
512 vlib_cli_output (vm, "%v", leak_report);
513 vec_free (leak_report);
517 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
519 unformat_input_t *si;
520 uword has_sub_commands =
521 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
524 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
527 if (has_sub_commands)
528 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
530 if (has_sub_commands && !error)
531 /* Found valid sub-command. */ ;
533 else if (c->function)
535 clib_error_t *c_error;
537 /* Skip white space for benefit of called function. */
538 unformat_skip_white_space (si);
540 if (unformat (si, "?"))
542 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
547 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
550 ELOG_TYPE_DECLARE (e) =
552 .format = "cli-cmd: %s",
560 ed = ELOG_DATA (&vm->elog_main, e);
561 ed->c = elog_string (&vm->elog_main, "%v", c->path);
565 vlib_worker_thread_barrier_sync (vm);
568 c_error = c->function (vm, si, c);
571 vlib_worker_thread_barrier_release (vm);
573 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
576 ELOG_TYPE_DECLARE (e) =
578 .format = "cli-cmd: %s %s",
579 .format_args = "T4T4",
586 ed = ELOG_DATA (&vm->elog_main, e);
587 ed->c = elog_string (&vm->elog_main, "%v", c->path);
590 vec_add1 (c_error->what, 0);
592 elog_string (&vm->elog_main, (char *) c_error->what);
593 _vec_len (c_error->what) -= 1;
596 ed->err = elog_string (&vm->elog_main, "OK");
602 clib_error_return (0, "%v: %v", c->path, c_error->what);
603 clib_error_free (c_error);
604 /* Free sub input. */
612 /* Free any previous error. */
613 clib_error_free (error);
617 error = clib_error_return (0, "%v: no sub-commands", c->path);
619 /* Free sub input. */
631 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
632 format_unformat_error, input);
634 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
639 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
640 __attribute__ ((weak));
643 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
647 /* Process CLI input. */
649 vlib_cli_input (vlib_main_t * vm,
650 unformat_input_t * input,
651 vlib_cli_output_function_t * function, uword function_arg)
653 vlib_process_t *cp = vlib_get_current_process (vm);
655 vlib_cli_output_function_t *save_function;
656 uword save_function_arg;
659 save_function = cp->output_function;
660 save_function_arg = cp->output_function_arg;
662 cp->output_function = function;
663 cp->output_function_arg = function_arg;
667 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
670 while (!error && !unformat (input, "%U", unformat_eof));
674 vlib_cli_output (vm, "%v", error->what);
675 vlib_unix_error_report (vm, error);
676 /* clib_error_return is unfortunately often called with a '0'
678 rv = error->code != 0 ? error->code : -1;
679 clib_error_free (error);
682 cp->output_function = save_function;
683 cp->output_function_arg = save_function_arg;
687 /* Output to current CLI connection. */
689 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
691 vlib_process_t *cp = vlib_get_current_process (vm);
696 s = va_format (0, fmt, &va);
699 /* Terminate with \n if not present. */
700 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
703 if ((!cp) || (!cp->output_function))
704 fformat (stdout, "%v", s);
706 cp->output_function (cp->output_function_arg, s, vec_len (s));
711 void *vl_msg_push_heap (void) __attribute__ ((weak));
713 vl_msg_push_heap (void)
718 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
720 vl_msg_pop_heap (void *oldheap)
724 void *vlib_stats_push_heap (void *) __attribute__ ((weak));
726 vlib_stats_push_heap (void *notused)
731 static clib_error_t *
732 show_memory_usage (vlib_main_t * vm,
733 unformat_input_t * input, vlib_cli_command_t * cmd)
735 int verbose __attribute__ ((unused)) = 0;
736 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
740 uword clib_mem_trace_enable_disable (uword enable);
744 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
746 if (unformat (input, "verbose"))
748 else if (unformat (input, "api-segment"))
750 else if (unformat (input, "stats-segment"))
752 else if (unformat (input, "main-heap"))
754 else if (unformat (input, "numa-heaps"))
758 error = clib_error_return (0, "unknown input `%U'",
759 format_unformat_error, input);
764 if ((api_segment + stats_segment + main_heap + numa_heaps) == 0)
765 return clib_error_return
766 (0, "Need one of api-segment, stats-segment, main-heap or numa-heaps");
770 void *oldheap = vl_msg_push_heap ();
771 was_enabled = clib_mem_trace_enable_disable (0);
773 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
774 vl_msg_pop_heap (oldheap);
775 u8 *s = vec_dup (s_in_svm);
777 oldheap = vl_msg_push_heap ();
779 clib_mem_trace_enable_disable (was_enabled);
780 vl_msg_pop_heap (oldheap);
781 vlib_cli_output (vm, "API segment");
782 vlib_cli_output (vm, "%v", s);
787 void *oldheap = vlib_stats_push_heap (0);
788 was_enabled = clib_mem_trace_enable_disable (0);
790 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
792 clib_mem_set_heap (oldheap);
793 u8 *s = vec_dup (s_in_svm);
795 oldheap = vlib_stats_push_heap (0);
799 clib_mem_trace_enable_disable (was_enabled);
800 clib_mem_set_heap (oldheap);
802 vlib_cli_output (vm, "Stats segment");
803 vlib_cli_output (vm, "%v", s);
812 * Note: the foreach_vlib_main causes allocator traffic,
813 * so shut off tracing before we go there...
815 was_enabled = clib_mem_trace_enable_disable (0);
820 struct dlmallinfo mi;
822 mspace = clib_per_cpu_mheaps[index];
824 mi = mspace_mallinfo (mspace);
825 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
826 vlib_worker_threads[index].name);
827 vlib_cli_output (vm, " %U\n", format_page_map,
828 pointer_to_uword (mspace_least_addr(mspace)),
830 vlib_cli_output (vm, " %U\n", format_mheap,
831 clib_per_cpu_mheaps[index],
837 /* Restore the trace flag */
838 clib_mem_trace_enable_disable (was_enabled);
842 struct dlmallinfo mi;
845 for (i = 0; i < ARRAY_LEN (clib_per_numa_mheaps); i++)
847 if (clib_per_numa_mheaps[i] == 0)
849 if (clib_per_numa_mheaps[i] == clib_per_cpu_mheaps[i])
851 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
854 was_enabled = clib_mem_trace_enable_disable (0);
855 mspace = clib_per_numa_mheaps[i];
857 mi = mspace_mallinfo (mspace);
858 vlib_cli_output (vm, "Numa %d:", i);
859 vlib_cli_output (vm, " %U\n", format_page_map,
860 pointer_to_uword (mspace_least_addr (mspace)),
862 vlib_cli_output (vm, " %U\n", format_mheap,
863 clib_per_numa_mheaps[index], verbose);
871 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
872 .path = "show memory",
873 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
875 .function = show_memory_usage,
879 static clib_error_t *
880 show_cpu (vlib_main_t * vm, unformat_input_t * input,
881 vlib_cli_command_t * cmd)
883 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
884 _("Model name", "%U", format_cpu_model_name);
885 _("Microarch model (family)", "%U", format_cpu_uarch);
886 _("Flags", "%U", format_cpu_flags);
887 _("Base frequency", "%.2f GHz",
888 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
894 * Displays various information about the CPU.
897 * @cliexstart{show cpu}
898 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
899 * Microarchitecture: Broadwell (Broadwell-EP/EX)
900 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
901 * Base Frequency: 3.20 GHz
905 VLIB_CLI_COMMAND (show_cpu_command, static) = {
907 .short_help = "Show cpu information",
908 .function = show_cpu,
912 static clib_error_t *
913 enable_disable_memory_trace (vlib_main_t * vm,
914 unformat_input_t * input,
915 vlib_cli_command_t * cmd)
917 unformat_input_t _line_input, *line_input = &_line_input;
920 int stats_segment = 0;
925 if (!unformat_user (input, unformat_line_input, line_input))
928 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
930 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
932 else if (unformat (line_input, "api-segment"))
934 else if (unformat (line_input, "stats-segment"))
936 else if (unformat (line_input, "main-heap"))
938 else if (unformat (line_input, "numa-heap %d", &numa_id))
942 unformat_free (line_input);
943 return clib_error_return (0, "invalid input");
946 unformat_free (line_input);
948 if ((api_segment + stats_segment + main_heap + (enable == 0)
949 + (numa_id != ~0)) == 0)
951 return clib_error_return
952 (0, "Need one of main-heap, stats-segment, api-segment,\n"
953 "numa-heap <nn> or disable");
956 /* Turn off current trace, if any */
957 if (current_traced_heap)
960 oldheap = clib_mem_set_heap (current_traced_heap);
962 clib_mem_set_heap (oldheap);
963 current_traced_heap = 0;
972 oldheap = vl_msg_push_heap ();
973 current_traced_heap = clib_mem_get_heap ();
975 vl_msg_pop_heap (oldheap);
982 oldheap = vlib_stats_push_heap (0);
983 current_traced_heap = clib_mem_get_heap ();
984 clib_mem_trace (stats_segment);
985 /* We don't want to call vlib_stats_pop_heap... */
987 clib_mem_set_heap (oldheap);
993 current_traced_heap = clib_mem_get_heap ();
994 clib_mem_trace (main_heap);
999 if (numa_id >= ARRAY_LEN (clib_per_numa_mheaps))
1000 return clib_error_return (0, "Numa %d out of range", numa_id);
1001 if (clib_per_numa_mheaps[numa_id] == 0)
1002 return clib_error_return (0, "Numa %d heap not active", numa_id);
1004 if (clib_per_numa_mheaps[numa_id] == clib_mem_get_heap ())
1005 return clib_error_return (0, "Numa %d uses the main heap...",
1007 current_traced_heap = clib_per_numa_mheaps[numa_id];
1008 oldheap = clib_mem_set_heap (current_traced_heap);
1010 clib_mem_set_heap (oldheap);
1018 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1019 .path = "memory-trace",
1020 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1021 " [numa-heap <numa-id>]\n",
1022 .function = enable_disable_memory_trace,
1026 static clib_error_t *
1027 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1028 vlib_cli_command_t * cmd)
1030 clib_file_main_t *fm = &file_main;
1033 /* environ(7) does not indicate a header for this */
1034 extern char **environ;
1036 /* Close all known open files */
1038 pool_foreach(f, fm->file_pool,
1040 if (f->file_descriptor > 2)
1041 close(f->file_descriptor);
1046 execve (vm->name, (char **) vm->argv, environ);
1052 VLIB_CLI_COMMAND (restart_cmd,static) = {
1054 .short_help = "restart process",
1055 .function = restart_cmd_fn,
1061 * A trivial test harness to verify the per-process output_function
1062 * is working correcty.
1065 static clib_error_t *
1066 sleep_ten_seconds (vlib_main_t * vm,
1067 unformat_input_t * input, vlib_cli_command_t * cmd)
1070 u16 my_id = rand ();
1072 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1074 for (i = 0; i < 10; i++)
1076 vlib_process_wait_for_event_or_clock (vm, 1.0);
1077 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1079 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1084 VLIB_CLI_COMMAND (ping_command, static) = {
1085 .path = "test sleep",
1086 .function = sleep_ten_seconds,
1087 .short_help = "Sleep for 10 seconds",
1090 #endif /* ifdef TEST_CODE */
1093 vlib_cli_normalize_path (char *input, char **result)
1098 uword index_of_last_space = ~0;
1103 /* Multiple white space -> single space. */
1110 if (l > 0 && s[l - 1] != ' ')
1118 if (l > 0 && s[l - 1] == ' ')
1119 index_of_last_space = vec_len (s);
1126 /* Remove any extra space at end. */
1127 if (l > 0 && s[l - 1] == ' ')
1131 return index_of_last_space;
1135 parent_path_len (char *path)
1138 for (i = vec_len (path) - 1; i >= 0; i--)
1147 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1149 vlib_cli_command_t *p, *c;
1150 vlib_cli_sub_command_t *sub_c;
1154 p = vec_elt_at_index (cm->commands, parent_index);
1155 c = vec_elt_at_index (cm->commands, child_index);
1157 l = parent_path_len (c->path);
1159 sub_name = vec_dup ((u8 *) c->path);
1162 ASSERT (l + 1 < vec_len (c->path));
1164 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1167 if (sub_name[0] == '%')
1170 vlib_cli_sub_rule_t *sr;
1173 vec_delete (sub_name, 1, 0);
1175 if (!p->sub_rule_index_by_name)
1176 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1177 sizeof (sub_name[0]),
1179 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1182 sr = vec_elt_at_index (p->sub_rules, q[0]);
1183 ASSERT (sr->command_index == child_index);
1187 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1188 vec_len (p->sub_rules));
1189 vec_add2 (p->sub_rules, sr, 1);
1190 sr->name = sub_name;
1191 sr->rule_index = sr - p->sub_rules;
1192 sr->command_index = child_index;
1196 if (!p->sub_command_index_by_name)
1197 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1198 sizeof (c->path[0]),
1201 /* Check if sub-command has already been created. */
1202 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1204 vec_free (sub_name);
1208 vec_add2 (p->sub_commands, sub_c, 1);
1209 sub_c->index = child_index;
1210 sub_c->name = sub_name;
1211 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1212 sub_c - p->sub_commands);
1214 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1215 for (i = 0; i < vec_len (sub_c->name); i++)
1218 vlib_cli_parse_position_t *pos;
1220 pos = vec_elt_at_index (p->sub_command_positions, i);
1223 pos->min_char = sub_c->name[i];
1225 n = sub_c->name[i] - pos->min_char;
1228 pos->min_char = sub_c->name[i];
1229 vec_insert (pos->bitmaps, -n, 0);
1233 vec_validate (pos->bitmaps, n);
1235 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1240 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1242 uword p_len, pi, *p;
1244 vlib_cli_command_t *c, *parent;
1246 /* Root command (index 0) should have already been added. */
1247 ASSERT (vec_len (cm->commands) > 0);
1249 c = vec_elt_at_index (cm->commands, ci);
1250 p_len = parent_path_len (c->path);
1252 /* No space? Parent is root command. */
1255 add_sub_command (cm, 0, ci);
1260 vec_add (p_path, c->path, p_len);
1262 p = hash_get_mem (cm->command_index_by_path, p_path);
1264 /* Parent exists? */
1267 /* Parent does not exist; create it. */
1268 vec_add2 (cm->commands, parent, 1);
1269 parent->path = p_path;
1270 hash_set_mem (cm->command_index_by_path, parent->path,
1271 parent - cm->commands);
1272 pi = parent - cm->commands;
1280 add_sub_command (cm, pi, ci);
1282 /* Create parent's parent. */
1284 vlib_cli_make_parent (cm, pi);
1288 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1290 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1294 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1296 vlib_cli_main_t *cm = &vm->cli_main;
1297 clib_error_t *error = 0;
1299 char *normalized_path;
1301 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1304 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1306 if (!cm->command_index_by_path)
1307 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1308 sizeof (c->path[0]),
1311 /* See if command already exists with given path. */
1312 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1315 vlib_cli_command_t *d;
1318 d = vec_elt_at_index (cm->commands, ci);
1320 /* If existing command was created via vlib_cli_make_parent
1321 replaced it with callers data. */
1322 if (vlib_cli_command_is_empty (d))
1324 vlib_cli_command_t save = d[0];
1326 ASSERT (!vlib_cli_command_is_empty (c));
1328 /* Copy callers fields. */
1331 /* Save internal fields. */
1332 d->path = save.path;
1333 d->sub_commands = save.sub_commands;
1334 d->sub_command_index_by_name = save.sub_command_index_by_name;
1335 d->sub_command_positions = save.sub_command_positions;
1336 d->sub_rules = save.sub_rules;
1340 clib_error_return (0, "duplicate command name with path %v",
1343 vec_free (normalized_path);
1349 /* Command does not exist: create it. */
1351 /* Add root command (index 0). */
1352 if (vec_len (cm->commands) == 0)
1354 /* Create command with index 0; path is empty string. */
1355 vec_resize (cm->commands, 1);
1358 ci = vec_len (cm->commands);
1359 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1360 vec_add1 (cm->commands, c[0]);
1362 c = vec_elt_at_index (cm->commands, ci);
1363 c->path = normalized_path;
1365 /* Don't inherit from registration. */
1366 c->sub_commands = 0;
1367 c->sub_command_index_by_name = 0;
1368 c->sub_command_positions = 0;
1371 vlib_cli_make_parent (cm, ci);
1376 /* $$$ turn back on again someday, maybe */
1378 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1380 vlib_cli_main_t *cm = &vm->cli_main;
1381 vlib_cli_parse_rule_t *r;
1382 clib_error_t *error = 0;
1386 if (!cm->parse_rule_index_by_name)
1387 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1388 sizeof (r->name[0]),
1391 /* Make vector copy of name. */
1392 r_name = format (0, "%s", r_reg->name);
1394 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1397 return clib_error_return (0, "duplicate parse rule name `%s'",
1401 vec_add2 (cm->parse_rules, r, 1);
1403 r->name = (char *) r_name;
1404 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1409 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1410 vlib_cli_parse_rule_t *
1412 vlib_cli_parse_rule_t *
1414 __attribute__ ((unused))
1416 clib_error_t *error = 0;
1417 vlib_cli_parse_rule_t *r;
1419 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1421 if (!r->name || strlen (r->name) == 0)
1423 error = clib_error_return (0, "parse rule with no name");
1427 error = vlib_cli_register_parse_rule (vm, r);
1437 static clib_error_t *
1438 elog_trace_command_fn (vlib_main_t * vm,
1439 unformat_input_t * input, vlib_cli_command_t * cmd)
1441 unformat_input_t _line_input, *line_input = &_line_input;
1443 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1444 u32 circuit_node_index;
1446 if (!unformat_user (input, unformat_line_input, line_input))
1449 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1451 if (unformat (line_input, "api"))
1453 else if (unformat (line_input, "dispatch"))
1455 else if (unformat (line_input, "circuit-node %U",
1456 unformat_vlib_node, vm, &circuit_node_index))
1458 else if (unformat (line_input, "cli"))
1460 else if (unformat (line_input, "barrier"))
1462 else if (unformat (line_input, "disable"))
1464 else if (unformat (line_input, "enable"))
1469 unformat_free (line_input);
1471 vl_api_set_elog_trace_api_messages
1472 (api ? enable : vl_api_get_elog_trace_api_messages ());
1473 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1474 vm->elog_trace_graph_dispatch = dispatch ?
1475 enable : vm->elog_trace_graph_dispatch;
1476 vm->elog_trace_graph_circuit = circuit ?
1477 enable : vm->elog_trace_graph_circuit;
1478 vlib_worker_threads->barrier_elog_enabled =
1479 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1480 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1483 * Set up start-of-buffer logic-analyzer trigger
1484 * for main loop event logs, which are fairly heavyweight.
1485 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1486 * will fully disable the scheme when the elog buffer fills.
1488 if (dispatch || circuit)
1490 elog_main_t *em = &vm->elog_main;
1492 em->n_total_events_disable_limit =
1493 em->n_total_events + vec_len (em->event_ring);
1498 vlib_cli_output (vm, "Current status:");
1501 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1502 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1503 vm->elog_trace_cli_commands ? "on" : "off");
1505 (vm, " Barrier sync trace: %s",
1506 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1508 (vm, " Graph Dispatch: %s",
1509 vm->elog_trace_graph_dispatch ? "on" : "off");
1511 (vm, " Graph Circuit: %s",
1512 vm->elog_trace_graph_circuit ? "on" : "off");
1513 if (vm->elog_trace_graph_circuit)
1516 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1522 * Control event logging of api, cli, and thread barrier events
1523 * With no arguments, displays the current trace status.
1524 * Name the event groups you wish to trace or stop tracing.
1528 * elog trace api cli barrier
1529 * elog trace api cli barrier disable
1530 * elog trace dispatch
1531 * elog trace circuit-node ethernet-input
1534 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1537 VLIB_CLI_COMMAND (elog_trace_command, static) =
1539 .path = "elog trace",
1540 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1541 "[circuit-node <name> e.g. ethernet-input][disable]",
1542 .function = elog_trace_command_fn,
1546 static clib_error_t *
1547 suspend_command_fn (vlib_main_t * vm,
1548 unformat_input_t * input, vlib_cli_command_t * cmd)
1550 vlib_process_suspend (vm, 30e-3);
1555 VLIB_CLI_COMMAND (suspend_command, static) =
1558 .short_help = "suspend debug CLI for 30ms",
1559 .function = suspend_command_fn,
1566 sort_cmds_by_path (void *a1, void *a2)
1570 vlib_main_t *vm = vlib_get_main ();
1571 vlib_cli_main_t *cm = &vm->cli_main;
1572 vlib_cli_command_t *c1, *c2;
1575 c1 = vec_elt_at_index (cm->commands, *index1);
1576 c2 = vec_elt_at_index (cm->commands, *index2);
1578 lmin = vec_len (c1->path);
1579 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1581 for (i = 0; i < lmin; i++)
1583 if (c1->path[i] < c2->path[i])
1585 else if (c1->path[i] > c2->path[i])
1594 vlib_cli_main_t *cm;
1595 u32 parent_command_index;
1597 int show_not_mp_safe;
1600 } vlib_cli_walk_args_t;
1603 cli_recursive_walk (vlib_cli_walk_args_t * aa)
1605 vlib_cli_command_t *parent;
1606 vlib_cli_sub_command_t *sub;
1607 vlib_cli_walk_args_t _a, *a = &_a;
1608 vlib_cli_main_t *cm;
1611 /* Copy args into this stack frame */
1615 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1617 if (parent->function)
1619 if (((a->show_mp_safe && parent->is_mp_safe)
1620 || (a->show_not_mp_safe && !parent->is_mp_safe))
1621 && (a->show_hit == 0 || parent->hit_counter))
1623 vec_add1 (cm->sort_vector, a->parent_command_index);
1627 parent->hit_counter = 0;
1630 for (i = 0; i < vec_len (parent->sub_commands); i++)
1632 sub = vec_elt_at_index (parent->sub_commands, i);
1633 a->parent_command_index = sub->index;
1634 cli_recursive_walk (a);
1639 format_mp_safe (u8 * s, va_list * args)
1641 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1642 int show_mp_safe = va_arg (*args, int);
1643 int show_not_mp_safe = va_arg (*args, int);
1644 int show_hit = va_arg (*args, int);
1645 int clear_hit = va_arg (*args, int);
1646 vlib_cli_command_t *c;
1647 vlib_cli_walk_args_t _a, *a = &_a;
1649 char *format_string = "\n%v";
1652 format_string = "\n%v: %u";
1654 vec_reset_length (cm->sort_vector);
1657 a->parent_command_index = 0;
1658 a->show_mp_safe = show_mp_safe;
1659 a->show_not_mp_safe = show_not_mp_safe;
1660 a->show_hit = show_hit;
1661 a->clear_hit = clear_hit;
1663 cli_recursive_walk (a);
1665 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1667 for (i = 0; i < vec_len (cm->sort_vector); i++)
1669 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1670 s = format (s, format_string, c->path, c->hit_counter);
1677 static clib_error_t *
1678 show_cli_command_fn (vlib_main_t * vm,
1679 unformat_input_t * input, vlib_cli_command_t * cmd)
1681 int show_mp_safe = 0;
1682 int show_not_mp_safe = 0;
1686 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1688 if (unformat (input, "mp-safe"))
1690 if (unformat (input, "not-mp-safe"))
1691 show_not_mp_safe = 1;
1692 else if (unformat (input, "hit"))
1694 else if (unformat (input, "clear-hit"))
1700 /* default set: all cli commands */
1701 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1702 show_mp_safe = show_not_mp_safe = 1;
1704 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1705 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1707 vlib_cli_output (vm, "hit counters cleared...");
1713 * Displays debug cli command information
1716 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1718 * "show cli" displays the entire debug cli:
1722 * adjacency counters
1728 * "show cli mp-safe" displays mp-safe debug CLI commands:
1742 * "show cli not-mp-safe" displays debug CLI commands
1743 * which cause worker thread barrier synchronization
1745 * "show cli hit" displays commands which have been executed. Qualify
1746 * as desired with "mp-safe" or "not-mp-safe".
1748 * "show cli clear-hit" clears the per-command hit counters.
1753 VLIB_CLI_COMMAND (show_cli_command, static) =
1756 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1757 .function = show_cli_command_fn,
1762 static clib_error_t *
1763 vlib_cli_init (vlib_main_t * vm)
1765 vlib_cli_main_t *cm = &vm->cli_main;
1766 clib_error_t *error = 0;
1767 vlib_cli_command_t *cmd;
1769 cmd = cm->cli_command_registrations;
1773 error = vlib_cli_register (vm, cmd);
1776 cmd = cmd->next_cli_command;
1781 VLIB_INIT_FUNCTION (vlib_cli_init);
1784 * fd.io coding-style-patch-verification: ON
1787 * eval: (c-set-style "gnu")