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 int vl_api_set_elog_trace_api_messages (int enable);
48 int vl_api_get_elog_trace_api_messages (void);
50 static void *current_traced_heap;
52 /* Root of all show commands. */
54 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
56 .short_help = "Show commands",
60 /* Root of all clear commands. */
62 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
64 .short_help = "Clear commands",
68 /* Root of all set commands. */
70 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
72 .short_help = "Set commands",
76 /* Root of all test commands. */
78 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
80 .short_help = "Test commands",
84 /* Returns bitmap of commands which match key. */
86 vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
90 vlib_cli_parse_position_t *p;
92 unformat_skip_white_space (input);
98 k = unformat_get_input (input);
112 case UNFORMAT_END_OF_INPUT:
113 /* White space or end of input removes any non-white
114 matches that were before possible. */
115 if (i < vec_len (c->sub_command_positions)
116 && clib_bitmap_count_set_bits (match) > 1)
118 p = vec_elt_at_index (c->sub_command_positions, i);
119 for (n = 0; n < vec_len (p->bitmaps); n++)
120 match = clib_bitmap_andnot (match, p->bitmaps[n]);
125 unformat_put_input (input);
129 if (i >= vec_len (c->sub_command_positions))
132 clib_bitmap_free (match);
136 p = vec_elt_at_index (c->sub_command_positions, i);
137 if (vec_len (p->bitmaps) == 0)
141 if (n < 0 || n >= vec_len (p->bitmaps))
145 match = clib_bitmap_dup (p->bitmaps[n]);
147 match = clib_bitmap_and (match, p->bitmaps[n]);
149 if (clib_bitmap_is_zero (match))
157 /* Looks for string based sub-input formatted { SUB-INPUT }. */
159 unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
161 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
167 c = unformat_get_input (i);
179 /* Put back paren. */
180 if (c != UNFORMAT_END_OF_INPUT)
181 unformat_put_input (i);
183 if (c == '{' && unformat (i, "%v", &s))
185 unformat_init_vector (sub_input, s);
194 static vlib_cli_command_t *
195 get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
197 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
198 return vec_elt_at_index (cm->commands, s->index);
202 unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
204 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
205 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
206 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
207 vlib_cli_main_t *cm = &vm->cli_main;
208 uword *match_bitmap, is_unique, index;
211 vlib_cli_sub_rule_t *sr;
212 vlib_cli_parse_rule_t *r;
213 vec_foreach (sr, c->sub_rules)
216 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
217 vec_add2 (cm->parse_rule_data, d, 1);
218 vec_reset_length (d[0]);
220 d[0] = _vec_resize (d[0],
221 /* length increment */ 1,
223 /* header_bytes */ 0,
224 /* data align */ sizeof (uword));
225 if (unformat_user (i, r->unformat_function, vm, d[0]))
227 *result = vec_elt_at_index (cm->commands, sr->command_index);
233 match_bitmap = vlib_cli_sub_command_match (c, i);
234 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
238 index = clib_bitmap_first_set (match_bitmap);
239 *result = get_sub_command (cm, c, index);
241 clib_bitmap_free (match_bitmap);
247 vlib_cli_cmp_strings (void *a1, void *a2)
249 u8 *c1 = *(u8 **) a1;
250 u8 *c2 = *(u8 **) a2;
252 return vec_cmp (c1, c2);
256 vlib_cli_get_possible_completions (u8 * str)
258 vlib_cli_command_t *c;
259 vlib_cli_sub_command_t *sc;
260 vlib_main_t *vm = vlib_get_main ();
261 vlib_cli_main_t *vcm = &vm->cli_main;
262 uword *match_bitmap = 0;
263 uword index, is_unique, help_next_level;
265 unformat_input_t input;
266 unformat_init_vector (&input, vec_dup (str));
267 c = vec_elt_at_index (vcm->commands, 0);
269 /* remove trailing whitespace, except for one of them */
270 while (vec_len (input.buffer) >= 2 &&
271 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
272 isspace (input.buffer[vec_len (input.buffer) - 2]))
274 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
277 /* if input is empty, directly return list of root commands */
278 if (vec_len (input.buffer) == 0 ||
279 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
281 vec_foreach (sc, c->sub_commands)
283 vec_add1 (result, (u8 *) sc->name);
288 /* add a trailing '?' so that vlib_cli_sub_command_match can find
289 * all commands starting with the input string */
290 vec_add1 (input.buffer, '?');
294 match_bitmap = vlib_cli_sub_command_match (c, &input);
295 /* no match: return no result */
296 if (match_bitmap == 0)
300 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
301 /* unique match: try to step one subcommand level further */
304 /* stop if no more input */
305 if (input.index >= vec_len (input.buffer) - 1)
310 index = clib_bitmap_first_set (match_bitmap);
311 c = get_sub_command (vcm, c, index);
312 clib_bitmap_free (match_bitmap);
315 /* multiple matches: stop here, return all matches */
319 /* remove trailing '?' */
320 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
322 /* if we have a space at the end of input, and a unique match,
323 * autocomplete the next level of subcommands */
324 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
326 clib_bitmap_foreach(index, match_bitmap, {
327 if (help_next_level && is_unique) {
328 c = get_sub_command (vcm, c, index);
329 vec_foreach (sc, c->sub_commands) {
330 vec_add1 (result, (u8*) sc->name);
332 goto done; /* break doesn't work in this macro-loop */
334 sc = &c->sub_commands[index];
335 vec_add1(result, (u8*) sc->name);
340 clib_bitmap_free (match_bitmap);
341 unformat_free (&input);
344 vec_sort_with_function (result, vlib_cli_cmp_strings);
349 format_vlib_cli_command_help (u8 * s, va_list * args)
351 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
352 int is_long = va_arg (*args, int);
353 if (is_long && c->long_help)
354 s = format (s, "%s", c->long_help);
355 else if (c->short_help)
356 s = format (s, "%s", c->short_help);
358 s = format (s, "%v commands", c->path);
363 format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
365 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
366 return format (s, "<%U>", format_c_identifier, r->name);
370 format_vlib_cli_path (u8 * s, va_list * args)
372 u8 *path = va_arg (*args, u8 *);
375 for (i = 0; i < vec_len (path); i++)
381 vec_add1 (s, '<'); /* start of <RULE> */
385 /* _ -> space in rules. */
386 vec_add1 (s, in_rule ? ' ' : '_');
392 vec_add1 (s, '>'); /* end of <RULE> */
399 vec_add1 (s, path[i]);
405 vec_add1 (s, '>'); /* terminate <RULE> */
410 static vlib_cli_command_t *
411 all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
413 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
414 vlib_cli_sub_command_t *sc;
415 vlib_cli_sub_rule_t *sr;
418 vec_add1 (subs, c[0]);
420 vec_foreach (sr, c->sub_rules)
421 subs = all_subs (cm, subs, sr->command_index);
422 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
428 vlib_cli_cmp_rule (void *a1, void *a2)
430 vlib_cli_sub_rule_t *r1 = a1;
431 vlib_cli_sub_rule_t *r2 = a2;
433 return vec_cmp (r1->name, r2->name);
437 vlib_cli_cmp_command (void *a1, void *a2)
439 vlib_cli_command_t *c1 = a1;
440 vlib_cli_command_t *c2 = a2;
442 return vec_cmp (c1->path, c2->path);
445 static clib_error_t *
446 vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
447 vlib_cli_main_t * cm,
448 unformat_input_t * input,
449 uword parent_command_index)
451 vlib_cli_command_t *parent, *c;
452 clib_error_t *error = 0;
453 unformat_input_t sub_input;
455 uword is_main_dispatch = cm == &vm->cli_main;
457 parent = vec_elt_at_index (cm->commands, parent_command_index);
458 if (is_main_dispatch && unformat (input, "help"))
460 uword help_at_end_of_line, i;
462 help_at_end_of_line =
463 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
468 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
471 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
478 /* help SUB-COMMAND => long format help.
479 "help" at end of line: show all commands. */
480 if (!help_at_end_of_line)
481 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
484 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
485 vlib_cli_output (vm, "%v: no sub-commands", c->path);
489 vlib_cli_sub_command_t *sc;
490 vlib_cli_sub_rule_t *sr, *subs;
492 subs = vec_dup (c->sub_rules);
494 /* Add in rules if any. */
495 vec_foreach (sc, c->sub_commands)
497 vec_add2 (subs, sr, 1);
499 sr->command_index = sc->index;
503 vec_sort_with_function (subs, vlib_cli_cmp_rule);
505 for (i = 0; i < vec_len (subs); i++)
507 vlib_cli_command_t *d;
508 vlib_cli_parse_rule_t *r;
510 d = vec_elt_at_index (cm->commands, subs[i].command_index);
512 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
520 format_vlib_cli_parse_rule_name, r,
521 format_vlib_cli_command_help, d, /* is_long */ 0);
526 format_vlib_cli_command_help, d, /* is_long */ 0);
533 else if (is_main_dispatch
534 && (unformat (input, "choices") || unformat (input, "?")))
536 vlib_cli_command_t *sub, *subs;
538 subs = all_subs (cm, 0, parent_command_index);
539 vec_sort_with_function (subs, vlib_cli_cmp_command);
540 vec_foreach (sub, subs)
541 vlib_cli_output (vm, " %-40U %U",
542 format_vlib_cli_path, sub->path,
543 format_vlib_cli_command_help, sub, /* is_long */ 0);
547 else if (unformat (input, "comment %v", &string))
552 else if (unformat (input, "uncomment %U",
553 unformat_vlib_cli_sub_input, &sub_input))
556 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
557 parent_command_index);
558 unformat_free (&sub_input);
560 else if (unformat (input, "leak-check %U",
561 unformat_vlib_cli_sub_input, &sub_input))
564 if (current_traced_heap)
567 oldheap = clib_mem_set_heap (current_traced_heap);
569 clib_mem_set_heap (oldheap);
570 current_traced_heap = 0;
574 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
575 parent_command_index);
576 unformat_free (&sub_input);
578 /* Otherwise, the clib_error_t shows up as a leak... */
581 vlib_cli_output (vm, "%v", error->what);
582 clib_error_free (error);
586 (void) clib_mem_trace_enable_disable (0);
587 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
588 1 /* verbose, i.e. print leaks */ );
590 vlib_cli_output (vm, "%v", leak_report);
591 vec_free (leak_report);
595 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
597 unformat_input_t *si;
598 uword has_sub_commands =
599 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
602 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
605 if (has_sub_commands)
606 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
608 if (has_sub_commands && !error)
609 /* Found valid sub-command. */ ;
611 else if (c->function)
613 clib_error_t *c_error;
615 /* Skip white space for benefit of called function. */
616 unformat_skip_white_space (si);
618 if (unformat (si, "?"))
620 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
625 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
628 ELOG_TYPE_DECLARE (e) =
630 .format = "cli-cmd: %s",
638 ed = ELOG_DATA (&vm->elog_main, e);
639 ed->c = elog_string (&vm->elog_main, c->path);
643 vlib_worker_thread_barrier_sync (vm);
645 c_error = c->function (vm, si, c);
648 vlib_worker_thread_barrier_release (vm);
650 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
653 ELOG_TYPE_DECLARE (e) =
655 .format = "cli-cmd: %s %s",
656 .format_args = "T4T4",
663 ed = ELOG_DATA (&vm->elog_main, e);
664 ed->c = elog_string (&vm->elog_main, c->path);
667 vec_add1 (c_error->what, 0);
668 ed->err = elog_string (&vm->elog_main,
669 (char *) c_error->what);
670 _vec_len (c_error->what) -= 1;
673 ed->err = elog_string (&vm->elog_main, "OK");
679 clib_error_return (0, "%v: %v", c->path, c_error->what);
680 clib_error_free (c_error);
681 /* Free sub input. */
689 /* Free any previous error. */
690 clib_error_free (error);
694 error = clib_error_return (0, "%v: no sub-commands", c->path);
696 /* Free sub input. */
708 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
709 format_unformat_error, input);
711 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
716 void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
717 __attribute__ ((weak));
720 vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
724 /* Process CLI input. */
726 vlib_cli_input (vlib_main_t * vm,
727 unformat_input_t * input,
728 vlib_cli_output_function_t * function, uword function_arg)
730 vlib_process_t *cp = vlib_get_current_process (vm);
731 vlib_cli_main_t *cm = &vm->cli_main;
733 vlib_cli_output_function_t *save_function;
734 uword save_function_arg;
737 save_function = cp->output_function;
738 save_function_arg = cp->output_function_arg;
740 cp->output_function = function;
741 cp->output_function_arg = function_arg;
745 vec_reset_length (cm->parse_rule_data);
746 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
749 while (!error && !unformat (input, "%U", unformat_eof));
753 vlib_cli_output (vm, "%v", error->what);
754 vlib_unix_error_report (vm, error);
755 /* clib_error_return is unfortunately often called with a '0'
757 rv = error->code != 0 ? error->code : -1;
758 clib_error_free (error);
761 cp->output_function = save_function;
762 cp->output_function_arg = save_function_arg;
766 /* Output to current CLI connection. */
768 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
770 vlib_process_t *cp = vlib_get_current_process (vm);
775 s = va_format (0, fmt, &va);
778 /* Terminate with \n if not present. */
779 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
782 if ((!cp) || (!cp->output_function))
783 fformat (stdout, "%v", s);
785 cp->output_function (cp->output_function_arg, s, vec_len (s));
790 void *vl_msg_push_heap (void) __attribute__ ((weak));
792 vl_msg_push_heap (void)
797 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
799 vl_msg_pop_heap (void *oldheap)
803 void *vlib_stats_push_heap (void *) __attribute__ ((weak));
805 vlib_stats_push_heap (void *notused)
810 static clib_error_t *
811 show_memory_usage (vlib_main_t * vm,
812 unformat_input_t * input, vlib_cli_command_t * cmd)
814 int verbose __attribute__ ((unused)) = 0;
815 int api_segment = 0, stats_segment = 0, main_heap = 0;
818 uword clib_mem_trace_enable_disable (uword enable);
822 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
824 if (unformat (input, "verbose"))
826 else if (unformat (input, "api-segment"))
828 else if (unformat (input, "stats-segment"))
830 else if (unformat (input, "main-heap"))
834 error = clib_error_return (0, "unknown input `%U'",
835 format_unformat_error, input);
840 if ((api_segment + stats_segment + main_heap) == 0)
841 return clib_error_return
842 (0, "Please supply one of api-segment, stats-segment or main-heap");
846 void *oldheap = vl_msg_push_heap ();
847 was_enabled = clib_mem_trace_enable_disable (0);
849 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
850 vl_msg_pop_heap (oldheap);
851 u8 *s = vec_dup (s_in_svm);
853 oldheap = vl_msg_push_heap ();
855 clib_mem_trace_enable_disable (was_enabled);
856 vl_msg_pop_heap (oldheap);
857 vlib_cli_output (vm, "API segment");
858 vlib_cli_output (vm, "%v", s);
863 void *oldheap = vlib_stats_push_heap (0);
864 was_enabled = clib_mem_trace_enable_disable (0);
866 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
868 clib_mem_set_heap (oldheap);
869 u8 *s = vec_dup (s_in_svm);
871 oldheap = vlib_stats_push_heap (0);
875 clib_mem_trace_enable_disable (was_enabled);
876 clib_mem_set_heap (oldheap);
878 vlib_cli_output (vm, "Stats segment");
879 vlib_cli_output (vm, "%v", s);
883 #if USE_DLMALLOC == 0
887 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
888 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
889 vlib_worker_threads[index].name);
890 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
891 h->vm_alloc_offset_from_header,
893 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
903 * Note: the foreach_vlib_main causes allocator traffic,
904 * so shut off tracing before we go there...
906 was_enabled = clib_mem_trace_enable_disable (0);
911 struct dlmallinfo mi;
913 mspace = clib_per_cpu_mheaps[index];
915 mi = mspace_mallinfo (mspace);
916 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
917 vlib_worker_threads[index].name);
918 vlib_cli_output (vm, " %U\n", format_page_map,
919 pointer_to_uword (mspace_least_addr(mspace)),
921 vlib_cli_output (vm, " %U\n", format_mheap,
922 clib_per_cpu_mheaps[index],
928 /* Restore the trace flag */
929 clib_mem_trace_enable_disable (was_enabled);
932 #endif /* USE_DLMALLOC */
937 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
938 .path = "show memory",
939 .short_help = "show memory [api-segment][stats-segment][verbose]",
940 .function = show_memory_usage,
944 static clib_error_t *
945 show_cpu (vlib_main_t * vm, unformat_input_t * input,
946 vlib_cli_command_t * cmd)
948 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
949 _("Model name", "%U", format_cpu_model_name);
950 _("Microarch model (family)", "%U", format_cpu_uarch);
951 _("Flags", "%U", format_cpu_flags);
952 _("Base frequency", "%.2f GHz",
953 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
959 * Displays various information about the CPU.
962 * @cliexstart{show cpu}
963 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
964 * Microarchitecture: Broadwell (Broadwell-EP/EX)
965 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
966 * Base Frequency: 3.20 GHz
970 VLIB_CLI_COMMAND (show_cpu_command, static) = {
972 .short_help = "Show cpu information",
973 .function = show_cpu,
977 static clib_error_t *
978 enable_disable_memory_trace (vlib_main_t * vm,
979 unformat_input_t * input,
980 vlib_cli_command_t * cmd)
982 unformat_input_t _line_input, *line_input = &_line_input;
985 int stats_segment = 0;
989 if (!unformat_user (input, unformat_line_input, line_input))
992 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
994 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
996 else if (unformat (line_input, "api-segment"))
998 else if (unformat (line_input, "stats-segment"))
1000 else if (unformat (line_input, "main-heap"))
1004 unformat_free (line_input);
1005 return clib_error_return (0, "invalid input");
1008 unformat_free (line_input);
1010 if ((api_segment + stats_segment + main_heap + (enable == 0)) == 0)
1012 return clib_error_return
1013 (0, "Need one of main-heap, stats-segment or api-segment");
1016 /* Turn off current trace, if any */
1017 if (current_traced_heap)
1020 oldheap = clib_mem_set_heap (current_traced_heap);
1022 clib_mem_set_heap (oldheap);
1023 current_traced_heap = 0;
1032 oldheap = vl_msg_push_heap ();
1033 current_traced_heap = clib_mem_get_heap ();
1035 vl_msg_pop_heap (oldheap);
1042 oldheap = vlib_stats_push_heap (0);
1043 current_traced_heap = clib_mem_get_heap ();
1044 clib_mem_trace (stats_segment);
1045 /* We don't want to call vlib_stats_pop_heap... */
1047 clib_mem_set_heap (oldheap);
1053 current_traced_heap = clib_mem_get_heap ();
1054 clib_mem_trace (main_heap);
1061 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1062 .path = "memory-trace",
1063 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n",
1064 .function = enable_disable_memory_trace,
1069 static clib_error_t *
1070 test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
1071 vlib_cli_command_t * cmd)
1073 #if USE_DLMALLOC == 0
1074 clib_error_t *error = 0;
1078 if (unformat (input, "on"))
1082 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1083 mheap = mheap_header(heap);
1084 mheap->flags |= MHEAP_FLAG_VALIDATE;
1085 // Turn off small object cache because it delays detection of errors
1086 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
1091 else if (unformat (input, "off"))
1095 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1096 mheap = mheap_header(heap);
1097 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
1098 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
1102 else if (unformat (input, "now"))
1106 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
1107 mheap = mheap_header(heap);
1108 mheap_validate(heap);
1111 vlib_cli_output (vm, "heap validation complete");
1116 return clib_error_return (0, "unknown input `%U'",
1117 format_unformat_error, input);
1122 return clib_error_return (0, "unimplemented...");
1123 #endif /* USE_DLMALLOC */
1127 VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1128 .path = "test heap-validate",
1129 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1130 .function = test_heap_validate,
1134 static clib_error_t *
1135 restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1136 vlib_cli_command_t * cmd)
1138 clib_file_main_t *fm = &file_main;
1141 /* environ(7) does not indicate a header for this */
1142 extern char **environ;
1144 /* Close all known open files */
1146 pool_foreach(f, fm->file_pool,
1148 if (f->file_descriptor > 2)
1149 close(f->file_descriptor);
1154 execve (vm->name, (char **) vm->argv, environ);
1160 VLIB_CLI_COMMAND (restart_cmd,static) = {
1162 .short_help = "restart process",
1163 .function = restart_cmd_fn,
1169 * A trivial test harness to verify the per-process output_function
1170 * is working correcty.
1173 static clib_error_t *
1174 sleep_ten_seconds (vlib_main_t * vm,
1175 unformat_input_t * input, vlib_cli_command_t * cmd)
1178 u16 my_id = rand ();
1180 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1182 for (i = 0; i < 10; i++)
1184 vlib_process_wait_for_event_or_clock (vm, 1.0);
1185 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1187 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1192 VLIB_CLI_COMMAND (ping_command, static) = {
1193 .path = "test sleep",
1194 .function = sleep_ten_seconds,
1195 .short_help = "Sleep for 10 seconds",
1198 #endif /* ifdef TEST_CODE */
1201 vlib_cli_normalize_path (char *input, char **result)
1206 uword index_of_last_space = ~0;
1211 /* Multiple white space -> single space. */
1218 if (l > 0 && s[l - 1] != ' ')
1226 if (l > 0 && s[l - 1] == ' ')
1227 index_of_last_space = vec_len (s);
1234 /* Remove any extra space at end. */
1235 if (l > 0 && s[l - 1] == ' ')
1239 return index_of_last_space;
1243 parent_path_len (char *path)
1246 for (i = vec_len (path) - 1; i >= 0; i--)
1255 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1257 vlib_cli_command_t *p, *c;
1258 vlib_cli_sub_command_t *sub_c;
1262 p = vec_elt_at_index (cm->commands, parent_index);
1263 c = vec_elt_at_index (cm->commands, child_index);
1265 l = parent_path_len (c->path);
1267 sub_name = vec_dup ((u8 *) c->path);
1270 ASSERT (l + 1 < vec_len (c->path));
1272 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1275 if (sub_name[0] == '%')
1278 vlib_cli_sub_rule_t *sr;
1281 vec_delete (sub_name, 1, 0);
1283 if (!p->sub_rule_index_by_name)
1284 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1285 sizeof (sub_name[0]),
1287 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1290 sr = vec_elt_at_index (p->sub_rules, q[0]);
1291 ASSERT (sr->command_index == child_index);
1295 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
1298 clib_error ("reference to unknown rule `%%%v' in path `%v'",
1303 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1304 vec_len (p->sub_rules));
1305 vec_add2 (p->sub_rules, sr, 1);
1306 sr->name = sub_name;
1307 sr->rule_index = q[0];
1308 sr->command_index = child_index;
1312 if (!p->sub_command_index_by_name)
1313 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1314 sizeof (c->path[0]),
1317 /* Check if sub-command has already been created. */
1318 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1320 vec_free (sub_name);
1324 vec_add2 (p->sub_commands, sub_c, 1);
1325 sub_c->index = child_index;
1326 sub_c->name = sub_name;
1327 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1328 sub_c - p->sub_commands);
1330 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1331 for (i = 0; i < vec_len (sub_c->name); i++)
1334 vlib_cli_parse_position_t *pos;
1336 pos = vec_elt_at_index (p->sub_command_positions, i);
1339 pos->min_char = sub_c->name[i];
1341 n = sub_c->name[i] - pos->min_char;
1344 pos->min_char = sub_c->name[i];
1345 vec_insert (pos->bitmaps, -n, 0);
1349 vec_validate (pos->bitmaps, n);
1351 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1356 vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1358 uword p_len, pi, *p;
1360 vlib_cli_command_t *c, *parent;
1362 /* Root command (index 0) should have already been added. */
1363 ASSERT (vec_len (cm->commands) > 0);
1365 c = vec_elt_at_index (cm->commands, ci);
1366 p_len = parent_path_len (c->path);
1368 /* No space? Parent is root command. */
1371 add_sub_command (cm, 0, ci);
1376 vec_add (p_path, c->path, p_len);
1378 p = hash_get_mem (cm->command_index_by_path, p_path);
1380 /* Parent exists? */
1383 /* Parent does not exist; create it. */
1384 vec_add2 (cm->commands, parent, 1);
1385 parent->path = p_path;
1386 hash_set_mem (cm->command_index_by_path, parent->path,
1387 parent - cm->commands);
1388 pi = parent - cm->commands;
1396 add_sub_command (cm, pi, ci);
1398 /* Create parent's parent. */
1400 vlib_cli_make_parent (cm, pi);
1404 vlib_cli_command_is_empty (vlib_cli_command_t * c)
1406 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1410 vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
1412 vlib_cli_main_t *cm = &vm->cli_main;
1413 clib_error_t *error = 0;
1415 char *normalized_path;
1417 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1420 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1422 if (!cm->command_index_by_path)
1423 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1424 sizeof (c->path[0]),
1427 /* See if command already exists with given path. */
1428 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1431 vlib_cli_command_t *d;
1434 d = vec_elt_at_index (cm->commands, ci);
1436 /* If existing command was created via vlib_cli_make_parent
1437 replaced it with callers data. */
1438 if (vlib_cli_command_is_empty (d))
1440 vlib_cli_command_t save = d[0];
1442 ASSERT (!vlib_cli_command_is_empty (c));
1444 /* Copy callers fields. */
1447 /* Save internal fields. */
1448 d->path = save.path;
1449 d->sub_commands = save.sub_commands;
1450 d->sub_command_index_by_name = save.sub_command_index_by_name;
1451 d->sub_command_positions = save.sub_command_positions;
1452 d->sub_rules = save.sub_rules;
1456 clib_error_return (0, "duplicate command name with path %v",
1459 vec_free (normalized_path);
1465 /* Command does not exist: create it. */
1467 /* Add root command (index 0). */
1468 if (vec_len (cm->commands) == 0)
1470 /* Create command with index 0; path is empty string. */
1471 vec_resize (cm->commands, 1);
1474 ci = vec_len (cm->commands);
1475 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1476 vec_add1 (cm->commands, c[0]);
1478 c = vec_elt_at_index (cm->commands, ci);
1479 c->path = normalized_path;
1481 /* Don't inherit from registration. */
1482 c->sub_commands = 0;
1483 c->sub_command_index_by_name = 0;
1484 c->sub_command_positions = 0;
1487 vlib_cli_make_parent (cm, ci);
1492 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1494 vlib_cli_main_t *cm = &vm->cli_main;
1495 vlib_cli_parse_rule_t *r;
1496 clib_error_t *error = 0;
1500 if (!cm->parse_rule_index_by_name)
1501 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1502 sizeof (r->name[0]),
1505 /* Make vector copy of name. */
1506 r_name = format (0, "%s", r_reg->name);
1508 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1511 return clib_error_return (0, "duplicate parse rule name `%s'",
1515 vec_add2 (cm->parse_rules, r, 1);
1517 r->name = (char *) r_name;
1518 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1524 /* $$$ turn back on again someday, maybe */
1525 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1526 vlib_cli_parse_rule_t *
1528 vlib_cli_parse_rule_t *
1530 __attribute__ ((unused))
1532 clib_error_t *error = 0;
1533 vlib_cli_parse_rule_t *r;
1535 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1537 if (!r->name || strlen (r->name) == 0)
1539 error = clib_error_return (0, "parse rule with no name");
1543 error = vlib_cli_register_parse_rule (vm, r);
1554 cli_path_compare (void *a1, void *a2)
1559 if ((vec_len (*s1) < vec_len (*s2)) &&
1560 memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0)
1564 if ((vec_len (*s1) > vec_len (*s2)) &&
1565 memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0)
1568 return vec_cmp (*s1, *s2);
1571 static clib_error_t *
1572 show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1573 vlib_cli_command_t * cmd)
1575 vlib_cli_main_t *cm = &vm->cli_main;
1576 vlib_cli_command_t *cli;
1577 u8 **paths = 0, **s;
1580 vec_foreach (cli, cm->commands)
1581 if (vec_len (cli->path) > 0)
1582 vec_add1 (paths, (u8 *) cli->path);
1584 vec_sort_with_function (paths, cli_path_compare);
1586 vec_foreach (s, paths)
1587 vlib_cli_output (vm, "%v", *s);
1595 VLIB_CLI_COMMAND (show_cli_command, static) = {
1597 .short_help = "Show cli commands",
1598 .function = show_cli_cmd_fn,
1602 static clib_error_t *
1603 elog_trace_command_fn (vlib_main_t * vm,
1604 unformat_input_t * input, vlib_cli_command_t * cmd)
1606 unformat_input_t _line_input, *line_input = &_line_input;
1608 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1609 u32 circuit_node_index;
1611 if (!unformat_user (input, unformat_line_input, line_input))
1614 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1616 if (unformat (line_input, "api"))
1618 else if (unformat (line_input, "dispatch"))
1620 else if (unformat (line_input, "circuit-node %U",
1621 unformat_vlib_node, vm, &circuit_node_index))
1623 else if (unformat (line_input, "cli"))
1625 else if (unformat (line_input, "barrier"))
1627 else if (unformat (line_input, "disable"))
1629 else if (unformat (line_input, "enable"))
1634 unformat_free (line_input);
1636 vl_api_set_elog_trace_api_messages
1637 (api ? enable : vl_api_get_elog_trace_api_messages ());
1638 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1639 vm->elog_trace_graph_dispatch = dispatch ?
1640 enable : vm->elog_trace_graph_dispatch;
1641 vm->elog_trace_graph_circuit = circuit ?
1642 enable : vm->elog_trace_graph_circuit;
1643 vlib_worker_threads->barrier_elog_enabled =
1644 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1645 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1648 * Set up start-of-buffer logic-analyzer trigger
1649 * for main loop event logs, which are fairly heavyweight.
1650 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1651 * will fully disable the scheme when the elog buffer fills.
1653 if (dispatch || circuit)
1655 elog_main_t *em = &vm->elog_main;
1657 em->n_total_events_disable_limit =
1658 em->n_total_events + vec_len (em->event_ring);
1663 vlib_cli_output (vm, "Current status:");
1666 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1667 vl_api_get_elog_trace_api_messages ()? "on" : "off",
1668 vm->elog_trace_cli_commands ? "on" : "off");
1670 (vm, " Barrier sync trace: %s",
1671 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1673 (vm, " Graph Dispatch: %s",
1674 vm->elog_trace_graph_dispatch ? "on" : "off");
1676 (vm, " Graph Circuit: %s",
1677 vm->elog_trace_graph_circuit ? "on" : "off");
1678 if (vm->elog_trace_graph_circuit)
1681 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
1687 * Control event logging of api, cli, and thread barrier events
1688 * With no arguments, displays the current trace status.
1689 * Name the event groups you wish to trace or stop tracing.
1693 * elog trace api cli barrier
1694 * elog trace api cli barrier disable
1695 * elog trace dispatch
1696 * elog trace circuit-node ethernet-input
1699 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1702 VLIB_CLI_COMMAND (elog_trace_command, static) =
1704 .path = "elog trace",
1705 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1706 "[circuit-node <name> e.g. ethernet-input][disable]",
1707 .function = elog_trace_command_fn,
1711 static clib_error_t *
1712 suspend_command_fn (vlib_main_t * vm,
1713 unformat_input_t * input, vlib_cli_command_t * cmd)
1715 vlib_process_suspend (vm, 30e-3);
1720 VLIB_CLI_COMMAND (suspend_command, static) =
1723 .short_help = "suspend debug CLI for 30ms",
1724 .function = suspend_command_fn,
1728 static clib_error_t *
1729 vlib_cli_init (vlib_main_t * vm)
1731 vlib_cli_main_t *cm = &vm->cli_main;
1732 clib_error_t *error = 0;
1733 vlib_cli_command_t *cmd;
1735 cmd = cm->cli_command_registrations;
1739 error = vlib_cli_register (vm, cmd);
1742 cmd = cmd->next_cli_command;
1747 VLIB_INIT_FUNCTION (vlib_cli_init);
1750 * fd.io coding-style-patch-verification: ON
1753 * eval: (c-set-style "gnu")