blob: 467d1f7b906c7cb2fa2692a8736d8bdaf5b0b779 [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
Dave Baracha1f5a952019-11-01 11:24:43 -040047/** \file src/vlib/cli.c Debug CLI Implementation
48 */
49
Dave Barachb09f4d02019-07-15 16:00:03 -040050int vl_api_set_elog_trace_api_messages (int enable);
51int vl_api_get_elog_trace_api_messages (void);
52
Dave Barachd67a4282019-06-15 12:46:13 -040053static void *current_traced_heap;
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055/* Root of all show commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040056/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070057VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
58 .path = "show",
59 .short_help = "Show commands",
60};
Dave Barach9b8ffd92016-07-08 08:13:45 -040061/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
63/* Root of all clear commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040064/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070065VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
66 .path = "clear",
67 .short_help = "Clear commands",
68};
Dave Barach9b8ffd92016-07-08 08:13:45 -040069/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71/* Root of all set commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040072/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070073VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
74 .path = "set",
75 .short_help = "Set commands",
76};
Dave Barach9b8ffd92016-07-08 08:13:45 -040077/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79/* Root of all test commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040080/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070081VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
82 .path = "test",
83 .short_help = "Test commands",
84};
Dave Barach9b8ffd92016-07-08 08:13:45 -040085/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87/* Returns bitmap of commands which match key. */
88static uword *
89vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
90{
91 int i, n;
Dave Barach9b8ffd92016-07-08 08:13:45 -040092 uword *match = 0;
93 vlib_cli_parse_position_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
95 unformat_skip_white_space (input);
96
Dave Barach9b8ffd92016-07-08 08:13:45 -040097 for (i = 0;; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 {
99 uword k;
100
101 k = unformat_get_input (input);
102 switch (k)
103 {
104 case 'a' ... 'z':
105 case 'A' ... 'Z':
106 case '0' ... '9':
Dave Barach9b8ffd92016-07-08 08:13:45 -0400107 case '-':
108 case '_':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 break;
110
Dave Barach9b8ffd92016-07-08 08:13:45 -0400111 case ' ':
112 case '\t':
113 case '\r':
114 case '\n':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 case UNFORMAT_END_OF_INPUT:
116 /* White space or end of input removes any non-white
117 matches that were before possible. */
118 if (i < vec_len (c->sub_command_positions)
119 && clib_bitmap_count_set_bits (match) > 1)
120 {
121 p = vec_elt_at_index (c->sub_command_positions, i);
122 for (n = 0; n < vec_len (p->bitmaps); n++)
123 match = clib_bitmap_andnot (match, p->bitmaps[n]);
124 }
125 goto done;
126
127 default:
128 unformat_put_input (input);
129 goto done;
130 }
131
132 if (i >= vec_len (c->sub_command_positions))
133 {
134 no_match:
135 clib_bitmap_free (match);
136 return 0;
137 }
138
139 p = vec_elt_at_index (c->sub_command_positions, i);
140 if (vec_len (p->bitmaps) == 0)
141 goto no_match;
142
143 n = k - p->min_char;
144 if (n < 0 || n >= vec_len (p->bitmaps))
145 goto no_match;
146
147 if (i == 0)
148 match = clib_bitmap_dup (p->bitmaps[n]);
149 else
150 match = clib_bitmap_and (match, p->bitmaps[n]);
151
152 if (clib_bitmap_is_zero (match))
153 goto no_match;
154 }
155
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 return match;
158}
159
160/* Looks for string based sub-input formatted { SUB-INPUT }. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161uword
162unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400164 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
165 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 uword c;
167
168 while (1)
169 {
170 c = unformat_get_input (i);
171 switch (c)
172 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173 case ' ':
174 case '\t':
175 case '\n':
176 case '\r':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 case '\f':
178 break;
179
180 case '{':
181 default:
182 /* Put back paren. */
183 if (c != UNFORMAT_END_OF_INPUT)
184 unformat_put_input (i);
185
186 if (c == '{' && unformat (i, "%v", &s))
187 {
188 unformat_init_vector (sub_input, s);
189 return 1;
190 }
191 return 0;
192 }
193 }
194 return 0;
195}
196
197static vlib_cli_command_t *
198get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
199{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 return vec_elt_at_index (cm->commands, s->index);
202}
203
Dave Barach9b8ffd92016-07-08 08:13:45 -0400204static uword
205unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
208 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
209 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
210 vlib_cli_main_t *cm = &vm->cli_main;
211 uword *match_bitmap, is_unique, index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 match_bitmap = vlib_cli_sub_command_match (c, i);
214 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
215 index = ~0;
216 if (is_unique)
217 {
218 index = clib_bitmap_first_set (match_bitmap);
219 *result = get_sub_command (cm, c, index);
220 }
221 clib_bitmap_free (match_bitmap);
222
223 return is_unique;
224}
225
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200226static int
227vlib_cli_cmp_strings (void *a1, void *a2)
228{
229 u8 *c1 = *(u8 **) a1;
230 u8 *c2 = *(u8 **) a2;
231
232 return vec_cmp (c1, c2);
233}
234
235u8 **
236vlib_cli_get_possible_completions (u8 * str)
237{
238 vlib_cli_command_t *c;
239 vlib_cli_sub_command_t *sc;
240 vlib_main_t *vm = vlib_get_main ();
241 vlib_cli_main_t *vcm = &vm->cli_main;
242 uword *match_bitmap = 0;
243 uword index, is_unique, help_next_level;
244 u8 **result = 0;
245 unformat_input_t input;
246 unformat_init_vector (&input, vec_dup (str));
247 c = vec_elt_at_index (vcm->commands, 0);
248
249 /* remove trailing whitespace, except for one of them */
250 while (vec_len (input.buffer) >= 2 &&
251 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
252 isspace (input.buffer[vec_len (input.buffer) - 2]))
253 {
254 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
255 }
256
257 /* if input is empty, directly return list of root commands */
258 if (vec_len (input.buffer) == 0 ||
259 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
260 {
261 vec_foreach (sc, c->sub_commands)
262 {
263 vec_add1 (result, (u8 *) sc->name);
264 }
265 goto done;
266 }
267
268 /* add a trailing '?' so that vlib_cli_sub_command_match can find
269 * all commands starting with the input string */
270 vec_add1 (input.buffer, '?');
271
272 while (1)
273 {
274 match_bitmap = vlib_cli_sub_command_match (c, &input);
275 /* no match: return no result */
276 if (match_bitmap == 0)
277 {
278 goto done;
279 }
280 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
281 /* unique match: try to step one subcommand level further */
282 if (is_unique)
283 {
284 /* stop if no more input */
285 if (input.index >= vec_len (input.buffer) - 1)
286 {
287 break;
288 }
289
290 index = clib_bitmap_first_set (match_bitmap);
291 c = get_sub_command (vcm, c, index);
292 clib_bitmap_free (match_bitmap);
293 continue;
294 }
295 /* multiple matches: stop here, return all matches */
296 break;
297 }
298
299 /* remove trailing '?' */
300 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
301
302 /* if we have a space at the end of input, and a unique match,
303 * autocomplete the next level of subcommands */
304 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
305 /* *INDENT-OFF* */
306 clib_bitmap_foreach(index, match_bitmap, {
307 if (help_next_level && is_unique) {
308 c = get_sub_command (vcm, c, index);
309 vec_foreach (sc, c->sub_commands) {
310 vec_add1 (result, (u8*) sc->name);
311 }
312 goto done; /* break doesn't work in this macro-loop */
313 }
314 sc = &c->sub_commands[index];
315 vec_add1(result, (u8*) sc->name);
316 });
317 /* *INDENT-ON* */
318
319done:
320 clib_bitmap_free (match_bitmap);
321 unformat_free (&input);
322
Yoann Desmouceaux4227eef2017-05-24 15:51:48 +0200323 if (result)
324 vec_sort_with_function (result, vlib_cli_cmp_strings);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200325 return result;
326}
327
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328static u8 *
329format_vlib_cli_command_help (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400331 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 int is_long = va_arg (*args, int);
333 if (is_long && c->long_help)
334 s = format (s, "%s", c->long_help);
335 else if (c->short_help)
336 s = format (s, "%s", c->short_help);
337 else
338 s = format (s, "%v commands", c->path);
339 return s;
340}
341
Dave Barach9b8ffd92016-07-08 08:13:45 -0400342static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343format_vlib_cli_path (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400345 u8 *path = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Dave Barach6b3f25c2019-12-09 10:45:47 -0500347 s = format (s, "%v", path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
349 return s;
350}
351
352static vlib_cli_command_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
356 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
358 if (c->function)
359 vec_add1 (subs, c[0]);
360
Dave Barach9b8ffd92016-07-08 08:13:45 -0400361 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362
363 return subs;
364}
365
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500366static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367vlib_cli_cmp_rule (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500368{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400369 vlib_cli_sub_rule_t *r1 = a1;
370 vlib_cli_sub_rule_t *r2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500371
372 return vec_cmp (r1->name, r2->name);
373}
374
375static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400376vlib_cli_cmp_command (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500377{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400378 vlib_cli_command_t *c1 = a1;
379 vlib_cli_command_t *c2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500380
381 return vec_cmp (c1->path, c2->path);
382}
383
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384static clib_error_t *
385vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
386 vlib_cli_main_t * cm,
387 unformat_input_t * input,
388 uword parent_command_index)
389{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 vlib_cli_command_t *parent, *c;
391 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 u8 *string;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 uword is_main_dispatch = cm == &vm->cli_main;
395
396 parent = vec_elt_at_index (cm->commands, parent_command_index);
397 if (is_main_dispatch && unformat (input, "help"))
398 {
399 uword help_at_end_of_line, i;
400
Dave Barach9b8ffd92016-07-08 08:13:45 -0400401 help_at_end_of_line =
402 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 while (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 c = parent;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406 if (unformat_user
407 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 ;
409
Dave Barach9b8ffd92016-07-08 08:13:45 -0400410 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 goto unknown;
412
413 else
414 break;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400415 }
416
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 /* help SUB-COMMAND => long format help.
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418 "help" at end of line: show all commands. */
419 if (!help_at_end_of_line)
420 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
421 /* is_long */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Dave Barach6b3f25c2019-12-09 10:45:47 -0500423 else if (vec_len (c->sub_commands) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 vlib_cli_output (vm, "%v: no sub-commands", c->path);
425
426 else
427 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500428 vlib_cli_sub_rule_t *sr, *subs = 0;
Dave Barach6d5df8d2019-12-11 09:46:56 -0500429 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
Dave Barach6d5df8d2019-12-11 09:46:56 -0500431 vec_foreach (sc, c->sub_commands)
432 {
433 vec_add2 (subs, sr, 1);
434 sr->name = sc->name;
435 sr->command_index = sc->index;
436 sr->rule_index = ~0;
437 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500439 vec_sort_with_function (subs, vlib_cli_cmp_rule);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441 for (i = 0; i < vec_len (subs); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
445 d = vec_elt_at_index (cm->commands, subs[i].command_index);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500446 vlib_cli_output
447 (vm, " %-30v %U", subs[i].name,
448 format_vlib_cli_command_help, d, /* is_long */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 }
450
451 vec_free (subs);
452 }
453 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454
455 else if (is_main_dispatch
456 && (unformat (input, "choices") || unformat (input, "?")))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400458 vlib_cli_command_t *sub, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459
460 subs = all_subs (cm, 0, parent_command_index);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500461 vec_sort_with_function (subs, vlib_cli_cmp_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 vec_foreach (sub, subs)
463 vlib_cli_output (vm, " %-40U %U",
464 format_vlib_cli_path, sub->path,
465 format_vlib_cli_command_help, sub, /* is_long */ 0);
466 vec_free (subs);
467 }
468
469 else if (unformat (input, "comment %v", &string))
470 {
471 vec_free (string);
472 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 else if (unformat (input, "uncomment %U",
475 unformat_vlib_cli_sub_input, &sub_input))
476 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 error =
478 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
479 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 unformat_free (&sub_input);
481 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400482 else if (unformat (input, "leak-check %U",
483 unformat_vlib_cli_sub_input, &sub_input))
484 {
485 u8 *leak_report;
Dave Barachd67a4282019-06-15 12:46:13 -0400486 if (current_traced_heap)
487 {
488 void *oldheap;
489 oldheap = clib_mem_set_heap (current_traced_heap);
490 clib_mem_trace (0);
491 clib_mem_set_heap (oldheap);
492 current_traced_heap = 0;
493 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400494 clib_mem_trace (1);
495 error =
496 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
497 parent_command_index);
498 unformat_free (&sub_input);
499
500 /* Otherwise, the clib_error_t shows up as a leak... */
501 if (error)
502 {
503 vlib_cli_output (vm, "%v", error->what);
504 clib_error_free (error);
505 error = 0;
506 }
507
508 (void) clib_mem_trace_enable_disable (0);
509 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
510 1 /* verbose, i.e. print leaks */ );
511 clib_mem_trace (0);
512 vlib_cli_output (vm, "%v", leak_report);
513 vec_free (leak_report);
514 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400515
516 else
517 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400519 unformat_input_t *si;
520 uword has_sub_commands =
521 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
522
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 si = input;
524 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
525 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400526
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 if (has_sub_commands)
528 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
529
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 if (has_sub_commands && !error)
531 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
533 else if (c->function)
534 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
537 /* Skip white space for benefit of called function. */
538 unformat_skip_white_space (si);
539
540 if (unformat (si, "?"))
541 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
543 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 }
545 else
546 {
Dave Barachc3a06552018-10-01 09:25:32 -0400547 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
548 {
549 /* *INDENT-OFF* */
550 ELOG_TYPE_DECLARE (e) =
551 {
552 .format = "cli-cmd: %s",
553 .format_args = "T4",
554 };
555 /* *INDENT-ON* */
556 struct
557 {
558 u32 c;
559 } *ed;
560 ed = ELOG_DATA (&vm->elog_main, e);
Benoît Ganne62d9fda2019-12-16 15:49:47 +0100561 ed->c = elog_string (&vm->elog_main, "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400562 }
563
Dave Barach9b8ffd92016-07-08 08:13:45 -0400564 if (!c->is_mp_safe)
565 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566
Dave Baracha1f5a952019-11-01 11:24:43 -0400567 c->hit_counter++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568 c_error = c->function (vm, si, c);
569
Dave Barach9b8ffd92016-07-08 08:13:45 -0400570 if (!c->is_mp_safe)
571 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
Dave Barachc3a06552018-10-01 09:25:32 -0400573 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
574 {
575 /* *INDENT-OFF* */
576 ELOG_TYPE_DECLARE (e) =
577 {
578 .format = "cli-cmd: %s %s",
579 .format_args = "T4T4",
580 };
581 /* *INDENT-ON* */
582 struct
583 {
584 u32 c, err;
585 } *ed;
586 ed = ELOG_DATA (&vm->elog_main, e);
Benoît Ganne62d9fda2019-12-16 15:49:47 +0100587 ed->c = elog_string (&vm->elog_main, "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400588 if (c_error)
589 {
590 vec_add1 (c_error->what, 0);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500591 ed->err =
592 elog_string (&vm->elog_main, (char *) c_error->what);
Dave Barachc3a06552018-10-01 09:25:32 -0400593 _vec_len (c_error->what) -= 1;
594 }
595 else
Dave Barachb09f4d02019-07-15 16:00:03 -0400596 ed->err = elog_string (&vm->elog_main, "OK");
Dave Barachc3a06552018-10-01 09:25:32 -0400597 }
598
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 if (c_error)
600 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400601 error =
602 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603 clib_error_free (c_error);
604 /* Free sub input. */
605 if (si != input)
606 unformat_free (si);
607
608 return error;
609 }
610 }
611
612 /* Free any previous error. */
613 clib_error_free (error);
614 }
615
Dave Barach9b8ffd92016-07-08 08:13:45 -0400616 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 error = clib_error_return (0, "%v: no sub-commands", c->path);
618
619 /* Free sub input. */
620 if (si != input)
621 unformat_free (si);
622 }
623
624 else
625 goto unknown;
626
627 return error;
628
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400631 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
632 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400634 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
635 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636}
637
638
Dave Barach9b8ffd92016-07-08 08:13:45 -0400639void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
640 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641
Dave Barach9b8ffd92016-07-08 08:13:45 -0400642void
643vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
644{
645}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
647/* Process CLI input. */
Ole Troan72d87582019-05-10 12:01:10 +0200648int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649vlib_cli_input (vlib_main_t * vm,
650 unformat_input_t * input,
651 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400653 vlib_process_t *cp = vlib_get_current_process (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400654 clib_error_t *error;
655 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656 uword save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200657 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000659 save_function = cp->output_function;
660 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000662 cp->output_function = function;
663 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664
Dave Barach9b8ffd92016-07-08 08:13:45 -0400665 do
666 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500667 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
668 /* parent */ 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400669 }
670 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
672 if (error)
673 {
674 vlib_cli_output (vm, "%v", error->what);
675 vlib_unix_error_report (vm, error);
Ole Troan72d87582019-05-10 12:01:10 +0200676 /* clib_error_return is unfortunately often called with a '0'
677 return code */
678 rv = error->code != 0 ? error->code : -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679 clib_error_free (error);
680 }
681
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000682 cp->output_function = save_function;
683 cp->output_function_arg = save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200684 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685}
686
687/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400688void
689vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400691 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400693 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694
695 va_start (va, fmt);
696 s = va_format (0, fmt, &va);
697 va_end (va);
698
699 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400700 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 vec_add1 (s, '\n');
702
Dave Barach9b8ffd92016-07-08 08:13:45 -0400703 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704 fformat (stdout, "%v", s);
705 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000706 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
708 vec_free (s);
709}
710
Ole Troan73710c72018-06-04 22:27:49 +0200711void *vl_msg_push_heap (void) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400712void *
713vl_msg_push_heap (void)
714{
715 return 0;
716}
717
Ole Troan73710c72018-06-04 22:27:49 +0200718void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400719void
720vl_msg_pop_heap (void *oldheap)
721{
722}
723
724void *vlib_stats_push_heap (void *) __attribute__ ((weak));
725void *
726vlib_stats_push_heap (void *notused)
727{
728 return 0;
729}
Ole Troan73710c72018-06-04 22:27:49 +0200730
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731static clib_error_t *
732show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400733 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734{
Dave Barachd67a4282019-06-15 12:46:13 -0400735 int verbose __attribute__ ((unused)) = 0;
736 int api_segment = 0, stats_segment = 0, main_heap = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400737 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738 u32 index = 0;
Dave Barachd67a4282019-06-15 12:46:13 -0400739 uword clib_mem_trace_enable_disable (uword enable);
740 uword was_enabled;
741
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Dave Barach9b8ffd92016-07-08 08:13:45 -0400743 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400745 if (unformat (input, "verbose"))
746 verbose = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200747 else if (unformat (input, "api-segment"))
748 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400749 else if (unformat (input, "stats-segment"))
750 stats_segment = 1;
751 else if (unformat (input, "main-heap"))
752 main_heap = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400753 else
754 {
755 error = clib_error_return (0, "unknown input `%U'",
756 format_unformat_error, input);
757 return error;
758 }
759 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
Dave Barachd67a4282019-06-15 12:46:13 -0400761 if ((api_segment + stats_segment + main_heap) == 0)
762 return clib_error_return
763 (0, "Please supply one of api-segment, stats-segment or main-heap");
764
Ole Troan73710c72018-06-04 22:27:49 +0200765 if (api_segment)
766 {
767 void *oldheap = vl_msg_push_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400768 was_enabled = clib_mem_trace_enable_disable (0);
Ole Troan73710c72018-06-04 22:27:49 +0200769 u8 *s_in_svm =
770 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
771 vl_msg_pop_heap (oldheap);
772 u8 *s = vec_dup (s_in_svm);
773
774 oldheap = vl_msg_push_heap ();
775 vec_free (s_in_svm);
Dave Barachd67a4282019-06-15 12:46:13 -0400776 clib_mem_trace_enable_disable (was_enabled);
Ole Troan73710c72018-06-04 22:27:49 +0200777 vl_msg_pop_heap (oldheap);
Dave Barachd67a4282019-06-15 12:46:13 -0400778 vlib_cli_output (vm, "API segment");
Ole Troan73710c72018-06-04 22:27:49 +0200779 vlib_cli_output (vm, "%v", s);
Dave Barachd67a4282019-06-15 12:46:13 -0400780 vec_free (s);
781 }
782 if (stats_segment)
783 {
784 void *oldheap = vlib_stats_push_heap (0);
785 was_enabled = clib_mem_trace_enable_disable (0);
786 u8 *s_in_svm =
787 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
788 if (oldheap)
789 clib_mem_set_heap (oldheap);
790 u8 *s = vec_dup (s_in_svm);
791
792 oldheap = vlib_stats_push_heap (0);
793 vec_free (s_in_svm);
794 if (oldheap)
795 {
796 clib_mem_trace_enable_disable (was_enabled);
797 clib_mem_set_heap (oldheap);
798 }
799 vlib_cli_output (vm, "Stats segment");
800 vlib_cli_output (vm, "%v", s);
Ole Troan73710c72018-06-04 22:27:49 +0200801 vec_free (s);
802 }
803
Dave Barach6a5adc32018-07-04 10:56:23 -0400804#if USE_DLMALLOC == 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400805 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806 foreach_vlib_main (
807 ({
Damjan Marion926b5642018-07-02 21:33:31 +0200808 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
Dave Barach6a5adc32018-07-04 10:56:23 -0400809 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
Damjan Marion926b5642018-07-02 21:33:31 +0200810 vlib_worker_threads[index].name);
811 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
812 h->vm_alloc_offset_from_header,
813 h->vm_alloc_size);
Dave Barach6a5adc32018-07-04 10:56:23 -0400814 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
815 verbose);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816 index++;
817 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400818 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400819#else
820 {
Dave Barachd67a4282019-06-15 12:46:13 -0400821 if (main_heap)
822 {
823 /*
824 * Note: the foreach_vlib_main causes allocator traffic,
825 * so shut off tracing before we go there...
826 */
827 was_enabled = clib_mem_trace_enable_disable (0);
Dave Barach6a5adc32018-07-04 10:56:23 -0400828
Dave Barachd67a4282019-06-15 12:46:13 -0400829 /* *INDENT-OFF* */
830 foreach_vlib_main (
831 ({
832 struct dlmallinfo mi;
833 void *mspace;
834 mspace = clib_per_cpu_mheaps[index];
Dave Barach6a5adc32018-07-04 10:56:23 -0400835
Dave Barachd67a4282019-06-15 12:46:13 -0400836 mi = mspace_mallinfo (mspace);
837 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
838 vlib_worker_threads[index].name);
839 vlib_cli_output (vm, " %U\n", format_page_map,
840 pointer_to_uword (mspace_least_addr(mspace)),
841 mi.arena);
842 vlib_cli_output (vm, " %U\n", format_mheap,
843 clib_per_cpu_mheaps[index],
844 verbose);
845 index++;
846 }));
847 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400848
Dave Barachd67a4282019-06-15 12:46:13 -0400849 /* Restore the trace flag */
850 clib_mem_trace_enable_disable (was_enabled);
851 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400852 }
853#endif /* USE_DLMALLOC */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854 return 0;
855}
856
Dave Barach9b8ffd92016-07-08 08:13:45 -0400857/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
859 .path = "show memory",
Dave Barachd67a4282019-06-15 12:46:13 -0400860 .short_help = "show memory [api-segment][stats-segment][verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861 .function = show_memory_usage,
862};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400863/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
865static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100866show_cpu (vlib_main_t * vm, unformat_input_t * input,
867 vlib_cli_command_t * cmd)
868{
869#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
870 _("Model name", "%U", format_cpu_model_name);
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800871 _("Microarch model (family)", "%U", format_cpu_uarch);
Damjan Marioneccf5092016-11-18 16:59:24 +0100872 _("Flags", "%U", format_cpu_flags);
873 _("Base frequency", "%.2f GHz",
874 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
875#undef _
876 return 0;
877}
878
879/*?
880 * Displays various information about the CPU.
881 *
882 * @cliexpar
883 * @cliexstart{show cpu}
884 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
885 * Microarchitecture: Broadwell (Broadwell-EP/EX)
886 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
887 * Base Frequency: 3.20 GHz
888 * @cliexend
889?*/
890/* *INDENT-OFF* */
891VLIB_CLI_COMMAND (show_cpu_command, static) = {
892 .path = "show cpu",
893 .short_help = "Show cpu information",
894 .function = show_cpu,
895};
Damjan Marioneccf5092016-11-18 16:59:24 +0100896/* *INDENT-ON* */
Ole Troan73710c72018-06-04 22:27:49 +0200897
Damjan Marioneccf5092016-11-18 16:59:24 +0100898static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899enable_disable_memory_trace (vlib_main_t * vm,
900 unformat_input_t * input,
901 vlib_cli_command_t * cmd)
902{
Ole Troan73710c72018-06-04 22:27:49 +0200903 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachd67a4282019-06-15 12:46:13 -0400904 int enable = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200905 int api_segment = 0;
Dave Barachd67a4282019-06-15 12:46:13 -0400906 int stats_segment = 0;
907 int main_heap = 0;
Ole Troan73710c72018-06-04 22:27:49 +0200908 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909
Ole Troan73710c72018-06-04 22:27:49 +0200910 if (!unformat_user (input, unformat_line_input, line_input))
911 return 0;
912
913 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 {
Dave Barach1ddbc012018-06-13 09:26:05 -0400915 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
Ole Troan73710c72018-06-04 22:27:49 +0200916 ;
917 else if (unformat (line_input, "api-segment"))
918 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400919 else if (unformat (line_input, "stats-segment"))
920 stats_segment = 1;
921 else if (unformat (line_input, "main-heap"))
922 main_heap = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200923 else
924 {
925 unformat_free (line_input);
926 return clib_error_return (0, "invalid input");
927 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 }
Ole Troan73710c72018-06-04 22:27:49 +0200929 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930
Dave Barachd67a4282019-06-15 12:46:13 -0400931 if ((api_segment + stats_segment + main_heap + (enable == 0)) == 0)
932 {
933 return clib_error_return
934 (0, "Need one of main-heap, stats-segment or api-segment");
935 }
936
937 /* Turn off current trace, if any */
938 if (current_traced_heap)
939 {
940 void *oldheap;
941 oldheap = clib_mem_set_heap (current_traced_heap);
942 clib_mem_trace (0);
943 clib_mem_set_heap (oldheap);
944 current_traced_heap = 0;
945 }
946
947 if (enable == 0)
948 return 0;
949
950 /* API segment */
Ole Troan73710c72018-06-04 22:27:49 +0200951 if (api_segment)
Dave Barachd67a4282019-06-15 12:46:13 -0400952 {
953 oldheap = vl_msg_push_heap ();
954 current_traced_heap = clib_mem_get_heap ();
955 clib_mem_trace (1);
956 vl_msg_pop_heap (oldheap);
957
958 }
959
960 /* Stats segment */
961 if (stats_segment)
962 {
963 oldheap = vlib_stats_push_heap (0);
964 current_traced_heap = clib_mem_get_heap ();
965 clib_mem_trace (stats_segment);
966 /* We don't want to call vlib_stats_pop_heap... */
967 if (oldheap)
968 clib_mem_set_heap (oldheap);
969 }
970
971 /* main_heap */
972 if (main_heap)
973 {
974 current_traced_heap = clib_mem_get_heap ();
975 clib_mem_trace (main_heap);
976 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977
Ole Troan73710c72018-06-04 22:27:49 +0200978 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979}
980
Dave Barach9b8ffd92016-07-08 08:13:45 -0400981/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
983 .path = "memory-trace",
Dave Barachd67a4282019-06-15 12:46:13 -0400984 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 .function = enable_disable_memory_trace,
986};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400987/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988
989
990static clib_error_t *
991test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400992 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993{
Dave Barach6a5adc32018-07-04 10:56:23 -0400994#if USE_DLMALLOC == 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400995 clib_error_t *error = 0;
996 void *heap;
997 mheap_t *mheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998
Dave Barach9b8ffd92016-07-08 08:13:45 -0400999 if (unformat (input, "on"))
1000 {
1001 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +02001003 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 mheap = mheap_header(heap);
1005 mheap->flags |= MHEAP_FLAG_VALIDATE;
1006 // Turn off small object cache because it delays detection of errors
1007 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
1008 });
Dave Barach9b8ffd92016-07-08 08:13:45 -04001009 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010
Dave Barach9b8ffd92016-07-08 08:13:45 -04001011 }
1012 else if (unformat (input, "off"))
1013 {
1014 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +02001016 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 mheap = mheap_header(heap);
1018 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
1019 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
1020 });
Dave Barach9b8ffd92016-07-08 08:13:45 -04001021 /* *INDENT-ON* */
1022 }
1023 else if (unformat (input, "now"))
1024 {
1025 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +02001027 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028 mheap = mheap_header(heap);
1029 mheap_validate(heap);
1030 });
Dave Barach9b8ffd92016-07-08 08:13:45 -04001031 /* *INDENT-ON* */
1032 vlib_cli_output (vm, "heap validation complete");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033
Dave Barach9b8ffd92016-07-08 08:13:45 -04001034 }
1035 else
1036 {
1037 return clib_error_return (0, "unknown input `%U'",
1038 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039 }
1040
Dave Barach9b8ffd92016-07-08 08:13:45 -04001041 return error;
Dave Barach6a5adc32018-07-04 10:56:23 -04001042#else
1043 return clib_error_return (0, "unimplemented...");
1044#endif /* USE_DLMALLOC */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045}
1046
Dave Barach9b8ffd92016-07-08 08:13:45 -04001047/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1049 .path = "test heap-validate",
1050 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1051 .function = test_heap_validate,
1052};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001053/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
Damjan Marione5ef1d72017-03-02 12:33:48 +01001055static clib_error_t *
1056restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1057 vlib_cli_command_t * cmd)
1058{
Chris Luke6edd3602018-06-12 22:45:06 -04001059 clib_file_main_t *fm = &file_main;
1060 clib_file_t *f;
Damjan Marione5ef1d72017-03-02 12:33:48 +01001061
Chris Luke6edd3602018-06-12 22:45:06 -04001062 /* environ(7) does not indicate a header for this */
1063 extern char **environ;
1064
1065 /* Close all known open files */
1066 /* *INDENT-OFF* */
1067 pool_foreach(f, fm->file_pool,
1068 ({
1069 if (f->file_descriptor > 2)
1070 close(f->file_descriptor);
1071 }));
1072 /* *INDENT-ON* */
1073
1074 /* Exec ourself */
1075 execve (vm->name, (char **) vm->argv, environ);
Damjan Marione5ef1d72017-03-02 12:33:48 +01001076
1077 return 0;
1078}
1079
1080/* *INDENT-OFF* */
1081VLIB_CLI_COMMAND (restart_cmd,static) = {
1082 .path = "restart",
1083 .short_help = "restart process",
1084 .function = restart_cmd_fn,
1085};
1086/* *INDENT-ON* */
1087
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001088#ifdef TEST_CODE
1089/*
1090 * A trivial test harness to verify the per-process output_function
1091 * is working correcty.
1092 */
1093
1094static clib_error_t *
1095sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001096 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001097{
1098 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001099 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001100
Dave Barach9b8ffd92016-07-08 08:13:45 -04001101 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001102
Dave Barach9b8ffd92016-07-08 08:13:45 -04001103 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001104 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001105 vlib_process_wait_for_event_or_clock (vm, 1.0);
1106 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001107 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001108 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001109 return 0;
1110}
1111
Dave Barach9b8ffd92016-07-08 08:13:45 -04001112/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001113VLIB_CLI_COMMAND (ping_command, static) = {
1114 .path = "test sleep",
1115 .function = sleep_ten_seconds,
1116 .short_help = "Sleep for 10 seconds",
1117};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001118/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001119#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120
Dave Barach9b8ffd92016-07-08 08:13:45 -04001121static uword
1122vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001124 char *i = input;
1125 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 uword l = 0;
1127 uword index_of_last_space = ~0;
1128
1129 while (*i != 0)
1130 {
1131 u8 c = *i++;
1132 /* Multiple white space -> single space. */
1133 switch (c)
1134 {
1135 case ' ':
1136 case '\t':
1137 case '\n':
1138 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -04001139 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140 {
1141 vec_add1 (s, ' ');
1142 l++;
1143 }
1144 break;
1145
1146 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001147 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148 index_of_last_space = vec_len (s);
1149 vec_add1 (s, c);
1150 l++;
1151 break;
1152 }
1153 }
1154
1155 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001156 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157 _vec_len (s) -= 1;
1158
1159 *result = s;
1160 return index_of_last_space;
1161}
1162
1163always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001164parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165{
1166 word i;
1167 for (i = vec_len (path) - 1; i >= 0; i--)
1168 {
1169 if (path[i] == ' ')
1170 return i;
1171 }
1172 return ~0;
1173}
1174
Dave Barach9b8ffd92016-07-08 08:13:45 -04001175static void
1176add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001178 vlib_cli_command_t *p, *c;
1179 vlib_cli_sub_command_t *sub_c;
1180 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181 word i, l;
1182
1183 p = vec_elt_at_index (cm->commands, parent_index);
1184 c = vec_elt_at_index (cm->commands, child_index);
1185
1186 l = parent_path_len (c->path);
1187 if (l == ~0)
1188 sub_name = vec_dup ((u8 *) c->path);
1189 else
1190 {
1191 ASSERT (l + 1 < vec_len (c->path));
1192 sub_name = 0;
1193 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1194 }
1195
1196 if (sub_name[0] == '%')
1197 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001198 uword *q;
1199 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001200
1201 /* Remove %. */
1202 vec_delete (sub_name, 1, 0);
1203
Dave Barach9b8ffd92016-07-08 08:13:45 -04001204 if (!p->sub_rule_index_by_name)
1205 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1206 sizeof (sub_name[0]),
1207 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1209 if (q)
1210 {
1211 sr = vec_elt_at_index (p->sub_rules, q[0]);
1212 ASSERT (sr->command_index == child_index);
1213 return;
1214 }
1215
Dave Barach9b8ffd92016-07-08 08:13:45 -04001216 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1217 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218 vec_add2 (p->sub_rules, sr, 1);
1219 sr->name = sub_name;
1220 sr->rule_index = q[0];
1221 sr->command_index = child_index;
1222 return;
1223 }
1224
Dave Barach9b8ffd92016-07-08 08:13:45 -04001225 if (!p->sub_command_index_by_name)
1226 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1227 sizeof (c->path[0]),
1228 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229
1230 /* Check if sub-command has already been created. */
1231 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1232 {
1233 vec_free (sub_name);
1234 return;
1235 }
1236
1237 vec_add2 (p->sub_commands, sub_c, 1);
1238 sub_c->index = child_index;
1239 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001240 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1241 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242
1243 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1244 for (i = 0; i < vec_len (sub_c->name); i++)
1245 {
1246 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001247 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
1249 pos = vec_elt_at_index (p->sub_command_positions, i);
1250
Dave Barach9b8ffd92016-07-08 08:13:45 -04001251 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252 pos->min_char = sub_c->name[i];
1253
1254 n = sub_c->name[i] - pos->min_char;
1255 if (n < 0)
1256 {
1257 pos->min_char = sub_c->name[i];
1258 vec_insert (pos->bitmaps, -n, 0);
1259 n = 0;
1260 }
1261
1262 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001263 pos->bitmaps[n] =
1264 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265 }
1266}
1267
1268static void
1269vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1270{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001271 uword p_len, pi, *p;
1272 char *p_path;
1273 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001274
1275 /* Root command (index 0) should have already been added. */
1276 ASSERT (vec_len (cm->commands) > 0);
1277
1278 c = vec_elt_at_index (cm->commands, ci);
1279 p_len = parent_path_len (c->path);
1280
Dave Barach9b8ffd92016-07-08 08:13:45 -04001281 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282 if (p_len == ~0)
1283 {
1284 add_sub_command (cm, 0, ci);
1285 return;
1286 }
1287
1288 p_path = 0;
1289 vec_add (p_path, c->path, p_len);
1290
1291 p = hash_get_mem (cm->command_index_by_path, p_path);
1292
1293 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001294 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001295 {
1296 /* Parent does not exist; create it. */
1297 vec_add2 (cm->commands, parent, 1);
1298 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001299 hash_set_mem (cm->command_index_by_path, parent->path,
1300 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301 pi = parent - cm->commands;
1302 }
1303 else
1304 {
1305 pi = p[0];
1306 vec_free (p_path);
1307 }
1308
1309 add_sub_command (cm, pi, ci);
1310
1311 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001312 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001313 vlib_cli_make_parent (cm, pi);
1314}
1315
1316always_inline uword
1317vlib_cli_command_is_empty (vlib_cli_command_t * c)
1318{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001319 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001320}
1321
Dave Barach9b8ffd92016-07-08 08:13:45 -04001322clib_error_t *
1323vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001324{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001325 vlib_cli_main_t *cm = &vm->cli_main;
1326 clib_error_t *error = 0;
1327 uword ci, *p;
1328 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329
1330 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1331 return error;
1332
1333 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1334
Dave Barach9b8ffd92016-07-08 08:13:45 -04001335 if (!cm->command_index_by_path)
1336 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001337 sizeof (c->path[0]),
1338 sizeof (uword));
1339
1340 /* See if command already exists with given path. */
1341 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1342 if (p)
1343 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001344 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345
1346 ci = p[0];
1347 d = vec_elt_at_index (cm->commands, ci);
1348
1349 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001350 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351 if (vlib_cli_command_is_empty (d))
1352 {
1353 vlib_cli_command_t save = d[0];
1354
Dave Barach9b8ffd92016-07-08 08:13:45 -04001355 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001356
1357 /* Copy callers fields. */
1358 d[0] = c[0];
1359
1360 /* Save internal fields. */
1361 d->path = save.path;
1362 d->sub_commands = save.sub_commands;
1363 d->sub_command_index_by_name = save.sub_command_index_by_name;
1364 d->sub_command_positions = save.sub_command_positions;
1365 d->sub_rules = save.sub_rules;
1366 }
1367 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001368 error =
1369 clib_error_return (0, "duplicate command name with path %v",
1370 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371
1372 vec_free (normalized_path);
1373 if (error)
1374 return error;
1375 }
1376 else
1377 {
1378 /* Command does not exist: create it. */
1379
1380 /* Add root command (index 0). */
1381 if (vec_len (cm->commands) == 0)
1382 {
1383 /* Create command with index 0; path is empty string. */
1384 vec_resize (cm->commands, 1);
1385 }
1386
1387 ci = vec_len (cm->commands);
1388 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1389 vec_add1 (cm->commands, c[0]);
1390
1391 c = vec_elt_at_index (cm->commands, ci);
1392 c->path = normalized_path;
1393
1394 /* Don't inherit from registration. */
1395 c->sub_commands = 0;
1396 c->sub_command_index_by_name = 0;
1397 c->sub_command_positions = 0;
1398 }
1399
1400 vlib_cli_make_parent (cm, ci);
1401 return 0;
1402}
1403
Dave Barach6b3f25c2019-12-09 10:45:47 -05001404#if 0
1405/* $$$ turn back on again someday, maybe */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001406clib_error_t *
1407vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1408{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001409 vlib_cli_main_t *cm = &vm->cli_main;
1410 vlib_cli_parse_rule_t *r;
1411 clib_error_t *error = 0;
1412 u8 *r_name;
1413 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001414
Dave Barach9b8ffd92016-07-08 08:13:45 -04001415 if (!cm->parse_rule_index_by_name)
1416 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001417 sizeof (r->name[0]),
1418 sizeof (uword));
1419
1420 /* Make vector copy of name. */
1421 r_name = format (0, "%s", r_reg->name);
1422
1423 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1424 {
1425 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001426 return clib_error_return (0, "duplicate parse rule name `%s'",
1427 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001428 }
1429
1430 vec_add2 (cm->parse_rules, r, 1);
1431 r[0] = r_reg[0];
1432 r->name = (char *) r_name;
1433 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1434
1435 return error;
1436}
1437
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1439 vlib_cli_parse_rule_t *
1440 lo,
1441 vlib_cli_parse_rule_t *
1442 hi)
1443 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001445 clib_error_t *error = 0;
1446 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447
1448 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1449 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001450 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451 {
1452 error = clib_error_return (0, "parse rule with no name");
1453 goto done;
1454 }
1455
1456 error = vlib_cli_register_parse_rule (vm, r);
1457 if (error)
1458 goto done;
1459 }
1460
Dave Barach9b8ffd92016-07-08 08:13:45 -04001461done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462 return error;
1463}
1464#endif
1465
Dave Barach9b8ffd92016-07-08 08:13:45 -04001466static clib_error_t *
Dave Barachc3a06552018-10-01 09:25:32 -04001467elog_trace_command_fn (vlib_main_t * vm,
1468 unformat_input_t * input, vlib_cli_command_t * cmd)
1469{
1470 unformat_input_t _line_input, *line_input = &_line_input;
1471 int enable = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001472 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1473 u32 circuit_node_index;
Dave Barachc3a06552018-10-01 09:25:32 -04001474
1475 if (!unformat_user (input, unformat_line_input, line_input))
1476 goto print_status;
1477
1478 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1479 {
1480 if (unformat (line_input, "api"))
1481 api = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001482 else if (unformat (line_input, "dispatch"))
1483 dispatch = 1;
1484 else if (unformat (line_input, "circuit-node %U",
1485 unformat_vlib_node, vm, &circuit_node_index))
1486 circuit = 1;
Dave Barachc3a06552018-10-01 09:25:32 -04001487 else if (unformat (line_input, "cli"))
1488 cli = 1;
1489 else if (unformat (line_input, "barrier"))
1490 barrier = 1;
1491 else if (unformat (line_input, "disable"))
1492 enable = 0;
1493 else if (unformat (line_input, "enable"))
1494 enable = 1;
1495 else
1496 break;
1497 }
1498 unformat_free (line_input);
1499
Dave Barachb09f4d02019-07-15 16:00:03 -04001500 vl_api_set_elog_trace_api_messages
1501 (api ? enable : vl_api_get_elog_trace_api_messages ());
Dave Barachc3a06552018-10-01 09:25:32 -04001502 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
Dave Barach900cbad2019-01-31 19:12:51 -05001503 vm->elog_trace_graph_dispatch = dispatch ?
1504 enable : vm->elog_trace_graph_dispatch;
1505 vm->elog_trace_graph_circuit = circuit ?
1506 enable : vm->elog_trace_graph_circuit;
Dave Barachc3a06552018-10-01 09:25:32 -04001507 vlib_worker_threads->barrier_elog_enabled =
1508 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
Dave Barach900cbad2019-01-31 19:12:51 -05001509 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1510
1511 /*
1512 * Set up start-of-buffer logic-analyzer trigger
1513 * for main loop event logs, which are fairly heavyweight.
1514 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1515 * will fully disable the scheme when the elog buffer fills.
1516 */
1517 if (dispatch || circuit)
1518 {
1519 elog_main_t *em = &vm->elog_main;
1520
1521 em->n_total_events_disable_limit =
1522 em->n_total_events + vec_len (em->event_ring);
1523 }
1524
Dave Barachc3a06552018-10-01 09:25:32 -04001525
1526print_status:
1527 vlib_cli_output (vm, "Current status:");
1528
1529 vlib_cli_output
1530 (vm, " Event log API message trace: %s\n CLI command trace: %s",
Dave Barachb09f4d02019-07-15 16:00:03 -04001531 vl_api_get_elog_trace_api_messages ()? "on" : "off",
Dave Barachc3a06552018-10-01 09:25:32 -04001532 vm->elog_trace_cli_commands ? "on" : "off");
1533 vlib_cli_output
1534 (vm, " Barrier sync trace: %s",
1535 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
Dave Barach900cbad2019-01-31 19:12:51 -05001536 vlib_cli_output
1537 (vm, " Graph Dispatch: %s",
1538 vm->elog_trace_graph_dispatch ? "on" : "off");
1539 vlib_cli_output
1540 (vm, " Graph Circuit: %s",
1541 vm->elog_trace_graph_circuit ? "on" : "off");
1542 if (vm->elog_trace_graph_circuit)
1543 vlib_cli_output
1544 (vm, " node %U",
1545 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
Dave Barachc3a06552018-10-01 09:25:32 -04001546
1547 return 0;
1548}
1549
1550/*?
1551 * Control event logging of api, cli, and thread barrier events
1552 * With no arguments, displays the current trace status.
1553 * Name the event groups you wish to trace or stop tracing.
1554 *
1555 * @cliexpar
1556 * @clistart
1557 * elog trace api cli barrier
1558 * elog trace api cli barrier disable
Dave Barach900cbad2019-01-31 19:12:51 -05001559 * elog trace dispatch
1560 * elog trace circuit-node ethernet-input
Dave Barachc3a06552018-10-01 09:25:32 -04001561 * elog trace
1562 * @cliend
1563 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1564?*/
1565/* *INDENT-OFF* */
1566VLIB_CLI_COMMAND (elog_trace_command, static) =
1567{
1568 .path = "elog trace",
Dave Barach900cbad2019-01-31 19:12:51 -05001569 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1570 "[circuit-node <name> e.g. ethernet-input][disable]",
Dave Barachc3a06552018-10-01 09:25:32 -04001571 .function = elog_trace_command_fn,
1572};
1573/* *INDENT-ON* */
1574
1575static clib_error_t *
Dave Barachc4abafd2019-09-04 12:09:32 -04001576suspend_command_fn (vlib_main_t * vm,
1577 unformat_input_t * input, vlib_cli_command_t * cmd)
1578{
1579 vlib_process_suspend (vm, 30e-3);
1580 return 0;
1581}
1582
1583/* *INDENT-OFF* */
1584VLIB_CLI_COMMAND (suspend_command, static) =
1585{
1586 .path = "suspend",
1587 .short_help = "suspend debug CLI for 30ms",
1588 .function = suspend_command_fn,
Dave Baracha1f5a952019-11-01 11:24:43 -04001589 .is_mp_safe = 1,
1590};
1591/* *INDENT-ON* */
1592
1593
1594static int
1595sort_cmds_by_path (void *a1, void *a2)
1596{
1597 u32 *index1 = a1;
1598 u32 *index2 = a2;
1599 vlib_main_t *vm = vlib_get_main ();
1600 vlib_cli_main_t *cm = &vm->cli_main;
1601 vlib_cli_command_t *c1, *c2;
1602 int i, lmin;
1603
1604 c1 = vec_elt_at_index (cm->commands, *index1);
1605 c2 = vec_elt_at_index (cm->commands, *index2);
1606
1607 lmin = vec_len (c1->path);
1608 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1609
1610 for (i = 0; i < lmin; i++)
1611 {
1612 if (c1->path[i] < c2->path[i])
1613 return -1;
1614 else if (c1->path[i] > c2->path[i])
1615 return 1;
1616 }
1617
1618 return 0;
1619}
1620
1621typedef struct
1622{
1623 vlib_cli_main_t *cm;
1624 u32 parent_command_index;
1625 int show_mp_safe;
1626 int show_not_mp_safe;
1627 int show_hit;
1628 int clear_hit;
1629} vlib_cli_walk_args_t;
1630
1631static void
1632cli_recursive_walk (vlib_cli_walk_args_t * aa)
1633{
1634 vlib_cli_command_t *parent;
1635 vlib_cli_sub_command_t *sub;
1636 vlib_cli_walk_args_t _a, *a = &_a;
1637 vlib_cli_main_t *cm;
1638 int i;
1639
1640 /* Copy args into this stack frame */
1641 *a = *aa;
1642 cm = a->cm;
1643
1644 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1645
1646 if (parent->function)
1647 {
1648 if (((a->show_mp_safe && parent->is_mp_safe)
1649 || (a->show_not_mp_safe && !parent->is_mp_safe))
1650 && (a->show_hit == 0 || parent->hit_counter))
1651 {
1652 vec_add1 (cm->sort_vector, a->parent_command_index);
1653 }
1654
1655 if (a->clear_hit)
1656 parent->hit_counter = 0;
1657 }
1658
1659 for (i = 0; i < vec_len (parent->sub_commands); i++)
1660 {
1661 sub = vec_elt_at_index (parent->sub_commands, i);
1662 a->parent_command_index = sub->index;
1663 cli_recursive_walk (a);
1664 }
1665}
1666
1667static u8 *
1668format_mp_safe (u8 * s, va_list * args)
1669{
1670 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1671 int show_mp_safe = va_arg (*args, int);
1672 int show_not_mp_safe = va_arg (*args, int);
1673 int show_hit = va_arg (*args, int);
1674 int clear_hit = va_arg (*args, int);
1675 vlib_cli_command_t *c;
1676 vlib_cli_walk_args_t _a, *a = &_a;
1677 int i;
1678 char *format_string = "\n%v";
1679
1680 if (show_hit)
1681 format_string = "\n%v: %u";
1682
1683 vec_reset_length (cm->sort_vector);
1684
1685 a->cm = cm;
1686 a->parent_command_index = 0;
1687 a->show_mp_safe = show_mp_safe;
1688 a->show_not_mp_safe = show_not_mp_safe;
1689 a->show_hit = show_hit;
1690 a->clear_hit = clear_hit;
1691
1692 cli_recursive_walk (a);
1693
1694 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1695
1696 for (i = 0; i < vec_len (cm->sort_vector); i++)
1697 {
1698 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1699 s = format (s, format_string, c->path, c->hit_counter);
1700 }
1701
1702 return s;
1703}
1704
1705
1706static clib_error_t *
1707show_cli_command_fn (vlib_main_t * vm,
1708 unformat_input_t * input, vlib_cli_command_t * cmd)
1709{
1710 int show_mp_safe = 0;
1711 int show_not_mp_safe = 0;
1712 int show_hit = 0;
1713 int clear_hit = 0;
1714
1715 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1716 {
1717 if (unformat (input, "mp-safe"))
1718 show_mp_safe = 1;
1719 if (unformat (input, "not-mp-safe"))
1720 show_not_mp_safe = 1;
1721 else if (unformat (input, "hit"))
1722 show_hit = 1;
1723 else if (unformat (input, "clear-hit"))
1724 clear_hit = 1;
1725 else
1726 break;
1727 }
1728
1729 /* default set: all cli commands */
1730 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1731 show_mp_safe = show_not_mp_safe = 1;
1732
1733 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1734 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1735 if (clear_hit)
1736 vlib_cli_output (vm, "hit counters cleared...");
1737
1738 return 0;
1739}
1740
1741/*?
1742 * Displays debug cli command information
1743 *
1744 * @cliexpar
1745 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1746 *
1747 * "show cli" displays the entire debug cli:
1748 *
1749 * abf attach
1750 * abf policy
1751 * adjacency counters
1752 * api trace
1753 * app ns
1754 * bfd key del
1755 * ... and so on ...
1756 *
1757 * "show cli mp-safe" displays mp-safe debug CLI commands:
1758 *
1759 * abf policy
1760 * binary-api
1761 * create vhost-user
1762 * exec
1763 * ip container
1764 * ip mroute
1765 * ip probe-neighbor
1766 * ip route
1767 * ip scan-neighbor
1768 * ip table
1769 * ip6 table
1770 *
1771 * "show cli not-mp-safe" displays debug CLI commands
1772 * which cause worker thread barrier synchronization
1773 *
1774 * "show cli hit" displays commands which have been executed. Qualify
1775 * as desired with "mp-safe" or "not-mp-safe".
1776 *
1777 * "show cli clear-hit" clears the per-command hit counters.
1778 * @cliexend
1779?*/
1780
1781/* *INDENT-OFF* */
1782VLIB_CLI_COMMAND (show_cli_command, static) =
1783{
1784 .path = "show cli",
1785 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1786 .function = show_cli_command_fn,
1787 .is_mp_safe = 1,
Dave Barachc4abafd2019-09-04 12:09:32 -04001788};
1789/* *INDENT-ON* */
1790
1791static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -04001792vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001793{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001794 vlib_cli_main_t *cm = &vm->cli_main;
1795 clib_error_t *error = 0;
1796 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001797
1798 cmd = cm->cli_command_registrations;
1799
1800 while (cmd)
1801 {
1802 error = vlib_cli_register (vm, cmd);
1803 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001804 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001805 cmd = cmd->next_cli_command;
1806 }
1807 return error;
1808}
1809
1810VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001811
1812/*
1813 * fd.io coding-style-patch-verification: ON
1814 *
1815 * Local Variables:
1816 * eval: (c-set-style "gnu")
1817 * End:
1818 */