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 /* Root of all show commands. */
49 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
51 .short_help = "Show commands",
55 /* Root of all clear commands. */
57 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
59 .short_help = "Clear commands",
63 /* Root of all set commands. */
65 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
67 .short_help = "Set commands",
71 /* Root of all test commands. */
73 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
75 .short_help = "Test commands",
79 /* Returns bitmap of commands which match key. */
81 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
85 vlib_cli_parse_position_t *p;
87 unformat_skip_white_space (input);
93 k = unformat_get_input (input);
107 case UNFORMAT_END_OF_INPUT:
108 /* White space or end of input removes any non-white
109 matches that were before possible. */
110 if (i < vec_len (c->sub_command_positions)
111 && clib_bitmap_count_set_bits (match) > 1)
113 p = vec_elt_at_index (c->sub_command_positions, i);
114 for (n = 0; n < vec_len (p->bitmaps); n++)
115 match = clib_bitmap_andnot (match, p->bitmaps[n]);
120 unformat_put_input (input);
124 if (i >= vec_len (c->sub_command_positions))
127 clib_bitmap_free (match);
131 p = vec_elt_at_index (c->sub_command_positions, i);
132 if (vec_len (p->bitmaps) == 0)
136 if (n < 0 || n >= vec_len (p->bitmaps))
140 match = clib_bitmap_dup (p->bitmaps[n]);
142 match = clib_bitmap_and (match, p->bitmaps[n]);
144 if (clib_bitmap_is_zero (match))
152 /* Looks for string based sub-input formatted { SUB-INPUT }. */
154 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
156 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
162 c = unformat_get_input (i);
174 /* Put back paren. */
175 if (c != UNFORMAT_END_OF_INPUT)
176 unformat_put_input (i);
178 if (c == '{' && unformat (i, "%v", &s))
180 unformat_init_vector (sub_input, s);
189 static vlib_cli_command_t *
190 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
192 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
193 return vec_elt_at_index (cm->commands, s->index);
197 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
199 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
200 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
201 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
202 vlib_cli_main_t *cm = &vm->cli_main;
203 uword *match_bitmap, is_unique, index;
206 vlib_cli_sub_rule_t *sr;
207 vlib_cli_parse_rule_t *r;
208 vec_foreach (sr, c->sub_rules)
211 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
212 vec_add2 (cm->parse_rule_data, d, 1);
213 vec_reset_length (d[0]);
215 d[0] = _vec_resize (d[0],
216 /* length increment */ 1,
218 /* header_bytes */ 0,
219 /* data align */ sizeof (uword));
220 if (unformat_user (i, r->unformat_function, vm, d[0]))
222 *result = vec_elt_at_index (cm->commands, sr->command_index);
228 match_bitmap = vlib_cli_sub_command_match (c, i);
229 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
233 index = clib_bitmap_first_set (match_bitmap);
234 *result = get_sub_command (cm, c, index);
236 clib_bitmap_free (match_bitmap);
242 vlib_cli_cmp_strings (void *a1, void *a2)
244 u8 *c1 = *(u8 **) a1;
245 u8 *c2 = *(u8 **) a2;
247 return vec_cmp (c1, c2);
251 vlib_cli_get_possible_completions (u8 * str)
253 vlib_cli_command_t *c;
254 vlib_cli_sub_command_t *sc;
255 vlib_main_t *vm = vlib_get_main ();
256 vlib_cli_main_t *vcm = &vm->cli_main;
257 uword *match_bitmap = 0;
258 uword index, is_unique, help_next_level;
260 unformat_input_t input;
261 unformat_init_vector (&input, vec_dup (str));
262 c = vec_elt_at_index (vcm->commands, 0);
264 /* remove trailing whitespace, except for one of them */
265 while (vec_len (input.buffer) >= 2 &&
266 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
267 isspace (input.buffer[vec_len (input.buffer) - 2]))
269 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
272 /* if input is empty, directly return list of root commands */
273 if (vec_len (input.buffer) == 0 ||
274 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
276 vec_foreach (sc, c->sub_commands)
278 vec_add1 (result, (u8 *) sc->name);
283 /* add a trailing '?' so that vlib_cli_sub_command_match can find
284 * all commands starting with the input string */
285 vec_add1 (input.buffer, '?');
289 match_bitmap = vlib_cli_sub_command_match (c, &input);
290 /* no match: return no result */
291 if (match_bitmap == 0)
295 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
296 /* unique match: try to step one subcommand level further */
299 /* stop if no more input */
300 if (input.index >= vec_len (input.buffer) - 1)
305 index = clib_bitmap_first_set (match_bitmap);
306 c = get_sub_command (vcm, c, index);
307 clib_bitmap_free (match_bitmap);
310 /* multiple matches: stop here, return all matches */
314 /* remove trailing '?' */
315 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
317 /* if we have a space at the end of input, and a unique match,
318 * autocomplete the next level of subcommands */
319 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
321 clib_bitmap_foreach(index, match_bitmap, {
322 if (help_next_level && is_unique) {
323 c = get_sub_command (vcm, c, index);
324 vec_foreach (sc, c->sub_commands) {
325 vec_add1 (result, (u8*) sc->name);
327 goto done; /* break doesn't work in this macro-loop */
329 sc = &c->sub_commands[index];
330 vec_add1(result, (u8*) sc->name);
335 clib_bitmap_free (match_bitmap);
336 unformat_free (&input);
339 vec_sort_with_function (result, vlib_cli_cmp_strings);
344 format_vlib_cli_command_help (u8 * s, va_list * args)
346 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
347 int is_long = va_arg (*args, int);
348 if (is_long && c->long_help)
349 s = format (s, "%s", c->long_help);
350 else if (c->short_help)
351 s = format (s, "%s", c->short_help);
353 s = format (s, "%v commands", c->path);
358 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
360 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
361 return format (s, "<%U>", format_c_identifier, r->name);
365 format_vlib_cli_path (u8 * s, va_list * args)
367 u8 *path = va_arg (*args, u8 *);
370 for (i = 0; i < vec_len (path); i++)
376 vec_add1 (s, '<'); /* start of <RULE> */
380 /* _ -> space in rules. */
381 vec_add1 (s, in_rule ? ' ' : '_');
387 vec_add1 (s, '>'); /* end of <RULE> */
394 vec_add1 (s, path[i]);
400 vec_add1 (s, '>'); /* terminate <RULE> */
405 static vlib_cli_command_t *
406 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
408 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
409 vlib_cli_sub_command_t *sc;
410 vlib_cli_sub_rule_t *sr;
413 vec_add1 (subs, c[0]);
415 vec_foreach (sr, c->sub_rules)
416 subs = all_subs (cm, subs, sr->command_index);
417 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
423 vlib_cli_cmp_rule (void *a1, void *a2)
425 vlib_cli_sub_rule_t *r1 = a1;
426 vlib_cli_sub_rule_t *r2 = a2;
428 return vec_cmp (r1->name, r2->name);
432 vlib_cli_cmp_command (void *a1, void *a2)
434 vlib_cli_command_t *c1 = a1;
435 vlib_cli_command_t *c2 = a2;
437 return vec_cmp (c1->path, c2->path);
440 static clib_error_t *
441 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
442 vlib_cli_main_t * cm,
443 unformat_input_t * input,
444 uword parent_command_index)
446 vlib_cli_command_t *parent, *c;
447 clib_error_t *error = 0;
448 unformat_input_t sub_input;
450 uword is_main_dispatch = cm == &vm->cli_main;
452 parent = vec_elt_at_index (cm->commands, parent_command_index);
453 if (is_main_dispatch && unformat (input, "help"))
455 uword help_at_end_of_line, i;
457 help_at_end_of_line =
458 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
463 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
466 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
473 /* help SUB-COMMAND => long format help.
474 "help" at end of line: show all commands. */
475 if (!help_at_end_of_line)
476 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
479 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
480 vlib_cli_output (vm, "%v: no sub-commands", c->path);
484 vlib_cli_sub_command_t *sc;
485 vlib_cli_sub_rule_t *sr, *subs;
487 subs = vec_dup (c->sub_rules);
489 /* Add in rules if any. */
490 vec_foreach (sc, c->sub_commands)
492 vec_add2 (subs, sr, 1);
494 sr->command_index = sc->index;
498 vec_sort_with_function (subs, vlib_cli_cmp_rule);
500 for (i = 0; i < vec_len (subs); i++)
502 vlib_cli_command_t *d;
503 vlib_cli_parse_rule_t *r;
505 d = vec_elt_at_index (cm->commands, subs[i].command_index);
507 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
515 format_vlib_cli_parse_rule_name, r,
516 format_vlib_cli_command_help, d, /* is_long */ 0);
521 format_vlib_cli_command_help, d, /* is_long */ 0);
528 else if (is_main_dispatch
529 && (unformat (input, "choices") || unformat (input, "?")))
531 vlib_cli_command_t *sub, *subs;
533 subs = all_subs (cm, 0, parent_command_index);
534 vec_sort_with_function (subs, vlib_cli_cmp_command);
535 vec_foreach (sub, subs)
536 vlib_cli_output (vm, " %-40U %U",
537 format_vlib_cli_path, sub->path,
538 format_vlib_cli_command_help, sub, /* is_long */ 0);
542 else if (unformat (input, "comment %v", &string))
547 else if (unformat (input, "uncomment %U",
548 unformat_vlib_cli_sub_input, &sub_input))
551 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
552 parent_command_index);
553 unformat_free (&sub_input);
555 else if (unformat (input, "leak-check %U",
556 unformat_vlib_cli_sub_input, &sub_input))
561 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
562 parent_command_index);
563 unformat_free (&sub_input);
565 /* Otherwise, the clib_error_t shows up as a leak... */
568 vlib_cli_output (vm, "%v", error->what);
569 clib_error_free (error);
573 (void) clib_mem_trace_enable_disable (0);
574 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
575 1 /* verbose, i.e. print leaks */ );
577 vlib_cli_output (vm, "%v", leak_report);
578 vec_free (leak_report);
582 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
584 unformat_input_t *si;
585 uword has_sub_commands =
586 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
589 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
592 if (has_sub_commands)
593 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
595 if (has_sub_commands && !error)
596 /* Found valid sub-command. */ ;
598 else if (c->function)
600 clib_error_t *c_error;
602 /* Skip white space for benefit of called function. */
603 unformat_skip_white_space (si);
605 if (unformat (si, "?"))
607 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
612 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
615 ELOG_TYPE_DECLARE (e) =
617 .format = "cli-cmd: %s",
625 ed = ELOG_DATA (&vm->elog_main, e);
626 ed->c = elog_global_id_for_msg_name (c->path);
630 vlib_worker_thread_barrier_sync (vm);
632 c_error = c->function (vm, si, c);
635 vlib_worker_thread_barrier_release (vm);
637 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
640 ELOG_TYPE_DECLARE (e) =
642 .format = "cli-cmd: %s %s",
643 .format_args = "T4T4",
650 ed = ELOG_DATA (&vm->elog_main, e);
651 ed->c = elog_global_id_for_msg_name (c->path);
654 vec_add1 (c_error->what, 0);
655 ed->err = elog_global_id_for_msg_name
656 ((const char *) c_error->what);
657 _vec_len (c_error->what) -= 1;
660 ed->err = elog_global_id_for_msg_name ("OK");
666 clib_error_return (0, "%v: %v", c->path, c_error->what);
667 clib_error_free (c_error);
668 /* Free sub input. */
676 /* Free any previous error. */
677 clib_error_free (error);
681 error = clib_error_return (0, "%v: no sub-commands", c->path);
683 /* Free sub input. */
695 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
696 format_unformat_error, input);
698 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
703 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
704 __attribute__ ((weak));
707 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
711 /* Process CLI input. */
713 vlib_cli_input (vlib_main_t * vm,
714 unformat_input_t * input,
715 vlib_cli_output_function_t * function, uword function_arg)
717 vlib_process_t *cp = vlib_get_current_process (vm);
718 vlib_cli_main_t *cm = &vm->cli_main;
720 vlib_cli_output_function_t *save_function;
721 uword save_function_arg;
724 save_function = cp->output_function;
725 save_function_arg = cp->output_function_arg;
727 cp->output_function = function;
728 cp->output_function_arg = function_arg;
732 vec_reset_length (cm->parse_rule_data);
733 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
736 while (!error && !unformat (input, "%U", unformat_eof));
740 vlib_cli_output (vm, "%v", error->what);
741 vlib_unix_error_report (vm, error);
742 /* clib_error_return is unfortunately often called with a '0'
744 rv = error->code != 0 ? error->code : -1;
745 clib_error_free (error);
748 cp->output_function = save_function;
749 cp->output_function_arg = save_function_arg;
753 /* Output to current CLI connection. */
755 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
757 vlib_process_t *cp = vlib_get_current_process (vm);
762 s = va_format (0, fmt, &va);
765 /* Terminate with \n if not present. */
766 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
769 if ((!cp) || (!cp->output_function))
770 fformat (stdout, "%v", s);
772 cp->output_function (cp->output_function_arg, s, vec_len (s));
777 void *vl_msg_push_heap (void) __attribute__ ((weak));
778 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
780 static clib_error_t *
781 show_memory_usage (vlib_main_t * vm,
782 unformat_input_t * input, vlib_cli_command_t * cmd)
784 int verbose __attribute__ ((unused)) = 0, api_segment = 0;
788 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
790 if (unformat (input, "verbose"))
792 else if (unformat (input, "api-segment"))
796 error = clib_error_return (0, "unknown input `%U'",
797 format_unformat_error, input);
804 void *oldheap = vl_msg_push_heap ();
806 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
807 vl_msg_pop_heap (oldheap);
808 u8 *s = vec_dup (s_in_svm);
810 oldheap = vl_msg_push_heap ();
812 vl_msg_pop_heap (oldheap);
813 vlib_cli_output (vm, "API segment start:");
814 vlib_cli_output (vm, "%v", s);
815 vlib_cli_output (vm, "API segment end:");
819 #if USE_DLMALLOC == 0
823 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
824 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
825 vlib_worker_threads[index].name);
826 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
827 h->vm_alloc_offset_from_header,
829 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
836 uword clib_mem_trace_enable_disable (uword enable);
840 * Note: the foreach_vlib_main cause allocator traffic,
841 * so shut off tracing before we go there...
843 was_enabled = clib_mem_trace_enable_disable (0);
848 struct dlmallinfo mi;
850 mspace = clib_per_cpu_mheaps[index];
852 mi = mspace_mallinfo (mspace);
853 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
854 vlib_worker_threads[index].name);
855 vlib_cli_output (vm, " %U\n", format_page_map,
856 pointer_to_uword (mspace_least_addr(mspace)),
858 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
864 /* Restore the trace flag */
865 clib_mem_trace_enable_disable (was_enabled);
867 #endif /* USE_DLMALLOC */
872 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
873 .path = "show memory",
874 .short_help = "[verbose | api-segment] Show current memory usage",
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,
913 static clib_error_t *
914 enable_disable_memory_trace (vlib_main_t * vm,
915 unformat_input_t * input,
916 vlib_cli_command_t * cmd)
918 unformat_input_t _line_input, *line_input = &_line_input;
924 if (!unformat_user (input, unformat_line_input, line_input))
927 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
929 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
931 else if (unformat (line_input, "api-segment"))
935 unformat_free (line_input);
936 return clib_error_return (0, "invalid input");
939 unformat_free (line_input);
942 oldheap = vl_msg_push_heap ();
943 clib_mem_trace (enable);
945 vl_msg_pop_heap (oldheap);
951 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
952 .path = "memory-trace",
953 .short_help = "on|off [api-segment] Enable/disable memory allocation trace",
954 .function = enable_disable_memory_trace,
959 static clib_error_t *
960 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
961 vlib_cli_command_t * cmd)
963 #if USE_DLMALLOC == 0
964 clib_error_t *error = 0;
968 if (unformat (input, "on"))
972 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
973 mheap = mheap_header(heap);
974 mheap->flags |= MHEAP_FLAG_VALIDATE;
975 // Turn off small object cache because it delays detection of errors
976 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
981 else if (unformat (input, "off"))
985 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
986 mheap = mheap_header(heap);
987 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
988 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
992 else if (unformat (input, "now"))
996 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
997 mheap = mheap_header(heap);
998 mheap_validate(heap);
1001 vlib_cli_output (vm, "heap validation complete");
1006 return clib_error_return (0, "unknown input `%U'",
1007 format_unformat_error, input);
1012 return clib_error_return (0, "unimplemented...");
1013 #endif /* USE_DLMALLOC */
1017 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1018 .path = "test heap-validate",
1019 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1020 .function = test_heap_validate,
1024 static clib_error_t *
1025 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1026 vlib_cli_command_t * cmd)
1028 clib_file_main_t *fm = &file_main;
1031 /* environ(7) does not indicate a header for this */
1032 extern char **environ;
1034 /* Close all known open files */
1036 pool_foreach(f, fm->file_pool,
1038 if (f->file_descriptor > 2)
1039 close(f->file_descriptor);
1044 execve (vm->name, (char **) vm->argv, environ);
1050 VLIB_CLI_COMMAND (restart_cmd,static) = {
1052 .short_help = "restart process",
1053 .function = restart_cmd_fn,
1059 * A trivial test harness to verify the per-process output_function
1060 * is working correcty.
1063 static clib_error_t *
1064 sleep_ten_seconds (vlib_main_t * vm,
1065 unformat_input_t * input, vlib_cli_command_t * cmd)
1068 u16 my_id = rand ();
1070 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1072 for (i = 0; i < 10; i++)
1074 vlib_process_wait_for_event_or_clock (vm, 1.0);
1075 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1077 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1082 VLIB_CLI_COMMAND (ping_command, static) = {
1083 .path = "test sleep",
1084 .function = sleep_ten_seconds,
1085 .short_help = "Sleep for 10 seconds",
1088 #endif /* ifdef TEST_CODE */
1091 vlib_cli_normalize_path (char *input, char **result)
1096 uword index_of_last_space = ~0;
1101 /* Multiple white space -> single space. */
1108 if (l > 0 && s[l - 1] != ' ')
1116 if (l > 0 && s[l - 1] == ' ')
1117 index_of_last_space = vec_len (s);
1124 /* Remove any extra space at end. */
1125 if (l > 0 && s[l - 1] == ' ')
1129 return index_of_last_space;
1133 parent_path_len (char *path)
1136 for (i = vec_len (path) - 1; i >= 0; i--)
1145 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1147 vlib_cli_command_t *p, *c;
1148 vlib_cli_sub_command_t *sub_c;
1152 p = vec_elt_at_index (cm->commands, parent_index);
1153 c = vec_elt_at_index (cm->commands, child_index);
1155 l = parent_path_len (c->path);
1157 sub_name = vec_dup ((u8 *) c->path);
1160 ASSERT (l + 1 < vec_len (c->path));
1162 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1165 if (sub_name[0] == '%')
1168 vlib_cli_sub_rule_t *sr;
1171 vec_delete (sub_name, 1, 0);
1173 if (!p->sub_rule_index_by_name)
1174 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1175 sizeof (sub_name[0]),
1177 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1180 sr = vec_elt_at_index (p->sub_rules, q[0]);
1181 ASSERT (sr->command_index == child_index);
1185 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
1188 clib_error ("reference to unknown rule `%%%v' in path `%v'",
1193 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1194 vec_len (p->sub_rules));
1195 vec_add2 (p->sub_rules, sr, 1);
1196 sr->name = sub_name;
1197 sr->rule_index = q[0];
1198 sr->command_index = child_index;
1202 if (!p->sub_command_index_by_name)
1203 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1204 sizeof (c->path[0]),
1207 /* Check if sub-command has already been created. */
1208 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1210 vec_free (sub_name);
1214 vec_add2 (p->sub_commands, sub_c, 1);
1215 sub_c->index = child_index;
1216 sub_c->name = sub_name;
1217 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1218 sub_c - p->sub_commands);
1220 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1221 for (i = 0; i < vec_len (sub_c->name); i++)
1224 vlib_cli_parse_position_t *pos;
1226 pos = vec_elt_at_index (p->sub_command_positions, i);
1229 pos->min_char = sub_c->name[i];
1231 n = sub_c->name[i] - pos->min_char;
1234 pos->min_char = sub_c->name[i];
1235 vec_insert (pos->bitmaps, -n, 0);
1239 vec_validate (pos->bitmaps, n);
1241 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1246 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1248 uword p_len, pi, *p;
1250 vlib_cli_command_t *c, *parent;
1252 /* Root command (index 0) should have already been added. */
1253 ASSERT (vec_len (cm->commands) > 0);
1255 c = vec_elt_at_index (cm->commands, ci);
1256 p_len = parent_path_len (c->path);
1258 /* No space? Parent is root command. */
1261 add_sub_command (cm, 0, ci);
1266 vec_add (p_path, c->path, p_len);
1268 p = hash_get_mem (cm->command_index_by_path, p_path);
1270 /* Parent exists? */
1273 /* Parent does not exist; create it. */
1274 vec_add2 (cm->commands, parent, 1);
1275 parent->path = p_path;
1276 hash_set_mem (cm->command_index_by_path, parent->path,
1277 parent - cm->commands);
1278 pi = parent - cm->commands;
1286 add_sub_command (cm, pi, ci);
1288 /* Create parent's parent. */
1290 vlib_cli_make_parent (cm, pi);
1294 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1296 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1300 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1302 vlib_cli_main_t *cm = &vm->cli_main;
1303 clib_error_t *error = 0;
1305 char *normalized_path;
1307 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1310 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1312 if (!cm->command_index_by_path)
1313 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1314 sizeof (c->path[0]),
1317 /* See if command already exists with given path. */
1318 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1321 vlib_cli_command_t *d;
1324 d = vec_elt_at_index (cm->commands, ci);
1326 /* If existing command was created via vlib_cli_make_parent
1327 replaced it with callers data. */
1328 if (vlib_cli_command_is_empty (d))
1330 vlib_cli_command_t save = d[0];
1332 ASSERT (!vlib_cli_command_is_empty (c));
1334 /* Copy callers fields. */
1337 /* Save internal fields. */
1338 d->path = save.path;
1339 d->sub_commands = save.sub_commands;
1340 d->sub_command_index_by_name = save.sub_command_index_by_name;
1341 d->sub_command_positions = save.sub_command_positions;
1342 d->sub_rules = save.sub_rules;
1346 clib_error_return (0, "duplicate command name with path %v",
1349 vec_free (normalized_path);
1355 /* Command does not exist: create it. */
1357 /* Add root command (index 0). */
1358 if (vec_len (cm->commands) == 0)
1360 /* Create command with index 0; path is empty string. */
1361 vec_resize (cm->commands, 1);
1364 ci = vec_len (cm->commands);
1365 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1366 vec_add1 (cm->commands, c[0]);
1368 c = vec_elt_at_index (cm->commands, ci);
1369 c->path = normalized_path;
1371 /* Don't inherit from registration. */
1372 c->sub_commands = 0;
1373 c->sub_command_index_by_name = 0;
1374 c->sub_command_positions = 0;
1377 vlib_cli_make_parent (cm, ci);
1382 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1384 vlib_cli_main_t *cm = &vm->cli_main;
1385 vlib_cli_parse_rule_t *r;
1386 clib_error_t *error = 0;
1390 if (!cm->parse_rule_index_by_name)
1391 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1392 sizeof (r->name[0]),
1395 /* Make vector copy of name. */
1396 r_name = format (0, "%s", r_reg->name);
1398 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1401 return clib_error_return (0, "duplicate parse rule name `%s'",
1405 vec_add2 (cm->parse_rules, r, 1);
1407 r->name = (char *) r_name;
1408 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1414 /* $$$ turn back on again someday, maybe */
1415 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1416 vlib_cli_parse_rule_t *
1418 vlib_cli_parse_rule_t *
1420 __attribute__ ((unused))
1422 clib_error_t *error = 0;
1423 vlib_cli_parse_rule_t *r;
1425 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1427 if (!r->name || strlen (r->name) == 0)
1429 error = clib_error_return (0, "parse rule with no name");
1433 error = vlib_cli_register_parse_rule (vm, r);
1444 cli_path_compare (void *a1, void *a2)
1449 if ((vec_len (*s1) < vec_len (*s2)) &&
1450 memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0)
1454 if ((vec_len (*s1) > vec_len (*s2)) &&
1455 memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0)
1458 return vec_cmp (*s1, *s2);
1461 static clib_error_t *
1462 show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1463 vlib_cli_command_t * cmd)
1465 vlib_cli_main_t *cm = &vm->cli_main;
1466 vlib_cli_command_t *cli;
1467 u8 **paths = 0, **s;
1470 vec_foreach (cli, cm->commands)
1471 if (vec_len (cli->path) > 0)
1472 vec_add1 (paths, (u8 *) cli->path);
1474 vec_sort_with_function (paths, cli_path_compare);
1476 vec_foreach (s, paths)
1477 vlib_cli_output (vm, "%v", *s);
1485 VLIB_CLI_COMMAND (show_cli_command, static) = {
1487 .short_help = "Show cli commands",
1488 .function = show_cli_cmd_fn,
1492 static clib_error_t *
1493 elog_trace_command_fn (vlib_main_t * vm,
1494 unformat_input_t * input, vlib_cli_command_t * cmd)
1496 unformat_input_t _line_input, *line_input = &_line_input;
1498 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1499 u32 circuit_node_index;
1501 if (!unformat_user (input, unformat_line_input, line_input))
1504 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1506 if (unformat (line_input, "api"))
1508 else if (unformat (line_input, "dispatch"))
1510 else if (unformat (line_input, "circuit-node %U",
1511 unformat_vlib_node, vm, &circuit_node_index))
1513 else if (unformat (line_input, "cli"))
1515 else if (unformat (line_input, "barrier"))
1517 else if (unformat (line_input, "disable"))
1519 else if (unformat (line_input, "enable"))
1524 unformat_free (line_input);
1526 vm->elog_trace_api_messages = api ? enable : vm->elog_trace_api_messages;
1527 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1528 vm->elog_trace_graph_dispatch = dispatch ?
1529 enable : vm->elog_trace_graph_dispatch;
1530 vm->elog_trace_graph_circuit = circuit ?
1531 enable : vm->elog_trace_graph_circuit;
1532 vlib_worker_threads->barrier_elog_enabled =
1533 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1534 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1537 * Set up start-of-buffer logic-analyzer trigger
1538 * for main loop event logs, which are fairly heavyweight.
1539 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1540 * will fully disable the scheme when the elog buffer fills.
1542 if (dispatch || circuit)
1544 elog_main_t *em = &vm->elog_main;
1546 em->n_total_events_disable_limit =
1547 em->n_total_events + vec_len (em->event_ring);
1552 vlib_cli_output (vm, "Current status:");
1555 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1556 vm->elog_trace_api_messages ? "on" : "off",
1557 vm->elog_trace_cli_commands ? "on" : "off");
1559 (vm, " Barrier sync trace: %s",
1560 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1562 (vm, " Graph Dispatch: %s",
1563 vm->elog_trace_graph_dispatch ? "on" : "off");
1565 (vm, " Graph Circuit: %s",
1566 vm->elog_trace_graph_circuit ? "on" : "off");
1567 if (vm->elog_trace_graph_circuit)
1570 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1576 * Control event logging of api, cli, and thread barrier events
1577 * With no arguments, displays the current trace status.
1578 * Name the event groups you wish to trace or stop tracing.
1582 * elog trace api cli barrier
1583 * elog trace api cli barrier disable
1584 * elog trace dispatch
1585 * elog trace circuit-node ethernet-input
1588 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1591 VLIB_CLI_COMMAND (elog_trace_command, static) =
1593 .path = "elog trace",
1594 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1595 "[circuit-node <name> e.g. ethernet-input][disable]",
1596 .function = elog_trace_command_fn,
1600 static clib_error_t *
1601 vlib_cli_init (vlib_main_t * vm)
1603 vlib_cli_main_t *cm = &vm->cli_main;
1604 clib_error_t *error = 0;
1605 vlib_cli_command_t *cmd;
1607 cmd = cm->cli_command_registrations;
1611 error = vlib_cli_register (vm, cmd);
1614 cmd = cmd->next_cli_command;
1619 VLIB_INIT_FUNCTION (vlib_cli_init);
1622 * fd.io coding-style-patch-verification: ON
1625 * eval: (c-set-style "gnu")