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