blob: 5cad95ee0047c8e4cf2bbc01b61ad1304aff1def [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{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100208 vlib_main_t __clib_unused *vm = va_arg (*args, vlib_main_t *);
209 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400210 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
211 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100212 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400213 uword *match_bitmap, is_unique, index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 match_bitmap = vlib_cli_sub_command_match (c, i);
216 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
217 index = ~0;
218 if (is_unique)
219 {
220 index = clib_bitmap_first_set (match_bitmap);
221 *result = get_sub_command (cm, c, index);
222 }
223 clib_bitmap_free (match_bitmap);
224
225 return is_unique;
226}
227
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200228static int
229vlib_cli_cmp_strings (void *a1, void *a2)
230{
231 u8 *c1 = *(u8 **) a1;
232 u8 *c2 = *(u8 **) a2;
233
234 return vec_cmp (c1, c2);
235}
236
237u8 **
238vlib_cli_get_possible_completions (u8 * str)
239{
240 vlib_cli_command_t *c;
241 vlib_cli_sub_command_t *sc;
Damjan Marionfd8deb42021-03-06 12:26:28 +0100242 vlib_global_main_t *vgm = vlib_get_global_main ();
243 vlib_cli_main_t *vcm = &vgm->cli_main;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200244 uword *match_bitmap = 0;
245 uword index, is_unique, help_next_level;
246 u8 **result = 0;
247 unformat_input_t input;
248 unformat_init_vector (&input, vec_dup (str));
249 c = vec_elt_at_index (vcm->commands, 0);
250
251 /* remove trailing whitespace, except for one of them */
252 while (vec_len (input.buffer) >= 2 &&
253 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
254 isspace (input.buffer[vec_len (input.buffer) - 2]))
255 {
256 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
257 }
258
259 /* if input is empty, directly return list of root commands */
260 if (vec_len (input.buffer) == 0 ||
261 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
262 {
263 vec_foreach (sc, c->sub_commands)
264 {
265 vec_add1 (result, (u8 *) sc->name);
266 }
267 goto done;
268 }
269
270 /* add a trailing '?' so that vlib_cli_sub_command_match can find
271 * all commands starting with the input string */
272 vec_add1 (input.buffer, '?');
273
274 while (1)
275 {
276 match_bitmap = vlib_cli_sub_command_match (c, &input);
277 /* no match: return no result */
278 if (match_bitmap == 0)
279 {
280 goto done;
281 }
282 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
283 /* unique match: try to step one subcommand level further */
284 if (is_unique)
285 {
286 /* stop if no more input */
287 if (input.index >= vec_len (input.buffer) - 1)
288 {
289 break;
290 }
291
292 index = clib_bitmap_first_set (match_bitmap);
293 c = get_sub_command (vcm, c, index);
294 clib_bitmap_free (match_bitmap);
295 continue;
296 }
297 /* multiple matches: stop here, return all matches */
298 break;
299 }
300
301 /* remove trailing '?' */
302 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
303
304 /* if we have a space at the end of input, and a unique match,
305 * autocomplete the next level of subcommands */
306 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
307 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100308 clib_bitmap_foreach (index, match_bitmap) {
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200309 if (help_next_level && is_unique) {
310 c = get_sub_command (vcm, c, index);
311 vec_foreach (sc, c->sub_commands) {
312 vec_add1 (result, (u8*) sc->name);
313 }
314 goto done; /* break doesn't work in this macro-loop */
315 }
316 sc = &c->sub_commands[index];
317 vec_add1(result, (u8*) sc->name);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100318 }
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200319 /* *INDENT-ON* */
320
321done:
322 clib_bitmap_free (match_bitmap);
323 unformat_free (&input);
324
Yoann Desmouceaux4227eef2017-05-24 15:51:48 +0200325 if (result)
326 vec_sort_with_function (result, vlib_cli_cmp_strings);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200327 return result;
328}
329
Dave Barach9b8ffd92016-07-08 08:13:45 -0400330static u8 *
331format_vlib_cli_command_help (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400333 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 int is_long = va_arg (*args, int);
335 if (is_long && c->long_help)
336 s = format (s, "%s", c->long_help);
337 else if (c->short_help)
338 s = format (s, "%s", c->short_help);
339 else
340 s = format (s, "%v commands", c->path);
341 return s;
342}
343
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400345format_vlib_cli_path (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 u8 *path = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Dave Barach6b3f25c2019-12-09 10:45:47 -0500349 s = format (s, "%v", path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
351 return s;
352}
353
354static vlib_cli_command_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400357 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
358 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
360 if (c->function)
361 vec_add1 (subs, c[0]);
362
Dave Barach9b8ffd92016-07-08 08:13:45 -0400363 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
365 return subs;
366}
367
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500368static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400369vlib_cli_cmp_rule (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500370{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400371 vlib_cli_sub_rule_t *r1 = a1;
372 vlib_cli_sub_rule_t *r2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500373
374 return vec_cmp (r1->name, r2->name);
375}
376
377static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400378vlib_cli_cmp_command (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500379{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400380 vlib_cli_command_t *c1 = a1;
381 vlib_cli_command_t *c2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500382
383 return vec_cmp (c1->path, c2->path);
384}
385
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386static clib_error_t *
387vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
388 vlib_cli_main_t * cm,
389 unformat_input_t * input,
390 uword parent_command_index)
391{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100392 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 vlib_cli_command_t *parent, *c;
394 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400396 u8 *string;
Damjan Marionfd8deb42021-03-06 12:26:28 +0100397 uword is_main_dispatch = cm == &vgm->cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
399 parent = vec_elt_at_index (cm->commands, parent_command_index);
400 if (is_main_dispatch && unformat (input, "help"))
401 {
402 uword help_at_end_of_line, i;
403
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404 help_at_end_of_line =
405 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 while (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400407 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 c = parent;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400409 if (unformat_user
410 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 ;
412
Dave Barach9b8ffd92016-07-08 08:13:45 -0400413 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 goto unknown;
415
416 else
417 break;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418 }
419
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 /* help SUB-COMMAND => long format help.
Dave Barach9b8ffd92016-07-08 08:13:45 -0400421 "help" at end of line: show all commands. */
422 if (!help_at_end_of_line)
423 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
424 /* is_long */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
Dave Barach6b3f25c2019-12-09 10:45:47 -0500426 else if (vec_len (c->sub_commands) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 vlib_cli_output (vm, "%v: no sub-commands", c->path);
428
429 else
430 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500431 vlib_cli_sub_rule_t *sr, *subs = 0;
Dave Barach6d5df8d2019-12-11 09:46:56 -0500432 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433
Dave Barach6d5df8d2019-12-11 09:46:56 -0500434 vec_foreach (sc, c->sub_commands)
435 {
436 vec_add2 (subs, sr, 1);
437 sr->name = sc->name;
438 sr->command_index = sc->index;
439 sr->rule_index = ~0;
440 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500442 vec_sort_with_function (subs, vlib_cli_cmp_rule);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 for (i = 0; i < vec_len (subs); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
448 d = vec_elt_at_index (cm->commands, subs[i].command_index);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500449 vlib_cli_output
450 (vm, " %-30v %U", subs[i].name,
451 format_vlib_cli_command_help, d, /* is_long */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 }
453
454 vec_free (subs);
455 }
456 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400457
458 else if (is_main_dispatch
459 && (unformat (input, "choices") || unformat (input, "?")))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400461 vlib_cli_command_t *sub, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
463 subs = all_subs (cm, 0, parent_command_index);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500464 vec_sort_with_function (subs, vlib_cli_cmp_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 vec_foreach (sub, subs)
466 vlib_cli_output (vm, " %-40U %U",
467 format_vlib_cli_path, sub->path,
468 format_vlib_cli_command_help, sub, /* is_long */ 0);
469 vec_free (subs);
470 }
471
472 else if (unformat (input, "comment %v", &string))
473 {
474 vec_free (string);
475 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476
Dave Barach3d0e0d12021-02-17 10:25:18 -0500477 else if (unformat (input, "vpplog %v", &string))
478 {
479 int i;
480 /*
481 * Delete leading whitespace, so "vpplog { this and that }"
482 * and "vpplog this" line up nicely.
483 */
484 for (i = 0; i < vec_len (string); i++)
485 if (string[i] != ' ')
486 break;
487 if (i > 0)
488 vec_delete (string, i, 0);
489
490 vlib_log_notice (cm->log, "CLI: %v", string);
491 vec_free (string);
492 }
493
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 else if (unformat (input, "uncomment %U",
495 unformat_vlib_cli_sub_input, &sub_input))
496 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400497 error =
498 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
499 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 unformat_free (&sub_input);
501 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400502 else if (unformat (input, "leak-check %U",
503 unformat_vlib_cli_sub_input, &sub_input))
504 {
505 u8 *leak_report;
Dave Barachd67a4282019-06-15 12:46:13 -0400506 if (current_traced_heap)
507 {
508 void *oldheap;
509 oldheap = clib_mem_set_heap (current_traced_heap);
510 clib_mem_trace (0);
511 clib_mem_set_heap (oldheap);
512 current_traced_heap = 0;
513 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400514 clib_mem_trace (1);
515 error =
516 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
517 parent_command_index);
518 unformat_free (&sub_input);
519
520 /* Otherwise, the clib_error_t shows up as a leak... */
521 if (error)
522 {
523 vlib_cli_output (vm, "%v", error->what);
524 clib_error_free (error);
525 error = 0;
526 }
527
528 (void) clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200529 leak_report = format (0, "%U", format_clib_mem_heap, 0,
Dave Barach8fdde3c2019-05-17 10:46:40 -0400530 1 /* verbose, i.e. print leaks */ );
531 clib_mem_trace (0);
532 vlib_cli_output (vm, "%v", leak_report);
533 vec_free (leak_report);
534 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535
536 else
537 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539 unformat_input_t *si;
540 uword has_sub_commands =
541 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
542
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 si = input;
544 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
545 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 if (has_sub_commands)
548 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
549
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550 if (has_sub_commands && !error)
551 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
553 else if (c->function)
554 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400555 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
557 /* Skip white space for benefit of called function. */
558 unformat_skip_white_space (si);
559
560 if (unformat (si, "?"))
561 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400562 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
563 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564 }
565 else
566 {
Dave Barachc3a06552018-10-01 09:25:32 -0400567 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
568 {
569 /* *INDENT-OFF* */
570 ELOG_TYPE_DECLARE (e) =
571 {
572 .format = "cli-cmd: %s",
573 .format_args = "T4",
574 };
575 /* *INDENT-ON* */
576 struct
577 {
578 u32 c;
579 } *ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100580 ed = ELOG_DATA (vlib_get_elog_main (), e);
581 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400582 }
583
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584 if (!c->is_mp_safe)
585 vlib_worker_thread_barrier_sync (vm);
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000586 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
587 clib_call_callbacks (cm->perf_counter_cbs, cm,
588 c - cm->commands, 0 /* before */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589
Dave Baracha1f5a952019-11-01 11:24:43 -0400590 c->hit_counter++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 c_error = c->function (vm, si, c);
592
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000593 if (PREDICT_FALSE (vec_len (cm->perf_counter_cbs) != 0))
594 clib_call_callbacks (cm->perf_counter_cbs, cm,
595 c - cm->commands, 1 /* after */ );
Dave Barach9b8ffd92016-07-08 08:13:45 -0400596 if (!c->is_mp_safe)
597 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598
Dave Barachc3a06552018-10-01 09:25:32 -0400599 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
600 {
601 /* *INDENT-OFF* */
602 ELOG_TYPE_DECLARE (e) =
603 {
604 .format = "cli-cmd: %s %s",
605 .format_args = "T4T4",
606 };
607 /* *INDENT-ON* */
608 struct
609 {
610 u32 c, err;
611 } *ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100612 ed = ELOG_DATA (vlib_get_elog_main (), e);
613 ed->c = elog_string (vlib_get_elog_main (), "%v", c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400614 if (c_error)
615 {
616 vec_add1 (c_error->what, 0);
Damjan Marionf553a2c2021-03-26 13:45:37 +0100617 ed->err = elog_string (vlib_get_elog_main (),
618 (char *) c_error->what);
Dave Barachc3a06552018-10-01 09:25:32 -0400619 _vec_len (c_error->what) -= 1;
620 }
621 else
Damjan Marionf553a2c2021-03-26 13:45:37 +0100622 ed->err = elog_string (vlib_get_elog_main (), "OK");
Dave Barachc3a06552018-10-01 09:25:32 -0400623 }
624
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 if (c_error)
626 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400627 error =
628 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 clib_error_free (c_error);
630 /* Free sub input. */
631 if (si != input)
632 unformat_free (si);
633
634 return error;
635 }
636 }
637
638 /* Free any previous error. */
639 clib_error_free (error);
640 }
641
Dave Barach9b8ffd92016-07-08 08:13:45 -0400642 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643 error = clib_error_return (0, "%v: no sub-commands", c->path);
644
645 /* Free sub input. */
646 if (si != input)
647 unformat_free (si);
648 }
649
650 else
651 goto unknown;
652
653 return error;
654
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
658 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400660 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
661 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662}
663
664
Dave Barach9b8ffd92016-07-08 08:13:45 -0400665void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
666 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Dave Barach9b8ffd92016-07-08 08:13:45 -0400668void
669vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
670{
671}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672
673/* Process CLI input. */
Ole Troan72d87582019-05-10 12:01:10 +0200674int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400675vlib_cli_input (vlib_main_t * vm,
676 unformat_input_t * input,
677 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100679 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680 vlib_process_t *cp = vlib_get_current_process (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400681 clib_error_t *error;
682 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 uword save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200684 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000686 save_function = cp->output_function;
687 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000689 cp->output_function = function;
690 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
Dave Barach9b8ffd92016-07-08 08:13:45 -0400692 do
693 {
Damjan Marionfd8deb42021-03-06 12:26:28 +0100694 error = vlib_cli_dispatch_sub_commands (vm, &vgm->cli_main, input,
Dave Barach6b3f25c2019-12-09 10:45:47 -0500695 /* parent */ 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400696 }
697 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
699 if (error)
700 {
701 vlib_cli_output (vm, "%v", error->what);
702 vlib_unix_error_report (vm, error);
Ole Troan72d87582019-05-10 12:01:10 +0200703 /* clib_error_return is unfortunately often called with a '0'
704 return code */
705 rv = error->code != 0 ? error->code : -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706 clib_error_free (error);
707 }
708
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000709 cp->output_function = save_function;
710 cp->output_function_arg = save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200711 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712}
713
714/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400715void
716vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400718 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400720 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721
722 va_start (va, fmt);
723 s = va_format (0, fmt, &va);
724 va_end (va);
725
Nathan Skrzypczak19ce5022020-11-23 17:56:32 +0100726 /* some format functions might return 0
727 * e.g. show int addr */
728 if (NULL == s)
729 return;
730
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 vec_add1 (s, '\n');
734
Dave Barach9b8ffd92016-07-08 08:13:45 -0400735 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 fformat (stdout, "%v", s);
737 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000738 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739
740 vec_free (s);
741}
742
Ole Troan73710c72018-06-04 22:27:49 +0200743void *vl_msg_push_heap (void) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400744void *
745vl_msg_push_heap (void)
746{
747 return 0;
748}
749
Ole Troan73710c72018-06-04 22:27:49 +0200750void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400751void
752vl_msg_pop_heap (void *oldheap)
753{
754}
755
756void *vlib_stats_push_heap (void *) __attribute__ ((weak));
757void *
758vlib_stats_push_heap (void *notused)
759{
760 return 0;
761}
Ole Troan73710c72018-06-04 22:27:49 +0200762
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763static clib_error_t *
764show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400765 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200767 clib_mem_main_t *mm = &clib_mem_main;
Dave Barachd67a4282019-06-15 12:46:13 -0400768 int verbose __attribute__ ((unused)) = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500769 int api_segment = 0, stats_segment = 0, main_heap = 0, numa_heaps = 0;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200770 int map = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400771 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 u32 index = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500773 int i;
Dave Barachd67a4282019-06-15 12:46:13 -0400774 uword clib_mem_trace_enable_disable (uword enable);
775 uword was_enabled;
776
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Dave Barach9b8ffd92016-07-08 08:13:45 -0400778 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400780 if (unformat (input, "verbose"))
781 verbose = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200782 else if (unformat (input, "api-segment"))
783 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400784 else if (unformat (input, "stats-segment"))
785 stats_segment = 1;
786 else if (unformat (input, "main-heap"))
787 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -0500788 else if (unformat (input, "numa-heaps"))
789 numa_heaps = 1;
Damjan Marion6bfd0762020-09-11 22:16:53 +0200790 else if (unformat (input, "map"))
791 map = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400792 else
793 {
794 error = clib_error_return (0, "unknown input `%U'",
795 format_unformat_error, input);
796 return error;
797 }
798 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Damjan Marion6bfd0762020-09-11 22:16:53 +0200800 if ((api_segment + stats_segment + main_heap + numa_heaps + map) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -0400801 return clib_error_return
Damjan Marion6bfd0762020-09-11 22:16:53 +0200802 (0, "Need one of api-segment, stats-segment, main-heap, numa-heaps "
803 "or map");
Dave Barachd67a4282019-06-15 12:46:13 -0400804
Ole Troan73710c72018-06-04 22:27:49 +0200805 if (api_segment)
806 {
807 void *oldheap = vl_msg_push_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400808 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200809 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Ole Troan73710c72018-06-04 22:27:49 +0200810 vl_msg_pop_heap (oldheap);
811 u8 *s = vec_dup (s_in_svm);
812
813 oldheap = vl_msg_push_heap ();
814 vec_free (s_in_svm);
Dave Barachd67a4282019-06-15 12:46:13 -0400815 clib_mem_trace_enable_disable (was_enabled);
Ole Troan73710c72018-06-04 22:27:49 +0200816 vl_msg_pop_heap (oldheap);
Dave Barachd67a4282019-06-15 12:46:13 -0400817 vlib_cli_output (vm, "API segment");
Ole Troan73710c72018-06-04 22:27:49 +0200818 vlib_cli_output (vm, "%v", s);
Dave Barachd67a4282019-06-15 12:46:13 -0400819 vec_free (s);
820 }
821 if (stats_segment)
822 {
823 void *oldheap = vlib_stats_push_heap (0);
824 was_enabled = clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200825 u8 *s_in_svm = format (0, "%U\n", format_clib_mem_heap, 0, 1);
Dave Barachd67a4282019-06-15 12:46:13 -0400826 if (oldheap)
827 clib_mem_set_heap (oldheap);
828 u8 *s = vec_dup (s_in_svm);
829
830 oldheap = vlib_stats_push_heap (0);
831 vec_free (s_in_svm);
832 if (oldheap)
833 {
834 clib_mem_trace_enable_disable (was_enabled);
835 clib_mem_set_heap (oldheap);
836 }
837 vlib_cli_output (vm, "Stats segment");
838 vlib_cli_output (vm, "%v", s);
Ole Troan73710c72018-06-04 22:27:49 +0200839 vec_free (s);
840 }
841
Dave Baracha690fdb2020-01-21 12:34:55 -0500842
Dave Barach6a5adc32018-07-04 10:56:23 -0400843 {
Dave Barachd67a4282019-06-15 12:46:13 -0400844 if (main_heap)
845 {
846 /*
847 * Note: the foreach_vlib_main causes allocator traffic,
848 * so shut off tracing before we go there...
849 */
850 was_enabled = clib_mem_trace_enable_disable (0);
Dave Barach6a5adc32018-07-04 10:56:23 -0400851
Dave Barachd67a4282019-06-15 12:46:13 -0400852 /* *INDENT-OFF* */
853 foreach_vlib_main (
854 ({
Dave Barachd67a4282019-06-15 12:46:13 -0400855 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
856 vlib_worker_threads[index].name);
Damjan Marion4537c302020-09-28 19:03:37 +0200857 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
Damjan Marion57d1ec02020-09-16 21:15:44 +0200858 mm->per_cpu_mheaps[index],
Dave Barachd67a4282019-06-15 12:46:13 -0400859 verbose);
860 index++;
861 }));
862 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400863
Dave Barachd67a4282019-06-15 12:46:13 -0400864 /* Restore the trace flag */
865 clib_mem_trace_enable_disable (was_enabled);
866 }
Dave Baracha690fdb2020-01-21 12:34:55 -0500867 if (numa_heaps)
868 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200869 for (i = 0; i < ARRAY_LEN (mm->per_numa_mheaps); i++)
Dave Baracha690fdb2020-01-21 12:34:55 -0500870 {
Damjan Marion57d1ec02020-09-16 21:15:44 +0200871 if (mm->per_numa_mheaps[i] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -0500872 continue;
Damjan Marion57d1ec02020-09-16 21:15:44 +0200873 if (mm->per_numa_mheaps[i] == mm->per_cpu_mheaps[i])
Dave Baracha690fdb2020-01-21 12:34:55 -0500874 {
875 vlib_cli_output (vm, "Numa %d uses the main heap...", i);
876 continue;
877 }
878 was_enabled = clib_mem_trace_enable_disable (0);
Dave Baracha690fdb2020-01-21 12:34:55 -0500879
Dave Baracha690fdb2020-01-21 12:34:55 -0500880 vlib_cli_output (vm, "Numa %d:", i);
Damjan Marion4537c302020-09-28 19:03:37 +0200881 vlib_cli_output (vm, " %U\n", format_clib_mem_heap,
Damjan Marion57d1ec02020-09-16 21:15:44 +0200882 mm->per_numa_mheaps[index], verbose);
Dave Baracha690fdb2020-01-21 12:34:55 -0500883 }
884 }
Damjan Marion6bfd0762020-09-11 22:16:53 +0200885 if (map)
886 {
887 clib_mem_page_stats_t stats = { };
888 clib_mem_vm_map_hdr_t *hdr = 0;
889 u8 *s = 0;
890 int numa = -1;
891
Damjan Marion5ef25162020-09-17 13:29:33 +0200892 s = format (s, "\n%-16s%7s%5s%7s%7s",
893 "StartAddr", "size", "FD", "PageSz", "Pages");
Damjan Marion6bfd0762020-09-11 22:16:53 +0200894 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
895 s = format (s, " Numa%u", numa);
896 s = format (s, " NotMap");
897 s = format (s, " Name");
898 vlib_cli_output (vm, "%v", s);
899 vec_reset_length (s);
900
901 while ((hdr = clib_mem_vm_get_next_map_hdr (hdr)))
902 {
903 clib_mem_get_page_stats ((void *) hdr->base_addr,
904 hdr->log2_page_sz, hdr->num_pages,
905 &stats);
Damjan Marion5ef25162020-09-17 13:29:33 +0200906 s = format (s, "%016lx%7U",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200907 hdr->base_addr, format_memory_size,
Damjan Marion5ef25162020-09-17 13:29:33 +0200908 hdr->num_pages << hdr->log2_page_sz);
909
910 if (hdr->fd != -1)
911 s = format (s, "%5d", hdr->fd);
912 else
913 s = format (s, "%5s", " ");
914
915 s = format (s, "%7U%7lu",
Damjan Marion6bfd0762020-09-11 22:16:53 +0200916 format_log2_page_size, hdr->log2_page_sz,
917 hdr->num_pages);
918 while ((numa = vlib_mem_get_next_numa_node (numa)) != -1)
919 s = format (s, "%6lu", stats.per_numa[numa]);
920 s = format (s, "%7lu", stats.not_mapped);
921 s = format (s, " %s", hdr->name);
922 vlib_cli_output (vm, "%v", s);
923 vec_reset_length (s);
924 }
925 vec_free (s);
926 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400927 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 return 0;
929}
930
Dave Barach9b8ffd92016-07-08 08:13:45 -0400931/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
933 .path = "show memory",
Dave Baracha690fdb2020-01-21 12:34:55 -0500934 .short_help = "show memory [api-segment][stats-segment][verbose]\n"
Damjan Marion6bfd0762020-09-11 22:16:53 +0200935 " [numa-heaps][map]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936 .function = show_memory_usage,
937};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400938/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939
940static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100941show_cpu (vlib_main_t * vm, unformat_input_t * input,
942 vlib_cli_command_t * cmd)
943{
944#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
945 _("Model name", "%U", format_cpu_model_name);
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800946 _("Microarch model (family)", "%U", format_cpu_uarch);
Damjan Marioneccf5092016-11-18 16:59:24 +0100947 _("Flags", "%U", format_cpu_flags);
948 _("Base frequency", "%.2f GHz",
949 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
950#undef _
951 return 0;
952}
953
954/*?
955 * Displays various information about the CPU.
956 *
957 * @cliexpar
958 * @cliexstart{show cpu}
959 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
960 * Microarchitecture: Broadwell (Broadwell-EP/EX)
961 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
962 * Base Frequency: 3.20 GHz
963 * @cliexend
964?*/
965/* *INDENT-OFF* */
966VLIB_CLI_COMMAND (show_cpu_command, static) = {
967 .path = "show cpu",
968 .short_help = "Show cpu information",
969 .function = show_cpu,
970};
Damjan Marioneccf5092016-11-18 16:59:24 +0100971/* *INDENT-ON* */
Ole Troan73710c72018-06-04 22:27:49 +0200972
Damjan Marioneccf5092016-11-18 16:59:24 +0100973static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974enable_disable_memory_trace (vlib_main_t * vm,
975 unformat_input_t * input,
976 vlib_cli_command_t * cmd)
977{
Damjan Marion57d1ec02020-09-16 21:15:44 +0200978 clib_mem_main_t *mm = &clib_mem_main;
Ole Troan73710c72018-06-04 22:27:49 +0200979 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachd67a4282019-06-15 12:46:13 -0400980 int enable = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200981 int api_segment = 0;
Dave Barachd67a4282019-06-15 12:46:13 -0400982 int stats_segment = 0;
983 int main_heap = 0;
Dave Baracha690fdb2020-01-21 12:34:55 -0500984 u32 numa_id = ~0;
Ole Troan73710c72018-06-04 22:27:49 +0200985 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
Ole Troan73710c72018-06-04 22:27:49 +0200987 if (!unformat_user (input, unformat_line_input, line_input))
988 return 0;
989
990 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991 {
Dave Barach1ddbc012018-06-13 09:26:05 -0400992 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
Ole Troan73710c72018-06-04 22:27:49 +0200993 ;
994 else if (unformat (line_input, "api-segment"))
995 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400996 else if (unformat (line_input, "stats-segment"))
997 stats_segment = 1;
998 else if (unformat (line_input, "main-heap"))
999 main_heap = 1;
Dave Baracha690fdb2020-01-21 12:34:55 -05001000 else if (unformat (line_input, "numa-heap %d", &numa_id))
1001 ;
Ole Troan73710c72018-06-04 22:27:49 +02001002 else
1003 {
1004 unformat_free (line_input);
1005 return clib_error_return (0, "invalid input");
1006 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007 }
Ole Troan73710c72018-06-04 22:27:49 +02001008 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009
Dave Baracha690fdb2020-01-21 12:34:55 -05001010 if ((api_segment + stats_segment + main_heap + (enable == 0)
1011 + (numa_id != ~0)) == 0)
Dave Barachd67a4282019-06-15 12:46:13 -04001012 {
1013 return clib_error_return
Dave Baracha690fdb2020-01-21 12:34:55 -05001014 (0, "Need one of main-heap, stats-segment, api-segment,\n"
1015 "numa-heap <nn> or disable");
Dave Barachd67a4282019-06-15 12:46:13 -04001016 }
1017
1018 /* Turn off current trace, if any */
1019 if (current_traced_heap)
1020 {
1021 void *oldheap;
1022 oldheap = clib_mem_set_heap (current_traced_heap);
1023 clib_mem_trace (0);
1024 clib_mem_set_heap (oldheap);
1025 current_traced_heap = 0;
1026 }
1027
1028 if (enable == 0)
1029 return 0;
1030
1031 /* API segment */
Ole Troan73710c72018-06-04 22:27:49 +02001032 if (api_segment)
Dave Barachd67a4282019-06-15 12:46:13 -04001033 {
1034 oldheap = vl_msg_push_heap ();
1035 current_traced_heap = clib_mem_get_heap ();
1036 clib_mem_trace (1);
1037 vl_msg_pop_heap (oldheap);
1038
1039 }
1040
1041 /* Stats segment */
1042 if (stats_segment)
1043 {
1044 oldheap = vlib_stats_push_heap (0);
1045 current_traced_heap = clib_mem_get_heap ();
1046 clib_mem_trace (stats_segment);
1047 /* We don't want to call vlib_stats_pop_heap... */
1048 if (oldheap)
1049 clib_mem_set_heap (oldheap);
1050 }
1051
1052 /* main_heap */
1053 if (main_heap)
1054 {
1055 current_traced_heap = clib_mem_get_heap ();
1056 clib_mem_trace (main_heap);
1057 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
Dave Baracha690fdb2020-01-21 12:34:55 -05001059 if (numa_id != ~0)
1060 {
Damjan Marion57d1ec02020-09-16 21:15:44 +02001061 if (numa_id >= ARRAY_LEN (mm->per_numa_mheaps))
Dave Baracha690fdb2020-01-21 12:34:55 -05001062 return clib_error_return (0, "Numa %d out of range", numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001063 if (mm->per_numa_mheaps[numa_id] == 0)
Dave Baracha690fdb2020-01-21 12:34:55 -05001064 return clib_error_return (0, "Numa %d heap not active", numa_id);
1065
Damjan Marion57d1ec02020-09-16 21:15:44 +02001066 if (mm->per_numa_mheaps[numa_id] == clib_mem_get_heap ())
Dave Baracha690fdb2020-01-21 12:34:55 -05001067 return clib_error_return (0, "Numa %d uses the main heap...",
1068 numa_id);
Damjan Marion57d1ec02020-09-16 21:15:44 +02001069 current_traced_heap = mm->per_numa_mheaps[numa_id];
Dave Baracha690fdb2020-01-21 12:34:55 -05001070 oldheap = clib_mem_set_heap (current_traced_heap);
1071 clib_mem_trace (1);
1072 clib_mem_set_heap (oldheap);
1073 }
1074
1075
Ole Troan73710c72018-06-04 22:27:49 +02001076 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077}
1078
Dave Barach9b8ffd92016-07-08 08:13:45 -04001079/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
1081 .path = "memory-trace",
Dave Baracha690fdb2020-01-21 12:34:55 -05001082 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n"
1083 " [numa-heap <numa-id>]\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 .function = enable_disable_memory_trace,
1085};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001086/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087
Damjan Marione5ef1d72017-03-02 12:33:48 +01001088static clib_error_t *
1089restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1090 vlib_cli_command_t * cmd)
1091{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001092 vlib_global_main_t *vgm = vlib_get_global_main ();
Chris Luke6edd3602018-06-12 22:45:06 -04001093 clib_file_main_t *fm = &file_main;
1094 clib_file_t *f;
Damjan Marione5ef1d72017-03-02 12:33:48 +01001095
Chris Luke6edd3602018-06-12 22:45:06 -04001096 /* environ(7) does not indicate a header for this */
1097 extern char **environ;
1098
1099 /* Close all known open files */
1100 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001101 pool_foreach (f, fm->file_pool)
1102 {
Chris Luke6edd3602018-06-12 22:45:06 -04001103 if (f->file_descriptor > 2)
1104 close(f->file_descriptor);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001105 }
Chris Luke6edd3602018-06-12 22:45:06 -04001106 /* *INDENT-ON* */
1107
1108 /* Exec ourself */
Damjan Marionfd8deb42021-03-06 12:26:28 +01001109 execve (vgm->name, (char **) vm->argv, environ);
Damjan Marione5ef1d72017-03-02 12:33:48 +01001110
1111 return 0;
1112}
1113
1114/* *INDENT-OFF* */
1115VLIB_CLI_COMMAND (restart_cmd,static) = {
1116 .path = "restart",
1117 .short_help = "restart process",
1118 .function = restart_cmd_fn,
1119};
1120/* *INDENT-ON* */
1121
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001122#ifdef TEST_CODE
1123/*
1124 * A trivial test harness to verify the per-process output_function
1125 * is working correcty.
1126 */
1127
1128static clib_error_t *
1129sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001130 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001131{
1132 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001133 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001134
Dave Barach9b8ffd92016-07-08 08:13:45 -04001135 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001136
Dave Barach9b8ffd92016-07-08 08:13:45 -04001137 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001138 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001139 vlib_process_wait_for_event_or_clock (vm, 1.0);
1140 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001141 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001142 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001143 return 0;
1144}
1145
Dave Barach9b8ffd92016-07-08 08:13:45 -04001146/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001147VLIB_CLI_COMMAND (ping_command, static) = {
1148 .path = "test sleep",
1149 .function = sleep_ten_seconds,
1150 .short_help = "Sleep for 10 seconds",
1151};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001152/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001153#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154
Dave Barach9b8ffd92016-07-08 08:13:45 -04001155static uword
1156vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001158 char *i = input;
1159 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160 uword l = 0;
1161 uword index_of_last_space = ~0;
1162
1163 while (*i != 0)
1164 {
1165 u8 c = *i++;
1166 /* Multiple white space -> single space. */
1167 switch (c)
1168 {
1169 case ' ':
1170 case '\t':
1171 case '\n':
1172 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -04001173 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001174 {
1175 vec_add1 (s, ' ');
1176 l++;
1177 }
1178 break;
1179
1180 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001181 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001182 index_of_last_space = vec_len (s);
1183 vec_add1 (s, c);
1184 l++;
1185 break;
1186 }
1187 }
1188
1189 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001190 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191 _vec_len (s) -= 1;
1192
1193 *result = s;
1194 return index_of_last_space;
1195}
1196
1197always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001198parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199{
1200 word i;
1201 for (i = vec_len (path) - 1; i >= 0; i--)
1202 {
1203 if (path[i] == ' ')
1204 return i;
1205 }
1206 return ~0;
1207}
1208
Dave Barach9b8ffd92016-07-08 08:13:45 -04001209static void
1210add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001212 vlib_cli_command_t *p, *c;
1213 vlib_cli_sub_command_t *sub_c;
1214 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215 word i, l;
1216
1217 p = vec_elt_at_index (cm->commands, parent_index);
1218 c = vec_elt_at_index (cm->commands, child_index);
1219
1220 l = parent_path_len (c->path);
1221 if (l == ~0)
1222 sub_name = vec_dup ((u8 *) c->path);
1223 else
1224 {
1225 ASSERT (l + 1 < vec_len (c->path));
1226 sub_name = 0;
1227 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1228 }
1229
1230 if (sub_name[0] == '%')
1231 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001232 uword *q;
1233 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001234
1235 /* Remove %. */
1236 vec_delete (sub_name, 1, 0);
1237
Dave Barach9b8ffd92016-07-08 08:13:45 -04001238 if (!p->sub_rule_index_by_name)
1239 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1240 sizeof (sub_name[0]),
1241 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1243 if (q)
1244 {
1245 sr = vec_elt_at_index (p->sub_rules, q[0]);
1246 ASSERT (sr->command_index == child_index);
1247 return;
1248 }
1249
Dave Barach9b8ffd92016-07-08 08:13:45 -04001250 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1251 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252 vec_add2 (p->sub_rules, sr, 1);
1253 sr->name = sub_name;
Dave Barach5c944ee2020-01-07 12:29:10 -05001254 sr->rule_index = sr - p->sub_rules;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255 sr->command_index = child_index;
1256 return;
1257 }
1258
Dave Barach9b8ffd92016-07-08 08:13:45 -04001259 if (!p->sub_command_index_by_name)
1260 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1261 sizeof (c->path[0]),
1262 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263
1264 /* Check if sub-command has already been created. */
1265 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1266 {
1267 vec_free (sub_name);
1268 return;
1269 }
1270
1271 vec_add2 (p->sub_commands, sub_c, 1);
1272 sub_c->index = child_index;
1273 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001274 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1275 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276
1277 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1278 for (i = 0; i < vec_len (sub_c->name); i++)
1279 {
1280 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001281 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282
1283 pos = vec_elt_at_index (p->sub_command_positions, i);
1284
Dave Barach9b8ffd92016-07-08 08:13:45 -04001285 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286 pos->min_char = sub_c->name[i];
1287
1288 n = sub_c->name[i] - pos->min_char;
1289 if (n < 0)
1290 {
1291 pos->min_char = sub_c->name[i];
1292 vec_insert (pos->bitmaps, -n, 0);
1293 n = 0;
1294 }
1295
1296 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001297 pos->bitmaps[n] =
1298 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001299 }
1300}
1301
1302static void
1303vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1304{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001305 uword p_len, pi, *p;
1306 char *p_path;
1307 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308
1309 /* Root command (index 0) should have already been added. */
1310 ASSERT (vec_len (cm->commands) > 0);
1311
1312 c = vec_elt_at_index (cm->commands, ci);
1313 p_len = parent_path_len (c->path);
1314
Dave Barach9b8ffd92016-07-08 08:13:45 -04001315 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316 if (p_len == ~0)
1317 {
1318 add_sub_command (cm, 0, ci);
1319 return;
1320 }
1321
1322 p_path = 0;
1323 vec_add (p_path, c->path, p_len);
1324
1325 p = hash_get_mem (cm->command_index_by_path, p_path);
1326
1327 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001328 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329 {
1330 /* Parent does not exist; create it. */
1331 vec_add2 (cm->commands, parent, 1);
1332 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001333 hash_set_mem (cm->command_index_by_path, parent->path,
1334 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335 pi = parent - cm->commands;
1336 }
1337 else
1338 {
1339 pi = p[0];
1340 vec_free (p_path);
1341 }
1342
1343 add_sub_command (cm, pi, ci);
1344
1345 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001346 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001347 vlib_cli_make_parent (cm, pi);
1348}
1349
1350always_inline uword
1351vlib_cli_command_is_empty (vlib_cli_command_t * c)
1352{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001353 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354}
1355
Dave Barach9b8ffd92016-07-08 08:13:45 -04001356clib_error_t *
1357vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001359 vlib_global_main_t *vgm = vlib_get_global_main ();
1360 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001361 clib_error_t *error = 0;
1362 uword ci, *p;
1363 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001364
1365 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1366 return error;
1367
1368 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1369
Dave Barach9b8ffd92016-07-08 08:13:45 -04001370 if (!cm->command_index_by_path)
1371 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372 sizeof (c->path[0]),
1373 sizeof (uword));
1374
1375 /* See if command already exists with given path. */
1376 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1377 if (p)
1378 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001379 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001380
1381 ci = p[0];
1382 d = vec_elt_at_index (cm->commands, ci);
1383
1384 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001385 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001386 if (vlib_cli_command_is_empty (d))
1387 {
1388 vlib_cli_command_t save = d[0];
1389
Dave Barach9b8ffd92016-07-08 08:13:45 -04001390 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001391
1392 /* Copy callers fields. */
1393 d[0] = c[0];
1394
1395 /* Save internal fields. */
1396 d->path = save.path;
1397 d->sub_commands = save.sub_commands;
1398 d->sub_command_index_by_name = save.sub_command_index_by_name;
1399 d->sub_command_positions = save.sub_command_positions;
1400 d->sub_rules = save.sub_rules;
1401 }
1402 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001403 error =
1404 clib_error_return (0, "duplicate command name with path %v",
1405 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001406
1407 vec_free (normalized_path);
1408 if (error)
1409 return error;
1410 }
1411 else
1412 {
1413 /* Command does not exist: create it. */
1414
1415 /* Add root command (index 0). */
1416 if (vec_len (cm->commands) == 0)
1417 {
1418 /* Create command with index 0; path is empty string. */
1419 vec_resize (cm->commands, 1);
1420 }
1421
1422 ci = vec_len (cm->commands);
1423 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1424 vec_add1 (cm->commands, c[0]);
1425
1426 c = vec_elt_at_index (cm->commands, ci);
1427 c->path = normalized_path;
1428
1429 /* Don't inherit from registration. */
1430 c->sub_commands = 0;
1431 c->sub_command_index_by_name = 0;
1432 c->sub_command_positions = 0;
1433 }
1434
1435 vlib_cli_make_parent (cm, ci);
1436 return 0;
1437}
1438
Dave Barach6b3f25c2019-12-09 10:45:47 -05001439#if 0
1440/* $$$ turn back on again someday, maybe */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001441clib_error_t *
1442vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1443{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001444 vlib_cli_main_t *cm = &vm->cli_main;
1445 vlib_cli_parse_rule_t *r;
1446 clib_error_t *error = 0;
1447 u8 *r_name;
1448 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001449
Dave Barach9b8ffd92016-07-08 08:13:45 -04001450 if (!cm->parse_rule_index_by_name)
1451 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452 sizeof (r->name[0]),
1453 sizeof (uword));
1454
1455 /* Make vector copy of name. */
1456 r_name = format (0, "%s", r_reg->name);
1457
1458 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1459 {
1460 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001461 return clib_error_return (0, "duplicate parse rule name `%s'",
1462 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463 }
1464
1465 vec_add2 (cm->parse_rules, r, 1);
1466 r[0] = r_reg[0];
1467 r->name = (char *) r_name;
1468 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1469
1470 return error;
1471}
1472
Dave Barach9b8ffd92016-07-08 08:13:45 -04001473static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1474 vlib_cli_parse_rule_t *
1475 lo,
1476 vlib_cli_parse_rule_t *
1477 hi)
1478 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001479{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001480 clib_error_t *error = 0;
1481 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001482
1483 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1484 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001485 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001486 {
1487 error = clib_error_return (0, "parse rule with no name");
1488 goto done;
1489 }
1490
1491 error = vlib_cli_register_parse_rule (vm, r);
1492 if (error)
1493 goto done;
1494 }
1495
Dave Barach9b8ffd92016-07-08 08:13:45 -04001496done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001497 return error;
1498}
1499#endif
1500
Dave Barach9b8ffd92016-07-08 08:13:45 -04001501static clib_error_t *
Dave Barachaa676742020-11-25 07:23:42 -05001502event_logger_trace_command_fn (vlib_main_t * vm,
1503 unformat_input_t * input,
1504 vlib_cli_command_t * cmd)
Dave Barachc3a06552018-10-01 09:25:32 -04001505{
1506 unformat_input_t _line_input, *line_input = &_line_input;
1507 int enable = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001508 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1509 u32 circuit_node_index;
Dave Barachc3a06552018-10-01 09:25:32 -04001510
1511 if (!unformat_user (input, unformat_line_input, line_input))
1512 goto print_status;
1513
1514 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1515 {
1516 if (unformat (line_input, "api"))
1517 api = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001518 else if (unformat (line_input, "dispatch"))
1519 dispatch = 1;
1520 else if (unformat (line_input, "circuit-node %U",
1521 unformat_vlib_node, vm, &circuit_node_index))
1522 circuit = 1;
Dave Barachc3a06552018-10-01 09:25:32 -04001523 else if (unformat (line_input, "cli"))
1524 cli = 1;
1525 else if (unformat (line_input, "barrier"))
1526 barrier = 1;
1527 else if (unformat (line_input, "disable"))
1528 enable = 0;
1529 else if (unformat (line_input, "enable"))
1530 enable = 1;
1531 else
1532 break;
1533 }
1534 unformat_free (line_input);
1535
Dave Barachb09f4d02019-07-15 16:00:03 -04001536 vl_api_set_elog_trace_api_messages
1537 (api ? enable : vl_api_get_elog_trace_api_messages ());
Dave Barachc3a06552018-10-01 09:25:32 -04001538 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
Dave Barach900cbad2019-01-31 19:12:51 -05001539 vm->elog_trace_graph_dispatch = dispatch ?
1540 enable : vm->elog_trace_graph_dispatch;
1541 vm->elog_trace_graph_circuit = circuit ?
1542 enable : vm->elog_trace_graph_circuit;
Dave Barachc3a06552018-10-01 09:25:32 -04001543 vlib_worker_threads->barrier_elog_enabled =
1544 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
Dave Barach900cbad2019-01-31 19:12:51 -05001545 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1546
1547 /*
1548 * Set up start-of-buffer logic-analyzer trigger
1549 * for main loop event logs, which are fairly heavyweight.
1550 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1551 * will fully disable the scheme when the elog buffer fills.
1552 */
1553 if (dispatch || circuit)
1554 {
Damjan Marionf553a2c2021-03-26 13:45:37 +01001555 elog_main_t *em = &vlib_global_main.elog_main;
Dave Barach900cbad2019-01-31 19:12:51 -05001556
1557 em->n_total_events_disable_limit =
1558 em->n_total_events + vec_len (em->event_ring);
1559 }
1560
Dave Barachc3a06552018-10-01 09:25:32 -04001561
1562print_status:
1563 vlib_cli_output (vm, "Current status:");
1564
1565 vlib_cli_output
1566 (vm, " Event log API message trace: %s\n CLI command trace: %s",
Dave Barachb09f4d02019-07-15 16:00:03 -04001567 vl_api_get_elog_trace_api_messages ()? "on" : "off",
Dave Barachc3a06552018-10-01 09:25:32 -04001568 vm->elog_trace_cli_commands ? "on" : "off");
1569 vlib_cli_output
1570 (vm, " Barrier sync trace: %s",
1571 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
Dave Barach900cbad2019-01-31 19:12:51 -05001572 vlib_cli_output
1573 (vm, " Graph Dispatch: %s",
1574 vm->elog_trace_graph_dispatch ? "on" : "off");
1575 vlib_cli_output
1576 (vm, " Graph Circuit: %s",
1577 vm->elog_trace_graph_circuit ? "on" : "off");
1578 if (vm->elog_trace_graph_circuit)
1579 vlib_cli_output
1580 (vm, " node %U",
1581 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
Dave Barachc3a06552018-10-01 09:25:32 -04001582
1583 return 0;
1584}
1585
1586/*?
1587 * Control event logging of api, cli, and thread barrier events
1588 * With no arguments, displays the current trace status.
1589 * Name the event groups you wish to trace or stop tracing.
1590 *
1591 * @cliexpar
1592 * @clistart
Dave Barachaa676742020-11-25 07:23:42 -05001593 * event-logger trace api cli barrier
1594 * event-logger trace api cli barrier disable
1595 * event-logger trace dispatch
1596 * event-logger trace circuit-node ethernet-input
Dave Barachc3a06552018-10-01 09:25:32 -04001597 * @cliend
Dave Barachaa676742020-11-25 07:23:42 -05001598 * @cliexcmd{event-logger trace [api][cli][barrier][disable]}
Dave Barachc3a06552018-10-01 09:25:32 -04001599?*/
1600/* *INDENT-OFF* */
Dave Barachaa676742020-11-25 07:23:42 -05001601VLIB_CLI_COMMAND (event_logger_trace_command, static) =
Dave Barachc3a06552018-10-01 09:25:32 -04001602{
Dave Barachaa676742020-11-25 07:23:42 -05001603 .path = "event-logger trace",
1604 .short_help = "event-logger trace [api][cli][barrier][dispatch]\n"
Dave Barach900cbad2019-01-31 19:12:51 -05001605 "[circuit-node <name> e.g. ethernet-input][disable]",
Dave Barachaa676742020-11-25 07:23:42 -05001606 .function = event_logger_trace_command_fn,
Dave Barachc3a06552018-10-01 09:25:32 -04001607};
1608/* *INDENT-ON* */
1609
1610static clib_error_t *
Dave Barachc4abafd2019-09-04 12:09:32 -04001611suspend_command_fn (vlib_main_t * vm,
1612 unformat_input_t * input, vlib_cli_command_t * cmd)
1613{
1614 vlib_process_suspend (vm, 30e-3);
1615 return 0;
1616}
1617
1618/* *INDENT-OFF* */
1619VLIB_CLI_COMMAND (suspend_command, static) =
1620{
1621 .path = "suspend",
1622 .short_help = "suspend debug CLI for 30ms",
1623 .function = suspend_command_fn,
Dave Baracha1f5a952019-11-01 11:24:43 -04001624 .is_mp_safe = 1,
1625};
1626/* *INDENT-ON* */
1627
1628
1629static int
1630sort_cmds_by_path (void *a1, void *a2)
1631{
1632 u32 *index1 = a1;
1633 u32 *index2 = a2;
Damjan Marionfd8deb42021-03-06 12:26:28 +01001634 vlib_global_main_t *vgm = vlib_get_global_main ();
1635 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Baracha1f5a952019-11-01 11:24:43 -04001636 vlib_cli_command_t *c1, *c2;
1637 int i, lmin;
1638
1639 c1 = vec_elt_at_index (cm->commands, *index1);
1640 c2 = vec_elt_at_index (cm->commands, *index2);
1641
1642 lmin = vec_len (c1->path);
1643 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1644
1645 for (i = 0; i < lmin; i++)
1646 {
1647 if (c1->path[i] < c2->path[i])
1648 return -1;
1649 else if (c1->path[i] > c2->path[i])
1650 return 1;
1651 }
1652
1653 return 0;
1654}
1655
1656typedef struct
1657{
1658 vlib_cli_main_t *cm;
1659 u32 parent_command_index;
1660 int show_mp_safe;
1661 int show_not_mp_safe;
1662 int show_hit;
1663 int clear_hit;
1664} vlib_cli_walk_args_t;
1665
1666static void
1667cli_recursive_walk (vlib_cli_walk_args_t * aa)
1668{
1669 vlib_cli_command_t *parent;
1670 vlib_cli_sub_command_t *sub;
1671 vlib_cli_walk_args_t _a, *a = &_a;
1672 vlib_cli_main_t *cm;
1673 int i;
1674
1675 /* Copy args into this stack frame */
1676 *a = *aa;
1677 cm = a->cm;
1678
1679 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1680
1681 if (parent->function)
1682 {
1683 if (((a->show_mp_safe && parent->is_mp_safe)
1684 || (a->show_not_mp_safe && !parent->is_mp_safe))
1685 && (a->show_hit == 0 || parent->hit_counter))
1686 {
1687 vec_add1 (cm->sort_vector, a->parent_command_index);
1688 }
1689
1690 if (a->clear_hit)
1691 parent->hit_counter = 0;
1692 }
1693
1694 for (i = 0; i < vec_len (parent->sub_commands); i++)
1695 {
1696 sub = vec_elt_at_index (parent->sub_commands, i);
1697 a->parent_command_index = sub->index;
1698 cli_recursive_walk (a);
1699 }
1700}
1701
1702static u8 *
1703format_mp_safe (u8 * s, va_list * args)
1704{
1705 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1706 int show_mp_safe = va_arg (*args, int);
1707 int show_not_mp_safe = va_arg (*args, int);
1708 int show_hit = va_arg (*args, int);
1709 int clear_hit = va_arg (*args, int);
1710 vlib_cli_command_t *c;
1711 vlib_cli_walk_args_t _a, *a = &_a;
1712 int i;
1713 char *format_string = "\n%v";
1714
1715 if (show_hit)
1716 format_string = "\n%v: %u";
1717
1718 vec_reset_length (cm->sort_vector);
1719
1720 a->cm = cm;
1721 a->parent_command_index = 0;
1722 a->show_mp_safe = show_mp_safe;
1723 a->show_not_mp_safe = show_not_mp_safe;
1724 a->show_hit = show_hit;
1725 a->clear_hit = clear_hit;
1726
1727 cli_recursive_walk (a);
1728
1729 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1730
1731 for (i = 0; i < vec_len (cm->sort_vector); i++)
1732 {
1733 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1734 s = format (s, format_string, c->path, c->hit_counter);
1735 }
1736
1737 return s;
1738}
1739
1740
1741static clib_error_t *
1742show_cli_command_fn (vlib_main_t * vm,
1743 unformat_input_t * input, vlib_cli_command_t * cmd)
1744{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001745 vlib_global_main_t *vgm = vlib_get_global_main ();
Dave Baracha1f5a952019-11-01 11:24:43 -04001746 int show_mp_safe = 0;
1747 int show_not_mp_safe = 0;
1748 int show_hit = 0;
1749 int clear_hit = 0;
1750
1751 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1752 {
1753 if (unformat (input, "mp-safe"))
1754 show_mp_safe = 1;
1755 if (unformat (input, "not-mp-safe"))
1756 show_not_mp_safe = 1;
1757 else if (unformat (input, "hit"))
1758 show_hit = 1;
1759 else if (unformat (input, "clear-hit"))
1760 clear_hit = 1;
1761 else
1762 break;
1763 }
1764
1765 /* default set: all cli commands */
1766 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1767 show_mp_safe = show_not_mp_safe = 1;
1768
Damjan Marionfd8deb42021-03-06 12:26:28 +01001769 vlib_cli_output (vm, "%U", format_mp_safe, &vgm->cli_main, show_mp_safe,
1770 show_not_mp_safe, show_hit, clear_hit);
Dave Baracha1f5a952019-11-01 11:24:43 -04001771 if (clear_hit)
1772 vlib_cli_output (vm, "hit counters cleared...");
1773
1774 return 0;
1775}
1776
1777/*?
1778 * Displays debug cli command information
1779 *
1780 * @cliexpar
1781 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1782 *
1783 * "show cli" displays the entire debug cli:
1784 *
1785 * abf attach
1786 * abf policy
1787 * adjacency counters
1788 * api trace
1789 * app ns
1790 * bfd key del
1791 * ... and so on ...
1792 *
1793 * "show cli mp-safe" displays mp-safe debug CLI commands:
1794 *
1795 * abf policy
1796 * binary-api
1797 * create vhost-user
1798 * exec
1799 * ip container
1800 * ip mroute
1801 * ip probe-neighbor
1802 * ip route
1803 * ip scan-neighbor
1804 * ip table
1805 * ip6 table
1806 *
1807 * "show cli not-mp-safe" displays debug CLI commands
1808 * which cause worker thread barrier synchronization
1809 *
1810 * "show cli hit" displays commands which have been executed. Qualify
1811 * as desired with "mp-safe" or "not-mp-safe".
1812 *
1813 * "show cli clear-hit" clears the per-command hit counters.
1814 * @cliexend
1815?*/
1816
1817/* *INDENT-OFF* */
1818VLIB_CLI_COMMAND (show_cli_command, static) =
1819{
1820 .path = "show cli",
1821 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1822 .function = show_cli_command_fn,
1823 .is_mp_safe = 1,
Dave Barachc4abafd2019-09-04 12:09:32 -04001824};
1825/* *INDENT-ON* */
1826
1827static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -04001828vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001829{
Damjan Marionfd8deb42021-03-06 12:26:28 +01001830 vlib_global_main_t *vgm = vlib_get_global_main ();
1831 vlib_cli_main_t *cm = &vgm->cli_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001832 clib_error_t *error = 0;
1833 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001834
1835 cmd = cm->cli_command_registrations;
1836
1837 while (cmd)
1838 {
1839 error = vlib_cli_register (vm, cmd);
1840 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001841 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001842 cmd = cmd->next_cli_command;
1843 }
Dave Barach3d0e0d12021-02-17 10:25:18 -05001844
1845 cm->log = vlib_log_register_class_rate_limit (
1846 "cli", "log", 0x7FFFFFFF /* aka no rate limit */);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001847 return error;
1848}
1849
1850VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001851
1852/*
1853 * fd.io coding-style-patch-verification: ON
1854 *
1855 * Local Variables:
1856 * eval: (c-set-style "gnu")
1857 * End:
1858 */