FD.io VPP  v21.01.1
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  * cli.c: command line interface
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39 
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 #include <vppinfra/callback.h>
43 #include <vppinfra/cpu.h>
44 #include <vppinfra/elog.h>
45 #include <unistd.h>
46 #include <ctype.h>
47 
48 /** \file src/vlib/cli.c Debug CLI Implementation
49  */
50 
51 int vl_api_set_elog_trace_api_messages (int enable);
53 
54 static void *current_traced_heap;
55 
56 /* Root of all show commands. */
57 /* *INDENT-OFF* */
58 VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
59  .path = "show",
60  .short_help = "Show commands",
61 };
62 /* *INDENT-ON* */
63 
64 /* Root of all clear commands. */
65 /* *INDENT-OFF* */
66 VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
67  .path = "clear",
68  .short_help = "Clear commands",
69 };
70 /* *INDENT-ON* */
71 
72 /* Root of all set commands. */
73 /* *INDENT-OFF* */
74 VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
75  .path = "set",
76  .short_help = "Set commands",
77 };
78 /* *INDENT-ON* */
79 
80 /* Root of all test commands. */
81 /* *INDENT-OFF* */
82 VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
83  .path = "test",
84  .short_help = "Test commands",
85 };
86 /* *INDENT-ON* */
87 
88 /* Returns bitmap of commands which match key. */
89 static uword *
91 {
92  int i, n;
93  uword *match = 0;
95 
97 
98  for (i = 0;; i++)
99  {
100  uword k;
101 
102  k = unformat_get_input (input);
103  switch (k)
104  {
105  case 'a' ... 'z':
106  case 'A' ... 'Z':
107  case '0' ... '9':
108  case '-':
109  case '_':
110  break;
111 
112  case ' ':
113  case '\t':
114  case '\r':
115  case '\n':
117  /* White space or end of input removes any non-white
118  matches that were before possible. */
119  if (i < vec_len (c->sub_command_positions)
120  && clib_bitmap_count_set_bits (match) > 1)
121  {
123  for (n = 0; n < vec_len (p->bitmaps); n++)
124  match = clib_bitmap_andnot (match, p->bitmaps[n]);
125  }
126  goto done;
127 
128  default:
129  unformat_put_input (input);
130  goto done;
131  }
132 
133  if (i >= vec_len (c->sub_command_positions))
134  {
135  no_match:
136  clib_bitmap_free (match);
137  return 0;
138  }
139 
141  if (vec_len (p->bitmaps) == 0)
142  goto no_match;
143 
144  n = k - p->min_char;
145  if (n < 0 || n >= vec_len (p->bitmaps))
146  goto no_match;
147 
148  if (i == 0)
149  match = clib_bitmap_dup (p->bitmaps[n]);
150  else
151  match = clib_bitmap_and (match, p->bitmaps[n]);
152 
153  if (clib_bitmap_is_zero (match))
154  goto no_match;
155  }
156 
157 done:
158  return match;
159 }
160 
161 /* Looks for string based sub-input formatted { SUB-INPUT }. */
162 uword
164 {
165  unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
166  u8 *s;
167  uword c;
168 
169  while (1)
170  {
171  c = unformat_get_input (i);
172  switch (c)
173  {
174  case ' ':
175  case '\t':
176  case '\n':
177  case '\r':
178  case '\f':
179  break;
180 
181  case '{':
182  default:
183  /* Put back paren. */
184  if (c != UNFORMAT_END_OF_INPUT)
185  unformat_put_input (i);
186 
187  if (c == '{' && unformat (i, "%v", &s))
188  {
189  unformat_init_vector (sub_input, s);
190  return 1;
191  }
192  return 0;
193  }
194  }
195  return 0;
196 }
197 
198 static vlib_cli_command_t *
200 {
202  return vec_elt_at_index (cm->commands, s->index);
203 }
204 
205 static uword
207 {
208  vlib_main_t *vm = va_arg (*args, vlib_main_t *);
209  vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
210  vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
211  vlib_cli_main_t *cm = &vm->cli_main;
212  uword *match_bitmap, is_unique, index;
213 
214  match_bitmap = vlib_cli_sub_command_match (c, i);
215  is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
216  index = ~0;
217  if (is_unique)
218  {
219  index = clib_bitmap_first_set (match_bitmap);
220  *result = get_sub_command (cm, c, index);
221  }
222  clib_bitmap_free (match_bitmap);
223 
224  return is_unique;
225 }
226 
227 static int
228 vlib_cli_cmp_strings (void *a1, void *a2)
229 {
230  u8 *c1 = *(u8 **) a1;
231  u8 *c2 = *(u8 **) a2;
232 
233  return vec_cmp (c1, c2);
234 }
235 
236 u8 **
238 {
241  vlib_main_t *vm = vlib_get_main ();
242  vlib_cli_main_t *vcm = &vm->cli_main;
243  uword *match_bitmap = 0;
244  uword index, is_unique, help_next_level;
245  u8 **result = 0;
246  unformat_input_t input;
247  unformat_init_vector (&input, vec_dup (str));
248  c = vec_elt_at_index (vcm->commands, 0);
249 
250  /* remove trailing whitespace, except for one of them */
251  while (vec_len (input.buffer) >= 2 &&
252  isspace (input.buffer[vec_len (input.buffer) - 1]) &&
253  isspace (input.buffer[vec_len (input.buffer) - 2]))
254  {
255  vec_del1 (input.buffer, vec_len (input.buffer) - 1);
256  }
257 
258  /* if input is empty, directly return list of root commands */
259  if (vec_len (input.buffer) == 0 ||
260  (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
261  {
262  vec_foreach (sc, c->sub_commands)
263  {
264  vec_add1 (result, (u8 *) sc->name);
265  }
266  goto done;
267  }
268 
269  /* add a trailing '?' so that vlib_cli_sub_command_match can find
270  * all commands starting with the input string */
271  vec_add1 (input.buffer, '?');
272 
273  while (1)
274  {
275  match_bitmap = vlib_cli_sub_command_match (c, &input);
276  /* no match: return no result */
277  if (match_bitmap == 0)
278  {
279  goto done;
280  }
281  is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
282  /* unique match: try to step one subcommand level further */
283  if (is_unique)
284  {
285  /* stop if no more input */
286  if (input.index >= vec_len (input.buffer) - 1)
287  {
288  break;
289  }
290 
291  index = clib_bitmap_first_set (match_bitmap);
292  c = get_sub_command (vcm, c, index);
293  clib_bitmap_free (match_bitmap);
294  continue;
295  }
296  /* multiple matches: stop here, return all matches */
297  break;
298  }
299 
300  /* remove trailing '?' */
301  vec_del1 (input.buffer, vec_len (input.buffer) - 1);
302 
303  /* if we have a space at the end of input, and a unique match,
304  * autocomplete the next level of subcommands */
305  help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
306  /* *INDENT-OFF* */
307  clib_bitmap_foreach (index, match_bitmap) {
308  if (help_next_level && is_unique) {
309  c = get_sub_command (vcm, c, index);
310  vec_foreach (sc, c->sub_commands) {
311  vec_add1 (result, (u8*) sc->name);
312  }
313  goto done; /* break doesn't work in this macro-loop */
314  }
315  sc = &c->sub_commands[index];
316  vec_add1(result, (u8*) sc->name);
317  }
318  /* *INDENT-ON* */
319 
320 done:
321  clib_bitmap_free (match_bitmap);
322  unformat_free (&input);
323 
324  if (result)
326  return result;
327 }
328 
329 static u8 *
330 format_vlib_cli_command_help (u8 * s, va_list * args)
331 {
332  vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
333  int is_long = va_arg (*args, int);
334  if (is_long && c->long_help)
335  s = format (s, "%s", c->long_help);
336  else if (c->short_help)
337  s = format (s, "%s", c->short_help);
338  else
339  s = format (s, "%v commands", c->path);
340  return s;
341 }
342 
343 static u8 *
344 format_vlib_cli_path (u8 * s, va_list * args)
345 {
346  u8 *path = va_arg (*args, u8 *);
347 
348  s = format (s, "%v", path);
349 
350  return s;
351 }
352 
353 static vlib_cli_command_t *
355 {
356  vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
358 
359  if (c->function)
360  vec_add1 (subs, c[0]);
361 
362  vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
363 
364  return subs;
365 }
366 
367 static int
368 vlib_cli_cmp_rule (void *a1, void *a2)
369 {
370  vlib_cli_sub_rule_t *r1 = a1;
371  vlib_cli_sub_rule_t *r2 = a2;
372 
373  return vec_cmp (r1->name, r2->name);
374 }
375 
376 static int
377 vlib_cli_cmp_command (void *a1, void *a2)
378 {
379  vlib_cli_command_t *c1 = a1;
380  vlib_cli_command_t *c2 = a2;
381 
382  return vec_cmp (c1->path, c2->path);
383 }
384 
385 static clib_error_t *
388  unformat_input_t * input,
389  uword parent_command_index)
390 {
391  vlib_cli_command_t *parent, *c;
392  clib_error_t *error = 0;
393  unformat_input_t sub_input;
394  u8 *string;
395  uword is_main_dispatch = cm == &vm->cli_main;
396 
397  parent = vec_elt_at_index (cm->commands, parent_command_index);
398  if (is_main_dispatch && unformat (input, "help"))
399  {
400  uword help_at_end_of_line, i;
401 
402  help_at_end_of_line =
404  while (1)
405  {
406  c = parent;
407  if (unformat_user
408  (input, unformat_vlib_cli_sub_command, vm, c, &parent))
409  ;
410 
411  else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
412  goto unknown;
413 
414  else
415  break;
416  }
417 
418  /* help SUB-COMMAND => long format help.
419  "help" at end of line: show all commands. */
420  if (!help_at_end_of_line)
422  /* is_long */ 1);
423 
424  else if (vec_len (c->sub_commands) == 0)
425  vlib_cli_output (vm, "%v: no sub-commands", c->path);
426 
427  else
428  {
429  vlib_cli_sub_rule_t *sr, *subs = 0;
431 
432  vec_foreach (sc, c->sub_commands)
433  {
434  vec_add2 (subs, sr, 1);
435  sr->name = sc->name;
436  sr->command_index = sc->index;
437  sr->rule_index = ~0;
438  }
439 
441 
442  for (i = 0; i < vec_len (subs); i++)
443  {
445 
446  d = vec_elt_at_index (cm->commands, subs[i].command_index);
448  (vm, " %-30v %U", subs[i].name,
449  format_vlib_cli_command_help, d, /* is_long */ 0);
450  }
451 
452  vec_free (subs);
453  }
454  }
455 
456  else if (is_main_dispatch
457  && (unformat (input, "choices") || unformat (input, "?")))
458  {
459  vlib_cli_command_t *sub, *subs;
460 
461  subs = all_subs (cm, 0, parent_command_index);
463  vec_foreach (sub, subs)
464  vlib_cli_output (vm, " %-40U %U",
466  format_vlib_cli_command_help, sub, /* is_long */ 0);
467  vec_free (subs);
468  }
469 
470  else if (unformat (input, "comment %v", &string))
471  {
472  vec_free (string);
473  }
474 
475  else if (unformat (input, "uncomment %U",
476  unformat_vlib_cli_sub_input, &sub_input))
477  {
478  error =
479  vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
480  parent_command_index);
481  unformat_free (&sub_input);
482  }
483  else if (unformat (input, "leak-check %U",
484  unformat_vlib_cli_sub_input, &sub_input))
485  {
486  u8 *leak_report;
488  {
489  void *oldheap;
491  clib_mem_trace (0);
492  clib_mem_set_heap (oldheap);
494  }
495  clib_mem_trace (1);
496  error =
497  vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
498  parent_command_index);
499  unformat_free (&sub_input);
500 
501  /* Otherwise, the clib_error_t shows up as a leak... */
502  if (error)
503  {
504  vlib_cli_output (vm, "%v", error->what);
505  clib_error_free (error);
506  error = 0;
507  }
508 
510  leak_report = format (0, "%U", format_clib_mem_heap, 0,
511  1 /* verbose, i.e. print leaks */ );
512  clib_mem_trace (0);
513  vlib_cli_output (vm, "%v", leak_report);
514  vec_free (leak_report);
515  }
516 
517  else
518  if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
519  {
521  uword has_sub_commands =
522  vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
523 
524  si = input;
525  if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
526  si = &sub_input;
527 
528  if (has_sub_commands)
529  error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
530 
531  if (has_sub_commands && !error)
532  /* Found valid sub-command. */ ;
533 
534  else if (c->function)
535  {
536  clib_error_t *c_error;
537 
538  /* Skip white space for benefit of called function. */
540 
541  if (unformat (si, "?"))
542  {
543  vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
544  0);
545  }
546  else
547  {
549  {
550  /* *INDENT-OFF* */
551  ELOG_TYPE_DECLARE (e) =
552  {
553  .format = "cli-cmd: %s",
554  .format_args = "T4",
555  };
556  /* *INDENT-ON* */
557  struct
558  {
559  u32 c;
560  } *ed;
561  ed = ELOG_DATA (&vm->elog_main, e);
562  ed->c = elog_string (&vm->elog_main, "%v", c->path);
563  }
564 
565  if (!c->is_mp_safe)
567  if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
569  c - cm->commands, 0 /* before */ );
570 
571  c->hit_counter++;
572  c_error = c->function (vm, si, c);
573 
574  if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
576  c - cm->commands, 1 /* after */ );
577  if (!c->is_mp_safe)
579 
581  {
582  /* *INDENT-OFF* */
583  ELOG_TYPE_DECLARE (e) =
584  {
585  .format = "cli-cmd: %s %s",
586  .format_args = "T4T4",
587  };
588  /* *INDENT-ON* */
589  struct
590  {
591  u32 c, err;
592  } *ed;
593  ed = ELOG_DATA (&vm->elog_main, e);
594  ed->c = elog_string (&vm->elog_main, "%v", c->path);
595  if (c_error)
596  {
597  vec_add1 (c_error->what, 0);
598  ed->err =
599  elog_string (&vm->elog_main, (char *) c_error->what);
600  _vec_len (c_error->what) -= 1;
601  }
602  else
603  ed->err = elog_string (&vm->elog_main, "OK");
604  }
605 
606  if (c_error)
607  {
608  error =
609  clib_error_return (0, "%v: %v", c->path, c_error->what);
610  clib_error_free (c_error);
611  /* Free sub input. */
612  if (si != input)
613  unformat_free (si);
614 
615  return error;
616  }
617  }
618 
619  /* Free any previous error. */
620  clib_error_free (error);
621  }
622 
623  else if (!error)
624  error = clib_error_return (0, "%v: no sub-commands", c->path);
625 
626  /* Free sub input. */
627  if (si != input)
628  unformat_free (si);
629  }
630 
631  else
632  goto unknown;
633 
634  return error;
635 
636 unknown:
637  if (parent->path)
638  return clib_error_return (0, "%v: unknown input `%U'", parent->path,
639  format_unformat_error, input);
640  else
641  return clib_error_return (0, "unknown input `%U'", format_unformat_error,
642  input);
643 }
644 
645 
647  __attribute__ ((weak));
648 
649 void
651 {
652 }
653 
654 /* Process CLI input. */
655 int
657  unformat_input_t * input,
658  vlib_cli_output_function_t * function, uword function_arg)
659 {
662  vlib_cli_output_function_t *save_function;
663  uword save_function_arg;
664  int rv = 0;
665 
666  save_function = cp->output_function;
667  save_function_arg = cp->output_function_arg;
668 
669  cp->output_function = function;
670  cp->output_function_arg = function_arg;
671 
672  do
673  {
674  error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
675  /* parent */ 0);
676  }
677  while (!error && !unformat (input, "%U", unformat_eof));
678 
679  if (error)
680  {
681  vlib_cli_output (vm, "%v", error->what);
682  vlib_unix_error_report (vm, error);
683  /* clib_error_return is unfortunately often called with a '0'
684  return code */
685  rv = error->code != 0 ? error->code : -1;
686  clib_error_free (error);
687  }
688 
689  cp->output_function = save_function;
690  cp->output_function_arg = save_function_arg;
691  return rv;
692 }
693 
694 /* Output to current CLI connection. */
695 void
696 vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
697 {
699  va_list va;
700  u8 *s;
701 
702  va_start (va, fmt);
703  s = va_format (0, fmt, &va);
704  va_end (va);
705 
706  /* some format functions might return 0
707  * e.g. show int addr */
708  if (NULL == s)
709  return;
710 
711  /* Terminate with \n if not present. */
712  if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
713  vec_add1 (s, '\n');
714 
715  if ((!cp) || (!cp->output_function))
716  fformat (stdout, "%v", s);
717  else
718  cp->output_function (cp->output_function_arg, s, vec_len (s));
719 
720  vec_free (s);
721 }
722 
723 void *vl_msg_push_heap (void) __attribute__ ((weak));
724 void *
726 {
727  return 0;
728 }
729 
730 void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
731 void
732 vl_msg_pop_heap (void *oldheap)
733 {
734 }
735 
736 void *vlib_stats_push_heap (void *) __attribute__ ((weak));
737 void *
738 vlib_stats_push_heap (void *notused)
739 {
740  return 0;
741 }
742 
743 static clib_error_t *
745  unformat_input_t * input, vlib_cli_command_t * cmd)
746 {
748  int verbose __attribute__ ((unused)) = 0;
749  int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
750  int map = 0;
752  u32 index = 0;
753  int i;
755  uword was_enabled;
756 
757 
759  {
760  if (unformat (input, "verbose"))
761  verbose = 1;
762  else if (unformat (input, "api-segment"))
763  api_segment = 1;
764  else if (unformat (input, "stats-segment"))
765  stats_segment = 1;
766  else if (unformat (input, "main-heap"))
767  main_heap = 1;
768  else if (unformat (input, "numa-heaps"))
769  numa_heaps = 1;
770  else if (unformat (input, "map"))
771  map = 1;
772  else
773  {
774  error = clib_error_return (0, "unknown input `%U'",
775  format_unformat_error, input);
776  return error;
777  }
778  }
779 
780  if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
781  return clib_error_return
782  (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
783  "or map");
784 
785  if (api_segment)
786  {
787  void *oldheap = vl_msg_push_heap ();
788  was_enabled = clib_mem_trace_enable_disable (0);
789  u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
790  vl_msg_pop_heap (oldheap);
791  u8 *s = vec_dup (s_in_svm);
792 
793  oldheap = vl_msg_push_heap ();
794  vec_free (s_in_svm);
795  clib_mem_trace_enable_disable (was_enabled);
796  vl_msg_pop_heap (oldheap);
797  vlib_cli_output (vm, "API segment");
798  vlib_cli_output (vm, "%v", s);
799  vec_free (s);
800  }
801  if (stats_segment)
802  {
803  void *oldheap = vlib_stats_push_heap (0);
804  was_enabled = clib_mem_trace_enable_disable (0);
805  u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
806  if (oldheap)
807  clib_mem_set_heap (oldheap);
808  u8 *s = vec_dup (s_in_svm);
809 
810  oldheap = vlib_stats_push_heap (0);
811  vec_free (s_in_svm);
812  if (oldheap)
813  {
814  clib_mem_trace_enable_disable (was_enabled);
815  clib_mem_set_heap (oldheap);
816  }
817  vlib_cli_output (vm, "Stats segment");
818  vlib_cli_output (vm, "%v", s);
819  vec_free (s);
820  }
821 
822 
823  {
824  if (main_heap)
825  {
826  /*
827  * Note: the foreach_vlib_main causes allocator traffic,
828  * so shut off tracing before we go there...
829  */
830  was_enabled = clib_mem_trace_enable_disable (0);
831 
832  /* *INDENT-OFF* */
834  ({
835  vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
836  vlib_worker_threads[index].name);
838  mm->per_cpu_mheaps[index],
839  verbose);
840  index++;
841  }));
842  /* *INDENT-ON* */
843 
844  /* Restore the trace flag */
845  clib_mem_trace_enable_disable (was_enabled);
846  }
847  if (numa_heaps)
848  {
849  for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
850  {
851  if (mm->per_numa_mheaps[i] == 0)
852  continue;
853  if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
854  {
855  vlib_cli_output (vm, "Numa %d uses the main heap...", i);
856  continue;
857  }
858  was_enabled = clib_mem_trace_enable_disable (0);
859 
860  vlib_cli_output (vm, "Numa %d:", i);
862  mm->per_numa_mheaps[index], verbose);
863  }
864  }
865  if (map)
866  {
867  clib_mem_page_stats_t stats = { };
868  clib_mem_vm_map_hdr_t *hdr = 0;
869  u8 *s = 0;
870  int numa = -1;
871 
872  s = format (s, "\n%-16s%7s%5s%7s%7s",
873  "StartAddr", "size", "FD", "PageSz", "Pages");
874  while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
875  s = format (s, " Numa%u", numa);
876  s = format (s, " NotMap");
877  s = format (s, " Name");
878  vlib_cli_output (vm, "%v", s);
879  vec_reset_length (s);
880 
881  while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
882  {
883  clib_mem_get_page_stats ((void *) hdr->base_addr,
884  hdr->log2_page_sz, hdr->num_pages,
885  &stats);
886  s = format (s, "%016lx%7U",
887  hdr->base_addr, format_memory_size,
888  hdr->num_pages << hdr->log2_page_sz);
889 
890  if (hdr->fd != -1)
891  s = format (s, "%5d", hdr->fd);
892  else
893  s = format (s, "%5s", " ");
894 
895  s = format (s, "%7U%7lu",
896  format_log2_page_size, hdr->log2_page_sz,
897  hdr->num_pages);
898  while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
899  s = format (s, "%6lu", stats.per_numa[numa]);
900  s = format (s, "%7lu", stats.not_mapped);
901  s = format (s, " %s", hdr->name);
902  vlib_cli_output (vm, "%v", s);
903  vec_reset_length (s);
904  }
905  vec_free (s);
906  }
907  }
908  return 0;
909 }
910 
911 /* *INDENT-OFF* */
912 VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
913  .path = "show memory",
914  .short_help = "show memory [api-segment][stats-segment][verbose]\n"
915  " [numa-heaps][map]",
916  .function = show_memory_usage,
917 };
918 /* *INDENT-ON* */
919 
920 static clib_error_t *
922  vlib_cli_command_t * cmd)
923 {
924 #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
925  _("Model name", "%U", format_cpu_model_name);
926  _("Microarch model (family)", "%U", format_cpu_uarch);
927  _("Flags", "%U", format_cpu_flags);
928  _("Base frequency", "%.2f GHz",
929  ((f64) vm->clib_time.clocks_per_second) * 1e-9);
930 #undef _
931  return 0;
932 }
933 
934 /*?
935  * Displays various information about the CPU.
936  *
937  * @cliexpar
938  * @cliexstart{show cpu}
939  * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
940  * Microarchitecture: Broadwell (Broadwell-EP/EX)
941  * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
942  * Base Frequency: 3.20 GHz
943  * @cliexend
944 ?*/
945 /* *INDENT-OFF* */
946 VLIB_CLI_COMMAND (show_cpu_command, static) = {
947  .path = "show cpu",
948  .short_help = "Show cpu information",
949  .function = show_cpu,
950 };
951 /* *INDENT-ON* */
952 
953 static clib_error_t *
955  unformat_input_t * input,
956  vlib_cli_command_t * cmd)
957 {
959  unformat_input_t _line_input, *line_input = &_line_input;
960  int enable = 1;
961  int api_segment = 0;
962  int stats_segment = 0;
963  int main_heap = 0;
964  u32 numa_id = ~0;
965  void *oldheap;
966 
967  if (!unformat_user (input, unformat_line_input, line_input))
968  return 0;
969 
970  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
971  {
972  if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
973  ;
974  else if (unformat (line_input, "api-segment"))
975  api_segment = 1;
976  else if (unformat (line_input, "stats-segment"))
977  stats_segment = 1;
978  else if (unformat (line_input, "main-heap"))
979  main_heap = 1;
980  else if (unformat (line_input, "numa-heap %d", &numa_id))
981  ;
982  else
983  {
984  unformat_free (line_input);
985  return clib_error_return (0, "invalid input");
986  }
987  }
988  unformat_free (line_input);
989 
990  if ((api_segment + stats_segment + main_heap + (enable == 0)
991  + (numa_id != ~0)) == 0)
992  {
993  return clib_error_return
994  (0, "Need one of main-heap, stats-segment, api-segment,\n"
995  "numa-heap <nn> or disable");
996  }
997 
998  /* Turn off current trace, if any */
1000  {
1001  void *oldheap;
1003  clib_mem_trace (0);
1004  clib_mem_set_heap (oldheap);
1005  current_traced_heap = 0;
1006  }
1007 
1008  if (enable == 0)
1009  return 0;
1010 
1011  /* API segment */
1012  if (api_segment)
1013  {
1014  oldheap = vl_msg_push_heap ();
1016  clib_mem_trace (1);
1017  vl_msg_pop_heap (oldheap);
1018 
1019  }
1020 
1021  /* Stats segment */
1022  if (stats_segment)
1023  {
1024  oldheap = vlib_stats_push_heap (0);
1026  clib_mem_trace (stats_segment);
1027  /* We don't want to call vlib_stats_pop_heap... */
1028  if (oldheap)
1029  clib_mem_set_heap (oldheap);
1030  }
1031 
1032  /* main_heap */
1033  if (main_heap)
1034  {
1036  clib_mem_trace (main_heap);
1037  }
1038 
1039  if (numa_id != ~0)
1040  {
1041  if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
1042  return clib_error_return (0, "Numa %d out of range", numa_id);
1043  if (mm->per_numa_mheaps[numa_id] == 0)
1044  return clib_error_return (0, "Numa %d heap not active", numa_id);
1045 
1046  if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
1047  return clib_error_return (0, "Numa %d uses the main heap...",
1048  numa_id);
1049  current_traced_heap = mm->per_numa_mheaps[numa_id];
1051  clib_mem_trace (1);
1052  clib_mem_set_heap (oldheap);
1053  }
1054 
1055 
1056  return 0;
1057 }
1058 
1059 /* *INDENT-OFF* */
1060 VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1061  .path = "memory-trace",
1062  .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1063  " [numa-heap <numa-id>]\n",
1064  .function = enable_disable_memory_trace,
1065 };
1066 /* *INDENT-ON* */
1067 
1068 static clib_error_t *
1070  vlib_cli_command_t * cmd)
1071 {
1073  clib_file_t *f;
1074 
1075  /* environ(7) does not indicate a header for this */
1076  extern char **environ;
1077 
1078  /* Close all known open files */
1079  /* *INDENT-OFF* */
1080  pool_foreach (f, fm->file_pool)
1081  {
1082  if (f->file_descriptor > 2)
1083  close(f->file_descriptor);
1084  }
1085  /* *INDENT-ON* */
1086 
1087  /* Exec ourself */
1088  execve (vm->name, (char **) vm->argv, environ);
1089 
1090  return 0;
1091 }
1092 
1093 /* *INDENT-OFF* */
1094 VLIB_CLI_COMMAND (restart_cmd,static) = {
1095  .path = "restart",
1096  .short_help = "restart process",
1097  .function = restart_cmd_fn,
1098 };
1099 /* *INDENT-ON* */
1100 
1101 #ifdef TEST_CODE
1102 /*
1103  * A trivial test harness to verify the per-process output_function
1104  * is working correcty.
1105  */
1106 
1107 static clib_error_t *
1108 sleep_ten_seconds (vlib_main_t * vm,
1109  unformat_input_t * input, vlib_cli_command_t * cmd)
1110 {
1111  u16 i;
1112  u16 my_id = rand ();
1113 
1114  vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
1115 
1116  for (i = 0; i < 10; i++)
1117  {
1119  vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
1120  }
1121  vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
1122  return 0;
1123 }
1124 
1125 /* *INDENT-OFF* */
1126 VLIB_CLI_COMMAND (ping_command, static) = {
1127  .path = "test sleep",
1128  .function = sleep_ten_seconds,
1129  .short_help = "Sleep for 10 seconds",
1130 };
1131 /* *INDENT-ON* */
1132 #endif /* ifdef TEST_CODE */
1133 
1134 static uword
1135 vlib_cli_normalize_path (char *input, char **result)
1136 {
1137  char *i = input;
1138  char *s = 0;
1139  uword l = 0;
1140  uword index_of_last_space = ~0;
1141 
1142  while (*i != 0)
1143  {
1144  u8 c = *i++;
1145  /* Multiple white space -> single space. */
1146  switch (c)
1147  {
1148  case ' ':
1149  case '\t':
1150  case '\n':
1151  case '\r':
1152  if (l > 0 && s[l - 1] != ' ')
1153  {
1154  vec_add1 (s, ' ');
1155  l++;
1156  }
1157  break;
1158 
1159  default:
1160  if (l > 0 && s[l - 1] == ' ')
1161  index_of_last_space = vec_len (s);
1162  vec_add1 (s, c);
1163  l++;
1164  break;
1165  }
1166  }
1167 
1168  /* Remove any extra space at end. */
1169  if (l > 0 && s[l - 1] == ' ')
1170  _vec_len (s) -= 1;
1171 
1172  *result = s;
1173  return index_of_last_space;
1174 }
1175 
1178 {
1179  word i;
1180  for (i = vec_len (path) - 1; i >= 0; i--)
1181  {
1182  if (path[i] == ' ')
1183  return i;
1184  }
1185  return ~0;
1186 }
1187 
1188 static void
1189 add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
1190 {
1191  vlib_cli_command_t *p, *c;
1192  vlib_cli_sub_command_t *sub_c;
1193  u8 *sub_name;
1194  word i, l;
1195 
1196  p = vec_elt_at_index (cm->commands, parent_index);
1197  c = vec_elt_at_index (cm->commands, child_index);
1198 
1199  l = parent_path_len (c->path);
1200  if (l == ~0)
1201  sub_name = vec_dup ((u8 *) c->path);
1202  else
1203  {
1204  ASSERT (l + 1 < vec_len (c->path));
1205  sub_name = 0;
1206  vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1207  }
1208 
1209  if (sub_name[0] == '%')
1210  {
1211  uword *q;
1212  vlib_cli_sub_rule_t *sr;
1213 
1214  /* Remove %. */
1215  vec_delete (sub_name, 1, 0);
1216 
1217  if (!p->sub_rule_index_by_name)
1218  p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1219  sizeof (sub_name[0]),
1220  sizeof (uword));
1221  q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1222  if (q)
1223  {
1224  sr = vec_elt_at_index (p->sub_rules, q[0]);
1225  ASSERT (sr->command_index == child_index);
1226  return;
1227  }
1228 
1229  hash_set_mem (p->sub_rule_index_by_name, sub_name,
1230  vec_len (p->sub_rules));
1231  vec_add2 (p->sub_rules, sr, 1);
1232  sr->name = sub_name;
1233  sr->rule_index = sr - p->sub_rules;
1234  sr->command_index = child_index;
1235  return;
1236  }
1237 
1238  if (!p->sub_command_index_by_name)
1239  p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1240  sizeof (c->path[0]),
1241  sizeof (uword));
1242 
1243  /* Check if sub-command has already been created. */
1244  if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1245  {
1246  vec_free (sub_name);
1247  return;
1248  }
1249 
1250  vec_add2 (p->sub_commands, sub_c, 1);
1251  sub_c->index = child_index;
1252  sub_c->name = sub_name;
1254  sub_c - p->sub_commands);
1255 
1256  vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1257  for (i = 0; i < vec_len (sub_c->name); i++)
1258  {
1259  int n;
1261 
1263 
1264  if (!pos->bitmaps)
1265  pos->min_char = sub_c->name[i];
1266 
1267  n = sub_c->name[i] - pos->min_char;
1268  if (n < 0)
1269  {
1270  pos->min_char = sub_c->name[i];
1271  vec_insert (pos->bitmaps, -n, 0);
1272  n = 0;
1273  }
1274 
1275  vec_validate (pos->bitmaps, n);
1276  pos->bitmaps[n] =
1277  clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
1278  }
1279 }
1280 
1281 static void
1283 {
1284  uword p_len, pi, *p;
1285  char *p_path;
1286  vlib_cli_command_t *c, *parent;
1287 
1288  /* Root command (index 0) should have already been added. */
1289  ASSERT (vec_len (cm->commands) > 0);
1290 
1291  c = vec_elt_at_index (cm->commands, ci);
1292  p_len = parent_path_len (c->path);
1293 
1294  /* No space? Parent is root command. */
1295  if (p_len == ~0)
1296  {
1297  add_sub_command (cm, 0, ci);
1298  return;
1299  }
1300 
1301  p_path = 0;
1302  vec_add (p_path, c->path, p_len);
1303 
1304  p = hash_get_mem (cm->command_index_by_path, p_path);
1305 
1306  /* Parent exists? */
1307  if (!p)
1308  {
1309  /* Parent does not exist; create it. */
1310  vec_add2 (cm->commands, parent, 1);
1311  parent->path = p_path;
1312  hash_set_mem (cm->command_index_by_path, parent->path,
1313  parent - cm->commands);
1314  pi = parent - cm->commands;
1315  }
1316  else
1317  {
1318  pi = p[0];
1319  vec_free (p_path);
1320  }
1321 
1322  add_sub_command (cm, pi, ci);
1323 
1324  /* Create parent's parent. */
1325  if (!p)
1326  vlib_cli_make_parent (cm, pi);
1327 }
1328 
1331 {
1332  return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
1333 }
1334 
1335 clib_error_t *
1337 {
1338  vlib_cli_main_t *cm = &vm->cli_main;
1339  clib_error_t *error = 0;
1340  uword ci, *p;
1341  char *normalized_path;
1342 
1343  if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1344  return error;
1345 
1346  (void) vlib_cli_normalize_path (c->path, &normalized_path);
1347 
1348  if (!cm->command_index_by_path)
1349  cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
1350  sizeof (c->path[0]),
1351  sizeof (uword));
1352 
1353  /* See if command already exists with given path. */
1354  p = hash_get_mem (cm->command_index_by_path, normalized_path);
1355  if (p)
1356  {
1357  vlib_cli_command_t *d;
1358 
1359  ci = p[0];
1360  d = vec_elt_at_index (cm->commands, ci);
1361 
1362  /* If existing command was created via vlib_cli_make_parent
1363  replaced it with callers data. */
1364  if (vlib_cli_command_is_empty (d))
1365  {
1366  vlib_cli_command_t save = d[0];
1367 
1369 
1370  /* Copy callers fields. */
1371  d[0] = c[0];
1372 
1373  /* Save internal fields. */
1374  d->path = save.path;
1375  d->sub_commands = save.sub_commands;
1378  d->sub_rules = save.sub_rules;
1379  }
1380  else
1381  error =
1382  clib_error_return (0, "duplicate command name with path %v",
1383  normalized_path);
1384 
1385  vec_free (normalized_path);
1386  if (error)
1387  return error;
1388  }
1389  else
1390  {
1391  /* Command does not exist: create it. */
1392 
1393  /* Add root command (index 0). */
1394  if (vec_len (cm->commands) == 0)
1395  {
1396  /* Create command with index 0; path is empty string. */
1397  vec_resize (cm->commands, 1);
1398  }
1399 
1400  ci = vec_len (cm->commands);
1401  hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1402  vec_add1 (cm->commands, c[0]);
1403 
1404  c = vec_elt_at_index (cm->commands, ci);
1405  c->path = normalized_path;
1406 
1407  /* Don't inherit from registration. */
1408  c->sub_commands = 0;
1410  c->sub_command_positions = 0;
1411  }
1412 
1413  vlib_cli_make_parent (cm, ci);
1414  return 0;
1415 }
1416 
1417 #if 0
1418 /* $$$ turn back on again someday, maybe */
1419 clib_error_t *
1421 {
1422  vlib_cli_main_t *cm = &vm->cli_main;
1424  clib_error_t *error = 0;
1425  u8 *r_name;
1426  uword *p;
1427 
1428  if (!cm->parse_rule_index_by_name)
1429  cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1430  sizeof (r->name[0]),
1431  sizeof (uword));
1432 
1433  /* Make vector copy of name. */
1434  r_name = format (0, "%s", r_reg->name);
1435 
1436  if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1437  {
1438  vec_free (r_name);
1439  return clib_error_return (0, "duplicate parse rule name `%s'",
1440  r_reg->name);
1441  }
1442 
1443  vec_add2 (cm->parse_rules, r, 1);
1444  r[0] = r_reg[0];
1445  r->name = (char *) r_name;
1446  hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1447 
1448  return error;
1449 }
1450 
1451 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1453  lo,
1455  hi)
1456  __attribute__ ((unused))
1457 {
1458  clib_error_t *error = 0;
1460 
1461  for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1462  {
1463  if (!r->name || strlen (r->name) == 0)
1464  {
1465  error = clib_error_return (0, "parse rule with no name");
1466  goto done;
1467  }
1468 
1469  error = vlib_cli_register_parse_rule (vm, r);
1470  if (error)
1471  goto done;
1472  }
1473 
1474 done:
1475  return error;
1476 }
1477 #endif
1478 
1479 static clib_error_t *
1481  unformat_input_t * input,
1482  vlib_cli_command_t * cmd)
1483 {
1484  unformat_input_t _line_input, *line_input = &_line_input;
1485  int enable = 1;
1486  int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1487  u32 circuit_node_index;
1488 
1489  if (!unformat_user (input, unformat_line_input, line_input))
1490  goto print_status;
1491 
1492  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1493  {
1494  if (unformat (line_input, "api"))
1495  api = 1;
1496  else if (unformat (line_input, "dispatch"))
1497  dispatch = 1;
1498  else if (unformat (line_input, "circuit-node %U",
1499  unformat_vlib_node, vm, &circuit_node_index))
1500  circuit = 1;
1501  else if (unformat (line_input, "cli"))
1502  cli = 1;
1503  else if (unformat (line_input, "barrier"))
1504  barrier = 1;
1505  else if (unformat (line_input, "disable"))
1506  enable = 0;
1507  else if (unformat (line_input, "enable"))
1508  enable = 1;
1509  else
1510  break;
1511  }
1512  unformat_free (line_input);
1513 
1515  (api ? enable : vl_api_get_elog_trace_api_messages ());
1516  vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
1517  vm->elog_trace_graph_dispatch = dispatch ?
1518  enable : vm->elog_trace_graph_dispatch;
1519  vm->elog_trace_graph_circuit = circuit ?
1520  enable : vm->elog_trace_graph_circuit;
1522  barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
1523  vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1524 
1525  /*
1526  * Set up start-of-buffer logic-analyzer trigger
1527  * for main loop event logs, which are fairly heavyweight.
1528  * See src/vlib/main/vlib_elog_main_loop_event(...), which
1529  * will fully disable the scheme when the elog buffer fills.
1530  */
1531  if (dispatch || circuit)
1532  {
1533  elog_main_t *em = &vm->elog_main;
1534 
1536  em->n_total_events + vec_len (em->event_ring);
1537  }
1538 
1539 
1540 print_status:
1541  vlib_cli_output (vm, "Current status:");
1542 
1544  (vm, " Event log API message trace: %s\n CLI command trace: %s",
1545  vl_api_get_elog_trace_api_messages ()? "on" : "off",
1546  vm->elog_trace_cli_commands ? "on" : "off");
1548  (vm, " Barrier sync trace: %s",
1549  vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
1551  (vm, " Graph Dispatch: %s",
1552  vm->elog_trace_graph_dispatch ? "on" : "off");
1554  (vm, " Graph Circuit: %s",
1555  vm->elog_trace_graph_circuit ? "on" : "off");
1556  if (vm->elog_trace_graph_circuit)
1558  (vm, " node %U",
1560 
1561  return 0;
1562 }
1563 
1564 /*?
1565  * Control event logging of api, cli, and thread barrier events
1566  * With no arguments, displays the current trace status.
1567  * Name the event groups you wish to trace or stop tracing.
1568  *
1569  * @cliexpar
1570  * @clistart
1571  * event-logger trace api cli barrier
1572  * event-logger trace api cli barrier disable
1573  * event-logger trace dispatch
1574  * event-logger trace circuit-node ethernet-input
1575  * @cliend
1576  * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
1577 ?*/
1578 /* *INDENT-OFF* */
1579 VLIB_CLI_COMMAND (event_logger_trace_command, static) =
1580 {
1581  .path = "event-logger trace",
1582  .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
1583  "[circuit-node <name> e.g. ethernet-input][disable]",
1584  .function = event_logger_trace_command_fn,
1585 };
1586 /* *INDENT-ON* */
1587 
1588 static clib_error_t *
1590  unformat_input_t * input, vlib_cli_command_t * cmd)
1591 {
1592  vlib_process_suspend (vm, 30e-3);
1593  return 0;
1594 }
1595 
1596 /* *INDENT-OFF* */
1597 VLIB_CLI_COMMAND (suspend_command, static) =
1598 {
1599  .path = "suspend",
1600  .short_help = "suspend debug CLI for 30ms",
1601  .function = suspend_command_fn,
1602  .is_mp_safe = 1,
1603 };
1604 /* *INDENT-ON* */
1605 
1606 
1607 static int
1608 sort_cmds_by_path (void *a1, void *a2)
1609 {
1610  u32 *index1 = a1;
1611  u32 *index2 = a2;
1612  vlib_main_t *vm = vlib_get_main ();
1613  vlib_cli_main_t *cm = &vm->cli_main;
1614  vlib_cli_command_t *c1, *c2;
1615  int i, lmin;
1616 
1617  c1 = vec_elt_at_index (cm->commands, *index1);
1618  c2 = vec_elt_at_index (cm->commands, *index2);
1619 
1620  lmin = vec_len (c1->path);
1621  lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1622 
1623  for (i = 0; i < lmin; i++)
1624  {
1625  if (c1->path[i] < c2->path[i])
1626  return -1;
1627  else if (c1->path[i] > c2->path[i])
1628  return 1;
1629  }
1630 
1631  return 0;
1632 }
1633 
1634 typedef struct
1635 {
1643 
1644 static void
1646 {
1647  vlib_cli_command_t *parent;
1649  vlib_cli_walk_args_t _a, *a = &_a;
1651  int i;
1652 
1653  /* Copy args into this stack frame */
1654  *a = *aa;
1655  cm = a->cm;
1656 
1657  parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1658 
1659  if (parent->function)
1660  {
1661  if (((a->show_mp_safe && parent->is_mp_safe)
1662  || (a->show_not_mp_safe && !parent->is_mp_safe))
1663  && (a->show_hit == 0 || parent->hit_counter))
1664  {
1666  }
1667 
1668  if (a->clear_hit)
1669  parent->hit_counter = 0;
1670  }
1671 
1672  for (i = 0; i < vec_len (parent->sub_commands); i++)
1673  {
1674  sub = vec_elt_at_index (parent->sub_commands, i);
1675  a->parent_command_index = sub->index;
1676  cli_recursive_walk (a);
1677  }
1678 }
1679 
1680 static u8 *
1681 format_mp_safe (u8 * s, va_list * args)
1682 {
1683  vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1684  int show_mp_safe = va_arg (*args, int);
1685  int show_not_mp_safe = va_arg (*args, int);
1686  int show_hit = va_arg (*args, int);
1687  int clear_hit = va_arg (*args, int);
1689  vlib_cli_walk_args_t _a, *a = &_a;
1690  int i;
1691  char *format_string = "\n%v";
1692 
1693  if (show_hit)
1694  format_string = "\n%v: %u";
1695 
1697 
1698  a->cm = cm;
1699  a->parent_command_index = 0;
1700  a->show_mp_safe = show_mp_safe;
1701  a->show_not_mp_safe = show_not_mp_safe;
1702  a->show_hit = show_hit;
1703  a->clear_hit = clear_hit;
1704 
1705  cli_recursive_walk (a);
1706 
1708 
1709  for (i = 0; i < vec_len (cm->sort_vector); i++)
1710  {
1711  c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1712  s = format (s, format_string, c->path, c->hit_counter);
1713  }
1714 
1715  return s;
1716 }
1717 
1718 
1719 static clib_error_t *
1721  unformat_input_t * input, vlib_cli_command_t * cmd)
1722 {
1723  int show_mp_safe = 0;
1724  int show_not_mp_safe = 0;
1725  int show_hit = 0;
1726  int clear_hit = 0;
1727 
1728  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1729  {
1730  if (unformat (input, "mp-safe"))
1731  show_mp_safe = 1;
1732  if (unformat (input, "not-mp-safe"))
1733  show_not_mp_safe = 1;
1734  else if (unformat (input, "hit"))
1735  show_hit = 1;
1736  else if (unformat (input, "clear-hit"))
1737  clear_hit = 1;
1738  else
1739  break;
1740  }
1741 
1742  /* default set: all cli commands */
1743  if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1744  show_mp_safe = show_not_mp_safe = 1;
1745 
1746  vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1747  show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1748  if (clear_hit)
1749  vlib_cli_output (vm, "hit counters cleared...");
1750 
1751  return 0;
1752 }
1753 
1754 /*?
1755  * Displays debug cli command information
1756  *
1757  * @cliexpar
1758  * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1759  *
1760  * "show cli" displays the entire debug cli:
1761  *
1762  * abf attach
1763  * abf policy
1764  * adjacency counters
1765  * api trace
1766  * app ns
1767  * bfd key del
1768  * ... and so on ...
1769  *
1770  * "show cli mp-safe" displays mp-safe debug CLI commands:
1771  *
1772  * abf policy
1773  * binary-api
1774  * create vhost-user
1775  * exec
1776  * ip container
1777  * ip mroute
1778  * ip probe-neighbor
1779  * ip route
1780  * ip scan-neighbor
1781  * ip table
1782  * ip6 table
1783  *
1784  * "show cli not-mp-safe" displays debug CLI commands
1785  * which cause worker thread barrier synchronization
1786  *
1787  * "show cli hit" displays commands which have been executed. Qualify
1788  * as desired with "mp-safe" or "not-mp-safe".
1789  *
1790  * "show cli clear-hit" clears the per-command hit counters.
1791  * @cliexend
1792 ?*/
1793 
1794 /* *INDENT-OFF* */
1795 VLIB_CLI_COMMAND (show_cli_command, static) =
1796 {
1797  .path = "show cli",
1798  .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1799  .function = show_cli_command_fn,
1800  .is_mp_safe = 1,
1801 };
1802 /* *INDENT-ON* */
1803 
1804 static clib_error_t *
1806 {
1807  vlib_cli_main_t *cm = &vm->cli_main;
1808  clib_error_t *error = 0;
1809  vlib_cli_command_t *cmd;
1810 
1811  cmd = cm->cli_command_registrations;
1812 
1813  while (cmd)
1814  {
1815  error = vlib_cli_register (vm, cmd);
1816  if (error)
1817  return error;
1818  cmd = cmd->next_cli_command;
1819  }
1820  return error;
1821 }
1822 
1824 
1825 /*
1826  * fd.io coding-style-patch-verification: ON
1827  *
1828  * Local Variables:
1829  * eval: (c-set-style "gnu")
1830  * End:
1831  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
__clib_export u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:387
uword output_function_arg
Definition: node.h:609
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:751
format_function_t format_vlib_node_name
Definition: node_funcs.h:1222
a
Definition: bitmap.h:544
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
unformat_function_t unformat_eof
Definition: format.h:292
static uword unformat_get_input(unformat_input_t *input)
Definition: format.h:191
int elog_trace_cli_commands
Definition: main.h:229
struct _clib_mem_vm_map_hdr clib_mem_vm_map_hdr_t
u32 parent_command_index
Definition: cli.c:1637
const char *const string
Definition: cJSON.h:172
vlib_cli_command_t * commands
Definition: cli.h:138
int vl_api_get_elog_trace_api_messages(void)
Definition: api_shared.c:1234
f64 clocks_per_second
Definition: time.h:54
static clib_mem_heap_t * clib_mem_set_heap(clib_mem_heap_t *heap)
Definition: mem.h:365
#define clib_bitmap_foreach(i, ai)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
static clib_error_t * show_cli_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:1720
format_function_t format_cpu_flags
Definition: cpu.h:445
u32 file_descriptor
Definition: file.h:54
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
static int vlib_cli_cmp_strings(void *a1, void *a2)
Definition: cli.c:228
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
uword * sub_rule_index_by_name
Definition: cli.h:121
#define hash_set_mem(h, key, value)
Definition: hash.h:275
clib_time_t clib_time
Definition: main.h:123
vl_api_fib_path_t path
Definition: mfib_types.api:44
__clib_export clib_mem_vm_map_hdr_t * clib_mem_vm_get_next_map_hdr(clib_mem_vm_map_hdr_t *hdr)
Definition: mem.c:388
int vlib_cli_input(vlib_main_t *vm, unformat_input_t *input, vlib_cli_output_function_t *function, uword function_arg)
Definition: cli.c:656
void * vlib_stats_push_heap(void *)
Definition: cli.c:738
vlib_cli_main_t cli_main
Definition: main.h:191
static clib_error_t * enable_disable_memory_trace(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:954
unsigned char u8
Definition: types.h:56
static clib_error_t * event_logger_trace_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:1480
#define clib_bitmap_dup(v)
Duplicate a bitmap.
Definition: bitmap.h:87
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:205
clib_error_t * vlib_cli_register_parse_rule(struct vlib_main_t *vm, vlib_cli_parse_rule_t *c)
#define fm
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:668
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
clib_file_t * file_pool
Definition: file.h:88
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:482
i64 word
Definition: types.h:111
static u8 * format_mp_safe(u8 *s, va_list *args)
Definition: cli.c:1681
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
Callback multiplex scheme For a fully worked-out example, see .../src/vlib/main.
void vlib_unix_error_report(vlib_main_t *, clib_error_t *)
Definition: cli.c:650
static clib_error_t * vlib_cli_dispatch_sub_commands(vlib_main_t *vm, vlib_cli_main_t *cm, unformat_input_t *input, uword parent_command_index)
Definition: cli.c:386
static uword clib_bitmap_is_zero(uword *ai)
predicate function; is an entire bitmap empty?
Definition: bitmap.h:57
description fragment has unexpected format
Definition: map.api:433
u8 * format_memory_size(u8 *s, va_list *va)
Definition: std-formats.c:209
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
uword * command_index_by_path
Definition: cli.h:141
clib_file_main_t file_main
Definition: main.c:63
static int vlib_cli_cmp_rule(void *a1, void *a2)
Definition: cli.c:368
static void * current_traced_heap
Definition: cli.c:54
u32 command_index
Definition: cli.h:68
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:281
static uword vlib_cli_normalize_path(char *input, char **result)
Definition: cli.c:1135
unsigned int u32
Definition: types.h:88
static u8 * format_vlib_cli_path(u8 *s, va_list *args)
Definition: cli.c:344
uword per_numa[CLIB_MAX_NUMAS]
Definition: mem.h:515
#define vlib_call_init_function(vm, x)
Definition: init.h:270
uword clib_mem_trace_enable_disable(uword enable)
Definition: mem_dlmalloc.c:522
static clib_error_t * show_memory_usage(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:744
static clib_error_t * show_cpu(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:921
unformat_function_t unformat_line_input
Definition: format.h:282
char * name
Definition: main.h:170
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
Definition: cJSON.c:84
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.c:34
static void add_sub_command(vlib_cli_main_t *cm, uword parent_index, uword child_index)
Definition: cli.c:1189
#define vec_insert(V, N, M)
Insert N vector elements starting at element M, initialize new elements to zero (no header...
Definition: vec.h:755
void vl_msg_pop_heap(void *oldheap)
Definition: cli.c:732
vlib_cli_command_function_t * function
Definition: cli.h:102
uword not_mapped
Definition: mem.h:514
static uword clib_bitmap_first_set(uword *ai)
Return the lowest numbered set bit in a bitmap.
Definition: bitmap.h:391
static vlib_cli_command_t * get_sub_command(vlib_cli_main_t *cm, vlib_cli_command_t *parent, u32 si)
Definition: cli.c:199
static_always_inline int vlib_mem_get_next_numa_node(int numa)
Definition: mem.h:523
vlib_cli_sub_rule_t * sub_rules
Definition: cli.h:124
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
unknown
Definition: map.api:399
static int vlib_cli_cmp_command(void *a1, void *a2)
Definition: cli.c:377
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:429
#define ELOG_DATA(em, f)
Definition: elog.h:484
elog_event_t * event_ring
Vector of events (circular buffer).
Definition: elog.h:149
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define always_inline
Definition: ipsec.h:28
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:875
static void unformat_put_input(unformat_input_t *input)
Definition: format.h:204
static void vlib_cli_make_parent(vlib_cli_main_t *cm, uword ci)
Definition: cli.c:1282
#define foreach_vlib_main(body)
Definition: threads.h:242
void * vl_msg_push_heap(void)
Definition: cli.c:725
int cJSON_bool fmt
Definition: cJSON.h:160
clib_error_t * vlib_cli_register(vlib_main_t *vm, vlib_cli_command_t *c)
Definition: cli.c:1336
void unformat_init_vector(unformat_input_t *input, u8 *vector_string)
Definition: unformat.c:1037
static uword * vlib_cli_sub_command_match(vlib_cli_command_t *c, unformat_input_t *input)
Definition: cli.c:90
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
vlib_cli_sub_command_t * sub_commands
Definition: cli.h:111
u8 ** argv
Definition: main.h:266
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
svmdb_client_t * c
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:444
int elog_trace_graph_dispatch
Definition: main.h:230
int show_not_mp_safe
Definition: cli.c:1639
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
int vl_api_set_elog_trace_api_messages(int enable)
Definition: api_shared.c:1223
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
counters map
Definition: map.api:356
void(** perf_counter_cbs)(struct vlib_cli_main_t *, u32 id, int before_or_after)
Definition: cli.h:152
static uword * clib_bitmap_andnot(uword *ai, uword *bi)
Logical operator across two bitmaps.
elog_main_t elog_main
Definition: main.h:224
static uword parent_path_len(char *path)
Definition: cli.c:1177
static clib_error_t * vlib_cli_init(vlib_main_t *vm)
Definition: cli.c:1805
#define ARRAY_LEN(x)
Definition: clib.h:67
uword ** bitmaps
Definition: cli.h:52
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:442
u32 n_total_events_disable_limit
When count reaches limit logging is disabled.
Definition: elog.h:139
string name[64]
Definition: ip.api:44
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
static clib_mem_heap_t * clib_mem_get_heap(void)
Definition: mem.h:359
void * per_cpu_mheaps[CLIB_MAX_MHEAPS]
Definition: mem.h:140
uword unformat_vlib_cli_sub_input(unformat_input_t *i, va_list *args)
Definition: cli.c:163
#define clib_elf_section_data_next(a, extra)
Definition: elf_clib.h:57
static clib_error_t * suspend_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:1589
#define ASSERT(truth)
u8 * format_clib_mem_heap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:422
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:854
u32 hit_counter
Definition: cli.h:130
__clib_export void clib_mem_get_page_stats(void *start, clib_mem_page_sz_t log2_page_size, uword n_pages, clib_mem_page_stats_t *stats)
Definition: mem.c:553
char * long_help
Definition: cli.h:99
uword is_mp_safe
Definition: cli.h:108
char * path
Definition: cli.h:95
#define clib_bitmap_free(v)
Free a bitmap.
Definition: bitmap.h:92
static int sort_cmds_by_path(void *a1, void *a2)
Definition: cli.c:1608
uword * sub_command_index_by_name
Definition: cli.h:114
u32 elog_trace_graph_circuit_node_index
Definition: main.h:232
#define vec_cmp(v1, v2)
Compare two vectors (only applicable to vectors of signed numbers).
Definition: vec.h:991
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
vl_api_ip4_address_t hi
Definition: arp.api:37
static vlib_cli_command_t * all_subs(vlib_cli_main_t *cm, vlib_cli_command_t *subs, u32 command_index)
Definition: cli.c:354
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
Definition: bitmap.h:468
struct vlib_cli_command_t * next_cli_command
Definition: cli.h:127
#define hash_create_vec(elts, key_bytes, value_bytes)
Definition: hash.h:668
static uword unformat_vlib_cli_sub_command(unformat_input_t *i, va_list *args)
Definition: cli.c:206
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
uword unformat_vlib_enable_disable(unformat_input_t *input, va_list *args)
Definition: format.c:116
static u8 * format_vlib_cli_command_help(u8 *s, va_list *args)
Definition: cli.c:330
static void cli_recursive_walk(vlib_cli_walk_args_t *aa)
Definition: cli.c:1645
static uword vlib_cli_command_is_empty(vlib_cli_command_t *c)
Definition: cli.c:1330
u64 uword
Definition: types.h:112
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1055
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
u32 index
Definition: flow_types.api:221
__clib_export u32 elog_string(elog_main_t *em, char *fmt,...)
add a string to the event-log string table
Definition: elog.c:582
__clib_export clib_mem_main_t clib_mem_main
Definition: mem.c:22
format_function_t format_cpu_uarch
Definition: cpu.h:443
#define clib_error_free(e)
Definition: error.h:86
unformat_function_t unformat_vlib_node
Definition: node_funcs.h:1228
#define hash_get_mem(h, key)
Definition: hash.h:269
vlib_cli_command_t * cli_command_registrations
Definition: cli.h:144
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1561
#define clib_call_callbacks(h,...)
call the specified callback set
Definition: callback.h:67
#define vec_foreach(var, vec)
Vector iterator.
Definition: file.h:51
vlib_cli_parse_position_t * sub_command_positions
Definition: cli.h:118
u32 n_total_events
Total number of events in buffer.
Definition: elog.h:135
void * per_numa_mheaps[CLIB_MAX_NUMAS]
Definition: mem.h:143
u8 si
Definition: lisp_types.api:47
void() vlib_cli_output_function_t(uword arg, u8 *buffer, uword buffer_bytes)
Definition: cli.h:133
vlib_cli_output_function_t * output_function
Definition: node.h:608
u32 * sort_vector
Definition: cli.h:147
void clib_mem_trace(int enable)
Definition: mem_dlmalloc.c:500
import vnet interface_types api
Definition: sample.api:20
char * short_help
Definition: cli.h:98
static clib_error_t * restart_cmd_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cli.c:1069
uword unformat_skip_white_space(unformat_input_t *input)
Definition: unformat.c:821
format_function_t format_cpu_model_name
Definition: cpu.h:444
int elog_trace_graph_circuit
Definition: main.h:231
static uword * clib_bitmap_and(uword *ai, uword *bi)
Logical operator across two bitmaps.
vlib_cli_main_t * cm
Definition: cli.c:1636
u8 ** vlib_cli_get_possible_completions(u8 *str)
Definition: cli.c:237
u8 * format_log2_page_size(u8 *s, va_list *va)
Definition: std-formats.c:273
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
u32 rule_index
Definition: cli.h:66
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170