blob: a88bd3a62f61e5b55baa5beba5e5d9f1c4c26177 [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>
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +000042#include <vppinfra/callback.h>
Damjan Marioneccf5092016-11-18 16:59:24 +010043#include <vppinfra/cpu.h>
Dave Barachc3a06552018-10-01 09:25:32 -040044#include <vppinfra/elog.h>
Damjan Marione5ef1d72017-03-02 12:33:48 +010045#include <unistd.h>
Yoann Desmouceaux3060e072017-05-18 11:00:48 +020046#include <ctype.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
Dave Baracha1f5a952019-11-01 11:24:43 -040048/** \file src/vlib/cli.c Debug CLI Implementation
49 */
50
Dave Barachb09f4d02019-07-15 16:00:03 -040051int vl_api_set_elog_trace_api_messages (int enable);
52int vl_api_get_elog_trace_api_messages (void);
53
Dave Barachd67a4282019-06-15 12:46:13 -040054static void *current_traced_heap;
55
Ed Warnickecb9cada2015-12-08 15:45:58 -070056/* Root of all show commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040057/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070058VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
59 .path = "show",
60 .short_help = "Show commands",
61};
Dave Barach9b8ffd92016-07-08 08:13:45 -040062/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
64/* Root of all clear commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040065/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070066VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
67 .path = "clear",
68 .short_help = "Clear commands",
69};
Dave Barach9b8ffd92016-07-08 08:13:45 -040070/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
72/* Root of all set commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040073/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070074VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
75 .path = "set",
76 .short_help = "Set commands",
77};
Dave Barach9b8ffd92016-07-08 08:13:45 -040078/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
80/* Root of all test commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040081/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070082VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
83 .path = "test",
84 .short_help = "Test commands",
85};
Dave Barach9b8ffd92016-07-08 08:13:45 -040086/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
88/* Returns bitmap of commands which match key. */
89static uword *
90vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
91{
92 int i, n;
Dave Barach9b8ffd92016-07-08 08:13:45 -040093 uword *match = 0;
94 vlib_cli_parse_position_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 unformat_skip_white_space (input);
97
Dave Barach9b8ffd92016-07-08 08:13:45 -040098 for (i = 0;; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 {
100 uword k;
101
102 k = unformat_get_input (input);
103 switch (k)
104 {
105 case 'a' ... 'z':
106 case 'A' ... 'Z':
107 case '0' ... '9':
Dave Barach9b8ffd92016-07-08 08:13:45 -0400108 case '-':
109 case '_':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 break;
111
Dave Barach9b8ffd92016-07-08 08:13:45 -0400112 case ' ':
113 case '\t':
114 case '\r':
115 case '\n':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 case UNFORMAT_END_OF_INPUT:
117 /* White space or end of input removes any non-white
118 matches that were before possible. */
119 if (i < vec_len (c->sub_command_positions)
120 && clib_bitmap_count_set_bits (match) > 1)
121 {
122 p = vec_elt_at_index (c->sub_command_positions, i);
123 for (n = 0; n < vec_len (p->bitmaps); n++)
124 match = clib_bitmap_andnot (match, p->bitmaps[n]);
125 }
126 goto done;
127
128 default:
129 unformat_put_input (input);
130 goto done;
131 }
132
133 if (i >= vec_len (c->sub_command_positions))
134 {
135 no_match:
136 clib_bitmap_free (match);
137 return 0;
138 }
139
140 p = vec_elt_at_index (c->sub_command_positions, i);
141 if (vec_len (p->bitmaps) == 0)
142 goto no_match;
143
144 n = k - p->min_char;
145 if (n < 0 || n >= vec_len (p->bitmaps))
146 goto no_match;
147
148 if (i == 0)
149 match = clib_bitmap_dup (p->bitmaps[n]);
150 else
151 match = clib_bitmap_and (match, p->bitmaps[n]);
152
153 if (clib_bitmap_is_zero (match))
154 goto no_match;
155 }
156
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 return match;
159}
160
161/* Looks for string based sub-input formatted { SUB-INPUT }. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400162uword
163unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
166 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 uword c;
168
169 while (1)
170 {
171 c = unformat_get_input (i);
172 switch (c)
173 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400174 case ' ':
175 case '\t':
176 case '\n':
177 case '\r':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 case '\f':
179 break;
180
181 case '{':
182 default:
183 /* Put back paren. */
184 if (c != UNFORMAT_END_OF_INPUT)
185 unformat_put_input (i);
186
187 if (c == '{' && unformat (i, "%v", &s))
188 {
189 unformat_init_vector (sub_input, s);
190 return 1;
191 }
192 return 0;
193 }
194 }
195 return 0;
196}
197
198static vlib_cli_command_t *
199get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
200{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400201 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 return vec_elt_at_index (cm->commands, s->index);
203}
204
Dave Barach9b8ffd92016-07-08 08:13:45 -0400205static uword
206unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400208 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
209 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
210 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
211 vlib_cli_main_t *cm = &vm->cli_main;
212 uword *match_bitmap, is_unique, index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 match_bitmap = vlib_cli_sub_command_match (c, i);
215 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
216 index = ~0;
217 if (is_unique)
218 {
219 index = clib_bitmap_first_set (match_bitmap);
220 *result = get_sub_command (cm, c, index);
221 }
222 clib_bitmap_free (match_bitmap);
223
224 return is_unique;
225}
226
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200227static int
228vlib_cli_cmp_strings (void *a1, void *a2)
229{
230 u8 *c1 = *(u8 **) a1;
231 u8 *c2 = *(u8 **) a2;
232
233 return vec_cmp (c1, c2);
234}
235
236u8 **
237vlib_cli_get_possible_completions (u8 * str)
238{
239 vlib_cli_command_t *c;
240 vlib_cli_sub_command_t *sc;
241 vlib_main_t *vm = vlib_get_main ();
242 vlib_cli_main_t *vcm = &vm->cli_main;
243 uword *match_bitmap = 0;
244 uword index, is_unique, help_next_level;
245 u8 **result = 0;
246 unformat_input_t input;
247 unformat_init_vector (&input, vec_dup (str));
248 c = vec_elt_at_index (vcm->commands, 0);
249
250 /* remove trailing whitespace, except for one of them */
251 while (vec_len (input.buffer) >= 2 &&
252 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
253 isspace (input.buffer[vec_len (input.buffer) - 2]))
254 {
255 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
256 }
257
258 /* if input is empty, directly return list of root commands */
259 if (vec_len (input.buffer) == 0 ||
260 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
261 {
262 vec_foreach (sc, c->sub_commands)
263 {
264 vec_add1 (result, (u8 *) sc->name);
265 }
266 goto done;
267 }
268
269 /* add a trailing '?' so that vlib_cli_sub_command_match can find
270 * all commands starting with the input string */
271 vec_add1 (input.buffer, '?');
272
273 while (1)
274 {
275 match_bitmap = vlib_cli_sub_command_match (c, &input);
276 /* no match: return no result */
277 if (match_bitmap == 0)
278 {
279 goto done;
280 }
281 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
282 /* unique match: try to step one subcommand level further */
283 if (is_unique)
284 {
285 /* stop if no more input */
286 if (input.index >= vec_len (input.buffer) - 1)
287 {
288 break;
289 }
290
291 index = clib_bitmap_first_set (match_bitmap);
292 c = get_sub_command (vcm, c, index);
293 clib_bitmap_free (match_bitmap);
294 continue;
295 }
296 /* multiple matches: stop here, return all matches */
297 break;
298 }
299
300 /* remove trailing '?' */
301 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
302
303 /* if we have a space at the end of input, and a unique match,
304 * autocomplete the next level of subcommands */
305 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
306 /* *INDENT-OFF* */
307 clib_bitmap_foreach(index, match_bitmap, {
308 if (help_next_level && is_unique) {
309 c = get_sub_command (vcm, c, index);
310 vec_foreach (sc, c->sub_commands) {
311 vec_add1 (result, (u8*) sc->name);
312 }
313 goto done; /* break doesn't work in this macro-loop */
314 }
315 sc = &c->sub_commands[index];
316 vec_add1(result, (u8*) sc->name);
317 });
318 /* *INDENT-ON* */
319
320done:
321 clib_bitmap_free (match_bitmap);
322 unformat_free (&input);
323
Yoann Desmouceaux4227eef2017-05-24 15:51:48 +0200324 if (result)
325 vec_sort_with_function (result, vlib_cli_cmp_strings);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200326 return result;
327}
328
Dave Barach9b8ffd92016-07-08 08:13:45 -0400329static u8 *
330format_vlib_cli_command_help (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400332 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 int is_long = va_arg (*args, int);
334 if (is_long && c->long_help)
335 s = format (s, "%s", c->long_help);
336 else if (c->short_help)
337 s = format (s, "%s", c->short_help);
338 else
339 s = format (s, "%v commands", c->path);
340 return s;
341}
342
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344format_vlib_cli_path (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400346 u8 *path = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347
Dave Barach6b3f25c2019-12-09 10:45:47 -0500348 s = format (s, "%v", path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
350 return s;
351}
352
353static vlib_cli_command_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
357 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358
359 if (c->function)
360 vec_add1 (subs, c[0]);
361
Dave Barach9b8ffd92016-07-08 08:13:45 -0400362 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
364 return subs;
365}
366
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500367static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400368vlib_cli_cmp_rule (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500369{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400370 vlib_cli_sub_rule_t *r1 = a1;
371 vlib_cli_sub_rule_t *r2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500372
373 return vec_cmp (r1->name, r2->name);
374}
375
376static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400377vlib_cli_cmp_command (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500378{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400379 vlib_cli_command_t *c1 = a1;
380 vlib_cli_command_t *c2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500381
382 return vec_cmp (c1->path, c2->path);
383}
384
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385static clib_error_t *
386vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
387 vlib_cli_main_t * cm,
388 unformat_input_t * input,
389 uword parent_command_index)
390{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400391 vlib_cli_command_t *parent, *c;
392 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400394 u8 *string;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 uword is_main_dispatch = cm == &vm->cli_main;
396
397 parent = vec_elt_at_index (cm->commands, parent_command_index);
398 if (is_main_dispatch && unformat (input, "help"))
399 {
400 uword help_at_end_of_line, i;
401
Dave Barach9b8ffd92016-07-08 08:13:45 -0400402 help_at_end_of_line =
403 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 while (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400405 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 c = parent;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400407 if (unformat_user
408 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 ;
410
Dave Barach9b8ffd92016-07-08 08:13:45 -0400411 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 goto unknown;
413
414 else
415 break;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400416 }
417
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 /* help SUB-COMMAND => long format help.
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419 "help" at end of line: show all commands. */
420 if (!help_at_end_of_line)
421 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
422 /* is_long */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
Dave Barach6b3f25c2019-12-09 10:45:47 -0500424 else if (vec_len (c->sub_commands) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 vlib_cli_output (vm, "%v: no sub-commands", c->path);
426
427 else
428 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500429 vlib_cli_sub_rule_t *sr, *subs = 0;
Dave Barach6d5df8d2019-12-11 09:46:56 -0500430 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Dave Barach6d5df8d2019-12-11 09:46:56 -0500432 vec_foreach (sc, c->sub_commands)
433 {
434 vec_add2 (subs, sr, 1);
435 sr->name = sc->name;
436 sr->command_index = sc->index;
437 sr->rule_index = ~0;
438 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500440 vec_sort_with_function (subs, vlib_cli_cmp_rule);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Dave Barach9b8ffd92016-07-08 08:13:45 -0400442 for (i = 0; i < vec_len (subs); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
446 d = vec_elt_at_index (cm->commands, subs[i].command_index);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500447 vlib_cli_output
448 (vm, " %-30v %U", subs[i].name,
449 format_vlib_cli_command_help, d, /* is_long */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 }
451
452 vec_free (subs);
453 }
454 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455
456 else if (is_main_dispatch
457 && (unformat (input, "choices") || unformat (input, "?")))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400459 vlib_cli_command_t *sub, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460
461 subs = all_subs (cm, 0, parent_command_index);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500462 vec_sort_with_function (subs, vlib_cli_cmp_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 vec_foreach (sub, subs)
464 vlib_cli_output (vm, " %-40U %U",
465 format_vlib_cli_path, sub->path,
466 format_vlib_cli_command_help, sub, /* is_long */ 0);
467 vec_free (subs);
468 }
469
470 else if (unformat (input, "comment %v", &string))
471 {
472 vec_free (string);
473 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 else if (unformat (input, "uncomment %U",
476 unformat_vlib_cli_sub_input, &sub_input))
477 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400478 error =
479 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
480 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 unformat_free (&sub_input);
482 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400483 else if (unformat (input, "leak-check %U",
484 unformat_vlib_cli_sub_input, &sub_input))
485 {
486 u8 *leak_report;
Dave Barachd67a4282019-06-15 12:46:13 -0400487 if (current_traced_heap)
488 {
489 void *oldheap;
490 oldheap = clib_mem_set_heap (current_traced_heap);
491 clib_mem_trace (0);
492 clib_mem_set_heap (oldheap);
493 current_traced_heap = 0;
494 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400495 clib_mem_trace (1);
496 error =
497 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
498 parent_command_index);
499 unformat_free (&sub_input);
500
501 /* Otherwise, the clib_error_t shows up as a leak... */
502 if (error)
503 {
504 vlib_cli_output (vm, "%v", error->what);
505 clib_error_free (error);
506 error = 0;
507 }
508
509 (void) clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200510 leak_report = format (0, "%U", format_clib_mem_heap, 0,
Dave Barach8fdde3c2019-05-17 10:46:40 -0400511 1 /* verbose, i.e. print leaks */ );
512 clib_mem_trace (0);
513 vlib_cli_output (vm, "%v", leak_report);
514 vec_free (leak_report);
515 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400516
517 else
518 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400520 unformat_input_t *si;
521 uword has_sub_commands =
522 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
523
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 si = input;
525 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
526 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400527
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 if (has_sub_commands)
529 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
530
Dave Barach9b8ffd92016-07-08 08:13:45 -0400531 if (has_sub_commands && !error)
532 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
534 else if (c->function)
535 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400536 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
538 /* Skip white space for benefit of called function. */
539 unformat_skip_white_space (si);
540
541 if (unformat (si, "?"))
542 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400543 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
544 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 }
546 else
547 {
Dave Barachc3a06552018-10-01 09:25:32 -0400548 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
549 {
550 /* *INDENT-OFF* */
551 ELOG_TYPE_DECLARE (e) =
552 {
553 .format = "cli-cmd: %s",
554 .format_args = "T4",
555 };
556 /* *INDENT-ON* */
557 struct
558 {
559 u32 c;
560 } *ed;
561 ed = ELOG_DATA (&vm->elog_main, e);
Benoît Ganne62d9fda2019-12-16 15:49:47 +0100562 ed->c = elog_string (&vm->elog_main, "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400563 }
564
Dave Barach9b8ffd92016-07-08 08:13:45 -0400565 if (!c->is_mp_safe)
566 vlib_worker_thread_barrier_sync (vm);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000567 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
568 clib_call_callbacks (cm->perf_counter_cbs, cm,
569 c - cm->commands, 0 /* before */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Dave Baracha1f5a952019-11-01 11:24:43 -0400571 c->hit_counter++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 c_error = c->function (vm, si, c);
573
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000574 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
575 clib_call_callbacks (cm->perf_counter_cbs, cm,
576 c - cm->commands, 1 /* after */ );
Dave Barach9b8ffd92016-07-08 08:13:45 -0400577 if (!c->is_mp_safe)
578 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Dave Barachc3a06552018-10-01 09:25:32 -0400580 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
581 {
582 /* *INDENT-OFF* */
583 ELOG_TYPE_DECLARE (e) =
584 {
585 .format = "cli-cmd: %s %s",
586 .format_args = "T4T4",
587 };
588 /* *INDENT-ON* */
589 struct
590 {
591 u32 c, err;
592 } *ed;
593 ed = ELOG_DATA (&vm->elog_main, e);
Benoît Ganne62d9fda2019-12-16 15:49:47 +0100594 ed->c = elog_string (&vm->elog_main, "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400595 if (c_error)
596 {
597 vec_add1 (c_error->what, 0);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500598 ed->err =
599 elog_string (&vm->elog_main, (char *) c_error->what);
Dave Barachc3a06552018-10-01 09:25:32 -0400600 _vec_len (c_error->what) -= 1;
601 }
602 else
Dave Barachb09f4d02019-07-15 16:00:03 -0400603 ed->err = elog_string (&vm->elog_main, "OK");
Dave Barachc3a06552018-10-01 09:25:32 -0400604 }
605
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 if (c_error)
607 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400608 error =
609 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610 clib_error_free (c_error);
611 /* Free sub input. */
612 if (si != input)
613 unformat_free (si);
614
615 return error;
616 }
617 }
618
619 /* Free any previous error. */
620 clib_error_free (error);
621 }
622
Dave Barach9b8ffd92016-07-08 08:13:45 -0400623 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624 error = clib_error_return (0, "%v: no sub-commands", c->path);
625
626 /* Free sub input. */
627 if (si != input)
628 unformat_free (si);
629 }
630
631 else
632 goto unknown;
633
634 return error;
635
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
639 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400641 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
642 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643}
644
645
Dave Barach9b8ffd92016-07-08 08:13:45 -0400646void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
647 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649void
650vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
651{
652}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
654/* Process CLI input. */
Ole Troan72d87582019-05-10 12:01:10 +0200655int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400656vlib_cli_input (vlib_main_t * vm,
657 unformat_input_t * input,
658 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400660 vlib_process_t *cp = vlib_get_current_process (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400661 clib_error_t *error;
662 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663 uword save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200664 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000666 save_function = cp->output_function;
667 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000669 cp->output_function = function;
670 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Dave Barach9b8ffd92016-07-08 08:13:45 -0400672 do
673 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500674 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
675 /* parent */ 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400676 }
677 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
679 if (error)
680 {
681 vlib_cli_output (vm, "%v", error->what);
682 vlib_unix_error_report (vm, error);
Ole Troan72d87582019-05-10 12:01:10 +0200683 /* clib_error_return is unfortunately often called with a '0'
684 return code */
685 rv = error->code != 0 ? error->code : -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 clib_error_free (error);
687 }
688
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000689 cp->output_function = save_function;
690 cp->output_function_arg = save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200691 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692}
693
694/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695void
696vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400698 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400700 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701
702 va_start (va, fmt);
703 s = va_format (0, fmt, &va);
704 va_end (va);
705
706 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400707 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708 vec_add1 (s, '\n');
709
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 fformat (stdout, "%v", s);
712 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000713 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714
715 vec_free (s);
716}
717
Ole Troan73710c72018-06-04 22:27:49 +0200718void *vl_msg_push_heap (void) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400719void *
720vl_msg_push_heap (void)
721{
722 return 0;
723}
724
Ole Troan73710c72018-06-04 22:27:49 +0200725void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400726void
727vl_msg_pop_heap (void *oldheap)
728{
729}
730
731void *vlib_stats_push_heap (void *) __attribute__ ((weak));
732void *
733vlib_stats_push_heap (void *notused)
734{
735 return 0;
736}
Ole Troan73710c72018-06-04 22:27:49 +0200737
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738static clib_error_t *
739show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400740 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200742 clib_mem_main_t *mm = &clib_mem_main;
Dave Barachd67a4282019-06-15 12:46:13 -0400743 int verbose __attribute__ ((unused)) = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500744 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200745 int map = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400746 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 u32 index = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500748 int i;
Dave Barachd67a4282019-06-15 12:46:13 -0400749 uword clib_mem_trace_enable_disable (uword enable);
750 uword was_enabled;
751
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752
Dave Barach9b8ffd92016-07-08 08:13:45 -0400753 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400755 if (unformat (input, "verbose"))
756 verbose = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200757 else if (unformat (input, "api-segment"))
758 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400759 else if (unformat (input, "stats-segment"))
760 stats_segment = 1;
761 else if (unformat (input, "main-heap"))
762 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -0500763 else if (unformat (input, "numa-heaps"))
764 numa_heaps = 1;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200765 else if (unformat (input, "map"))
766 map = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400767 else
768 {
769 error = clib_error_return (0, "unknown input `%U'",
770 format_unformat_error, input);
771 return error;
772 }
773 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
Damjan Marion6bfd0762020-09-11 22:16:53 +0200775 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -0400776 return clib_error_return
Damjan Marion6bfd0762020-09-11 22:16:53 +0200777 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
778 "or map");
Dave Barachd67a4282019-06-15 12:46:13 -0400779
Ole Troan73710c72018-06-04 22:27:49 +0200780 if (api_segment)
781 {
782 void *oldheap = vl_msg_push_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400783 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200784 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Ole Troan73710c72018-06-04 22:27:49 +0200785 vl_msg_pop_heap (oldheap);
786 u8 *s = vec_dup (s_in_svm);
787
788 oldheap = vl_msg_push_heap ();
789 vec_free (s_in_svm);
Dave Barachd67a4282019-06-15 12:46:13 -0400790 clib_mem_trace_enable_disable (was_enabled);
Ole Troan73710c72018-06-04 22:27:49 +0200791 vl_msg_pop_heap (oldheap);
Dave Barachd67a4282019-06-15 12:46:13 -0400792 vlib_cli_output (vm, "API segment");
Ole Troan73710c72018-06-04 22:27:49 +0200793 vlib_cli_output (vm, "%v", s);
Dave Barachd67a4282019-06-15 12:46:13 -0400794 vec_free (s);
795 }
796 if (stats_segment)
797 {
798 void *oldheap = vlib_stats_push_heap (0);
799 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200800 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Dave Barachd67a4282019-06-15 12:46:13 -0400801 if (oldheap)
802 clib_mem_set_heap (oldheap);
803 u8 *s = vec_dup (s_in_svm);
804
805 oldheap = vlib_stats_push_heap (0);
806 vec_free (s_in_svm);
807 if (oldheap)
808 {
809 clib_mem_trace_enable_disable (was_enabled);
810 clib_mem_set_heap (oldheap);
811 }
812 vlib_cli_output (vm, "Stats segment");
813 vlib_cli_output (vm, "%v", s);
Ole Troan73710c72018-06-04 22:27:49 +0200814 vec_free (s);
815 }
816
Dave Baracha690fdb2020-01-21 12:34:55 -0500817
Dave Barach6a5adc32018-07-04 10:56:23 -0400818 {
Dave Barachd67a4282019-06-15 12:46:13 -0400819 if (main_heap)
820 {
821 /*
822 * Note: the foreach_vlib_main causes allocator traffic,
823 * so shut off tracing before we go there...
824 */
825 was_enabled = clib_mem_trace_enable_disable (0);
Dave Barach6a5adc32018-07-04 10:56:23 -0400826
Dave Barachd67a4282019-06-15 12:46:13 -0400827 /* *INDENT-OFF* */
828 foreach_vlib_main (
829 ({
Dave Barachd67a4282019-06-15 12:46:13 -0400830 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
831 vlib_worker_threads[index].name);
Damjan Marion4537c302020-09-28 19:03:37 +0200832 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
Damjan Marion57d1ec02020-09-16 21:15:44 +0200833 mm->per_cpu_mheaps[index],
Dave Barachd67a4282019-06-15 12:46:13 -0400834 verbose);
835 index++;
836 }));
837 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400838
Dave Barachd67a4282019-06-15 12:46:13 -0400839 /* Restore the trace flag */
840 clib_mem_trace_enable_disable (was_enabled);
841 }
Dave Baracha690fdb2020-01-21 12:34:55 -0500842 if (numa_heaps)
843 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200844 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
Dave Baracha690fdb2020-01-21 12:34:55 -0500845 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200846 if (mm->per_numa_mheaps[i] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -0500847 continue;
Damjan Marion57d1ec02020-09-16 21:15:44 +0200848 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
Dave Baracha690fdb2020-01-21 12:34:55 -0500849 {
850 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
851 continue;
852 }
853 was_enabled = clib_mem_trace_enable_disable (0);
Dave Baracha690fdb2020-01-21 12:34:55 -0500854
Dave Baracha690fdb2020-01-21 12:34:55 -0500855 vlib_cli_output (vm, "Numa %d:", i);
Damjan Marion4537c302020-09-28 19:03:37 +0200856 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
Damjan Marion57d1ec02020-09-16 21:15:44 +0200857 mm->per_numa_mheaps[index], verbose);
Dave Baracha690fdb2020-01-21 12:34:55 -0500858 }
859 }
Damjan Marion6bfd0762020-09-11 22:16:53 +0200860 if (map)
861 {
862 clib_mem_page_stats_t stats = { };
863 clib_mem_vm_map_hdr_t *hdr = 0;
864 u8 *s = 0;
865 int numa = -1;
866
Damjan Marion5ef25162020-09-17 13:29:33 +0200867 s = format (s, "\n%-16s%7s%5s%7s%7s",
868 "StartAddr", "size", "FD", "PageSz", "Pages");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200869 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
870 s = format (s, " Numa%u", numa);
871 s = format (s, " NotMap");
872 s = format (s, " Name");
873 vlib_cli_output (vm, "%v", s);
874 vec_reset_length (s);
875
876 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
877 {
878 clib_mem_get_page_stats ((void *) hdr->base_addr,
879 hdr->log2_page_sz, hdr->num_pages,
880 &stats);
Damjan Marion5ef25162020-09-17 13:29:33 +0200881 s = format (s, "%016lx%7U",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200882 hdr->base_addr, format_memory_size,
Damjan Marion5ef25162020-09-17 13:29:33 +0200883 hdr->num_pages << hdr->log2_page_sz);
884
885 if (hdr->fd != -1)
886 s = format (s, "%5d", hdr->fd);
887 else
888 s = format (s, "%5s", " ");
889
890 s = format (s, "%7U%7lu",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200891 format_log2_page_size, hdr->log2_page_sz,
892 hdr->num_pages);
893 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
894 s = format (s, "%6lu", stats.per_numa[numa]);
895 s = format (s, "%7lu", stats.not_mapped);
896 s = format (s, " %s", hdr->name);
897 vlib_cli_output (vm, "%v", s);
898 vec_reset_length (s);
899 }
900 vec_free (s);
901 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400902 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 return 0;
904}
905
Dave Barach9b8ffd92016-07-08 08:13:45 -0400906/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
908 .path = "show memory",
Dave Baracha690fdb2020-01-21 12:34:55 -0500909 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
Damjan Marion6bfd0762020-09-11 22:16:53 +0200910 " [numa-heaps][map]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911 .function = show_memory_usage,
912};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400913/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
915static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100916show_cpu (vlib_main_t * vm, unformat_input_t * input,
917 vlib_cli_command_t * cmd)
918{
919#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
920 _("Model name", "%U", format_cpu_model_name);
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800921 _("Microarch model (family)", "%U", format_cpu_uarch);
Damjan Marioneccf5092016-11-18 16:59:24 +0100922 _("Flags", "%U", format_cpu_flags);
923 _("Base frequency", "%.2f GHz",
924 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
925#undef _
926 return 0;
927}
928
929/*?
930 * Displays various information about the CPU.
931 *
932 * @cliexpar
933 * @cliexstart{show cpu}
934 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
935 * Microarchitecture: Broadwell (Broadwell-EP/EX)
936 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
937 * Base Frequency: 3.20 GHz
938 * @cliexend
939?*/
940/* *INDENT-OFF* */
941VLIB_CLI_COMMAND (show_cpu_command, static) = {
942 .path = "show cpu",
943 .short_help = "Show cpu information",
944 .function = show_cpu,
945};
Damjan Marioneccf5092016-11-18 16:59:24 +0100946/* *INDENT-ON* */
Ole Troan73710c72018-06-04 22:27:49 +0200947
Damjan Marioneccf5092016-11-18 16:59:24 +0100948static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949enable_disable_memory_trace (vlib_main_t * vm,
950 unformat_input_t * input,
951 vlib_cli_command_t * cmd)
952{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200953 clib_mem_main_t *mm = &clib_mem_main;
Ole Troan73710c72018-06-04 22:27:49 +0200954 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachd67a4282019-06-15 12:46:13 -0400955 int enable = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200956 int api_segment = 0;
Dave Barachd67a4282019-06-15 12:46:13 -0400957 int stats_segment = 0;
958 int main_heap = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500959 u32 numa_id = ~0;
Ole Troan73710c72018-06-04 22:27:49 +0200960 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961
Ole Troan73710c72018-06-04 22:27:49 +0200962 if (!unformat_user (input, unformat_line_input, line_input))
963 return 0;
964
965 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966 {
Dave Barach1ddbc012018-06-13 09:26:05 -0400967 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
Ole Troan73710c72018-06-04 22:27:49 +0200968 ;
969 else if (unformat (line_input, "api-segment"))
970 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400971 else if (unformat (line_input, "stats-segment"))
972 stats_segment = 1;
973 else if (unformat (line_input, "main-heap"))
974 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -0500975 else if (unformat (line_input, "numa-heap %d", &numa_id))
976 ;
Ole Troan73710c72018-06-04 22:27:49 +0200977 else
978 {
979 unformat_free (line_input);
980 return clib_error_return (0, "invalid input");
981 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982 }
Ole Troan73710c72018-06-04 22:27:49 +0200983 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984
Dave Baracha690fdb2020-01-21 12:34:55 -0500985 if ((api_segment + stats_segment + main_heap + (enable == 0)
986 + (numa_id != ~0)) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -0400987 {
988 return clib_error_return
Dave Baracha690fdb2020-01-21 12:34:55 -0500989 (0, "Need one of main-heap, stats-segment, api-segment,\n"
990 "numa-heap <nn> or disable");
Dave Barachd67a4282019-06-15 12:46:13 -0400991 }
992
993 /* Turn off current trace, if any */
994 if (current_traced_heap)
995 {
996 void *oldheap;
997 oldheap = clib_mem_set_heap (current_traced_heap);
998 clib_mem_trace (0);
999 clib_mem_set_heap (oldheap);
1000 current_traced_heap = 0;
1001 }
1002
1003 if (enable == 0)
1004 return 0;
1005
1006 /* API segment */
Ole Troan73710c72018-06-04 22:27:49 +02001007 if (api_segment)
Dave Barachd67a4282019-06-15 12:46:13 -04001008 {
1009 oldheap = vl_msg_push_heap ();
1010 current_traced_heap = clib_mem_get_heap ();
1011 clib_mem_trace (1);
1012 vl_msg_pop_heap (oldheap);
1013
1014 }
1015
1016 /* Stats segment */
1017 if (stats_segment)
1018 {
1019 oldheap = vlib_stats_push_heap (0);
1020 current_traced_heap = clib_mem_get_heap ();
1021 clib_mem_trace (stats_segment);
1022 /* We don't want to call vlib_stats_pop_heap... */
1023 if (oldheap)
1024 clib_mem_set_heap (oldheap);
1025 }
1026
1027 /* main_heap */
1028 if (main_heap)
1029 {
1030 current_traced_heap = clib_mem_get_heap ();
1031 clib_mem_trace (main_heap);
1032 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033
Dave Baracha690fdb2020-01-21 12:34:55 -05001034 if (numa_id != ~0)
1035 {
Damjan Marion57d1ec02020-09-16 21:15:44 +02001036 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
Dave Baracha690fdb2020-01-21 12:34:55 -05001037 return clib_error_return (0, "Numa %d out of range", numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001038 if (mm->per_numa_mheaps[numa_id] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -05001039 return clib_error_return (0, "Numa %d heap not active", numa_id);
1040
Damjan Marion57d1ec02020-09-16 21:15:44 +02001041 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
Dave Baracha690fdb2020-01-21 12:34:55 -05001042 return clib_error_return (0, "Numa %d uses the main heap...",
1043 numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001044 current_traced_heap = mm->per_numa_mheaps[numa_id];
Dave Baracha690fdb2020-01-21 12:34:55 -05001045 oldheap = clib_mem_set_heap (current_traced_heap);
1046 clib_mem_trace (1);
1047 clib_mem_set_heap (oldheap);
1048 }
1049
1050
Ole Troan73710c72018-06-04 22:27:49 +02001051 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052}
1053
Dave Barach9b8ffd92016-07-08 08:13:45 -04001054/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1056 .path = "memory-trace",
Dave Baracha690fdb2020-01-21 12:34:55 -05001057 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1058 " [numa-heap <numa-id>]\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059 .function = enable_disable_memory_trace,
1060};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001061/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062
Damjan Marione5ef1d72017-03-02 12:33:48 +01001063static clib_error_t *
1064restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1065 vlib_cli_command_t * cmd)
1066{
Chris Luke6edd3602018-06-12 22:45:06 -04001067 clib_file_main_t *fm = &file_main;
1068 clib_file_t *f;
Damjan Marione5ef1d72017-03-02 12:33:48 +01001069
Chris Luke6edd3602018-06-12 22:45:06 -04001070 /* environ(7) does not indicate a header for this */
1071 extern char **environ;
1072
1073 /* Close all known open files */
1074 /* *INDENT-OFF* */
1075 pool_foreach(f, fm->file_pool,
1076 ({
1077 if (f->file_descriptor > 2)
1078 close(f->file_descriptor);
1079 }));
1080 /* *INDENT-ON* */
1081
1082 /* Exec ourself */
1083 execve (vm->name, (char **) vm->argv, environ);
Damjan Marione5ef1d72017-03-02 12:33:48 +01001084
1085 return 0;
1086}
1087
1088/* *INDENT-OFF* */
1089VLIB_CLI_COMMAND (restart_cmd,static) = {
1090 .path = "restart",
1091 .short_help = "restart process",
1092 .function = restart_cmd_fn,
1093};
1094/* *INDENT-ON* */
1095
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001096#ifdef TEST_CODE
1097/*
1098 * A trivial test harness to verify the per-process output_function
1099 * is working correcty.
1100 */
1101
1102static clib_error_t *
1103sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001104 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001105{
1106 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001107 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001108
Dave Barach9b8ffd92016-07-08 08:13:45 -04001109 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001110
Dave Barach9b8ffd92016-07-08 08:13:45 -04001111 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001112 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001113 vlib_process_wait_for_event_or_clock (vm, 1.0);
1114 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001115 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001116 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001117 return 0;
1118}
1119
Dave Barach9b8ffd92016-07-08 08:13:45 -04001120/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001121VLIB_CLI_COMMAND (ping_command, static) = {
1122 .path = "test sleep",
1123 .function = sleep_ten_seconds,
1124 .short_help = "Sleep for 10 seconds",
1125};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001126/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001127#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128
Dave Barach9b8ffd92016-07-08 08:13:45 -04001129static uword
1130vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001132 char *i = input;
1133 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001134 uword l = 0;
1135 uword index_of_last_space = ~0;
1136
1137 while (*i != 0)
1138 {
1139 u8 c = *i++;
1140 /* Multiple white space -> single space. */
1141 switch (c)
1142 {
1143 case ' ':
1144 case '\t':
1145 case '\n':
1146 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -04001147 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148 {
1149 vec_add1 (s, ' ');
1150 l++;
1151 }
1152 break;
1153
1154 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001155 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156 index_of_last_space = vec_len (s);
1157 vec_add1 (s, c);
1158 l++;
1159 break;
1160 }
1161 }
1162
1163 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001164 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 _vec_len (s) -= 1;
1166
1167 *result = s;
1168 return index_of_last_space;
1169}
1170
1171always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001172parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173{
1174 word i;
1175 for (i = vec_len (path) - 1; i >= 0; i--)
1176 {
1177 if (path[i] == ' ')
1178 return i;
1179 }
1180 return ~0;
1181}
1182
Dave Barach9b8ffd92016-07-08 08:13:45 -04001183static void
1184add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001186 vlib_cli_command_t *p, *c;
1187 vlib_cli_sub_command_t *sub_c;
1188 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189 word i, l;
1190
1191 p = vec_elt_at_index (cm->commands, parent_index);
1192 c = vec_elt_at_index (cm->commands, child_index);
1193
1194 l = parent_path_len (c->path);
1195 if (l == ~0)
1196 sub_name = vec_dup ((u8 *) c->path);
1197 else
1198 {
1199 ASSERT (l + 1 < vec_len (c->path));
1200 sub_name = 0;
1201 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1202 }
1203
1204 if (sub_name[0] == '%')
1205 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001206 uword *q;
1207 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
1209 /* Remove %. */
1210 vec_delete (sub_name, 1, 0);
1211
Dave Barach9b8ffd92016-07-08 08:13:45 -04001212 if (!p->sub_rule_index_by_name)
1213 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1214 sizeof (sub_name[0]),
1215 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1217 if (q)
1218 {
1219 sr = vec_elt_at_index (p->sub_rules, q[0]);
1220 ASSERT (sr->command_index == child_index);
1221 return;
1222 }
1223
Dave Barach9b8ffd92016-07-08 08:13:45 -04001224 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1225 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226 vec_add2 (p->sub_rules, sr, 1);
1227 sr->name = sub_name;
Dave Barach5c944ee2020-01-07 12:29:10 -05001228 sr->rule_index = sr - p->sub_rules;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229 sr->command_index = child_index;
1230 return;
1231 }
1232
Dave Barach9b8ffd92016-07-08 08:13:45 -04001233 if (!p->sub_command_index_by_name)
1234 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1235 sizeof (c->path[0]),
1236 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237
1238 /* Check if sub-command has already been created. */
1239 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1240 {
1241 vec_free (sub_name);
1242 return;
1243 }
1244
1245 vec_add2 (p->sub_commands, sub_c, 1);
1246 sub_c->index = child_index;
1247 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001248 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1249 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250
1251 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1252 for (i = 0; i < vec_len (sub_c->name); i++)
1253 {
1254 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001255 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256
1257 pos = vec_elt_at_index (p->sub_command_positions, i);
1258
Dave Barach9b8ffd92016-07-08 08:13:45 -04001259 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260 pos->min_char = sub_c->name[i];
1261
1262 n = sub_c->name[i] - pos->min_char;
1263 if (n < 0)
1264 {
1265 pos->min_char = sub_c->name[i];
1266 vec_insert (pos->bitmaps, -n, 0);
1267 n = 0;
1268 }
1269
1270 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001271 pos->bitmaps[n] =
1272 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273 }
1274}
1275
1276static void
1277vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1278{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001279 uword p_len, pi, *p;
1280 char *p_path;
1281 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282
1283 /* Root command (index 0) should have already been added. */
1284 ASSERT (vec_len (cm->commands) > 0);
1285
1286 c = vec_elt_at_index (cm->commands, ci);
1287 p_len = parent_path_len (c->path);
1288
Dave Barach9b8ffd92016-07-08 08:13:45 -04001289 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290 if (p_len == ~0)
1291 {
1292 add_sub_command (cm, 0, ci);
1293 return;
1294 }
1295
1296 p_path = 0;
1297 vec_add (p_path, c->path, p_len);
1298
1299 p = hash_get_mem (cm->command_index_by_path, p_path);
1300
1301 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001302 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001303 {
1304 /* Parent does not exist; create it. */
1305 vec_add2 (cm->commands, parent, 1);
1306 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001307 hash_set_mem (cm->command_index_by_path, parent->path,
1308 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 pi = parent - cm->commands;
1310 }
1311 else
1312 {
1313 pi = p[0];
1314 vec_free (p_path);
1315 }
1316
1317 add_sub_command (cm, pi, ci);
1318
1319 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001320 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001321 vlib_cli_make_parent (cm, pi);
1322}
1323
1324always_inline uword
1325vlib_cli_command_is_empty (vlib_cli_command_t * c)
1326{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001327 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328}
1329
Dave Barach9b8ffd92016-07-08 08:13:45 -04001330clib_error_t *
1331vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001333 vlib_cli_main_t *cm = &vm->cli_main;
1334 clib_error_t *error = 0;
1335 uword ci, *p;
1336 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001337
1338 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1339 return error;
1340
1341 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1342
Dave Barach9b8ffd92016-07-08 08:13:45 -04001343 if (!cm->command_index_by_path)
1344 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345 sizeof (c->path[0]),
1346 sizeof (uword));
1347
1348 /* See if command already exists with given path. */
1349 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1350 if (p)
1351 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001352 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001353
1354 ci = p[0];
1355 d = vec_elt_at_index (cm->commands, ci);
1356
1357 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001358 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001359 if (vlib_cli_command_is_empty (d))
1360 {
1361 vlib_cli_command_t save = d[0];
1362
Dave Barach9b8ffd92016-07-08 08:13:45 -04001363 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001364
1365 /* Copy callers fields. */
1366 d[0] = c[0];
1367
1368 /* Save internal fields. */
1369 d->path = save.path;
1370 d->sub_commands = save.sub_commands;
1371 d->sub_command_index_by_name = save.sub_command_index_by_name;
1372 d->sub_command_positions = save.sub_command_positions;
1373 d->sub_rules = save.sub_rules;
1374 }
1375 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001376 error =
1377 clib_error_return (0, "duplicate command name with path %v",
1378 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001379
1380 vec_free (normalized_path);
1381 if (error)
1382 return error;
1383 }
1384 else
1385 {
1386 /* Command does not exist: create it. */
1387
1388 /* Add root command (index 0). */
1389 if (vec_len (cm->commands) == 0)
1390 {
1391 /* Create command with index 0; path is empty string. */
1392 vec_resize (cm->commands, 1);
1393 }
1394
1395 ci = vec_len (cm->commands);
1396 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1397 vec_add1 (cm->commands, c[0]);
1398
1399 c = vec_elt_at_index (cm->commands, ci);
1400 c->path = normalized_path;
1401
1402 /* Don't inherit from registration. */
1403 c->sub_commands = 0;
1404 c->sub_command_index_by_name = 0;
1405 c->sub_command_positions = 0;
1406 }
1407
1408 vlib_cli_make_parent (cm, ci);
1409 return 0;
1410}
1411
Dave Barach6b3f25c2019-12-09 10:45:47 -05001412#if 0
1413/* $$$ turn back on again someday, maybe */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001414clib_error_t *
1415vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1416{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001417 vlib_cli_main_t *cm = &vm->cli_main;
1418 vlib_cli_parse_rule_t *r;
1419 clib_error_t *error = 0;
1420 u8 *r_name;
1421 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422
Dave Barach9b8ffd92016-07-08 08:13:45 -04001423 if (!cm->parse_rule_index_by_name)
1424 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001425 sizeof (r->name[0]),
1426 sizeof (uword));
1427
1428 /* Make vector copy of name. */
1429 r_name = format (0, "%s", r_reg->name);
1430
1431 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1432 {
1433 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001434 return clib_error_return (0, "duplicate parse rule name `%s'",
1435 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436 }
1437
1438 vec_add2 (cm->parse_rules, r, 1);
1439 r[0] = r_reg[0];
1440 r->name = (char *) r_name;
1441 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1442
1443 return error;
1444}
1445
Dave Barach9b8ffd92016-07-08 08:13:45 -04001446static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1447 vlib_cli_parse_rule_t *
1448 lo,
1449 vlib_cli_parse_rule_t *
1450 hi)
1451 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001453 clib_error_t *error = 0;
1454 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455
1456 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1457 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001458 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001459 {
1460 error = clib_error_return (0, "parse rule with no name");
1461 goto done;
1462 }
1463
1464 error = vlib_cli_register_parse_rule (vm, r);
1465 if (error)
1466 goto done;
1467 }
1468
Dave Barach9b8ffd92016-07-08 08:13:45 -04001469done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001470 return error;
1471}
1472#endif
1473
Dave Barach9b8ffd92016-07-08 08:13:45 -04001474static clib_error_t *
Dave Barachc3a06552018-10-01 09:25:32 -04001475elog_trace_command_fn (vlib_main_t * vm,
1476 unformat_input_t * input, vlib_cli_command_t * cmd)
1477{
1478 unformat_input_t _line_input, *line_input = &_line_input;
1479 int enable = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001480 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1481 u32 circuit_node_index;
Dave Barachc3a06552018-10-01 09:25:32 -04001482
1483 if (!unformat_user (input, unformat_line_input, line_input))
1484 goto print_status;
1485
1486 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1487 {
1488 if (unformat (line_input, "api"))
1489 api = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001490 else if (unformat (line_input, "dispatch"))
1491 dispatch = 1;
1492 else if (unformat (line_input, "circuit-node %U",
1493 unformat_vlib_node, vm, &circuit_node_index))
1494 circuit = 1;
Dave Barachc3a06552018-10-01 09:25:32 -04001495 else if (unformat (line_input, "cli"))
1496 cli = 1;
1497 else if (unformat (line_input, "barrier"))
1498 barrier = 1;
1499 else if (unformat (line_input, "disable"))
1500 enable = 0;
1501 else if (unformat (line_input, "enable"))
1502 enable = 1;
1503 else
1504 break;
1505 }
1506 unformat_free (line_input);
1507
Dave Barachb09f4d02019-07-15 16:00:03 -04001508 vl_api_set_elog_trace_api_messages
1509 (api ? enable : vl_api_get_elog_trace_api_messages ());
Dave Barachc3a06552018-10-01 09:25:32 -04001510 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
Dave Barach900cbad2019-01-31 19:12:51 -05001511 vm->elog_trace_graph_dispatch = dispatch ?
1512 enable : vm->elog_trace_graph_dispatch;
1513 vm->elog_trace_graph_circuit = circuit ?
1514 enable : vm->elog_trace_graph_circuit;
Dave Barachc3a06552018-10-01 09:25:32 -04001515 vlib_worker_threads->barrier_elog_enabled =
1516 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
Dave Barach900cbad2019-01-31 19:12:51 -05001517 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1518
1519 /*
1520 * Set up start-of-buffer logic-analyzer trigger
1521 * for main loop event logs, which are fairly heavyweight.
1522 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1523 * will fully disable the scheme when the elog buffer fills.
1524 */
1525 if (dispatch || circuit)
1526 {
1527 elog_main_t *em = &vm->elog_main;
1528
1529 em->n_total_events_disable_limit =
1530 em->n_total_events + vec_len (em->event_ring);
1531 }
1532
Dave Barachc3a06552018-10-01 09:25:32 -04001533
1534print_status:
1535 vlib_cli_output (vm, "Current status:");
1536
1537 vlib_cli_output
1538 (vm, " Event log API message trace: %s\n CLI command trace: %s",
Dave Barachb09f4d02019-07-15 16:00:03 -04001539 vl_api_get_elog_trace_api_messages ()? "on" : "off",
Dave Barachc3a06552018-10-01 09:25:32 -04001540 vm->elog_trace_cli_commands ? "on" : "off");
1541 vlib_cli_output
1542 (vm, " Barrier sync trace: %s",
1543 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
Dave Barach900cbad2019-01-31 19:12:51 -05001544 vlib_cli_output
1545 (vm, " Graph Dispatch: %s",
1546 vm->elog_trace_graph_dispatch ? "on" : "off");
1547 vlib_cli_output
1548 (vm, " Graph Circuit: %s",
1549 vm->elog_trace_graph_circuit ? "on" : "off");
1550 if (vm->elog_trace_graph_circuit)
1551 vlib_cli_output
1552 (vm, " node %U",
1553 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
Dave Barachc3a06552018-10-01 09:25:32 -04001554
1555 return 0;
1556}
1557
1558/*?
1559 * Control event logging of api, cli, and thread barrier events
1560 * With no arguments, displays the current trace status.
1561 * Name the event groups you wish to trace or stop tracing.
1562 *
1563 * @cliexpar
1564 * @clistart
1565 * elog trace api cli barrier
1566 * elog trace api cli barrier disable
Dave Barach900cbad2019-01-31 19:12:51 -05001567 * elog trace dispatch
1568 * elog trace circuit-node ethernet-input
Dave Barachc3a06552018-10-01 09:25:32 -04001569 * elog trace
1570 * @cliend
1571 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1572?*/
1573/* *INDENT-OFF* */
1574VLIB_CLI_COMMAND (elog_trace_command, static) =
1575{
1576 .path = "elog trace",
Dave Barach900cbad2019-01-31 19:12:51 -05001577 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1578 "[circuit-node <name> e.g. ethernet-input][disable]",
Dave Barachc3a06552018-10-01 09:25:32 -04001579 .function = elog_trace_command_fn,
1580};
1581/* *INDENT-ON* */
1582
1583static clib_error_t *
Dave Barachc4abafd2019-09-04 12:09:32 -04001584suspend_command_fn (vlib_main_t * vm,
1585 unformat_input_t * input, vlib_cli_command_t * cmd)
1586{
1587 vlib_process_suspend (vm, 30e-3);
1588 return 0;
1589}
1590
1591/* *INDENT-OFF* */
1592VLIB_CLI_COMMAND (suspend_command, static) =
1593{
1594 .path = "suspend",
1595 .short_help = "suspend debug CLI for 30ms",
1596 .function = suspend_command_fn,
Dave Baracha1f5a952019-11-01 11:24:43 -04001597 .is_mp_safe = 1,
1598};
1599/* *INDENT-ON* */
1600
1601
1602static int
1603sort_cmds_by_path (void *a1, void *a2)
1604{
1605 u32 *index1 = a1;
1606 u32 *index2 = a2;
1607 vlib_main_t *vm = vlib_get_main ();
1608 vlib_cli_main_t *cm = &vm->cli_main;
1609 vlib_cli_command_t *c1, *c2;
1610 int i, lmin;
1611
1612 c1 = vec_elt_at_index (cm->commands, *index1);
1613 c2 = vec_elt_at_index (cm->commands, *index2);
1614
1615 lmin = vec_len (c1->path);
1616 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1617
1618 for (i = 0; i < lmin; i++)
1619 {
1620 if (c1->path[i] < c2->path[i])
1621 return -1;
1622 else if (c1->path[i] > c2->path[i])
1623 return 1;
1624 }
1625
1626 return 0;
1627}
1628
1629typedef struct
1630{
1631 vlib_cli_main_t *cm;
1632 u32 parent_command_index;
1633 int show_mp_safe;
1634 int show_not_mp_safe;
1635 int show_hit;
1636 int clear_hit;
1637} vlib_cli_walk_args_t;
1638
1639static void
1640cli_recursive_walk (vlib_cli_walk_args_t * aa)
1641{
1642 vlib_cli_command_t *parent;
1643 vlib_cli_sub_command_t *sub;
1644 vlib_cli_walk_args_t _a, *a = &_a;
1645 vlib_cli_main_t *cm;
1646 int i;
1647
1648 /* Copy args into this stack frame */
1649 *a = *aa;
1650 cm = a->cm;
1651
1652 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1653
1654 if (parent->function)
1655 {
1656 if (((a->show_mp_safe && parent->is_mp_safe)
1657 || (a->show_not_mp_safe && !parent->is_mp_safe))
1658 && (a->show_hit == 0 || parent->hit_counter))
1659 {
1660 vec_add1 (cm->sort_vector, a->parent_command_index);
1661 }
1662
1663 if (a->clear_hit)
1664 parent->hit_counter = 0;
1665 }
1666
1667 for (i = 0; i < vec_len (parent->sub_commands); i++)
1668 {
1669 sub = vec_elt_at_index (parent->sub_commands, i);
1670 a->parent_command_index = sub->index;
1671 cli_recursive_walk (a);
1672 }
1673}
1674
1675static u8 *
1676format_mp_safe (u8 * s, va_list * args)
1677{
1678 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1679 int show_mp_safe = va_arg (*args, int);
1680 int show_not_mp_safe = va_arg (*args, int);
1681 int show_hit = va_arg (*args, int);
1682 int clear_hit = va_arg (*args, int);
1683 vlib_cli_command_t *c;
1684 vlib_cli_walk_args_t _a, *a = &_a;
1685 int i;
1686 char *format_string = "\n%v";
1687
1688 if (show_hit)
1689 format_string = "\n%v: %u";
1690
1691 vec_reset_length (cm->sort_vector);
1692
1693 a->cm = cm;
1694 a->parent_command_index = 0;
1695 a->show_mp_safe = show_mp_safe;
1696 a->show_not_mp_safe = show_not_mp_safe;
1697 a->show_hit = show_hit;
1698 a->clear_hit = clear_hit;
1699
1700 cli_recursive_walk (a);
1701
1702 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1703
1704 for (i = 0; i < vec_len (cm->sort_vector); i++)
1705 {
1706 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1707 s = format (s, format_string, c->path, c->hit_counter);
1708 }
1709
1710 return s;
1711}
1712
1713
1714static clib_error_t *
1715show_cli_command_fn (vlib_main_t * vm,
1716 unformat_input_t * input, vlib_cli_command_t * cmd)
1717{
1718 int show_mp_safe = 0;
1719 int show_not_mp_safe = 0;
1720 int show_hit = 0;
1721 int clear_hit = 0;
1722
1723 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1724 {
1725 if (unformat (input, "mp-safe"))
1726 show_mp_safe = 1;
1727 if (unformat (input, "not-mp-safe"))
1728 show_not_mp_safe = 1;
1729 else if (unformat (input, "hit"))
1730 show_hit = 1;
1731 else if (unformat (input, "clear-hit"))
1732 clear_hit = 1;
1733 else
1734 break;
1735 }
1736
1737 /* default set: all cli commands */
1738 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1739 show_mp_safe = show_not_mp_safe = 1;
1740
1741 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1742 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1743 if (clear_hit)
1744 vlib_cli_output (vm, "hit counters cleared...");
1745
1746 return 0;
1747}
1748
1749/*?
1750 * Displays debug cli command information
1751 *
1752 * @cliexpar
1753 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1754 *
1755 * "show cli" displays the entire debug cli:
1756 *
1757 * abf attach
1758 * abf policy
1759 * adjacency counters
1760 * api trace
1761 * app ns
1762 * bfd key del
1763 * ... and so on ...
1764 *
1765 * "show cli mp-safe" displays mp-safe debug CLI commands:
1766 *
1767 * abf policy
1768 * binary-api
1769 * create vhost-user
1770 * exec
1771 * ip container
1772 * ip mroute
1773 * ip probe-neighbor
1774 * ip route
1775 * ip scan-neighbor
1776 * ip table
1777 * ip6 table
1778 *
1779 * "show cli not-mp-safe" displays debug CLI commands
1780 * which cause worker thread barrier synchronization
1781 *
1782 * "show cli hit" displays commands which have been executed. Qualify
1783 * as desired with "mp-safe" or "not-mp-safe".
1784 *
1785 * "show cli clear-hit" clears the per-command hit counters.
1786 * @cliexend
1787?*/
1788
1789/* *INDENT-OFF* */
1790VLIB_CLI_COMMAND (show_cli_command, static) =
1791{
1792 .path = "show cli",
1793 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1794 .function = show_cli_command_fn,
1795 .is_mp_safe = 1,
Dave Barachc4abafd2019-09-04 12:09:32 -04001796};
1797/* *INDENT-ON* */
1798
1799static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -04001800vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001801{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001802 vlib_cli_main_t *cm = &vm->cli_main;
1803 clib_error_t *error = 0;
1804 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001805
1806 cmd = cm->cli_command_registrations;
1807
1808 while (cmd)
1809 {
1810 error = vlib_cli_register (vm, cmd);
1811 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001812 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001813 cmd = cmd->next_cli_command;
1814 }
1815 return error;
1816}
1817
1818VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001819
1820/*
1821 * fd.io coding-style-patch-verification: ON
1822 *
1823 * Local Variables:
1824 * eval: (c-set-style "gnu")
1825 * End:
1826 */