blob: 3215a3264f56784213403d03dc12853f1fecec36 [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* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100307 clib_bitmap_foreach (index, match_bitmap) {
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200308 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);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100317 }
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200318 /* *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
Dave Barach3d0e0d12021-02-17 10:25:18 -0500475 else if (unformat (input, "vpplog %v", &string))
476 {
477 int i;
478 /*
479 * Delete leading whitespace, so "vpplog { this and that }"
480 * and "vpplog this" line up nicely.
481 */
482 for (i = 0; i < vec_len (string); i++)
483 if (string[i] != ' ')
484 break;
485 if (i > 0)
486 vec_delete (string, i, 0);
487
488 vlib_log_notice (cm->log, "CLI: %v", string);
489 vec_free (string);
490 }
491
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 else if (unformat (input, "uncomment %U",
493 unformat_vlib_cli_sub_input, &sub_input))
494 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400495 error =
496 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
497 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 unformat_free (&sub_input);
499 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400500 else if (unformat (input, "leak-check %U",
501 unformat_vlib_cli_sub_input, &sub_input))
502 {
503 u8 *leak_report;
Dave Barachd67a4282019-06-15 12:46:13 -0400504 if (current_traced_heap)
505 {
506 void *oldheap;
507 oldheap = clib_mem_set_heap (current_traced_heap);
508 clib_mem_trace (0);
509 clib_mem_set_heap (oldheap);
510 current_traced_heap = 0;
511 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400512 clib_mem_trace (1);
513 error =
514 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
515 parent_command_index);
516 unformat_free (&sub_input);
517
518 /* Otherwise, the clib_error_t shows up as a leak... */
519 if (error)
520 {
521 vlib_cli_output (vm, "%v", error->what);
522 clib_error_free (error);
523 error = 0;
524 }
525
526 (void) clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200527 leak_report = format (0, "%U", format_clib_mem_heap, 0,
Dave Barach8fdde3c2019-05-17 10:46:40 -0400528 1 /* verbose, i.e. print leaks */ );
529 clib_mem_trace (0);
530 vlib_cli_output (vm, "%v", leak_report);
531 vec_free (leak_report);
532 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533
534 else
535 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400537 unformat_input_t *si;
538 uword has_sub_commands =
539 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
540
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 si = input;
542 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
543 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400544
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 if (has_sub_commands)
546 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
547
Dave Barach9b8ffd92016-07-08 08:13:45 -0400548 if (has_sub_commands && !error)
549 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
551 else if (c->function)
552 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
555 /* Skip white space for benefit of called function. */
556 unformat_skip_white_space (si);
557
558 if (unformat (si, "?"))
559 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400560 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
561 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 }
563 else
564 {
Dave Barachc3a06552018-10-01 09:25:32 -0400565 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
566 {
567 /* *INDENT-OFF* */
568 ELOG_TYPE_DECLARE (e) =
569 {
570 .format = "cli-cmd: %s",
571 .format_args = "T4",
572 };
573 /* *INDENT-ON* */
574 struct
575 {
576 u32 c;
577 } *ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100578 ed = ELOG_DATA (vlib_get_elog_main (), e);
579 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400580 }
581
Dave Barach9b8ffd92016-07-08 08:13:45 -0400582 if (!c->is_mp_safe)
583 vlib_worker_thread_barrier_sync (vm);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000584 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
585 clib_call_callbacks (cm->perf_counter_cbs, cm,
586 c - cm->commands, 0 /* before */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Dave Baracha1f5a952019-11-01 11:24:43 -0400588 c->hit_counter++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 c_error = c->function (vm, si, c);
590
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000591 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
592 clib_call_callbacks (cm->perf_counter_cbs, cm,
593 c - cm->commands, 1 /* after */ );
Dave Barach9b8ffd92016-07-08 08:13:45 -0400594 if (!c->is_mp_safe)
595 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
Dave Barachc3a06552018-10-01 09:25:32 -0400597 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
598 {
599 /* *INDENT-OFF* */
600 ELOG_TYPE_DECLARE (e) =
601 {
602 .format = "cli-cmd: %s %s",
603 .format_args = "T4T4",
604 };
605 /* *INDENT-ON* */
606 struct
607 {
608 u32 c, err;
609 } *ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100610 ed = ELOG_DATA (vlib_get_elog_main (), e);
611 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400612 if (c_error)
613 {
614 vec_add1 (c_error->what, 0);
Damjan Marionf553a2c2021-03-26 13:45:37 +0100615 ed->err = elog_string (vlib_get_elog_main (),
616 (char *) c_error->what);
Dave Barachc3a06552018-10-01 09:25:32 -0400617 _vec_len (c_error->what) -= 1;
618 }
619 else
Damjan Marionf553a2c2021-03-26 13:45:37 +0100620 ed->err = elog_string (vlib_get_elog_main (), "OK");
Dave Barachc3a06552018-10-01 09:25:32 -0400621 }
622
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 if (c_error)
624 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400625 error =
626 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 clib_error_free (c_error);
628 /* Free sub input. */
629 if (si != input)
630 unformat_free (si);
631
632 return error;
633 }
634 }
635
636 /* Free any previous error. */
637 clib_error_free (error);
638 }
639
Dave Barach9b8ffd92016-07-08 08:13:45 -0400640 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 error = clib_error_return (0, "%v: no sub-commands", c->path);
642
643 /* Free sub input. */
644 if (si != input)
645 unformat_free (si);
646 }
647
648 else
649 goto unknown;
650
651 return error;
652
Dave Barach9b8ffd92016-07-08 08:13:45 -0400653unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
656 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400658 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
659 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660}
661
662
Dave Barach9b8ffd92016-07-08 08:13:45 -0400663void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
664 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Dave Barach9b8ffd92016-07-08 08:13:45 -0400666void
667vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
668{
669}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670
671/* Process CLI input. */
Ole Troan72d87582019-05-10 12:01:10 +0200672int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400673vlib_cli_input (vlib_main_t * vm,
674 unformat_input_t * input,
675 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400677 vlib_process_t *cp = vlib_get_current_process (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400678 clib_error_t *error;
679 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680 uword save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200681 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000683 save_function = cp->output_function;
684 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000686 cp->output_function = function;
687 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688
Dave Barach9b8ffd92016-07-08 08:13:45 -0400689 do
690 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500691 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
692 /* parent */ 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400693 }
694 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
696 if (error)
697 {
698 vlib_cli_output (vm, "%v", error->what);
699 vlib_unix_error_report (vm, error);
Ole Troan72d87582019-05-10 12:01:10 +0200700 /* clib_error_return is unfortunately often called with a '0'
701 return code */
702 rv = error->code != 0 ? error->code : -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703 clib_error_free (error);
704 }
705
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000706 cp->output_function = save_function;
707 cp->output_function_arg = save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200708 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709}
710
711/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400712void
713vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400715 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
719 va_start (va, fmt);
720 s = va_format (0, fmt, &va);
721 va_end (va);
722
Nathan Skrzypczak19ce5022020-11-23 17:56:32 +0100723 /* some format functions might return 0
724 * e.g. show int addr */
725 if (NULL == s)
726 return;
727
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400729 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730 vec_add1 (s, '\n');
731
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 fformat (stdout, "%v", s);
734 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000735 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736
737 vec_free (s);
738}
739
Ole Troan73710c72018-06-04 22:27:49 +0200740void *vl_msg_push_heap (void) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400741void *
742vl_msg_push_heap (void)
743{
744 return 0;
745}
746
Ole Troan73710c72018-06-04 22:27:49 +0200747void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400748void
749vl_msg_pop_heap (void *oldheap)
750{
751}
752
753void *vlib_stats_push_heap (void *) __attribute__ ((weak));
754void *
755vlib_stats_push_heap (void *notused)
756{
757 return 0;
758}
Ole Troan73710c72018-06-04 22:27:49 +0200759
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760static clib_error_t *
761show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400762 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200764 clib_mem_main_t *mm = &clib_mem_main;
Dave Barachd67a4282019-06-15 12:46:13 -0400765 int verbose __attribute__ ((unused)) = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500766 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200767 int map = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400768 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 u32 index = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500770 int i;
Dave Barachd67a4282019-06-15 12:46:13 -0400771 uword clib_mem_trace_enable_disable (uword enable);
772 uword was_enabled;
773
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
Dave Barach9b8ffd92016-07-08 08:13:45 -0400775 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400777 if (unformat (input, "verbose"))
778 verbose = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200779 else if (unformat (input, "api-segment"))
780 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400781 else if (unformat (input, "stats-segment"))
782 stats_segment = 1;
783 else if (unformat (input, "main-heap"))
784 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -0500785 else if (unformat (input, "numa-heaps"))
786 numa_heaps = 1;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200787 else if (unformat (input, "map"))
788 map = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400789 else
790 {
791 error = clib_error_return (0, "unknown input `%U'",
792 format_unformat_error, input);
793 return error;
794 }
795 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796
Damjan Marion6bfd0762020-09-11 22:16:53 +0200797 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -0400798 return clib_error_return
Damjan Marion6bfd0762020-09-11 22:16:53 +0200799 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
800 "or map");
Dave Barachd67a4282019-06-15 12:46:13 -0400801
Ole Troan73710c72018-06-04 22:27:49 +0200802 if (api_segment)
803 {
804 void *oldheap = vl_msg_push_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400805 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200806 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Ole Troan73710c72018-06-04 22:27:49 +0200807 vl_msg_pop_heap (oldheap);
808 u8 *s = vec_dup (s_in_svm);
809
810 oldheap = vl_msg_push_heap ();
811 vec_free (s_in_svm);
Dave Barachd67a4282019-06-15 12:46:13 -0400812 clib_mem_trace_enable_disable (was_enabled);
Ole Troan73710c72018-06-04 22:27:49 +0200813 vl_msg_pop_heap (oldheap);
Dave Barachd67a4282019-06-15 12:46:13 -0400814 vlib_cli_output (vm, "API segment");
Ole Troan73710c72018-06-04 22:27:49 +0200815 vlib_cli_output (vm, "%v", s);
Dave Barachd67a4282019-06-15 12:46:13 -0400816 vec_free (s);
817 }
818 if (stats_segment)
819 {
820 void *oldheap = vlib_stats_push_heap (0);
821 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200822 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Dave Barachd67a4282019-06-15 12:46:13 -0400823 if (oldheap)
824 clib_mem_set_heap (oldheap);
825 u8 *s = vec_dup (s_in_svm);
826
827 oldheap = vlib_stats_push_heap (0);
828 vec_free (s_in_svm);
829 if (oldheap)
830 {
831 clib_mem_trace_enable_disable (was_enabled);
832 clib_mem_set_heap (oldheap);
833 }
834 vlib_cli_output (vm, "Stats segment");
835 vlib_cli_output (vm, "%v", s);
Ole Troan73710c72018-06-04 22:27:49 +0200836 vec_free (s);
837 }
838
Dave Baracha690fdb2020-01-21 12:34:55 -0500839
Dave Barach6a5adc32018-07-04 10:56:23 -0400840 {
Dave Barachd67a4282019-06-15 12:46:13 -0400841 if (main_heap)
842 {
843 /*
844 * Note: the foreach_vlib_main causes allocator traffic,
845 * so shut off tracing before we go there...
846 */
847 was_enabled = clib_mem_trace_enable_disable (0);
Dave Barach6a5adc32018-07-04 10:56:23 -0400848
Dave Barachd67a4282019-06-15 12:46:13 -0400849 /* *INDENT-OFF* */
850 foreach_vlib_main (
851 ({
Dave Barachd67a4282019-06-15 12:46:13 -0400852 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
853 vlib_worker_threads[index].name);
Damjan Marion4537c302020-09-28 19:03:37 +0200854 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
Damjan Marion57d1ec02020-09-16 21:15:44 +0200855 mm->per_cpu_mheaps[index],
Dave Barachd67a4282019-06-15 12:46:13 -0400856 verbose);
857 index++;
858 }));
859 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400860
Dave Barachd67a4282019-06-15 12:46:13 -0400861 /* Restore the trace flag */
862 clib_mem_trace_enable_disable (was_enabled);
863 }
Dave Baracha690fdb2020-01-21 12:34:55 -0500864 if (numa_heaps)
865 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200866 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
Dave Baracha690fdb2020-01-21 12:34:55 -0500867 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200868 if (mm->per_numa_mheaps[i] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -0500869 continue;
Damjan Marion57d1ec02020-09-16 21:15:44 +0200870 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
Dave Baracha690fdb2020-01-21 12:34:55 -0500871 {
872 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
873 continue;
874 }
875 was_enabled = clib_mem_trace_enable_disable (0);
Dave Baracha690fdb2020-01-21 12:34:55 -0500876
Dave Baracha690fdb2020-01-21 12:34:55 -0500877 vlib_cli_output (vm, "Numa %d:", i);
Damjan Marion4537c302020-09-28 19:03:37 +0200878 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
Damjan Marion57d1ec02020-09-16 21:15:44 +0200879 mm->per_numa_mheaps[index], verbose);
Dave Baracha690fdb2020-01-21 12:34:55 -0500880 }
881 }
Damjan Marion6bfd0762020-09-11 22:16:53 +0200882 if (map)
883 {
884 clib_mem_page_stats_t stats = { };
885 clib_mem_vm_map_hdr_t *hdr = 0;
886 u8 *s = 0;
887 int numa = -1;
888
Damjan Marion5ef25162020-09-17 13:29:33 +0200889 s = format (s, "\n%-16s%7s%5s%7s%7s",
890 "StartAddr", "size", "FD", "PageSz", "Pages");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200891 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
892 s = format (s, " Numa%u", numa);
893 s = format (s, " NotMap");
894 s = format (s, " Name");
895 vlib_cli_output (vm, "%v", s);
896 vec_reset_length (s);
897
898 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
899 {
900 clib_mem_get_page_stats ((void *) hdr->base_addr,
901 hdr->log2_page_sz, hdr->num_pages,
902 &stats);
Damjan Marion5ef25162020-09-17 13:29:33 +0200903 s = format (s, "%016lx%7U",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200904 hdr->base_addr, format_memory_size,
Damjan Marion5ef25162020-09-17 13:29:33 +0200905 hdr->num_pages << hdr->log2_page_sz);
906
907 if (hdr->fd != -1)
908 s = format (s, "%5d", hdr->fd);
909 else
910 s = format (s, "%5s", " ");
911
912 s = format (s, "%7U%7lu",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200913 format_log2_page_size, hdr->log2_page_sz,
914 hdr->num_pages);
915 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
916 s = format (s, "%6lu", stats.per_numa[numa]);
917 s = format (s, "%7lu", stats.not_mapped);
918 s = format (s, " %s", hdr->name);
919 vlib_cli_output (vm, "%v", s);
920 vec_reset_length (s);
921 }
922 vec_free (s);
923 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400924 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 return 0;
926}
927
Dave Barach9b8ffd92016-07-08 08:13:45 -0400928/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
930 .path = "show memory",
Dave Baracha690fdb2020-01-21 12:34:55 -0500931 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
Damjan Marion6bfd0762020-09-11 22:16:53 +0200932 " [numa-heaps][map]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933 .function = show_memory_usage,
934};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400935/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936
937static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100938show_cpu (vlib_main_t * vm, unformat_input_t * input,
939 vlib_cli_command_t * cmd)
940{
941#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
942 _("Model name", "%U", format_cpu_model_name);
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800943 _("Microarch model (family)", "%U", format_cpu_uarch);
Damjan Marioneccf5092016-11-18 16:59:24 +0100944 _("Flags", "%U", format_cpu_flags);
945 _("Base frequency", "%.2f GHz",
946 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
947#undef _
948 return 0;
949}
950
951/*?
952 * Displays various information about the CPU.
953 *
954 * @cliexpar
955 * @cliexstart{show cpu}
956 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
957 * Microarchitecture: Broadwell (Broadwell-EP/EX)
958 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
959 * Base Frequency: 3.20 GHz
960 * @cliexend
961?*/
962/* *INDENT-OFF* */
963VLIB_CLI_COMMAND (show_cpu_command, static) = {
964 .path = "show cpu",
965 .short_help = "Show cpu information",
966 .function = show_cpu,
967};
Damjan Marioneccf5092016-11-18 16:59:24 +0100968/* *INDENT-ON* */
Ole Troan73710c72018-06-04 22:27:49 +0200969
Damjan Marioneccf5092016-11-18 16:59:24 +0100970static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971enable_disable_memory_trace (vlib_main_t * vm,
972 unformat_input_t * input,
973 vlib_cli_command_t * cmd)
974{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200975 clib_mem_main_t *mm = &clib_mem_main;
Ole Troan73710c72018-06-04 22:27:49 +0200976 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachd67a4282019-06-15 12:46:13 -0400977 int enable = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200978 int api_segment = 0;
Dave Barachd67a4282019-06-15 12:46:13 -0400979 int stats_segment = 0;
980 int main_heap = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500981 u32 numa_id = ~0;
Ole Troan73710c72018-06-04 22:27:49 +0200982 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
Ole Troan73710c72018-06-04 22:27:49 +0200984 if (!unformat_user (input, unformat_line_input, line_input))
985 return 0;
986
987 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988 {
Dave Barach1ddbc012018-06-13 09:26:05 -0400989 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
Ole Troan73710c72018-06-04 22:27:49 +0200990 ;
991 else if (unformat (line_input, "api-segment"))
992 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400993 else if (unformat (line_input, "stats-segment"))
994 stats_segment = 1;
995 else if (unformat (line_input, "main-heap"))
996 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -0500997 else if (unformat (line_input, "numa-heap %d", &numa_id))
998 ;
Ole Troan73710c72018-06-04 22:27:49 +0200999 else
1000 {
1001 unformat_free (line_input);
1002 return clib_error_return (0, "invalid input");
1003 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 }
Ole Troan73710c72018-06-04 22:27:49 +02001005 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006
Dave Baracha690fdb2020-01-21 12:34:55 -05001007 if ((api_segment + stats_segment + main_heap + (enable == 0)
1008 + (numa_id != ~0)) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -04001009 {
1010 return clib_error_return
Dave Baracha690fdb2020-01-21 12:34:55 -05001011 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1012 "numa-heap <nn> or disable");
Dave Barachd67a4282019-06-15 12:46:13 -04001013 }
1014
1015 /* Turn off current trace, if any */
1016 if (current_traced_heap)
1017 {
1018 void *oldheap;
1019 oldheap = clib_mem_set_heap (current_traced_heap);
1020 clib_mem_trace (0);
1021 clib_mem_set_heap (oldheap);
1022 current_traced_heap = 0;
1023 }
1024
1025 if (enable == 0)
1026 return 0;
1027
1028 /* API segment */
Ole Troan73710c72018-06-04 22:27:49 +02001029 if (api_segment)
Dave Barachd67a4282019-06-15 12:46:13 -04001030 {
1031 oldheap = vl_msg_push_heap ();
1032 current_traced_heap = clib_mem_get_heap ();
1033 clib_mem_trace (1);
1034 vl_msg_pop_heap (oldheap);
1035
1036 }
1037
1038 /* Stats segment */
1039 if (stats_segment)
1040 {
1041 oldheap = vlib_stats_push_heap (0);
1042 current_traced_heap = clib_mem_get_heap ();
1043 clib_mem_trace (stats_segment);
1044 /* We don't want to call vlib_stats_pop_heap... */
1045 if (oldheap)
1046 clib_mem_set_heap (oldheap);
1047 }
1048
1049 /* main_heap */
1050 if (main_heap)
1051 {
1052 current_traced_heap = clib_mem_get_heap ();
1053 clib_mem_trace (main_heap);
1054 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055
Dave Baracha690fdb2020-01-21 12:34:55 -05001056 if (numa_id != ~0)
1057 {
Damjan Marion57d1ec02020-09-16 21:15:44 +02001058 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
Dave Baracha690fdb2020-01-21 12:34:55 -05001059 return clib_error_return (0, "Numa %d out of range", numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001060 if (mm->per_numa_mheaps[numa_id] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -05001061 return clib_error_return (0, "Numa %d heap not active", numa_id);
1062
Damjan Marion57d1ec02020-09-16 21:15:44 +02001063 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
Dave Baracha690fdb2020-01-21 12:34:55 -05001064 return clib_error_return (0, "Numa %d uses the main heap...",
1065 numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001066 current_traced_heap = mm->per_numa_mheaps[numa_id];
Dave Baracha690fdb2020-01-21 12:34:55 -05001067 oldheap = clib_mem_set_heap (current_traced_heap);
1068 clib_mem_trace (1);
1069 clib_mem_set_heap (oldheap);
1070 }
1071
1072
Ole Troan73710c72018-06-04 22:27:49 +02001073 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074}
1075
Dave Barach9b8ffd92016-07-08 08:13:45 -04001076/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1078 .path = "memory-trace",
Dave Baracha690fdb2020-01-21 12:34:55 -05001079 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1080 " [numa-heap <numa-id>]\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081 .function = enable_disable_memory_trace,
1082};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001083/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
Damjan Marione5ef1d72017-03-02 12:33:48 +01001085static clib_error_t *
1086restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1087 vlib_cli_command_t * cmd)
1088{
Chris Luke6edd3602018-06-12 22:45:06 -04001089 clib_file_main_t *fm = &file_main;
1090 clib_file_t *f;
Damjan Marione5ef1d72017-03-02 12:33:48 +01001091
Chris Luke6edd3602018-06-12 22:45:06 -04001092 /* environ(7) does not indicate a header for this */
1093 extern char **environ;
1094
1095 /* Close all known open files */
1096 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001097 pool_foreach (f, fm->file_pool)
1098 {
Chris Luke6edd3602018-06-12 22:45:06 -04001099 if (f->file_descriptor > 2)
1100 close(f->file_descriptor);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001101 }
Chris Luke6edd3602018-06-12 22:45:06 -04001102 /* *INDENT-ON* */
1103
1104 /* Exec ourself */
1105 execve (vm->name, (char **) vm->argv, environ);
Damjan Marione5ef1d72017-03-02 12:33:48 +01001106
1107 return 0;
1108}
1109
1110/* *INDENT-OFF* */
1111VLIB_CLI_COMMAND (restart_cmd,static) = {
1112 .path = "restart",
1113 .short_help = "restart process",
1114 .function = restart_cmd_fn,
1115};
1116/* *INDENT-ON* */
1117
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001118#ifdef TEST_CODE
1119/*
1120 * A trivial test harness to verify the per-process output_function
1121 * is working correcty.
1122 */
1123
1124static clib_error_t *
1125sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001126 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001127{
1128 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001129 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001130
Dave Barach9b8ffd92016-07-08 08:13:45 -04001131 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001132
Dave Barach9b8ffd92016-07-08 08:13:45 -04001133 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001134 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001135 vlib_process_wait_for_event_or_clock (vm, 1.0);
1136 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001137 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001138 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001139 return 0;
1140}
1141
Dave Barach9b8ffd92016-07-08 08:13:45 -04001142/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001143VLIB_CLI_COMMAND (ping_command, static) = {
1144 .path = "test sleep",
1145 .function = sleep_ten_seconds,
1146 .short_help = "Sleep for 10 seconds",
1147};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001148/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001149#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150
Dave Barach9b8ffd92016-07-08 08:13:45 -04001151static uword
1152vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001154 char *i = input;
1155 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156 uword l = 0;
1157 uword index_of_last_space = ~0;
1158
1159 while (*i != 0)
1160 {
1161 u8 c = *i++;
1162 /* Multiple white space -> single space. */
1163 switch (c)
1164 {
1165 case ' ':
1166 case '\t':
1167 case '\n':
1168 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -04001169 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170 {
1171 vec_add1 (s, ' ');
1172 l++;
1173 }
1174 break;
1175
1176 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001177 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 index_of_last_space = vec_len (s);
1179 vec_add1 (s, c);
1180 l++;
1181 break;
1182 }
1183 }
1184
1185 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001186 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001187 _vec_len (s) -= 1;
1188
1189 *result = s;
1190 return index_of_last_space;
1191}
1192
1193always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001194parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195{
1196 word i;
1197 for (i = vec_len (path) - 1; i >= 0; i--)
1198 {
1199 if (path[i] == ' ')
1200 return i;
1201 }
1202 return ~0;
1203}
1204
Dave Barach9b8ffd92016-07-08 08:13:45 -04001205static void
1206add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001208 vlib_cli_command_t *p, *c;
1209 vlib_cli_sub_command_t *sub_c;
1210 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 word i, l;
1212
1213 p = vec_elt_at_index (cm->commands, parent_index);
1214 c = vec_elt_at_index (cm->commands, child_index);
1215
1216 l = parent_path_len (c->path);
1217 if (l == ~0)
1218 sub_name = vec_dup ((u8 *) c->path);
1219 else
1220 {
1221 ASSERT (l + 1 < vec_len (c->path));
1222 sub_name = 0;
1223 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1224 }
1225
1226 if (sub_name[0] == '%')
1227 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001228 uword *q;
1229 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230
1231 /* Remove %. */
1232 vec_delete (sub_name, 1, 0);
1233
Dave Barach9b8ffd92016-07-08 08:13:45 -04001234 if (!p->sub_rule_index_by_name)
1235 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1236 sizeof (sub_name[0]),
1237 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1239 if (q)
1240 {
1241 sr = vec_elt_at_index (p->sub_rules, q[0]);
1242 ASSERT (sr->command_index == child_index);
1243 return;
1244 }
1245
Dave Barach9b8ffd92016-07-08 08:13:45 -04001246 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1247 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248 vec_add2 (p->sub_rules, sr, 1);
1249 sr->name = sub_name;
Dave Barach5c944ee2020-01-07 12:29:10 -05001250 sr->rule_index = sr - p->sub_rules;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251 sr->command_index = child_index;
1252 return;
1253 }
1254
Dave Barach9b8ffd92016-07-08 08:13:45 -04001255 if (!p->sub_command_index_by_name)
1256 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1257 sizeof (c->path[0]),
1258 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259
1260 /* Check if sub-command has already been created. */
1261 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1262 {
1263 vec_free (sub_name);
1264 return;
1265 }
1266
1267 vec_add2 (p->sub_commands, sub_c, 1);
1268 sub_c->index = child_index;
1269 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001270 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1271 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001272
1273 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1274 for (i = 0; i < vec_len (sub_c->name); i++)
1275 {
1276 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001277 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278
1279 pos = vec_elt_at_index (p->sub_command_positions, i);
1280
Dave Barach9b8ffd92016-07-08 08:13:45 -04001281 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282 pos->min_char = sub_c->name[i];
1283
1284 n = sub_c->name[i] - pos->min_char;
1285 if (n < 0)
1286 {
1287 pos->min_char = sub_c->name[i];
1288 vec_insert (pos->bitmaps, -n, 0);
1289 n = 0;
1290 }
1291
1292 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001293 pos->bitmaps[n] =
1294 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001295 }
1296}
1297
1298static void
1299vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1300{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001301 uword p_len, pi, *p;
1302 char *p_path;
1303 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304
1305 /* Root command (index 0) should have already been added. */
1306 ASSERT (vec_len (cm->commands) > 0);
1307
1308 c = vec_elt_at_index (cm->commands, ci);
1309 p_len = parent_path_len (c->path);
1310
Dave Barach9b8ffd92016-07-08 08:13:45 -04001311 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312 if (p_len == ~0)
1313 {
1314 add_sub_command (cm, 0, ci);
1315 return;
1316 }
1317
1318 p_path = 0;
1319 vec_add (p_path, c->path, p_len);
1320
1321 p = hash_get_mem (cm->command_index_by_path, p_path);
1322
1323 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001324 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325 {
1326 /* Parent does not exist; create it. */
1327 vec_add2 (cm->commands, parent, 1);
1328 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001329 hash_set_mem (cm->command_index_by_path, parent->path,
1330 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331 pi = parent - cm->commands;
1332 }
1333 else
1334 {
1335 pi = p[0];
1336 vec_free (p_path);
1337 }
1338
1339 add_sub_command (cm, pi, ci);
1340
1341 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001342 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343 vlib_cli_make_parent (cm, pi);
1344}
1345
1346always_inline uword
1347vlib_cli_command_is_empty (vlib_cli_command_t * c)
1348{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001349 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001350}
1351
Dave Barach9b8ffd92016-07-08 08:13:45 -04001352clib_error_t *
1353vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001355 vlib_cli_main_t *cm = &vm->cli_main;
1356 clib_error_t *error = 0;
1357 uword ci, *p;
1358 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001359
1360 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1361 return error;
1362
1363 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1364
Dave Barach9b8ffd92016-07-08 08:13:45 -04001365 if (!cm->command_index_by_path)
1366 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001367 sizeof (c->path[0]),
1368 sizeof (uword));
1369
1370 /* See if command already exists with given path. */
1371 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1372 if (p)
1373 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001374 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375
1376 ci = p[0];
1377 d = vec_elt_at_index (cm->commands, ci);
1378
1379 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001380 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381 if (vlib_cli_command_is_empty (d))
1382 {
1383 vlib_cli_command_t save = d[0];
1384
Dave Barach9b8ffd92016-07-08 08:13:45 -04001385 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001386
1387 /* Copy callers fields. */
1388 d[0] = c[0];
1389
1390 /* Save internal fields. */
1391 d->path = save.path;
1392 d->sub_commands = save.sub_commands;
1393 d->sub_command_index_by_name = save.sub_command_index_by_name;
1394 d->sub_command_positions = save.sub_command_positions;
1395 d->sub_rules = save.sub_rules;
1396 }
1397 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001398 error =
1399 clib_error_return (0, "duplicate command name with path %v",
1400 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401
1402 vec_free (normalized_path);
1403 if (error)
1404 return error;
1405 }
1406 else
1407 {
1408 /* Command does not exist: create it. */
1409
1410 /* Add root command (index 0). */
1411 if (vec_len (cm->commands) == 0)
1412 {
1413 /* Create command with index 0; path is empty string. */
1414 vec_resize (cm->commands, 1);
1415 }
1416
1417 ci = vec_len (cm->commands);
1418 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1419 vec_add1 (cm->commands, c[0]);
1420
1421 c = vec_elt_at_index (cm->commands, ci);
1422 c->path = normalized_path;
1423
1424 /* Don't inherit from registration. */
1425 c->sub_commands = 0;
1426 c->sub_command_index_by_name = 0;
1427 c->sub_command_positions = 0;
1428 }
1429
1430 vlib_cli_make_parent (cm, ci);
1431 return 0;
1432}
1433
Dave Barach6b3f25c2019-12-09 10:45:47 -05001434#if 0
1435/* $$$ turn back on again someday, maybe */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436clib_error_t *
1437vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1438{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001439 vlib_cli_main_t *cm = &vm->cli_main;
1440 vlib_cli_parse_rule_t *r;
1441 clib_error_t *error = 0;
1442 u8 *r_name;
1443 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444
Dave Barach9b8ffd92016-07-08 08:13:45 -04001445 if (!cm->parse_rule_index_by_name)
1446 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447 sizeof (r->name[0]),
1448 sizeof (uword));
1449
1450 /* Make vector copy of name. */
1451 r_name = format (0, "%s", r_reg->name);
1452
1453 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1454 {
1455 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001456 return clib_error_return (0, "duplicate parse rule name `%s'",
1457 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458 }
1459
1460 vec_add2 (cm->parse_rules, r, 1);
1461 r[0] = r_reg[0];
1462 r->name = (char *) r_name;
1463 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1464
1465 return error;
1466}
1467
Dave Barach9b8ffd92016-07-08 08:13:45 -04001468static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1469 vlib_cli_parse_rule_t *
1470 lo,
1471 vlib_cli_parse_rule_t *
1472 hi)
1473 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001474{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001475 clib_error_t *error = 0;
1476 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001477
1478 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1479 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001480 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001481 {
1482 error = clib_error_return (0, "parse rule with no name");
1483 goto done;
1484 }
1485
1486 error = vlib_cli_register_parse_rule (vm, r);
1487 if (error)
1488 goto done;
1489 }
1490
Dave Barach9b8ffd92016-07-08 08:13:45 -04001491done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001492 return error;
1493}
1494#endif
1495
Dave Barach9b8ffd92016-07-08 08:13:45 -04001496static clib_error_t *
Dave Barachaa676742020-11-25 07:23:42 -05001497event_logger_trace_command_fn (vlib_main_t * vm,
1498 unformat_input_t * input,
1499 vlib_cli_command_t * cmd)
Dave Barachc3a06552018-10-01 09:25:32 -04001500{
1501 unformat_input_t _line_input, *line_input = &_line_input;
1502 int enable = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001503 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1504 u32 circuit_node_index;
Dave Barachc3a06552018-10-01 09:25:32 -04001505
1506 if (!unformat_user (input, unformat_line_input, line_input))
1507 goto print_status;
1508
1509 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1510 {
1511 if (unformat (line_input, "api"))
1512 api = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001513 else if (unformat (line_input, "dispatch"))
1514 dispatch = 1;
1515 else if (unformat (line_input, "circuit-node %U",
1516 unformat_vlib_node, vm, &circuit_node_index))
1517 circuit = 1;
Dave Barachc3a06552018-10-01 09:25:32 -04001518 else if (unformat (line_input, "cli"))
1519 cli = 1;
1520 else if (unformat (line_input, "barrier"))
1521 barrier = 1;
1522 else if (unformat (line_input, "disable"))
1523 enable = 0;
1524 else if (unformat (line_input, "enable"))
1525 enable = 1;
1526 else
1527 break;
1528 }
1529 unformat_free (line_input);
1530
Dave Barachb09f4d02019-07-15 16:00:03 -04001531 vl_api_set_elog_trace_api_messages
1532 (api ? enable : vl_api_get_elog_trace_api_messages ());
Dave Barachc3a06552018-10-01 09:25:32 -04001533 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
Dave Barach900cbad2019-01-31 19:12:51 -05001534 vm->elog_trace_graph_dispatch = dispatch ?
1535 enable : vm->elog_trace_graph_dispatch;
1536 vm->elog_trace_graph_circuit = circuit ?
1537 enable : vm->elog_trace_graph_circuit;
Dave Barachc3a06552018-10-01 09:25:32 -04001538 vlib_worker_threads->barrier_elog_enabled =
1539 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
Dave Barach900cbad2019-01-31 19:12:51 -05001540 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1541
1542 /*
1543 * Set up start-of-buffer logic-analyzer trigger
1544 * for main loop event logs, which are fairly heavyweight.
1545 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1546 * will fully disable the scheme when the elog buffer fills.
1547 */
1548 if (dispatch || circuit)
1549 {
Damjan Marionf553a2c2021-03-26 13:45:37 +01001550 elog_main_t *em = &vlib_global_main.elog_main;
Dave Barach900cbad2019-01-31 19:12:51 -05001551
1552 em->n_total_events_disable_limit =
1553 em->n_total_events + vec_len (em->event_ring);
1554 }
1555
Dave Barachc3a06552018-10-01 09:25:32 -04001556
1557print_status:
1558 vlib_cli_output (vm, "Current status:");
1559
1560 vlib_cli_output
1561 (vm, " Event log API message trace: %s\n CLI command trace: %s",
Dave Barachb09f4d02019-07-15 16:00:03 -04001562 vl_api_get_elog_trace_api_messages ()? "on" : "off",
Dave Barachc3a06552018-10-01 09:25:32 -04001563 vm->elog_trace_cli_commands ? "on" : "off");
1564 vlib_cli_output
1565 (vm, " Barrier sync trace: %s",
1566 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
Dave Barach900cbad2019-01-31 19:12:51 -05001567 vlib_cli_output
1568 (vm, " Graph Dispatch: %s",
1569 vm->elog_trace_graph_dispatch ? "on" : "off");
1570 vlib_cli_output
1571 (vm, " Graph Circuit: %s",
1572 vm->elog_trace_graph_circuit ? "on" : "off");
1573 if (vm->elog_trace_graph_circuit)
1574 vlib_cli_output
1575 (vm, " node %U",
1576 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
Dave Barachc3a06552018-10-01 09:25:32 -04001577
1578 return 0;
1579}
1580
1581/*?
1582 * Control event logging of api, cli, and thread barrier events
1583 * With no arguments, displays the current trace status.
1584 * Name the event groups you wish to trace or stop tracing.
1585 *
1586 * @cliexpar
1587 * @clistart
Dave Barachaa676742020-11-25 07:23:42 -05001588 * event-logger trace api cli barrier
1589 * event-logger trace api cli barrier disable
1590 * event-logger trace dispatch
1591 * event-logger trace circuit-node ethernet-input
Dave Barachc3a06552018-10-01 09:25:32 -04001592 * @cliend
Dave Barachaa676742020-11-25 07:23:42 -05001593 * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
Dave Barachc3a06552018-10-01 09:25:32 -04001594?*/
1595/* *INDENT-OFF* */
Dave Barachaa676742020-11-25 07:23:42 -05001596VLIB_CLI_COMMAND (event_logger_trace_command, static) =
Dave Barachc3a06552018-10-01 09:25:32 -04001597{
Dave Barachaa676742020-11-25 07:23:42 -05001598 .path = "event-logger trace",
1599 .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
Dave Barach900cbad2019-01-31 19:12:51 -05001600 "[circuit-node <name> e.g. ethernet-input][disable]",
Dave Barachaa676742020-11-25 07:23:42 -05001601 .function = event_logger_trace_command_fn,
Dave Barachc3a06552018-10-01 09:25:32 -04001602};
1603/* *INDENT-ON* */
1604
1605static clib_error_t *
Dave Barachc4abafd2019-09-04 12:09:32 -04001606suspend_command_fn (vlib_main_t * vm,
1607 unformat_input_t * input, vlib_cli_command_t * cmd)
1608{
1609 vlib_process_suspend (vm, 30e-3);
1610 return 0;
1611}
1612
1613/* *INDENT-OFF* */
1614VLIB_CLI_COMMAND (suspend_command, static) =
1615{
1616 .path = "suspend",
1617 .short_help = "suspend debug CLI for 30ms",
1618 .function = suspend_command_fn,
Dave Baracha1f5a952019-11-01 11:24:43 -04001619 .is_mp_safe = 1,
1620};
1621/* *INDENT-ON* */
1622
1623
1624static int
1625sort_cmds_by_path (void *a1, void *a2)
1626{
1627 u32 *index1 = a1;
1628 u32 *index2 = a2;
1629 vlib_main_t *vm = vlib_get_main ();
1630 vlib_cli_main_t *cm = &vm->cli_main;
1631 vlib_cli_command_t *c1, *c2;
1632 int i, lmin;
1633
1634 c1 = vec_elt_at_index (cm->commands, *index1);
1635 c2 = vec_elt_at_index (cm->commands, *index2);
1636
1637 lmin = vec_len (c1->path);
1638 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1639
1640 for (i = 0; i < lmin; i++)
1641 {
1642 if (c1->path[i] < c2->path[i])
1643 return -1;
1644 else if (c1->path[i] > c2->path[i])
1645 return 1;
1646 }
1647
1648 return 0;
1649}
1650
1651typedef struct
1652{
1653 vlib_cli_main_t *cm;
1654 u32 parent_command_index;
1655 int show_mp_safe;
1656 int show_not_mp_safe;
1657 int show_hit;
1658 int clear_hit;
1659} vlib_cli_walk_args_t;
1660
1661static void
1662cli_recursive_walk (vlib_cli_walk_args_t * aa)
1663{
1664 vlib_cli_command_t *parent;
1665 vlib_cli_sub_command_t *sub;
1666 vlib_cli_walk_args_t _a, *a = &_a;
1667 vlib_cli_main_t *cm;
1668 int i;
1669
1670 /* Copy args into this stack frame */
1671 *a = *aa;
1672 cm = a->cm;
1673
1674 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1675
1676 if (parent->function)
1677 {
1678 if (((a->show_mp_safe && parent->is_mp_safe)
1679 || (a->show_not_mp_safe && !parent->is_mp_safe))
1680 && (a->show_hit == 0 || parent->hit_counter))
1681 {
1682 vec_add1 (cm->sort_vector, a->parent_command_index);
1683 }
1684
1685 if (a->clear_hit)
1686 parent->hit_counter = 0;
1687 }
1688
1689 for (i = 0; i < vec_len (parent->sub_commands); i++)
1690 {
1691 sub = vec_elt_at_index (parent->sub_commands, i);
1692 a->parent_command_index = sub->index;
1693 cli_recursive_walk (a);
1694 }
1695}
1696
1697static u8 *
1698format_mp_safe (u8 * s, va_list * args)
1699{
1700 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1701 int show_mp_safe = va_arg (*args, int);
1702 int show_not_mp_safe = va_arg (*args, int);
1703 int show_hit = va_arg (*args, int);
1704 int clear_hit = va_arg (*args, int);
1705 vlib_cli_command_t *c;
1706 vlib_cli_walk_args_t _a, *a = &_a;
1707 int i;
1708 char *format_string = "\n%v";
1709
1710 if (show_hit)
1711 format_string = "\n%v: %u";
1712
1713 vec_reset_length (cm->sort_vector);
1714
1715 a->cm = cm;
1716 a->parent_command_index = 0;
1717 a->show_mp_safe = show_mp_safe;
1718 a->show_not_mp_safe = show_not_mp_safe;
1719 a->show_hit = show_hit;
1720 a->clear_hit = clear_hit;
1721
1722 cli_recursive_walk (a);
1723
1724 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1725
1726 for (i = 0; i < vec_len (cm->sort_vector); i++)
1727 {
1728 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1729 s = format (s, format_string, c->path, c->hit_counter);
1730 }
1731
1732 return s;
1733}
1734
1735
1736static clib_error_t *
1737show_cli_command_fn (vlib_main_t * vm,
1738 unformat_input_t * input, vlib_cli_command_t * cmd)
1739{
1740 int show_mp_safe = 0;
1741 int show_not_mp_safe = 0;
1742 int show_hit = 0;
1743 int clear_hit = 0;
1744
1745 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1746 {
1747 if (unformat (input, "mp-safe"))
1748 show_mp_safe = 1;
1749 if (unformat (input, "not-mp-safe"))
1750 show_not_mp_safe = 1;
1751 else if (unformat (input, "hit"))
1752 show_hit = 1;
1753 else if (unformat (input, "clear-hit"))
1754 clear_hit = 1;
1755 else
1756 break;
1757 }
1758
1759 /* default set: all cli commands */
1760 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1761 show_mp_safe = show_not_mp_safe = 1;
1762
1763 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1764 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1765 if (clear_hit)
1766 vlib_cli_output (vm, "hit counters cleared...");
1767
1768 return 0;
1769}
1770
1771/*?
1772 * Displays debug cli command information
1773 *
1774 * @cliexpar
1775 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1776 *
1777 * "show cli" displays the entire debug cli:
1778 *
1779 * abf attach
1780 * abf policy
1781 * adjacency counters
1782 * api trace
1783 * app ns
1784 * bfd key del
1785 * ... and so on ...
1786 *
1787 * "show cli mp-safe" displays mp-safe debug CLI commands:
1788 *
1789 * abf policy
1790 * binary-api
1791 * create vhost-user
1792 * exec
1793 * ip container
1794 * ip mroute
1795 * ip probe-neighbor
1796 * ip route
1797 * ip scan-neighbor
1798 * ip table
1799 * ip6 table
1800 *
1801 * "show cli not-mp-safe" displays debug CLI commands
1802 * which cause worker thread barrier synchronization
1803 *
1804 * "show cli hit" displays commands which have been executed. Qualify
1805 * as desired with "mp-safe" or "not-mp-safe".
1806 *
1807 * "show cli clear-hit" clears the per-command hit counters.
1808 * @cliexend
1809?*/
1810
1811/* *INDENT-OFF* */
1812VLIB_CLI_COMMAND (show_cli_command, static) =
1813{
1814 .path = "show cli",
1815 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1816 .function = show_cli_command_fn,
1817 .is_mp_safe = 1,
Dave Barachc4abafd2019-09-04 12:09:32 -04001818};
1819/* *INDENT-ON* */
1820
1821static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -04001822vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001823{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001824 vlib_cli_main_t *cm = &vm->cli_main;
1825 clib_error_t *error = 0;
1826 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001827
1828 cmd = cm->cli_command_registrations;
1829
1830 while (cmd)
1831 {
1832 error = vlib_cli_register (vm, cmd);
1833 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001834 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001835 cmd = cmd->next_cli_command;
1836 }
Dave Barach3d0e0d12021-02-17 10:25:18 -05001837
1838 cm->log = vlib_log_register_class_rate_limit (
1839 "cli", "log", 0x7FFFFFFF /* aka no rate limit */);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001840 return error;
1841}
1842
1843VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001844
1845/*
1846 * fd.io coding-style-patch-verification: ON
1847 *
1848 * Local Variables:
1849 * eval: (c-set-style "gnu")
1850 * End:
1851 */