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 static void *current_traced_heap;
49 /* Root of all show commands. */
51 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
53 .short_help = "Show commands",
57 /* Root of all clear commands. */
59 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
61 .short_help = "Clear commands",
65 /* Root of all set commands. */
67 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
69 .short_help = "Set commands",
73 /* Root of all test commands. */
75 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
77 .short_help = "Test commands",
81 /* Returns bitmap of commands which match key. */
83 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
87 vlib_cli_parse_position_t *p;
89 unformat_skip_white_space (input);
95 k = unformat_get_input (input);
109 case UNFORMAT_END_OF_INPUT:
110 /* White space or end of input removes any non-white
111 matches that were before possible. */
112 if (i < vec_len (c->sub_command_positions)
113 && clib_bitmap_count_set_bits (match) > 1)
115 p = vec_elt_at_index (c->sub_command_positions, i);
116 for (n = 0; n < vec_len (p->bitmaps); n++)
117 match = clib_bitmap_andnot (match, p->bitmaps[n]);
122 unformat_put_input (input);
126 if (i >= vec_len (c->sub_command_positions))
129 clib_bitmap_free (match);
133 p = vec_elt_at_index (c->sub_command_positions, i);
134 if (vec_len (p->bitmaps) == 0)
138 if (n < 0 || n >= vec_len (p->bitmaps))
142 match = clib_bitmap_dup (p->bitmaps[n]);
144 match = clib_bitmap_and (match, p->bitmaps[n]);
146 if (clib_bitmap_is_zero (match))
154 /* Looks for string based sub-input formatted { SUB-INPUT }. */
156 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
158 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
164 c = unformat_get_input (i);
176 /* Put back paren. */
177 if (c != UNFORMAT_END_OF_INPUT)
178 unformat_put_input (i);
180 if (c == '{' && unformat (i, "%v", &s))
182 unformat_init_vector (sub_input, s);
191 static vlib_cli_command_t *
192 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
194 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
195 return vec_elt_at_index (cm->commands, s->index);
199 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
201 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
202 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
203 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
204 vlib_cli_main_t *cm = &vm->cli_main;
205 uword *match_bitmap, is_unique, index;
208 vlib_cli_sub_rule_t *sr;
209 vlib_cli_parse_rule_t *r;
210 vec_foreach (sr, c->sub_rules)
213 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
214 vec_add2 (cm->parse_rule_data, d, 1);
215 vec_reset_length (d[0]);
217 d[0] = _vec_resize (d[0],
218 /* length increment */ 1,
220 /* header_bytes */ 0,
221 /* data align */ sizeof (uword));
222 if (unformat_user (i, r->unformat_function, vm, d[0]))
224 *result = vec_elt_at_index (cm->commands, sr->command_index);
230 match_bitmap = vlib_cli_sub_command_match (c, i);
231 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
235 index = clib_bitmap_first_set (match_bitmap);
236 *result = get_sub_command (cm, c, index);
238 clib_bitmap_free (match_bitmap);
244 vlib_cli_cmp_strings (void *a1, void *a2)
246 u8 *c1 = *(u8 **) a1;
247 u8 *c2 = *(u8 **) a2;
249 return vec_cmp (c1, c2);
253 vlib_cli_get_possible_completions (u8 * str)
255 vlib_cli_command_t *c;
256 vlib_cli_sub_command_t *sc;
257 vlib_main_t *vm = vlib_get_main ();
258 vlib_cli_main_t *vcm = &vm->cli_main;
259 uword *match_bitmap = 0;
260 uword index, is_unique, help_next_level;
262 unformat_input_t input;
263 unformat_init_vector (&input, vec_dup (str));
264 c = vec_elt_at_index (vcm->commands, 0);
266 /* remove trailing whitespace, except for one of them */
267 while (vec_len (input.buffer) >= 2 &&
268 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
269 isspace (input.buffer[vec_len (input.buffer) - 2]))
271 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
274 /* if input is empty, directly return list of root commands */
275 if (vec_len (input.buffer) == 0 ||
276 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
278 vec_foreach (sc, c->sub_commands)
280 vec_add1 (result, (u8 *) sc->name);
285 /* add a trailing '?' so that vlib_cli_sub_command_match can find
286 * all commands starting with the input string */
287 vec_add1 (input.buffer, '?');
291 match_bitmap = vlib_cli_sub_command_match (c, &input);
292 /* no match: return no result */
293 if (match_bitmap == 0)
297 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
298 /* unique match: try to step one subcommand level further */
301 /* stop if no more input */
302 if (input.index >= vec_len (input.buffer) - 1)
307 index = clib_bitmap_first_set (match_bitmap);
308 c = get_sub_command (vcm, c, index);
309 clib_bitmap_free (match_bitmap);
312 /* multiple matches: stop here, return all matches */
316 /* remove trailing '?' */
317 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
319 /* if we have a space at the end of input, and a unique match,
320 * autocomplete the next level of subcommands */
321 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
323 clib_bitmap_foreach(index, match_bitmap, {
324 if (help_next_level && is_unique) {
325 c = get_sub_command (vcm, c, index);
326 vec_foreach (sc, c->sub_commands) {
327 vec_add1 (result, (u8*) sc->name);
329 goto done; /* break doesn't work in this macro-loop */
331 sc = &c->sub_commands[index];
332 vec_add1(result, (u8*) sc->name);
337 clib_bitmap_free (match_bitmap);
338 unformat_free (&input);
341 vec_sort_with_function (result, vlib_cli_cmp_strings);
346 format_vlib_cli_command_help (u8 * s, va_list * args)
348 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
349 int is_long = va_arg (*args, int);
350 if (is_long && c->long_help)
351 s = format (s, "%s", c->long_help);
352 else if (c->short_help)
353 s = format (s, "%s", c->short_help);
355 s = format (s, "%v commands", c->path);
360 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
362 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
363 return format (s, "<%U>", format_c_identifier, r->name);
367 format_vlib_cli_path (u8 * s, va_list * args)
369 u8 *path = va_arg (*args, u8 *);
372 for (i = 0; i < vec_len (path); i++)
378 vec_add1 (s, '<'); /* start of <RULE> */
382 /* _ -> space in rules. */
383 vec_add1 (s, in_rule ? ' ' : '_');
389 vec_add1 (s, '>'); /* end of <RULE> */
396 vec_add1 (s, path[i]);
402 vec_add1 (s, '>'); /* terminate <RULE> */
407 static vlib_cli_command_t *
408 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
410 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
411 vlib_cli_sub_command_t *sc;
412 vlib_cli_sub_rule_t *sr;
415 vec_add1 (subs, c[0]);
417 vec_foreach (sr, c->sub_rules)
418 subs = all_subs (cm, subs, sr->command_index);
419 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
425 vlib_cli_cmp_rule (void *a1, void *a2)
427 vlib_cli_sub_rule_t *r1 = a1;
428 vlib_cli_sub_rule_t *r2 = a2;
430 return vec_cmp (r1->name, r2->name);
434 vlib_cli_cmp_command (void *a1, void *a2)
436 vlib_cli_command_t *c1 = a1;
437 vlib_cli_command_t *c2 = a2;
439 return vec_cmp (c1->path, c2->path);
442 static clib_error_t *
443 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
444 vlib_cli_main_t * cm,
445 unformat_input_t * input,
446 uword parent_command_index)
448 vlib_cli_command_t *parent, *c;
449 clib_error_t *error = 0;
450 unformat_input_t sub_input;
452 uword is_main_dispatch = cm == &vm->cli_main;
454 parent = vec_elt_at_index (cm->commands, parent_command_index);
455 if (is_main_dispatch && unformat (input, "help"))
457 uword help_at_end_of_line, i;
459 help_at_end_of_line =
460 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
465 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
468 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
475 /* help SUB-COMMAND => long format help.
476 "help" at end of line: show all commands. */
477 if (!help_at_end_of_line)
478 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
481 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
482 vlib_cli_output (vm, "%v: no sub-commands", c->path);
486 vlib_cli_sub_command_t *sc;
487 vlib_cli_sub_rule_t *sr, *subs;
489 subs = vec_dup (c->sub_rules);
491 /* Add in rules if any. */
492 vec_foreach (sc, c->sub_commands)
494 vec_add2 (subs, sr, 1);
496 sr->command_index = sc->index;
500 vec_sort_with_function (subs, vlib_cli_cmp_rule);
502 for (i = 0; i < vec_len (subs); i++)
504 vlib_cli_command_t *d;
505 vlib_cli_parse_rule_t *r;
507 d = vec_elt_at_index (cm->commands, subs[i].command_index);
509 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
517 format_vlib_cli_parse_rule_name, r,
518 format_vlib_cli_command_help, d, /* is_long */ 0);
523 format_vlib_cli_command_help, d, /* is_long */ 0);
530 else if (is_main_dispatch
531 && (unformat (input, "choices") || unformat (input, "?")))
533 vlib_cli_command_t *sub, *subs;
535 subs = all_subs (cm, 0, parent_command_index);
536 vec_sort_with_function (subs, vlib_cli_cmp_command);
537 vec_foreach (sub, subs)
538 vlib_cli_output (vm, " %-40U %U",
539 format_vlib_cli_path, sub->path,
540 format_vlib_cli_command_help, sub, /* is_long */ 0);
544 else if (unformat (input, "comment %v", &string))
549 else if (unformat (input, "uncomment %U",
550 unformat_vlib_cli_sub_input, &sub_input))
553 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
554 parent_command_index);
555 unformat_free (&sub_input);
557 else if (unformat (input, "leak-check %U",
558 unformat_vlib_cli_sub_input, &sub_input))
561 if (current_traced_heap)
564 oldheap = clib_mem_set_heap (current_traced_heap);
566 clib_mem_set_heap (oldheap);
567 current_traced_heap = 0;
571 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
572 parent_command_index);
573 unformat_free (&sub_input);
575 /* Otherwise, the clib_error_t shows up as a leak... */
578 vlib_cli_output (vm, "%v", error->what);
579 clib_error_free (error);
583 (void) clib_mem_trace_enable_disable (0);
584 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
585 1 /* verbose, i.e. print leaks */ );
587 vlib_cli_output (vm, "%v", leak_report);
588 vec_free (leak_report);
592 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
594 unformat_input_t *si;
595 uword has_sub_commands =
596 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
599 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
602 if (has_sub_commands)
603 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
605 if (has_sub_commands && !error)
606 /* Found valid sub-command. */ ;
608 else if (c->function)
610 clib_error_t *c_error;
612 /* Skip white space for benefit of called function. */
613 unformat_skip_white_space (si);
615 if (unformat (si, "?"))
617 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
622 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
625 ELOG_TYPE_DECLARE (e) =
627 .format = "cli-cmd: %s",
635 ed = ELOG_DATA (&vm->elog_main, e);
636 ed->c = elog_global_id_for_msg_name (c->path);
640 vlib_worker_thread_barrier_sync (vm);
642 c_error = c->function (vm, si, c);
645 vlib_worker_thread_barrier_release (vm);
647 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
650 ELOG_TYPE_DECLARE (e) =
652 .format = "cli-cmd: %s %s",
653 .format_args = "T4T4",
660 ed = ELOG_DATA (&vm->elog_main, e);
661 ed->c = elog_global_id_for_msg_name (c->path);
664 vec_add1 (c_error->what, 0);
665 ed->err = elog_global_id_for_msg_name
666 ((const char *) c_error->what);
667 _vec_len (c_error->what) -= 1;
670 ed->err = elog_global_id_for_msg_name ("OK");
676 clib_error_return (0, "%v: %v", c->path, c_error->what);
677 clib_error_free (c_error);
678 /* Free sub input. */
686 /* Free any previous error. */
687 clib_error_free (error);
691 error = clib_error_return (0, "%v: no sub-commands", c->path);
693 /* Free sub input. */
705 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
706 format_unformat_error, input);
708 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
713 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
714 __attribute__ ((weak));
717 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
721 /* Process CLI input. */
723 vlib_cli_input (vlib_main_t * vm,
724 unformat_input_t * input,
725 vlib_cli_output_function_t * function, uword function_arg)
727 vlib_process_t *cp = vlib_get_current_process (vm);
728 vlib_cli_main_t *cm = &vm->cli_main;
730 vlib_cli_output_function_t *save_function;
731 uword save_function_arg;
734 save_function = cp->output_function;
735 save_function_arg = cp->output_function_arg;
737 cp->output_function = function;
738 cp->output_function_arg = function_arg;
742 vec_reset_length (cm->parse_rule_data);
743 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
746 while (!error && !unformat (input, "%U", unformat_eof));
750 vlib_cli_output (vm, "%v", error->what);
751 vlib_unix_error_report (vm, error);
752 /* clib_error_return is unfortunately often called with a '0'
754 rv = error->code != 0 ? error->code : -1;
755 clib_error_free (error);
758 cp->output_function = save_function;
759 cp->output_function_arg = save_function_arg;
763 /* Output to current CLI connection. */
765 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
767 vlib_process_t *cp = vlib_get_current_process (vm);
772 s = va_format (0, fmt, &va);
775 /* Terminate with \n if not present. */
776 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
779 if ((!cp) || (!cp->output_function))
780 fformat (stdout, "%v", s);
782 cp->output_function (cp->output_function_arg, s, vec_len (s));
787 void *vl_msg_push_heap (void) __attribute__ ((weak));
789 vl_msg_push_heap (void)
794 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
796 vl_msg_pop_heap (void *oldheap)
800 void *vlib_stats_push_heap (void *) __attribute__ ((weak));
802 vlib_stats_push_heap (void *notused)
807 static clib_error_t *
808 show_memory_usage (vlib_main_t * vm,
809 unformat_input_t * input, vlib_cli_command_t * cmd)
811 int verbose __attribute__ ((unused)) = 0;
812 int api_segment = 0, stats_segment = 0, main_heap = 0;
815 uword clib_mem_trace_enable_disable (uword enable);
819 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
821 if (unformat (input, "verbose"))
823 else if (unformat (input, "api-segment"))
825 else if (unformat (input, "stats-segment"))
827 else if (unformat (input, "main-heap"))
831 error = clib_error_return (0, "unknown input `%U'",
832 format_unformat_error, input);
837 if ((api_segment + stats_segment + main_heap) == 0)
838 return clib_error_return
839 (0, "Please supply one of api-segment, stats-segment or main-heap");
843 void *oldheap = vl_msg_push_heap ();
844 was_enabled = clib_mem_trace_enable_disable (0);
846 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
847 vl_msg_pop_heap (oldheap);
848 u8 *s = vec_dup (s_in_svm);
850 oldheap = vl_msg_push_heap ();
852 clib_mem_trace_enable_disable (was_enabled);
853 vl_msg_pop_heap (oldheap);
854 vlib_cli_output (vm, "API segment");
855 vlib_cli_output (vm, "%v", s);
860 void *oldheap = vlib_stats_push_heap (0);
861 was_enabled = clib_mem_trace_enable_disable (0);
863 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
865 clib_mem_set_heap (oldheap);
866 u8 *s = vec_dup (s_in_svm);
868 oldheap = vlib_stats_push_heap (0);
872 clib_mem_trace_enable_disable (was_enabled);
873 clib_mem_set_heap (oldheap);
875 vlib_cli_output (vm, "Stats segment");
876 vlib_cli_output (vm, "%v", s);
880 #if USE_DLMALLOC == 0
884 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
885 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
886 vlib_worker_threads[index].name);
887 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
888 h->vm_alloc_offset_from_header,
890 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
900 * Note: the foreach_vlib_main causes allocator traffic,
901 * so shut off tracing before we go there...
903 was_enabled = clib_mem_trace_enable_disable (0);
908 struct dlmallinfo mi;
910 mspace = clib_per_cpu_mheaps[index];
912 mi = mspace_mallinfo (mspace);
913 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
914 vlib_worker_threads[index].name);
915 vlib_cli_output (vm, " %U\n", format_page_map,
916 pointer_to_uword (mspace_least_addr(mspace)),
918 vlib_cli_output (vm, " %U\n", format_mheap,
919 clib_per_cpu_mheaps[index],
925 /* Restore the trace flag */
926 clib_mem_trace_enable_disable (was_enabled);
929 #endif /* USE_DLMALLOC */
934 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
935 .path = "show memory",
936 .short_help = "show memory [api-segment][stats-segment][verbose]",
937 .function = show_memory_usage,
941 static clib_error_t *
942 show_cpu (vlib_main_t * vm, unformat_input_t * input,
943 vlib_cli_command_t * cmd)
945 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
946 _("Model name", "%U", format_cpu_model_name);
947 _("Microarch model (family)", "%U", format_cpu_uarch);
948 _("Flags", "%U", format_cpu_flags);
949 _("Base frequency", "%.2f GHz",
950 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
956 * Displays various information about the CPU.
959 * @cliexstart{show cpu}
960 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
961 * Microarchitecture: Broadwell (Broadwell-EP/EX)
962 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
963 * Base Frequency: 3.20 GHz
967 VLIB_CLI_COMMAND (show_cpu_command, static) = {
969 .short_help = "Show cpu information",
970 .function = show_cpu,
974 static clib_error_t *
975 enable_disable_memory_trace (vlib_main_t * vm,
976 unformat_input_t * input,
977 vlib_cli_command_t * cmd)
979 unformat_input_t _line_input, *line_input = &_line_input;
982 int stats_segment = 0;
986 if (!unformat_user (input, unformat_line_input, line_input))
989 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
991 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
993 else if (unformat (line_input, "api-segment"))
995 else if (unformat (line_input, "stats-segment"))
997 else if (unformat (line_input, "main-heap"))
1001 unformat_free (line_input);
1002 return clib_error_return (0, "invalid input");
1005 unformat_free (line_input);
1007 if ((api_segment + stats_segment + main_heap + (enable == 0)) == 0)
1009 return clib_error_return
1010 (0, "Need one of main-heap, stats-segment or api-segment");
1013 /* Turn off current trace, if any */
1014 if (current_traced_heap)
1017 oldheap = clib_mem_set_heap (current_traced_heap);
1019 clib_mem_set_heap (oldheap);
1020 current_traced_heap = 0;
1029 oldheap = vl_msg_push_heap ();
1030 current_traced_heap = clib_mem_get_heap ();
1032 vl_msg_pop_heap (oldheap);
1039 oldheap = vlib_stats_push_heap (0);
1040 current_traced_heap = clib_mem_get_heap ();
1041 clib_mem_trace (stats_segment);
1042 /* We don't want to call vlib_stats_pop_heap... */
1044 clib_mem_set_heap (oldheap);
1050 current_traced_heap = clib_mem_get_heap ();
1051 clib_mem_trace (main_heap);
1058 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1059 .path = "memory-trace",
1060 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n",
1061 .function = enable_disable_memory_trace,
1066 static clib_error_t *
1067 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
1068 vlib_cli_command_t * cmd)
1070 #if USE_DLMALLOC == 0
1071 clib_error_t *error = 0;
1075 if (unformat (input, "on"))
1079 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1080 mheap = mheap_header(heap);
1081 mheap->flags |= MHEAP_FLAG_VALIDATE;
1082 // Turn off small object cache because it delays detection of errors
1083 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
1088 else if (unformat (input, "off"))
1092 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1093 mheap = mheap_header(heap);
1094 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
1095 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
1099 else if (unformat (input, "now"))
1103 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1104 mheap = mheap_header(heap);
1105 mheap_validate(heap);
1108 vlib_cli_output (vm, "heap validation complete");
1113 return clib_error_return (0, "unknown input `%U'",
1114 format_unformat_error, input);
1119 return clib_error_return (0, "unimplemented...");
1120 #endif /* USE_DLMALLOC */
1124 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1125 .path = "test heap-validate",
1126 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1127 .function = test_heap_validate,
1131 static clib_error_t *
1132 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1133 vlib_cli_command_t * cmd)
1135 clib_file_main_t *fm = &file_main;
1138 /* environ(7) does not indicate a header for this */
1139 extern char **environ;
1141 /* Close all known open files */
1143 pool_foreach(f, fm->file_pool,
1145 if (f->file_descriptor > 2)
1146 close(f->file_descriptor);
1151 execve (vm->name, (char **) vm->argv, environ);
1157 VLIB_CLI_COMMAND (restart_cmd,static) = {
1159 .short_help = "restart process",
1160 .function = restart_cmd_fn,
1166 * A trivial test harness to verify the per-process output_function
1167 * is working correcty.
1170 static clib_error_t *
1171 sleep_ten_seconds (vlib_main_t * vm,
1172 unformat_input_t * input, vlib_cli_command_t * cmd)
1175 u16 my_id = rand ();
1177 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1179 for (i = 0; i < 10; i++)
1181 vlib_process_wait_for_event_or_clock (vm, 1.0);
1182 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1184 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1189 VLIB_CLI_COMMAND (ping_command, static) = {
1190 .path = "test sleep",
1191 .function = sleep_ten_seconds,
1192 .short_help = "Sleep for 10 seconds",
1195 #endif /* ifdef TEST_CODE */
1198 vlib_cli_normalize_path (char *input, char **result)
1203 uword index_of_last_space = ~0;
1208 /* Multiple white space -> single space. */
1215 if (l > 0 && s[l - 1] != ' ')
1223 if (l > 0 && s[l - 1] == ' ')
1224 index_of_last_space = vec_len (s);
1231 /* Remove any extra space at end. */
1232 if (l > 0 && s[l - 1] == ' ')
1236 return index_of_last_space;
1240 parent_path_len (char *path)
1243 for (i = vec_len (path) - 1; i >= 0; i--)
1252 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1254 vlib_cli_command_t *p, *c;
1255 vlib_cli_sub_command_t *sub_c;
1259 p = vec_elt_at_index (cm->commands, parent_index);
1260 c = vec_elt_at_index (cm->commands, child_index);
1262 l = parent_path_len (c->path);
1264 sub_name = vec_dup ((u8 *) c->path);
1267 ASSERT (l + 1 < vec_len (c->path));
1269 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1272 if (sub_name[0] == '%')
1275 vlib_cli_sub_rule_t *sr;
1278 vec_delete (sub_name, 1, 0);
1280 if (!p->sub_rule_index_by_name)
1281 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1282 sizeof (sub_name[0]),
1284 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1287 sr = vec_elt_at_index (p->sub_rules, q[0]);
1288 ASSERT (sr->command_index == child_index);
1292 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
1295 clib_error ("reference to unknown rule `%%%v' in path `%v'",
1300 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1301 vec_len (p->sub_rules));
1302 vec_add2 (p->sub_rules, sr, 1);
1303 sr->name = sub_name;
1304 sr->rule_index = q[0];
1305 sr->command_index = child_index;
1309 if (!p->sub_command_index_by_name)
1310 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1311 sizeof (c->path[0]),
1314 /* Check if sub-command has already been created. */
1315 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1317 vec_free (sub_name);
1321 vec_add2 (p->sub_commands, sub_c, 1);
1322 sub_c->index = child_index;
1323 sub_c->name = sub_name;
1324 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1325 sub_c - p->sub_commands);
1327 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1328 for (i = 0; i < vec_len (sub_c->name); i++)
1331 vlib_cli_parse_position_t *pos;
1333 pos = vec_elt_at_index (p->sub_command_positions, i);
1336 pos->min_char = sub_c->name[i];
1338 n = sub_c->name[i] - pos->min_char;
1341 pos->min_char = sub_c->name[i];
1342 vec_insert (pos->bitmaps, -n, 0);
1346 vec_validate (pos->bitmaps, n);
1348 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1353 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1355 uword p_len, pi, *p;
1357 vlib_cli_command_t *c, *parent;
1359 /* Root command (index 0) should have already been added. */
1360 ASSERT (vec_len (cm->commands) > 0);
1362 c = vec_elt_at_index (cm->commands, ci);
1363 p_len = parent_path_len (c->path);
1365 /* No space? Parent is root command. */
1368 add_sub_command (cm, 0, ci);
1373 vec_add (p_path, c->path, p_len);
1375 p = hash_get_mem (cm->command_index_by_path, p_path);
1377 /* Parent exists? */
1380 /* Parent does not exist; create it. */
1381 vec_add2 (cm->commands, parent, 1);
1382 parent->path = p_path;
1383 hash_set_mem (cm->command_index_by_path, parent->path,
1384 parent - cm->commands);
1385 pi = parent - cm->commands;
1393 add_sub_command (cm, pi, ci);
1395 /* Create parent's parent. */
1397 vlib_cli_make_parent (cm, pi);
1401 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1403 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1407 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1409 vlib_cli_main_t *cm = &vm->cli_main;
1410 clib_error_t *error = 0;
1412 char *normalized_path;
1414 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1417 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1419 if (!cm->command_index_by_path)
1420 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1421 sizeof (c->path[0]),
1424 /* See if command already exists with given path. */
1425 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1428 vlib_cli_command_t *d;
1431 d = vec_elt_at_index (cm->commands, ci);
1433 /* If existing command was created via vlib_cli_make_parent
1434 replaced it with callers data. */
1435 if (vlib_cli_command_is_empty (d))
1437 vlib_cli_command_t save = d[0];
1439 ASSERT (!vlib_cli_command_is_empty (c));
1441 /* Copy callers fields. */
1444 /* Save internal fields. */
1445 d->path = save.path;
1446 d->sub_commands = save.sub_commands;
1447 d->sub_command_index_by_name = save.sub_command_index_by_name;
1448 d->sub_command_positions = save.sub_command_positions;
1449 d->sub_rules = save.sub_rules;
1453 clib_error_return (0, "duplicate command name with path %v",
1456 vec_free (normalized_path);
1462 /* Command does not exist: create it. */
1464 /* Add root command (index 0). */
1465 if (vec_len (cm->commands) == 0)
1467 /* Create command with index 0; path is empty string. */
1468 vec_resize (cm->commands, 1);
1471 ci = vec_len (cm->commands);
1472 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1473 vec_add1 (cm->commands, c[0]);
1475 c = vec_elt_at_index (cm->commands, ci);
1476 c->path = normalized_path;
1478 /* Don't inherit from registration. */
1479 c->sub_commands = 0;
1480 c->sub_command_index_by_name = 0;
1481 c->sub_command_positions = 0;
1484 vlib_cli_make_parent (cm, ci);
1489 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1491 vlib_cli_main_t *cm = &vm->cli_main;
1492 vlib_cli_parse_rule_t *r;
1493 clib_error_t *error = 0;
1497 if (!cm->parse_rule_index_by_name)
1498 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1499 sizeof (r->name[0]),
1502 /* Make vector copy of name. */
1503 r_name = format (0, "%s", r_reg->name);
1505 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1508 return clib_error_return (0, "duplicate parse rule name `%s'",
1512 vec_add2 (cm->parse_rules, r, 1);
1514 r->name = (char *) r_name;
1515 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1521 /* $$$ turn back on again someday, maybe */
1522 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1523 vlib_cli_parse_rule_t *
1525 vlib_cli_parse_rule_t *
1527 __attribute__ ((unused))
1529 clib_error_t *error = 0;
1530 vlib_cli_parse_rule_t *r;
1532 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1534 if (!r->name || strlen (r->name) == 0)
1536 error = clib_error_return (0, "parse rule with no name");
1540 error = vlib_cli_register_parse_rule (vm, r);
1551 cli_path_compare (void *a1, void *a2)
1556 if ((vec_len (*s1) < vec_len (*s2)) &&
1557 memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0)
1561 if ((vec_len (*s1) > vec_len (*s2)) &&
1562 memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0)
1565 return vec_cmp (*s1, *s2);
1568 static clib_error_t *
1569 show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1570 vlib_cli_command_t * cmd)
1572 vlib_cli_main_t *cm = &vm->cli_main;
1573 vlib_cli_command_t *cli;
1574 u8 **paths = 0, **s;
1577 vec_foreach (cli, cm->commands)
1578 if (vec_len (cli->path) > 0)
1579 vec_add1 (paths, (u8 *) cli->path);
1581 vec_sort_with_function (paths, cli_path_compare);
1583 vec_foreach (s, paths)
1584 vlib_cli_output (vm, "%v", *s);
1592 VLIB_CLI_COMMAND (show_cli_command, static) = {
1594 .short_help = "Show cli commands",
1595 .function = show_cli_cmd_fn,
1599 static clib_error_t *
1600 elog_trace_command_fn (vlib_main_t * vm,
1601 unformat_input_t * input, vlib_cli_command_t * cmd)
1603 unformat_input_t _line_input, *line_input = &_line_input;
1605 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1606 u32 circuit_node_index;
1608 if (!unformat_user (input, unformat_line_input, line_input))
1611 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1613 if (unformat (line_input, "api"))
1615 else if (unformat (line_input, "dispatch"))
1617 else if (unformat (line_input, "circuit-node %U",
1618 unformat_vlib_node, vm, &circuit_node_index))
1620 else if (unformat (line_input, "cli"))
1622 else if (unformat (line_input, "barrier"))
1624 else if (unformat (line_input, "disable"))
1626 else if (unformat (line_input, "enable"))
1631 unformat_free (line_input);
1633 vm->elog_trace_api_messages = api ? enable : vm->elog_trace_api_messages;
1634 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1635 vm->elog_trace_graph_dispatch = dispatch ?
1636 enable : vm->elog_trace_graph_dispatch;
1637 vm->elog_trace_graph_circuit = circuit ?
1638 enable : vm->elog_trace_graph_circuit;
1639 vlib_worker_threads->barrier_elog_enabled =
1640 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1641 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1644 * Set up start-of-buffer logic-analyzer trigger
1645 * for main loop event logs, which are fairly heavyweight.
1646 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1647 * will fully disable the scheme when the elog buffer fills.
1649 if (dispatch || circuit)
1651 elog_main_t *em = &vm->elog_main;
1653 em->n_total_events_disable_limit =
1654 em->n_total_events + vec_len (em->event_ring);
1659 vlib_cli_output (vm, "Current status:");
1662 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1663 vm->elog_trace_api_messages ? "on" : "off",
1664 vm->elog_trace_cli_commands ? "on" : "off");
1666 (vm, " Barrier sync trace: %s",
1667 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1669 (vm, " Graph Dispatch: %s",
1670 vm->elog_trace_graph_dispatch ? "on" : "off");
1672 (vm, " Graph Circuit: %s",
1673 vm->elog_trace_graph_circuit ? "on" : "off");
1674 if (vm->elog_trace_graph_circuit)
1677 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1683 * Control event logging of api, cli, and thread barrier events
1684 * With no arguments, displays the current trace status.
1685 * Name the event groups you wish to trace or stop tracing.
1689 * elog trace api cli barrier
1690 * elog trace api cli barrier disable
1691 * elog trace dispatch
1692 * elog trace circuit-node ethernet-input
1695 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1698 VLIB_CLI_COMMAND (elog_trace_command, static) =
1700 .path = "elog trace",
1701 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1702 "[circuit-node <name> e.g. ethernet-input][disable]",
1703 .function = elog_trace_command_fn,
1707 static clib_error_t *
1708 vlib_cli_init (vlib_main_t * vm)
1710 vlib_cli_main_t *cm = &vm->cli_main;
1711 clib_error_t *error = 0;
1712 vlib_cli_command_t *cmd;
1714 cmd = cm->cli_command_registrations;
1718 error = vlib_cli_register (vm, cmd);
1721 cmd = cmd->next_cli_command;
1726 VLIB_INIT_FUNCTION (vlib_cli_init);
1729 * fd.io coding-style-patch-verification: ON
1732 * eval: (c-set-style "gnu")