blob: 0ecdac386f381e66772db949cd96ba38a0120101 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * cli.c: command line interface
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#include <vlib/vlib.h>
Chris Luke6edd3602018-06-12 22:45:06 -040041#include <vlib/unix/unix.h>
Damjan Marioneccf5092016-11-18 16:59:24 +010042#include <vppinfra/cpu.h>
Dave Barachc3a06552018-10-01 09:25:32 -040043#include <vppinfra/elog.h>
Damjan Marione5ef1d72017-03-02 12:33:48 +010044#include <unistd.h>
Yoann Desmouceaux3060e072017-05-18 11:00:48 +020045#include <ctype.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
Dave Baracha1f5a952019-11-01 11:24:43 -040047/** \file src/vlib/cli.c Debug CLI Implementation
48 */
49
Dave Barachb09f4d02019-07-15 16:00:03 -040050int vl_api_set_elog_trace_api_messages (int enable);
51int vl_api_get_elog_trace_api_messages (void);
52
Dave Barachd67a4282019-06-15 12:46:13 -040053static void *current_traced_heap;
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055/* Root of all show commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040056/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070057VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
58 .path = "show",
59 .short_help = "Show commands",
60};
Dave Barach9b8ffd92016-07-08 08:13:45 -040061/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
63/* Root of all clear commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040064/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070065VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
66 .path = "clear",
67 .short_help = "Clear commands",
68};
Dave Barach9b8ffd92016-07-08 08:13:45 -040069/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71/* Root of all set commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040072/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070073VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
74 .path = "set",
75 .short_help = "Set commands",
76};
Dave Barach9b8ffd92016-07-08 08:13:45 -040077/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79/* Root of all test commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040080/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070081VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
82 .path = "test",
83 .short_help = "Test commands",
84};
Dave Barach9b8ffd92016-07-08 08:13:45 -040085/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87/* Returns bitmap of commands which match key. */
88static uword *
89vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
90{
91 int i, n;
Dave Barach9b8ffd92016-07-08 08:13:45 -040092 uword *match = 0;
93 vlib_cli_parse_position_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
95 unformat_skip_white_space (input);
96
Dave Barach9b8ffd92016-07-08 08:13:45 -040097 for (i = 0;; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 {
99 uword k;
100
101 k = unformat_get_input (input);
102 switch (k)
103 {
104 case 'a' ... 'z':
105 case 'A' ... 'Z':
106 case '0' ... '9':
Dave Barach9b8ffd92016-07-08 08:13:45 -0400107 case '-':
108 case '_':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 break;
110
Dave Barach9b8ffd92016-07-08 08:13:45 -0400111 case ' ':
112 case '\t':
113 case '\r':
114 case '\n':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 case UNFORMAT_END_OF_INPUT:
116 /* White space or end of input removes any non-white
117 matches that were before possible. */
118 if (i < vec_len (c->sub_command_positions)
119 && clib_bitmap_count_set_bits (match) > 1)
120 {
121 p = vec_elt_at_index (c->sub_command_positions, i);
122 for (n = 0; n < vec_len (p->bitmaps); n++)
123 match = clib_bitmap_andnot (match, p->bitmaps[n]);
124 }
125 goto done;
126
127 default:
128 unformat_put_input (input);
129 goto done;
130 }
131
132 if (i >= vec_len (c->sub_command_positions))
133 {
134 no_match:
135 clib_bitmap_free (match);
136 return 0;
137 }
138
139 p = vec_elt_at_index (c->sub_command_positions, i);
140 if (vec_len (p->bitmaps) == 0)
141 goto no_match;
142
143 n = k - p->min_char;
144 if (n < 0 || n >= vec_len (p->bitmaps))
145 goto no_match;
146
147 if (i == 0)
148 match = clib_bitmap_dup (p->bitmaps[n]);
149 else
150 match = clib_bitmap_and (match, p->bitmaps[n]);
151
152 if (clib_bitmap_is_zero (match))
153 goto no_match;
154 }
155
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 return match;
158}
159
160/* Looks for string based sub-input formatted { SUB-INPUT }. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161uword
162unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400164 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
165 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 uword c;
167
168 while (1)
169 {
170 c = unformat_get_input (i);
171 switch (c)
172 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173 case ' ':
174 case '\t':
175 case '\n':
176 case '\r':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 case '\f':
178 break;
179
180 case '{':
181 default:
182 /* Put back paren. */
183 if (c != UNFORMAT_END_OF_INPUT)
184 unformat_put_input (i);
185
186 if (c == '{' && unformat (i, "%v", &s))
187 {
188 unformat_init_vector (sub_input, s);
189 return 1;
190 }
191 return 0;
192 }
193 }
194 return 0;
195}
196
197static vlib_cli_command_t *
198get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
199{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 return vec_elt_at_index (cm->commands, s->index);
202}
203
Dave Barach9b8ffd92016-07-08 08:13:45 -0400204static uword
205unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
208 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
209 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
210 vlib_cli_main_t *cm = &vm->cli_main;
211 uword *match_bitmap, is_unique, index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 match_bitmap = vlib_cli_sub_command_match (c, i);
214 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
215 index = ~0;
216 if (is_unique)
217 {
218 index = clib_bitmap_first_set (match_bitmap);
219 *result = get_sub_command (cm, c, index);
220 }
221 clib_bitmap_free (match_bitmap);
222
223 return is_unique;
224}
225
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200226static int
227vlib_cli_cmp_strings (void *a1, void *a2)
228{
229 u8 *c1 = *(u8 **) a1;
230 u8 *c2 = *(u8 **) a2;
231
232 return vec_cmp (c1, c2);
233}
234
235u8 **
236vlib_cli_get_possible_completions (u8 * str)
237{
238 vlib_cli_command_t *c;
239 vlib_cli_sub_command_t *sc;
240 vlib_main_t *vm = vlib_get_main ();
241 vlib_cli_main_t *vcm = &vm->cli_main;
242 uword *match_bitmap = 0;
243 uword index, is_unique, help_next_level;
244 u8 **result = 0;
245 unformat_input_t input;
246 unformat_init_vector (&input, vec_dup (str));
247 c = vec_elt_at_index (vcm->commands, 0);
248
249 /* remove trailing whitespace, except for one of them */
250 while (vec_len (input.buffer) >= 2 &&
251 isspace (input.buffer[vec_len (input.buffer) - 1]) &&
252 isspace (input.buffer[vec_len (input.buffer) - 2]))
253 {
254 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
255 }
256
257 /* if input is empty, directly return list of root commands */
258 if (vec_len (input.buffer) == 0 ||
259 (vec_len (input.buffer) == 1 && isspace (input.buffer[0])))
260 {
261 vec_foreach (sc, c->sub_commands)
262 {
263 vec_add1 (result, (u8 *) sc->name);
264 }
265 goto done;
266 }
267
268 /* add a trailing '?' so that vlib_cli_sub_command_match can find
269 * all commands starting with the input string */
270 vec_add1 (input.buffer, '?');
271
272 while (1)
273 {
274 match_bitmap = vlib_cli_sub_command_match (c, &input);
275 /* no match: return no result */
276 if (match_bitmap == 0)
277 {
278 goto done;
279 }
280 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
281 /* unique match: try to step one subcommand level further */
282 if (is_unique)
283 {
284 /* stop if no more input */
285 if (input.index >= vec_len (input.buffer) - 1)
286 {
287 break;
288 }
289
290 index = clib_bitmap_first_set (match_bitmap);
291 c = get_sub_command (vcm, c, index);
292 clib_bitmap_free (match_bitmap);
293 continue;
294 }
295 /* multiple matches: stop here, return all matches */
296 break;
297 }
298
299 /* remove trailing '?' */
300 vec_del1 (input.buffer, vec_len (input.buffer) - 1);
301
302 /* if we have a space at the end of input, and a unique match,
303 * autocomplete the next level of subcommands */
304 help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]);
305 /* *INDENT-OFF* */
306 clib_bitmap_foreach(index, match_bitmap, {
307 if (help_next_level && is_unique) {
308 c = get_sub_command (vcm, c, index);
309 vec_foreach (sc, c->sub_commands) {
310 vec_add1 (result, (u8*) sc->name);
311 }
312 goto done; /* break doesn't work in this macro-loop */
313 }
314 sc = &c->sub_commands[index];
315 vec_add1(result, (u8*) sc->name);
316 });
317 /* *INDENT-ON* */
318
319done:
320 clib_bitmap_free (match_bitmap);
321 unformat_free (&input);
322
Yoann Desmouceaux4227eef2017-05-24 15:51:48 +0200323 if (result)
324 vec_sort_with_function (result, vlib_cli_cmp_strings);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +0200325 return result;
326}
327
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328static u8 *
329format_vlib_cli_command_help (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400331 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 int is_long = va_arg (*args, int);
333 if (is_long && c->long_help)
334 s = format (s, "%s", c->long_help);
335 else if (c->short_help)
336 s = format (s, "%s", c->short_help);
337 else
338 s = format (s, "%v commands", c->path);
339 return s;
340}
341
Dave Barach9b8ffd92016-07-08 08:13:45 -0400342static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343format_vlib_cli_path (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400345 u8 *path = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Dave Barach6b3f25c2019-12-09 10:45:47 -0500347 s = format (s, "%v", path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
349 return s;
350}
351
352static vlib_cli_command_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400353all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
356 vlib_cli_sub_command_t *sc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
358 if (c->function)
359 vec_add1 (subs, c[0]);
360
Dave Barach9b8ffd92016-07-08 08:13:45 -0400361 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362
363 return subs;
364}
365
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500366static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367vlib_cli_cmp_rule (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500368{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400369 vlib_cli_sub_rule_t *r1 = a1;
370 vlib_cli_sub_rule_t *r2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500371
372 return vec_cmp (r1->name, r2->name);
373}
374
375static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400376vlib_cli_cmp_command (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500377{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400378 vlib_cli_command_t *c1 = a1;
379 vlib_cli_command_t *c2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500380
381 return vec_cmp (c1->path, c2->path);
382}
383
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384static clib_error_t *
385vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
386 vlib_cli_main_t * cm,
387 unformat_input_t * input,
388 uword parent_command_index)
389{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 vlib_cli_command_t *parent, *c;
391 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 u8 *string;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 uword is_main_dispatch = cm == &vm->cli_main;
Dave Barach6b3f25c2019-12-09 10:45:47 -0500395 uword value;
396 u8 *key;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397
398 parent = vec_elt_at_index (cm->commands, parent_command_index);
399 if (is_main_dispatch && unformat (input, "help"))
400 {
401 uword help_at_end_of_line, i;
402
Dave Barach9b8ffd92016-07-08 08:13:45 -0400403 help_at_end_of_line =
404 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 while (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 c = parent;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400408 if (unformat_user
409 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 ;
411
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 goto unknown;
414
415 else
416 break;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400417 }
418
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 /* help SUB-COMMAND => long format help.
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420 "help" at end of line: show all commands. */
421 if (!help_at_end_of_line)
422 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
423 /* is_long */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Dave Barach6b3f25c2019-12-09 10:45:47 -0500425 else if (vec_len (c->sub_commands) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426 vlib_cli_output (vm, "%v: no sub-commands", c->path);
427
428 else
429 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500430 vlib_cli_sub_rule_t *sr, *subs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Dave Barach6b3f25c2019-12-09 10:45:47 -0500432 /* *INDENT-OFF* */
433 hash_foreach_mem (key, value, c->sub_command_index_by_name,
434 ({
435 (void) key;
436 vec_add2 (subs, sr, 1);
437 sr->name = c->sub_commands[value].name;
438 sr->command_index = value;
439 sr->rule_index = ~0;
440 }));
441 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500443 vec_sort_with_function (subs, vlib_cli_cmp_rule);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
Dave Barach9b8ffd92016-07-08 08:13:45 -0400445 for (i = 0; i < vec_len (subs); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
449 d = vec_elt_at_index (cm->commands, subs[i].command_index);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500450 vlib_cli_output
451 (vm, " %-30v %U", subs[i].name,
452 format_vlib_cli_command_help, d, /* is_long */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 }
454
455 vec_free (subs);
456 }
457 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400458
459 else if (is_main_dispatch
460 && (unformat (input, "choices") || unformat (input, "?")))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400462 vlib_cli_command_t *sub, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
464 subs = all_subs (cm, 0, parent_command_index);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500465 vec_sort_with_function (subs, vlib_cli_cmp_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 vec_foreach (sub, subs)
467 vlib_cli_output (vm, " %-40U %U",
468 format_vlib_cli_path, sub->path,
469 format_vlib_cli_command_help, sub, /* is_long */ 0);
470 vec_free (subs);
471 }
472
473 else if (unformat (input, "comment %v", &string))
474 {
475 vec_free (string);
476 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 else if (unformat (input, "uncomment %U",
479 unformat_vlib_cli_sub_input, &sub_input))
480 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400481 error =
482 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
483 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 unformat_free (&sub_input);
485 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400486 else if (unformat (input, "leak-check %U",
487 unformat_vlib_cli_sub_input, &sub_input))
488 {
489 u8 *leak_report;
Dave Barachd67a4282019-06-15 12:46:13 -0400490 if (current_traced_heap)
491 {
492 void *oldheap;
493 oldheap = clib_mem_set_heap (current_traced_heap);
494 clib_mem_trace (0);
495 clib_mem_set_heap (oldheap);
496 current_traced_heap = 0;
497 }
Dave Barach8fdde3c2019-05-17 10:46:40 -0400498 clib_mem_trace (1);
499 error =
500 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
501 parent_command_index);
502 unformat_free (&sub_input);
503
504 /* Otherwise, the clib_error_t shows up as a leak... */
505 if (error)
506 {
507 vlib_cli_output (vm, "%v", error->what);
508 clib_error_free (error);
509 error = 0;
510 }
511
512 (void) clib_mem_trace_enable_disable (0);
513 leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
514 1 /* verbose, i.e. print leaks */ );
515 clib_mem_trace (0);
516 vlib_cli_output (vm, "%v", leak_report);
517 vec_free (leak_report);
518 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400519
520 else
521 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400523 unformat_input_t *si;
524 uword has_sub_commands =
525 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
526
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 si = input;
528 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
529 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 if (has_sub_commands)
532 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
533
Dave Barach9b8ffd92016-07-08 08:13:45 -0400534 if (has_sub_commands && !error)
535 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
537 else if (c->function)
538 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540
541 /* Skip white space for benefit of called function. */
542 unformat_skip_white_space (si);
543
544 if (unformat (si, "?"))
545 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400546 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
547 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 }
549 else
550 {
Dave Barachc3a06552018-10-01 09:25:32 -0400551 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
552 {
553 /* *INDENT-OFF* */
554 ELOG_TYPE_DECLARE (e) =
555 {
556 .format = "cli-cmd: %s",
557 .format_args = "T4",
558 };
559 /* *INDENT-ON* */
560 struct
561 {
562 u32 c;
563 } *ed;
564 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachb09f4d02019-07-15 16:00:03 -0400565 ed->c = elog_string (&vm->elog_main, c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400566 }
567
Dave Barach9b8ffd92016-07-08 08:13:45 -0400568 if (!c->is_mp_safe)
569 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Dave Baracha1f5a952019-11-01 11:24:43 -0400571 c->hit_counter++;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 c_error = c->function (vm, si, c);
573
Dave Barach9b8ffd92016-07-08 08:13:45 -0400574 if (!c->is_mp_safe)
575 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
Dave Barachc3a06552018-10-01 09:25:32 -0400577 if (PREDICT_FALSE (vm->elog_trace_cli_commands))
578 {
579 /* *INDENT-OFF* */
580 ELOG_TYPE_DECLARE (e) =
581 {
582 .format = "cli-cmd: %s %s",
583 .format_args = "T4T4",
584 };
585 /* *INDENT-ON* */
586 struct
587 {
588 u32 c, err;
589 } *ed;
590 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachb09f4d02019-07-15 16:00:03 -0400591 ed->c = elog_string (&vm->elog_main, c->path);
Dave Barachc3a06552018-10-01 09:25:32 -0400592 if (c_error)
593 {
594 vec_add1 (c_error->what, 0);
Dave Barach6b3f25c2019-12-09 10:45:47 -0500595 ed->err =
596 elog_string (&vm->elog_main, (char *) c_error->what);
Dave Barachc3a06552018-10-01 09:25:32 -0400597 _vec_len (c_error->what) -= 1;
598 }
599 else
Dave Barachb09f4d02019-07-15 16:00:03 -0400600 ed->err = elog_string (&vm->elog_main, "OK");
Dave Barachc3a06552018-10-01 09:25:32 -0400601 }
602
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603 if (c_error)
604 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400605 error =
606 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607 clib_error_free (c_error);
608 /* Free sub input. */
609 if (si != input)
610 unformat_free (si);
611
612 return error;
613 }
614 }
615
616 /* Free any previous error. */
617 clib_error_free (error);
618 }
619
Dave Barach9b8ffd92016-07-08 08:13:45 -0400620 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 error = clib_error_return (0, "%v: no sub-commands", c->path);
622
623 /* Free sub input. */
624 if (si != input)
625 unformat_free (si);
626 }
627
628 else
629 goto unknown;
630
631 return error;
632
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
636 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
639 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640}
641
642
Dave Barach9b8ffd92016-07-08 08:13:45 -0400643void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
644 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645
Dave Barach9b8ffd92016-07-08 08:13:45 -0400646void
647vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
648{
649}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650
651/* Process CLI input. */
Ole Troan72d87582019-05-10 12:01:10 +0200652int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400653vlib_cli_input (vlib_main_t * vm,
654 unformat_input_t * input,
655 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 vlib_process_t *cp = vlib_get_current_process (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400658 clib_error_t *error;
659 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660 uword save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200661 int rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000663 save_function = cp->output_function;
664 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000666 cp->output_function = function;
667 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
Dave Barach9b8ffd92016-07-08 08:13:45 -0400669 do
670 {
Dave Barach6b3f25c2019-12-09 10:45:47 -0500671 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
672 /* parent */ 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400673 }
674 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675
676 if (error)
677 {
678 vlib_cli_output (vm, "%v", error->what);
679 vlib_unix_error_report (vm, error);
Ole Troan72d87582019-05-10 12:01:10 +0200680 /* clib_error_return is unfortunately often called with a '0'
681 return code */
682 rv = error->code != 0 ? error->code : -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 clib_error_free (error);
684 }
685
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000686 cp->output_function = save_function;
687 cp->output_function_arg = save_function_arg;
Ole Troan72d87582019-05-10 12:01:10 +0200688 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689}
690
691/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400692void
693vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400697 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
699 va_start (va, fmt);
700 s = va_format (0, fmt, &va);
701 va_end (va);
702
703 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400704 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705 vec_add1 (s, '\n');
706
Dave Barach9b8ffd92016-07-08 08:13:45 -0400707 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708 fformat (stdout, "%v", s);
709 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000710 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
712 vec_free (s);
713}
714
Ole Troan73710c72018-06-04 22:27:49 +0200715void *vl_msg_push_heap (void) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400716void *
717vl_msg_push_heap (void)
718{
719 return 0;
720}
721
Ole Troan73710c72018-06-04 22:27:49 +0200722void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak));
Dave Barachd67a4282019-06-15 12:46:13 -0400723void
724vl_msg_pop_heap (void *oldheap)
725{
726}
727
728void *vlib_stats_push_heap (void *) __attribute__ ((weak));
729void *
730vlib_stats_push_heap (void *notused)
731{
732 return 0;
733}
Ole Troan73710c72018-06-04 22:27:49 +0200734
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735static clib_error_t *
736show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400737 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738{
Dave Barachd67a4282019-06-15 12:46:13 -0400739 int verbose __attribute__ ((unused)) = 0;
740 int api_segment = 0, stats_segment = 0, main_heap = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400741 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742 u32 index = 0;
Dave Barachd67a4282019-06-15 12:46:13 -0400743 uword clib_mem_trace_enable_disable (uword enable);
744 uword was_enabled;
745
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746
Dave Barach9b8ffd92016-07-08 08:13:45 -0400747 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400749 if (unformat (input, "verbose"))
750 verbose = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200751 else if (unformat (input, "api-segment"))
752 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400753 else if (unformat (input, "stats-segment"))
754 stats_segment = 1;
755 else if (unformat (input, "main-heap"))
756 main_heap = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757 else
758 {
759 error = clib_error_return (0, "unknown input `%U'",
760 format_unformat_error, input);
761 return error;
762 }
763 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
Dave Barachd67a4282019-06-15 12:46:13 -0400765 if ((api_segment + stats_segment + main_heap) == 0)
766 return clib_error_return
767 (0, "Please supply one of api-segment, stats-segment or main-heap");
768
Ole Troan73710c72018-06-04 22:27:49 +0200769 if (api_segment)
770 {
771 void *oldheap = vl_msg_push_heap ();
Dave Barachd67a4282019-06-15 12:46:13 -0400772 was_enabled = clib_mem_trace_enable_disable (0);
Ole Troan73710c72018-06-04 22:27:49 +0200773 u8 *s_in_svm =
774 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
775 vl_msg_pop_heap (oldheap);
776 u8 *s = vec_dup (s_in_svm);
777
778 oldheap = vl_msg_push_heap ();
779 vec_free (s_in_svm);
Dave Barachd67a4282019-06-15 12:46:13 -0400780 clib_mem_trace_enable_disable (was_enabled);
Ole Troan73710c72018-06-04 22:27:49 +0200781 vl_msg_pop_heap (oldheap);
Dave Barachd67a4282019-06-15 12:46:13 -0400782 vlib_cli_output (vm, "API segment");
Ole Troan73710c72018-06-04 22:27:49 +0200783 vlib_cli_output (vm, "%v", s);
Dave Barachd67a4282019-06-15 12:46:13 -0400784 vec_free (s);
785 }
786 if (stats_segment)
787 {
788 void *oldheap = vlib_stats_push_heap (0);
789 was_enabled = clib_mem_trace_enable_disable (0);
790 u8 *s_in_svm =
791 format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1);
792 if (oldheap)
793 clib_mem_set_heap (oldheap);
794 u8 *s = vec_dup (s_in_svm);
795
796 oldheap = vlib_stats_push_heap (0);
797 vec_free (s_in_svm);
798 if (oldheap)
799 {
800 clib_mem_trace_enable_disable (was_enabled);
801 clib_mem_set_heap (oldheap);
802 }
803 vlib_cli_output (vm, "Stats segment");
804 vlib_cli_output (vm, "%v", s);
Ole Troan73710c72018-06-04 22:27:49 +0200805 vec_free (s);
806 }
807
Dave Barach6a5adc32018-07-04 10:56:23 -0400808#if USE_DLMALLOC == 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400809 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810 foreach_vlib_main (
811 ({
Damjan Marion926b5642018-07-02 21:33:31 +0200812 mheap_t *h = mheap_header (clib_per_cpu_mheaps[index]);
Dave Barach6a5adc32018-07-04 10:56:23 -0400813 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
Damjan Marion926b5642018-07-02 21:33:31 +0200814 vlib_worker_threads[index].name);
815 vlib_cli_output (vm, " %U\n", format_page_map, pointer_to_uword (h) -
816 h->vm_alloc_offset_from_header,
817 h->vm_alloc_size);
Dave Barach6a5adc32018-07-04 10:56:23 -0400818 vlib_cli_output (vm, " %U\n", format_mheap, clib_per_cpu_mheaps[index],
819 verbose);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820 index++;
821 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400822 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400823#else
824 {
Dave Barachd67a4282019-06-15 12:46:13 -0400825 if (main_heap)
826 {
827 /*
828 * Note: the foreach_vlib_main causes allocator traffic,
829 * so shut off tracing before we go there...
830 */
831 was_enabled = clib_mem_trace_enable_disable (0);
Dave Barach6a5adc32018-07-04 10:56:23 -0400832
Dave Barachd67a4282019-06-15 12:46:13 -0400833 /* *INDENT-OFF* */
834 foreach_vlib_main (
835 ({
836 struct dlmallinfo mi;
837 void *mspace;
838 mspace = clib_per_cpu_mheaps[index];
Dave Barach6a5adc32018-07-04 10:56:23 -0400839
Dave Barachd67a4282019-06-15 12:46:13 -0400840 mi = mspace_mallinfo (mspace);
841 vlib_cli_output (vm, "%sThread %d %s\n", index ? "\n":"", index,
842 vlib_worker_threads[index].name);
843 vlib_cli_output (vm, " %U\n", format_page_map,
844 pointer_to_uword (mspace_least_addr(mspace)),
845 mi.arena);
846 vlib_cli_output (vm, " %U\n", format_mheap,
847 clib_per_cpu_mheaps[index],
848 verbose);
849 index++;
850 }));
851 /* *INDENT-ON* */
Dave Barach6a5adc32018-07-04 10:56:23 -0400852
Dave Barachd67a4282019-06-15 12:46:13 -0400853 /* Restore the trace flag */
854 clib_mem_trace_enable_disable (was_enabled);
855 }
Dave Barach6a5adc32018-07-04 10:56:23 -0400856 }
857#endif /* USE_DLMALLOC */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858 return 0;
859}
860
Dave Barach9b8ffd92016-07-08 08:13:45 -0400861/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
863 .path = "show memory",
Dave Barachd67a4282019-06-15 12:46:13 -0400864 .short_help = "show memory [api-segment][stats-segment][verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865 .function = show_memory_usage,
866};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400867/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868
869static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100870show_cpu (vlib_main_t * vm, unformat_input_t * input,
871 vlib_cli_command_t * cmd)
872{
873#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
874 _("Model name", "%U", format_cpu_model_name);
Paul Vinciguerrad6897c12018-12-30 11:07:36 -0800875 _("Microarch model (family)", "%U", format_cpu_uarch);
Damjan Marioneccf5092016-11-18 16:59:24 +0100876 _("Flags", "%U", format_cpu_flags);
877 _("Base frequency", "%.2f GHz",
878 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
879#undef _
880 return 0;
881}
882
883/*?
884 * Displays various information about the CPU.
885 *
886 * @cliexpar
887 * @cliexstart{show cpu}
888 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
889 * Microarchitecture: Broadwell (Broadwell-EP/EX)
890 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
891 * Base Frequency: 3.20 GHz
892 * @cliexend
893?*/
894/* *INDENT-OFF* */
895VLIB_CLI_COMMAND (show_cpu_command, static) = {
896 .path = "show cpu",
897 .short_help = "Show cpu information",
898 .function = show_cpu,
899};
Damjan Marioneccf5092016-11-18 16:59:24 +0100900/* *INDENT-ON* */
Ole Troan73710c72018-06-04 22:27:49 +0200901
Damjan Marioneccf5092016-11-18 16:59:24 +0100902static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903enable_disable_memory_trace (vlib_main_t * vm,
904 unformat_input_t * input,
905 vlib_cli_command_t * cmd)
906{
Ole Troan73710c72018-06-04 22:27:49 +0200907 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachd67a4282019-06-15 12:46:13 -0400908 int enable = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200909 int api_segment = 0;
Dave Barachd67a4282019-06-15 12:46:13 -0400910 int stats_segment = 0;
911 int main_heap = 0;
Ole Troan73710c72018-06-04 22:27:49 +0200912 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913
Ole Troan73710c72018-06-04 22:27:49 +0200914 if (!unformat_user (input, unformat_line_input, line_input))
915 return 0;
916
917 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918 {
Dave Barach1ddbc012018-06-13 09:26:05 -0400919 if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable))
Ole Troan73710c72018-06-04 22:27:49 +0200920 ;
921 else if (unformat (line_input, "api-segment"))
922 api_segment = 1;
Dave Barachd67a4282019-06-15 12:46:13 -0400923 else if (unformat (line_input, "stats-segment"))
924 stats_segment = 1;
925 else if (unformat (line_input, "main-heap"))
926 main_heap = 1;
Ole Troan73710c72018-06-04 22:27:49 +0200927 else
928 {
929 unformat_free (line_input);
930 return clib_error_return (0, "invalid input");
931 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 }
Ole Troan73710c72018-06-04 22:27:49 +0200933 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934
Dave Barachd67a4282019-06-15 12:46:13 -0400935 if ((api_segment + stats_segment + main_heap + (enable == 0)) == 0)
936 {
937 return clib_error_return
938 (0, "Need one of main-heap, stats-segment or api-segment");
939 }
940
941 /* Turn off current trace, if any */
942 if (current_traced_heap)
943 {
944 void *oldheap;
945 oldheap = clib_mem_set_heap (current_traced_heap);
946 clib_mem_trace (0);
947 clib_mem_set_heap (oldheap);
948 current_traced_heap = 0;
949 }
950
951 if (enable == 0)
952 return 0;
953
954 /* API segment */
Ole Troan73710c72018-06-04 22:27:49 +0200955 if (api_segment)
Dave Barachd67a4282019-06-15 12:46:13 -0400956 {
957 oldheap = vl_msg_push_heap ();
958 current_traced_heap = clib_mem_get_heap ();
959 clib_mem_trace (1);
960 vl_msg_pop_heap (oldheap);
961
962 }
963
964 /* Stats segment */
965 if (stats_segment)
966 {
967 oldheap = vlib_stats_push_heap (0);
968 current_traced_heap = clib_mem_get_heap ();
969 clib_mem_trace (stats_segment);
970 /* We don't want to call vlib_stats_pop_heap... */
971 if (oldheap)
972 clib_mem_set_heap (oldheap);
973 }
974
975 /* main_heap */
976 if (main_heap)
977 {
978 current_traced_heap = clib_mem_get_heap ();
979 clib_mem_trace (main_heap);
980 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981
Ole Troan73710c72018-06-04 22:27:49 +0200982 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983}
984
Dave Barach9b8ffd92016-07-08 08:13:45 -0400985/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
987 .path = "memory-trace",
Dave Barachd67a4282019-06-15 12:46:13 -0400988 .short_help = "memory-trace on|off [api-segment][stats-segment][main-heap]\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989 .function = enable_disable_memory_trace,
990};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400991/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992
993
994static clib_error_t *
995test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400996 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997{
Dave Barach6a5adc32018-07-04 10:56:23 -0400998#if USE_DLMALLOC == 0
Dave Barach9b8ffd92016-07-08 08:13:45 -0400999 clib_error_t *error = 0;
1000 void *heap;
1001 mheap_t *mheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002
Dave Barach9b8ffd92016-07-08 08:13:45 -04001003 if (unformat (input, "on"))
1004 {
1005 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +02001007 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 mheap = mheap_header(heap);
1009 mheap->flags |= MHEAP_FLAG_VALIDATE;
1010 // Turn off small object cache because it delays detection of errors
1011 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
1012 });
Dave Barach9b8ffd92016-07-08 08:13:45 -04001013 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014
Dave Barach9b8ffd92016-07-08 08:13:45 -04001015 }
1016 else if (unformat (input, "off"))
1017 {
1018 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +02001020 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 mheap = mheap_header(heap);
1022 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
1023 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
1024 });
Dave Barach9b8ffd92016-07-08 08:13:45 -04001025 /* *INDENT-ON* */
1026 }
1027 else if (unformat (input, "now"))
1028 {
1029 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030 foreach_vlib_main({
Damjan Marion586afd72017-04-05 19:18:20 +02001031 heap = clib_per_cpu_mheaps[this_vlib_main->thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032 mheap = mheap_header(heap);
1033 mheap_validate(heap);
1034 });
Dave Barach9b8ffd92016-07-08 08:13:45 -04001035 /* *INDENT-ON* */
1036 vlib_cli_output (vm, "heap validation complete");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037
Dave Barach9b8ffd92016-07-08 08:13:45 -04001038 }
1039 else
1040 {
1041 return clib_error_return (0, "unknown input `%U'",
1042 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043 }
1044
Dave Barach9b8ffd92016-07-08 08:13:45 -04001045 return error;
Dave Barach6a5adc32018-07-04 10:56:23 -04001046#else
1047 return clib_error_return (0, "unimplemented...");
1048#endif /* USE_DLMALLOC */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049}
1050
Dave Barach9b8ffd92016-07-08 08:13:45 -04001051/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
1053 .path = "test heap-validate",
1054 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
1055 .function = test_heap_validate,
1056};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001057/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
Damjan Marione5ef1d72017-03-02 12:33:48 +01001059static clib_error_t *
1060restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input,
1061 vlib_cli_command_t * cmd)
1062{
Chris Luke6edd3602018-06-12 22:45:06 -04001063 clib_file_main_t *fm = &file_main;
1064 clib_file_t *f;
Damjan Marione5ef1d72017-03-02 12:33:48 +01001065
Chris Luke6edd3602018-06-12 22:45:06 -04001066 /* environ(7) does not indicate a header for this */
1067 extern char **environ;
1068
1069 /* Close all known open files */
1070 /* *INDENT-OFF* */
1071 pool_foreach(f, fm->file_pool,
1072 ({
1073 if (f->file_descriptor > 2)
1074 close(f->file_descriptor);
1075 }));
1076 /* *INDENT-ON* */
1077
1078 /* Exec ourself */
1079 execve (vm->name, (char **) vm->argv, environ);
Damjan Marione5ef1d72017-03-02 12:33:48 +01001080
1081 return 0;
1082}
1083
1084/* *INDENT-OFF* */
1085VLIB_CLI_COMMAND (restart_cmd,static) = {
1086 .path = "restart",
1087 .short_help = "restart process",
1088 .function = restart_cmd_fn,
1089};
1090/* *INDENT-ON* */
1091
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001092#ifdef TEST_CODE
1093/*
1094 * A trivial test harness to verify the per-process output_function
1095 * is working correcty.
1096 */
1097
1098static clib_error_t *
1099sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001100 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001101{
1102 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001103 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001104
Dave Barach9b8ffd92016-07-08 08:13:45 -04001105 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001106
Dave Barach9b8ffd92016-07-08 08:13:45 -04001107 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001108 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001109 vlib_process_wait_for_event_or_clock (vm, 1.0);
1110 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001111 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001112 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001113 return 0;
1114}
1115
Dave Barach9b8ffd92016-07-08 08:13:45 -04001116/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001117VLIB_CLI_COMMAND (ping_command, static) = {
1118 .path = "test sleep",
1119 .function = sleep_ten_seconds,
1120 .short_help = "Sleep for 10 seconds",
1121};
Dave Barach9b8ffd92016-07-08 08:13:45 -04001122/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00001123#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124
Dave Barach9b8ffd92016-07-08 08:13:45 -04001125static uword
1126vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001128 char *i = input;
1129 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130 uword l = 0;
1131 uword index_of_last_space = ~0;
1132
1133 while (*i != 0)
1134 {
1135 u8 c = *i++;
1136 /* Multiple white space -> single space. */
1137 switch (c)
1138 {
1139 case ' ':
1140 case '\t':
1141 case '\n':
1142 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -04001143 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 {
1145 vec_add1 (s, ' ');
1146 l++;
1147 }
1148 break;
1149
1150 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001151 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152 index_of_last_space = vec_len (s);
1153 vec_add1 (s, c);
1154 l++;
1155 break;
1156 }
1157 }
1158
1159 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001160 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161 _vec_len (s) -= 1;
1162
1163 *result = s;
1164 return index_of_last_space;
1165}
1166
1167always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001168parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169{
1170 word i;
1171 for (i = vec_len (path) - 1; i >= 0; i--)
1172 {
1173 if (path[i] == ' ')
1174 return i;
1175 }
1176 return ~0;
1177}
1178
Dave Barach9b8ffd92016-07-08 08:13:45 -04001179static void
1180add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001182 vlib_cli_command_t *p, *c;
1183 vlib_cli_sub_command_t *sub_c;
1184 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185 word i, l;
1186
1187 p = vec_elt_at_index (cm->commands, parent_index);
1188 c = vec_elt_at_index (cm->commands, child_index);
1189
1190 l = parent_path_len (c->path);
1191 if (l == ~0)
1192 sub_name = vec_dup ((u8 *) c->path);
1193 else
1194 {
1195 ASSERT (l + 1 < vec_len (c->path));
1196 sub_name = 0;
1197 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
1198 }
1199
1200 if (sub_name[0] == '%')
1201 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001202 uword *q;
1203 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204
1205 /* Remove %. */
1206 vec_delete (sub_name, 1, 0);
1207
Dave Barach9b8ffd92016-07-08 08:13:45 -04001208 if (!p->sub_rule_index_by_name)
1209 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
1210 sizeof (sub_name[0]),
1211 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
1213 if (q)
1214 {
1215 sr = vec_elt_at_index (p->sub_rules, q[0]);
1216 ASSERT (sr->command_index == child_index);
1217 return;
1218 }
1219
Dave Barach9b8ffd92016-07-08 08:13:45 -04001220 hash_set_mem (p->sub_rule_index_by_name, sub_name,
1221 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222 vec_add2 (p->sub_rules, sr, 1);
1223 sr->name = sub_name;
1224 sr->rule_index = q[0];
1225 sr->command_index = child_index;
1226 return;
1227 }
1228
Dave Barach9b8ffd92016-07-08 08:13:45 -04001229 if (!p->sub_command_index_by_name)
1230 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
1231 sizeof (c->path[0]),
1232 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233
1234 /* Check if sub-command has already been created. */
1235 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
1236 {
1237 vec_free (sub_name);
1238 return;
1239 }
1240
1241 vec_add2 (p->sub_commands, sub_c, 1);
1242 sub_c->index = child_index;
1243 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001244 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
1245 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246
1247 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
1248 for (i = 0; i < vec_len (sub_c->name); i++)
1249 {
1250 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001251 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252
1253 pos = vec_elt_at_index (p->sub_command_positions, i);
1254
Dave Barach9b8ffd92016-07-08 08:13:45 -04001255 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256 pos->min_char = sub_c->name[i];
1257
1258 n = sub_c->name[i] - pos->min_char;
1259 if (n < 0)
1260 {
1261 pos->min_char = sub_c->name[i];
1262 vec_insert (pos->bitmaps, -n, 0);
1263 n = 0;
1264 }
1265
1266 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001267 pos->bitmaps[n] =
1268 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269 }
1270}
1271
1272static void
1273vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
1274{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001275 uword p_len, pi, *p;
1276 char *p_path;
1277 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278
1279 /* Root command (index 0) should have already been added. */
1280 ASSERT (vec_len (cm->commands) > 0);
1281
1282 c = vec_elt_at_index (cm->commands, ci);
1283 p_len = parent_path_len (c->path);
1284
Dave Barach9b8ffd92016-07-08 08:13:45 -04001285 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286 if (p_len == ~0)
1287 {
1288 add_sub_command (cm, 0, ci);
1289 return;
1290 }
1291
1292 p_path = 0;
1293 vec_add (p_path, c->path, p_len);
1294
1295 p = hash_get_mem (cm->command_index_by_path, p_path);
1296
1297 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001298 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001299 {
1300 /* Parent does not exist; create it. */
1301 vec_add2 (cm->commands, parent, 1);
1302 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001303 hash_set_mem (cm->command_index_by_path, parent->path,
1304 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305 pi = parent - cm->commands;
1306 }
1307 else
1308 {
1309 pi = p[0];
1310 vec_free (p_path);
1311 }
1312
1313 add_sub_command (cm, pi, ci);
1314
1315 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001316 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001317 vlib_cli_make_parent (cm, pi);
1318}
1319
1320always_inline uword
1321vlib_cli_command_is_empty (vlib_cli_command_t * c)
1322{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001323 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001324}
1325
Dave Barach9b8ffd92016-07-08 08:13:45 -04001326clib_error_t *
1327vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001329 vlib_cli_main_t *cm = &vm->cli_main;
1330 clib_error_t *error = 0;
1331 uword ci, *p;
1332 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333
1334 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1335 return error;
1336
1337 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1338
Dave Barach9b8ffd92016-07-08 08:13:45 -04001339 if (!cm->command_index_by_path)
1340 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001341 sizeof (c->path[0]),
1342 sizeof (uword));
1343
1344 /* See if command already exists with given path. */
1345 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1346 if (p)
1347 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001348 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349
1350 ci = p[0];
1351 d = vec_elt_at_index (cm->commands, ci);
1352
1353 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001354 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355 if (vlib_cli_command_is_empty (d))
1356 {
1357 vlib_cli_command_t save = d[0];
1358
Dave Barach9b8ffd92016-07-08 08:13:45 -04001359 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001360
1361 /* Copy callers fields. */
1362 d[0] = c[0];
1363
1364 /* Save internal fields. */
1365 d->path = save.path;
1366 d->sub_commands = save.sub_commands;
1367 d->sub_command_index_by_name = save.sub_command_index_by_name;
1368 d->sub_command_positions = save.sub_command_positions;
1369 d->sub_rules = save.sub_rules;
1370 }
1371 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001372 error =
1373 clib_error_return (0, "duplicate command name with path %v",
1374 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375
1376 vec_free (normalized_path);
1377 if (error)
1378 return error;
1379 }
1380 else
1381 {
1382 /* Command does not exist: create it. */
1383
1384 /* Add root command (index 0). */
1385 if (vec_len (cm->commands) == 0)
1386 {
1387 /* Create command with index 0; path is empty string. */
1388 vec_resize (cm->commands, 1);
1389 }
1390
1391 ci = vec_len (cm->commands);
1392 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1393 vec_add1 (cm->commands, c[0]);
1394
1395 c = vec_elt_at_index (cm->commands, ci);
1396 c->path = normalized_path;
1397
1398 /* Don't inherit from registration. */
1399 c->sub_commands = 0;
1400 c->sub_command_index_by_name = 0;
1401 c->sub_command_positions = 0;
1402 }
1403
1404 vlib_cli_make_parent (cm, ci);
1405 return 0;
1406}
1407
Dave Barach6b3f25c2019-12-09 10:45:47 -05001408#if 0
1409/* $$$ turn back on again someday, maybe */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410clib_error_t *
1411vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1412{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001413 vlib_cli_main_t *cm = &vm->cli_main;
1414 vlib_cli_parse_rule_t *r;
1415 clib_error_t *error = 0;
1416 u8 *r_name;
1417 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001418
Dave Barach9b8ffd92016-07-08 08:13:45 -04001419 if (!cm->parse_rule_index_by_name)
1420 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001421 sizeof (r->name[0]),
1422 sizeof (uword));
1423
1424 /* Make vector copy of name. */
1425 r_name = format (0, "%s", r_reg->name);
1426
1427 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1428 {
1429 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001430 return clib_error_return (0, "duplicate parse rule name `%s'",
1431 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432 }
1433
1434 vec_add2 (cm->parse_rules, r, 1);
1435 r[0] = r_reg[0];
1436 r->name = (char *) r_name;
1437 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1438
1439 return error;
1440}
1441
Dave Barach9b8ffd92016-07-08 08:13:45 -04001442static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1443 vlib_cli_parse_rule_t *
1444 lo,
1445 vlib_cli_parse_rule_t *
1446 hi)
1447 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001449 clib_error_t *error = 0;
1450 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001451
1452 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1453 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001454 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455 {
1456 error = clib_error_return (0, "parse rule with no name");
1457 goto done;
1458 }
1459
1460 error = vlib_cli_register_parse_rule (vm, r);
1461 if (error)
1462 goto done;
1463 }
1464
Dave Barach9b8ffd92016-07-08 08:13:45 -04001465done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001466 return error;
1467}
1468#endif
1469
Dave Barach9b8ffd92016-07-08 08:13:45 -04001470static clib_error_t *
Dave Barachc3a06552018-10-01 09:25:32 -04001471elog_trace_command_fn (vlib_main_t * vm,
1472 unformat_input_t * input, vlib_cli_command_t * cmd)
1473{
1474 unformat_input_t _line_input, *line_input = &_line_input;
1475 int enable = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001476 int api = 0, cli = 0, barrier = 0, dispatch = 0, circuit = 0;
1477 u32 circuit_node_index;
Dave Barachc3a06552018-10-01 09:25:32 -04001478
1479 if (!unformat_user (input, unformat_line_input, line_input))
1480 goto print_status;
1481
1482 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1483 {
1484 if (unformat (line_input, "api"))
1485 api = 1;
Dave Barach900cbad2019-01-31 19:12:51 -05001486 else if (unformat (line_input, "dispatch"))
1487 dispatch = 1;
1488 else if (unformat (line_input, "circuit-node %U",
1489 unformat_vlib_node, vm, &circuit_node_index))
1490 circuit = 1;
Dave Barachc3a06552018-10-01 09:25:32 -04001491 else if (unformat (line_input, "cli"))
1492 cli = 1;
1493 else if (unformat (line_input, "barrier"))
1494 barrier = 1;
1495 else if (unformat (line_input, "disable"))
1496 enable = 0;
1497 else if (unformat (line_input, "enable"))
1498 enable = 1;
1499 else
1500 break;
1501 }
1502 unformat_free (line_input);
1503
Dave Barachb09f4d02019-07-15 16:00:03 -04001504 vl_api_set_elog_trace_api_messages
1505 (api ? enable : vl_api_get_elog_trace_api_messages ());
Dave Barachc3a06552018-10-01 09:25:32 -04001506 vm->elog_trace_cli_commands = cli ? enable : vm->elog_trace_cli_commands;
Dave Barach900cbad2019-01-31 19:12:51 -05001507 vm->elog_trace_graph_dispatch = dispatch ?
1508 enable : vm->elog_trace_graph_dispatch;
1509 vm->elog_trace_graph_circuit = circuit ?
1510 enable : vm->elog_trace_graph_circuit;
Dave Barachc3a06552018-10-01 09:25:32 -04001511 vlib_worker_threads->barrier_elog_enabled =
1512 barrier ? enable : vlib_worker_threads->barrier_elog_enabled;
Dave Barach900cbad2019-01-31 19:12:51 -05001513 vm->elog_trace_graph_circuit_node_index = circuit_node_index;
1514
1515 /*
1516 * Set up start-of-buffer logic-analyzer trigger
1517 * for main loop event logs, which are fairly heavyweight.
1518 * See src/vlib/main/vlib_elog_main_loop_event(...), which
1519 * will fully disable the scheme when the elog buffer fills.
1520 */
1521 if (dispatch || circuit)
1522 {
1523 elog_main_t *em = &vm->elog_main;
1524
1525 em->n_total_events_disable_limit =
1526 em->n_total_events + vec_len (em->event_ring);
1527 }
1528
Dave Barachc3a06552018-10-01 09:25:32 -04001529
1530print_status:
1531 vlib_cli_output (vm, "Current status:");
1532
1533 vlib_cli_output
1534 (vm, " Event log API message trace: %s\n CLI command trace: %s",
Dave Barachb09f4d02019-07-15 16:00:03 -04001535 vl_api_get_elog_trace_api_messages ()? "on" : "off",
Dave Barachc3a06552018-10-01 09:25:32 -04001536 vm->elog_trace_cli_commands ? "on" : "off");
1537 vlib_cli_output
1538 (vm, " Barrier sync trace: %s",
1539 vlib_worker_threads->barrier_elog_enabled ? "on" : "off");
Dave Barach900cbad2019-01-31 19:12:51 -05001540 vlib_cli_output
1541 (vm, " Graph Dispatch: %s",
1542 vm->elog_trace_graph_dispatch ? "on" : "off");
1543 vlib_cli_output
1544 (vm, " Graph Circuit: %s",
1545 vm->elog_trace_graph_circuit ? "on" : "off");
1546 if (vm->elog_trace_graph_circuit)
1547 vlib_cli_output
1548 (vm, " node %U",
1549 format_vlib_node_name, vm, vm->elog_trace_graph_circuit_node_index);
Dave Barachc3a06552018-10-01 09:25:32 -04001550
1551 return 0;
1552}
1553
1554/*?
1555 * Control event logging of api, cli, and thread barrier events
1556 * With no arguments, displays the current trace status.
1557 * Name the event groups you wish to trace or stop tracing.
1558 *
1559 * @cliexpar
1560 * @clistart
1561 * elog trace api cli barrier
1562 * elog trace api cli barrier disable
Dave Barach900cbad2019-01-31 19:12:51 -05001563 * elog trace dispatch
1564 * elog trace circuit-node ethernet-input
Dave Barachc3a06552018-10-01 09:25:32 -04001565 * elog trace
1566 * @cliend
1567 * @cliexcmd{elog trace [api][cli][barrier][disable]}
1568?*/
1569/* *INDENT-OFF* */
1570VLIB_CLI_COMMAND (elog_trace_command, static) =
1571{
1572 .path = "elog trace",
Dave Barach900cbad2019-01-31 19:12:51 -05001573 .short_help = "elog trace [api][cli][barrier][dispatch]\n"
1574 "[circuit-node <name> e.g. ethernet-input][disable]",
Dave Barachc3a06552018-10-01 09:25:32 -04001575 .function = elog_trace_command_fn,
1576};
1577/* *INDENT-ON* */
1578
1579static clib_error_t *
Dave Barachc4abafd2019-09-04 12:09:32 -04001580suspend_command_fn (vlib_main_t * vm,
1581 unformat_input_t * input, vlib_cli_command_t * cmd)
1582{
1583 vlib_process_suspend (vm, 30e-3);
1584 return 0;
1585}
1586
1587/* *INDENT-OFF* */
1588VLIB_CLI_COMMAND (suspend_command, static) =
1589{
1590 .path = "suspend",
1591 .short_help = "suspend debug CLI for 30ms",
1592 .function = suspend_command_fn,
Dave Baracha1f5a952019-11-01 11:24:43 -04001593 .is_mp_safe = 1,
1594};
1595/* *INDENT-ON* */
1596
1597
1598static int
1599sort_cmds_by_path (void *a1, void *a2)
1600{
1601 u32 *index1 = a1;
1602 u32 *index2 = a2;
1603 vlib_main_t *vm = vlib_get_main ();
1604 vlib_cli_main_t *cm = &vm->cli_main;
1605 vlib_cli_command_t *c1, *c2;
1606 int i, lmin;
1607
1608 c1 = vec_elt_at_index (cm->commands, *index1);
1609 c2 = vec_elt_at_index (cm->commands, *index2);
1610
1611 lmin = vec_len (c1->path);
1612 lmin = (vec_len (c2->path) >= lmin) ? lmin : vec_len (c2->path);
1613
1614 for (i = 0; i < lmin; i++)
1615 {
1616 if (c1->path[i] < c2->path[i])
1617 return -1;
1618 else if (c1->path[i] > c2->path[i])
1619 return 1;
1620 }
1621
1622 return 0;
1623}
1624
1625typedef struct
1626{
1627 vlib_cli_main_t *cm;
1628 u32 parent_command_index;
1629 int show_mp_safe;
1630 int show_not_mp_safe;
1631 int show_hit;
1632 int clear_hit;
1633} vlib_cli_walk_args_t;
1634
1635static void
1636cli_recursive_walk (vlib_cli_walk_args_t * aa)
1637{
1638 vlib_cli_command_t *parent;
1639 vlib_cli_sub_command_t *sub;
1640 vlib_cli_walk_args_t _a, *a = &_a;
1641 vlib_cli_main_t *cm;
1642 int i;
1643
1644 /* Copy args into this stack frame */
1645 *a = *aa;
1646 cm = a->cm;
1647
1648 parent = vec_elt_at_index (cm->commands, a->parent_command_index);
1649
1650 if (parent->function)
1651 {
1652 if (((a->show_mp_safe && parent->is_mp_safe)
1653 || (a->show_not_mp_safe && !parent->is_mp_safe))
1654 && (a->show_hit == 0 || parent->hit_counter))
1655 {
1656 vec_add1 (cm->sort_vector, a->parent_command_index);
1657 }
1658
1659 if (a->clear_hit)
1660 parent->hit_counter = 0;
1661 }
1662
1663 for (i = 0; i < vec_len (parent->sub_commands); i++)
1664 {
1665 sub = vec_elt_at_index (parent->sub_commands, i);
1666 a->parent_command_index = sub->index;
1667 cli_recursive_walk (a);
1668 }
1669}
1670
1671static u8 *
1672format_mp_safe (u8 * s, va_list * args)
1673{
1674 vlib_cli_main_t *cm = va_arg (*args, vlib_cli_main_t *);
1675 int show_mp_safe = va_arg (*args, int);
1676 int show_not_mp_safe = va_arg (*args, int);
1677 int show_hit = va_arg (*args, int);
1678 int clear_hit = va_arg (*args, int);
1679 vlib_cli_command_t *c;
1680 vlib_cli_walk_args_t _a, *a = &_a;
1681 int i;
1682 char *format_string = "\n%v";
1683
1684 if (show_hit)
1685 format_string = "\n%v: %u";
1686
1687 vec_reset_length (cm->sort_vector);
1688
1689 a->cm = cm;
1690 a->parent_command_index = 0;
1691 a->show_mp_safe = show_mp_safe;
1692 a->show_not_mp_safe = show_not_mp_safe;
1693 a->show_hit = show_hit;
1694 a->clear_hit = clear_hit;
1695
1696 cli_recursive_walk (a);
1697
1698 vec_sort_with_function (cm->sort_vector, sort_cmds_by_path);
1699
1700 for (i = 0; i < vec_len (cm->sort_vector); i++)
1701 {
1702 c = vec_elt_at_index (cm->commands, cm->sort_vector[i]);
1703 s = format (s, format_string, c->path, c->hit_counter);
1704 }
1705
1706 return s;
1707}
1708
1709
1710static clib_error_t *
1711show_cli_command_fn (vlib_main_t * vm,
1712 unformat_input_t * input, vlib_cli_command_t * cmd)
1713{
1714 int show_mp_safe = 0;
1715 int show_not_mp_safe = 0;
1716 int show_hit = 0;
1717 int clear_hit = 0;
1718
1719 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1720 {
1721 if (unformat (input, "mp-safe"))
1722 show_mp_safe = 1;
1723 if (unformat (input, "not-mp-safe"))
1724 show_not_mp_safe = 1;
1725 else if (unformat (input, "hit"))
1726 show_hit = 1;
1727 else if (unformat (input, "clear-hit"))
1728 clear_hit = 1;
1729 else
1730 break;
1731 }
1732
1733 /* default set: all cli commands */
1734 if (clear_hit == 0 && (show_mp_safe + show_not_mp_safe) == 0)
1735 show_mp_safe = show_not_mp_safe = 1;
1736
1737 vlib_cli_output (vm, "%U", format_mp_safe, &vm->cli_main,
1738 show_mp_safe, show_not_mp_safe, show_hit, clear_hit);
1739 if (clear_hit)
1740 vlib_cli_output (vm, "hit counters cleared...");
1741
1742 return 0;
1743}
1744
1745/*?
1746 * Displays debug cli command information
1747 *
1748 * @cliexpar
1749 * @cliexstart{show cli [mp-safe][not-mp-safe][hit][clear-hit]}
1750 *
1751 * "show cli" displays the entire debug cli:
1752 *
1753 * abf attach
1754 * abf policy
1755 * adjacency counters
1756 * api trace
1757 * app ns
1758 * bfd key del
1759 * ... and so on ...
1760 *
1761 * "show cli mp-safe" displays mp-safe debug CLI commands:
1762 *
1763 * abf policy
1764 * binary-api
1765 * create vhost-user
1766 * exec
1767 * ip container
1768 * ip mroute
1769 * ip probe-neighbor
1770 * ip route
1771 * ip scan-neighbor
1772 * ip table
1773 * ip6 table
1774 *
1775 * "show cli not-mp-safe" displays debug CLI commands
1776 * which cause worker thread barrier synchronization
1777 *
1778 * "show cli hit" displays commands which have been executed. Qualify
1779 * as desired with "mp-safe" or "not-mp-safe".
1780 *
1781 * "show cli clear-hit" clears the per-command hit counters.
1782 * @cliexend
1783?*/
1784
1785/* *INDENT-OFF* */
1786VLIB_CLI_COMMAND (show_cli_command, static) =
1787{
1788 .path = "show cli",
1789 .short_help = "show cli [mp-safe][not-mp-safe][hit][clear-hit]",
1790 .function = show_cli_command_fn,
1791 .is_mp_safe = 1,
Dave Barachc4abafd2019-09-04 12:09:32 -04001792};
1793/* *INDENT-ON* */
1794
1795static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -04001796vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001797{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001798 vlib_cli_main_t *cm = &vm->cli_main;
1799 clib_error_t *error = 0;
1800 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001801
1802 cmd = cm->cli_command_registrations;
1803
1804 while (cmd)
1805 {
1806 error = vlib_cli_register (vm, cmd);
1807 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001808 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001809 cmd = cmd->next_cli_command;
1810 }
1811 return error;
1812}
1813
1814VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001815
1816/*
1817 * fd.io coding-style-patch-verification: ON
1818 *
1819 * Local Variables:
1820 * eval: (c-set-style "gnu")
1821 * End:
1822 */