blob: 9c53200f8cf2fd76b9ceeac1cca93850da793721 [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>
Damjan Marion8973b072022-03-01 15:51:18 +010041#include <vlib/stats/stats.h>
Chris Luke6edd3602018-06-12 22:45:06 -040042#include <vlib/unix/unix.h>
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +000043#include <vppinfra/callback.h>
Damjan Marioneccf5092016-11-18 16:59:24 +010044#include <vppinfra/cpu.h>
Dave Barachc3a06552018-10-01 09:25:32 -040045#include <vppinfra/elog.h>
Damjan Marione5ef1d72017-03-02 12:33:48 +010046#include <unistd.h>
Yoann Desmouceaux3060e072017-05-18 11:00:48 +020047#include <ctype.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Dave Baracha1f5a952019-11-01 11:24:43 -040049/** \file src/vlib/cli.c Debug CLI Implementation
50 */
51
Dave Barachb09f4d02019-07-15 16:00:03 -040052int vl_api_set_elog_trace_api_messages (int enable);
53int vl_api_get_elog_trace_api_messages (void);
54
Dave Barachd67a4282019-06-15 12:46:13 -040055static void *current_traced_heap;
56
Ed Warnickecb9cada2015-12-08 15:45:58 -070057/* Root of all show commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040058/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070059VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
60 .path = "show",
61 .short_help = "Show commands",
62};
Dave Barach9b8ffd92016-07-08 08:13:45 -040063/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
65/* Root of all clear commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040066/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070067VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
68 .path = "clear",
69 .short_help = "Clear commands",
70};
Dave Barach9b8ffd92016-07-08 08:13:45 -040071/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
73/* Root of all set commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040074/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070075VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
76 .path = "set",
77 .short_help = "Set commands",
78};
Dave Barach9b8ffd92016-07-08 08:13:45 -040079/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81/* Root of all test commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040082/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070083VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
84 .path = "test",
85 .short_help = "Test commands",
86};
Dave Barach9b8ffd92016-07-08 08:13:45 -040087/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
89/* Returns bitmap of commands which match key. */
90static uword *
91vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
92{
93 int i, n;
Dave Barach9b8ffd92016-07-08 08:13:45 -040094 uword *match = 0;
95 vlib_cli_parse_position_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 unformat_skip_white_space (input);
98
Dave Barach9b8ffd92016-07-08 08:13:45 -040099 for (i = 0;; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 {
101 uword k;
102
103 k = unformat_get_input (input);
104 switch (k)
105 {
106 case 'a' ... 'z':
107 case 'A' ... 'Z':
108 case '0' ... '9':
Dave Barach9b8ffd92016-07-08 08:13:45 -0400109 case '-':
110 case '_':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 break;
112
Dave Barach9b8ffd92016-07-08 08:13:45 -0400113 case ' ':
114 case '\t':
115 case '\r':
116 case '\n':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 case UNFORMAT_END_OF_INPUT:
118 /* White space or end of input removes any non-white
119 matches that were before possible. */
120 if (i < vec_len (c->sub_command_positions)
121 && clib_bitmap_count_set_bits (match) > 1)
122 {
123 p = vec_elt_at_index (c->sub_command_positions, i);
124 for (n = 0; n < vec_len (p->bitmaps); n++)
125 match = clib_bitmap_andnot (match, p->bitmaps[n]);
126 }
127 goto done;
128
129 default:
130 unformat_put_input (input);
131 goto done;
132 }
133
134 if (i >= vec_len (c->sub_command_positions))
135 {
136 no_match:
137 clib_bitmap_free (match);
138 return 0;
139 }
140
141 p = vec_elt_at_index (c->sub_command_positions, i);
142 if (vec_len (p->bitmaps) == 0)
143 goto no_match;
144
145 n = k - p->min_char;
146 if (n < 0 || n >= vec_len (p->bitmaps))
147 goto no_match;
148
149 if (i == 0)
150 match = clib_bitmap_dup (p->bitmaps[n]);
151 else
152 match = clib_bitmap_and (match, p->bitmaps[n]);
153
154 if (clib_bitmap_is_zero (match))
155 goto no_match;
156 }
157
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 return match;
160}
161
Damjan Marionc50bcbd2022-05-08 20:48:37 +0200162uword
163unformat_vlib_cli_line (unformat_input_t *i, va_list *va)
164{
165 unformat_input_t *result = va_arg (*va, unformat_input_t *);
166 u8 *line = 0;
167 uword c;
168 int skip;
169
170next_line:
171 skip = 0;
172
173 /* skip leading whitespace if any */
174 unformat_skip_white_space (i);
175
176 if (unformat_is_eof (i))
177 return 0;
178
179 while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT)
180 {
181 if (c == '\\')
182 {
183 c = unformat_get_input (i);
184
185 if (c == '\n')
186 {
187 if (!skip)
188 vec_add1 (line, '\n');
189 skip = 0;
190 continue;
191 }
192
193 if (!skip)
194 vec_add1 (line, '\\');
195
196 if (c == UNFORMAT_END_OF_INPUT)
197 break;
198
199 if (!skip)
200 vec_add1 (line, c);
201 continue;
202 }
203
204 if (c == '#')
205 skip = 1;
206 else if (c == '\n')
207 break;
208
209 if (!skip)
210 vec_add1 (line, c);
211 }
212
213 if (line == 0)
214 goto next_line;
215
216 unformat_init_vector (result, line);
217 return 1;
218}
219
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220/* Looks for string based sub-input formatted { SUB-INPUT }. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221uword
222unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400224 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
225 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 uword c;
227
228 while (1)
229 {
230 c = unformat_get_input (i);
231 switch (c)
232 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400233 case ' ':
234 case '\t':
235 case '\n':
236 case '\r':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 case '\f':
238 break;
239
240 case '{':
241 default:
242 /* Put back paren. */
243 if (c != UNFORMAT_END_OF_INPUT)
244 unformat_put_input (i);
245
246 if (c == '{' && unformat (i, "%v", &s))
247 {
248 unformat_init_vector (sub_input, s);
249 return 1;
250 }
251 return 0;
252 }
253 }
254 return 0;
255}
256
257static vlib_cli_command_t *
258get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
259{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400260 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 return vec_elt_at_index (cm->commands, s->index);
262}
263
Dave Barach9b8ffd92016-07-08 08:13:45 -0400264static uword
265unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100267 vlib_main_t __clib_unused *vm = va_arg (*args, vlib_main_t *);
268 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
270 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100271 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272 uword *match_bitmap, is_unique, index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 match_bitmap = vlib_cli_sub_command_match (c, i);
275 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
276 index = ~0;
277 if (is_unique)
278 {
279 index = clib_bitmap_first_set (match_bitmap);
280 *result = get_sub_command (cm, c, index);
281 }
282 clib_bitmap_free (match_bitmap);
283
284 return is_unique;
285}
286
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200287static int
288vlib_cli_cmp_strings (void *a1, void *a2)
289{
290 u8 *c1 = *(u8 **) a1;
291 u8 *c2 = *(u8 **) a2;
292
293 return vec_cmp (c1, c2);
294}
295
296u8 **
297vlib_cli_get_possible_completions (u8 * str)
298{
299 vlib_cli_command_t *c;
300 vlib_cli_sub_command_t *sc;
Damjan Marionfd8deb42021-03-06 12:26:28 +0100301 vlib_global_main_t *vgm = vlib_get_global_main ();
302 vlib_cli_main_t *vcm = &vgm->cli_main;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200303 uword *match_bitmap = 0;
304 uword index, is_unique, help_next_level;
305 u8 **result = 0;
306 unformat_input_t input;
307 unformat_init_vector (&input, vec_dup (str));
308 c = vec_elt_at_index (vcm->commands, 0);
309
310 /* remove trailing whitespace, except for one of them */
311 while (vec_len (input.buffer) >= 2 &&
312 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
313 isspace (input.buffer[vec_len (input.buffer) - 2]))
314 {
315 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
316 }
317
318 /* if input is empty, directly return list of root commands */
319 if (vec_len (input.buffer) == 0 ||
320 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
321 {
322 vec_foreach (sc, c->sub_commands)
323 {
324 vec_add1 (result, (u8 *) sc->name);
325 }
326 goto done;
327 }
328
329 /* add a trailing '?' so that vlib_cli_sub_command_match can find
330 * all commands starting with the input string */
331 vec_add1 (input.buffer, '?');
332
333 while (1)
334 {
335 match_bitmap = vlib_cli_sub_command_match (c, &input);
336 /* no match: return no result */
337 if (match_bitmap == 0)
338 {
339 goto done;
340 }
341 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
342 /* unique match: try to step one subcommand level further */
343 if (is_unique)
344 {
345 /* stop if no more input */
346 if (input.index >= vec_len (input.buffer) - 1)
347 {
348 break;
349 }
350
351 index = clib_bitmap_first_set (match_bitmap);
352 c = get_sub_command (vcm, c, index);
353 clib_bitmap_free (match_bitmap);
354 continue;
355 }
356 /* multiple matches: stop here, return all matches */
357 break;
358 }
359
360 /* remove trailing '?' */
361 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
362
363 /* if we have a space at the end of input, and a unique match,
364 * autocomplete the next level of subcommands */
365 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
366 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100367 clib_bitmap_foreach (index, match_bitmap) {
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200368 if (help_next_level && is_unique) {
369 c = get_sub_command (vcm, c, index);
370 vec_foreach (sc, c->sub_commands) {
371 vec_add1 (result, (u8*) sc->name);
372 }
373 goto done; /* break doesn't work in this macro-loop */
374 }
375 sc = &c->sub_commands[index];
376 vec_add1(result, (u8*) sc->name);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100377 }
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200378 /* *INDENT-ON* */
379
380done:
381 clib_bitmap_free (match_bitmap);
382 unformat_free (&input);
383
Yoann Desmouceaux4227eef2017-05-24 15:51:48 +0200384 if (result)
385 vec_sort_with_function (result, vlib_cli_cmp_strings);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200386 return result;
387}
388
Dave Barach9b8ffd92016-07-08 08:13:45 -0400389static u8 *
390format_vlib_cli_command_help (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400392 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 int is_long = va_arg (*args, int);
394 if (is_long && c->long_help)
395 s = format (s, "%s", c->long_help);
396 else if (c->short_help)
397 s = format (s, "%s", c->short_help);
398 else
399 s = format (s, "%v commands", c->path);
400 return s;
401}
402
Dave Barach9b8ffd92016-07-08 08:13:45 -0400403static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404format_vlib_cli_path (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406 u8 *path = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
Dave Barach6b3f25c2019-12-09 10:45:47 -0500408 s = format (s, "%v", path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
410 return s;
411}
412
413static vlib_cli_command_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400414all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400416 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
417 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
419 if (c->function)
420 vec_add1 (subs, c[0]);
421
Dave Barach9b8ffd92016-07-08 08:13:45 -0400422 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
424 return subs;
425}
426
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500427static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400428vlib_cli_cmp_rule (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500429{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400430 vlib_cli_sub_rule_t *r1 = a1;
431 vlib_cli_sub_rule_t *r2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500432
433 return vec_cmp (r1->name, r2->name);
434}
435
436static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400437vlib_cli_cmp_command (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500438{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439 vlib_cli_command_t *c1 = a1;
440 vlib_cli_command_t *c2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500441
442 return vec_cmp (c1->path, c2->path);
443}
444
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445static clib_error_t *
446vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
447 vlib_cli_main_t * cm,
448 unformat_input_t * input,
449 uword parent_command_index)
450{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100451 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400452 vlib_cli_command_t *parent, *c;
453 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455 u8 *string;
Damjan Marionfd8deb42021-03-06 12:26:28 +0100456 uword is_main_dispatch = cm == &vgm->cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457
458 parent = vec_elt_at_index (cm->commands, parent_command_index);
459 if (is_main_dispatch && unformat (input, "help"))
460 {
461 uword help_at_end_of_line, i;
462
Dave Barach9b8ffd92016-07-08 08:13:45 -0400463 help_at_end_of_line =
464 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 while (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400466 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 c = parent;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400468 if (unformat_user
469 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470 ;
471
Dave Barach9b8ffd92016-07-08 08:13:45 -0400472 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 goto unknown;
474
475 else
476 break;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 }
478
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 /* help SUB-COMMAND => long format help.
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480 "help" at end of line: show all commands. */
481 if (!help_at_end_of_line)
482 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
483 /* is_long */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
Dave Barach6b3f25c2019-12-09 10:45:47 -0500485 else if (vec_len (c->sub_commands) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486 vlib_cli_output (vm, "%v: no sub-commands", c->path);
487
488 else
489 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500490 vlib_cli_sub_rule_t *sr, *subs = 0;
Dave Barach6d5df8d2019-12-11 09:46:56 -0500491 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492
Dave Barach6d5df8d2019-12-11 09:46:56 -0500493 vec_foreach (sc, c->sub_commands)
494 {
495 vec_add2 (subs, sr, 1);
496 sr->name = sc->name;
497 sr->command_index = sc->index;
498 sr->rule_index = ~0;
499 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500501 vec_sort_with_function (subs, vlib_cli_cmp_rule);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502
Dave Barach9b8ffd92016-07-08 08:13:45 -0400503 for (i = 0; i < vec_len (subs); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400505 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
507 d = vec_elt_at_index (cm->commands, subs[i].command_index);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500508 vlib_cli_output
509 (vm, " %-30v %U", subs[i].name,
510 format_vlib_cli_command_help, d, /* is_long */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511 }
512
513 vec_free (subs);
514 }
515 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400516
517 else if (is_main_dispatch
518 && (unformat (input, "choices") || unformat (input, "?")))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400520 vlib_cli_command_t *sub, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
522 subs = all_subs (cm, 0, parent_command_index);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500523 vec_sort_with_function (subs, vlib_cli_cmp_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 vec_foreach (sub, subs)
525 vlib_cli_output (vm, " %-40U %U",
526 format_vlib_cli_path, sub->path,
527 format_vlib_cli_command_help, sub, /* is_long */ 0);
528 vec_free (subs);
529 }
530
531 else if (unformat (input, "comment %v", &string))
532 {
533 vec_free (string);
534 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535
Dave Barach3d0e0d12021-02-17 10:25:18 -0500536 else if (unformat (input, "vpplog %v", &string))
537 {
538 int i;
539 /*
540 * Delete leading whitespace, so "vpplog { this and that }"
541 * and "vpplog this" line up nicely.
542 */
543 for (i = 0; i < vec_len (string); i++)
544 if (string[i] != ' ')
545 break;
546 if (i > 0)
547 vec_delete (string, i, 0);
548
549 vlib_log_notice (cm->log, "CLI: %v", string);
550 vec_free (string);
551 }
552
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 else if (unformat (input, "uncomment %U",
554 unformat_vlib_cli_sub_input, &sub_input))
555 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400556 error =
557 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
558 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 unformat_free (&sub_input);
560 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400561 else if (unformat (input, "leak-check %U",
562 unformat_vlib_cli_sub_input, &sub_input))
563 {
564 u8 *leak_report;
Dave Barachd67a4282019-06-15 12:46:13 -0400565 if (current_traced_heap)
566 {
567 void *oldheap;
568 oldheap = clib_mem_set_heap (current_traced_heap);
569 clib_mem_trace (0);
570 clib_mem_set_heap (oldheap);
571 current_traced_heap = 0;
572 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400573 clib_mem_trace (1);
574 error =
575 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
576 parent_command_index);
577 unformat_free (&sub_input);
578
579 /* Otherwise, the clib_error_t shows up as a leak... */
580 if (error)
581 {
582 vlib_cli_output (vm, "%v", error->what);
583 clib_error_free (error);
584 error = 0;
585 }
586
587 (void) clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200588 leak_report = format (0, "%U", format_clib_mem_heap, 0,
Dave Barach8fdde3c2019-05-17 10:46:40 -0400589 1 /* verbose, i.e. print leaks */ );
590 clib_mem_trace (0);
591 vlib_cli_output (vm, "%v", leak_report);
592 vec_free (leak_report);
593 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400594
595 else
596 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400598 unformat_input_t *si;
599 uword has_sub_commands =
600 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
601
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602 si = input;
603 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
604 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400605
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 if (has_sub_commands)
607 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
608
Dave Barach9b8ffd92016-07-08 08:13:45 -0400609 if (has_sub_commands && !error)
610 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
612 else if (c->function)
613 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400614 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615
616 /* Skip white space for benefit of called function. */
617 unformat_skip_white_space (si);
618
619 if (unformat (si, "?"))
620 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400621 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
622 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 }
624 else
625 {
Dave Barachc3a06552018-10-01 09:25:32 -0400626 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
627 {
628 /* *INDENT-OFF* */
629 ELOG_TYPE_DECLARE (e) =
630 {
631 .format = "cli-cmd: %s",
632 .format_args = "T4",
633 };
634 /* *INDENT-ON* */
635 struct
636 {
637 u32 c;
638 } *ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100639 ed = ELOG_DATA (vlib_get_elog_main (), e);
640 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400641 }
642
Dave Barach9b8ffd92016-07-08 08:13:45 -0400643 if (!c->is_mp_safe)
644 vlib_worker_thread_barrier_sync (vm);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000645 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
646 clib_call_callbacks (cm->perf_counter_cbs, cm,
647 c - cm->commands, 0 /* before */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648
Dave Baracha1f5a952019-11-01 11:24:43 -0400649 c->hit_counter++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 c_error = c->function (vm, si, c);
651
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000652 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
653 clib_call_callbacks (cm->perf_counter_cbs, cm,
654 c - cm->commands, 1 /* after */ );
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655 if (!c->is_mp_safe)
656 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657
Dave Barachc3a06552018-10-01 09:25:32 -0400658 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
659 {
660 /* *INDENT-OFF* */
661 ELOG_TYPE_DECLARE (e) =
662 {
663 .format = "cli-cmd: %s %s",
664 .format_args = "T4T4",
665 };
666 /* *INDENT-ON* */
667 struct
668 {
669 u32 c, err;
670 } *ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100671 ed = ELOG_DATA (vlib_get_elog_main (), e);
672 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400673 if (c_error)
674 {
675 vec_add1 (c_error->what, 0);
Damjan Marionf553a2c2021-03-26 13:45:37 +0100676 ed->err = elog_string (vlib_get_elog_main (),
677 (char *) c_error->what);
Damjan Marion8bea5892022-04-04 22:40:45 +0200678 vec_dec_len (c_error->what, 1);
Dave Barachc3a06552018-10-01 09:25:32 -0400679 }
680 else
Damjan Marionf553a2c2021-03-26 13:45:37 +0100681 ed->err = elog_string (vlib_get_elog_main (), "OK");
Dave Barachc3a06552018-10-01 09:25:32 -0400682 }
683
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684 if (c_error)
685 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400686 error =
687 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 clib_error_free (c_error);
689 /* Free sub input. */
690 if (si != input)
691 unformat_free (si);
692
693 return error;
694 }
695 }
696
697 /* Free any previous error. */
698 clib_error_free (error);
699 }
700
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702 error = clib_error_return (0, "%v: no sub-commands", c->path);
703
704 /* Free sub input. */
705 if (si != input)
706 unformat_free (si);
707 }
708
709 else
710 goto unknown;
711
712 return error;
713
Dave Barach9b8ffd92016-07-08 08:13:45 -0400714unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400716 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
717 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
720 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721}
722
723
Dave Barach9b8ffd92016-07-08 08:13:45 -0400724void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
725 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727void
728vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
729{
730}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731
732/* Process CLI input. */
Ole Troan72d87582019-05-10 12:01:10 +0200733int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400734vlib_cli_input (vlib_main_t * vm,
735 unformat_input_t * input,
736 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100738 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400739 vlib_process_t *cp = vlib_get_current_process (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400740 clib_error_t *error;
741 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742 uword save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200743 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000745 save_function = cp->output_function;
746 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000748 cp->output_function = function;
749 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751 do
752 {
Damjan Marionfd8deb42021-03-06 12:26:28 +0100753 error = vlib_cli_dispatch_sub_commands (vm, &vgm->cli_main, input,
Dave Barach6b3f25c2019-12-09 10:45:47 -0500754 /* parent */ 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400755 }
756 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757
758 if (error)
759 {
760 vlib_cli_output (vm, "%v", error->what);
761 vlib_unix_error_report (vm, error);
Ole Troan72d87582019-05-10 12:01:10 +0200762 /* clib_error_return is unfortunately often called with a '0'
763 return code */
764 rv = error->code != 0 ? error->code : -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765 clib_error_free (error);
766 }
767
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000768 cp->output_function = save_function;
769 cp->output_function_arg = save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200770 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771}
772
773/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400774void
775vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400777 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400779 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
781 va_start (va, fmt);
782 s = va_format (0, fmt, &va);
783 va_end (va);
784
Nathan Skrzypczak19ce5022020-11-23 17:56:32 +0100785 /* some format functions might return 0
786 * e.g. show int addr */
787 if (NULL == s)
788 return;
789
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400791 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 vec_add1 (s, '\n');
793
Dave Barach9b8ffd92016-07-08 08:13:45 -0400794 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795 fformat (stdout, "%v", s);
796 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000797 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798
799 vec_free (s);
800}
801
Ole Troan73710c72018-06-04 22:27:49 +0200802void *vl_msg_push_heap (void) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400803void *
804vl_msg_push_heap (void)
805{
806 return 0;
807}
808
Ole Troan73710c72018-06-04 22:27:49 +0200809void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400810void
811vl_msg_pop_heap (void *oldheap)
812{
813}
814
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815static clib_error_t *
816show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400817 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200819 clib_mem_main_t *mm = &clib_mem_main;
Dave Barachd67a4282019-06-15 12:46:13 -0400820 int verbose __attribute__ ((unused)) = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500821 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200822 int map = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400823 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824 u32 index = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500825 int i;
Dave Barachd67a4282019-06-15 12:46:13 -0400826 uword clib_mem_trace_enable_disable (uword enable);
827 uword was_enabled;
828
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829
Dave Barach9b8ffd92016-07-08 08:13:45 -0400830 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400832 if (unformat (input, "verbose"))
833 verbose = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200834 else if (unformat (input, "api-segment"))
835 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400836 else if (unformat (input, "stats-segment"))
837 stats_segment = 1;
838 else if (unformat (input, "main-heap"))
839 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -0500840 else if (unformat (input, "numa-heaps"))
841 numa_heaps = 1;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200842 else if (unformat (input, "map"))
843 map = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400844 else
845 {
846 error = clib_error_return (0, "unknown input `%U'",
847 format_unformat_error, input);
848 return error;
849 }
850 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851
Damjan Marion6bfd0762020-09-11 22:16:53 +0200852 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -0400853 return clib_error_return
Damjan Marion6bfd0762020-09-11 22:16:53 +0200854 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
855 "or map");
Dave Barachd67a4282019-06-15 12:46:13 -0400856
Ole Troan73710c72018-06-04 22:27:49 +0200857 if (api_segment)
858 {
859 void *oldheap = vl_msg_push_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400860 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200861 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Ole Troan73710c72018-06-04 22:27:49 +0200862 vl_msg_pop_heap (oldheap);
863 u8 *s = vec_dup (s_in_svm);
864
865 oldheap = vl_msg_push_heap ();
866 vec_free (s_in_svm);
Dave Barachd67a4282019-06-15 12:46:13 -0400867 clib_mem_trace_enable_disable (was_enabled);
Ole Troan73710c72018-06-04 22:27:49 +0200868 vl_msg_pop_heap (oldheap);
Dave Barachd67a4282019-06-15 12:46:13 -0400869 vlib_cli_output (vm, "API segment");
Ole Troan73710c72018-06-04 22:27:49 +0200870 vlib_cli_output (vm, "%v", s);
Dave Barachd67a4282019-06-15 12:46:13 -0400871 vec_free (s);
872 }
873 if (stats_segment)
874 {
Damjan Mariondd298e82022-10-12 16:02:18 +0200875 void *oldheap = vlib_stats_set_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400876 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200877 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Dave Barachd67a4282019-06-15 12:46:13 -0400878 if (oldheap)
879 clib_mem_set_heap (oldheap);
880 u8 *s = vec_dup (s_in_svm);
881
Damjan Mariondd298e82022-10-12 16:02:18 +0200882 oldheap = vlib_stats_set_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400883 vec_free (s_in_svm);
884 if (oldheap)
885 {
886 clib_mem_trace_enable_disable (was_enabled);
887 clib_mem_set_heap (oldheap);
888 }
889 vlib_cli_output (vm, "Stats segment");
890 vlib_cli_output (vm, "%v", s);
Ole Troan73710c72018-06-04 22:27:49 +0200891 vec_free (s);
892 }
893
Dave Baracha690fdb2020-01-21 12:34:55 -0500894
Dave Barach6a5adc32018-07-04 10:56:23 -0400895 {
Dave Barachd67a4282019-06-15 12:46:13 -0400896 if (main_heap)
897 {
898 /*
899 * Note: the foreach_vlib_main causes allocator traffic,
900 * so shut off tracing before we go there...
901 */
902 was_enabled = clib_mem_trace_enable_disable (0);
Dave Barach6a5adc32018-07-04 10:56:23 -0400903
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100904 foreach_vlib_main ()
905 {
906 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n" : "", index,
907 vlib_worker_threads[index].name);
908 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
909 mm->per_cpu_mheaps[index], verbose);
910 index++;
911 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400912
Dave Barachd67a4282019-06-15 12:46:13 -0400913 /* Restore the trace flag */
914 clib_mem_trace_enable_disable (was_enabled);
915 }
Dave Baracha690fdb2020-01-21 12:34:55 -0500916 if (numa_heaps)
917 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200918 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
Dave Baracha690fdb2020-01-21 12:34:55 -0500919 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200920 if (mm->per_numa_mheaps[i] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -0500921 continue;
Damjan Marion57d1ec02020-09-16 21:15:44 +0200922 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
Dave Baracha690fdb2020-01-21 12:34:55 -0500923 {
924 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
925 continue;
926 }
927 was_enabled = clib_mem_trace_enable_disable (0);
Dave Baracha690fdb2020-01-21 12:34:55 -0500928
Dave Baracha690fdb2020-01-21 12:34:55 -0500929 vlib_cli_output (vm, "Numa %d:", i);
Damjan Marion4537c302020-09-28 19:03:37 +0200930 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
Damjan Marion57d1ec02020-09-16 21:15:44 +0200931 mm->per_numa_mheaps[index], verbose);
Dave Baracha690fdb2020-01-21 12:34:55 -0500932 }
933 }
Damjan Marion6bfd0762020-09-11 22:16:53 +0200934 if (map)
935 {
936 clib_mem_page_stats_t stats = { };
937 clib_mem_vm_map_hdr_t *hdr = 0;
938 u8 *s = 0;
939 int numa = -1;
940
Damjan Marion5ef25162020-09-17 13:29:33 +0200941 s = format (s, "\n%-16s%7s%5s%7s%7s",
942 "StartAddr", "size", "FD", "PageSz", "Pages");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200943 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
944 s = format (s, " Numa%u", numa);
945 s = format (s, " NotMap");
946 s = format (s, " Name");
947 vlib_cli_output (vm, "%v", s);
948 vec_reset_length (s);
949
950 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
951 {
952 clib_mem_get_page_stats ((void *) hdr->base_addr,
953 hdr->log2_page_sz, hdr->num_pages,
954 &stats);
Damjan Marion5ef25162020-09-17 13:29:33 +0200955 s = format (s, "%016lx%7U",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200956 hdr->base_addr, format_memory_size,
Damjan Marion5ef25162020-09-17 13:29:33 +0200957 hdr->num_pages << hdr->log2_page_sz);
958
959 if (hdr->fd != -1)
960 s = format (s, "%5d", hdr->fd);
961 else
962 s = format (s, "%5s", " ");
963
964 s = format (s, "%7U%7lu",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200965 format_log2_page_size, hdr->log2_page_sz,
966 hdr->num_pages);
967 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
968 s = format (s, "%6lu", stats.per_numa[numa]);
969 s = format (s, "%7lu", stats.not_mapped);
970 s = format (s, " %s", hdr->name);
971 vlib_cli_output (vm, "%v", s);
972 vec_reset_length (s);
973 }
974 vec_free (s);
975 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400976 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977 return 0;
978}
979
Dave Barach9b8ffd92016-07-08 08:13:45 -0400980/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
982 .path = "show memory",
Dave Baracha690fdb2020-01-21 12:34:55 -0500983 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
Damjan Marion6bfd0762020-09-11 22:16:53 +0200984 " [numa-heaps][map]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 .function = show_memory_usage,
986};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400987/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988
989static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100990show_cpu (vlib_main_t * vm, unformat_input_t * input,
991 vlib_cli_command_t * cmd)
992{
993#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
994 _("Model name", "%U", format_cpu_model_name);
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800995 _("Microarch model (family)", "%U", format_cpu_uarch);
Damjan Marioneccf5092016-11-18 16:59:24 +0100996 _("Flags", "%U", format_cpu_flags);
997 _("Base frequency", "%.2f GHz",
998 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
999#undef _
1000 return 0;
1001}
1002
1003/*?
1004 * Displays various information about the CPU.
1005 *
1006 * @cliexpar
1007 * @cliexstart{show cpu}
1008 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
1009 * Microarchitecture: Broadwell (Broadwell-EP/EX)
1010 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
1011 * Base Frequency: 3.20 GHz
1012 * @cliexend
1013?*/
1014/* *INDENT-OFF* */
1015VLIB_CLI_COMMAND (show_cpu_command, static) = {
1016 .path = "show cpu",
1017 .short_help = "Show cpu information",
1018 .function = show_cpu,
1019};
Damjan Marioneccf5092016-11-18 16:59:24 +01001020/* *INDENT-ON* */
Ole Troan73710c72018-06-04 22:27:49 +02001021
Damjan Marioneccf5092016-11-18 16:59:24 +01001022static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023enable_disable_memory_trace (vlib_main_t * vm,
1024 unformat_input_t * input,
1025 vlib_cli_command_t * cmd)
1026{
Damjan Marion57d1ec02020-09-16 21:15:44 +02001027 clib_mem_main_t *mm = &clib_mem_main;
Ole Troan73710c72018-06-04 22:27:49 +02001028 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachd67a4282019-06-15 12:46:13 -04001029 int enable = 1;
Ole Troan73710c72018-06-04 22:27:49 +02001030 int api_segment = 0;
Dave Barachd67a4282019-06-15 12:46:13 -04001031 int stats_segment = 0;
1032 int main_heap = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -05001033 u32 numa_id = ~0;
Ole Troan73710c72018-06-04 22:27:49 +02001034 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035
Ole Troan73710c72018-06-04 22:27:49 +02001036 if (!unformat_user (input, unformat_line_input, line_input))
1037 return 0;
1038
1039 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 {
Dave Barach1ddbc012018-06-13 09:26:05 -04001041 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
Ole Troan73710c72018-06-04 22:27:49 +02001042 ;
1043 else if (unformat (line_input, "api-segment"))
1044 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -04001045 else if (unformat (line_input, "stats-segment"))
1046 stats_segment = 1;
1047 else if (unformat (line_input, "main-heap"))
1048 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -05001049 else if (unformat (line_input, "numa-heap %d", &numa_id))
1050 ;
Ole Troan73710c72018-06-04 22:27:49 +02001051 else
1052 {
1053 unformat_free (line_input);
1054 return clib_error_return (0, "invalid input");
1055 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056 }
Ole Troan73710c72018-06-04 22:27:49 +02001057 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
Dave Baracha690fdb2020-01-21 12:34:55 -05001059 if ((api_segment + stats_segment + main_heap + (enable == 0)
1060 + (numa_id != ~0)) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -04001061 {
1062 return clib_error_return
Dave Baracha690fdb2020-01-21 12:34:55 -05001063 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1064 "numa-heap <nn> or disable");
Dave Barachd67a4282019-06-15 12:46:13 -04001065 }
1066
1067 /* Turn off current trace, if any */
1068 if (current_traced_heap)
1069 {
1070 void *oldheap;
1071 oldheap = clib_mem_set_heap (current_traced_heap);
1072 clib_mem_trace (0);
1073 clib_mem_set_heap (oldheap);
1074 current_traced_heap = 0;
1075 }
1076
1077 if (enable == 0)
1078 return 0;
1079
1080 /* API segment */
Ole Troan73710c72018-06-04 22:27:49 +02001081 if (api_segment)
Dave Barachd67a4282019-06-15 12:46:13 -04001082 {
1083 oldheap = vl_msg_push_heap ();
1084 current_traced_heap = clib_mem_get_heap ();
1085 clib_mem_trace (1);
1086 vl_msg_pop_heap (oldheap);
1087
1088 }
1089
1090 /* Stats segment */
1091 if (stats_segment)
1092 {
Damjan Marion8973b072022-03-01 15:51:18 +01001093 oldheap = vlib_stats_set_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -04001094 current_traced_heap = clib_mem_get_heap ();
1095 clib_mem_trace (stats_segment);
1096 /* We don't want to call vlib_stats_pop_heap... */
1097 if (oldheap)
1098 clib_mem_set_heap (oldheap);
1099 }
1100
1101 /* main_heap */
1102 if (main_heap)
1103 {
1104 current_traced_heap = clib_mem_get_heap ();
1105 clib_mem_trace (main_heap);
1106 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107
Dave Baracha690fdb2020-01-21 12:34:55 -05001108 if (numa_id != ~0)
1109 {
Damjan Marion57d1ec02020-09-16 21:15:44 +02001110 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
Dave Baracha690fdb2020-01-21 12:34:55 -05001111 return clib_error_return (0, "Numa %d out of range", numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001112 if (mm->per_numa_mheaps[numa_id] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -05001113 return clib_error_return (0, "Numa %d heap not active", numa_id);
1114
Damjan Marion57d1ec02020-09-16 21:15:44 +02001115 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
Dave Baracha690fdb2020-01-21 12:34:55 -05001116 return clib_error_return (0, "Numa %d uses the main heap...",
1117 numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001118 current_traced_heap = mm->per_numa_mheaps[numa_id];
Dave Baracha690fdb2020-01-21 12:34:55 -05001119 oldheap = clib_mem_set_heap (current_traced_heap);
1120 clib_mem_trace (1);
1121 clib_mem_set_heap (oldheap);
1122 }
1123
1124
Ole Troan73710c72018-06-04 22:27:49 +02001125 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126}
1127
Dave Barach9b8ffd92016-07-08 08:13:45 -04001128/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1130 .path = "memory-trace",
Dave Baracha690fdb2020-01-21 12:34:55 -05001131 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1132 " [numa-heap <numa-id>]\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133 .function = enable_disable_memory_trace,
1134};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001135/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136
Damjan Marione5ef1d72017-03-02 12:33:48 +01001137static clib_error_t *
1138restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1139 vlib_cli_command_t * cmd)
1140{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001141 vlib_global_main_t *vgm = vlib_get_global_main ();
Chris Luke6edd3602018-06-12 22:45:06 -04001142 clib_file_main_t *fm = &file_main;
1143 clib_file_t *f;
Damjan Marione5ef1d72017-03-02 12:33:48 +01001144
Chris Luke6edd3602018-06-12 22:45:06 -04001145 /* environ(7) does not indicate a header for this */
1146 extern char **environ;
1147
1148 /* Close all known open files */
1149 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001150 pool_foreach (f, fm->file_pool)
1151 {
Chris Luke6edd3602018-06-12 22:45:06 -04001152 if (f->file_descriptor > 2)
1153 close(f->file_descriptor);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001154 }
Chris Luke6edd3602018-06-12 22:45:06 -04001155 /* *INDENT-ON* */
1156
1157 /* Exec ourself */
Damjan Marion2e90b292022-04-04 18:48:11 +02001158 execve (vgm->name, (char **) vgm->argv, environ);
Damjan Marione5ef1d72017-03-02 12:33:48 +01001159
1160 return 0;
1161}
1162
1163/* *INDENT-OFF* */
1164VLIB_CLI_COMMAND (restart_cmd,static) = {
1165 .path = "restart",
1166 .short_help = "restart process",
1167 .function = restart_cmd_fn,
1168};
1169/* *INDENT-ON* */
1170
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001171#ifdef TEST_CODE
1172/*
1173 * A trivial test harness to verify the per-process output_function
1174 * is working correcty.
1175 */
1176
1177static clib_error_t *
1178sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001179 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001180{
1181 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001182 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001183
Dave Barach9b8ffd92016-07-08 08:13:45 -04001184 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001185
Dave Barach9b8ffd92016-07-08 08:13:45 -04001186 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001187 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001188 vlib_process_wait_for_event_or_clock (vm, 1.0);
1189 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001190 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001191 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001192 return 0;
1193}
1194
Dave Barach9b8ffd92016-07-08 08:13:45 -04001195/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001196VLIB_CLI_COMMAND (ping_command, static) = {
1197 .path = "test sleep",
1198 .function = sleep_ten_seconds,
1199 .short_help = "Sleep for 10 seconds",
1200};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001201/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001202#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001203
Dave Barach9b8ffd92016-07-08 08:13:45 -04001204static uword
1205vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001207 char *i = input;
1208 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 uword l = 0;
1210 uword index_of_last_space = ~0;
1211
1212 while (*i != 0)
1213 {
1214 u8 c = *i++;
1215 /* Multiple white space -> single space. */
1216 switch (c)
1217 {
1218 case ' ':
1219 case '\t':
1220 case '\n':
1221 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -04001222 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 {
1224 vec_add1 (s, ' ');
1225 l++;
1226 }
1227 break;
1228
1229 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001230 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231 index_of_last_space = vec_len (s);
1232 vec_add1 (s, c);
1233 l++;
1234 break;
1235 }
1236 }
1237
1238 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001239 if (l > 0 && s[l - 1] == ' ')
Damjan Marion8bea5892022-04-04 22:40:45 +02001240 vec_dec_len (s, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241
1242 *result = s;
1243 return index_of_last_space;
1244}
1245
1246always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001247parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248{
1249 word i;
1250 for (i = vec_len (path) - 1; i >= 0; i--)
1251 {
1252 if (path[i] == ' ')
1253 return i;
1254 }
1255 return ~0;
1256}
1257
Dave Barach9b8ffd92016-07-08 08:13:45 -04001258static void
1259add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001261 vlib_cli_command_t *p, *c;
1262 vlib_cli_sub_command_t *sub_c;
1263 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264 word i, l;
1265
1266 p = vec_elt_at_index (cm->commands, parent_index);
1267 c = vec_elt_at_index (cm->commands, child_index);
1268
1269 l = parent_path_len (c->path);
1270 if (l == ~0)
1271 sub_name = vec_dup ((u8 *) c->path);
1272 else
1273 {
1274 ASSERT (l + 1 < vec_len (c->path));
1275 sub_name = 0;
1276 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1277 }
1278
Dave Barachbfb9a662021-06-21 10:31:35 -04001279 /* "Can't happen," check mainly to shut up coverity */
1280 ALWAYS_ASSERT (sub_name != 0);
1281
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282 if (sub_name[0] == '%')
1283 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001284 uword *q;
1285 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286
1287 /* Remove %. */
1288 vec_delete (sub_name, 1, 0);
1289
Dave Barach9b8ffd92016-07-08 08:13:45 -04001290 if (!p->sub_rule_index_by_name)
1291 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1292 sizeof (sub_name[0]),
1293 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1295 if (q)
1296 {
1297 sr = vec_elt_at_index (p->sub_rules, q[0]);
1298 ASSERT (sr->command_index == child_index);
1299 return;
1300 }
1301
Dave Barach9b8ffd92016-07-08 08:13:45 -04001302 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1303 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304 vec_add2 (p->sub_rules, sr, 1);
1305 sr->name = sub_name;
Dave Barach5c944ee2020-01-07 12:29:10 -05001306 sr->rule_index = sr - p->sub_rules;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001307 sr->command_index = child_index;
1308 return;
1309 }
1310
Dave Barach9b8ffd92016-07-08 08:13:45 -04001311 if (!p->sub_command_index_by_name)
1312 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1313 sizeof (c->path[0]),
1314 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315
1316 /* Check if sub-command has already been created. */
1317 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1318 {
1319 vec_free (sub_name);
1320 return;
1321 }
1322
1323 vec_add2 (p->sub_commands, sub_c, 1);
1324 sub_c->index = child_index;
1325 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001326 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1327 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328
1329 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1330 for (i = 0; i < vec_len (sub_c->name); i++)
1331 {
1332 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001333 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001334
1335 pos = vec_elt_at_index (p->sub_command_positions, i);
1336
Dave Barach9b8ffd92016-07-08 08:13:45 -04001337 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338 pos->min_char = sub_c->name[i];
1339
1340 n = sub_c->name[i] - pos->min_char;
1341 if (n < 0)
1342 {
1343 pos->min_char = sub_c->name[i];
1344 vec_insert (pos->bitmaps, -n, 0);
1345 n = 0;
1346 }
1347
1348 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001349 pos->bitmaps[n] =
1350 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351 }
1352}
1353
1354static void
1355vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1356{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001357 uword p_len, pi, *p;
1358 char *p_path;
1359 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001360
1361 /* Root command (index 0) should have already been added. */
1362 ASSERT (vec_len (cm->commands) > 0);
1363
1364 c = vec_elt_at_index (cm->commands, ci);
1365 p_len = parent_path_len (c->path);
1366
Dave Barach9b8ffd92016-07-08 08:13:45 -04001367 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368 if (p_len == ~0)
1369 {
1370 add_sub_command (cm, 0, ci);
1371 return;
1372 }
1373
1374 p_path = 0;
1375 vec_add (p_path, c->path, p_len);
1376
1377 p = hash_get_mem (cm->command_index_by_path, p_path);
1378
1379 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001380 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381 {
1382 /* Parent does not exist; create it. */
1383 vec_add2 (cm->commands, parent, 1);
1384 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001385 hash_set_mem (cm->command_index_by_path, parent->path,
1386 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001387 pi = parent - cm->commands;
1388 }
1389 else
1390 {
1391 pi = p[0];
1392 vec_free (p_path);
1393 }
1394
1395 add_sub_command (cm, pi, ci);
1396
1397 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001398 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001399 vlib_cli_make_parent (cm, pi);
1400}
1401
1402always_inline uword
1403vlib_cli_command_is_empty (vlib_cli_command_t * c)
1404{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001405 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001406}
1407
Dave Barach9b8ffd92016-07-08 08:13:45 -04001408clib_error_t *
1409vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001411 vlib_global_main_t *vgm = vlib_get_global_main ();
1412 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001413 clib_error_t *error = 0;
1414 uword ci, *p;
1415 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001416
1417 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1418 return error;
1419
1420 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1421
Dave Barach9b8ffd92016-07-08 08:13:45 -04001422 if (!cm->command_index_by_path)
1423 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424 sizeof (c->path[0]),
1425 sizeof (uword));
1426
1427 /* See if command already exists with given path. */
1428 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1429 if (p)
1430 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001431 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
1433 ci = p[0];
1434 d = vec_elt_at_index (cm->commands, ci);
1435
1436 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001437 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438 if (vlib_cli_command_is_empty (d))
1439 {
1440 vlib_cli_command_t save = d[0];
1441
Dave Barach9b8ffd92016-07-08 08:13:45 -04001442 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001443
1444 /* Copy callers fields. */
1445 d[0] = c[0];
1446
1447 /* Save internal fields. */
1448 d->path = save.path;
1449 d->sub_commands = save.sub_commands;
1450 d->sub_command_index_by_name = save.sub_command_index_by_name;
1451 d->sub_command_positions = save.sub_command_positions;
1452 d->sub_rules = save.sub_rules;
1453 }
1454 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001455 error =
1456 clib_error_return (0, "duplicate command name with path %v",
1457 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458
1459 vec_free (normalized_path);
1460 if (error)
1461 return error;
1462 }
1463 else
1464 {
1465 /* Command does not exist: create it. */
1466
1467 /* Add root command (index 0). */
1468 if (vec_len (cm->commands) == 0)
1469 {
1470 /* Create command with index 0; path is empty string. */
1471 vec_resize (cm->commands, 1);
1472 }
1473
1474 ci = vec_len (cm->commands);
1475 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1476 vec_add1 (cm->commands, c[0]);
1477
1478 c = vec_elt_at_index (cm->commands, ci);
1479 c->path = normalized_path;
1480
1481 /* Don't inherit from registration. */
1482 c->sub_commands = 0;
1483 c->sub_command_index_by_name = 0;
1484 c->sub_command_positions = 0;
1485 }
1486
1487 vlib_cli_make_parent (cm, ci);
1488 return 0;
1489}
1490
Dave Barach6b3f25c2019-12-09 10:45:47 -05001491#if 0
1492/* $$$ turn back on again someday, maybe */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001493clib_error_t *
1494vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1495{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001496 vlib_cli_main_t *cm = &vm->cli_main;
1497 vlib_cli_parse_rule_t *r;
1498 clib_error_t *error = 0;
1499 u8 *r_name;
1500 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001501
Dave Barach9b8ffd92016-07-08 08:13:45 -04001502 if (!cm->parse_rule_index_by_name)
1503 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001504 sizeof (r->name[0]),
1505 sizeof (uword));
1506
1507 /* Make vector copy of name. */
1508 r_name = format (0, "%s", r_reg->name);
1509
1510 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1511 {
1512 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001513 return clib_error_return (0, "duplicate parse rule name `%s'",
1514 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515 }
1516
1517 vec_add2 (cm->parse_rules, r, 1);
1518 r[0] = r_reg[0];
1519 r->name = (char *) r_name;
1520 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1521
1522 return error;
1523}
1524
Dave Barach9b8ffd92016-07-08 08:13:45 -04001525static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1526 vlib_cli_parse_rule_t *
1527 lo,
1528 vlib_cli_parse_rule_t *
1529 hi)
1530 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001531{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001532 clib_error_t *error = 0;
1533 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001534
1535 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1536 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001537 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001538 {
1539 error = clib_error_return (0, "parse rule with no name");
1540 goto done;
1541 }
1542
1543 error = vlib_cli_register_parse_rule (vm, r);
1544 if (error)
1545 goto done;
1546 }
1547
Dave Barach9b8ffd92016-07-08 08:13:45 -04001548done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001549 return error;
1550}
1551#endif
1552
Dave Barach9b8ffd92016-07-08 08:13:45 -04001553static clib_error_t *
Dave Barachaa676742020-11-25 07:23:42 -05001554event_logger_trace_command_fn (vlib_main_t * vm,
1555 unformat_input_t * input,
1556 vlib_cli_command_t * cmd)
Dave Barachc3a06552018-10-01 09:25:32 -04001557{
1558 unformat_input_t _line_input, *line_input = &_line_input;
1559 int enable = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001560 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1561 u32 circuit_node_index;
Dave Barachc3a06552018-10-01 09:25:32 -04001562
1563 if (!unformat_user (input, unformat_line_input, line_input))
1564 goto print_status;
1565
1566 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1567 {
1568 if (unformat (line_input, "api"))
1569 api = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001570 else if (unformat (line_input, "dispatch"))
1571 dispatch = 1;
1572 else if (unformat (line_input, "circuit-node %U",
1573 unformat_vlib_node, vm, &circuit_node_index))
1574 circuit = 1;
Dave Barachc3a06552018-10-01 09:25:32 -04001575 else if (unformat (line_input, "cli"))
1576 cli = 1;
1577 else if (unformat (line_input, "barrier"))
1578 barrier = 1;
1579 else if (unformat (line_input, "disable"))
1580 enable = 0;
1581 else if (unformat (line_input, "enable"))
1582 enable = 1;
1583 else
1584 break;
1585 }
1586 unformat_free (line_input);
1587
Dave Barachb09f4d02019-07-15 16:00:03 -04001588 vl_api_set_elog_trace_api_messages
1589 (api ? enable : vl_api_get_elog_trace_api_messages ());
Dave Barachc3a06552018-10-01 09:25:32 -04001590 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
Dave Barach900cbad2019-01-31 19:12:51 -05001591 vm->elog_trace_graph_dispatch = dispatch ?
1592 enable : vm->elog_trace_graph_dispatch;
1593 vm->elog_trace_graph_circuit = circuit ?
1594 enable : vm->elog_trace_graph_circuit;
Dave Barachc3a06552018-10-01 09:25:32 -04001595 vlib_worker_threads->barrier_elog_enabled =
1596 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
Dave Barach900cbad2019-01-31 19:12:51 -05001597 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1598
1599 /*
1600 * Set up start-of-buffer logic-analyzer trigger
1601 * for main loop event logs, which are fairly heavyweight.
1602 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1603 * will fully disable the scheme when the elog buffer fills.
1604 */
1605 if (dispatch || circuit)
1606 {
Damjan Marionf553a2c2021-03-26 13:45:37 +01001607 elog_main_t *em = &vlib_global_main.elog_main;
Dave Barach900cbad2019-01-31 19:12:51 -05001608
1609 em->n_total_events_disable_limit =
1610 em->n_total_events + vec_len (em->event_ring);
1611 }
1612
Dave Barachc3a06552018-10-01 09:25:32 -04001613
1614print_status:
1615 vlib_cli_output (vm, "Current status:");
1616
1617 vlib_cli_output
1618 (vm, " Event log API message trace: %s\n CLI command trace: %s",
Dave Barachb09f4d02019-07-15 16:00:03 -04001619 vl_api_get_elog_trace_api_messages ()? "on" : "off",
Dave Barachc3a06552018-10-01 09:25:32 -04001620 vm->elog_trace_cli_commands ? "on" : "off");
1621 vlib_cli_output
1622 (vm, " Barrier sync trace: %s",
1623 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
Dave Barach900cbad2019-01-31 19:12:51 -05001624 vlib_cli_output
1625 (vm, " Graph Dispatch: %s",
1626 vm->elog_trace_graph_dispatch ? "on" : "off");
1627 vlib_cli_output
1628 (vm, " Graph Circuit: %s",
1629 vm->elog_trace_graph_circuit ? "on" : "off");
1630 if (vm->elog_trace_graph_circuit)
1631 vlib_cli_output
1632 (vm, " node %U",
1633 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
Dave Barachc3a06552018-10-01 09:25:32 -04001634
1635 return 0;
1636}
1637
1638/*?
1639 * Control event logging of api, cli, and thread barrier events
1640 * With no arguments, displays the current trace status.
1641 * Name the event groups you wish to trace or stop tracing.
1642 *
1643 * @cliexpar
1644 * @clistart
Dave Barachaa676742020-11-25 07:23:42 -05001645 * event-logger trace api cli barrier
1646 * event-logger trace api cli barrier disable
1647 * event-logger trace dispatch
1648 * event-logger trace circuit-node ethernet-input
Dave Barachc3a06552018-10-01 09:25:32 -04001649 * @cliend
Dave Barachaa676742020-11-25 07:23:42 -05001650 * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
Dave Barachc3a06552018-10-01 09:25:32 -04001651?*/
1652/* *INDENT-OFF* */
Dave Barachaa676742020-11-25 07:23:42 -05001653VLIB_CLI_COMMAND (event_logger_trace_command, static) =
Dave Barachc3a06552018-10-01 09:25:32 -04001654{
Dave Barachaa676742020-11-25 07:23:42 -05001655 .path = "event-logger trace",
1656 .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
Dave Barach900cbad2019-01-31 19:12:51 -05001657 "[circuit-node <name> e.g. ethernet-input][disable]",
Dave Barachaa676742020-11-25 07:23:42 -05001658 .function = event_logger_trace_command_fn,
Dave Barachc3a06552018-10-01 09:25:32 -04001659};
1660/* *INDENT-ON* */
1661
1662static clib_error_t *
Dave Barachc4abafd2019-09-04 12:09:32 -04001663suspend_command_fn (vlib_main_t * vm,
1664 unformat_input_t * input, vlib_cli_command_t * cmd)
1665{
1666 vlib_process_suspend (vm, 30e-3);
1667 return 0;
1668}
1669
1670/* *INDENT-OFF* */
1671VLIB_CLI_COMMAND (suspend_command, static) =
1672{
1673 .path = "suspend",
1674 .short_help = "suspend debug CLI for 30ms",
1675 .function = suspend_command_fn,
Dave Baracha1f5a952019-11-01 11:24:43 -04001676 .is_mp_safe = 1,
1677};
1678/* *INDENT-ON* */
1679
1680
1681static int
1682sort_cmds_by_path (void *a1, void *a2)
1683{
1684 u32 *index1 = a1;
1685 u32 *index2 = a2;
Damjan Marionfd8deb42021-03-06 12:26:28 +01001686 vlib_global_main_t *vgm = vlib_get_global_main ();
1687 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Baracha1f5a952019-11-01 11:24:43 -04001688 vlib_cli_command_t *c1, *c2;
1689 int i, lmin;
1690
1691 c1 = vec_elt_at_index (cm->commands, *index1);
1692 c2 = vec_elt_at_index (cm->commands, *index2);
1693
1694 lmin = vec_len (c1->path);
1695 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1696
1697 for (i = 0; i < lmin; i++)
1698 {
1699 if (c1->path[i] < c2->path[i])
1700 return -1;
1701 else if (c1->path[i] > c2->path[i])
1702 return 1;
1703 }
1704
1705 return 0;
1706}
1707
1708typedef struct
1709{
1710 vlib_cli_main_t *cm;
1711 u32 parent_command_index;
1712 int show_mp_safe;
1713 int show_not_mp_safe;
1714 int show_hit;
1715 int clear_hit;
1716} vlib_cli_walk_args_t;
1717
1718static void
1719cli_recursive_walk (vlib_cli_walk_args_t * aa)
1720{
1721 vlib_cli_command_t *parent;
1722 vlib_cli_sub_command_t *sub;
1723 vlib_cli_walk_args_t _a, *a = &_a;
1724 vlib_cli_main_t *cm;
1725 int i;
1726
1727 /* Copy args into this stack frame */
1728 *a = *aa;
1729 cm = a->cm;
1730
1731 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1732
1733 if (parent->function)
1734 {
1735 if (((a->show_mp_safe && parent->is_mp_safe)
1736 || (a->show_not_mp_safe && !parent->is_mp_safe))
1737 && (a->show_hit == 0 || parent->hit_counter))
1738 {
1739 vec_add1 (cm->sort_vector, a->parent_command_index);
1740 }
1741
1742 if (a->clear_hit)
1743 parent->hit_counter = 0;
1744 }
1745
1746 for (i = 0; i < vec_len (parent->sub_commands); i++)
1747 {
1748 sub = vec_elt_at_index (parent->sub_commands, i);
1749 a->parent_command_index = sub->index;
1750 cli_recursive_walk (a);
1751 }
1752}
1753
1754static u8 *
1755format_mp_safe (u8 * s, va_list * args)
1756{
1757 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1758 int show_mp_safe = va_arg (*args, int);
1759 int show_not_mp_safe = va_arg (*args, int);
1760 int show_hit = va_arg (*args, int);
1761 int clear_hit = va_arg (*args, int);
1762 vlib_cli_command_t *c;
1763 vlib_cli_walk_args_t _a, *a = &_a;
1764 int i;
1765 char *format_string = "\n%v";
1766
1767 if (show_hit)
1768 format_string = "\n%v: %u";
1769
1770 vec_reset_length (cm->sort_vector);
1771
1772 a->cm = cm;
1773 a->parent_command_index = 0;
1774 a->show_mp_safe = show_mp_safe;
1775 a->show_not_mp_safe = show_not_mp_safe;
1776 a->show_hit = show_hit;
1777 a->clear_hit = clear_hit;
1778
1779 cli_recursive_walk (a);
1780
1781 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1782
1783 for (i = 0; i < vec_len (cm->sort_vector); i++)
1784 {
1785 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1786 s = format (s, format_string, c->path, c->hit_counter);
1787 }
1788
1789 return s;
1790}
1791
1792
1793static clib_error_t *
1794show_cli_command_fn (vlib_main_t * vm,
1795 unformat_input_t * input, vlib_cli_command_t * cmd)
1796{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001797 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Baracha1f5a952019-11-01 11:24:43 -04001798 int show_mp_safe = 0;
1799 int show_not_mp_safe = 0;
1800 int show_hit = 0;
1801 int clear_hit = 0;
1802
1803 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1804 {
1805 if (unformat (input, "mp-safe"))
1806 show_mp_safe = 1;
1807 if (unformat (input, "not-mp-safe"))
1808 show_not_mp_safe = 1;
1809 else if (unformat (input, "hit"))
1810 show_hit = 1;
1811 else if (unformat (input, "clear-hit"))
1812 clear_hit = 1;
1813 else
1814 break;
1815 }
1816
1817 /* default set: all cli commands */
1818 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1819 show_mp_safe = show_not_mp_safe = 1;
1820
Damjan Marionfd8deb42021-03-06 12:26:28 +01001821 vlib_cli_output (vm, "%U", format_mp_safe, &vgm->cli_main, show_mp_safe,
1822 show_not_mp_safe, show_hit, clear_hit);
Dave Baracha1f5a952019-11-01 11:24:43 -04001823 if (clear_hit)
1824 vlib_cli_output (vm, "hit counters cleared...");
1825
1826 return 0;
1827}
1828
1829/*?
1830 * Displays debug cli command information
1831 *
1832 * @cliexpar
1833 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1834 *
1835 * "show cli" displays the entire debug cli:
1836 *
1837 * abf attach
1838 * abf policy
1839 * adjacency counters
1840 * api trace
1841 * app ns
1842 * bfd key del
1843 * ... and so on ...
1844 *
1845 * "show cli mp-safe" displays mp-safe debug CLI commands:
1846 *
1847 * abf policy
1848 * binary-api
1849 * create vhost-user
1850 * exec
1851 * ip container
1852 * ip mroute
1853 * ip probe-neighbor
1854 * ip route
1855 * ip scan-neighbor
1856 * ip table
1857 * ip6 table
1858 *
1859 * "show cli not-mp-safe" displays debug CLI commands
1860 * which cause worker thread barrier synchronization
1861 *
1862 * "show cli hit" displays commands which have been executed. Qualify
1863 * as desired with "mp-safe" or "not-mp-safe".
1864 *
1865 * "show cli clear-hit" clears the per-command hit counters.
1866 * @cliexend
1867?*/
1868
1869/* *INDENT-OFF* */
1870VLIB_CLI_COMMAND (show_cli_command, static) =
1871{
1872 .path = "show cli",
1873 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1874 .function = show_cli_command_fn,
1875 .is_mp_safe = 1,
Dave Barachc4abafd2019-09-04 12:09:32 -04001876};
1877/* *INDENT-ON* */
1878
1879static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -04001880vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001881{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001882 vlib_global_main_t *vgm = vlib_get_global_main ();
1883 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001884 clib_error_t *error = 0;
1885 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886
1887 cmd = cm->cli_command_registrations;
1888
1889 while (cmd)
1890 {
1891 error = vlib_cli_register (vm, cmd);
1892 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001893 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001894 cmd = cmd->next_cli_command;
1895 }
Dave Barach3d0e0d12021-02-17 10:25:18 -05001896
1897 cm->log = vlib_log_register_class_rate_limit (
1898 "cli", "log", 0x7FFFFFFF /* aka no rate limit */);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001899 return error;
1900}
1901
1902VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001903
1904/*
1905 * fd.io coding-style-patch-verification: ON
1906 *
1907 * Local Variables:
1908 * eval: (c-set-style "gnu")
1909 * End:
1910 */