blob: 6d83e2a3ce3141e8ecacc8670ebfea27c2b857a4 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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>
Chris Luke6edd3602018-06-12 22:45:06 -040041#include <vlib/unix/unix.h>
Damjan Marioneccf5092016-11-18 16:59:24 +010042#include <vppinfra/cpu.h>
Dave Barachc3a06552018-10-01 09:25:32 -040043#include <vppinfra/elog.h>
Damjan Marione5ef1d72017-03-02 12:33:48 +010044#include <unistd.h>
Yoann Desmouceaux3060e072017-05-18 11:00:48 +020045#include <ctype.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47/* Root of all show commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040048/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070049VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
50 .path = "show",
51 .short_help = "Show commands",
52};
Dave Barach9b8ffd92016-07-08 08:13:45 -040053/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55/* Root of all clear commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040056/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070057VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
58 .path = "clear",
59 .short_help = "Clear commands",
60};
Dave Barach9b8ffd92016-07-08 08:13:45 -040061/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
63/* Root of all set commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040064/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070065VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
66 .path = "set",
67 .short_help = "Set commands",
68};
Dave Barach9b8ffd92016-07-08 08:13:45 -040069/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71/* Root of all test commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040072/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070073VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
74 .path = "test",
75 .short_help = "Test commands",
76};
Dave Barach9b8ffd92016-07-08 08:13:45 -040077/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79/* Returns bitmap of commands which match key. */
80static uword *
81vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
82{
83 int i, n;
Dave Barach9b8ffd92016-07-08 08:13:45 -040084 uword *match = 0;
85 vlib_cli_parse_position_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87 unformat_skip_white_space (input);
88
Dave Barach9b8ffd92016-07-08 08:13:45 -040089 for (i = 0;; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 {
91 uword k;
92
93 k = unformat_get_input (input);
94 switch (k)
95 {
96 case 'a' ... 'z':
97 case 'A' ... 'Z':
98 case '0' ... '9':
Dave Barach9b8ffd92016-07-08 08:13:45 -040099 case '-':
100 case '_':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 break;
102
Dave Barach9b8ffd92016-07-08 08:13:45 -0400103 case ' ':
104 case '\t':
105 case '\r':
106 case '\n':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 case UNFORMAT_END_OF_INPUT:
108 /* White space or end of input removes any non-white
109 matches that were before possible. */
110 if (i < vec_len (c->sub_command_positions)
111 && clib_bitmap_count_set_bits (match) > 1)
112 {
113 p = vec_elt_at_index (c->sub_command_positions, i);
114 for (n = 0; n < vec_len (p->bitmaps); n++)
115 match = clib_bitmap_andnot (match, p->bitmaps[n]);
116 }
117 goto done;
118
119 default:
120 unformat_put_input (input);
121 goto done;
122 }
123
124 if (i >= vec_len (c->sub_command_positions))
125 {
126 no_match:
127 clib_bitmap_free (match);
128 return 0;
129 }
130
131 p = vec_elt_at_index (c->sub_command_positions, i);
132 if (vec_len (p->bitmaps) == 0)
133 goto no_match;
134
135 n = k - p->min_char;
136 if (n < 0 || n >= vec_len (p->bitmaps))
137 goto no_match;
138
139 if (i == 0)
140 match = clib_bitmap_dup (p->bitmaps[n]);
141 else
142 match = clib_bitmap_and (match, p->bitmaps[n]);
143
144 if (clib_bitmap_is_zero (match))
145 goto no_match;
146 }
147
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 return match;
150}
151
152/* Looks for string based sub-input formatted { SUB-INPUT }. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400153uword
154unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
157 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 uword c;
159
160 while (1)
161 {
162 c = unformat_get_input (i);
163 switch (c)
164 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 case ' ':
166 case '\t':
167 case '\n':
168 case '\r':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 case '\f':
170 break;
171
172 case '{':
173 default:
174 /* Put back paren. */
175 if (c != UNFORMAT_END_OF_INPUT)
176 unformat_put_input (i);
177
178 if (c == '{' && unformat (i, "%v", &s))
179 {
180 unformat_init_vector (sub_input, s);
181 return 1;
182 }
183 return 0;
184 }
185 }
186 return 0;
187}
188
189static vlib_cli_command_t *
190get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
191{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 return vec_elt_at_index (cm->commands, s->index);
194}
195
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196static uword
197unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400199 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
200 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
201 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
202 vlib_cli_main_t *cm = &vm->cli_main;
203 uword *match_bitmap, is_unique, index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
205 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400206 vlib_cli_sub_rule_t *sr;
207 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 vec_foreach (sr, c->sub_rules)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400209 {
210 void **d;
211 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
212 vec_add2 (cm->parse_rule_data, d, 1);
213 vec_reset_length (d[0]);
214 if (r->data_size)
215 d[0] = _vec_resize (d[0],
216 /* length increment */ 1,
217 r->data_size,
218 /* header_bytes */ 0,
219 /* data align */ sizeof (uword));
220 if (unformat_user (i, r->unformat_function, vm, d[0]))
221 {
222 *result = vec_elt_at_index (cm->commands, sr->command_index);
223 return 1;
224 }
225 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 }
227
228 match_bitmap = vlib_cli_sub_command_match (c, i);
229 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
230 index = ~0;
231 if (is_unique)
232 {
233 index = clib_bitmap_first_set (match_bitmap);
234 *result = get_sub_command (cm, c, index);
235 }
236 clib_bitmap_free (match_bitmap);
237
238 return is_unique;
239}
240
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200241static int
242vlib_cli_cmp_strings (void *a1, void *a2)
243{
244 u8 *c1 = *(u8 **) a1;
245 u8 *c2 = *(u8 **) a2;
246
247 return vec_cmp (c1, c2);
248}
249
250u8 **
251vlib_cli_get_possible_completions (u8 * str)
252{
253 vlib_cli_command_t *c;
254 vlib_cli_sub_command_t *sc;
255 vlib_main_t *vm = vlib_get_main ();
256 vlib_cli_main_t *vcm = &vm->cli_main;
257 uword *match_bitmap = 0;
258 uword index, is_unique, help_next_level;
259 u8 **result = 0;
260 unformat_input_t input;
261 unformat_init_vector (&input, vec_dup (str));
262 c = vec_elt_at_index (vcm->commands, 0);
263
264 /* remove trailing whitespace, except for one of them */
265 while (vec_len (input.buffer) >= 2 &&
266 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
267 isspace (input.buffer[vec_len (input.buffer) - 2]))
268 {
269 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
270 }
271
272 /* if input is empty, directly return list of root commands */
273 if (vec_len (input.buffer) == 0 ||
274 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
275 {
276 vec_foreach (sc, c->sub_commands)
277 {
278 vec_add1 (result, (u8 *) sc->name);
279 }
280 goto done;
281 }
282
283 /* add a trailing '?' so that vlib_cli_sub_command_match can find
284 * all commands starting with the input string */
285 vec_add1 (input.buffer, '?');
286
287 while (1)
288 {
289 match_bitmap = vlib_cli_sub_command_match (c, &input);
290 /* no match: return no result */
291 if (match_bitmap == 0)
292 {
293 goto done;
294 }
295 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
296 /* unique match: try to step one subcommand level further */
297 if (is_unique)
298 {
299 /* stop if no more input */
300 if (input.index >= vec_len (input.buffer) - 1)
301 {
302 break;
303 }
304
305 index = clib_bitmap_first_set (match_bitmap);
306 c = get_sub_command (vcm, c, index);
307 clib_bitmap_free (match_bitmap);
308 continue;
309 }
310 /* multiple matches: stop here, return all matches */
311 break;
312 }
313
314 /* remove trailing '?' */
315 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
316
317 /* if we have a space at the end of input, and a unique match,
318 * autocomplete the next level of subcommands */
319 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
320 /* *INDENT-OFF* */
321 clib_bitmap_foreach(index, match_bitmap, {
322 if (help_next_level && is_unique) {
323 c = get_sub_command (vcm, c, index);
324 vec_foreach (sc, c->sub_commands) {
325 vec_add1 (result, (u8*) sc->name);
326 }
327 goto done; /* break doesn't work in this macro-loop */
328 }
329 sc = &c->sub_commands[index];
330 vec_add1(result, (u8*) sc->name);
331 });
332 /* *INDENT-ON* */
333
334done:
335 clib_bitmap_free (match_bitmap);
336 unformat_free (&input);
337
Yoann Desmouceaux4227eef2017-05-24 15:51:48 +0200338 if (result)
339 vec_sort_with_function (result, vlib_cli_cmp_strings);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200340 return result;
341}
342
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343static u8 *
344format_vlib_cli_command_help (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400346 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347 int is_long = va_arg (*args, int);
348 if (is_long && c->long_help)
349 s = format (s, "%s", c->long_help);
350 else if (c->short_help)
351 s = format (s, "%s", c->short_help);
352 else
353 s = format (s, "%v commands", c->path);
354 return s;
355}
356
Dave Barach9b8ffd92016-07-08 08:13:45 -0400357static u8 *
358format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 return format (s, "<%U>", format_c_identifier, r->name);
362}
363
Dave Barach9b8ffd92016-07-08 08:13:45 -0400364static u8 *
365format_vlib_cli_path (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 u8 *path = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 int i, in_rule;
369 in_rule = 0;
370 for (i = 0; i < vec_len (path); i++)
371 {
372 switch (path[i])
373 {
374 case '%':
375 in_rule = 1;
376 vec_add1 (s, '<'); /* start of <RULE> */
377 break;
378
379 case '_':
380 /* _ -> space in rules. */
381 vec_add1 (s, in_rule ? ' ' : '_');
382 break;
383
384 case ' ':
385 if (in_rule)
386 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387 vec_add1 (s, '>'); /* end of <RULE> */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 in_rule = 0;
389 }
390 vec_add1 (s, ' ');
391 break;
392
393 default:
394 vec_add1 (s, path[i]);
395 break;
396 }
397 }
398
399 if (in_rule)
400 vec_add1 (s, '>'); /* terminate <RULE> */
401
402 return s;
403}
404
405static vlib_cli_command_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400408 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
409 vlib_cli_sub_command_t *sc;
410 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
412 if (c->function)
413 vec_add1 (subs, c[0]);
414
415 vec_foreach (sr, c->sub_rules)
416 subs = all_subs (cm, subs, sr->command_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400417 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
419 return subs;
420}
421
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500422static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400423vlib_cli_cmp_rule (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500424{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400425 vlib_cli_sub_rule_t *r1 = a1;
426 vlib_cli_sub_rule_t *r2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500427
428 return vec_cmp (r1->name, r2->name);
429}
430
431static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400432vlib_cli_cmp_command (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500433{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400434 vlib_cli_command_t *c1 = a1;
435 vlib_cli_command_t *c2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500436
437 return vec_cmp (c1->path, c2->path);
438}
439
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440static clib_error_t *
441vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
442 vlib_cli_main_t * cm,
443 unformat_input_t * input,
444 uword parent_command_index)
445{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 vlib_cli_command_t *parent, *c;
447 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400449 u8 *string;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 uword is_main_dispatch = cm == &vm->cli_main;
451
452 parent = vec_elt_at_index (cm->commands, parent_command_index);
453 if (is_main_dispatch && unformat (input, "help"))
454 {
455 uword help_at_end_of_line, i;
456
Dave Barach9b8ffd92016-07-08 08:13:45 -0400457 help_at_end_of_line =
458 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 while (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 c = parent;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400462 if (unformat_user
463 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 ;
465
Dave Barach9b8ffd92016-07-08 08:13:45 -0400466 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 goto unknown;
468
469 else
470 break;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 }
472
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 /* help SUB-COMMAND => long format help.
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 "help" at end of line: show all commands. */
475 if (!help_at_end_of_line)
476 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
477 /* is_long */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
479 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
480 vlib_cli_output (vm, "%v: no sub-commands", c->path);
481
482 else
483 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400484 vlib_cli_sub_command_t *sc;
485 vlib_cli_sub_rule_t *sr, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
487 subs = vec_dup (c->sub_rules);
488
489 /* Add in rules if any. */
490 vec_foreach (sc, c->sub_commands)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400491 {
492 vec_add2 (subs, sr, 1);
493 sr->name = sc->name;
494 sr->command_index = sc->index;
495 sr->rule_index = ~0;
496 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500498 vec_sort_with_function (subs, vlib_cli_cmp_rule);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499
Dave Barach9b8ffd92016-07-08 08:13:45 -0400500 for (i = 0; i < vec_len (subs); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400502 vlib_cli_command_t *d;
503 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
505 d = vec_elt_at_index (cm->commands, subs[i].command_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400506 r =
507 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
508 subs
509 [i].rule_index) :
510 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
512 if (r)
513 vlib_cli_output
514 (vm, " %-30U %U",
515 format_vlib_cli_parse_rule_name, r,
516 format_vlib_cli_command_help, d, /* is_long */ 0);
517 else
518 vlib_cli_output
519 (vm, " %-30v %U",
520 subs[i].name,
521 format_vlib_cli_command_help, d, /* is_long */ 0);
522 }
523
524 vec_free (subs);
525 }
526 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400527
528 else if (is_main_dispatch
529 && (unformat (input, "choices") || unformat (input, "?")))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400531 vlib_cli_command_t *sub, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
533 subs = all_subs (cm, 0, parent_command_index);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500534 vec_sort_with_function (subs, vlib_cli_cmp_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535 vec_foreach (sub, subs)
536 vlib_cli_output (vm, " %-40U %U",
537 format_vlib_cli_path, sub->path,
538 format_vlib_cli_command_help, sub, /* is_long */ 0);
539 vec_free (subs);
540 }
541
542 else if (unformat (input, "comment %v", &string))
543 {
544 vec_free (string);
545 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 else if (unformat (input, "uncomment %U",
548 unformat_vlib_cli_sub_input, &sub_input))
549 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550 error =
551 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
552 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 unformat_free (&sub_input);
554 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400555 else if (unformat (input, "leak-check %U",
556 unformat_vlib_cli_sub_input, &sub_input))
557 {
558 u8 *leak_report;
559 clib_mem_trace (1);
560 error =
561 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
562 parent_command_index);
563 unformat_free (&sub_input);
564
565 /* Otherwise, the clib_error_t shows up as a leak... */
566 if (error)
567 {
568 vlib_cli_output (vm, "%v", error->what);
569 clib_error_free (error);
570 error = 0;
571 }
572
573 (void) clib_mem_trace_enable_disable (0);
574 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
575 1 /* verbose, i.e. print leaks */ );
576 clib_mem_trace (0);
577 vlib_cli_output (vm, "%v", leak_report);
578 vec_free (leak_report);
579 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580
581 else
582 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584 unformat_input_t *si;
585 uword has_sub_commands =
586 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
587
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588 si = input;
589 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
590 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400591
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 if (has_sub_commands)
593 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
594
Dave Barach9b8ffd92016-07-08 08:13:45 -0400595 if (has_sub_commands && !error)
596 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
598 else if (c->function)
599 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
602 /* Skip white space for benefit of called function. */
603 unformat_skip_white_space (si);
604
605 if (unformat (si, "?"))
606 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400607 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
608 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609 }
610 else
611 {
Dave Barachc3a06552018-10-01 09:25:32 -0400612 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
613 {
614 /* *INDENT-OFF* */
615 ELOG_TYPE_DECLARE (e) =
616 {
617 .format = "cli-cmd: %s",
618 .format_args = "T4",
619 };
620 /* *INDENT-ON* */
621 struct
622 {
623 u32 c;
624 } *ed;
625 ed = ELOG_DATA (&vm->elog_main, e);
626 ed->c = elog_global_id_for_msg_name (c->path);
627 }
628
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629 if (!c->is_mp_safe)
630 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
632 c_error = c->function (vm, si, c);
633
Dave Barach9b8ffd92016-07-08 08:13:45 -0400634 if (!c->is_mp_safe)
635 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Dave Barachc3a06552018-10-01 09:25:32 -0400637 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
638 {
639 /* *INDENT-OFF* */
640 ELOG_TYPE_DECLARE (e) =
641 {
642 .format = "cli-cmd: %s %s",
643 .format_args = "T4T4",
644 };
645 /* *INDENT-ON* */
646 struct
647 {
648 u32 c, err;
649 } *ed;
650 ed = ELOG_DATA (&vm->elog_main, e);
651 ed->c = elog_global_id_for_msg_name (c->path);
652 if (c_error)
653 {
654 vec_add1 (c_error->what, 0);
655 ed->err = elog_global_id_for_msg_name
656 ((const char *) c_error->what);
657 _vec_len (c_error->what) -= 1;
658 }
659 else
660 ed->err = elog_global_id_for_msg_name ("OK");
661 }
662
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663 if (c_error)
664 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400665 error =
666 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667 clib_error_free (c_error);
668 /* Free sub input. */
669 if (si != input)
670 unformat_free (si);
671
672 return error;
673 }
674 }
675
676 /* Free any previous error. */
677 clib_error_free (error);
678 }
679
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681 error = clib_error_return (0, "%v: no sub-commands", c->path);
682
683 /* Free sub input. */
684 if (si != input)
685 unformat_free (si);
686 }
687
688 else
689 goto unknown;
690
691 return error;
692
Dave Barach9b8ffd92016-07-08 08:13:45 -0400693unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
696 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400698 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
699 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700}
701
702
Dave Barach9b8ffd92016-07-08 08:13:45 -0400703void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
704 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Dave Barach9b8ffd92016-07-08 08:13:45 -0400706void
707vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
708{
709}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
711/* Process CLI input. */
Ole Troan72d87582019-05-10 12:01:10 +0200712int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400713vlib_cli_input (vlib_main_t * vm,
714 unformat_input_t * input,
715 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 vlib_process_t *cp = vlib_get_current_process (vm);
718 vlib_cli_main_t *cm = &vm->cli_main;
719 clib_error_t *error;
720 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 uword save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200722 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000724 save_function = cp->output_function;
725 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000727 cp->output_function = function;
728 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729
Dave Barach9b8ffd92016-07-08 08:13:45 -0400730 do
731 {
732 vec_reset_length (cm->parse_rule_data);
733 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
734 0);
735 }
736 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
738 if (error)
739 {
740 vlib_cli_output (vm, "%v", error->what);
741 vlib_unix_error_report (vm, error);
Ole Troan72d87582019-05-10 12:01:10 +0200742 /* clib_error_return is unfortunately often called with a '0'
743 return code */
744 rv = error->code != 0 ? error->code : -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 clib_error_free (error);
746 }
747
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000748 cp->output_function = save_function;
749 cp->output_function_arg = save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200750 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751}
752
753/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400754void
755vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400759 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
761 va_start (va, fmt);
762 s = va_format (0, fmt, &va);
763 va_end (va);
764
765 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400766 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 vec_add1 (s, '\n');
768
Dave Barach9b8ffd92016-07-08 08:13:45 -0400769 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770 fformat (stdout, "%v", s);
771 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000772 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773
774 vec_free (s);
775}
776
Ole Troan73710c72018-06-04 22:27:49 +0200777void *vl_msg_push_heap (void) __attribute__ ((weak));
778void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
779
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780static clib_error_t *
781show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400782 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783{
Dave Barach6a5adc32018-07-04 10:56:23 -0400784 int verbose __attribute__ ((unused)) = 0, api_segment = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400785 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786 u32 index = 0;
787
Dave Barach9b8ffd92016-07-08 08:13:45 -0400788 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400790 if (unformat (input, "verbose"))
791 verbose = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200792 else if (unformat (input, "api-segment"))
793 api_segment = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400794 else
795 {
796 error = clib_error_return (0, "unknown input `%U'",
797 format_unformat_error, input);
798 return error;
799 }
800 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801
Ole Troan73710c72018-06-04 22:27:49 +0200802 if (api_segment)
803 {
804 void *oldheap = vl_msg_push_heap ();
805 u8 *s_in_svm =
806 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
807 vl_msg_pop_heap (oldheap);
808 u8 *s = vec_dup (s_in_svm);
809
810 oldheap = vl_msg_push_heap ();
811 vec_free (s_in_svm);
812 vl_msg_pop_heap (oldheap);
813 vlib_cli_output (vm, "API segment start:");
814 vlib_cli_output (vm, "%v", s);
815 vlib_cli_output (vm, "API segment end:");
816 vec_free (s);
817 }
818
Dave Barach6a5adc32018-07-04 10:56:23 -0400819#if USE_DLMALLOC == 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400820 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 foreach_vlib_main (
822 ({
Damjan Marion926b5642018-07-02 21:33:31 +0200823 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
Dave Barach6a5adc32018-07-04 10:56:23 -0400824 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
Damjan Marion926b5642018-07-02 21:33:31 +0200825 vlib_worker_threads[index].name);
826 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
827 h->vm_alloc_offset_from_header,
828 h->vm_alloc_size);
Dave Barach6a5adc32018-07-04 10:56:23 -0400829 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
830 verbose);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 index++;
832 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400833 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400834#else
835 {
836 uword clib_mem_trace_enable_disable (uword enable);
837 uword was_enabled;
838
839 /*
840 * Note: the foreach_vlib_main cause allocator traffic,
841 * so shut off tracing before we go there...
842 */
843 was_enabled = clib_mem_trace_enable_disable (0);
844
845 /* *INDENT-OFF* */
846 foreach_vlib_main (
847 ({
Dave Barachaf7dd5b2018-08-23 17:08:44 -0400848 struct dlmallinfo mi;
Dave Barach6a5adc32018-07-04 10:56:23 -0400849 void *mspace;
850 mspace = clib_per_cpu_mheaps[index];
851
852 mi = mspace_mallinfo (mspace);
853 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
854 vlib_worker_threads[index].name);
855 vlib_cli_output (vm, " %U\n", format_page_map,
856 pointer_to_uword (mspace_least_addr(mspace)),
857 mi.arena);
858 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
859 verbose);
860 index++;
861 }));
862 /* *INDENT-ON* */
863
864 /* Restore the trace flag */
865 clib_mem_trace_enable_disable (was_enabled);
866 }
867#endif /* USE_DLMALLOC */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868 return 0;
869}
870
Dave Barach9b8ffd92016-07-08 08:13:45 -0400871/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
873 .path = "show memory",
Ole Troan73710c72018-06-04 22:27:49 +0200874 .short_help = "[verbose | api-segment] Show current memory usage",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 .function = show_memory_usage,
876};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400877/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878
879static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100880show_cpu (vlib_main_t * vm, unformat_input_t * input,
881 vlib_cli_command_t * cmd)
882{
883#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
884 _("Model name", "%U", format_cpu_model_name);
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800885 _("Microarch model (family)", "%U", format_cpu_uarch);
Damjan Marioneccf5092016-11-18 16:59:24 +0100886 _("Flags", "%U", format_cpu_flags);
887 _("Base frequency", "%.2f GHz",
888 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
889#undef _
890 return 0;
891}
892
893/*?
894 * Displays various information about the CPU.
895 *
896 * @cliexpar
897 * @cliexstart{show cpu}
898 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
899 * Microarchitecture: Broadwell (Broadwell-EP/EX)
900 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
901 * Base Frequency: 3.20 GHz
902 * @cliexend
903?*/
904/* *INDENT-OFF* */
905VLIB_CLI_COMMAND (show_cpu_command, static) = {
906 .path = "show cpu",
907 .short_help = "Show cpu information",
908 .function = show_cpu,
909};
910
911/* *INDENT-ON* */
Ole Troan73710c72018-06-04 22:27:49 +0200912
Damjan Marioneccf5092016-11-18 16:59:24 +0100913static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914enable_disable_memory_trace (vlib_main_t * vm,
915 unformat_input_t * input,
916 vlib_cli_command_t * cmd)
917{
Ole Troan73710c72018-06-04 22:27:49 +0200918 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 int enable;
Ole Troan73710c72018-06-04 22:27:49 +0200920 int api_segment = 0;
921 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922
Ole Troan73710c72018-06-04 22:27:49 +0200923
924 if (!unformat_user (input, unformat_line_input, line_input))
925 return 0;
926
927 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 {
Dave Barach1ddbc012018-06-13 09:26:05 -0400929 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
Ole Troan73710c72018-06-04 22:27:49 +0200930 ;
931 else if (unformat (line_input, "api-segment"))
932 api_segment = 1;
933 else
934 {
935 unformat_free (line_input);
936 return clib_error_return (0, "invalid input");
937 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938 }
Ole Troan73710c72018-06-04 22:27:49 +0200939 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940
Ole Troan73710c72018-06-04 22:27:49 +0200941 if (api_segment)
942 oldheap = vl_msg_push_heap ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 clib_mem_trace (enable);
Ole Troan73710c72018-06-04 22:27:49 +0200944 if (api_segment)
945 vl_msg_pop_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946
Ole Troan73710c72018-06-04 22:27:49 +0200947 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948}
949
Dave Barach9b8ffd92016-07-08 08:13:45 -0400950/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
952 .path = "memory-trace",
Ole Troan73710c72018-06-04 22:27:49 +0200953 .short_help = "on|off [api-segment] Enable/disable memory allocation trace",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954 .function = enable_disable_memory_trace,
955};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400956/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
958
959static clib_error_t *
960test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400961 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962{
Dave Barach6a5adc32018-07-04 10:56:23 -0400963#if USE_DLMALLOC == 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400964 clib_error_t *error = 0;
965 void *heap;
966 mheap_t *mheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967
Dave Barach9b8ffd92016-07-08 08:13:45 -0400968 if (unformat (input, "on"))
969 {
970 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +0200972 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973 mheap = mheap_header(heap);
974 mheap->flags |= MHEAP_FLAG_VALIDATE;
975 // Turn off small object cache because it delays detection of errors
976 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
977 });
Dave Barach9b8ffd92016-07-08 08:13:45 -0400978 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
Dave Barach9b8ffd92016-07-08 08:13:45 -0400980 }
981 else if (unformat (input, "off"))
982 {
983 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +0200985 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986 mheap = mheap_header(heap);
987 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
988 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
989 });
Dave Barach9b8ffd92016-07-08 08:13:45 -0400990 /* *INDENT-ON* */
991 }
992 else if (unformat (input, "now"))
993 {
994 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +0200996 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 mheap = mheap_header(heap);
998 mheap_validate(heap);
999 });
Dave Barach9b8ffd92016-07-08 08:13:45 -04001000 /* *INDENT-ON* */
1001 vlib_cli_output (vm, "heap validation complete");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002
Dave Barach9b8ffd92016-07-08 08:13:45 -04001003 }
1004 else
1005 {
1006 return clib_error_return (0, "unknown input `%U'",
1007 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 }
1009
Dave Barach9b8ffd92016-07-08 08:13:45 -04001010 return error;
Dave Barach6a5adc32018-07-04 10:56:23 -04001011#else
1012 return clib_error_return (0, "unimplemented...");
1013#endif /* USE_DLMALLOC */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014}
1015
Dave Barach9b8ffd92016-07-08 08:13:45 -04001016/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1018 .path = "test heap-validate",
1019 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1020 .function = test_heap_validate,
1021};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001022/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
Damjan Marione5ef1d72017-03-02 12:33:48 +01001024static clib_error_t *
1025restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1026 vlib_cli_command_t * cmd)
1027{
Chris Luke6edd3602018-06-12 22:45:06 -04001028 clib_file_main_t *fm = &file_main;
1029 clib_file_t *f;
Damjan Marione5ef1d72017-03-02 12:33:48 +01001030
Chris Luke6edd3602018-06-12 22:45:06 -04001031 /* environ(7) does not indicate a header for this */
1032 extern char **environ;
1033
1034 /* Close all known open files */
1035 /* *INDENT-OFF* */
1036 pool_foreach(f, fm->file_pool,
1037 ({
1038 if (f->file_descriptor > 2)
1039 close(f->file_descriptor);
1040 }));
1041 /* *INDENT-ON* */
1042
1043 /* Exec ourself */
1044 execve (vm->name, (char **) vm->argv, environ);
Damjan Marione5ef1d72017-03-02 12:33:48 +01001045
1046 return 0;
1047}
1048
1049/* *INDENT-OFF* */
1050VLIB_CLI_COMMAND (restart_cmd,static) = {
1051 .path = "restart",
1052 .short_help = "restart process",
1053 .function = restart_cmd_fn,
1054};
1055/* *INDENT-ON* */
1056
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001057#ifdef TEST_CODE
1058/*
1059 * A trivial test harness to verify the per-process output_function
1060 * is working correcty.
1061 */
1062
1063static clib_error_t *
1064sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001065 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001066{
1067 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001068 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001069
Dave Barach9b8ffd92016-07-08 08:13:45 -04001070 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001071
Dave Barach9b8ffd92016-07-08 08:13:45 -04001072 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001073 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001074 vlib_process_wait_for_event_or_clock (vm, 1.0);
1075 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001076 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001077 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001078 return 0;
1079}
1080
Dave Barach9b8ffd92016-07-08 08:13:45 -04001081/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001082VLIB_CLI_COMMAND (ping_command, static) = {
1083 .path = "test sleep",
1084 .function = sleep_ten_seconds,
1085 .short_help = "Sleep for 10 seconds",
1086};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001087/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001088#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089
Dave Barach9b8ffd92016-07-08 08:13:45 -04001090static uword
1091vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001093 char *i = input;
1094 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 uword l = 0;
1096 uword index_of_last_space = ~0;
1097
1098 while (*i != 0)
1099 {
1100 u8 c = *i++;
1101 /* Multiple white space -> single space. */
1102 switch (c)
1103 {
1104 case ' ':
1105 case '\t':
1106 case '\n':
1107 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -04001108 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109 {
1110 vec_add1 (s, ' ');
1111 l++;
1112 }
1113 break;
1114
1115 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001116 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117 index_of_last_space = vec_len (s);
1118 vec_add1 (s, c);
1119 l++;
1120 break;
1121 }
1122 }
1123
1124 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001125 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 _vec_len (s) -= 1;
1127
1128 *result = s;
1129 return index_of_last_space;
1130}
1131
1132always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001133parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134{
1135 word i;
1136 for (i = vec_len (path) - 1; i >= 0; i--)
1137 {
1138 if (path[i] == ' ')
1139 return i;
1140 }
1141 return ~0;
1142}
1143
Dave Barach9b8ffd92016-07-08 08:13:45 -04001144static void
1145add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001147 vlib_cli_command_t *p, *c;
1148 vlib_cli_sub_command_t *sub_c;
1149 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150 word i, l;
1151
1152 p = vec_elt_at_index (cm->commands, parent_index);
1153 c = vec_elt_at_index (cm->commands, child_index);
1154
1155 l = parent_path_len (c->path);
1156 if (l == ~0)
1157 sub_name = vec_dup ((u8 *) c->path);
1158 else
1159 {
1160 ASSERT (l + 1 < vec_len (c->path));
1161 sub_name = 0;
1162 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1163 }
1164
1165 if (sub_name[0] == '%')
1166 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001167 uword *q;
1168 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169
1170 /* Remove %. */
1171 vec_delete (sub_name, 1, 0);
1172
Dave Barach9b8ffd92016-07-08 08:13:45 -04001173 if (!p->sub_rule_index_by_name)
1174 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1175 sizeof (sub_name[0]),
1176 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1178 if (q)
1179 {
1180 sr = vec_elt_at_index (p->sub_rules, q[0]);
1181 ASSERT (sr->command_index == child_index);
1182 return;
1183 }
1184
1185 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001186 if (!q)
Ed Warnicke853e7202016-08-12 11:42:26 -07001187 {
1188 clib_error ("reference to unknown rule `%%%v' in path `%v'",
1189 sub_name, c->path);
1190 return;
1191 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
Dave Barach9b8ffd92016-07-08 08:13:45 -04001193 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1194 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195 vec_add2 (p->sub_rules, sr, 1);
1196 sr->name = sub_name;
1197 sr->rule_index = q[0];
1198 sr->command_index = child_index;
1199 return;
1200 }
1201
Dave Barach9b8ffd92016-07-08 08:13:45 -04001202 if (!p->sub_command_index_by_name)
1203 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1204 sizeof (c->path[0]),
1205 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206
1207 /* Check if sub-command has already been created. */
1208 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1209 {
1210 vec_free (sub_name);
1211 return;
1212 }
1213
1214 vec_add2 (p->sub_commands, sub_c, 1);
1215 sub_c->index = child_index;
1216 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001217 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1218 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219
1220 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1221 for (i = 0; i < vec_len (sub_c->name); i++)
1222 {
1223 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001224 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
1226 pos = vec_elt_at_index (p->sub_command_positions, i);
1227
Dave Barach9b8ffd92016-07-08 08:13:45 -04001228 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229 pos->min_char = sub_c->name[i];
1230
1231 n = sub_c->name[i] - pos->min_char;
1232 if (n < 0)
1233 {
1234 pos->min_char = sub_c->name[i];
1235 vec_insert (pos->bitmaps, -n, 0);
1236 n = 0;
1237 }
1238
1239 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001240 pos->bitmaps[n] =
1241 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 }
1243}
1244
1245static void
1246vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1247{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001248 uword p_len, pi, *p;
1249 char *p_path;
1250 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251
1252 /* Root command (index 0) should have already been added. */
1253 ASSERT (vec_len (cm->commands) > 0);
1254
1255 c = vec_elt_at_index (cm->commands, ci);
1256 p_len = parent_path_len (c->path);
1257
Dave Barach9b8ffd92016-07-08 08:13:45 -04001258 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259 if (p_len == ~0)
1260 {
1261 add_sub_command (cm, 0, ci);
1262 return;
1263 }
1264
1265 p_path = 0;
1266 vec_add (p_path, c->path, p_len);
1267
1268 p = hash_get_mem (cm->command_index_by_path, p_path);
1269
1270 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001271 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001272 {
1273 /* Parent does not exist; create it. */
1274 vec_add2 (cm->commands, parent, 1);
1275 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001276 hash_set_mem (cm->command_index_by_path, parent->path,
1277 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278 pi = parent - cm->commands;
1279 }
1280 else
1281 {
1282 pi = p[0];
1283 vec_free (p_path);
1284 }
1285
1286 add_sub_command (cm, pi, ci);
1287
1288 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001289 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290 vlib_cli_make_parent (cm, pi);
1291}
1292
1293always_inline uword
1294vlib_cli_command_is_empty (vlib_cli_command_t * c)
1295{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001296 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001297}
1298
Dave Barach9b8ffd92016-07-08 08:13:45 -04001299clib_error_t *
1300vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001302 vlib_cli_main_t *cm = &vm->cli_main;
1303 clib_error_t *error = 0;
1304 uword ci, *p;
1305 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001306
1307 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1308 return error;
1309
1310 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1311
Dave Barach9b8ffd92016-07-08 08:13:45 -04001312 if (!cm->command_index_by_path)
1313 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001314 sizeof (c->path[0]),
1315 sizeof (uword));
1316
1317 /* See if command already exists with given path. */
1318 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1319 if (p)
1320 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001321 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001322
1323 ci = p[0];
1324 d = vec_elt_at_index (cm->commands, ci);
1325
1326 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001327 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328 if (vlib_cli_command_is_empty (d))
1329 {
1330 vlib_cli_command_t save = d[0];
1331
Dave Barach9b8ffd92016-07-08 08:13:45 -04001332 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333
1334 /* Copy callers fields. */
1335 d[0] = c[0];
1336
1337 /* Save internal fields. */
1338 d->path = save.path;
1339 d->sub_commands = save.sub_commands;
1340 d->sub_command_index_by_name = save.sub_command_index_by_name;
1341 d->sub_command_positions = save.sub_command_positions;
1342 d->sub_rules = save.sub_rules;
1343 }
1344 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001345 error =
1346 clib_error_return (0, "duplicate command name with path %v",
1347 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001348
1349 vec_free (normalized_path);
1350 if (error)
1351 return error;
1352 }
1353 else
1354 {
1355 /* Command does not exist: create it. */
1356
1357 /* Add root command (index 0). */
1358 if (vec_len (cm->commands) == 0)
1359 {
1360 /* Create command with index 0; path is empty string. */
1361 vec_resize (cm->commands, 1);
1362 }
1363
1364 ci = vec_len (cm->commands);
1365 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1366 vec_add1 (cm->commands, c[0]);
1367
1368 c = vec_elt_at_index (cm->commands, ci);
1369 c->path = normalized_path;
1370
1371 /* Don't inherit from registration. */
1372 c->sub_commands = 0;
1373 c->sub_command_index_by_name = 0;
1374 c->sub_command_positions = 0;
1375 }
1376
1377 vlib_cli_make_parent (cm, ci);
1378 return 0;
1379}
1380
1381clib_error_t *
1382vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1383{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001384 vlib_cli_main_t *cm = &vm->cli_main;
1385 vlib_cli_parse_rule_t *r;
1386 clib_error_t *error = 0;
1387 u8 *r_name;
1388 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001389
Dave Barach9b8ffd92016-07-08 08:13:45 -04001390 if (!cm->parse_rule_index_by_name)
1391 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001392 sizeof (r->name[0]),
1393 sizeof (uword));
1394
1395 /* Make vector copy of name. */
1396 r_name = format (0, "%s", r_reg->name);
1397
1398 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1399 {
1400 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001401 return clib_error_return (0, "duplicate parse rule name `%s'",
1402 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001403 }
1404
1405 vec_add2 (cm->parse_rules, r, 1);
1406 r[0] = r_reg[0];
1407 r->name = (char *) r_name;
1408 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1409
1410 return error;
1411}
1412
1413#if 0
1414/* $$$ turn back on again someday, maybe */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001415static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1416 vlib_cli_parse_rule_t *
1417 lo,
1418 vlib_cli_parse_rule_t *
1419 hi)
1420 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001421{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001422 clib_error_t *error = 0;
1423 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424
1425 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1426 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001427 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428 {
1429 error = clib_error_return (0, "parse rule with no name");
1430 goto done;
1431 }
1432
1433 error = vlib_cli_register_parse_rule (vm, r);
1434 if (error)
1435 goto done;
1436 }
1437
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439 return error;
1440}
1441#endif
1442
Damjan Marion9c2b9f42017-04-21 13:56:40 +02001443static int
1444cli_path_compare (void *a1, void *a2)
1445{
1446 u8 **s1 = a1;
1447 u8 **s2 = a2;
1448
1449 if ((vec_len (*s1) < vec_len (*s2)) &&
1450 memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0)
1451 return -1;
1452
1453
1454 if ((vec_len (*s1) > vec_len (*s2)) &&
1455 memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0)
1456 return 1;
1457
1458 return vec_cmp (*s1, *s2);
1459}
1460
1461static clib_error_t *
1462show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1463 vlib_cli_command_t * cmd)
1464{
1465 vlib_cli_main_t *cm = &vm->cli_main;
1466 vlib_cli_command_t *cli;
1467 u8 **paths = 0, **s;
1468
1469 /* *INDENT-OFF* */
1470 vec_foreach (cli, cm->commands)
1471 if (vec_len (cli->path) > 0)
1472 vec_add1 (paths, (u8 *) cli->path);
1473
1474 vec_sort_with_function (paths, cli_path_compare);
1475
1476 vec_foreach (s, paths)
1477 vlib_cli_output (vm, "%v", *s);
1478 /* *INDENT-ON* */
1479
1480 vec_free (paths);
1481 return 0;
1482}
1483
1484/* *INDENT-OFF* */
1485VLIB_CLI_COMMAND (show_cli_command, static) = {
1486 .path = "show cli",
1487 .short_help = "Show cli commands",
1488 .function = show_cli_cmd_fn,
1489};
1490/* *INDENT-ON* */
1491
Dave Barach9b8ffd92016-07-08 08:13:45 -04001492static clib_error_t *
Dave Barachc3a06552018-10-01 09:25:32 -04001493elog_trace_command_fn (vlib_main_t * vm,
1494 unformat_input_t * input, vlib_cli_command_t * cmd)
1495{
1496 unformat_input_t _line_input, *line_input = &_line_input;
1497 int enable = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001498 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1499 u32 circuit_node_index;
Dave Barachc3a06552018-10-01 09:25:32 -04001500
1501 if (!unformat_user (input, unformat_line_input, line_input))
1502 goto print_status;
1503
1504 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1505 {
1506 if (unformat (line_input, "api"))
1507 api = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001508 else if (unformat (line_input, "dispatch"))
1509 dispatch = 1;
1510 else if (unformat (line_input, "circuit-node %U",
1511 unformat_vlib_node, vm, &circuit_node_index))
1512 circuit = 1;
Dave Barachc3a06552018-10-01 09:25:32 -04001513 else if (unformat (line_input, "cli"))
1514 cli = 1;
1515 else if (unformat (line_input, "barrier"))
1516 barrier = 1;
1517 else if (unformat (line_input, "disable"))
1518 enable = 0;
1519 else if (unformat (line_input, "enable"))
1520 enable = 1;
1521 else
1522 break;
1523 }
1524 unformat_free (line_input);
1525
1526 vm->elog_trace_api_messages = api ? enable : vm->elog_trace_api_messages;
1527 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
Dave Barach900cbad2019-01-31 19:12:51 -05001528 vm->elog_trace_graph_dispatch = dispatch ?
1529 enable : vm->elog_trace_graph_dispatch;
1530 vm->elog_trace_graph_circuit = circuit ?
1531 enable : vm->elog_trace_graph_circuit;
Dave Barachc3a06552018-10-01 09:25:32 -04001532 vlib_worker_threads->barrier_elog_enabled =
1533 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
Dave Barach900cbad2019-01-31 19:12:51 -05001534 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1535
1536 /*
1537 * Set up start-of-buffer logic-analyzer trigger
1538 * for main loop event logs, which are fairly heavyweight.
1539 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1540 * will fully disable the scheme when the elog buffer fills.
1541 */
1542 if (dispatch || circuit)
1543 {
1544 elog_main_t *em = &vm->elog_main;
1545
1546 em->n_total_events_disable_limit =
1547 em->n_total_events + vec_len (em->event_ring);
1548 }
1549
Dave Barachc3a06552018-10-01 09:25:32 -04001550
1551print_status:
1552 vlib_cli_output (vm, "Current status:");
1553
1554 vlib_cli_output
1555 (vm, " Event log API message trace: %s\n CLI command trace: %s",
1556 vm->elog_trace_api_messages ? "on" : "off",
1557 vm->elog_trace_cli_commands ? "on" : "off");
1558 vlib_cli_output
1559 (vm, " Barrier sync trace: %s",
1560 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
Dave Barach900cbad2019-01-31 19:12:51 -05001561 vlib_cli_output
1562 (vm, " Graph Dispatch: %s",
1563 vm->elog_trace_graph_dispatch ? "on" : "off");
1564 vlib_cli_output
1565 (vm, " Graph Circuit: %s",
1566 vm->elog_trace_graph_circuit ? "on" : "off");
1567 if (vm->elog_trace_graph_circuit)
1568 vlib_cli_output
1569 (vm, " node %U",
1570 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
Dave Barachc3a06552018-10-01 09:25:32 -04001571
1572 return 0;
1573}
1574
1575/*?
1576 * Control event logging of api, cli, and thread barrier events
1577 * With no arguments, displays the current trace status.
1578 * Name the event groups you wish to trace or stop tracing.
1579 *
1580 * @cliexpar
1581 * @clistart
1582 * elog trace api cli barrier
1583 * elog trace api cli barrier disable
Dave Barach900cbad2019-01-31 19:12:51 -05001584 * elog trace dispatch
1585 * elog trace circuit-node ethernet-input
Dave Barachc3a06552018-10-01 09:25:32 -04001586 * elog trace
1587 * @cliend
1588 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1589?*/
1590/* *INDENT-OFF* */
1591VLIB_CLI_COMMAND (elog_trace_command, static) =
1592{
1593 .path = "elog trace",
Dave Barach900cbad2019-01-31 19:12:51 -05001594 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1595 "[circuit-node <name> e.g. ethernet-input][disable]",
Dave Barachc3a06552018-10-01 09:25:32 -04001596 .function = elog_trace_command_fn,
1597};
1598/* *INDENT-ON* */
1599
1600static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -04001601vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001602{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001603 vlib_cli_main_t *cm = &vm->cli_main;
1604 clib_error_t *error = 0;
1605 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606
1607 cmd = cm->cli_command_registrations;
1608
1609 while (cmd)
1610 {
1611 error = vlib_cli_register (vm, cmd);
1612 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001613 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001614 cmd = cmd->next_cli_command;
1615 }
1616 return error;
1617}
1618
1619VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001620
1621/*
1622 * fd.io coding-style-patch-verification: ON
1623 *
1624 * Local Variables:
1625 * eval: (c-set-style "gnu")
1626 * End:
1627 */